RendererControl.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Data;
  4. using Avalonia.Media;
  5. using Avalonia.Rendering.SceneGraph;
  6. using Ryujinx.Common.Configuration;
  7. using SPB.Windowing;
  8. using System;
  9. namespace Ryujinx.Ava.Ui.Controls
  10. {
  11. internal abstract class RendererControl : Control
  12. {
  13. protected object Image { get; set; }
  14. public event EventHandler<EventArgs> RendererInitialized;
  15. public event EventHandler<Size> SizeChanged;
  16. protected Size RenderSize { get; private set; }
  17. public bool IsStarted { get; private set; }
  18. public GraphicsDebugLevel DebugLevel { get; }
  19. private bool _isInitialized;
  20. protected ICustomDrawOperation DrawOperation { get; private set; }
  21. public RendererControl(GraphicsDebugLevel graphicsDebugLevel)
  22. {
  23. DebugLevel = graphicsDebugLevel;
  24. IObservable<Rect> resizeObservable = this.GetObservable(BoundsProperty);
  25. resizeObservable.Subscribe(Resized);
  26. Focusable = true;
  27. }
  28. protected void Resized(Rect rect)
  29. {
  30. SizeChanged?.Invoke(this, rect.Size);
  31. if (!rect.IsEmpty)
  32. {
  33. RenderSize = rect.Size * VisualRoot.RenderScaling;
  34. DrawOperation = CreateDrawOperation();
  35. }
  36. }
  37. protected abstract ICustomDrawOperation CreateDrawOperation();
  38. protected abstract void CreateWindow();
  39. public override void Render(DrawingContext context)
  40. {
  41. if (!_isInitialized)
  42. {
  43. CreateWindow();
  44. OnRendererInitialized();
  45. _isInitialized = true;
  46. }
  47. if (!IsStarted || Image == null)
  48. {
  49. return;
  50. }
  51. if (DrawOperation != null)
  52. {
  53. context.Custom(DrawOperation);
  54. }
  55. base.Render(context);
  56. }
  57. protected void OnRendererInitialized()
  58. {
  59. RendererInitialized?.Invoke(this, EventArgs.Empty);
  60. }
  61. internal abstract void Present(object image);
  62. internal void Start()
  63. {
  64. IsStarted = true;
  65. }
  66. internal void Stop()
  67. {
  68. IsStarted = false;
  69. }
  70. public abstract void DestroyBackgroundContext();
  71. internal abstract void MakeCurrent();
  72. internal abstract void MakeCurrent(SwappableNativeWindowBase window);
  73. }
  74. }