From f8a11e8f8b537a41a907097e0384ecd4bd36111a Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Fri, 9 Feb 2024 01:48:28 +0200 Subject: [PATCH] Add SKImage.ToRawShader --- binding/SkiaSharp/SKImage.cs | 20 ++++++++++++++++++++ binding/SkiaSharp/SkiaApi.generated.cs | 14 ++++++++++++++ externals/skia | 2 +- tests/Tests/SkiaSharp/SKImageTest.cs | 25 +++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/binding/SkiaSharp/SKImage.cs b/binding/SkiaSharp/SKImage.cs index 834b05519b5..4f4f3ff6125 100644 --- a/binding/SkiaSharp/SKImage.cs +++ b/binding/SkiaSharp/SKImage.cs @@ -415,6 +415,26 @@ public SKShader ToShader (SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamp private SKShader ToShader (SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamplingOptions sampling, SKMatrix* localMatrix) => SKShader.GetObject (SkiaApi.sk_image_make_shader (Handle, tileX, tileY, &sampling, localMatrix)); + // ToRawShader + + public SKShader ToRawShader () => + ToRawShader (SKShaderTileMode.Clamp, SKShaderTileMode.Clamp, SKSamplingOptions.Default, null); + + public SKShader ToRawShader (SKShaderTileMode tileX, SKShaderTileMode tileY) => + ToRawShader (tileX, tileY, SKSamplingOptions.Default, null); + + public SKShader ToRawShader (SKShaderTileMode tileX, SKShaderTileMode tileY, SKMatrix localMatrix) => + ToRawShader (tileX, tileY, SKSamplingOptions.Default, &localMatrix); + + public SKShader ToRawShader (SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamplingOptions sampling) => + ToRawShader (tileX, tileY, sampling, null); + + public SKShader ToRawShader (SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamplingOptions sampling, SKMatrix localMatrix) => + ToRawShader (tileX, tileY, sampling, &localMatrix); + + private SKShader ToRawShader (SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamplingOptions sampling, SKMatrix* localMatrix) => + SKShader.GetObject (SkiaApi.sk_image_make_raw_shader (Handle, tileX, tileY, &sampling, localMatrix)); + // PeekPixels public bool PeekPixels (SKPixmap pixmap) diff --git a/binding/SkiaSharp/SkiaApi.generated.cs b/binding/SkiaSharp/SkiaApi.generated.cs index 662eb8cb8ce..96d4351d7ea 100644 --- a/binding/SkiaSharp/SkiaApi.generated.cs +++ b/binding/SkiaSharp/SkiaApi.generated.cs @@ -4960,6 +4960,20 @@ internal static sk_shader_t sk_image_make_shader (sk_image_t image, SKShaderTile (sk_image_make_shader_delegate ??= GetSymbol ("sk_image_make_shader")).Invoke (image, tileX, tileY, sampling, cmatrix); #endif + // sk_shader_t* sk_image_make_raw_shader(const sk_image_t* image, sk_shader_tilemode_t tileX, sk_shader_tilemode_t tileY, const sk_sampling_options_t* sampling, const sk_matrix_t* cmatrix) + #if !USE_DELEGATES + [DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)] + internal static extern sk_shader_t sk_image_make_raw_shader (sk_image_t image, SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamplingOptions* sampling, SKMatrix* cmatrix); + #else + private partial class Delegates { + [UnmanagedFunctionPointer (CallingConvention.Cdecl)] + internal delegate sk_shader_t sk_image_make_raw_shader (sk_image_t image, SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamplingOptions* sampling, SKMatrix* cmatrix); + } + private static Delegates.sk_image_make_raw_shader sk_image_make_raw_shader_delegate; + internal static sk_shader_t sk_image_make_raw_shader (sk_image_t image, SKShaderTileMode tileX, SKShaderTileMode tileY, SKSamplingOptions* sampling, SKMatrix* cmatrix) => + (sk_image_make_raw_shader_delegate ??= GetSymbol ("sk_image_make_raw_shader")).Invoke (image, tileX, tileY, sampling, cmatrix); + #endif + // sk_image_t* sk_image_make_subset(const sk_image_t* cimage, gr_direct_context_t* context, const sk_irect_t* subset) #if !USE_DELEGATES [DllImport (SKIA, CallingConvention = CallingConvention.Cdecl)] diff --git a/externals/skia b/externals/skia index 519e4b30258..07f48574d03 160000 --- a/externals/skia +++ b/externals/skia @@ -1 +1 @@ -Subproject commit 519e4b302588722e71ca71c2531749747a789cf0 +Subproject commit 07f48574d0357418e4cb58f8614428a1b6fc8781 diff --git a/tests/Tests/SkiaSharp/SKImageTest.cs b/tests/Tests/SkiaSharp/SKImageTest.cs index ce350854a32..e8942b5ef28 100644 --- a/tests/Tests/SkiaSharp/SKImageTest.cs +++ b/tests/Tests/SkiaSharp/SKImageTest.cs @@ -900,5 +900,30 @@ public void CanDecodePotentiallyCorruptPngFiles(string filename, int x, int y, u Assert.Equal((SKColor)color, pixmap.GetPixelColor(x, y)); } + + [SkippableFact] + public void CanCreateRawShader() + { + var path = Path.Combine(PathToImages, "baboon.png"); + + using var image = SKImage.FromEncodedData(path); + Assert.NotNull(image); + + using var shader = image.ToRawShader(); + Assert.NotNull(shader); + } + + [SkippableFact] + public void CannotCreateRawShaderWithBicubicSampling() + { + var path = Path.Combine(PathToImages, "baboon.png"); + + using var image = SKImage.FromEncodedData(path); + Assert.NotNull(image); + + var sampling = new SKSamplingOptions(SKCubicResampler.Mitchell); + using var shader = image.ToRawShader(SKShaderTileMode.Clamp, SKShaderTileMode.Clamp, sampling); + Assert.Null(shader); + } } }