From 44304c269238bf891122ceb1abcfd226fad22379 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Mon, 18 Sep 2023 21:21:00 +0200 Subject: [PATCH 1/2] Use spans instead of arrays --- binding/SkiaSharp/SKData.cs | 11 ++--------- binding/SkiaSharp/SKImage.cs | 24 ------------------------ binding/SkiaSharp/SKPathEffect.cs | 2 +- binding/SkiaSharp/SKRoundRect.cs | 2 +- 4 files changed, 4 insertions(+), 35 deletions(-) diff --git a/binding/SkiaSharp/SKData.cs b/binding/SkiaSharp/SKData.cs index 5bb53d47ca..e0ef2e59d0 100644 --- a/binding/SkiaSharp/SKData.cs +++ b/binding/SkiaSharp/SKData.cs @@ -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 bytes) => CreateCopy (bytes, (ulong)bytes.Length); - public static SKData CreateCopy (byte[] bytes, ulong length) + public static SKData CreateCopy (ReadOnlySpan bytes, ulong length) { fixed (byte* b = bytes) { return GetObject (SkiaApi.sk_data_new_with_copy (b, (IntPtr)length)); } } - public static SKData CreateCopy (ReadOnlySpan bytes) - { - fixed (byte* b = bytes) { - return CreateCopy ((IntPtr)b, (ulong)bytes.Length); - } - } - // Create public static SKData Create (int size) => diff --git a/binding/SkiaSharp/SKImage.cs b/binding/SkiaSharp/SKImage.cs index 834b05519b..bfc35a770f 100644 --- a/binding/SkiaSharp/SKImage.cs +++ b/binding/SkiaSharp/SKImage.cs @@ -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); @@ -183,18 +171,6 @@ public static SKImage FromEncodedData (ReadOnlySpan 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) diff --git a/binding/SkiaSharp/SKPathEffect.cs b/binding/SkiaSharp/SKPathEffect.cs index d21d3d7cc4..d668dba370 100644 --- a/binding/SkiaSharp/SKPathEffect.cs +++ b/binding/SkiaSharp/SKPathEffect.cs @@ -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 intervals, float phase) { if (intervals == null) throw new ArgumentNullException(nameof(intervals)); diff --git a/binding/SkiaSharp/SKRoundRect.cs b/binding/SkiaSharp/SKRoundRect.cs index 1238cfe1c9..399590fb64 100644 --- a/binding/SkiaSharp/SKRoundRect.cs +++ b/binding/SkiaSharp/SKRoundRect.cs @@ -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 radii) { if (radii == null) throw new ArgumentNullException (nameof (radii)); From ab998a32e2d8d20ab6f1b75e9ed61e3fe5e058e0 Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Mon, 18 Sep 2023 20:19:26 +0000 Subject: [PATCH 2/2] Add some tests --- tests/Tests/SkiaSharp/SKDataTest.cs | 9 +++++++ tests/Tests/SkiaSharp/SKImageTest.cs | 30 ++++++++++++++++++++++ tests/Tests/SkiaSharp/SKRoundRectTest.cs | 32 +++++++++++++++++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/tests/Tests/SkiaSharp/SKDataTest.cs b/tests/Tests/SkiaSharp/SKDataTest.cs index b064bd3497..16b5fb7dd3 100644 --- a/tests/Tests/SkiaSharp/SKDataTest.cs +++ b/tests/Tests/SkiaSharp/SKDataTest.cs @@ -37,6 +37,15 @@ public void ValidDataProperties() Assert.Equal(OddData, data.ToArray()); } + [SkippableFact] + public void ValidDataPropertiesWithSpan() + { + var data = SKData.CreateCopy(new ReadOnlySpan(OddData)); + + Assert.Equal(OddData.Length, data.Size); + Assert.Equal(OddData, data.ToArray()); + } + [SkippableFact] public void AsStreamReturnsCorrectStreamData() { diff --git a/tests/Tests/SkiaSharp/SKImageTest.cs b/tests/Tests/SkiaSharp/SKImageTest.cs index ce350854a3..01759f13d9 100644 --- a/tests/Tests/SkiaSharp/SKImageTest.cs +++ b/tests/Tests/SkiaSharp/SKImageTest.cs @@ -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() { @@ -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((void*)px, (int)length); + using (var image = SKImage.FromPixelCopy(bmp.Info, dst)) + { + ValidateTestPixmap(image.PeekPixels()); + } + } + } + [SkippableFact] public void TestFromPixelCopyStream() { diff --git a/tests/Tests/SkiaSharp/SKRoundRectTest.cs b/tests/Tests/SkiaSharp/SKRoundRectTest.cs index 5a2e9f4944..64e537465b 100644 --- a/tests/Tests/SkiaSharp/SKRoundRectTest.cs +++ b/tests/Tests/SkiaSharp/SKRoundRectTest.cs @@ -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[] @@ -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 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() {