diff --git a/src/Avalonia.OpenGL/Controls/OpenGlControlBase.cs b/src/Avalonia.OpenGL/Controls/OpenGlControlBase.cs index ff86c79ea3..9978a19165 100644 --- a/src/Avalonia.OpenGL/Controls/OpenGlControlBase.cs +++ b/src/Avalonia.OpenGL/Controls/OpenGlControlBase.cs @@ -20,6 +20,9 @@ namespace Avalonia.OpenGL.Controls private Task? _initialization; private OpenGlControlBaseResources? _resources; private Compositor? _compositor; + + [MemberNotNullWhen(true, nameof(_resources))] + private bool IsInitializedSuccessfully => _initialization is { Status: TaskStatus.RanToCompletion, Result: true }; protected GlVersion GlVersion => _resources?.Context.Version ?? default; public OpenGlControlBase() @@ -29,7 +32,7 @@ namespace Avalonia.OpenGL.Controls private void DoCleanup() { - if (_initialization is { Status: TaskStatus.RanToCompletion } && _resources != null) + if (IsInitializedSuccessfully) { try { @@ -86,14 +89,13 @@ namespace Avalonia.OpenGL.Controls ctx = contextFactory.CreateContext(null); if (ctx.TryGetFeature(out var externalObjects)) _resources = OpenGlControlBaseResources.TryCreate(ctx, surface, interop, externalObjects); - else - ctx.Dispose(); } if(_resources == null) { Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase", "Unable to initialize OpenGL: current platform does not support multithreaded context sharing and shared memory"); + ctx?.Dispose(); return false; } } @@ -137,15 +139,13 @@ namespace Avalonia.OpenGL.Controls if (_initialization != null) { // Check if we've previously failed to initialize OpenGL on this platform - if (_initialization is { IsCompleted: true, Result: false } || - _initialization?.IsFaulted == true) - return false; - - // Check if we are still waiting for init to complete - if (_initialization is { IsCompleted: false }) + // or if we are still waiting for init to complete + if (!IsInitializedSuccessfully) + { return false; + } - if (_resources!.Context.IsLost) + if (_resources.Context.IsLost) ContextLost(); else return true; @@ -157,8 +157,10 @@ namespace Avalonia.OpenGL.Controls { try { - await _initialization; - RequestNextFrameRendering(); + if (await _initialization) + { + RequestNextFrameRendering(); + } } catch { @@ -224,7 +226,7 @@ namespace Avalonia.OpenGL.Controls public void RequestNextFrameRendering() { - if ((_initialization == null || _initialization is { Status: TaskStatus.RanToCompletion }) && + if ((_initialization == null || IsInitializedSuccessfully) && !_updateQueued && _compositor != null) { _updateQueued = true;