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

Add SKMatrix.MapPoints/MapVectors, SKPath.AddPoly overloads with Span<SKPoint> #3030

Merged
merged 2 commits into from
Oct 28, 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
24 changes: 24 additions & 0 deletions binding/SkiaSharp/SKMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,18 @@ public readonly SKPoint MapPoint (float x, float y)
return result;
}

public readonly void MapPoints (Span<SKPoint> result, ReadOnlySpan<SKPoint> points)
{
if (result.Length != points.Length)
throw new ArgumentException ("Buffers must be the same size.");

fixed (SKMatrix* t = &this)
fixed (SKPoint* rp = result)
fixed (SKPoint* pp = points) {
SkiaApi.sk_matrix_map_points (t, rp, pp, result.Length);
}
}

public readonly void MapPoints (SKPoint[] result, SKPoint[] points)
{
if (result == null)
Expand Down Expand Up @@ -359,6 +371,18 @@ public readonly SKPoint MapVector (float x, float y)
return result;
}

public readonly void MapVectors (Span<SKPoint> result, ReadOnlySpan<SKPoint> vectors)
{
if (result.Length != vectors.Length)
throw new ArgumentException ("Buffers must be the same size.");

fixed (SKMatrix* t = &this)
fixed (SKPoint* rp = result)
fixed (SKPoint* pp = vectors) {
SkiaApi.sk_matrix_map_vectors (t, rp, pp, result.Length);
}
}

public readonly void MapVectors (SKPoint[] result, SKPoint[] vectors)
{
if (result == null)
Expand Down
9 changes: 9 additions & 0 deletions binding/SkiaSharp/SKPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,15 @@ public void AddRoundRect (SKRect rect, float rx, float ry, SKPathDirection dir =
public void AddCircle (float x, float y, float radius, SKPathDirection dir = SKPathDirection.Clockwise) =>
SkiaApi.sk_path_add_circle (Handle, x, y, radius, dir);

public void AddPoly (ReadOnlySpan<SKPoint> points, bool close = true)
{
if (points == null)
throw new ArgumentNullException (nameof (points));
fixed (SKPoint* p = points) {
SkiaApi.sk_path_add_poly (Handle, p, points.Length, close);
}
}

public void AddPoly (SKPoint[] points, bool close = true)
{
if (points == null)
Expand Down
66 changes: 66 additions & 0 deletions tests/Tests/SkiaSharp/SKMatrixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ public void MatrixMapsPoints()
Assert.Equal(expectedResult, source);
}

[SkippableFact]
public void MatrixMapsSpanPoints()
{
Span<SKPoint> source = stackalloc[] {
new SKPoint(0, 0),
new SKPoint(-10, -10),
new SKPoint(-10, 10),
new SKPoint(10, -10),
new SKPoint(10, 10),
new SKPoint(-5, -5),
new SKPoint(-5, 5),
new SKPoint(5, -5),
new SKPoint(5, 5),
};

Span<SKPoint> expectedResult = stackalloc[] {
new SKPoint(10, 10),
new SKPoint(0, 0),
new SKPoint(0, 20),
new SKPoint(20, 0),
new SKPoint(20, 20),
new SKPoint(5, 5),
new SKPoint(5, 15),
new SKPoint(15, 5),
new SKPoint(15, 15),
};

var matrix = SKMatrix.CreateTranslation(10, 10);
matrix.MapPoints(source, source);

Assert.True(expectedResult.SequenceEqual(source));
}

[SkippableFact]
public void MapRectCreatesModifiedRect()
{
Expand Down Expand Up @@ -134,6 +167,39 @@ public void MatrixMapsVectors()
Assert.Equal(expectedResult, source);
}

[SkippableFact]
public void MatrixMapsSpanVectors()
{
Span<SKPoint> source = stackalloc[] {
new SKPoint(0, 0),
new SKPoint(-10, -10),
new SKPoint(-10, 10),
new SKPoint(10, -10),
new SKPoint(10, 10),
new SKPoint(-5, -5),
new SKPoint(-5, 5),
new SKPoint(5, -5),
new SKPoint(5, 5),
};

Span<SKPoint> expectedResult = stackalloc[] {
new SKPoint(0, 0),
new SKPoint(-10, -10),
new SKPoint(-10, 10),
new SKPoint(10, -10),
new SKPoint(10, 10),
new SKPoint(-5, -5),
new SKPoint(-5, 5),
new SKPoint(5, -5),
new SKPoint(5, 5),
};

var matrix = SKMatrix.CreateTranslation(10, 10);
matrix.MapVectors(source, source);

Assert.True(expectedResult.SequenceEqual(source));
}

[SkippableFact]
public void MatrixMapsVector()
{
Expand Down
Loading