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

Implement SkCanvas::SaveLayerRec #2962

Open
wants to merge 3 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
42 changes: 35 additions & 7 deletions binding/SkiaSharp/SKCanvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,32 @@ public bool QuickReject (SKPath path)

// Save*

#nullable enable
public int Save ()
{
if (Handle == IntPtr.Zero)
throw new ObjectDisposedException ("SKCanvas");
return SkiaApi.sk_canvas_save (Handle);
}

public int SaveLayer (SKRect limit, SKPaint paint)
{
return SkiaApi.sk_canvas_save_layer (Handle, &limit, paint == null ? IntPtr.Zero : paint.Handle);
}
public int SaveLayer (SKRect limit, SKPaint? paint) =>
SkiaApi.sk_canvas_save_layer (Handle, &limit, paint?.Handle ?? IntPtr.Zero);

public int SaveLayer (SKPaint? paint) =>
SkiaApi.sk_canvas_save_layer (Handle, null, paint?.Handle ?? IntPtr.Zero);

public int SaveLayer (SKPaint paint)
public int SaveLayer (SKCanvasSaveLayerRec rec)
{
return SkiaApi.sk_canvas_save_layer (Handle, null, paint == null ? IntPtr.Zero : paint.Handle);
if (rec is null)
return SaveLayer ();

var native = rec.ToNative ();
return SkiaApi.sk_canvas_save_layer_rec (Handle, &native);
}

public int SaveLayer () =>
SaveLayer (null);
SkiaApi.sk_canvas_save_layer (Handle, null, IntPtr.Zero);
#nullable disable

// DrawColor

Expand Down Expand Up @@ -1080,4 +1087,25 @@ public void Restore ()
}
}
}

#nullable enable
public unsafe class SKCanvasSaveLayerRec
{
public SKRect? Bounds { get; set; }

public SKPaint? Paint { get; set; }

public SKImageFilter? Backdrop { get; set; }

public SKCanvasSaveLayerRecFlags Flags { get; set; }

internal SKCanvasSaveLayerRecNative ToNative () =>
new SKCanvasSaveLayerRecNative {
fBounds = Bounds is { } bounds ? &bounds : (SKRect*)null,
fPaint = Paint?.Handle ?? IntPtr.Zero,
fBackdrop = Backdrop?.Handle ?? IntPtr.Zero,
fFlags = Flags
};
}
#nullable disable
}
72 changes: 72 additions & 0 deletions binding/SkiaSharp/SkiaApi.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3088,6 +3088,25 @@ internal static Int32 sk_canvas_save_layer (sk_canvas_t ccanvas, SKRect* crect,
(sk_canvas_save_layer_delegate ??= GetSymbol<Delegates.sk_canvas_save_layer> ("sk_canvas_save_layer")).Invoke (ccanvas, crect, cpaint);
#endif

// int sk_canvas_save_layer_rec(sk_canvas_t* ccanvas, const sk_canvas_savelayerrec_t* crec)
#if !USE_DELEGATES
#if USE_LIBRARY_IMPORT
[LibraryImport (SKIA)]
internal static partial Int32 sk_canvas_save_layer_rec (sk_canvas_t ccanvas, SKCanvasSaveLayerRecNative* crec);
#else // !USE_LIBRARY_IMPORT
[DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)]
internal static extern Int32 sk_canvas_save_layer_rec (sk_canvas_t ccanvas, SKCanvasSaveLayerRecNative* crec);
#endif
#else
private partial class Delegates {
[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
internal delegate Int32 sk_canvas_save_layer_rec (sk_canvas_t ccanvas, SKCanvasSaveLayerRecNative* crec);
}
private static Delegates.sk_canvas_save_layer_rec sk_canvas_save_layer_rec_delegate;
internal static Int32 sk_canvas_save_layer_rec (sk_canvas_t ccanvas, SKCanvasSaveLayerRecNative* crec) =>
(sk_canvas_save_layer_rec_delegate ??= GetSymbol<Delegates.sk_canvas_save_layer_rec> ("sk_canvas_save_layer_rec")).Invoke (ccanvas, crec);
#endif

// void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy)
#if !USE_DELEGATES
#if USE_LIBRARY_IMPORT
Expand Down Expand Up @@ -17932,6 +17951,47 @@ public readonly override int GetHashCode ()

}

// sk_canvas_savelayerrec_t
[StructLayout (LayoutKind.Sequential)]
internal unsafe partial struct SKCanvasSaveLayerRecNative : IEquatable<SKCanvasSaveLayerRecNative> {
// public sk_rect_t* fBounds
public SKRect* fBounds;

// public sk_paint_t* fPaint
public sk_paint_t fPaint;

// public sk_imagefilter_t* fBackdrop
public sk_imagefilter_t fBackdrop;

// public sk_canvas_savelayerrec_flags_t fFlags
public SKCanvasSaveLayerRecFlags fFlags;

public readonly bool Equals (SKCanvasSaveLayerRecNative obj) =>
#pragma warning disable CS8909
fBounds == obj.fBounds && fPaint == obj.fPaint && fBackdrop == obj.fBackdrop && fFlags == obj.fFlags;
#pragma warning restore CS8909

public readonly override bool Equals (object obj) =>
obj is SKCanvasSaveLayerRecNative f && Equals (f);

public static bool operator == (SKCanvasSaveLayerRecNative left, SKCanvasSaveLayerRecNative right) =>
left.Equals (right);

public static bool operator != (SKCanvasSaveLayerRecNative left, SKCanvasSaveLayerRecNative right) =>
!left.Equals (right);

public readonly override int GetHashCode ()
{
var hash = new HashCode ();
hash.Add (fBounds);
hash.Add (fPaint);
hash.Add (fBackdrop);
hash.Add (fFlags);
return hash.ToHashCode ();
}

}

// sk_codec_frameinfo_t
[StructLayout (LayoutKind.Sequential)]
public unsafe partial struct SKCodecFrameInfo : IEquatable<SKCodecFrameInfo> {
Expand Down Expand Up @@ -20101,6 +20161,18 @@ public enum SKBlurStyle {
Inner = 3,
}

// sk_canvas_savelayerrec_flags_t
public enum SKCanvasSaveLayerRecFlags {
// NONE_SK_CANVAS_SAVELAYERREC_FLAGS = 0
None = 0,
// PRESERVE_LCD_TEXT_SK_CANVAS_SAVELAYERREC_FLAGS = 1 << 1
PreserveLcdText = 2,
// INITIALIZE_WITH_PREVIOUS_SK_CANVAS_SAVELAYERREC_FLAGS = 1 << 2
InitializeWithPrevious = 4,
// F16_COLOR_TYPE_SK_CANVAS_SAVELAYERREC_FLAGS = 1 << 4
F16ColorType = 16,
}

// sk_clipop_t
public enum SKClipOperation {
// DIFFERENCE_SK_CLIPOP = 0
Expand Down
7 changes: 7 additions & 0 deletions binding/libSkiaSharp.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@
"cs": "GRContextOptionsNative",
"internal": true
},
"sk_canvas_savelayerrec_t": {
"cs": "SKCanvasSaveLayerRecNative",
"internal": true
},
"sk_canvas_savelayerrec_flags_t": {
"cs": "SKCanvasSaveLayerRecFlags"
},
"sk_color4f_t": {
"cs": "SKColorF",
"readonly": true,
Expand Down
2 changes: 1 addition & 1 deletion externals/skia
54 changes: 54 additions & 0 deletions tests/Tests/SkiaSharp/SKCanvasTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,5 +689,59 @@ public void DrawPointReplacesColor(uint initial, uint color)
canvas.DrawPoint(0, 0, color);
Assert.Equal(color, bmp.GetPixel(0, 0));
}

[SkippableFact]
public void SaveLayerRecWithPaintIsCorrect()
{
using var bmp = new SKBitmap(100, 100);
using var canvas = new SKCanvas(bmp);
canvas.Clear(SKColors.Green);

var rec = new SKCanvasSaveLayerRec
{
Paint = new SKPaint
{
BlendMode = SKBlendMode.Plus
},
Flags = SKCanvasSaveLayerRecFlags.InitializeWithPrevious
};
canvas.SaveLayer(rec);

using var paint = new SKPaint
{
BlendMode = SKBlendMode.Clear
};
canvas.DrawCircle(50, 50, 40, paint);

canvas.Restore();

Assert.Equal(SKColors.Green, bmp.GetPixel(50, 50));
Assert.Equal(SKColors.Green, bmp.GetPixel(15, 50));
Assert.Equal((SKColor)0xFF00FF00, bmp.GetPixel(15, 15));
}

[SkippableFact]
public void SaveLayerRecWithImageFilterIsCorrect()
{
using var bmp = new SKBitmap(80, 80);
using var canvas = new SKCanvas(bmp);
canvas.Clear(SKColors.White);

canvas.DrawCircle(10, 10, 10, new SKPaint { Color = SKColors.Red });

var rec = new SKCanvasSaveLayerRec
{
Backdrop = SKImageFilter.CreateMatrix(SKMatrix.CreateScale(4, 4), SKSamplingOptions.Default),
};
canvas.SaveLayer(rec);

canvas.DrawCircle(40, 40, 10, new SKPaint { Color = SKColors.Green });

canvas.Restore();

Assert.Equal(SKColors.Red, bmp.GetPixel(25, 40));
Assert.Equal(SKColors.Red, bmp.GetPixel(40, 25));
Assert.Equal(SKColors.Green, bmp.GetPixel(40, 40));
}
}
}