aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Controls/VulkanRendererControl.cs
blob: fdbd8df978a62b25e7811c0f256736df60e54a92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using Avalonia;
using Avalonia.Platform;
using Avalonia.Rendering.SceneGraph;
using Avalonia.Skia;
using Avalonia.Threading;
using Ryujinx.Ava.Ui.Backend.Vulkan;
using Ryujinx.Ava.Ui.Vulkan;
using Ryujinx.Common.Configuration;
using Ryujinx.Graphics.Vulkan;
using Silk.NET.Vulkan;
using SkiaSharp;
using SPB.Windowing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ryujinx.Ava.Ui.Controls
{
    internal class VulkanRendererControl : RendererControl
    {
        private VulkanPlatformInterface _platformInterface;

        public VulkanRendererControl(GraphicsDebugLevel graphicsDebugLevel) : base(graphicsDebugLevel)
        {
            _platformInterface = AvaloniaLocator.Current.GetService<VulkanPlatformInterface>();
        }

        public override void DestroyBackgroundContext()
        {

        }

        protected override ICustomDrawOperation CreateDrawOperation()
        {
            return new VulkanDrawOperation(this);
        }

        protected override void CreateWindow()
        {
        }

        internal override void MakeCurrent()
        {
        }

        internal override void MakeCurrent(SwappableNativeWindowBase window)
        {
        }

        internal override void Present(object image)
        {
            Dispatcher.UIThread.InvokeAsync(() =>
            {
                Image = image;
            }).Wait();

            QueueRender();
        }

        private class VulkanDrawOperation : ICustomDrawOperation
        {
            public Rect Bounds { get; }

            private readonly VulkanRendererControl _control;

            public VulkanDrawOperation(VulkanRendererControl control)
            {
                _control = control;
                Bounds = _control.Bounds;
            }

            public void Dispose()
            {

            }

            public bool Equals(ICustomDrawOperation other)
            {
                return other is VulkanDrawOperation operation && Equals(this, operation) && operation.Bounds == Bounds;
            }

            public bool HitTest(Point p)
            {
                return Bounds.Contains(p);
            }

            public void Render(IDrawingContextImpl context)
            {
                if (_control.Image == null || _control.RenderSize.Width == 0 || _control.RenderSize.Height == 0)
                {
                    return;
                }

                var image = (PresentImageInfo)_control.Image;

                if (context is not ISkiaDrawingContextImpl skiaDrawingContextImpl)
                {
                    return;
                }

                _control._platformInterface.Device.QueueWaitIdle();

                var gpu = AvaloniaLocator.Current.GetService<VulkanSkiaGpu>();

                var imageInfo = new GRVkImageInfo()
                {
                    CurrentQueueFamily = _control._platformInterface.PhysicalDevice.QueueFamilyIndex,
                    Format = (uint)Format.R8G8B8A8Unorm,
                    Image = image.Image.Handle,
                    ImageLayout = (uint)ImageLayout.ColorAttachmentOptimal,
                    ImageTiling = (uint)ImageTiling.Optimal,
                    ImageUsageFlags = (uint)(ImageUsageFlags.ImageUsageColorAttachmentBit
                                             | ImageUsageFlags.ImageUsageTransferSrcBit
                                             | ImageUsageFlags.ImageUsageTransferDstBit),
                    LevelCount = 1,
                    SampleCount = 1,
                    Protected = false,
                    Alloc = new GRVkAlloc()
                    {
                        Memory = image.Memory.Handle,
                        Flags = 0,
                        Offset = image.MemoryOffset,
                        Size = image.MemorySize
                    }
                };

                using var backendTexture = new GRBackendRenderTarget(
                    (int)_control.RenderSize.Width,
                    (int)_control.RenderSize.Height,
                    1,
                    imageInfo);

                using var surface = SKSurface.Create(
                    gpu.GrContext,
                    backendTexture,
                    GRSurfaceOrigin.TopLeft,
                    SKColorType.Rgba8888);

                if (surface == null)
                {
                    return;
                }

                var rect = new Rect(new Point(), _control.RenderSize);

                using var snapshot = surface.Snapshot();
                skiaDrawingContextImpl.SkCanvas.DrawImage(snapshot, rect.ToSKRect(), _control.Bounds.ToSKRect(), new SKPaint());
            }
        }
    }
}