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

More Span<T> and ReadOnlySpan<T> #2669

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e0de203
Use spans instead of arrays
mattleibow Sep 18, 2023
3a77f16
Add some tests
mattleibow Sep 18, 2023
7741e78
Convert many arrays to spans, or add span overloads where array overl…
ScrubN Nov 10, 2023
c87b9fa
Remove some unnecessary null checks.
ScrubN Nov 10, 2023
8d4bf7f
Fix duplicate SetRectRadii overloads
ScrubN Nov 6, 2024
8f77fcc
Swap out `new T[0]` for `Array.Empty<T> ()`
ScrubN Nov 10, 2023
8428942
Fix apparent memory leak when drawing vertices
ScrubN Nov 10, 2023
9f6f9ce
Minor memory improvements
ScrubN Nov 10, 2023
a70e287
Fix typo
ScrubN Nov 13, 2023
cc3fe2f
Add ReadOnlySpan<char> overloads to SKShaper.Shape and convert DrawSh…
ScrubN Nov 14, 2023
d4985d3
Add tests for SKShaper.Shape with ReadOnlySpan<char>
ScrubN Nov 14, 2023
47926c8
Minor adjustments
ScrubN Nov 14, 2023
535d4e3
Add some span-array overload comparison tests
ScrubN Nov 14, 2023
175fa00
Convert LINQ to code
ScrubN Nov 7, 2024
2b32428
Restore some array overloads & reduce duplicate code between span and…
ScrubN Nov 7, 2024
53d4f74
Restore more array overloads & add some missing span overloads
ScrubN Nov 7, 2024
8bb55f1
Fix method order to preserve blame
ScrubN Nov 7, 2024
1ae486a
Almost done restoring array methods
ScrubN Nov 7, 2024
f1336f0
Fix recursive method
ScrubN Nov 7, 2024
34b0dd6
Fix build
ScrubN Nov 7, 2024
3d355e3
Revert changes to SKStream
ScrubN Nov 7, 2024
590f43d
Restore even more array/string overloads
ScrubN Nov 7, 2024
27bd94d
Add missing DrawText* span overloads
ScrubN Nov 7, 2024
47479bf
Cleanup
ScrubN Nov 7, 2024
b38d0d9
Fix SKColorSpaceTransferFn.SetValues length check
ScrubN Dec 2, 2024
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
Prev Previous commit
Next Next commit
Almost done restoring array methods
  • Loading branch information
ScrubN committed Nov 7, 2024
commit 1ae486a3a8ac468f5034a36548465d5aad7a1e58
2 changes: 1 addition & 1 deletion binding/SkiaSharp/SKData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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));
return CreateCopy ((IntPtr)b, (ulong)bytes.Length);
}
}

Expand Down
12 changes: 9 additions & 3 deletions binding/SkiaSharp/SKImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,22 @@ public static SKImage FromEncodedData (SKData data)

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

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

public static SKImage FromEncodedData (byte[] data)
{
if (data == null)
throw new ArgumentNullException (nameof (data));

return FromEncodedData (data.AsSpan ());
}

public static SKImage FromEncodedData (SKStream data)
{
if (data == null)
Expand Down
22 changes: 14 additions & 8 deletions binding/SkiaSharp/SKPMColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ public SKPMColor (uint value)
public static SKPMColor PreMultiply (SKColor color) =>
SkiaApi.sk_color_premultiply ((uint)color);

public static SKPMColor[] PreMultiply (SKColor[] colors) =>
PreMultiply (colors.AsSpan ());

public static SKPMColor[] PreMultiply (ReadOnlySpan<SKColor> colors)
{
var pmcolors = new SKPMColor[colors.Length];
fixed (SKColor* c = colors)
fixed (SKPMColor* pm = pmcolors) {
SkiaApi.sk_color_premultiply_array ((uint*)c, colors.Length, (uint*)pm);
}
PreMultiply (pmcolors.AsSpan (), colors);
return pmcolors;
}

public static void PreMultiply (SKPMColor[] pmcolors, SKColor[] colors) =>
PreMultiply (pmcolors.AsSpan (), colors.AsSpan ());

public static void PreMultiply (Span<SKPMColor> pmcolors, ReadOnlySpan<SKColor> colors)
{
if (pmcolors.Length != colors.Length)
Expand All @@ -48,16 +51,19 @@ public static void PreMultiply (Span<SKPMColor> pmcolors, ReadOnlySpan<SKColor>
public static SKColor UnPreMultiply (SKPMColor pmcolor) =>
SkiaApi.sk_color_unpremultiply ((uint)pmcolor);

public static SKColor[] UnPreMultiply (SKPMColor[] pmcolors) =>
UnPreMultiply (pmcolors.AsSpan ());

public static SKColor[] UnPreMultiply (ReadOnlySpan<SKPMColor> pmcolors)
{
var colors = new SKColor[pmcolors.Length];
fixed (SKColor* c = colors)
fixed (SKPMColor* pm = pmcolors) {
SkiaApi.sk_color_unpremultiply_array ((uint*)pm, pmcolors.Length, (uint*)c);
}
UnPreMultiply (colors.AsSpan (), pmcolors);
return colors;
}

public static void UnPreMultiply (SKColor[] colors, SKPMColor[] pmcolors) =>
UnPreMultiply (colors.AsSpan (), pmcolors.AsSpan ());

public static void UnPreMultiply (Span<SKColor> colors, ReadOnlySpan<SKPMColor> pmcolors)
{
if (colors.Length != pmcolors.Length)
Expand Down
8 changes: 7 additions & 1 deletion binding/SkiaSharp/SKPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ public SKPoint[] GetPoints (int max)
return points;
}

public int GetPoints (SKPoint[] points, int max) =>
GetPoints (points.AsSpan (), max);

public int GetPoints (Span<SKPoint> points, int max)
{
if (points == null)
Expand Down Expand Up @@ -524,9 +527,12 @@ public static int ConvertConicToQuads (SKPoint p0, SKPoint p1, SKPoint p2, float
var quadCount = 1 << pow2;
var ptCount = 2 * quadCount + 1;
pts = new SKPoint[ptCount];
return ConvertConicToQuads (p0, p1, p2, w, new Span<SKPoint> (pts), pow2);
return ConvertConicToQuads (p0, p1, p2, w, pts.AsSpan (), pow2);
}

public static int ConvertConicToQuads (SKPoint p0, SKPoint p1, SKPoint p2, float w, SKPoint[] pts, int pow2) =>
ConvertConicToQuads (p0, p1, p2, w, pts.AsSpan (), pow2);

public static int ConvertConicToQuads (SKPoint p0, SKPoint p1, SKPoint p2, float w, Span<SKPoint> pts, int pow2)
{
if (pts == null)
Expand Down