-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSurfaceLoader.cs
196 lines (173 loc) · 7.09 KB
/
SurfaceLoader.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
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using System.Threading.Tasks;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Text;
using Microsoft.Graphics.Canvas.UI.Composition;
using Windows.Foundation;
using Windows.Graphics.DirectX;
using Windows.UI;
using Windows.UI.Composition;
namespace Microsoft.Toolkit.Uwp.UI.Animations
{
/// <summary>
/// A delegate for load time effects.
/// </summary>
/// <param name="bitmap">The bitmap.</param>
/// <param name="device">The device.</param>
/// <param name="sizeTarget">The size target.</param>
/// <returns>A CompositeDrawingSurface</returns>
public delegate CompositionDrawingSurface LoadTimeEffectHandler(CanvasBitmap bitmap, CompositionGraphicsDevice device, Size sizeTarget);
/// <summary>
/// The SurfaceLoader is responsible to loading images into Composition Objects.
/// </summary>
public class SurfaceLoader
{
/// <summary>
/// A flag to store the intialized state.
/// </summary>
private static bool _intialized;
/// <summary>
/// The compositor
/// </summary>
private static Compositor _compositor;
/// <summary>
/// The canvas device
/// </summary>
private static CanvasDevice _canvasDevice;
/// <summary>
/// The composition graphic device to determinde which GPU is handling the request.
/// </summary>
private static CompositionGraphicsDevice _compositionDevice;
/// <summary>
/// Initializes the specified compositor.
/// </summary>
/// <param name="compositor">The compositor.</param>
public static void Initialize(Compositor compositor)
{
if (!_intialized)
{
_compositor = compositor;
_canvasDevice = new CanvasDevice();
_compositionDevice = CanvasComposition.CreateCompositionGraphicsDevice(_compositor, _canvasDevice);
_intialized = true;
}
}
/// <summary>
/// Uninitializes this instance.
/// </summary>
public static void Uninitialize()
{
_compositor = null;
if (_compositionDevice != null)
{
_compositionDevice.Dispose();
_compositionDevice = null;
}
if (_canvasDevice != null)
{
_canvasDevice.Dispose();
_canvasDevice = null;
}
_intialized = false;
}
/// <summary>
/// Gets a value indicating whether this instance is initialized.
/// </summary>
/// <value>
/// <c>true</c> if this instance is initialized; otherwise, <c>false</c>.
/// </value>
public static bool IsInitialized
{
get
{
return _intialized;
}
}
/// <summary>
/// Loads an image from the URI.
/// </summary>
/// <param name="uri">The URI.</param>
/// <returns><see cref="CompositionDrawingSurface"/></returns>
public static async Task<CompositionDrawingSurface> LoadFromUri(Uri uri)
{
return await LoadFromUri(uri, Size.Empty);
}
/// <summary>
/// Loads an image from URI with a specified size.
/// </summary>
/// <param name="uri">The URI.</param>
/// <param name="sizeTarget">The size target.</param>
/// <returns><see cref="CompositionDrawingSurface"/></returns>
public static async Task<CompositionDrawingSurface> LoadFromUri(Uri uri, Size sizeTarget)
{
CanvasBitmap bitmap = await CanvasBitmap.LoadAsync(_canvasDevice, uri);
Size sizeSource = bitmap.Size;
if (sizeTarget.IsEmpty)
{
sizeTarget = sizeSource;
}
CompositionDrawingSurface surface = _compositionDevice.CreateDrawingSurface(
sizeTarget,
DirectXPixelFormat.B8G8R8A8UIntNormalized,
DirectXAlphaMode.Premultiplied);
using (var ds = CanvasComposition.CreateDrawingSession(surface))
{
ds.Clear(Color.FromArgb(0, 0, 0, 0));
ds.DrawImage(bitmap, new Rect(0, 0, sizeTarget.Width, sizeTarget.Height), new Rect(0, 0, sizeSource.Width, sizeSource.Height));
}
return surface;
}
/// <summary>
/// Loads the text on to a <see cref="CompositionDrawingSurface"/>.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="sizeTarget">The size target.</param>
/// <param name="textFormat">The text format.</param>
/// <param name="textColor">Color of the text.</param>
/// <param name="bgColor">Color of the bg.</param>
/// <returns><see cref="CompositionDrawingSurface"/></returns>
public static CompositionDrawingSurface LoadText(string text, Size sizeTarget, CanvasTextFormat textFormat, Color textColor, Color bgColor)
{
CompositionDrawingSurface surface = _compositionDevice.CreateDrawingSurface(
sizeTarget,
DirectXPixelFormat.B8G8R8A8UIntNormalized,
DirectXAlphaMode.Premultiplied);
using (var ds = CanvasComposition.CreateDrawingSession(surface))
{
ds.Clear(bgColor);
ds.DrawText(text, new Rect(0, 0, sizeTarget.Width, sizeTarget.Height), textColor, textFormat);
}
return surface;
}
/// <summary>
/// Loads an image from URI, with a specified size.
/// </summary>
/// <param name="uri">The URI.</param>
/// <param name="sizeTarget">The size target.</param>
/// <param name="loadEffectHandler">The load effect handler callback.</param>
/// <returns><see cref="CompositionDrawingSurface"/></returns>
public static async Task<CompositionDrawingSurface> LoadFromUri(Uri uri, Size sizeTarget, LoadTimeEffectHandler loadEffectHandler)
{
if (loadEffectHandler != null)
{
var bitmap = await CanvasBitmap.LoadAsync(_canvasDevice, uri);
return loadEffectHandler(bitmap, _compositionDevice, sizeTarget);
}
else
{
return await LoadFromUri(uri, sizeTarget);
}
}
}
}