Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced a way to lease the underlying platform graphics context from Skia context #14652

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions api/Avalonia.Skia.nupkg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Avalonia.Skia.ISkiaSharpApiLease.TryLeasePlatformGraphicsApi</Target>
<Left>baseline/netstandard2.0/Avalonia.Skia.dll</Left>
<Right>target/netstandard2.0/Avalonia.Skia.dll</Right>
</Suppression>
</Suppressions>
54 changes: 50 additions & 4 deletions src/Skia/Avalonia.Skia/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private class ApiLease : ISkiaSharpApiLease
private readonly DrawingContextImpl _context;
private readonly SKMatrix _revertTransform;
private bool _isDisposed;
private bool _leased;

public ApiLease(DrawingContextImpl context)
{
Expand All @@ -107,11 +108,26 @@ public ApiLease(DrawingContextImpl context)
_context._leased = true;
}

public SKCanvas SkCanvas => _context.Canvas;
void CheckLease()
{
if (_leased)
throw new InvalidOperationException("The underlying graphics API is currently leased");
}

T CheckLease<T>(T rv)
{
CheckLease();
return rv;
}

public SKCanvas SkCanvas => CheckLease(_context.Canvas);
// GrContext is accessible during the lease since one might want to wrap native resources
// Into Skia ones
public GRContext? GrContext => _context.GrContext;
public SKSurface? SkSurface => _context.Surface;
public double CurrentOpacity => _context._currentOpacity;

public SKSurface? SkSurface => CheckLease(_context.Surface);
public double CurrentOpacity => CheckLease(_context._currentOpacity);


public void Dispose()
{
if (!_isDisposed)
Expand All @@ -121,6 +137,36 @@ public void Dispose()
_isDisposed = true;
}
}

class PlatformApiLease : ISkiaSharpPlatformGraphicsApiLease
{
private readonly ApiLease _parent;

public PlatformApiLease(ApiLease parent, IPlatformGraphicsContext context)
{
_parent = parent;
_parent.GrContext?.Flush();
Context = context;
_parent._leased = true;
}

public void Dispose()
{
_parent._leased = false;
_parent.GrContext?.ResetContext();
}

public IPlatformGraphicsContext Context { get; }
}

public ISkiaSharpPlatformGraphicsApiLease? TryLeasePlatformGraphicsApi()
{
CheckLease();
if (_context._gpu is ISkiaGpuWithPlatformGraphicsContext gpu &&
gpu.PlatformGraphicsContext is { } context)
return new PlatformApiLease(this, context);
return null;
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/Skia/Avalonia.Skia/Gpu/ISkiaGpu.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Avalonia.Metadata;
using Avalonia.Platform;
using SkiaSharp;

Expand All @@ -24,6 +25,13 @@ public interface ISkiaGpu : IPlatformGraphicsContext
/// <param name="session">An optional custom render session.</param>
ISkiaSurface? TryCreateSurface(PixelSize size, ISkiaGpuRenderSession? session);
}

//TODO12: Merge into ISkiaGpu
[Unstable]
public interface ISkiaGpuWithPlatformGraphicsContext : ISkiaGpu
{
IPlatformGraphicsContext? PlatformGraphicsContext { get; }
}

public interface ISkiaSurface : IDisposable
{
Expand Down
4 changes: 3 additions & 1 deletion src/Skia/Avalonia.Skia/Gpu/Metal/SkiaMetalGpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Avalonia.Metal;
using Avalonia.Platform;
using SkiaSharp;

namespace Avalonia.Skia.Metal;

internal class SkiaMetalGpu : ISkiaGpu
internal class SkiaMetalGpu : ISkiaGpu, ISkiaGpuWithPlatformGraphicsContext
{
private SkiaMetalApi _api = new();
private GRContext? _context;
Expand All @@ -31,6 +32,7 @@ public void Dispose()

public bool IsLost => false;
public IDisposable EnsureCurrent() => _device.EnsureCurrent();
public IPlatformGraphicsContext? PlatformGraphicsContext => _device;

public ISkiaGpuRenderTarget? TryCreateRenderTarget(IEnumerable<object> surfaces)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Skia/Avalonia.Skia/Gpu/OpenGl/GlSkiaGpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

namespace Avalonia.Skia
{
internal class GlSkiaGpu : ISkiaGpu, IOpenGlTextureSharingRenderInterfaceContextFeature
internal class GlSkiaGpu : ISkiaGpu, IOpenGlTextureSharingRenderInterfaceContextFeature,
ISkiaGpuWithPlatformGraphicsContext
{
private readonly GRContext _grContext;
private readonly IGlContext _glContext;
Expand Down Expand Up @@ -152,6 +153,7 @@ public void Dispose()

public bool IsLost => _glContext.IsLost;
public IDisposable EnsureCurrent() => _glContext.EnsureCurrent();
public IPlatformGraphicsContext? PlatformGraphicsContext => _glContext;

public object? TryGetFeature(Type featureType)
{
Expand Down
8 changes: 8 additions & 0 deletions src/Skia/Avalonia.Skia/ISkiaSharpApiLeaseFeature.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Avalonia.Metadata;
using Avalonia.Platform;
using SkiaSharp;

namespace Avalonia.Skia;
Expand All @@ -17,4 +18,11 @@ public interface ISkiaSharpApiLease : IDisposable
GRContext? GrContext { get; }
SKSurface? SkSurface { get; }
double CurrentOpacity { get; }
ISkiaSharpPlatformGraphicsApiLease? TryLeasePlatformGraphicsApi();
}

[Unstable]
public interface ISkiaSharpPlatformGraphicsApiLease : IDisposable
{
IPlatformGraphicsContext Context { get; }
}