AvaloniaNativePlatformOpenGlInterface.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using Avalonia.OpenGL;
  3. using Avalonia.Native.Interop;
  4. using System.Drawing;
  5. using Avalonia.OpenGL.Surfaces;
  6. using Avalonia.Threading;
  7. namespace Avalonia.Native
  8. {
  9. class AvaloniaNativePlatformOpenGlInterface : IPlatformOpenGlInterface
  10. {
  11. private readonly IAvnGlDisplay _display;
  12. public AvaloniaNativePlatformOpenGlInterface(IAvnGlDisplay display)
  13. {
  14. _display = display;
  15. var immediate = display.CreateContext(null);
  16. int major, minor;
  17. GlInterface glInterface;
  18. using (immediate.MakeCurrent())
  19. {
  20. var basic = new GlBasicInfoInterface(display.GetProcAddress);
  21. basic.GetIntegerv(GlConsts.GL_MAJOR_VERSION, out major);
  22. basic.GetIntegerv(GlConsts.GL_MINOR_VERSION, out minor);
  23. _version = new GlVersion(GlProfileType.OpenGL, major, minor);
  24. glInterface = new GlInterface(_version, (name) =>
  25. {
  26. var rv = _display.GetProcAddress(name);
  27. return rv;
  28. });
  29. }
  30. GlDisplay = new GlDisplay(display, glInterface, immediate.SampleCount, immediate.StencilSize);
  31. MainContext = new GlContext(GlDisplay, null, immediate, _version);
  32. }
  33. internal GlContext MainContext { get; }
  34. public IGlContext PrimaryContext => MainContext;
  35. public bool CanShareContexts => true;
  36. public bool CanCreateContexts => true;
  37. internal GlDisplay GlDisplay;
  38. private readonly GlVersion _version;
  39. public IGlContext CreateSharedContext() => new GlContext(GlDisplay,
  40. MainContext, _display.CreateContext(MainContext.Context), _version);
  41. public IGlContext CreateContext() => new GlContext(GlDisplay,
  42. null, _display.CreateContext(null), _version);
  43. }
  44. class GlDisplay
  45. {
  46. private readonly IAvnGlDisplay _display;
  47. public GlDisplay(IAvnGlDisplay display, GlInterface glInterface, int sampleCount, int stencilSize)
  48. {
  49. _display = display;
  50. SampleCount = sampleCount;
  51. StencilSize = stencilSize;
  52. GlInterface = glInterface;
  53. }
  54. public GlInterface GlInterface { get; }
  55. public int SampleCount { get; }
  56. public int StencilSize { get; }
  57. public void ClearContext() => _display.LegacyClearCurrentContext();
  58. }
  59. class GlContext : IGlContext
  60. {
  61. private readonly GlDisplay _display;
  62. private readonly GlContext _sharedWith;
  63. public IAvnGlContext Context { get; private set; }
  64. public GlContext(GlDisplay display, GlContext sharedWith, IAvnGlContext context, GlVersion version)
  65. {
  66. _display = display;
  67. _sharedWith = sharedWith;
  68. Context = context;
  69. Version = version;
  70. }
  71. public GlVersion Version { get; }
  72. public GlInterface GlInterface => _display.GlInterface;
  73. public int SampleCount => _display.SampleCount;
  74. public int StencilSize => _display.StencilSize;
  75. public IDisposable MakeCurrent() => Context.MakeCurrent();
  76. public IDisposable EnsureCurrent() => MakeCurrent();
  77. public bool IsSharedWith(IGlContext context)
  78. {
  79. var c = (GlContext)context;
  80. return c == this
  81. || c._sharedWith == this
  82. || _sharedWith == context
  83. || _sharedWith != null && _sharedWith == c._sharedWith;
  84. }
  85. public void Dispose()
  86. {
  87. Context.Dispose();
  88. Context = null;
  89. }
  90. }
  91. class GlPlatformSurfaceRenderTarget : IGlPlatformSurfaceRenderTarget
  92. {
  93. private IAvnGlSurfaceRenderTarget _target;
  94. private readonly IGlContext _context;
  95. public GlPlatformSurfaceRenderTarget(IAvnGlSurfaceRenderTarget target, IGlContext context)
  96. {
  97. _target = target;
  98. _context = context;
  99. }
  100. public IGlPlatformSurfaceRenderingSession BeginDraw()
  101. {
  102. var feature = (AvaloniaNativePlatformOpenGlInterface)AvaloniaLocator.Current.GetService<IPlatformOpenGlInterface>();
  103. return new GlPlatformSurfaceRenderingSession(_context, _target.BeginDrawing());
  104. }
  105. public void Dispose()
  106. {
  107. _target?.Dispose();
  108. _target = null;
  109. }
  110. }
  111. class GlPlatformSurfaceRenderingSession : IGlPlatformSurfaceRenderingSession
  112. {
  113. private IAvnGlSurfaceRenderingSession _session;
  114. public GlPlatformSurfaceRenderingSession(IGlContext context, IAvnGlSurfaceRenderingSession session)
  115. {
  116. Context = context;
  117. _session = session;
  118. }
  119. public IGlContext Context { get; }
  120. public PixelSize Size
  121. {
  122. get
  123. {
  124. var s = _session.GetPixelSize();
  125. return new PixelSize(s.Width, s.Height);
  126. }
  127. }
  128. public double Scaling => _session.GetScaling();
  129. public bool IsYFlipped => true;
  130. public void Dispose()
  131. {
  132. _session?.Dispose();
  133. _session = null;
  134. }
  135. }
  136. class GlPlatformSurface : IGlPlatformSurface
  137. {
  138. private readonly IAvnWindowBase _window;
  139. private readonly IGlContext _context;
  140. public GlPlatformSurface(IAvnWindowBase window, IGlContext context)
  141. {
  142. _window = window;
  143. _context = context;
  144. }
  145. public IGlPlatformSurfaceRenderTarget CreateGlRenderTarget()
  146. {
  147. return new GlPlatformSurfaceRenderTarget(_window.CreateGlRenderTarget(), _context);
  148. }
  149. }
  150. }