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

Do not stretch the background #2922

Merged
merged 3 commits into from
Jul 5, 2024
Merged
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
68 changes: 48 additions & 20 deletions source/SkiaSharp.Views/SkiaSharp.Views.WinUI/SKXamlCanvas.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Windows.ApplicationModel;
using Windows.Graphics.Display;

#if WINDOWS
using Microsoft.UI.Dispatching;
Expand Down Expand Up @@ -39,6 +38,7 @@ public partial class SKXamlCanvas : Canvas

private IntPtr pixels;
private WriteableBitmap bitmap;
private ImageBrush brush;
private bool ignorePixelScaling;
private bool isVisible = true;

Expand Down Expand Up @@ -101,8 +101,13 @@ private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyCh
private void OnXamlRootChanged(XamlRoot xamlRoot = null, XamlRootChangedEventArgs e = null)
{
var root = xamlRoot ?? XamlRoot;
Dpi = root?.RasterizationScale ?? 1.0;
Invalidate();
var newDpi = root?.RasterizationScale ?? 1.0;
if (newDpi != Dpi)
Comment on lines +104 to +105
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 check here prevents hundreds of invalidations. The XamlRootChanged event will fire when any of the properties of the XAML root changes. This does not seem to fire just when the current view gets a new XAML root as the name implies.

{
Dpi = newDpi;
UpdateBrushScale();
Invalidate();
}
}
#else
private void OnDpiChanged(DisplayInformation sender, object args = null)
Expand Down Expand Up @@ -141,7 +146,7 @@ private void OnUnloaded(object sender, RoutedEventArgs e)
return;

#if WINDOWS
if(XamlRoot != null)
if (XamlRoot != null)
{
XamlRoot.Changed -= OnXamlRootChanged;
}
Expand Down Expand Up @@ -170,20 +175,26 @@ private void DoInvalidate()
if (!isVisible)
return;

var info = CreateBitmap(out var unscaledSize, out var dpi);
var (info, viewSize, dpi) = CreateBitmap();

if (info.Width <= 0 || info.Height <= 0)
{
CanvasSize = SKSize.Empty;
return;
}

var userVisibleSize = IgnorePixelScaling ? unscaledSize : info.Size;
// This is here because the property name is confusing and backwards.
// True actually means to ignore the pixel scaling of the raw pixel
// size and instead use the view size such that sizes match the XAML
// elements.
var matchUI = IgnorePixelScaling;

var userVisibleSize = matchUI ? viewSize : info.Size;
CanvasSize = userVisibleSize;

using (var surface = SKSurface.Create(info, pixels, info.RowBytes))
{
if (IgnorePixelScaling)
if (matchUI)
{
var canvas = surface.Canvas;
canvas.Scale(dpi);
Expand All @@ -195,30 +206,30 @@ private void DoInvalidate()
bitmap.Invalidate();
}

private SKSizeI CreateSize(out SKSizeI unscaledSize, out float dpi)
private (SKSizeI ViewSize, SKSizeI PixelSize, float Dpi) CreateSize()
{
unscaledSize = SKSizeI.Empty;
dpi = (float)Dpi;

var w = ActualWidth;
var h = ActualHeight;

if (!IsPositive(w) || !IsPositive(h))
return SKSizeI.Empty;
return (SKSizeI.Empty, SKSizeI.Empty, 1);

unscaledSize = new SKSizeI((int)w, (int)h);
return new SKSizeI((int)(w * dpi), (int)(h * dpi));
var dpi = (float)Dpi;
var viewSize = new SKSizeI((int)w, (int)h);
var pixelSize = new SKSizeI((int)(w * dpi), (int)(h * dpi));

return (viewSize, pixelSize, dpi);

static bool IsPositive(double value)
{
return !double.IsNaN(value) && !double.IsInfinity(value) && value > 0;
}
}

private SKImageInfo CreateBitmap(out SKSizeI unscaledSize, out float dpi)
private (SKImageInfo Info, SKSizeI PixelSize, float Dpi) CreateBitmap()
{
var size = CreateSize(out unscaledSize, out dpi);
var info = new SKImageInfo(size.Width, size.Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
var (viewSize, pixelSize, dpi) = CreateSize();
var info = new SKImageInfo(pixelSize.Width, pixelSize.Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);

if (bitmap?.PixelWidth != info.Width || bitmap?.PixelHeight != info.Height)
FreeBitmap();
Expand All @@ -228,22 +239,39 @@ private SKImageInfo CreateBitmap(out SKSizeI unscaledSize, out float dpi)
bitmap = new WriteableBitmap(info.Width, info.Height);
pixels = bitmap.GetPixels();

var brush = new ImageBrush
brush = new ImageBrush
{
ImageSource = bitmap,
AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top,
Stretch = Stretch.Fill
Stretch = Stretch.None
};
UpdateBrushScale();

Background = brush;
}

return info;
return (info, viewSize, dpi);
}

private void UpdateBrushScale()
{
if (brush == null)
return;

var scale = 1.0 / Dpi;

brush.Transform = new ScaleTransform
{
ScaleX = scale,
ScaleY = scale
};
}

private void FreeBitmap()
{
Background = null;
brush = null;
bitmap = null;
pixels = IntPtr.Zero;
}
Expand Down
Loading