@geastack/engine 0.1.21 → 0.1.23

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.
@@ -45,7 +45,11 @@ namespace {
45
45
  enum class Kind : std::uint8_t { None, Svg, Group, Path, Polyline, Polygon, Line, Circle, Ellipse, Rect, Skip };
46
46
 
47
47
  struct TagIds {
48
- std::int16_t svg, g, path, polyline, polygon, line, circle, ellipse, rect, defs, title, desc, text;
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; }
49
53
  };
50
54
 
51
55
  const TagIds &tagIds()
@@ -53,7 +57,8 @@ const TagIds &tagIds()
53
57
  static const TagIds ids = {internTag("svg"), internTag("g"), internTag("path"), internTag("polyline"),
54
58
  internTag("polygon"), internTag("line"), internTag("circle"), internTag("ellipse"),
55
59
  internTag("rect"), internTag("defs"), internTag("title"), internTag("desc"),
56
- internTag("text")};
60
+ internTag("text"), internTag("linearGradient"), internTag("stop"),
61
+ internTag("lineargradient")};
57
62
  return ids;
58
63
  }
59
64
 
@@ -74,14 +79,14 @@ Kind kindOf(std::int16_t tag)
74
79
  if (tag == t.circle) return Kind::Circle;
75
80
  if (tag == t.ellipse) return Kind::Ellipse;
76
81
  if (tag == t.rect) return Kind::Rect;
77
- if (tag == t.defs || tag == t.title || tag == t.desc || tag == t.text) return Kind::Skip;
82
+ if (tag == t.defs || tag == t.title || tag == t.desc || tag == t.text || t.isLinearGradient(tag) || tag == t.stop) return Kind::Skip;
78
83
  return Kind::None;
79
84
  }
80
85
 
81
86
  bool isSvgTagName(const char *tag)
82
87
  {
83
88
  static const char *const names[] = {"svg", "g", "path", "polyline", "polygon", "line", "circle",
84
- "ellipse", "rect", "defs", "title", "desc", "text"};
89
+ "ellipse", "rect", "defs", "title", "desc", "text", "linearGradient", "lineargradient", "stop"};
85
90
  for (const char *n : names)
86
91
  if (std::strcmp(tag, n) == 0) return true;
87
92
  return false;
@@ -98,10 +103,78 @@ struct Attrs {
98
103
  }
99
104
  };
100
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
+
101
118
  struct ResolvedPaint {
102
119
  bool on = false;
103
120
  gea::framework::graphics::pixel::native_t color = 0;
104
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; }
105
178
  };
106
179
 
107
180
  struct Item {
@@ -117,6 +190,9 @@ struct Item {
117
190
  // stroke style): an unchanged item keeps them across scene rebuilds, so a
118
191
  // frame that moves one shape re-flattens only that shape.
119
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;
120
196
  // The transform's translation when the geometry was built. The key leaves
121
197
  // translation out, so a shape that only moved (a scrolling chart) shifts
122
198
  // its edges instead of re-flattening and re-stroking.
@@ -157,6 +233,11 @@ void translateItem(Item &item, float dx, float dy)
157
233
  struct Scene {
158
234
  bool dirty = true;
159
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
160
241
  int width = -1, height = -1;
161
242
  std::uint64_t signature = 0;
162
243
  std::vector<Item> items;
@@ -167,7 +248,7 @@ struct Scene {
167
248
  // refresh's dirty collection through damage().
168
249
  bool damageValid = false;
169
250
  bool damageFull = true;
170
- Bounds damage;
251
+ DamageSet damage;
171
252
  // The svg box as last recorded, to tell a scene-only change apart from a
172
253
  // change to the box's own paint (background, border, position).
173
254
  bool boxChanged = true;
@@ -208,7 +289,14 @@ void invalidateRoot(int root)
208
289
  Node &n = state.nodes[root];
209
290
  tree().markNodeDisplayCommandsDirty(root);
210
291
  // A first DrawSvg needs a fresh recording; later ones are replaced in place.
211
- if (!it->second.recorded) tree().markDisplayListContentDirty();
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();
212
300
  n.render.dirty = 1;
213
301
  #if GEA_CSS_SCROLLING
214
302
  n.render.non_scroll_dirty = 1;
@@ -364,18 +452,152 @@ std::uint64_t floatBits(float f)
364
452
  // The previous scene's items, consumed as the new scene reuses them.
365
453
  bool samePaint(const ResolvedPaint &a, const ResolvedPaint &b)
366
454
  {
367
- return a.on == b.on && (!a.on || (a.color == b.color && a.alpha == b.alpha));
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;
368
466
  }
369
467
 
370
468
  // Everything an item can touch on screen, in svg-local pixels.
371
- void addItemDamage(Bounds &damage, const Item &item)
469
+ void addItemDamage(DamageSet &damage, const Item &item)
372
470
  {
373
471
  constexpr float kAntialias = 2.0f;
374
472
  for (const Bounds *b : {&item.fillBounds, &item.strokeBounds}) {
375
473
  if (b->empty()) continue;
376
- damage.add(Point{b->x0 - kAntialias, b->y0 - kAntialias});
377
- damage.add(Point{b->x1 + kAntialias, b->y1 + kAntialias});
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
+ }
378
599
  }
600
+ return true;
379
601
  }
380
602
 
381
603
  // The previous scene's items, consumed as the new scene reuses them; also
@@ -385,7 +607,7 @@ struct ItemCache {
385
607
  std::size_t hint = 0;
386
608
  long lastTaken = -1;
387
609
  bool full = false;
388
- Bounds damage;
610
+ DamageSet damage;
389
611
  Item *take(int node, std::uint64_t key)
390
612
  {
391
613
  if (!items) return nullptr;
@@ -424,6 +646,55 @@ struct ItemCache {
424
646
  }
425
647
  };
426
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
+
427
698
  void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &cache)
428
699
  {
429
700
  const auto &state = treeState();
@@ -445,14 +716,16 @@ void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &ca
445
716
  if (kind != Kind::Svg && kind != Kind::Group) {
446
717
  if (!ctx.visible || ctx.opacity <= 0) return;
447
718
  const ResolvedPaint fill =
448
- kind == Kind::Line ? ResolvedPaint{} : resolvePaint(ctx.fill, node, ctx.opacity * ctx.fillOpacity);
449
- const ResolvedPaint stroke = resolvePaint(ctx.stroke, node, ctx.opacity * ctx.strokeOpacity);
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);
450
725
  const float width = ctx.strokeWidth * ctx.matrix.scale();
451
726
  const bool stroked = stroke.on && width > 0;
452
727
  auto it = g_attrs.find(id);
453
- std::uint64_t key = mixKey(0x51ed27ull, (static_cast<std::uint64_t>(id) << 32) |
454
- (it == g_attrs.end() ? 0u : it->second.generation));
455
- key = mixKey(key, static_cast<std::uint16_t>(node.tag_id));
728
+ std::uint64_t key = mixKey(0x51ed27ull, static_cast<std::uint16_t>(node.tag_id));
456
729
  for (float f : {ctx.matrix.a, ctx.matrix.b, ctx.matrix.c, ctx.matrix.d})
457
730
  key = mixKey(key, floatBits(f));
458
731
  key = mixKey(key, static_cast<std::uint64_t>(fill.on) | static_cast<std::uint64_t>(stroked) << 1 |
@@ -461,6 +734,8 @@ void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &ca
461
734
  key = mixKey(key, floatBits(width) | static_cast<std::uint64_t>(ctx.cap) << 32 |
462
735
  static_cast<std::uint64_t>(ctx.join) << 40);
463
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));
464
739
  if (Item *cached = cache.take(id, key)) {
465
740
  Item item = std::move(*cached);
466
741
  cached->node = -1;
@@ -480,7 +755,8 @@ void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &ca
480
755
  return;
481
756
  }
482
757
  Item item;
483
- if (Item *old = cache.stale(id)) {
758
+ Item *old = cache.stale(id);
759
+ if (old) {
484
760
  item.fillEdges.swap(old->fillEdges);
485
761
  item.strokeEdges.swap(old->strokeEdges);
486
762
  item.fillEdges.clear();
@@ -488,6 +764,7 @@ void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &ca
488
764
  }
489
765
  item.node = id;
490
766
  item.geometryKey = key;
767
+ item.styleKey = styleKey;
491
768
  item.tx = ctx.matrix.e;
492
769
  item.ty = ctx.matrix.f;
493
770
  buildGeometry(id, kind, ctx.matrix, item.geometry);
@@ -512,6 +789,22 @@ void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &ca
512
789
  buildEdges(item.geometry.contours, item.fillEdges);
513
790
  }
514
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
+ }
515
808
  addItemDamage(cache.damage, item);
516
809
  items.push_back(std::move(item));
517
810
  return;
@@ -535,6 +828,7 @@ void buildScene(int root, Scene &scene, int width, int height)
535
828
  walk(root, ctx, scene.items, cache);
536
829
  cache.finish();
537
830
  scene.dirty = false;
831
+ scene.built = true;
538
832
  scene.damageValid = true;
539
833
  scene.damageFull = !cache.items || cache.full;
540
834
  scene.damage = cache.damage;
@@ -546,6 +840,7 @@ std::uint64_t mix(std::uint64_t h, std::uint64_t v)
546
840
  return h;
547
841
  }
548
842
 
843
+
549
844
  std::uint64_t subtreeSignature(int root)
550
845
  {
551
846
  const auto &state = treeState();
@@ -607,6 +902,10 @@ inline unsigned svgPaintCycles() { return 0; }
607
902
  // in flight around us) and its own scratch bank; only the core that created
608
903
  // it submits, and only when idle, so a paint that is itself running in a
609
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
+
610
909
  struct PaintWorker {
611
910
  TaskHandle_t task = nullptr;
612
911
  int ownerCore = -1;
@@ -770,6 +1069,87 @@ GEA_SVG_HOT void paintCoverage(gea::framework::graphics::Canvas *canvas, int ban
770
1069
  Rasterizer &raster = rasters[bank];
771
1070
  ScratchVector<std::uint8_t> &row = rows[bank];
772
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
+ }
773
1153
  fillCoverage(
774
1154
  raster, row, edges, x0, y0, x1, y1, evenOdd, std::min(1.0f, paint.alpha),
775
1155
  [&](int y, int x, const std::uint8_t *cov, int count) { sink.coverage(y, x, cov, count); },
@@ -796,7 +1176,7 @@ bool cssDeclarationValue(StyleDeclaration declaration, const char *value, Proper
796
1176
  property = declaration == StyleDeclaration::SvgFill ? Property::SvgFill : Property::SvgStroke;
797
1177
  if (wide) { encoded = 0; return true; }
798
1178
  const Paint paint = parsePaint(v.c_str());
799
- if (paint.kind == PaintKind::Unset) return false;
1179
+ if (paint.kind == PaintKind::Unset || paint.kind == PaintKind::Url) return false;
800
1180
  encoded = encodePaint(paint);
801
1181
  return true;
802
1182
  }
@@ -849,24 +1229,47 @@ bool cssDeclarationValue(StyleDeclaration declaration, const char *value, Proper
849
1229
 
850
1230
  bool isRoot(const Node &node) { return node.tag_id == g_svgTag; }
851
1231
 
852
- bool damage(int root, int &x0, int &y0, int &x1, int &y1)
1232
+ bool damage(int root, int &x0, int &y0, int &x1, int &y1, int (*extra)[4], int *extraCount)
853
1233
  {
1234
+ if (extraCount) *extraCount = 0;
854
1235
  auto it = g_scenes.find(root);
855
1236
  if (it == g_scenes.end()) return false;
856
1237
  Scene &scene = it->second;
857
1238
  const bool valid = scene.damageValid && !scene.damageFull && !scene.boxChanged;
858
1239
  scene.damageValid = false;
859
1240
  if (!valid) return false;
860
- if (scene.damage.empty()) {
861
- x0 = y0 = 0;
862
- x1 = y1 = -1;
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;
863
1257
  return true;
864
1258
  }
865
- x0 = std::max(0, static_cast<int>(std::floor(scene.damage.x0)));
866
- y0 = std::max(0, static_cast<int>(std::floor(scene.damage.y0)));
867
- x1 = std::min(scene.width - 1, static_cast<int>(std::ceil(scene.damage.x1)));
868
- y1 = std::min(scene.height - 1, static_cast<int>(std::ceil(scene.damage.y1)));
869
- if (x0 > x1 || y0 > y1) x0 = y0 = 0, x1 = y1 = -1;
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
+ }
870
1273
  return true;
871
1274
  }
872
1275
 
@@ -965,8 +1368,26 @@ void clearAll()
965
1368
 
966
1369
  void validateScenes()
967
1370
  {
968
- if (g_scenes.empty()) return;
969
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;
970
1391
  for (auto it = g_scenes.begin(); it != g_scenes.end();) {
971
1392
  const int root = it->first;
972
1393
  if (root >= state.nodeCount || !state.nodeActive[root] || state.nodes[root].tag_id != g_svgTag) {
@@ -996,6 +1417,9 @@ void validateScenes()
996
1417
  int count = 0;
997
1418
  for (auto &[root, scene] : g_scenes) {
998
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;
999
1423
  const Node &n = state.nodes[root];
1000
1424
  if (n.layout.width != scene.width || n.layout.height != scene.height) continue;
1001
1425
  std::size_t weight = 1;
@@ -1053,6 +1477,7 @@ void record(const Node &node)
1053
1477
  scene.recorded = true;
1054
1478
  }
1055
1479
 
1480
+
1056
1481
  void paint(const DisplayCommand &c)
1057
1482
  {
1058
1483
  auto it = g_scenes.find(c.svg.nodeId);
@@ -1081,13 +1506,23 @@ void paint(const DisplayCommand &c)
1081
1506
  const unsigned c0 = svgPaintCycles();
1082
1507
  struct Time { Band &b; unsigned c0; ~Time() { b.cycles = svgPaintCycles() - c0; } } time{b, c0};
1083
1508
  #endif
1084
- for (const Item &item : *b.items) {
1085
- if (item.fill.on)
1086
- paintCoverage(b.canvas, b.bank, b.markDirty, item.fillEdges, item.fillBounds, item.evenOdd, item.fill, b.ox,
1087
- b.oy, b.cx0, y0, b.cx1, y1);
1088
- if (!item.strokeEdges.empty())
1089
- paintCoverage(b.canvas, b.bank, b.markDirty, item.strokeEdges, item.strokeBounds, false, item.stroke, b.ox,
1090
- b.oy, b.cx0, y0, b.cx1, y1);
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
+ }
1091
1526
  }
1092
1527
  };
1093
1528
  Band band{&it->second.items, canvas, c.svg.x, c.svg.y, cx0, cx1, scratchBank(), true, 0};
@@ -1099,8 +1534,12 @@ void paint(const DisplayCommand &c)
1099
1534
  // low, a gauge's arc high), so a fixed half split leaves one core waiting
1100
1535
  // on the other; each paint moves the split row to where the last paint's
1101
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.
1102
1541
  constexpr int kMinSplitRows = 48;
1103
- if (cy1 - cy0 >= kMinSplitRows && (cx1 - cx0) * (cy1 - cy0) >= 8000) {
1542
+ if (cy1 - cy0 >= kMinSplitRows && (cx1 - cx0) * (cy1 - cy0) >= 8000 && !gea_display_second_core_busy()) {
1104
1543
  Scene &scene = it->second;
1105
1544
  const int rows = cy1 - cy0;
1106
1545
  const int mid = cy0 + std::clamp(static_cast<int>(rows * scene.split + 0.5f), 1, rows - 1);