Skip to content

Commit 1310439

Browse files
Move canvas convenience APIs to extensions
1 parent 356bbd8 commit 1310439

4 files changed

Lines changed: 57 additions & 87 deletions

File tree

src/ImageSharp.Drawing.WebGPU/WebGPURuntime.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -406,22 +406,6 @@ internal static int RunComputePipelineSupportProbe()
406406
}
407407
}
408408

409-
/// <summary>
410-
/// Process-exit cleanup callback.
411-
/// </summary>
412-
/// <param name="sender">Event sender.</param>
413-
/// <param name="e">Event arguments.</param>
414-
private static void OnProcessExit(object? sender, EventArgs e)
415-
{
416-
_ = sender;
417-
_ = e;
418-
419-
lock (Sync)
420-
{
421-
DisposeRuntimeCore();
422-
}
423-
}
424-
425409
private static void DisposeRuntimeCore()
426410
{
427411
ClearDeviceStateCache();
@@ -476,7 +460,14 @@ private static void EnsureInitialized()
476460
{
477461
if (!processExitHooked)
478462
{
479-
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
463+
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
464+
{
465+
lock (Sync)
466+
{
467+
DisposeRuntimeCore();
468+
}
469+
};
470+
480471
processExitHooked = true;
481472
}
482473

src/ImageSharp.Drawing/Processing/DrawingCanvasShapeExtensions.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,38 @@
44
namespace SixLabors.ImageSharp.Drawing.Processing;
55

66
/// <summary>
7-
/// Convenience shape helpers that build paths and forward to the core <see cref="IDrawingCanvas"/> fill and draw primitives.
7+
/// Convenience canvas helpers that forward to the core <see cref="IDrawingCanvas"/> primitives.
88
/// </summary>
99
public static class DrawingCanvasShapeExtensions
1010
{
11+
/// <summary>
12+
/// Saves the current drawing state and begins an isolated compositing layer over the whole canvas.
13+
/// </summary>
14+
/// <param name="canvas">The destination canvas.</param>
15+
/// <returns>The save count after the layer state has been pushed.</returns>
16+
public static int SaveLayer(this IDrawingCanvas canvas)
17+
=> canvas.SaveLayer(new GraphicsOptions(), canvas.Bounds);
18+
19+
/// <summary>
20+
/// Saves the current drawing state and begins an isolated compositing layer over the whole canvas.
21+
/// </summary>
22+
/// <param name="canvas">The destination canvas.</param>
23+
/// <param name="layerOptions">Graphics options controlling how the layer is composited on restore.</param>
24+
/// <returns>The save count after the layer state has been pushed.</returns>
25+
public static int SaveLayer(this IDrawingCanvas canvas, GraphicsOptions layerOptions)
26+
=> canvas.SaveLayer(layerOptions, canvas.Bounds);
27+
28+
/// <summary>
29+
/// Fills the whole canvas using the given brush.
30+
/// </summary>
31+
/// <param name="canvas">The destination canvas.</param>
32+
/// <param name="brush">Brush used to shade destination pixels.</param>
33+
public static void Fill(this IDrawingCanvas canvas, Brush brush)
34+
{
35+
Rectangle bounds = canvas.Bounds;
36+
canvas.Fill(brush, new RectangularPolygon(bounds.X, bounds.Y, bounds.Width, bounds.Height));
37+
}
38+
1139
/// <summary>
1240
/// Fills a local region using the given brush.
1341
/// </summary>
@@ -17,6 +45,26 @@ public static class DrawingCanvasShapeExtensions
1745
public static void Fill(this IDrawingCanvas canvas, Brush brush, Rectangle region)
1846
=> canvas.Fill(brush, new RectangularPolygon(region.X, region.Y, region.Width, region.Height));
1947

48+
/// <summary>
49+
/// Clears the whole canvas using the given brush and clear-style composition options.
50+
/// </summary>
51+
/// <param name="canvas">The destination canvas.</param>
52+
/// <param name="brush">Brush used to shade destination pixels during clear.</param>
53+
public static void Clear(this IDrawingCanvas canvas, Brush brush)
54+
{
55+
Rectangle bounds = canvas.Bounds;
56+
canvas.Clear(brush, new RectangularPolygon(bounds.X, bounds.Y, bounds.Width, bounds.Height));
57+
}
58+
59+
/// <summary>
60+
/// Clears a local region using the given brush and clear-style composition options.
61+
/// </summary>
62+
/// <param name="canvas">The destination canvas.</param>
63+
/// <param name="brush">Brush used to shade destination pixels during clear.</param>
64+
/// <param name="region">Region to clear in local coordinates.</param>
65+
public static void Clear(this IDrawingCanvas canvas, Brush brush, Rectangle region)
66+
=> canvas.Clear(brush, new RectangularPolygon(region.X, region.Y, region.Width, region.Height));
67+
2068
/// <summary>
2169
/// Fills all paths in a collection using the given brush.
2270
/// </summary>

src/ImageSharp.Drawing/Processing/DrawingCanvas{TPixel}.cs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,6 @@ private int SaveCore(DrawingOptions options, IReadOnlyList<IPath> clipPaths)
191191
return this.savedStates.Count;
192192
}
193193

194-
/// <inheritdoc />
195-
public int SaveLayer()
196-
=> this.SaveLayer(new GraphicsOptions());
197-
198-
/// <inheritdoc />
199-
public int SaveLayer(GraphicsOptions layerOptions)
200-
=> this.SaveLayer(layerOptions, this.Bounds);
201-
202194
/// <inheritdoc />
203195
public int SaveLayer(GraphicsOptions layerOptions, Rectangle bounds)
204196
{
@@ -278,22 +270,6 @@ public DrawingCanvas<TPixel> CreateRegion(Rectangle region)
278270
IDrawingCanvas IDrawingCanvas.CreateRegion(Rectangle region)
279271
=> this.CreateRegion(region);
280272

281-
/// <inheritdoc />
282-
public void Clear(Brush brush)
283-
{
284-
DrawingCanvasState state = this.ResolveState();
285-
DrawingOptions options = state.Options.CloneForClearOperation();
286-
this.ExecuteWithTemporaryState(options, state.ClipPaths, () => this.Fill(brush));
287-
}
288-
289-
/// <inheritdoc />
290-
public void Clear(Brush brush, Rectangle region)
291-
{
292-
DrawingCanvasState state = this.ResolveState();
293-
DrawingOptions options = state.Options.CloneForClearOperation();
294-
this.ExecuteWithTemporaryState(options, state.ClipPaths, () => this.Fill(brush, region));
295-
}
296-
297273
/// <inheritdoc />
298274
public void Clear(Brush brush, IPath path)
299275
{
@@ -302,10 +278,6 @@ public void Clear(Brush brush, IPath path)
302278
this.ExecuteWithTemporaryState(options, state.ClipPaths, () => this.Fill(brush, path));
303279
}
304280

305-
/// <inheritdoc />
306-
public void Fill(Brush brush)
307-
=> this.Fill(brush, new RectangularPolygon(this.Bounds.X, this.Bounds.Y, this.Bounds.Width, this.Bounds.Height));
308-
309281
/// <inheritdoc />
310282
public void Fill(Brush brush, IPath path)
311283
{

src/ImageSharp.Drawing/Processing/IDrawingCanvas.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,6 @@ public interface IDrawingCanvas : IDisposable
4545
/// <returns>The save count after the previous state has been pushed.</returns>
4646
public int Save(DrawingOptions options, params IPath[] clipPaths);
4747

48-
/// <summary>
49-
/// Saves the current drawing state and begins an isolated compositing layer.
50-
/// Subsequent draw commands are recorded into an isolated logical layer. When
51-
/// <see cref="Restore"/> closes the layer, that layer becomes eligible for
52-
/// composition during the next <see cref="Flush"/> or <see cref="IDisposable.Dispose"/>.
53-
/// </summary>
54-
/// <returns>The save count after the layer state has been pushed.</returns>
55-
public int SaveLayer();
56-
57-
/// <summary>
58-
/// Saves the current drawing state and begins an isolated compositing layer.
59-
/// Subsequent draw commands are recorded into an isolated logical layer. When
60-
/// <see cref="Restore"/> closes the layer, that layer is composed during the next
61-
/// <see cref="Flush"/> or <see cref="IDisposable.Dispose"/> using the specified
62-
/// <paramref name="layerOptions"/> (blend mode, alpha composition, opacity).
63-
/// </summary>
64-
/// <param name="layerOptions">
65-
/// Graphics options controlling how the layer is composited on restore.
66-
/// </param>
67-
/// <returns>The save count after the layer state has been pushed.</returns>
68-
public int SaveLayer(GraphicsOptions layerOptions);
69-
7048
/// <summary>
7149
/// Saves the current drawing state and begins an isolated compositing layer
7250
/// bounded to a subregion. Subsequent draw commands are recorded into that isolated
@@ -113,32 +91,13 @@ public interface IDrawingCanvas : IDisposable
11391
/// <returns>A child canvas with local origin at (0,0).</returns>
11492
public IDrawingCanvas CreateRegion(Rectangle region);
11593

116-
/// <summary>
117-
/// Clears the whole canvas using the given brush and clear-style composition options.
118-
/// </summary>
119-
/// <param name="brush">Brush used to shade destination pixels during clear.</param>
120-
public void Clear(Brush brush);
121-
122-
/// <summary>
123-
/// Clears a local region using the given brush and clear-style composition options.
124-
/// </summary>
125-
/// <param name="brush">Brush used to shade destination pixels during clear.</param>
126-
/// <param name="region">Region to clear in local coordinates.</param>
127-
public void Clear(Brush brush, Rectangle region);
128-
12994
/// <summary>
13095
/// Clears a path region using the given brush and clear-style composition options.
13196
/// </summary>
13297
/// <param name="brush">Brush used to shade destination pixels during clear.</param>
13398
/// <param name="path">The path region to clear.</param>
13499
public void Clear(Brush brush, IPath path);
135100

136-
/// <summary>
137-
/// Fills the whole canvas using the given brush.
138-
/// </summary>
139-
/// <param name="brush">Brush used to shade destination pixels.</param>
140-
public void Fill(Brush brush);
141-
142101
/// <summary>
143102
/// Fills a path in local coordinates using the given brush.
144103
/// </summary>

0 commit comments

Comments
 (0)