Skip to content

Commit e1c2605

Browse files
Cache WebGPU support and gate drawing backend
1 parent d88e6de commit e1c2605

4 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/ImageSharp.Drawing.WebGPU/WebGPUDrawingBackend.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public sealed unsafe partial class WebGPUDrawingBackend : IDrawingBackend, IDisp
4949
private const int CallbackTimeoutMilliseconds = 10_000;
5050

5151
private readonly DefaultDrawingBackend fallbackBackend;
52+
private static bool? isSupported;
5253
private bool isDisposed;
5354

5455
private static readonly Dictionary<Type, CompositePixelRegistration> CompositePixelHandlers = CreateCompositePixelHandlers();
@@ -75,24 +76,6 @@ private enum PreparedBrushType : uint
7576
Recolor = 8,
7677
}
7778

78-
/// <summary>
79-
/// Gets a value indicating whether WebGPU is available on the current system.
80-
/// This probes the runtime by attempting to acquire an adapter and device.
81-
/// </summary>
82-
/// <returns><see langword="true"/> when WebGPU is functional; otherwise <see langword="false"/>.</returns>
83-
public static bool IsSupported()
84-
{
85-
try
86-
{
87-
using WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
88-
return WebGPURuntime.TryGetOrCreateDevice(out _, out _, out _);
89-
}
90-
catch
91-
{
92-
return false;
93-
}
94-
}
95-
9679
/// <summary>
9780
/// Gets the testing-only diagnostic counter for total coverage preparation requests.
9881
/// </summary>
@@ -154,6 +137,26 @@ public static bool IsSupported()
154137
/// </summary>
155138
internal int TestingComputePathBatchCount { get; private set; }
156139

140+
/// <summary>
141+
/// Gets a value indicating whether WebGPU is available on the current system.
142+
/// This probes the runtime by attempting to acquire an adapter and device.
143+
/// The result is cached after the first probe.
144+
/// </summary>
145+
public bool IsSupported => isSupported ??= ProbeSupport();
146+
147+
private static bool ProbeSupport()
148+
{
149+
try
150+
{
151+
using WebGPURuntime.Lease lease = WebGPURuntime.Acquire();
152+
return WebGPURuntime.TryGetOrCreateDevice(out _, out _, out _);
153+
}
154+
catch
155+
{
156+
return false;
157+
}
158+
}
159+
157160
/// <inheritdoc />
158161
public void FlushCompositions<TPixel>(
159162
Configuration configuration,
@@ -324,7 +327,7 @@ public void ReleaseFrameResources<TPixel>(
324327
ICanvasFrame<TPixel> target)
325328
where TPixel : unmanaged, IPixel<TPixel>
326329
{
327-
nint targetIdentity = (nint)RuntimeHelpers.GetHashCode(target);
330+
nint targetIdentity = RuntimeHelpers.GetHashCode(target);
328331
WebGPUFlushContext.ReleaseCpuTargetEntries(targetIdentity);
329332
}
330333

src/ImageSharp.Drawing/Processing/Backends/IDrawingBackend.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace SixLabors.ImageSharp.Drawing.Processing.Backends;
1010
/// </summary>
1111
public interface IDrawingBackend
1212
{
13+
/// <summary>
14+
/// Gets a value indicating whether this backend is available on the current system.
15+
/// </summary>
16+
public bool IsSupported => true;
17+
1318
/// <summary>
1419
/// Flushes queued composition operations for the target.
1520
/// </summary>

src/ImageSharp.Drawing/Processing/RasterizerDefaultsExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public static void SetDrawingBackend(this Configuration configuration, IDrawingB
4343
internal static IDrawingBackend GetDrawingBackend(this IImageProcessingContext context)
4444
{
4545
if (context.Properties.TryGetValue(typeof(IDrawingBackend), out object? backend) &&
46-
backend is IDrawingBackend configured)
46+
backend is IDrawingBackend configured &&
47+
configured.IsSupported)
4748
{
4849
return configured;
4950
}
@@ -59,7 +60,8 @@ internal static IDrawingBackend GetDrawingBackend(this IImageProcessingContext c
5960
internal static IDrawingBackend GetDrawingBackend(this Configuration configuration)
6061
{
6162
if (configuration.Properties.TryGetValue(typeof(IDrawingBackend), out object? backend) &&
62-
backend is IDrawingBackend configured)
63+
backend is IDrawingBackend configured &&
64+
configured.IsSupported)
6365
{
6466
return configured;
6567
}

tests/ImageSharp.Drawing.Tests/TestUtilities/Attributes/WebGPUAttributes.cs renamed to tests/ImageSharp.Drawing.Tests/TestUtilities/Attributes/WebGPUFactAttribute.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class WebGPUFactAttribute : FactAttribute
1212
{
1313
public WebGPUFactAttribute()
1414
{
15-
if (!WebGPUDrawingBackend.IsSupported())
15+
using WebGPUDrawingBackend backend = new();
16+
if (!backend.IsSupported)
1617
{
1718
this.Skip = "WebGPU is not available on this system.";
1819
}
@@ -26,7 +27,8 @@ public class WebGPUTheoryAttribute : TheoryAttribute
2627
{
2728
public WebGPUTheoryAttribute()
2829
{
29-
if (!WebGPUDrawingBackend.IsSupported())
30+
using WebGPUDrawingBackend backend = new();
31+
if (!backend.IsSupported)
3032
{
3133
this.Skip = "WebGPU is not available on this system.";
3234
}

0 commit comments

Comments
 (0)