Skip to content

Fix the SKImage.FromPicture implementation #3231

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

Merged
merged 3 commits into from
Apr 10, 2025
Merged
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
12 changes: 7 additions & 5 deletions binding/SkiaSharp/SKImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,24 +330,26 @@ public static SKImage FromAdoptedTexture (GRRecordingContext context, GRBackendT
// create a new image from a picture

public static SKImage FromPicture (SKPicture picture, SKSizeI dimensions) =>
FromPicture (picture, dimensions, null, null, false, null, null);
FromPicture (picture, dimensions, null, null, false, SKColorSpace.CreateSrgb (), null);

public static SKImage FromPicture (SKPicture picture, SKSizeI dimensions, SKMatrix matrix) =>
FromPicture (picture, dimensions, &matrix, null, false, null, null);
FromPicture (picture, dimensions, &matrix, null, false, SKColorSpace.CreateSrgb (), null);

public static SKImage FromPicture (SKPicture picture, SKSizeI dimensions, SKPaint paint) =>
FromPicture (picture, dimensions, null, paint, false, null, null);
FromPicture (picture, dimensions, null, paint, false, SKColorSpace.CreateSrgb (), null);

public static SKImage FromPicture (SKPicture picture, SKSizeI dimensions, SKMatrix matrix, SKPaint paint) =>
FromPicture (picture, dimensions, &matrix, paint, false, null, null);
FromPicture (picture, dimensions, &matrix, paint, false, SKColorSpace.CreateSrgb (), null);

private static SKImage FromPicture (SKPicture picture, SKSizeI dimensions, SKMatrix* matrix, SKPaint paint, bool useFloatingPointBitDepth, SKColorSpace colorspace, SKSurfaceProperties props)
{
if (picture == null)
throw new ArgumentNullException (nameof (picture));

var p = paint?.Handle ?? IntPtr.Zero;
return GetObject (SkiaApi.sk_image_new_from_picture (picture.Handle, &dimensions, matrix, p, useFloatingPointBitDepth, colorspace?.Handle ?? IntPtr.Zero, props?.Handle ?? IntPtr.Zero));
var cs = colorspace?.Handle ?? IntPtr.Zero;
var prps = props?.Handle ?? IntPtr.Zero;
return GetObject (SkiaApi.sk_image_new_from_picture (picture.Handle, &dimensions, matrix, p, useFloatingPointBitDepth, cs, prps));
}

public SKData Encode ()
Expand Down
2 changes: 1 addition & 1 deletion externals/skia
Submodule skia updated 1 files
+1 −1 src/c/sk_image.cpp
13 changes: 13 additions & 0 deletions tests/Tests/SkiaSharp/SKImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -925,5 +925,18 @@ public void CannotCreateRawShaderWithBicubicSampling()
using var shader = image.ToRawShader(SKShaderTileMode.Clamp, SKShaderTileMode.Clamp, sampling);
Assert.Null(shader);
}

[SkippableFact]
public void FromPictureDoesNotCrash()
{
using var picture = CreateTestPicture();

using var image = SKImage.FromPicture(picture, new SKSizeI(40, 40));

using var data = image.Encode(SKEncodedImageFormat.Png, 100);
using var bmp = SKBitmap.Decode(data);

ValidateTestBitmap(bmp);
}
}
}
2 changes: 1 addition & 1 deletion tests/Tests/SkiaSharp/SKTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected static SKPicture CreateTestPicture(byte alpha = 255)
return recorder.EndRecording();
}

private static void DrawTestBitmap(SKCanvas canvas, int width, int height, byte alpha = 255)
protected static void DrawTestBitmap(SKCanvas canvas, int width, int height, byte alpha = 255)
{
using var paint = new SKPaint();

Expand Down