aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Controls/RendererControl.cs
blob: 130348f24ef8a3d878f2cd0a6525b4665cda71d7 (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
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Media;
using Avalonia.Rendering.SceneGraph;
using Ryujinx.Common.Configuration;
using SPB.Windowing;
using System;

namespace Ryujinx.Ava.Ui.Controls
{
    internal abstract class RendererControl : Control
    {
        protected object _image;

        static RendererControl()
        {
            AffectsRender<RendererControl>(ImageProperty);
        }

        public readonly static StyledProperty<object> ImageProperty =
            AvaloniaProperty.Register<RendererControl, object>(
                nameof(Image),
                0,
                inherits: true,
                defaultBindingMode: BindingMode.TwoWay);

        protected object Image
        {
            get => _image;
            set => SetAndRaise(ImageProperty, ref _image, value);
        }

        public event EventHandler<EventArgs> RendererInitialized;
        public event EventHandler<Size> SizeChanged;

        protected Size RenderSize { get; private set; }
        public bool IsStarted { get; private set; }

        public GraphicsDebugLevel DebugLevel { get; }

        private bool _isInitialized;

        protected ICustomDrawOperation DrawOperation { get; private set; }

        public RendererControl(GraphicsDebugLevel graphicsDebugLevel)
        {
            DebugLevel = graphicsDebugLevel;
            IObservable<Rect> resizeObservable = this.GetObservable(BoundsProperty);

            resizeObservable.Subscribe(Resized);

            Focusable = true;
        }

        protected void Resized(Rect rect)
        {
            SizeChanged?.Invoke(this, rect.Size);

            if (!rect.IsEmpty)
            {
                RenderSize = rect.Size * VisualRoot.RenderScaling;

                DrawOperation?.Dispose();
                DrawOperation = CreateDrawOperation();
            }
        }

        protected abstract ICustomDrawOperation CreateDrawOperation();
        protected abstract void CreateWindow();

        public override void Render(DrawingContext context)
        {
            if (!_isInitialized)
            {
                CreateWindow();

                OnRendererInitialized();
                _isInitialized = true;
            }

            if (!IsStarted || Image == null)
            {
                return;
            }

            if (DrawOperation != null)
            {
                context.Custom(DrawOperation);
            }

            base.Render(context);
        }

        protected void OnRendererInitialized()
        {
            RendererInitialized?.Invoke(this, EventArgs.Empty);
        }

        public void QueueRender()
        {
            Program.RenderTimer.TickNow();
        }

        internal abstract void Present(object image);

        internal void Start()
        {
            IsStarted = true;
            QueueRender();
        }

        internal void Stop()
        {
            IsStarted = false;
        }

        public abstract void DestroyBackgroundContext();
        internal abstract void MakeCurrent();
        internal abstract void MakeCurrent(SwappableNativeWindowBase window);
    }
}