@native-sdk/cli 0.7.0 → 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/app.zig +8 -0
- package/build.zig +114 -103
- package/native-sdk.d.ts +6 -3
- package/package.json +9 -9
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/sdk/text.ts +25 -14
- package/packages/core/src/checker.ts +39 -0
- package/packages/core/src/emitter.ts +125 -1
- package/skill-data/native-ui/SKILL.md +11 -7
- package/src/platform/macos/appkit_host.m +76 -4
- package/src/platform/macos/cef_host.mm +24 -2
- package/src/platform/macos/root.zig +13 -0
- package/src/platform/types.zig +4 -0
- package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
- package/src/platform/windows/gpu_surface_renderer.h +86 -0
- package/src/platform/windows/root.zig +298 -20
- package/src/platform/windows/webview2_host.cpp +343 -33
- package/src/primitives/canvas/code.zig +501 -33
- package/src/primitives/canvas/code_tests.zig +1029 -6
- package/src/primitives/canvas/events.zig +111 -1
- package/src/primitives/canvas/frame.zig +45 -1
- package/src/primitives/canvas/gpu.zig +6 -0
- package/src/primitives/canvas/markdown_tests.zig +17 -18
- package/src/primitives/canvas/render_tests.zig +93 -0
- package/src/primitives/canvas/root.zig +6 -0
- package/src/primitives/canvas/serialization.zig +150 -21
- package/src/primitives/canvas/text.zig +6 -0
- package/src/primitives/canvas/themes/geist.zig +51 -5
- package/src/primitives/canvas/tokens.zig +33 -4
- package/src/primitives/canvas/ui.zig +160 -36
- package/src/primitives/canvas/ui_markup.zig +60 -9
- package/src/primitives/canvas/ui_markup_compiled.zig +41 -0
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
- package/src/primitives/canvas/ui_markup_contract.zig +17 -1
- package/src/primitives/canvas/ui_markup_view.zig +44 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +80 -6
- package/src/primitives/canvas/ui_schema.zig +18 -0
- package/src/primitives/canvas/ui_schema_tests.zig +10 -7
- package/src/primitives/canvas/ui_tests.zig +40 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
- package/src/primitives/canvas/widget_invalidation.zig +12 -1
- package/src/primitives/canvas/widget_layout.zig +260 -21
- package/src/primitives/canvas/widget_metrics.zig +47 -6
- package/src/primitives/canvas/widget_render.zig +849 -78
- package/src/primitives/canvas/widget_render_controls.zig +83 -25
- package/src/primitives/canvas/widget_render_style.zig +52 -8
- package/src/primitives/canvas/widget_runtime.zig +2 -0
- package/src/primitives/canvas/widget_text_input.zig +167 -8
- package/src/primitives/canvas/widget_tree.zig +6 -5
- package/src/primitives/canvas/widgets.zig +78 -2
- package/src/runtime/canvas_budget_edge_tests.zig +3 -2
- package/src/runtime/canvas_frame.zig +60 -7
- package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
- package/src/runtime/canvas_frame_packet_tests.zig +83 -9
- package/src/runtime/canvas_frame_patch_tests.zig +16 -0
- package/src/runtime/canvas_images.zig +8 -2
- package/src/runtime/canvas_limits.zig +13 -6
- package/src/runtime/canvas_widget_clipboard_tests.zig +8 -5
- package/src/runtime/canvas_widget_context_menu.zig +7 -2
- package/src/runtime/canvas_widget_event_tests.zig +18 -14
- package/src/runtime/canvas_widget_events.zig +51 -20
- package/src/runtime/canvas_widget_runtime.zig +108 -4
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
- package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
- package/src/runtime/canvas_widget_state.zig +27 -0
- package/src/runtime/canvas_widget_text_tests.zig +326 -0
- package/src/runtime/core.zig +15 -5
- package/src/runtime/effects.zig +6 -0
- package/src/runtime/effects_tests.zig +30 -0
- package/src/runtime/gpu_surface_events.zig +50 -19
- package/src/runtime/media_surface_tests.zig +1 -1
- package/src/runtime/runtime_event_tests.zig +74 -0
- package/src/runtime/ui_app.zig +10 -8
- package/src/runtime/ui_app_tests.zig +67 -0
- package/src/runtime/view.zig +60 -16
- package/src/runtime/view_canvas.zig +15 -0
- package/src/runtime/view_widget_scroll.zig +48 -18
- package/src/runtime/view_widget_text.zig +59 -16
- package/src/runtime/window_view_runtime.zig +10 -0
- package/src/tooling/templates.zig +9 -6
|
@@ -0,0 +1,2280 @@
|
|
|
1
|
+
#include "gpu_surface_renderer.h"
|
|
2
|
+
|
|
3
|
+
#include <d2d1.h>
|
|
4
|
+
#include <dwrite_3.h>
|
|
5
|
+
|
|
6
|
+
#include <algorithm>
|
|
7
|
+
#include <atomic>
|
|
8
|
+
#include <cmath>
|
|
9
|
+
#include <cstring>
|
|
10
|
+
#include <limits>
|
|
11
|
+
#include <map>
|
|
12
|
+
#include <set>
|
|
13
|
+
#include <string>
|
|
14
|
+
#include <utility>
|
|
15
|
+
#include <vector>
|
|
16
|
+
|
|
17
|
+
namespace {
|
|
18
|
+
|
|
19
|
+
/* Compact binary gpu-surface packet decoding (wire format v5).
|
|
20
|
+
*
|
|
21
|
+
* This independent decoder deliberately repeats the encoder's tags and
|
|
22
|
+
* bounds rather than sharing packed structs across the Zig/C++ ABI. A
|
|
23
|
+
* version or layout disagreement is a refused present, which makes the
|
|
24
|
+
* runtime resynchronize/fall back instead of drawing corrupt content. */
|
|
25
|
+
constexpr uint8_t kPacketVersion = 5;
|
|
26
|
+
constexpr size_t kRetainedCommandCap = 2048;
|
|
27
|
+
constexpr size_t kDirtyRectCap = kWindowsGpuDirtyRectCap;
|
|
28
|
+
constexpr uint32_t kMaxSurfacePixels = 8192;
|
|
29
|
+
constexpr float kBezierCircle = 0.5522847498307936f;
|
|
30
|
+
|
|
31
|
+
constexpr uint64_t canvasFontResourceId(uint64_t font_id) {
|
|
32
|
+
/* Zero is DrawText's public default. It and the styled sans variants
|
|
33
|
+
* resolve through the same bundled Geist regular resource the engine's
|
|
34
|
+
* reference metrics use. */
|
|
35
|
+
return font_id == 0 || (font_id >= 3 && font_id <= 6) ? 1 : font_id;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
constexpr uint64_t canvasFallbackFontResourceId(uint64_t font_id) {
|
|
39
|
+
return font_id == 2 ? 0 : 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
constexpr float canvasStrokeWidth(float width) {
|
|
43
|
+
return width > 0 ? width : 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static_assert(canvasFontResourceId(0) == 1, "default canvas text uses bundled Geist");
|
|
47
|
+
static_assert(canvasFontResourceId(2) == 2, "the built-in mono face keeps its resource id");
|
|
48
|
+
static_assert(canvasFontResourceId(6) == 1, "styled sans variants use bundled Geist");
|
|
49
|
+
static_assert(canvasFallbackFontResourceId(2) == 0, "mono keeps its platform fallback");
|
|
50
|
+
static_assert(canvasFallbackFontResourceId(64) == 1, "missing application fonts fall back to Geist");
|
|
51
|
+
static_assert(canvasStrokeWidth(-1.0f) == 0, "negative canvas strokes are empty");
|
|
52
|
+
static_assert(canvasStrokeWidth(0.0f) == 0, "zero-width canvas strokes are empty");
|
|
53
|
+
static_assert(canvasStrokeWidth(0.5f) == 0.5f, "subpixel canvas strokes keep their width");
|
|
54
|
+
|
|
55
|
+
template <typename T>
|
|
56
|
+
static void releaseCom(T *&value) {
|
|
57
|
+
if (value) value->Release();
|
|
58
|
+
value = nullptr;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static float clamp01(float value) {
|
|
62
|
+
return std::max(0.0f, std::min(1.0f, value));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static uint64_t gpuClockNs() {
|
|
66
|
+
static LARGE_INTEGER frequency = [] {
|
|
67
|
+
LARGE_INTEGER value = {};
|
|
68
|
+
QueryPerformanceFrequency(&value);
|
|
69
|
+
return value;
|
|
70
|
+
}();
|
|
71
|
+
LARGE_INTEGER counter = {};
|
|
72
|
+
QueryPerformanceCounter(&counter);
|
|
73
|
+
const uint64_t ticks_per_second = static_cast<uint64_t>(frequency.QuadPart);
|
|
74
|
+
if (ticks_per_second == 0) return 0;
|
|
75
|
+
const uint64_t ticks = static_cast<uint64_t>(counter.QuadPart);
|
|
76
|
+
return (ticks / ticks_per_second) * 1000000000ULL +
|
|
77
|
+
((ticks % ticks_per_second) * 1000000000ULL) / ticks_per_second;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
struct Point {
|
|
81
|
+
float x = 0;
|
|
82
|
+
float y = 0;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
struct Rect {
|
|
86
|
+
float x = 0;
|
|
87
|
+
float y = 0;
|
|
88
|
+
float width = 0;
|
|
89
|
+
float height = 0;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
struct Radius {
|
|
93
|
+
float top_left = 0;
|
|
94
|
+
float top_right = 0;
|
|
95
|
+
float bottom_right = 0;
|
|
96
|
+
float bottom_left = 0;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
struct Color {
|
|
100
|
+
float r = 0;
|
|
101
|
+
float g = 0;
|
|
102
|
+
float b = 0;
|
|
103
|
+
float a = 1;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
struct Affine {
|
|
107
|
+
float a = 1;
|
|
108
|
+
float b = 0;
|
|
109
|
+
float c = 0;
|
|
110
|
+
float d = 1;
|
|
111
|
+
float tx = 0;
|
|
112
|
+
float ty = 0;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
constexpr float maxTransformAxisLengthSquared(const Affine &matrix) {
|
|
116
|
+
const float x_scale_squared = matrix.a * matrix.a + matrix.b * matrix.b;
|
|
117
|
+
const float y_scale_squared = matrix.c * matrix.c + matrix.d * matrix.d;
|
|
118
|
+
return x_scale_squared > y_scale_squared ? x_scale_squared : y_scale_squared;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
static float transformScale(const Affine &matrix) {
|
|
122
|
+
return std::max(0.0001f, std::sqrt(maxTransformAxisLengthSquared(matrix)));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static_assert(maxTransformAxisLengthSquared(Affine{2, 0, 0, 2, 0, 0}) == 4,
|
|
126
|
+
"uniform command scaling must scale effect kernels");
|
|
127
|
+
static_assert(maxTransformAxisLengthSquared(Affine{0, 3, -2, 0, 0, 0}) == 9,
|
|
128
|
+
"rotated nonuniform transforms use their largest axis scale");
|
|
129
|
+
|
|
130
|
+
static Rect normalized(Rect rect) {
|
|
131
|
+
if (rect.width < 0) {
|
|
132
|
+
rect.x += rect.width;
|
|
133
|
+
rect.width = -rect.width;
|
|
134
|
+
}
|
|
135
|
+
if (rect.height < 0) {
|
|
136
|
+
rect.y += rect.height;
|
|
137
|
+
rect.height = -rect.height;
|
|
138
|
+
}
|
|
139
|
+
return rect;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static bool empty(Rect rect) {
|
|
143
|
+
rect = normalized(rect);
|
|
144
|
+
return rect.width <= 0 || rect.height <= 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
static bool intersects(Rect left, Rect right) {
|
|
148
|
+
left = normalized(left);
|
|
149
|
+
right = normalized(right);
|
|
150
|
+
return left.x < right.x + right.width && right.x < left.x + left.width &&
|
|
151
|
+
left.y < right.y + right.height && right.y < left.y + left.height;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
static Rect intersection(Rect left, Rect right) {
|
|
155
|
+
left = normalized(left);
|
|
156
|
+
right = normalized(right);
|
|
157
|
+
const float x0 = std::max(left.x, right.x);
|
|
158
|
+
const float y0 = std::max(left.y, right.y);
|
|
159
|
+
const float x1 = std::min(left.x + left.width, right.x + right.width);
|
|
160
|
+
const float y1 = std::min(left.y + left.height, right.y + right.height);
|
|
161
|
+
return {x0, y0, std::max(0.0f, x1 - x0), std::max(0.0f, y1 - y0)};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static bool contains(Rect rect, double x, double y) {
|
|
165
|
+
rect = normalized(rect);
|
|
166
|
+
return x >= rect.x && y >= rect.y && x < rect.x + rect.width && y < rect.y + rect.height;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static D2D1_RECT_F d2dRect(Rect rect) {
|
|
170
|
+
rect = normalized(rect);
|
|
171
|
+
return D2D1::RectF(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static Rect transformedRect(Rect rect, const Affine &matrix) {
|
|
175
|
+
rect = normalized(rect);
|
|
176
|
+
const Point corners[4] = {
|
|
177
|
+
{rect.x, rect.y},
|
|
178
|
+
{rect.x + rect.width, rect.y},
|
|
179
|
+
{rect.x, rect.y + rect.height},
|
|
180
|
+
{rect.x + rect.width, rect.y + rect.height},
|
|
181
|
+
};
|
|
182
|
+
float x0 = std::numeric_limits<float>::infinity();
|
|
183
|
+
float y0 = std::numeric_limits<float>::infinity();
|
|
184
|
+
float x1 = -std::numeric_limits<float>::infinity();
|
|
185
|
+
float y1 = -std::numeric_limits<float>::infinity();
|
|
186
|
+
for (const Point &point : corners) {
|
|
187
|
+
const float x = point.x * matrix.a + point.y * matrix.c + matrix.tx;
|
|
188
|
+
const float y = point.x * matrix.b + point.y * matrix.d + matrix.ty;
|
|
189
|
+
x0 = std::min(x0, x);
|
|
190
|
+
y0 = std::min(y0, y);
|
|
191
|
+
x1 = std::max(x1, x);
|
|
192
|
+
y1 = std::max(y1, y);
|
|
193
|
+
}
|
|
194
|
+
return {x0, y0, x1 - x0, y1 - y0};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static D2D1_COLOR_F d2dColor(Color color, float opacity = 1.0f) {
|
|
198
|
+
return D2D1::ColorF(clamp01(color.r), clamp01(color.g), clamp01(color.b), clamp01(color.a * opacity));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
static uint32_t packedColor(Color color) {
|
|
202
|
+
const uint32_t r = static_cast<uint32_t>(std::lround(clamp01(color.r) * 255.0f));
|
|
203
|
+
const uint32_t g = static_cast<uint32_t>(std::lround(clamp01(color.g) * 255.0f));
|
|
204
|
+
const uint32_t b = static_cast<uint32_t>(std::lround(clamp01(color.b) * 255.0f));
|
|
205
|
+
const uint32_t a = static_cast<uint32_t>(std::lround(clamp01(color.a) * 255.0f));
|
|
206
|
+
return (a << 24) | (r << 16) | (g << 8) | b;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
class Reader {
|
|
210
|
+
public:
|
|
211
|
+
Reader(const uint8_t *bytes, size_t length) : bytes_(bytes), length_(length) {}
|
|
212
|
+
|
|
213
|
+
bool failed() const { return failed_; }
|
|
214
|
+
bool finished() const { return !failed_ && offset_ == length_; }
|
|
215
|
+
size_t remaining() const { return offset_ <= length_ ? length_ - offset_ : 0; }
|
|
216
|
+
|
|
217
|
+
bool bytes(void *destination, size_t count) {
|
|
218
|
+
if (!has(count)) return false;
|
|
219
|
+
if (count) std::memcpy(destination, bytes_ + offset_, count);
|
|
220
|
+
offset_ += count;
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
uint8_t u8() {
|
|
225
|
+
uint8_t value = 0;
|
|
226
|
+
bytes(&value, sizeof(value));
|
|
227
|
+
return value;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
uint16_t u16() {
|
|
231
|
+
uint8_t raw[2] = {};
|
|
232
|
+
if (!bytes(raw, sizeof(raw))) return 0;
|
|
233
|
+
return static_cast<uint16_t>(raw[0]) |
|
|
234
|
+
static_cast<uint16_t>(static_cast<uint16_t>(raw[1]) << 8);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
uint32_t u32() {
|
|
238
|
+
uint8_t raw[4] = {};
|
|
239
|
+
if (!bytes(raw, sizeof(raw))) return 0;
|
|
240
|
+
return static_cast<uint32_t>(raw[0]) |
|
|
241
|
+
(static_cast<uint32_t>(raw[1]) << 8) |
|
|
242
|
+
(static_cast<uint32_t>(raw[2]) << 16) |
|
|
243
|
+
(static_cast<uint32_t>(raw[3]) << 24);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
uint64_t u64() {
|
|
247
|
+
uint8_t raw[8] = {};
|
|
248
|
+
if (!bytes(raw, sizeof(raw))) return 0;
|
|
249
|
+
uint64_t value = 0;
|
|
250
|
+
for (unsigned index = 0; index < 8; ++index) value |= static_cast<uint64_t>(raw[index]) << (index * 8);
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
float f32() {
|
|
255
|
+
const uint32_t bits = u32();
|
|
256
|
+
float value = 0;
|
|
257
|
+
std::memcpy(&value, &bits, sizeof(value));
|
|
258
|
+
if (!std::isfinite(value)) failed_ = true;
|
|
259
|
+
return value;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
std::string string() {
|
|
263
|
+
const uint32_t count = u32();
|
|
264
|
+
if (!has(count)) return {};
|
|
265
|
+
std::string value(reinterpret_cast<const char *>(bytes_ + offset_), count);
|
|
266
|
+
offset_ += count;
|
|
267
|
+
return value;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
void fail() { failed_ = true; }
|
|
271
|
+
|
|
272
|
+
private:
|
|
273
|
+
bool has(size_t count) {
|
|
274
|
+
if (failed_ || count > remaining()) {
|
|
275
|
+
failed_ = true;
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const uint8_t *bytes_ = nullptr;
|
|
282
|
+
size_t length_ = 0;
|
|
283
|
+
size_t offset_ = 0;
|
|
284
|
+
bool failed_ = false;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
static Point readPoint(Reader &reader) {
|
|
288
|
+
return {reader.f32(), reader.f32()};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
static Rect readRect(Reader &reader) {
|
|
292
|
+
return {reader.f32(), reader.f32(), reader.f32(), reader.f32()};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
static Radius readRadius(Reader &reader) {
|
|
296
|
+
return {reader.f32(), reader.f32(), reader.f32(), reader.f32()};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static Color readColor(Reader &reader) {
|
|
300
|
+
return {reader.f32(), reader.f32(), reader.f32(), reader.f32()};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
struct PathElement {
|
|
304
|
+
enum class Verb : uint8_t { move, line, quadratic, cubic, close };
|
|
305
|
+
Verb verb = Verb::move;
|
|
306
|
+
Point points[3] = {};
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
struct Shape {
|
|
310
|
+
enum class Kind : uint8_t { none, rect, rounded_rect, stroke_rect, line, path };
|
|
311
|
+
Kind kind = Kind::none;
|
|
312
|
+
Rect rect = {};
|
|
313
|
+
Radius radius = {};
|
|
314
|
+
Point from = {};
|
|
315
|
+
Point to = {};
|
|
316
|
+
float width = 1;
|
|
317
|
+
std::vector<PathElement> path;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
struct GradientStop {
|
|
321
|
+
float offset = 0;
|
|
322
|
+
Color color = {};
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
struct Paint {
|
|
326
|
+
enum class Kind : uint8_t { none, color, linear_gradient };
|
|
327
|
+
Kind kind = Kind::none;
|
|
328
|
+
Color color = {};
|
|
329
|
+
Point start = {};
|
|
330
|
+
Point end = {};
|
|
331
|
+
std::vector<GradientStop> stops;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
struct ImageCommand {
|
|
335
|
+
uint64_t id = 0;
|
|
336
|
+
bool has_src = false;
|
|
337
|
+
Rect src = {};
|
|
338
|
+
Rect dst = {};
|
|
339
|
+
float opacity = 1;
|
|
340
|
+
uint8_t fit = 0;
|
|
341
|
+
uint8_t sampling = 1;
|
|
342
|
+
Radius radius = {};
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
struct TextLine {
|
|
346
|
+
float x = 0;
|
|
347
|
+
float baseline = 0;
|
|
348
|
+
std::string text;
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
struct PositionedGlyph {
|
|
352
|
+
uint16_t id = 0;
|
|
353
|
+
uint64_t font_id = 0;
|
|
354
|
+
float x = 0;
|
|
355
|
+
float baseline = 0;
|
|
356
|
+
float advance = 0;
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
struct PositionedTextFragment {
|
|
360
|
+
float x = 0;
|
|
361
|
+
float baseline = 0;
|
|
362
|
+
std::string text;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
struct TextCommand {
|
|
366
|
+
uint64_t font_id = 0;
|
|
367
|
+
float size = 12;
|
|
368
|
+
Point origin = {};
|
|
369
|
+
Color color = {};
|
|
370
|
+
std::string text;
|
|
371
|
+
bool has_positioned_glyphs = false;
|
|
372
|
+
std::vector<PositionedGlyph> positioned_glyphs;
|
|
373
|
+
std::vector<PositionedTextFragment> positioned_fragments;
|
|
374
|
+
bool has_layout = false;
|
|
375
|
+
float max_width = 0;
|
|
376
|
+
float line_height = 0;
|
|
377
|
+
uint8_t wrap = 1;
|
|
378
|
+
uint8_t align = 0;
|
|
379
|
+
bool has_lines = false;
|
|
380
|
+
std::vector<TextLine> lines;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
struct Effect {
|
|
384
|
+
enum class Kind : uint8_t { none, shadow, blur };
|
|
385
|
+
Kind kind = Kind::none;
|
|
386
|
+
Rect rect = {};
|
|
387
|
+
Radius radius = {};
|
|
388
|
+
Point offset = {};
|
|
389
|
+
float blur = 0;
|
|
390
|
+
float spread = 0;
|
|
391
|
+
Color color = {};
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
struct Command {
|
|
395
|
+
uint8_t kind = 0xff;
|
|
396
|
+
Rect bounds = {};
|
|
397
|
+
float opacity = 1;
|
|
398
|
+
float stroke_width = 1;
|
|
399
|
+
uint8_t cap = 0;
|
|
400
|
+
bool has_id = false;
|
|
401
|
+
uint64_t id = 0;
|
|
402
|
+
bool has_clip = false;
|
|
403
|
+
Rect clip = {};
|
|
404
|
+
bool has_transform = false;
|
|
405
|
+
Affine transform = {};
|
|
406
|
+
Shape shape;
|
|
407
|
+
Paint paint;
|
|
408
|
+
ImageCommand image;
|
|
409
|
+
TextCommand text;
|
|
410
|
+
Effect effect;
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
struct KeyedCommand {
|
|
414
|
+
uint64_t key = 0;
|
|
415
|
+
Command command;
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
struct ImageMeta {
|
|
419
|
+
uint64_t id = 0;
|
|
420
|
+
uint64_t fingerprint = 0;
|
|
421
|
+
uint32_t width = 0;
|
|
422
|
+
uint32_t height = 0;
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
struct ImageAction {
|
|
426
|
+
uint8_t kind = 0;
|
|
427
|
+
uint64_t id = 0;
|
|
428
|
+
uint64_t fingerprint = 0;
|
|
429
|
+
uint32_t image_index = UINT32_MAX;
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
struct DecodedPacket {
|
|
433
|
+
uint8_t load_action = 0;
|
|
434
|
+
uint64_t generation = 0;
|
|
435
|
+
bool has_scissor = false;
|
|
436
|
+
Rect scissor = {};
|
|
437
|
+
std::vector<Rect> dirty_rects;
|
|
438
|
+
std::vector<ImageMeta> images;
|
|
439
|
+
std::vector<ImageAction> image_actions;
|
|
440
|
+
std::vector<KeyedCommand> commands;
|
|
441
|
+
std::vector<uint64_t> evicts;
|
|
442
|
+
std::vector<KeyedCommand> upserts;
|
|
443
|
+
std::vector<uint64_t> order;
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
static bool readShape(Reader &reader, Shape *shape) {
|
|
447
|
+
switch (reader.u8()) {
|
|
448
|
+
case 1:
|
|
449
|
+
shape->kind = Shape::Kind::rect;
|
|
450
|
+
shape->rect = readRect(reader);
|
|
451
|
+
break;
|
|
452
|
+
case 2:
|
|
453
|
+
shape->kind = Shape::Kind::rounded_rect;
|
|
454
|
+
shape->rect = readRect(reader);
|
|
455
|
+
shape->radius = readRadius(reader);
|
|
456
|
+
break;
|
|
457
|
+
case 3:
|
|
458
|
+
shape->kind = Shape::Kind::stroke_rect;
|
|
459
|
+
shape->rect = readRect(reader);
|
|
460
|
+
shape->radius = readRadius(reader);
|
|
461
|
+
shape->width = reader.f32();
|
|
462
|
+
break;
|
|
463
|
+
case 4:
|
|
464
|
+
shape->kind = Shape::Kind::line;
|
|
465
|
+
shape->from = readPoint(reader);
|
|
466
|
+
shape->to = readPoint(reader);
|
|
467
|
+
shape->width = reader.f32();
|
|
468
|
+
break;
|
|
469
|
+
case 5: {
|
|
470
|
+
shape->kind = Shape::Kind::path;
|
|
471
|
+
const uint32_t count = reader.u32();
|
|
472
|
+
if (reader.failed() || count > reader.remaining() || count > 65536) return false;
|
|
473
|
+
shape->path.reserve(count);
|
|
474
|
+
for (uint32_t index = 0; index < count; ++index) {
|
|
475
|
+
PathElement element;
|
|
476
|
+
const uint8_t verb = reader.u8();
|
|
477
|
+
if (verb > 4) return false;
|
|
478
|
+
element.verb = static_cast<PathElement::Verb>(verb);
|
|
479
|
+
const size_t point_count = verb <= 1 ? 1 : (verb == 2 ? 2 : (verb == 3 ? 3 : 0));
|
|
480
|
+
for (size_t point = 0; point < point_count; ++point) element.points[point] = readPoint(reader);
|
|
481
|
+
shape->path.push_back(element);
|
|
482
|
+
}
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
default:
|
|
486
|
+
return false;
|
|
487
|
+
}
|
|
488
|
+
return !reader.failed();
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
static bool readPaint(Reader &reader, Paint *paint) {
|
|
492
|
+
switch (reader.u8()) {
|
|
493
|
+
case 1:
|
|
494
|
+
paint->kind = Paint::Kind::color;
|
|
495
|
+
paint->color = readColor(reader);
|
|
496
|
+
break;
|
|
497
|
+
case 2: {
|
|
498
|
+
paint->kind = Paint::Kind::linear_gradient;
|
|
499
|
+
paint->start = readPoint(reader);
|
|
500
|
+
paint->end = readPoint(reader);
|
|
501
|
+
const uint32_t count = reader.u32();
|
|
502
|
+
if (reader.failed() || count > reader.remaining() || count > 4096) return false;
|
|
503
|
+
paint->stops.reserve(count);
|
|
504
|
+
for (uint32_t index = 0; index < count; ++index) {
|
|
505
|
+
GradientStop stop;
|
|
506
|
+
stop.offset = reader.f32();
|
|
507
|
+
stop.color = readColor(reader);
|
|
508
|
+
paint->stops.push_back(stop);
|
|
509
|
+
}
|
|
510
|
+
if (paint->stops.empty()) return false;
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
default:
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
return !reader.failed();
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
static bool readImage(Reader &reader, ImageCommand *image) {
|
|
520
|
+
image->id = reader.u64();
|
|
521
|
+
image->has_src = reader.u8() != 0;
|
|
522
|
+
if (image->has_src) image->src = readRect(reader);
|
|
523
|
+
image->dst = readRect(reader);
|
|
524
|
+
image->opacity = reader.f32();
|
|
525
|
+
image->fit = reader.u8();
|
|
526
|
+
image->sampling = reader.u8();
|
|
527
|
+
image->radius = readRadius(reader);
|
|
528
|
+
return !reader.failed() && image->id != 0 && image->fit <= 2 && image->sampling <= 1;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
static bool readText(Reader &reader, TextCommand *text) {
|
|
532
|
+
text->font_id = reader.u64();
|
|
533
|
+
text->size = reader.f32();
|
|
534
|
+
text->origin = readPoint(reader);
|
|
535
|
+
text->color = readColor(reader);
|
|
536
|
+
text->text = reader.string();
|
|
537
|
+
text->has_positioned_glyphs = reader.u8() != 0;
|
|
538
|
+
if (text->has_positioned_glyphs) {
|
|
539
|
+
const uint32_t glyph_count = reader.u32();
|
|
540
|
+
/* Every glyph consumes at least id u16 + flags u8 + three f32s;
|
|
541
|
+
* font overrides add u64. Bound both allocation and framing. */
|
|
542
|
+
if (reader.failed() || glyph_count > 65536 || glyph_count > reader.remaining() / 15) return false;
|
|
543
|
+
text->positioned_glyphs.reserve(glyph_count);
|
|
544
|
+
for (uint32_t index = 0; index < glyph_count; ++index) {
|
|
545
|
+
PositionedGlyph glyph;
|
|
546
|
+
glyph.id = reader.u16();
|
|
547
|
+
const uint8_t flags = reader.u8();
|
|
548
|
+
if (flags > 1) return false;
|
|
549
|
+
glyph.font_id = (flags & 1) != 0 ? reader.u64() : text->font_id;
|
|
550
|
+
glyph.x = reader.f32();
|
|
551
|
+
glyph.baseline = reader.f32();
|
|
552
|
+
glyph.advance = reader.f32();
|
|
553
|
+
text->positioned_glyphs.push_back(glyph);
|
|
554
|
+
}
|
|
555
|
+
const uint32_t fragment_count = reader.u32();
|
|
556
|
+
if (reader.failed() || fragment_count > 64 || fragment_count > reader.remaining() / 12) return false;
|
|
557
|
+
text->positioned_fragments.reserve(fragment_count);
|
|
558
|
+
for (uint32_t index = 0; index < fragment_count; ++index) {
|
|
559
|
+
PositionedTextFragment fragment;
|
|
560
|
+
fragment.x = reader.f32();
|
|
561
|
+
fragment.baseline = reader.f32();
|
|
562
|
+
fragment.text = reader.string();
|
|
563
|
+
text->positioned_fragments.push_back(std::move(fragment));
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
text->has_layout = reader.u8() != 0;
|
|
567
|
+
if (!text->has_layout) return !reader.failed();
|
|
568
|
+
text->max_width = reader.f32();
|
|
569
|
+
text->line_height = reader.f32();
|
|
570
|
+
text->wrap = reader.u8();
|
|
571
|
+
text->align = reader.u8();
|
|
572
|
+
if (text->wrap > 2 || text->align > 2) return false;
|
|
573
|
+
text->has_lines = reader.u8() != 0;
|
|
574
|
+
if (!text->has_lines) return !reader.failed();
|
|
575
|
+
const uint32_t count = reader.u32();
|
|
576
|
+
if (reader.failed() || count > reader.remaining() || count > 4096) return false;
|
|
577
|
+
text->lines.reserve(count);
|
|
578
|
+
for (uint32_t index = 0; index < count; ++index) {
|
|
579
|
+
TextLine line;
|
|
580
|
+
line.x = reader.f32();
|
|
581
|
+
line.baseline = reader.f32();
|
|
582
|
+
line.text = reader.string();
|
|
583
|
+
text->lines.push_back(std::move(line));
|
|
584
|
+
}
|
|
585
|
+
return !reader.failed();
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
static bool readEffect(Reader &reader, Effect *effect) {
|
|
589
|
+
switch (reader.u8()) {
|
|
590
|
+
case 1:
|
|
591
|
+
effect->kind = Effect::Kind::shadow;
|
|
592
|
+
effect->rect = readRect(reader);
|
|
593
|
+
effect->radius = readRadius(reader);
|
|
594
|
+
effect->offset = readPoint(reader);
|
|
595
|
+
effect->blur = reader.f32();
|
|
596
|
+
effect->spread = reader.f32();
|
|
597
|
+
effect->color = readColor(reader);
|
|
598
|
+
break;
|
|
599
|
+
case 2:
|
|
600
|
+
effect->kind = Effect::Kind::blur;
|
|
601
|
+
effect->rect = readRect(reader);
|
|
602
|
+
effect->blur = reader.f32();
|
|
603
|
+
break;
|
|
604
|
+
default:
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
return !reader.failed();
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
enum : uint8_t {
|
|
611
|
+
kCommandFlagId = 0x01,
|
|
612
|
+
kCommandFlagClip = 0x02,
|
|
613
|
+
kCommandFlagTransform = 0x04,
|
|
614
|
+
kCommandFlagShape = 0x08,
|
|
615
|
+
kCommandFlagPaint = 0x10,
|
|
616
|
+
kCommandFlagImage = 0x20,
|
|
617
|
+
kCommandFlagText = 0x40,
|
|
618
|
+
kCommandFlagEffect = 0x80,
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
static bool readCommand(Reader &reader, Command *command) {
|
|
622
|
+
command->kind = reader.u8();
|
|
623
|
+
const uint8_t flags = reader.u8();
|
|
624
|
+
command->bounds = readRect(reader);
|
|
625
|
+
command->opacity = reader.f32();
|
|
626
|
+
command->stroke_width = reader.f32();
|
|
627
|
+
command->cap = reader.u8();
|
|
628
|
+
if (reader.failed() || command->kind > 13 || command->cap > 1) return false;
|
|
629
|
+
if (flags & kCommandFlagId) {
|
|
630
|
+
command->has_id = true;
|
|
631
|
+
command->id = reader.u64();
|
|
632
|
+
}
|
|
633
|
+
if (flags & kCommandFlagClip) {
|
|
634
|
+
command->has_clip = true;
|
|
635
|
+
command->clip = readRect(reader);
|
|
636
|
+
}
|
|
637
|
+
if (flags & kCommandFlagTransform) {
|
|
638
|
+
command->has_transform = true;
|
|
639
|
+
command->transform = {reader.f32(), reader.f32(), reader.f32(), reader.f32(), reader.f32(), reader.f32()};
|
|
640
|
+
}
|
|
641
|
+
if ((flags & kCommandFlagShape) && !readShape(reader, &command->shape)) return false;
|
|
642
|
+
if ((flags & kCommandFlagPaint) && !readPaint(reader, &command->paint)) return false;
|
|
643
|
+
if ((flags & kCommandFlagImage) && !readImage(reader, &command->image)) return false;
|
|
644
|
+
if ((flags & kCommandFlagText) && !readText(reader, &command->text)) return false;
|
|
645
|
+
if ((flags & kCommandFlagEffect) && !readEffect(reader, &command->effect)) return false;
|
|
646
|
+
if (reader.failed()) return false;
|
|
647
|
+
|
|
648
|
+
/* Treat the packet as an untrusted ABI boundary even though today's
|
|
649
|
+
* producer lives in the same process. Every command kind has one
|
|
650
|
+
* exact payload shape; accepting unrelated optional sections would
|
|
651
|
+
* let a corrupt packet advance the reader successfully but silently
|
|
652
|
+
* draw a different command than the bytes describe. */
|
|
653
|
+
const uint8_t payload_flags = flags &
|
|
654
|
+
(kCommandFlagShape | kCommandFlagPaint | kCommandFlagImage | kCommandFlagText | kCommandFlagEffect);
|
|
655
|
+
switch (command->kind) {
|
|
656
|
+
case 0:
|
|
657
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
658
|
+
command->shape.kind == Shape::Kind::rect && command->paint.kind == Paint::Kind::color;
|
|
659
|
+
case 1:
|
|
660
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
661
|
+
command->shape.kind == Shape::Kind::rect && command->paint.kind == Paint::Kind::linear_gradient;
|
|
662
|
+
case 2:
|
|
663
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
664
|
+
command->shape.kind == Shape::Kind::rounded_rect && command->paint.kind == Paint::Kind::color;
|
|
665
|
+
case 3:
|
|
666
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
667
|
+
command->shape.kind == Shape::Kind::rounded_rect && command->paint.kind == Paint::Kind::linear_gradient;
|
|
668
|
+
case 4:
|
|
669
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
670
|
+
command->shape.kind == Shape::Kind::stroke_rect && command->paint.kind == Paint::Kind::color;
|
|
671
|
+
case 5:
|
|
672
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
673
|
+
command->shape.kind == Shape::Kind::stroke_rect && command->paint.kind == Paint::Kind::linear_gradient;
|
|
674
|
+
case 6:
|
|
675
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
676
|
+
command->shape.kind == Shape::Kind::line && command->paint.kind == Paint::Kind::color;
|
|
677
|
+
case 7:
|
|
678
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
679
|
+
command->shape.kind == Shape::Kind::line && command->paint.kind == Paint::Kind::linear_gradient;
|
|
680
|
+
case 8: case 9:
|
|
681
|
+
return payload_flags == (kCommandFlagShape | kCommandFlagPaint) &&
|
|
682
|
+
command->shape.kind == Shape::Kind::path && command->paint.kind != Paint::Kind::none;
|
|
683
|
+
case 10:
|
|
684
|
+
return payload_flags == kCommandFlagImage && command->image.id != 0;
|
|
685
|
+
case 11:
|
|
686
|
+
/* The shared model keeps the text color both in the text
|
|
687
|
+
* payload and in its resource paint metadata, so both
|
|
688
|
+
* sections intentionally ride the canonical wire packet. */
|
|
689
|
+
return payload_flags == (kCommandFlagPaint | kCommandFlagText) &&
|
|
690
|
+
command->paint.kind == Paint::Kind::color;
|
|
691
|
+
case 12:
|
|
692
|
+
return payload_flags == kCommandFlagEffect && command->effect.kind == Effect::Kind::shadow;
|
|
693
|
+
case 13:
|
|
694
|
+
return payload_flags == kCommandFlagEffect && command->effect.kind == Effect::Kind::blur;
|
|
695
|
+
default:
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
static bool readKeyedCommands(Reader &reader, std::vector<KeyedCommand> *commands, uint32_t count) {
|
|
701
|
+
if (reader.failed() || count > kRetainedCommandCap || count > reader.remaining()) return false;
|
|
702
|
+
commands->reserve(count);
|
|
703
|
+
for (uint32_t index = 0; index < count; ++index) {
|
|
704
|
+
KeyedCommand keyed;
|
|
705
|
+
keyed.key = reader.u64();
|
|
706
|
+
if (!readCommand(reader, &keyed.command)) return false;
|
|
707
|
+
commands->push_back(std::move(keyed));
|
|
708
|
+
}
|
|
709
|
+
return true;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
static bool decodePacket(const uint8_t *bytes, size_t length, DecodedPacket *packet) {
|
|
713
|
+
if (!bytes || length < 16 || std::memcmp(bytes, "NSGP", 4) != 0) return false;
|
|
714
|
+
Reader reader(bytes, length);
|
|
715
|
+
uint8_t magic[4] = {};
|
|
716
|
+
if (!reader.bytes(magic, sizeof(magic)) || reader.u8() != kPacketVersion) return false;
|
|
717
|
+
packet->load_action = reader.u8();
|
|
718
|
+
const uint8_t flags = reader.u8();
|
|
719
|
+
(void)reader.u8();
|
|
720
|
+
packet->generation = reader.u64();
|
|
721
|
+
if (packet->load_action < 1 || packet->load_action > 3 || (flags & ~0x03u) != 0) return false;
|
|
722
|
+
|
|
723
|
+
packet->has_scissor = (flags & 0x01) != 0;
|
|
724
|
+
if (packet->has_scissor) packet->scissor = readRect(reader);
|
|
725
|
+
if (flags & 0x02) {
|
|
726
|
+
if (!packet->has_scissor) return false;
|
|
727
|
+
const uint32_t count = reader.u32();
|
|
728
|
+
if (reader.failed() || count == 0 || count > kDirtyRectCap) return false;
|
|
729
|
+
packet->dirty_rects.reserve(count);
|
|
730
|
+
for (uint32_t index = 0; index < count; ++index) packet->dirty_rects.push_back(readRect(reader));
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
const uint32_t image_count = reader.u32();
|
|
734
|
+
if (reader.failed() || image_count > reader.remaining() || image_count > 65536) return false;
|
|
735
|
+
packet->images.reserve(image_count);
|
|
736
|
+
for (uint32_t index = 0; index < image_count; ++index) {
|
|
737
|
+
ImageMeta image;
|
|
738
|
+
image.id = reader.u64();
|
|
739
|
+
image.fingerprint = reader.u64();
|
|
740
|
+
image.width = reader.u32();
|
|
741
|
+
image.height = reader.u32();
|
|
742
|
+
packet->images.push_back(image);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
const uint32_t action_count = reader.u32();
|
|
746
|
+
if (reader.failed() || action_count > reader.remaining() || action_count > 65536) return false;
|
|
747
|
+
packet->image_actions.reserve(action_count);
|
|
748
|
+
for (uint32_t index = 0; index < action_count; ++index) {
|
|
749
|
+
ImageAction action;
|
|
750
|
+
action.kind = reader.u8();
|
|
751
|
+
action.id = reader.u64();
|
|
752
|
+
action.fingerprint = reader.u64();
|
|
753
|
+
action.image_index = reader.u32();
|
|
754
|
+
if (action.kind > 2) return false;
|
|
755
|
+
packet->image_actions.push_back(action);
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
if (packet->load_action == 3) {
|
|
759
|
+
const uint32_t evict_count = reader.u32();
|
|
760
|
+
if (reader.failed() || evict_count > kRetainedCommandCap || evict_count > reader.remaining()) return false;
|
|
761
|
+
packet->evicts.reserve(evict_count);
|
|
762
|
+
for (uint32_t index = 0; index < evict_count; ++index) packet->evicts.push_back(reader.u64());
|
|
763
|
+
|
|
764
|
+
const uint32_t upsert_count = reader.u32();
|
|
765
|
+
if (!readKeyedCommands(reader, &packet->upserts, upsert_count)) return false;
|
|
766
|
+
|
|
767
|
+
const uint32_t order_count = reader.u32();
|
|
768
|
+
if (reader.failed() || order_count > kRetainedCommandCap || order_count > reader.remaining()) return false;
|
|
769
|
+
packet->order.reserve(order_count);
|
|
770
|
+
for (uint32_t index = 0; index < order_count; ++index) packet->order.push_back(reader.u64());
|
|
771
|
+
} else {
|
|
772
|
+
const uint32_t command_count = reader.u32();
|
|
773
|
+
if (!readKeyedCommands(reader, &packet->commands, command_count)) return false;
|
|
774
|
+
}
|
|
775
|
+
return reader.finished();
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
struct ImageResource {
|
|
779
|
+
uint32_t width = 0;
|
|
780
|
+
uint32_t height = 0;
|
|
781
|
+
uint64_t serial = 0;
|
|
782
|
+
/* Direct2D's native 32-bit format: premultiplied BGRA, top-down. */
|
|
783
|
+
std::vector<uint8_t> bgra;
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
constexpr bool imageActionResolvesResource(uint8_t kind) {
|
|
787
|
+
return kind == 0 || kind == 1; /* upload or retain */
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
constexpr bool imageMetadataMatchesResource(
|
|
791
|
+
bool resource_present,
|
|
792
|
+
uint32_t metadata_width,
|
|
793
|
+
uint32_t metadata_height,
|
|
794
|
+
uint32_t resource_width,
|
|
795
|
+
uint32_t resource_height
|
|
796
|
+
) {
|
|
797
|
+
/* The renderer-wide store is authoritative. An absent entry is the
|
|
798
|
+
* legitimate "not registered (yet/anymore)" state and draws nothing,
|
|
799
|
+
* even though its packet metadata is therefore 0x0. */
|
|
800
|
+
return !resource_present ||
|
|
801
|
+
(metadata_width != 0 && metadata_height != 0 &&
|
|
802
|
+
metadata_width == resource_width && metadata_height == resource_height);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
static_assert(imageActionResolvesResource(0), "image uploads resolve the renderer-wide store");
|
|
806
|
+
static_assert(imageActionResolvesResource(1), "image retains reconcile the renderer-wide store");
|
|
807
|
+
static_assert(!imageActionResolvesResource(2), "image evictions only remove surface state");
|
|
808
|
+
static_assert(imageMetadataMatchesResource(false, 0, 0, 0, 0),
|
|
809
|
+
"an unregistered image is a valid transient packet resource");
|
|
810
|
+
static_assert(imageMetadataMatchesResource(true, 64, 32, 64, 32),
|
|
811
|
+
"a registered image must match its packet metadata");
|
|
812
|
+
static_assert(!imageMetadataMatchesResource(true, 64, 32, 32, 64),
|
|
813
|
+
"mismatched registered-image metadata is rejected");
|
|
814
|
+
|
|
815
|
+
class FontBytesOwner final : public IUnknown {
|
|
816
|
+
public:
|
|
817
|
+
explicit FontBytesOwner(const uint8_t *bytes, size_t length) : bytes_(bytes, bytes + length) {}
|
|
818
|
+
|
|
819
|
+
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **object) override {
|
|
820
|
+
if (!object) return E_POINTER;
|
|
821
|
+
*object = nullptr;
|
|
822
|
+
if (iid == __uuidof(IUnknown)) {
|
|
823
|
+
*object = static_cast<IUnknown *>(this);
|
|
824
|
+
AddRef();
|
|
825
|
+
return S_OK;
|
|
826
|
+
}
|
|
827
|
+
return E_NOINTERFACE;
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; }
|
|
831
|
+
ULONG STDMETHODCALLTYPE Release() override {
|
|
832
|
+
const ULONG refs = --refs_;
|
|
833
|
+
if (refs == 0) delete this;
|
|
834
|
+
return refs;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
const void *data() const { return bytes_.data(); }
|
|
838
|
+
UINT32 size() const { return static_cast<UINT32>(bytes_.size()); }
|
|
839
|
+
|
|
840
|
+
private:
|
|
841
|
+
std::atomic<ULONG> refs_{1};
|
|
842
|
+
std::vector<uint8_t> bytes_;
|
|
843
|
+
};
|
|
844
|
+
|
|
845
|
+
struct FontResource {
|
|
846
|
+
uint64_t token = 0;
|
|
847
|
+
FontBytesOwner *owner = nullptr;
|
|
848
|
+
IDWriteFontFile *file = nullptr;
|
|
849
|
+
IDWriteFontCollection1 *collection = nullptr;
|
|
850
|
+
std::wstring family;
|
|
851
|
+
|
|
852
|
+
~FontResource() {
|
|
853
|
+
releaseCom(collection);
|
|
854
|
+
releaseCom(file);
|
|
855
|
+
releaseCom(owner);
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
class GpuSurfaceImpl;
|
|
860
|
+
|
|
861
|
+
class GpuRendererImpl final : public WindowsGpuRenderer, public std::enable_shared_from_this<GpuRendererImpl> {
|
|
862
|
+
public:
|
|
863
|
+
GpuRendererImpl() = default;
|
|
864
|
+
~GpuRendererImpl() override {
|
|
865
|
+
fonts_.clear();
|
|
866
|
+
images_.clear();
|
|
867
|
+
releaseCom(font_fallback_);
|
|
868
|
+
if (dwrite_factory_ && memory_font_loader_) dwrite_factory_->UnregisterFontFileLoader(memory_font_loader_);
|
|
869
|
+
releaseCom(memory_font_loader_);
|
|
870
|
+
releaseCom(dwrite_factory5_);
|
|
871
|
+
releaseCom(dwrite_factory_);
|
|
872
|
+
releaseCom(d2d_factory_);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
bool initialize() {
|
|
876
|
+
D2D1_FACTORY_OPTIONS options = {};
|
|
877
|
+
if (FAILED(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, options, &d2d_factory_))) return false;
|
|
878
|
+
IUnknown *unknown = nullptr;
|
|
879
|
+
if (FAILED(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), &unknown)) || !unknown) return false;
|
|
880
|
+
dwrite_factory_ = static_cast<IDWriteFactory *>(unknown);
|
|
881
|
+
if (FAILED(dwrite_factory_->QueryInterface(__uuidof(IDWriteFactory5), reinterpret_cast<void **>(&dwrite_factory5_))) ||
|
|
882
|
+
!dwrite_factory5_) return false;
|
|
883
|
+
if (FAILED(dwrite_factory5_->CreateInMemoryFontFileLoader(&memory_font_loader_)) ||
|
|
884
|
+
!memory_font_loader_) return false;
|
|
885
|
+
if (FAILED(dwrite_factory_->RegisterFontFileLoader(memory_font_loader_))) return false;
|
|
886
|
+
|
|
887
|
+
/* Text layout is planned against one explicit face and the engine's
|
|
888
|
+
* deterministic .notdef advance. An empty custom fallback prevents
|
|
889
|
+
* IDWriteTextLayout from silently substituting machine fonts for
|
|
890
|
+
* missing glyphs; every created layout installs this object below. */
|
|
891
|
+
IDWriteFontFallbackBuilder *fallback_builder = nullptr;
|
|
892
|
+
HRESULT fallback_result = dwrite_factory5_->CreateFontFallbackBuilder(&fallback_builder);
|
|
893
|
+
if (SUCCEEDED(fallback_result) && fallback_builder) {
|
|
894
|
+
fallback_result = fallback_builder->CreateFontFallback(&font_fallback_);
|
|
895
|
+
}
|
|
896
|
+
releaseCom(fallback_builder);
|
|
897
|
+
return SUCCEEDED(fallback_result) && font_fallback_;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
std::shared_ptr<WindowsGpuSurface> createSurface(HWND hwnd) override;
|
|
901
|
+
|
|
902
|
+
bool uploadImage(uint64_t id, uint32_t width, uint32_t height, const uint8_t *rgba, size_t rgba_len) override {
|
|
903
|
+
if (id == 0 || width == 0 || height == 0 || !rgba) return false;
|
|
904
|
+
const size_t pixels = static_cast<size_t>(width) * static_cast<size_t>(height);
|
|
905
|
+
if (pixels > std::numeric_limits<size_t>::max() / 4 || rgba_len != pixels * 4) return false;
|
|
906
|
+
auto resource = std::make_shared<ImageResource>();
|
|
907
|
+
resource->width = width;
|
|
908
|
+
resource->height = height;
|
|
909
|
+
resource->serial = next_resource_serial_++;
|
|
910
|
+
resource->bgra.resize(rgba_len);
|
|
911
|
+
for (size_t index = 0; index < pixels; ++index) {
|
|
912
|
+
const uint8_t *source = rgba + index * 4;
|
|
913
|
+
uint8_t *destination = resource->bgra.data() + index * 4;
|
|
914
|
+
const uint32_t alpha = source[3];
|
|
915
|
+
destination[0] = static_cast<uint8_t>((static_cast<uint32_t>(source[2]) * alpha + 127) / 255);
|
|
916
|
+
destination[1] = static_cast<uint8_t>((static_cast<uint32_t>(source[1]) * alpha + 127) / 255);
|
|
917
|
+
destination[2] = static_cast<uint8_t>((static_cast<uint32_t>(source[0]) * alpha + 127) / 255);
|
|
918
|
+
destination[3] = source[3];
|
|
919
|
+
}
|
|
920
|
+
images_[id] = std::move(resource);
|
|
921
|
+
return true;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
bool removeImage(uint64_t id) override {
|
|
925
|
+
if (id == 0) return false;
|
|
926
|
+
images_.erase(id);
|
|
927
|
+
return true;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
bool registerFont(uint64_t id, const uint8_t *ttf, size_t ttf_len, uint64_t *token) override {
|
|
931
|
+
if (!token || id == 0 || !ttf || ttf_len == 0 || ttf_len > UINT32_MAX ||
|
|
932
|
+
!dwrite_factory5_ || !memory_font_loader_) return false;
|
|
933
|
+
|
|
934
|
+
auto resource = std::make_shared<FontResource>();
|
|
935
|
+
resource->owner = new (std::nothrow) FontBytesOwner(ttf, ttf_len);
|
|
936
|
+
if (!resource->owner) return false;
|
|
937
|
+
if (FAILED(memory_font_loader_->CreateInMemoryFontFileReference(
|
|
938
|
+
dwrite_factory_, resource->owner->data(), resource->owner->size(), resource->owner, &resource->file)) || !resource->file) {
|
|
939
|
+
return false;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
IDWriteFontSetBuilder1 *builder = nullptr;
|
|
943
|
+
IDWriteFontSet *font_set = nullptr;
|
|
944
|
+
HRESULT result = dwrite_factory5_->CreateFontSetBuilder(&builder);
|
|
945
|
+
if (SUCCEEDED(result)) result = builder->AddFontFile(resource->file);
|
|
946
|
+
if (SUCCEEDED(result)) result = builder->CreateFontSet(&font_set);
|
|
947
|
+
if (SUCCEEDED(result)) result = dwrite_factory5_->CreateFontCollectionFromFontSet(font_set, &resource->collection);
|
|
948
|
+
releaseCom(font_set);
|
|
949
|
+
releaseCom(builder);
|
|
950
|
+
if (FAILED(result) || !resource->collection || resource->collection->GetFontFamilyCount() == 0) return false;
|
|
951
|
+
|
|
952
|
+
IDWriteFontFamily1 *family = nullptr;
|
|
953
|
+
IDWriteLocalizedStrings *names = nullptr;
|
|
954
|
+
result = resource->collection->GetFontFamily(0, &family);
|
|
955
|
+
if (SUCCEEDED(result)) result = family->GetFamilyNames(&names);
|
|
956
|
+
UINT32 length = 0;
|
|
957
|
+
if (SUCCEEDED(result)) result = names->GetStringLength(0, &length);
|
|
958
|
+
if (SUCCEEDED(result)) {
|
|
959
|
+
resource->family.resize(static_cast<size_t>(length) + 1);
|
|
960
|
+
result = names->GetString(0, resource->family.data(), length + 1);
|
|
961
|
+
if (SUCCEEDED(result)) resource->family.resize(length);
|
|
962
|
+
}
|
|
963
|
+
releaseCom(names);
|
|
964
|
+
releaseCom(family);
|
|
965
|
+
if (FAILED(result) || resource->family.empty()) return false;
|
|
966
|
+
|
|
967
|
+
resource->token = next_font_token_++;
|
|
968
|
+
if (resource->token == 0) resource->token = next_font_token_++;
|
|
969
|
+
fonts_[id] = resource;
|
|
970
|
+
*token = resource->token;
|
|
971
|
+
return true;
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
bool unregisterFont(uint64_t id, uint64_t token) override {
|
|
975
|
+
auto found = fonts_.find(id);
|
|
976
|
+
if (found != fonts_.end() && found->second->token == token) fonts_.erase(found);
|
|
977
|
+
return true;
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
ID2D1Factory *d2dFactory() const { return d2d_factory_; }
|
|
981
|
+
IDWriteFactory *dwriteFactory() const { return dwrite_factory_; }
|
|
982
|
+
IDWriteFontFallback *fontFallback() const { return font_fallback_; }
|
|
983
|
+
|
|
984
|
+
std::shared_ptr<ImageResource> image(uint64_t id) const {
|
|
985
|
+
auto found = images_.find(id);
|
|
986
|
+
return found == images_.end() ? nullptr : found->second;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
std::shared_ptr<FontResource> font(uint64_t id) const {
|
|
990
|
+
auto found = fonts_.find(id);
|
|
991
|
+
return found == fonts_.end() ? nullptr : found->second;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
private:
|
|
995
|
+
ID2D1Factory *d2d_factory_ = nullptr;
|
|
996
|
+
IDWriteFactory *dwrite_factory_ = nullptr;
|
|
997
|
+
IDWriteFactory5 *dwrite_factory5_ = nullptr;
|
|
998
|
+
IDWriteInMemoryFontFileLoader *memory_font_loader_ = nullptr;
|
|
999
|
+
IDWriteFontFallback *font_fallback_ = nullptr;
|
|
1000
|
+
std::map<uint64_t, std::shared_ptr<ImageResource>> images_;
|
|
1001
|
+
std::map<uint64_t, std::shared_ptr<FontResource>> fonts_;
|
|
1002
|
+
uint64_t next_resource_serial_ = 1;
|
|
1003
|
+
uint64_t next_font_token_ = 1;
|
|
1004
|
+
};
|
|
1005
|
+
|
|
1006
|
+
static bool makeRoundedGeometry(ID2D1Factory *factory, Rect input, Radius input_radius, ID2D1PathGeometry **geometry) {
|
|
1007
|
+
if (!factory || !geometry) return false;
|
|
1008
|
+
*geometry = nullptr;
|
|
1009
|
+
const Rect rect = normalized(input);
|
|
1010
|
+
const float limit = std::max(0.0f, std::min(rect.width, rect.height) * 0.5f);
|
|
1011
|
+
const float tl = std::max(0.0f, std::min(limit, input_radius.top_left));
|
|
1012
|
+
const float tr = std::max(0.0f, std::min(limit, input_radius.top_right));
|
|
1013
|
+
const float br = std::max(0.0f, std::min(limit, input_radius.bottom_right));
|
|
1014
|
+
const float bl = std::max(0.0f, std::min(limit, input_radius.bottom_left));
|
|
1015
|
+
ID2D1PathGeometry *path = nullptr;
|
|
1016
|
+
ID2D1GeometrySink *sink = nullptr;
|
|
1017
|
+
HRESULT result = factory->CreatePathGeometry(&path);
|
|
1018
|
+
if (SUCCEEDED(result)) result = path->Open(&sink);
|
|
1019
|
+
if (FAILED(result) || !sink) {
|
|
1020
|
+
releaseCom(sink);
|
|
1021
|
+
releaseCom(path);
|
|
1022
|
+
return false;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
const float x0 = rect.x;
|
|
1026
|
+
const float y0 = rect.y;
|
|
1027
|
+
const float x1 = rect.x + rect.width;
|
|
1028
|
+
const float y1 = rect.y + rect.height;
|
|
1029
|
+
sink->BeginFigure(D2D1::Point2F(x0 + tl, y0), D2D1_FIGURE_BEGIN_FILLED);
|
|
1030
|
+
sink->AddLine(D2D1::Point2F(x1 - tr, y0));
|
|
1031
|
+
if (tr > 0) sink->AddBezier(D2D1::BezierSegment(
|
|
1032
|
+
D2D1::Point2F(x1 - tr + tr * kBezierCircle, y0),
|
|
1033
|
+
D2D1::Point2F(x1, y0 + tr - tr * kBezierCircle),
|
|
1034
|
+
D2D1::Point2F(x1, y0 + tr)));
|
|
1035
|
+
else sink->AddLine(D2D1::Point2F(x1, y0));
|
|
1036
|
+
sink->AddLine(D2D1::Point2F(x1, y1 - br));
|
|
1037
|
+
if (br > 0) sink->AddBezier(D2D1::BezierSegment(
|
|
1038
|
+
D2D1::Point2F(x1, y1 - br + br * kBezierCircle),
|
|
1039
|
+
D2D1::Point2F(x1 - br + br * kBezierCircle, y1),
|
|
1040
|
+
D2D1::Point2F(x1 - br, y1)));
|
|
1041
|
+
else sink->AddLine(D2D1::Point2F(x1, y1));
|
|
1042
|
+
sink->AddLine(D2D1::Point2F(x0 + bl, y1));
|
|
1043
|
+
if (bl > 0) sink->AddBezier(D2D1::BezierSegment(
|
|
1044
|
+
D2D1::Point2F(x0 + bl - bl * kBezierCircle, y1),
|
|
1045
|
+
D2D1::Point2F(x0, y1 - bl + bl * kBezierCircle),
|
|
1046
|
+
D2D1::Point2F(x0, y1 - bl)));
|
|
1047
|
+
else sink->AddLine(D2D1::Point2F(x0, y1));
|
|
1048
|
+
sink->AddLine(D2D1::Point2F(x0, y0 + tl));
|
|
1049
|
+
if (tl > 0) sink->AddBezier(D2D1::BezierSegment(
|
|
1050
|
+
D2D1::Point2F(x0, y0 + tl - tl * kBezierCircle),
|
|
1051
|
+
D2D1::Point2F(x0 + tl - tl * kBezierCircle, y0),
|
|
1052
|
+
D2D1::Point2F(x0 + tl, y0)));
|
|
1053
|
+
else sink->AddLine(D2D1::Point2F(x0, y0));
|
|
1054
|
+
sink->EndFigure(D2D1_FIGURE_END_CLOSED);
|
|
1055
|
+
result = sink->Close();
|
|
1056
|
+
releaseCom(sink);
|
|
1057
|
+
if (FAILED(result)) {
|
|
1058
|
+
releaseCom(path);
|
|
1059
|
+
return false;
|
|
1060
|
+
}
|
|
1061
|
+
*geometry = path;
|
|
1062
|
+
return true;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
static bool makePathGeometry(ID2D1Factory *factory, const Shape &shape, bool filled, ID2D1PathGeometry **geometry) {
|
|
1066
|
+
if (!factory || !geometry || shape.kind != Shape::Kind::path) return false;
|
|
1067
|
+
*geometry = nullptr;
|
|
1068
|
+
ID2D1PathGeometry *path = nullptr;
|
|
1069
|
+
ID2D1GeometrySink *sink = nullptr;
|
|
1070
|
+
HRESULT result = factory->CreatePathGeometry(&path);
|
|
1071
|
+
if (SUCCEEDED(result)) result = path->Open(&sink);
|
|
1072
|
+
if (FAILED(result) || !sink) {
|
|
1073
|
+
releaseCom(sink);
|
|
1074
|
+
releaseCom(path);
|
|
1075
|
+
return false;
|
|
1076
|
+
}
|
|
1077
|
+
bool figure_open = false;
|
|
1078
|
+
for (const PathElement &element : shape.path) {
|
|
1079
|
+
switch (element.verb) {
|
|
1080
|
+
case PathElement::Verb::move:
|
|
1081
|
+
if (figure_open) sink->EndFigure(D2D1_FIGURE_END_OPEN);
|
|
1082
|
+
sink->BeginFigure(D2D1::Point2F(element.points[0].x, element.points[0].y),
|
|
1083
|
+
filled ? D2D1_FIGURE_BEGIN_FILLED : D2D1_FIGURE_BEGIN_HOLLOW);
|
|
1084
|
+
figure_open = true;
|
|
1085
|
+
break;
|
|
1086
|
+
case PathElement::Verb::line:
|
|
1087
|
+
if (!figure_open) { result = E_INVALIDARG; }
|
|
1088
|
+
else sink->AddLine(D2D1::Point2F(element.points[0].x, element.points[0].y));
|
|
1089
|
+
break;
|
|
1090
|
+
case PathElement::Verb::quadratic:
|
|
1091
|
+
if (!figure_open) { result = E_INVALIDARG; }
|
|
1092
|
+
else sink->AddQuadraticBezier(D2D1::QuadraticBezierSegment(
|
|
1093
|
+
D2D1::Point2F(element.points[0].x, element.points[0].y),
|
|
1094
|
+
D2D1::Point2F(element.points[1].x, element.points[1].y)));
|
|
1095
|
+
break;
|
|
1096
|
+
case PathElement::Verb::cubic:
|
|
1097
|
+
if (!figure_open) { result = E_INVALIDARG; }
|
|
1098
|
+
else sink->AddBezier(D2D1::BezierSegment(
|
|
1099
|
+
D2D1::Point2F(element.points[0].x, element.points[0].y),
|
|
1100
|
+
D2D1::Point2F(element.points[1].x, element.points[1].y),
|
|
1101
|
+
D2D1::Point2F(element.points[2].x, element.points[2].y)));
|
|
1102
|
+
break;
|
|
1103
|
+
case PathElement::Verb::close:
|
|
1104
|
+
if (!figure_open) { result = E_INVALIDARG; }
|
|
1105
|
+
else {
|
|
1106
|
+
sink->EndFigure(D2D1_FIGURE_END_CLOSED);
|
|
1107
|
+
figure_open = false;
|
|
1108
|
+
}
|
|
1109
|
+
break;
|
|
1110
|
+
}
|
|
1111
|
+
if (FAILED(result)) break;
|
|
1112
|
+
}
|
|
1113
|
+
if (figure_open) sink->EndFigure(D2D1_FIGURE_END_OPEN);
|
|
1114
|
+
if (SUCCEEDED(result)) result = sink->Close();
|
|
1115
|
+
releaseCom(sink);
|
|
1116
|
+
if (FAILED(result)) {
|
|
1117
|
+
releaseCom(path);
|
|
1118
|
+
return false;
|
|
1119
|
+
}
|
|
1120
|
+
*geometry = path;
|
|
1121
|
+
return true;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
class GpuSurfaceImpl final : public WindowsGpuSurface {
|
|
1125
|
+
public:
|
|
1126
|
+
GpuSurfaceImpl(std::shared_ptr<GpuRendererImpl> renderer, HWND hwnd) : renderer_(std::move(renderer)), hwnd_(hwnd) {
|
|
1127
|
+
D2D1_STROKE_STYLE_PROPERTIES style = D2D1::StrokeStyleProperties();
|
|
1128
|
+
style.startCap = D2D1_CAP_STYLE_FLAT;
|
|
1129
|
+
style.endCap = D2D1_CAP_STYLE_FLAT;
|
|
1130
|
+
style.dashCap = D2D1_CAP_STYLE_FLAT;
|
|
1131
|
+
style.lineJoin = D2D1_LINE_JOIN_MITER;
|
|
1132
|
+
renderer_->d2dFactory()->CreateStrokeStyle(style, nullptr, 0, &rect_stroke_);
|
|
1133
|
+
style.lineJoin = D2D1_LINE_JOIN_ROUND;
|
|
1134
|
+
renderer_->d2dFactory()->CreateStrokeStyle(style, nullptr, 0, &butt_stroke_);
|
|
1135
|
+
style.startCap = D2D1_CAP_STYLE_ROUND;
|
|
1136
|
+
style.endCap = D2D1_CAP_STYLE_ROUND;
|
|
1137
|
+
style.dashCap = D2D1_CAP_STYLE_ROUND;
|
|
1138
|
+
renderer_->d2dFactory()->CreateStrokeStyle(style, nullptr, 0, &round_stroke_);
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
~GpuSurfaceImpl() override {
|
|
1142
|
+
releaseDeviceResources(false);
|
|
1143
|
+
releaseCom(round_stroke_);
|
|
1144
|
+
releaseCom(butt_stroke_);
|
|
1145
|
+
releaseCom(rect_stroke_);
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
int present(const WindowsGpuPacketPresent &present, WindowsGpuPresentInfo *info) override;
|
|
1149
|
+
bool paint(const RECT *paint_rects, size_t paint_rect_count) override;
|
|
1150
|
+
void abandonContent() override { releaseDeviceResources(true); }
|
|
1151
|
+
bool hasContent() const override { return content_valid_ && backing_bitmap_; }
|
|
1152
|
+
bool readColorAt(double logical_x, double logical_y, uint32_t *color) override;
|
|
1153
|
+
uint32_t representativeColorAt(double logical_x, double logical_y) const override;
|
|
1154
|
+
|
|
1155
|
+
private:
|
|
1156
|
+
struct CachedBitmap {
|
|
1157
|
+
uint64_t serial = 0;
|
|
1158
|
+
ID2D1Bitmap *bitmap = nullptr;
|
|
1159
|
+
};
|
|
1160
|
+
|
|
1161
|
+
void releaseImageBitmaps() {
|
|
1162
|
+
for (auto &entry : image_bitmaps_) releaseCom(entry.second.bitmap);
|
|
1163
|
+
image_bitmaps_.clear();
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
void releaseImageBitmap(uint64_t id) {
|
|
1167
|
+
auto found = image_bitmaps_.find(id);
|
|
1168
|
+
if (found == image_bitmaps_.end()) return;
|
|
1169
|
+
releaseCom(found->second.bitmap);
|
|
1170
|
+
image_bitmaps_.erase(found);
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
void releaseDeviceResources(bool drop_retained) {
|
|
1174
|
+
releaseImageBitmaps();
|
|
1175
|
+
releaseCom(blur_snapshot_);
|
|
1176
|
+
releaseCom(backing_bitmap_);
|
|
1177
|
+
releaseCom(backing_target_);
|
|
1178
|
+
releaseCom(window_target_);
|
|
1179
|
+
content_valid_ = false;
|
|
1180
|
+
if (drop_retained) {
|
|
1181
|
+
retained_valid_ = false;
|
|
1182
|
+
retained_commands_.clear();
|
|
1183
|
+
retained_order_.clear();
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
bool ensureWindowTarget() {
|
|
1188
|
+
if (window_target_) return true;
|
|
1189
|
+
RECT client = {};
|
|
1190
|
+
if (!hwnd_ || !GetClientRect(hwnd_, &client)) return false;
|
|
1191
|
+
const UINT width = static_cast<UINT>(std::max<LONG>(1, client.right - client.left));
|
|
1192
|
+
const UINT height = static_cast<UINT>(std::max<LONG>(1, client.bottom - client.top));
|
|
1193
|
+
const D2D1_RENDER_TARGET_PROPERTIES properties = D2D1::RenderTargetProperties(
|
|
1194
|
+
D2D1_RENDER_TARGET_TYPE_HARDWARE,
|
|
1195
|
+
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
|
|
1196
|
+
static_cast<FLOAT>(96.0 * scale_), static_cast<FLOAT>(96.0 * scale_),
|
|
1197
|
+
D2D1_RENDER_TARGET_USAGE_NONE,
|
|
1198
|
+
D2D1_FEATURE_LEVEL_DEFAULT);
|
|
1199
|
+
const D2D1_HWND_RENDER_TARGET_PROPERTIES hwnd_properties = D2D1::HwndRenderTargetProperties(
|
|
1200
|
+
hwnd_, D2D1::SizeU(width, height), D2D1_PRESENT_OPTIONS_NONE);
|
|
1201
|
+
return SUCCEEDED(renderer_->d2dFactory()->CreateHwndRenderTarget(properties, hwnd_properties, &window_target_));
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
bool ensureTargets(double surface_width, double surface_height, double scale, uint32_t pixel_width, uint32_t pixel_height) {
|
|
1205
|
+
const bool dimensions_changed = backing_target_ &&
|
|
1206
|
+
(pixel_width_ != pixel_width || pixel_height_ != pixel_height || scale_ != scale ||
|
|
1207
|
+
surface_width_ != surface_width || surface_height_ != surface_height);
|
|
1208
|
+
if (dimensions_changed) {
|
|
1209
|
+
releaseImageBitmaps();
|
|
1210
|
+
releaseCom(blur_snapshot_);
|
|
1211
|
+
releaseCom(backing_bitmap_);
|
|
1212
|
+
releaseCom(backing_target_);
|
|
1213
|
+
content_valid_ = false;
|
|
1214
|
+
}
|
|
1215
|
+
scale_ = scale;
|
|
1216
|
+
surface_width_ = surface_width;
|
|
1217
|
+
surface_height_ = surface_height;
|
|
1218
|
+
pixel_width_ = pixel_width;
|
|
1219
|
+
pixel_height_ = pixel_height;
|
|
1220
|
+
|
|
1221
|
+
if (window_target_) window_target_->SetDpi(static_cast<FLOAT>(96.0 * scale_), static_cast<FLOAT>(96.0 * scale_));
|
|
1222
|
+
if (!ensureWindowTarget()) return false;
|
|
1223
|
+
if (backing_target_) return true;
|
|
1224
|
+
|
|
1225
|
+
const D2D1_SIZE_F desired = D2D1::SizeF(static_cast<FLOAT>(surface_width_), static_cast<FLOAT>(surface_height_));
|
|
1226
|
+
const D2D1_SIZE_U pixels = D2D1::SizeU(pixel_width_, pixel_height_);
|
|
1227
|
+
const D2D1_PIXEL_FORMAT format = D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
|
|
1228
|
+
HRESULT result = window_target_->CreateCompatibleRenderTarget(
|
|
1229
|
+
&desired, &pixels, &format, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_GDI_COMPATIBLE, &backing_target_);
|
|
1230
|
+
if (SUCCEEDED(result) && backing_target_) {
|
|
1231
|
+
backing_target_->SetDpi(static_cast<FLOAT>(96.0 * scale_), static_cast<FLOAT>(96.0 * scale_));
|
|
1232
|
+
}
|
|
1233
|
+
return SUCCEEDED(result) && backing_target_;
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
bool makeBrush(const Paint &paint, float opacity, ID2D1Brush **brush) {
|
|
1237
|
+
if (!brush || !backing_target_) return false;
|
|
1238
|
+
*brush = nullptr;
|
|
1239
|
+
if (paint.kind == Paint::Kind::color) {
|
|
1240
|
+
ID2D1SolidColorBrush *solid = nullptr;
|
|
1241
|
+
if (FAILED(backing_target_->CreateSolidColorBrush(d2dColor(paint.color, opacity), &solid))) return false;
|
|
1242
|
+
*brush = solid;
|
|
1243
|
+
return true;
|
|
1244
|
+
}
|
|
1245
|
+
if (paint.kind != Paint::Kind::linear_gradient || paint.stops.empty()) return false;
|
|
1246
|
+
std::vector<D2D1_GRADIENT_STOP> stops;
|
|
1247
|
+
stops.reserve(paint.stops.size());
|
|
1248
|
+
for (const GradientStop &source : paint.stops) {
|
|
1249
|
+
D2D1_GRADIENT_STOP stop = {};
|
|
1250
|
+
stop.position = clamp01(source.offset);
|
|
1251
|
+
stop.color = d2dColor(source.color, opacity);
|
|
1252
|
+
stops.push_back(stop);
|
|
1253
|
+
}
|
|
1254
|
+
ID2D1GradientStopCollection *collection = nullptr;
|
|
1255
|
+
ID2D1LinearGradientBrush *gradient = nullptr;
|
|
1256
|
+
HRESULT result = backing_target_->CreateGradientStopCollection(
|
|
1257
|
+
stops.data(), static_cast<UINT32>(stops.size()), D2D1_GAMMA_2_2,
|
|
1258
|
+
D2D1_EXTEND_MODE_CLAMP, &collection);
|
|
1259
|
+
if (SUCCEEDED(result)) {
|
|
1260
|
+
result = backing_target_->CreateLinearGradientBrush(
|
|
1261
|
+
D2D1::LinearGradientBrushProperties(
|
|
1262
|
+
D2D1::Point2F(paint.start.x, paint.start.y),
|
|
1263
|
+
D2D1::Point2F(paint.end.x, paint.end.y)), collection, &gradient);
|
|
1264
|
+
}
|
|
1265
|
+
releaseCom(collection);
|
|
1266
|
+
if (FAILED(result)) {
|
|
1267
|
+
releaseCom(gradient);
|
|
1268
|
+
return false;
|
|
1269
|
+
}
|
|
1270
|
+
*brush = gradient;
|
|
1271
|
+
return true;
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
bool makeShapeGeometry(const Shape &shape, bool filled, ID2D1Geometry **geometry) {
|
|
1275
|
+
if (!geometry) return false;
|
|
1276
|
+
*geometry = nullptr;
|
|
1277
|
+
if (shape.kind == Shape::Kind::rect) {
|
|
1278
|
+
ID2D1RectangleGeometry *rect = nullptr;
|
|
1279
|
+
if (FAILED(renderer_->d2dFactory()->CreateRectangleGeometry(d2dRect(shape.rect), &rect))) return false;
|
|
1280
|
+
*geometry = rect;
|
|
1281
|
+
return true;
|
|
1282
|
+
}
|
|
1283
|
+
if (shape.kind == Shape::Kind::rounded_rect || shape.kind == Shape::Kind::stroke_rect) {
|
|
1284
|
+
ID2D1PathGeometry *rounded = nullptr;
|
|
1285
|
+
if (!makeRoundedGeometry(renderer_->d2dFactory(), shape.rect, shape.radius, &rounded)) return false;
|
|
1286
|
+
*geometry = rounded;
|
|
1287
|
+
return true;
|
|
1288
|
+
}
|
|
1289
|
+
if (shape.kind == Shape::Kind::path) {
|
|
1290
|
+
ID2D1PathGeometry *path = nullptr;
|
|
1291
|
+
if (!makePathGeometry(renderer_->d2dFactory(), shape, filled, &path)) return false;
|
|
1292
|
+
*geometry = path;
|
|
1293
|
+
return true;
|
|
1294
|
+
}
|
|
1295
|
+
return false;
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
bool drawPaintedShape(const Command &command, bool stroke) {
|
|
1299
|
+
const float stroke_width = canvasStrokeWidth(
|
|
1300
|
+
command.shape.kind == Shape::Kind::line ? command.shape.width : command.stroke_width);
|
|
1301
|
+
/* Match the reference renderer: non-positive strokes are no-ops,
|
|
1302
|
+
* while positive fractional widths remain valid Direct2D widths. */
|
|
1303
|
+
if (stroke && stroke_width <= 0) return true;
|
|
1304
|
+
ID2D1Brush *brush = nullptr;
|
|
1305
|
+
if (!makeBrush(command.paint, clamp01(command.opacity), &brush)) return false;
|
|
1306
|
+
if (command.shape.kind == Shape::Kind::line) {
|
|
1307
|
+
if (!stroke) {
|
|
1308
|
+
releaseCom(brush);
|
|
1309
|
+
return false;
|
|
1310
|
+
}
|
|
1311
|
+
backing_target_->DrawLine(
|
|
1312
|
+
D2D1::Point2F(command.shape.from.x, command.shape.from.y),
|
|
1313
|
+
D2D1::Point2F(command.shape.to.x, command.shape.to.y),
|
|
1314
|
+
brush, stroke_width, command.cap == 1 ? round_stroke_ : butt_stroke_);
|
|
1315
|
+
releaseCom(brush);
|
|
1316
|
+
return true;
|
|
1317
|
+
}
|
|
1318
|
+
ID2D1Geometry *geometry = nullptr;
|
|
1319
|
+
if (!makeShapeGeometry(command.shape, !stroke, &geometry)) {
|
|
1320
|
+
releaseCom(brush);
|
|
1321
|
+
return false;
|
|
1322
|
+
}
|
|
1323
|
+
if (stroke) {
|
|
1324
|
+
ID2D1StrokeStyle *stroke_style = command.shape.kind == Shape::Kind::stroke_rect
|
|
1325
|
+
? rect_stroke_
|
|
1326
|
+
: (command.cap == 1 ? round_stroke_ : butt_stroke_);
|
|
1327
|
+
backing_target_->DrawGeometry(geometry, brush, stroke_width, stroke_style);
|
|
1328
|
+
} else {
|
|
1329
|
+
backing_target_->FillGeometry(geometry, brush);
|
|
1330
|
+
}
|
|
1331
|
+
releaseCom(geometry);
|
|
1332
|
+
releaseCom(brush);
|
|
1333
|
+
return true;
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
bool ensureImageBitmap(uint64_t id, ID2D1Bitmap **bitmap) {
|
|
1337
|
+
if (!bitmap || !backing_target_) return false;
|
|
1338
|
+
*bitmap = nullptr;
|
|
1339
|
+
auto resource_found = image_cache_.find(id);
|
|
1340
|
+
if (resource_found == image_cache_.end() || !resource_found->second) return true;
|
|
1341
|
+
const std::shared_ptr<ImageResource> &resource = resource_found->second;
|
|
1342
|
+
CachedBitmap &cached = image_bitmaps_[id];
|
|
1343
|
+
if (cached.bitmap && cached.serial == resource->serial) {
|
|
1344
|
+
*bitmap = cached.bitmap;
|
|
1345
|
+
return true;
|
|
1346
|
+
}
|
|
1347
|
+
releaseCom(cached.bitmap);
|
|
1348
|
+
cached.serial = 0;
|
|
1349
|
+
const D2D1_BITMAP_PROPERTIES properties = D2D1::BitmapProperties(
|
|
1350
|
+
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED), 96.0f, 96.0f);
|
|
1351
|
+
HRESULT result = backing_target_->CreateBitmap(
|
|
1352
|
+
D2D1::SizeU(resource->width, resource->height), resource->bgra.data(),
|
|
1353
|
+
resource->width * 4, properties, &cached.bitmap);
|
|
1354
|
+
if (FAILED(result) || !cached.bitmap) return false;
|
|
1355
|
+
cached.serial = resource->serial;
|
|
1356
|
+
*bitmap = cached.bitmap;
|
|
1357
|
+
return true;
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
bool drawImage(const Command &command) {
|
|
1361
|
+
ID2D1Bitmap *bitmap = nullptr;
|
|
1362
|
+
if (!ensureImageBitmap(command.image.id, &bitmap)) return false;
|
|
1363
|
+
if (!bitmap) return true; /* registered image not available yet */
|
|
1364
|
+
const auto resource_found = image_cache_.find(command.image.id);
|
|
1365
|
+
if (resource_found == image_cache_.end() || !resource_found->second) return true;
|
|
1366
|
+
const ImageResource &resource = *resource_found->second;
|
|
1367
|
+
|
|
1368
|
+
Rect source = command.image.has_src ? normalized(command.image.src) :
|
|
1369
|
+
Rect{0, 0, static_cast<float>(resource.width), static_cast<float>(resource.height)};
|
|
1370
|
+
source = intersection(source, Rect{0, 0, static_cast<float>(resource.width), static_cast<float>(resource.height)});
|
|
1371
|
+
Rect requested = normalized(command.image.dst);
|
|
1372
|
+
if (empty(source) || empty(requested)) return false;
|
|
1373
|
+
Rect destination = requested;
|
|
1374
|
+
if (command.image.fit == 1 || command.image.fit == 2) {
|
|
1375
|
+
const float source_aspect = source.width / source.height;
|
|
1376
|
+
const float destination_aspect = requested.width / requested.height;
|
|
1377
|
+
float width = requested.width;
|
|
1378
|
+
float height = requested.height;
|
|
1379
|
+
if (command.image.fit == 1) {
|
|
1380
|
+
if (destination_aspect > source_aspect) width = height * source_aspect;
|
|
1381
|
+
else height = width / source_aspect;
|
|
1382
|
+
} else {
|
|
1383
|
+
if (destination_aspect > source_aspect) height = width / source_aspect;
|
|
1384
|
+
else width = height * source_aspect;
|
|
1385
|
+
}
|
|
1386
|
+
destination = {
|
|
1387
|
+
requested.x + (requested.width - width) * 0.5f,
|
|
1388
|
+
requested.y + (requested.height - height) * 0.5f,
|
|
1389
|
+
width,
|
|
1390
|
+
height,
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
ID2D1Layer *layer = nullptr;
|
|
1395
|
+
ID2D1PathGeometry *mask = nullptr;
|
|
1396
|
+
const float max_radius = std::max(std::max(command.image.radius.top_left, command.image.radius.top_right),
|
|
1397
|
+
std::max(command.image.radius.bottom_right, command.image.radius.bottom_left));
|
|
1398
|
+
if (max_radius > 0) {
|
|
1399
|
+
if (!makeRoundedGeometry(renderer_->d2dFactory(), requested, command.image.radius, &mask) ||
|
|
1400
|
+
FAILED(backing_target_->CreateLayer(nullptr, &layer))) {
|
|
1401
|
+
releaseCom(mask);
|
|
1402
|
+
releaseCom(layer);
|
|
1403
|
+
return false;
|
|
1404
|
+
}
|
|
1405
|
+
D2D1_LAYER_PARAMETERS parameters = D2D1::LayerParameters();
|
|
1406
|
+
parameters.contentBounds = D2D1::InfiniteRect();
|
|
1407
|
+
parameters.geometricMask = mask;
|
|
1408
|
+
parameters.maskAntialiasMode = D2D1_ANTIALIAS_MODE_PER_PRIMITIVE;
|
|
1409
|
+
parameters.opacity = 1.0f;
|
|
1410
|
+
backing_target_->PushLayer(parameters, layer);
|
|
1411
|
+
} else if (command.image.fit == 2) {
|
|
1412
|
+
/* Cover expands one destination axis past the requested frame.
|
|
1413
|
+
* A zero-radius image still has a rectangular destination mask;
|
|
1414
|
+
* without this clip the expanded bitmap paints over siblings. */
|
|
1415
|
+
backing_target_->PushAxisAlignedClip(d2dRect(requested), D2D1_ANTIALIAS_MODE_ALIASED);
|
|
1416
|
+
}
|
|
1417
|
+
backing_target_->DrawBitmap(bitmap, d2dRect(destination),
|
|
1418
|
+
clamp01(command.opacity * command.image.opacity),
|
|
1419
|
+
command.image.sampling == 0 ? D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR : D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
|
|
1420
|
+
d2dRect(source));
|
|
1421
|
+
if (layer) backing_target_->PopLayer();
|
|
1422
|
+
else if (command.image.fit == 2) backing_target_->PopAxisAlignedClip();
|
|
1423
|
+
releaseCom(layer);
|
|
1424
|
+
releaseCom(mask);
|
|
1425
|
+
return true;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
static bool widenUtf8(const std::string &value, std::wstring *wide) {
|
|
1429
|
+
if (!wide) return false;
|
|
1430
|
+
wide->clear();
|
|
1431
|
+
if (value.empty()) return true;
|
|
1432
|
+
if (value.size() > INT_MAX) return false;
|
|
1433
|
+
const int count = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(), static_cast<int>(value.size()), nullptr, 0);
|
|
1434
|
+
if (count <= 0) return false;
|
|
1435
|
+
wide->resize(static_cast<size_t>(count));
|
|
1436
|
+
return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value.data(), static_cast<int>(value.size()), wide->data(), count) == count;
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
static DWRITE_FONT_WEIGHT canvasFontWeight(uint64_t font_id) {
|
|
1440
|
+
if (font_id == 3) return DWRITE_FONT_WEIGHT_MEDIUM;
|
|
1441
|
+
if (font_id == 4 || font_id == 6) return DWRITE_FONT_WEIGHT_BOLD;
|
|
1442
|
+
return DWRITE_FONT_WEIGHT_NORMAL;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
static DWRITE_FONT_STYLE canvasFontStyle(uint64_t font_id) {
|
|
1446
|
+
return font_id == 5 || font_id == 6 ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
std::shared_ptr<FontResource> fontResource(uint64_t font_id) const {
|
|
1450
|
+
std::shared_ptr<FontResource> resource = renderer_->font(canvasFontResourceId(font_id));
|
|
1451
|
+
if (!resource) {
|
|
1452
|
+
const uint64_t fallback_id = canvasFallbackFontResourceId(font_id);
|
|
1453
|
+
if (fallback_id != 0) resource = renderer_->font(fallback_id);
|
|
1454
|
+
}
|
|
1455
|
+
return resource;
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
bool createGlyphFace(uint64_t font_id, IDWriteFontFace **face) const {
|
|
1459
|
+
if (!face) return false;
|
|
1460
|
+
*face = nullptr;
|
|
1461
|
+
const DWRITE_FONT_WEIGHT weight = canvasFontWeight(font_id);
|
|
1462
|
+
const DWRITE_FONT_STYLE style = canvasFontStyle(font_id);
|
|
1463
|
+
std::shared_ptr<FontResource> custom = fontResource(font_id);
|
|
1464
|
+
IDWriteFont *font = nullptr;
|
|
1465
|
+
HRESULT result = E_FAIL;
|
|
1466
|
+
if (custom) {
|
|
1467
|
+
IDWriteFontFamily1 *family = nullptr;
|
|
1468
|
+
result = custom->collection->GetFontFamily(0, &family);
|
|
1469
|
+
if (SUCCEEDED(result)) result = family->GetFirstMatchingFont(
|
|
1470
|
+
weight, DWRITE_FONT_STRETCH_NORMAL, style, &font);
|
|
1471
|
+
releaseCom(family);
|
|
1472
|
+
} else {
|
|
1473
|
+
IDWriteFontCollection *collection = nullptr;
|
|
1474
|
+
IDWriteFontFamily *family = nullptr;
|
|
1475
|
+
const wchar_t *family_name = font_id == 2 ? L"Consolas" : L"Segoe UI";
|
|
1476
|
+
UINT32 family_index = 0;
|
|
1477
|
+
BOOL exists = FALSE;
|
|
1478
|
+
result = renderer_->dwriteFactory()->GetSystemFontCollection(&collection);
|
|
1479
|
+
if (SUCCEEDED(result)) result = collection->FindFamilyName(family_name, &family_index, &exists);
|
|
1480
|
+
if (SUCCEEDED(result) && !exists) result = E_FAIL;
|
|
1481
|
+
if (SUCCEEDED(result)) result = collection->GetFontFamily(family_index, &family);
|
|
1482
|
+
if (SUCCEEDED(result)) result = family->GetFirstMatchingFont(
|
|
1483
|
+
weight, DWRITE_FONT_STRETCH_NORMAL, style, &font);
|
|
1484
|
+
releaseCom(family);
|
|
1485
|
+
releaseCom(collection);
|
|
1486
|
+
}
|
|
1487
|
+
if (SUCCEEDED(result) && font) result = font->CreateFontFace(face);
|
|
1488
|
+
releaseCom(font);
|
|
1489
|
+
return SUCCEEDED(result) && *face;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
bool createTextFormat(const TextCommand &text, IDWriteTextFormat **format) {
|
|
1493
|
+
if (!format) return false;
|
|
1494
|
+
*format = nullptr;
|
|
1495
|
+
/* IDs 3..6 are styled variants of the built-in sans face, not
|
|
1496
|
+
* independent assets. Resolve them through registered Geist id 1
|
|
1497
|
+
* and let DirectWrite select/simulate the requested traits. */
|
|
1498
|
+
const uint64_t resource_id = canvasFontResourceId(text.font_id);
|
|
1499
|
+
std::shared_ptr<FontResource> custom = renderer_->font(resource_id);
|
|
1500
|
+
/* An absent application font follows the reference/AppKit fallback
|
|
1501
|
+
* contract too: mono keeps its platform mono fallback, every other
|
|
1502
|
+
* id uses bundled Geist when that registration succeeded. */
|
|
1503
|
+
if (!custom) {
|
|
1504
|
+
const uint64_t fallback_id = canvasFallbackFontResourceId(text.font_id);
|
|
1505
|
+
if (fallback_id != 0) custom = renderer_->font(fallback_id);
|
|
1506
|
+
}
|
|
1507
|
+
const wchar_t *family = L"Segoe UI";
|
|
1508
|
+
IDWriteFontCollection *collection = nullptr;
|
|
1509
|
+
const DWRITE_FONT_WEIGHT weight = canvasFontWeight(text.font_id);
|
|
1510
|
+
const DWRITE_FONT_STYLE style = canvasFontStyle(text.font_id);
|
|
1511
|
+
if (custom) {
|
|
1512
|
+
family = custom->family.c_str();
|
|
1513
|
+
collection = custom->collection;
|
|
1514
|
+
} else if (text.font_id == 2) family = L"Consolas";
|
|
1515
|
+
HRESULT result = renderer_->dwriteFactory()->CreateTextFormat(
|
|
1516
|
+
family, collection, weight, style, DWRITE_FONT_STRETCH_NORMAL,
|
|
1517
|
+
std::max(1.0f, text.size), L"en-us", format);
|
|
1518
|
+
if (FAILED(result) || !*format) return false;
|
|
1519
|
+
(*format)->SetTextAlignment(text.align == 1 ? DWRITE_TEXT_ALIGNMENT_CENTER :
|
|
1520
|
+
(text.align == 2 ? DWRITE_TEXT_ALIGNMENT_TRAILING : DWRITE_TEXT_ALIGNMENT_LEADING));
|
|
1521
|
+
(*format)->SetWordWrapping(text.wrap == 0 ? DWRITE_WORD_WRAPPING_NO_WRAP :
|
|
1522
|
+
(text.wrap == 2 ? DWRITE_WORD_WRAPPING_CHARACTER : DWRITE_WORD_WRAPPING_WRAP));
|
|
1523
|
+
if (text.line_height > 0) {
|
|
1524
|
+
(*format)->SetLineSpacing(DWRITE_LINE_SPACING_METHOD_UNIFORM, text.line_height, std::min(text.line_height, text.size));
|
|
1525
|
+
}
|
|
1526
|
+
return true;
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
bool createTextLayout(
|
|
1530
|
+
const std::wstring &value,
|
|
1531
|
+
IDWriteTextFormat *format,
|
|
1532
|
+
float width,
|
|
1533
|
+
float height,
|
|
1534
|
+
IDWriteTextLayout **layout
|
|
1535
|
+
) {
|
|
1536
|
+
if (!format || !layout || !renderer_->fontFallback()) return false;
|
|
1537
|
+
*layout = nullptr;
|
|
1538
|
+
HRESULT result = renderer_->dwriteFactory()->CreateTextLayout(
|
|
1539
|
+
value.data(), static_cast<UINT32>(value.size()), format,
|
|
1540
|
+
std::max(1.0f, width), std::max(1.0f, height), layout);
|
|
1541
|
+
IDWriteTextLayout2 *layout2 = nullptr;
|
|
1542
|
+
if (SUCCEEDED(result) && *layout) {
|
|
1543
|
+
result = (*layout)->QueryInterface(
|
|
1544
|
+
__uuidof(IDWriteTextLayout2), reinterpret_cast<void **>(&layout2));
|
|
1545
|
+
}
|
|
1546
|
+
if (SUCCEEDED(result) && layout2) {
|
|
1547
|
+
result = layout2->SetFontFallback(renderer_->fontFallback());
|
|
1548
|
+
}
|
|
1549
|
+
releaseCom(layout2);
|
|
1550
|
+
if (FAILED(result)) {
|
|
1551
|
+
releaseCom(*layout);
|
|
1552
|
+
return false;
|
|
1553
|
+
}
|
|
1554
|
+
return *layout != nullptr;
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
bool drawText(const Command &command) {
|
|
1558
|
+
const TextCommand &text = command.text;
|
|
1559
|
+
IDWriteTextFormat *format = nullptr;
|
|
1560
|
+
ID2D1SolidColorBrush *brush = nullptr;
|
|
1561
|
+
if (!createTextFormat(text, &format) ||
|
|
1562
|
+
FAILED(backing_target_->CreateSolidColorBrush(d2dColor(text.color, command.opacity), &brush))) {
|
|
1563
|
+
releaseCom(brush);
|
|
1564
|
+
releaseCom(format);
|
|
1565
|
+
return false;
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
auto draw_line = [&](const std::string &utf8, float x, float baseline) -> bool {
|
|
1569
|
+
if (utf8.empty()) return true;
|
|
1570
|
+
std::wstring value;
|
|
1571
|
+
if (!widenUtf8(utf8, &value)) return false;
|
|
1572
|
+
IDWriteTextLayout *layout = nullptr;
|
|
1573
|
+
HRESULT result = createTextLayout(
|
|
1574
|
+
value, format, 100000.0f, std::max(4.0f, text.size * 4.0f), &layout) ? S_OK : E_FAIL;
|
|
1575
|
+
DWRITE_LINE_METRICS metrics = {};
|
|
1576
|
+
UINT32 actual = 0;
|
|
1577
|
+
if (SUCCEEDED(result)) result = layout->GetLineMetrics(&metrics, 1, &actual);
|
|
1578
|
+
if (SUCCEEDED(result) && actual == 1) {
|
|
1579
|
+
backing_target_->DrawTextLayout(D2D1::Point2F(x, baseline - metrics.baseline), layout, brush,
|
|
1580
|
+
D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
|
|
1581
|
+
}
|
|
1582
|
+
releaseCom(layout);
|
|
1583
|
+
return SUCCEEDED(result) && actual == 1;
|
|
1584
|
+
};
|
|
1585
|
+
|
|
1586
|
+
if (text.has_positioned_glyphs) {
|
|
1587
|
+
bool result = true;
|
|
1588
|
+
std::map<uint64_t, IDWriteFontFace *> faces;
|
|
1589
|
+
for (const PositionedGlyph &glyph : text.positioned_glyphs) {
|
|
1590
|
+
IDWriteFontFace *face = nullptr;
|
|
1591
|
+
auto found = faces.find(glyph.font_id);
|
|
1592
|
+
if (found == faces.end()) {
|
|
1593
|
+
if (!createGlyphFace(glyph.font_id, &face)) {
|
|
1594
|
+
result = false;
|
|
1595
|
+
break;
|
|
1596
|
+
}
|
|
1597
|
+
faces[glyph.font_id] = face;
|
|
1598
|
+
} else {
|
|
1599
|
+
face = found->second;
|
|
1600
|
+
}
|
|
1601
|
+
const UINT16 glyph_index = glyph.id;
|
|
1602
|
+
const FLOAT glyph_advance = glyph.advance;
|
|
1603
|
+
const DWRITE_GLYPH_OFFSET glyph_offset = {};
|
|
1604
|
+
DWRITE_GLYPH_RUN glyph_run = {};
|
|
1605
|
+
glyph_run.fontFace = face;
|
|
1606
|
+
glyph_run.fontEmSize = std::max(1.0f, text.size);
|
|
1607
|
+
glyph_run.glyphCount = 1;
|
|
1608
|
+
glyph_run.glyphIndices = &glyph_index;
|
|
1609
|
+
glyph_run.glyphAdvances = &glyph_advance;
|
|
1610
|
+
glyph_run.glyphOffsets = &glyph_offset;
|
|
1611
|
+
backing_target_->DrawGlyphRun(
|
|
1612
|
+
D2D1::Point2F(glyph.x, glyph.baseline), &glyph_run, brush, DWRITE_MEASURING_MODE_NATURAL);
|
|
1613
|
+
}
|
|
1614
|
+
if (result) {
|
|
1615
|
+
/* Positioned fragments already carry their final x; do not
|
|
1616
|
+
* apply center/trailing alignment inside the wide one-line
|
|
1617
|
+
* helper layout a second time. */
|
|
1618
|
+
format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
|
|
1619
|
+
for (const PositionedTextFragment &fragment : text.positioned_fragments) {
|
|
1620
|
+
if (!draw_line(fragment.text, fragment.x, fragment.baseline)) {
|
|
1621
|
+
result = false;
|
|
1622
|
+
break;
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
for (auto &entry : faces) releaseCom(entry.second);
|
|
1627
|
+
releaseCom(brush);
|
|
1628
|
+
releaseCom(format);
|
|
1629
|
+
return result;
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
bool result = true;
|
|
1633
|
+
if (text.has_layout && text.has_lines) {
|
|
1634
|
+
/* Engine-measured lines already carry their final aligned x.
|
|
1635
|
+
* Applying DirectWrite alignment again would center/trail the
|
|
1636
|
+
* run inside the deliberately wide one-line layout and move
|
|
1637
|
+
* short labels (counts, centered buttons) off the surface. */
|
|
1638
|
+
format->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
|
|
1639
|
+
for (const TextLine &line : text.lines) {
|
|
1640
|
+
if (!draw_line(line.text, line.x, line.baseline)) {
|
|
1641
|
+
result = false;
|
|
1642
|
+
break;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
} else if (!text.has_layout) {
|
|
1646
|
+
/* A raw DrawText origin is a baseline, not the top of an em
|
|
1647
|
+
* box. DirectWrite's baseline comes from the registered face's
|
|
1648
|
+
* metrics and is not guaranteed to equal `size`, so use the
|
|
1649
|
+
* same measured-baseline path as engine-planned lines. */
|
|
1650
|
+
result = draw_line(text.text, text.origin.x, text.origin.y);
|
|
1651
|
+
} else {
|
|
1652
|
+
std::wstring value;
|
|
1653
|
+
result = widenUtf8(text.text, &value);
|
|
1654
|
+
if (result && !value.empty()) {
|
|
1655
|
+
const float width = text.max_width > 0 ? text.max_width : 100000.0f;
|
|
1656
|
+
const float height = std::max(text.line_height, text.size * 1.25f) * 4096.0f;
|
|
1657
|
+
IDWriteTextLayout *layout = nullptr;
|
|
1658
|
+
result = createTextLayout(value, format, width, height, &layout);
|
|
1659
|
+
if (result) {
|
|
1660
|
+
backing_target_->DrawTextLayout(
|
|
1661
|
+
D2D1::Point2F(text.origin.x, text.origin.y - text.size), layout, brush,
|
|
1662
|
+
D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
|
|
1663
|
+
}
|
|
1664
|
+
releaseCom(layout);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
releaseCom(brush);
|
|
1668
|
+
releaseCom(format);
|
|
1669
|
+
return result;
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
bool drawShadow(const Command &command) {
|
|
1673
|
+
const Effect &effect = command.effect;
|
|
1674
|
+
const float blur = std::max(0.0f, effect.blur);
|
|
1675
|
+
const unsigned steps = blur > 0.25f ? 12u : 1u;
|
|
1676
|
+
float weight_sum = 0;
|
|
1677
|
+
for (unsigned index = 0; index < steps; ++index) weight_sum += static_cast<float>(index + 1);
|
|
1678
|
+
for (unsigned index = 0; index < steps; ++index) {
|
|
1679
|
+
const float inward = static_cast<float>(index + 1) / static_cast<float>(steps);
|
|
1680
|
+
/* Spread is signed: negative values inset the shadow caster
|
|
1681
|
+
* before the blur halo expands it. The default card/overlay
|
|
1682
|
+
* tokens depend on that contraction. */
|
|
1683
|
+
const float expansion = effect.spread + blur * (1.0f - inward);
|
|
1684
|
+
Rect rect = normalized(effect.rect);
|
|
1685
|
+
rect.x += effect.offset.x - expansion;
|
|
1686
|
+
rect.y += effect.offset.y - expansion;
|
|
1687
|
+
rect.width += expansion * 2;
|
|
1688
|
+
rect.height += expansion * 2;
|
|
1689
|
+
if (rect.width <= 0 || rect.height <= 0) continue;
|
|
1690
|
+
Radius radius = effect.radius;
|
|
1691
|
+
radius.top_left = std::max(0.0f, radius.top_left + expansion);
|
|
1692
|
+
radius.top_right = std::max(0.0f, radius.top_right + expansion);
|
|
1693
|
+
radius.bottom_right = std::max(0.0f, radius.bottom_right + expansion);
|
|
1694
|
+
radius.bottom_left = std::max(0.0f, radius.bottom_left + expansion);
|
|
1695
|
+
ID2D1PathGeometry *geometry = nullptr;
|
|
1696
|
+
ID2D1SolidColorBrush *brush = nullptr;
|
|
1697
|
+
Color layer_color = effect.color;
|
|
1698
|
+
layer_color.a *= static_cast<float>(index + 1) / weight_sum;
|
|
1699
|
+
if (!makeRoundedGeometry(renderer_->d2dFactory(), rect, radius, &geometry) ||
|
|
1700
|
+
FAILED(backing_target_->CreateSolidColorBrush(d2dColor(layer_color, command.opacity), &brush))) {
|
|
1701
|
+
releaseCom(brush);
|
|
1702
|
+
releaseCom(geometry);
|
|
1703
|
+
return false;
|
|
1704
|
+
}
|
|
1705
|
+
backing_target_->FillGeometry(geometry, brush);
|
|
1706
|
+
releaseCom(brush);
|
|
1707
|
+
releaseCom(geometry);
|
|
1708
|
+
}
|
|
1709
|
+
return true;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
bool ensureBlurSnapshot() {
|
|
1713
|
+
if (blur_snapshot_) return true;
|
|
1714
|
+
if (!backing_target_ || pixel_width_ == 0 || pixel_height_ == 0) return false;
|
|
1715
|
+
const D2D1_BITMAP_PROPERTIES properties = D2D1::BitmapProperties(
|
|
1716
|
+
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
|
|
1717
|
+
static_cast<FLOAT>(96.0 * scale_), static_cast<FLOAT>(96.0 * scale_));
|
|
1718
|
+
return SUCCEEDED(backing_target_->CreateBitmap(
|
|
1719
|
+
D2D1::SizeU(pixel_width_, pixel_height_), nullptr, 0, properties, &blur_snapshot_)) && blur_snapshot_;
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
/* Resume the backing target after a segment-ending backdrop blur.
|
|
1723
|
+
* Direct2D 1.0 has no effects graph, but its bitmaps and compatible
|
|
1724
|
+
* targets stay in the hardware resource domain: copy the current
|
|
1725
|
+
* backdrop on-GPU, approximate the box blur with a 5x5 separable-
|
|
1726
|
+
* Gaussian sample kernel into a temporary GPU target, then composite
|
|
1727
|
+
* that target over only the affected rect. There is no readback and
|
|
1728
|
+
* no full RGBA allocation/swizzle on the CPU. */
|
|
1729
|
+
Rect blurTarget(const Command &command, const Rect *outer_clip) const {
|
|
1730
|
+
Rect target = command.has_transform ? transformedRect(command.effect.rect, command.transform) : normalized(command.effect.rect);
|
|
1731
|
+
target = intersection(target, Rect{0, 0, static_cast<float>(surface_width_), static_cast<float>(surface_height_)});
|
|
1732
|
+
if (command.has_clip) target = intersection(target, command.clip);
|
|
1733
|
+
if (outer_clip) target = intersection(target, *outer_clip);
|
|
1734
|
+
return target;
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
bool resumeAndDrawBlur(const Command &command, Rect target) {
|
|
1738
|
+
auto resume = [&] {
|
|
1739
|
+
backing_target_->BeginDraw();
|
|
1740
|
+
backing_target_->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
1741
|
+
};
|
|
1742
|
+
|
|
1743
|
+
releaseCom(backing_bitmap_);
|
|
1744
|
+
if (FAILED(backing_target_->GetBitmap(&backing_bitmap_)) || !backing_bitmap_ ||
|
|
1745
|
+
!ensureBlurSnapshot() || FAILED(blur_snapshot_->CopyFromBitmap(nullptr, backing_bitmap_, nullptr))) {
|
|
1746
|
+
resume();
|
|
1747
|
+
return false;
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
const float opacity = clamp01(command.opacity);
|
|
1751
|
+
|
|
1752
|
+
const double pixel_width_value = std::ceil(target.width * scale_);
|
|
1753
|
+
const double pixel_height_value = std::ceil(target.height * scale_);
|
|
1754
|
+
if (pixel_width_value < 1 || pixel_height_value < 1 ||
|
|
1755
|
+
pixel_width_value > kMaxSurfacePixels || pixel_height_value > kMaxSurfacePixels) {
|
|
1756
|
+
resume();
|
|
1757
|
+
return false;
|
|
1758
|
+
}
|
|
1759
|
+
const D2D1_SIZE_F desired = D2D1::SizeF(target.width, target.height);
|
|
1760
|
+
const D2D1_SIZE_U pixels = D2D1::SizeU(
|
|
1761
|
+
static_cast<UINT32>(pixel_width_value), static_cast<UINT32>(pixel_height_value));
|
|
1762
|
+
const D2D1_PIXEL_FORMAT format = D2D1::PixelFormat(
|
|
1763
|
+
DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED);
|
|
1764
|
+
ID2D1BitmapRenderTarget *temporary = nullptr;
|
|
1765
|
+
ID2D1Bitmap *blurred = nullptr;
|
|
1766
|
+
HRESULT result = backing_target_->CreateCompatibleRenderTarget(
|
|
1767
|
+
&desired, &pixels, &format, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, &temporary);
|
|
1768
|
+
if (FAILED(result) || !temporary) {
|
|
1769
|
+
releaseCom(temporary);
|
|
1770
|
+
resume();
|
|
1771
|
+
return false;
|
|
1772
|
+
}
|
|
1773
|
+
temporary->SetDpi(static_cast<FLOAT>(96.0 * scale_), static_cast<FLOAT>(96.0 * scale_));
|
|
1774
|
+
temporary->BeginDraw();
|
|
1775
|
+
temporary->Clear(D2D1::ColorF(0, 0, 0, 0));
|
|
1776
|
+
/* D2D's target DPI applies the presentation scale, but this blur
|
|
1777
|
+
* bypasses the command transform after converting its rect to a
|
|
1778
|
+
* device-space bounding box. Scale the kernel explicitly so a
|
|
1779
|
+
* transformed blur keeps parity with the reference/AppKit paths. */
|
|
1780
|
+
const float radius = std::min(64.0f,
|
|
1781
|
+
std::max(0.0f, command.effect.blur * transformScale(command.transform)));
|
|
1782
|
+
const float offsets[5] = {-1.0f, -0.5f, 0.0f, 0.5f, 1.0f};
|
|
1783
|
+
const float weights[5] = {1.0f, 4.0f, 6.0f, 4.0f, 1.0f};
|
|
1784
|
+
ID2D1BitmapBrush *sample_brush = nullptr;
|
|
1785
|
+
const D2D1_BITMAP_BRUSH_PROPERTIES sample_properties = D2D1::BitmapBrushProperties(
|
|
1786
|
+
D2D1_EXTEND_MODE_CLAMP, D2D1_EXTEND_MODE_CLAMP, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR);
|
|
1787
|
+
result = temporary->CreateBitmapBrush(blur_snapshot_, &sample_properties, nullptr, &sample_brush);
|
|
1788
|
+
if (SUCCEEDED(result) && !sample_brush) result = E_FAIL;
|
|
1789
|
+
if (SUCCEEDED(result) && sample_brush) {
|
|
1790
|
+
float accumulated_weight = 0;
|
|
1791
|
+
for (size_t y = 0; y < 5; ++y) {
|
|
1792
|
+
for (size_t x = 0; x < 5; ++x) {
|
|
1793
|
+
const float dx = offsets[x] * radius;
|
|
1794
|
+
const float dy = offsets[y] * radius;
|
|
1795
|
+
const float weight = weights[x] * weights[y];
|
|
1796
|
+
accumulated_weight += weight;
|
|
1797
|
+
/* Translate the whole-surface snapshot beneath the
|
|
1798
|
+
* target-local output. The brush clamps each sample
|
|
1799
|
+
* at a surface edge, so a full-surface blur keeps
|
|
1800
|
+
* nonzero kernel offsets instead of collapsing every
|
|
1801
|
+
* tap onto the unblurred source. */
|
|
1802
|
+
sample_brush->SetOpacity(weight / accumulated_weight);
|
|
1803
|
+
sample_brush->SetTransform(D2D1::Matrix3x2F::Translation(
|
|
1804
|
+
-(target.x + dx), -(target.y + dy)));
|
|
1805
|
+
temporary->FillRectangle(
|
|
1806
|
+
D2D1::RectF(0, 0, target.width, target.height), sample_brush);
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
const HRESULT draw_result = temporary->EndDraw();
|
|
1811
|
+
if (SUCCEEDED(result)) result = draw_result;
|
|
1812
|
+
if (SUCCEEDED(result)) result = temporary->GetBitmap(&blurred);
|
|
1813
|
+
|
|
1814
|
+
resume();
|
|
1815
|
+
if (SUCCEEDED(result) && blurred) {
|
|
1816
|
+
backing_target_->PushAxisAlignedClip(d2dRect(target), D2D1_ANTIALIAS_MODE_ALIASED);
|
|
1817
|
+
backing_target_->DrawBitmap(blurred, d2dRect(target), opacity,
|
|
1818
|
+
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, nullptr);
|
|
1819
|
+
backing_target_->PopAxisAlignedClip();
|
|
1820
|
+
}
|
|
1821
|
+
releaseCom(sample_brush);
|
|
1822
|
+
releaseCom(blurred);
|
|
1823
|
+
releaseCom(temporary);
|
|
1824
|
+
return SUCCEEDED(result);
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
bool drawCommandList(const std::vector<const Command *> &commands, const Rect *outer_clip) {
|
|
1828
|
+
for (const Command *command : commands) {
|
|
1829
|
+
if (command->kind == 13) {
|
|
1830
|
+
/* Cull before ending the current segment or copying the
|
|
1831
|
+
* full backing bitmap. A localized blur outside a dirty
|
|
1832
|
+
* patch must cost no more than any other culled command. */
|
|
1833
|
+
const Rect target = blurTarget(*command, outer_clip);
|
|
1834
|
+
if (empty(target) || command->effect.blur <= 0 || command->opacity <= 0) continue;
|
|
1835
|
+
const HRESULT segment = backing_target_->EndDraw();
|
|
1836
|
+
if (FAILED(segment)) {
|
|
1837
|
+
backing_target_->BeginDraw();
|
|
1838
|
+
return false;
|
|
1839
|
+
}
|
|
1840
|
+
if (!resumeAndDrawBlur(*command, target)) return false;
|
|
1841
|
+
continue;
|
|
1842
|
+
}
|
|
1843
|
+
if (!drawCommand(*command, outer_clip)) return false;
|
|
1844
|
+
}
|
|
1845
|
+
return true;
|
|
1846
|
+
}
|
|
1847
|
+
|
|
1848
|
+
bool drawCommand(const Command &command, const Rect *outer_clip) {
|
|
1849
|
+
if (outer_clip && !intersects(command.bounds, *outer_clip)) return true;
|
|
1850
|
+
backing_target_->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
1851
|
+
if (outer_clip) backing_target_->PushAxisAlignedClip(d2dRect(*outer_clip), D2D1_ANTIALIAS_MODE_ALIASED);
|
|
1852
|
+
if (command.has_clip) backing_target_->PushAxisAlignedClip(d2dRect(command.clip), D2D1_ANTIALIAS_MODE_ALIASED);
|
|
1853
|
+
if (command.has_transform) {
|
|
1854
|
+
const Affine &value = command.transform;
|
|
1855
|
+
backing_target_->SetTransform(D2D1::Matrix3x2F(value.a, value.b, value.c, value.d, value.tx, value.ty));
|
|
1856
|
+
}
|
|
1857
|
+
bool ok = false;
|
|
1858
|
+
switch (command.kind) {
|
|
1859
|
+
case 0: case 1: case 2: case 3: case 8:
|
|
1860
|
+
ok = drawPaintedShape(command, false);
|
|
1861
|
+
break;
|
|
1862
|
+
case 4: case 5: case 6: case 7: case 9:
|
|
1863
|
+
ok = drawPaintedShape(command, true);
|
|
1864
|
+
break;
|
|
1865
|
+
case 10:
|
|
1866
|
+
ok = drawImage(command);
|
|
1867
|
+
break;
|
|
1868
|
+
case 11:
|
|
1869
|
+
ok = drawText(command);
|
|
1870
|
+
break;
|
|
1871
|
+
case 12:
|
|
1872
|
+
ok = drawShadow(command);
|
|
1873
|
+
break;
|
|
1874
|
+
default:
|
|
1875
|
+
ok = false;
|
|
1876
|
+
break;
|
|
1877
|
+
}
|
|
1878
|
+
backing_target_->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
1879
|
+
if (command.has_clip) backing_target_->PopAxisAlignedClip();
|
|
1880
|
+
if (outer_clip) backing_target_->PopAxisAlignedClip();
|
|
1881
|
+
return ok;
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
bool commandsSupported(const std::vector<const Command *> &commands) const {
|
|
1885
|
+
for (const Command *command : commands) {
|
|
1886
|
+
if (!command || command->kind > 13) return false;
|
|
1887
|
+
if (command->kind <= 9 && (command->shape.kind == Shape::Kind::none || command->paint.kind == Paint::Kind::none)) return false;
|
|
1888
|
+
if (command->kind == 10 && command->image.id == 0) return false;
|
|
1889
|
+
if (command->kind == 12 && command->effect.kind != Effect::Kind::shadow) return false;
|
|
1890
|
+
if (command->kind == 13 && command->effect.kind != Effect::Kind::blur) return false;
|
|
1891
|
+
}
|
|
1892
|
+
return true;
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
bool applyImageActions(const DecodedPacket &packet) {
|
|
1896
|
+
auto next = image_cache_;
|
|
1897
|
+
for (const ImageAction &action : packet.image_actions) {
|
|
1898
|
+
if (action.kind == 2) next.erase(action.id);
|
|
1899
|
+
else if (!imageActionResolvesResource(action.kind)) return false;
|
|
1900
|
+
}
|
|
1901
|
+
for (const ImageAction &action : packet.image_actions) {
|
|
1902
|
+
if (!imageActionResolvesResource(action.kind)) continue;
|
|
1903
|
+
if (action.image_index == UINT32_MAX || action.image_index >= packet.images.size()) return false;
|
|
1904
|
+
const ImageMeta &meta = packet.images[action.image_index];
|
|
1905
|
+
if (meta.id != action.id || meta.id == 0) return false;
|
|
1906
|
+
std::shared_ptr<ImageResource> resource = renderer_->image(meta.id);
|
|
1907
|
+
if (!imageMetadataMatchesResource(
|
|
1908
|
+
resource != nullptr,
|
|
1909
|
+
meta.width,
|
|
1910
|
+
meta.height,
|
|
1911
|
+
resource ? resource->width : 0,
|
|
1912
|
+
resource ? resource->height : 0)) return false;
|
|
1913
|
+
if (resource) next[meta.id] = std::move(resource);
|
|
1914
|
+
else next.erase(meta.id);
|
|
1915
|
+
}
|
|
1916
|
+
image_cache_ = std::move(next);
|
|
1917
|
+
/* The renderer-wide remove releases the shared pixels, but each
|
|
1918
|
+
* surface owns its own Direct2D bitmap. Any action that reconciles
|
|
1919
|
+
* an id to absent must drop that COM resource too so unregisters do
|
|
1920
|
+
* not leave stale texture memory behind. */
|
|
1921
|
+
for (const ImageAction &action : packet.image_actions) {
|
|
1922
|
+
if (image_cache_.find(action.id) == image_cache_.end()) {
|
|
1923
|
+
releaseImageBitmap(action.id);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
return true;
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
bool renderCommands(const std::vector<const Command *> &commands, const DecodedPacket &packet, Color clear, bool full_surface) {
|
|
1930
|
+
backing_target_->BeginDraw();
|
|
1931
|
+
backing_target_->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
1932
|
+
bool ok = true;
|
|
1933
|
+
if (full_surface) {
|
|
1934
|
+
backing_target_->Clear(d2dColor(clear));
|
|
1935
|
+
ok = drawCommandList(commands, nullptr);
|
|
1936
|
+
} else if (packet.has_scissor) {
|
|
1937
|
+
std::vector<Rect> regions = packet.dirty_rects;
|
|
1938
|
+
if (regions.empty()) regions.push_back(packet.scissor);
|
|
1939
|
+
ID2D1SolidColorBrush *clear_brush = nullptr;
|
|
1940
|
+
if (FAILED(backing_target_->CreateSolidColorBrush(d2dColor(clear), &clear_brush))) ok = false;
|
|
1941
|
+
for (const Rect &source_region : regions) {
|
|
1942
|
+
if (!ok) break;
|
|
1943
|
+
Rect region = intersection(normalized(source_region), normalized(packet.scissor));
|
|
1944
|
+
region = intersection(region, Rect{0, 0, static_cast<float>(surface_width_), static_cast<float>(surface_height_)});
|
|
1945
|
+
if (empty(region)) continue;
|
|
1946
|
+
backing_target_->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
1947
|
+
backing_target_->PushAxisAlignedClip(d2dRect(region), D2D1_ANTIALIAS_MODE_ALIASED);
|
|
1948
|
+
/* Normal gpu_surface windows are opaque; source-over is
|
|
1949
|
+
* therefore byte-equivalent to copy-clear here. Alpha
|
|
1950
|
+
* top-level windows intentionally use the pixel path. */
|
|
1951
|
+
backing_target_->FillRectangle(d2dRect(region), clear_brush);
|
|
1952
|
+
backing_target_->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
1953
|
+
backing_target_->PopAxisAlignedClip();
|
|
1954
|
+
if (!drawCommandList(commands, ®ion)) { ok = false; break; }
|
|
1955
|
+
}
|
|
1956
|
+
releaseCom(clear_brush);
|
|
1957
|
+
} else {
|
|
1958
|
+
/* A load without a scissor intentionally overlays the
|
|
1959
|
+
* supplied command list without clearing retained pixels. */
|
|
1960
|
+
ok = drawCommandList(commands, nullptr);
|
|
1961
|
+
}
|
|
1962
|
+
const HRESULT end = backing_target_->EndDraw();
|
|
1963
|
+
if (!ok) return false;
|
|
1964
|
+
if (FAILED(end)) {
|
|
1965
|
+
releaseDeviceResources(true);
|
|
1966
|
+
return false;
|
|
1967
|
+
}
|
|
1968
|
+
releaseCom(backing_bitmap_);
|
|
1969
|
+
if (FAILED(backing_target_->GetBitmap(&backing_bitmap_)) || !backing_bitmap_) {
|
|
1970
|
+
releaseDeviceResources(true);
|
|
1971
|
+
return false;
|
|
1972
|
+
}
|
|
1973
|
+
content_valid_ = true;
|
|
1974
|
+
return true;
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
std::shared_ptr<GpuRendererImpl> renderer_;
|
|
1978
|
+
HWND hwnd_ = nullptr;
|
|
1979
|
+
ID2D1HwndRenderTarget *window_target_ = nullptr;
|
|
1980
|
+
ID2D1BitmapRenderTarget *backing_target_ = nullptr;
|
|
1981
|
+
ID2D1Bitmap *backing_bitmap_ = nullptr;
|
|
1982
|
+
ID2D1Bitmap *blur_snapshot_ = nullptr;
|
|
1983
|
+
ID2D1StrokeStyle *rect_stroke_ = nullptr;
|
|
1984
|
+
ID2D1StrokeStyle *butt_stroke_ = nullptr;
|
|
1985
|
+
ID2D1StrokeStyle *round_stroke_ = nullptr;
|
|
1986
|
+
std::map<uint64_t, CachedBitmap> image_bitmaps_;
|
|
1987
|
+
std::map<uint64_t, std::shared_ptr<ImageResource>> image_cache_;
|
|
1988
|
+
std::map<uint64_t, Command> retained_commands_;
|
|
1989
|
+
std::vector<uint64_t> retained_order_;
|
|
1990
|
+
uint64_t retained_generation_ = 0;
|
|
1991
|
+
bool retained_valid_ = false;
|
|
1992
|
+
std::vector<Command> last_commands_;
|
|
1993
|
+
Color clear_color_ = {};
|
|
1994
|
+
double surface_width_ = 0;
|
|
1995
|
+
double surface_height_ = 0;
|
|
1996
|
+
double scale_ = 1;
|
|
1997
|
+
uint32_t pixel_width_ = 0;
|
|
1998
|
+
uint32_t pixel_height_ = 0;
|
|
1999
|
+
bool content_valid_ = false;
|
|
2000
|
+
};
|
|
2001
|
+
|
|
2002
|
+
int GpuSurfaceImpl::present(const WindowsGpuPacketPresent &present, WindowsGpuPresentInfo *info) {
|
|
2003
|
+
if (info) *info = {};
|
|
2004
|
+
/* A no-change packet is normally a cheap completion. After device
|
|
2005
|
+
* loss there is no retained bitmap to paint, though: decode the same
|
|
2006
|
+
* full-list payload and rebuild it. If the runtime first offers a
|
|
2007
|
+
* patch, retained_valid_ rejects it and the runtime resends a keyed
|
|
2008
|
+
* full packet in this frame. */
|
|
2009
|
+
if (!present.requires_render && content_valid_) return 1;
|
|
2010
|
+
if (!present.representable || present.unsupported_command_count != 0 ||
|
|
2011
|
+
!present.packet || present.packet_len == 0 || present.surface_width <= 0 || present.surface_height <= 0) return 0;
|
|
2012
|
+
|
|
2013
|
+
const uint64_t decode_begin_ns = gpuClockNs();
|
|
2014
|
+
DecodedPacket packet;
|
|
2015
|
+
if (!decodePacket(present.packet, present.packet_len, &packet)) return 0;
|
|
2016
|
+
const uint64_t draw_begin_ns = gpuClockNs();
|
|
2017
|
+
const double scale = present.scale > 0 ? present.scale : 1.0;
|
|
2018
|
+
const double pixel_width_value = std::ceil(present.surface_width * scale);
|
|
2019
|
+
const double pixel_height_value = std::ceil(present.surface_height * scale);
|
|
2020
|
+
if (!std::isfinite(pixel_width_value) || !std::isfinite(pixel_height_value) ||
|
|
2021
|
+
pixel_width_value < 1 || pixel_height_value < 1 ||
|
|
2022
|
+
pixel_width_value > kMaxSurfacePixels || pixel_height_value > kMaxSurfacePixels) return 0;
|
|
2023
|
+
const uint32_t pixel_width = static_cast<uint32_t>(pixel_width_value);
|
|
2024
|
+
const uint32_t pixel_height = static_cast<uint32_t>(pixel_height_value);
|
|
2025
|
+
|
|
2026
|
+
std::map<uint64_t, Command> next_retained;
|
|
2027
|
+
std::vector<uint64_t> next_order;
|
|
2028
|
+
std::vector<const Command *> draw_commands;
|
|
2029
|
+
const bool patch = packet.load_action == 3;
|
|
2030
|
+
if (patch) {
|
|
2031
|
+
if (!retained_valid_ || packet.generation == 0 || packet.generation != retained_generation_) return 0;
|
|
2032
|
+
next_retained = retained_commands_;
|
|
2033
|
+
for (uint64_t key : packet.evicts) {
|
|
2034
|
+
auto found = next_retained.find(key);
|
|
2035
|
+
if (found == next_retained.end()) {
|
|
2036
|
+
retained_valid_ = false;
|
|
2037
|
+
return 0;
|
|
2038
|
+
}
|
|
2039
|
+
next_retained.erase(found);
|
|
2040
|
+
}
|
|
2041
|
+
for (const KeyedCommand &upsert : packet.upserts) next_retained[upsert.key] = upsert.command;
|
|
2042
|
+
if (next_retained.size() > kRetainedCommandCap || packet.order.size() != next_retained.size()) {
|
|
2043
|
+
retained_valid_ = false;
|
|
2044
|
+
return 0;
|
|
2045
|
+
}
|
|
2046
|
+
std::set<uint64_t> seen;
|
|
2047
|
+
next_order.reserve(packet.order.size());
|
|
2048
|
+
draw_commands.reserve(packet.order.size());
|
|
2049
|
+
for (uint64_t key : packet.order) {
|
|
2050
|
+
auto found = next_retained.find(key);
|
|
2051
|
+
if (found == next_retained.end() || !seen.insert(key).second) {
|
|
2052
|
+
retained_valid_ = false;
|
|
2053
|
+
return 0;
|
|
2054
|
+
}
|
|
2055
|
+
next_order.push_back(key);
|
|
2056
|
+
draw_commands.push_back(&found->second);
|
|
2057
|
+
}
|
|
2058
|
+
} else {
|
|
2059
|
+
draw_commands.reserve(packet.commands.size());
|
|
2060
|
+
for (const KeyedCommand &keyed : packet.commands) draw_commands.push_back(&keyed.command);
|
|
2061
|
+
}
|
|
2062
|
+
if (present.command_count != 0 && present.command_count != draw_commands.size()) {
|
|
2063
|
+
if (patch) retained_valid_ = false;
|
|
2064
|
+
return 0;
|
|
2065
|
+
}
|
|
2066
|
+
if (!commandsSupported(draw_commands)) return 0;
|
|
2067
|
+
|
|
2068
|
+
const bool full_surface = packet.load_action == 2 || (patch && !packet.has_scissor);
|
|
2069
|
+
const bool same_backing = content_valid_ && pixel_width_ == pixel_width && pixel_height_ == pixel_height &&
|
|
2070
|
+
surface_width_ == present.surface_width && surface_height_ == present.surface_height && scale_ == scale;
|
|
2071
|
+
if (!full_surface && !same_backing) {
|
|
2072
|
+
if (patch) retained_valid_ = false;
|
|
2073
|
+
return 0;
|
|
2074
|
+
}
|
|
2075
|
+
if (!ensureTargets(present.surface_width, present.surface_height, scale, pixel_width, pixel_height)) {
|
|
2076
|
+
releaseDeviceResources(true);
|
|
2077
|
+
return 0;
|
|
2078
|
+
}
|
|
2079
|
+
if (!full_surface && !content_valid_) {
|
|
2080
|
+
if (patch) retained_valid_ = false;
|
|
2081
|
+
return 0;
|
|
2082
|
+
}
|
|
2083
|
+
if (!applyImageActions(packet)) {
|
|
2084
|
+
if (patch) retained_valid_ = false;
|
|
2085
|
+
return 0;
|
|
2086
|
+
}
|
|
2087
|
+
|
|
2088
|
+
const Color clear = {
|
|
2089
|
+
static_cast<float>(present.clear_rgba[0]) / 255.0f,
|
|
2090
|
+
static_cast<float>(present.clear_rgba[1]) / 255.0f,
|
|
2091
|
+
static_cast<float>(present.clear_rgba[2]) / 255.0f,
|
|
2092
|
+
/* Direct2D child surfaces are the opaque path. Per-pixel-alpha
|
|
2093
|
+
* top-level windows are refused before this renderer and use the
|
|
2094
|
+
* exact layered pixel compositor, matching the GDI path's forced
|
|
2095
|
+
* opaque destination alpha here. */
|
|
2096
|
+
1.0f,
|
|
2097
|
+
};
|
|
2098
|
+
if (!renderCommands(draw_commands, packet, clear, full_surface)) {
|
|
2099
|
+
if (patch) retained_valid_ = false;
|
|
2100
|
+
return 0;
|
|
2101
|
+
}
|
|
2102
|
+
|
|
2103
|
+
if (patch) {
|
|
2104
|
+
retained_commands_ = std::move(next_retained);
|
|
2105
|
+
retained_order_ = std::move(next_order);
|
|
2106
|
+
/* generation is unchanged */
|
|
2107
|
+
} else {
|
|
2108
|
+
bool retainable = packet.load_action == 2 && packet.generation != 0 &&
|
|
2109
|
+
packet.commands.size() <= kRetainedCommandCap;
|
|
2110
|
+
std::map<uint64_t, Command> retained;
|
|
2111
|
+
std::vector<uint64_t> order;
|
|
2112
|
+
if (retainable) {
|
|
2113
|
+
for (const KeyedCommand &keyed : packet.commands) {
|
|
2114
|
+
if (retained.find(keyed.key) != retained.end()) {
|
|
2115
|
+
retainable = false;
|
|
2116
|
+
break;
|
|
2117
|
+
}
|
|
2118
|
+
retained.emplace(keyed.key, keyed.command);
|
|
2119
|
+
order.push_back(keyed.key);
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
if (retainable) {
|
|
2123
|
+
retained_commands_ = std::move(retained);
|
|
2124
|
+
retained_order_ = std::move(order);
|
|
2125
|
+
retained_generation_ = packet.generation;
|
|
2126
|
+
retained_valid_ = true;
|
|
2127
|
+
} else {
|
|
2128
|
+
retained_commands_.clear();
|
|
2129
|
+
retained_order_.clear();
|
|
2130
|
+
retained_generation_ = 0;
|
|
2131
|
+
retained_valid_ = false;
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
|
|
2135
|
+
last_commands_.clear();
|
|
2136
|
+
last_commands_.reserve(draw_commands.size());
|
|
2137
|
+
if (patch) {
|
|
2138
|
+
for (uint64_t key : retained_order_) last_commands_.push_back(retained_commands_.at(key));
|
|
2139
|
+
} else {
|
|
2140
|
+
for (const KeyedCommand &keyed : packet.commands) last_commands_.push_back(keyed.command);
|
|
2141
|
+
}
|
|
2142
|
+
clear_color_ = clear;
|
|
2143
|
+
|
|
2144
|
+
if (info) {
|
|
2145
|
+
info->did_render = true;
|
|
2146
|
+
info->decode_ns = draw_begin_ns - decode_begin_ns;
|
|
2147
|
+
info->draw_ns = gpuClockNs() - draw_begin_ns;
|
|
2148
|
+
info->nonblank = clear.r != 0 || clear.g != 0 || clear.b != 0 || !last_commands_.empty();
|
|
2149
|
+
info->sample_color = representativeColorAt(present.surface_width * 0.5, present.surface_height * 0.5);
|
|
2150
|
+
if (!full_surface && packet.has_scissor) {
|
|
2151
|
+
const Rect surface = {0, 0, static_cast<float>(present.surface_width), static_cast<float>(present.surface_height)};
|
|
2152
|
+
const Rect scissor = intersection(normalized(packet.scissor), surface);
|
|
2153
|
+
const std::vector<Rect> ®ions = packet.dirty_rects;
|
|
2154
|
+
auto append_dirty = [&](Rect dirty) {
|
|
2155
|
+
dirty = intersection(normalized(dirty), scissor);
|
|
2156
|
+
if (empty(dirty) || info->dirty_rect_count >= kWindowsGpuDirtyRectCap) return;
|
|
2157
|
+
RECT &pixels = info->dirty_rects[info->dirty_rect_count];
|
|
2158
|
+
pixels.left = std::max<LONG>(0, static_cast<LONG>(std::floor(dirty.x * scale)));
|
|
2159
|
+
pixels.top = std::max<LONG>(0, static_cast<LONG>(std::floor(dirty.y * scale)));
|
|
2160
|
+
pixels.right = std::min<LONG>(static_cast<LONG>(pixel_width), static_cast<LONG>(std::ceil((dirty.x + dirty.width) * scale)));
|
|
2161
|
+
pixels.bottom = std::min<LONG>(static_cast<LONG>(pixel_height), static_cast<LONG>(std::ceil((dirty.y + dirty.height) * scale)));
|
|
2162
|
+
if (pixels.right > pixels.left && pixels.bottom > pixels.top) info->dirty_rect_count += 1;
|
|
2163
|
+
};
|
|
2164
|
+
if (regions.empty()) append_dirty(scissor);
|
|
2165
|
+
else for (Rect dirty : regions) append_dirty(dirty);
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
return 1;
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
bool GpuSurfaceImpl::paint(const RECT *paint_rects, size_t paint_rect_count) {
|
|
2172
|
+
if (!content_valid_ || !backing_bitmap_) return true;
|
|
2173
|
+
if (paint_rect_count > 0 && paint_rects == nullptr) return false;
|
|
2174
|
+
if (!ensureWindowTarget()) return false;
|
|
2175
|
+
RECT client = {};
|
|
2176
|
+
if (!GetClientRect(hwnd_, &client)) return false;
|
|
2177
|
+
const UINT client_width = static_cast<UINT>(std::max<LONG>(1, client.right - client.left));
|
|
2178
|
+
const UINT client_height = static_cast<UINT>(std::max<LONG>(1, client.bottom - client.top));
|
|
2179
|
+
const D2D1_SIZE_U current = window_target_->GetPixelSize();
|
|
2180
|
+
if ((current.width != client_width || current.height != client_height) &&
|
|
2181
|
+
FAILED(window_target_->Resize(D2D1::SizeU(client_width, client_height)))) {
|
|
2182
|
+
releaseDeviceResources(true);
|
|
2183
|
+
return false;
|
|
2184
|
+
}
|
|
2185
|
+
window_target_->SetDpi(static_cast<FLOAT>(96.0 * scale_), static_cast<FLOAT>(96.0 * scale_));
|
|
2186
|
+
const float logical_client_width = static_cast<float>(client_width / scale_);
|
|
2187
|
+
const float logical_client_height = static_cast<float>(client_height / scale_);
|
|
2188
|
+
window_target_->BeginDraw();
|
|
2189
|
+
window_target_->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
2190
|
+
for (size_t index = 0; index < paint_rect_count; ++index) {
|
|
2191
|
+
const RECT &paint_rect = paint_rects[index];
|
|
2192
|
+
const Rect clip = {
|
|
2193
|
+
static_cast<float>(paint_rect.left / scale_),
|
|
2194
|
+
static_cast<float>(paint_rect.top / scale_),
|
|
2195
|
+
static_cast<float>((paint_rect.right - paint_rect.left) / scale_),
|
|
2196
|
+
static_cast<float>((paint_rect.bottom - paint_rect.top) / scale_),
|
|
2197
|
+
};
|
|
2198
|
+
if (empty(clip)) continue;
|
|
2199
|
+
window_target_->PushAxisAlignedClip(d2dRect(clip), D2D1_ANTIALIAS_MODE_ALIASED);
|
|
2200
|
+
window_target_->DrawBitmap(backing_bitmap_,
|
|
2201
|
+
D2D1::RectF(0, 0, logical_client_width, logical_client_height), 1.0f,
|
|
2202
|
+
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, nullptr);
|
|
2203
|
+
window_target_->PopAxisAlignedClip();
|
|
2204
|
+
}
|
|
2205
|
+
const HRESULT result = window_target_->EndDraw();
|
|
2206
|
+
if (result == D2DERR_RECREATE_TARGET || FAILED(result)) {
|
|
2207
|
+
releaseDeviceResources(true);
|
|
2208
|
+
return false;
|
|
2209
|
+
}
|
|
2210
|
+
return true;
|
|
2211
|
+
}
|
|
2212
|
+
|
|
2213
|
+
bool GpuSurfaceImpl::readColorAt(double logical_x, double logical_y, uint32_t *color) {
|
|
2214
|
+
if (!color || !content_valid_ || !backing_target_ || !backing_bitmap_ || !(scale_ > 0) ||
|
|
2215
|
+
!std::isfinite(logical_x) || !std::isfinite(logical_y)) return false;
|
|
2216
|
+
const double pixel_x_value = std::floor(logical_x * scale_);
|
|
2217
|
+
const double pixel_y_value = std::floor(logical_y * scale_);
|
|
2218
|
+
if (pixel_x_value < 0 || pixel_y_value < 0 ||
|
|
2219
|
+
pixel_x_value >= pixel_width_ || pixel_y_value >= pixel_height_) return false;
|
|
2220
|
+
|
|
2221
|
+
ID2D1GdiInteropRenderTarget *interop = nullptr;
|
|
2222
|
+
HRESULT result = backing_target_->QueryInterface(
|
|
2223
|
+
__uuidof(ID2D1GdiInteropRenderTarget), reinterpret_cast<void **>(&interop));
|
|
2224
|
+
if (FAILED(result) || !interop) {
|
|
2225
|
+
releaseCom(interop);
|
|
2226
|
+
return false;
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
/* The backing target alone is GDI-compatible. COPY synchronizes its
|
|
2230
|
+
* retained GPU bitmap into the returned DC, and GetPixel reads exactly
|
|
2231
|
+
* one device pixel. Hidden-titlebar caption sampling is the sole caller,
|
|
2232
|
+
* so ordinary packet frames never pay this synchronization cost. */
|
|
2233
|
+
backing_target_->BeginDraw();
|
|
2234
|
+
HDC dc = nullptr;
|
|
2235
|
+
result = interop->GetDC(D2D1_DC_INITIALIZE_MODE_COPY, &dc);
|
|
2236
|
+
COLORREF sampled = CLR_INVALID;
|
|
2237
|
+
HRESULT released = S_OK;
|
|
2238
|
+
if (SUCCEEDED(result) && dc) {
|
|
2239
|
+
sampled = GetPixel(dc, static_cast<int>(pixel_x_value), static_cast<int>(pixel_y_value));
|
|
2240
|
+
released = interop->ReleaseDC(nullptr);
|
|
2241
|
+
}
|
|
2242
|
+
const HRESULT ended = backing_target_->EndDraw();
|
|
2243
|
+
releaseCom(interop);
|
|
2244
|
+
if (FAILED(result) || FAILED(released) || FAILED(ended) || sampled == CLR_INVALID) return false;
|
|
2245
|
+
*color = 0xff000000u |
|
|
2246
|
+
(static_cast<uint32_t>(GetRValue(sampled)) << 16) |
|
|
2247
|
+
(static_cast<uint32_t>(GetGValue(sampled)) << 8) |
|
|
2248
|
+
static_cast<uint32_t>(GetBValue(sampled));
|
|
2249
|
+
return true;
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
uint32_t GpuSurfaceImpl::representativeColorAt(double logical_x, double logical_y) const {
|
|
2253
|
+
Color result = clear_color_;
|
|
2254
|
+
for (const Command &command : last_commands_) {
|
|
2255
|
+
if (!contains(command.bounds, logical_x, logical_y) || command.opacity <= 0) continue;
|
|
2256
|
+
const bool solid_fill = (command.kind == 0 || command.kind == 2 || command.kind == 8) &&
|
|
2257
|
+
command.paint.kind == Paint::Kind::color;
|
|
2258
|
+
if (!solid_fill) continue;
|
|
2259
|
+
const Color source = command.paint.color;
|
|
2260
|
+
const float alpha = clamp01(source.a * command.opacity);
|
|
2261
|
+
result.r = source.r * alpha + result.r * (1.0f - alpha);
|
|
2262
|
+
result.g = source.g * alpha + result.g * (1.0f - alpha);
|
|
2263
|
+
result.b = source.b * alpha + result.b * (1.0f - alpha);
|
|
2264
|
+
result.a = alpha + result.a * (1.0f - alpha);
|
|
2265
|
+
}
|
|
2266
|
+
return packedColor(result);
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2269
|
+
std::shared_ptr<WindowsGpuSurface> GpuRendererImpl::createSurface(HWND hwnd) {
|
|
2270
|
+
if (!hwnd) return nullptr;
|
|
2271
|
+
return std::make_shared<GpuSurfaceImpl>(shared_from_this(), hwnd);
|
|
2272
|
+
}
|
|
2273
|
+
|
|
2274
|
+
} // namespace
|
|
2275
|
+
|
|
2276
|
+
std::shared_ptr<WindowsGpuRenderer> createWindowsGpuRenderer() {
|
|
2277
|
+
auto renderer = std::make_shared<GpuRendererImpl>();
|
|
2278
|
+
if (!renderer->initialize()) return nullptr;
|
|
2279
|
+
return renderer;
|
|
2280
|
+
}
|