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

Open
wants to merge 16 commits into
base: dev/reduce-arrays
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ However, these are easy to install as they are found on the various websites. If

Here are some links to show the differences in our code as compared to Google's code.

What version are we on? [**m88**](https://github.com/google/skia/tree/chrome/m88)
Are we up-to-date with Google? [Compare](https://github.com/mono/skia/compare/xamarin-mobile-bindings...google:chrome/m88)
What have we added? [Compare](https://github.com/google/skia/compare/chrome/m88...mono:xamarin-mobile-bindings)
What version are we on? [**m115**](https://github.com/google/skia/tree/chrome/m115)
Are we up-to-date with Google? [Compare](https://github.com/mono/skia/compare/skiasharp...google:chrome/m115)
What have we added? [Compare](https://github.com/google/skia/compare/chrome/m115...mono:skiasharp)
93 changes: 57 additions & 36 deletions binding/SkiaSharp/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public void DrawPath (SKPath path, SKPaint paint)

// DrawPoints

public void DrawPoints (SKPointMode mode, SKPoint[] points, SKPaint paint)
public void DrawPoints (SKPointMode mode, ReadOnlySpan<SKPoint> points, SKPaint paint)
{
if (paint == null)
throw new ArgumentNullException (nameof (paint));
Expand Down Expand Up @@ -603,28 +603,28 @@ public void DrawText (SKTextBlob text, float x, float y, SKPaint paint)

// DrawText

[Obsolete ("Use DrawText(string text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete ("Use DrawText(ReadOnlySpan<char> text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public void DrawText (string text, SKPoint p, SKPaint paint) =>
DrawText (text, p, paint.TextAlign, paint.GetFont (), paint);
DrawText (text.AsSpan (), p, paint.TextAlign, paint.GetFont (), paint);

[Obsolete ("Use DrawText(string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete ("Use DrawText(ReadOnlySpan<char> text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public void DrawText (string text, float x, float y, SKPaint paint) =>
DrawText (text, x, y, paint.TextAlign, paint.GetFont (), paint);
DrawText (text.AsSpan (), x, y, paint.TextAlign, paint.GetFont (), paint);

public void DrawText (string text, SKPoint p, SKFont font, SKPaint paint) =>
public void DrawText (ReadOnlySpan<char> text, SKPoint p, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
DrawText (text, p, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public void DrawText (string text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
public void DrawText (ReadOnlySpan<char> text, SKPoint p, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
DrawText (text, p.X, p.Y, textAlign, font, paint);

public void DrawText (string text, float x, float y, SKFont font, SKPaint paint) =>
public void DrawText (ReadOnlySpan<char> text, float x, float y, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
DrawText (text, x, y, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public void DrawText (string text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
public void DrawText (ReadOnlySpan<char> text, float x, float y, SKTextAlign textAlign, SKFont font, SKPaint paint)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
Expand All @@ -649,40 +649,40 @@ public void DrawText (string text, float x, float y, SKTextAlign textAlign, SKFo

// DrawTextOnPath

[Obsolete ("Use DrawTextOnPath(string text, SKPath path, float hOffset, float vOffset, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete ("Use DrawTextOnPath(ReadOnlySpan<char> text, SKPath path, float hOffset, float vOffset, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public void DrawTextOnPath (string text, SKPath path, SKPoint offset, SKPaint paint) =>
DrawTextOnPath (text, path, offset, true, paint);

[Obsolete ("Use DrawTextOnPath(string text, SKPath path, float hOffset, float vOffset, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete ("Use DrawTextOnPath(ReadOnlySpan<char> text, SKPath path, float hOffset, float vOffset, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public void DrawTextOnPath (string text, SKPath path, float hOffset, float vOffset, SKPaint paint) =>
DrawTextOnPath (text, path, new SKPoint (hOffset, vOffset), true, paint);

[Obsolete ("Use DrawTextOnPath(string text, SKPath path, SKPoint offset, bool warpGlyphs, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
[Obsolete ("Use DrawTextOnPath(ReadOnlySpan<char> text, SKPath path, SKPoint offset, bool warpGlyphs, SKTextAlign textAlign, SKFont font, SKPaint paint) instead.")]
public void DrawTextOnPath (string text, SKPath path, SKPoint offset, bool warpGlyphs, SKPaint paint) =>
DrawTextOnPath (text, path, offset, warpGlyphs, paint.GetFont (), paint);
DrawTextOnPath (text.AsSpan (), path, offset, warpGlyphs, paint.GetFont (), paint);

public void DrawTextOnPath (string text, SKPath path, SKPoint offset, SKFont font, SKPaint paint) =>
public void DrawTextOnPath (ReadOnlySpan<char> text, SKPath path, SKPoint offset, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
DrawTextOnPath (text, path, offset, true, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public void DrawTextOnPath (string text, SKPath path, SKPoint offset, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
public void DrawTextOnPath (ReadOnlySpan<char> text, SKPath path, SKPoint offset, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
DrawTextOnPath (text, path, offset, true, textAlign, font, paint);

public void DrawTextOnPath (string text, SKPath path, float hOffset, float vOffset, SKFont font, SKPaint paint) =>
public void DrawTextOnPath (ReadOnlySpan<char> text, SKPath path, float hOffset, float vOffset, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
DrawTextOnPath (text, path, new SKPoint (hOffset, vOffset), true, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public void DrawTextOnPath (string text, SKPath path, float hOffset, float vOffset, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
public void DrawTextOnPath (ReadOnlySpan<char> text, SKPath path, float hOffset, float vOffset, SKTextAlign textAlign, SKFont font, SKPaint paint) =>
DrawTextOnPath (text, path, new SKPoint (hOffset, vOffset), true, textAlign, font, paint);

public void DrawTextOnPath (string text, SKPath path, SKPoint offset, bool warpGlyphs, SKFont font, SKPaint paint) =>
public void DrawTextOnPath (ReadOnlySpan<char> text, SKPath path, SKPoint offset, bool warpGlyphs, SKFont font, SKPaint paint) =>
#pragma warning disable CS0618 // Type or member is obsolete (TODO: replace paint.TextAlign with SKTextAlign.Left)
DrawTextOnPath (text, path, offset, warpGlyphs, paint.TextAlign, font, paint);
#pragma warning restore CS0618 // Type or member is obsolete

public void DrawTextOnPath (string text, SKPath path, SKPoint offset, bool warpGlyphs, SKTextAlign textAlign, SKFont font, SKPaint paint)
public void DrawTextOnPath (ReadOnlySpan<char> text, SKPath path, SKPoint offset, bool warpGlyphs, SKTextAlign textAlign, SKFont font, SKPaint paint)
{
if (text == null)
throw new ArgumentNullException (nameof (text));
Expand Down Expand Up @@ -732,6 +732,13 @@ public SKData DrawUrlAnnotation (SKRect rect, string value)
return data;
}

public SKData DrawUrlAnnotation (SKRect rect, ReadOnlySpan<char> value)
{
var data = SKData.FromCString (value);
DrawUrlAnnotation (rect, data);
return data;
}

public void DrawNamedDestinationAnnotation (SKPoint point, SKData value)
{
SkiaApi.sk_canvas_draw_named_destination_annotation (Handle, &point, value == null ? IntPtr.Zero : value.Handle);
Expand All @@ -744,6 +751,13 @@ public SKData DrawNamedDestinationAnnotation (SKPoint point, string value)
return data;
}

public SKData DrawNamedDestinationAnnotation (SKPoint point, ReadOnlySpan<char> value)
{
var data = SKData.FromCString (value);
DrawNamedDestinationAnnotation (point, data);
return data;
}

public void DrawLinkDestinationAnnotation (SKRect rect, SKData value)
{
SkiaApi.sk_canvas_draw_link_destination_annotation (Handle, &rect, value == null ? IntPtr.Zero : value.Handle);
Expand All @@ -756,6 +770,13 @@ public SKData DrawLinkDestinationAnnotation (SKRect rect, string value)
return data;
}

public SKData DrawLinkDestinationAnnotation (SKRect rect, ReadOnlySpan<char> value)
{
var data = SKData.FromCString (value);
DrawLinkDestinationAnnotation (rect, data);
return data;
}

// Draw*NinePatch

public void DrawBitmapNinePatch (SKBitmap bitmap, SKRectI center, SKRect dst, SKPaint paint = null) =>
Expand Down Expand Up @@ -879,27 +900,27 @@ public void SetMatrix (in SKMatrix44 matrix)

// DrawVertices

public void DrawVertices (SKVertexMode vmode, SKPoint[] vertices, SKColor[] colors, SKPaint paint)
public void DrawVertices (SKVertexMode vmode, ReadOnlySpan<SKPoint> vertices, ReadOnlySpan<SKColor> colors, SKPaint paint)
{
var vert = SKVertices.CreateCopy (vmode, vertices, colors);
using var vert = SKVertices.CreateCopy (vmode, vertices, colors);
DrawVertices (vert, SKBlendMode.Modulate, paint);
}

public void DrawVertices (SKVertexMode vmode, SKPoint[] vertices, SKPoint[] texs, SKColor[] colors, SKPaint paint)
public void DrawVertices (SKVertexMode vmode, ReadOnlySpan<SKPoint> vertices, ReadOnlySpan<SKPoint> texs, ReadOnlySpan<SKColor> colors, SKPaint paint)
{
var vert = SKVertices.CreateCopy (vmode, vertices, texs, colors);
using var vert = SKVertices.CreateCopy (vmode, vertices, texs, colors);
DrawVertices (vert, SKBlendMode.Modulate, paint);
}

public void DrawVertices (SKVertexMode vmode, SKPoint[] vertices, SKPoint[] texs, SKColor[] colors, UInt16[] indices, SKPaint paint)
public void DrawVertices (SKVertexMode vmode, ReadOnlySpan<SKPoint> vertices, ReadOnlySpan<SKPoint> texs, ReadOnlySpan<SKColor> colors, ReadOnlySpan<UInt16> indices, SKPaint paint)
{
var vert = SKVertices.CreateCopy (vmode, vertices, texs, colors, indices);
using var vert = SKVertices.CreateCopy (vmode, vertices, texs, colors, indices);
DrawVertices (vert, SKBlendMode.Modulate, paint);
}

public void DrawVertices (SKVertexMode vmode, SKPoint[] vertices, SKPoint[] texs, SKColor[] colors, SKBlendMode mode, UInt16[] indices, SKPaint paint)
public void DrawVertices (SKVertexMode vmode, ReadOnlySpan<SKPoint> vertices, ReadOnlySpan<SKPoint> texs, ReadOnlySpan<SKColor> colors, SKBlendMode mode, ReadOnlySpan<UInt16> indices, SKPaint paint)
{
var vert = SKVertices.CreateCopy (vmode, vertices, texs, colors, indices);
using var vert = SKVertices.CreateCopy (vmode, vertices, texs, colors, indices);
DrawVertices (vert, mode, paint);
}

Expand Down Expand Up @@ -937,25 +958,25 @@ public void DrawRoundRectDifference (SKRoundRect outer, SKRoundRect inner, SKPai

// DrawAtlas

public void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[] transforms, SKPaint paint) =>
public void DrawAtlas (SKImage atlas, ReadOnlySpan<SKRect> sprites, ReadOnlySpan<SKRotationScaleMatrix> transforms, SKPaint paint) =>
DrawAtlas (atlas, sprites, transforms, null, SKBlendMode.Dst, SKSamplingOptions.Default, null, paint);

public void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[] transforms, SKSamplingOptions sampling, SKPaint paint) =>
public void DrawAtlas (SKImage atlas, ReadOnlySpan<SKRect> sprites, ReadOnlySpan<SKRotationScaleMatrix> transforms, SKSamplingOptions sampling, SKPaint paint) =>
DrawAtlas (atlas, sprites, transforms, null, SKBlendMode.Dst, sampling, null, paint);

public void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[] transforms, SKColor[] colors, SKBlendMode mode, SKPaint paint) =>
public void DrawAtlas (SKImage atlas, ReadOnlySpan<SKRect> sprites, ReadOnlySpan<SKRotationScaleMatrix> transforms, ReadOnlySpan<SKColor> colors, SKBlendMode mode, SKPaint paint) =>
DrawAtlas (atlas, sprites, transforms, colors, mode, SKSamplingOptions.Default, null, paint);

public void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[] transforms, SKColor[] colors, SKBlendMode mode, SKSamplingOptions sampling, SKPaint paint) =>
public void DrawAtlas (SKImage atlas, ReadOnlySpan<SKRect> sprites, ReadOnlySpan<SKRotationScaleMatrix> transforms, ReadOnlySpan<SKColor> colors, SKBlendMode mode, SKSamplingOptions sampling, SKPaint paint) =>
DrawAtlas (atlas, sprites, transforms, colors, mode, sampling, null, paint);

public void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[] transforms, SKColor[] colors, SKBlendMode mode, SKRect cullRect, SKPaint paint) =>
public void DrawAtlas (SKImage atlas, ReadOnlySpan<SKRect> sprites, ReadOnlySpan<SKRotationScaleMatrix> transforms, ReadOnlySpan<SKColor> colors, SKBlendMode mode, SKRect cullRect, SKPaint paint) =>
DrawAtlas (atlas, sprites, transforms, colors, mode, SKSamplingOptions.Default, &cullRect, paint);

public void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[] transforms, SKColor[] colors, SKBlendMode mode, SKSamplingOptions sampling, SKRect cullRect, SKPaint paint) =>
public void DrawAtlas (SKImage atlas, ReadOnlySpan<SKRect> sprites, ReadOnlySpan<SKRotationScaleMatrix> transforms, ReadOnlySpan<SKColor> colors, SKBlendMode mode, SKSamplingOptions sampling, SKRect cullRect, SKPaint paint) =>
DrawAtlas (atlas, sprites, transforms, colors, mode, sampling, &cullRect, paint);

private void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[] transforms, SKColor[] colors, SKBlendMode mode, SKSamplingOptions sampling, SKRect* cullRect, SKPaint paint)
private void DrawAtlas (SKImage atlas, ReadOnlySpan<SKRect> sprites, ReadOnlySpan<SKRotationScaleMatrix> transforms, ReadOnlySpan<SKColor> colors, SKBlendMode mode, SKSamplingOptions sampling, SKRect* cullRect, SKPaint paint)
{
if (atlas == null)
throw new ArgumentNullException (nameof (atlas));
Expand All @@ -978,10 +999,10 @@ private void DrawAtlas (SKImage atlas, SKRect[] sprites, SKRotationScaleMatrix[]

// DrawPatch

public void DrawPatch (SKPoint[] cubics, SKColor[] colors, SKPoint[] texCoords, SKPaint paint) =>
public void DrawPatch (ReadOnlySpan<SKPoint> cubics, ReadOnlySpan<SKColor> colors, ReadOnlySpan<SKPoint> texCoords, SKPaint paint) =>
DrawPatch (cubics, colors, texCoords, SKBlendMode.Modulate, paint);

public void DrawPatch (SKPoint[] cubics, SKColor[] colors, SKPoint[] texCoords, SKBlendMode mode, SKPaint paint)
public void DrawPatch (ReadOnlySpan<SKPoint> cubics, ReadOnlySpan<SKColor> colors, ReadOnlySpan<SKPoint> texCoords, SKBlendMode mode, SKPaint paint)
{
if (cubics == null)
throw new ArgumentNullException (nameof (cubics));
Expand Down
2 changes: 1 addition & 1 deletion binding/SkiaSharp/SKCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public SKCodecResult GetPixels (SKImageInfo info, out byte[] pixels)
return GetPixels (info, pixels);
}

public SKCodecResult GetPixels (SKImageInfo info, byte[] pixels)
public SKCodecResult GetPixels (SKImageInfo info, Span<byte> pixels)
{
if (pixels == null)
throw new ArgumentNullException (nameof (pixels));
Expand Down
10 changes: 3 additions & 7 deletions binding/SkiaSharp/SKColorFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ public static SKColorFilter CreateCompose(SKColorFilter outer, SKColorFilter inn
return GetObject (SkiaApi.sk_colorfilter_new_compose(outer.Handle, inner.Handle));
}

public static SKColorFilter CreateColorMatrix(float[] matrix)
public static SKColorFilter CreateColorMatrix (ReadOnlySpan<float> matrix)
{
if (matrix == null)
throw new ArgumentNullException(nameof(matrix));
if (matrix.Length != 20)
throw new ArgumentException("Matrix must have a length of 20.", nameof(matrix));
fixed (float* m = matrix) {
Expand All @@ -54,18 +52,16 @@ public static SKColorFilter CreateLumaColor()
return GetObject (SkiaApi.sk_colorfilter_new_luma_color());
}

public static SKColorFilter CreateTable(byte[] table)
public static SKColorFilter CreateTable (ReadOnlySpan<byte> table)
{
if (table == null)
throw new ArgumentNullException(nameof(table));
if (table.Length != TableMaxLength)
throw new ArgumentException($"Table must have a length of {TableMaxLength}.", nameof(table));
fixed (byte* t = table) {
return GetObject (SkiaApi.sk_colorfilter_new_table (t));
}
}

public static SKColorFilter CreateTable(byte[] tableA, byte[] tableR, byte[] tableG, byte[] tableB)
public static SKColorFilter CreateTable (ReadOnlySpan<byte> tableA, ReadOnlySpan<byte> tableR, ReadOnlySpan<byte> tableG, ReadOnlySpan<byte> tableB)
{
if (tableA != null && tableA.Length != TableMaxLength)
throw new ArgumentException($"Table A must have a length of {TableMaxLength}.", nameof(tableA));
Expand Down
5 changes: 4 additions & 1 deletion binding/SkiaSharp/SKColorSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public static bool Equal (SKColorSpace left, SKColorSpace right)
public static SKColorSpace CreateIcc (IntPtr input, long length) =>
CreateIcc (SKColorSpaceIccProfile.Create (input, length));

public static SKColorSpace CreateIcc (byte[] input, long length)
public static SKColorSpace CreateIcc (byte[] input, long length) =>
CreateIcc (input.AsSpan (), length);

public static SKColorSpace CreateIcc (ReadOnlySpan<byte> input, long length)
{
if (input == null)
throw new ArgumentNullException (nameof (input));
Expand Down