-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy pathSKXamlCanvas.cs
279 lines (231 loc) · 6.1 KB
/
SKXamlCanvas.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using Windows.ApplicationModel;
#if WINDOWS
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
#else
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
#endif
#if WINDOWS
namespace SkiaSharp.Views.Windows
#else
namespace SkiaSharp.Views.UWP
#endif
{
public partial class SKXamlCanvas : Canvas
{
private const float DpiBase = 96.0f;
private static readonly DependencyProperty ProxyVisibilityProperty =
DependencyProperty.Register(
"ProxyVisibility",
typeof(Visibility),
typeof(SKXamlCanvas),
new PropertyMetadata(Visibility.Visible, OnVisibilityChanged));
private static bool designMode = DesignMode.DesignModeEnabled;
private IntPtr pixels;
private WriteableBitmap bitmap;
private ImageBrush brush;
private bool ignorePixelScaling;
private bool isVisible = true;
// workaround for https://github.com/mono/SkiaSharp/issues/1118
private int loadUnloadCounter = 0;
public SKXamlCanvas()
{
if (designMode)
return;
#if !WINDOWS
var display = DisplayInformation.GetForCurrentView();
OnDpiChanged(display);
#endif
Loaded += OnLoaded;
Unloaded += OnUnloaded;
SizeChanged += OnSizeChanged;
var binding = new Binding
{
Path = new PropertyPath(nameof(Visibility)),
Source = this
};
SetBinding(ProxyVisibilityProperty, binding);
}
public SKSize CanvasSize { get; private set; }
public bool IgnorePixelScaling
{
get => ignorePixelScaling;
set
{
ignorePixelScaling = value;
Invalidate();
}
}
public double Dpi { get; private set; } = 1;
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
{
PaintSurface?.Invoke(this, e);
}
private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is SKXamlCanvas canvas && e.NewValue is Visibility visibility)
{
canvas.isVisible = visibility == Visibility.Visible;
canvas.Invalidate();
}
}
#if WINDOWS
private void OnXamlRootChanged(XamlRoot xamlRoot = null, XamlRootChangedEventArgs e = null)
{
var root = xamlRoot ?? XamlRoot;
var newDpi = root?.RasterizationScale ?? 1.0;
if (newDpi != Dpi)
{
Dpi = newDpi;
UpdateBrushScale();
Invalidate();
}
}
#else
private void OnDpiChanged(DisplayInformation sender, object args = null)
{
Dpi = sender.LogicalDpi / DpiBase;
Invalidate();
}
#endif
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
Invalidate();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
loadUnloadCounter++;
if (loadUnloadCounter != 1)
return;
#if WINDOWS
XamlRoot.Changed += OnXamlRootChanged;
OnXamlRootChanged();
#else
var display = DisplayInformation.GetForCurrentView();
display.DpiChanged += OnDpiChanged;
OnDpiChanged(display);
#endif
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
loadUnloadCounter--;
if (loadUnloadCounter != 0)
return;
#if WINDOWS
if (XamlRoot != null)
{
XamlRoot.Changed -= OnXamlRootChanged;
}
#else
var display = DisplayInformation.GetForCurrentView();
display.DpiChanged -= OnDpiChanged;
#endif
FreeBitmap();
}
public void Invalidate()
{
#if WINDOWS
DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal, DoInvalidate);
#else
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, DoInvalidate);
#endif
}
private void DoInvalidate()
{
if (designMode)
return;
if (!isVisible)
return;
var (info, viewSize, dpi) = CreateBitmap();
if (info.Width <= 0 || info.Height <= 0)
{
CanvasSize = SKSize.Empty;
return;
}
// 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 (matchUI)
{
var canvas = surface.Canvas;
canvas.Scale(dpi);
canvas.Save();
}
OnPaintSurface(new SKPaintSurfaceEventArgs(surface, info.WithSize(userVisibleSize), info));
}
bitmap.Invalidate();
}
private (SKSizeI ViewSize, SKSizeI PixelSize, float Dpi) CreateSize()
{
var w = ActualWidth;
var h = ActualHeight;
if (!IsPositive(w) || !IsPositive(h))
return (SKSizeI.Empty, SKSizeI.Empty, 1);
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 Info, SKSizeI PixelSize, float Dpi) CreateBitmap()
{
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();
if (bitmap == null && info.Width > 0 && info.Height > 0)
{
bitmap = new WriteableBitmap(info.Width, info.Height);
pixels = bitmap.GetPixels();
brush = new ImageBrush
{
ImageSource = bitmap,
AlignmentX = AlignmentX.Left,
AlignmentY = AlignmentY.Top,
Stretch = Stretch.None
};
UpdateBrushScale();
Background = brush;
}
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;
}
}
}