VulkanRendererControl.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using Avalonia;
  2. using Avalonia.Media;
  3. using Avalonia.Platform;
  4. using Avalonia.Rendering.SceneGraph;
  5. using Avalonia.Skia;
  6. using Avalonia.Threading;
  7. using Ryujinx.Ava.Ui.Backend.Vulkan;
  8. using Ryujinx.Ava.Ui.Vulkan;
  9. using Ryujinx.Common.Configuration;
  10. using Ryujinx.Graphics.Vulkan;
  11. using Silk.NET.Vulkan;
  12. using SkiaSharp;
  13. using SPB.Windowing;
  14. using System;
  15. using System.Collections.Concurrent;
  16. namespace Ryujinx.Ava.Ui.Controls
  17. {
  18. internal class VulkanRendererControl : RendererControl
  19. {
  20. private const int MaxImagesInFlight = 3;
  21. private VulkanPlatformInterface _platformInterface;
  22. private ConcurrentQueue<PresentImageInfo> _imagesInFlight;
  23. private PresentImageInfo _currentImage;
  24. public VulkanRendererControl(GraphicsDebugLevel graphicsDebugLevel) : base(graphicsDebugLevel)
  25. {
  26. _platformInterface = AvaloniaLocator.Current.GetService<VulkanPlatformInterface>();
  27. _imagesInFlight = new ConcurrentQueue<PresentImageInfo>();
  28. }
  29. public override void DestroyBackgroundContext()
  30. {
  31. }
  32. protected override ICustomDrawOperation CreateDrawOperation()
  33. {
  34. return new VulkanDrawOperation(this);
  35. }
  36. protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
  37. {
  38. base.OnDetachedFromVisualTree(e);
  39. _imagesInFlight.Clear();
  40. if (_platformInterface.MainSurface.Display != null)
  41. {
  42. _platformInterface.MainSurface.Display.Presented -= Window_Presented;
  43. }
  44. _currentImage?.Put();
  45. _currentImage = null;
  46. }
  47. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  48. {
  49. base.OnAttachedToVisualTree(e);
  50. _platformInterface.MainSurface.Display.Presented += Window_Presented;
  51. }
  52. private void Window_Presented(object sender, EventArgs e)
  53. {
  54. _platformInterface.MainSurface.Device.QueueWaitIdle();
  55. _currentImage?.Put();
  56. _currentImage = null;
  57. }
  58. public override void Render(DrawingContext context)
  59. {
  60. base.Render(context);
  61. }
  62. protected override void CreateWindow()
  63. {
  64. }
  65. internal override void MakeCurrent()
  66. {
  67. }
  68. internal override void MakeCurrent(SwappableNativeWindowBase window)
  69. {
  70. }
  71. internal override void Present(object image)
  72. {
  73. Image = image;
  74. _imagesInFlight.Enqueue((PresentImageInfo)image);
  75. if (_imagesInFlight.Count > MaxImagesInFlight)
  76. {
  77. _imagesInFlight.TryDequeue(out _);
  78. }
  79. Dispatcher.UIThread.Post(InvalidateVisual);
  80. }
  81. private PresentImageInfo GetImage()
  82. {
  83. lock (_imagesInFlight)
  84. {
  85. if (!_imagesInFlight.TryDequeue(out _currentImage))
  86. {
  87. _currentImage = (PresentImageInfo)Image;
  88. }
  89. return _currentImage;
  90. }
  91. }
  92. private class VulkanDrawOperation : ICustomDrawOperation
  93. {
  94. public Rect Bounds { get; }
  95. private readonly VulkanRendererControl _control;
  96. private bool _isDestroyed;
  97. public VulkanDrawOperation(VulkanRendererControl control)
  98. {
  99. _control = control;
  100. Bounds = _control.Bounds;
  101. }
  102. public void Dispose()
  103. {
  104. if (_isDestroyed)
  105. {
  106. return;
  107. }
  108. _isDestroyed = true;
  109. }
  110. public bool Equals(ICustomDrawOperation other)
  111. {
  112. return other is VulkanDrawOperation operation && Equals(this, operation) && operation.Bounds == Bounds;
  113. }
  114. public bool HitTest(Point p)
  115. {
  116. return Bounds.Contains(p);
  117. }
  118. public unsafe void Render(IDrawingContextImpl context)
  119. {
  120. if (_isDestroyed || _control.Image == null || _control.RenderSize.Width == 0 || _control.RenderSize.Height == 0 ||
  121. context is not ISkiaDrawingContextImpl skiaDrawingContextImpl)
  122. {
  123. return;
  124. }
  125. var image = _control.GetImage();
  126. if (!image.State.IsValid)
  127. {
  128. _control._currentImage = null;
  129. return;
  130. }
  131. var gpu = AvaloniaLocator.Current.GetService<VulkanSkiaGpu>();
  132. image.Get();
  133. var imageInfo = new GRVkImageInfo()
  134. {
  135. CurrentQueueFamily = _control._platformInterface.PhysicalDevice.QueueFamilyIndex,
  136. Format = (uint)Format.R8G8B8A8Unorm,
  137. Image = image.Image.Handle,
  138. ImageLayout = (uint)ImageLayout.TransferSrcOptimal,
  139. ImageTiling = (uint)ImageTiling.Optimal,
  140. ImageUsageFlags = (uint)(ImageUsageFlags.ImageUsageColorAttachmentBit
  141. | ImageUsageFlags.ImageUsageTransferSrcBit
  142. | ImageUsageFlags.ImageUsageTransferDstBit),
  143. LevelCount = 1,
  144. SampleCount = 1,
  145. Protected = false,
  146. Alloc = new GRVkAlloc()
  147. {
  148. Memory = image.Memory.Handle,
  149. Flags = 0,
  150. Offset = image.MemoryOffset,
  151. Size = image.MemorySize
  152. }
  153. };
  154. using var backendTexture = new GRBackendRenderTarget(
  155. (int)image.Extent.Width,
  156. (int)image.Extent.Height,
  157. 1,
  158. imageInfo);
  159. var vulkan = AvaloniaLocator.Current.GetService<VulkanPlatformInterface>();
  160. using var surface = SKSurface.Create(
  161. skiaDrawingContextImpl.GrContext,
  162. backendTexture,
  163. GRSurfaceOrigin.TopLeft,
  164. SKColorType.Rgba8888);
  165. if (surface == null)
  166. {
  167. return;
  168. }
  169. var rect = new Rect(new Point(), new Size(image.Extent.Width, image.Extent.Height));
  170. using var snapshot = surface.Snapshot();
  171. skiaDrawingContextImpl.SkCanvas.DrawImage(snapshot, rect.ToSKRect(), _control.Bounds.ToSKRect(),
  172. new SKPaint());
  173. }
  174. }
  175. }
  176. }