RendererControl.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. internal 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. if (!rect.IsEmpty)
  60. {
  61. RenderSize = rect.Size * VisualRoot.RenderScaling;
  62. _glDrawOperation?.Dispose();
  63. _glDrawOperation = new GlDrawOperation(this);
  64. }
  65. }
  66. public override void Render(DrawingContext context)
  67. {
  68. if (!_isInitialized)
  69. {
  70. CreateWindow();
  71. OnGlInitialized();
  72. _isInitialized = true;
  73. }
  74. if (GameContext == null || !IsStarted || Image == 0)
  75. {
  76. return;
  77. }
  78. if (_glDrawOperation != null)
  79. {
  80. context.Custom(_glDrawOperation);
  81. }
  82. base.Render(context);
  83. }
  84. protected void OnGlInitialized()
  85. {
  86. GlInitialized?.Invoke(this, EventArgs.Empty);
  87. }
  88. public void QueueRender()
  89. {
  90. Program.RenderTimer.TickNow();
  91. }
  92. internal void Present(object image)
  93. {
  94. Dispatcher.UIThread.InvokeAsync(() =>
  95. {
  96. Image = (int)image;
  97. }).Wait();
  98. if (_fence != IntPtr.Zero)
  99. {
  100. GL.DeleteSync(_fence);
  101. }
  102. _fence = GL.FenceSync(SyncCondition.SyncGpuCommandsComplete, WaitSyncFlags.None);
  103. QueueRender();
  104. _gameBackgroundWindow.SwapBuffers();
  105. }
  106. internal void Start()
  107. {
  108. IsStarted = true;
  109. QueueRender();
  110. }
  111. internal void Stop()
  112. {
  113. IsStarted = false;
  114. }
  115. public void DestroyBackgroundContext()
  116. {
  117. _image = 0;
  118. if (_fence != IntPtr.Zero)
  119. {
  120. _glDrawOperation.Dispose();
  121. GL.DeleteSync(_fence);
  122. }
  123. GlDrawOperation.DeleteFramebuffer();
  124. GameContext?.Dispose();
  125. _gameBackgroundWindow?.Dispose();
  126. }
  127. internal void MakeCurrent()
  128. {
  129. GameContext.MakeCurrent(_gameBackgroundWindow);
  130. }
  131. internal void MakeCurrent(SwappableNativeWindowBase window)
  132. {
  133. GameContext.MakeCurrent(window);
  134. }
  135. protected void CreateWindow()
  136. {
  137. var flags = OpenGLContextFlags.Compat;
  138. if (DebugLevel != GraphicsDebugLevel.None)
  139. {
  140. flags |= OpenGLContextFlags.Debug;
  141. }
  142. _gameBackgroundWindow = PlatformHelper.CreateOpenGLWindow(FramebufferFormat.Default, 0, 0, 100, 100);
  143. _gameBackgroundWindow.Hide();
  144. GameContext = PlatformHelper.CreateOpenGLContext(FramebufferFormat.Default, Major, Minor, flags, shareContext: PrimaryContext);
  145. GameContext.Initialize(_gameBackgroundWindow);
  146. }
  147. private class GlDrawOperation : ICustomDrawOperation
  148. {
  149. private static int _framebuffer;
  150. public Rect Bounds { get; }
  151. private readonly RendererControl _control;
  152. public GlDrawOperation(RendererControl control)
  153. {
  154. _control = control;
  155. Bounds = _control.Bounds;
  156. }
  157. public void Dispose() { }
  158. public static void DeleteFramebuffer()
  159. {
  160. if (_framebuffer == 0)
  161. {
  162. GL.DeleteFramebuffer(_framebuffer);
  163. }
  164. _framebuffer = 0;
  165. }
  166. public bool Equals(ICustomDrawOperation other)
  167. {
  168. return other is GlDrawOperation operation && Equals(this, operation) && operation.Bounds == Bounds;
  169. }
  170. public bool HitTest(Point p)
  171. {
  172. return Bounds.Contains(p);
  173. }
  174. private void CreateRenderTarget()
  175. {
  176. _framebuffer = GL.GenFramebuffer();
  177. }
  178. public void Render(IDrawingContextImpl context)
  179. {
  180. if (_control.Image == 0)
  181. {
  182. return;
  183. }
  184. if (_framebuffer == 0)
  185. {
  186. CreateRenderTarget();
  187. }
  188. int currentFramebuffer = GL.GetInteger(GetPName.FramebufferBinding);
  189. var image = _control.Image;
  190. var fence = _control._fence;
  191. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _framebuffer);
  192. GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2D, image, 0);
  193. GL.BindFramebuffer(FramebufferTarget.Framebuffer, currentFramebuffer);
  194. if (context is not ISkiaDrawingContextImpl skiaDrawingContextImpl)
  195. {
  196. return;
  197. }
  198. var imageInfo = new SKImageInfo((int)_control.RenderSize.Width, (int)_control.RenderSize.Height, SKColorType.Rgba8888);
  199. var glInfo = new GRGlFramebufferInfo((uint)_framebuffer, SKColorType.Rgba8888.ToGlSizedFormat());
  200. GL.WaitSync(fence, WaitSyncFlags.None, ulong.MaxValue);
  201. using var backendTexture = new GRBackendRenderTarget(imageInfo.Width, imageInfo.Height, 1, 0, glInfo);
  202. using var surface = SKSurface.Create(skiaDrawingContextImpl.GrContext, backendTexture, GRSurfaceOrigin.BottomLeft, SKColorType.Rgba8888);
  203. if (surface == null)
  204. {
  205. return;
  206. }
  207. var rect = new Rect(new Point(), _control.RenderSize);
  208. using var snapshot = surface.Snapshot();
  209. skiaDrawingContextImpl.SkCanvas.DrawImage(snapshot, rect.ToSKRect(), _control.Bounds.ToSKRect(), new SKPaint());
  210. }
  211. }
  212. }
  213. }