@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,279 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "absolute_leaf_refresh.h"
3
+
4
+ #include "internal.h" // ViewRenderer::anyTransformActive (whole-tree transform fast-out)
5
+ #include "refresh_perf.h"
6
+ #include "tree_state.h"
7
+
8
+ namespace gea::embedded::ui {
9
+
10
+ namespace {
11
+
12
+ struct ScopedRefreshStat
13
+ {
14
+ std::int64_t start;
15
+ std::int64_t &slot;
16
+
17
+ explicit ScopedRefreshStat(std::int64_t &slot)
18
+ : start(refreshPerfNowUs()), slot(slot)
19
+ {
20
+ }
21
+
22
+ ~ScopedRefreshStat()
23
+ {
24
+ slot += refreshPerfNowUs() - start;
25
+ }
26
+ };
27
+
28
+ bool hasTransformState(const Node &node)
29
+ {
30
+ // Whole-tree fast out: a transform-free tree (the common case — e.g. the bouncing
31
+ // balls) skips all the pooled rstyle transform reads below. Safe to gate here now
32
+ // that anyTransformPresent() carries a DURABLE no-transform cache: it no longer
33
+ // re-scans on every mid-refresh refreshSerial bump, so calling it from mode() (which
34
+ // runs before those bumps) doesn't trigger the scan-then-invalidate that regressed
35
+ // fps before. A transform ADD routes through setStyle → clears the durable cache.
36
+ if (!ViewRenderer::anyTransformActive()) return false;
37
+ const RareStyle &rs = rstyle(node.style); // one pool lookup, not 11
38
+ return rs.transform_rotate != 0 ||
39
+ node.render.previous_transform_rotate != 0 ||
40
+ rs.transform_rotate_x != 0 ||
41
+ node.render.previous_transform_rotate_x != 0 ||
42
+ rs.transform_rotate_y != 0 ||
43
+ node.render.previous_transform_rotate_y != 0 ||
44
+ rs.transform_translate_x != 0 ||
45
+ node.render.previous_transform_translate_x != 0 ||
46
+ rs.transform_translate_y != 0 ||
47
+ node.render.previous_transform_translate_y != 0 ||
48
+ rs.transform_translate_z != 0 ||
49
+ node.render.previous_transform_translate_z != 0 ||
50
+ rs.transform_translate_x_percent != 0 ||
51
+ node.render.previous_transform_translate_x_percent != 0 ||
52
+ rs.transform_translate_y_percent != 0 ||
53
+ node.render.previous_transform_translate_y_percent != 0 ||
54
+ rs.transform_scale_x != 1000 ||
55
+ node.render.previous_transform_scale_x != 1000 ||
56
+ rs.transform_scale_y != 1000 ||
57
+ node.render.previous_transform_scale_y != 1000 ||
58
+ rs.perspective > 0 ||
59
+ node.render.previous_perspective > 0;
60
+ }
61
+
62
+ int alignedAbsoluteOffset(const Node &parent, const Node &child, bool horizontal)
63
+ {
64
+ const bool row = usesRowLayout(parent.style);
65
+ const int crossAlign = child.style.align_self >= 0 ? child.style.align_self : parent.style.align_items;
66
+ const int align = horizontal
67
+ ? (row ? parent.style.justify_content : crossAlign)
68
+ : (row ? crossAlign : parent.style.justify_content);
69
+ const int parentSize = horizontal ? parent.layout.width : parent.layout.height;
70
+ const int childSize = horizontal ? child.layout.width : child.layout.height;
71
+ const int beforeMargin = horizontal ? child.style.margin[3] : child.style.margin[0];
72
+ const int afterMargin = horizontal ? child.style.margin[1] : child.style.margin[2];
73
+ int available = parentSize;
74
+ if (available < 0) available = 0;
75
+ const int occupied = childSize + beforeMargin + afterMargin;
76
+ int free = available - occupied;
77
+ if (free < 0) free = 0;
78
+
79
+ int offset = beforeMargin;
80
+ if (align == 1) offset += free / 2;
81
+ else if (align == 2) offset += free;
82
+ return offset;
83
+ }
84
+
85
+ int resolvedPositionOffset(const Node &parent, const Node &child, int side)
86
+ {
87
+ const int basis = (side == 0 || side == 2) ? parent.layout.height : parent.layout.width;
88
+ int offset = child.style.pos_offsets[side] != kUnset ? child.style.pos_offsets[side] : 0;
89
+ const int percent = child.style.pos_offset_percent[side];
90
+ if (percent != kUnset) {
91
+ const int numerator = basis * percent;
92
+ offset += (numerator + (numerator >= 0 ? 500 : -500)) / 1000;
93
+ }
94
+ return offset;
95
+ }
96
+
97
+ bool hasPositionOffset(const Node &node, int side)
98
+ {
99
+ return node.style.pos_offsets[side] != kUnset || node.style.pos_offset_percent[side] != kUnset;
100
+ }
101
+
102
+ bool hasExplicitWidth(const Node &node)
103
+ {
104
+ return node.style.width != kUnset || node.style.width_percent != kUnset;
105
+ }
106
+
107
+ bool hasExplicitHeight(const Node &node)
108
+ {
109
+ return node.style.height != kUnset || node.style.height_percent != kUnset;
110
+ }
111
+
112
+ bool retainableAbsoluteLayoutNode(const Node &node)
113
+ {
114
+ if ((!isViewLikeNodeType(node.type) && node.type != NodeType::Image) || node.style.position != 1) return false;
115
+ if (node.parent < 0) return false;
116
+ if (!hasExplicitWidth(node) || !hasExplicitHeight(node)) return false;
117
+ if (node.first_child >= 0) {
118
+ if (node.style.width != kUnset && node.style.width != node.layout.width) return false;
119
+ if (node.style.height != kUnset && node.style.height != node.layout.height) return false;
120
+ if (node.layout.width != node.layout.previous_width || node.layout.height != node.layout.previous_height) return false;
121
+ }
122
+ if (node.style.min_width != 0 ||
123
+ node.style.min_height != 0 ||
124
+ node.style.max_width != kUnset ||
125
+ node.style.max_height != kUnset) return false;
126
+ if (node.style.flex != 0 ||
127
+ node.style.padding[0] ||
128
+ node.style.padding[1] ||
129
+ node.style.padding[2] ||
130
+ node.style.padding[3]) return false;
131
+ if (node.style.margin[0] || node.style.margin[1] || node.style.margin[2] || node.style.margin[3]) return false;
132
+ return true;
133
+ }
134
+
135
+ bool retainableAbsoluteNode(const Node &node)
136
+ {
137
+ if (!retainableAbsoluteLayoutNode(node)) return false;
138
+ if (hasTransformState(node)) return false;
139
+ if (rstyle(node.style).transform_origin_x != node.render.previous_transform_origin_x ||
140
+ rstyle(node.style).transform_origin_y != node.render.previous_transform_origin_y) return false;
141
+ return true;
142
+ }
143
+
144
+ bool retainableStaticTransformedPaintNode(const Node &node)
145
+ {
146
+ if (!isViewLikeNodeType(node.type) || node.style.position != 1) return false;
147
+ if (node.parent < 0) return false;
148
+ if (!hasTransformState(node) || node.render.transform_dirty) return false;
149
+ if (node.layout.x != node.layout.previous_x ||
150
+ node.layout.y != node.layout.previous_y ||
151
+ node.layout.width != node.layout.previous_width ||
152
+ node.layout.height != node.layout.previous_height)
153
+ return false;
154
+ if (rstyle(node.style).transform_origin_x != node.render.previous_transform_origin_x ||
155
+ rstyle(node.style).transform_origin_y != node.render.previous_transform_origin_y)
156
+ return false;
157
+ return true;
158
+ }
159
+
160
+ void translateDescendantLayouts(TreeState &state, int node, int dx, int dy)
161
+ {
162
+ if ((dx == 0 && dy == 0) || node < 0 || node >= state.nodeCount) return;
163
+ for (int child = state.nodes[node].first_child; child >= 0; child = state.nodes[child].next_sibling) {
164
+ state.nodes[child].layout.x += dx;
165
+ state.nodes[child].layout.y += dy;
166
+ translateDescendantLayouts(state, child, dx, dy);
167
+ }
168
+ }
169
+
170
+ } // namespace
171
+
172
+ int AbsoluteLeafRefresh::mode()
173
+ {
174
+ auto &state = treeState();
175
+ auto &perf = refreshPerfStatsMutable();
176
+ ScopedRefreshStat timer(perf.treeAbsModeUs);
177
+ perf.treeAbsModeCalls++;
178
+ int dirtyNodes = 0;
179
+ for (int i = 0; i < state.nodeCount; i++) {
180
+ if (state.nodes[i].render.dirty)
181
+ dirtyNodes++;
182
+ }
183
+ perf.treeAbsModeDirtyNodes += dirtyNodes;
184
+ if (dirtyNodes == 0) {
185
+ perf.treeAbsModeNoop++;
186
+ return -1;
187
+ }
188
+ auto reject = [&](int node, int reason) {
189
+ perf.treeAbsModeFull++;
190
+ perf.treeAbsModeRejectNode = node;
191
+ perf.treeAbsModeRejectReason = reason;
192
+ return 0;
193
+ };
194
+ for (int i = 0; i < state.nodeCount; i++) {
195
+ Node *n = &state.nodes[i];
196
+ if (!n->render.dirty) continue;
197
+ if (n->render.transform_dirty) {
198
+ if (retainableAbsoluteLayoutNode(*n)) continue;
199
+ if (n->layout.x != n->layout.previous_x ||
200
+ n->layout.y != n->layout.previous_y ||
201
+ n->layout.width != n->layout.previous_width ||
202
+ n->layout.height != n->layout.previous_height) return reject(i, 1);
203
+ continue;
204
+ }
205
+ if (n->type == NodeType::VirtualList) return reject(i, 2);
206
+ // Text nodes whose new content was pre-measured by Tree::setText are
207
+ // content-only updates and don't need a full re-layout pass. Absolute
208
+ // text may resize out of flow; in-flow text is admitted only when
209
+ // setText confirmed the existing layout box stayed unchanged.
210
+ if (n->type == NodeType::Text && n->render.text_layout_stable &&
211
+ n->parent >= 0 && n->first_child < 0) {
212
+ if (hasTransformState(*n)) return reject(i, 3);
213
+ if (rstyle(n->style).transform_origin_x != n->render.previous_transform_origin_x ||
214
+ rstyle(n->style).transform_origin_y != n->render.previous_transform_origin_y) return reject(i, 4);
215
+ if (n->style.position != 1 &&
216
+ (n->layout.x != n->layout.previous_x ||
217
+ n->layout.y != n->layout.previous_y ||
218
+ n->layout.width != n->layout.previous_width ||
219
+ n->layout.height != n->layout.previous_height))
220
+ return reject(i, 5);
221
+ continue;
222
+ }
223
+ // An absolute <img> leaf that reaches here is content-dirty — its image
224
+ // id changed (e.g. a runtime src swap: an EPUB cover extracted to SD and
225
+ // applied via setAttribute after mount). The fast position-only refresh
226
+ // would re-blit the stale cached paint and never re-record the new image,
227
+ // so force a full re-record. Rare, so the lost fast-path frame is fine.
228
+ if (n->type == NodeType::Image) return reject(i, 7);
229
+ if (retainableStaticTransformedPaintNode(*n)) continue;
230
+ if (!retainableAbsoluteNode(*n)) return reject(i, 6);
231
+ // A size change (layout != style) stays on the fast path only for leaves:
232
+ // out of flow, no children -> refreshPositions() reconciles layout from
233
+ // style with no tree-wide relayout. Non-leaf nodes must keep stable size
234
+ // so their descendants can be translated instead of reflowed.
235
+ }
236
+ perf.treeAbsModeFast++;
237
+ return 1;
238
+ }
239
+
240
+ void AbsoluteLeafRefresh::refreshPositions()
241
+ {
242
+ auto &state = treeState();
243
+ for (int i = 0; i < state.nodeCount; i++) {
244
+ Node *n = &state.nodes[i];
245
+ if (!n->render.dirty) continue;
246
+ if (n->render.transform_dirty && !retainableAbsoluteLayoutNode(*n)) continue;
247
+ if (n->type == NodeType::Text && n->render.text_layout_stable && n->style.position != 1) continue;
248
+ // Reconcile size from style for resized absolute leaves (mode() allowed
249
+ // the change). Before position so right/bottom-anchored offsets use the
250
+ // new extent. Percent-sized nodes keep the already-resolved layout box;
251
+ // the full layout pass owns recomputing them when their containing block
252
+ // changes.
253
+ if (n->style.width != kUnset) n->layout.width = n->style.width;
254
+ if (n->style.height != kUnset) n->layout.height = n->style.height;
255
+ Node *p = &state.nodes[n->parent];
256
+ const int before_x = n->layout.x;
257
+ const int before_y = n->layout.y;
258
+ int parent_x = p->layout.x;
259
+ if (p->style.overflow == 2 && scrollsOverflowX(p->style)) parent_x -= p->layout.scroll_x;
260
+ if (hasPositionOffset(*n, 3))
261
+ n->layout.x = parent_x + resolvedPositionOffset(*p, *n, 3);
262
+ else if (hasPositionOffset(*n, 1))
263
+ n->layout.x = parent_x + p->layout.width - n->layout.width - resolvedPositionOffset(*p, *n, 1);
264
+ else
265
+ n->layout.x = parent_x + alignedAbsoluteOffset(*p, *n, true);
266
+
267
+ int parent_y = p->layout.y;
268
+ if (p->style.overflow == 2 && scrollsOverflowY(p->style)) parent_y -= p->layout.scroll_y;
269
+ if (hasPositionOffset(*n, 0))
270
+ n->layout.y = parent_y + resolvedPositionOffset(*p, *n, 0);
271
+ else if (hasPositionOffset(*n, 2))
272
+ n->layout.y = parent_y + p->layout.height - n->layout.height - resolvedPositionOffset(*p, *n, 2);
273
+ else
274
+ n->layout.y = parent_y + alignedAbsoluteOffset(*p, *n, false);
275
+ translateDescendantLayouts(state, i, n->layout.x - before_x, n->layout.y - before_y);
276
+ }
277
+ }
278
+
279
+ } // namespace gea::embedded::ui
@@ -0,0 +1,12 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ namespace gea::embedded::ui {
5
+
6
+ class AbsoluteLeafRefresh {
7
+ public:
8
+ static int mode();
9
+ static void refreshPositions();
10
+ };
11
+
12
+ } // namespace gea::embedded::ui