DrawingSurfaceDemoBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Numerics;
  3. using System.Threading.Tasks;
  4. using Avalonia;
  5. using Avalonia.Controls;
  6. using Avalonia.LogicalTree;
  7. using Avalonia.Rendering.Composition;
  8. using Avalonia.VisualTree;
  9. namespace GpuInterop;
  10. public abstract class DrawingSurfaceDemoBase : Control, IGpuDemo
  11. {
  12. private CompositionSurfaceVisual? _visual;
  13. private Compositor? _compositor;
  14. private Action _update;
  15. private string _info;
  16. private bool _updateQueued;
  17. private bool _initialized;
  18. protected CompositionDrawingSurface Surface { get; private set; }
  19. public DrawingSurfaceDemoBase()
  20. {
  21. _update = UpdateFrame;
  22. }
  23. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  24. {
  25. base.OnAttachedToVisualTree(e);
  26. Initialize();
  27. }
  28. protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  29. {
  30. if (_initialized)
  31. FreeGraphicsResources();
  32. _initialized = false;
  33. base.OnDetachedFromLogicalTree(e);
  34. }
  35. async void Initialize()
  36. {
  37. try
  38. {
  39. var selfVisual = ElementComposition.GetElementVisual(this)!;
  40. _compositor = selfVisual.Compositor;
  41. Surface = _compositor.CreateDrawingSurface();
  42. _visual = _compositor.CreateSurfaceVisual();
  43. _visual.Size = new Vector2((float)Bounds.Width, (float)Bounds.Height);
  44. _visual.Surface = Surface;
  45. ElementComposition.SetElementChildVisual(this, _visual);
  46. var (res, info) = await DoInitialize(_compositor, Surface);
  47. _info = info;
  48. if (ParentControl != null)
  49. ParentControl.Info = info;
  50. _initialized = res;
  51. QueueNextFrame();
  52. }
  53. catch (Exception e)
  54. {
  55. if (ParentControl != null)
  56. ParentControl.Info = e.ToString();
  57. }
  58. }
  59. void UpdateFrame()
  60. {
  61. _updateQueued = false;
  62. var root = this.GetVisualRoot();
  63. if (root == null)
  64. return;
  65. _visual!.Size = new Vector2((float)Bounds.Width, (float)Bounds.Height);
  66. var size = PixelSize.FromSize(Bounds.Size, root.RenderScaling);
  67. RenderFrame(size);
  68. if (SupportsDisco && Disco > 0)
  69. QueueNextFrame();
  70. }
  71. void QueueNextFrame()
  72. {
  73. if (_initialized && !_updateQueued && _compositor != null)
  74. {
  75. _updateQueued = true;
  76. _compositor?.RequestCompositionUpdate(_update);
  77. }
  78. }
  79. protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
  80. {
  81. if(change.Property == BoundsProperty)
  82. QueueNextFrame();
  83. base.OnPropertyChanged(change);
  84. }
  85. async Task<(bool success, string info)> DoInitialize(Compositor compositor,
  86. CompositionDrawingSurface compositionDrawingSurface)
  87. {
  88. var interop = await compositor.TryGetCompositionGpuInterop();
  89. if (interop == null)
  90. return (false, "Compositor doesn't support interop for the current backend");
  91. return InitializeGraphicsResources(compositor, compositionDrawingSurface, interop);
  92. }
  93. protected abstract (bool success, string info) InitializeGraphicsResources(Compositor compositor,
  94. CompositionDrawingSurface compositionDrawingSurface, ICompositionGpuInterop gpuInterop);
  95. protected abstract void FreeGraphicsResources();
  96. protected abstract void RenderFrame(PixelSize pixelSize);
  97. protected virtual bool SupportsDisco => false;
  98. public void Update(GpuDemo parent, float yaw, float pitch, float roll, float disco)
  99. {
  100. ParentControl = parent;
  101. if (ParentControl != null)
  102. {
  103. ParentControl.Info = _info;
  104. ParentControl.DiscoVisible = true;
  105. }
  106. Yaw = yaw;
  107. Pitch = pitch;
  108. Roll = roll;
  109. Disco = disco;
  110. QueueNextFrame();
  111. }
  112. public GpuDemo? ParentControl { get; private set; }
  113. public float Disco { get; private set; }
  114. public float Roll { get; private set; }
  115. public float Pitch { get; private set; }
  116. public float Yaw { get; private set; }
  117. }