@geastack/engine 0.1.20 → 0.1.22
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.
- package/canvas.cpp +150 -27
- package/image_store.cpp +308 -1
- package/package.json +1 -1
- package/ui/CSS_FEATURES.md +51 -0
- package/ui/canvas_element.cpp +12 -16
- package/ui/core.cpp +10 -7
- package/ui/document.cpp +25 -0
- package/ui/input.cpp +341 -11
- package/ui/input_render.cpp +62 -17
- package/ui/input_retirement.h +3 -0
- package/ui/internal.h +38 -1
- package/ui/layout.cpp +68 -16
- package/ui/layout_snapshot.cpp +119 -70
- package/ui/layout_snapshot.h +1 -0
- package/ui/node_features.h +6 -0
- package/ui/node_model.h +23 -2
- package/ui/render.cpp +1154 -64
- package/ui/root_scroll_refresh.cpp +372 -66
- package/ui/root_scroll_refresh.h +4 -0
- package/ui/style.cpp +74 -21
- package/ui/style.h +18 -1
- package/ui/svg_element.h +98 -0
- package/ui/svg_element_impl.h +1589 -0
- package/ui/svg_geometry.h +1767 -0
- package/ui/text.cpp +7 -6
- package/ui/tree_events.cpp +15 -0
- package/ui/tree_internal.h +12 -0
- package/ui/tree_nodes.cpp +37 -2
- package/ui/tree_render.cpp +290 -22
- package/ui/tree_state.h +9 -0
- package/ui/tree_style.cpp +32 -0
- package/ui/view.cpp +27 -9
- package/ui/viewport_region.cpp +53 -0
- package/ui/viewport_region.h +3 -0
- package/ui/virtual_keyboard.cpp +279 -23
- package/ui/virtual_keyboard_icons.h +114 -0
|
@@ -0,0 +1,1589 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
//
|
|
3
|
+
// Implementation of svg_element.h. Included once, from tree_render.cpp, so
|
|
4
|
+
// every target's existing engine source list builds it with no manifest
|
|
5
|
+
// change. Do not include it anywhere else.
|
|
6
|
+
|
|
7
|
+
#if GEA_UI_SVG
|
|
8
|
+
|
|
9
|
+
#include "svg_element.h"
|
|
10
|
+
#include "svg_geometry.h"
|
|
11
|
+
|
|
12
|
+
#include "display.h"
|
|
13
|
+
#include "internal.h"
|
|
14
|
+
#include "tree_internal.h"
|
|
15
|
+
#include "tree_state.h"
|
|
16
|
+
|
|
17
|
+
#include <algorithm>
|
|
18
|
+
#include <atomic>
|
|
19
|
+
#include <climits>
|
|
20
|
+
#include <cstdio>
|
|
21
|
+
#include <cstring>
|
|
22
|
+
#include <string>
|
|
23
|
+
#include <unordered_map>
|
|
24
|
+
#include <utility>
|
|
25
|
+
#include <vector>
|
|
26
|
+
|
|
27
|
+
#if __has_include("freertos/FreeRTOS.h")
|
|
28
|
+
#include "freertos/FreeRTOS.h"
|
|
29
|
+
#include "freertos/task.h"
|
|
30
|
+
#define GEA_SVG_HAS_FREERTOS 1
|
|
31
|
+
#else
|
|
32
|
+
#define GEA_SVG_HAS_FREERTOS 0
|
|
33
|
+
#endif
|
|
34
|
+
#if __has_include("pico.h")
|
|
35
|
+
#include "pico.h"
|
|
36
|
+
#define GEA_SVG_HAS_PICO 1
|
|
37
|
+
#else
|
|
38
|
+
#define GEA_SVG_HAS_PICO 0
|
|
39
|
+
#endif
|
|
40
|
+
|
|
41
|
+
namespace gea::embedded::ui::svg {
|
|
42
|
+
|
|
43
|
+
namespace {
|
|
44
|
+
|
|
45
|
+
enum class Kind : std::uint8_t { None, Svg, Group, Path, Polyline, Polygon, Line, Circle, Ellipse, Rect, Skip };
|
|
46
|
+
|
|
47
|
+
struct TagIds {
|
|
48
|
+
std::int16_t svg, g, path, polyline, polygon, line, circle, ellipse, rect, defs, title, desc, text, linearGradient, stop;
|
|
49
|
+
// The element factory lowercases tag names the HTML way, so a JSX
|
|
50
|
+
// <linearGradient> arrives as "lineargradient"; accept both spellings.
|
|
51
|
+
std::int16_t linearGradientLower;
|
|
52
|
+
bool isLinearGradient(std::int16_t tag) const { return tag == linearGradient || tag == linearGradientLower; }
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const TagIds &tagIds()
|
|
56
|
+
{
|
|
57
|
+
static const TagIds ids = {internTag("svg"), internTag("g"), internTag("path"), internTag("polyline"),
|
|
58
|
+
internTag("polygon"), internTag("line"), internTag("circle"), internTag("ellipse"),
|
|
59
|
+
internTag("rect"), internTag("defs"), internTag("title"), internTag("desc"),
|
|
60
|
+
internTag("text"), internTag("linearGradient"), internTag("stop"),
|
|
61
|
+
internTag("lineargradient")};
|
|
62
|
+
return ids;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// -2 until the first SVG tag is interned: isRoot is then a single compare that
|
|
66
|
+
// can never match, and the tag table is never touched by apps without <svg>.
|
|
67
|
+
std::int16_t g_svgTag = -2;
|
|
68
|
+
|
|
69
|
+
Kind kindOf(std::int16_t tag)
|
|
70
|
+
{
|
|
71
|
+
if (g_svgTag == -2) return Kind::None;
|
|
72
|
+
const TagIds &t = tagIds();
|
|
73
|
+
if (tag == t.svg) return Kind::Svg;
|
|
74
|
+
if (tag == t.g) return Kind::Group;
|
|
75
|
+
if (tag == t.path) return Kind::Path;
|
|
76
|
+
if (tag == t.polyline) return Kind::Polyline;
|
|
77
|
+
if (tag == t.polygon) return Kind::Polygon;
|
|
78
|
+
if (tag == t.line) return Kind::Line;
|
|
79
|
+
if (tag == t.circle) return Kind::Circle;
|
|
80
|
+
if (tag == t.ellipse) return Kind::Ellipse;
|
|
81
|
+
if (tag == t.rect) return Kind::Rect;
|
|
82
|
+
if (tag == t.defs || tag == t.title || tag == t.desc || tag == t.text || t.isLinearGradient(tag) || tag == t.stop) return Kind::Skip;
|
|
83
|
+
return Kind::None;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
bool isSvgTagName(const char *tag)
|
|
87
|
+
{
|
|
88
|
+
static const char *const names[] = {"svg", "g", "path", "polyline", "polygon", "line", "circle",
|
|
89
|
+
"ellipse", "rect", "defs", "title", "desc", "text", "linearGradient", "lineargradient", "stop"};
|
|
90
|
+
for (const char *n : names)
|
|
91
|
+
if (std::strcmp(tag, n) == 0) return true;
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
struct Attrs {
|
|
96
|
+
std::vector<std::pair<std::string, std::string>> values;
|
|
97
|
+
std::uint32_t generation = 0;
|
|
98
|
+
const char *get(const char *name) const
|
|
99
|
+
{
|
|
100
|
+
for (const auto &kv : values)
|
|
101
|
+
if (kv.first == name) return kv.second.c_str();
|
|
102
|
+
return nullptr;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// A linearGradient paint: its vector (svg-local pixels, or fractions of the
|
|
107
|
+
// shape's box with bboxUnits) and up to kMaxStops stops, opacity folded in.
|
|
108
|
+
struct GradientPaint {
|
|
109
|
+
static constexpr int kMaxStops = 8;
|
|
110
|
+
bool bboxUnits = true;
|
|
111
|
+
float x1 = 0, y1 = 0, x2 = 1, y2 = 0;
|
|
112
|
+
int stops = 0;
|
|
113
|
+
float offset[kMaxStops] = {};
|
|
114
|
+
Color color[kMaxStops];
|
|
115
|
+
float alpha[kMaxStops] = {};
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
struct ResolvedPaint {
|
|
119
|
+
bool on = false;
|
|
120
|
+
gea::framework::graphics::pixel::native_t color = 0;
|
|
121
|
+
float alpha = 1.0f;
|
|
122
|
+
bool gradient = false;
|
|
123
|
+
GradientPaint grad;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// A rebuild's damage as a few boxes rather than one: an svg whose shapes
|
|
127
|
+
// change in separate places (two ends of an arc, three gauges' tips) repaints
|
|
128
|
+
// each place, not the box spanning them. Boxes that touch, or whose union
|
|
129
|
+
// costs little more than both, merge; past kMax the cheapest union is taken.
|
|
130
|
+
struct DamageSet {
|
|
131
|
+
static constexpr int kMax = 4;
|
|
132
|
+
Bounds box[kMax];
|
|
133
|
+
int count = 0;
|
|
134
|
+
bool empty() const { return count == 0; }
|
|
135
|
+
static float area(const Bounds &b) { return (b.x1 - b.x0) * (b.y1 - b.y0); }
|
|
136
|
+
static Bounds unite(const Bounds &a, const Bounds &b)
|
|
137
|
+
{
|
|
138
|
+
Bounds u = a;
|
|
139
|
+
u.add(Point{b.x0, b.y0});
|
|
140
|
+
u.add(Point{b.x1, b.y1});
|
|
141
|
+
return u;
|
|
142
|
+
}
|
|
143
|
+
void add(const Bounds &b)
|
|
144
|
+
{
|
|
145
|
+
if (b.empty()) return;
|
|
146
|
+
for (int i = 0; i < count; ++i) {
|
|
147
|
+
const Bounds u = unite(box[i], b);
|
|
148
|
+
const bool touch = box[i].x0 <= b.x1 && b.x0 <= box[i].x1 && box[i].y0 <= b.y1 && b.y0 <= box[i].y1;
|
|
149
|
+
if (touch || area(u) <= area(box[i]) + area(b) + 256.0f) {
|
|
150
|
+
box[i] = u;
|
|
151
|
+
// The grown box may now reach another: fold until stable.
|
|
152
|
+
for (int j = 0; j < count; ++j) {
|
|
153
|
+
if (j == i) continue;
|
|
154
|
+
const Bounds v = unite(box[i], box[j]);
|
|
155
|
+
if (area(v) <= area(box[i]) + area(box[j]) + 256.0f) {
|
|
156
|
+
box[i] = v;
|
|
157
|
+
box[j] = box[--count];
|
|
158
|
+
if (i == count) i = j;
|
|
159
|
+
j = -1;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (count < kMax) {
|
|
166
|
+
box[count++] = b;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
int best = 0;
|
|
170
|
+
float bestCost = 1e30f;
|
|
171
|
+
for (int i = 0; i < count; ++i) {
|
|
172
|
+
const float cost = area(unite(box[i], b)) - area(box[i]);
|
|
173
|
+
if (cost < bestCost) bestCost = cost, best = i;
|
|
174
|
+
}
|
|
175
|
+
box[best] = unite(box[best], b);
|
|
176
|
+
}
|
|
177
|
+
void clear() { count = 0; }
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
struct Item {
|
|
181
|
+
int node = -1;
|
|
182
|
+
Path geometry;
|
|
183
|
+
EdgeList fillEdges, strokeEdges; // what paint rasterizes, built once per scene
|
|
184
|
+
ResolvedPaint fill, stroke;
|
|
185
|
+
bool evenOdd = false;
|
|
186
|
+
bool hittable = true;
|
|
187
|
+
float halfStroke = 0.0f;
|
|
188
|
+
Bounds fillBounds, strokeBounds;
|
|
189
|
+
// Everything the geometry and edges depend on (attributes, transform,
|
|
190
|
+
// stroke style): an unchanged item keeps them across scene rebuilds, so a
|
|
191
|
+
// frame that moves one shape re-flattens only that shape.
|
|
192
|
+
std::uint64_t geometryKey = 0;
|
|
193
|
+
// The key without the node's attributes: two builds of one node with the
|
|
194
|
+
// same styleKey differ only in their path, so damage is the path's diff.
|
|
195
|
+
std::uint64_t styleKey = 0;
|
|
196
|
+
// The transform's translation when the geometry was built. The key leaves
|
|
197
|
+
// translation out, so a shape that only moved (a scrolling chart) shifts
|
|
198
|
+
// its edges instead of re-flattening and re-stroking.
|
|
199
|
+
float tx = 0.0f, ty = 0.0f;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
void translateBounds(Bounds &b, float dx, float dy)
|
|
203
|
+
{
|
|
204
|
+
if (b.empty()) return;
|
|
205
|
+
b.x0 += dx;
|
|
206
|
+
b.x1 += dx;
|
|
207
|
+
b.y0 += dy;
|
|
208
|
+
b.y1 += dy;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
void translateItem(Item &item, float dx, float dy)
|
|
212
|
+
{
|
|
213
|
+
for (EdgeList *edges : {&item.fillEdges, &item.strokeEdges})
|
|
214
|
+
for (Edge &e : *edges) {
|
|
215
|
+
e.a.x += dx;
|
|
216
|
+
e.a.y += dy;
|
|
217
|
+
e.b.x += dx;
|
|
218
|
+
e.b.y += dy;
|
|
219
|
+
e.top += dy;
|
|
220
|
+
e.bottom += dy;
|
|
221
|
+
}
|
|
222
|
+
for (Contour &c : item.geometry.contours)
|
|
223
|
+
for (Point &p : c) {
|
|
224
|
+
p.x += dx;
|
|
225
|
+
p.y += dy;
|
|
226
|
+
}
|
|
227
|
+
translateBounds(item.fillBounds, dx, dy);
|
|
228
|
+
translateBounds(item.strokeBounds, dx, dy);
|
|
229
|
+
item.tx += dx;
|
|
230
|
+
item.ty += dy;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
struct Scene {
|
|
234
|
+
bool dirty = true;
|
|
235
|
+
bool recorded = false;
|
|
236
|
+
// The display list's record serial when the scene was made: once a whole
|
|
237
|
+
// recording has run since and still left the svg out, its box lay outside
|
|
238
|
+
// the record clip (see invalidateRoot).
|
|
239
|
+
std::uint32_t createdSerial = DisplayList::instance().recordSerial();
|
|
240
|
+
bool built = false; // built at least once
|
|
241
|
+
int width = -1, height = -1;
|
|
242
|
+
std::uint64_t signature = 0;
|
|
243
|
+
std::vector<Item> items;
|
|
244
|
+
// Damage of the last rebuild, in svg-local pixels: the old and new bounds
|
|
245
|
+
// of every item whose geometry or paint changed, and of every item that
|
|
246
|
+
// went away. damageFull asks for the whole box (first build, resize,
|
|
247
|
+
// z-order change, or a change to the svg box itself). Consumed once by
|
|
248
|
+
// refresh's dirty collection through damage().
|
|
249
|
+
bool damageValid = false;
|
|
250
|
+
bool damageFull = true;
|
|
251
|
+
DamageSet damage;
|
|
252
|
+
// The svg box as last recorded, to tell a scene-only change apart from a
|
|
253
|
+
// change to the box's own paint (background, border, position).
|
|
254
|
+
bool boxChanged = true;
|
|
255
|
+
bool haveBox = false;
|
|
256
|
+
int boxX = 0, boxY = 0;
|
|
257
|
+
alignas(ComputedStyle) unsigned char boxStyle[sizeof(ComputedStyle)] = {};
|
|
258
|
+
// Where paint splits the box's rows between the two cores, as a fraction
|
|
259
|
+
// of its height, steered each paint toward equal time on both.
|
|
260
|
+
float split = 0.5f;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
std::unordered_map<int, Attrs> g_attrs;
|
|
264
|
+
std::unordered_map<int, Scene> g_scenes;
|
|
265
|
+
|
|
266
|
+
Tree &tree() { return Tree::instance(); }
|
|
267
|
+
|
|
268
|
+
Kind kindOfNode(int node)
|
|
269
|
+
{
|
|
270
|
+
auto &state = treeState();
|
|
271
|
+
if (node < 0 || node >= state.nodeCount || !state.nodeActive[node]) return Kind::None;
|
|
272
|
+
return kindOf(state.nodes[node].tag_id);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
int rootOf(int node)
|
|
276
|
+
{
|
|
277
|
+
auto &state = treeState();
|
|
278
|
+
for (int n = node; n >= 0 && n < state.nodeCount; n = state.nodes[n].parent)
|
|
279
|
+
if (state.nodes[n].tag_id == g_svgTag) return n;
|
|
280
|
+
return -1;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
void invalidateRoot(int root)
|
|
284
|
+
{
|
|
285
|
+
auto it = g_scenes.find(root);
|
|
286
|
+
if (it == g_scenes.end()) return;
|
|
287
|
+
it->second.dirty = true;
|
|
288
|
+
auto &state = treeState();
|
|
289
|
+
Node &n = state.nodes[root];
|
|
290
|
+
tree().markNodeDisplayCommandsDirty(root);
|
|
291
|
+
// A first DrawSvg needs a fresh recording; later ones are replaced in place.
|
|
292
|
+
// Not when a recording since the scene was made already left it out: the
|
|
293
|
+
// svg is outside the record clip (below the fold, or on a pager page out of
|
|
294
|
+
// view), a new recording would leave it out again, and whatever brings it
|
|
295
|
+
// into view (a scroll, a relayout) records it then. Asking anyway rebuilt
|
|
296
|
+
// the whole list every frame for the CrowPanel demo's animated meters on
|
|
297
|
+
// the Analytics page, wherever the pager stood: ~31 ms a frame.
|
|
298
|
+
if (!it->second.recorded && DisplayList::instance().recordSerial() == it->second.createdSerial)
|
|
299
|
+
tree().markDisplayListContentDirty();
|
|
300
|
+
n.render.dirty = 1;
|
|
301
|
+
#if GEA_CSS_SCROLLING
|
|
302
|
+
n.render.non_scroll_dirty = 1;
|
|
303
|
+
#endif
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const char *attr(int node, const char *name)
|
|
307
|
+
{
|
|
308
|
+
auto it = g_attrs.find(node);
|
|
309
|
+
return it == g_attrs.end() ? nullptr : it->second.get(name);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
float attrLength(int node, const char *name, float fallback)
|
|
313
|
+
{
|
|
314
|
+
float v = fallback;
|
|
315
|
+
const char *s = attr(node, name);
|
|
316
|
+
if (s && parseLength(s, v)) return v;
|
|
317
|
+
return fallback;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
float attrNumber(int node, const char *name, float fallback)
|
|
321
|
+
{
|
|
322
|
+
const char *s = attr(node, name);
|
|
323
|
+
if (!s) return fallback;
|
|
324
|
+
Scanner sc(s);
|
|
325
|
+
float v = fallback;
|
|
326
|
+
if (!sc.number(v)) return fallback;
|
|
327
|
+
if (*sc.rest() == '%') v /= 100.0f;
|
|
328
|
+
return v;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Inherited paint state while walking the svg subtree.
|
|
332
|
+
struct Context {
|
|
333
|
+
Paint fill{PaintKind::Color, Color{0, 0, 0, 255}};
|
|
334
|
+
Paint stroke{PaintKind::None, Color{}};
|
|
335
|
+
float strokeWidth = 1.0f;
|
|
336
|
+
float fillOpacity = 1.0f, strokeOpacity = 1.0f;
|
|
337
|
+
LineCap cap = LineCap::Butt;
|
|
338
|
+
LineJoin join = LineJoin::Miter;
|
|
339
|
+
float miterLimit = 4.0f;
|
|
340
|
+
bool evenOdd = false;
|
|
341
|
+
bool visible = true;
|
|
342
|
+
bool hittable = true;
|
|
343
|
+
float opacity = 1.0f; // group opacity, folded into each shape
|
|
344
|
+
Matrix matrix;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
ResolvedPaint resolvePaint(const Paint &paint, const Node &node, float opacity)
|
|
348
|
+
{
|
|
349
|
+
ResolvedPaint out;
|
|
350
|
+
if (paint.kind == PaintKind::None || paint.kind == PaintKind::Unset) return out;
|
|
351
|
+
if (paint.kind == PaintKind::CurrentColor) {
|
|
352
|
+
out.color = static_cast<gea::framework::graphics::pixel::native_t>(node.computedStyle().text_color);
|
|
353
|
+
out.alpha = opacity * node.computedStyle().text_alpha / 255.0f;
|
|
354
|
+
} else {
|
|
355
|
+
out.color = gea::framework::graphics::pixel::nativeColor(paint.color.r, paint.color.g, paint.color.b);
|
|
356
|
+
out.alpha = opacity * paint.color.a / 255.0f;
|
|
357
|
+
}
|
|
358
|
+
out.on = out.alpha > 0.002f;
|
|
359
|
+
return out;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
void applyNodePaint(int id, const Node &node, Context &ctx)
|
|
363
|
+
{
|
|
364
|
+
// Presentation attributes first; CSS (RareStyle) overrides them.
|
|
365
|
+
if (const char *s = attr(id, "fill")) { const Paint p = parsePaint(s); if (p.kind != PaintKind::Unset) ctx.fill = p; }
|
|
366
|
+
if (const char *s = attr(id, "stroke")) { const Paint p = parsePaint(s); if (p.kind != PaintKind::Unset) ctx.stroke = p; }
|
|
367
|
+
if (const char *s = attr(id, "stroke-width")) { float w; if (parseLength(s, w) && w >= 0) ctx.strokeWidth = w; }
|
|
368
|
+
if (attr(id, "fill-opacity")) ctx.fillOpacity = std::clamp(attrNumber(id, "fill-opacity", 1), 0.0f, 1.0f);
|
|
369
|
+
if (attr(id, "stroke-opacity")) ctx.strokeOpacity = std::clamp(attrNumber(id, "stroke-opacity", 1), 0.0f, 1.0f);
|
|
370
|
+
if (attr(id, "stroke-miterlimit")) ctx.miterLimit = std::max(1.0f, attrNumber(id, "stroke-miterlimit", 4));
|
|
371
|
+
if (const char *s = attr(id, "stroke-linecap")) {
|
|
372
|
+
if (!std::strcmp(s, "round")) ctx.cap = LineCap::Round;
|
|
373
|
+
else if (!std::strcmp(s, "square")) ctx.cap = LineCap::Square;
|
|
374
|
+
else if (!std::strcmp(s, "butt")) ctx.cap = LineCap::Butt;
|
|
375
|
+
}
|
|
376
|
+
if (const char *s = attr(id, "stroke-linejoin")) {
|
|
377
|
+
if (!std::strcmp(s, "round")) ctx.join = LineJoin::Round;
|
|
378
|
+
else if (!std::strcmp(s, "bevel")) ctx.join = LineJoin::Bevel;
|
|
379
|
+
else if (!std::strncmp(s, "miter", 5)) ctx.join = LineJoin::Miter;
|
|
380
|
+
}
|
|
381
|
+
if (const char *s = attr(id, "fill-rule")) ctx.evenOdd = !std::strcmp(s, "evenodd");
|
|
382
|
+
if (const char *s = attr(id, "visibility")) ctx.visible = std::strcmp(s, "hidden") != 0 && std::strcmp(s, "collapse") != 0;
|
|
383
|
+
if (const char *s = attr(id, "pointer-events")) ctx.hittable = std::strcmp(s, "none") != 0;
|
|
384
|
+
|
|
385
|
+
const RareStyle &rare = rstyle(node.computedStyle());
|
|
386
|
+
if (rare.svg_fill) ctx.fill = decodePaint(rare.svg_fill);
|
|
387
|
+
if (rare.svg_stroke) ctx.stroke = decodePaint(rare.svg_stroke);
|
|
388
|
+
if (rare.svg_stroke_width >= 0) ctx.strokeWidth = rare.svg_stroke_width / 100.0f;
|
|
389
|
+
if (rare.svg_fill_opacity >= 0) ctx.fillOpacity = rare.svg_fill_opacity / 1000.0f;
|
|
390
|
+
if (rare.svg_stroke_opacity >= 0) ctx.strokeOpacity = rare.svg_stroke_opacity / 1000.0f;
|
|
391
|
+
if (rare.svg_stroke_linecap >= 0) ctx.cap = static_cast<LineCap>(rare.svg_stroke_linecap);
|
|
392
|
+
if (rare.svg_stroke_linejoin >= 0) ctx.join = static_cast<LineJoin>(rare.svg_stroke_linejoin);
|
|
393
|
+
if (rare.svg_fill_rule >= 0) ctx.evenOdd = rare.svg_fill_rule == 1;
|
|
394
|
+
if (node.computedStyle().visibility != 0) ctx.visible = false;
|
|
395
|
+
if (node.computedStyle().pointer_events == 1) ctx.hittable = false;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
void buildGeometry(int id, Kind kind, const Matrix &m, Path &out)
|
|
399
|
+
{
|
|
400
|
+
PathBuilder b(out, m, 0.2f);
|
|
401
|
+
switch (kind) {
|
|
402
|
+
case Kind::Path:
|
|
403
|
+
if (const char *d = attr(id, "d")) parsePathData(d, b);
|
|
404
|
+
break;
|
|
405
|
+
case Kind::Polyline:
|
|
406
|
+
case Kind::Polygon:
|
|
407
|
+
if (const char *p = attr(id, "points")) addPoints(b, p, kind == Kind::Polygon);
|
|
408
|
+
break;
|
|
409
|
+
case Kind::Line:
|
|
410
|
+
b.moveTo({attrLength(id, "x1", 0), attrLength(id, "y1", 0)});
|
|
411
|
+
b.lineTo({attrLength(id, "x2", 0), attrLength(id, "y2", 0)});
|
|
412
|
+
b.finish();
|
|
413
|
+
break;
|
|
414
|
+
case Kind::Circle: {
|
|
415
|
+
const float r = attrLength(id, "r", 0);
|
|
416
|
+
addEllipse(b, attrLength(id, "cx", 0), attrLength(id, "cy", 0), r, r);
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
case Kind::Ellipse: {
|
|
420
|
+
float rx = attrLength(id, "rx", -1), ry = attrLength(id, "ry", -1);
|
|
421
|
+
if (rx < 0) rx = ry;
|
|
422
|
+
if (ry < 0) ry = rx;
|
|
423
|
+
addEllipse(b, attrLength(id, "cx", 0), attrLength(id, "cy", 0), rx, ry);
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
case Kind::Rect: {
|
|
427
|
+
float rx = attrLength(id, "rx", -1), ry = attrLength(id, "ry", -1);
|
|
428
|
+
if (rx < 0) rx = ry;
|
|
429
|
+
if (ry < 0) ry = rx;
|
|
430
|
+
addRect(b, attrLength(id, "x", 0), attrLength(id, "y", 0), attrLength(id, "width", 0),
|
|
431
|
+
attrLength(id, "height", 0), std::max(rx, 0.0f), std::max(ry, 0.0f));
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
default:
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
std::uint64_t mixKey(std::uint64_t h, std::uint64_t v)
|
|
440
|
+
{
|
|
441
|
+
h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
|
|
442
|
+
return h;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
std::uint64_t floatBits(float f)
|
|
446
|
+
{
|
|
447
|
+
std::uint32_t u;
|
|
448
|
+
std::memcpy(&u, &f, sizeof(u));
|
|
449
|
+
return u;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// The previous scene's items, consumed as the new scene reuses them.
|
|
453
|
+
bool samePaint(const ResolvedPaint &a, const ResolvedPaint &b)
|
|
454
|
+
{
|
|
455
|
+
if (a.on != b.on || a.gradient != b.gradient) return false;
|
|
456
|
+
if (!a.on) return true;
|
|
457
|
+
if (!a.gradient) return a.color == b.color && a.alpha == b.alpha;
|
|
458
|
+
const GradientPaint &g = a.grad, &h = b.grad;
|
|
459
|
+
if (g.bboxUnits != h.bboxUnits || g.x1 != h.x1 || g.y1 != h.y1 || g.x2 != h.x2 || g.y2 != h.y2 || g.stops != h.stops)
|
|
460
|
+
return false;
|
|
461
|
+
for (int i = 0; i < g.stops; ++i)
|
|
462
|
+
if (g.offset[i] != h.offset[i] || g.alpha[i] != h.alpha[i] || g.color[i].r != h.color[i].r ||
|
|
463
|
+
g.color[i].g != h.color[i].g || g.color[i].b != h.color[i].b)
|
|
464
|
+
return false;
|
|
465
|
+
return true;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// Everything an item can touch on screen, in svg-local pixels.
|
|
469
|
+
void addItemDamage(DamageSet &damage, const Item &item)
|
|
470
|
+
{
|
|
471
|
+
constexpr float kAntialias = 2.0f;
|
|
472
|
+
for (const Bounds *b : {&item.fillBounds, &item.strokeBounds}) {
|
|
473
|
+
if (b->empty()) continue;
|
|
474
|
+
Bounds g;
|
|
475
|
+
g.add(Point{b->x0 - kAntialias, b->y0 - kAntialias});
|
|
476
|
+
g.add(Point{b->x1 + kAntialias, b->y1 + kAntialias});
|
|
477
|
+
damage.add(g);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// How far, in half widths, a miter join of `path` reaches from its vertex:
|
|
482
|
+
// strokeContour puts a miter tip at hw / cos(turn / 2), and bevels instead
|
|
483
|
+
// when that ratio is over the limit; every other outline point stays within
|
|
484
|
+
// one half width of the centre line. A flattened curve turns a few degrees
|
|
485
|
+
// per vertex, so its joins reach barely past hw -- where the miter limit (4 by
|
|
486
|
+
// default, SVG's default join being miter) made a gauge's moving tip damage a
|
|
487
|
+
// box ~30 px wider on every side than the stroke can paint.
|
|
488
|
+
float miterReach(const Path &path, float miterLimit)
|
|
489
|
+
{
|
|
490
|
+
float worst = 1.0f;
|
|
491
|
+
for (std::size_t i = 0; i < path.contours.size(); ++i) {
|
|
492
|
+
const auto &c = path.contours[i];
|
|
493
|
+
const bool closed = path.closed[i] != 0;
|
|
494
|
+
Point first{}, firstDir{}, prev{}, prevDir{};
|
|
495
|
+
bool haveFirst = false, haveDir = false;
|
|
496
|
+
auto turnTo = [&](Point d) {
|
|
497
|
+
if (haveDir) {
|
|
498
|
+
const float cosTurn = std::clamp(prevDir.x * d.x + prevDir.y * d.y, -1.0f, 1.0f);
|
|
499
|
+
const float cosHalf = std::sqrt((1 + cosTurn) * 0.5f);
|
|
500
|
+
if (cosHalf > 1e-4f && 1.0f / cosHalf <= miterLimit) worst = std::max(worst, 1.0f / cosHalf);
|
|
501
|
+
} else {
|
|
502
|
+
firstDir = d;
|
|
503
|
+
}
|
|
504
|
+
prevDir = d;
|
|
505
|
+
haveDir = true;
|
|
506
|
+
};
|
|
507
|
+
auto segmentTo = [&](Point p) {
|
|
508
|
+
const float dx = p.x - prev.x, dy = p.y - prev.y;
|
|
509
|
+
const float len2 = dx * dx + dy * dy;
|
|
510
|
+
if (len2 <= 1e-8f) return false; // the stroker drops it too
|
|
511
|
+
const float inv = 1.0f / std::sqrt(len2);
|
|
512
|
+
turnTo(Point{dx * inv, dy * inv});
|
|
513
|
+
prev = p;
|
|
514
|
+
return true;
|
|
515
|
+
};
|
|
516
|
+
for (const Point &p : c) {
|
|
517
|
+
if (!haveFirst) {
|
|
518
|
+
first = prev = p;
|
|
519
|
+
haveFirst = true;
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
segmentTo(p);
|
|
523
|
+
}
|
|
524
|
+
if (closed && haveDir) {
|
|
525
|
+
segmentTo(first); // the closing segment, then the join back into the first
|
|
526
|
+
turnTo(firstDir);
|
|
527
|
+
}
|
|
528
|
+
if (worst >= miterLimit) return miterLimit;
|
|
529
|
+
}
|
|
530
|
+
return worst;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// Damage between two flattenings of one stroked open path. Arcs flatten onto
|
|
534
|
+
// a fixed angle grid, so the old and new points coincide over a shared run,
|
|
535
|
+
// possibly at an index offset when the start moved too. Each contour aligns
|
|
536
|
+
// on that run and damages the two ends outside it (a moved start, a moved
|
|
537
|
+
// end), each including the run's boundary point: a stroke piece spans two
|
|
538
|
+
// points and a join three, all within `reach` of the centre line. False when
|
|
539
|
+
// the contours do not line up (a different count, a closed contour that
|
|
540
|
+
// changed); the caller then damages both whole boxes.
|
|
541
|
+
bool strokeDiffDamage(const Path &a, const Path &b, float reach, DamageSet &damage)
|
|
542
|
+
{
|
|
543
|
+
if (a.contours.size() != b.contours.size()) return false;
|
|
544
|
+
constexpr float kSame = 1.0f / 256.0f;
|
|
545
|
+
auto near = [](Point p, Point q) { return std::fabs(p.x - q.x) <= kSame && std::fabs(p.y - q.y) <= kSame; };
|
|
546
|
+
for (std::size_t i = 0; i < a.contours.size(); ++i) {
|
|
547
|
+
const auto &ca = a.contours[i];
|
|
548
|
+
const auto &cb = b.contours[i];
|
|
549
|
+
const long na = static_cast<long>(ca.size()), nb = static_cast<long>(cb.size());
|
|
550
|
+
if (na == nb) {
|
|
551
|
+
long k = 0;
|
|
552
|
+
while (k < na && near(ca[k], cb[k])) ++k;
|
|
553
|
+
if (k == na) {
|
|
554
|
+
if (a.closed[i] != b.closed[i]) return false;
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
if (a.closed[i] || b.closed[i]) return false;
|
|
559
|
+
auto range = [&](Bounds &out, const auto &c, long from, long to) {
|
|
560
|
+
for (long k = std::max(0L, from); k <= to && k < static_cast<long>(c.size()); ++k) {
|
|
561
|
+
out.add(Point{c[k].x - reach, c[k].y - reach});
|
|
562
|
+
out.add(Point{c[k].x + reach, c[k].y + reach});
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
// Find the shared run: a point of `a` (tried at a few places) that
|
|
566
|
+
// `b` also has, and the offset between their indices.
|
|
567
|
+
long lo = -1, hi = -1, offset = 0;
|
|
568
|
+
for (long probe : {na / 2, na / 4, (3 * na) / 4, 1L, na - 2}) {
|
|
569
|
+
if (probe < 0 || probe >= na) continue;
|
|
570
|
+
for (long j = 0; j < nb; ++j) {
|
|
571
|
+
if (!near(ca[probe], cb[j])) continue;
|
|
572
|
+
offset = j - probe;
|
|
573
|
+
lo = hi = probe;
|
|
574
|
+
while (lo > 0 && lo - 1 + offset >= 0 && near(ca[lo - 1], cb[lo - 1 + offset])) --lo;
|
|
575
|
+
while (hi + 1 < na && hi + 1 + offset < nb && near(ca[hi + 1], cb[hi + 1 + offset])) ++hi;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
if (lo >= 0) break;
|
|
579
|
+
}
|
|
580
|
+
if (lo < 0) {
|
|
581
|
+
Bounds all;
|
|
582
|
+
range(all, ca, 0, na - 1);
|
|
583
|
+
range(all, cb, 0, nb - 1);
|
|
584
|
+
damage.add(all);
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
if (!(lo == 0 && offset == 0)) {
|
|
588
|
+
Bounds head;
|
|
589
|
+
range(head, ca, 0, lo);
|
|
590
|
+
range(head, cb, 0, lo + offset);
|
|
591
|
+
damage.add(head);
|
|
592
|
+
}
|
|
593
|
+
if (!(hi == na - 1 && hi + offset == nb - 1)) {
|
|
594
|
+
Bounds tail;
|
|
595
|
+
range(tail, ca, hi, na - 1);
|
|
596
|
+
range(tail, cb, hi + offset, nb - 1);
|
|
597
|
+
damage.add(tail);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return true;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// The previous scene's items, consumed as the new scene reuses them; also
|
|
604
|
+
// accumulates the rebuild's damage.
|
|
605
|
+
struct ItemCache {
|
|
606
|
+
std::vector<Item> *items = nullptr;
|
|
607
|
+
std::size_t hint = 0;
|
|
608
|
+
long lastTaken = -1;
|
|
609
|
+
bool full = false;
|
|
610
|
+
DamageSet damage;
|
|
611
|
+
Item *take(int node, std::uint64_t key)
|
|
612
|
+
{
|
|
613
|
+
if (!items) return nullptr;
|
|
614
|
+
const std::size_t n = items->size();
|
|
615
|
+
for (std::size_t k = 0; k < n; ++k) {
|
|
616
|
+
const std::size_t index = (hint + k) % n;
|
|
617
|
+
Item &it = (*items)[index];
|
|
618
|
+
if (it.node == node) {
|
|
619
|
+
hint = (index + 1) % n;
|
|
620
|
+
if (it.geometryKey != key) return nullptr;
|
|
621
|
+
// A reused item that moved below one drawn after it changes the
|
|
622
|
+
// stacking of unchanged pixels: repaint everything.
|
|
623
|
+
if (static_cast<long>(index) < lastTaken) full = true;
|
|
624
|
+
lastTaken = static_cast<long>(index);
|
|
625
|
+
return ⁢
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return nullptr;
|
|
629
|
+
}
|
|
630
|
+
// The previous item of `node` whose geometry changed: its edge buffers are
|
|
631
|
+
// handed to the rebuilt item, so a shape that changes every frame reuses
|
|
632
|
+
// its allocations instead of regrowing them. Its bounds stay for finish().
|
|
633
|
+
Item *stale(int node)
|
|
634
|
+
{
|
|
635
|
+
if (!items) return nullptr;
|
|
636
|
+
for (Item &it : *items)
|
|
637
|
+
if (it.node == node) return ⁢
|
|
638
|
+
return nullptr;
|
|
639
|
+
}
|
|
640
|
+
// Old items nobody reused (removed, hidden or rebuilt) leave damage behind.
|
|
641
|
+
void finish()
|
|
642
|
+
{
|
|
643
|
+
if (!items) return;
|
|
644
|
+
for (const Item &it : *items)
|
|
645
|
+
if (it.node >= 0) addItemDamage(damage, it);
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
// fill/stroke="url(#id)": the svg's <linearGradient id> with its <stop>s.
|
|
650
|
+
// Unknown or stopless references paint nothing, as in a browser.
|
|
651
|
+
ResolvedPaint resolveGradient(const Paint &paint, int id, const Matrix &matrix, float opacity)
|
|
652
|
+
{
|
|
653
|
+
ResolvedPaint out;
|
|
654
|
+
const auto &state = treeState();
|
|
655
|
+
const int root = rootOf(id);
|
|
656
|
+
const std::int16_t stopTag = tagIds().stop;
|
|
657
|
+
int grad = -1;
|
|
658
|
+
for (int n = 0; n < state.nodeCount && grad < 0; ++n) {
|
|
659
|
+
if (!state.nodeActive[n] || !tagIds().isLinearGradient(state.nodes[n].tag_id)) continue;
|
|
660
|
+
const char *gid = attr(n, "id");
|
|
661
|
+
if (gid && !std::strcmp(gid, paint.ref) && rootOf(n) == root) grad = n;
|
|
662
|
+
}
|
|
663
|
+
if (grad < 0) return out;
|
|
664
|
+
GradientPaint &g = out.grad;
|
|
665
|
+
const char *units = attr(grad, "gradientUnits");
|
|
666
|
+
g.bboxUnits = !(units && !std::strcmp(units, "userSpaceOnUse"));
|
|
667
|
+
g.x1 = attrNumber(grad, "x1", 0);
|
|
668
|
+
g.y1 = attrNumber(grad, "y1", 0);
|
|
669
|
+
g.x2 = attrNumber(grad, "x2", 1);
|
|
670
|
+
g.y2 = attrNumber(grad, "y2", 0);
|
|
671
|
+
if (!g.bboxUnits) {
|
|
672
|
+
const Point a = matrix.apply(Point{g.x1, g.y1}), b = matrix.apply(Point{g.x2, g.y2});
|
|
673
|
+
g.x1 = a.x; g.y1 = a.y; g.x2 = b.x; g.y2 = b.y;
|
|
674
|
+
}
|
|
675
|
+
float last = 0;
|
|
676
|
+
bool visible = false;
|
|
677
|
+
for (int s = state.nodes[grad].first_child; s >= 0 && s < state.nodeCount && g.stops < GradientPaint::kMaxStops;
|
|
678
|
+
s = state.nodes[s].next_sibling) {
|
|
679
|
+
if (!state.nodeActive[s] || state.nodes[s].tag_id != stopTag) continue;
|
|
680
|
+
Color c{0, 0, 0, 255};
|
|
681
|
+
if (const char *sc = attr(s, "stop-color")) parseColor(sc, c);
|
|
682
|
+
const float a = opacity * std::clamp(attrNumber(s, "stop-opacity", 1), 0.0f, 1.0f) * c.a / 255.0f;
|
|
683
|
+
last = std::max(last, std::clamp(attrNumber(s, "offset", 0), 0.0f, 1.0f));
|
|
684
|
+
g.offset[g.stops] = last;
|
|
685
|
+
g.color[g.stops] = c;
|
|
686
|
+
g.alpha[g.stops] = a;
|
|
687
|
+
visible = visible || a > 0.002f;
|
|
688
|
+
++g.stops;
|
|
689
|
+
}
|
|
690
|
+
if (g.stops == 0 || !visible) return out;
|
|
691
|
+
out.on = true;
|
|
692
|
+
out.gradient = true;
|
|
693
|
+
out.alpha = 1.0f;
|
|
694
|
+
out.color = gea::framework::graphics::pixel::nativeColor(g.color[0].r, g.color[0].g, g.color[0].b);
|
|
695
|
+
return out;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &cache)
|
|
699
|
+
{
|
|
700
|
+
const auto &state = treeState();
|
|
701
|
+
const Node &node = state.nodes[id];
|
|
702
|
+
const Kind kind = kindOf(node.tag_id);
|
|
703
|
+
if (kind == Kind::None || kind == Kind::Skip) return;
|
|
704
|
+
if (node.computedStyle().display == kDisplayNone) return;
|
|
705
|
+
if (const char *d = attr(id, "display"); d && !std::strcmp(d, "none")) return;
|
|
706
|
+
Context ctx = parent;
|
|
707
|
+
applyNodePaint(id, node, ctx);
|
|
708
|
+
if (kind != Kind::Svg) {
|
|
709
|
+
Matrix local;
|
|
710
|
+
if (const char *t = attr(id, "transform")) parseTransform(t, local);
|
|
711
|
+
ctx.matrix = ctx.matrix * local;
|
|
712
|
+
float opacity = std::clamp(attrNumber(id, "opacity", 1), 0.0f, 1.0f);
|
|
713
|
+
opacity *= node.computedStyle().opacity / 255.0f;
|
|
714
|
+
ctx.opacity *= opacity;
|
|
715
|
+
}
|
|
716
|
+
if (kind != Kind::Svg && kind != Kind::Group) {
|
|
717
|
+
if (!ctx.visible || ctx.opacity <= 0) return;
|
|
718
|
+
const ResolvedPaint fill =
|
|
719
|
+
kind == Kind::Line ? ResolvedPaint{}
|
|
720
|
+
: ctx.fill.kind == PaintKind::Url ? resolveGradient(ctx.fill, id, ctx.matrix, ctx.opacity * ctx.fillOpacity)
|
|
721
|
+
: resolvePaint(ctx.fill, node, ctx.opacity * ctx.fillOpacity);
|
|
722
|
+
const ResolvedPaint stroke = ctx.stroke.kind == PaintKind::Url
|
|
723
|
+
? resolveGradient(ctx.stroke, id, ctx.matrix, ctx.opacity * ctx.strokeOpacity)
|
|
724
|
+
: resolvePaint(ctx.stroke, node, ctx.opacity * ctx.strokeOpacity);
|
|
725
|
+
const float width = ctx.strokeWidth * ctx.matrix.scale();
|
|
726
|
+
const bool stroked = stroke.on && width > 0;
|
|
727
|
+
auto it = g_attrs.find(id);
|
|
728
|
+
std::uint64_t key = mixKey(0x51ed27ull, static_cast<std::uint16_t>(node.tag_id));
|
|
729
|
+
for (float f : {ctx.matrix.a, ctx.matrix.b, ctx.matrix.c, ctx.matrix.d})
|
|
730
|
+
key = mixKey(key, floatBits(f));
|
|
731
|
+
key = mixKey(key, static_cast<std::uint64_t>(fill.on) | static_cast<std::uint64_t>(stroked) << 1 |
|
|
732
|
+
static_cast<std::uint64_t>(ctx.evenOdd) << 2);
|
|
733
|
+
if (stroked)
|
|
734
|
+
key = mixKey(key, floatBits(width) | static_cast<std::uint64_t>(ctx.cap) << 32 |
|
|
735
|
+
static_cast<std::uint64_t>(ctx.join) << 40);
|
|
736
|
+
if (stroked && ctx.join == LineJoin::Miter) key = mixKey(key, floatBits(ctx.miterLimit));
|
|
737
|
+
const std::uint64_t styleKey = key;
|
|
738
|
+
key = mixKey(key, (static_cast<std::uint64_t>(id) << 32) | (it == g_attrs.end() ? 0u : it->second.generation));
|
|
739
|
+
if (Item *cached = cache.take(id, key)) {
|
|
740
|
+
Item item = std::move(*cached);
|
|
741
|
+
cached->node = -1;
|
|
742
|
+
const float dx = ctx.matrix.e - item.tx, dy = ctx.matrix.f - item.ty;
|
|
743
|
+
if (dx != 0.0f || dy != 0.0f) {
|
|
744
|
+
addItemDamage(cache.damage, item); // where it was
|
|
745
|
+
translateItem(item, dx, dy);
|
|
746
|
+
addItemDamage(cache.damage, item); // where it is
|
|
747
|
+
} else if (!samePaint(item.fill, fill) || !samePaint(item.stroke, stroke)) {
|
|
748
|
+
addItemDamage(cache.damage, item);
|
|
749
|
+
}
|
|
750
|
+
item.fill = fill;
|
|
751
|
+
item.stroke = stroke;
|
|
752
|
+
item.hittable = ctx.hittable;
|
|
753
|
+
if (item.strokeEdges.empty() && !item.fill.on && !item.hittable) return;
|
|
754
|
+
items.push_back(std::move(item));
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
Item item;
|
|
758
|
+
Item *old = cache.stale(id);
|
|
759
|
+
if (old) {
|
|
760
|
+
item.fillEdges.swap(old->fillEdges);
|
|
761
|
+
item.strokeEdges.swap(old->strokeEdges);
|
|
762
|
+
item.fillEdges.clear();
|
|
763
|
+
item.strokeEdges.clear();
|
|
764
|
+
}
|
|
765
|
+
item.node = id;
|
|
766
|
+
item.geometryKey = key;
|
|
767
|
+
item.styleKey = styleKey;
|
|
768
|
+
item.tx = ctx.matrix.e;
|
|
769
|
+
item.ty = ctx.matrix.f;
|
|
770
|
+
buildGeometry(id, kind, ctx.matrix, item.geometry);
|
|
771
|
+
if (item.geometry.empty()) return;
|
|
772
|
+
item.fill = fill;
|
|
773
|
+
item.stroke = stroke;
|
|
774
|
+
item.evenOdd = ctx.evenOdd;
|
|
775
|
+
item.hittable = ctx.hittable;
|
|
776
|
+
if (stroked) {
|
|
777
|
+
StrokeStyle style;
|
|
778
|
+
style.width = width;
|
|
779
|
+
style.cap = ctx.cap;
|
|
780
|
+
style.join = ctx.join;
|
|
781
|
+
style.miterLimit = ctx.miterLimit;
|
|
782
|
+
// The stroke outline only feeds the rasterizer (hit testing measures
|
|
783
|
+
// distance to the path), so it goes straight to edges.
|
|
784
|
+
strokePathEdges(item.geometry, style, item.strokeEdges, item.strokeBounds);
|
|
785
|
+
item.halfStroke = width / 2;
|
|
786
|
+
}
|
|
787
|
+
if (item.fill.on) {
|
|
788
|
+
item.fillBounds.add(item.geometry.contours);
|
|
789
|
+
buildEdges(item.geometry.contours, item.fillEdges);
|
|
790
|
+
}
|
|
791
|
+
if (item.strokeEdges.empty() && !item.fill.on && !item.hittable) return;
|
|
792
|
+
// A stroke-only shape rebuilt with the same style and translation
|
|
793
|
+
// changes only around the points its path changed: damage that, and
|
|
794
|
+
// retire the old item so finish() does not add its whole box.
|
|
795
|
+
if (old && stroked && !item.fill.on && !old->fill.on && old->styleKey == styleKey && old->tx == item.tx &&
|
|
796
|
+
old->ty == item.ty && samePaint(old->stroke, item.stroke)) {
|
|
797
|
+
float reach = item.halfStroke;
|
|
798
|
+
if (ctx.join == LineJoin::Miter)
|
|
799
|
+
reach *= std::max(miterReach(old->geometry, ctx.miterLimit), miterReach(item.geometry, ctx.miterLimit));
|
|
800
|
+
if (ctx.cap == LineCap::Square) reach = std::max(reach, item.halfStroke * 1.4143f);
|
|
801
|
+
const bool diffOk = strokeDiffDamage(old->geometry, item.geometry, reach + 2.0f, cache.damage);
|
|
802
|
+
if (diffOk) {
|
|
803
|
+
old->node = -1;
|
|
804
|
+
items.push_back(std::move(item));
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
addItemDamage(cache.damage, item);
|
|
809
|
+
items.push_back(std::move(item));
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
for (int child = node.first_child; child >= 0; child = state.nodes[child].next_sibling) walk(child, ctx, items, cache);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
void buildScene(int root, Scene &scene, int width, int height)
|
|
816
|
+
{
|
|
817
|
+
std::vector<Item> previous = std::move(scene.items);
|
|
818
|
+
scene.items.clear();
|
|
819
|
+
ItemCache cache;
|
|
820
|
+
// A resized viewport changes every matrix, so nothing would match anyway.
|
|
821
|
+
if (scene.width == width && scene.height == height) cache.items = &previous;
|
|
822
|
+
scene.width = width;
|
|
823
|
+
scene.height = height;
|
|
824
|
+
Context ctx;
|
|
825
|
+
float vb[4];
|
|
826
|
+
if (const char *v = attr(root, "viewBox"); v && parseNumberList(v, vb, 4))
|
|
827
|
+
ctx.matrix = viewBoxTransform(vb, static_cast<float>(width), static_cast<float>(height), attr(root, "preserveAspectRatio"));
|
|
828
|
+
walk(root, ctx, scene.items, cache);
|
|
829
|
+
cache.finish();
|
|
830
|
+
scene.dirty = false;
|
|
831
|
+
scene.built = true;
|
|
832
|
+
scene.damageValid = true;
|
|
833
|
+
scene.damageFull = !cache.items || cache.full;
|
|
834
|
+
scene.damage = cache.damage;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
std::uint64_t mix(std::uint64_t h, std::uint64_t v)
|
|
838
|
+
{
|
|
839
|
+
h ^= v + 0x9e3779b97f4a7c15ull + (h << 6) + (h >> 2);
|
|
840
|
+
return h;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
std::uint64_t subtreeSignature(int root)
|
|
845
|
+
{
|
|
846
|
+
const auto &state = treeState();
|
|
847
|
+
std::uint64_t h = 0xcbf29ce484222325ull;
|
|
848
|
+
auto visit = [&](auto &&self, int id) -> void {
|
|
849
|
+
const Node &n = state.nodes[id];
|
|
850
|
+
const auto &s = n.computedStyle();
|
|
851
|
+
const RareStyle &r = rstyle(s);
|
|
852
|
+
h = mix(h, static_cast<std::uint64_t>(id) << 16 | static_cast<std::uint16_t>(n.tag_id));
|
|
853
|
+
auto it = g_attrs.find(id);
|
|
854
|
+
h = mix(h, it == g_attrs.end() ? 0 : it->second.generation);
|
|
855
|
+
h = mix(h, static_cast<std::uint32_t>(r.svg_fill));
|
|
856
|
+
h = mix(h, static_cast<std::uint32_t>(r.svg_stroke));
|
|
857
|
+
h = mix(h, static_cast<std::uint32_t>(r.svg_stroke_width));
|
|
858
|
+
h = mix(h, static_cast<std::uint64_t>(static_cast<std::uint16_t>(r.svg_fill_opacity)) << 16 |
|
|
859
|
+
static_cast<std::uint16_t>(r.svg_stroke_opacity));
|
|
860
|
+
h = mix(h, static_cast<std::uint64_t>(static_cast<std::uint8_t>(r.svg_stroke_linecap)) << 16 |
|
|
861
|
+
static_cast<std::uint64_t>(static_cast<std::uint8_t>(r.svg_stroke_linejoin)) << 8 |
|
|
862
|
+
static_cast<std::uint8_t>(r.svg_fill_rule));
|
|
863
|
+
h = mix(h, static_cast<std::uint64_t>(s.text_color));
|
|
864
|
+
h = mix(h, static_cast<std::uint64_t>(s.text_alpha) << 24 | static_cast<std::uint64_t>(s.opacity) << 16 |
|
|
865
|
+
static_cast<std::uint64_t>(static_cast<std::uint8_t>(s.display)) << 8 |
|
|
866
|
+
static_cast<std::uint64_t>(static_cast<std::uint8_t>(s.visibility)) << 4 |
|
|
867
|
+
static_cast<std::uint8_t>(s.pointer_events));
|
|
868
|
+
if (id != root && kindOf(n.tag_id) == Kind::Svg) return; // nested svg: its own scene
|
|
869
|
+
for (int c = n.first_child; c >= 0; c = state.nodes[c].next_sibling) self(self, c);
|
|
870
|
+
};
|
|
871
|
+
visit(visit, root);
|
|
872
|
+
return h;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
inline int scratchBank()
|
|
876
|
+
{
|
|
877
|
+
#if GEA_SVG_HAS_FREERTOS
|
|
878
|
+
return xPortGetCoreID() & 1;
|
|
879
|
+
#elif GEA_SVG_HAS_PICO
|
|
880
|
+
return get_core_num() & 1;
|
|
881
|
+
#else
|
|
882
|
+
return 0;
|
|
883
|
+
#endif
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// Scratch bank of the svg paint worker (banks 0/1 belong to the two cores'
|
|
887
|
+
// inline paints, which may run at once when the renderer splits a region).
|
|
888
|
+
constexpr int kWorkerBank = 2;
|
|
889
|
+
|
|
890
|
+
#if GEA_SVG_HAS_FREERTOS && !CONFIG_FREERTOS_UNICORE
|
|
891
|
+
#define GEA_SVG_PARALLEL_PAINT 1
|
|
892
|
+
#if __has_include("esp_cpu.h")
|
|
893
|
+
#include "esp_cpu.h"
|
|
894
|
+
inline unsigned svgPaintCycles() { return esp_cpu_get_cycle_count(); }
|
|
895
|
+
#else
|
|
896
|
+
// No cycle counter: the split stays at the middle.
|
|
897
|
+
inline unsigned svgPaintCycles() { return 0; }
|
|
898
|
+
#endif
|
|
899
|
+
// One svg's rows split across both cores: the frame core paints the top half
|
|
900
|
+
// while this worker, pinned to the other core, paints the bottom half. It owns
|
|
901
|
+
// its own task (never the display's row worker, whose single job slot may be
|
|
902
|
+
// in flight around us) and its own scratch bank; only the core that created
|
|
903
|
+
// it submits, and only when idle, so a paint that is itself running in a
|
|
904
|
+
// band on the second core simply paints inline.
|
|
905
|
+
// Whether the display driver is running work of its own on the second core
|
|
906
|
+
// right now (see paint); drivers without such work keep this default.
|
|
907
|
+
extern "C" __attribute__((weak)) bool gea_display_second_core_busy() { return false; }
|
|
908
|
+
|
|
909
|
+
struct PaintWorker {
|
|
910
|
+
TaskHandle_t task = nullptr;
|
|
911
|
+
int ownerCore = -1;
|
|
912
|
+
bool attempted = false;
|
|
913
|
+
std::atomic<bool> busy{false};
|
|
914
|
+
std::atomic<bool> done{true};
|
|
915
|
+
void (*fn)(void *, int, int) = nullptr;
|
|
916
|
+
void *ctx = nullptr;
|
|
917
|
+
int y0 = 0, y1 = -1;
|
|
918
|
+
};
|
|
919
|
+
PaintWorker g_paintWorker;
|
|
920
|
+
|
|
921
|
+
void paintWorkerTask(void *)
|
|
922
|
+
{
|
|
923
|
+
for (;;) {
|
|
924
|
+
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
925
|
+
g_paintWorker.fn(g_paintWorker.ctx, g_paintWorker.y0, g_paintWorker.y1);
|
|
926
|
+
g_paintWorker.done.store(true, std::memory_order_release);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
bool paintWorkerSubmit(void (*fn)(void *, int, int), void *ctx, int y0, int y1)
|
|
931
|
+
{
|
|
932
|
+
const int core = static_cast<int>(xPortGetCoreID());
|
|
933
|
+
if (!g_paintWorker.attempted) {
|
|
934
|
+
g_paintWorker.attempted = true;
|
|
935
|
+
g_paintWorker.ownerCore = core;
|
|
936
|
+
// The stack holds only locals (scratch rows live on the heap), so it
|
|
937
|
+
// may sit in PSRAM: internal RAM is the scarce pool on these parts.
|
|
938
|
+
if (xTaskCreatePinnedToCoreWithCaps(paintWorkerTask, "gea_svg", 6144, nullptr, uxTaskPriorityGet(nullptr),
|
|
939
|
+
&g_paintWorker.task, core == 0 ? 1 : 0,
|
|
940
|
+
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) != pdPASS &&
|
|
941
|
+
xTaskCreatePinnedToCore(paintWorkerTask, "gea_svg", 6144, nullptr, uxTaskPriorityGet(nullptr),
|
|
942
|
+
&g_paintWorker.task, core == 0 ? 1 : 0) != pdPASS)
|
|
943
|
+
g_paintWorker.task = nullptr;
|
|
944
|
+
}
|
|
945
|
+
if (!g_paintWorker.task || core != g_paintWorker.ownerCore) return false;
|
|
946
|
+
if (g_paintWorker.busy.exchange(true, std::memory_order_acq_rel)) return false;
|
|
947
|
+
g_paintWorker.fn = fn;
|
|
948
|
+
g_paintWorker.ctx = ctx;
|
|
949
|
+
g_paintWorker.y0 = y0;
|
|
950
|
+
g_paintWorker.y1 = y1;
|
|
951
|
+
g_paintWorker.done.store(false, std::memory_order_release);
|
|
952
|
+
xTaskNotifyGive(g_paintWorker.task);
|
|
953
|
+
return true;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
void paintWorkerWait()
|
|
957
|
+
{
|
|
958
|
+
while (!g_paintWorker.done.load(std::memory_order_acquire)) {
|
|
959
|
+
}
|
|
960
|
+
g_paintWorker.busy.store(false, std::memory_order_release);
|
|
961
|
+
}
|
|
962
|
+
#else
|
|
963
|
+
#define GEA_SVG_PARALLEL_PAINT 0
|
|
964
|
+
#endif
|
|
965
|
+
|
|
966
|
+
// Blends a shape's coverage straight into the canvas rows. The rasterizer
|
|
967
|
+
// hands over many short runs per row (every anti-aliased edge is one), so each
|
|
968
|
+
// run is blended here, inline, into a row pointer fetched once per row: on an
|
|
969
|
+
// ESP32-S3 one canvas call costs ~250 cycles before it touches a pixel, more
|
|
970
|
+
// than a typical edge run. Rows the canvas cannot expose directly (rotated,
|
|
971
|
+
// packed, global alpha) go through its per-run entry points instead. Dirty
|
|
972
|
+
// marking is one box for the whole shape.
|
|
973
|
+
struct RowSink {
|
|
974
|
+
using Pixel = gea::framework::graphics::pixel::native_t;
|
|
975
|
+
gea::framework::graphics::Canvas *canvas;
|
|
976
|
+
Pixel color;
|
|
977
|
+
int ox, oy;
|
|
978
|
+
int rowY = INT_MIN;
|
|
979
|
+
Pixel *row = nullptr;
|
|
980
|
+
int cx0 = 0, cx1 = -1;
|
|
981
|
+
int dx0 = INT_MAX, dy0 = INT_MAX, dx1 = INT_MIN, dy1 = INT_MIN;
|
|
982
|
+
Pixel lastBg = 0, lastOut = 0;
|
|
983
|
+
int lastA = -1;
|
|
984
|
+
|
|
985
|
+
bool select(int y)
|
|
986
|
+
{
|
|
987
|
+
if (y != rowY) {
|
|
988
|
+
rowY = y;
|
|
989
|
+
row = canvas->directRow(y, &cx0, &cx1);
|
|
990
|
+
}
|
|
991
|
+
return row != nullptr;
|
|
992
|
+
}
|
|
993
|
+
void touched(int y, int a, int b)
|
|
994
|
+
{
|
|
995
|
+
if (a < dx0) dx0 = a;
|
|
996
|
+
if (b > dx1) dx1 = b;
|
|
997
|
+
if (y < dy0) dy0 = y;
|
|
998
|
+
if (y > dy1) dy1 = y;
|
|
999
|
+
}
|
|
1000
|
+
Pixel blendOne(Pixel bg, int a)
|
|
1001
|
+
{
|
|
1002
|
+
if (bg != lastBg || a != lastA) {
|
|
1003
|
+
lastBg = bg;
|
|
1004
|
+
lastA = a;
|
|
1005
|
+
lastOut = gea::framework::graphics::pixel::blendNative(color, bg, a);
|
|
1006
|
+
}
|
|
1007
|
+
return lastOut;
|
|
1008
|
+
}
|
|
1009
|
+
void span(int y, int x, int count, std::uint8_t cov)
|
|
1010
|
+
{
|
|
1011
|
+
y += oy;
|
|
1012
|
+
x += ox;
|
|
1013
|
+
if (!select(y)) {
|
|
1014
|
+
canvas->blendCoverageSpan(y, x, count, cov, color, false);
|
|
1015
|
+
touched(y, x, x + count - 1);
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
const int a = std::max(x, cx0), b = std::min(x + count - 1, cx1);
|
|
1019
|
+
if (a > b) return;
|
|
1020
|
+
Pixel *p = row + a;
|
|
1021
|
+
const int n = b - a + 1;
|
|
1022
|
+
if (cov == 255) {
|
|
1023
|
+
std::fill_n(p, n, color);
|
|
1024
|
+
} else {
|
|
1025
|
+
for (int i = 0; i < n; ++i) p[i] = blendOne(p[i], cov);
|
|
1026
|
+
}
|
|
1027
|
+
touched(y, a, b);
|
|
1028
|
+
}
|
|
1029
|
+
void coverage(int y, int x, const std::uint8_t *cov, int count)
|
|
1030
|
+
{
|
|
1031
|
+
y += oy;
|
|
1032
|
+
x += ox;
|
|
1033
|
+
if (!select(y)) {
|
|
1034
|
+
canvas->blendCoverageRow(y, x, count, cov, color, false);
|
|
1035
|
+
touched(y, x, x + count - 1);
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
int a = x, b = x + count - 1;
|
|
1039
|
+
if (a < cx0) a = cx0;
|
|
1040
|
+
if (b > cx1) b = cx1;
|
|
1041
|
+
if (a > b) return;
|
|
1042
|
+
for (int px = a; px <= b; ++px) {
|
|
1043
|
+
const int c = cov[px - x];
|
|
1044
|
+
if (c == 0) continue;
|
|
1045
|
+
row[px] = c == 255 ? color : blendOne(row[px], c);
|
|
1046
|
+
}
|
|
1047
|
+
touched(y, a, b);
|
|
1048
|
+
}
|
|
1049
|
+
void markDirty()
|
|
1050
|
+
{
|
|
1051
|
+
if (dx0 <= dx1 && dy0 <= dy1) canvas->markDirty(dx0, dy0, dx1, dy1);
|
|
1052
|
+
}
|
|
1053
|
+
};
|
|
1054
|
+
|
|
1055
|
+
GEA_SVG_HOT void paintCoverage(gea::framework::graphics::Canvas *canvas, int bank, bool markDirty, const EdgeList &edges,
|
|
1056
|
+
const Bounds &bounds, bool evenOdd, const ResolvedPaint &paint, int ox, int oy, int cx0, int cy0,
|
|
1057
|
+
int cx1, int cy1)
|
|
1058
|
+
{
|
|
1059
|
+
if (!canvas || bounds.empty()) return;
|
|
1060
|
+
// Pixel rectangle in svg-local coordinates, clipped to the canvas clip.
|
|
1061
|
+
const int x0 = std::max(static_cast<int>(std::floor(bounds.x0)) - 1, cx0 - ox);
|
|
1062
|
+
const int y0 = std::max(static_cast<int>(std::floor(bounds.y0)) - 1, cy0 - oy);
|
|
1063
|
+
const int x1 = std::min(static_cast<int>(std::ceil(bounds.x1)) + 1, cx1 - ox);
|
|
1064
|
+
const int y1 = std::min(static_cast<int>(std::ceil(bounds.y1)) + 1, cy1 - oy);
|
|
1065
|
+
// One scratch bank per core: a large dirty region replays in two bands on
|
|
1066
|
+
// both cores at once (canvas.cpp's occlusion banks follow the same rule).
|
|
1067
|
+
static Rasterizer rasters[3];
|
|
1068
|
+
static ScratchVector<std::uint8_t> rows[3];
|
|
1069
|
+
Rasterizer &raster = rasters[bank];
|
|
1070
|
+
ScratchVector<std::uint8_t> &row = rows[bank];
|
|
1071
|
+
RowSink sink{canvas, paint.color, ox, oy};
|
|
1072
|
+
if (paint.gradient) {
|
|
1073
|
+
// A 256-step table along the gradient vector: colour and alpha per step.
|
|
1074
|
+
const GradientPaint &g = paint.grad;
|
|
1075
|
+
float gx1 = g.x1, gy1 = g.y1, gx2 = g.x2, gy2 = g.y2;
|
|
1076
|
+
if (g.bboxUnits) {
|
|
1077
|
+
const float bw = bounds.x1 - bounds.x0, bh = bounds.y1 - bounds.y0;
|
|
1078
|
+
gx1 = bounds.x0 + g.x1 * bw; gx2 = bounds.x0 + g.x2 * bw;
|
|
1079
|
+
gy1 = bounds.y0 + g.y1 * bh; gy2 = bounds.y0 + g.y2 * bh;
|
|
1080
|
+
}
|
|
1081
|
+
const float vx = gx2 - gx1, vy = gy2 - gy1;
|
|
1082
|
+
const float len2 = vx * vx + vy * vy;
|
|
1083
|
+
RowSink::Pixel lutColor[256];
|
|
1084
|
+
std::uint8_t lutAlpha[256];
|
|
1085
|
+
for (int i = 0; i < 256; ++i) {
|
|
1086
|
+
const float t = i / 255.0f;
|
|
1087
|
+
int k = 0;
|
|
1088
|
+
while (k + 1 < g.stops && g.offset[k + 1] <= t) ++k;
|
|
1089
|
+
float r, gg, b, a;
|
|
1090
|
+
if (k + 1 >= g.stops || t <= g.offset[0]) {
|
|
1091
|
+
const int s = t <= g.offset[0] ? 0 : k;
|
|
1092
|
+
r = g.color[s].r; gg = g.color[s].g; b = g.color[s].b; a = g.alpha[s];
|
|
1093
|
+
} else {
|
|
1094
|
+
const float span = g.offset[k + 1] - g.offset[k];
|
|
1095
|
+
const float f = span > 0 ? (t - g.offset[k]) / span : 1.0f;
|
|
1096
|
+
r = g.color[k].r + (g.color[k + 1].r - g.color[k].r) * f;
|
|
1097
|
+
gg = g.color[k].g + (g.color[k + 1].g - g.color[k].g) * f;
|
|
1098
|
+
b = g.color[k].b + (g.color[k + 1].b - g.color[k].b) * f;
|
|
1099
|
+
a = g.alpha[k] + (g.alpha[k + 1] - g.alpha[k]) * f;
|
|
1100
|
+
}
|
|
1101
|
+
lutColor[i] = gea::framework::graphics::pixel::nativeColor(static_cast<std::uint8_t>(r + 0.5f), static_cast<std::uint8_t>(gg + 0.5f),
|
|
1102
|
+
static_cast<std::uint8_t>(b + 0.5f));
|
|
1103
|
+
lutAlpha[i] = static_cast<std::uint8_t>(std::clamp(a, 0.0f, 1.0f) * 255.0f + 0.5f);
|
|
1104
|
+
}
|
|
1105
|
+
auto stepAt = [&](float px, float py) {
|
|
1106
|
+
if (len2 <= 1e-6f) return 255;
|
|
1107
|
+
const float t = std::clamp(((px - gx1) * vx + (py - gy1) * vy) / len2, 0.0f, 1.0f);
|
|
1108
|
+
return static_cast<int>(t * 255.0f + 0.5f);
|
|
1109
|
+
};
|
|
1110
|
+
// A vertical vector (LVGL's chart fade) is one step per row; any other
|
|
1111
|
+
// direction is evaluated per pixel.
|
|
1112
|
+
const bool perRow = std::fabs(vx) * 64.0f <= std::fabs(vy);
|
|
1113
|
+
auto use = [&](int step) {
|
|
1114
|
+
if (sink.color != lutColor[step]) { sink.color = lutColor[step]; sink.lastA = -1; }
|
|
1115
|
+
return lutAlpha[step];
|
|
1116
|
+
};
|
|
1117
|
+
static ScratchVector<std::uint8_t> scaled[3];
|
|
1118
|
+
ScratchVector<std::uint8_t> &tmp = scaled[bank];
|
|
1119
|
+
fillCoverage(
|
|
1120
|
+
raster, row, edges, x0, y0, x1, y1, evenOdd, 1.0f,
|
|
1121
|
+
[&](int y, int x, const std::uint8_t *cov, int count) {
|
|
1122
|
+
if (perRow) {
|
|
1123
|
+
const int a = use(stepAt(gx1, y + 0.5f));
|
|
1124
|
+
if (a == 0) return;
|
|
1125
|
+
if (tmp.size() < static_cast<std::size_t>(count)) tmp.resize(count);
|
|
1126
|
+
for (int i = 0; i < count; ++i) tmp[i] = static_cast<std::uint8_t>((cov[i] * a + 127) / 255);
|
|
1127
|
+
sink.coverage(y, x, tmp.data(), count);
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
for (int i = 0; i < count; ++i) {
|
|
1131
|
+
if (!cov[i]) continue;
|
|
1132
|
+
const int a = use(stepAt(x + i + 0.5f, y + 0.5f));
|
|
1133
|
+
const int c = (cov[i] * a + 127) / 255;
|
|
1134
|
+
if (c) sink.span(y, x + i, 1, static_cast<std::uint8_t>(c));
|
|
1135
|
+
}
|
|
1136
|
+
},
|
|
1137
|
+
[&](int y, int x, int count, std::uint8_t cov) {
|
|
1138
|
+
if (perRow) {
|
|
1139
|
+
const int a = use(stepAt(gx1, y + 0.5f));
|
|
1140
|
+
const int c = (cov * a + 127) / 255;
|
|
1141
|
+
if (c) sink.span(y, x, count, static_cast<std::uint8_t>(c));
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1144
|
+
for (int i = 0; i < count; ++i) {
|
|
1145
|
+
const int a = use(stepAt(x + i + 0.5f, y + 0.5f));
|
|
1146
|
+
const int c = (cov * a + 127) / 255;
|
|
1147
|
+
if (c) sink.span(y, x + i, 1, static_cast<std::uint8_t>(c));
|
|
1148
|
+
}
|
|
1149
|
+
});
|
|
1150
|
+
if (markDirty) sink.markDirty();
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
fillCoverage(
|
|
1154
|
+
raster, row, edges, x0, y0, x1, y1, evenOdd, std::min(1.0f, paint.alpha),
|
|
1155
|
+
[&](int y, int x, const std::uint8_t *cov, int count) { sink.coverage(y, x, cov, count); },
|
|
1156
|
+
[&](int y, int x, int count, std::uint8_t cov) { sink.span(y, x, count, cov); });
|
|
1157
|
+
if (markDirty) sink.markDirty();
|
|
1158
|
+
}
|
|
1159
|
+
|
|
1160
|
+
} // namespace
|
|
1161
|
+
|
|
1162
|
+
bool cssDeclarationValue(StyleDeclaration declaration, const char *value, Property &property, int &encoded)
|
|
1163
|
+
{
|
|
1164
|
+
if (!value) return false;
|
|
1165
|
+
while (*value == ' ' || *value == '\t') ++value;
|
|
1166
|
+
std::string v(value);
|
|
1167
|
+
if (const auto bang = v.find("!important"); bang != std::string::npos) v.erase(bang);
|
|
1168
|
+
while (!v.empty() && (v.back() == ' ' || v.back() == '\t')) v.pop_back();
|
|
1169
|
+
auto lower = v;
|
|
1170
|
+
for (char &c : lower) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
1171
|
+
// CSS-wide keywords reset to "unset" (inherit through the svg tree).
|
|
1172
|
+
const bool wide = lower == "inherit" || lower == "unset" || lower == "initial" || lower == "revert";
|
|
1173
|
+
switch (declaration) {
|
|
1174
|
+
case StyleDeclaration::SvgFill:
|
|
1175
|
+
case StyleDeclaration::SvgStroke: {
|
|
1176
|
+
property = declaration == StyleDeclaration::SvgFill ? Property::SvgFill : Property::SvgStroke;
|
|
1177
|
+
if (wide) { encoded = 0; return true; }
|
|
1178
|
+
const Paint paint = parsePaint(v.c_str());
|
|
1179
|
+
if (paint.kind == PaintKind::Unset || paint.kind == PaintKind::Url) return false;
|
|
1180
|
+
encoded = encodePaint(paint);
|
|
1181
|
+
return true;
|
|
1182
|
+
}
|
|
1183
|
+
case StyleDeclaration::SvgStrokeWidth: {
|
|
1184
|
+
property = Property::SvgStrokeWidth;
|
|
1185
|
+
if (wide) { encoded = -1; return true; }
|
|
1186
|
+
float w = 0;
|
|
1187
|
+
if (!parseLength(v.c_str(), w) || w < 0) return false;
|
|
1188
|
+
encoded = static_cast<int>(std::lround(w * 100));
|
|
1189
|
+
return true;
|
|
1190
|
+
}
|
|
1191
|
+
case StyleDeclaration::SvgFillOpacity:
|
|
1192
|
+
case StyleDeclaration::SvgStrokeOpacity: {
|
|
1193
|
+
property = declaration == StyleDeclaration::SvgFillOpacity ? Property::SvgFillOpacity : Property::SvgStrokeOpacity;
|
|
1194
|
+
if (wide) { encoded = -1; return true; }
|
|
1195
|
+
Scanner sc(v.c_str());
|
|
1196
|
+
float o = 1;
|
|
1197
|
+
if (!sc.number(o)) return false;
|
|
1198
|
+
if (*sc.rest() == '%') o /= 100.0f;
|
|
1199
|
+
encoded = static_cast<int>(std::lround(std::clamp(o, 0.0f, 1.0f) * 1000));
|
|
1200
|
+
return true;
|
|
1201
|
+
}
|
|
1202
|
+
case StyleDeclaration::SvgStrokeLinecap:
|
|
1203
|
+
property = Property::SvgStrokeLinecap;
|
|
1204
|
+
if (wide) encoded = -1;
|
|
1205
|
+
else if (lower == "butt") encoded = static_cast<int>(LineCap::Butt);
|
|
1206
|
+
else if (lower == "round") encoded = static_cast<int>(LineCap::Round);
|
|
1207
|
+
else if (lower == "square") encoded = static_cast<int>(LineCap::Square);
|
|
1208
|
+
else return false;
|
|
1209
|
+
return true;
|
|
1210
|
+
case StyleDeclaration::SvgStrokeLinejoin:
|
|
1211
|
+
property = Property::SvgStrokeLinejoin;
|
|
1212
|
+
if (wide) encoded = -1;
|
|
1213
|
+
else if (lower == "miter" || lower == "miter-clip" || lower == "arcs") encoded = static_cast<int>(LineJoin::Miter);
|
|
1214
|
+
else if (lower == "round") encoded = static_cast<int>(LineJoin::Round);
|
|
1215
|
+
else if (lower == "bevel") encoded = static_cast<int>(LineJoin::Bevel);
|
|
1216
|
+
else return false;
|
|
1217
|
+
return true;
|
|
1218
|
+
case StyleDeclaration::SvgFillRule:
|
|
1219
|
+
property = Property::SvgFillRule;
|
|
1220
|
+
if (wide) encoded = -1;
|
|
1221
|
+
else if (lower == "nonzero") encoded = 0;
|
|
1222
|
+
else if (lower == "evenodd") encoded = 1;
|
|
1223
|
+
else return false;
|
|
1224
|
+
return true;
|
|
1225
|
+
default:
|
|
1226
|
+
return false;
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
bool isRoot(const Node &node) { return node.tag_id == g_svgTag; }
|
|
1231
|
+
|
|
1232
|
+
bool damage(int root, int &x0, int &y0, int &x1, int &y1, int (*extra)[4], int *extraCount)
|
|
1233
|
+
{
|
|
1234
|
+
if (extraCount) *extraCount = 0;
|
|
1235
|
+
auto it = g_scenes.find(root);
|
|
1236
|
+
if (it == g_scenes.end()) return false;
|
|
1237
|
+
Scene &scene = it->second;
|
|
1238
|
+
const bool valid = scene.damageValid && !scene.damageFull && !scene.boxChanged;
|
|
1239
|
+
scene.damageValid = false;
|
|
1240
|
+
if (!valid) return false;
|
|
1241
|
+
auto toPixels = [&](const Bounds &b, int &bx0, int &by0, int &bx1, int &by1) {
|
|
1242
|
+
bx0 = std::max(0, static_cast<int>(std::floor(b.x0)));
|
|
1243
|
+
by0 = std::max(0, static_cast<int>(std::floor(b.y0)));
|
|
1244
|
+
bx1 = std::min(scene.width - 1, static_cast<int>(std::ceil(b.x1)));
|
|
1245
|
+
by1 = std::min(scene.height - 1, static_cast<int>(std::ceil(b.y1)));
|
|
1246
|
+
return bx0 <= bx1 && by0 <= by1;
|
|
1247
|
+
};
|
|
1248
|
+
x0 = y0 = 0;
|
|
1249
|
+
x1 = y1 = -1;
|
|
1250
|
+
const DamageSet &d = scene.damage;
|
|
1251
|
+
// Without room for the extra boxes, report their union.
|
|
1252
|
+
Bounds all;
|
|
1253
|
+
if (!extra || !extraCount)
|
|
1254
|
+
for (int k = 0; k < d.count; ++k) all = DamageSet::unite(all.empty() ? d.box[k] : all, d.box[k]);
|
|
1255
|
+
if (!extra || !extraCount) {
|
|
1256
|
+
if (!all.empty() && !toPixels(all, x0, y0, x1, y1)) x0 = y0 = 0, x1 = y1 = -1;
|
|
1257
|
+
return true;
|
|
1258
|
+
}
|
|
1259
|
+
bool first = true;
|
|
1260
|
+
for (int k = 0; k < d.count; ++k) {
|
|
1261
|
+
int bx0, by0, bx1, by1;
|
|
1262
|
+
if (!toPixels(d.box[k], bx0, by0, bx1, by1)) continue;
|
|
1263
|
+
if (first) {
|
|
1264
|
+
x0 = bx0, y0 = by0, x1 = bx1, y1 = by1;
|
|
1265
|
+
first = false;
|
|
1266
|
+
} else if (*extraCount < kExtraDamage) {
|
|
1267
|
+
int *r = extra[(*extraCount)++];
|
|
1268
|
+
r[0] = bx0, r[1] = by0, r[2] = bx1, r[3] = by1;
|
|
1269
|
+
} else {
|
|
1270
|
+
x0 = std::min(x0, bx0), y0 = std::min(y0, by0), x1 = std::max(x1, bx1), y1 = std::max(y1, by1);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
return true;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
bool isShape(int node)
|
|
1277
|
+
{
|
|
1278
|
+
if (g_svgTag == -2) return false;
|
|
1279
|
+
const Kind kind = kindOfNode(node);
|
|
1280
|
+
return kind != Kind::None && kind != Kind::Svg;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
void noteTagChanged(int node)
|
|
1284
|
+
{
|
|
1285
|
+
auto &state = treeState();
|
|
1286
|
+
if (node < 0 || node >= state.nodeCount) return;
|
|
1287
|
+
if (g_svgTag == -2) {
|
|
1288
|
+
// Intern the SVG tag set only once an SVG tag actually appears.
|
|
1289
|
+
if (!isSvgTagName(tagFromId(state.nodes[node].tag_id))) return;
|
|
1290
|
+
g_svgTag = tagIds().svg;
|
|
1291
|
+
}
|
|
1292
|
+
const Kind kind = kindOf(state.nodes[node].tag_id);
|
|
1293
|
+
if (kind == Kind::None) {
|
|
1294
|
+
g_scenes.erase(node);
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
if (kind == Kind::Svg) {
|
|
1298
|
+
g_scenes[node];
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
// Shapes, groups and metadata take no part in CSS layout: out of flow, no
|
|
1302
|
+
// box. Inline (not default) styles, so this holds in every build.
|
|
1303
|
+
tree().setStyle(node, Property::Position, 1);
|
|
1304
|
+
tree().setStyle(node, Property::Width, 0);
|
|
1305
|
+
tree().setStyle(node, Property::Height, 0);
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
bool noteAttribute(int node, const char *name, const char *value)
|
|
1309
|
+
{
|
|
1310
|
+
if (!name || kindOfNode(node) == Kind::None) return false;
|
|
1311
|
+
Attrs &attrs = g_attrs[node];
|
|
1312
|
+
auto it = std::find_if(attrs.values.begin(), attrs.values.end(), [&](const auto &kv) { return kv.first == name; });
|
|
1313
|
+
if (value) {
|
|
1314
|
+
if (it != attrs.values.end()) {
|
|
1315
|
+
if (it->second == value) return true;
|
|
1316
|
+
it->second = value;
|
|
1317
|
+
} else {
|
|
1318
|
+
attrs.values.emplace_back(name, value);
|
|
1319
|
+
}
|
|
1320
|
+
} else if (it != attrs.values.end()) {
|
|
1321
|
+
attrs.values.erase(it);
|
|
1322
|
+
} else {
|
|
1323
|
+
return true;
|
|
1324
|
+
}
|
|
1325
|
+
// Globally unique, so a reused node id never matches a stale cached item.
|
|
1326
|
+
static std::uint32_t serial = 0;
|
|
1327
|
+
attrs.generation = ++serial;
|
|
1328
|
+
if (kindOfNode(node) == Kind::Svg && (!std::strcmp(name, "width") || !std::strcmp(name, "height"))) {
|
|
1329
|
+
// The width/height attributes size the svg box at presentational
|
|
1330
|
+
// (lowest) priority: any authored CSS width/height wins.
|
|
1331
|
+
float px = 0;
|
|
1332
|
+
const bool w = name[0] == 'w';
|
|
1333
|
+
const Property p = w ? Property::Width : Property::Height;
|
|
1334
|
+
if (value && parseLength(value, px)) {
|
|
1335
|
+
// CSS px, like any authored width/height: scaled to device pixels.
|
|
1336
|
+
const int device = static_cast<int>(std::lround(px * devicePixelRatio()));
|
|
1337
|
+
#if GEA_UI_DEFAULT_STYLES
|
|
1338
|
+
tree().setDefaultStyle(node, p, device);
|
|
1339
|
+
#else
|
|
1340
|
+
tree().setStyle(node, p, device);
|
|
1341
|
+
#endif
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
noteNodeChanged(node);
|
|
1345
|
+
return true;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
const char *attribute(int node, const char *name) { return name ? attr(node, name) : nullptr; }
|
|
1349
|
+
|
|
1350
|
+
void noteNodeChanged(int node)
|
|
1351
|
+
{
|
|
1352
|
+
const int root = rootOf(node);
|
|
1353
|
+
if (root >= 0) invalidateRoot(root);
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
void releaseNode(int node)
|
|
1357
|
+
{
|
|
1358
|
+
if (g_svgTag == -2) return;
|
|
1359
|
+
g_attrs.erase(node);
|
|
1360
|
+
g_scenes.erase(node);
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
void clearAll()
|
|
1364
|
+
{
|
|
1365
|
+
g_attrs.clear();
|
|
1366
|
+
g_scenes.clear();
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
void validateScenes()
|
|
1370
|
+
{
|
|
1371
|
+
auto &state = treeState();
|
|
1372
|
+
#if GEA_SVG_PARALLEL_PAINT
|
|
1373
|
+
// A scene is otherwise first built when its svg is first recorded, i.e. when
|
|
1374
|
+
// it scrolls into view: on the CrowPanel's Analytics tab the first flick that
|
|
1375
|
+
// revealed the meter row built a dozen gauge scenes inside one frame (147 and
|
|
1376
|
+
// 226 ms frames), so the page jumped 150-250 px at once mid-scroll. Laid-out
|
|
1377
|
+
// svgs off screen get their scene here instead, built below with the rest
|
|
1378
|
+
// (on both cores) in the frame that mounted them.
|
|
1379
|
+
if (g_svgTag >= 0) {
|
|
1380
|
+
for (int id = 0; id < state.nodeCount; ++id) {
|
|
1381
|
+
const Node &n = state.nodes[id];
|
|
1382
|
+
if (n.tag_id != g_svgTag || !state.nodeActive[id] || n.layout.width <= 0 || n.layout.height <= 0) continue;
|
|
1383
|
+
if (g_scenes.find(id) != g_scenes.end()) continue;
|
|
1384
|
+
Scene &scene = g_scenes.emplace(id, Scene{}).first->second;
|
|
1385
|
+
scene.width = n.layout.width;
|
|
1386
|
+
scene.height = n.layout.height;
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
#endif
|
|
1390
|
+
if (g_scenes.empty()) return;
|
|
1391
|
+
for (auto it = g_scenes.begin(); it != g_scenes.end();) {
|
|
1392
|
+
const int root = it->first;
|
|
1393
|
+
if (root >= state.nodeCount || !state.nodeActive[root] || state.nodes[root].tag_id != g_svgTag) {
|
|
1394
|
+
it = g_scenes.erase(it);
|
|
1395
|
+
continue;
|
|
1396
|
+
}
|
|
1397
|
+
const std::uint64_t sig = subtreeSignature(root);
|
|
1398
|
+
if (sig != it->second.signature) {
|
|
1399
|
+
it->second.signature = sig;
|
|
1400
|
+
if (it->second.recorded) invalidateRoot(root);
|
|
1401
|
+
else it->second.dirty = true;
|
|
1402
|
+
}
|
|
1403
|
+
++it;
|
|
1404
|
+
}
|
|
1405
|
+
#if GEA_SVG_PARALLEL_PAINT
|
|
1406
|
+
// Rebuild the changed scenes across both cores now, before the display
|
|
1407
|
+
// list records them (record() then finds them built). Building reads the
|
|
1408
|
+
// tree, attributes and styles, which nothing mutates during a refresh, and
|
|
1409
|
+
// writes only its own scene. Each scene goes to the less loaded core,
|
|
1410
|
+
// weighed by its previous edge count; the heaviest go first.
|
|
1411
|
+
struct Job {
|
|
1412
|
+
int root;
|
|
1413
|
+
Scene *scene;
|
|
1414
|
+
std::size_t weight;
|
|
1415
|
+
};
|
|
1416
|
+
Job jobs[16];
|
|
1417
|
+
int count = 0;
|
|
1418
|
+
for (auto &[root, scene] : g_scenes) {
|
|
1419
|
+
if (!scene.dirty || scene.width <= 0 || count == 16) continue;
|
|
1420
|
+
// Off screen and built once already (an animated needle that has not
|
|
1421
|
+
// been shown yet): its first record rebuilds it, so don't every frame.
|
|
1422
|
+
if (!scene.recorded && scene.built) continue;
|
|
1423
|
+
const Node &n = state.nodes[root];
|
|
1424
|
+
if (n.layout.width != scene.width || n.layout.height != scene.height) continue;
|
|
1425
|
+
std::size_t weight = 1;
|
|
1426
|
+
for (const Item &item : scene.items) weight += item.fillEdges.size() + item.strokeEdges.size();
|
|
1427
|
+
jobs[count++] = {root, &scene, weight};
|
|
1428
|
+
}
|
|
1429
|
+
if (count < 2) return;
|
|
1430
|
+
std::sort(jobs, jobs + count, [](const Job &a, const Job &b) { return a.weight > b.weight; });
|
|
1431
|
+
Job mine[16], theirs[16];
|
|
1432
|
+
int nMine = 0, nTheirs = 0;
|
|
1433
|
+
std::size_t loadMine = 0, loadTheirs = 0;
|
|
1434
|
+
for (int i = 0; i < count; ++i) {
|
|
1435
|
+
if (loadMine <= loadTheirs) mine[nMine++] = jobs[i], loadMine += jobs[i].weight;
|
|
1436
|
+
else theirs[nTheirs++] = jobs[i], loadTheirs += jobs[i].weight;
|
|
1437
|
+
}
|
|
1438
|
+
auto build = [](void *p, int, int count) {
|
|
1439
|
+
const Job *list = static_cast<const Job *>(p);
|
|
1440
|
+
for (int i = 0; i < count; ++i) buildScene(list[i].root, *list[i].scene, list[i].scene->width, list[i].scene->height);
|
|
1441
|
+
};
|
|
1442
|
+
const bool split = nTheirs > 0 && paintWorkerSubmit(build, theirs, 0, nTheirs);
|
|
1443
|
+
build(mine, 0, nMine);
|
|
1444
|
+
if (split) paintWorkerWait();
|
|
1445
|
+
else build(theirs, 0, nTheirs);
|
|
1446
|
+
#endif
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
void record(const Node &node)
|
|
1450
|
+
{
|
|
1451
|
+
const int id = static_cast<int>(&node - tree().nodes());
|
|
1452
|
+
auto it = g_scenes.find(id);
|
|
1453
|
+
if (it == g_scenes.end()) it = g_scenes.emplace(id, Scene{}).first;
|
|
1454
|
+
Scene &scene = it->second;
|
|
1455
|
+
if (node.layout.width <= 0 || node.layout.height <= 0) return;
|
|
1456
|
+
// Padding bytes can only make this report a change that did not happen,
|
|
1457
|
+
// which costs a full-box repaint, never a missed one.
|
|
1458
|
+
scene.boxChanged = !scene.haveBox || scene.boxX != node.layout.x || scene.boxY != node.layout.y ||
|
|
1459
|
+
std::memcmp(scene.boxStyle, &node.computedStyle(), sizeof(ComputedStyle)) != 0;
|
|
1460
|
+
scene.haveBox = true;
|
|
1461
|
+
scene.boxX = node.layout.x;
|
|
1462
|
+
scene.boxY = node.layout.y;
|
|
1463
|
+
std::memcpy(scene.boxStyle, &node.computedStyle(), sizeof(ComputedStyle));
|
|
1464
|
+
if (scene.dirty || scene.width != node.layout.width || scene.height != node.layout.height) {
|
|
1465
|
+
buildScene(id, scene, node.layout.width, node.layout.height);
|
|
1466
|
+
scene.signature = subtreeSignature(id);
|
|
1467
|
+
}
|
|
1468
|
+
if (scene.items.empty()) return;
|
|
1469
|
+
DisplayCommand *cmd = DisplayList::instance().append();
|
|
1470
|
+
if (!cmd) return;
|
|
1471
|
+
cmd->type = DisplayCommandType::DrawSvg;
|
|
1472
|
+
cmd->bx = node.layout.x;
|
|
1473
|
+
cmd->by = node.layout.y;
|
|
1474
|
+
cmd->bw = node.layout.width;
|
|
1475
|
+
cmd->bh = node.layout.height;
|
|
1476
|
+
cmd->svg = {static_cast<std::int16_t>(id), node.layout.x, node.layout.y, node.layout.width, node.layout.height};
|
|
1477
|
+
scene.recorded = true;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
|
|
1481
|
+
void paint(const DisplayCommand &c)
|
|
1482
|
+
{
|
|
1483
|
+
auto it = g_scenes.find(c.svg.nodeId);
|
|
1484
|
+
if (it == g_scenes.end()) return;
|
|
1485
|
+
auto *canvas = gea::platform::display::Display::canvas();
|
|
1486
|
+
if (!canvas) return;
|
|
1487
|
+
int cx0, cy0, cx1, cy1;
|
|
1488
|
+
canvas->currentClip(&cx0, &cy0, &cx1, &cy1);
|
|
1489
|
+
// The svg viewport clips its content (overflow: hidden is SVG's default).
|
|
1490
|
+
cx0 = std::max(cx0, static_cast<int>(c.svg.x));
|
|
1491
|
+
cy0 = std::max(cy0, static_cast<int>(c.svg.y));
|
|
1492
|
+
cx1 = std::min(cx1 + 1, c.svg.x + c.svg.w);
|
|
1493
|
+
cy1 = std::min(cy1 + 1, c.svg.y + c.svg.h);
|
|
1494
|
+
if (cx0 >= cx1 || cy0 >= cy1) return;
|
|
1495
|
+
struct Band {
|
|
1496
|
+
const std::vector<Item> *items;
|
|
1497
|
+
gea::framework::graphics::Canvas *canvas;
|
|
1498
|
+
int ox, oy, cx0, cx1;
|
|
1499
|
+
int bank;
|
|
1500
|
+
bool markDirty;
|
|
1501
|
+
unsigned cycles;
|
|
1502
|
+
};
|
|
1503
|
+
auto paintRows = [](void *p, int y0, int y1) {
|
|
1504
|
+
Band &b = *static_cast<Band *>(p);
|
|
1505
|
+
#if GEA_SVG_PARALLEL_PAINT
|
|
1506
|
+
const unsigned c0 = svgPaintCycles();
|
|
1507
|
+
struct Time { Band &b; unsigned c0; ~Time() { b.cycles = svgPaintCycles() - c0; } } time{b, c0};
|
|
1508
|
+
#endif
|
|
1509
|
+
// In row bands, every item per band (so stacking holds): the band's
|
|
1510
|
+
// pixels stay hot in the cache while each item blends into them, where
|
|
1511
|
+
// item-by-item over the whole box re-reads every row once per item. On
|
|
1512
|
+
// an RGB panel scanning out of PSRAM that traffic and the dirty lines
|
|
1513
|
+
// it evicts landed inside bounce refills: banding took the Analytics
|
|
1514
|
+
// meters from 5-9 refill overruns (tears) per 9k refills to about none.
|
|
1515
|
+
constexpr int kBandRows = 24;
|
|
1516
|
+
for (int by0 = y0; by0 < y1; by0 += kBandRows) {
|
|
1517
|
+
const int by1 = std::min(y1, by0 + kBandRows);
|
|
1518
|
+
for (const Item &item : *b.items) {
|
|
1519
|
+
if (item.fill.on)
|
|
1520
|
+
paintCoverage(b.canvas, b.bank, b.markDirty, item.fillEdges, item.fillBounds, item.evenOdd, item.fill, b.ox,
|
|
1521
|
+
b.oy, b.cx0, by0, b.cx1, by1);
|
|
1522
|
+
if (!item.strokeEdges.empty())
|
|
1523
|
+
paintCoverage(b.canvas, b.bank, b.markDirty, item.strokeEdges, item.strokeBounds, false, item.stroke, b.ox,
|
|
1524
|
+
b.oy, b.cx0, by0, b.cx1, by1);
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
};
|
|
1528
|
+
Band band{&it->second.items, canvas, c.svg.x, c.svg.y, cx0, cx1, scratchBank(), true, 0};
|
|
1529
|
+
#if GEA_SVG_PARALLEL_PAINT
|
|
1530
|
+
// Split tall paints across both cores; rows are disjoint, so the two
|
|
1531
|
+
// halves never touch the same pixels. Neither marks rows dirty (the dirty
|
|
1532
|
+
// rect list is not concurrent); the clipped box is marked once below.
|
|
1533
|
+
// Ink is rarely spread evenly over the rows (a chart's filled area sits
|
|
1534
|
+
// low, a gauge's arc high), so a fixed half split leaves one core waiting
|
|
1535
|
+
// on the other; each paint moves the split row to where the last paint's
|
|
1536
|
+
// measured time would have divided equally.
|
|
1537
|
+
// Not while the display's own work holds the second core (a sideways
|
|
1538
|
+
// scroll's shifted copy): the helper would only time-slice with it, and
|
|
1539
|
+
// this core spins in paintWorkerWait meanwhile. On the CrowPanel 7.0 that
|
|
1540
|
+
// made the Analytics charts cost 13-30 ms of every swipe frame's strip.
|
|
1541
|
+
constexpr int kMinSplitRows = 48;
|
|
1542
|
+
if (cy1 - cy0 >= kMinSplitRows && (cx1 - cx0) * (cy1 - cy0) >= 8000 && !gea_display_second_core_busy()) {
|
|
1543
|
+
Scene &scene = it->second;
|
|
1544
|
+
const int rows = cy1 - cy0;
|
|
1545
|
+
const int mid = cy0 + std::clamp(static_cast<int>(rows * scene.split + 0.5f), 1, rows - 1);
|
|
1546
|
+
Band low = band;
|
|
1547
|
+
low.bank = kWorkerBank;
|
|
1548
|
+
low.markDirty = false;
|
|
1549
|
+
band.markDirty = false;
|
|
1550
|
+
if (paintWorkerSubmit(paintRows, &low, mid, cy1)) {
|
|
1551
|
+
paintRows(&band, cy0, mid);
|
|
1552
|
+
paintWorkerWait();
|
|
1553
|
+
canvas->markDirty(cx0, cy0, cx1 - 1, cy1 - 1);
|
|
1554
|
+
const float top = static_cast<float>(band.cycles), bottom = static_cast<float>(low.cycles);
|
|
1555
|
+
const float half = (top + bottom) * 0.5f;
|
|
1556
|
+
if (half > 0) {
|
|
1557
|
+
const int topRows = mid - cy0, bottomRows = cy1 - mid;
|
|
1558
|
+
// Rows each half's cost spreads over evenly, up to the time midpoint.
|
|
1559
|
+
const float at = top >= half ? topRows * (half / top) : topRows + bottomRows * ((half - top) / bottom);
|
|
1560
|
+
const float target = at / rows;
|
|
1561
|
+
scene.split = std::clamp(0.5f * scene.split + 0.5f * target, 0.1f, 0.9f);
|
|
1562
|
+
}
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
band.markDirty = true;
|
|
1566
|
+
}
|
|
1567
|
+
#endif
|
|
1568
|
+
paintRows(&band, cy0, cy1);
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
int hitTest(int rootId, int x, int y)
|
|
1572
|
+
{
|
|
1573
|
+
auto it = g_scenes.find(rootId);
|
|
1574
|
+
if (it == g_scenes.end()) return -1;
|
|
1575
|
+
const Node &root = tree().node(rootId);
|
|
1576
|
+
const Point p{static_cast<float>(x - root.layout.x), static_cast<float>(y - root.layout.y)};
|
|
1577
|
+
constexpr float kSlop = 3.0f; // fingers are wider than hairlines
|
|
1578
|
+
const auto &items = it->second.items;
|
|
1579
|
+
for (auto item = items.rbegin(); item != items.rend(); ++item) {
|
|
1580
|
+
if (!item->hittable) continue;
|
|
1581
|
+
if (item->fill.on && insideFill(item->geometry.contours, p, item->evenOdd)) return item->node;
|
|
1582
|
+
if (!item->strokeEdges.empty() && distanceToPath(item->geometry, p) <= item->halfStroke + kSlop) return item->node;
|
|
1583
|
+
}
|
|
1584
|
+
return -1;
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
} // namespace gea::embedded::ui::svg
|
|
1588
|
+
|
|
1589
|
+
#endif // GEA_UI_SVG
|