@geastack/engine 0.1.5

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.
Files changed (86) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +29 -0
  3. package/bitmap_font.cpp +165 -0
  4. package/canvas.cpp +4608 -0
  5. package/components/Button.d.ts +4 -0
  6. package/components/Button.tsx +32 -0
  7. package/components/Image.d.ts +3 -0
  8. package/components/Image.tsx +17 -0
  9. package/components/Text.d.ts +3 -0
  10. package/components/Text.tsx +9 -0
  11. package/components/View.d.ts +3 -0
  12. package/components/View.tsx +24 -0
  13. package/components/index.d.ts +8 -0
  14. package/components/index.ts +4 -0
  15. package/css/animation.h +252 -0
  16. package/css/declarative.h +131 -0
  17. package/css/easing.h +160 -0
  18. package/css/engine.h +223 -0
  19. package/css/interpolate.h +38 -0
  20. package/image_store.cpp +807 -0
  21. package/package.json +62 -0
  22. package/rasterized_font.cpp +570 -0
  23. package/touch_runtime.cpp +513 -0
  24. package/ui/absolute_leaf_refresh.cpp +279 -0
  25. package/ui/absolute_leaf_refresh.h +12 -0
  26. package/ui/canvas_element.cpp +2314 -0
  27. package/ui/canvas_element.h +353 -0
  28. package/ui/canvas_store.cpp +188 -0
  29. package/ui/canvas_store.h +59 -0
  30. package/ui/core.cpp +135 -0
  31. package/ui/css_atom.cpp +155 -0
  32. package/ui/css_atom.h +21 -0
  33. package/ui/dirty_regions.cpp +66 -0
  34. package/ui/dirty_regions.h +34 -0
  35. package/ui/display_invalidation.cpp +101 -0
  36. package/ui/display_invalidation.h +15 -0
  37. package/ui/document.cpp +356 -0
  38. package/ui/document.h +101 -0
  39. package/ui/image.h +16 -0
  40. package/ui/image_element.cpp +13 -0
  41. package/ui/image_node.cpp +165 -0
  42. package/ui/input.cpp +936 -0
  43. package/ui/input_render.cpp +213 -0
  44. package/ui/internal.h +567 -0
  45. package/ui/layout.cpp +2025 -0
  46. package/ui/layout_snapshot.cpp +82 -0
  47. package/ui/layout_snapshot.h +12 -0
  48. package/ui/mirror.h +25 -0
  49. package/ui/node.cpp +243 -0
  50. package/ui/node.h +154 -0
  51. package/ui/node_lifecycle.cpp +137 -0
  52. package/ui/node_lifecycle.h +13 -0
  53. package/ui/node_model.h +597 -0
  54. package/ui/refresh_perf.h +235 -0
  55. package/ui/refresh_trace.h +68 -0
  56. package/ui/render.cpp +12097 -0
  57. package/ui/root_scroll_refresh.cpp +859 -0
  58. package/ui/root_scroll_refresh.h +30 -0
  59. package/ui/state_init.h +21 -0
  60. package/ui/style.cpp +14994 -0
  61. package/ui/style.h +727 -0
  62. package/ui/style_values.h +31 -0
  63. package/ui/text.cpp +2010 -0
  64. package/ui/text.h +15 -0
  65. package/ui/text_element.cpp +15 -0
  66. package/ui/tree_events.cpp +621 -0
  67. package/ui/tree_events.h +69 -0
  68. package/ui/tree_internal.h +191 -0
  69. package/ui/tree_nodes.cpp +628 -0
  70. package/ui/tree_render.cpp +3138 -0
  71. package/ui/tree_scroll_mirror.cpp +101 -0
  72. package/ui/tree_state.cpp +574 -0
  73. package/ui/tree_state.h +235 -0
  74. package/ui/tree_style.cpp +698 -0
  75. package/ui/ui.h +9 -0
  76. package/ui/view.cpp +2232 -0
  77. package/ui/view.h +14 -0
  78. package/ui/view_element.cpp +13 -0
  79. package/ui/viewport_region.cpp +36 -0
  80. package/ui/viewport_region.h +12 -0
  81. package/ui/virtual_keyboard.cpp +908 -0
  82. package/ui/virtual_keyboard.h +59 -0
  83. package/vendor/AnimatedGIF/AnimatedGIF.c +982 -0
  84. package/vendor/AnimatedGIF/AnimatedGIF.h +216 -0
  85. package/vendor/stb/stb_image.h +7988 -0
  86. package/vendor/stb/stb_truetype.h +5079 -0
@@ -0,0 +1,2314 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "canvas_element.h"
3
+
4
+ #include "display.h"
5
+ #include "internal.h"
6
+ #include "pixel.h"
7
+ #include "tree_internal.h"
8
+
9
+ #include <graphics/font.h>
10
+ #include <image.h>
11
+
12
+ #include <algorithm>
13
+ #include <chrono>
14
+ #include <cmath>
15
+ #include <cstdio>
16
+ #include <cstdlib>
17
+ #include <utility>
18
+ #include <vector>
19
+
20
+ #include "gea_perf_config.h" // GEA_EMBEDDED_CANVAS_PERF_DETAIL (+ GEA_EMBEDDED_PERF master)
21
+
22
+ namespace gea::embedded::ui {
23
+
24
+ namespace {
25
+
26
+ CanvasPerfStats gCanvasPerfStats;
27
+
28
+ // NEVER-RESET totals (the window stats above reset per perf interval).
29
+ int gTotalBeginBatch = 0;
30
+ int gTotalEndBatch = 0;
31
+ int gTotalFillRect = 0;
32
+ int gTotalDrawImage = 0;
33
+ int gTotalDrawImageNullPixels = 0;
34
+ int gTotalPresentBatchOk = 0;
35
+
36
+ // Canvas-2D imperative colours arrive already in this board's native pixel
37
+ // (geatsc folds literals via nativeFromRgba8888 at build time; runtime values go
38
+ // through geaCanvasValueNative / parseCanvasColor). So these are identity
39
+ // pass-throughs now, kept as named seams for the geatsc-emitted call sites.
40
+ gea::framework::graphics::pixel::native_t canvasRgb565(gea::framework::graphics::pixel::native_t color)
41
+ {
42
+ return color;
43
+ }
44
+
45
+ std::int64_t nowUs()
46
+ {
47
+ #if GEA_EMBEDDED_CANVAS_PERF_DETAIL
48
+ const auto now = std::chrono::steady_clock::now().time_since_epoch();
49
+ return std::chrono::duration_cast<std::chrono::microseconds>(now).count();
50
+ #else
51
+ return 0;
52
+ #endif
53
+ }
54
+
55
+ int clampColorChannel(int value)
56
+ {
57
+ if (value < 0) return 0;
58
+ if (value > 255) return 255;
59
+ return value;
60
+ }
61
+
62
+ gea::framework::graphics::pixel::native_t parseCanvasColor(const std::string &value)
63
+ {
64
+ const char *cursor = value.c_str();
65
+ auto skipSpace = [&]() {
66
+ while (*cursor == ' ' || *cursor == '\t' || *cursor == '\n' || *cursor == '\r') cursor++;
67
+ };
68
+ auto readCssNumber = [&](int &out) -> bool {
69
+ skipSpace();
70
+ char *end = nullptr;
71
+ long parsed = std::strtol(cursor, &end, 10);
72
+ if (end == cursor) return false;
73
+ cursor = end;
74
+ if (*cursor == '.') {
75
+ cursor++;
76
+ while (*cursor >= '0' && *cursor <= '9') cursor++;
77
+ }
78
+ skipSpace();
79
+ out = static_cast<int>(parsed);
80
+ return true;
81
+ };
82
+ auto consume = [&](char expected) -> bool {
83
+ skipSpace();
84
+ if (*cursor != expected) return false;
85
+ cursor++;
86
+ return true;
87
+ };
88
+
89
+ int r = 0, g = 0, b = 0;
90
+ if (value.rfind("rgb(", 0) == 0) {
91
+ cursor += 4;
92
+ if (readCssNumber(r) && consume(',') && readCssNumber(g) && consume(',') &&
93
+ readCssNumber(b) && consume(')')) {
94
+ return gea::framework::graphics::pixel::nativeColor(
95
+ clampColorChannel(r),
96
+ clampColorChannel(g),
97
+ clampColorChannel(b));
98
+ }
99
+ }
100
+ if (std::sscanf(value.c_str(), "rgb(%d,%d,%d)", &r, &g, &b) == 3 ||
101
+ std::sscanf(value.c_str(), "rgb(%d, %d, %d)", &r, &g, &b) == 3) {
102
+ return gea::framework::graphics::pixel::nativeColor(
103
+ clampColorChannel(r),
104
+ clampColorChannel(g),
105
+ clampColorChannel(b));
106
+ }
107
+ if (value.size() == 7 && value[0] == '#') {
108
+ char *end = nullptr;
109
+ const long rgb = std::strtol(value.c_str() + 1, &end, 16);
110
+ if (end && *end == '\0') {
111
+ return gea::framework::graphics::pixel::nativeColor(
112
+ static_cast<int>((rgb >> 16) & 0xff),
113
+ static_cast<int>((rgb >> 8) & 0xff),
114
+ static_cast<int>(rgb & 0xff));
115
+ }
116
+ }
117
+ return gea::framework::graphics::pixel::nativeColor(255, 255, 255);
118
+ }
119
+
120
+ int rounded(double value)
121
+ {
122
+ if (!std::isfinite(value)) return 0;
123
+ return static_cast<int>(value + (value >= 0 ? 0.5 : -0.5));
124
+ }
125
+
126
+ std::uint8_t alphaFromUnit(double value)
127
+ {
128
+ if (!std::isfinite(value)) return 255;
129
+ if (value < 0.0) value = 0.0;
130
+ if (value > 1.0) value = 1.0;
131
+ return static_cast<std::uint8_t>(value * 255.0 + 0.5);
132
+ }
133
+
134
+ // Pixel size from a CSS font shorthand ("16px Inter") — 0 when absent.
135
+ int fontPxFromCss(const std::string &font)
136
+ {
137
+ const char *cursor = font.c_str();
138
+ while (*cursor) {
139
+ char *end = nullptr;
140
+ const double parsed = std::strtod(cursor, &end);
141
+ if (end != cursor && parsed > 0.0) {
142
+ while (*end == ' ' || *end == '\t') end++;
143
+ if (end[0] == 'p' && end[1] == 'x') return static_cast<int>(parsed + 0.5);
144
+ cursor = end;
145
+ continue;
146
+ }
147
+ cursor++;
148
+ }
149
+ return 0;
150
+ }
151
+
152
+ // First family name after the size in a CSS font shorthand; empty when none.
153
+ std::string fontFamilyFromCss(const std::string &font)
154
+ {
155
+ const std::size_t px = font.find("px");
156
+ std::size_t start = px == std::string::npos ? 0 : px + 2;
157
+ while (start < font.size() && (font[start] == ' ' || font[start] == '"' || font[start] == '\'')) start++;
158
+ std::size_t end = start;
159
+ while (end < font.size() && font[end] != ',' && font[end] != '"' && font[end] != '\'') end++;
160
+ while (end > start && font[end - 1] == ' ') end--;
161
+ return font.substr(start, end - start);
162
+ }
163
+
164
+ float fontScaleFromCss(const std::string &font)
165
+ {
166
+ const char *cursor = font.c_str();
167
+ while (*cursor) {
168
+ char *end = nullptr;
169
+ const double parsed = std::strtod(cursor, &end);
170
+ if (end != cursor && parsed > 0.0) {
171
+ while (*end == ' ' || *end == '\t') end++;
172
+ if (end[0] == 'p' && end[1] == 'x') return static_cast<float>(parsed / 16.0);
173
+ cursor = end;
174
+ continue;
175
+ }
176
+ cursor++;
177
+ }
178
+ return 1.0f;
179
+ }
180
+
181
+ } // namespace
182
+
183
+ void canvasPerfStatsReset()
184
+ {
185
+ gCanvasPerfStats = {};
186
+ }
187
+
188
+ CanvasPerfStats canvasPerfStatsRead()
189
+ {
190
+ #if GEA_EMBEDDED_CANVAS_PERF_DETAIL
191
+ return gCanvasPerfStats;
192
+ #else
193
+ // Perf off: nothing observes gCanvasPerfStats, so the per-op `…Calls++` writes
194
+ // in the hot draw path (fillCircle ×1000/frame, etc.) become dead stores to a
195
+ // never-read, internal-linkage static and are dead-store-eliminated — no
196
+ // counter machinery survives. (The timing reads were already compiled out:
197
+ // the file-local nowUs() returns 0 when GEA_EMBEDDED_CANVAS_PERF_DETAIL is 0.)
198
+ return {};
199
+ #endif
200
+ }
201
+
202
+ void canvasTotalsRead(int *begin, int *end, int *fillRect, int *drawImage, int *nullPixels, int *presentOk)
203
+ {
204
+ *begin = gTotalBeginBatch;
205
+ *end = gTotalEndBatch;
206
+ *fillRect = gTotalFillRect;
207
+ *drawImage = gTotalDrawImage;
208
+ *nullPixels = gTotalDrawImageNullPixels;
209
+ *presentOk = gTotalPresentBatchOk;
210
+ }
211
+
212
+ CanvasPresentCommand &CanvasRenderingContext2D::appendPresentCommand(CanvasPresentCommandType type)
213
+ {
214
+ if (presentCommandCount_ < presentCommands_.size()) {
215
+ CanvasPresentCommand &command = presentCommands_[presentCommandCount_++];
216
+ command.type = type;
217
+ return command;
218
+ }
219
+ presentCommands_.emplace_back();
220
+ presentCommandCount_ = presentCommands_.size();
221
+ CanvasPresentCommand &command = presentCommands_.back();
222
+ command.type = type;
223
+ return command;
224
+ }
225
+
226
+ void CanvasRenderingContext2D::resetPresentCommands()
227
+ {
228
+ presentCommandCount_ = 0;
229
+ }
230
+
231
+ #if defined(GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT) && GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT
232
+
233
+ CanvasElement CanvasElement::create()
234
+ {
235
+ return CanvasElement(-1);
236
+ }
237
+
238
+ CanvasRenderingContext2D CanvasElement::getContext2D() const
239
+ {
240
+ return CanvasRenderingContext2D(-1);
241
+ }
242
+
243
+ gea::framework::graphics::Canvas *CanvasRenderingContext2D::canvas()
244
+ {
245
+ return gea::platform::display::Display::canvas();
246
+ }
247
+
248
+ gea::framework::graphics::Canvas *CanvasRenderingContext2D::drawingCanvas()
249
+ {
250
+ return canvas();
251
+ }
252
+
253
+ bool CanvasRenderingContext2D::recordingPresentBatch() const
254
+ {
255
+ return presentRecording_ && batchDepth_ > 0;
256
+ }
257
+
258
+ void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixel::native_t color)
259
+ {
260
+ resetPresentCommands();
261
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::Clear);
262
+ command.clearColor = color;
263
+ batchDirty_ = true;
264
+ }
265
+
266
+ void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
267
+ {
268
+ if (w <= 0 || h <= 0) return;
269
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillRectRgb565);
270
+ command.x = x;
271
+ command.y = y;
272
+ command.w = w;
273
+ command.h = h;
274
+ command.color = color;
275
+ command.alpha = globalAlpha_;
276
+ batchDirty_ = true;
277
+ }
278
+
279
+ void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
280
+ {
281
+ if (w <= 0 || h <= 0) return;
282
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::StrokeRectRgb565);
283
+ command.x = x;
284
+ command.y = y;
285
+ command.w = w;
286
+ command.h = h;
287
+ command.color = color;
288
+ command.alpha = globalAlpha_;
289
+ batchDirty_ = true;
290
+ }
291
+
292
+ void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
293
+ int y0,
294
+ int x1,
295
+ int y1,
296
+ int x2,
297
+ int y2,
298
+ gea::framework::graphics::pixel::native_t color)
299
+ {
300
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTriangleRgb565);
301
+ command.x0 = x0;
302
+ command.y0 = y0;
303
+ command.x1 = x1;
304
+ command.y1 = y1;
305
+ command.x2 = x2;
306
+ command.y2 = y2;
307
+ command.color = color;
308
+ command.alpha = globalAlpha_;
309
+ batchDirty_ = true;
310
+ }
311
+
312
+ void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
313
+ {
314
+ if (radius <= 0) return;
315
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCircleRgb565);
316
+ command.x = x;
317
+ command.y = y;
318
+ command.radius = radius;
319
+ command.color = color;
320
+ command.alpha = globalAlpha_;
321
+ batchDirty_ = true;
322
+ }
323
+
324
+ void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
325
+ {
326
+ if (radius <= 0) return;
327
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::StrokeCircleRgb565);
328
+ command.x = x;
329
+ command.y = y;
330
+ command.radius = radius;
331
+ command.color = color;
332
+ command.alpha = globalAlpha_;
333
+ batchDirty_ = true;
334
+ }
335
+
336
+ // Pointer + count forms: no temporary vector, and only the live `count`
337
+ // elements are copied into the recorded command (which reuses its capacity
338
+ // across frames, so this is a memcpy into an already-sized buffer).
339
+ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
340
+ const std::uint16_t *ys,
341
+ int radius,
342
+ const gea::framework::graphics::pixel::native_t *colors,
343
+ int count)
344
+ {
345
+ if (radius <= 0 || count <= 0 || !xs || !ys || !colors) return;
346
+ const std::size_t limit = static_cast<std::size_t>(count);
347
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
348
+ command.radius = radius;
349
+ command.alpha = globalAlpha_;
350
+ command.xs.assign(xs, xs + limit);
351
+ command.ys.assign(ys, ys + limit);
352
+ command.colors.assign(colors, colors + limit);
353
+ batchDirty_ = true;
354
+ }
355
+
356
+ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
357
+ const std::uint16_t *ys,
358
+ int radius,
359
+ const gea::framework::graphics::pixel::NativeColor *colors,
360
+ int count)
361
+ {
362
+ if (radius <= 0 || count <= 0 || !xs || !ys || !colors) return;
363
+ const std::size_t limit = static_cast<std::size_t>(count);
364
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
365
+ command.radius = radius;
366
+ command.alpha = globalAlpha_;
367
+ command.xs.assign(xs, xs + limit);
368
+ command.ys.assign(ys, ys + limit);
369
+ command.colors.resize(limit);
370
+ for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
371
+ batchDirty_ = true;
372
+ }
373
+
374
+ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
375
+ const std::vector<std::uint16_t> &ys,
376
+ int radius,
377
+ const std::vector<gea::framework::graphics::pixel::native_t> &colors,
378
+ int count)
379
+ {
380
+ if (radius <= 0) return;
381
+ std::size_t limit = xs.size() < ys.size() ? xs.size() : ys.size();
382
+ if (colors.size() < limit) limit = colors.size();
383
+ if (count >= 0 && static_cast<std::size_t>(count) < limit) limit = static_cast<std::size_t>(count);
384
+ if (limit == 0) return;
385
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
386
+ command.radius = radius;
387
+ command.alpha = globalAlpha_;
388
+ command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(limit));
389
+ command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
390
+ command.colors.assign(colors.begin(), colors.begin() + static_cast<std::ptrdiff_t>(limit));
391
+ batchDirty_ = true;
392
+ }
393
+
394
+ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
395
+ const std::vector<std::uint16_t> &ys,
396
+ int radius,
397
+ const std::vector<gea::framework::graphics::pixel::NativeColor> &colors,
398
+ int count)
399
+ {
400
+ if (radius <= 0) return;
401
+ std::size_t limit = xs.size() < ys.size() ? xs.size() : ys.size();
402
+ if (colors.size() < limit) limit = colors.size();
403
+ if (count >= 0 && static_cast<std::size_t>(count) < limit) limit = static_cast<std::size_t>(count);
404
+ if (limit == 0) return;
405
+
406
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
407
+ command.radius = radius;
408
+ command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(limit));
409
+ command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
410
+ command.colors.resize(limit);
411
+ for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
412
+ command.alpha = globalAlpha_;
413
+ batchDirty_ = true;
414
+ }
415
+
416
+ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std::uint16_t> &xs,
417
+ const std::vector<std::uint16_t> &ys,
418
+ int radius,
419
+ gea::framework::graphics::pixel::native_t color)
420
+ {
421
+ const int capped = static_cast<int>(std::min(xs.size(), ys.size()));
422
+ if (capped <= 0 || radius <= 0) return;
423
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
424
+ command.radius = radius;
425
+ command.alpha = globalAlpha_;
426
+ command.xs.assign(xs.begin(), xs.begin() + capped);
427
+ command.ys.assign(ys.begin(), ys.begin() + capped);
428
+ command.colors.assign(static_cast<std::size_t>(capped), canvasRgb565(color));
429
+ batchDirty_ = true;
430
+ }
431
+
432
+ void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
433
+ const std::vector<std::int32_t> &y0s,
434
+ const std::vector<std::int32_t> &x1s,
435
+ const std::vector<std::int32_t> &y1s,
436
+ const std::vector<std::int32_t> &x2s,
437
+ const std::vector<std::int32_t> &y2s,
438
+ const std::vector<std::uint32_t> &colors,
439
+ const std::vector<std::int32_t> &order,
440
+ int count)
441
+ {
442
+ std::size_t limit = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size(), order.size()});
443
+ if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
444
+ if (limit == 0) return;
445
+ using gea::framework::graphics::TriangleEntry;
446
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
447
+ command.alpha = globalAlpha_;
448
+ command.triangles.resize(limit);
449
+ const std::int32_t *px0 = x0s.data();
450
+ const std::int32_t *py0 = y0s.data();
451
+ const std::int32_t *px1 = x1s.data();
452
+ const std::int32_t *py1 = y1s.data();
453
+ const std::int32_t *px2 = x2s.data();
454
+ const std::int32_t *py2 = y2s.data();
455
+ const std::uint32_t *pcolors = colors.data();
456
+ const std::int32_t *porder = order.data();
457
+ TriangleEntry *dst = command.triangles.data();
458
+ const std::size_t sourceSize = x0s.size();
459
+ for (std::size_t i = 0; i < limit; i++) {
460
+ const std::int32_t rawIndex = porder[i];
461
+ const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
462
+ ? static_cast<std::size_t>(rawIndex)
463
+ : 0;
464
+ TriangleEntry &t = dst[i];
465
+ t.x0 = static_cast<std::int16_t>(px0[o]);
466
+ t.y0 = static_cast<std::int16_t>(py0[o]);
467
+ t.x1 = static_cast<std::int16_t>(px1[o]);
468
+ t.y1 = static_cast<std::int16_t>(py1[o]);
469
+ t.x2 = static_cast<std::int16_t>(px2[o]);
470
+ t.y2 = static_cast<std::int16_t>(py2[o]);
471
+ t.color = gea::framework::graphics::pixel::nativeFromRrggbbaa(pcolors[o]);
472
+ const std::int16_t lo01 = t.y0 < t.y1 ? t.y0 : t.y1;
473
+ const std::int16_t hi01 = t.y0 > t.y1 ? t.y0 : t.y1;
474
+ t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
475
+ t.rowY1 = hi01 > t.y2 ? hi01 : t.y2;
476
+ }
477
+ batchDirty_ = true;
478
+ }
479
+
480
+ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::graphics::pixel::native_t *pixels,
481
+ const std::uint8_t *alphaPixels,
482
+ int srcWidth,
483
+ int srcHeight,
484
+ int x,
485
+ int y)
486
+ {
487
+ if (!pixels || srcWidth <= 0 || srcHeight <= 0) return;
488
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImage);
489
+ command.pixels = pixels;
490
+ command.alphaPixels = alphaPixels;
491
+ command.srcWidth = srcWidth;
492
+ command.srcHeight = srcHeight;
493
+ command.x = x;
494
+ command.y = y;
495
+ command.alpha = globalAlpha_;
496
+ batchDirty_ = true;
497
+ }
498
+
499
+ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework::graphics::pixel::native_t *pixels,
500
+ const std::uint8_t *alphaPixels,
501
+ int srcWidth,
502
+ int srcHeight,
503
+ int x,
504
+ int y,
505
+ int w,
506
+ int h,
507
+ int radius)
508
+ {
509
+ if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
510
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageScaled);
511
+ command.pixels = pixels;
512
+ command.alphaPixels = alphaPixels;
513
+ command.srcWidth = srcWidth;
514
+ command.srcHeight = srcHeight;
515
+ command.x = x;
516
+ command.y = y;
517
+ command.w = w;
518
+ command.h = h;
519
+ command.radius = radius;
520
+ command.alpha = globalAlpha_;
521
+ batchDirty_ = true;
522
+ }
523
+ void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::framework::graphics::pixel::native_t *, const std::uint8_t *, int, int, int, int, int, int) {}
524
+ void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework::graphics::pixel::native_t *, const std::uint8_t *, int, int, int, int, int) {}
525
+ void CanvasRenderingContext2D::appendPresentFillText(const std::string &, int, int, gea::framework::graphics::pixel::native_t) {}
526
+ void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphics::Canvas &) {}
527
+
528
+ bool CanvasRenderingContext2D::presentBatch()
529
+ {
530
+ if (presentCommandCount_ == 0) return false;
531
+ gTotalPresentBatchOk++;
532
+ std::vector<gea::platform::display::DisplayPresentCommand> commands;
533
+ commands.reserve(presentCommandCount_);
534
+ for (std::size_t commandIndex = 0; commandIndex < presentCommandCount_; ++commandIndex) {
535
+ const CanvasPresentCommand &command = presentCommands_[commandIndex];
536
+ gea::platform::display::DisplayPresentCommand displayCommand{};
537
+ switch (command.type) {
538
+ case CanvasPresentCommandType::Clear:
539
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::Clear;
540
+ displayCommand.clear.color = command.clearColor;
541
+ break;
542
+ case CanvasPresentCommandType::FillRectRgb565:
543
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillRectRgb565;
544
+ displayCommand.fillRectRgb565 = {command.x, command.y, command.w, command.h, command.color, command.alpha};
545
+ break;
546
+ case CanvasPresentCommandType::StrokeRectRgb565:
547
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeRectRgb565;
548
+ displayCommand.strokeRectRgb565 = {command.x, command.y, command.w, command.h, command.color, command.alpha};
549
+ break;
550
+ case CanvasPresentCommandType::FillTriangleRgb565:
551
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillTriangleRgb565;
552
+ displayCommand.fillTriangleRgb565 = {command.x0, command.y0, command.x1, command.y1, command.x2, command.y2, command.color, command.alpha};
553
+ break;
554
+ case CanvasPresentCommandType::FillCircleRgb565:
555
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCircleRgb565;
556
+ displayCommand.fillCircleRgb565 = {command.x, command.y, command.radius, command.color, command.alpha};
557
+ break;
558
+ case CanvasPresentCommandType::StrokeCircleRgb565:
559
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeCircleRgb565;
560
+ displayCommand.strokeCircleRgb565 = {command.x, command.y, command.radius, command.color, command.alpha};
561
+ break;
562
+ case CanvasPresentCommandType::FillCirclesRgb565:
563
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCirclesRgb565;
564
+ displayCommand.fillCirclesRgb565.xs = command.xs.data();
565
+ displayCommand.fillCirclesRgb565.ys = command.ys.data();
566
+ displayCommand.fillCirclesRgb565.colors = command.colors.data();
567
+ displayCommand.fillCirclesRgb565.count = static_cast<int>(command.xs.size());
568
+ displayCommand.fillCirclesRgb565.radius = command.radius;
569
+ displayCommand.fillCirclesRgb565.alpha = command.alpha;
570
+ break;
571
+ case CanvasPresentCommandType::FillTrianglesRgb565:
572
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillTrianglesRgb565;
573
+ displayCommand.fillTrianglesRgb565.entries = command.triangles.data();
574
+ displayCommand.fillTrianglesRgb565.count = static_cast<int>(command.triangles.size());
575
+ displayCommand.fillTrianglesRgb565.alpha = command.alpha;
576
+ break;
577
+ case CanvasPresentCommandType::DrawImage:
578
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImage;
579
+ displayCommand.drawImage = {command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
580
+ command.x, command.y, command.alpha};
581
+ break;
582
+ case CanvasPresentCommandType::DrawImageScaled:
583
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageScaled;
584
+ displayCommand.drawImageScaled = {command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
585
+ command.x, command.y, command.w, command.h,
586
+ command.alpha, command.radius};
587
+ break;
588
+ default:
589
+ continue;
590
+ }
591
+ commands.push_back(displayCommand);
592
+ }
593
+ return gea::platform::display::Display::present(commands.data(), static_cast<int>(commands.size()));
594
+ }
595
+
596
+ void CanvasRenderingContext2D::applyDrawState(gea::framework::graphics::Canvas &surface) const
597
+ {
598
+ surface.setGlobalAlpha(globalAlpha_);
599
+ }
600
+
601
+ void CanvasRenderingContext2D::markDirty()
602
+ {
603
+ if (batchDepth_ > 0) batchDirty_ = true;
604
+ }
605
+
606
+ void CanvasRenderingContext2D::markDrawDirty()
607
+ {
608
+ markDirty();
609
+ }
610
+
611
+ void CanvasRenderingContext2D::setFillStyle(const std::string &value)
612
+ {
613
+ if (fillStyleCached_ && value == fillStyleSource_) return;
614
+ fillStyle_ = parseCanvasColor(value);
615
+ fillStyleSource_ = value;
616
+ fillStyleCached_ = true;
617
+ }
618
+
619
+ void CanvasRenderingContext2D::setFillStyleRgb565(gea::framework::graphics::pixel::native_t color)
620
+ {
621
+ fillStyle_ = canvasRgb565(color);
622
+ fillStyleCached_ = false;
623
+ }
624
+
625
+ void CanvasRenderingContext2D::setStrokeStyle(const std::string &value)
626
+ {
627
+ if (strokeStyleCached_ && value == strokeStyleSource_) return;
628
+ strokeStyle_ = parseCanvasColor(value);
629
+ strokeStyleSource_ = value;
630
+ strokeStyleCached_ = true;
631
+ }
632
+
633
+ void CanvasRenderingContext2D::setStrokeStyleRgb565(gea::framework::graphics::pixel::native_t color)
634
+ {
635
+ strokeStyle_ = canvasRgb565(color);
636
+ strokeStyleCached_ = false;
637
+ }
638
+
639
+ void CanvasRenderingContext2D::setGlobalAlpha(double alpha) { globalAlpha_ = alphaFromUnit(alpha); }
640
+ void CanvasRenderingContext2D::setLineWidth(double width) { if (std::isfinite(width) && width > 0.0) lineWidth_ = width; }
641
+ void CanvasRenderingContext2D::setFont(const std::string &font) { lastFontStr_ = font; fontScale_ = fontScaleFromCss(font); fontSizePx_ = fontPxFromCss(font); }
642
+
643
+ void CanvasRenderingContext2D::clear()
644
+ {
645
+ clearRect(0, 0, gea::platform::display::kWidth, gea::platform::display::kHeight);
646
+ }
647
+
648
+ void CanvasRenderingContext2D::clearRect(int x, int y, int w, int h)
649
+ {
650
+ const std::uint8_t previousAlpha = globalAlpha_;
651
+ globalAlpha_ = 255;
652
+ fillRect(x, y, w, h);
653
+ globalAlpha_ = previousAlpha;
654
+ }
655
+
656
+ void CanvasRenderingContext2D::clearRect(double x, double y, double w, double h) { clearRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
657
+
658
+ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
659
+ {
660
+ if (w <= 0 || h <= 0) return;
661
+ gTotalFillRect++;
662
+ if (recordingPresentBatch()) {
663
+ appendPresentFillRect(x, y, w, h, fillStyle_);
664
+ return;
665
+ }
666
+ gea::platform::display::Display::fillRect(x, y, w, h, fillStyle_);
667
+ }
668
+
669
+ void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h) { fillRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
670
+
671
+ void CanvasRenderingContext2D::strokeRect(int x, int y, int w, int h)
672
+ {
673
+ if (recordingPresentBatch()) {
674
+ appendPresentStrokeRect(x, y, w, h, strokeStyle_);
675
+ return;
676
+ }
677
+ gea::platform::display::Display::strokeRect(x, y, w, h, strokeStyle_);
678
+ }
679
+
680
+ void CanvasRenderingContext2D::strokeRect(double x, double y, double w, double h) { strokeRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
681
+
682
+ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius) { fillCircleRgb565(x, y, radius, fillStyle_); }
683
+ void CanvasRenderingContext2D::fillCircle(double x, double y, double radius) { fillCircle(rounded(x), rounded(y), rounded(radius)); }
684
+
685
+ void CanvasRenderingContext2D::strokeCircle(int x, int y, int radius)
686
+ {
687
+ if (recordingPresentBatch()) {
688
+ appendPresentStrokeCircle(x, y, radius, strokeStyle_);
689
+ return;
690
+ }
691
+ gea::platform::display::Display::strokeCircle(x, y, radius, strokeStyle_);
692
+ }
693
+
694
+ void CanvasRenderingContext2D::strokeCircle(double x, double y, double radius) { strokeCircle(rounded(x), rounded(y), rounded(radius)); }
695
+
696
+ void CanvasRenderingContext2D::fillCircleRgb565(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
697
+ {
698
+ if (recordingPresentBatch()) {
699
+ appendPresentFillCircle(x, y, radius, canvasRgb565(color));
700
+ return;
701
+ }
702
+ gea::platform::display::Display::fillCircle(x, y, radius, canvasRgb565(color));
703
+ }
704
+
705
+ void CanvasRenderingContext2D::fillCircleRgb565(double x, double y, double radius, gea::framework::graphics::pixel::native_t color)
706
+ {
707
+ fillCircleRgb565(rounded(x), rounded(y), rounded(radius), color);
708
+ }
709
+
710
+ void CanvasRenderingContext2D::fillTriangleRgb565(int x0, int y0, int x1, int y1, int x2, int y2, gea::framework::graphics::pixel::native_t color)
711
+ {
712
+ if (recordingPresentBatch()) {
713
+ appendPresentFillTriangle(x0, y0, x1, y1, x2, y2, canvasRgb565(color));
714
+ return;
715
+ }
716
+ gea::platform::display::Display::fillTriangle(x0, y0, x1, y1, x2, y2, canvasRgb565(color));
717
+ }
718
+
719
+ void CanvasRenderingContext2D::fillTriangleRgb565(double x0, double y0, double x1, double y1, double x2, double y2, gea::framework::graphics::pixel::native_t color)
720
+ {
721
+ fillTriangleRgb565(rounded(x0), rounded(y0), rounded(x1), rounded(y1), rounded(x2), rounded(y2), color);
722
+ }
723
+
724
+ void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::int32_t> &x0s,
725
+ const std::vector<std::int32_t> &y0s,
726
+ const std::vector<std::int32_t> &x1s,
727
+ const std::vector<std::int32_t> &y1s,
728
+ const std::vector<std::int32_t> &x2s,
729
+ const std::vector<std::int32_t> &y2s,
730
+ const std::vector<std::uint32_t> &colors,
731
+ const std::vector<std::int32_t> &order,
732
+ int count)
733
+ {
734
+ if (recordingPresentBatch()) appendPresentTriangles(x0s, y0s, x1s, y1s, x2s, y2s, colors, order, count);
735
+ }
736
+
737
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::uint16_t *xs, const std::uint16_t *ys, int radius, const gea::framework::graphics::pixel::native_t *colors, int count) { if (recordingPresentBatch()) appendPresentCircles(xs, ys, radius, colors, count); }
738
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::uint16_t *xs, const std::uint16_t *ys, int radius, const gea::framework::graphics::pixel::NativeColor *colors, int count) { if (recordingPresentBatch()) appendPresentCircles(xs, ys, radius, colors, count); }
739
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::int32_t> &, const std::vector<std::int32_t> &, int, const std::vector<gea::framework::graphics::pixel::native_t> &) {}
740
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t> &xs, const std::vector<std::uint16_t> &ys, int radius, const std::vector<gea::framework::graphics::pixel::native_t> &colors, int count) { if (recordingPresentBatch()) appendPresentCircles(xs, ys, radius, colors, count); }
741
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t> &xs, const std::vector<std::uint16_t> &ys, int radius, const std::vector<gea::framework::graphics::pixel::NativeColor> &colors, int count) { if (recordingPresentBatch()) appendPresentCircles(xs, ys, radius, colors, count); }
742
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::int32_t> &, const std::vector<std::int32_t> &, int, gea::framework::graphics::pixel::native_t) {}
743
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t> &xs, const std::vector<std::uint16_t> &ys, int radius, gea::framework::graphics::pixel::native_t color) { if (recordingPresentBatch()) appendPresentCirclesUniform(xs, ys, radius, color); }
744
+
745
+ void CanvasRenderingContext2D::beginPath() { pathX_.clear(); pathY_.clear(); pathStarts_.clear(); hasArc_ = false; pathClosed_ = false; }
746
+ void CanvasRenderingContext2D::arc(double x, double y, double radius, double, double) { arcX_ = x; arcY_ = y; arcRadius_ = radius; hasArc_ = true; }
747
+ void CanvasRenderingContext2D::moveTo(double x, double y) { pathStarts_.push_back(static_cast<int>(pathX_.size())); pathX_.push_back(x); pathY_.push_back(y); }
748
+ void CanvasRenderingContext2D::lineTo(double x, double y) { pathX_.push_back(x); pathY_.push_back(y); }
749
+ void CanvasRenderingContext2D::closePath() { pathClosed_ = true; }
750
+ void CanvasRenderingContext2D::fill() { if (hasArc_) fillCircle(arcX_, arcY_, arcRadius_); }
751
+ void CanvasRenderingContext2D::stroke() { if (hasArc_) strokeCircle(arcX_, arcY_, arcRadius_); }
752
+ void CanvasRenderingContext2D::fillText(const std::string &, int, int) {}
753
+ void CanvasRenderingContext2D::fillText(const std::string &text, double x, double y) { fillText(text, rounded(x), rounded(y)); }
754
+ // Images on the direct path record a present command like every other
755
+ // primitive here. These used to be stubs, so ctx.drawImage() silently drew
756
+ // NOTHING on a direct-canvas board -- the call was counted and then dropped,
757
+ // which reads as a black screen rather than as an error. The display layer has
758
+ // always had DrawImage/DrawImageScaled commands; only this side was missing.
759
+ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
760
+ {
761
+ gTotalDrawImage++;
762
+ auto &images = gea::framework::graphics::ImageStore::instance();
763
+ const int srcW = images.width(imageId);
764
+ const int srcH = images.height(imageId);
765
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
766
+ if (!pixels || srcW <= 0 || srcH <= 0) { gTotalDrawImageNullPixels++; return; }
767
+ if (!recordingPresentBatch()) return;
768
+ const std::int64_t started = nowUs();
769
+ gCanvasPerfStats.drawImageCalls++;
770
+ appendPresentDrawImage(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy);
771
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
772
+ }
773
+ void CanvasRenderingContext2D::drawImage(int imageId, double x, double y) { drawImage(imageId, rounded(x), rounded(y)); }
774
+ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, int dh)
775
+ {
776
+ gTotalDrawImage++;
777
+ auto &images = gea::framework::graphics::ImageStore::instance();
778
+ const int srcW = images.width(imageId);
779
+ const int srcH = images.height(imageId);
780
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
781
+ if (!pixels || srcW <= 0 || srcH <= 0) { gTotalDrawImageNullPixels++; return; }
782
+ if (!recordingPresentBatch()) return;
783
+ const std::int64_t started = nowUs();
784
+ gCanvasPerfStats.drawImageCalls++;
785
+ // A 1:1 destination takes the unscaled command, whose executor is a row
786
+ // memcpy (or a per-pixel alpha blend) instead of the resampler.
787
+ if (dw == srcW && dh == srcH) appendPresentDrawImage(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy);
788
+ else appendPresentDrawImageScaled(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh);
789
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
790
+ }
791
+ void CanvasRenderingContext2D::drawImage(int imageId, double x, double y, double w, double h) { drawImage(imageId, rounded(x), rounded(y), rounded(w), rounded(h)); }
792
+ double CanvasRenderingContext2D::measureTextInkCenter(const std::string &text)
793
+ {
794
+ auto *surface = drawingCanvas();
795
+ if (!surface) surface = canvas();
796
+ if (!surface) return 0.0;
797
+ return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
798
+ }
799
+
800
+ double CanvasRenderingContext2D::measureText(const std::string &text)
801
+ {
802
+ auto *surface = drawingCanvas();
803
+ if (!surface) surface = canvas();
804
+ if (!surface) return 0.0;
805
+ return static_cast<double>(surface->measureTextFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
806
+ }
807
+
808
+ void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int dw, int dh)
809
+ {
810
+ gTotalDrawImage++;
811
+ auto &images = gea::framework::graphics::ImageStore::instance();
812
+ const int srcW = images.width(imageId);
813
+ const int srcH = images.height(imageId);
814
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
815
+ if (!pixels || srcW <= 0 || srcH <= 0 || dw <= 0 || dh <= 0) { gTotalDrawImageNullPixels++; return; }
816
+ if (!recordingPresentBatch()) return;
817
+ gCanvasPerfStats.drawImageCalls++;
818
+ const int radius = (dw < dh ? dw : dh) / 2;
819
+ appendPresentDrawImageScaled(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh, radius);
820
+ }
821
+ void CanvasRenderingContext2D::drawImageCircle(int imageId, double x, double y, double w, double h) { drawImageCircle(imageId, rounded(x), rounded(y), rounded(w), rounded(h)); }
822
+ void CanvasRenderingContext2D::drawImageRotated90CW(int, int, int, int, int) {}
823
+ void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, double x, double y, double w, double h) { drawImageRotated90CW(imageId, rounded(x), rounded(y), rounded(w), rounded(h)); }
824
+ void CanvasRenderingContext2D::drawImageTiledX(int, int, int, int) {}
825
+ void CanvasRenderingContext2D::drawImageTiledX(int imageId, double x, double y, double w) { drawImageTiledX(imageId, rounded(x), rounded(y), rounded(w)); }
826
+ void CanvasRenderingContext2D::flush() { gea::platform::display::Display::flush(); }
827
+
828
+ void CanvasRenderingContext2D::beginBatch()
829
+ {
830
+ gTotalBeginBatch++;
831
+ gCanvasPerfStats.batchBeginCalls++;
832
+ batchDepth_++;
833
+ if (batchDepth_ != 1) return;
834
+ batchDirty_ = false;
835
+ resetPresentCommands();
836
+ presentRecording_ = true;
837
+ }
838
+
839
+ void CanvasRenderingContext2D::endBatch()
840
+ {
841
+ gTotalEndBatch++;
842
+ gCanvasPerfStats.batchEndCalls++;
843
+ if (batchDepth_ <= 0) return;
844
+ batchDepth_--;
845
+ if (batchDepth_ > 0) return;
846
+ if (batchDirty_) {
847
+ gCanvasPerfStats.batchFlushCalls++;
848
+ (void)presentBatch();
849
+ }
850
+ resetPresentCommands();
851
+ presentRecording_ = false;
852
+ batchDirty_ = false;
853
+ }
854
+
855
+ void CanvasRenderer::record(const Node &) {}
856
+
857
+ #else
858
+
859
+ CanvasElement CanvasElement::create()
860
+ {
861
+ return CanvasElement(Tree::instance().createCanvas());
862
+ }
863
+
864
+ CanvasRenderingContext2D CanvasElement::getContext2D() const
865
+ {
866
+ return CanvasRenderingContext2D(id_);
867
+ }
868
+
869
+ gea::framework::graphics::Canvas *CanvasRenderingContext2D::canvas()
870
+ {
871
+ const std::int64_t started = nowUs();
872
+ gCanvasPerfStats.canvasLookupCalls++;
873
+ gea::framework::graphics::Canvas *result = nullptr;
874
+ if (batchCanvas_) {
875
+ result = batchCanvas_;
876
+ } else if (nodeId_ >= 0) {
877
+ result = Tree::instance().ensureCanvas(nodeId_);
878
+ }
879
+ gCanvasPerfStats.canvasLookupUs += nowUs() - started;
880
+ return result;
881
+ }
882
+
883
+ gea::framework::graphics::Canvas *CanvasRenderingContext2D::drawingCanvas()
884
+ {
885
+ if (presentRecording_) {
886
+ auto *surface = canvas();
887
+ if (surface) {
888
+ replayPresentBatchToCanvas(*surface);
889
+ resetPresentCommands();
890
+ presentRecording_ = false;
891
+ batchCanvas_ = surface;
892
+ batchDirty_ = true;
893
+ }
894
+ return surface;
895
+ }
896
+ if (batchCanvas_) return batchCanvas_;
897
+ return canvas();
898
+ }
899
+
900
+ bool CanvasRenderingContext2D::recordingPresentBatch() const
901
+ {
902
+ return presentRecording_ && batchDepth_ > 0 && nodeId_ >= 0;
903
+ }
904
+
905
+ void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixel::native_t color)
906
+ {
907
+ resetPresentCommands();
908
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::Clear);
909
+ command.clearColor = color;
910
+ batchDirty_ = true;
911
+ }
912
+
913
+ void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
914
+ {
915
+ if (w <= 0 || h <= 0) return;
916
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillRectRgb565);
917
+ command.x = x;
918
+ command.y = y;
919
+ command.w = w;
920
+ command.h = h;
921
+ command.color = color;
922
+ command.alpha = globalAlpha_;
923
+ batchDirty_ = true;
924
+ }
925
+
926
+ void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
927
+ {
928
+ if (w <= 0 || h <= 0) return;
929
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::StrokeRectRgb565);
930
+ command.x = x;
931
+ command.y = y;
932
+ command.w = w;
933
+ command.h = h;
934
+ command.color = color;
935
+ command.alpha = globalAlpha_;
936
+ batchDirty_ = true;
937
+ }
938
+
939
+ void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
940
+ int y0,
941
+ int x1,
942
+ int y1,
943
+ int x2,
944
+ int y2,
945
+ gea::framework::graphics::pixel::native_t color)
946
+ {
947
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTriangleRgb565);
948
+ command.x0 = x0;
949
+ command.y0 = y0;
950
+ command.x1 = x1;
951
+ command.y1 = y1;
952
+ command.x2 = x2;
953
+ command.y2 = y2;
954
+ command.color = color;
955
+ command.alpha = globalAlpha_;
956
+ batchDirty_ = true;
957
+ }
958
+
959
+ void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
960
+ {
961
+ if (radius <= 0) return;
962
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCircleRgb565);
963
+ command.x = x;
964
+ command.y = y;
965
+ command.radius = radius;
966
+ command.color = color;
967
+ command.alpha = globalAlpha_;
968
+ batchDirty_ = true;
969
+ }
970
+
971
+ void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
972
+ {
973
+ if (radius <= 0) return;
974
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::StrokeCircleRgb565);
975
+ command.x = x;
976
+ command.y = y;
977
+ command.radius = radius;
978
+ command.color = color;
979
+ command.alpha = globalAlpha_;
980
+ batchDirty_ = true;
981
+ }
982
+
983
+ // Pointer + count forms (standard, non-direct-canvas variant). No temporary
984
+ // vector, and only the live `count` elements are copied into the recorded
985
+ // command, whose capacity is reused across frames.
986
+ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
987
+ const std::uint16_t *ys,
988
+ int radius,
989
+ const gea::framework::graphics::pixel::native_t *colors,
990
+ int count)
991
+ {
992
+ if (radius <= 0 || count <= 0 || !xs || !ys || !colors) return;
993
+ const std::size_t limit = static_cast<std::size_t>(count);
994
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
995
+ command.radius = radius;
996
+ command.alpha = globalAlpha_;
997
+ command.xs.assign(xs, xs + limit);
998
+ command.ys.assign(ys, ys + limit);
999
+ command.colors.assign(colors, colors + limit);
1000
+ batchDirty_ = true;
1001
+ }
1002
+
1003
+ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
1004
+ const std::uint16_t *ys,
1005
+ int radius,
1006
+ const gea::framework::graphics::pixel::NativeColor *colors,
1007
+ int count)
1008
+ {
1009
+ if (radius <= 0 || count <= 0 || !xs || !ys || !colors) return;
1010
+ const std::size_t limit = static_cast<std::size_t>(count);
1011
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
1012
+ command.radius = radius;
1013
+ command.alpha = globalAlpha_;
1014
+ command.xs.assign(xs, xs + limit);
1015
+ command.ys.assign(ys, ys + limit);
1016
+ command.colors.resize(limit);
1017
+ for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
1018
+ batchDirty_ = true;
1019
+ }
1020
+
1021
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::uint16_t *xs,
1022
+ const std::uint16_t *ys,
1023
+ int radius,
1024
+ const gea::framework::graphics::pixel::native_t *colors,
1025
+ int count)
1026
+ {
1027
+ if (recordingPresentBatch()) {
1028
+ gCanvasPerfStats.fillCircleCalls += count > 0 ? count : 0;
1029
+ appendPresentCircles(xs, ys, radius, colors, count);
1030
+ return;
1031
+ }
1032
+ auto *surface = drawingCanvas();
1033
+ if (!surface || radius <= 0 || count <= 0) return;
1034
+ gCanvasPerfStats.fillCircleCalls += count;
1035
+ applyDrawState(*surface);
1036
+ surface->fillCirclesRgb565(xs, ys, count, radius, colors);
1037
+ markDrawDirty();
1038
+ }
1039
+
1040
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::uint16_t *xs,
1041
+ const std::uint16_t *ys,
1042
+ int radius,
1043
+ const gea::framework::graphics::pixel::NativeColor *colors,
1044
+ int count)
1045
+ {
1046
+ if (recordingPresentBatch()) {
1047
+ gCanvasPerfStats.fillCircleCalls += count > 0 ? count : 0;
1048
+ appendPresentCircles(xs, ys, radius, colors, count);
1049
+ return;
1050
+ }
1051
+ if (count <= 0 || !colors) return;
1052
+ std::vector<gea::framework::graphics::pixel::native_t> native(static_cast<std::size_t>(count));
1053
+ for (int i = 0; i < count; ++i) native[static_cast<std::size_t>(i)] = colors[i].value;
1054
+ fillCirclesRgb565(xs, ys, radius, native.data(), count);
1055
+ }
1056
+
1057
+
1058
+ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
1059
+ const std::vector<std::uint16_t> &ys,
1060
+ int radius,
1061
+ const std::vector<gea::framework::graphics::pixel::native_t> &colors,
1062
+ int count)
1063
+ {
1064
+ if (radius <= 0) return;
1065
+ std::size_t limit = xs.size() < ys.size() ? xs.size() : ys.size();
1066
+ if (colors.size() < limit) limit = colors.size();
1067
+ if (count >= 0 && static_cast<std::size_t>(count) < limit) limit = static_cast<std::size_t>(count);
1068
+ const std::size_t count_ = limit;
1069
+ if (count_ == 0) return;
1070
+
1071
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
1072
+ command.radius = radius;
1073
+ command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(count_));
1074
+ command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(count_));
1075
+ command.colors.assign(colors.begin(), colors.begin() + static_cast<std::ptrdiff_t>(count_));
1076
+ command.alpha = globalAlpha_;
1077
+ batchDirty_ = true;
1078
+ }
1079
+
1080
+ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
1081
+ const std::vector<std::uint16_t> &ys,
1082
+ int radius,
1083
+ const std::vector<gea::framework::graphics::pixel::NativeColor> &colors,
1084
+ int count)
1085
+ {
1086
+ if (radius <= 0) return;
1087
+ std::size_t limit = xs.size() < ys.size() ? xs.size() : ys.size();
1088
+ if (colors.size() < limit) limit = colors.size();
1089
+ if (count >= 0 && static_cast<std::size_t>(count) < limit) limit = static_cast<std::size_t>(count);
1090
+ if (limit == 0) return;
1091
+
1092
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
1093
+ command.radius = radius;
1094
+ command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(limit));
1095
+ command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
1096
+ command.colors.resize(limit);
1097
+ for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
1098
+ command.alpha = globalAlpha_;
1099
+ batchDirty_ = true;
1100
+ }
1101
+
1102
+ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std::uint16_t> &xs,
1103
+ const std::vector<std::uint16_t> &ys,
1104
+ int radius,
1105
+ gea::framework::graphics::pixel::native_t color)
1106
+ {
1107
+ if (radius <= 0) return;
1108
+ const std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1109
+ if (count == 0) return;
1110
+
1111
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
1112
+ command.radius = radius;
1113
+ command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(count));
1114
+ command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(count));
1115
+ command.colors.assign(count, color);
1116
+ command.alpha = globalAlpha_;
1117
+ batchDirty_ = true;
1118
+ }
1119
+
1120
+ void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
1121
+ const std::vector<std::int32_t> &y0s,
1122
+ const std::vector<std::int32_t> &x1s,
1123
+ const std::vector<std::int32_t> &y1s,
1124
+ const std::vector<std::int32_t> &x2s,
1125
+ const std::vector<std::int32_t> &y2s,
1126
+ const std::vector<std::uint32_t> &colors,
1127
+ const std::vector<std::int32_t> &order,
1128
+ int count)
1129
+ {
1130
+ std::size_t limit = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size(), order.size()});
1131
+ if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
1132
+ if (limit == 0) return;
1133
+ using gea::framework::graphics::TriangleEntry;
1134
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
1135
+ command.alpha = globalAlpha_;
1136
+ command.triangles.resize(limit);
1137
+ const std::int32_t *px0 = x0s.data();
1138
+ const std::int32_t *py0 = y0s.data();
1139
+ const std::int32_t *px1 = x1s.data();
1140
+ const std::int32_t *py1 = y1s.data();
1141
+ const std::int32_t *px2 = x2s.data();
1142
+ const std::int32_t *py2 = y2s.data();
1143
+ const std::uint32_t *pcolors = colors.data();
1144
+ const std::int32_t *porder = order.data();
1145
+ TriangleEntry *dst = command.triangles.data();
1146
+ const std::size_t sourceSize = x0s.size();
1147
+ for (std::size_t i = 0; i < limit; i++) {
1148
+ const std::int32_t rawIndex = porder[i];
1149
+ const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
1150
+ ? static_cast<std::size_t>(rawIndex)
1151
+ : 0;
1152
+ TriangleEntry &t = dst[i];
1153
+ t.x0 = static_cast<std::int16_t>(px0[o]);
1154
+ t.y0 = static_cast<std::int16_t>(py0[o]);
1155
+ t.x1 = static_cast<std::int16_t>(px1[o]);
1156
+ t.y1 = static_cast<std::int16_t>(py1[o]);
1157
+ t.x2 = static_cast<std::int16_t>(px2[o]);
1158
+ t.y2 = static_cast<std::int16_t>(py2[o]);
1159
+ t.color = gea::framework::graphics::pixel::nativeFromRrggbbaa(pcolors[o]);
1160
+ const std::int16_t lo01 = t.y0 < t.y1 ? t.y0 : t.y1;
1161
+ const std::int16_t hi01 = t.y0 > t.y1 ? t.y0 : t.y1;
1162
+ t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
1163
+ t.rowY1 = hi01 > t.y2 ? hi01 : t.y2;
1164
+ }
1165
+ batchDirty_ = true;
1166
+ }
1167
+
1168
+ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::graphics::pixel::native_t *pixels,
1169
+ const std::uint8_t *alphaPixels,
1170
+ int srcWidth,
1171
+ int srcHeight,
1172
+ int x,
1173
+ int y)
1174
+ {
1175
+ if (!pixels || srcWidth <= 0 || srcHeight <= 0) return;
1176
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImage);
1177
+ command.pixels = pixels;
1178
+ command.alphaPixels = alphaPixels;
1179
+ command.srcWidth = srcWidth;
1180
+ command.srcHeight = srcHeight;
1181
+ command.x = x;
1182
+ command.y = y;
1183
+ command.alpha = globalAlpha_;
1184
+ batchDirty_ = true;
1185
+ }
1186
+
1187
+ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework::graphics::pixel::native_t *pixels,
1188
+ const std::uint8_t *alphaPixels,
1189
+ int srcWidth,
1190
+ int srcHeight,
1191
+ int x,
1192
+ int y,
1193
+ int w,
1194
+ int h,
1195
+ int radius)
1196
+ {
1197
+ if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
1198
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageScaled);
1199
+ command.pixels = pixels;
1200
+ command.alphaPixels = alphaPixels;
1201
+ command.srcWidth = srcWidth;
1202
+ command.srcHeight = srcHeight;
1203
+ command.x = x;
1204
+ command.y = y;
1205
+ command.w = w;
1206
+ command.h = h;
1207
+ command.radius = radius;
1208
+ command.alpha = globalAlpha_;
1209
+ batchDirty_ = true;
1210
+ }
1211
+
1212
+ void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::framework::graphics::pixel::native_t *pixels,
1213
+ const std::uint8_t *alphaPixels,
1214
+ int srcWidth,
1215
+ int srcHeight,
1216
+ int x,
1217
+ int y,
1218
+ int w,
1219
+ int h)
1220
+ {
1221
+ if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
1222
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageRotated90CW);
1223
+ command.pixels = pixels;
1224
+ command.alphaPixels = alphaPixels;
1225
+ command.srcWidth = srcWidth;
1226
+ command.srcHeight = srcHeight;
1227
+ command.x = x;
1228
+ command.y = y;
1229
+ command.w = w;
1230
+ command.h = h;
1231
+ command.alpha = globalAlpha_;
1232
+ batchDirty_ = true;
1233
+ }
1234
+
1235
+ void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework::graphics::pixel::native_t *pixels,
1236
+ const std::uint8_t *alphaPixels,
1237
+ int srcWidth,
1238
+ int srcHeight,
1239
+ int x,
1240
+ int y,
1241
+ int w)
1242
+ {
1243
+ if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0) return;
1244
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageTiledX);
1245
+ command.pixels = pixels;
1246
+ command.alphaPixels = alphaPixels;
1247
+ command.srcWidth = srcWidth;
1248
+ command.srcHeight = srcHeight;
1249
+ command.x = x;
1250
+ command.y = y;
1251
+ command.w = w;
1252
+ command.alpha = globalAlpha_;
1253
+ batchDirty_ = true;
1254
+ }
1255
+
1256
+ void CanvasRenderingContext2D::appendPresentFillText(const std::string &text, int x, int y, gea::framework::graphics::pixel::native_t color)
1257
+ {
1258
+ if (text.empty()) return;
1259
+ CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillText);
1260
+ command.text = text;
1261
+ command.x = x;
1262
+ command.y = y;
1263
+ command.color = color;
1264
+ command.scale = fontScale_;
1265
+ command.fontFamilyId = fontFamilyId_;
1266
+ command.fontSizePx = fontSizePx_;
1267
+ command.alpha = globalAlpha_;
1268
+ batchDirty_ = true;
1269
+ }
1270
+
1271
+ void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphics::Canvas &surface)
1272
+ {
1273
+ for (std::size_t commandIndex = 0; commandIndex < presentCommandCount_; ++commandIndex) {
1274
+ const CanvasPresentCommand &command = presentCommands_[commandIndex];
1275
+ surface.setGlobalAlpha(command.alpha);
1276
+ switch (command.type) {
1277
+ case CanvasPresentCommandType::Clear:
1278
+ surface.clear(command.clearColor);
1279
+ break;
1280
+ case CanvasPresentCommandType::FillRectRgb565:
1281
+ surface.fillRect(command.x, command.y, command.w, command.h, command.color);
1282
+ break;
1283
+ case CanvasPresentCommandType::StrokeRectRgb565:
1284
+ surface.strokeRect(command.x, command.y, command.w, command.h, command.color);
1285
+ break;
1286
+ case CanvasPresentCommandType::FillTriangleRgb565:
1287
+ surface.fillTriangle(command.x0, command.y0, command.x1, command.y1, command.x2, command.y2, command.color);
1288
+ break;
1289
+ case CanvasPresentCommandType::FillCircleRgb565:
1290
+ surface.fillCircle(command.x, command.y, command.radius, command.color);
1291
+ break;
1292
+ case CanvasPresentCommandType::StrokeCircleRgb565:
1293
+ surface.strokeCircle(command.x, command.y, command.radius, command.color);
1294
+ break;
1295
+ case CanvasPresentCommandType::FillCirclesRgb565:
1296
+ surface.fillCirclesRgb565(command.xs.data(),
1297
+ command.ys.data(),
1298
+ static_cast<int>(command.xs.size()),
1299
+ command.radius,
1300
+ command.colors.data());
1301
+ break;
1302
+ case CanvasPresentCommandType::FillTrianglesRgb565:
1303
+ // Entries are depth-ordered; draw in order.
1304
+ for (const auto &t : command.triangles) {
1305
+ surface.fillTriangle(t.x0, t.y0, t.x1, t.y1, t.x2, t.y2, t.color);
1306
+ }
1307
+ break;
1308
+ case CanvasPresentCommandType::DrawImage:
1309
+ surface.drawImage(command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight, command.x, command.y);
1310
+ break;
1311
+ case CanvasPresentCommandType::DrawImageScaled:
1312
+ // Honour the corner radius, exactly like the chunk-raster path
1313
+ // (display_present.h rasterCommandRows). Without this the replay
1314
+ // fallback drew every image as a FULL SQUARE, so a JPEG icon's
1315
+ // opaque black corners chipped into its neighbours. This path is
1316
+ // taken whenever presentBatch is rejected -- e.g. every frame a
1317
+ // popup appears/disappears -- which is exactly when the app showed
1318
+ // the square-corner artifact.
1319
+ if (command.radius > 0)
1320
+ surface.drawImageRounded(command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1321
+ command.x, command.y, command.w, command.h, command.radius, command.radius,
1322
+ command.radius, command.radius);
1323
+ else
1324
+ surface.drawImage(command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1325
+ command.x, command.y, command.w, command.h);
1326
+ break;
1327
+ case CanvasPresentCommandType::DrawImageRotated90CW:
1328
+ surface.drawImageRotated90CW(command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1329
+ command.x, command.y, command.w, command.h);
1330
+ break;
1331
+ case CanvasPresentCommandType::DrawImageTiledX:
1332
+ surface.drawImageTiledX(command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1333
+ command.x, command.y, command.w);
1334
+ break;
1335
+ case CanvasPresentCommandType::FillText:
1336
+ if (command.fontFamilyId >= 0)
1337
+ surface.drawTextFontFamily(command.text.c_str(), command.x, command.y, command.color, command.fontFamilyId, command.fontSizePx);
1338
+ else
1339
+ surface.drawText(command.text.c_str(), command.x, command.y, command.color, command.scale);
1340
+ break;
1341
+ }
1342
+ }
1343
+ surface.setGlobalAlpha(globalAlpha_);
1344
+ }
1345
+
1346
+ bool CanvasRenderingContext2D::presentBatch()
1347
+ {
1348
+ if (presentCommandCount_ == 0) return false;
1349
+ gTotalPresentBatchOk++;
1350
+
1351
+ std::vector<gea::platform::display::DisplayPresentCommand> commands;
1352
+ commands.reserve(presentCommandCount_);
1353
+ for (std::size_t commandIndex = 0; commandIndex < presentCommandCount_; ++commandIndex) {
1354
+ const CanvasPresentCommand &command = presentCommands_[commandIndex];
1355
+ gea::platform::display::DisplayPresentCommand displayCommand{};
1356
+ switch (command.type) {
1357
+ case CanvasPresentCommandType::Clear:
1358
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::Clear;
1359
+ displayCommand.clear.color = command.clearColor;
1360
+ break;
1361
+ case CanvasPresentCommandType::FillRectRgb565:
1362
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillRectRgb565;
1363
+ displayCommand.fillRectRgb565 = {command.x, command.y, command.w, command.h, command.color, command.alpha};
1364
+ break;
1365
+ case CanvasPresentCommandType::StrokeRectRgb565:
1366
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeRectRgb565;
1367
+ displayCommand.strokeRectRgb565 = {command.x, command.y, command.w, command.h, command.color, command.alpha};
1368
+ break;
1369
+ case CanvasPresentCommandType::FillTriangleRgb565:
1370
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillTriangleRgb565;
1371
+ displayCommand.fillTriangleRgb565 = {
1372
+ command.x0, command.y0, command.x1, command.y1, command.x2, command.y2, command.color, command.alpha};
1373
+ break;
1374
+ case CanvasPresentCommandType::FillCircleRgb565:
1375
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCircleRgb565;
1376
+ displayCommand.fillCircleRgb565 = {command.x, command.y, command.radius, command.color, command.alpha};
1377
+ break;
1378
+ case CanvasPresentCommandType::StrokeCircleRgb565:
1379
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeCircleRgb565;
1380
+ displayCommand.strokeCircleRgb565 = {command.x, command.y, command.radius, command.color, command.alpha};
1381
+ break;
1382
+ case CanvasPresentCommandType::FillCirclesRgb565:
1383
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCirclesRgb565;
1384
+ displayCommand.fillCirclesRgb565.xs = command.xs.data();
1385
+ displayCommand.fillCirclesRgb565.ys = command.ys.data();
1386
+ displayCommand.fillCirclesRgb565.colors = command.colors.data();
1387
+ displayCommand.fillCirclesRgb565.count = static_cast<int>(command.xs.size());
1388
+ displayCommand.fillCirclesRgb565.radius = command.radius;
1389
+ displayCommand.fillCirclesRgb565.alpha = command.alpha;
1390
+ break;
1391
+ case CanvasPresentCommandType::FillTrianglesRgb565:
1392
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillTrianglesRgb565;
1393
+ displayCommand.fillTrianglesRgb565.entries = command.triangles.data();
1394
+ displayCommand.fillTrianglesRgb565.count = static_cast<int>(command.triangles.size());
1395
+ displayCommand.fillTrianglesRgb565.alpha = command.alpha;
1396
+ break;
1397
+ case CanvasPresentCommandType::DrawImage:
1398
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImage;
1399
+ displayCommand.drawImage = {
1400
+ command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight, command.x, command.y, command.alpha};
1401
+ break;
1402
+ case CanvasPresentCommandType::DrawImageScaled:
1403
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageScaled;
1404
+ displayCommand.drawImageScaled = {
1405
+ command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1406
+ command.x, command.y, command.w, command.h, command.alpha, command.radius};
1407
+ break;
1408
+ case CanvasPresentCommandType::DrawImageRotated90CW:
1409
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageRotated90CW;
1410
+ displayCommand.drawImageRotated90CW = {
1411
+ command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1412
+ command.x, command.y, command.w, command.h, command.alpha};
1413
+ break;
1414
+ case CanvasPresentCommandType::DrawImageTiledX:
1415
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageTiledX;
1416
+ displayCommand.drawImageTiledX = {
1417
+ command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight, command.x, command.y, command.w, command.alpha};
1418
+ break;
1419
+ case CanvasPresentCommandType::FillText:
1420
+ displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillText;
1421
+ displayCommand.fillText = {command.text.c_str(), command.x, command.y, command.color, command.scale,
1422
+ command.fontFamilyId, command.fontSizePx, command.alpha};
1423
+ break;
1424
+ }
1425
+ commands.push_back(displayCommand);
1426
+ }
1427
+ return gea::platform::display::Display::present(commands.data(), static_cast<int>(commands.size()));
1428
+ }
1429
+
1430
+ void CanvasRenderingContext2D::applyDrawState(gea::framework::graphics::Canvas &surface) const
1431
+ {
1432
+ surface.setGlobalAlpha(globalAlpha_);
1433
+ }
1434
+
1435
+ void CanvasRenderingContext2D::markDirty()
1436
+ {
1437
+ const std::int64_t started = nowUs();
1438
+ gCanvasPerfStats.markDirtyCalls++;
1439
+ if (batchDepth_ > 0) {
1440
+ batchDirty_ = true;
1441
+ gCanvasPerfStats.markDirtyUs += nowUs() - started;
1442
+ return;
1443
+ }
1444
+ if (nodeId_ >= 0) Tree::instance().markCanvasDirty(nodeId_);
1445
+ gCanvasPerfStats.markDirtyUs += nowUs() - started;
1446
+ }
1447
+
1448
+ void CanvasRenderingContext2D::markDrawDirty()
1449
+ {
1450
+ if (batchDepth_ > 0) {
1451
+ batchDirty_ = true;
1452
+ return;
1453
+ }
1454
+ markDirty();
1455
+ }
1456
+
1457
+ void CanvasRenderingContext2D::setFillStyle(const std::string &value)
1458
+ {
1459
+ if (fillStyleCached_ && value == fillStyleSource_) return;
1460
+ fillStyle_ = parseCanvasColor(value);
1461
+ fillStyleSource_ = value;
1462
+ fillStyleCached_ = true;
1463
+ }
1464
+
1465
+ void CanvasRenderingContext2D::setFillStyleRgb565(gea::framework::graphics::pixel::native_t color)
1466
+ {
1467
+ fillStyle_ = canvasRgb565(color);
1468
+ fillStyleCached_ = false;
1469
+ }
1470
+
1471
+ void CanvasRenderingContext2D::setStrokeStyle(const std::string &value)
1472
+ {
1473
+ if (strokeStyleCached_ && value == strokeStyleSource_) return;
1474
+ strokeStyle_ = parseCanvasColor(value);
1475
+ strokeStyleSource_ = value;
1476
+ strokeStyleCached_ = true;
1477
+ }
1478
+
1479
+ void CanvasRenderingContext2D::setStrokeStyleRgb565(gea::framework::graphics::pixel::native_t color)
1480
+ {
1481
+ strokeStyle_ = canvasRgb565(color);
1482
+ strokeStyleCached_ = false;
1483
+ }
1484
+
1485
+ void CanvasRenderingContext2D::setGlobalAlpha(double alpha)
1486
+ {
1487
+ globalAlpha_ = alphaFromUnit(alpha);
1488
+ }
1489
+
1490
+ void CanvasRenderingContext2D::setLineWidth(double width)
1491
+ {
1492
+ if (!std::isfinite(width) || width <= 0.0) return;
1493
+ lineWidth_ = width;
1494
+ }
1495
+
1496
+ void CanvasRenderingContext2D::setFont(const std::string &font)
1497
+ {
1498
+ // Re-setting the same font (common: a per-frame FPS/HUD readout) is a no-op —
1499
+ // skip the CSS parse + FontRegistry family/atlas lookups entirely.
1500
+ if (font == lastFontStr_) return;
1501
+ lastFontStr_ = font;
1502
+ fontScale_ = fontScaleFromCss(font);
1503
+ const int px = fontPxFromCss(font);
1504
+ if (px > 0) fontSizePx_ = px;
1505
+ const std::string family = fontFamilyFromCss(font);
1506
+ fontFamilyId_ = family.empty() ? -1 : gea::framework::graphics::FontRegistry::familyId(family.c_str());
1507
+ // Only commit to the baked path when an atlas actually exists near this
1508
+ // size; otherwise stay on the scaled bitmap font.
1509
+ if (fontFamilyId_ >= 0 &&
1510
+ !gea::framework::graphics::FontRegistry::rasterizedFamily(fontFamilyId_, fontSizePx_).valid()) {
1511
+ fontFamilyId_ = -1;
1512
+ }
1513
+ }
1514
+
1515
+ void CanvasRenderingContext2D::clear()
1516
+ {
1517
+ if (recordingPresentBatch()) {
1518
+ appendPresentClear(0);
1519
+ return;
1520
+ }
1521
+ auto *surface = drawingCanvas();
1522
+ if (!surface) return;
1523
+ clearRect(0, 0, surface->width(), surface->height());
1524
+ }
1525
+
1526
+ void CanvasRenderingContext2D::clearRect(int x, int y, int w, int h)
1527
+ {
1528
+ if (recordingPresentBatch()) {
1529
+ const std::uint8_t previousAlpha = globalAlpha_;
1530
+ globalAlpha_ = 255;
1531
+ appendPresentFillRect(x, y, w, h, 0);
1532
+ globalAlpha_ = previousAlpha;
1533
+ return;
1534
+ }
1535
+ auto *surface = drawingCanvas();
1536
+ if (!surface) return;
1537
+ const std::int64_t started = nowUs();
1538
+ gCanvasPerfStats.clearRectCalls++;
1539
+ surface->clearRect(x, y, w, h);
1540
+ gCanvasPerfStats.clearRectUs += nowUs() - started;
1541
+ markDrawDirty();
1542
+ }
1543
+
1544
+ void CanvasRenderingContext2D::clearRect(double x, double y, double w, double h)
1545
+ {
1546
+ clearRect(rounded(x), rounded(y), rounded(w), rounded(h));
1547
+ }
1548
+
1549
+ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
1550
+ {
1551
+ gTotalFillRect++;
1552
+ if (recordingPresentBatch()) {
1553
+ const std::int64_t started = nowUs();
1554
+ gCanvasPerfStats.fillRectCalls++;
1555
+ appendPresentFillRect(x, y, w, h, fillStyle_);
1556
+ gCanvasPerfStats.fillRectUs += nowUs() - started;
1557
+ return;
1558
+ }
1559
+ auto *surface = drawingCanvas();
1560
+ if (!surface) return;
1561
+ const std::int64_t started = nowUs();
1562
+ gCanvasPerfStats.fillRectCalls++;
1563
+ applyDrawState(*surface);
1564
+ surface->fillRect(x, y, w, h, fillStyle_);
1565
+ gCanvasPerfStats.fillRectUs += nowUs() - started;
1566
+ markDrawDirty();
1567
+ }
1568
+
1569
+ void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h)
1570
+ {
1571
+ fillRect(rounded(x), rounded(y), rounded(w), rounded(h));
1572
+ }
1573
+
1574
+ void CanvasRenderingContext2D::strokeRect(int x, int y, int w, int h)
1575
+ {
1576
+ if (recordingPresentBatch()) {
1577
+ appendPresentStrokeRect(x, y, w, h, strokeStyle_);
1578
+ return;
1579
+ }
1580
+ auto *surface = drawingCanvas();
1581
+ if (!surface) return;
1582
+ applyDrawState(*surface);
1583
+ surface->strokeRect(x, y, w, h, strokeStyle_);
1584
+ markDrawDirty();
1585
+ }
1586
+
1587
+ void CanvasRenderingContext2D::strokeRect(double x, double y, double w, double h)
1588
+ {
1589
+ strokeRect(rounded(x), rounded(y), rounded(w), rounded(h));
1590
+ }
1591
+
1592
+ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius)
1593
+ {
1594
+ if (recordingPresentBatch()) {
1595
+ const std::int64_t started = nowUs();
1596
+ gCanvasPerfStats.fillCircleCalls++;
1597
+ appendPresentFillCircle(x, y, radius, fillStyle_);
1598
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1599
+ return;
1600
+ }
1601
+ auto *surface = drawingCanvas();
1602
+ if (!surface) return;
1603
+ const std::int64_t started = nowUs();
1604
+ gCanvasPerfStats.fillCircleCalls++;
1605
+ applyDrawState(*surface);
1606
+ surface->fillCircle(x, y, radius, fillStyle_);
1607
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1608
+ markDrawDirty();
1609
+ }
1610
+
1611
+ void CanvasRenderingContext2D::fillCircle(double x, double y, double radius)
1612
+ {
1613
+ fillCircle(rounded(x), rounded(y), rounded(radius));
1614
+ }
1615
+
1616
+ void CanvasRenderingContext2D::strokeCircle(int x, int y, int radius)
1617
+ {
1618
+ if (recordingPresentBatch()) {
1619
+ appendPresentStrokeCircle(x, y, radius, strokeStyle_);
1620
+ return;
1621
+ }
1622
+ auto *surface = drawingCanvas();
1623
+ if (!surface) return;
1624
+ applyDrawState(*surface);
1625
+ surface->strokeCircle(x, y, radius, strokeStyle_);
1626
+ markDrawDirty();
1627
+ }
1628
+
1629
+ void CanvasRenderingContext2D::strokeCircle(double x, double y, double radius)
1630
+ {
1631
+ strokeCircle(rounded(x), rounded(y), rounded(radius));
1632
+ }
1633
+
1634
+ void CanvasRenderingContext2D::fillCircleRgb565(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
1635
+ {
1636
+ if (recordingPresentBatch()) {
1637
+ const std::int64_t started = nowUs();
1638
+ gCanvasPerfStats.fillCircleCalls++;
1639
+ appendPresentFillCircle(x, y, radius, canvasRgb565(color));
1640
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1641
+ return;
1642
+ }
1643
+ auto *surface = drawingCanvas();
1644
+ if (!surface) return;
1645
+ const std::int64_t started = nowUs();
1646
+ gCanvasPerfStats.fillCircleCalls++;
1647
+ applyDrawState(*surface);
1648
+ surface->fillCircle(x, y, radius, canvasRgb565(color));
1649
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1650
+ markDrawDirty();
1651
+ }
1652
+
1653
+ void CanvasRenderingContext2D::fillCircleRgb565(double x, double y, double radius, gea::framework::graphics::pixel::native_t color)
1654
+ {
1655
+ fillCircleRgb565(rounded(x), rounded(y), rounded(radius), color);
1656
+ }
1657
+
1658
+ void CanvasRenderingContext2D::fillTriangleRgb565(int x0,
1659
+ int y0,
1660
+ int x1,
1661
+ int y1,
1662
+ int x2,
1663
+ int y2,
1664
+ gea::framework::graphics::pixel::native_t color)
1665
+ {
1666
+ if (recordingPresentBatch()) {
1667
+ const std::int64_t started = nowUs();
1668
+ gCanvasPerfStats.fillTriangleCalls++;
1669
+ appendPresentFillTriangle(x0, y0, x1, y1, x2, y2, canvasRgb565(color));
1670
+ gCanvasPerfStats.fillTriangleUs += nowUs() - started;
1671
+ return;
1672
+ }
1673
+ auto *surface = drawingCanvas();
1674
+ if (!surface) return;
1675
+ const std::int64_t started = nowUs();
1676
+ gCanvasPerfStats.fillTriangleCalls++;
1677
+ applyDrawState(*surface);
1678
+ surface->fillTriangle(x0, y0, x1, y1, x2, y2, canvasRgb565(color));
1679
+ gCanvasPerfStats.fillTriangleUs += nowUs() - started;
1680
+ markDrawDirty();
1681
+ }
1682
+
1683
+ void CanvasRenderingContext2D::fillTriangleRgb565(double x0,
1684
+ double y0,
1685
+ double x1,
1686
+ double y1,
1687
+ double x2,
1688
+ double y2,
1689
+ gea::framework::graphics::pixel::native_t color)
1690
+ {
1691
+ fillTriangleRgb565(rounded(x0), rounded(y0), rounded(x1), rounded(y1), rounded(x2), rounded(y2), color);
1692
+ }
1693
+
1694
+ void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::int32_t> &x0s,
1695
+ const std::vector<std::int32_t> &y0s,
1696
+ const std::vector<std::int32_t> &x1s,
1697
+ const std::vector<std::int32_t> &y1s,
1698
+ const std::vector<std::int32_t> &x2s,
1699
+ const std::vector<std::int32_t> &y2s,
1700
+ const std::vector<std::uint32_t> &colors,
1701
+ const std::vector<std::int32_t> &order,
1702
+ int count)
1703
+ {
1704
+ std::size_t limit = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size(), order.size()});
1705
+ if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
1706
+ if (limit == 0) return;
1707
+ if (recordingPresentBatch()) {
1708
+ const std::int64_t started = nowUs();
1709
+ gCanvasPerfStats.fillTriangleCalls += static_cast<int>(limit);
1710
+ appendPresentTriangles(x0s, y0s, x1s, y1s, x2s, y2s, colors, order, count);
1711
+ gCanvasPerfStats.fillTriangleUs += nowUs() - started;
1712
+ return;
1713
+ }
1714
+ auto *surface = drawingCanvas();
1715
+ if (!surface) return;
1716
+ const std::int64_t started = nowUs();
1717
+ gCanvasPerfStats.fillTriangleCalls += static_cast<int>(limit);
1718
+ applyDrawState(*surface);
1719
+ const std::size_t sourceSize = x0s.size();
1720
+ for (std::size_t i = 0; i < limit; i++) {
1721
+ const std::int32_t rawIndex = order[i];
1722
+ const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
1723
+ ? static_cast<std::size_t>(rawIndex)
1724
+ : 0;
1725
+ surface->fillTriangle(x0s[o], y0s[o], x1s[o], y1s[o], x2s[o], y2s[o],
1726
+ gea::framework::graphics::pixel::nativeFromRrggbbaa(colors[o]));
1727
+ }
1728
+ gCanvasPerfStats.fillTriangleUs += nowUs() - started;
1729
+ markDrawDirty();
1730
+ }
1731
+
1732
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::int32_t> &xs,
1733
+ const std::vector<std::int32_t> &ys,
1734
+ int radius,
1735
+ const std::vector<gea::framework::graphics::pixel::native_t> &colors)
1736
+ {
1737
+ auto *surface = drawingCanvas();
1738
+ if (!surface || radius <= 0) return;
1739
+ const std::int64_t started = nowUs();
1740
+ std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1741
+ if (colors.size() < count) count = colors.size();
1742
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1743
+ applyDrawState(*surface);
1744
+ for (std::size_t i = 0; i < count; i++) {
1745
+ surface->fillCircle(xs[i], ys[i], radius, canvasRgb565(colors[i]));
1746
+ }
1747
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1748
+ markDrawDirty();
1749
+ }
1750
+
1751
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t> &xs,
1752
+ const std::vector<std::uint16_t> &ys,
1753
+ int radius,
1754
+ const std::vector<gea::framework::graphics::pixel::native_t> &colors,
1755
+ int countArg)
1756
+ {
1757
+ if (recordingPresentBatch()) {
1758
+ const std::int64_t started = nowUs();
1759
+ std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1760
+ if (colors.size() < count) count = colors.size();
1761
+ if (countArg >= 0 && static_cast<std::size_t>(countArg) < count) count = static_cast<std::size_t>(countArg);
1762
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1763
+ appendPresentCircles(xs, ys, radius, colors, static_cast<int>(count));
1764
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1765
+ return;
1766
+ }
1767
+ auto *surface = drawingCanvas();
1768
+ if (!surface || radius <= 0) return;
1769
+ std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1770
+ if (colors.size() < count) count = colors.size();
1771
+ if (countArg >= 0 && static_cast<std::size_t>(countArg) < count) count = static_cast<std::size_t>(countArg);
1772
+ const std::int64_t started = nowUs();
1773
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1774
+ applyDrawState(*surface);
1775
+ surface->fillCirclesRgb565(xs.data(), ys.data(), static_cast<int>(count), radius, colors.data());
1776
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1777
+ markDrawDirty();
1778
+ }
1779
+
1780
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t> &xs,
1781
+ const std::vector<std::uint16_t> &ys,
1782
+ int radius,
1783
+ const std::vector<gea::framework::graphics::pixel::NativeColor> &colors,
1784
+ int countArg)
1785
+ {
1786
+ if (recordingPresentBatch()) {
1787
+ const std::int64_t started = nowUs();
1788
+ std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1789
+ if (colors.size() < count) count = colors.size();
1790
+ if (countArg >= 0 && static_cast<std::size_t>(countArg) < count) count = static_cast<std::size_t>(countArg);
1791
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1792
+ appendPresentCircles(xs, ys, radius, colors, static_cast<int>(count));
1793
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1794
+ return;
1795
+ }
1796
+ auto *surface = drawingCanvas();
1797
+ if (!surface || radius <= 0) return;
1798
+ std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1799
+ if (colors.size() < count) count = colors.size();
1800
+ if (countArg >= 0 && static_cast<std::size_t>(countArg) < count) count = static_cast<std::size_t>(countArg);
1801
+ const std::int64_t started = nowUs();
1802
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1803
+ std::vector<gea::framework::graphics::pixel::native_t> nativeColors;
1804
+ nativeColors.reserve(count);
1805
+ for (std::size_t i = 0; i < count; ++i) nativeColors.push_back(colors[i].value);
1806
+ applyDrawState(*surface);
1807
+ surface->fillCirclesRgb565(xs.data(), ys.data(), static_cast<int>(count), radius, nativeColors.data());
1808
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1809
+ markDrawDirty();
1810
+ }
1811
+
1812
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::int32_t> &xs,
1813
+ const std::vector<std::int32_t> &ys,
1814
+ int radius,
1815
+ gea::framework::graphics::pixel::native_t color)
1816
+ {
1817
+ auto *surface = drawingCanvas();
1818
+ if (!surface || radius <= 0) return;
1819
+ const std::int64_t started = nowUs();
1820
+ const std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1821
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1822
+ applyDrawState(*surface);
1823
+ for (std::size_t i = 0; i < count; i++) {
1824
+ surface->fillCircle(xs[i], ys[i], radius, canvasRgb565(color));
1825
+ }
1826
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1827
+ markDrawDirty();
1828
+ }
1829
+
1830
+ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t> &xs,
1831
+ const std::vector<std::uint16_t> &ys,
1832
+ int radius,
1833
+ gea::framework::graphics::pixel::native_t color)
1834
+ {
1835
+ if (recordingPresentBatch()) {
1836
+ const std::int64_t started = nowUs();
1837
+ const std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1838
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1839
+ appendPresentCirclesUniform(xs, ys, radius, canvasRgb565(color));
1840
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1841
+ return;
1842
+ }
1843
+ auto *surface = drawingCanvas();
1844
+ if (!surface || radius <= 0) return;
1845
+ const std::size_t count = xs.size() < ys.size() ? xs.size() : ys.size();
1846
+ const std::int64_t started = nowUs();
1847
+ gCanvasPerfStats.fillCircleCalls += static_cast<int>(count);
1848
+ applyDrawState(*surface);
1849
+ surface->fillCirclesRgb565Uniform(xs.data(), ys.data(), static_cast<int>(count), radius, canvasRgb565(color));
1850
+ gCanvasPerfStats.fillCircleUs += nowUs() - started;
1851
+ markDrawDirty();
1852
+ }
1853
+
1854
+ void CanvasRenderingContext2D::beginPath()
1855
+ {
1856
+ hasArc_ = false;
1857
+ pathX_.clear();
1858
+ pathY_.clear();
1859
+ pathStarts_.clear();
1860
+ pathClosed_ = false;
1861
+ }
1862
+
1863
+ void CanvasRenderingContext2D::arc(double x, double y, double radius, double, double)
1864
+ {
1865
+ arcX_ = x;
1866
+ arcY_ = y;
1867
+ arcRadius_ = radius;
1868
+ hasArc_ = true;
1869
+ }
1870
+
1871
+ void CanvasRenderingContext2D::moveTo(double x, double y)
1872
+ {
1873
+ // Standard canvas semantics: moveTo starts a NEW subpath (it does not clear
1874
+ // the path — beginPath does). This lets one path hold many rings.
1875
+ pathStarts_.push_back(static_cast<int>(pathX_.size()));
1876
+ pathX_.push_back(x);
1877
+ pathY_.push_back(y);
1878
+ pathClosed_ = false;
1879
+ }
1880
+
1881
+ void CanvasRenderingContext2D::lineTo(double x, double y)
1882
+ {
1883
+ if (pathX_.empty()) moveTo(x, y);
1884
+ else {
1885
+ pathX_.push_back(x);
1886
+ pathY_.push_back(y);
1887
+ }
1888
+ }
1889
+
1890
+ void CanvasRenderingContext2D::closePath()
1891
+ {
1892
+ pathClosed_ = true;
1893
+ }
1894
+
1895
+ void CanvasRenderingContext2D::fill()
1896
+ {
1897
+ // Batch-aware (mirrors fillRect): when recording a present batch the spans
1898
+ // are appended as present commands so they actually reach the display; only
1899
+ // outside a batch do we draw straight to the surface. (The app composites
1900
+ // the map inside beginBatch/endBatch, so direct draws would never present.)
1901
+ const bool rec = recordingPresentBatch();
1902
+ auto *surface = rec ? canvas() : drawingCanvas();
1903
+ if (!surface) return;
1904
+ if (!rec) applyDrawState(*surface);
1905
+ if (hasArc_) {
1906
+ const int cx = rounded(arcX_);
1907
+ const int cy = rounded(arcY_);
1908
+ const int r = rounded(arcRadius_);
1909
+ if (rec) appendPresentFillCircle(cx, cy, r, fillStyle_);
1910
+ else {
1911
+ surface->fillCircle(cx, cy, r, fillStyle_);
1912
+ markDrawDirty();
1913
+ }
1914
+ return;
1915
+ }
1916
+ const std::size_t n = pathX_.size();
1917
+ if (n < 3 || pathStarts_.empty()) return;
1918
+
1919
+ // Even-odd scanline fill over all subpaths (each implicitly closed). Unlike
1920
+ // a triangle fan (convex only), this fills arbitrary concave shapes AND
1921
+ // holes — coastlines, lakes-with-islands, building courtyards. Inside spans
1922
+ // emit as 1px fillRects.
1923
+ double minYf = pathY_[0];
1924
+ double maxYf = pathY_[0];
1925
+ for (std::size_t i = 1; i < n; i++) {
1926
+ if (pathY_[i] < minYf) minYf = pathY_[i];
1927
+ if (pathY_[i] > maxYf) maxYf = pathY_[i];
1928
+ }
1929
+ const int yTop = std::max(0, static_cast<int>(std::floor(minYf)));
1930
+ const int yBot = std::min(surface->height() - 1, static_cast<int>(std::ceil(maxYf)));
1931
+ const std::size_t nStarts = pathStarts_.size();
1932
+ static std::vector<double> nodes;
1933
+ for (int y = yTop; y <= yBot; y++) {
1934
+ const double yc = y + 0.5;
1935
+ nodes.clear();
1936
+ for (std::size_t s = 0; s < nStarts; s++) {
1937
+ const std::size_t a = static_cast<std::size_t>(pathStarts_[s]);
1938
+ const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(pathStarts_[s + 1]) : n;
1939
+ if (b - a < 2) continue;
1940
+ for (std::size_t i = a; i < b; i++) {
1941
+ const std::size_t j = (i + 1 < b) ? i + 1 : a; // close the ring
1942
+ const double yi = pathY_[i];
1943
+ const double yj = pathY_[j];
1944
+ if ((yi < yc && yj >= yc) || (yj < yc && yi >= yc)) {
1945
+ const double t = (yc - yi) / (yj - yi);
1946
+ nodes.push_back(pathX_[i] + t * (pathX_[j] - pathX_[i]));
1947
+ }
1948
+ }
1949
+ }
1950
+ if (nodes.size() < 2) continue;
1951
+ std::sort(nodes.begin(), nodes.end());
1952
+ for (std::size_t k = 0; k + 1 < nodes.size(); k += 2) {
1953
+ const int xa = static_cast<int>(std::lround(nodes[k]));
1954
+ const int xb = static_cast<int>(std::lround(nodes[k + 1]));
1955
+ if (xb > xa) {
1956
+ if (rec) appendPresentFillRect(xa, y, xb - xa, 1, fillStyle_);
1957
+ else surface->fillRect(xa, y, xb - xa, 1, fillStyle_);
1958
+ }
1959
+ }
1960
+ }
1961
+ if (!rec) markDrawDirty();
1962
+ }
1963
+
1964
+ void CanvasRenderingContext2D::stroke()
1965
+ {
1966
+ const bool rec = recordingPresentBatch();
1967
+ auto *surface = rec ? canvas() : drawingCanvas();
1968
+ if (!surface) return;
1969
+ if (!rec) applyDrawState(*surface);
1970
+ if (hasArc_) {
1971
+ const int cx = rounded(arcX_);
1972
+ const int cy = rounded(arcY_);
1973
+ const int r = rounded(arcRadius_);
1974
+ if (rec) appendPresentStrokeCircle(cx, cy, r, strokeStyle_);
1975
+ else {
1976
+ surface->strokeCircle(cx, cy, r, strokeStyle_);
1977
+ markDrawDirty();
1978
+ }
1979
+ return;
1980
+ }
1981
+ const std::size_t n = pathX_.size();
1982
+ if (n < 2 || pathStarts_.empty()) return;
1983
+ // Honor lineWidth: every segment is an oriented quad (two triangles), with a
1984
+ // round disc at each joint/cap for widths ≥ ~2px — roads, casings, rivers.
1985
+ // Always quad (not Bresenham) so it has a present-command representation.
1986
+ const double half = std::max(0.5, lineWidth_ * 0.5);
1987
+ const std::size_t nStarts = pathStarts_.size();
1988
+ for (std::size_t s = 0; s < nStarts; s++) {
1989
+ const std::size_t a = static_cast<std::size_t>(pathStarts_[s]);
1990
+ const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(pathStarts_[s + 1]) : n;
1991
+ if (b - a < 2) continue;
1992
+ const std::size_t lastSeg = pathClosed_ ? b : b - 1;
1993
+ for (std::size_t i = a; i < lastSeg; i++) {
1994
+ const std::size_t j = (i + 1 < b) ? i + 1 : a; // wrap when closed
1995
+ const double dx = pathX_[j] - pathX_[i];
1996
+ const double dy = pathY_[j] - pathY_[i];
1997
+ const double len = std::sqrt(dx * dx + dy * dy);
1998
+ if (len < 1e-6) continue;
1999
+ const double px = -dy / len * half; // perpendicular offset
2000
+ const double py = dx / len * half;
2001
+ const int ax0 = rounded(pathX_[i] + px), ay0 = rounded(pathY_[i] + py);
2002
+ const int ax1 = rounded(pathX_[i] - px), ay1 = rounded(pathY_[i] - py);
2003
+ const int bx0 = rounded(pathX_[j] + px), by0 = rounded(pathY_[j] + py);
2004
+ const int bx1 = rounded(pathX_[j] - px), by1 = rounded(pathY_[j] - py);
2005
+ if (rec) {
2006
+ appendPresentFillTriangle(ax0, ay0, ax1, ay1, bx1, by1, strokeStyle_);
2007
+ appendPresentFillTriangle(ax0, ay0, bx1, by1, bx0, by0, strokeStyle_);
2008
+ } else {
2009
+ surface->fillTriangle(ax0, ay0, ax1, ay1, bx1, by1, strokeStyle_);
2010
+ surface->fillTriangle(ax0, ay0, bx1, by1, bx0, by0, strokeStyle_);
2011
+ }
2012
+ }
2013
+ if (half >= 1.0) {
2014
+ const int r = rounded(half);
2015
+ for (std::size_t i = a; i < b; i++) {
2016
+ const int cx = rounded(pathX_[i]);
2017
+ const int cy = rounded(pathY_[i]);
2018
+ if (rec) appendPresentFillCircle(cx, cy, r, strokeStyle_);
2019
+ else surface->fillCircle(cx, cy, r, strokeStyle_);
2020
+ }
2021
+ }
2022
+ }
2023
+ if (!rec) markDrawDirty();
2024
+ }
2025
+
2026
+ void CanvasRenderingContext2D::fillText(const std::string &text, int x, int y)
2027
+ {
2028
+ if (recordingPresentBatch()) {
2029
+ const std::int64_t started = nowUs();
2030
+ gCanvasPerfStats.fillTextCalls++;
2031
+ appendPresentFillText(text, x, y, fillStyle_);
2032
+ gCanvasPerfStats.fillTextUs += nowUs() - started;
2033
+ return;
2034
+ }
2035
+ auto *surface = drawingCanvas();
2036
+ if (!surface || text.empty()) return;
2037
+ const std::int64_t started = nowUs();
2038
+ gCanvasPerfStats.fillTextCalls++;
2039
+ applyDrawState(*surface);
2040
+ if (fontFamilyId_ >= 0) surface->drawTextFontFamily(text.c_str(), x, y, fillStyle_, fontFamilyId_, fontSizePx_);
2041
+ else surface->drawText(text.c_str(), x, y, fillStyle_, fontScale_);
2042
+ gCanvasPerfStats.fillTextUs += nowUs() - started;
2043
+ markDrawDirty();
2044
+ }
2045
+
2046
+ void CanvasRenderingContext2D::fillText(const std::string &text, double x, double y)
2047
+ {
2048
+ fillText(text, rounded(x), rounded(y));
2049
+ }
2050
+
2051
+ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
2052
+ {
2053
+ auto &images = gea::framework::graphics::ImageStore::instance();
2054
+ const int srcW = images.width(imageId);
2055
+ const int srcH = images.height(imageId);
2056
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
2057
+ if (!pixels || srcW <= 0 || srcH <= 0) return;
2058
+ const std::int64_t started = nowUs();
2059
+ gCanvasPerfStats.drawImageCalls++;
2060
+ if (recordingPresentBatch()) {
2061
+ appendPresentDrawImage(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy);
2062
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2063
+ return;
2064
+ }
2065
+ auto *surface = drawingCanvas();
2066
+ if (!surface) return;
2067
+ applyDrawState(*surface);
2068
+ surface->drawImage(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy);
2069
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2070
+ markDrawDirty();
2071
+ }
2072
+
2073
+ void CanvasRenderingContext2D::drawImage(int imageId, double dx, double dy)
2074
+ {
2075
+ drawImage(imageId, rounded(dx), rounded(dy));
2076
+ }
2077
+
2078
+ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, int dh)
2079
+ {
2080
+ gTotalDrawImage++;
2081
+ auto &images = gea::framework::graphics::ImageStore::instance();
2082
+ const int srcW = images.width(imageId);
2083
+ const int srcH = images.height(imageId);
2084
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
2085
+ if (!pixels || srcW <= 0 || srcH <= 0) { gTotalDrawImageNullPixels++; return; }
2086
+ const std::int64_t started = nowUs();
2087
+ gCanvasPerfStats.drawImageCalls++;
2088
+ if (recordingPresentBatch()) {
2089
+ appendPresentDrawImageScaled(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh);
2090
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2091
+ return;
2092
+ }
2093
+ auto *surface = drawingCanvas();
2094
+ if (!surface) return;
2095
+ applyDrawState(*surface);
2096
+ surface->drawImage(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh);
2097
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2098
+ markDrawDirty();
2099
+ }
2100
+
2101
+ void CanvasRenderingContext2D::drawImage(int imageId, double dx, double dy, double dw, double dh)
2102
+ {
2103
+ drawImage(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2104
+ }
2105
+
2106
+ double CanvasRenderingContext2D::measureTextInkCenter(const std::string &text)
2107
+ {
2108
+ auto *surface = drawingCanvas();
2109
+ if (!surface) surface = canvas();
2110
+ if (!surface) return 0.0;
2111
+ return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
2112
+ }
2113
+
2114
+ double CanvasRenderingContext2D::measureText(const std::string &text)
2115
+ {
2116
+ auto *surface = drawingCanvas();
2117
+ if (!surface) surface = canvas();
2118
+ if (!surface) return 0.0;
2119
+ return static_cast<double>(surface->measureTextFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
2120
+ }
2121
+
2122
+ void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int dw, int dh)
2123
+ {
2124
+ gTotalDrawImage++;
2125
+ auto &images = gea::framework::graphics::ImageStore::instance();
2126
+ const int srcW = images.width(imageId);
2127
+ const int srcH = images.height(imageId);
2128
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
2129
+ if (!pixels || srcW <= 0 || srcH <= 0 || dw <= 0 || dh <= 0) { gTotalDrawImageNullPixels++; return; }
2130
+ const std::int64_t started = nowUs();
2131
+ gCanvasPerfStats.drawImageCalls++;
2132
+ const int radius = (dw < dh ? dw : dh) / 2;
2133
+ if (recordingPresentBatch()) {
2134
+ appendPresentDrawImageScaled(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh, radius);
2135
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2136
+ return;
2137
+ }
2138
+ auto *surface = drawingCanvas();
2139
+ if (!surface) return;
2140
+ applyDrawState(*surface);
2141
+ surface->drawImageRounded(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh, radius, radius,
2142
+ radius, radius);
2143
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2144
+ markDrawDirty();
2145
+ }
2146
+
2147
+ void CanvasRenderingContext2D::drawImageCircle(int imageId, double dx, double dy, double dw, double dh)
2148
+ {
2149
+ drawImageCircle(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2150
+ }
2151
+
2152
+ void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, int dx, int dy, int dw, int dh)
2153
+ {
2154
+ gTotalDrawImage++;
2155
+ auto &images = gea::framework::graphics::ImageStore::instance();
2156
+ const int srcW = images.width(imageId);
2157
+ const int srcH = images.height(imageId);
2158
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
2159
+ if (!pixels || srcW <= 0 || srcH <= 0 || dw <= 0 || dh <= 0) { gTotalDrawImageNullPixels++; return; }
2160
+ const std::int64_t started = nowUs();
2161
+ gCanvasPerfStats.drawImageCalls++;
2162
+ if (recordingPresentBatch()) {
2163
+ appendPresentDrawImageRotated90CW(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh);
2164
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2165
+ return;
2166
+ }
2167
+ auto *surface = drawingCanvas();
2168
+ if (!surface) return;
2169
+ applyDrawState(*surface);
2170
+ surface->drawImageRotated90CW(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, dw, dh);
2171
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2172
+ markDrawDirty();
2173
+ }
2174
+
2175
+ void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, double dx, double dy, double dw, double dh)
2176
+ {
2177
+ drawImageRotated90CW(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2178
+ }
2179
+
2180
+ void CanvasRenderingContext2D::drawImageTiledX(int imageId, int dx, int dy, int width)
2181
+ {
2182
+ auto &images = gea::framework::graphics::ImageStore::instance();
2183
+ const int srcW = images.width(imageId);
2184
+ const int srcH = images.height(imageId);
2185
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(imageId);
2186
+ if (!pixels || srcW <= 0 || srcH <= 0 || width <= 0) return;
2187
+ const std::int64_t started = nowUs();
2188
+ gCanvasPerfStats.drawImageCalls++;
2189
+ if (recordingPresentBatch()) {
2190
+ appendPresentDrawImageTiledX(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, width);
2191
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2192
+ return;
2193
+ }
2194
+ auto *surface = drawingCanvas();
2195
+ if (!surface) return;
2196
+ applyDrawState(*surface);
2197
+ surface->drawImageTiledX(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy, width);
2198
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2199
+ markDrawDirty();
2200
+ }
2201
+
2202
+ void CanvasRenderingContext2D::drawImageTiledX(int imageId, double dx, double dy, double width)
2203
+ {
2204
+ drawImageTiledX(imageId, rounded(dx), rounded(dy), rounded(width));
2205
+ }
2206
+
2207
+ void CanvasRenderingContext2D::flush()
2208
+ {
2209
+ if (nodeId_ < 0) return;
2210
+ if (!Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2211
+ markDirty();
2212
+ return;
2213
+ }
2214
+ gea::platform::display::Display::flush();
2215
+ }
2216
+
2217
+ void CanvasRenderingContext2D::beginBatch()
2218
+ {
2219
+ gTotalBeginBatch++;
2220
+ gCanvasPerfStats.batchBeginCalls++;
2221
+ batchDepth_++;
2222
+ if (batchDepth_ != 1) return;
2223
+ batchDirty_ = false;
2224
+ resetPresentCommands();
2225
+ // Re-evaluate display-backed eligibility every frame without binding the
2226
+ // display canvas: command-recorded batches do not need a framebuffer until
2227
+ // they fall back to replay. This avoids expensive target-side preservation
2228
+ // on double-buffered panels.
2229
+ if (nodeId_ >= 0 && Tree::instance().markDisplayBackedCanvas(nodeId_)) {
2230
+ presentRecording_ = true;
2231
+ batchCanvas_ = nullptr;
2232
+ return;
2233
+ }
2234
+ if (nodeId_ >= 0) Tree::instance().ensureCanvas(nodeId_);
2235
+ if (nodeId_ >= 0 && Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2236
+ presentRecording_ = true;
2237
+ batchCanvas_ = nullptr;
2238
+ return;
2239
+ }
2240
+ presentRecording_ = false;
2241
+ batchCanvas_ = canvas();
2242
+ }
2243
+
2244
+ void CanvasRenderingContext2D::endBatch()
2245
+ {
2246
+ gTotalEndBatch++;
2247
+ gCanvasPerfStats.batchEndCalls++;
2248
+ if (batchDepth_ <= 0) return;
2249
+ batchDepth_--;
2250
+ if (batchDepth_ > 0) return;
2251
+ batchCanvas_ = nullptr;
2252
+ if (batchDirty_ && nodeId_ >= 0) {
2253
+ if (Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2254
+ const std::int64_t started = nowUs();
2255
+ gCanvasPerfStats.batchFlushCalls++;
2256
+ bool presented = false;
2257
+ if (presentRecording_) presented = presentBatch();
2258
+ if (presented) {
2259
+ Tree::instance().clearNodeDisplayCommandDirty(nodeId_);
2260
+ }
2261
+ if (!presented) {
2262
+ // Replay+flush paints the OFF-SCREEN composed buffer over the
2263
+ // scanout — when this interleaves with a double-buffered present
2264
+ // path the screen alternates fresh/stale frames (flicker).
2265
+ // Loud while diagnosing that exact symptom.
2266
+ static int replayFalls = 0;
2267
+ replayFalls++;
2268
+ if ((replayFalls & 7) == 1) std::printf("[canvas] presentBatch REJECTED -> replay+flush (#%d)\n", replayFalls);
2269
+ presentRecording_ = false;
2270
+ auto *surface = canvas();
2271
+ if (surface) replayPresentBatchToCanvas(*surface);
2272
+ gea::platform::display::Display::flush();
2273
+ }
2274
+ gCanvasPerfStats.batchFlushUs += nowUs() - started;
2275
+ } else {
2276
+ Tree::instance().markCanvasDirty(nodeId_);
2277
+ }
2278
+ }
2279
+ resetPresentCommands();
2280
+ presentRecording_ = false;
2281
+ batchDirty_ = false;
2282
+ }
2283
+
2284
+ void CanvasRenderer::record(const Node &node)
2285
+ {
2286
+ const Node *nodes = Tree::instance().nodes();
2287
+ const int id = static_cast<int>(&node - nodes);
2288
+ if (Tree::instance().isDisplayBackedCanvas(id)) return;
2289
+
2290
+ const auto *surface = Tree::instance().canvas(id);
2291
+ if (!surface || !surface->pixels() || surface->width() <= 0 || surface->height() <= 0) return;
2292
+
2293
+ DisplayCommand *cmd = DisplayList::instance().append();
2294
+ if (!cmd) return;
2295
+ cmd->type = DisplayCommandType::BlitImage;
2296
+ cmd->bx = node.layout.x;
2297
+ cmd->by = node.layout.y;
2298
+ cmd->bw = node.layout.width;
2299
+ cmd->bh = node.layout.height;
2300
+ cmd->blit.pixels = surface->pixels();
2301
+ cmd->blit.alpha = nullptr;
2302
+ cmd->blit.sourceWidth = surface->width();
2303
+ cmd->blit.sourceHeight = surface->height();
2304
+ cmd->blit.dx = node.layout.x;
2305
+ cmd->blit.dy = node.layout.y;
2306
+ // The canvas surface is stored in the framebuffer's native packing; on the
2307
+ // grayscale targets that is sub-byte packed (2 px/byte on GRAY4, 4 px/byte on
2308
+ // GRAY2), so flag it for the blit to unpack.
2309
+ cmd->blit.sourcePacked = GEA_PIXEL_STORAGE_PACKED ? 1 : 0;
2310
+ }
2311
+
2312
+ #endif
2313
+
2314
+ } // namespace gea::embedded::ui