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

[release/2.x] Do not stretch the background #2925

Open
wants to merge 1 commit into
base: release/2.x
Choose a base branch
from
Open
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.UWP/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)
{
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