A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

66 lines
1.9 KiB

using System;
using System.Collections.Generic;
using Avalonia.Metadata;
using Avalonia.Platform;
using Avalonia.Reactive;
namespace Avalonia.Rendering;
[Unstable]
// TODO: Make it internal once legacy renderers are removed
public class PlatformRenderInterfaceContextManager
{
private readonly IPlatformGraphics? _graphics;
private IPlatformRenderInterfaceContext? _backend;
private OwnedDisposable<IPlatformGraphicsContext>? _gpuContext;
public PlatformRenderInterfaceContextManager(IPlatformGraphics? graphics)
{
_graphics = graphics;
}
public void EnsureValidBackendContext()
{
if (_backend == null || _gpuContext?.Value.IsLost == true)
{
_backend?.Dispose();
_backend = null;
_gpuContext?.Dispose();
_gpuContext = null;
if (_graphics != null)
{
if (_graphics.UsesSharedContext)
_gpuContext = new OwnedDisposable<IPlatformGraphicsContext>(_graphics.GetSharedContext(), false);
else
_gpuContext = new OwnedDisposable<IPlatformGraphicsContext>(_graphics.CreateContext(), true);
}
_backend = AvaloniaLocator.Current.GetRequiredService<IPlatformRenderInterface>()
.CreateBackendContext(_gpuContext?.Value);
}
}
public IPlatformRenderInterfaceContext Value
{
get
{
EnsureValidBackendContext();
return _backend!;
}
}
public IDisposable EnsureCurrent()
{
EnsureValidBackendContext();
if (_gpuContext.HasValue)
return _gpuContext.Value.Value.EnsureCurrent();
return Disposable.Empty;
}
public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
{
EnsureValidBackendContext();
return _backend!.CreateRenderTarget(surfaces);
}
}