Skip to content

Commit

Permalink
Merge pull request unoplatform#17035 from ramezgerges/surfacebrush_re…
Browse files Browse the repository at this point in the history
…size_null

fix(surfacebrush): check that the final size isn't empty before resizing images.
  • Loading branch information
ramezgerges authored Jun 10, 2024
2 parents 11d6e33 + 4ab4b27 commit 1c16eb6
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/Uno.UI.Composition/Composition/CompositionSurfaceBrush.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,40 @@ internal override void UpdatePaint(SKPaint fillPaint, SKRect bounds)
{
var backgroundArea = GetArrangedImageRect(new Size(scs.Image!.Width, scs.Image.Height), bounds);

// Adding image downscaling in the shader matrix directly is very blurry
// since the default downsampler in Skia is really low quality (but really fast).
// We force Lanczos instead.
// https://github.com/mono/SkiaSharp/issues/520#issuecomment-444973518
if (backgroundArea.Width <= 0 || backgroundArea.Height <= 0)
{
fillPaint.Shader = null;
}
else
{
// Adding image downscaling in the shader matrix directly is very blurry
// since the default downsampler in Skia is really low quality (but really fast).
// We force Lanczos instead.
// https://github.com/mono/SkiaSharp/issues/520#issuecomment-444973518
#pragma warning disable CS0612 // Type or member is obsolete
var resizedBitmap = scs.Image.ToSKBitmap().Resize(scs.Image.Info.WithSize((int)backgroundArea.Size.Width, (int)backgroundArea.Size.Height), SKBitmapResizeMethod.Lanczos3.ToFilterQuality());
var resizedBitmap = scs.Image.ToSKBitmap().Resize(scs.Image.Info.WithSize((int)backgroundArea.Size.Width, (int)backgroundArea.Size.Height), SKBitmapResizeMethod.Lanczos3.ToFilterQuality());
#pragma warning restore CS0612 // Type or member is obsolete
var resizedImage = SKImage.FromBitmap(resizedBitmap);
var resizedImage = SKImage.FromBitmap(resizedBitmap);

var matrix = Matrix3x2.CreateTranslation((float)backgroundArea.Left, (float)backgroundArea.Top);
matrix *= TransformMatrix;
var imageShader = SKShader.CreateImage(resizedImage, SKShaderTileMode.Decal, SKShaderTileMode.Decal, matrix.ToSKMatrix());
var matrix = Matrix3x2.CreateTranslation((float)backgroundArea.Left, (float)backgroundArea.Top);
matrix *= TransformMatrix;
var imageShader = SKShader.CreateImage(resizedImage, SKShaderTileMode.Decal, SKShaderTileMode.Decal, matrix.ToSKMatrix());

if (UsePaintColorToColorSurface)
{
// use the set color instead of the image pixel values. This is what happens on WinUI.
var blendedShader = SKShader.CreateColorFilter(imageShader, SKColorFilter.CreateBlendMode(fillPaint.Color, SKBlendMode.SrcIn));
if (UsePaintColorToColorSurface)
{
// use the set color instead of the image pixel values. This is what happens on WinUI.
var blendedShader = SKShader.CreateColorFilter(imageShader, SKColorFilter.CreateBlendMode(fillPaint.Color, SKBlendMode.SrcIn));

fillPaint.Shader = blendedShader;
}
else
{
fillPaint.Shader = imageShader;
}
fillPaint.Shader = blendedShader;
}
else
{
fillPaint.Shader = imageShader;
}

fillPaint.IsAntialias = true;
fillPaint.FilterQuality = SKFilterQuality.High;
fillPaint.IsAntialias = true;
fillPaint.FilterQuality = SKFilterQuality.High;
}
}
else if (Surface is ISkiaSurface skiaSurface)
{
Expand All @@ -122,7 +129,9 @@ internal override void UpdatePaint(SKPaint fillPaint, SKRect bounds)
fillPaint.IsDither = true;
}
else
{
fillPaint.Shader = null;
}
}
else
{
Expand Down

0 comments on commit 1c16eb6

Please sign in to comment.