OpenGLRendererControl.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Avalonia;
  2. using Avalonia.OpenGL;
  3. using Avalonia.Platform;
  4. using Avalonia.Rendering.SceneGraph;
  5. using Avalonia.Skia;
  6. using Avalonia.Threading;
  7. using OpenTK.Graphics.OpenGL;
  8. using Ryujinx.Common.Configuration;
  9. using SkiaSharp;
  10. using SPB.Graphics;
  11. using SPB.Graphics.OpenGL;
  12. using SPB.Platform;
  13. using SPB.Windowing;
  14. using System;
  15. namespace Ryujinx.Ava.Ui.Controls
  16. {
  17. internal class OpenGLRendererControl : RendererControl
  18. {
  19. public int Major { get; }
  20. public int Minor { get; }
  21. public OpenGLContextBase GameContext { get; set; }
  22. public static OpenGLContextBase PrimaryContext =>
  23. AvaloniaLocator.Current.GetService<IPlatformOpenGlInterface>()
  24. .PrimaryContext.AsOpenGLContextBase();
  25. private SwappableNativeWindowBase _gameBackgroundWindow;
  26. private IntPtr _fence;
  27. public OpenGLRendererControl(int major, int minor, GraphicsDebugLevel graphicsDebugLevel) : base(graphicsDebugLevel)
  28. {
  29. Major = major;
  30. Minor = minor;
  31. }
  32. public override void DestroyBackgroundContext()
  33. {
  34. Image = null;
  35. if (_fence != IntPtr.Zero)
  36. {
  37. DrawOperation.Dispose();
  38. GL.DeleteSync(_fence);
  39. }
  40. GlDrawOperation.DeleteFramebuffer();
  41. GameContext?.Dispose();
  42. _gameBackgroundWindow?.Dispose();
  43. }
  44. internal override void Present(object image)
  45. {
  46. Dispatcher.UIThread.InvokeAsync(() =>
  47. {
  48. Image = (int)image;
  49. InvalidateVisual();
  50. }).Wait();
  51. if (_fence != IntPtr.Zero)
  52. {
  53. GL.DeleteSync(_fence);
  54. }
  55. _fence = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None);
  56. InvalidateVisual();
  57. _gameBackgroundWindow.SwapBuffers();
  58. }
  59. internal override void MakeCurrent()
  60. {
  61. GameContext.MakeCurrent(_gameBackgroundWindow);
  62. }
  63. internal override void MakeCurrent(SwappableNativeWindowBase window)
  64. {
  65. GameContext.MakeCurrent(window);
  66. }
  67. protected override void CreateWindow()
  68. {
  69. var flags = OpenGLContextFlags.Compat;
  70. if (DebugLevel != GraphicsDebugLevel.None)
  71. {
  72. flags |= OpenGLContextFlags.Debug;
  73. }
  74. _gameBackgroundWindow = PlatformHelper.CreateOpenGLWindow(FramebufferFormat.Default, 0, 0, 100, 100);
  75. _gameBackgroundWindow.Hide();
  76. GameContext = PlatformHelper.CreateOpenGLContext(FramebufferFormat.Default, Major, Minor, flags, shareContext: PrimaryContext);
  77. GameContext.Initialize(_gameBackgroundWindow);
  78. }
  79. protected override ICustomDrawOperation CreateDrawOperation()
  80. {
  81. return new GlDrawOperation(this);
  82. }
  83. private class GlDrawOperation : ICustomDrawOperation
  84. {
  85. private static int _framebuffer;
  86. public Rect Bounds { get; }
  87. private readonly OpenGLRendererControl _control;
  88. public GlDrawOperation(OpenGLRendererControl control)
  89. {
  90. _control = control;
  91. Bounds = _control.Bounds;
  92. }
  93. public void Dispose() { }
  94. public static void DeleteFramebuffer()
  95. {
  96. if (_framebuffer == 0)
  97. {
  98. GL.DeleteFramebuffer(_framebuffer);
  99. }
  100. _framebuffer = 0;
  101. }
  102. public bool Equals(ICustomDrawOperation other)
  103. {
  104. return other is GlDrawOperation operation && Equals(this, operation) && operation.Bounds == Bounds;
  105. }
  106. public bool HitTest(Point p)
  107. {
  108. return Bounds.Contains(p);
  109. }
  110. private void CreateRenderTarget()
  111. {
  112. _framebuffer = GL.GenFramebuffer();
  113. }
  114. public void Render(IDrawingContextImpl context)
  115. {
  116. if (_control.Image == null)
  117. {
  118. return;
  119. }
  120. if (_framebuffer == 0)
  121. {
  122. CreateRenderTarget();
  123. }
  124. int currentFramebuffer = GL.GetInteger(GetPName.FramebufferBinding);
  125. var image = _control.Image;
  126. var fence = _control._fence;
  127. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
  128. GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, (int)image, 0);
  129. GL.BindFramebuffer(FramebufferTarget.Framebuffer, currentFramebuffer);
  130. if (context is not ISkiaDrawingContextImpl skiaDrawingContextImpl)
  131. {
  132. return;
  133. }
  134. var imageInfo = new SKImageInfo((int)_control.RenderSize.Width, (int)_control.RenderSize.Height, SKColorType.Rgba8888);
  135. var glInfo = new GRGlFramebufferInfo((uint)_framebuffer, SKColorType.Rgba8888.ToGlSizedFormat());
  136. GL.WaitSync(fence, WaitSyncFlags.None, ulong.MaxValue);
  137. using var backendTexture = new GRBackendRenderTarget(imageInfo.Width, imageInfo.Height, 1, 0, glInfo);
  138. using var surface = SKSurface.Create(skiaDrawingContextImpl.GrContext, backendTexture, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
  139. if (surface == null)
  140. {
  141. return;
  142. }
  143. var rect = new Rect(new Point(), _control.RenderSize);
  144. using var snapshot = surface.Snapshot();
  145. skiaDrawingContextImpl.SkCanvas.DrawImage(snapshot, rect.ToSKRect(), _control.Bounds.ToSKRect(), new SKPaint());
  146. }
  147. }
  148. }
  149. }