@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
package/ui/view.cpp ADDED
@@ -0,0 +1,2232 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "internal.h"
3
+ #include "state_init.h"
4
+ #include "tree_state.h"
5
+ #include <pixel.h>
6
+
7
+ #include <algorithm>
8
+ #include <cmath>
9
+ #include <cstdio>
10
+
11
+ #ifdef quad
12
+ #undef quad
13
+ #endif
14
+
15
+ #ifndef GEA_EMBEDDED_RENDER_HOT_SRAM
16
+ #define GEA_EMBEDDED_RENDER_HOT_SRAM 0
17
+ #endif
18
+
19
+ #if GEA_EMBEDDED_RENDER_HOT_SRAM
20
+ #define GEA_VIEW_HOT_SRAM_SECTION(name) __attribute__((noinline, noclone, section(".time_critical.gea_view." name)))
21
+ #else
22
+ #define GEA_VIEW_HOT_SRAM_SECTION(name)
23
+ #endif
24
+
25
+ namespace gea::embedded::ui {
26
+
27
+ constexpr double kPi = 3.14159265358979323846;
28
+ constexpr float kPiF = 3.14159265358979323846f;
29
+ // Reciprocal constants so the per-corner transform math uses hardware FPU
30
+ // multiplies. The ESP32-S3 (Xtensa LX7) FPU has mul/add/sub but NO divide
31
+ // instruction, so `/ 1000.0f` lowered to a software __divsf3 call — measured
32
+ // as the dominant per-call cost of applyNodeTransform (12 software divides ×
33
+ // ~2300 calls/frame on the spinning css-3d-cube). `* kInv1000f` is a single
34
+ // hardware mul; sub-pixel accurate for screen coords (the values feeding this
35
+ // are integer permille/pixel quantities <= ~512).
36
+ constexpr float kInv1000f = 1.0f / 1000.0f;
37
+ constexpr float kInv1800f = 1.0f / 1800.0f;
38
+ // Number of constant-color strips a transformed (3D/perspective) linear gradient
39
+ // is sliced into (each strip is a FillQuad). Measured cheap on-device (~20ms for
40
+ // all of css-3d-cube's face strips combined — the frame cost was the full-screen
41
+ // background gradient, not these), so favor smoothness.
42
+ constexpr int kTransformedGradientSteps = 72;
43
+
44
+ uint8_t combineAlpha(uint8_t parentAlpha, uint8_t localAlpha)
45
+ {
46
+ return static_cast<uint8_t>((static_cast<int>(parentAlpha) * static_cast<int>(localAlpha) + 127) / 255);
47
+ }
48
+
49
+ bool borderIsSameOpaqueSolidBackground(const Node &node, uint8_t parentAlpha)
50
+ {
51
+ return node.style.has_bg &&
52
+ node.style.bg_fill == 0 &&
53
+ node.style.bg_color == node.style.border_color &&
54
+ combineAlpha(parentAlpha, node.style.bg_alpha) == combineAlpha(parentAlpha, node.style.border_alpha);
55
+ }
56
+
57
+ void appendAlphaCommand(uint8_t alpha, int bx, int by, int bw, int bh)
58
+ {
59
+ DisplayCommand *cmd = DisplayList::instance().append();
60
+ if (!cmd) return;
61
+ cmd->type = DisplayCommandType::SetAlpha;
62
+ cmd->bx = bx; cmd->by = by; cmd->bw = bw; cmd->bh = bh;
63
+ cmd->alpha.alpha = alpha;
64
+ }
65
+
66
+ uint16_t interpolateColor(uint16_t from, uint16_t to, int permille)
67
+ {
68
+ if (permille <= 0) return from;
69
+ if (permille >= 1000) return to;
70
+ return gea::framework::graphics::pixel::blend(to, from, (permille * 255 + 500) / 1000);
71
+ }
72
+
73
+ uint8_t interpolateAlpha(uint8_t from, uint8_t to, int permille)
74
+ {
75
+ if (permille <= 0) return from;
76
+ if (permille >= 1000) return to;
77
+ return static_cast<uint8_t>((static_cast<int>(from) * (1000 - permille) + static_cast<int>(to) * permille + 500) / 1000);
78
+ }
79
+
80
+ uint16_t interpolatePremultipliedColor(uint16_t fromColor, uint8_t fromAlpha,
81
+ uint16_t toColor, uint8_t toAlpha,
82
+ int permille)
83
+ {
84
+ if (permille <= 0) return fromColor;
85
+ if (permille >= 1000) return toColor;
86
+ int fr = 0, fg = 0, fb = 0, tr = 0, tg = 0, tb = 0;
87
+ gea::framework::graphics::pixel::unpackRgb565(fromColor, &fr, &fg, &fb);
88
+ gea::framework::graphics::pixel::unpackRgb565(toColor, &tr, &tg, &tb);
89
+ fr = (fr * 255 + 15) / 31;
90
+ fg = (fg * 255 + 31) / 63;
91
+ fb = (fb * 255 + 15) / 31;
92
+ tr = (tr * 255 + 15) / 31;
93
+ tg = (tg * 255 + 31) / 63;
94
+ tb = (tb * 255 + 15) / 31;
95
+ const int fromWeight = static_cast<int>(fromAlpha) * (1000 - permille);
96
+ const int toWeight = static_cast<int>(toAlpha) * permille;
97
+ const int weight = fromWeight + toWeight;
98
+ if (weight <= 0) return toColor;
99
+ const int r = (fr * fromWeight + tr * toWeight + weight / 2) / weight;
100
+ const int g = (fg * fromWeight + tg * toWeight + weight / 2) / weight;
101
+ const int b = (fb * fromWeight + tb * toWeight + weight / 2) / weight;
102
+ // Panel-endian-aware: framebuffer pixels are byte-swapped when
103
+ // GEA_EMBEDDED_PIXEL_PANEL_ENDIAN=1 (the endpoints above return the already
104
+ // byte-swapped stop colors). Emitting raw rgb565FromRgb888 here made the
105
+ // interpolated middle of transformed gradient faces land un-swapped → scrambled
106
+ // hues on device. fromRgb888 == rgb565FromRgb888 on host.
107
+ return gea::framework::graphics::pixel::fromRgb888(r, g, b);
108
+ }
109
+
110
+ int clampPermille(int value)
111
+ {
112
+ if (value < 0) return 0;
113
+ if (value > 1000) return 1000;
114
+ return value;
115
+ }
116
+
117
+ int gradientToStop(const Node &node)
118
+ {
119
+ const int toStop = rstyle(node.style).bg_gradient_to_stop > 0 ? rstyle(node.style).bg_gradient_to_stop : 1000;
120
+ return std::max(1, toStop);
121
+ }
122
+
123
+ int stopRangePermille(int permille, int start, int end)
124
+ {
125
+ if (permille <= start) return 0;
126
+ if (permille >= end) return 1000;
127
+ const int span = end - start;
128
+ if (span <= 0) return 1000;
129
+ return ((permille - start) * 1000 + span / 2) / span;
130
+ }
131
+
132
+ uint16_t gradientColorAt(const Node &node, int permille)
133
+ {
134
+ permille = clampPermille(permille);
135
+ const int toStop = gradientToStop(node);
136
+ if (rstyle(node.style).bg_gradient_has_mid && rstyle(node.style).bg_gradient_mid_stop > 0 && rstyle(node.style).bg_gradient_mid_stop < toStop) {
137
+ const int stop = rstyle(node.style).bg_gradient_mid_stop;
138
+ if (permille <= stop)
139
+ return interpolatePremultipliedColor(rstyle(node.style).bg_gradient_from_color,
140
+ rstyle(node.style).bg_gradient_from_alpha,
141
+ rstyle(node.style).bg_gradient_mid_color,
142
+ rstyle(node.style).bg_gradient_mid_alpha,
143
+ stopRangePermille(permille, 0, stop));
144
+ return interpolatePremultipliedColor(rstyle(node.style).bg_gradient_mid_color,
145
+ rstyle(node.style).bg_gradient_mid_alpha,
146
+ rstyle(node.style).bg_gradient_to_color,
147
+ rstyle(node.style).bg_gradient_to_alpha,
148
+ stopRangePermille(permille, stop, toStop));
149
+ }
150
+ return interpolatePremultipliedColor(rstyle(node.style).bg_gradient_from_color,
151
+ rstyle(node.style).bg_gradient_from_alpha,
152
+ rstyle(node.style).bg_gradient_to_color,
153
+ rstyle(node.style).bg_gradient_to_alpha,
154
+ stopRangePermille(permille, 0, toStop));
155
+ }
156
+
157
+ uint8_t gradientAlphaAt(const Node &node, int permille)
158
+ {
159
+ permille = clampPermille(permille);
160
+ const int toStop = gradientToStop(node);
161
+ if (rstyle(node.style).bg_gradient_has_mid && rstyle(node.style).bg_gradient_mid_stop > 0 && rstyle(node.style).bg_gradient_mid_stop < toStop) {
162
+ const int stop = rstyle(node.style).bg_gradient_mid_stop;
163
+ if (permille <= stop)
164
+ return interpolateAlpha(rstyle(node.style).bg_gradient_from_alpha,
165
+ rstyle(node.style).bg_gradient_mid_alpha,
166
+ stopRangePermille(permille, 0, stop));
167
+ return interpolateAlpha(rstyle(node.style).bg_gradient_mid_alpha,
168
+ rstyle(node.style).bg_gradient_to_alpha,
169
+ stopRangePermille(permille, stop, toStop));
170
+ }
171
+ return interpolateAlpha(rstyle(node.style).bg_gradient_from_alpha, rstyle(node.style).bg_gradient_to_alpha, stopRangePermille(permille, 0, toStop));
172
+ }
173
+
174
+ void appendFillRectWithAlpha(int x, int y, int w, int h, uint16_t color, uint8_t alpha, uint8_t parentAlpha, int bx, int by, int bw, int bh)
175
+ {
176
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, alpha);
177
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(effectiveAlpha, bx, by, bw, bh);
178
+ DisplayCommand *cmd = DisplayList::instance().append();
179
+ if (cmd) {
180
+ cmd->type = DisplayCommandType::FillRect;
181
+ cmd->bx = bx; cmd->by = by; cmd->bw = bw; cmd->bh = bh;
182
+ cmd->fill.x = x; cmd->fill.y = y;
183
+ cmd->fill.w = w; cmd->fill.h = h;
184
+ cmd->fill.color = color;
185
+ }
186
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(parentAlpha, bx, by, bw, bh);
187
+ }
188
+
189
+ void appendFillRectRaw(int x, int y, int w, int h, uint16_t color, int bx, int by, int bw, int bh)
190
+ {
191
+ DisplayCommand *cmd = DisplayList::instance().append();
192
+ if (!cmd) return;
193
+ cmd->type = DisplayCommandType::FillRect;
194
+ cmd->bx = bx; cmd->by = by; cmd->bw = bw; cmd->bh = bh;
195
+ cmd->fill.x = x; cmd->fill.y = y;
196
+ cmd->fill.w = w; cmd->fill.h = h;
197
+ cmd->fill.color = color;
198
+ }
199
+
200
+ void appendFillQuadWithAlpha(const int16_t *xs, const int16_t *ys, uint16_t color, uint8_t alpha, uint8_t parentAlpha,
201
+ int bx, int by, int bw, int bh,
202
+ int lx = 0, int ly = 0, int lw = 0, int lh = 0, uint8_t reprojectMode = 0)
203
+ {
204
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, alpha);
205
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(effectiveAlpha, bx, by, bw, bh);
206
+ DisplayCommand *cmd = DisplayList::instance().append();
207
+ if (cmd) {
208
+ cmd->type = DisplayCommandType::FillQuad;
209
+ cmd->bx = bx;
210
+ cmd->by = by;
211
+ cmd->bw = bw;
212
+ cmd->bh = bh;
213
+ cmd->quad.x0 = xs[0]; cmd->quad.y0 = ys[0];
214
+ cmd->quad.x1 = xs[1]; cmd->quad.y1 = ys[1];
215
+ cmd->quad.x2 = xs[2]; cmd->quad.y2 = ys[2];
216
+ cmd->quad.x3 = xs[3]; cmd->quad.y3 = ys[3];
217
+ cmd->quad.lx = static_cast<int16_t>(lx); cmd->quad.ly = static_cast<int16_t>(ly);
218
+ cmd->quad.lw = static_cast<int16_t>(lw); cmd->quad.lh = static_cast<int16_t>(lh);
219
+ cmd->quad.aux = 0;
220
+ cmd->quad.color = color;
221
+ cmd->quad.reprojectMode = reprojectMode;
222
+ }
223
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(parentAlpha, bx, by, bw, bh);
224
+ }
225
+
226
+ void appendFillQuadRaw(const int16_t *xs, const int16_t *ys, uint16_t color, int bx, int by, int bw, int bh,
227
+ int lx = 0, int ly = 0, int lw = 0, int lh = 0, uint8_t reprojectMode = 0)
228
+ {
229
+ DisplayCommand *cmd = DisplayList::instance().append();
230
+ if (!cmd) return;
231
+ cmd->type = DisplayCommandType::FillQuad;
232
+ cmd->bx = bx;
233
+ cmd->by = by;
234
+ cmd->bw = bw;
235
+ cmd->bh = bh;
236
+ cmd->quad.x0 = xs[0]; cmd->quad.y0 = ys[0];
237
+ cmd->quad.x1 = xs[1]; cmd->quad.y1 = ys[1];
238
+ cmd->quad.x2 = xs[2]; cmd->quad.y2 = ys[2];
239
+ cmd->quad.x3 = xs[3]; cmd->quad.y3 = ys[3];
240
+ cmd->quad.lx = static_cast<int16_t>(lx); cmd->quad.ly = static_cast<int16_t>(ly);
241
+ cmd->quad.lw = static_cast<int16_t>(lw); cmd->quad.lh = static_cast<int16_t>(lh);
242
+ cmd->quad.aux = 0;
243
+ cmd->quad.color = color;
244
+ cmd->quad.reprojectMode = reprojectMode;
245
+ }
246
+
247
+ void appendFillQuadStrokeSegmentRaw(const int16_t *xs, const int16_t *ys, uint16_t color,
248
+ int bx, int by, int bw, int bh,
249
+ float localX0, float localY0, float localX1, float localY1, float strokeWidth)
250
+ {
251
+ DisplayCommand *cmd = DisplayList::instance().append();
252
+ if (!cmd) return;
253
+ cmd->type = DisplayCommandType::FillQuad;
254
+ cmd->bx = bx;
255
+ cmd->by = by;
256
+ cmd->bw = bw;
257
+ cmd->bh = bh;
258
+ cmd->quad.x0 = xs[0]; cmd->quad.y0 = ys[0];
259
+ cmd->quad.x1 = xs[1]; cmd->quad.y1 = ys[1];
260
+ cmd->quad.x2 = xs[2]; cmd->quad.y2 = ys[2];
261
+ cmd->quad.x3 = xs[3]; cmd->quad.y3 = ys[3];
262
+ cmd->quad.lx = static_cast<int16_t>(std::lroundf(localX0 * 8.0f));
263
+ cmd->quad.ly = static_cast<int16_t>(std::lroundf(localY0 * 8.0f));
264
+ cmd->quad.lw = static_cast<int16_t>(std::lroundf(localX1 * 8.0f));
265
+ cmd->quad.lh = static_cast<int16_t>(std::lroundf(localY1 * 8.0f));
266
+ cmd->quad.aux = static_cast<int16_t>(std::lroundf(strokeWidth * 8.0f));
267
+ cmd->quad.color = color;
268
+ cmd->quad.reprojectMode = 2;
269
+ }
270
+
271
+ void boundsFromCorners(const int16_t *xs, const int16_t *ys, int *x0, int *y0, int *x1, int *y1);
272
+
273
+ void appendLinearGradientRectRaw(const Node &node, int x, int y, int w, int h,
274
+ uint16_t fromColor, uint16_t midColor, uint16_t toColor,
275
+ uint16_t midStop, uint16_t toStop, int16_t angle, uint8_t fromAlpha,
276
+ uint8_t midAlpha, uint8_t toAlpha, uint8_t hasMid)
277
+ {
278
+ DisplayCommand *cmd = DisplayList::instance().append();
279
+ if (!cmd) return;
280
+ cmd->type = DisplayCommandType::FillLinearGradient;
281
+ cmd->bx = x;
282
+ cmd->by = y;
283
+ cmd->bw = w;
284
+ cmd->bh = h;
285
+ cmd->gradient.x = x;
286
+ cmd->gradient.y = y;
287
+ cmd->gradient.w = w;
288
+ cmd->gradient.h = h;
289
+ cmd->gradient.tl = node.style.border_radius[0];
290
+ cmd->gradient.tr = node.style.border_radius[1];
291
+ cmd->gradient.br = node.style.border_radius[2];
292
+ cmd->gradient.bl = node.style.border_radius[3];
293
+ cmd->gradient.fromColor = fromColor;
294
+ cmd->gradient.midColor = midColor;
295
+ cmd->gradient.toColor = toColor;
296
+ cmd->gradient.midStop = midStop;
297
+ cmd->gradient.toStop = toStop > 0 ? toStop : 1000;
298
+ cmd->gradient.angle = angle;
299
+ cmd->gradient.fromAlpha = fromAlpha;
300
+ cmd->gradient.midAlpha = midAlpha;
301
+ cmd->gradient.toAlpha = toAlpha;
302
+ cmd->gradient.hasMid = hasMid;
303
+ }
304
+
305
+ void appendLinearGradientRectRaw(const Node &node, int x, int y, int w, int h)
306
+ {
307
+ appendLinearGradientRectRaw(node,
308
+ x,
309
+ y,
310
+ w,
311
+ h,
312
+ rstyle(node.style).bg_gradient_from_color,
313
+ rstyle(node.style).bg_gradient_mid_color,
314
+ rstyle(node.style).bg_gradient_to_color,
315
+ rstyle(node.style).bg_gradient_mid_stop,
316
+ rstyle(node.style).bg_gradient_to_stop,
317
+ rstyle(node.style).bg_gradient_angle,
318
+ rstyle(node.style).bg_gradient_from_alpha,
319
+ rstyle(node.style).bg_gradient_mid_alpha,
320
+ rstyle(node.style).bg_gradient_to_alpha,
321
+ rstyle(node.style).bg_gradient_has_mid);
322
+ }
323
+
324
+ void appendRadialGradientRectRaw(const Node &node, int x, int y, int w, int h)
325
+ {
326
+ DisplayCommand *cmd = DisplayList::instance().append();
327
+ if (!cmd) return;
328
+ cmd->type = DisplayCommandType::FillRadialGradient;
329
+ cmd->bx = x;
330
+ cmd->by = y;
331
+ cmd->bw = w;
332
+ cmd->bh = h;
333
+ cmd->radialGradient.x = x;
334
+ cmd->radialGradient.y = y;
335
+ cmd->radialGradient.w = w;
336
+ cmd->radialGradient.h = h;
337
+ cmd->radialGradient.tl = node.style.border_radius[0];
338
+ cmd->radialGradient.tr = node.style.border_radius[1];
339
+ cmd->radialGradient.br = node.style.border_radius[2];
340
+ cmd->radialGradient.bl = node.style.border_radius[3];
341
+ cmd->radialGradient.cxPermille = rstyle(node.style).bg_radial_gradient_cx;
342
+ cmd->radialGradient.cyPermille = rstyle(node.style).bg_radial_gradient_cy;
343
+ cmd->radialGradient.rxPermille = rstyle(node.style).bg_radial_gradient_rx;
344
+ cmd->radialGradient.ryPermille = rstyle(node.style).bg_radial_gradient_ry;
345
+ cmd->radialGradient.fromColor = rstyle(node.style).bg_radial_gradient_from_color;
346
+ cmd->radialGradient.toColor = rstyle(node.style).bg_radial_gradient_to_color;
347
+ cmd->radialGradient.stopPermille = rstyle(node.style).bg_radial_gradient_stop;
348
+ cmd->radialGradient.fromAlpha = rstyle(node.style).bg_radial_gradient_from_alpha;
349
+ cmd->radialGradient.toAlpha = rstyle(node.style).bg_radial_gradient_to_alpha;
350
+ }
351
+
352
+ void appendDrawLineRaw(int x0, int y0, int x1, int y1, uint16_t color)
353
+ {
354
+ DisplayCommand *cmd = DisplayList::instance().append();
355
+ if (!cmd) return;
356
+ cmd->type = DisplayCommandType::DrawLine;
357
+ const int bx0 = std::min(x0, x1);
358
+ const int by0 = std::min(y0, y1);
359
+ const int bx1 = std::max(x0, x1);
360
+ const int by1 = std::max(y0, y1);
361
+ cmd->bx = bx0;
362
+ cmd->by = by0;
363
+ cmd->bw = bx1 - bx0 + 1;
364
+ cmd->bh = by1 - by0 + 1;
365
+ cmd->line.x0 = x0;
366
+ cmd->line.y0 = y0;
367
+ cmd->line.x1 = x1;
368
+ cmd->line.y1 = y1;
369
+ cmd->line.color = color;
370
+ }
371
+
372
+ void appendStrokeSegmentBandRaw(float x0, float y0, float x1, float y1, float width, uint16_t color,
373
+ float localX0 = 0.0f, float localY0 = 0.0f,
374
+ float localX1 = 0.0f, float localY1 = 0.0f,
375
+ bool reprojectable = false)
376
+ {
377
+ // All-float with a single reciprocal (was double sqrt + 4 double divides per
378
+ // band; called ~144x/frame for the cube's two rings). The Xtensa LX7 has no
379
+ // hardware double and no divide, so this was a heavy chunk of the ring record.
380
+ const float dx = x1 - x0;
381
+ const float dy = y1 - y0;
382
+ const float len2 = dx * dx + dy * dy;
383
+ if (len2 < 1e-6f) return;
384
+ const float inv = 1.0f / std::sqrt(len2);
385
+
386
+ const float half = width * 0.5f;
387
+ const float nx = -dy * half * inv;
388
+ const float ny = dx * half * inv;
389
+ const float ex = dx * 0.75f * inv;
390
+ const float ey = dy * 0.75f * inv;
391
+ int16_t xs[4] = {
392
+ static_cast<int16_t>(std::lroundf(x0 - ex + nx)),
393
+ static_cast<int16_t>(std::lroundf(x1 + ex + nx)),
394
+ static_cast<int16_t>(std::lroundf(x1 + ex - nx)),
395
+ static_cast<int16_t>(std::lroundf(x0 - ex - nx)),
396
+ };
397
+ int16_t ys[4] = {
398
+ static_cast<int16_t>(std::lroundf(y0 - ey + ny)),
399
+ static_cast<int16_t>(std::lroundf(y1 + ey + ny)),
400
+ static_cast<int16_t>(std::lroundf(y1 + ey - ny)),
401
+ static_cast<int16_t>(std::lroundf(y0 - ey - ny)),
402
+ };
403
+ int bx0, by0, bx1, by1;
404
+ boundsFromCorners(xs, ys, &bx0, &by0, &bx1, &by1);
405
+ if (reprojectable)
406
+ appendFillQuadStrokeSegmentRaw(xs, ys, color, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1,
407
+ localX0, localY0, localX1, localY1, width);
408
+ else
409
+ appendFillQuadRaw(xs, ys, color, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
410
+ }
411
+
412
+ bool gradientAlphaNearlyConstant(const Node &node)
413
+ {
414
+ uint8_t minAlpha = std::min(rstyle(node.style).bg_gradient_from_alpha, rstyle(node.style).bg_gradient_to_alpha);
415
+ uint8_t maxAlpha = std::max(rstyle(node.style).bg_gradient_from_alpha, rstyle(node.style).bg_gradient_to_alpha);
416
+ if (rstyle(node.style).bg_gradient_has_mid) {
417
+ minAlpha = std::min(minAlpha, rstyle(node.style).bg_gradient_mid_alpha);
418
+ maxAlpha = std::max(maxAlpha, rstyle(node.style).bg_gradient_mid_alpha);
419
+ }
420
+ return maxAlpha - minAlpha <= 8;
421
+ }
422
+
423
+ bool hasAnyRadius(const Node &node)
424
+ {
425
+ return node.style.border_radius[0] || node.style.border_radius[1] ||
426
+ node.style.border_radius[2] || node.style.border_radius[3] ||
427
+ node.style.border_radius_percent[0] != kUnset ||
428
+ node.style.border_radius_percent[1] != kUnset ||
429
+ node.style.border_radius_percent[2] != kUnset ||
430
+ node.style.border_radius_percent[3] != kUnset;
431
+ }
432
+
433
+ bool hasAnyPercentRadius(const Node &node)
434
+ {
435
+ return node.style.border_radius_percent[0] != kUnset ||
436
+ node.style.border_radius_percent[1] != kUnset ||
437
+ node.style.border_radius_percent[2] != kUnset ||
438
+ node.style.border_radius_percent[3] != kUnset;
439
+ }
440
+
441
+ int integerSqrt(int n)
442
+ {
443
+ if (n <= 0) return 0;
444
+ int x = n;
445
+ int y = (x + 1) / 2;
446
+ while (y < x) {
447
+ x = y;
448
+ y = (x + n / x) / 2;
449
+ }
450
+ return x;
451
+ }
452
+
453
+ int normalizedRadius(int radius, int maxRadius)
454
+ {
455
+ if (radius < 0) return 0;
456
+ return radius > maxRadius ? maxRadius : radius;
457
+ }
458
+
459
+ void roundedNodeRowSpan(const Node &node, int y, int *x0, int *x1)
460
+ {
461
+ const int maxRadius = std::min(node.layout.width / 2, node.layout.height / 2);
462
+ const int tl = normalizedRadius(node.style.border_radius[0], maxRadius);
463
+ const int tr = normalizedRadius(node.style.border_radius[1], maxRadius);
464
+ const int br = normalizedRadius(node.style.border_radius[2], maxRadius);
465
+ const int bl = normalizedRadius(node.style.border_radius[3], maxRadius);
466
+ if ((tl | tr | br | bl) == 0) return;
467
+
468
+ const int left = node.layout.x;
469
+ const int right = node.layout.x + node.layout.width - 1;
470
+ const int top = node.layout.y;
471
+ const int bottom = node.layout.y + node.layout.height - 1;
472
+ int rowLeft = left;
473
+ int rowRight = right;
474
+
475
+ if (tl > 0 && y >= top && y <= top + tl) {
476
+ const int dy = top + tl - y;
477
+ const int dx = integerSqrt(tl * tl - dy * dy);
478
+ rowLeft = std::max(rowLeft, left + tl - dx);
479
+ }
480
+ if (bl > 0 && y >= bottom - bl && y <= bottom) {
481
+ const int dy = y - (bottom - bl);
482
+ const int dx = integerSqrt(bl * bl - dy * dy);
483
+ rowLeft = std::max(rowLeft, left + bl - dx);
484
+ }
485
+ if (tr > 0 && y >= top && y <= top + tr) {
486
+ const int dy = top + tr - y;
487
+ const int dx = integerSqrt(tr * tr - dy * dy);
488
+ rowRight = std::min(rowRight, right - tr + dx);
489
+ }
490
+ if (br > 0 && y >= bottom - br && y <= bottom) {
491
+ const int dy = y - (bottom - br);
492
+ const int dx = integerSqrt(br * br - dy * dy);
493
+ rowRight = std::min(rowRight, right - br + dx);
494
+ }
495
+
496
+ *x0 = std::max(*x0, rowLeft);
497
+ *x1 = std::min(*x1, rowRight);
498
+ }
499
+
500
+ bool isFullyRoundedShape(const Node &node)
501
+ {
502
+ if (hasAnyPercentRadius(node)) return false;
503
+ if (std::abs(node.layout.width - node.layout.height) > 1) return false;
504
+ const int halfMin = std::min(node.layout.width, node.layout.height) / 2;
505
+ if (halfMin <= 0) return false;
506
+ const int minRadius = std::max(1, halfMin - 1);
507
+ return node.style.border_radius[0] >= minRadius &&
508
+ node.style.border_radius[1] >= minRadius &&
509
+ node.style.border_radius[2] >= minRadius &&
510
+ node.style.border_radius[3] >= minRadius;
511
+ }
512
+
513
+ void resolvedBorderRadii8(const Node &node, int16_t rx8[4], int16_t ry8[4])
514
+ {
515
+ const double width = static_cast<double>(std::max(0, static_cast<int>(node.layout.width)));
516
+ const double height = static_cast<double>(std::max(0, static_cast<int>(node.layout.height)));
517
+ double rx[4]{};
518
+ double ry[4]{};
519
+ for (int i = 0; i < 4; i++) {
520
+ if (node.style.border_radius_percent[i] != kUnset) {
521
+ const double p = static_cast<double>(node.style.border_radius_percent[i]) / 1000.0;
522
+ rx[i] = std::max(0.0, width * p);
523
+ ry[i] = std::max(0.0, height * p);
524
+ } else {
525
+ const double r = static_cast<double>(std::max(0, static_cast<int>(node.style.border_radius[i])));
526
+ rx[i] = r;
527
+ ry[i] = r;
528
+ }
529
+ }
530
+
531
+ double scale = 1.0;
532
+ const auto constrain = [&](double limit, double sum) {
533
+ if (limit > 0.0 && sum > limit) scale = std::min(scale, limit / sum);
534
+ };
535
+ constrain(width, rx[0] + rx[1]);
536
+ constrain(width, rx[3] + rx[2]);
537
+ constrain(height, ry[0] + ry[3]);
538
+ constrain(height, ry[1] + ry[2]);
539
+
540
+ for (int i = 0; i < 4; i++) {
541
+ rx8[i] = static_cast<int16_t>(std::max(0L, std::min(32767L, std::lround(rx[i] * scale * 8.0))));
542
+ ry8[i] = static_cast<int16_t>(std::max(0L, std::min(32767L, std::lround(ry[i] * scale * 8.0))));
543
+ }
544
+ }
545
+
546
+ bool resolvedCircularBorderRadii(const Node &node, int radii[4])
547
+ {
548
+ int16_t rx8[4]{};
549
+ int16_t ry8[4]{};
550
+ resolvedBorderRadii8(node, rx8, ry8);
551
+ for (int i = 0; i < 4; i++) {
552
+ if (std::abs(static_cast<int>(rx8[i]) - static_cast<int>(ry8[i])) > 4) return false;
553
+ const int radius8 = (static_cast<int>(rx8[i]) + static_cast<int>(ry8[i])) / 2;
554
+ radii[i] = std::max(0, std::min(32767, (radius8 + 4) / 8));
555
+ }
556
+ return true;
557
+ }
558
+
559
+ void appendFillRoundedRectWithAlpha(const Node &node, uint8_t parentAlpha, const int *resolvedRadii = nullptr)
560
+ {
561
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, node.style.bg_alpha);
562
+ if (effectiveAlpha != parentAlpha)
563
+ appendAlphaCommand(effectiveAlpha, node.layout.x, node.layout.y, node.layout.width, node.layout.height);
564
+ DisplayCommand *cmd = DisplayList::instance().append();
565
+ if (cmd) {
566
+ const int tl = resolvedRadii ? resolvedRadii[0] : node.style.border_radius[0];
567
+ const int tr = resolvedRadii ? resolvedRadii[1] : node.style.border_radius[1];
568
+ const int br = resolvedRadii ? resolvedRadii[2] : node.style.border_radius[2];
569
+ const int bl = resolvedRadii ? resolvedRadii[3] : node.style.border_radius[3];
570
+ cmd->type = DisplayCommandType::FillRoundedRect;
571
+ cmd->bx = node.layout.x; cmd->by = node.layout.y; cmd->bw = node.layout.width; cmd->bh = node.layout.height;
572
+ cmd->fillRoundedRect.x = node.layout.x; cmd->fillRoundedRect.y = node.layout.y;
573
+ cmd->fillRoundedRect.w = node.layout.width; cmd->fillRoundedRect.h = node.layout.height;
574
+ cmd->fillRoundedRect.tl = tl;
575
+ cmd->fillRoundedRect.tr = tr;
576
+ cmd->fillRoundedRect.br = br;
577
+ cmd->fillRoundedRect.bl = bl;
578
+ cmd->fillRoundedRect.color = node.style.bg_color;
579
+ }
580
+ if (effectiveAlpha != parentAlpha)
581
+ appendAlphaCommand(parentAlpha, node.layout.x, node.layout.y, node.layout.width, node.layout.height);
582
+ }
583
+
584
+ bool appendResolvedCircularRoundedRectWithAlpha(const Node &node, uint8_t parentAlpha)
585
+ {
586
+ int radii[4]{};
587
+ if (!resolvedCircularBorderRadii(node, radii)) return false;
588
+ appendFillRoundedRectWithAlpha(node, parentAlpha, radii);
589
+ return true;
590
+ }
591
+
592
+ bool resolvedRadiiAreCircleLike(int w, int h, const int16_t rx8[4], const int16_t ry8[4])
593
+ {
594
+ if (w <= 0 || h <= 0 || std::abs(w - h) > 1)
595
+ return false;
596
+ const int targetRx8 = w * 4;
597
+ const int targetRy8 = h * 4;
598
+ for (int i = 0; i < 4; i++) {
599
+ if (std::abs(static_cast<int>(rx8[i]) - targetRx8) > 8)
600
+ return false;
601
+ if (std::abs(static_cast<int>(ry8[i]) - targetRy8) > 8)
602
+ return false;
603
+ }
604
+ return true;
605
+ }
606
+
607
+ bool transformedCornersCircle(const int16_t xs[4], const int16_t ys[4], int *cx, int *cy, int *r)
608
+ {
609
+ if (!cx || !cy || !r)
610
+ return false;
611
+ if (std::abs(static_cast<int>(xs[0]) - static_cast<int>(xs[3])) > 1 ||
612
+ std::abs(static_cast<int>(xs[1]) - static_cast<int>(xs[2])) > 1 ||
613
+ std::abs(static_cast<int>(ys[0]) - static_cast<int>(ys[1])) > 1 ||
614
+ std::abs(static_cast<int>(ys[3]) - static_cast<int>(ys[2])) > 1)
615
+ return false;
616
+
617
+ const float left = 0.5f * static_cast<float>(static_cast<int>(xs[0]) + static_cast<int>(xs[3]));
618
+ const float right = 0.5f * static_cast<float>(static_cast<int>(xs[1]) + static_cast<int>(xs[2]));
619
+ const float top = 0.5f * static_cast<float>(static_cast<int>(ys[0]) + static_cast<int>(ys[1]));
620
+ const float bottom = 0.5f * static_cast<float>(static_cast<int>(ys[2]) + static_cast<int>(ys[3]));
621
+ const float width = right - left;
622
+ const float height = bottom - top;
623
+ if (width <= 0.0f || height <= 0.0f || std::fabs(width - height) > 1.5f)
624
+ return false;
625
+
626
+ const int radius = static_cast<int>(std::lround(std::min(width, height) * 0.5f));
627
+ if (radius <= 0)
628
+ return false;
629
+ *cx = static_cast<int>(std::lround((left + right) * 0.5f));
630
+ *cy = static_cast<int>(std::lround((top + bottom) * 0.5f));
631
+ *r = radius;
632
+ return true;
633
+ }
634
+
635
+ void appendFillCircleWithAlpha(int cx, int cy, int r, gea::framework::graphics::pixel::native_t color, uint8_t effectiveAlpha, uint8_t parentAlpha)
636
+ {
637
+ if (r <= 0)
638
+ return;
639
+ const int x = cx - r;
640
+ const int y = cy - r;
641
+ const int d = r * 2 + 1;
642
+ if (effectiveAlpha != parentAlpha)
643
+ appendAlphaCommand(effectiveAlpha, x, y, d, d);
644
+ DisplayCommand *cmd = DisplayList::instance().append();
645
+ if (cmd) {
646
+ cmd->type = DisplayCommandType::FillCircle;
647
+ cmd->bx = static_cast<int16_t>(x);
648
+ cmd->by = static_cast<int16_t>(y);
649
+ cmd->bw = static_cast<int16_t>(d);
650
+ cmd->bh = static_cast<int16_t>(d);
651
+ cmd->fillCircle.cx = static_cast<int16_t>(cx);
652
+ cmd->fillCircle.cy = static_cast<int16_t>(cy);
653
+ cmd->fillCircle.r = static_cast<int16_t>(r);
654
+ cmd->fillCircle.color = color;
655
+ }
656
+ if (effectiveAlpha != parentAlpha)
657
+ appendAlphaCommand(parentAlpha, x, y, d, d);
658
+ }
659
+
660
+ void appendStrokeWithAlpha(const Node &node, uint8_t parentAlpha)
661
+ {
662
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, node.style.border_alpha);
663
+ if (effectiveAlpha != parentAlpha)
664
+ appendAlphaCommand(effectiveAlpha, node.layout.x, node.layout.y, node.layout.width, node.layout.height);
665
+
666
+ DisplayCommand *cmd = DisplayList::instance().append();
667
+ if (cmd) {
668
+ // Canvas::strokeRect is deliberately the fast one-pixel primitive. Route
669
+ // wider square CSS borders through the width-aware rounded-rect rasterizer
670
+ // with zero radii instead of silently collapsing border-width to 1px.
671
+ if (hasAnyRadius(node) || node.style.border_width > 1) {
672
+ cmd->type = DisplayCommandType::StrokeRoundedRect;
673
+ cmd->bx = node.layout.x; cmd->by = node.layout.y; cmd->bw = node.layout.width; cmd->bh = node.layout.height;
674
+ cmd->strokeRoundedRect.x = node.layout.x; cmd->strokeRoundedRect.y = node.layout.y;
675
+ cmd->strokeRoundedRect.w = node.layout.width; cmd->strokeRoundedRect.h = node.layout.height;
676
+ cmd->strokeRoundedRect.tl = node.style.border_radius[0];
677
+ cmd->strokeRoundedRect.tr = node.style.border_radius[1];
678
+ cmd->strokeRoundedRect.br = node.style.border_radius[2];
679
+ cmd->strokeRoundedRect.bl = node.style.border_radius[3];
680
+ cmd->strokeRoundedRect.lineWidth = node.style.border_width;
681
+ cmd->strokeRoundedRect.color = node.style.border_color;
682
+ } else {
683
+ cmd->type = DisplayCommandType::StrokeRect;
684
+ cmd->bx = node.layout.x; cmd->by = node.layout.y; cmd->bw = node.layout.width; cmd->bh = node.layout.height;
685
+ cmd->stroke.x = node.layout.x; cmd->stroke.y = node.layout.y;
686
+ cmd->stroke.w = node.layout.width; cmd->stroke.h = node.layout.height;
687
+ cmd->stroke.color = node.style.border_color;
688
+ }
689
+ }
690
+
691
+ if (effectiveAlpha != parentAlpha)
692
+ appendAlphaCommand(parentAlpha, node.layout.x, node.layout.y, node.layout.width, node.layout.height);
693
+ }
694
+
695
+ void boundsFromCorners(const int16_t *xs, const int16_t *ys, int *x0, int *y0, int *x1, int *y1)
696
+ {
697
+ *x0 = *x1 = xs[0];
698
+ *y0 = *y1 = ys[0];
699
+ for (int i = 1; i < 4; i++) {
700
+ if (xs[i] < *x0) *x0 = xs[i];
701
+ if (xs[i] > *x1) *x1 = xs[i];
702
+ if (ys[i] < *y0) *y0 = ys[i];
703
+ if (ys[i] > *y1) *y1 = ys[i];
704
+ }
705
+ }
706
+
707
+ class ViewGeometry {
708
+ public:
709
+ // float (single-precision) so the per-corner transform math runs on the
710
+ // ESP32-S3 hardware FPU. double here is software-emulated and was ~60% of a
711
+ // spinning css-3d-cube frame (619ms of dlist record). float is sub-pixel
712
+ // accurate for screen coords (<= ~512px) through the short transform chain.
713
+ struct Point3 {
714
+ float x;
715
+ float y;
716
+ float z;
717
+ };
718
+
719
+ static int nodeIndex(const Node &node)
720
+ {
721
+ const Node *nodes = Tree::instance().nodes();
722
+ const auto index = &node - nodes;
723
+ return index >= 0 && index < Tree::instance().nodeCount() ? static_cast<int>(index) : -1;
724
+ }
725
+
726
+ static bool nodeHasLocalTransform(const Node &node, bool usePrevious)
727
+ {
728
+ const RareStyle &rs = rstyle(node.style); // one pool lookup, not 10
729
+ const int rotate = usePrevious ? node.render.previous_transform_rotate : rs.transform_rotate;
730
+ const int rotateX = usePrevious ? node.render.previous_transform_rotate_x : rs.transform_rotate_x;
731
+ const int rotateY = usePrevious ? node.render.previous_transform_rotate_y : rs.transform_rotate_y;
732
+ const int tx = usePrevious ? node.render.previous_transform_translate_x : rs.transform_translate_x;
733
+ const int ty = usePrevious ? node.render.previous_transform_translate_y : rs.transform_translate_y;
734
+ const int tz = usePrevious ? node.render.previous_transform_translate_z : rs.transform_translate_z;
735
+ const int txPercent = usePrevious ? node.render.previous_transform_translate_x_percent : rs.transform_translate_x_percent;
736
+ const int tyPercent = usePrevious ? node.render.previous_transform_translate_y_percent : rs.transform_translate_y_percent;
737
+ const int sx = usePrevious ? node.render.previous_transform_scale_x : rs.transform_scale_x;
738
+ const int sy = usePrevious ? node.render.previous_transform_scale_y : rs.transform_scale_y;
739
+ return (rotate % 3600) != 0 ||
740
+ (rotateX % 3600) != 0 ||
741
+ (rotateY % 3600) != 0 ||
742
+ tx != 0 ||
743
+ ty != 0 ||
744
+ tz != 0 ||
745
+ txPercent != 0 ||
746
+ tyPercent != 0 ||
747
+ sx != 1000 ||
748
+ sy != 1000;
749
+ }
750
+
751
+ // True iff any node carries a non-identity transform/perspective this frame
752
+ // or last frame. Cached per refresh (keyed on refreshSerial): the DOM doesn't
753
+ // change between refreshes, so one O(n) scan replaces the ancestor-chain walk
754
+ // that hasTransformChain() would otherwise run for every node, every
755
+ // recordBox. When no transform exists anywhere — the overwhelmingly common
756
+ // case (e.g. tilt-breakout: no transforms at all) — the entire keyframe-3D
757
+ // record path is bypassed at O(1).
758
+ static bool GEA_VIEW_HOT_SRAM_SECTION("any_transform_present") anyTransformPresent()
759
+ {
760
+ auto &state = treeState();
761
+ if (state.transformScanSerial == state.refreshSerial) return state.transformPresent;
762
+ // Durable no-transform cache: a transform-free tree stays transform-free until a
763
+ // transform property is set, and both setStyle(transform) sites clear
764
+ // transformScanValid. So a cached `false` holds across mid-refresh refreshSerial
765
+ // bumps with no re-scan — eliminating the scan-per-phase that made the mode gate
766
+ // regress. (A cached `true` is intentionally not held: re-scan per refreshSerial
767
+ // so a transform removal is noticed.)
768
+ if (state.transformScanValid && !state.transformPresent) return false;
769
+ bool present = false;
770
+ for (int i = 0; i < state.nodeCount; i++) {
771
+ const Node &n = state.nodes[i];
772
+ if (nodeHasLocalTransform(n, false) || nodeHasLocalTransform(n, true) ||
773
+ rstyle(n.style).perspective > 0 || n.render.previous_perspective > 0) {
774
+ present = true;
775
+ break;
776
+ }
777
+ }
778
+ state.transformPresent = present;
779
+ state.transformScanSerial = state.refreshSerial;
780
+ state.transformScanValid = true;
781
+ return present;
782
+ }
783
+
784
+ static bool GEA_VIEW_HOT_SRAM_SECTION("has_transform_chain") hasTransformChain(const Node &node, bool usePrevious)
785
+ {
786
+ if (!anyTransformPresent()) return false;
787
+ auto &state = treeState();
788
+ for (int id = nodeIndex(node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent) {
789
+ if (nodeHasLocalTransform(state.nodes[id], usePrevious)) return true;
790
+ const int perspective = usePrevious ? state.nodes[id].render.previous_perspective : rstyle(state.nodes[id].style).perspective;
791
+ if (perspective > 0) return true;
792
+ }
793
+ return false;
794
+ }
795
+
796
+ // Rotations take precomputed sin/cos (cached per node per frame by
797
+ // nodeRotation) so applyNodeTransform doesn't re-evaluate software sin/cos
798
+ // per corner per gradient strip — the dominant cost of rebuilding a
799
+ // transformed subtree's display list each frame.
800
+ static void rotateX(Point3 &p, float s, float c)
801
+ {
802
+ const float y = p.y * c - p.z * s;
803
+ const float z = p.y * s + p.z * c;
804
+ p.y = y;
805
+ p.z = z;
806
+ }
807
+
808
+ static void rotateY(Point3 &p, float s, float c)
809
+ {
810
+ const float x = p.x * c + p.z * s;
811
+ const float z = -p.x * s + p.z * c;
812
+ p.x = x;
813
+ p.z = z;
814
+ }
815
+
816
+ static void rotateZ(Point3 &p, float s, float c)
817
+ {
818
+ const float x = p.x * c - p.y * s;
819
+ const float y = p.x * s + p.y * c;
820
+ p.x = x;
821
+ p.y = y;
822
+ }
823
+
824
+ // Full per-node affine transform for the current frame, precomputed once and
825
+ // reused across every corner/strip projected through it.
826
+ struct NodeTransformCache {
827
+ #if GEA_EMBEDDED_UI_STATE_DYNAMIC_INIT
828
+ // Not inlined: the -1 node id would make the cache a .data object (see
829
+ // state_init.h).
830
+ __attribute__((noinline)) NodeTransformCache() {}
831
+ #endif
832
+ int nodeId = -1;
833
+ int x = 0, y = 0, w = 0, h = 0;
834
+ int rotate = 0, rotateX = 0, rotateY = 0;
835
+ int tx = 0, ty = 0, tz = 0, txPercent = 0, tyPercent = 0;
836
+ int sx = 1000, sy = 1000, originX = 500, originY = 500;
837
+ float ox = 0.0f, oy = 0.0f; // transform-origin in absolute coords
838
+ float scaleX = 1.0f, scaleY = 1.0f;
839
+ float transX = 0.0f, transY = 0.0f, transZ = 0.0f; // translate (incl % of size)
840
+ float sinZ = 0.0f, cosZ = 1.0f, sinY = 0.0f, cosY = 1.0f, sinX = 0.0f, cosX = 1.0f;
841
+ // Previous-frame coefficients (from render.previous_* / layout.previous_*),
842
+ // filled LAZILY on first dirty-bounds (previous-frame) access so the hot
843
+ // reproject path never pays for them. Lets the dirty-collect pass project
844
+ // previous-frame corners with the same pure-FPU cached apply as the current
845
+ // frame instead of re-walking the ancestor chain + recomputing 6 software
846
+ // trig per corner (the css-3d-cube dirty cost).
847
+ bool prevValid = false;
848
+ float pOx = 0.0f, pOy = 0.0f;
849
+ float pScaleX = 1.0f, pScaleY = 1.0f;
850
+ float pTransX = 0.0f, pTransY = 0.0f, pTransZ = 0.0f;
851
+ float pSinZ = 0.0f, pCosZ = 1.0f, pSinY = 0.0f, pCosY = 1.0f, pSinX = 0.0f, pCosX = 1.0f;
852
+ };
853
+
854
+ struct AverageDepthCacheEntry {
855
+ int nodeId = -1;
856
+ int depth = 0;
857
+ };
858
+
859
+ struct CornerTransformCacheEntry {
860
+ int nodeId = -1;
861
+ int16_t xs[4]{};
862
+ int16_t ys[4]{};
863
+ };
864
+
865
+ #ifndef GEA_EMBEDDED_UI_TRANSFORM_CACHE_SLOTS
866
+ #define GEA_EMBEDDED_UI_TRANSFORM_CACHE_SLOTS 64
867
+ #endif
868
+ #ifndef GEA_EMBEDDED_UI_DEPTH_CACHE_SLOTS
869
+ #define GEA_EMBEDDED_UI_DEPTH_CACHE_SLOTS 64
870
+ #endif
871
+ #ifndef GEA_EMBEDDED_UI_CORNER_CACHE_SLOTS
872
+ #define GEA_EMBEDDED_UI_CORNER_CACHE_SLOTS 64
873
+ #endif
874
+ static constexpr int kActiveTransformCacheSlots = GEA_EMBEDDED_UI_TRANSFORM_CACHE_SLOTS;
875
+ static constexpr int kActiveDepthCacheSlots = GEA_EMBEDDED_UI_DEPTH_CACHE_SLOTS;
876
+ static constexpr int kActiveCornerCacheSlots = GEA_EMBEDDED_UI_CORNER_CACHE_SLOTS;
877
+ static_assert(kActiveTransformCacheSlots > 0);
878
+ static_assert(kActiveDepthCacheSlots > 0);
879
+ static_assert(kActiveCornerCacheSlots > 0);
880
+ static_assert((kActiveTransformCacheSlots & (kActiveTransformCacheSlots - 1)) == 0);
881
+ static_assert((kActiveDepthCacheSlots & (kActiveDepthCacheSlots - 1)) == 0);
882
+ static_assert((kActiveCornerCacheSlots & (kActiveCornerCacheSlots - 1)) == 0);
883
+
884
+ struct ActiveTransformCache {
885
+ #if GEA_EMBEDDED_UI_STATE_DYNAMIC_INIT
886
+ // Not inlined: the all-ones serial would make the cache a .data object
887
+ // (see state_init.h).
888
+ __attribute__((noinline)) ActiveTransformCache() {}
889
+ #endif
890
+ uint64_t serial = ~0ull;
891
+ NodeTransformCache entries[kActiveTransformCacheSlots]{};
892
+ };
893
+
894
+ struct ActiveDepthCache {
895
+ uint64_t serial = ~0ull;
896
+ AverageDepthCacheEntry entries[kActiveDepthCacheSlots]{};
897
+ };
898
+
899
+ struct ActiveCornerCache {
900
+ uint64_t serial = ~0ull;
901
+ CornerTransformCacheEntry entries[kActiveCornerCacheSlots]{};
902
+ };
903
+
904
+ static void resetCache(NodeTransformCache *entries, int count)
905
+ {
906
+ for (int i = 0; i < count; i++) entries[i].nodeId = -1;
907
+ }
908
+
909
+ static void resetCache(AverageDepthCacheEntry *entries, int count)
910
+ {
911
+ for (int i = 0; i < count; i++) entries[i].nodeId = -1;
912
+ }
913
+
914
+ static void resetCache(CornerTransformCacheEntry *entries, int count)
915
+ {
916
+ for (int i = 0; i < count; i++) entries[i].nodeId = -1;
917
+ }
918
+
919
+ static void fillNodeTransform(NodeTransformCache &e, const Node &node, int id)
920
+ {
921
+ // Hoist the RareStyle pool lookup out of the per-field reads: rstyle() is an
922
+ // out-of-line std::deque indexing (segmented, double pointer-chase, cache
923
+ // miss on the S3). Calling it once per node instead of ~16x per fill is the
924
+ // difference between a direct-field read and a per-field deque probe — the
925
+ // transform hot path (reproject) re-fills changed nodes every frame.
926
+ const RareStyle &rs = rstyle(node.style);
927
+ e.x = node.layout.x;
928
+ e.y = node.layout.y;
929
+ e.w = node.layout.width;
930
+ e.h = node.layout.height;
931
+ e.rotate = rs.transform_rotate;
932
+ e.rotateX = rs.transform_rotate_x;
933
+ e.rotateY = rs.transform_rotate_y;
934
+ e.tx = rs.transform_translate_x;
935
+ e.ty = rs.transform_translate_y;
936
+ e.tz = rs.transform_translate_z;
937
+ e.txPercent = rs.transform_translate_x_percent;
938
+ e.tyPercent = rs.transform_translate_y_percent;
939
+ e.sx = rs.transform_scale_x;
940
+ e.sy = rs.transform_scale_y;
941
+ e.originX = rs.transform_origin_x;
942
+ e.originY = rs.transform_origin_y;
943
+ const float fw = static_cast<float>(node.layout.width);
944
+ const float fh = static_cast<float>(node.layout.height);
945
+ e.ox = static_cast<float>(node.layout.x) + fw * static_cast<float>(rs.transform_origin_x) * kInv1000f;
946
+ e.oy = static_cast<float>(node.layout.y) + fh * static_cast<float>(rs.transform_origin_y) * kInv1000f;
947
+ e.scaleX = static_cast<float>(rs.transform_scale_x) * kInv1000f;
948
+ e.scaleY = static_cast<float>(rs.transform_scale_y) * kInv1000f;
949
+ e.transX = static_cast<float>(rs.transform_translate_x) +
950
+ fw * static_cast<float>(rs.transform_translate_x_percent) * kInv1000f;
951
+ e.transY = static_cast<float>(rs.transform_translate_y) +
952
+ fh * static_cast<float>(rs.transform_translate_y_percent) * kInv1000f;
953
+ e.transZ = static_cast<float>(rs.transform_translate_z);
954
+ const float az = static_cast<float>(rs.transform_rotate) * kPiF * kInv1800f;
955
+ const float ay = static_cast<float>(rs.transform_rotate_y) * kPiF * kInv1800f;
956
+ const float ax = static_cast<float>(rs.transform_rotate_x) * kPiF * kInv1800f;
957
+ e.sinZ = sinf(az); e.cosZ = cosf(az);
958
+ e.sinY = sinf(ay); e.cosY = cosf(ay);
959
+ e.sinX = sinf(ax); e.cosX = cosf(ax);
960
+ // Previous-frame coefficients are filled lazily (only the dirty-bounds pass
961
+ // needs them); mark invalid so the first prev access recomputes for this id.
962
+ e.prevValid = false;
963
+ e.nodeId = id;
964
+ }
965
+
966
+ // Previous-frame analogue of fillNodeTransform: precompute one node's transform
967
+ // coefficients from its SNAPSHOTTED previous-frame fields (render.previous_* +
968
+ // layout.previous_*). Lets transformRectCorners' previous-frame path gather the
969
+ // chain ONCE and apply pure-FPU to all 4 corners — instead of re-walking the
970
+ // ancestor chain and recomputing 6 software trig PER CORNER (the css-3d-cube
971
+ // dirty-collect's dominant cost: ~280 applyNodeTransform calls/frame). The math
972
+ // mirrors applyNodeTransform's usePrevious branch exactly, so the projected
973
+ // corners are bit-identical — no visual change.
974
+ static void fillNodeTransformPrev(NodeTransformCache &e, const Node &node)
975
+ {
976
+ const int x = node.layout.previous_x;
977
+ const int y = node.layout.previous_y;
978
+ const int w = node.layout.previous_width;
979
+ const int h = node.layout.previous_height;
980
+ const int rotate = node.render.previous_transform_rotate;
981
+ const int rotateXValue = node.render.previous_transform_rotate_x;
982
+ const int rotateYValue = node.render.previous_transform_rotate_y;
983
+ const int tx = node.render.previous_transform_translate_x;
984
+ const int ty = node.render.previous_transform_translate_y;
985
+ const int tz = node.render.previous_transform_translate_z;
986
+ const int txPercent = node.render.previous_transform_translate_x_percent;
987
+ const int tyPercent = node.render.previous_transform_translate_y_percent;
988
+ const int sx = node.render.previous_transform_scale_x;
989
+ const int sy = node.render.previous_transform_scale_y;
990
+ const int originX = node.render.previous_transform_origin_x;
991
+ const int originY = node.render.previous_transform_origin_y;
992
+ const float fw = static_cast<float>(w);
993
+ const float fh = static_cast<float>(h);
994
+ e.pOx = static_cast<float>(x) + fw * static_cast<float>(originX) * kInv1000f;
995
+ e.pOy = static_cast<float>(y) + fh * static_cast<float>(originY) * kInv1000f;
996
+ e.pScaleX = static_cast<float>(sx) * kInv1000f;
997
+ e.pScaleY = static_cast<float>(sy) * kInv1000f;
998
+ e.pTransX = static_cast<float>(tx) + fw * static_cast<float>(txPercent) * kInv1000f;
999
+ e.pTransY = static_cast<float>(ty) + fh * static_cast<float>(tyPercent) * kInv1000f;
1000
+ e.pTransZ = static_cast<float>(tz);
1001
+ const float az = static_cast<float>(rotate) * kPiF * kInv1800f;
1002
+ const float ay = static_cast<float>(rotateYValue) * kPiF * kInv1800f;
1003
+ const float ax = static_cast<float>(rotateXValue) * kPiF * kInv1800f;
1004
+ e.pSinZ = sinf(az); e.pCosZ = cosf(az);
1005
+ e.pSinY = sinf(ay); e.pCosY = cosf(ay);
1006
+ e.pSinX = sinf(ax); e.pCosX = cosf(ax);
1007
+ }
1008
+
1009
+ [[maybe_unused]] static bool nodeTransformMatches(const NodeTransformCache &e, const Node &node, int id)
1010
+ {
1011
+ // One RareStyle pool lookup, not one per compared field (see fillNodeTransform).
1012
+ const RareStyle &rs = rstyle(node.style);
1013
+ return e.nodeId == id &&
1014
+ e.x == node.layout.x &&
1015
+ e.y == node.layout.y &&
1016
+ e.w == node.layout.width &&
1017
+ e.h == node.layout.height &&
1018
+ e.rotate == rs.transform_rotate &&
1019
+ e.rotateX == rs.transform_rotate_x &&
1020
+ e.rotateY == rs.transform_rotate_y &&
1021
+ e.tx == rs.transform_translate_x &&
1022
+ e.ty == rs.transform_translate_y &&
1023
+ e.tz == rs.transform_translate_z &&
1024
+ e.txPercent == rs.transform_translate_x_percent &&
1025
+ e.tyPercent == rs.transform_translate_y_percent &&
1026
+ e.sx == rs.transform_scale_x &&
1027
+ e.sy == rs.transform_scale_y &&
1028
+ e.originX == rs.transform_origin_x &&
1029
+ e.originY == rs.transform_origin_y;
1030
+ }
1031
+
1032
+ static NodeTransformCache *cachedNodeTransform(const Node &node)
1033
+ {
1034
+ static ActiveTransformCache cache;
1035
+ const int id = nodeIndex(node);
1036
+ if (id < 0) return nullptr;
1037
+ const uint64_t serial = treeState().refreshSerial;
1038
+ if (cache.serial != serial) {
1039
+ resetCache(cache.entries, kActiveTransformCacheSlots);
1040
+ cache.serial = serial;
1041
+ }
1042
+ NodeTransformCache &e = cache.entries[id & (kActiveTransformCacheSlots - 1)];
1043
+ // The cache was reset above on a serial change, and the STYLE half of a
1044
+ // transform is fixed within a refresh serial (the serial bumps whenever the
1045
+ // tree re-records / a style mutates). So a matching id lets us skip the
1046
+ // per-call nodeTransformMatches style comparison (an rstyle() pool lookup + 16
1047
+ // field compares against the scattered RareStyle entry) that the reproject
1048
+ // otherwise pays ~72x/frame (12 commands x ~6 ancestors) — the dominant
1049
+ // residual reproject cost vs spine's inline-ComputedStyle reads.
1050
+ //
1051
+ // The LAYOUT half is not covered by that argument: transform-origin and
1052
+ // percentage translate are resolved against the node's own box, and layout can
1053
+ // change without bumping the refresh serial (layout runs inside a refresh, and
1054
+ // callers such as hit-testing read geometry between refreshes). A stale ox/oy/
1055
+ // transX/transY silently projects the current rect through the PREVIOUS box's
1056
+ // coefficients — e.g. translate(-50%,-50%) of a 480x16 button still shifting by
1057
+ // (-240,-8) after the button became 47x47, so hit-testing answered for a rect
1058
+ // that is nowhere on screen. Four int compares against fields the caller has
1059
+ // already touched (node.layout is hot) is cheap next to the deque probe, and it
1060
+ // keeps the fast path honest.
1061
+ if (e.nodeId == id) {
1062
+ if (e.x == node.layout.x && e.y == node.layout.y &&
1063
+ e.w == node.layout.width && e.h == node.layout.height)
1064
+ return &e;
1065
+ fillNodeTransform(e, node, id);
1066
+ return &e;
1067
+ }
1068
+ if (e.nodeId != -1) return nullptr;
1069
+ fillNodeTransform(e, node, id);
1070
+ return &e;
1071
+ }
1072
+
1073
+ // Current-frame transform coefficients for a node, computed once per refresh
1074
+ // and reused across every corner/strip projected through it.
1075
+ // transformRectCorners runs 4 corners x N strips per node (e.g. 4x72 for a
1076
+ // gradient face), and EACH applyNodeTransform previously recomputed the origin/
1077
+ // scale/translate (6 software __divsf3 — the ESP32-S3 FPU has no divide) plus 3
1078
+ // software sin/cos. That per-call arithmetic was the dominant cost of rebuilding
1079
+ // a transformed subtree's display list (measured ~38ms / ~2300 calls per spinning
1080
+ // css-3d-cube frame). Precomputing it per node collapses the hot path to ~12
1081
+ // hardware FPU mul/adds + a cache lookup. Keyed on refreshSerial: the transform
1082
+ // is fixed within a refresh, and the serial bumps when the tree re-records (the
1083
+ // animating cube each frame). Only the current frame is cached; the colder
1084
+ // previous-frame (dirty-bounds) path computes directly.
1085
+ static const NodeTransformCache &nodeTransform(const Node &node)
1086
+ {
1087
+ if (NodeTransformCache *e = cachedNodeTransform(node)) return *e;
1088
+ static NodeTransformCache fallback;
1089
+ fillNodeTransform(fallback, node, nodeIndex(node));
1090
+ return fallback;
1091
+ }
1092
+
1093
+ // Pure hardware-FPU application of precomputed transform coefficients to a
1094
+ // point — no cache lookup, no divides, no trig. transformRectCorners hoists
1095
+ // the per-node cache lookup out of its 4-corner loop and calls this directly.
1096
+ static void applyCachedTransform(const NodeTransformCache &t, Point3 &p)
1097
+ {
1098
+ p.x -= t.ox;
1099
+ p.y -= t.oy;
1100
+ p.x *= t.scaleX;
1101
+ p.y *= t.scaleY;
1102
+ p.x += t.transX;
1103
+ p.y += t.transY;
1104
+ p.z += t.transZ;
1105
+ rotateZ(p, t.sinZ, t.cosZ);
1106
+ rotateY(p, t.sinY, t.cosY);
1107
+ rotateX(p, t.sinX, t.cosX);
1108
+ p.x += t.ox;
1109
+ p.y += t.oy;
1110
+ }
1111
+
1112
+ // Previous-frame analogue of applyCachedTransform — pure-FPU application of the
1113
+ // lazily-cached previous-frame coefficients. Identical math to applyNodeTransform's
1114
+ // usePrevious branch, so the projected corner is bit-identical (no visual change).
1115
+ static void applyCachedTransformPrev(const NodeTransformCache &t, Point3 &p)
1116
+ {
1117
+ p.x -= t.pOx;
1118
+ p.y -= t.pOy;
1119
+ p.x *= t.pScaleX;
1120
+ p.y *= t.pScaleY;
1121
+ p.x += t.pTransX;
1122
+ p.y += t.pTransY;
1123
+ p.z += t.pTransZ;
1124
+ rotateZ(p, t.pSinZ, t.pCosZ);
1125
+ rotateY(p, t.pSinY, t.pCosY);
1126
+ rotateX(p, t.pSinX, t.pCosX);
1127
+ p.x += t.pOx;
1128
+ p.y += t.pOy;
1129
+ }
1130
+
1131
+ // Gather the ancestor chain's cached transform coefficients for the current
1132
+ // frame, leaf-first. Returns the chain length, or -1 if it exceeds `cap` (the
1133
+ // caller then falls back to the per-ancestor walk). Hoisting this out of a
1134
+ // multi-point projection loop turns N points x chain cache-lookups into one
1135
+ // chain gather + pure-FPU application per point.
1136
+ static constexpr int kMaxChainDepth = 64; // DOM nesting is shallow; far beyond any real tree
1137
+ static int GEA_VIEW_HOT_SRAM_SECTION("gather_transform_chain") gatherTransformChain(const Node &node, const NodeTransformCache **chain)
1138
+ {
1139
+ auto &state = treeState();
1140
+ int depth = 0;
1141
+ for (int id = nodeIndex(node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent) {
1142
+ if (depth >= kMaxChainDepth) return -1;
1143
+ NodeTransformCache *entry = cachedNodeTransform(state.nodes[id]);
1144
+ if (!entry) return -1;
1145
+ chain[depth++] = entry;
1146
+ }
1147
+ return depth;
1148
+ }
1149
+
1150
+ // Previous-frame analogue of gatherTransformChain: gathers the same ancestor
1151
+ // chain, lazily filling each node's previous-frame coefficients on first access.
1152
+ // The dirty-bounds pass projects previous-frame corners through this so the
1153
+ // previous-frame projection is O(nodes) cached pure-FPU — matching the current
1154
+ // frame — instead of re-walking + recomputing 6 software trig per corner.
1155
+ static int gatherTransformChainPrev(const Node &node, const NodeTransformCache **chain)
1156
+ {
1157
+ auto &state = treeState();
1158
+ int depth = 0;
1159
+ for (int id = nodeIndex(node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent) {
1160
+ if (depth >= kMaxChainDepth) return -1;
1161
+ NodeTransformCache *entry = cachedNodeTransform(state.nodes[id]);
1162
+ if (!entry) return -1;
1163
+ if (!entry->prevValid) {
1164
+ fillNodeTransformPrev(*entry, state.nodes[id]);
1165
+ entry->prevValid = true;
1166
+ }
1167
+ chain[depth++] = entry;
1168
+ }
1169
+ return depth;
1170
+ }
1171
+
1172
+ static void applyNodeTransform(const Node &node, bool usePrevious, Point3 &p)
1173
+ {
1174
+ // Hot path (current frame): use the per-node precomputed coefficients so
1175
+ // the projection is pure hardware-FPU mul/add — no software divides, no
1176
+ // int->float conversions, no per-call trig.
1177
+ if (!usePrevious) {
1178
+ applyCachedTransform(nodeTransform(node), p);
1179
+ return;
1180
+ }
1181
+
1182
+ // Cold path (previous frame): only the dirty-region bounds pass needs this,
1183
+ // and previous-frame fields aren't cached, so compute inline.
1184
+ const int x = node.layout.previous_x;
1185
+ const int y = node.layout.previous_y;
1186
+ const int w = node.layout.previous_width;
1187
+ const int h = node.layout.previous_height;
1188
+ const int rotate = node.render.previous_transform_rotate;
1189
+ const int rotateXValue = node.render.previous_transform_rotate_x;
1190
+ const int rotateYValue = node.render.previous_transform_rotate_y;
1191
+ const int tx = node.render.previous_transform_translate_x;
1192
+ const int ty = node.render.previous_transform_translate_y;
1193
+ const int tz = node.render.previous_transform_translate_z;
1194
+ const int txPercent = node.render.previous_transform_translate_x_percent;
1195
+ const int tyPercent = node.render.previous_transform_translate_y_percent;
1196
+ const int sx = node.render.previous_transform_scale_x;
1197
+ const int sy = node.render.previous_transform_scale_y;
1198
+ const int originX = node.render.previous_transform_origin_x;
1199
+ const int originY = node.render.previous_transform_origin_y;
1200
+
1201
+ const float ox = static_cast<float>(x) + static_cast<float>(w) * static_cast<float>(originX) * kInv1000f;
1202
+ const float oy = static_cast<float>(y) + static_cast<float>(h) * static_cast<float>(originY) * kInv1000f;
1203
+ p.x -= ox;
1204
+ p.y -= oy;
1205
+ p.x *= static_cast<float>(sx) * kInv1000f;
1206
+ p.y *= static_cast<float>(sy) * kInv1000f;
1207
+ p.x += static_cast<float>(tx) + static_cast<float>(w) * static_cast<float>(txPercent) * kInv1000f;
1208
+ p.y += static_cast<float>(ty) + static_cast<float>(h) * static_cast<float>(tyPercent) * kInv1000f;
1209
+ p.z += tz;
1210
+ rotateZ(p, sinf(static_cast<float>(rotate) * kPiF * kInv1800f), cosf(static_cast<float>(rotate) * kPiF * kInv1800f));
1211
+ rotateY(p, sinf(static_cast<float>(rotateYValue) * kPiF * kInv1800f), cosf(static_cast<float>(rotateYValue) * kPiF * kInv1800f));
1212
+ rotateX(p, sinf(static_cast<float>(rotateXValue) * kPiF * kInv1800f), cosf(static_cast<float>(rotateXValue) * kPiF * kInv1800f));
1213
+ p.x += ox;
1214
+ p.y += oy;
1215
+ }
1216
+
1217
+ static int nearestPerspectiveNode(const Node &node, bool usePrevious)
1218
+ {
1219
+ if (!anyTransformPresent()) return -1;
1220
+ auto &state = treeState();
1221
+ for (int id = nodeIndex(node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent) {
1222
+ const int perspective = usePrevious ? state.nodes[id].render.previous_perspective : rstyle(state.nodes[id].style).perspective;
1223
+ if (perspective > 0) return id;
1224
+ }
1225
+ return -1;
1226
+ }
1227
+
1228
+ static void applyPerspective(int perspectiveNode, bool usePrevious, Point3 &p)
1229
+ {
1230
+ if (perspectiveNode < 0) return;
1231
+ const Node &node = treeState().nodes[perspectiveNode];
1232
+ const int x = usePrevious ? node.layout.previous_x : node.layout.x;
1233
+ const int y = usePrevious ? node.layout.previous_y : node.layout.y;
1234
+ const int w = usePrevious ? node.layout.previous_width : node.layout.width;
1235
+ const int h = usePrevious ? node.layout.previous_height : node.layout.height;
1236
+ const RareStyle &rs = rstyle(node.style); // one pool lookup, not 3
1237
+ const int perspective = usePrevious ? node.render.previous_perspective : rs.perspective;
1238
+ const int originX = usePrevious ? node.render.previous_perspective_origin_x : rs.perspective_origin_x;
1239
+ const int originY = usePrevious ? node.render.previous_perspective_origin_y : rs.perspective_origin_y;
1240
+ if (perspective <= 0) return;
1241
+ const float poX = static_cast<float>(x) + static_cast<float>(w) * static_cast<float>(originX) * kInv1000f;
1242
+ const float poY = static_cast<float>(y) + static_cast<float>(h) * static_cast<float>(originY) * kInv1000f;
1243
+ float scale = static_cast<float>(perspective) / (static_cast<float>(perspective) - p.z);
1244
+ if (!std::isfinite(scale)) scale = 1.0f;
1245
+ if (scale < 0.05f) scale = 0.05f;
1246
+ if (scale > 8.0f) scale = 8.0f;
1247
+ p.x = poX + (p.x - poX) * scale;
1248
+ p.y = poY + (p.y - poY) * scale;
1249
+ }
1250
+
1251
+ // Optional xs8/ys8 receive the same corners in 1/8-px fixed point (sub-pixel),
1252
+ // so adjacent faces' shared edges coincide and the rasterizer tiles them
1253
+ // watertight instead of cracking at int16 round-to-nearest straddles. Clamped to
1254
+ // int16 range (off-screen corners are clipped by the rasterizer regardless).
1255
+ static void GEA_VIEW_HOT_SRAM_SECTION("transform_rect_corners") transformRectCorners(const Node &node, bool usePrevious, int x, int y, int w, int h, int16_t *xs, int16_t *ys,
1256
+ int16_t *xs8 = nullptr, int16_t *ys8 = nullptr)
1257
+ {
1258
+ Point3 points[4] = {
1259
+ {(float)x, (float)y, 0.0f},
1260
+ {(float)(x + w), (float)y, 0.0f},
1261
+ {(float)(x + w), (float)(y + h), 0.0f},
1262
+ {(float)x, (float)(y + h), 0.0f},
1263
+ };
1264
+ auto fx8 = [](float v) -> int16_t {
1265
+ long s = std::lroundf(v * 8.0f);
1266
+ return (int16_t)(s < -32768 ? -32768 : (s > 32767 ? 32767 : s));
1267
+ };
1268
+ auto &state = treeState();
1269
+ const int perspectiveNode = nearestPerspectiveNode(node, usePrevious);
1270
+ const NodeTransformCache *chain[kMaxChainDepth];
1271
+ // Both frames now gather the chain ONCE and apply pure-FPU cached coefficients
1272
+ // to all 4 corners. The previous-frame path (dirty-bounds collection) used to
1273
+ // re-walk the ancestor chain and recompute 6 software trig PER CORNER — the
1274
+ // dominant cost of the css-3d-cube dirty pass (~280 applyNodeTransform calls/
1275
+ // frame). It now matches the current frame: O(nodes) cached, O(1) per corner.
1276
+ const int depth = usePrevious ? gatherTransformChainPrev(node, chain) : gatherTransformChain(node, chain);
1277
+ if (depth >= 0) {
1278
+ // Apply the gathered chain to all 4 corners with pure FPU math — the
1279
+ // cache lookup happened once during the gather, not per corner.
1280
+ for (int i = 0; i < 4; i++) {
1281
+ for (int k = 0; k < depth; k++) {
1282
+ if (usePrevious)
1283
+ applyCachedTransformPrev(*chain[k], points[i]);
1284
+ else
1285
+ applyCachedTransform(*chain[k], points[i]);
1286
+ }
1287
+ applyPerspective(perspectiveNode, usePrevious, points[i]);
1288
+ xs[i] = (int16_t)std::lroundf(points[i].x);
1289
+ ys[i] = (int16_t)std::lroundf(points[i].y);
1290
+ if (xs8) { xs8[i] = fx8(points[i].x); ys8[i] = fx8(points[i].y); }
1291
+ }
1292
+ return;
1293
+ }
1294
+ // Pathologically deep chain (gather returned -1): per-corner walk fallback.
1295
+ for (int i = 0; i < 4; i++) {
1296
+ for (int id = nodeIndex(node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent)
1297
+ applyNodeTransform(state.nodes[id], usePrevious, points[i]);
1298
+ applyPerspective(perspectiveNode, usePrevious, points[i]);
1299
+ xs[i] = (int16_t)std::lroundf(points[i].x);
1300
+ ys[i] = (int16_t)std::lroundf(points[i].y);
1301
+ if (xs8) { xs8[i] = fx8(points[i].x); ys8[i] = fx8(points[i].y); }
1302
+ }
1303
+ }
1304
+
1305
+ static void transformPoint(const Node &node, bool usePrevious, float x, float y, float z, int16_t *outX, int16_t *outY)
1306
+ {
1307
+ Point3 point{x, y, z};
1308
+ auto &state = treeState();
1309
+ const int perspectiveNode = nearestPerspectiveNode(node, usePrevious);
1310
+ for (int id = nodeIndex(node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent)
1311
+ applyNodeTransform(state.nodes[id], usePrevious, point);
1312
+ applyPerspective(perspectiveNode, usePrevious, point);
1313
+ *outX = static_cast<int16_t>(std::lroundf(point.x));
1314
+ *outY = static_cast<int16_t>(std::lroundf(point.y));
1315
+ }
1316
+
1317
+ // Prepared projector for plotting MANY points through one node's transform
1318
+ // (e.g. an ellipse-stroke ring's ~60 segment vertices). Gathers the ancestor
1319
+ // chain's cached coefficients ONCE; each projectPoint is then pure FPU math
1320
+ // instead of re-walking the chain + cache lookup per point (current frame only).
1321
+ struct ChainProjector {
1322
+ const NodeTransformCache *chain[kMaxChainDepth];
1323
+ int depth = -1; // -1 => chain too deep, fall back to per-point walk
1324
+ int perspectiveNode = -1;
1325
+ const Node *node = nullptr;
1326
+ };
1327
+ static ChainProjector GEA_VIEW_HOT_SRAM_SECTION("prepare_projector") prepareProjector(const Node &node)
1328
+ {
1329
+ ChainProjector pr;
1330
+ pr.node = &node;
1331
+ pr.perspectiveNode = nearestPerspectiveNode(node, false);
1332
+ pr.depth = gatherTransformChain(node, pr.chain);
1333
+ return pr;
1334
+ }
1335
+ static void GEA_VIEW_HOT_SRAM_SECTION("project_point") projectPoint(const ChainProjector &pr, float x, float y, float z, int16_t *outX, int16_t *outY)
1336
+ {
1337
+ Point3 p{x, y, z};
1338
+ if (pr.depth >= 0) {
1339
+ for (int k = 0; k < pr.depth; k++) applyCachedTransform(*pr.chain[k], p);
1340
+ } else {
1341
+ auto &state = treeState();
1342
+ for (int id = nodeIndex(*pr.node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent)
1343
+ applyNodeTransform(state.nodes[id], false, p);
1344
+ }
1345
+ applyPerspective(pr.perspectiveNode, false, p);
1346
+ *outX = static_cast<int16_t>(std::lroundf(p.x));
1347
+ *outY = static_cast<int16_t>(std::lroundf(p.y));
1348
+ }
1349
+
1350
+ static int GEA_VIEW_HOT_SRAM_SECTION("average_depth_compute") averageDepthCompute(const Node &node, bool usePrevious)
1351
+ {
1352
+ const int x = usePrevious ? node.layout.previous_x : node.layout.x;
1353
+ const int y = usePrevious ? node.layout.previous_y : node.layout.y;
1354
+ const int w = usePrevious ? node.layout.previous_width : node.layout.width;
1355
+ const int h = usePrevious ? node.layout.previous_height : node.layout.height;
1356
+ const float fx = static_cast<float>(x), fy = static_cast<float>(y);
1357
+ const float fw = static_cast<float>(w), fh = static_cast<float>(h);
1358
+ Point3 points[5] = {
1359
+ {fx, fy, 0.0f},
1360
+ {fx + fw, fy, 0.0f},
1361
+ {fx + fw, fy + fh, 0.0f},
1362
+ {fx, fy + fh, 0.0f},
1363
+ {fx + fw * 0.5f, fy + fh * 0.5f, 0.0f},
1364
+ };
1365
+ // Gather the ancestor chain once and apply to all 5 depth-probe points with
1366
+ // pure FPU math (same hoist as transformRectCorners). This is the z-sort's
1367
+ // hot path — without it each point re-did a cache lookup per ancestor.
1368
+ const NodeTransformCache *chain[kMaxChainDepth];
1369
+ const int depth = usePrevious ? gatherTransformChainPrev(node, chain) : gatherTransformChain(node, chain);
1370
+ if (depth >= 0) {
1371
+ for (int i = 0; i < 5; i++)
1372
+ for (int k = 0; k < depth; k++) {
1373
+ if (usePrevious)
1374
+ applyCachedTransformPrev(*chain[k], points[i]);
1375
+ else
1376
+ applyCachedTransform(*chain[k], points[i]);
1377
+ }
1378
+ } else {
1379
+ auto &state = treeState();
1380
+ for (int i = 0; i < 5; i++)
1381
+ for (int id = nodeIndex(node); id >= 0 && id < state.nodeCount; id = state.nodes[id].parent)
1382
+ applyNodeTransform(state.nodes[id], usePrevious, points[i]);
1383
+ }
1384
+ float sum = 0.0f;
1385
+ for (const Point3 &p : points) sum += p.z;
1386
+ return static_cast<int>(std::lroundf(sum * 10.0f / 5.0f));
1387
+ }
1388
+
1389
+ // Painter's-depth for the current frame, cached per node per refresh. The
1390
+ // child z-sort's subtreeDepth() recurses every subtree at every tree level, so
1391
+ // without this each node's 5-projection depth was recomputed many times per
1392
+ // frame (the dominant ~16ms 'sort' cost on css-3d-cube). The previous-frame
1393
+ // path isn't cached (cold, used only for dirty bounds).
1394
+ static int GEA_VIEW_HOT_SRAM_SECTION("average_depth") averageDepth(const Node &node, bool usePrevious)
1395
+ {
1396
+ if (usePrevious) return averageDepthCompute(node, true);
1397
+ static ActiveDepthCache cache;
1398
+ const int id = nodeIndex(node);
1399
+ const uint64_t serial = treeState().refreshSerial;
1400
+ if (cache.serial != serial) {
1401
+ resetCache(cache.entries, kActiveDepthCacheSlots);
1402
+ cache.serial = serial;
1403
+ }
1404
+ if (id >= 0) {
1405
+ AverageDepthCacheEntry &e = cache.entries[id & (kActiveDepthCacheSlots - 1)];
1406
+ if (e.nodeId == id) return e.depth;
1407
+ if (e.nodeId != -1) return averageDepthCompute(node, false);
1408
+ const int d = averageDepthCompute(node, false);
1409
+ e.depth = d;
1410
+ e.nodeId = id;
1411
+ return d;
1412
+ }
1413
+ return averageDepthCompute(node, false);
1414
+ }
1415
+
1416
+ // The node's four projected screen-space corners for the current frame, cached
1417
+ // per node per refresh. recordBox projects the same node rect for its bounds
1418
+ // AND its fill quad, and nodeOverlapsClip projects it a third time — caching
1419
+ // collapses those (and the dirty pass's current-frame half) to one projection.
1420
+ static void GEA_VIEW_HOT_SRAM_SECTION("transform_corners") transformCorners(const Node &node, bool usePrevious, int16_t *xs, int16_t *ys)
1421
+ {
1422
+ if (usePrevious) {
1423
+ // Cache previous-frame corners per node per refresh, mirroring the current-frame
1424
+ // path below. The dirty-bounds collect projects each node's previous corners
1425
+ // REDUNDANTLY — once while walking the dynamic container's whole subtree
1426
+ // (transformedSubtreeBoundsRect) and again when the child is visited as its own
1427
+ // dirty node — so without this the spinning cube re-projects every face/label's
1428
+ // previous corners 2-3x/frame. One cache lookup collapses that to once.
1429
+ static ActiveCornerCache prevCache;
1430
+ const int pid = nodeIndex(node);
1431
+ const uint64_t pserial = treeState().refreshSerial;
1432
+ if (prevCache.serial != pserial) {
1433
+ resetCache(prevCache.entries, kActiveCornerCacheSlots);
1434
+ prevCache.serial = pserial;
1435
+ }
1436
+ if (pid >= 0) {
1437
+ CornerTransformCacheEntry &e = prevCache.entries[pid & (kActiveCornerCacheSlots - 1)];
1438
+ if (e.nodeId != pid) {
1439
+ if (e.nodeId != -1) {
1440
+ // Slot collision with another node this refresh: compute uncached.
1441
+ transformRectCorners(node, true, node.layout.previous_x, node.layout.previous_y,
1442
+ node.layout.previous_width, node.layout.previous_height, xs, ys);
1443
+ return;
1444
+ }
1445
+ transformRectCorners(node, true, node.layout.previous_x, node.layout.previous_y,
1446
+ node.layout.previous_width, node.layout.previous_height, e.xs, e.ys);
1447
+ e.nodeId = pid;
1448
+ }
1449
+ for (int i = 0; i < 4; i++) { xs[i] = e.xs[i]; ys[i] = e.ys[i]; }
1450
+ return;
1451
+ }
1452
+ transformRectCorners(node, true, node.layout.previous_x, node.layout.previous_y,
1453
+ node.layout.previous_width, node.layout.previous_height, xs, ys);
1454
+ return;
1455
+ }
1456
+ static ActiveCornerCache cache;
1457
+ const int id = nodeIndex(node);
1458
+ const uint64_t serial = treeState().refreshSerial;
1459
+ if (cache.serial != serial) {
1460
+ resetCache(cache.entries, kActiveCornerCacheSlots);
1461
+ cache.serial = serial;
1462
+ }
1463
+ if (id >= 0) {
1464
+ CornerTransformCacheEntry &e = cache.entries[id & (kActiveCornerCacheSlots - 1)];
1465
+ if (e.nodeId != id) {
1466
+ if (e.nodeId != -1) {
1467
+ transformRectCorners(node, false, node.layout.x, node.layout.y,
1468
+ node.layout.width, node.layout.height, xs, ys);
1469
+ return;
1470
+ }
1471
+ transformRectCorners(node, false, node.layout.x, node.layout.y,
1472
+ node.layout.width, node.layout.height, e.xs, e.ys);
1473
+ e.nodeId = id;
1474
+ }
1475
+ for (int i = 0; i < 4; i++) { xs[i] = e.xs[i]; ys[i] = e.ys[i]; }
1476
+ return;
1477
+ }
1478
+ transformRectCorners(node, false, node.layout.x, node.layout.y,
1479
+ node.layout.width, node.layout.height, xs, ys);
1480
+ }
1481
+ };
1482
+
1483
+ void appendSideBordersWithAlpha(const Node &node, uint8_t parentAlpha)
1484
+ {
1485
+ const int x = node.layout.x;
1486
+ const int y = node.layout.y;
1487
+ const int w = node.layout.width;
1488
+ const int h = node.layout.height;
1489
+ if (w <= 0 || h <= 0 || !hasSideBorder(node.style)) return;
1490
+
1491
+ const bool transformed = ViewGeometry::hasTransformChain(node, false);
1492
+ for (int side = 0; side < 4; ++side) {
1493
+ const int borderWidth = rstyle(node.style).border_side_width[side];
1494
+ if (borderWidth <= 0) continue;
1495
+ int sx = x;
1496
+ int sy = y;
1497
+ int sw = w;
1498
+ int sh = h;
1499
+ if (side == 0) {
1500
+ sh = std::min(borderWidth, h);
1501
+ } else if (side == 1) {
1502
+ sw = std::min(borderWidth, w);
1503
+ sx = x + w - sw;
1504
+ } else if (side == 2) {
1505
+ sh = std::min(borderWidth, h);
1506
+ sy = y + h - sh;
1507
+ } else {
1508
+ sw = std::min(borderWidth, w);
1509
+ }
1510
+ if (sw <= 0 || sh <= 0) continue;
1511
+ if (transformed) {
1512
+ int16_t xs[4], ys[4];
1513
+ ViewGeometry::transformRectCorners(node, false, sx, sy, sw, sh, xs, ys);
1514
+ int bx0, by0, bx1, by1;
1515
+ boundsFromCorners(xs, ys, &bx0, &by0, &bx1, &by1);
1516
+ appendFillQuadWithAlpha(xs,
1517
+ ys,
1518
+ rstyle(node.style).border_side_color[side],
1519
+ rstyle(node.style).border_side_alpha[side],
1520
+ parentAlpha,
1521
+ bx0,
1522
+ by0,
1523
+ bx1 - bx0 + 1,
1524
+ by1 - by0 + 1,
1525
+ sx,
1526
+ sy,
1527
+ sw,
1528
+ sh,
1529
+ true);
1530
+ } else {
1531
+ appendFillRectWithAlpha(sx,
1532
+ sy,
1533
+ sw,
1534
+ sh,
1535
+ rstyle(node.style).border_side_color[side],
1536
+ rstyle(node.style).border_side_alpha[side],
1537
+ parentAlpha,
1538
+ sx,
1539
+ sy,
1540
+ sw,
1541
+ sh);
1542
+ }
1543
+ }
1544
+ }
1545
+
1546
+ void recordLinearGradientBackground(const Node &node, uint8_t parentAlpha)
1547
+ {
1548
+ const int x = node.layout.x;
1549
+ const int y = node.layout.y;
1550
+ const int w = node.layout.width;
1551
+ const int h = node.layout.height;
1552
+ if (w <= 0 || h <= 0) return;
1553
+
1554
+ if (!ViewGeometry::hasTransformChain(node, false)) {
1555
+ appendLinearGradientRectRaw(node, x, y, w, h);
1556
+ return;
1557
+ }
1558
+
1559
+ // Transformed (3D/perspective) face: emit ONE per-pixel-shaded gradient quad
1560
+ // instead of ~72 solid-color strips. The drawer inverse-maps each screen pixel
1561
+ // to face-local space and samples the gradient directly — exact (no banding),
1562
+ // alpha-correct, and 1 command vs 72 (the strips were ~575 quads/frame on the
1563
+ // 6-face spinning cube, dominating both record and replay).
1564
+ int16_t xs[4], ys[4], fx[4], fy[4];
1565
+ ViewGeometry::transformRectCorners(node, false, x, y, w, h, xs, ys, fx, fy);
1566
+ int bx0, by0, bx1, by1;
1567
+ boundsFromCorners(xs, ys, &bx0, &by0, &bx1, &by1);
1568
+ DisplayCommand *cmd = DisplayList::instance().append();
1569
+ if (!cmd) return;
1570
+ cmd->type = DisplayCommandType::FillTransformedLinearGradient;
1571
+ cmd->bx = bx0;
1572
+ cmd->by = by0;
1573
+ cmd->bw = bx1 - bx0 + 1;
1574
+ cmd->bh = by1 - by0 + 1;
1575
+ auto &g = cmd->transformedGradient;
1576
+ g.x0 = xs[0]; g.y0 = ys[0];
1577
+ g.x1 = xs[1]; g.y1 = ys[1];
1578
+ g.x2 = xs[2]; g.y2 = ys[2];
1579
+ g.x3 = xs[3]; g.y3 = ys[3];
1580
+ g.fx0 = fx[0]; g.fy0 = fy[0];
1581
+ g.fx1 = fx[1]; g.fy1 = fy[1];
1582
+ g.fx2 = fx[2]; g.fy2 = fy[2];
1583
+ g.fx3 = fx[3]; g.fy3 = fy[3];
1584
+ g.lx = static_cast<int16_t>(x);
1585
+ g.ly = static_cast<int16_t>(y);
1586
+ g.lw = static_cast<int16_t>(w);
1587
+ g.lh = static_cast<int16_t>(h);
1588
+ g.fromColor = rstyle(node.style).bg_gradient_from_color;
1589
+ g.midColor = rstyle(node.style).bg_gradient_mid_color;
1590
+ g.toColor = rstyle(node.style).bg_gradient_to_color;
1591
+ g.midStop = static_cast<uint16_t>(rstyle(node.style).bg_gradient_mid_stop);
1592
+ g.toStop = static_cast<uint16_t>(gradientToStop(node));
1593
+ g.angle = static_cast<int16_t>(rstyle(node.style).bg_gradient_angle);
1594
+ g.fromAlpha = rstyle(node.style).bg_gradient_from_alpha;
1595
+ g.midAlpha = rstyle(node.style).bg_gradient_mid_alpha;
1596
+ g.toAlpha = rstyle(node.style).bg_gradient_to_alpha;
1597
+ g.hasMid = rstyle(node.style).bg_gradient_has_mid ? 1 : 0;
1598
+ g.backfaceHidden = node.style.backface_hidden ? 1 : 0;
1599
+ // Transformed rectangular borders ride this command as an edge frame (the
1600
+ // span rasterizer paints the first/last edgeWidth px of every span, tracing
1601
+ // the quad outline). Emitting projected stroke quads instead would disarm
1602
+ // the transform-reproject fast path every frame.
1603
+ if (node.style.border_width > 0 && node.style.border_alpha > 0 &&
1604
+ !isFullyRoundedShape(node)) {
1605
+ g.edgeColor = node.style.border_color;
1606
+ g.edgeAlpha = node.style.border_alpha;
1607
+ const int ew = node.style.border_width < 1 ? 1 : node.style.border_width;
1608
+ g.edgeWidth = static_cast<uint8_t>(ew > 8 ? 8 : ew);
1609
+ } else {
1610
+ g.edgeColor = 0;
1611
+ g.edgeAlpha = 0;
1612
+ g.edgeWidth = 0;
1613
+ }
1614
+ }
1615
+
1616
+ void recordRadialGradientBackground(const Node &node)
1617
+ {
1618
+ if (!rstyle(node.style).bg_radial_gradient) return;
1619
+ const int x = node.layout.x;
1620
+ const int y = node.layout.y;
1621
+ const int w = node.layout.width;
1622
+ const int h = node.layout.height;
1623
+ if (w <= 0 || h <= 0) return;
1624
+ if (ViewGeometry::hasTransformChain(node, false)) return;
1625
+ appendRadialGradientRectRaw(node, x, y, w, h);
1626
+ }
1627
+
1628
+ void recordOverlayLinearGradientBackground(const Node &node)
1629
+ {
1630
+ if (!rstyle(node.style).bg_overlay_gradient) return;
1631
+ const int x = node.layout.x;
1632
+ const int y = node.layout.y;
1633
+ const int w = node.layout.width;
1634
+ const int h = node.layout.height;
1635
+ if (w <= 0 || h <= 0) return;
1636
+ if (ViewGeometry::hasTransformChain(node, false)) return;
1637
+ appendLinearGradientRectRaw(node,
1638
+ x,
1639
+ y,
1640
+ w,
1641
+ h,
1642
+ rstyle(node.style).bg_overlay_gradient_from_color,
1643
+ rstyle(node.style).bg_overlay_gradient_mid_color,
1644
+ rstyle(node.style).bg_overlay_gradient_to_color,
1645
+ rstyle(node.style).bg_overlay_gradient_mid_stop,
1646
+ rstyle(node.style).bg_overlay_gradient_to_stop,
1647
+ rstyle(node.style).bg_overlay_gradient_angle,
1648
+ rstyle(node.style).bg_overlay_gradient_from_alpha,
1649
+ rstyle(node.style).bg_overlay_gradient_mid_alpha,
1650
+ rstyle(node.style).bg_overlay_gradient_to_alpha,
1651
+ rstyle(node.style).bg_overlay_gradient_has_mid);
1652
+ }
1653
+
1654
+ void recordTransformedEllipseFill(const Node &node, uint8_t parentAlpha)
1655
+ {
1656
+ const int x = node.layout.x;
1657
+ const int y = node.layout.y;
1658
+ const int w = node.layout.width;
1659
+ const int h = node.layout.height;
1660
+ if (w <= 0 || h <= 0) return;
1661
+
1662
+ int bx0, by0, bx1, by1;
1663
+ ViewRenderer::transformedBounds(node, false, &bx0, &by0, &bx1, &by1);
1664
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, node.style.bg_alpha);
1665
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(effectiveAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1666
+
1667
+ const double cx = static_cast<double>(x) + static_cast<double>(w) * 0.5;
1668
+ const double cy = static_cast<double>(y) + static_cast<double>(h) * 0.5;
1669
+ const double rx = static_cast<double>(w) * 0.5;
1670
+ const double ry = static_cast<double>(h) * 0.5;
1671
+ int steps = h < 28 ? h : 28;
1672
+ if (steps < 6) steps = 6;
1673
+ for (int i = 0; i < steps; i++) {
1674
+ const int sy = y + (h * i) / steps;
1675
+ const int ey = y + (h * (i + 1)) / steps;
1676
+ const int sh = ey - sy;
1677
+ if (sh <= 0) continue;
1678
+ const double midY = static_cast<double>(sy) + static_cast<double>(sh) * 0.5;
1679
+ double t = ry > 0.0 ? (midY - cy) / ry : 0.0;
1680
+ if (t < -1.0) t = -1.0;
1681
+ if (t > 1.0) t = 1.0;
1682
+ const double half = rx * std::sqrt(std::max(0.0, 1.0 - t * t));
1683
+ const int sx = static_cast<int>(std::lround(cx - half));
1684
+ const int sw = static_cast<int>(std::lround(half * 2.0));
1685
+ if (sw <= 0) continue;
1686
+ int16_t xs[4], ys[4];
1687
+ ViewGeometry::transformRectCorners(node, false, sx, sy, sw, sh, xs, ys);
1688
+ int qx0, qy0, qx1, qy1;
1689
+ boundsFromCorners(xs, ys, &qx0, &qy0, &qx1, &qy1);
1690
+ appendFillQuadRaw(xs, ys, node.style.bg_color, qx0, qy0, qx1 - qx0 + 1, qy1 - qy0 + 1,
1691
+ sx, sy, sw, sh, true);
1692
+ }
1693
+
1694
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(parentAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1695
+ }
1696
+
1697
+ void recordTransformedRoundedRectFill(const Node &node, uint8_t parentAlpha)
1698
+ {
1699
+ const int x = node.layout.x;
1700
+ const int y = node.layout.y;
1701
+ const int w = node.layout.width;
1702
+ const int h = node.layout.height;
1703
+ if (w <= 0 || h <= 0) return;
1704
+
1705
+ int16_t xs[4], ys[4];
1706
+ ViewGeometry::transformRectCorners(node, false, x, y, w, h, xs, ys);
1707
+ int bx0, by0, bx1, by1;
1708
+ boundsFromCorners(xs, ys, &bx0, &by0, &bx1, &by1);
1709
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, node.style.bg_alpha);
1710
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(effectiveAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1711
+ int16_t rx8[4]{};
1712
+ int16_t ry8[4]{};
1713
+ resolvedBorderRadii8(node, rx8, ry8);
1714
+ DisplayCommand *cmd = DisplayList::instance().append();
1715
+ if (cmd) {
1716
+ cmd->type = DisplayCommandType::FillTransformedRoundedRect;
1717
+ cmd->bx = bx0;
1718
+ cmd->by = by0;
1719
+ cmd->bw = bx1 - bx0 + 1;
1720
+ cmd->bh = by1 - by0 + 1;
1721
+ auto &r = cmd->transformedRoundedRect;
1722
+ r.x0 = xs[0]; r.y0 = ys[0];
1723
+ r.x1 = xs[1]; r.y1 = ys[1];
1724
+ r.x2 = xs[2]; r.y2 = ys[2];
1725
+ r.x3 = xs[3]; r.y3 = ys[3];
1726
+ r.lx = static_cast<int16_t>(x);
1727
+ r.ly = static_cast<int16_t>(y);
1728
+ r.lw = static_cast<int16_t>(w);
1729
+ r.lh = static_cast<int16_t>(h);
1730
+ r.tlRx8 = rx8[0]; r.tlRy8 = ry8[0];
1731
+ r.trRx8 = rx8[1]; r.trRy8 = ry8[1];
1732
+ r.brRx8 = rx8[2]; r.brRy8 = ry8[2];
1733
+ r.blRx8 = rx8[3]; r.blRy8 = ry8[3];
1734
+ r.color = node.style.bg_color;
1735
+ r.backfaceHidden = node.style.backface_hidden ? 1 : 0;
1736
+ }
1737
+
1738
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(parentAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1739
+ }
1740
+
1741
+ void recordTransformedEllipseStroke(const Node &node, uint8_t parentAlpha)
1742
+ {
1743
+ const int x = node.layout.x;
1744
+ const int y = node.layout.y;
1745
+ const int w = node.layout.width;
1746
+ const int h = node.layout.height;
1747
+ if (w <= 0 || h <= 0 || node.style.border_width <= 0) return;
1748
+
1749
+ int bx0, by0, bx1, by1;
1750
+ ViewRenderer::transformedBounds(node, false, &bx0, &by0, &bx1, &by1);
1751
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, node.style.border_alpha);
1752
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(effectiveAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1753
+
1754
+ const float cx = static_cast<float>(x) + static_cast<float>(w) * 0.5f;
1755
+ const float cy = static_cast<float>(y) + static_cast<float>(h) * 0.5f;
1756
+ const float stroke = static_cast<float>(node.style.border_width < 1 ? 1 : node.style.border_width);
1757
+ const float screenStroke = std::max(1.6f, stroke * 1.2f);
1758
+ const float rx = std::max(0.0f, static_cast<float>(w) * 0.5f - stroke * 0.5f);
1759
+ const float ry = std::max(0.0f, static_cast<float>(h) * 0.5f - stroke * 0.5f);
1760
+ // Segment count adaptive to ring size (~1 segment per 4px of diameter), clamped.
1761
+ // Was a flat 256; each segment is a transformed stroke band (record + replay),
1762
+ // so this is kept as low as stays visually smooth for the cube's thin rings.
1763
+ int segments = (w > h ? w : h) / 4;
1764
+ if (segments < 14) segments = 14;
1765
+ if (segments > 40) segments = 40;
1766
+ const float angStep = (2.0f * kPiF) / static_cast<float>(segments);
1767
+ const ViewGeometry::ChainProjector projector = ViewGeometry::prepareProjector(node);
1768
+ int16_t prevX = 0;
1769
+ int16_t prevY = 0;
1770
+ float prevLocalX = 0.0f;
1771
+ float prevLocalY = 0.0f;
1772
+ for (int i = 0; i <= segments; i++) {
1773
+ const float a = static_cast<float>(i) * angStep;
1774
+ const float localX = cx + rx * std::cos(a);
1775
+ const float localY = cy + ry * std::sin(a);
1776
+ int16_t px, py;
1777
+ ViewGeometry::projectPoint(projector, localX, localY, 0.0f, &px, &py);
1778
+ if (i > 0)
1779
+ appendStrokeSegmentBandRaw(prevX, prevY, px, py, screenStroke, node.style.border_color,
1780
+ prevLocalX, prevLocalY, localX, localY, true);
1781
+ prevX = px;
1782
+ prevY = py;
1783
+ prevLocalX = localX;
1784
+ prevLocalY = localY;
1785
+ }
1786
+
1787
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(parentAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1788
+ }
1789
+
1790
+ void appendBackgroundGridRectRaw(const Node &node, int x, int y, int w, int h)
1791
+ {
1792
+ if (w <= 0 || h <= 0) return;
1793
+ if (ViewGeometry::hasTransformChain(node, false)) {
1794
+ int16_t xs[4], ys[4];
1795
+ ViewGeometry::transformRectCorners(node, false, x, y, w, h, xs, ys);
1796
+ // Corners are TL,TR,BR,BL. Near the horizon (or a far perspective edge) a grid
1797
+ // strip projects to under 1px and its two long edges round to the same row/col —
1798
+ // a degenerate, zero-area quad the rasterizer can't fill, so the line vanishes.
1799
+ // Round the thin axis up to a minimum of 1px so every line still draws.
1800
+ if (ys[2] - ys[1] < 1) ys[2] = ys[1] + 1; // right edge height (BR below TR)
1801
+ if (ys[3] - ys[0] < 1) ys[3] = ys[0] + 1; // left edge height (BL below TL)
1802
+ if (xs[1] - xs[0] < 1) xs[1] = xs[0] + 1; // top edge width (TR right of TL)
1803
+ if (xs[2] - xs[3] < 1) xs[2] = xs[3] + 1; // bottom edge width (BR right of BL)
1804
+ int bx0, by0, bx1, by1;
1805
+ boundsFromCorners(xs, ys, &bx0, &by0, &bx1, &by1);
1806
+ appendFillQuadRaw(xs, ys, rstyle(node.style).bg_grid_color, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1,
1807
+ x, y, w, h, 3);
1808
+ } else {
1809
+ if (!hasAnyRadius(node)) {
1810
+ appendFillRectRaw(x, y, w, h, rstyle(node.style).bg_grid_color, x, y, w, h);
1811
+ return;
1812
+ }
1813
+ int runX0 = 0;
1814
+ int runX1 = -1;
1815
+ int runY = 0;
1816
+ int runH = 0;
1817
+ auto flushRun = [&]() {
1818
+ if (runH > 0 && runX0 <= runX1)
1819
+ appendFillRectRaw(runX0, runY, runX1 - runX0 + 1, runH, rstyle(node.style).bg_grid_color, runX0, runY, runX1 - runX0 + 1, runH);
1820
+ runH = 0;
1821
+ };
1822
+ for (int py = y; py < y + h; ++py) {
1823
+ int rowX0 = x;
1824
+ int rowX1 = x + w - 1;
1825
+ roundedNodeRowSpan(node, py, &rowX0, &rowX1);
1826
+ if (rowX0 > rowX1) {
1827
+ flushRun();
1828
+ continue;
1829
+ }
1830
+ if (runH > 0 && rowX0 == runX0 && rowX1 == runX1) {
1831
+ ++runH;
1832
+ continue;
1833
+ }
1834
+ flushRun();
1835
+ runX0 = rowX0;
1836
+ runX1 = rowX1;
1837
+ runY = py;
1838
+ runH = 1;
1839
+ }
1840
+ flushRun();
1841
+ }
1842
+ }
1843
+
1844
+ void recordBackgroundGrid(const Node &node, uint8_t parentAlpha)
1845
+ {
1846
+ if (rstyle(node.style).bg_grid_axes == 0) return;
1847
+ const int x0 = node.layout.x;
1848
+ const int y0 = node.layout.y;
1849
+ const int x1 = node.layout.x + node.layout.width;
1850
+ const int y1 = node.layout.y + node.layout.height;
1851
+ if (x1 <= x0 || y1 <= y0) return;
1852
+
1853
+ int bx0 = x0, by0 = y0, bx1 = x1 - 1, by1 = y1 - 1;
1854
+ if (ViewGeometry::hasTransformChain(node, false))
1855
+ ViewRenderer::transformedBounds(node, false, &bx0, &by0, &bx1, &by1);
1856
+ const uint8_t effectiveAlpha = combineAlpha(parentAlpha, rstyle(node.style).bg_grid_alpha);
1857
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(effectiveAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1858
+
1859
+ if ((rstyle(node.style).bg_grid_axes & 1) != 0 && rstyle(node.style).bg_grid_line_x > 0) {
1860
+ const int step = rstyle(node.style).bg_grid_step_x > 0 ? rstyle(node.style).bg_grid_step_x : rstyle(node.style).bg_grid_line_x;
1861
+ if (step > 0) {
1862
+ for (int x = x0; x < x1; x += step) {
1863
+ const int w = std::min<int>(rstyle(node.style).bg_grid_line_x, x1 - x);
1864
+ appendBackgroundGridRectRaw(node, x, y0, w, y1 - y0);
1865
+ }
1866
+ }
1867
+ }
1868
+ if ((rstyle(node.style).bg_grid_axes & 2) != 0 && rstyle(node.style).bg_grid_line_y > 0) {
1869
+ const int step = rstyle(node.style).bg_grid_step_y > 0 ? rstyle(node.style).bg_grid_step_y : rstyle(node.style).bg_grid_line_y;
1870
+ if (step > 0) {
1871
+ for (int y = y0; y < y1; y += step) {
1872
+ const int h = std::min<int>(rstyle(node.style).bg_grid_line_y, y1 - y);
1873
+ appendBackgroundGridRectRaw(node, x0, y, x1 - x0, h);
1874
+ }
1875
+ }
1876
+ }
1877
+
1878
+ if (effectiveAlpha != parentAlpha) appendAlphaCommand(parentAlpha, bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1);
1879
+ }
1880
+
1881
+ uint8_t insetShadowAlphaAt(int depth, int solidDepth, int blurRadius, uint8_t baseAlpha)
1882
+ {
1883
+ if (baseAlpha == 0) return 0;
1884
+ if (depth < solidDepth) return baseAlpha;
1885
+ if (blurRadius <= 0) return 0;
1886
+ const int blurDepth = depth - solidDepth;
1887
+ if (blurDepth >= blurRadius) return 0;
1888
+ double t = 1.0 - (static_cast<double>(blurDepth) + 0.5) / static_cast<double>(blurRadius);
1889
+ if (t < 0.0) t = 0.0;
1890
+ if (t > 1.0) t = 1.0;
1891
+ const int alpha = static_cast<int>(static_cast<double>(baseAlpha) * t * t + 0.5);
1892
+ return static_cast<uint8_t>(alpha < 0 ? 0 : alpha > 255 ? 255 : alpha);
1893
+ }
1894
+
1895
+ void appendInsetShadowBand(const Node &node, uint8_t parentAlpha, int x, int y, int w, int h, uint8_t alpha)
1896
+ {
1897
+ if (alpha == 0 || w <= 0 || h <= 0) return;
1898
+ if (ViewGeometry::hasTransformChain(node, false)) {
1899
+ int16_t xs[4], ys[4];
1900
+ ViewGeometry::transformRectCorners(node, false, x, y, w, h, xs, ys);
1901
+ int bx0, by0, bx1, by1;
1902
+ boundsFromCorners(xs, ys, &bx0, &by0, &bx1, &by1);
1903
+ appendFillQuadWithAlpha(xs,
1904
+ ys,
1905
+ rstyle(node.style).box_shadow_color,
1906
+ alpha,
1907
+ parentAlpha,
1908
+ bx0,
1909
+ by0,
1910
+ bx1 - bx0 + 1,
1911
+ by1 - by0 + 1,
1912
+ x,
1913
+ y,
1914
+ w,
1915
+ h,
1916
+ true);
1917
+ return;
1918
+ }
1919
+ if (!hasAnyRadius(node)) {
1920
+ appendFillRectWithAlpha(x, y, w, h, rstyle(node.style).box_shadow_color, alpha, parentAlpha, x, y, w, h);
1921
+ return;
1922
+ }
1923
+ int runX0 = 0;
1924
+ int runX1 = -1;
1925
+ int runY = 0;
1926
+ int runH = 0;
1927
+ auto flushRun = [&]() {
1928
+ if (runH > 0 && runX0 <= runX1)
1929
+ appendFillRectWithAlpha(runX0,
1930
+ runY,
1931
+ runX1 - runX0 + 1,
1932
+ runH,
1933
+ rstyle(node.style).box_shadow_color,
1934
+ alpha,
1935
+ parentAlpha,
1936
+ runX0,
1937
+ runY,
1938
+ runX1 - runX0 + 1,
1939
+ runH);
1940
+ runH = 0;
1941
+ };
1942
+ for (int py = y; py < y + h; ++py) {
1943
+ int rowX0 = x;
1944
+ int rowX1 = x + w - 1;
1945
+ roundedNodeRowSpan(node, py, &rowX0, &rowX1);
1946
+ if (rowX0 > rowX1) {
1947
+ flushRun();
1948
+ continue;
1949
+ }
1950
+ if (runH > 0 && rowX0 == runX0 && rowX1 == runX1) {
1951
+ ++runH;
1952
+ continue;
1953
+ }
1954
+ flushRun();
1955
+ runX0 = rowX0;
1956
+ runX1 = rowX1;
1957
+ runY = py;
1958
+ runH = 1;
1959
+ }
1960
+ flushRun();
1961
+ }
1962
+
1963
+ void recordInsetBoxShadow(const Node &node, uint8_t parentAlpha)
1964
+ {
1965
+ if (!rstyle(node.style).box_shadow_inset || rstyle(node.style).box_shadow_alpha == 0) return;
1966
+ const int x = node.layout.x;
1967
+ const int y = node.layout.y;
1968
+ const int w = node.layout.width;
1969
+ const int h = node.layout.height;
1970
+ if (w <= 0 || h <= 0) return;
1971
+
1972
+ const int blur = std::max<int>(0, rstyle(node.style).box_shadow_blur_radius);
1973
+ const int spread = std::max<int>(0, rstyle(node.style).box_shadow_spread);
1974
+ const int ox = rstyle(node.style).box_shadow_offset_x;
1975
+ const int oy = rstyle(node.style).box_shadow_offset_y;
1976
+ const uint8_t baseAlpha = rstyle(node.style).box_shadow_alpha;
1977
+
1978
+ const int leftSolid = spread + std::max(0, ox);
1979
+ const int rightSolid = spread + std::max(0, -ox);
1980
+ const int topSolid = spread + std::max(0, oy);
1981
+ const int bottomSolid = spread + std::max(0, -oy);
1982
+ const int leftExtent = std::min(w / 2, leftSolid + blur);
1983
+ const int rightExtent = std::min(w / 2, rightSolid + blur);
1984
+ const int topExtent = std::min(h / 2, topSolid + blur);
1985
+ const int bottomExtent = std::min(h / 2, bottomSolid + blur);
1986
+
1987
+ for (int d = 0; d < topExtent; ++d)
1988
+ appendInsetShadowBand(node, parentAlpha, x, y + d, w, 1, insetShadowAlphaAt(d, topSolid, blur, baseAlpha));
1989
+ for (int d = 0; d < bottomExtent; ++d)
1990
+ appendInsetShadowBand(node, parentAlpha, x, y + h - d - 1, w, 1, insetShadowAlphaAt(d, bottomSolid, blur, baseAlpha));
1991
+ for (int d = 0; d < leftExtent; ++d)
1992
+ appendInsetShadowBand(node, parentAlpha, x + d, y, 1, h, insetShadowAlphaAt(d, leftSolid, blur, baseAlpha));
1993
+ for (int d = 0; d < rightExtent; ++d)
1994
+ appendInsetShadowBand(node, parentAlpha, x + w - d - 1, y, 1, h, insetShadowAlphaAt(d, rightSolid, blur, baseAlpha));
1995
+ }
1996
+
1997
+ void GEA_VIEW_HOT_SRAM_SECTION("view_renderer_transformed_bounds") ViewRenderer::transformedBounds(const Node &node, bool use_prev, int *x0, int *y0, int *x1, int *y1)
1998
+ {
1999
+ const Node *n = &node;
2000
+ int x = use_prev ? n->layout.previous_x : n->layout.x;
2001
+ int y = use_prev ? n->layout.previous_y : n->layout.y;
2002
+ int w = use_prev ? n->layout.previous_width : n->layout.width;
2003
+ int h = use_prev ? n->layout.previous_height : n->layout.height;
2004
+
2005
+ if (w <= 0 || h <= 0) {
2006
+ *x0 = x; *y0 = y; *x1 = x - 1; *y1 = y - 1;
2007
+ return;
2008
+ }
2009
+
2010
+ // Hot fast path: a node with no RareStyle entry (rare_style < 0) has no local
2011
+ // transform/perspective/filter, so in a transform-free tree its current bounds are
2012
+ // exactly the layout box. In a transformed tree we must still walk ancestors:
2013
+ // otherwise plain children (for example a face label <span>) ignore their
2014
+ // transformed parent plane and paint as axis-aligned overlay text.
2015
+ if (!use_prev && n->style.rare_style < 0 && !ViewGeometry::anyTransformPresent()) {
2016
+ *x0 = x;
2017
+ *y0 = y;
2018
+ *x1 = x + w - 1;
2019
+ *y1 = y + h - 1;
2020
+ return;
2021
+ }
2022
+
2023
+ int rotate = use_prev ? n->render.previous_transform_rotate : rstyle(n->style).transform_rotate;
2024
+
2025
+ auto expandForBlur = [&]() {
2026
+ const int radius = use_prev ? n->render.previous_filter_blur_radius : rstyle(n->style).filter_blur_radius;
2027
+ if (radius <= 0) return;
2028
+ const int extentX = std::max(1, radius) * 5;
2029
+ const int extentY = std::max(1, radius) * 5;
2030
+ *x0 -= extentX;
2031
+ *y0 -= extentY;
2032
+ *x1 += extentX;
2033
+ *y1 += extentY;
2034
+ };
2035
+
2036
+ if ((rotate % 3600) == 0 && !ViewGeometry::hasTransformChain(node, use_prev)) {
2037
+ *x0 = x;
2038
+ *y0 = y;
2039
+ *x1 = x + w - 1;
2040
+ *y1 = y + h - 1;
2041
+ expandForBlur();
2042
+ return;
2043
+ }
2044
+
2045
+ int16_t xs[4], ys[4];
2046
+ ViewGeometry::transformCorners(*n, use_prev, xs, ys);
2047
+ *x0 = *x1 = xs[0];
2048
+ *y0 = *y1 = ys[0];
2049
+ for (int i = 1; i < 4; i++) {
2050
+ if (xs[i] < *x0) *x0 = xs[i];
2051
+ if (xs[i] > *x1) *x1 = xs[i];
2052
+ if (ys[i] < *y0) *y0 = ys[i];
2053
+ if (ys[i] > *y1) *y1 = ys[i];
2054
+ }
2055
+ expandForBlur();
2056
+ }
2057
+
2058
+ void GEA_VIEW_HOT_SRAM_SECTION("view_renderer_transformed_corners") ViewRenderer::transformedCorners(const Node &node, bool usePrevious, int16_t *xs, int16_t *ys)
2059
+ {
2060
+ ViewGeometry::transformCorners(node, usePrevious, xs, ys);
2061
+ }
2062
+
2063
+ void GEA_VIEW_HOT_SRAM_SECTION("view_renderer_transformed_point") ViewRenderer::transformedPoint(const Node &node, bool usePrevious, float x, float y, float z, int16_t *outX, int16_t *outY)
2064
+ {
2065
+ ViewGeometry::transformPoint(node, usePrevious, x, y, z, outX, outY);
2066
+ }
2067
+
2068
+ void GEA_VIEW_HOT_SRAM_SECTION("view_renderer_transformed_rect_corners") ViewRenderer::transformedRectCorners(const Node &node, bool usePrevious, int x, int y, int w, int h, int16_t *xs, int16_t *ys,
2069
+ int16_t *xs8, int16_t *ys8)
2070
+ {
2071
+ ViewGeometry::transformRectCorners(node, usePrevious, x, y, w, h, xs, ys, xs8, ys8);
2072
+ }
2073
+
2074
+ int GEA_VIEW_HOT_SRAM_SECTION("view_renderer_transformed_depth") ViewRenderer::transformedDepth(const Node &node, bool usePrevious)
2075
+ {
2076
+ return ViewGeometry::averageDepth(node, usePrevious);
2077
+ }
2078
+
2079
+ bool GEA_VIEW_HOT_SRAM_SECTION("view_renderer_any_transform_active") ViewRenderer::anyTransformActive()
2080
+ {
2081
+ return ViewGeometry::anyTransformPresent();
2082
+ }
2083
+
2084
+ bool ViewRenderer::recordClipBegin(const Node &node)
2085
+ {
2086
+ const Node *n = &node;
2087
+ if (ViewGeometry::hasTransformChain(*n, false)) return 0;
2088
+ if (!isViewLikeNodeType(n->type) || n->style.overflow == 0) return 0;
2089
+ if (n->first_child < 0) return 0;
2090
+
2091
+ DisplayCommand *cmd = DisplayList::instance().append();
2092
+ if (cmd) {
2093
+ cmd->type = DisplayCommandType::PushClip;
2094
+ cmd->bx = n->layout.x;
2095
+ cmd->by = n->layout.y;
2096
+ cmd->bw = n->layout.width;
2097
+ cmd->bh = n->layout.height;
2098
+ cmd->clip.x = n->layout.x;
2099
+ cmd->clip.y = n->layout.y;
2100
+ cmd->clip.w = n->layout.width;
2101
+ cmd->clip.h = n->layout.height;
2102
+ cmd->clip.nodeId = static_cast<int16_t>(&node - Tree::instance().nodes());
2103
+ }
2104
+ return 1;
2105
+ }
2106
+
2107
+ int ViewRenderer::scrollMaxX(const Node &node)
2108
+ {
2109
+ if (node.type == NodeType::VirtualList) return 0;
2110
+ int max_x = node.layout.scroll_content_width - node.layout.width;
2111
+ return max_x > 0 ? max_x : 0;
2112
+ }
2113
+
2114
+ int ViewRenderer::scrollMaxY(const Node &node)
2115
+ {
2116
+ if (node.type == NodeType::VirtualList) {
2117
+ const int id = static_cast<int>(&node - Tree::instance().nodes());
2118
+ return VirtualListRenderer::scrollMaxY(id);
2119
+ }
2120
+ int max_y = node.layout.scroll_content_height - node.layout.height;
2121
+ return max_y > 0 ? max_y : 0;
2122
+ }
2123
+
2124
+ void ViewRenderer::recordClipEnd(const Node &node)
2125
+ {
2126
+ const Node *n = &node;
2127
+ DisplayCommand *cmd = DisplayList::instance().append();
2128
+ if (!cmd) return;
2129
+ cmd->type = DisplayCommandType::PopClip;
2130
+ cmd->bx = n->layout.x;
2131
+ cmd->by = n->layout.y;
2132
+ cmd->bw = n->layout.width;
2133
+ cmd->bh = n->layout.height;
2134
+ cmd->clip.nodeId = static_cast<int16_t>(&node - Tree::instance().nodes());
2135
+ }
2136
+
2137
+ void GEA_VIEW_HOT_SRAM_SECTION("view_renderer_record_box") ViewRenderer::recordBox(const Node &node, uint8_t parentAlpha)
2138
+ {
2139
+ const Node *n = &node;
2140
+ int x = n->layout.x;
2141
+ int y = n->layout.y;
2142
+ int w = n->layout.width;
2143
+ int h = n->layout.height;
2144
+ if (w <= 0 || h <= 0) return;
2145
+
2146
+ if (n->style.has_bg) {
2147
+ if (n->style.bg_fill == 1) {
2148
+ recordLinearGradientBackground(*n, parentAlpha);
2149
+ recordRadialGradientBackground(*n);
2150
+ recordOverlayLinearGradientBackground(*n);
2151
+ recordBackgroundGrid(*n, parentAlpha);
2152
+ } else if (ViewGeometry::hasTransformChain(*n, false)) {
2153
+ int16_t xs[4], ys[4];
2154
+ int bx0, by0, bx1, by1;
2155
+ ViewRenderer::transformedBounds(*n, false, &bx0, &by0, &bx1, &by1);
2156
+ if (hasAnyRadius(*n)) {
2157
+ recordTransformedRoundedRectFill(*n, parentAlpha);
2158
+ } else {
2159
+ ViewGeometry::transformCorners(*n, false, xs, ys);
2160
+ appendFillQuadWithAlpha(xs, ys, n->style.bg_color, n->style.bg_alpha, parentAlpha,
2161
+ bx0, by0, bx1 - bx0 + 1, by1 - by0 + 1,
2162
+ x, y, w, h, true);
2163
+ }
2164
+ recordBackgroundGrid(*n, parentAlpha);
2165
+ } else {
2166
+ if (hasAnyRadius(*n)) {
2167
+ if (hasAnyPercentRadius(*n) && !appendResolvedCircularRoundedRectWithAlpha(*n, parentAlpha))
2168
+ recordTransformedRoundedRectFill(*n, parentAlpha);
2169
+ else if (!hasAnyPercentRadius(*n))
2170
+ appendFillRoundedRectWithAlpha(*n, parentAlpha);
2171
+ } else {
2172
+ appendFillRectWithAlpha(x, y, w, h, n->style.bg_color, n->style.bg_alpha, parentAlpha, x, y, w, h);
2173
+ }
2174
+ recordBackgroundGrid(*n, parentAlpha);
2175
+ }
2176
+ }
2177
+
2178
+ recordInsetBoxShadow(*n, parentAlpha);
2179
+
2180
+ if (n->style.border_width > 0 && !borderIsSameOpaqueSolidBackground(*n, parentAlpha)) {
2181
+ if (ViewGeometry::hasTransformChain(*n, false)) {
2182
+ if (isFullyRoundedShape(*n)) recordTransformedEllipseStroke(*n, parentAlpha);
2183
+ } else {
2184
+ appendStrokeWithAlpha(*n, parentAlpha);
2185
+ }
2186
+ }
2187
+ appendSideBordersWithAlpha(*n, parentAlpha);
2188
+ }
2189
+
2190
+ void ViewRenderer::recordScrollbar(const Node &node)
2191
+ {
2192
+ const Node *n = &node;
2193
+ if (!isViewLikeNodeType(n->type) || (n->type != NodeType::VirtualList && !scrollsOverflowY(n->style))) return;
2194
+ if (n->layout.height <= 0 || n->layout.scroll_content_height <= n->layout.height) return;
2195
+
2196
+ int track_h = n->layout.height - 12;
2197
+ if (track_h < 24) return;
2198
+
2199
+ int thumb_h = (n->layout.height * track_h) / n->layout.scroll_content_height;
2200
+ if (thumb_h < 24) thumb_h = 24;
2201
+ if (thumb_h > track_h) thumb_h = track_h;
2202
+
2203
+ int max_scroll = n->layout.scroll_content_height - n->layout.height;
2204
+ int thumb_y = n->layout.y + 6;
2205
+ if (max_scroll > 0)
2206
+ thumb_y += (n->layout.scroll_y * (track_h - thumb_h)) / max_scroll;
2207
+
2208
+ DisplayCommand *cmd = DisplayList::instance().append();
2209
+ if (!cmd) return;
2210
+ cmd->type = DisplayCommandType::FillRoundedRect;
2211
+ cmd->bx = n->layout.x + n->layout.width - 7;
2212
+ cmd->by = thumb_y;
2213
+ cmd->bw = 3;
2214
+ cmd->bh = thumb_h;
2215
+ cmd->fillRoundedRect.x = cmd->bx;
2216
+ cmd->fillRoundedRect.y = cmd->by;
2217
+ cmd->fillRoundedRect.w = cmd->bw;
2218
+ cmd->fillRoundedRect.h = cmd->bh;
2219
+ cmd->fillRoundedRect.tl = 2;
2220
+ cmd->fillRoundedRect.tr = 2;
2221
+ cmd->fillRoundedRect.br = 2;
2222
+ cmd->fillRoundedRect.bl = 2;
2223
+ // Command colours are panel-order natives, so this has to go through the one
2224
+ // authoring conversion (nativeFromRrggbbaa): it applies the panel byte-swap a
2225
+ // bare fromRgb565 would leave off on 16-bit panels AND narrows to the board's
2226
+ // native_t on the grayscale panels, where native_t is a byte and the RGB565
2227
+ // constant would not fit. 0x8C8E94 is the exact RGB888 expansion of the RGB565
2228
+ // 0x8C72 this used to spell, so 16-bit panels emit the identical pixel.
2229
+ cmd->fillRoundedRect.color = gea::framework::graphics::pixel::nativeFromRrggbbaa(0x8C8E94FFu);
2230
+ }
2231
+
2232
+ } // namespace gea::embedded::ui