RendererControl.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Data;
  4. using Avalonia.Media;
  5. using Avalonia.OpenGL;
  6. using Avalonia.Platform;
  7. using Avalonia.Rendering.SceneGraph;
  8. using Avalonia.Skia;
  9. using Avalonia.Threading;
  10. using OpenTK.Graphics.OpenGL;
  11. using Ryujinx.Common.Configuration;
  12. using SkiaSharp;
  13. using SPB.Graphics;
  14. using SPB.Graphics.OpenGL;
  15. using SPB.Platform;
  16. using SPB.Windowing;
  17. using System;
  18. namespace Ryujinx.Ava.Ui.Controls
  19. {
  20. public class RendererControl : Control
  21. {
  22. private int _image;
  23. static RendererControl()
  24. {
  25. AffectsRender<RendererControl>(ImageProperty);
  26. }
  27. public readonly static StyledProperty<int> ImageProperty =
  28. AvaloniaProperty.Register<RendererControl, int>(nameof(Image), 0, inherits: true, defaultBindingMode: BindingMode.TwoWay);
  29. protected int Image
  30. {
  31. get => _image;
  32. set => SetAndRaise(ImageProperty, ref _image, value);
  33. }
  34. public event EventHandler<EventArgs> GlInitialized;
  35. public event EventHandler<Size> SizeChanged;
  36. protected Size RenderSize { get; private set; }
  37. public bool IsStarted { get; private set; }
  38. public int Major { get; }
  39. public int Minor { get; }
  40. public GraphicsDebugLevel DebugLevel { get; }
  41. public OpenGLContextBase GameContext { get; set; }
  42. public static OpenGLContextBase PrimaryContext => AvaloniaLocator.Current.GetService<IPlatformOpenGlInterface>().PrimaryContext.AsOpenGLContextBase();
  43. private SwappableNativeWindowBase _gameBackgroundWindow;
  44. private bool _isInitialized;
  45. private IntPtr _fence;
  46. private GlDrawOperation _glDrawOperation;
  47. public RendererControl(int major, int minor, GraphicsDebugLevel graphicsDebugLevel)
  48. {
  49. Major = major;
  50. Minor = minor;
  51. DebugLevel = graphicsDebugLevel;
  52. IObservable<Rect> resizeObservable = this.GetObservable(BoundsProperty);
  53. resizeObservable.Subscribe(Resized);
  54. Focusable = true;
  55. }
  56. private void Resized(Rect rect)
  57. {
  58. SizeChanged?.Invoke(this, rect.Size);
  59. RenderSize = rect.Size * Program.WindowScaleFactor;
  60. _glDrawOperation?.Dispose();
  61. _glDrawOperation = new GlDrawOperation(this);
  62. }
  63. public override void Render(DrawingContext context)
  64. {
  65. if (!_isInitialized)
  66. {
  67. CreateWindow();
  68. OnGlInitialized();
  69. _isInitialized = true;
  70. }
  71. if (GameContext == null || !IsStarted || Image == 0)
  72. {
  73. return;
  74. }
  75. if (_glDrawOperation != null)
  76. {
  77. context.Custom(_glDrawOperation);
  78. }
  79. base.Render(context);
  80. }
  81. protected void OnGlInitialized()
  82. {
  83. GlInitialized?.Invoke(this, EventArgs.Empty);
  84. }
  85. public void QueueRender()
  86. {
  87. Program.RenderTimer.TickNow();
  88. }
  89. internal void Present(object image)
  90. {
  91. Dispatcher.UIThread.InvokeAsync(() =>
  92. {
  93. Image = (int)image;
  94. }).Wait();
  95. if (_fence != IntPtr.Zero)
  96. {
  97. GL.DeleteSync(_fence);
  98. }
  99. _fence = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None);
  100. QueueRender();
  101. _gameBackgroundWindow.SwapBuffers();
  102. }
  103. internal void Start()
  104. {
  105. IsStarted = true;
  106. QueueRender();
  107. }
  108. internal void Stop()
  109. {
  110. IsStarted = false;
  111. }
  112. public void DestroyBackgroundContext()
  113. {
  114. _image = 0;
  115. if (_fence != IntPtr.Zero)
  116. {
  117. _glDrawOperation.Dispose();
  118. GL.DeleteSync(_fence);
  119. }
  120. GlDrawOperation.DeleteFramebuffer();
  121. GameContext?.Dispose();
  122. _gameBackgroundWindow?.Dispose();
  123. }
  124. internal void MakeCurrent()
  125. {
  126. GameContext.MakeCurrent(_gameBackgroundWindow);
  127. }
  128. internal void MakeCurrent(SwappableNativeWindowBase window)
  129. {
  130. GameContext.MakeCurrent(window);
  131. }
  132. protected void CreateWindow()
  133. {
  134. var flags = OpenGLContextFlags.Compat;
  135. if (DebugLevel != GraphicsDebugLevel.None)
  136. {
  137. flags |= OpenGLContextFlags.Debug;
  138. }
  139. _gameBackgroundWindow = PlatformHelper.CreateOpenGLWindow(FramebufferFormat.Default, 0, 0, 100, 100);
  140. _gameBackgroundWindow.Hide();
  141. GameContext = PlatformHelper.CreateOpenGLContext(FramebufferFormat.Default, Major, Minor, flags, shareContext: PrimaryContext);
  142. GameContext.Initialize(_gameBackgroundWindow);
  143. }
  144. private class GlDrawOperation : ICustomDrawOperation
  145. {
  146. private static int _framebuffer;
  147. public Rect Bounds { get; }
  148. private readonly RendererControl _control;
  149. public GlDrawOperation(RendererControl control)
  150. {
  151. _control = control;
  152. Bounds = _control.Bounds;
  153. }
  154. public void Dispose() { }
  155. public static void DeleteFramebuffer()
  156. {
  157. if (_framebuffer == 0)
  158. {
  159. GL.DeleteFramebuffer(_framebuffer);
  160. }
  161. _framebuffer = 0;
  162. }
  163. public bool Equals(ICustomDrawOperation other)
  164. {
  165. return other is GlDrawOperation operation && Equals(this, operation) && operation.Bounds == Bounds;
  166. }
  167. public bool HitTest(Point p)
  168. {
  169. return Bounds.Contains(p);
  170. }
  171. private void CreateRenderTarget()
  172. {
  173. _framebuffer = GL.GenFramebuffer();
  174. }
  175. public void Render(IDrawingContextImpl context)
  176. {
  177. if (_control.Image == 0)
  178. {
  179. return;
  180. }
  181. if (_framebuffer == 0)
  182. {
  183. CreateRenderTarget();
  184. }
  185. int currentFramebuffer = GL.GetInteger(GetPName.FramebufferBinding);
  186. var image = _control.Image;
  187. var fence = _control._fence;
  188. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
  189. GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, image, 0);
  190. GL.BindFramebuffer(FramebufferTarget.Framebuffer, currentFramebuffer);
  191. if (context is not ISkiaDrawingContextImpl skiaDrawingContextImpl)
  192. {
  193. return;
  194. }
  195. var imageInfo = new SKImageInfo((int)_control.RenderSize.Width, (int)_control.RenderSize.Height, SKColorType.Rgba8888);
  196. var glInfo = new GRGlFramebufferInfo((uint)_framebuffer, SKColorType.Rgba8888.ToGlSizedFormat());
  197. GL.WaitSync(fence, WaitSyncFlags.None, ulong.MaxValue);
  198. using var backendTexture = new GRBackendRenderTarget(imageInfo.Width, imageInfo.Height, 1, 0, glInfo);
  199. using var surface = SKSurface.Create(skiaDrawingContextImpl.GrContext, backendTexture, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
  200. if (surface == null)
  201. {
  202. return;
  203. }
  204. var rect = new Rect(new Point(), _control.RenderSize);
  205. using var snapshot = surface.Snapshot();
  206. skiaDrawingContextImpl.SkCanvas.DrawImage(snapshot, rect.ToSKRect(), _control.Bounds.ToSKRect(), new SKPaint());
  207. }
  208. }
  209. }
  210. }