OpenGlLeasePage.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Media;
  5. using Avalonia.OpenGL;
  6. using Avalonia.Platform;
  7. using Avalonia.Rendering.Composition;
  8. using Avalonia.Skia;
  9. using ControlCatalog.Pages.OpenGl;
  10. using SkiaSharp;
  11. using static Avalonia.OpenGL.GlConsts;
  12. namespace ControlCatalog.Pages;
  13. public partial class OpenGlLeasePage : UserControl
  14. {
  15. private CompositionCustomVisual? _visual;
  16. class GlVisual : CompositionCustomVisualHandler
  17. {
  18. private OpenGlContent _content;
  19. private Parameters _parameters;
  20. private bool _contentInitialized;
  21. private OpenGlFbo? _fbo;
  22. private bool _reRender;
  23. private IGlContext? _gl;
  24. public GlVisual(OpenGlContent content, Parameters parameters)
  25. {
  26. _content = content;
  27. _parameters = parameters;
  28. }
  29. public override void OnRender(ImmediateDrawingContext drawingContext)
  30. {
  31. if (_parameters.Disco > 0.01f)
  32. RegisterForNextAnimationFrameUpdate();
  33. var bounds = GetRenderBounds();
  34. var size = PixelSize.FromSize(bounds.Size, 1);
  35. if (size.Width < 1 || size.Height < 1)
  36. return;
  37. if(drawingContext.TryGetFeature<ISkiaSharpApiLeaseFeature>(out var skiaFeature))
  38. {
  39. using var skiaLease = skiaFeature.Lease();
  40. var grContext = skiaLease.GrContext;
  41. if (grContext == null)
  42. return;
  43. SKImage? snapshot;
  44. using (var platformApiLease = skiaLease.TryLeasePlatformGraphicsApi())
  45. {
  46. if (platformApiLease?.Context is not IGlContext glContext)
  47. return;
  48. var gl = glContext.GlInterface;
  49. if (_gl != glContext)
  50. {
  51. // The old context is lost
  52. _fbo = null;
  53. _contentInitialized = false;
  54. _gl = glContext;
  55. }
  56. gl.GetIntegerv(GL_FRAMEBUFFER_BINDING, out var oldFb);
  57. _fbo ??= new OpenGlFbo(glContext, grContext);
  58. if (_fbo.Size != size)
  59. _fbo.Resize(size);
  60. gl.BindFramebuffer(GL_FRAMEBUFFER, _fbo.Fbo);
  61. if (!_contentInitialized)
  62. {
  63. _content.Init(gl, glContext.Version);
  64. _contentInitialized = true;
  65. }
  66. _content.OnOpenGlRender(gl, _fbo.Fbo, size, _parameters.Yaw, _parameters.Pitch,
  67. _parameters.Roll, _parameters.Disco);
  68. snapshot = _fbo.Snapshot();
  69. gl.BindFramebuffer(GL_FRAMEBUFFER, oldFb);
  70. }
  71. using(snapshot)
  72. if (snapshot != null)
  73. skiaLease.SkCanvas.DrawImage(snapshot, new SKRect(0, 0,
  74. (float)bounds.Width, (float)bounds.Height));
  75. }
  76. }
  77. public override void OnAnimationFrameUpdate()
  78. {
  79. if (_reRender || _parameters.Disco > 0.01f)
  80. {
  81. _reRender = false;
  82. Invalidate();
  83. }
  84. base.OnAnimationFrameUpdate();
  85. }
  86. public override void OnMessage(object message)
  87. {
  88. if (message is Parameters p)
  89. {
  90. _parameters = p;
  91. _reRender = true;
  92. RegisterForNextAnimationFrameUpdate();
  93. }
  94. else if (message is DisposeMessage)
  95. {
  96. if (_gl != null)
  97. {
  98. try
  99. {
  100. if (_fbo != null || _contentInitialized)
  101. {
  102. using (_gl.MakeCurrent())
  103. {
  104. if (_contentInitialized)
  105. _content.Deinit(_gl.GlInterface);
  106. _contentInitialized = false;
  107. _fbo?.Dispose();
  108. _fbo = null;
  109. }
  110. }
  111. }
  112. catch (Exception e)
  113. {
  114. Console.WriteLine(e.ToString());
  115. }
  116. _gl = null;
  117. }
  118. }
  119. base.OnMessage(message);
  120. }
  121. }
  122. public class Parameters
  123. {
  124. public float Yaw;
  125. public float Pitch;
  126. public float Roll;
  127. public float Disco;
  128. }
  129. public class DisposeMessage
  130. {
  131. }
  132. public OpenGlLeasePage()
  133. {
  134. InitializeComponent();
  135. }
  136. private void KnobsPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs change)
  137. {
  138. if (change.Property == GlPageKnobs.YawProperty
  139. || change.Property == GlPageKnobs.RollProperty
  140. || change.Property == GlPageKnobs.PitchProperty
  141. || change.Property == GlPageKnobs.DiscoProperty)
  142. _visual?.SendHandlerMessage(GetParameters());
  143. }
  144. Parameters GetParameters() => new()
  145. {
  146. Yaw = Knobs!.Yaw, Pitch = Knobs.Pitch, Roll = Knobs.Roll, Disco = Knobs.Disco
  147. };
  148. private void ViewportAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
  149. {
  150. var visual = ElementComposition.GetElementVisual(Viewport);
  151. if(visual == null)
  152. return;
  153. _visual = visual.Compositor.CreateCustomVisual(new GlVisual(new OpenGlContent(), GetParameters()));
  154. ElementComposition.SetElementChildVisual(Viewport, _visual);
  155. UpdateSize(Bounds.Size);
  156. }
  157. private void UpdateSize(Size size)
  158. {
  159. if (_visual != null)
  160. _visual.Size = new Vector(size.Width, size.Height);
  161. }
  162. protected override Size ArrangeOverride(Size finalSize)
  163. {
  164. var size = base.ArrangeOverride(finalSize);
  165. UpdateSize(size);
  166. return size;
  167. }
  168. private void ViewportDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
  169. {
  170. _visual?.SendHandlerMessage(new DisposeMessage());
  171. _visual = null;
  172. ElementComposition.SetElementChildVisual(Viewport, null);
  173. base.OnDetachedFromVisualTree(e);
  174. }
  175. }