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

Start using spans instead of arrays #2617

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 2 additions & 9 deletions binding/SkiaSharp/SKData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,16 @@ public static SKData CreateCopy (IntPtr bytes, ulong length)
return GetObject (SkiaApi.sk_data_new_with_copy ((void*)bytes, (IntPtr)length));
}

public static SKData CreateCopy (byte[] bytes) =>
public static SKData CreateCopy (ReadOnlySpan<byte> bytes) =>
CreateCopy (bytes, (ulong)bytes.Length);

public static SKData CreateCopy (byte[] bytes, ulong length)
public static SKData CreateCopy (ReadOnlySpan<byte> bytes, ulong length)
{
fixed (byte* b = bytes) {
return GetObject (SkiaApi.sk_data_new_with_copy (b, (IntPtr)length));
}
}

public static SKData CreateCopy (ReadOnlySpan<byte> bytes)
{
fixed (byte* b = bytes) {
return CreateCopy ((IntPtr)b, (ulong)bytes.Length);
}
}

// Create

public static SKData Create (int size) =>
Expand Down
24 changes: 0 additions & 24 deletions binding/SkiaSharp/SKImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ public static SKImage FromPixelCopy (SKImageInfo info, Stream pixels, int rowByt
}
}

public static SKImage FromPixelCopy (SKImageInfo info, byte[] pixels) =>
FromPixelCopy (info, pixels, info.RowBytes);

public static SKImage FromPixelCopy (SKImageInfo info, byte[] pixels, int rowBytes)
{
if (pixels == null)
throw new ArgumentNullException (nameof (pixels));
using (var data = SKData.CreateCopy (pixels)) {
return FromPixels (info, data, rowBytes);
}
}

public static SKImage FromPixelCopy (SKImageInfo info, IntPtr pixels) =>
FromPixelCopy (info, pixels, info.RowBytes);

Expand Down Expand Up @@ -183,18 +171,6 @@ public static SKImage FromEncodedData (ReadOnlySpan<byte> data)
}
}

public static SKImage FromEncodedData (byte[] data)
{
if (data == null)
throw new ArgumentNullException (nameof (data));
if (data.Length == 0)
throw new ArgumentException ("The data buffer was empty.");

using (var skdata = SKData.CreateCopy (data)) {
return FromEncodedData (skdata);
}
}

public static SKImage FromEncodedData (SKStream data)
{
if (data == null)
Expand Down
2 changes: 1 addition & 1 deletion binding/SkiaSharp/SKPathEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static SKPathEffect Create2DPath(SKMatrix matrix, SKPath path)
return GetObject(SkiaApi.sk_path_effect_create_2d_path(&matrix, path.Handle));
}

public static SKPathEffect CreateDash(float[] intervals, float phase)
public static SKPathEffect CreateDash(ReadOnlySpan<float> intervals, float phase)
{
if (intervals == null)
throw new ArgumentNullException(nameof(intervals));
Expand Down
2 changes: 1 addition & 1 deletion binding/SkiaSharp/SKRoundRect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void SetNinePatch (SKRect rect, float leftRadius, float topRadius, float
SkiaApi.sk_rrect_set_nine_patch (Handle, &rect, leftRadius, topRadius, rightRadius, bottomRadius);
}

public void SetRectRadii (SKRect rect, SKPoint[] radii)
public void SetRectRadii (SKRect rect, ReadOnlySpan<SKPoint> radii)
{
if (radii == null)
throw new ArgumentNullException (nameof (radii));
Expand Down
9 changes: 9 additions & 0 deletions tests/Tests/SkiaSharp/SKDataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public void ValidDataProperties()
Assert.Equal(OddData, data.ToArray());
}

[SkippableFact]
public void ValidDataPropertiesWithSpan()
{
var data = SKData.CreateCopy(new ReadOnlySpan<byte>(OddData));

Assert.Equal(OddData.Length, data.Size);
Assert.Equal(OddData, data.ToArray());
}

[SkippableFact]
public void AsStreamReturnsCorrectStreamData()
{
Expand Down
30 changes: 30 additions & 0 deletions tests/Tests/SkiaSharp/SKImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ namespace SkiaSharp.Tests
{
public class SKImageTest : SKTest
{
[SkippableFact]
public void TestDecodeByteArray()
{
var data = SKData.Create(Path.Combine(PathToImages, "baboon.jpg"));
var image = SKImage.FromEncodedData(data.ToArray());
Assert.NotNull(image);
}

[SkippableFact]
public void TestDecodeSpan()
{
var data = SKData.Create(Path.Combine(PathToImages, "baboon.jpg"));
var image = SKImage.FromEncodedData(data.AsSpan());
Assert.NotNull(image);
}

[SkippableFact]
public void TestLazyImage()
{
Expand Down Expand Up @@ -208,6 +224,20 @@ public void TestFromPixelCopyByteArray()
}
}

[SkippableFact]
public unsafe void TestFromPixelCopySpan()
{
using (var bmp = CreateTestBitmap())
{
var px = bmp.GetPixels(out var length);
var dst = new Span<byte>((void*)px, (int)length);
using (var image = SKImage.FromPixelCopy(bmp.Info, dst))
{
ValidateTestPixmap(image.PeekPixels());
}
}
}

[SkippableFact]
public void TestFromPixelCopyStream()
{
Expand Down
32 changes: 31 additions & 1 deletion tests/Tests/SkiaSharp/SKRoundRectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void CanNotConstructRectWithInvalidRadii()
}

[SkippableFact]
public void CanConstructRectWithRadii()
public void CanConstructRectWithRadiiArray()
{
var rect = SKRect.Create(10, 10, 100, 100);
var radii = new[]
Expand Down Expand Up @@ -136,6 +136,36 @@ public void CanConstructRectWithRadii()
Assert.Equal(radii[3], rrect.GetRadii(SKRoundRectCorner.LowerLeft));
}

[SkippableFact]
public void CanConstructRectWithRadiiSpan()
{
var rect = SKRect.Create(10, 10, 100, 100);
Span<SKPoint> radii = stackalloc[]
{
new SKPoint(1, 2),
new SKPoint(3, 4),
new SKPoint(5, 6),
new SKPoint(7, 8)
};

var rrect = new SKRoundRect();
rrect.SetRectRadii(rect, radii);

Assert.NotNull(rrect);
Assert.True(rrect.IsValid);

Assert.Equal(SKRoundRectType.Complex, rrect.Type);

Assert.Equal(100f, rrect.Width);
Assert.Equal(100f, rrect.Height);
Assert.Equal(rect, rrect.Rect);

Assert.Equal(radii[0], rrect.GetRadii(SKRoundRectCorner.UpperLeft));
Assert.Equal(radii[1], rrect.GetRadii(SKRoundRectCorner.UpperRight));
Assert.Equal(radii[2], rrect.GetRadii(SKRoundRectCorner.LowerRight));
Assert.Equal(radii[3], rrect.GetRadii(SKRoundRectCorner.LowerLeft));
}

[SkippableFact]
public void CanCopy()
{
Expand Down