OpenGLRendererControl.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. }).Wait();
  50. if (_fence != IntPtr.Zero)
  51. {
  52. GL.DeleteSync(_fence);
  53. }
  54. _fence = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None);
  55. QueueRender();
  56. _gameBackgroundWindow.SwapBuffers();
  57. }
  58. internal override void MakeCurrent()
  59. {
  60. GameContext.MakeCurrent(_gameBackgroundWindow);
  61. }
  62. internal override void MakeCurrent(SwappableNativeWindowBase window)
  63. {
  64. GameContext.MakeCurrent(window);
  65. }
  66. protected override void CreateWindow()
  67. {
  68. var flags = OpenGLContextFlags.Compat;
  69. if (DebugLevel != GraphicsDebugLevel.None)
  70. {
  71. flags |= OpenGLContextFlags.Debug;
  72. }
  73. _gameBackgroundWindow = PlatformHelper.CreateOpenGLWindow(FramebufferFormat.Default, 0, 0, 100, 100);
  74. _gameBackgroundWindow.Hide();
  75. GameContext = PlatformHelper.CreateOpenGLContext(FramebufferFormat.Default, Major, Minor, flags, shareContext: PrimaryContext);
  76. GameContext.Initialize(_gameBackgroundWindow);
  77. }
  78. protected override ICustomDrawOperation CreateDrawOperation()
  79. {
  80. return new GlDrawOperation(this);
  81. }
  82. private class GlDrawOperation : ICustomDrawOperation
  83. {
  84. private static int _framebuffer;
  85. public Rect Bounds { get; }
  86. private readonly OpenGLRendererControl _control;
  87. public GlDrawOperation(OpenGLRendererControl control)
  88. {
  89. _control = control;
  90. Bounds = _control.Bounds;
  91. }
  92. public void Dispose() { }
  93. public static void DeleteFramebuffer()
  94. {
  95. if (_framebuffer == 0)
  96. {
  97. GL.DeleteFramebuffer(_framebuffer);
  98. }
  99. _framebuffer = 0;
  100. }
  101. public bool Equals(ICustomDrawOperation other)
  102. {
  103. return other is GlDrawOperation operation && Equals(this, operation) && operation.Bounds == Bounds;
  104. }
  105. public bool HitTest(Point p)
  106. {
  107. return Bounds.Contains(p);
  108. }
  109. private void CreateRenderTarget()
  110. {
  111. _framebuffer = GL.GenFramebuffer();
  112. }
  113. public void Render(IDrawingContextImpl context)
  114. {
  115. if (_control.Image == null)
  116. {
  117. return;
  118. }
  119. if (_framebuffer == 0)
  120. {
  121. CreateRenderTarget();
  122. }
  123. int currentFramebuffer = GL.GetInteger(GetPName.FramebufferBinding);
  124. var image = _control.Image;
  125. var fence = _control._fence;
  126. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
  127. GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, (int)image, 0);
  128. GL.BindFramebuffer(FramebufferTarget.Framebuffer, currentFramebuffer);
  129. if (context is not ISkiaDrawingContextImpl skiaDrawingContextImpl)
  130. {
  131. return;
  132. }
  133. var imageInfo = new SKImageInfo((int)_control.RenderSize.Width, (int)_control.RenderSize.Height, SKColorType.Rgba8888);
  134. var glInfo = new GRGlFramebufferInfo((uint)_framebuffer, SKColorType.Rgba8888.ToGlSizedFormat());
  135. GL.WaitSync(fence, WaitSyncFlags.None, ulong.MaxValue);
  136. using var backendTexture = new GRBackendRenderTarget(imageInfo.Width, imageInfo.Height, 1, 0, glInfo);
  137. using var surface = SKSurface.Create(skiaDrawingContextImpl.GrContext, backendTexture, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
  138. if (surface == null)
  139. {
  140. return;
  141. }
  142. var rect = new Rect(new Point(), _control.RenderSize);
  143. using var snapshot = surface.Snapshot();
  144. skiaDrawingContextImpl.SkCanvas.DrawImage(snapshot, rect.ToSKRect(), _control.Bounds.ToSKRect(), new SKPaint());
  145. }
  146. }
  147. }
  148. }