-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
OpenGlLeasePage.xaml.cs
216 lines (184 loc) · 6.83 KB
/
OpenGlLeasePage.xaml.cs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.OpenGL;
using Avalonia.Platform;
using Avalonia.Rendering.Composition;
using Avalonia.Skia;
using ControlCatalog.Pages.OpenGl;
using SkiaSharp;
using static Avalonia.OpenGL.GlConsts;
namespace ControlCatalog.Pages;
public class OpenGlLeasePage : UserControl
{
private readonly Control _viewport;
private readonly GlPageKnobs _knobs;
private CompositionCustomVisual? _visual;
class GlVisual : CompositionCustomVisualHandler
{
private OpenGlContent _content;
private Parameters _parameters;
private bool _contentInitialized;
private OpenGlFbo? _fbo;
private bool _reRender;
private IGlContext? _gl;
public GlVisual(OpenGlContent content, Parameters parameters)
{
_content = content;
_parameters = parameters;
}
public override void OnRender(ImmediateDrawingContext drawingContext)
{
if (_parameters.Disco > 0.01f)
RegisterForNextAnimationFrameUpdate();
var bounds = GetRenderBounds();
var size = PixelSize.FromSize(bounds.Size, 1);
if (size.Width < 1 || size.Height < 1)
return;
if(drawingContext.TryGetFeature<ISkiaSharpApiLeaseFeature>(out var skiaFeature))
{
using var skiaLease = skiaFeature.Lease();
var grContext = skiaLease.GrContext;
if (grContext == null)
return;
SKImage? snapshot;
using (var platformApiLease = skiaLease.TryLeasePlatformGraphicsApi())
{
if (platformApiLease?.Context is not IGlContext glContext)
return;
var gl = glContext.GlInterface;
if (_gl != glContext)
{
// The old context is lost
_fbo = null;
_contentInitialized = false;
_gl = glContext;
}
gl.GetIntegerv(GL_FRAMEBUFFER_BINDING, out var oldFb);
_fbo ??= new OpenGlFbo(glContext, grContext);
if (_fbo.Size != size)
_fbo.Resize(size);
gl.BindFramebuffer(GL_FRAMEBUFFER, _fbo.Fbo);
if (!_contentInitialized)
{
_content.Init(gl, glContext.Version);
_contentInitialized = true;
}
_content.OnOpenGlRender(gl, _fbo.Fbo, size, _parameters.Yaw, _parameters.Pitch,
_parameters.Roll, _parameters.Disco);
snapshot = _fbo.Snapshot();
gl.BindFramebuffer(GL_FRAMEBUFFER, oldFb);
}
using(snapshot)
if (snapshot != null)
skiaLease.SkCanvas.DrawImage(snapshot, new SKRect(0, 0,
(float)bounds.Width, (float)bounds.Height));
}
}
public override void OnAnimationFrameUpdate()
{
if (_reRender || _parameters.Disco > 0.01f)
{
_reRender = false;
Invalidate();
}
base.OnAnimationFrameUpdate();
}
public override void OnMessage(object message)
{
if (message is Parameters p)
{
_parameters = p;
_reRender = true;
RegisterForNextAnimationFrameUpdate();
}
else if (message is DisposeMessage)
{
if (_gl != null)
{
try
{
if (_fbo != null || _contentInitialized)
{
using (_gl.MakeCurrent())
{
if (_contentInitialized)
_content.Deinit(_gl.GlInterface);
_contentInitialized = false;
_fbo?.Dispose();
_fbo = null;
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
_gl = null;
}
}
base.OnMessage(message);
}
}
public class Parameters
{
public float Yaw;
public float Pitch;
public float Roll;
public float Disco;
}
public class DisposeMessage
{
}
public OpenGlLeasePage()
{
AvaloniaXamlLoader.Load(this);
_viewport = this.FindControl<Control>("Viewport")!;
_viewport.AttachedToVisualTree += ViewportAttachedToVisualTree;
_viewport.DetachedFromVisualTree += ViewportDetachedFromVisualTree;
_knobs = this.FindControl<GlPageKnobs>("Knobs")!;
_knobs.PropertyChanged += KnobsPropertyChanged;
}
private void KnobsPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs change)
{
if (change.Property == GlPageKnobs.YawProperty
|| change.Property == GlPageKnobs.RollProperty
|| change.Property == GlPageKnobs.PitchProperty
|| change.Property == GlPageKnobs.DiscoProperty)
_visual?.SendHandlerMessage(GetParameters());
}
Parameters GetParameters() => new()
{
Yaw = _knobs!.Yaw, Pitch = _knobs.Pitch, Roll = _knobs.Roll, Disco = _knobs.Disco
};
private void ViewportAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
{
var visual = ElementComposition.GetElementVisual(_viewport!);
if(visual == null)
return;
_visual = visual.Compositor.CreateCustomVisual(new GlVisual(new OpenGlContent(), GetParameters()));
ElementComposition.SetElementChildVisual(_viewport, _visual);
UpdateSize(Bounds.Size);
}
private void UpdateSize(Size size)
{
if (_visual != null)
_visual.Size = new Vector(size.Width, size.Height);
}
protected override Size ArrangeOverride(Size finalSize)
{
var size = base.ArrangeOverride(finalSize);
UpdateSize(size);
return size;
}
private void ViewportDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
{
_visual?.SendHandlerMessage(new DisposeMessage());
_visual = null;
ElementComposition.SetElementChildVisual(_viewport, null);
base.OnDetachedFromVisualTree(e);
}
}