Skip to content

Commit 3fc9b18

Browse files
Remove overly defensive guards
1 parent d3c3979 commit 3fc9b18

8 files changed

Lines changed: 21 additions & 94 deletions

File tree

src/ImageSharp.Drawing.WebGPU/WebGPUDeviceContext{TPixel}.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,6 @@ internal WebGPUDeviceContext(Configuration configuration, WebGPUDeviceHandle dev
108108
Guard.NotNull(queueHandle, nameof(queueHandle));
109109
EnsurePixelTypeSupported();
110110

111-
if (deviceHandle.IsInvalid)
112-
{
113-
throw new ArgumentOutOfRangeException(nameof(deviceHandle), "Device handle must be non-zero.");
114-
}
115-
116-
if (queueHandle.IsInvalid)
117-
{
118-
throw new ArgumentOutOfRangeException(nameof(queueHandle), "Queue handle must be non-zero.");
119-
}
120-
121111
this.DeviceHandle = deviceHandle;
122112
this.QueueHandle = queueHandle;
123113
this.Backend = new WebGPUDrawingBackend();
@@ -322,16 +312,6 @@ private NativeSurface CreateSurface(
322312
Guard.NotNull(textureHandle, nameof(textureHandle));
323313
Guard.NotNull(textureViewHandle, nameof(textureViewHandle));
324314

325-
if (textureHandle.IsInvalid)
326-
{
327-
throw new ArgumentOutOfRangeException(nameof(textureHandle), "Texture handle must be non-zero.");
328-
}
329-
330-
if (textureViewHandle.IsInvalid)
331-
{
332-
throw new ArgumentOutOfRangeException(nameof(textureViewHandle), "Texture view handle must be non-zero.");
333-
}
334-
335315
return WebGPUNativeSurfaceFactory.Create<TPixel>(
336316
this.DeviceHandle,
337317
this.QueueHandle,

src/ImageSharp.Drawing.WebGPU/WebGPUNativeSurfaceFactory.cs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ internal static NativeSurface Create<TPixel>(
6666
Guard.NotNull(targetTextureHandle, nameof(targetTextureHandle));
6767
Guard.NotNull(targetTextureViewHandle, nameof(targetTextureViewHandle));
6868

69-
ValidateCommon(deviceHandle, queueHandle, targetTextureHandle, targetTextureViewHandle, width, height);
69+
Guard.MustBeGreaterThan(width, 0, nameof(width));
70+
Guard.MustBeGreaterThan(height, 0, nameof(height));
7071
ValidatePixelCompatibility<TPixel>(targetFormat);
7172

7273
NativeSurface nativeSurface = new(TPixel.GetPixelTypeInfo());
@@ -81,41 +82,6 @@ internal static NativeSurface Create<TPixel>(
8182
return nativeSurface;
8283
}
8384

84-
/// <summary>
85-
/// Validates the shared handle and size requirements for every native-surface factory entry point.
86-
/// </summary>
87-
private static void ValidateCommon(
88-
WebGPUDeviceHandle deviceHandle,
89-
WebGPUQueueHandle queueHandle,
90-
WebGPUTextureHandle targetTextureHandle,
91-
WebGPUTextureViewHandle targetTextureViewHandle,
92-
int width,
93-
int height)
94-
{
95-
if (deviceHandle.IsInvalid)
96-
{
97-
throw new ArgumentOutOfRangeException(nameof(deviceHandle), "Device handle must be non-zero.");
98-
}
99-
100-
if (queueHandle.IsInvalid)
101-
{
102-
throw new ArgumentOutOfRangeException(nameof(queueHandle), "Queue handle must be non-zero.");
103-
}
104-
105-
if (targetTextureHandle.IsInvalid)
106-
{
107-
throw new ArgumentOutOfRangeException(nameof(targetTextureHandle), "Texture handle must be non-zero.");
108-
}
109-
110-
if (targetTextureViewHandle.IsInvalid)
111-
{
112-
throw new ArgumentOutOfRangeException(nameof(targetTextureViewHandle), "Texture view handle must be non-zero.");
113-
}
114-
115-
Guard.MustBeGreaterThan(width, 0, nameof(width));
116-
Guard.MustBeGreaterThan(height, 0, nameof(height));
117-
}
118-
11985
/// <summary>
12086
/// Validates that the requested pixel type maps to the supplied WebGPU texture format.
12187
/// </summary>

src/ImageSharp.Drawing/CubicBezierLineSegment.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,10 @@ public sealed class CubicBezierLineSegment : ILineSegment
2525
/// <param name="points">The points.</param>
2626
public CubicBezierLineSegment(PointF[] points)
2727
{
28-
this.controlPoints = points ?? throw new ArgumentNullException(nameof(points));
29-
30-
Guard.MustBeGreaterThanOrEqualTo(this.controlPoints.Length, 4, nameof(points));
31-
32-
int correctPointCount = (this.controlPoints.Length - 1) % 3;
33-
if (correctPointCount != 0)
34-
{
35-
throw new ArgumentOutOfRangeException(nameof(points), "points must be a multiple of 3 plus 1 long.");
36-
}
28+
Guard.NotNull(points, nameof(points));
29+
Guard.MustBeGreaterThanOrEqualTo(points.Length, 4, nameof(points));
30+
Guard.IsTrue((points.Length - 1) % 3 == 0, nameof(points), "points must be a multiple of 3 plus 1 long.");
31+
this.controlPoints = points;
3732
}
3833

3934
/// <summary>

src/ImageSharp.Drawing/LinearLineSegment.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public LinearLineSegment(PointF point1, PointF point2, params PointF[] additiona
4444
/// <param name="points">The points.</param>
4545
public LinearLineSegment(PointF[] points)
4646
{
47-
this.points = points ?? throw new ArgumentNullException(nameof(points));
48-
49-
Guard.MustBeGreaterThanOrEqualTo(this.points.Length, 2, nameof(points));
50-
this.Bounds = CalculateBounds(this.points);
47+
Guard.NotNull(points, nameof(points));
48+
Guard.MustBeGreaterThanOrEqualTo(points.Length, 2, nameof(points));
49+
this.points = points;
50+
this.Bounds = CalculateBounds(points);
5151
}
5252

5353
/// <summary>

src/ImageSharp.Drawing/Path.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Path(Path path)
5353
/// <param name="segments">The segments.</param>
5454
public Path(params ILineSegment[] segments)
5555
{
56-
ArgumentNullException.ThrowIfNull(segments);
56+
Guard.NotNull(segments, nameof(segments));
5757
this.lineSegments = segments;
5858
}
5959

src/ImageSharp.Drawing/PathCollection.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public PathCollection(IEnumerable<IPath> paths)
3030
/// <param name="paths">The collection of paths</param>
3131
public PathCollection(params IPath[] paths)
3232
{
33-
this.paths = paths ?? throw new ArgumentNullException(nameof(paths));
33+
Guard.NotNull(paths, nameof(paths));
34+
this.paths = paths;
3435

35-
if (this.paths.Length == 0)
36+
if (paths.Length == 0)
3637
{
3738
this.bounds = new RectangleF(0, 0, 0, 0);
3839
}

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,6 @@ public void DrawText(
487487
Pen? pen)
488488
{
489489
this.EnsureNotDisposed();
490-
Guard.NotNull(textOptions, nameof(textOptions));
491490

492491
if (text.IsEmpty)
493492
{
@@ -584,7 +583,6 @@ public void DrawGlyphs(
584583
public RectangleF MeasureTextAdvance(RichTextOptions textOptions, ReadOnlySpan<char> text)
585584
{
586585
this.EnsureNotDisposed();
587-
Guard.NotNull(textOptions, nameof(textOptions));
588586

589587
if (text.IsEmpty)
590588
{
@@ -599,7 +597,6 @@ public RectangleF MeasureTextAdvance(RichTextOptions textOptions, ReadOnlySpan<c
599597
public RectangleF MeasureTextBounds(RichTextOptions textOptions, ReadOnlySpan<char> text)
600598
{
601599
this.EnsureNotDisposed();
602-
Guard.NotNull(textOptions, nameof(textOptions));
603600

604601
if (text.IsEmpty)
605602
{
@@ -614,7 +611,6 @@ public RectangleF MeasureTextBounds(RichTextOptions textOptions, ReadOnlySpan<ch
614611
public RectangleF MeasureTextRenderableBounds(RichTextOptions textOptions, ReadOnlySpan<char> text)
615612
{
616613
this.EnsureNotDisposed();
617-
Guard.NotNull(textOptions, nameof(textOptions));
618614

619615
if (text.IsEmpty)
620616
{
@@ -629,7 +625,6 @@ public RectangleF MeasureTextRenderableBounds(RichTextOptions textOptions, ReadO
629625
public RectangleF MeasureTextSize(RichTextOptions textOptions, ReadOnlySpan<char> text)
630626
{
631627
this.EnsureNotDisposed();
632-
Guard.NotNull(textOptions, nameof(textOptions));
633628

634629
if (text.IsEmpty)
635630
{
@@ -644,7 +639,6 @@ public RectangleF MeasureTextSize(RichTextOptions textOptions, ReadOnlySpan<char
644639
public bool TryMeasureCharacterAdvances(RichTextOptions textOptions, ReadOnlySpan<char> text, out ReadOnlySpan<GlyphBounds> advances)
645640
{
646641
this.EnsureNotDisposed();
647-
Guard.NotNull(textOptions, nameof(textOptions));
648642

649643
if (text.IsEmpty)
650644
{
@@ -659,7 +653,6 @@ public bool TryMeasureCharacterAdvances(RichTextOptions textOptions, ReadOnlySpa
659653
public bool TryMeasureCharacterBounds(RichTextOptions textOptions, ReadOnlySpan<char> text, out ReadOnlySpan<GlyphBounds> bounds)
660654
{
661655
this.EnsureNotDisposed();
662-
Guard.NotNull(textOptions, nameof(textOptions));
663656

664657
if (text.IsEmpty)
665658
{
@@ -674,7 +667,6 @@ public bool TryMeasureCharacterBounds(RichTextOptions textOptions, ReadOnlySpan<
674667
public bool TryMeasureCharacterRenderableBounds(RichTextOptions textOptions, ReadOnlySpan<char> text, out ReadOnlySpan<GlyphBounds> bounds)
675668
{
676669
this.EnsureNotDisposed();
677-
Guard.NotNull(textOptions, nameof(textOptions));
678670

679671
if (text.IsEmpty)
680672
{
@@ -689,7 +681,6 @@ public bool TryMeasureCharacterRenderableBounds(RichTextOptions textOptions, Rea
689681
public bool TryMeasureCharacterSizes(RichTextOptions textOptions, ReadOnlySpan<char> text, out ReadOnlySpan<GlyphBounds> sizes)
690682
{
691683
this.EnsureNotDisposed();
692-
Guard.NotNull(textOptions, nameof(textOptions));
693684

694685
if (text.IsEmpty)
695686
{
@@ -704,7 +695,6 @@ public bool TryMeasureCharacterSizes(RichTextOptions textOptions, ReadOnlySpan<c
704695
public int CountTextLines(RichTextOptions textOptions, ReadOnlySpan<char> text)
705696
{
706697
this.EnsureNotDisposed();
707-
Guard.NotNull(textOptions, nameof(textOptions));
708698

709699
if (text.IsEmpty)
710700
{
@@ -718,7 +708,6 @@ public int CountTextLines(RichTextOptions textOptions, ReadOnlySpan<char> text)
718708
public LineMetrics[] GetTextLineMetrics(RichTextOptions textOptions, ReadOnlySpan<char> text)
719709
{
720710
this.EnsureNotDisposed();
721-
Guard.NotNull(textOptions, nameof(textOptions));
722711

723712
if (text.IsEmpty)
724713
{
@@ -754,7 +743,11 @@ public void DrawImage(
754743
Rectangle sourceRect,
755744
RectangleF destinationRect,
756745
IResampler? sampler = null)
757-
=> this.DrawImageCore(image, sourceRect, destinationRect, sampler, ownsSourceImage: false);
746+
{
747+
this.EnsureNotDisposed();
748+
Guard.NotNull(image, nameof(image));
749+
this.DrawImageCore(image, sourceRect, destinationRect, sampler, ownsSourceImage: false);
750+
}
758751

759752
private void DrawImageCore(
760753
Image<TPixel> image,
@@ -763,8 +756,6 @@ private void DrawImageCore(
763756
IResampler? sampler,
764757
bool ownsSourceImage)
765758
{
766-
this.EnsureNotDisposed();
767-
Guard.NotNull(image, nameof(image));
768759
bool disposeSourceImage = ownsSourceImage;
769760

770761
DrawingCanvasState state = this.ResolveState();
@@ -1075,10 +1066,6 @@ private Brush NormalizeBrush(Brush brush)
10751066
/// <param name="path">Path to fill.</param>
10761067
private void EnqueueFillPath(Brush brush, IPath path)
10771068
{
1078-
this.EnsureNotDisposed();
1079-
Guard.NotNull(path, nameof(path));
1080-
Guard.NotNull(brush, nameof(brush));
1081-
10821069
DrawingCanvasState state = this.ResolveState();
10831070
IPath closed = path.AsClosedPath();
10841071

@@ -1101,8 +1088,6 @@ private void DrawTextOperations(
11011088
DrawingOptions drawingOptions,
11021089
IReadOnlyList<IPath> clipPaths)
11031090
{
1104-
this.EnsureNotDisposed();
1105-
11061091
// Build composition commands and enforce render-pass ordering while preserving
11071092
// original emission order inside each pass. This preserves overlapping color-font
11081093
// layer compositing semantics (for example emoji mouth/teeth layers).
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)