@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,356 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "document.h"
3
+
4
+ #include "internal.h"
5
+
6
+ #include "display.h"
7
+ #include "tree_internal.h"
8
+
9
+ #include <algorithm>
10
+ #include <cctype>
11
+ #include <cstring>
12
+ #include <utility>
13
+ #include <vector>
14
+ #include <string>
15
+
16
+ namespace gea::embedded::ui {
17
+
18
+ namespace {
19
+
20
+ bool g_direct_canvas_context_used = false;
21
+
22
+ std::string normalizeSelector(const char *raw)
23
+ {
24
+ if (!raw) return std::string();
25
+ std::string selector(raw);
26
+ auto first = std::find_if_not(selector.begin(), selector.end(), [](unsigned char ch) { return std::isspace(ch); });
27
+ auto last = std::find_if_not(selector.rbegin(), selector.rend(), [](unsigned char ch) { return std::isspace(ch); }).base();
28
+ if (first >= last) return std::string();
29
+ return std::string(first, last);
30
+ }
31
+
32
+ std::string lowerAscii(std::string value)
33
+ {
34
+ for (char &ch : value) ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
35
+ return value;
36
+ }
37
+
38
+ bool matchesSelector(Tree &tree, int nodeId, const std::string &selector)
39
+ {
40
+ if (selector.empty()) return false;
41
+ if (selector == "body") {
42
+ const int mountedRoot = tree.mountedRoot();
43
+ return (mountedRoot >= 0 && nodeId == mountedRoot) || (mountedRoot < 0 && nodeId == 0);
44
+ }
45
+ if (selector[0] == '#') {
46
+ if (selector.size() <= 1) return false;
47
+ return std::strcmp(tree.getAttribute(nodeId, "id"), selector.c_str() + 1) == 0;
48
+ }
49
+ if (selector[0] == '.') {
50
+ if (selector.size() <= 1) return false;
51
+ return tree.hasClass(nodeId, selector.substr(1));
52
+ }
53
+ return lowerAscii(tree.tagName(nodeId)) == lowerAscii(selector);
54
+ }
55
+
56
+ NodeHandle findElementById(Tree &tree, const char *id)
57
+ {
58
+ if (!id || id[0] == '\0') return NodeHandle();
59
+ for (int nodeId = 0; nodeId < tree.nodeCount(); nodeId++) {
60
+ if (std::strcmp(tree.getAttribute(nodeId, "id"), id) == 0) return NodeHandle(nodeId);
61
+ }
62
+ return NodeHandle();
63
+ }
64
+
65
+ } // namespace
66
+
67
+ Document &Document::instance()
68
+ {
69
+ static Document document;
70
+ return document;
71
+ }
72
+
73
+ ViewElement Document::body() const
74
+ {
75
+ Tree &tree = Tree::instance();
76
+ if (tree.mountedRoot() >= 0) return ViewElement(tree.mountedRoot());
77
+ if (tree.nodeCount() > 0) return ViewElement(0);
78
+ return ViewElement();
79
+ }
80
+
81
+ ViewElement Document::createView() const
82
+ {
83
+ return ViewElement::create();
84
+ }
85
+
86
+ ViewElement Document::createButton() const
87
+ {
88
+ return ViewElement(Tree::instance().createButton());
89
+ }
90
+
91
+ TextElement Document::createText(const char *text) const
92
+ {
93
+ return TextElement::create(text);
94
+ }
95
+
96
+ ImageElement Document::createImage() const
97
+ {
98
+ return ImageElement::create();
99
+ }
100
+
101
+ CanvasElement Document::createCanvas() const
102
+ {
103
+ return CanvasElement::create();
104
+ }
105
+
106
+ int Document::mountedCanvasNode() const
107
+ {
108
+ auto &tree = Tree::instance();
109
+ const int root = tree.mountedRoot();
110
+ if (root < 0) return -1;
111
+ return tree.node(root).type == NodeType::Canvas ? root : -1;
112
+ }
113
+
114
+ void Document::markDirectCanvasContextUsed()
115
+ {
116
+ g_direct_canvas_context_used = true;
117
+ }
118
+
119
+ bool Document::directCanvasContextUsed()
120
+ {
121
+ return g_direct_canvas_context_used;
122
+ }
123
+
124
+ CameraElement Document::createCamera() const
125
+ {
126
+ return CameraElement::create();
127
+ }
128
+
129
+ NodeHandle Document::createAudio() const
130
+ {
131
+ return NodeHandle(Tree::instance().createAudio());
132
+ }
133
+
134
+ NodeHandle Document::createDocumentFragment() const
135
+ {
136
+ return NodeHandle(Tree::instance().createView());
137
+ }
138
+
139
+ ViewElement Document::createVirtualList() const
140
+ {
141
+ return ViewElement(Tree::instance().createVirtualList());
142
+ }
143
+
144
+ NodeHandle Document::createElement(const char *tag) const
145
+ {
146
+ std::string lowered = tag ? tag : "";
147
+ std::transform(lowered.begin(), lowered.end(), lowered.begin(), [](unsigned char ch) { return std::tolower(ch); });
148
+ NodeHandle node;
149
+ if (lowered == "button") node = createButton();
150
+ else if (lowered == "canvas") node = createCanvas();
151
+ else if (lowered == "audio") node = createAudio();
152
+ else if (lowered == "img" || lowered == "image") node = createImage();
153
+ else if (lowered == "virtual-list") node = createVirtualList();
154
+ else node = createView();
155
+ if (!lowered.empty()) node.setTagName(lowered.c_str());
156
+ return node;
157
+ }
158
+
159
+ NodeHandle Document::getElementById(const char *id) const
160
+ {
161
+ if (!id || id[0] == '\0') return NodeHandle();
162
+ Tree &tree = Tree::instance();
163
+ NodeHandle existing = findElementById(tree, id);
164
+ if (existing) return existing;
165
+ return std::strcmp(id, "app") == 0 ? ensureAppRoot("app") : NodeHandle();
166
+ }
167
+
168
+ NodeHandle Document::querySelector(const char *selector) const
169
+ {
170
+ const std::string normalized = normalizeSelector(selector);
171
+ if (normalized == "body" || normalized == "#app") return ensureAppRoot("app");
172
+ const std::vector<NodeHandle> matches = querySelectorAll(normalized.c_str());
173
+ return matches.empty() ? NodeHandle() : matches.front();
174
+ }
175
+
176
+ std::vector<NodeHandle> Document::querySelectorAll(const char *selector) const
177
+ {
178
+ std::vector<NodeHandle> matches;
179
+ const std::string normalized = normalizeSelector(selector);
180
+ if (normalized.empty()) return matches;
181
+ if (normalized == "body" || normalized == "#app") {
182
+ matches.push_back(ensureAppRoot("app"));
183
+ return matches;
184
+ }
185
+
186
+ Tree &tree = Tree::instance();
187
+ for (int nodeId = 0; nodeId < tree.nodeCount(); nodeId++) {
188
+ if (matchesSelector(tree, nodeId, normalized)) matches.push_back(NodeHandle(nodeId));
189
+ }
190
+ return matches;
191
+ }
192
+
193
+ namespace {
194
+ // Preferred mount size, set by platform targets via Application::init.
195
+ // ensureAppRoot reads this when creating the implicit #app root so
196
+ // fullscreen targets get the panel-sized root instead of kWidth × kHeight.
197
+ int g_preferred_mount_w = 0;
198
+ int g_preferred_mount_h = 0;
199
+
200
+ struct DocumentEventListener {
201
+ std::string type;
202
+ gea::framework::events::EventListener listener;
203
+ int boundRoot = -1;
204
+ };
205
+
206
+ std::vector<DocumentEventListener> &documentEventListeners()
207
+ {
208
+ static std::vector<DocumentEventListener> listeners;
209
+ return listeners;
210
+ }
211
+
212
+ void bindDocumentEventListeners()
213
+ {
214
+ const int root = Tree::instance().mountedRoot();
215
+ if (root < 0) return;
216
+ for (auto &entry : documentEventListeners()) {
217
+ if (entry.boundRoot == root) continue;
218
+ Tree::instance().setEventListener(root, entry.type.c_str(), entry.listener);
219
+ entry.boundRoot = root;
220
+ }
221
+ }
222
+
223
+ // Map a pointer/touch event type to a slot index, aliasing pointer* onto its
224
+ // touch* sibling (the web binds onPointerDown/Move/Up; the device synthesizes
225
+ // touch*). Mirrors eventTypeIndex's pointer/touch grouping in tree_events.cpp.
226
+ int pointerTypeIndex(const char *type)
227
+ {
228
+ if (!type) return -1;
229
+ if (std::strcmp(type, "click") == 0) return 0;
230
+ if (std::strcmp(type, "touchstart") == 0 || std::strcmp(type, "pointerdown") == 0) return 1;
231
+ if (std::strcmp(type, "touchmove") == 0 || std::strcmp(type, "pointermove") == 0) return 2;
232
+ if (std::strcmp(type, "touchend") == 0 || std::strcmp(type, "pointerup") == 0 ||
233
+ std::strcmp(type, "pointercancel") == 0)
234
+ return 3;
235
+ return -1;
236
+ }
237
+ } // namespace
238
+
239
+ bool dispatchDocumentPointer(gea::framework::events::PointerEvent &event)
240
+ {
241
+ const int wantIdx = pointerTypeIndex(event.typeName());
242
+ if (wantIdx < 0) return false;
243
+ bool fired = false;
244
+ for (auto &entry : documentEventListeners()) {
245
+ if (pointerTypeIndex(entry.type.c_str()) != wantIdx) continue;
246
+ if (!static_cast<bool>(entry.listener)) continue;
247
+ entry.listener(event);
248
+ fired = true;
249
+ if (event.propagationStopped) break;
250
+ }
251
+ return fired;
252
+ }
253
+
254
+ void resetDocumentDelegatedEventListeners()
255
+ {
256
+ documentEventListeners().clear();
257
+ }
258
+
259
+ void Document::setPreferredMountSize(int width, int height)
260
+ {
261
+ g_preferred_mount_w = width;
262
+ g_preferred_mount_h = height;
263
+ }
264
+
265
+ int Document::preferredMountWidth() { return g_preferred_mount_w; }
266
+ int Document::preferredMountHeight() { return g_preferred_mount_h; }
267
+
268
+ ViewElement Document::ensureAppRoot(const char *id) const
269
+ {
270
+ const char *rootId = (id && id[0] != '\0') ? id : "app";
271
+ NodeHandle existing = findElementById(Tree::instance(), rootId);
272
+ ViewElement root = existing ? ViewElement(existing.id()) : createView();
273
+ Tree &tree = Tree::instance();
274
+ const int rootW = g_preferred_mount_w > 0 ? g_preferred_mount_w
275
+ : tree.mountedWidth() > 0 ? tree.mountedWidth()
276
+ : gea::platform::display::kWidth;
277
+ const int rootH = g_preferred_mount_h > 0 ? g_preferred_mount_h
278
+ : tree.mountedHeight() > 0 ? tree.mountedHeight()
279
+ : gea::platform::display::kHeight;
280
+ if (!existing) {
281
+ root.setAttribute("id", rootId);
282
+ root.style().width(rootW);
283
+ root.style().height(rootH);
284
+ }
285
+ if (tree.mountedRoot() < 0) mount(root, rootW, rootH);
286
+ return root;
287
+ }
288
+
289
+ void Document::clear() const
290
+ {
291
+ g_direct_canvas_context_used = false;
292
+ Tree::instance().clear();
293
+ }
294
+
295
+ void Document::mount(NodeHandle root, int width, int height) const
296
+ {
297
+ // Override caller-supplied dimensions with the platform's preferred
298
+ // mount size if it set one. The JSX → C++ bundler emits hardcoded
299
+ // `kWidth, kHeight` here, which is the right thing for fixed-size
300
+ // embedded panels but wrong for fullscreen platforms (geaos passes
301
+ // /dev/fb0's dimensions). Targets that don't call
302
+ // Document::setPreferredMountSize keep the existing behavior.
303
+ if (g_preferred_mount_w > 0) {
304
+ width = g_preferred_mount_w;
305
+ // Also override the root's inline width so app CSS (`width: 100vw`)
306
+ // has a parent the right size.
307
+ root.style().width(width);
308
+ }
309
+ if (g_preferred_mount_h > 0) {
310
+ height = g_preferred_mount_h;
311
+ root.style().height(height);
312
+ }
313
+ Tree::instance().mount(root.id(), width, height);
314
+ bindDocumentEventListeners();
315
+ }
316
+
317
+ void Document::refresh(NodeHandle root, int width, int height) const
318
+ {
319
+ Tree::instance().refresh(root.id(), width, height);
320
+ }
321
+
322
+ void Document::refreshMountedIfDirty() const
323
+ {
324
+ if (directCanvasContextUsed()) return;
325
+ // Post-fling image repaint: the first quiet frame after scroll-held image
326
+ // blits marks the tree dirty so this very refresh repaints the covers.
327
+ rootScrollImageHoldTick();
328
+ auto &tree = Tree::instance();
329
+ const int root = tree.mountedRoot();
330
+ if (root < 0) return;
331
+ if (!tree.refreshRequired()) return;
332
+ tree.refresh(root, tree.mountedWidth(), tree.mountedHeight());
333
+ }
334
+
335
+ void Document::frame(int timestampMs) const
336
+ {
337
+ Tree::instance().frame(timestampMs);
338
+ }
339
+
340
+ void Document::addEventListener(const char *type, gea::framework::events::EventListener listener) const
341
+ {
342
+ if (!type || type[0] == '\0' || !listener) return;
343
+ // `rotary` (and `keydown` bound on a non-input) is a genuine document-level
344
+ // event — it has no element target, so it is stored off-tree as a singleton
345
+ // and dispatched directly by the runtime, never bound to a node.
346
+ if (std::strcmp(type, "rotary") == 0 || std::strcmp(type, "keydown") == 0) {
347
+ setDocumentEventListener(type, std::move(listener));
348
+ return;
349
+ }
350
+ // Everything else delegates through the native body node (event bubbles up
351
+ // the tree to the mounted root, where this listener filters by target).
352
+ documentEventListeners().push_back(DocumentEventListener{type, std::move(listener), -1});
353
+ bindDocumentEventListeners();
354
+ }
355
+
356
+ } // namespace gea::embedded::ui
package/ui/document.h ADDED
@@ -0,0 +1,101 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "events.h"
5
+ #include "image.h"
6
+ #include "canvas_element.h"
7
+ #include "camera_element.h"
8
+ #include "node.h"
9
+ #include "text.h"
10
+ #include "view.h"
11
+
12
+ #include <string>
13
+ #include <vector>
14
+
15
+ namespace gea::embedded::ui {
16
+
17
+ class Document {
18
+ public:
19
+ static Document &instance();
20
+
21
+ ViewElement body() const;
22
+ ViewElement createView() const;
23
+ ViewElement createButton() const;
24
+ TextElement createText(const char *text = "") const;
25
+ // geatsc emits std::string arguments at typed call sites (a string literal
26
+ // arrives as `std::string("...")`), so the factories the plugin binds take
27
+ // both forms — the binding emit passes the argument through untouched
28
+ // instead of adapting it with .c_str().
29
+ TextElement createText(const std::string &text) const { return createText(text.c_str()); }
30
+ ImageElement createImage() const;
31
+ CanvasElement createCanvas() const;
32
+ // Node id of the mounted root when it is a <canvas> (the shape a previous
33
+ // Display.ctx() call mounts), else -1. Lets Display.ctx() behave as an
34
+ // idempotent getter instead of rebuilding the document on every call.
35
+ int mountedCanvasNode() const;
36
+ CameraElement createCamera() const;
37
+ NodeHandle createAudio() const;
38
+ // A DocumentFragment for the compiled-component runtime (batched list /
39
+ // conditional children). The embedded tree has no fragment node kind, so this
40
+ // returns a detached container node -- children append to it and it is then
41
+ // appended to the real parent. Not a dissolving fragment, but functional.
42
+ NodeHandle createDocumentFragment() const;
43
+ ViewElement createVirtualList() const;
44
+ // Generic tag-string factory (the DOM document.createElement shape): maps
45
+ // the known embedded tags to their typed creators and any other tag to a
46
+ // plain view, stamping the tag via setTagName so tag-driven behaviours
47
+ // (e.g. the <input> VirtualKeyboard focus hook) still see it. Returns the
48
+ // TYPED handle — boxing happens only if the caller stores it dynamically.
49
+ NodeHandle createElement(const char *tag) const;
50
+ NodeHandle createElement(const std::string &tag) const { return createElement(tag.c_str()); }
51
+ NodeHandle getElementById(const char *id) const;
52
+ // geatsc's `nativeDocumentMethods` binding for `document.getElementById(...)`
53
+ // passes its argument through untouched (see `createElement`'s identical
54
+ // comment above) — the compiler's `host-document-calls.ts` only ever plans a
55
+ // template of the exact shape `symbol({arg0}, ...)`, never one with a
56
+ // `.c_str()` (or any other) adaptation baked in, so the `const char *`
57
+ // overload alone left every typed `document.getElementById(id)` with no call
58
+ // plan at all. Both overloads exist so whichever argument form geatsc emits
59
+ // (string literal or `std::string`) resolves without the plugin needing to
60
+ // adapt it.
61
+ NodeHandle getElementById(const std::string &id) const { return getElementById(id.c_str()); }
62
+ NodeHandle querySelector(const char *selector) const;
63
+ NodeHandle querySelector(const std::string &selector) const { return querySelector(selector.c_str()); }
64
+ std::vector<NodeHandle> querySelectorAll(const char *selector) const;
65
+ std::vector<NodeHandle> querySelectorAll(const std::string &selector) const { return querySelectorAll(selector.c_str()); }
66
+ ViewElement ensureAppRoot(const char *id = "app") const;
67
+
68
+ // Platforms set this before Application::init to control the size of
69
+ // the implicit #app root created by ensureAppRoot — e.g. geaos passes
70
+ // the /dev/fb0 dimensions so apps that use `width: 100vw` get the full
71
+ // panel instead of the framework's default 410×502.
72
+ static void setPreferredMountSize(int width, int height);
73
+ static int preferredMountWidth();
74
+ static int preferredMountHeight();
75
+ static void markDirectCanvasContextUsed();
76
+ static bool directCanvasContextUsed();
77
+
78
+ void clear() const;
79
+ void mount(NodeHandle root, int width, int height) const;
80
+ void refresh(NodeHandle root, int width, int height) const;
81
+ void refreshMountedIfDirty() const;
82
+ void frame(int timestampMs) const;
83
+ void addEventListener(const char *type, gea::framework::events::EventListener listener) const;
84
+
85
+ private:
86
+ Document() = default;
87
+ };
88
+
89
+ // Bracket the initial app mount. Between these calls, per-node class-style
90
+ // recomputes are deferred so a large tree + large stylesheet don't mount in
91
+ // O(nodes^2 x rules); endStyleMountBatch() runs one recomputeAllClassStyles()
92
+ // pass with identical results. Defined in style.cpp.
93
+ void beginStyleMountBatch();
94
+ void endStyleMountBatch();
95
+ // True while an initial-mount style batch is open (between begin/endStyleMountBatch).
96
+ // During this window per-node class styles are NOT yet applied, so anything that
97
+ // presents to the panel would show unstyled content; the render path uses this to
98
+ // suppress that flush until styles land. Defined in style.cpp.
99
+ bool styleMountBatchActive();
100
+
101
+ } // namespace gea::embedded::ui
package/ui/image.h ADDED
@@ -0,0 +1,16 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #pragma once
3
+
4
+ #include "node.h"
5
+
6
+ namespace gea::embedded::ui {
7
+
8
+ class ImageElement : public NodeHandle {
9
+ public:
10
+ using NodeHandle::NodeHandle;
11
+ static ImageElement create();
12
+ void imageId(int id) const { style().imageId(id); }
13
+ void fit(int value) const { style().imageFit(value); }
14
+ };
15
+
16
+ } // namespace gea::embedded::ui
@@ -0,0 +1,13 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "image.h"
3
+
4
+ #include "tree_internal.h"
5
+
6
+ namespace gea::embedded::ui {
7
+
8
+ ImageElement ImageElement::create()
9
+ {
10
+ return ImageElement(Tree::instance().createImage());
11
+ }
12
+
13
+ } // namespace gea::embedded::ui
@@ -0,0 +1,165 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "internal.h"
3
+ #include "image.h"
4
+
5
+ #include <image.h>
6
+
7
+ namespace gea::embedded::ui {
8
+
9
+ namespace {
10
+
11
+ struct ImageFitRect {
12
+ int x = 0;
13
+ int y = 0;
14
+ int width = 0;
15
+ int height = 0;
16
+ };
17
+
18
+ class ImageFitResolver {
19
+ public:
20
+ static ImageFitRect resolve(const Node &node, int imageWidth, int imageHeight)
21
+ {
22
+ ImageFitRect rect{
23
+ node.layout.x,
24
+ node.layout.y,
25
+ node.layout.width,
26
+ node.layout.height,
27
+ };
28
+
29
+ if (node.style.image_fit == 1 || node.style.image_fit == 4) {
30
+ const int scaledWidth = (imageWidth * node.layout.height) / imageHeight;
31
+ const int scaledHeight = (imageHeight * node.layout.width) / imageWidth;
32
+ if (scaledWidth <= node.layout.width) {
33
+ rect.width = scaledWidth;
34
+ rect.height = node.layout.height;
35
+ } else {
36
+ rect.width = node.layout.width;
37
+ rect.height = scaledHeight;
38
+ }
39
+ if (node.style.image_fit == 4 && rect.width >= imageWidth && rect.height >= imageHeight) {
40
+ rect.width = imageWidth;
41
+ rect.height = imageHeight;
42
+ }
43
+ center(node, rect);
44
+ } else if (node.style.image_fit == 2) {
45
+ const int scaledWidth = (imageWidth * node.layout.height) / imageHeight;
46
+ const int scaledHeight = (imageHeight * node.layout.width) / imageWidth;
47
+ if (scaledWidth >= node.layout.width) {
48
+ rect.width = scaledWidth;
49
+ rect.height = node.layout.height;
50
+ } else {
51
+ rect.width = node.layout.width;
52
+ rect.height = scaledHeight;
53
+ }
54
+ center(node, rect);
55
+ } else if (node.style.image_fit == 3) {
56
+ rect.width = imageWidth;
57
+ rect.height = imageHeight;
58
+ center(node, rect);
59
+ }
60
+
61
+ return rect;
62
+ }
63
+
64
+ private:
65
+ static void center(const Node &node, ImageFitRect &rect)
66
+ {
67
+ rect.x = node.layout.x + (node.layout.width - rect.width) / 2;
68
+ rect.y = node.layout.y + (node.layout.height - rect.height) / 2;
69
+ }
70
+ };
71
+
72
+ bool hasImageRadius(const Node &node)
73
+ {
74
+ return node.style.border_radius[0] > 0 ||
75
+ node.style.border_radius[1] > 0 ||
76
+ node.style.border_radius[2] > 0 ||
77
+ node.style.border_radius[3] > 0;
78
+ }
79
+
80
+ void setScaledBlitRadius(DisplayCommand *cmd, const Node &node)
81
+ {
82
+ cmd->scaledBlit.tl = node.style.border_radius[0];
83
+ cmd->scaledBlit.tr = node.style.border_radius[1];
84
+ cmd->scaledBlit.br = node.style.border_radius[2];
85
+ cmd->scaledBlit.bl = node.style.border_radius[3];
86
+ }
87
+
88
+ bool applyAxisAlignedTransform(const Node &node, ImageFitRect &rect)
89
+ {
90
+ int16_t xs[4] = {};
91
+ int16_t ys[4] = {};
92
+ ViewRenderer::transformedRectCorners(node, false, rect.x, rect.y, rect.width, rect.height, xs, ys);
93
+ if (xs[0] != xs[3] || xs[1] != xs[2] || ys[0] != ys[1] || ys[2] != ys[3]) return false;
94
+ if (xs[1] < xs[0] || ys[2] < ys[0]) return false;
95
+
96
+ rect.x = xs[0];
97
+ rect.y = ys[0];
98
+ rect.width = xs[1] - xs[0];
99
+ rect.height = ys[2] - ys[0];
100
+ return rect.width > 0 && rect.height > 0;
101
+ }
102
+
103
+ } // namespace
104
+
105
+ void ImageRenderer::layout(int id)
106
+ {
107
+ Node *n = &Tree::instance().nodes()[id];
108
+ auto &images = gea::framework::graphics::ImageStore::instance();
109
+ int iw = images.width(n->image_id);
110
+ int ih = images.height(n->image_id);
111
+
112
+ if (n->style.width == kUnset && n->style.width_percent == kUnset && iw > 0) n->layout.width = iw;
113
+ if (n->style.height == kUnset && n->style.height_percent == kUnset && ih > 0) n->layout.height = ih;
114
+ n->layout.width = LayoutEngine::instance().clampSize(n->layout.width, n->style.min_width, n->style.max_width);
115
+ n->layout.height = LayoutEngine::instance().clampSize(n->layout.height, n->style.min_height, n->style.max_height);
116
+ }
117
+
118
+ void ImageRenderer::record(const Node &node)
119
+ {
120
+ const Node *n = &node;
121
+ if (n->type != NodeType::Image || n->image_id < 0) return;
122
+
123
+ auto &images = gea::framework::graphics::ImageStore::instance();
124
+ const gea::framework::graphics::pixel::native_t *pixels = images.currentPixels(n->image_id);
125
+ if (!pixels) return;
126
+ const uint8_t *alpha = images.currentAlpha(n->image_id);
127
+
128
+ int iw = images.width(n->image_id);
129
+ int ih = images.height(n->image_id);
130
+ if (iw <= 0 || ih <= 0) return;
131
+
132
+ ImageFitRect fit = ImageFitResolver::resolve(*n, iw, ih);
133
+ applyAxisAlignedTransform(*n, fit);
134
+
135
+ if (iw == fit.width && ih == fit.height && !hasImageRadius(*n)) {
136
+ DisplayCommand *cmd = DisplayList::instance().append();
137
+ if (!cmd) return;
138
+ cmd->type = DisplayCommandType::BlitImage;
139
+ cmd->bx = fit.x; cmd->by = fit.y; cmd->bw = fit.width; cmd->bh = fit.height;
140
+ cmd->blit.pixels = pixels;
141
+ cmd->blit.alpha = alpha;
142
+ cmd->blit.sourceWidth = iw;
143
+ cmd->blit.sourceHeight = ih;
144
+ cmd->blit.dx = fit.x;
145
+ cmd->blit.dy = fit.y;
146
+ cmd->blit.sourcePacked = 0; // decoded images are unpacked (one value per byte)
147
+ return;
148
+ }
149
+
150
+ DisplayCommand *cmd = DisplayList::instance().append();
151
+ if (!cmd) return;
152
+ cmd->type = DisplayCommandType::BlitImageScaled;
153
+ cmd->bx = fit.x; cmd->by = fit.y; cmd->bw = fit.width; cmd->bh = fit.height;
154
+ cmd->scaledBlit.pixels = pixels;
155
+ cmd->scaledBlit.alpha = alpha;
156
+ cmd->scaledBlit.sourceWidth = iw;
157
+ cmd->scaledBlit.sourceHeight = ih;
158
+ cmd->scaledBlit.dx = fit.x;
159
+ cmd->scaledBlit.dy = fit.y;
160
+ cmd->scaledBlit.dw = fit.width;
161
+ cmd->scaledBlit.dh = fit.height;
162
+ setScaledBlitRadius(cmd, *n);
163
+ }
164
+
165
+ } // namespace gea::embedded::ui