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

Use newer SkiaSharp APIs #216

Merged
merged 8 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion build/SkiaSharp.HarfBuzz.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.6" />
<PackageReference Include="SkiaSharp.HarfBuzz" Version="2.88.8-preview.1.1" />
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion build/SkiaSharp.Linux.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.8-preview.1.1" />
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion build/SkiaSharp.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.6" />
<PackageReference Include="SkiaSharp" Version="2.88.8-preview.1.1" />
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now the NuGet is just a preview, but once I manage to get all the other things ready and it lives for a few days and gets a few download I will push it stable.

mattleibow marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions src/ShimSkiaSharp/SKImageFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public static SKImageFilter CreateOffset(float dx, float dy, SKImageFilter? inpu
public static SKImageFilter CreatePaint(SKPaint paint, CropRect? cropRect = null)
=> new PaintImageFilter(paint, cropRect);

public static SKImageFilter CreateShader(SKShader shader, bool dither, CropRect? cropRect = null)
=> new ShaderImageFilter(shader, dither, cropRect);
Comment on lines +55 to +56
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shader filter is the new filter that is preferred over paint filter. According to Google: google/skia@7d0f853

SkImageFilters::Paint did not use every slot of the SkPaint, with only
its color, alpha, color filter, and shader having a meaningful effect on
the image filter result. It was always blended into a transparent dst,
so blend mode wasn't very relevant, and it was always filled to whatever
required geometry, so stroke style, path effect, and mask filters were
ignored or not well specified.

Color, alpha, and color filter can all be combined into an SkShader, so
a more constrained SkImageFilters::Shader provides the same useful
capabilities without as many surprises.

The Paint filter right now may do extra things, but it appears that all the things can be done via merged filters. For example, a fill image/color can be a done with an Image/Color shader. I am not too familiar with how the image filters get created in Svg.Skia and all the potential caveats so I did not feel too comfortable switching just yet.

In 2.x, the Shader filter is really a paint filter with the shader set so should be identical to 3.x Shader. In 3.x, the Paint filter is really a lie and is just the Shader from the paint, so the behavior might be different. https://github.com/mono/SkiaSharp/pull/2789/files#diff-a167a40fc94fe828c0c2d37f6e69d29949af733c40bf8a8b632223a277b2f712R426

I may try be smarter and detect if you just have a Shader, then use a Shader filter, if you just have a Color or something then use that filter type. However, it may be better to just do it right in the caller so SkiaSharp does not do weird things.

If you have ideas on this, I can try and make the compat even more awesome.


public static SKImageFilter CreatePicture(SKPicture picture, SKRect cropRect)
=> new PictureImageFilter(picture, cropRect);

Expand Down Expand Up @@ -99,6 +102,8 @@ public record OffsetImageFilter(float Dx, float Dy, SKImageFilter? Input, SKImag

public record PaintImageFilter(SKPaint? Paint, SKImageFilter.CropRect? Clip) : SKImageFilter;

public record ShaderImageFilter(SKShader? Shader, bool Dither, SKImageFilter.CropRect? Clip) : SKImageFilter;

public record PictureImageFilter(SKPicture? Picture, SKRect? Clip) : SKImageFilter;

public record PointLitDiffuseImageFilter(SKPoint3 Location, SKColor LightColor, float SurfaceScale, float Kd, SKImageFilter? Input, SKImageFilter.CropRect? Clip) : SKImageFilter;
Expand Down
20 changes: 20 additions & 0 deletions src/Svg.CodeGen.Skia/SkiaCSharpModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,26 @@ public static void ToSKImageFilter(this SKImageFilter? imageFilter, SkiaCSharpCo
sb.AppendLine($"{indent}{counter.PaintVarName}{counterPaint}?.Dispose();");
return;
}
case ShaderImageFilter shaderImageFilter:
{
if (shaderImageFilter.Shader is null)
{
sb.AppendLine($"{indent}var {counter.ImageFilterVarName}{counterImageFilter} = default(SKImageFilter);");
return;
}

var counterShader = ++counter.Shader;
shaderImageFilter.Shader.ToSKShader(counter, sb, indent);

sb.Append($"{indent}var {counter.ImageFilterVarName}{counterImageFilter} = ");
sb.AppendLine($"SKImageFilter.CreateShader(");
sb.AppendLine($"{indent} {counter.ShaderVarName}{counterShader},");
sb.AppendLine($"{indent} {shaderImageFilter.Dither.ToBoolString()},");
sb.AppendLine($"{indent} {shaderImageFilter.Clip?.ToCropRect() ?? "null"});");

sb.AppendLine($"{indent}{counter.ShaderVarName}{counterShader}?.Dispose();");
return;
}
Comment on lines +1066 to +1085
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I did this right... I don't think this code path will be hit currently as no current API actually uses shader image filters, but this is for when we do.

case PictureImageFilter pictureImageFilter:
{
if (pictureImageFilter.Picture is null)
Expand Down
Loading