Skip to content

Commit 17f7b4c

Browse files
Merge pull request #292 from SixLabors/js/drawing-tweaks
Additional drawing performance fixes
2 parents 5755e6f + 914a3d9 commit 17f7b4c

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed

src/ImageSharp.Drawing/Shapes/PolygonClipper/Clipper.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Numerics;
5-
64
namespace SixLabors.ImageSharp.Drawing.Shapes.PolygonClipper;
75

86
/// <summary>
97
/// Library to clip polygons.
108
/// </summary>
119
internal class Clipper
1210
{
13-
// To make the floating point polygons compatable with clipper we have to scale them.
14-
private const float ScalingFactor = 1000F;
1511
private readonly PolygonClipper polygonClipper;
1612

1713
/// <summary>
@@ -34,17 +30,17 @@ public IPath[] GenerateClippedShapes(ClippingOperation operation, IntersectionRu
3430
FillRule fillRule = rule == IntersectionRule.EvenOdd ? FillRule.EvenOdd : FillRule.NonZero;
3531
this.polygonClipper.Execute(operation, fillRule, closedPaths, openPaths);
3632

37-
var shapes = new IPath[closedPaths.Count + openPaths.Count];
33+
IPath[] shapes = new IPath[closedPaths.Count + openPaths.Count];
3834

3935
int index = 0;
4036
for (int i = 0; i < closedPaths.Count; i++)
4137
{
4238
PathF path = closedPaths[i];
43-
var points = new PointF[path.Count];
39+
PointF[] points = new PointF[path.Count];
4440

4541
for (int j = 0; j < path.Count; j++)
4642
{
47-
points[j] = path[j] / ScalingFactor;
43+
points[j] = path[j];
4844
}
4945

5046
shapes[index++] = new Polygon(points);
@@ -53,11 +49,11 @@ public IPath[] GenerateClippedShapes(ClippingOperation operation, IntersectionRu
5349
for (int i = 0; i < openPaths.Count; i++)
5450
{
5551
PathF path = openPaths[i];
56-
var points = new PointF[path.Count];
52+
PointF[] points = new PointF[path.Count];
5753

5854
for (int j = 0; j < path.Count; j++)
5955
{
60-
points[j] = path[j] / ScalingFactor;
56+
points[j] = path[j];
6157
}
6258

6359
shapes[index++] = new Polygon(points);
@@ -107,7 +103,7 @@ internal void AddPath(ISimplePath path, ClippingType clippingType)
107103
PathF points = new(vectors.Length);
108104
for (int i = 0; i < vectors.Length; i++)
109105
{
110-
points.Add((Vector2)vectors[i] * ScalingFactor);
106+
points.Add(vectors[i]);
111107
}
112108

113109
this.polygonClipper.AddPath(points, clippingType, !path.IsClosed);

src/ImageSharp.Drawing/Shapes/Rasterization/ScanEdgeCollection.Build.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void RoundY(ReadOnlySpan<PointF> vertices, Span<float> destination, float
9898
if (verticesLength > 0)
9999
{
100100
ri = vertices.Length - (remainder / 2);
101-
float maxIterations = verticesLength / (Vector256<float>.Count * 2);
101+
nint maxIterations = verticesLength / (Vector256<float>.Count * 2);
102102
ref Vector256<float> sourceBase = ref Unsafe.As<PointF, Vector256<float>>(ref MemoryMarshal.GetReference(vertices));
103103
ref Vector256<float> destinationBase = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(destination));
104104

@@ -136,7 +136,7 @@ static void RoundY(ReadOnlySpan<PointF> vertices, Span<float> destination, float
136136
if (verticesLength > 0)
137137
{
138138
ri = vertices.Length - (remainder / 2);
139-
float maxIterations = verticesLength / (Vector128<float>.Count * 2);
139+
nint maxIterations = verticesLength / (Vector128<float>.Count * 2);
140140
ref Vector128<float> sourceBase = ref Unsafe.As<PointF, Vector128<float>>(ref MemoryMarshal.GetReference(vertices));
141141
ref Vector128<float> destinationBase = ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(destination));
142142

@@ -172,7 +172,7 @@ static void RoundY(ReadOnlySpan<PointF> vertices, Span<float> destination, float
172172
if (verticesLength > 0)
173173
{
174174
ri = vertices.Length - (remainder / 2);
175-
float maxIterations = verticesLength / (Vector128<float>.Count * 2);
175+
nint maxIterations = verticesLength / (Vector128<float>.Count * 2);
176176
ref Vector128<float> sourceBase = ref Unsafe.As<PointF, Vector128<float>>(ref MemoryMarshal.GetReference(vertices));
177177
ref Vector128<float> destinationBase = ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(destination));
178178

@@ -186,7 +186,7 @@ static void RoundY(ReadOnlySpan<PointF> vertices, Span<float> destination, float
186186
Vector128<float> points1 = Unsafe.Add(ref sourceBase, j);
187187
Vector128<float> points2 = Unsafe.Add(ref sourceBase, j + 1);
188188

189-
// Shuffle the points to group the Y properties
189+
// Shuffle the points to group the Y
190190
Vector128<float> pointsY = AdvSimdShuffle(points1, points2, 0b11_01_11_01);
191191

192192
// Multiply by the subsampling ratio, round, then multiply by the inverted subsampling ratio and assign.

tests/ImageSharp.Drawing.Benchmarks/Drawing/Rounding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private static void RoundYAvx(ReadOnlySpan<PointF> vertices, Span<float> destina
5252
if (verticesLength > 0)
5353
{
5454
ri = vertices.Length - (remainder / 2);
55-
float maxIterations = verticesLength / (Vector256<float>.Count * 2);
55+
nint maxIterations = verticesLength / (Vector256<float>.Count * 2);
5656
ref Vector256<float> sourceBase = ref Unsafe.As<PointF, Vector256<float>>(ref MemoryMarshal.GetReference(vertices));
5757
ref Vector256<float> destinationBase = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(destination));
5858

@@ -99,7 +99,7 @@ private static void RoundYSse41(ReadOnlySpan<PointF> vertices, Span<float> desti
9999
if (verticesLength > 0)
100100
{
101101
ri = vertices.Length - (remainder / 2);
102-
float maxIterations = verticesLength / (Vector128<float>.Count * 2);
102+
nint maxIterations = verticesLength / (Vector128<float>.Count * 2);
103103
ref Vector128<float> sourceBase = ref Unsafe.As<PointF, Vector128<float>>(ref MemoryMarshal.GetReference(vertices));
104104
ref Vector128<float> destinationBase = ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(destination));
105105

0 commit comments

Comments
 (0)