Browse Source

Enhance OpenGlControlBase.cs (#17011)

1. Dispose IGlContext correctly
2. Refactor OpenGL initialization check
pull/17516/head
Heku 1 year ago
committed by GitHub
parent
commit
f47220570b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 28
      src/Avalonia.OpenGL/Controls/OpenGlControlBase.cs

28
src/Avalonia.OpenGL/Controls/OpenGlControlBase.cs

@ -20,6 +20,9 @@ namespace Avalonia.OpenGL.Controls
private Task<bool>? _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<IGlContextExternalObjectsFeature>(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;

Loading…
Cancel
Save