@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,628 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "internal.h"
3
+ #include "display.h"
4
+ #include "host/display_orientation.h"
5
+ #include "mirror.h"
6
+ #include "node_lifecycle.h"
7
+ #include "tree_state.h"
8
+ #include "virtual_keyboard.h"
9
+
10
+ #include <cstdio>
11
+ #include <cstdlib>
12
+ #include <cstring>
13
+
14
+ namespace gea::embedded::ui {
15
+
16
+ namespace {
17
+
18
+ bool nodeCanDrawWithoutCanvas(const Node &node)
19
+ {
20
+ if (node.style.display == 1) return false;
21
+ if (node.type == NodeType::Canvas) return false;
22
+ // A <camera> leaf paints itself from the platform preview buffer (or a
23
+ // native overlay), never via the box renderer — treat it like a canvas.
24
+ if (node.type == NodeType::Camera) return false;
25
+ if (node.style.has_bg || hasAnyBorder(node.style)) return true;
26
+ if (node.type == NodeType::Text && !node.text.empty()) return true;
27
+ if (node.type == NodeType::Image && node.image_id >= 0) return true;
28
+ if (node.type == NodeType::VirtualList) return true;
29
+ if (isViewLikeNodeType(node.type) && node.style.overflow == 2) return true;
30
+ return false;
31
+ }
32
+
33
+ // Diagnostics for GEADEV: why/how often the display-backed gate fails.
34
+ int gDisplayBackedFailReason = 0;
35
+ int gDisplayBackedFailExtra = 0;
36
+ int gDisplayBackedFailCount = 0;
37
+ int gCanvasRebindCount = 0;
38
+ int gCanvasLastMode = -1;
39
+
40
+ bool canUseDisplayFramebuffer(TreeState &state, int id, int width, int height)
41
+ {
42
+ #ifdef __EMSCRIPTEN__
43
+ // Web/WASM: the display "panel" IS the framebuffer the document render paints
44
+ // into (the browser scans it out after every frame; there is no separate GRAM).
45
+ // A display-backed canvas presents during the rAF phase, but the per-frame
46
+ // document render (Application::frame -> Document::frame, which runs AFTER the
47
+ // rAF callbacks) repaints that same framebuffer from the retained display list,
48
+ // clobbering the canvas's fresh draws with stale content — the canvas freezes
49
+ // at its first frame and only a thin mis-located strip survives. On real panels
50
+ // present() pushes the canvas commands to the panel over its own transport, so
51
+ // it never collides with the framebuffer; on web the fast path's invariant
52
+ // ("nothing else repaints my region") is violated. Use the owned-surface +
53
+ // document-blit path on web, which composites the canvas's current pixels every
54
+ // frame and animates correctly. (Tree-less GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT
55
+ // builds don't come through here — with no document render there's no
56
+ // collision, and web Display::present() rasterizes their batches directly
57
+ // into the framebuffer.)
58
+ (void)state;
59
+ (void)id;
60
+ (void)width;
61
+ (void)height;
62
+ return false;
63
+ #else
64
+ if (state.mountedRoot < 0) return false;
65
+
66
+ static int dbgFalse = 0;
67
+ auto failWhy = [&](int reason, int extra) {
68
+ dbgFalse++;
69
+ gDisplayBackedFailReason = reason;
70
+ gDisplayBackedFailExtra = extra;
71
+ gDisplayBackedFailCount = dbgFalse;
72
+ return false;
73
+ };
74
+ const int displayWidth = gea::framework::display::detail::DisplayOrientationState::width();
75
+ const int displayHeight = gea::framework::display::detail::DisplayOrientationState::height();
76
+ if (displayWidth <= 0 || displayHeight <= 0) return failWhy(1, 0);
77
+ if (width != displayWidth || height != displayHeight) return failWhy(2, width);
78
+
79
+ Node &canvasNode = state.nodes[id];
80
+ if (canvasNode.layout.x != 0 || canvasNode.layout.y != 0) return failWhy(3, canvasNode.layout.x);
81
+ if (canvasNode.layout.width != displayWidth ||
82
+ canvasNode.layout.height != displayHeight)
83
+ return failWhy(4, canvasNode.layout.width);
84
+
85
+ int visibleCanvasCount = 0;
86
+ for (int i = 0; i < state.nodeCount; i++) {
87
+ Node &node = state.nodes[i];
88
+ if (node.style.display == 1) continue;
89
+ if (node.type == NodeType::Canvas) visibleCanvasCount++;
90
+ if (i != id && nodeCanDrawWithoutCanvas(node)) return failWhy(5, i);
91
+ }
92
+
93
+ if (visibleCanvasCount != 1) return failWhy(6, visibleCanvasCount);
94
+ return true;
95
+ #endif
96
+ }
97
+
98
+ void resetNodeSlot(TreeState &state, int id)
99
+ {
100
+ if (id < 0 || id >= kMaxNodes) return;
101
+ // The node's text buffer dies with the slot; retained display commands hold
102
+ // a raw pointer into it (see DisplayList::scrubNodeText) — neutralize them
103
+ // before NodeLifecycle::init frees the string, or a stale-list replay in the
104
+ // gap before the next record reads freed memory.
105
+ if (!state.nodes[id].text.empty()) DisplayList::instance().scrubNodeText(state.nodes[id].text.c_str());
106
+ releaseRareData(id); // frees the rare-data block: attributes, listeners, custom props, virtual-list
107
+ releaseRareStyle(state.nodes[id].style.rare_style); // frees the cold-style block
108
+ state.classLists[id].clear();
109
+ state.canvases.remove(id);
110
+ state.nodeCommandDirty[id] = 0;
111
+ state.nodeCommandDirtyBoundsValid[id] = 0;
112
+ state.nodeCommandDirtyCanOverpaint[id] = 0;
113
+ state.nodeBackdropCooldown[id] = 0;
114
+ state.nodeInBackdrop[id] = 0;
115
+ state.nodeActive[id] = 0;
116
+ NodeLifecycle::init(&state.nodes[id], NodeType::View);
117
+ state.nodes[id].style.display = kDisplayNone;
118
+ }
119
+
120
+ void trimInactiveTail(TreeState &state)
121
+ {
122
+ while (state.nodeCount > 0 && !state.nodeActive[state.nodeCount - 1]) {
123
+ state.nodeCount--;
124
+ }
125
+ }
126
+
127
+ bool localAbsoluteLeafTreeMutation(const TreeState &state, int child)
128
+ {
129
+ if (child < 0 || child >= state.nodeCount) return false;
130
+ const Node &node = state.nodes[child];
131
+ if (node.style.display == kDisplayNone) return false;
132
+ if (node.style.position != 1) return false;
133
+ if (node.style.width == kUnset || node.style.height == kUnset) return false;
134
+ if (node.first_child >= 0) return false;
135
+ if (node.type == NodeType::Text || node.type == NodeType::Canvas || node.type == NodeType::VirtualList) return false;
136
+ if (!isViewLikeNodeType(node.type) && node.type != NodeType::Image) return false;
137
+ if (rstyle(node.style).filter_blur_radius > 0 || node.style.mask_right_fade_width > 0) return false;
138
+ if (rstyle(node.style).box_shadow_alpha > 0) return false;
139
+ return true;
140
+ }
141
+
142
+ } // namespace
143
+
144
+ void Tree::clear()
145
+ {
146
+ auto &state = treeState();
147
+ state.refreshSerial++;
148
+ if (state.refreshSerial == 0) state.refreshSerial = 1;
149
+ for (int i = 0; i < state.nodeCount; i++) {
150
+ releaseRareData(i); // frees the rare-data block: attributes, listeners, custom props, styles, virtual-list
151
+ state.classLists[i].clear();
152
+ }
153
+ // Document-level handlers live off-tree; drop them so the next app doesn't
154
+ // inherit the previous app's dial/hardware-button/pointer handlers.
155
+ // Delegated document pointer/touch handlers also bind onto the mounted root;
156
+ // releaseRareData above drops those per-node listener slots, so the binding
157
+ // cache must be reset with them.
158
+ resetDocumentEventListeners();
159
+ resetDocumentDelegatedEventListeners();
160
+ resetRareStylePool(); // drop all cold-style entries; node handles reset by the Node{} loop below
161
+ state.canvases.clear();
162
+ std::memset(state.nodeActive, 0, sizeof(state.nodeActive));
163
+ // Reset EVERY slot of state.nodes[], not just [0, nodeCount). If the
164
+ // next app's tree has fewer nodes than the previous app's tree, the
165
+ // leftover slots still carry stale data (transform_rotate from a
166
+ // rotated clock hand, previous_x/y layout, blink_visible, etc.)
167
+ // that refresh / display-list / hit-test paths assume is a clean
168
+ // baseline. std::memset CAN'T be used here: Node contains a
169
+ // `std::string text` member, and zeroing its internal pointer leaks
170
+ // any heap-allocated buffer the previous app left behind AND leaves
171
+ // the string in a state where the next operation null-derefs.
172
+ // Per-slot assignment from a default-constructed Node invokes
173
+ // std::string's operator= which deallocates the LHS's heap if any
174
+ // and resets to an empty SSO state, while still zeroing all the
175
+ // POD fields. Slightly slower than memset but runs once per app
176
+ // switch and bounded at kMaxNodes (~512).
177
+ for (int i = 0; i < kMaxNodes; i++) state.nodes[i] = Node{};
178
+ state.nodeCount = 0;
179
+ state.mountedRoot = -1;
180
+ state.mountedWidth = 0;
181
+ state.mountedHeight = 0;
182
+ state.displayListDirty = true;
183
+ std::memset(state.nodeCommandDirty, 0, sizeof(state.nodeCommandDirty));
184
+ std::memset(state.nodeCommandDirtyBoundsValid, 0, sizeof(state.nodeCommandDirtyBoundsValid));
185
+ std::memset(state.nodeCommandDirtyCanOverpaint, 0, sizeof(state.nodeCommandDirtyCanOverpaint));
186
+ std::memset(state.scrollDirtyNodes, 0, sizeof(state.scrollDirtyNodes));
187
+ state.scrollDirtyAny = false;
188
+ state.pendingScrollIntoViewNode = -1;
189
+ // Drop display-list commands and growable render scratch from the previous app.
190
+ // Per-frame DisplayList::clear() keeps capacity for reuse; app/tree reset is the
191
+ // point where a large app should stop poisoning a smaller app's memory shape.
192
+ DisplayList::instance().resetStorage();
193
+ Mirror::instance().clearScrollDirty();
194
+ // Drop any active software-scroll-register state so the new app's
195
+ // drawing doesn't get rotated through the previous app's offset. On
196
+ // platforms without the register this is a no-op.
197
+ gea::platform::display::Display::resetScrollRegion();
198
+ // Drop touch / momentum / scroll-tracker state too. Without this the
199
+ // next app's first animation-frame tick can drive a fling against a
200
+ // stale node id from the previous tree — which scrolls (or marks
201
+ // dirty) nodes in the new app that have no business being touched,
202
+ // erasing whatever the synchronous init paint just drew.
203
+ Tree::instance().resetInput();
204
+ // Same for the on-screen keyboard — it holds node ids it mounted as
205
+ // children of the previous app's root, and those slots are about to
206
+ // be reused. Dropping our handles forces re-mount on the next focus
207
+ // event in the new app.
208
+ VirtualKeyboard::instance().reset();
209
+ }
210
+
211
+ int Tree::createNode(NodeType type)
212
+ {
213
+ auto &state = treeState();
214
+ int id = -1;
215
+ for (int i = 0; i < state.nodeCount; i++) {
216
+ if (state.nodeActive[i]) continue;
217
+ id = i;
218
+ break;
219
+ }
220
+ if (id < 0) {
221
+ if (state.nodeCount >= kMaxNodes) return -1;
222
+ id = state.nodeCount++;
223
+ }
224
+ state.nodeActive[id] = 1;
225
+ releaseRareData(id); // free any block from a prior occupant before reinit
226
+ NodeLifecycle::init(&state.nodes[id], type);
227
+ state.classLists[id].clear();
228
+ if (type == NodeType::VirtualList) VirtualListRenderer::init(id);
229
+ return id;
230
+ }
231
+
232
+ int Tree::cloneNode(int sourceId, bool deep)
233
+ {
234
+ auto &state = treeState();
235
+ if (sourceId < 0 || sourceId >= state.nodeCount || !state.nodeActive[sourceId]) return -1;
236
+ const int id = createNode(state.nodes[sourceId].type);
237
+ if (id < 0) return -1;
238
+ // nodes[] is a fixed array, so taking the source reference after createNode
239
+ // is safe; an ACTIVE source slot can never be the one createNode reused.
240
+ const Node &source = state.nodes[sourceId];
241
+ Node &clone = state.nodes[id];
242
+ clone.style = source.style;
243
+ // clone.style copied source's rare_style HANDLE (shared pool entry). Give the
244
+ // clone its own entry with the same contents, so mutating one never aliases.
245
+ clone.style.rare_style = -1;
246
+ if (source.style.rare_style >= 0) rstyleMut(clone.style) = rstyle(source.style);
247
+ clone.text = source.text;
248
+ clone.image_id = source.image_id;
249
+ clone.tag_id = source.tag_id; // interned tag id — just copy the handle
250
+ // Copy attributes + custom properties (DOM cloneNode semantics) but NOT
251
+ // listeners or virtual-list state. All live in the rare-data block; allocate
252
+ // one for the clone only if the source has any. (rareDataFor returns a deque
253
+ // element whose address is stable across the ensureRareData growth below, so
254
+ // srcRd stays valid.)
255
+ if (const NodeRareData *srcRd = rareDataFor(sourceId)) {
256
+ NodeRareData &cloneRd = ensureRareData(id);
257
+ cloneRd.attributes = srcRd->attributes;
258
+ cloneRd.customProperties = srcRd->customProperties;
259
+ cloneRd.defaultStyles = srcRd->defaultStyles;
260
+ cloneRd.inlineStyles = srcRd->inlineStyles;
261
+ }
262
+ state.classLists[id] = state.classLists[sourceId];
263
+ // Listeners deliberately NOT copied (DOM cloneNode semantics) — compiled
264
+ // templates attach handlers to the clone after cloning.
265
+ if (deep) {
266
+ for (int child = source.first_child; child >= 0; child = state.nodes[child].next_sibling) {
267
+ const int childClone = cloneNode(child, true);
268
+ if (childClone >= 0) setParent(childClone, id);
269
+ }
270
+ }
271
+ return id;
272
+ }
273
+
274
+ int Tree::firstChildOf(int node)
275
+ {
276
+ auto &state = treeState();
277
+ if (node < 0 || node >= state.nodeCount || !state.nodeActive[node]) return -1;
278
+ return state.nodes[node].first_child;
279
+ }
280
+
281
+ int Tree::childAt(int node, int index)
282
+ {
283
+ auto &state = treeState();
284
+ if (node < 0 || node >= state.nodeCount || !state.nodeActive[node] || index < 0) return -1;
285
+ int child = state.nodes[node].first_child;
286
+ while (child >= 0 && index > 0) {
287
+ child = state.nodes[child].next_sibling;
288
+ index--;
289
+ }
290
+ return child;
291
+ }
292
+
293
+ int Tree::createView() { return createNode(NodeType::View); }
294
+ int Tree::createButton() { return createNode(NodeType::Button); }
295
+ int Tree::createText() { return createNode(NodeType::Text); }
296
+ int Tree::createImage() { return createNode(NodeType::Image); }
297
+ int Tree::createCanvas() { return createNode(NodeType::Canvas); }
298
+ int Tree::createCamera() { return createNode(NodeType::Camera); }
299
+ int Tree::createAudio()
300
+ {
301
+ const int id = createNode(NodeType::Audio);
302
+ if (id >= 0) treeState().nodes[id].style.display = kDisplayNone;
303
+ return id;
304
+ }
305
+ int Tree::createVirtualList() { return createNode(NodeType::VirtualList); }
306
+
307
+ gea::framework::graphics::Canvas *Tree::ensureCanvas(int id, int width, int height)
308
+ {
309
+ auto &state = treeState();
310
+ if (id < 0 || id >= state.nodeCount) return nullptr;
311
+ Node &node = state.nodes[id];
312
+ if (node.type != NodeType::Canvas) return nullptr;
313
+
314
+ if (width <= 0) width = node.layout.width;
315
+ if (height <= 0) height = node.layout.height;
316
+ if (width <= 0) width = std::atoi(getAttribute(id, "width"));
317
+ if (height <= 0) height = std::atoi(getAttribute(id, "height"));
318
+ if (width <= 0 || height <= 0) return nullptr;
319
+
320
+ auto *slot = state.canvases.ensure(id);
321
+ if (!slot) return nullptr;
322
+ const auto previousMode = slot->mode;
323
+ const auto *previousCanvas = slot->canvas();
324
+ const int previousWidth = slot->width;
325
+ const int previousHeight = slot->height;
326
+
327
+ gea::framework::graphics::Canvas *canvas = nullptr;
328
+ if (canUseDisplayFramebuffer(state, id, width, height)) {
329
+ auto *displayCanvas = gea::platform::display::Display::canvas();
330
+ canvas = displayCanvas ? state.canvases.bindDisplayFramebuffer(*slot, *displayCanvas) : nullptr;
331
+ } else {
332
+ canvas = state.canvases.bindOwned(*slot, width, height);
333
+ }
334
+ gCanvasLastMode = static_cast<int>(slot->mode);
335
+
336
+ if (canvas &&
337
+ (previousMode != slot->mode ||
338
+ previousCanvas != slot->canvas() ||
339
+ previousWidth != slot->width ||
340
+ previousHeight != slot->height)) {
341
+ gCanvasRebindCount++;
342
+ markNodeDisplayCommandsDirty(id);
343
+ }
344
+
345
+ return canvas;
346
+ }
347
+
348
+ bool Tree::canUseDisplayBackedCanvas(int id, int width, int height)
349
+ {
350
+ auto &state = treeState();
351
+ if (id < 0 || id >= state.nodeCount) return false;
352
+ Node &node = state.nodes[id];
353
+ if (node.type != NodeType::Canvas) return false;
354
+ if (width <= 0) width = node.layout.width;
355
+ if (height <= 0) height = node.layout.height;
356
+ if (width <= 0) width = std::atoi(getAttribute(id, "width"));
357
+ if (height <= 0) height = std::atoi(getAttribute(id, "height"));
358
+ if (width <= 0 || height <= 0) return false;
359
+ return canUseDisplayFramebuffer(state, id, width, height);
360
+ }
361
+
362
+ bool Tree::markDisplayBackedCanvas(int id, int width, int height)
363
+ {
364
+ auto &state = treeState();
365
+ if (id < 0 || id >= state.nodeCount) return false;
366
+ Node &node = state.nodes[id];
367
+ if (node.type != NodeType::Canvas) return false;
368
+ if (width <= 0) width = node.layout.width;
369
+ if (height <= 0) height = node.layout.height;
370
+ if (width <= 0) width = std::atoi(getAttribute(id, "width"));
371
+ if (height <= 0) height = std::atoi(getAttribute(id, "height"));
372
+ if (width <= 0 || height <= 0) return false;
373
+ if (!canUseDisplayFramebuffer(state, id, width, height)) return false;
374
+
375
+ auto *slot = state.canvases.ensure(id);
376
+ if (!slot) return false;
377
+ const auto previousMode = slot->mode;
378
+ const auto *previousCanvas = slot->canvas();
379
+ const int previousWidth = slot->width;
380
+ const int previousHeight = slot->height;
381
+
382
+ state.canvases.markDisplayFramebuffer(*slot, width, height);
383
+ gCanvasLastMode = static_cast<int>(slot->mode);
384
+ if (previousMode != slot->mode ||
385
+ previousCanvas != slot->canvas() ||
386
+ previousWidth != slot->width ||
387
+ previousHeight != slot->height) {
388
+ gCanvasRebindCount++;
389
+ markNodeDisplayCommandsDirty(id);
390
+ }
391
+ return true;
392
+ }
393
+
394
+ gea::framework::graphics::Canvas *Tree::ensureCameraSurface(int id, int width, int height)
395
+ {
396
+ auto &state = treeState();
397
+ if (id < 0 || id >= state.nodeCount) return nullptr;
398
+ Node &node = state.nodes[id];
399
+ if (node.type != NodeType::Camera) return nullptr;
400
+
401
+ if (width <= 0) width = node.layout.width;
402
+ if (height <= 0) height = node.layout.height;
403
+ if (width <= 0) width = std::atoi(getAttribute(id, "width"));
404
+ if (height <= 0) height = std::atoi(getAttribute(id, "height"));
405
+ if (width <= 0 || height <= 0) return nullptr;
406
+
407
+ auto *slot = state.canvases.ensure(id);
408
+ if (!slot) return nullptr;
409
+ const auto *previousCanvas = slot->canvas();
410
+ const int previousWidth = slot->width;
411
+ const int previousHeight = slot->height;
412
+
413
+ // Camera frames are always streamed into an owned buffer (no full-screen
414
+ // display-framebuffer fast path: the backend fills these pixels, not the
415
+ // app, and the buffer is sized to the node's CSS rect).
416
+ gea::framework::graphics::Canvas *canvas = state.canvases.bindOwned(*slot, width, height);
417
+
418
+ if (canvas &&
419
+ (previousCanvas != slot->canvas() || previousWidth != slot->width || previousHeight != slot->height)) {
420
+ markNodeDisplayCommandsDirty(id);
421
+ }
422
+ return canvas;
423
+ }
424
+
425
+ const gea::framework::graphics::Canvas *Tree::canvas(int id) const
426
+ {
427
+ const auto &state = treeState();
428
+ if (id < 0 || id >= state.nodeCount) return nullptr;
429
+ const auto *slot = state.canvases.find(id);
430
+ return slot && slot->canvas() && slot->canvas()->pixels() ? slot->canvas() : nullptr;
431
+ }
432
+
433
+ void Tree::displayBackedFailDebug(int *reason, int *extra, int *count) const
434
+ {
435
+ *reason = gDisplayBackedFailReason;
436
+ *extra = gDisplayBackedFailExtra;
437
+ *count = gDisplayBackedFailCount;
438
+ }
439
+
440
+ void Tree::canvasSlotDebug(int id, int *mode, int *rebinds) const
441
+ {
442
+ const auto &state = treeState();
443
+ const auto *slot = state.canvases.find(id);
444
+ *mode = slot ? static_cast<int>(slot->mode) : -1;
445
+ *rebinds = gCanvasRebindCount;
446
+ }
447
+
448
+ bool Tree::isDisplayBackedCanvas(int id) const
449
+ {
450
+ const auto &state = treeState();
451
+ if (id < 0 || id >= state.nodeCount) return false;
452
+ const auto *slot = state.canvases.find(id);
453
+ return slot && slot->displayBacked();
454
+ }
455
+
456
+ void Tree::markCanvasDirty(int id)
457
+ {
458
+ auto &state = treeState();
459
+ if (id < 0 || id >= state.nodeCount) return;
460
+ const auto *surface = state.canvases.find(id);
461
+ if (surface && surface->displayBacked()) return;
462
+ state.nodes[id].render.dirty = 1;
463
+ state.nodes[id].render.layout_dirty = 1;
464
+ state.nodes[id].render.non_scroll_dirty = 1;
465
+ // Content-only repaint: the canvas's BlitImage command is unchanged frame
466
+ // to frame (same surface pointer, same rect) — only the pixels it points
467
+ // at were redrawn. The in-place refresh path re-records and emits a flush
468
+ // rect only for command-dirty nodes; with just render.dirty it sees an
469
+ // identical command, produces no dirty rect, and the fresh draws never
470
+ // reach the display (same trap as the per-frame <camera> preview repaint
471
+ // in tree_render.cpp, which uses the content-dirty mark for this reason).
472
+ markNodeDisplayCommandsDirty(id);
473
+ }
474
+
475
+ // A reparent that puts `parent` under `child` closes a cycle in the parent chain.
476
+ // Every consumer walks that chain unbounded -- style's pending-root coverage test,
477
+ // the display list's dynamic-subtree test, layout's scope search -- so a single
478
+ // cycle wedges the frame task until the watchdog reboots the board. Refuse the
479
+ // move and name both ends: the caller that asked for it is the actual defect.
480
+ bool reparentWouldCycle(const TreeState &state, int child, int parent)
481
+ {
482
+ int steps = 0;
483
+ for (int a = parent; a >= 0 && a < state.nodeCount; a = state.nodes[a].parent) {
484
+ if (a == child) {
485
+ std::printf("[tree] refused reparent child=%d under its own descendant parent=%d\n", child, parent);
486
+ return true;
487
+ }
488
+ if (++steps > state.nodeCount) {
489
+ std::printf("[tree] parent chain from %d already cyclic (count=%d)\n", parent, state.nodeCount);
490
+ return true;
491
+ }
492
+ }
493
+ return false;
494
+ }
495
+
496
+ void Tree::setParent(int child, int parent)
497
+ {
498
+ auto &state = treeState();
499
+ if (child < 0 || child >= state.nodeCount || parent < 0 || parent >= state.nodeCount) return;
500
+ if (child == parent) return;
501
+ if (reparentWouldCycle(state, child, parent)) return;
502
+ Node *c = &state.nodes[child];
503
+ const bool localMutation = localAbsoluteLeafTreeMutation(state, child);
504
+ if (c->parent >= 0) {
505
+ Node *old_parent = &state.nodes[c->parent];
506
+ if (old_parent->first_child == child) old_parent->first_child = c->next_sibling;
507
+ if (old_parent->last_child == child) old_parent->last_child = c->prev_sibling;
508
+ if (!localMutation) {
509
+ old_parent->render.dirty = 1;
510
+ old_parent->render.layout_dirty = 1;
511
+ old_parent->render.non_scroll_dirty = 1;
512
+ }
513
+ }
514
+ if (c->prev_sibling >= 0) state.nodes[c->prev_sibling].next_sibling = c->next_sibling;
515
+ if (c->next_sibling >= 0) state.nodes[c->next_sibling].prev_sibling = c->prev_sibling;
516
+
517
+ c->parent = parent;
518
+ c->next_sibling = -1;
519
+ c->prev_sibling = state.nodes[parent].last_child;
520
+ if (state.nodes[parent].last_child >= 0)
521
+ state.nodes[state.nodes[parent].last_child].next_sibling = child;
522
+ else
523
+ state.nodes[parent].first_child = child;
524
+ state.nodes[parent].last_child = child;
525
+ if (!localMutation) {
526
+ state.nodes[parent].render.dirty = 1;
527
+ state.nodes[parent].render.layout_dirty = 1;
528
+ state.nodes[parent].render.non_scroll_dirty = 1;
529
+ }
530
+ c->render.dirty = 1;
531
+ c->render.layout_dirty = 1;
532
+ c->render.non_scroll_dirty = 1;
533
+ if (localMutation)
534
+ markDisplayListContentDirty();
535
+ else
536
+ markDisplayListDirty();
537
+ StyleSheet::instance().recomputeSubtree(child);
538
+ }
539
+
540
+ void Tree::insertBefore(int child, int parent, int reference)
541
+ {
542
+ auto &state = treeState();
543
+ if (child < 0 || child >= state.nodeCount || parent < 0 || parent >= state.nodeCount) return;
544
+ if (child == parent) return;
545
+ if (reparentWouldCycle(state, child, parent)) return;
546
+ // No valid reference sibling under `parent` (or it's the child itself) → append.
547
+ if (reference < 0 || reference >= state.nodeCount || reference == child ||
548
+ state.nodes[reference].parent != parent) {
549
+ setParent(child, parent);
550
+ return;
551
+ }
552
+
553
+ Node *c = &state.nodes[child];
554
+ const bool localMutation = localAbsoluteLeafTreeMutation(state, child);
555
+ // Detach `child` from its current position first; this may mutate the
556
+ // reference node's prev_sibling if they were adjacent, so re-read it after.
557
+ if (c->parent >= 0) {
558
+ Node *old_parent = &state.nodes[c->parent];
559
+ if (old_parent->first_child == child) old_parent->first_child = c->next_sibling;
560
+ if (old_parent->last_child == child) old_parent->last_child = c->prev_sibling;
561
+ if (!localMutation) {
562
+ old_parent->render.dirty = 1;
563
+ old_parent->render.layout_dirty = 1;
564
+ old_parent->render.non_scroll_dirty = 1;
565
+ }
566
+ }
567
+ if (c->prev_sibling >= 0) state.nodes[c->prev_sibling].next_sibling = c->next_sibling;
568
+ if (c->next_sibling >= 0) state.nodes[c->next_sibling].prev_sibling = c->prev_sibling;
569
+
570
+ const int ref_prev = state.nodes[reference].prev_sibling;
571
+ c->parent = parent;
572
+ c->next_sibling = reference;
573
+ c->prev_sibling = ref_prev;
574
+ if (ref_prev >= 0)
575
+ state.nodes[ref_prev].next_sibling = child;
576
+ else
577
+ state.nodes[parent].first_child = child;
578
+ state.nodes[reference].prev_sibling = child;
579
+
580
+ if (!localMutation) {
581
+ state.nodes[parent].render.dirty = 1;
582
+ state.nodes[parent].render.layout_dirty = 1;
583
+ state.nodes[parent].render.non_scroll_dirty = 1;
584
+ }
585
+ c->render.dirty = 1;
586
+ c->render.layout_dirty = 1;
587
+ c->render.non_scroll_dirty = 1;
588
+ if (localMutation)
589
+ markDisplayListContentDirty();
590
+ else
591
+ markDisplayListDirty();
592
+ StyleSheet::instance().recomputeSubtree(child);
593
+ }
594
+
595
+ void Tree::removeNode(int id)
596
+ {
597
+ auto &state = treeState();
598
+ if (id < 0 || id >= state.nodeCount || !state.nodeActive[id]) return;
599
+ while (state.nodes[id].first_child >= 0) {
600
+ removeNode(state.nodes[id].first_child);
601
+ if (id >= state.nodeCount || !state.nodeActive[id]) return;
602
+ }
603
+ Node *n = &state.nodes[id];
604
+ markDisplayListDirty();
605
+ n->render.dirty = 1;
606
+ n->render.layout_dirty = 1;
607
+ n->render.non_scroll_dirty = 1;
608
+ if (n->parent >= 0) {
609
+ Node *p = &state.nodes[n->parent];
610
+ p->render.dirty = 1;
611
+ p->render.layout_dirty = 1;
612
+ p->render.non_scroll_dirty = 1;
613
+ if (p->first_child == id) p->first_child = n->next_sibling;
614
+ if (p->last_child == id) p->last_child = n->prev_sibling;
615
+ }
616
+ if (n->prev_sibling >= 0) state.nodes[n->prev_sibling].next_sibling = n->next_sibling;
617
+ if (n->next_sibling >= 0) state.nodes[n->next_sibling].prev_sibling = n->prev_sibling;
618
+ n->style.display = 1;
619
+ n->parent = -1;
620
+ n->first_child = -1;
621
+ n->last_child = -1;
622
+ n->prev_sibling = -1;
623
+ n->next_sibling = -1;
624
+ resetNodeSlot(state, id);
625
+ trimInactiveTail(state);
626
+ }
627
+
628
+ } // namespace gea::embedded::ui