Skip to content

Commit f472205

Browse files
authored
Enhance OpenGlControlBase.cs (#17011)
1. Dispose IGlContext correctly 2. Refactor OpenGL initialization check
1 parent 214ffa7 commit f472205

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/Avalonia.OpenGL/Controls/OpenGlControlBase.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public abstract class OpenGlControlBase : Control
2020
private Task<bool>? _initialization;
2121
private OpenGlControlBaseResources? _resources;
2222
private Compositor? _compositor;
23+
24+
[MemberNotNullWhen(true, nameof(_resources))]
25+
private bool IsInitializedSuccessfully => _initialization is { Status: TaskStatus.RanToCompletion, Result: true };
2326
protected GlVersion GlVersion => _resources?.Context.Version ?? default;
2427

2528
public OpenGlControlBase()
@@ -29,7 +32,7 @@ public OpenGlControlBase()
2932

3033
private void DoCleanup()
3134
{
32-
if (_initialization is { Status: TaskStatus.RanToCompletion } && _resources != null)
35+
if (IsInitializedSuccessfully)
3336
{
3437
try
3538
{
@@ -86,14 +89,13 @@ private bool EnsureInitializedCore(
8689
ctx = contextFactory.CreateContext(null);
8790
if (ctx.TryGetFeature<IGlContextExternalObjectsFeature>(out var externalObjects))
8891
_resources = OpenGlControlBaseResources.TryCreate(ctx, surface, interop, externalObjects);
89-
else
90-
ctx.Dispose();
9192
}
9293

9394
if(_resources == null)
9495
{
9596
Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
9697
"Unable to initialize OpenGL: current platform does not support multithreaded context sharing and shared memory");
98+
ctx?.Dispose();
9799
return false;
98100
}
99101
}
@@ -137,15 +139,13 @@ private bool EnsureInitialized()
137139
if (_initialization != null)
138140
{
139141
// Check if we've previously failed to initialize OpenGL on this platform
140-
if (_initialization is { IsCompleted: true, Result: false } ||
141-
_initialization?.IsFaulted == true)
142-
return false;
143-
144-
// Check if we are still waiting for init to complete
145-
if (_initialization is { IsCompleted: false })
142+
// or if we are still waiting for init to complete
143+
if (!IsInitializedSuccessfully)
144+
{
146145
return false;
146+
}
147147

148-
if (_resources!.Context.IsLost)
148+
if (_resources.Context.IsLost)
149149
ContextLost();
150150
else
151151
return true;
@@ -157,8 +157,10 @@ async void ContinueOnInitialization()
157157
{
158158
try
159159
{
160-
await _initialization;
161-
RequestNextFrameRendering();
160+
if (await _initialization)
161+
{
162+
RequestNextFrameRendering();
163+
}
162164
}
163165
catch
164166
{
@@ -224,7 +226,7 @@ await _compositor.TryGetRenderInterfaceFeature(
224226

225227
public void RequestNextFrameRendering()
226228
{
227-
if ((_initialization == null || _initialization is { Status: TaskStatus.RanToCompletion }) &&
229+
if ((_initialization == null || IsInitializedSuccessfully) &&
228230
!_updateQueued && _compositor != null)
229231
{
230232
_updateQueued = true;

0 commit comments

Comments
 (0)