@geastack/engine 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +29 -0
  3. package/bitmap_font.cpp +165 -0
  4. package/canvas.cpp +4608 -0
  5. package/components/Button.d.ts +4 -0
  6. package/components/Button.tsx +32 -0
  7. package/components/Image.d.ts +3 -0
  8. package/components/Image.tsx +17 -0
  9. package/components/Text.d.ts +3 -0
  10. package/components/Text.tsx +9 -0
  11. package/components/View.d.ts +3 -0
  12. package/components/View.tsx +24 -0
  13. package/components/index.d.ts +8 -0
  14. package/components/index.ts +4 -0
  15. package/css/animation.h +252 -0
  16. package/css/declarative.h +131 -0
  17. package/css/easing.h +160 -0
  18. package/css/engine.h +223 -0
  19. package/css/interpolate.h +38 -0
  20. package/image_store.cpp +807 -0
  21. package/package.json +62 -0
  22. package/rasterized_font.cpp +570 -0
  23. package/touch_runtime.cpp +513 -0
  24. package/ui/absolute_leaf_refresh.cpp +279 -0
  25. package/ui/absolute_leaf_refresh.h +12 -0
  26. package/ui/canvas_element.cpp +2314 -0
  27. package/ui/canvas_element.h +353 -0
  28. package/ui/canvas_store.cpp +188 -0
  29. package/ui/canvas_store.h +59 -0
  30. package/ui/core.cpp +135 -0
  31. package/ui/css_atom.cpp +155 -0
  32. package/ui/css_atom.h +21 -0
  33. package/ui/dirty_regions.cpp +66 -0
  34. package/ui/dirty_regions.h +34 -0
  35. package/ui/display_invalidation.cpp +101 -0
  36. package/ui/display_invalidation.h +15 -0
  37. package/ui/document.cpp +356 -0
  38. package/ui/document.h +101 -0
  39. package/ui/image.h +16 -0
  40. package/ui/image_element.cpp +13 -0
  41. package/ui/image_node.cpp +165 -0
  42. package/ui/input.cpp +936 -0
  43. package/ui/input_render.cpp +213 -0
  44. package/ui/internal.h +567 -0
  45. package/ui/layout.cpp +2025 -0
  46. package/ui/layout_snapshot.cpp +82 -0
  47. package/ui/layout_snapshot.h +12 -0
  48. package/ui/mirror.h +25 -0
  49. package/ui/node.cpp +243 -0
  50. package/ui/node.h +154 -0
  51. package/ui/node_lifecycle.cpp +137 -0
  52. package/ui/node_lifecycle.h +13 -0
  53. package/ui/node_model.h +597 -0
  54. package/ui/refresh_perf.h +235 -0
  55. package/ui/refresh_trace.h +68 -0
  56. package/ui/render.cpp +12097 -0
  57. package/ui/root_scroll_refresh.cpp +859 -0
  58. package/ui/root_scroll_refresh.h +30 -0
  59. package/ui/state_init.h +21 -0
  60. package/ui/style.cpp +14994 -0
  61. package/ui/style.h +727 -0
  62. package/ui/style_values.h +31 -0
  63. package/ui/text.cpp +2010 -0
  64. package/ui/text.h +15 -0
  65. package/ui/text_element.cpp +15 -0
  66. package/ui/tree_events.cpp +621 -0
  67. package/ui/tree_events.h +69 -0
  68. package/ui/tree_internal.h +191 -0
  69. package/ui/tree_nodes.cpp +628 -0
  70. package/ui/tree_render.cpp +3138 -0
  71. package/ui/tree_scroll_mirror.cpp +101 -0
  72. package/ui/tree_state.cpp +574 -0
  73. package/ui/tree_state.h +235 -0
  74. package/ui/tree_style.cpp +698 -0
  75. package/ui/ui.h +9 -0
  76. package/ui/view.cpp +2232 -0
  77. package/ui/view.h +14 -0
  78. package/ui/view_element.cpp +13 -0
  79. package/ui/viewport_region.cpp +36 -0
  80. package/ui/viewport_region.h +12 -0
  81. package/ui/virtual_keyboard.cpp +908 -0
  82. package/ui/virtual_keyboard.h +59 -0
  83. package/vendor/AnimatedGIF/AnimatedGIF.c +982 -0
  84. package/vendor/AnimatedGIF/AnimatedGIF.h +216 -0
  85. package/vendor/stb/stb_image.h +7988 -0
  86. package/vendor/stb/stb_truetype.h +5079 -0
@@ -0,0 +1,353 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "canvas.h"
5
+ #include "node.h"
6
+
7
+ #include <cstdint>
8
+ #include <cstddef>
9
+ #include <string>
10
+ #include <vector>
11
+
12
+ namespace gea::embedded::ui {
13
+
14
+ struct CanvasPerfStats {
15
+ std::int64_t canvasLookupUs = 0;
16
+ std::int64_t markDirtyUs = 0;
17
+ std::int64_t clearRectUs = 0;
18
+ std::int64_t fillRectUs = 0;
19
+ std::int64_t fillCircleUs = 0;
20
+ std::int64_t fillTriangleUs = 0;
21
+ std::int64_t drawImageUs = 0;
22
+ std::int64_t fillTextUs = 0;
23
+ std::int64_t batchFlushUs = 0;
24
+ int canvasLookupCalls = 0;
25
+ int markDirtyCalls = 0;
26
+ int clearRectCalls = 0;
27
+ int fillRectCalls = 0;
28
+ int fillCircleCalls = 0;
29
+ int fillTriangleCalls = 0;
30
+ int drawImageCalls = 0;
31
+ int fillTextCalls = 0;
32
+ int batchBeginCalls = 0;
33
+ int batchEndCalls = 0;
34
+ int batchFlushCalls = 0;
35
+ };
36
+
37
+ enum class CanvasPresentCommandType : std::uint8_t {
38
+ Clear,
39
+ FillRectRgb565,
40
+ StrokeRectRgb565,
41
+ FillTriangleRgb565,
42
+ FillCircleRgb565,
43
+ StrokeCircleRgb565,
44
+ FillCirclesRgb565,
45
+ DrawImage,
46
+ DrawImageScaled,
47
+ DrawImageRotated90CW,
48
+ DrawImageTiledX,
49
+ FillText,
50
+ FillTrianglesRgb565
51
+ };
52
+
53
+ struct CanvasPresentCommand {
54
+ CanvasPresentCommandType type = CanvasPresentCommandType::Clear;
55
+ gea::framework::graphics::pixel::native_t clearColor = 0;
56
+ gea::framework::graphics::pixel::native_t color = 0;
57
+ const gea::framework::graphics::pixel::native_t *pixels = nullptr;
58
+ const std::uint8_t *alphaPixels = nullptr;
59
+ int srcWidth = 0;
60
+ int srcHeight = 0;
61
+ int x = 0;
62
+ int y = 0;
63
+ int w = 0;
64
+ int h = 0;
65
+ int x0 = 0;
66
+ int y0 = 0;
67
+ int x1 = 0;
68
+ int y1 = 0;
69
+ int x2 = 0;
70
+ int y2 = 0;
71
+ int radius = 0;
72
+ float scale = 1.0f;
73
+ // Baked-font routing for FillText: a valid family id draws via the
74
+ // rasterized Inter atlases; -1 falls back to the scaled bitmap font.
75
+ int fontFamilyId = -1;
76
+ int fontSizePx = 16;
77
+ std::uint8_t alpha = 255;
78
+ std::string text;
79
+ std::vector<std::uint16_t> xs;
80
+ std::vector<std::uint16_t> ys;
81
+ std::vector<gea::framework::graphics::pixel::native_t> colors;
82
+ // FillTrianglesRgb565: entries pre-packed in painter's (depth) order.
83
+ std::vector<gea::framework::graphics::TriangleEntry> triangles;
84
+ };
85
+
86
+ void canvasPerfStatsReset();
87
+ CanvasPerfStats canvasPerfStatsRead();
88
+ void canvasTotalsRead(int *begin, int *end, int *fillRect, int *drawImage, int *nullPixels, int *presentOk);
89
+
90
+ class CanvasRenderingContext2D {
91
+ public:
92
+ // A non-explicit default ctor is required so this type can be a by-value
93
+ // field of a geatsc-generated anonymous-record struct: those structs are
94
+ // aggregates that the generated code value-initializes with `Record{}`,
95
+ // which copy-`{}`-initializes each field — and copy-init cannot select an
96
+ // `explicit` constructor. `explicit` is kept on the int form so a bare
97
+ // `int` still can't implicitly convert to a context.
98
+ CanvasRenderingContext2D() : nodeId_(-1) {}
99
+ explicit CanvasRenderingContext2D(int nodeId) : nodeId_(nodeId) {}
100
+
101
+ bool valid() const { return nodeId_ >= 0; }
102
+ // The absence state `valid()` already answers, in the spelling a test uses.
103
+ // A default-constructed context refers to no canvas (`nodeId_ == -1`) and
104
+ // `getContext('2d')` on a node that is not a canvas hands one back, so
105
+ // `if (!ctx) return` is the guard an app writes -- and every other gea
106
+ // handle answers it this way (`gea::NativeHandle`, the Apple bridge's
107
+ // wrappers, `gea::host::GeaEmbeddedImage`). Explicit, so it never
108
+ // participates in arithmetic or overload resolution.
109
+ explicit operator bool() const { return valid(); }
110
+ void setFillStyle(const std::string &value);
111
+ void setFillStyleRgb565(gea::framework::graphics::pixel::native_t color);
112
+ void setStrokeStyle(const std::string &value);
113
+ void setStrokeStyleRgb565(gea::framework::graphics::pixel::native_t color);
114
+ void setGlobalAlpha(double alpha);
115
+ void setLineWidth(double width);
116
+ void setFont(const std::string &font);
117
+ void setTextBaseline(const std::string &) {}
118
+ void setTextAlign(const std::string &) {}
119
+ void clear();
120
+ void clearRect(int x, int y, int w, int h);
121
+ void clearRect(double x, double y, double w, double h);
122
+ void fillRect(int x, int y, int w, int h);
123
+ void fillRect(double x, double y, double w, double h);
124
+ void strokeRect(int x, int y, int w, int h);
125
+ void strokeRect(double x, double y, double w, double h);
126
+ void fillCircle(int x, int y, int radius);
127
+ void fillCircle(double x, double y, double radius);
128
+ void strokeCircle(int x, int y, int radius);
129
+ void strokeCircle(double x, double y, double radius);
130
+ void fillCircleRgb565(int x, int y, int radius, gea::framework::graphics::pixel::native_t color);
131
+ void fillCircleRgb565(double x, double y, double radius, gea::framework::graphics::pixel::native_t color);
132
+ void fillTriangleRgb565(int x0, int y0, int x1, int y1, int x2, int y2, gea::framework::graphics::pixel::native_t color);
133
+ void fillTriangleRgb565(double x0, double y0, double x1, double y1, double x2, double y2, gea::framework::graphics::pixel::native_t color);
134
+ void fillCirclesRgb565(const std::vector<std::int32_t> &xs,
135
+ const std::vector<std::int32_t> &ys,
136
+ int radius,
137
+ const std::vector<gea::framework::graphics::pixel::native_t> &colors);
138
+ // count>=0 batches only the first `count` triples (lets callers reuse a fixed-size
139
+ // scratch array without a per-frame slice/copy); count<0 uses the full array length.
140
+ // Pointer + count overloads. Callers whose coordinates live in a typed array
141
+ // (the standard batching idiom: fixed-size scratch Uint16Array reused every
142
+ // frame with a smaller live `count`) bound to the std::vector overloads only
143
+ // through gea_cpp_typed_array's implicit operator std::vector<Element>(),
144
+ // which heap-allocates and copies the FULL array per call and ignores
145
+ // `count`. Measured on bubble-grid: ~150us per call independent of how many
146
+ // circles it carried, ~2.3ms/frame across 15 calls. These take the elements
147
+ // where they already are.
148
+ void fillCirclesRgb565(const std::uint16_t *xs,
149
+ const std::uint16_t *ys,
150
+ int radius,
151
+ const gea::framework::graphics::pixel::native_t *colors,
152
+ int count);
153
+ void fillCirclesRgb565(const std::uint16_t *xs,
154
+ const std::uint16_t *ys,
155
+ int radius,
156
+ const gea::framework::graphics::pixel::NativeColor *colors,
157
+ int count);
158
+ void fillCirclesRgb565(const std::vector<std::uint16_t> &xs,
159
+ const std::vector<std::uint16_t> &ys,
160
+ int radius,
161
+ const std::vector<gea::framework::graphics::pixel::native_t> &colors,
162
+ int count = -1);
163
+ void fillCirclesRgb565(const std::vector<std::uint16_t> &xs,
164
+ const std::vector<std::uint16_t> &ys,
165
+ int radius,
166
+ const std::vector<gea::framework::graphics::pixel::NativeColor> &colors,
167
+ int count = -1);
168
+ void fillCirclesRgb565(const std::vector<std::int32_t> &xs,
169
+ const std::vector<std::int32_t> &ys,
170
+ int radius,
171
+ gea::framework::graphics::pixel::native_t color);
172
+ void fillCirclesRgb565(const std::vector<std::uint16_t> &xs,
173
+ const std::vector<std::uint16_t> &ys,
174
+ int radius,
175
+ gea::framework::graphics::pixel::native_t color);
176
+ // Batched triangles, one recorded command per call: `order[0..count)` gives
177
+ // the paint (depth) order into the coordinate/colour arrays. colors hold
178
+ // 0xRRGGBBAA app values (converted to native pixels during packing). This
179
+ // replaces per-triangle fillTriangleRgb565 calls whose per-command record +
180
+ // replay dispatch dominated at a few hundred triangles per frame.
181
+ void fillTrianglesRgb565Sorted(const std::vector<std::int32_t> &x0s,
182
+ const std::vector<std::int32_t> &y0s,
183
+ const std::vector<std::int32_t> &x1s,
184
+ const std::vector<std::int32_t> &y1s,
185
+ const std::vector<std::int32_t> &x2s,
186
+ const std::vector<std::int32_t> &y2s,
187
+ const std::vector<std::uint32_t> &colors,
188
+ const std::vector<std::int32_t> &order,
189
+ int count);
190
+ void beginPath();
191
+ void arc(double x, double y, double radius, double startAngle, double endAngle);
192
+ void moveTo(double x, double y);
193
+ void lineTo(double x, double y);
194
+ void closePath();
195
+ void fill();
196
+ void stroke();
197
+ void fillText(const std::string &text, int x, int y);
198
+ void fillText(const std::string &text, double x, double y);
199
+ void drawImage(int imageId, int dx, int dy);
200
+ void drawImage(int imageId, double dx, double dy);
201
+ void drawImage(int imageId, int dx, int dy, int dw, int dh);
202
+ void drawImage(int imageId, double dx, double dy, double dw, double dh);
203
+ void drawImageRotated90CW(int imageId, int dx, int dy, int dw, int dh);
204
+ void drawImageRotated90CW(int imageId, double dx, double dy, double dw, double dh);
205
+ // Draw an image masked to a circle inscribed in the destination box. Lets an
206
+ // OPAQUE sprite (a JPEG, which has no alpha channel) render as a round tile
207
+ // without carrying an alpha plane: the blit simply skips the pixels outside
208
+ // the shape. Square opaque tiles would otherwise paint their corners over
209
+ // whatever they overlap.
210
+ // Width in pixels of `text` in the context's CURRENT font, so callers can
211
+ // centre or right-align. textAlign is a no-op on this canvas and there was
212
+ // no measureText, which left callers hand-tuning a width factor per string.
213
+ double measureText(const std::string &text);
214
+ // Offset from fillText's y anchor to the vertical centre of the text's ink.
215
+ double measureTextInkCenter(const std::string &text);
216
+ void drawImageCircle(int imageId, int dx, int dy, int dw, int dh);
217
+ void drawImageCircle(int imageId, double dx, double dy, double dw, double dh);
218
+ void drawImageTiledX(int imageId, int dx, int dy, int width);
219
+ void drawImageTiledX(int imageId, double dx, double dy, double width);
220
+ void flush();
221
+ void beginBatch();
222
+ void endBatch();
223
+
224
+ private:
225
+ gea::framework::graphics::Canvas *canvas();
226
+ gea::framework::graphics::Canvas *drawingCanvas();
227
+ bool recordingPresentBatch() const;
228
+ void appendPresentClear(gea::framework::graphics::pixel::native_t color);
229
+ void appendPresentFillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color);
230
+ void appendPresentStrokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color);
231
+ void appendPresentFillTriangle(int x0,
232
+ int y0,
233
+ int x1,
234
+ int y1,
235
+ int x2,
236
+ int y2,
237
+ gea::framework::graphics::pixel::native_t color);
238
+ void appendPresentFillCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color);
239
+ void appendPresentStrokeCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color);
240
+ void appendPresentCircles(const std::uint16_t *xs,
241
+ const std::uint16_t *ys,
242
+ int radius,
243
+ const gea::framework::graphics::pixel::native_t *colors,
244
+ int count);
245
+ void appendPresentCircles(const std::uint16_t *xs,
246
+ const std::uint16_t *ys,
247
+ int radius,
248
+ const gea::framework::graphics::pixel::NativeColor *colors,
249
+ int count);
250
+ void appendPresentCircles(const std::vector<std::uint16_t> &xs,
251
+ const std::vector<std::uint16_t> &ys,
252
+ int radius,
253
+ const std::vector<gea::framework::graphics::pixel::native_t> &colors,
254
+ int count = -1);
255
+ void appendPresentCircles(const std::vector<std::uint16_t> &xs,
256
+ const std::vector<std::uint16_t> &ys,
257
+ int radius,
258
+ const std::vector<gea::framework::graphics::pixel::NativeColor> &colors,
259
+ int count = -1);
260
+ void appendPresentCirclesUniform(const std::vector<std::uint16_t> &xs,
261
+ const std::vector<std::uint16_t> &ys,
262
+ int radius,
263
+ gea::framework::graphics::pixel::native_t color);
264
+ void appendPresentTriangles(const std::vector<std::int32_t> &x0s,
265
+ const std::vector<std::int32_t> &y0s,
266
+ const std::vector<std::int32_t> &x1s,
267
+ const std::vector<std::int32_t> &y1s,
268
+ const std::vector<std::int32_t> &x2s,
269
+ const std::vector<std::int32_t> &y2s,
270
+ const std::vector<std::uint32_t> &colors,
271
+ const std::vector<std::int32_t> &order,
272
+ int count);
273
+ void appendPresentDrawImage(const gea::framework::graphics::pixel::native_t *pixels,
274
+ const std::uint8_t *alphaPixels,
275
+ int srcWidth,
276
+ int srcHeight,
277
+ int x,
278
+ int y);
279
+ void appendPresentDrawImageScaled(const gea::framework::graphics::pixel::native_t *pixels,
280
+ const std::uint8_t *alphaPixels,
281
+ int srcWidth,
282
+ int srcHeight,
283
+ int x,
284
+ int y,
285
+ int w,
286
+ int h,
287
+ int radius = 0);
288
+ void appendPresentDrawImageRotated90CW(const gea::framework::graphics::pixel::native_t *pixels,
289
+ const std::uint8_t *alphaPixels,
290
+ int srcWidth,
291
+ int srcHeight,
292
+ int x,
293
+ int y,
294
+ int w,
295
+ int h);
296
+ void appendPresentDrawImageTiledX(const gea::framework::graphics::pixel::native_t *pixels,
297
+ const std::uint8_t *alphaPixels,
298
+ int srcWidth,
299
+ int srcHeight,
300
+ int x,
301
+ int y,
302
+ int w);
303
+ void appendPresentFillText(const std::string &text, int x, int y, gea::framework::graphics::pixel::native_t color);
304
+ CanvasPresentCommand &appendPresentCommand(CanvasPresentCommandType type);
305
+ void resetPresentCommands();
306
+ void replayPresentBatchToCanvas(gea::framework::graphics::Canvas &surface);
307
+ bool presentBatch();
308
+ void applyDrawState(gea::framework::graphics::Canvas &surface) const;
309
+ void markDirty();
310
+ void markDrawDirty();
311
+
312
+ int nodeId_;
313
+ gea::framework::graphics::Canvas *batchCanvas_ = nullptr;
314
+ std::vector<CanvasPresentCommand> presentCommands_;
315
+ std::size_t presentCommandCount_ = 0;
316
+ gea::framework::graphics::pixel::native_t fillStyle_ = gea::framework::graphics::pixel::nativeColor(255, 255, 255);
317
+ gea::framework::graphics::pixel::native_t strokeStyle_ = gea::framework::graphics::pixel::nativeColor(255, 255, 255);
318
+ std::string fillStyleSource_;
319
+ std::string strokeStyleSource_;
320
+ double arcX_ = 0.0;
321
+ double arcY_ = 0.0;
322
+ double arcRadius_ = 0.0;
323
+ std::vector<double> pathX_;
324
+ std::vector<double> pathY_;
325
+ // Start index in pathX_/pathY_ of each subpath (one per moveTo), so a path
326
+ // can hold MULTIPLE rings/contours — required for concave polygons with
327
+ // holes (even-odd fill) and multi-part strokes.
328
+ std::vector<int> pathStarts_;
329
+ double lineWidth_ = 1.0;
330
+ float fontScale_ = 1.0f;
331
+ int fontFamilyId_ = -1;
332
+ int fontSizePx_ = 16;
333
+ // Cache the last CSS font string so setFont can skip the parse + FontRegistry
334
+ // lookups when an app re-sets the same font every frame (e.g. an FPS readout).
335
+ std::string lastFontStr_;
336
+ std::uint8_t globalAlpha_ = 255;
337
+ int batchDepth_ = 0;
338
+ bool fillStyleCached_ = false;
339
+ bool strokeStyleCached_ = false;
340
+ bool hasArc_ = false;
341
+ bool pathClosed_ = false;
342
+ bool presentRecording_ = false;
343
+ bool batchDirty_ = false;
344
+ };
345
+
346
+ class CanvasElement : public NodeHandle {
347
+ public:
348
+ using NodeHandle::NodeHandle;
349
+ static CanvasElement create();
350
+ CanvasRenderingContext2D getContext2D() const;
351
+ };
352
+
353
+ } // namespace gea::embedded::ui
@@ -0,0 +1,188 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "canvas_store.h"
3
+
4
+ #include "internal.h"
5
+ #include "memory.h"
6
+
7
+ #include <cstddef>
8
+ #include <new>
9
+
10
+ namespace gea::embedded::ui {
11
+
12
+ namespace {
13
+
14
+ void releaseOwnedPixels(CanvasSurfaceState *surface)
15
+ {
16
+ if (!surface || !surface->ownsPixels()) return;
17
+ // The retained display list can still hold a BlitImage recorded against this
18
+ // buffer (the node's re-record only happens later in the frame). On hosts
19
+ // whose allocator munmaps large frees (geaos) a replay in that gap reads
20
+ // unmapped memory — SIGSEGV. Kill the dangling references before freeing.
21
+ DisplayList::instance().scrubBlitPixels(surface->pixels);
22
+ gea::framework::memory::Allocator::free(surface->pixels);
23
+ surface->pixels = nullptr;
24
+ }
25
+
26
+ void releaseSurface(CanvasSurfaceState *surface)
27
+ {
28
+ if (!surface) return;
29
+ releaseOwnedPixels(surface);
30
+ delete surface;
31
+ }
32
+
33
+ } // namespace
34
+
35
+ bool CanvasSurfaceState::ownsPixels() const
36
+ {
37
+ return mode == CanvasSurfaceMode::OwnedBitmap && pixels != nullptr;
38
+ }
39
+
40
+ bool CanvasSurfaceState::displayBacked() const
41
+ {
42
+ return mode == CanvasSurfaceMode::DisplayFramebuffer;
43
+ }
44
+
45
+ gea::framework::graphics::Canvas *CanvasSurfaceState::canvas()
46
+ {
47
+ return activeCanvas;
48
+ }
49
+
50
+ const gea::framework::graphics::Canvas *CanvasSurfaceState::canvas() const
51
+ {
52
+ return activeCanvas;
53
+ }
54
+
55
+ CanvasSurfaceState *CanvasStore::find(int nodeId)
56
+ {
57
+ for (auto *surface = head_; surface; surface = surface->next) {
58
+ if (surface->nodeId == nodeId) return surface;
59
+ }
60
+ return nullptr;
61
+ }
62
+
63
+ const CanvasSurfaceState *CanvasStore::find(int nodeId) const
64
+ {
65
+ for (const auto *surface = head_; surface; surface = surface->next) {
66
+ if (surface->nodeId == nodeId) return surface;
67
+ }
68
+ return nullptr;
69
+ }
70
+
71
+ CanvasSurfaceState *CanvasStore::ensure(int nodeId)
72
+ {
73
+ if (auto *surface = find(nodeId)) return surface;
74
+ auto *surface = new (std::nothrow) CanvasSurfaceState();
75
+ if (!surface) return nullptr;
76
+ surface->nodeId = nodeId;
77
+ surface->next = head_;
78
+ head_ = surface;
79
+ return surface;
80
+ }
81
+
82
+ gea::framework::graphics::Canvas *CanvasStore::bindOwned(CanvasSurfaceState &surface, int width, int height)
83
+ {
84
+ if (width <= 0 || height <= 0) return nullptr;
85
+
86
+ const bool copyFromDisplay = surface.displayBacked() && surface.canvas() &&
87
+ surface.canvas()->pixels() &&
88
+ surface.canvas()->width() == width &&
89
+ surface.canvas()->height() == height;
90
+ const gea::framework::graphics::pixel::native_t *displayPixels = copyFromDisplay ? surface.canvas()->pixels() : nullptr;
91
+
92
+ if (!surface.displayBacked() && surface.pixels && surface.width == width && surface.height == height) {
93
+ surface.activeCanvas = &surface.localCanvas;
94
+ return surface.canvas();
95
+ }
96
+
97
+ releaseOwnedPixels(&surface);
98
+ surface.mode = CanvasSurfaceMode::OwnedBitmap;
99
+ surface.activeCanvas = &surface.localCanvas;
100
+ surface.pixels = nullptr;
101
+ surface.width = 0;
102
+ surface.height = 0;
103
+
104
+ // Cache-line aligned (size rounded up to the alignment) so DMA engines — the
105
+ // ESP32-P4 PPA in particular — can scale/rotate straight into this buffer with
106
+ // no bounce copy. A few padding bytes per canvas is a negligible cost.
107
+ constexpr std::size_t kCanvasAlign = 128;
108
+ const auto pixelBytes = static_cast<std::size_t>(width) * static_cast<std::size_t>(height) * sizeof(gea::framework::graphics::pixel::native_t);
109
+ const auto byteCount = (pixelBytes + (kCanvasAlign - 1)) & ~(kCanvasAlign - 1);
110
+ surface.pixels = static_cast<gea::framework::graphics::pixel::native_t *>(
111
+ gea::framework::memory::Allocator::allocatePreferSpiram(byteCount, kCanvasAlign));
112
+ if (!surface.pixels) return nullptr;
113
+
114
+ surface.width = width;
115
+ surface.height = height;
116
+ surface.localCanvas.bindPixels(surface.pixels, width, height);
117
+ if (displayPixels)
118
+ surface.localCanvas.drawImage(displayPixels, nullptr, width, height, 0, 0);
119
+ else
120
+ surface.localCanvas.clear(0);
121
+ return surface.canvas();
122
+ }
123
+
124
+ void CanvasStore::markDisplayFramebuffer(CanvasSurfaceState &surface, int width, int height)
125
+ {
126
+ if (width <= 0 || height <= 0) return;
127
+ releaseOwnedPixels(&surface);
128
+ surface.mode = CanvasSurfaceMode::DisplayFramebuffer;
129
+ surface.activeCanvas = nullptr;
130
+ surface.pixels = nullptr;
131
+ surface.width = width;
132
+ surface.height = height;
133
+ }
134
+
135
+ gea::framework::graphics::Canvas *CanvasStore::bindDisplayFramebuffer(CanvasSurfaceState &surface,
136
+ gea::framework::graphics::Canvas &displayCanvas)
137
+ {
138
+ if (!displayCanvas.pixels() || displayCanvas.width() <= 0 || displayCanvas.height() <= 0) return nullptr;
139
+ if (surface.displayBacked() &&
140
+ surface.canvas() == &displayCanvas &&
141
+ surface.pixels == displayCanvas.pixels() &&
142
+ surface.width == displayCanvas.width() &&
143
+ surface.height == displayCanvas.height())
144
+ return surface.canvas();
145
+
146
+ const bool copyFromOwned = surface.ownsPixels() &&
147
+ surface.width == displayCanvas.width() &&
148
+ surface.height == displayCanvas.height();
149
+ gea::framework::graphics::pixel::native_t *ownedPixels = copyFromOwned ? surface.pixels : nullptr;
150
+
151
+ if (ownedPixels) displayCanvas.drawImage(ownedPixels, nullptr, surface.width, surface.height, 0, 0);
152
+ releaseOwnedPixels(&surface);
153
+
154
+ surface.mode = CanvasSurfaceMode::DisplayFramebuffer;
155
+ surface.activeCanvas = &displayCanvas;
156
+ surface.pixels = displayCanvas.pixels();
157
+ surface.width = displayCanvas.width();
158
+ surface.height = displayCanvas.height();
159
+ return surface.canvas();
160
+ }
161
+
162
+ void CanvasStore::remove(int nodeId)
163
+ {
164
+ CanvasSurfaceState **link = &head_;
165
+ while (*link) {
166
+ auto *surface = *link;
167
+ if (surface->nodeId != nodeId) {
168
+ link = &surface->next;
169
+ continue;
170
+ }
171
+ *link = surface->next;
172
+ releaseSurface(surface);
173
+ return;
174
+ }
175
+ }
176
+
177
+ void CanvasStore::clear()
178
+ {
179
+ auto *surface = head_;
180
+ while (surface) {
181
+ auto *next = surface->next;
182
+ releaseSurface(surface);
183
+ surface = next;
184
+ }
185
+ head_ = nullptr;
186
+ }
187
+
188
+ } // namespace gea::embedded::ui
@@ -0,0 +1,59 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "canvas.h"
5
+
6
+ #include <cstdint>
7
+
8
+ namespace gea::embedded::ui {
9
+
10
+ enum class CanvasSurfaceMode : std::uint8_t {
11
+ OwnedBitmap,
12
+ DisplayFramebuffer
13
+ };
14
+
15
+ struct CanvasSurfaceState {
16
+ int nodeId = -1;
17
+ CanvasSurfaceMode mode = CanvasSurfaceMode::OwnedBitmap;
18
+ gea::framework::graphics::Canvas localCanvas;
19
+ gea::framework::graphics::Canvas *activeCanvas = nullptr;
20
+ gea::framework::graphics::pixel::native_t *pixels = nullptr;
21
+ int width = 0;
22
+ int height = 0;
23
+ CanvasSurfaceState *next = nullptr;
24
+
25
+ bool ownsPixels() const;
26
+ bool displayBacked() const;
27
+ gea::framework::graphics::Canvas *canvas();
28
+ const gea::framework::graphics::Canvas *canvas() const;
29
+ };
30
+
31
+ class CanvasStore {
32
+ public:
33
+ CanvasSurfaceState *find(int nodeId);
34
+ const CanvasSurfaceState *find(int nodeId) const;
35
+ CanvasSurfaceState *ensure(int nodeId);
36
+ gea::framework::graphics::Canvas *bindOwned(CanvasSurfaceState &surface, int width, int height);
37
+ void markDisplayFramebuffer(CanvasSurfaceState &surface, int width, int height);
38
+ gea::framework::graphics::Canvas *bindDisplayFramebuffer(CanvasSurfaceState &surface,
39
+ gea::framework::graphics::Canvas &displayCanvas);
40
+ void remove(int nodeId);
41
+ void clear();
42
+
43
+ template <typename Fn>
44
+ void forEach(Fn fn)
45
+ {
46
+ for (auto *surface = head_; surface; surface = surface->next) fn(*surface);
47
+ }
48
+
49
+ template <typename Fn>
50
+ void forEach(Fn fn) const
51
+ {
52
+ for (const auto *surface = head_; surface; surface = surface->next) fn(*surface);
53
+ }
54
+
55
+ private:
56
+ CanvasSurfaceState *head_ = nullptr;
57
+ };
58
+
59
+ } // namespace gea::embedded::ui