@geastack/engine 0.1.5 → 0.1.6

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/ui/render.cpp CHANGED
@@ -6,6 +6,7 @@
6
6
  #include "graphics/font.h"
7
7
  #include "memory.h"
8
8
  #include "tree_state.h"
9
+ #include "style_values.h"
9
10
  #include <pixel.h>
10
11
 
11
12
  #include <algorithm>
@@ -516,6 +517,7 @@ int gLastScrollUiFrame = -1000;
516
517
  int commandCapacity = 0;
517
518
  bool commandsExternal = false;
518
519
  int commandCount = 0;
520
+ bool textClippedBackgrounds = false;
519
521
  // Sticky: an append() was dropped because the command buffer is full.
520
522
  // Reset by clear(); lets a caller detect a truncated record.
521
523
  bool commandOverflow = false;
@@ -775,6 +777,7 @@ int gLastScrollUiFrame = -1000;
775
777
  commandCapacity = 0;
776
778
  commandsExternal = false;
777
779
  commandCount = 0;
780
+ textClippedBackgrounds = false;
778
781
  appendOverride = nullptr;
779
782
  appendOverrideCount = nullptr;
780
783
  appendOverrideCapacity = 0;
@@ -1344,7 +1347,7 @@ int gLastScrollUiFrame = -1000;
1344
1347
  return node.first_child < 0 &&
1345
1348
  node.type == NodeType::View &&
1346
1349
  node.style.has_bg &&
1347
- node.style.bg_fill == 0 &&
1350
+ !styleHasBackgroundImage(node.style) && rstyle(node.style).bg_clip == 0 &&
1348
1351
  !hasAnyBorder(node.style);
1349
1352
  }
1350
1353
 
@@ -1577,8 +1580,13 @@ int gLastScrollUiFrame = -1000;
1577
1580
  return true;
1578
1581
  }
1579
1582
 
1580
- bool roundedRectScreenSpanHasCircularRadii(const RoundedRectScreenSpan &r)
1583
+ bool roundedRectScreenSpanFitsCanvas(const RoundedRectScreenSpan &r)
1581
1584
  {
1585
+ // Both direct replay and batching call a Canvas primitive that clamps
1586
+ // radii to half the box. Larger CSS curves require the exact shader.
1587
+ const int maxRadius8 = std::min(r.w, r.h) * 4;
1588
+ if (r.tlRx8 > maxRadius8 || r.trRx8 > maxRadius8 ||
1589
+ r.brRx8 > maxRadius8 || r.blRx8 > maxRadius8) return false;
1582
1590
  return std::abs(r.tlRx8 - r.tlRy8) <= 1 &&
1583
1591
  std::abs(r.trRx8 - r.trRy8) <= 1 &&
1584
1592
  std::abs(r.brRx8 - r.brRy8) <= 1 &&
@@ -1643,7 +1651,7 @@ int gLastScrollUiFrame = -1000;
1643
1651
  RoundedRectScreenSpan span{};
1644
1652
  if (!transformedRoundedRectToScreenSpan(r, &span))
1645
1653
  return false;
1646
- if (!roundedRectScreenSpanHasCircularRadii(span))
1654
+ if (!roundedRectScreenSpanFitsCanvas(span))
1647
1655
  return false;
1648
1656
 
1649
1657
  if (drawCircleLikeScreenSpan(span, r.color))
@@ -2660,14 +2668,22 @@ int gLastScrollUiFrame = -1000;
2660
2668
  }
2661
2669
  }
2662
2670
 
2663
- GEA_RENDER_HOT_SRAM void drawProjectedText(const DisplayCommand &command)
2671
+ GEA_RENDER_HOT_SRAM void drawProjectedText(const DisplayCommand &authoredCommand,
2672
+ uint8_t *coverageRow = nullptr, int coverageX = 0)
2664
2673
  {
2674
+ // Keep deferred commands pointing to authored node storage. Normalize
2675
+ // locally on replay, using the same content as layout and cache hashing.
2676
+ std::string preparedText;
2677
+ DisplayCommand command = authoredCommand;
2678
+ command.projectedText.text = TextRenderer::prepareText(command.projectedText.text,
2679
+ command.projectedText.textTransform, command.projectedText.whiteSpace, preparedText);
2680
+ command.projectedText.textTransform = 0;
2665
2681
  auto *canvas = gea::platform::display::Display::canvas();
2666
2682
  if (!canvas || !canvas->pixels() || canvas->width() <= 0 || canvas->height() <= 0)
2667
2683
  return;
2668
2684
  if (!command.projectedText.text || !command.projectedText.text[0])
2669
2685
  return;
2670
- if (command.projectedText.alpha == 0 || command.projectedText.srcW <= 0 || command.projectedText.srcH <= 0)
2686
+ if ((!coverageRow && command.projectedText.alpha == 0) || command.projectedText.srcW <= 0 || command.projectedText.srcH <= 0)
2671
2687
  return;
2672
2688
 
2673
2689
  // Back-face cull (CSS backface-visibility:hidden) — same winding test as the gradient
@@ -2704,7 +2720,7 @@ int gLastScrollUiFrame = -1000;
2704
2720
  return;
2705
2721
 
2706
2722
  const int parentAlpha = gea::platform::display::Display::alpha();
2707
- if (parentAlpha == 0)
2723
+ if (!coverageRow && parentAlpha == 0)
2708
2724
  return;
2709
2725
 
2710
2726
  const int stride = canvas->strideBytes() / static_cast<int>(sizeof(gea::framework::graphics::pixel::native_t));
@@ -3067,6 +3083,10 @@ int gLastScrollUiFrame = -1000;
3067
3083
  if (coverage <= 0)
3068
3084
  continue;
3069
3085
 
3086
+ if (coverageRow) {
3087
+ coverageRow[xi - coverageX] = std::max<int>(coverageRow[xi - coverageX], coverage);
3088
+ continue;
3089
+ }
3070
3090
  int alpha = (coverage * static_cast<int>(command.projectedText.alpha) + 127) / 255;
3071
3091
  if (parentAlpha != 255) // parentAlpha==255 makes the second scale an identity
3072
3092
  alpha = (alpha * parentAlpha + 127) / 255;
@@ -4515,6 +4535,64 @@ int gLastScrollUiFrame = -1000;
4515
4535
  return true;
4516
4536
  }
4517
4537
 
4538
+ void cssRoundedBorderContours(const decltype(DisplayCommand{}.strokeRoundedRect) &s,
4539
+ TransformedRoundedRectCommand &outer,
4540
+ TransformedRoundedRectCommand &inner)
4541
+ {
4542
+ outer.lx = s.x; outer.ly = s.y; outer.lw = s.w; outer.lh = s.h;
4543
+ outer.tlRx8 = s.rx8[0]; outer.tlRy8 = s.ry8[0];
4544
+ outer.trRx8 = s.rx8[1]; outer.trRy8 = s.ry8[1];
4545
+ outer.brRx8 = s.rx8[2]; outer.brRy8 = s.ry8[2];
4546
+ outer.blRx8 = s.rx8[3]; outer.blRy8 = s.ry8[3];
4547
+ inner = outer;
4548
+ inner.lx += s.lineWidth; inner.ly += s.lineWidth;
4549
+ inner.lw = std::max(0, s.w - 2 * s.lineWidth);
4550
+ inner.lh = std::max(0, s.h - 2 * s.lineWidth);
4551
+ inner.tlRx8 = std::max(0, s.rx8[0] - 8 * s.lineWidth); inner.tlRy8 = std::max(0, s.ry8[0] - 8 * s.lineWidth);
4552
+ inner.trRx8 = std::max(0, s.rx8[1] - 8 * s.lineWidth); inner.trRy8 = std::max(0, s.ry8[1] - 8 * s.lineWidth);
4553
+ inner.brRx8 = std::max(0, s.rx8[2] - 8 * s.lineWidth); inner.brRy8 = std::max(0, s.ry8[2] - 8 * s.lineWidth);
4554
+ inner.blRx8 = std::max(0, s.rx8[3] - 8 * s.lineWidth); inner.blRy8 = std::max(0, s.ry8[3] - 8 * s.lineWidth);
4555
+ }
4556
+
4557
+ int cssRoundedBorderCoverage(const TransformedRoundedRectCommand &outer,
4558
+ const TransformedRoundedRectCommand &inner,
4559
+ int x, int y, int samples)
4560
+ {
4561
+ int coverage = 0;
4562
+ for (int iy = 0; iy < samples; ++iy) for (int ix = 0; ix < samples; ++ix) {
4563
+ const float px = x + (ix + 0.5f) / samples, py = y + (iy + 0.5f) / samples;
4564
+ coverage += roundedRectContainsFast(outer, px, py) && !roundedRectContainsFast(inner, px, py);
4565
+ }
4566
+ return coverage;
4567
+ }
4568
+
4569
+ void drawCssRoundedBorder(const DisplayCommand &command)
4570
+ {
4571
+ const auto &s = command.strokeRoundedRect;
4572
+ auto *canvas = gea::platform::display::Display::canvas();
4573
+ if (!canvas || !canvas->pixels() || s.w <= 0 || s.h <= 0 || s.lineWidth <= 0) return;
4574
+ TransformedRoundedRectCommand outer{}, inner{};
4575
+ cssRoundedBorderContours(s, outer, inner);
4576
+ int x0, y0, x1, y1;
4577
+ gea::platform::display::Display::clip(&x0, &y0, &x1, &y1);
4578
+ x0 = std::max({x0, 0, int(s.x)}); y0 = std::max({y0, 0, int(s.y)});
4579
+ x1 = std::min({x1, canvas->width() - 1, s.x + s.w - 1});
4580
+ y1 = std::min({y1, canvas->height() - 1, s.y + s.h - 1});
4581
+ if (x1 < x0 || y1 < y0) return;
4582
+ const int samples = std::max(1, gea::framework::graphics::Canvas::antialiasSamples());
4583
+ const int alpha = gea::platform::display::Display::alpha();
4584
+ if (!alpha) return;
4585
+ const int stride = canvas->strideBytes() / sizeof(gea::framework::graphics::pixel::native_t);
4586
+ for (int y = y0; y <= y1; ++y) {
4587
+ auto *row = canvas->pixels() + canvas->rowToPhysical(y) * stride;
4588
+ for (int x = x0; x <= x1; ++x) {
4589
+ const int coverage = cssRoundedBorderCoverage(outer, inner, x, y, samples);
4590
+ if (coverage) paintCoveragePixel(row[x], s.color, alpha, coverage, samples * samples);
4591
+ }
4592
+ }
4593
+ canvas->markDirty(x0, y0, x1, y1);
4594
+ }
4595
+
4518
4596
  void drawTransformedRoundedRect(const DisplayCommand &command)
4519
4597
  {
4520
4598
  auto *canvas = gea::platform::display::Display::canvas();
@@ -4631,28 +4709,20 @@ int gLastScrollUiFrame = -1000;
4631
4709
  bool hasRetainedTransformState(const Node &node)
4632
4710
  {
4633
4711
  const RareStyle &rs = rstyle(node.style); // one pool lookup, not 11
4634
- return rs.transform_rotate != 0 ||
4635
- node.render.previous_transform_rotate != 0 ||
4636
- rs.transform_rotate_x != 0 ||
4637
- node.render.previous_transform_rotate_x != 0 ||
4638
- rs.transform_rotate_y != 0 ||
4639
- node.render.previous_transform_rotate_y != 0 ||
4640
- rs.transform_translate_x != 0 ||
4641
- node.render.previous_transform_translate_x != 0 ||
4642
- rs.transform_translate_y != 0 ||
4643
- node.render.previous_transform_translate_y != 0 ||
4644
- rs.transform_translate_z != 0 ||
4645
- node.render.previous_transform_translate_z != 0 ||
4646
- rs.transform_translate_x_percent != 0 ||
4647
- node.render.previous_transform_translate_x_percent != 0 ||
4648
- rs.transform_translate_y_percent != 0 ||
4649
- node.render.previous_transform_translate_y_percent != 0 ||
4650
- rs.transform_scale_x != 1000 ||
4651
- node.render.previous_transform_scale_x != 1000 ||
4652
- rs.transform_scale_y != 1000 ||
4653
- node.render.previous_transform_scale_y != 1000 ||
4654
- rs.perspective > 0 ||
4655
- node.render.previous_perspective > 0;
4712
+ const bool currentTransformable = ViewRenderer::isTransformableBox(node);
4713
+ return (currentTransformable && hasIndividualLinearTransform(rs)) ||
4714
+ (node.render.previous_transformable_box && hadIndividualLinearTransform(node.render)) ||
4715
+ (currentTransformable && (rs.transform_rotate != 0 || rs.transform_rotate_x != 0 ||
4716
+ rs.transform_rotate_y != 0 || composedTranslateX(rs) != 0 || composedTranslateY(rs) != 0 ||
4717
+ composedTranslateZ(rs) != 0 || composedTranslateXPercent(rs) != 0 ||
4718
+ composedTranslateYPercent(rs) != 0 || rs.transform_scale_x != 1000 ||
4719
+ rs.transform_scale_y != 1000 || rs.transform_scale_z != 1000 || rs.perspective > 0)) ||
4720
+ (node.render.previous_transformable_box && (node.render.previous_transform_rotate != 0 ||
4721
+ node.render.previous_transform_rotate_x != 0 || node.render.previous_transform_rotate_y != 0 ||
4722
+ node.render.previous_transform_translate_x != 0 || node.render.previous_transform_translate_y != 0 ||
4723
+ node.render.previous_transform_translate_z != 0 || node.render.previous_transform_translate_x_percent != 0 ||
4724
+ node.render.previous_transform_translate_y_percent != 0 || node.render.previous_transform_scale_x != 1000 ||
4725
+ node.render.previous_transform_scale_y != 1000 || node.render.previous_transform_scale_z != 1000 || node.render.previous_perspective > 0));
4656
4726
  }
4657
4727
 
4658
4728
  struct ClipMath
@@ -4671,10 +4741,9 @@ int gLastScrollUiFrame = -1000;
4671
4741
 
4672
4742
  static void clampToNode(const Node &node, int *cx0, int *cy0, int *cx1, int *cy1)
4673
4743
  {
4674
- int nx0 = node.layout.x;
4675
- int ny0 = node.layout.y;
4676
- int nx1 = node.layout.x + node.layout.width - 1;
4677
- int ny1 = node.layout.y + node.layout.height - 1;
4744
+ int nx0, ny0, w, h;
4745
+ overflowClipBounds(node, nx0, ny0, w, h);
4746
+ int nx1 = nx0 + w - 1, ny1 = ny0 + h - 1;
4678
4747
  if (*cx0 < nx0)
4679
4748
  *cx0 = nx0;
4680
4749
  if (*cy0 < ny0)
@@ -4688,71 +4757,129 @@ int gLastScrollUiFrame = -1000;
4688
4757
 
4689
4758
  struct ChildZSorter
4690
4759
  {
4760
+ struct Key { int phase, level; };
4761
+ static bool effectGroup(const Node &node)
4762
+ {
4763
+ const auto &s = node.style;
4764
+ const auto &rs = rstyle(s);
4765
+ const bool transformable = ViewRenderer::isTransformableBox(node);
4766
+ return s.opacity < 255 || s.mask_right_fade_width > 0 || rs.filter_present || rs.filter_blur_radius ||
4767
+ (transformable && (rs.translate_present || rs.transform_present || rs.rotate_present || rs.scale_present ||
4768
+ rs.transform_preserve_3d || rs.perspective || rs.transform_rotate || rs.transform_rotate_x || rs.transform_rotate_y ||
4769
+ composedTranslateX(rs) || composedTranslateY(rs) || composedTranslateZ(rs) ||
4770
+ composedTranslateXPercent(rs) || composedTranslateYPercent(rs) ||
4771
+ rs.transform_scale_x != 1000 || rs.transform_scale_y != 1000 || rs.transform_scale_z != 1000)) || (rs.containment & (4 | 16));
4772
+ }
4773
+ static Key key(int id)
4774
+ {
4775
+ const Node *nodes = Tree::instance().nodes();
4776
+ const Node &n = nodes[id];
4777
+ const auto &s = n.style;
4778
+ const bool positioned = s.position != 0;
4779
+ const bool item = n.parent >= 0 && !isOutOfFlowPosition(s.position) &&
4780
+ (nodes[n.parent].style.display == kDisplayFlex || isDisplayGrid(nodes[n.parent].style));
4781
+ const int level = !s.z_index_auto && (positioned || item) ? s.z_index : 0;
4782
+ if (level < 0) return {0, level};
4783
+ if (level > 0) return {5, level};
4784
+ if (positioned || effectGroup(n) || (item && !s.z_index_auto)) return {4, 0};
4785
+ if (item) return {3, 0};
4786
+ if (s.float_side) return {2, 0};
4787
+ return {LayoutEngine::isInlineLevelNode(n) ? 3 : 1, 0};
4788
+ }
4789
+
4790
+ static int compareKeys(int first, int second)
4791
+ {
4792
+ const auto a = key(first), b = key(second);
4793
+ return a.phase != b.phase ? a.phase - b.phase : a.level - b.level;
4794
+ }
4795
+
4796
+ static bool participatesIn3DContext(int node, int contextRoot)
4797
+ {
4798
+ const Node *nodes = Tree::instance().nodes();
4799
+ if (contextRoot < 0 || !ViewRenderer::isTransformableBox(nodes[contextRoot]) || !preserves3D(nodes[contextRoot].style))
4800
+ return false;
4801
+ // A 3D rendering context follows DOM ancestry, not containing-block
4802
+ // ancestry. Every parent between a participant and the context root
4803
+ // must preserve 3D; a flat parent composites its descendants as a
4804
+ // single plane and prevents abs/fixed descendants from being hoisted
4805
+ // into this context.
4806
+ for (int parent = nodes[node].parent; parent >= 0 && parent != contextRoot; parent = nodes[parent].parent)
4807
+ if (!ViewRenderer::isTransformableBox(nodes[parent]) || !preserves3D(nodes[parent].style))
4808
+ return false;
4809
+ for (int parent = nodes[node].parent; parent >= 0; parent = nodes[parent].parent)
4810
+ if (parent == contextRoot)
4811
+ return true;
4812
+ return false;
4813
+ }
4814
+
4691
4815
  static int depth(int node)
4692
4816
  {
4693
4817
  Node *nodes = Tree::instance().nodes();
4694
4818
  return ViewRenderer::transformedDepth(nodes[node], false);
4695
4819
  }
4696
4820
 
4697
- static int subtreeDepth(int node)
4821
+ static int subtreeDepth(int node, int contextRoot)
4698
4822
  {
4699
4823
  Tree &tree = Tree::instance();
4700
4824
  Node *nodes = tree.nodes();
4701
- if (node < 0 || node >= tree.nodeCount())
4825
+ if (node < 0 || node >= tree.nodeCount() || !participatesIn3DContext(node, contextRoot))
4702
4826
  return 0;
4703
4827
  // Memoize per node per refresh. The child z-sort runs at every tree level,
4704
4828
  // so an ancestor's sort recurses a subtree that a descendant's sort then
4705
4829
  // re-walks. Caching collapses those repeated O(subtree) walks to O(1).
4706
4830
  static int memo[kMaxNodes];
4707
4831
  static uint64_t memoSerial[kMaxNodes];
4832
+ static int memoContextRoot[kMaxNodes];
4708
4833
  const uint64_t serial = tree.refreshSerial();
4709
- if (node < kMaxNodes && memoSerial[node] == serial)
4834
+ if (node < kMaxNodes && memoSerial[node] == serial && memoContextRoot[node] == contextRoot)
4710
4835
  return memo[node];
4711
4836
  int maxDepth = depth(node);
4712
4837
  for (int child = nodes[node].first_child; child >= 0 && child < tree.nodeCount(); child = nodes[child].next_sibling)
4713
4838
  {
4714
- maxDepth = std::max(maxDepth, subtreeDepth(child));
4839
+ if (participatesIn3DContext(child, contextRoot))
4840
+ maxDepth = std::max(maxDepth, subtreeDepth(child, contextRoot));
4715
4841
  }
4716
4842
  if (node < kMaxNodes)
4717
4843
  {
4718
4844
  memo[node] = maxDepth;
4719
4845
  memoSerial[node] = serial;
4846
+ memoContextRoot[node] = contextRoot;
4720
4847
  }
4721
4848
  return maxDepth;
4722
4849
  }
4723
4850
 
4724
4851
  static bool afterByDepth(int existing, int existingDepth, int candidate, int candidateDepth)
4725
4852
  {
4726
- Node *nodes = Tree::instance().nodes();
4727
- if (nodes[existing].style.z_index != nodes[candidate].style.z_index)
4728
- return nodes[existing].style.z_index > nodes[candidate].style.z_index;
4853
+ const int order = compareKeys(existing, candidate);
4854
+ if (order) return order > 0;
4729
4855
  return existingDepth > candidateDepth;
4730
4856
  }
4731
4857
 
4732
- static void sort(int *children, int child_count)
4858
+ static void sort(int *children, int child_count, int contextRoot)
4733
4859
  {
4734
4860
  if (child_count < 2)
4735
4861
  return;
4736
4862
  // Fast path: when document order already equals paint order, skip the
4737
- // sort entirely. That holds whenever no sibling carries a z-index AND no
4738
- // transform/3D depth is active — the overwhelmingly common 2D case
4863
+ // sort entirely when every sibling has the same paint phase and stack
4864
+ // level and no transform/3D depth is active — the common 2D case
4739
4865
  // (typography, breakout). subtreeDepth() recurses the whole subtree and
4740
4866
  // each node costs 5 double-precision projections, so this avoids it
4741
4867
  // entirely for 2D trees.
4742
- const bool transforms = ViewRenderer::anyTransformActive();
4743
- Node *nodes = Tree::instance().nodes();
4868
+ const bool transforms = contextRoot >= 0 && ViewRenderer::isTransformableBox(Tree::instance().nodes()[contextRoot]) &&
4869
+ preserves3D(Tree::instance().nodes()[contextRoot].style) &&
4870
+ ViewRenderer::anyTransformActive();
4744
4871
  if (!transforms)
4745
4872
  {
4746
- bool anyZ = false;
4747
- for (int i = 0; i < child_count; i++)
4873
+ bool mixed = false;
4874
+ for (int i = 1; i < child_count; i++)
4748
4875
  {
4749
- if (nodes[children[i]].style.z_index != 0)
4876
+ if (compareKeys(children[0], children[i]))
4750
4877
  {
4751
- anyZ = true;
4878
+ mixed = true;
4752
4879
  break;
4753
4880
  }
4754
4881
  }
4755
- if (!anyZ)
4882
+ if (!mixed)
4756
4883
  return;
4757
4884
  }
4758
4885
  // Precompute each child's painter's-depth key ONCE. Recomputing
@@ -4761,12 +4888,12 @@ int gLastScrollUiFrame = -1000;
4761
4888
  // css-animation-showcase: 21 children → ~440 comparisons, each doing a
4762
4889
  // recursive subtree walk of 5 projections/node). Computing it up front is
4763
4890
  // O(k * subtree) + O(k^2) integer comparisons. Depth only varies under an
4764
- // active transform; without one it's constant, so skip the walk. sort()
4765
- // is not re-entrant (it finishes before recordNode recurses), so a static
4766
- // scratch buffer is safe and keeps it off the recursion stack.
4767
- static int depthKey[kMaxChildren];
4891
+ // active transform; without one it's constant, so skip the walk. A
4892
+ // context may include hoisted descendants beyond the immediate-child
4893
+ // scratch limit, so size this buffer to its actual participant count.
4894
+ std::vector<int> depthKey(child_count);
4768
4895
  for (int i = 0; i < child_count; i++)
4769
- depthKey[i] = transforms ? subtreeDepth(children[i]) : 0;
4896
+ depthKey[i] = transforms ? subtreeDepth(children[i], contextRoot) : 0;
4770
4897
  for (int i = 1; i < child_count; i++)
4771
4898
  {
4772
4899
  const int childKey = children[i];
@@ -4794,10 +4921,15 @@ int gLastScrollUiFrame = -1000;
4794
4921
  c->by += dy;
4795
4922
  switch (c->type)
4796
4923
  {
4797
- case DisplayCommandType::PushClip:
4798
- c->clip.x += dx;
4799
- c->clip.y += dy;
4924
+ case DisplayCommandType::PushClip: {
4925
+ const int id = c->clip.nodeId;
4926
+ const Node *owner = id >= 0 && id < Tree::instance().nodeCount() ? &Tree::instance().nodes()[id] : nullptr;
4927
+ if (!owner || overflowX(owner->style)) c->clip.x += dx;
4928
+ else c->bx -= dx;
4929
+ if (!owner || overflowY(owner->style)) c->clip.y += dy;
4930
+ else c->by -= dy;
4800
4931
  break;
4932
+ }
4801
4933
  case DisplayCommandType::FillRect:
4802
4934
  c->fill.x += dx;
4803
4935
  c->fill.y += dy;
@@ -4936,8 +5068,6 @@ int gLastScrollUiFrame = -1000;
4936
5068
  if (rowX0 > rowX1)
4937
5069
  continue;
4938
5070
  int spanW = rowX1 - rowX0 + 1;
4939
- if (spanW > gea::platform::display::kWidth)
4940
- spanW = gea::platform::display::kWidth;
4941
5071
  const long long off = static_cast<long long>(y) * scrW + rowX0;
4942
5072
  gea::platform::display::Display::blitImage(color + off, alpha + off, spanW, 1, rowX0, y);
4943
5073
  }
@@ -5340,12 +5470,14 @@ int gLastScrollUiFrame = -1000;
5340
5470
  static_cast<long long>(scrW) * scrH;
5341
5471
  const bool bgCacheable = bgBuf && bgCanvas && bgCanvas->pixels() && bgOpaque && bgLarge &&
5342
5472
  scrW > 0 && scrH > 0 && bgCap >= scrW * scrH;
5343
- const std::uint16_t bgKeyNow[10] = {
5473
+ const std::uint16_t bgKeyNow[14] = {
5344
5474
  static_cast<std::uint16_t>(c.gradient.fromColor), static_cast<std::uint16_t>(c.gradient.midColor), static_cast<std::uint16_t>(c.gradient.toColor), c.gradient.midStop,
5345
5475
  c.gradient.toStop, static_cast<std::uint16_t>(c.gradient.angle), c.gradient.fromAlpha,
5346
- c.gradient.midAlpha, c.gradient.toAlpha, c.gradient.hasMid};
5347
- static std::uint16_t bgKey[10];
5348
- static std::uint16_t bgLastKey[10];
5476
+ c.gradient.midAlpha, c.gradient.toAlpha, c.gradient.hasMid,
5477
+ static_cast<std::uint16_t>(c.gradient.x), static_cast<std::uint16_t>(c.gradient.y),
5478
+ static_cast<std::uint16_t>(c.gradient.w), static_cast<std::uint16_t>(c.gradient.h)};
5479
+ static std::uint16_t bgKey[14];
5480
+ static std::uint16_t bgLastKey[14];
5349
5481
  static int bgStable = 0;
5350
5482
  static bool bgValid = false;
5351
5483
  static int bgKW = 0, bgKH = 0;
@@ -5359,8 +5491,6 @@ int gLastScrollUiFrame = -1000;
5359
5491
  if (rowX0 > rowX1)
5360
5492
  continue;
5361
5493
  int spanW = rowX1 - rowX0 + 1;
5362
- if (spanW > gea::platform::display::kWidth)
5363
- spanW = gea::platform::display::kWidth;
5364
5494
  gea::platform::display::Display::blitImage(
5365
5495
  bgBuf + static_cast<long long>(y) * scrW + rowX0, nullptr, spanW, 1, rowX0, y);
5366
5496
  }
@@ -5582,62 +5712,64 @@ int gLastScrollUiFrame = -1000;
5582
5712
  if (rowX0 > rowX1)
5583
5713
  continue;
5584
5714
  const uint8_t *bayerRow = bayer4[y & 3];
5585
- int spanW = rowX1 - rowX0 + 1;
5586
- if (spanW > gea::platform::display::kWidth)
5587
- spanW = gea::platform::display::kWidth;
5588
- float projection = (static_cast<float>(rowX0) + 0.5f) * stepX + (static_cast<float>(y) + 0.5f) * stepY;
5589
- bool rowOpaque = true;
5590
- if (stepX == 0.0f)
5715
+ // Keep the device-sized buffer, but cover wider simulator viewports in chunks.
5716
+ for (int chunkX = rowX0; chunkX <= rowX1; chunkX += gea::platform::display::kWidth)
5591
5717
  {
5592
- // Vertical gradient (the common `to bottom` case): the projection —
5593
- // hence the permille bucket, LUT entry, and alpha — is constant
5594
- // across the row; only the 4-periodic bayer COLUMN pattern varies.
5595
- // Compute the 4-pixel pattern once with exactly the per-pixel math
5596
- // (bit-identical) and fill the row at store speed instead of paying
5597
- // the DDA + clamp + LUT + dither chain per pixel.
5598
- const int permille = clampPermille(static_cast<int>((projection - fMinProjection) * invSpanPermille + 0.5f));
5599
- const DitherStop &e = lut[permille];
5600
- gea::framework::graphics::pixel::native_t pattern[4];
5601
- for (int k = 0; k < 4; k++)
5718
+ const int spanW = std::min(gea::platform::display::kWidth, rowX1 - chunkX + 1);
5719
+ float projection = (static_cast<float>(chunkX) + 0.5f) * stepX + (static_cast<float>(y) + 0.5f) * stepY;
5720
+ bool rowOpaque = true;
5721
+ if (stepX == 0.0f)
5602
5722
  {
5723
+ // Vertical gradient (the common `to bottom` case): the projection —
5724
+ // hence the permille bucket, LUT entry, and alpha — is constant
5725
+ // across the row; only the 4-periodic bayer COLUMN pattern varies.
5726
+ // Compute the 4-pixel pattern once with exactly the per-pixel math
5727
+ // (bit-identical) and fill the row at store speed instead of paying
5728
+ // the DDA + clamp + LUT + dither chain per pixel.
5729
+ const int permille = clampPermille(static_cast<int>((projection - fMinProjection) * invSpanPermille + 0.5f));
5730
+ const DitherStop &e = lut[permille];
5731
+ gea::framework::graphics::pixel::native_t pattern[4];
5732
+ for (int k = 0; k < 4; k++)
5733
+ {
5603
5734
  #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565
5604
- const int threshold = bayerRow[k] * 16 + 8;
5605
- const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
5606
- const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
5607
- const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
5608
- pattern[k] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
5735
+ const int threshold = bayerRow[k] * 16 + 8;
5736
+ const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
5737
+ const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
5738
+ const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
5739
+ pattern[k] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
5609
5740
  #else
5610
- pattern[k] = e.color;
5741
+ pattern[k] = e.color;
5611
5742
  #endif
5743
+ }
5744
+ for (int i = 0; i < spanW; i++)
5745
+ rowColor[i] = pattern[(chunkX + i) & 3];
5746
+ std::memset(rowAlpha, e.alpha, static_cast<std::size_t>(spanW));
5747
+ rowOpaque = e.alpha == 255;
5612
5748
  }
5613
- for (int i = 0; i < spanW; i++)
5614
- rowColor[i] = pattern[(rowX0 + i) & 3];
5615
- std::memset(rowAlpha, e.alpha, static_cast<std::size_t>(spanW));
5616
- rowOpaque = e.alpha == 255;
5617
- }
5618
- else
5619
- for (int i = 0; i < spanW; i++, projection += stepX)
5620
- {
5621
- const int permille = clampPermille(static_cast<int>((projection - fMinProjection) * invSpanPermille + 0.5f));
5622
- const DitherStop &e = lut[permille];
5749
+ else
5750
+ for (int i = 0; i < spanW; i++, projection += stepX)
5751
+ {
5752
+ const int permille = clampPermille(static_cast<int>((projection - fMinProjection) * invSpanPermille + 0.5f));
5753
+ const DitherStop &e = lut[permille];
5623
5754
  #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565
5624
- const int threshold = bayerRow[(rowX0 + i) & 3] * 16 + 8;
5625
- const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
5626
- const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
5627
- const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
5628
- rowColor[i] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
5755
+ const int threshold = bayerRow[(chunkX + i) & 3] * 16 + 8;
5756
+ const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
5757
+ const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
5758
+ const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
5759
+ rowColor[i] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
5629
5760
  #else
5630
- rowColor[i] = e.color; // full colour: no dither (8888 doesn't band)
5761
+ rowColor[i] = e.color; // full colour: no dither (8888 doesn't band)
5631
5762
  #endif
5632
- rowAlpha[i] = e.alpha;
5633
- if (e.alpha != 255)
5634
- rowOpaque = false;
5763
+ rowAlpha[i] = e.alpha;
5764
+ if (e.alpha != 255)
5765
+ rowOpaque = false;
5766
+ }
5767
+ // alpha=nullptr -> blitImage's opaque memcpy (baseAlpha==255) or whole-row
5768
+ // global blend (baseAlpha<255); the rowAlpha mask -> per-pixel combine
5769
+ // with baseAlpha. Equivalent to the old fillRun(combineAlpha(baseAlpha,
5770
+ // localAlpha)). baseAlpha is read from Display::alpha() inside blitImage.
5771
+ gea::platform::display::Display::blitImage(rowColor, rowOpaque ? nullptr : rowAlpha, spanW, 1, chunkX, y);
5635
5772
  }
5636
- // alpha=nullptr -> blitImage's opaque memcpy (baseAlpha==255) or whole-row
5637
- // global blend (baseAlpha<255); the rowAlpha mask -> per-pixel combine
5638
- // with baseAlpha. Equivalent to the old fillRun(combineAlpha(baseAlpha,
5639
- // localAlpha)). baseAlpha is read from Display::alpha() inside blitImage.
5640
- gea::platform::display::Display::blitImage(rowColor, rowOpaque ? nullptr : rowAlpha, spanW, 1, rowX0, y);
5641
5773
  }
5642
5774
 
5643
5775
  // Once the gradient has been identical for 2 frames, render it in full into
@@ -5993,40 +6125,42 @@ int gLastScrollUiFrame = -1000;
5993
6125
  continue;
5994
6126
  const float dy = (static_cast<float>(y) + 0.5f - centerY) * invRadiusY;
5995
6127
  const uint8_t *bayerRow = bayer4[y & 3];
5996
- int spanW = rowX1 - rowX0 + 1;
5997
- if (spanW > gea::platform::display::kWidth)
5998
- spanW = gea::platform::display::kWidth;
5999
- bool rowOpaque = true;
6000
- for (int i = 0; i < spanW; ++i)
6128
+ // Keep the device-sized buffer, but cover wider simulator viewports in chunks.
6129
+ for (int chunkX = rowX0; chunkX <= rowX1; chunkX += gea::platform::display::kWidth)
6001
6130
  {
6002
- const int xx = rowX0 + i;
6003
- const int mi = mirrorSum - xx - rowX0; // index of xx's mirror in this span
6004
- int permille;
6005
- if (mirrorable && mi >= 0 && mi < i)
6131
+ const int spanW = std::min(gea::platform::display::kWidth, rowX1 - chunkX + 1);
6132
+ bool rowOpaque = true;
6133
+ for (int i = 0; i < spanW; ++i)
6006
6134
  {
6007
- permille = rowPermille[mi];
6008
- }
6009
- else
6010
- {
6011
- const float dx = (static_cast<float>(xx) + 0.5f - centerX) * invRadiusX;
6012
- permille = static_cast<int>(std::sqrt(dx * dx + dy * dy) * 1000.0f + 0.5f);
6013
- }
6014
- rowPermille[i] = static_cast<std::uint16_t>(permille > 1000 ? 1000 : permille);
6015
- const LinearGradientDrawer::DitherStop &e = lut[rowPermille[i]];
6135
+ const int xx = chunkX + i;
6136
+ const int mi = mirrorSum - xx - chunkX; // index of xx's mirror in this span
6137
+ int permille;
6138
+ if (mirrorable && mi >= 0 && mi < i)
6139
+ {
6140
+ permille = rowPermille[mi];
6141
+ }
6142
+ else
6143
+ {
6144
+ const float dx = (static_cast<float>(xx) + 0.5f - centerX) * invRadiusX;
6145
+ permille = static_cast<int>(std::sqrt(dx * dx + dy * dy) * 1000.0f + 0.5f);
6146
+ }
6147
+ rowPermille[i] = static_cast<std::uint16_t>(permille > 1000 ? 1000 : permille);
6148
+ const LinearGradientDrawer::DitherStop &e = lut[rowPermille[i]];
6016
6149
  #if GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565
6017
- const int threshold = bayerRow[xx & 3] * 16 + 8;
6018
- const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
6019
- const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
6020
- const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
6021
- rowColor[i] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
6150
+ const int threshold = bayerRow[xx & 3] * 16 + 8;
6151
+ const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
6152
+ const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
6153
+ const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
6154
+ rowColor[i] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
6022
6155
  #else
6023
- rowColor[i] = e.color; // full colour: no dither
6156
+ rowColor[i] = e.color; // full colour: no dither
6024
6157
  #endif
6025
- rowAlpha[i] = e.alpha;
6026
- if (e.alpha != 255)
6027
- rowOpaque = false;
6158
+ rowAlpha[i] = e.alpha;
6159
+ if (e.alpha != 255)
6160
+ rowOpaque = false;
6161
+ }
6162
+ gea::platform::display::Display::blitImage(rowColor, rowOpaque ? nullptr : rowAlpha, spanW, 1, chunkX, y);
6028
6163
  }
6029
- gea::platform::display::Display::blitImage(rowColor, rowOpaque ? nullptr : rowAlpha, spanW, 1, rowX0, y);
6030
6164
  }
6031
6165
 
6032
6166
  // Bake the full box into the cache once params are stable for 2 frames.
@@ -6055,6 +6189,93 @@ int gLastScrollUiFrame = -1000;
6055
6189
 
6056
6190
  struct DisplayCommandDrawer
6057
6191
  {
6192
+ struct TextInkCommand {
6193
+ const DisplayCommand *command;
6194
+ int x0, y0, x1, y1;
6195
+ };
6196
+
6197
+ static void collectTextInk(int id, int owner, int x0, int y0, int x1, int y1,
6198
+ std::vector<TextInkCommand> &ink)
6199
+ {
6200
+ auto &tree = Tree::instance();
6201
+ if (id < 0 || id >= tree.nodeCount()) return;
6202
+ const auto &node = tree.nodes()[id];
6203
+ if (node.style.display == 1 || (id != owner && isOutOfFlowPosition(node.style.position))) return;
6204
+ if (state.hasNodeScratchFor(id) && node.style.visibility == 0) {
6205
+ const int begin = state.nodeDrawStart[id], end = state.nodeDrawEnd[id];
6206
+ for (int ci = std::max(0, begin); ci < end && ci < state.commandCount; ++ci) {
6207
+ const auto &c = state.commands[ci];
6208
+ if (c.type == DisplayCommandType::DrawText || c.type == DisplayCommandType::DrawProjectedText || c.textDecorationInk)
6209
+ ink.push_back({&c, x0, y0, x1, y1});
6210
+ }
6211
+ }
6212
+ // Match the overflow clip recorded for descendants of this node.
6213
+ // Positioned in-flow and stacking-context descendants still contribute;
6214
+ // their opacity does not change the shape of the mask.
6215
+ const int clipIndex = state.hasNodeScratchFor(id) ? state.nodeDrawEnd[id] : -1;
6216
+ if (clipIndex >= 0 && clipIndex < state.commandCount) {
6217
+ const auto &clip = state.commands[clipIndex];
6218
+ if (clip.type == DisplayCommandType::PushClip && clip.clip.nodeId == id) {
6219
+ x0 = std::max<int>(x0, clip.clip.x); y0 = std::max<int>(y0, clip.clip.y);
6220
+ x1 = std::min<int>(x1, clip.clip.x+clip.clip.w-1); y1 = std::min<int>(y1, clip.clip.y+clip.clip.h-1);
6221
+ }
6222
+ }
6223
+ if (x0 > x1 || y0 > y1) return;
6224
+ for (int child = node.first_child; child >= 0; child = tree.nodes()[child].next_sibling)
6225
+ collectTextInk(child, owner, x0, y0, x1, y1, ink);
6226
+ }
6227
+
6228
+ static void replayTextClipped(const DisplayCommand &command)
6229
+ {
6230
+ using gea::platform::display::Display;
6231
+ using namespace gea::framework::graphics;
6232
+ auto *canvas = Display::canvas();
6233
+ if (!canvas) return;
6234
+ int x0, y0, x1, y1;
6235
+ Display::clip(&x0, &y0, &x1, &y1);
6236
+ x0 = std::max({x0, 0, static_cast<int>(command.bx)});
6237
+ y0 = std::max({y0, 0, static_cast<int>(command.by)});
6238
+ x1 = std::min({x1, canvas->width()-1, command.bx+command.bw-1});
6239
+ y1 = std::min({y1, canvas->height()-1, command.by+command.bh-1});
6240
+ if (x0 > x1 || y0 > y1) return;
6241
+ std::vector<TextInkCommand> ink;
6242
+ collectTextInk(command.textClipOwner, command.textClipOwner, x0, y0, x1, y1, ink);
6243
+ if (ink.empty()) return;
6244
+ DisplayCommand paint = command;
6245
+ paint.textClipOwner = -1;
6246
+ // Stack-local, bounded scratch remains safe if independent dirty rows
6247
+ // are replayed on separate workers. No viewport-sized backing store.
6248
+ constexpr int chunkSize = 128;
6249
+ pixel::native_t before[chunkSize];
6250
+ uint8_t coverage[chunkSize];
6251
+ for (int y = y0; y <= y1; ++y) for (int x = x0; x <= x1; x += chunkSize) {
6252
+ const int width = std::min(chunkSize, x1-x+1);
6253
+ std::fill_n(coverage, width, 0);
6254
+ Display::pushClip(x, y, width, 1);
6255
+ for (const auto &entry : ink) {
6256
+ if (y < entry.y0 || y > entry.y1 || x > entry.x1 || x+width <= entry.x0) continue;
6257
+ const auto &c = *entry.command;
6258
+ Display::pushClip(entry.x0, entry.y0, entry.x1-entry.x0+1, entry.y1-entry.y0+1);
6259
+ if (c.type == DisplayCommandType::DrawText)
6260
+ TextRenderer::unionCoverageRow(c, y, x, width, coverage);
6261
+ else if (c.type == DisplayCommandType::DrawProjectedText)
6262
+ drawProjectedText(c, coverage, x);
6263
+ else if (c.textDecorationInk && c.type == DisplayCommandType::FillRect && y >= c.fill.y && y < c.fill.y+c.fill.h)
6264
+ for (int px = std::max({x, entry.x0, static_cast<int>(c.fill.x)}); px < std::min({x+width, entry.x1+1, c.fill.x+c.fill.w}); ++px)
6265
+ coverage[px-x] = 255;
6266
+ Display::popClip();
6267
+ }
6268
+ if (std::any_of(coverage, coverage+width, [](uint8_t a) { return a != 0; })) {
6269
+ for (int i = 0; i < width; ++i) before[i] = canvas->readPixelNative(x+i, y);
6270
+ replay(paint);
6271
+ for (int i = 0; i < width; ++i) if (coverage[i] != 255)
6272
+ canvas->writePixelNativeExact(x+i, y, coverage[i] == 0 ? before[i] :
6273
+ pixel::blendNative(canvas->readPixelNative(x+i, y), before[i], coverage[i]));
6274
+ }
6275
+ Display::popClip();
6276
+ }
6277
+ }
6278
+
6058
6279
  static void noteReplayCommand(const DisplayCommand &c)
6059
6280
  {
6060
6281
  auto &perf = refreshPerfStatsMutable();
@@ -6084,6 +6305,11 @@ int gLastScrollUiFrame = -1000;
6084
6305
 
6085
6306
  static void GEA_RENDER_HOT_SRAM replay(const DisplayCommand &c)
6086
6307
  {
6308
+ if (c.textClipOwner >= 0 && c.type != DisplayCommandType::SetAlpha &&
6309
+ c.type != DisplayCommandType::PushClip && c.type != DisplayCommandType::PopClip) {
6310
+ replayTextClipped(c);
6311
+ return;
6312
+ }
6087
6313
  noteReplayCommand(c);
6088
6314
  const std::int64_t __replayT0 = refreshPerfNowUs();
6089
6315
  switch (c.type)
@@ -6130,6 +6356,7 @@ int gLastScrollUiFrame = -1000;
6130
6356
  gea::platform::display::Display::strokeRect(c.stroke.x, c.stroke.y, c.stroke.w, c.stroke.h, c.stroke.color);
6131
6357
  break;
6132
6358
  case DisplayCommandType::StrokeRoundedRect:
6359
+ if (c.strokeRoundedRect.cssRadii) { drawCssRoundedBorder(c); break; }
6133
6360
  gea::platform::display::Display::strokeRoundedRect(c.strokeRoundedRect.x, c.strokeRoundedRect.y,
6134
6361
  c.strokeRoundedRect.w, c.strokeRoundedRect.h,
6135
6362
  c.strokeRoundedRect.tl, c.strokeRoundedRect.tr, c.strokeRoundedRect.br, c.strokeRoundedRect.bl,
@@ -6311,7 +6538,7 @@ int gLastScrollUiFrame = -1000;
6311
6538
  const bool simpleBackgroundLeaf = node.first_child < 0 &&
6312
6539
  node.type == NodeType::View &&
6313
6540
  node.style.has_bg &&
6314
- node.style.bg_fill == 0 &&
6541
+ !styleHasBackgroundImage(node.style) && rstyle(node.style).bg_clip == 0 &&
6315
6542
  !hasAnyBorder(node.style);
6316
6543
  if (!simpleBackgroundLeaf)
6317
6544
  return -1;
@@ -6375,7 +6602,28 @@ int gLastScrollUiFrame = -1000;
6375
6602
  cmd->filterBlur.sourceAlphaCap = static_cast<int16_t>(filterBlurSourceAlphaCap(node, parentAlpha));
6376
6603
  }
6377
6604
 
6378
- static void recordNode(int id, uint8_t parent_alpha, int cx0, int cy0, int cx1, int cy1, const Node *mask_node, bool insideDirty = false)
6605
+ struct RecordClipFrame {
6606
+ const Node &node;
6607
+ const RecordClipFrame *parent;
6608
+ };
6609
+
6610
+ // Fixed descendants are painted in place in z-order, but their viewport
6611
+ // containing block is outside the ordinary ancestors' overflow clips.
6612
+ struct SuspendedRecordClips {
6613
+ const RecordClipFrame *clips;
6614
+ explicit SuspendedRecordClips(const RecordClipFrame *value) : clips(value) {
6615
+ for (auto *frame = clips; frame; frame = frame->parent)
6616
+ ViewRenderer::recordClipEnd(frame->node);
6617
+ }
6618
+ static void restore(const RecordClipFrame *frame) {
6619
+ if (!frame) return;
6620
+ restore(frame->parent);
6621
+ ViewRenderer::recordClipBegin(frame->node);
6622
+ }
6623
+ ~SuspendedRecordClips() { restore(clips); }
6624
+ };
6625
+
6626
+ static void recordNode(int id, uint8_t parent_alpha, int cx0, int cy0, int cx1, int cy1, const Node *mask_node, bool insideDirty = false, const RecordClipFrame *clips = nullptr, bool groupRoot = true, bool includePositioned = true)
6379
6627
  {
6380
6628
  if (!state.hasNodeScratchFor(id))
6381
6629
  return;
@@ -6391,7 +6639,7 @@ int gLastScrollUiFrame = -1000;
6391
6639
  return;
6392
6640
  }
6393
6641
  Node *n = &Tree::instance().nodes()[id];
6394
- if (n->style.display == 1)
6642
+ if (n->style.display == 1 || isCollapsedFlexSubtree(*n) || ViewRenderer::backfaceSubtreeHidden(*n))
6395
6643
  {
6396
6644
  state.clearNodeRange(id);
6397
6645
  return;
@@ -6413,14 +6661,35 @@ int gLastScrollUiFrame = -1000;
6413
6661
  // guarantees the parent's background fill is what replays in the
6414
6662
  // dirty rect — same semantics as display:none, without forcing app
6415
6663
  // code to choose between the two patterns.
6416
- if (n->style.opacity == 0)
6664
+ bool contributesTextMask = false;
6665
+ if (n->style.opacity == 0) {
6666
+ const auto *nodes = Tree::instance().nodes();
6667
+ for (int ancestor = id; ancestor >= 0; ancestor = nodes[ancestor].parent) {
6668
+ if (isOutOfFlowPosition(nodes[ancestor].style.position)) break;
6669
+ const int parent = nodes[ancestor].parent;
6670
+ if (parent >= 0 && StyleValues::hasTextBackgroundClip(nodes[parent].style)) {
6671
+ contributesTextMask = true; break;
6672
+ }
6673
+ }
6674
+ }
6675
+ if (n->style.opacity == 0 && !contributesTextMask)
6417
6676
  {
6418
6677
  state.clearNodeRange(id);
6419
6678
  return;
6420
6679
  }
6421
6680
 
6422
- int overlaps_clip = ClipMath::nodeOverlapsClip(*n, cx0, cy0, cx1, cy1);
6423
- if (!overlaps_clip && (!isViewLikeNodeType(n->type) || n->style.overflow != 0))
6681
+ const bool viewportFixed = LayoutEngine::isViewportFixed(*n);
6682
+ SuspendedRecordClips suspended(viewportFixed ? clips : nullptr);
6683
+ if (viewportFixed) {
6684
+ clips = nullptr;
6685
+ cx0 = cy0 = 0;
6686
+ const auto *canvas = gea::platform::display::Display::canvas();
6687
+ cx1 = (canvas ? canvas->width() : treeState().mountedWidth) - 1;
6688
+ cy1 = (canvas ? canvas->height() : treeState().mountedHeight) - 1;
6689
+ }
6690
+ int overlaps_clip = isDocumentCanvasRoot(*n) || ClipMath::nodeOverlapsClip(*n, cx0, cy0, cx1, cy1);
6691
+ if (!overlaps_clip && (!isViewLikeNodeType(n->type) || (overflowX(n->style) && overflowY(n->style))) &&
6692
+ !LayoutEngine::containsViewportFixed(id))
6424
6693
  {
6425
6694
  state.clearNodeRange(id);
6426
6695
  return;
@@ -6432,7 +6701,10 @@ int gLastScrollUiFrame = -1000;
6432
6701
  int h = n->layout.height;
6433
6702
 
6434
6703
  DisplayList &list = DisplayList::instance();
6435
- int pushed_clip = overlaps_clip ? ViewRenderer::recordClipBegin(*n) : 0;
6704
+ // A culled ancestor may still be traversed to reach a fixed child.
6705
+ // Keep its clip active for ordinary children; the fixed child alone
6706
+ // suspends that clip when it enters viewport coordinates.
6707
+ int pushed_clip = 0;
6436
6708
  uint8_t cur_alpha = parent_alpha;
6437
6709
  const Node *active_mask = mask_node;
6438
6710
 
@@ -6457,6 +6729,7 @@ int gLastScrollUiFrame = -1000;
6457
6729
  active_mask = n;
6458
6730
 
6459
6731
  int draw_start = state.commandCount;
6732
+ const bool paintsOwnBox = overlaps_clip && n->style.visibility == 0;
6460
6733
  const bool nativeTextInput = isNativeTextInputView(*n);
6461
6734
  const bool filtered = overlaps_clip && rstyle(n->style).filter_blur_radius > 0;
6462
6735
  uint8_t draw_alpha = cur_alpha;
@@ -6474,26 +6747,26 @@ int gLastScrollUiFrame = -1000;
6474
6747
  if (filtered)
6475
6748
  recordFilterBlur(id, *n, DisplayCommandType::BeginFilterBlur, draw_alpha);
6476
6749
 
6477
- if (overlaps_clip && !nativeTextInput)
6750
+ if (paintsOwnBox && !nativeTextInput)
6478
6751
  ViewRenderer::recordBox(*n, draw_alpha);
6479
6752
 
6480
- if (overlaps_clip && n->type == NodeType::Text)
6753
+ if (paintsOwnBox && n->type == NodeType::Text)
6481
6754
  {
6482
6755
  TextRenderer::record(*n, draw_alpha);
6483
6756
  }
6484
- else if (overlaps_clip && n->type == NodeType::Image)
6757
+ else if (paintsOwnBox && n->type == NodeType::Image)
6485
6758
  {
6486
6759
  ImageRenderer::record(*n);
6487
6760
  }
6488
- else if (overlaps_clip && n->type == NodeType::Canvas)
6761
+ else if (paintsOwnBox && n->type == NodeType::Canvas)
6489
6762
  {
6490
6763
  CanvasRenderer::record(*n);
6491
6764
  }
6492
- else if (overlaps_clip && n->type == NodeType::Camera)
6765
+ else if (paintsOwnBox && n->type == NodeType::Camera)
6493
6766
  {
6494
6767
  CameraRenderer::record(*n);
6495
6768
  }
6496
- else if (overlaps_clip && n->type == NodeType::View)
6769
+ else if (paintsOwnBox && n->type == NodeType::View)
6497
6770
  {
6498
6771
  // `<input>` JSX elements lower to a View node with tag_name "input".
6499
6772
  // On native text-input targets, UIKit/AppKit owns the full visual
@@ -6509,14 +6782,10 @@ int gLastScrollUiFrame = -1000;
6509
6782
  if (draw_end > draw_start && state.drawNodeOrderCount < state.nodeScratchCapacity)
6510
6783
  state.drawNodeOrder[state.drawNodeOrderCount++] = id;
6511
6784
 
6512
- int depth = state.recordDepth++;
6513
- int child_count = 0;
6514
- int *children = state.childrenForDepth(depth);
6515
- if (children)
6516
- {
6517
- child_count = LayoutEngine::instance().collectChildren(id, children, kMaxChildren, false);
6518
- ChildZSorter::sort(children, child_count);
6519
- }
6785
+ // A box's own background, border and outline are outside its content clip.
6786
+ pushed_clip = ViewRenderer::recordClipBegin(*n);
6787
+ ++state.recordDepth;
6788
+ const auto children = PaintOrder::collectChildren(id, groupRoot, includePositioned);
6520
6789
 
6521
6790
  int child_cx0 = cx0, child_cy0 = cy0, child_cx1 = cx1, child_cy1 = cy1;
6522
6791
  if (pushed_clip && id == state.recordExpandedClipNode)
@@ -6529,12 +6798,38 @@ int gLastScrollUiFrame = -1000;
6529
6798
  else if (pushed_clip)
6530
6799
  ClipMath::clampToNode(*n, &child_cx0, &child_cy0, &child_cx1, &child_cy1);
6531
6800
 
6532
- for (int i = 0; i < child_count; i++)
6533
- recordNode(children[i], cur_alpha, child_cx0, child_cy0, child_cx1, child_cy1, active_mask, insideDirty || n->render.dirty);
6801
+ const RecordClipFrame ownClip{*n, clips};
6802
+ for (int child : children) {
6803
+ // A positioned descendant can paint in this context while its
6804
+ // intervening non-stacking ancestors still supply overflow clips.
6805
+ Node *nodes = Tree::instance().nodes();
6806
+ std::vector<int> ancestors;
6807
+ bool dirty = insideDirty || n->render.dirty;
6808
+ for (int p = nodes[child].parent; p >= 0 && p != id; p = nodes[p].parent) {
6809
+ dirty |= nodes[p].render.dirty;
6810
+ if (nodes[p].style.overflow != 0) ancestors.push_back(p);
6811
+ }
6812
+ std::vector<RecordClipFrame> frames;
6813
+ frames.reserve(ancestors.size());
6814
+ const RecordClipFrame *childClips = pushed_clip ? &ownClip : clips;
6815
+ int x0 = child_cx0, y0 = child_cy0, x1 = child_cx1, y1 = child_cy1;
6816
+ for (auto p = ancestors.rbegin(); p != ancestors.rend(); ++p) {
6817
+ const Node &ancestor = nodes[*p];
6818
+ if (ViewRenderer::recordClipBegin(ancestor)) {
6819
+ frames.push_back({ancestor, childClips}); childClips = &frames.back();
6820
+ if (*p == state.recordExpandedClipNode) {
6821
+ x0 = state.recordExpandedClipX0; y0 = state.recordExpandedClipY0;
6822
+ x1 = state.recordExpandedClipX1; y1 = state.recordExpandedClipY1;
6823
+ } else ClipMath::clampToNode(ancestor, &x0, &y0, &x1, &y1);
6824
+ }
6825
+ }
6826
+ recordNode(child, cur_alpha, x0, y0, x1, y1, active_mask, dirty, childClips, PaintOrder::isGroup(child), PaintOrder::isContext(child));
6827
+ for (auto frame = frames.rbegin(); frame != frames.rend(); ++frame) ViewRenderer::recordClipEnd(frame->node);
6828
+ }
6534
6829
 
6535
6830
  state.recordDepth--;
6536
6831
 
6537
- if (overlaps_clip && !nativeTextInput)
6832
+ if (paintsOwnBox && !nativeTextInput)
6538
6833
  ViewRenderer::recordScrollbar(*n);
6539
6834
 
6540
6835
  if (filtered && !nativeTextInput)
@@ -6650,6 +6945,7 @@ int gLastScrollUiFrame = -1000;
6650
6945
 
6651
6946
  bool append(const DisplayCommand &c)
6652
6947
  {
6948
+ if (c.textClipOwner >= 0) return false;
6653
6949
  if (c.type == DisplayCommandType::FillRoundedRect)
6654
6950
  return appendBox(c.fillRoundedRect.x,
6655
6951
  c.fillRoundedRect.y,
@@ -6677,7 +6973,7 @@ int gLastScrollUiFrame = -1000;
6677
6973
 
6678
6974
  RoundedRectScreenSpan span{};
6679
6975
  if (!transformedRoundedRectToScreenSpan(r, &span) ||
6680
- !roundedRectScreenSpanHasCircularRadii(span))
6976
+ !roundedRectScreenSpanFitsCanvas(span))
6681
6977
  return false;
6682
6978
  if (roundedRectScreenSpanIsCircle(span))
6683
6979
  {
@@ -7160,21 +7456,18 @@ int gLastScrollUiFrame = -1000;
7160
7456
  if (count >= kScratchDepth)
7161
7457
  return -1;
7162
7458
  chain[count++] = cursor;
7459
+ if (LayoutEngine::isViewportFixed(nodes[cursor])) break;
7163
7460
  }
7164
7461
  int pushed = 0;
7165
- for (int i = count - 1; i >= 0; i--)
7462
+ for (int i = count - 1; i > 0; i--)
7166
7463
  {
7167
7464
  const int id = chain[i];
7168
7465
  if (!nodeRecordsOverflowClip(id, nodes, nodeCount))
7169
7466
  continue;
7170
7467
  const Node &clip = nodes[id];
7171
- auto *canvas = gea::platform::display::Display::canvas();
7172
- if (canvas && canvas->width() > 0 && canvas->height() > 0 &&
7173
- clip.layout.x <= 0 && clip.layout.y <= 0 &&
7174
- clip.layout.x + clip.layout.width >= canvas->width() &&
7175
- clip.layout.y + clip.layout.height >= canvas->height())
7176
- continue;
7177
- gea::platform::display::Display::pushClip(clip.layout.x, clip.layout.y, clip.layout.width, clip.layout.height);
7468
+ int x, y, w, h;
7469
+ overflowClipBounds(clip, x, y, w, h);
7470
+ gea::platform::display::Display::pushClip(x, y, w, h);
7178
7471
  pushed++;
7179
7472
  }
7180
7473
  return pushed;
@@ -7285,6 +7578,7 @@ int gLastScrollUiFrame = -1000;
7285
7578
 
7286
7579
  static bool canUseSimpleDirtyReplay(int width, int height)
7287
7580
  {
7581
+ if (DisplayList::instance().hasTextClippedBackgrounds()) return false;
7288
7582
  #if GEA_EMBEDDED_SIMPLE_REPLAY_DEBUG
7289
7583
  static int dbgN = 0;
7290
7584
  const bool dbg = (dbgN++ % 600) == 0;
@@ -8053,7 +8347,8 @@ int gLastScrollUiFrame = -1000;
8053
8347
  // fully paint, so a replay of that region reproduces the full replay instead of
8054
8348
  // re-blending antialiased edges over their own previous blend. Display::clear()
8055
8349
  // leaves 0 on every backend (see each target's clearNoFlush), and Tree::mount()
8056
- // is the only place it runs.
8350
+ // is the only place it runs. HTML documents instead use the white UA
8351
+ // canvas recorded below their root background.
8057
8352
  static void GEA_RENDER_HOT_SRAM restoreClearBaseInRegion(int x0, int y0, int x1, int y1)
8058
8353
  {
8059
8354
  if (x0 > x1 || y0 > y1)
@@ -8061,7 +8356,10 @@ int gLastScrollUiFrame = -1000;
8061
8356
  Tree &tree = Tree::instance();
8062
8357
  if (regionHasOpaqueBase(x0, y0, x1, y1, tree.nodes(), tree.nodeCount()))
8063
8358
  return;
8064
- gea::platform::display::Display::fillRect(x0, y0, x1 - x0 + 1, y1 - y0 + 1, 0x0000);
8359
+ const int root = tree.mountedRoot();
8360
+ const auto base = root >= 0 && root < tree.nodeCount() && isDocumentCanvasRoot(tree.nodes()[root])
8361
+ ? gea::framework::graphics::pixel::nativeColor(255, 255, 255) : 0;
8362
+ gea::platform::display::Display::fillRect(x0, y0, x1 - x0 + 1, y1 - y0 + 1, base);
8065
8363
  }
8066
8364
 
8067
8365
  // Painter's-algorithm occlusion cull: the LAST node (in draw order) whose
@@ -8223,6 +8521,7 @@ int gLastScrollUiFrame = -1000;
8223
8521
 
8224
8522
  static bool replayFillRectInRegion(const DisplayCommand &c, const DisplayReplayRegion &region)
8225
8523
  {
8524
+ if (c.textClipOwner >= 0) return false;
8226
8525
  if (c.type != DisplayCommandType::FillRect || !commandOverlapsRegion(c, region))
8227
8526
  return false;
8228
8527
  int x0 = c.fill.x;
@@ -8246,6 +8545,7 @@ int gLastScrollUiFrame = -1000;
8246
8545
 
8247
8546
  static bool replayContainedRoundedRectFillInRegion(const DisplayCommand &c, int x0, int y0, int x1, int y1)
8248
8547
  {
8548
+ if (c.textClipOwner >= 0) return false;
8249
8549
  if (c.type != DisplayCommandType::FillRoundedRect)
8250
8550
  return false;
8251
8551
  if (!regionInsideRetainedBackground(c, x0, y0, x1, y1))
@@ -8703,19 +9003,119 @@ int gLastScrollUiFrame = -1000;
8703
9003
 
8704
9004
  } // namespace
8705
9005
 
9006
+ void PaintOrder::sortChildren(int *children, int count, int contextRoot)
9007
+ {
9008
+ ChildZSorter::sort(children, count, contextRoot);
9009
+ }
9010
+
9011
+ bool PaintOrder::isContext(int id)
9012
+ {
9013
+ const auto *nodes = Tree::instance().nodes();
9014
+ const auto &n = nodes[id];
9015
+ const bool item = n.parent >= 0 && !isOutOfFlowPosition(n.style.position) &&
9016
+ (nodes[n.parent].style.display == kDisplayFlex || isDisplayGrid(nodes[n.parent].style));
9017
+ return n.parent < 0 || n.style.position == kPositionFixed || ChildZSorter::effectGroup(n) ||
9018
+ (!n.style.z_index_auto && (n.style.position != 0 || item));
9019
+ }
9020
+
9021
+ bool PaintOrder::isGroup(int id)
9022
+ {
9023
+ const auto *nodes = Tree::instance().nodes();
9024
+ const auto &n = nodes[id];
9025
+ const bool item = n.parent >= 0 &&
9026
+ (nodes[n.parent].style.display == kDisplayFlex || isDisplayGrid(nodes[n.parent].style));
9027
+ return isContext(id) || n.style.position != 0 || n.style.float_side || item;
9028
+ }
9029
+
9030
+ std::vector<int> PaintOrder::collectChildren(int root, bool groupRoot, bool includePositioned)
9031
+ {
9032
+ const auto *nodes = Tree::instance().nodes();
9033
+ std::vector<int> result;
9034
+ if (!groupRoot) return result; // Ordinary boxes contribute only their own paint.
9035
+ // CSS painting follows stacking contexts rather than DOM parentage.
9036
+ // Float, flex/grid-item and positioned-auto groups keep their ordinary
9037
+ // contents together, while their positioned descendants participate in
9038
+ // the enclosing real context. Ordinary block/inline wrappers contribute
9039
+ // separate entries so block backgrounds remain below floats and text.
9040
+ auto walk = [&](auto &&self, int parent, bool withinGroup) -> void {
9041
+ std::vector<int> children;
9042
+ for (int child = nodes[parent].first_child; child >= 0; child = nodes[child].next_sibling)
9043
+ if (!isDisplayNone(nodes[child].style)) children.push_back(child);
9044
+ if (nodes[parent].style.display == kDisplayFlex || isDisplayGrid(nodes[parent].style)) {
9045
+ std::stable_sort(children.begin(), children.end(), [&](int a, int b) {
9046
+ const int oa = isOutOfFlowPosition(nodes[a].style.position) ? 0 : nodes[a].style.order;
9047
+ const int ob = isOutOfFlowPosition(nodes[b].style.position) ? 0 : nodes[b].style.order;
9048
+ return oa < ob;
9049
+ });
9050
+ }
9051
+ for (int child : children) {
9052
+ const auto &n = nodes[child];
9053
+ if (n.style.blink_interval_ms > 0 && !n.style.blink_visible) continue;
9054
+ const bool context = isContext(child), positioned = n.style.position != 0 || context;
9055
+ const bool group = isGroup(child);
9056
+ if (positioned) {
9057
+ if (includePositioned) result.push_back(child);
9058
+ if (!context && includePositioned) self(self, child, true);
9059
+ } else {
9060
+ if (!withinGroup) result.push_back(child);
9061
+ self(self, child, withinGroup || group);
9062
+ }
9063
+ }
9064
+ };
9065
+ walk(walk, root, false);
9066
+ sortChildren(result.data(), static_cast<int>(result.size()), root);
9067
+ return result;
9068
+ }
9069
+
9070
+ int PaintOrder::compareNodes(int first, int second)
9071
+ {
9072
+ // This is also the recorder's traversal, so retained draws cannot invent a
9073
+ // second hierarchy or lose document-order ties after a depth crossing.
9074
+ static int ranks[kMaxNodes];
9075
+ static uint64_t serial = ~0ull;
9076
+ static uint32_t recordSerial = 0;
9077
+ auto &tree = Tree::instance();
9078
+ if (serial != tree.refreshSerial() || recordSerial != state.displayListSerial) {
9079
+ std::fill(std::begin(ranks), std::end(ranks), -1);
9080
+ int next = 0;
9081
+ auto visit = [&](auto &&self, int node, bool groupRoot, bool positioned) -> void {
9082
+ ranks[node] = next++;
9083
+ for (int child : collectChildren(node, groupRoot, positioned))
9084
+ self(self, child, isGroup(child), isContext(child));
9085
+ };
9086
+ for (int id = 0; id < tree.nodeCount(); ++id)
9087
+ if (tree.nodes()[id].parent < 0) visit(visit, id, true, true);
9088
+ serial = tree.refreshSerial(); recordSerial = state.displayListSerial;
9089
+ }
9090
+ return ranks[first] - ranks[second];
9091
+ }
9092
+
8706
9093
  DisplayList &DisplayList::instance()
8707
9094
  {
8708
9095
  static DisplayList displayList;
8709
9096
  return displayList;
8710
9097
  }
8711
9098
 
9099
+ static int recordingTextClipOwner = -1;
9100
+
9101
+ int DisplayList::setRecordingTextClipOwner(int owner)
9102
+ {
9103
+ const int previous = recordingTextClipOwner;
9104
+ recordingTextClipOwner = owner;
9105
+ return previous;
9106
+ }
9107
+
8712
9108
  DisplayCommand *DisplayList::append()
8713
9109
  {
9110
+ if (recordingTextClipOwner >= 0) state.textClippedBackgrounds = true;
8714
9111
  if (state.appendOverride)
8715
9112
  {
8716
9113
  if (!state.appendOverrideCount || *state.appendOverrideCount >= state.appendOverrideCapacity)
8717
9114
  return nullptr;
8718
- return &state.appendOverride[(*state.appendOverrideCount)++];
9115
+ auto *command = &state.appendOverride[(*state.appendOverrideCount)++];
9116
+ command->textClipOwner = recordingTextClipOwner;
9117
+ command->textDecorationInk = false;
9118
+ return command;
8719
9119
  }
8720
9120
  if (!state.commands)
8721
9121
  {
@@ -8773,7 +9173,10 @@ int gLastScrollUiFrame = -1000;
8773
9173
  state.commandOverflow = true;
8774
9174
  return nullptr;
8775
9175
  }
8776
- return &state.commands[state.commandCount++];
9176
+ auto *command = &state.commands[state.commandCount++];
9177
+ command->textClipOwner = recordingTextClipOwner;
9178
+ command->textDecorationInk = false;
9179
+ return command;
8777
9180
  }
8778
9181
 
8779
9182
  void DisplayList::clear()
@@ -8785,6 +9188,7 @@ int gLastScrollUiFrame = -1000;
8785
9188
  state.filterBlurCacheHits = 0;
8786
9189
  state.filterBlurCacheMisses = 0;
8787
9190
  state.commandCount = 0;
9191
+ state.textClippedBackgrounds = false;
8788
9192
  state.commandOverflow = false;
8789
9193
  state.drawNodeOrderCount = 0;
8790
9194
  if (!state.ensureNodeScratchCapacity(tree.nodeCount()))
@@ -9032,6 +9436,18 @@ int gLastScrollUiFrame = -1000;
9032
9436
  return false;
9033
9437
  }
9034
9438
 
9439
+ // A flat hidden face can remove its entire paint subtree. Rebuild when
9440
+ // its visibility changes; an unchanged hidden group needs no commands,
9441
+ // and an unchanged visible group can still use the reprojection path.
9442
+ // The recorder gives even an empty visible container a valid range.
9443
+ for (int i = 0; i < state.drawNodeOrderCount; ++i)
9444
+ if (state.drawNodeOrder[i] < 0 || state.drawNodeOrder[i] >= nodeCount) return false;
9445
+ for (int id = 0; id < nodeCount; ++id) {
9446
+ if (!nodes[id].style.backface_hidden || !ViewRenderer::isTransformableBox(nodes[id]) || preserves3D(nodes[id].style)) continue;
9447
+ const bool wasRecorded = state.nodeDrawStart[id] >= 0;
9448
+ if (wasRecorded == ViewRenderer::backfaceSubtreeHidden(nodes[id])) return false;
9449
+ }
9450
+
9035
9451
  // Validate first (no partial patching): every recorded command's screen geometry
9036
9452
  // must be recomputable from a stored local rect. Anything else — clips, plain
9037
9453
  // fills/strokes, images, blur, raw text — means the structure isn't a pure
@@ -9285,86 +9701,17 @@ int gLastScrollUiFrame = -1000;
9285
9701
  state.reprojectDirtyX1 = rdX1;
9286
9702
  state.reprojectDirtyY1 = rdY1;
9287
9703
 
9288
- // CRITICAL: the full record re-sorts children back-to-front every frame
9289
- // (ChildZSorter, recordNode); the replay walks drawNodeOrder. Patching geometry
9290
- // alone leaves the draw order frozen at the arming frame's pose, so as the cube
9291
- // spins the faces composite in stale z-order — the far faces blend wrong (read
9292
- // too transparent). Re-sort drawNodeOrder by current projected depth + z-index
9293
- // here, ascending = back-to-front, matching ChildZSorter::afterByDepth. Commands
9294
- // don't move; only the per-node draw order the replay follows.
9295
- const int dn = state.drawNodeOrderCount;
9296
- static int sortDepth[kMaxNodes];
9297
- static int sortZ[kMaxNodes];
9298
- const auto nodeHasCommands = [&](int id) -> bool
9299
- {
9300
- return id >= 0 && id < nodeCount &&
9301
- state.nodeDrawStart[id] >= 0 &&
9302
- state.nodeDrawEnd[id] > state.nodeDrawStart[id];
9303
- };
9304
- const auto outermostStackingZ = [&](int id) -> int
9305
- {
9306
- int z = 0;
9307
- for (int p = id; p >= 0 && p < nodeCount; p = nodes[p].parent)
9308
- {
9309
- const int nodeZ = nodes[p].style.z_index;
9310
- if (nodeZ != 0)
9311
- z = nodeZ;
9312
- }
9313
- return z;
9314
- };
9315
- for (int i = 0; i < dn; i++)
9316
- {
9317
- int node = state.drawNodeOrder[i];
9318
- if (node < 0 || node >= nodeCount)
9319
- {
9320
- sortDepth[i] = 0;
9321
- sortZ[i] = 0;
9322
- continue;
9323
- }
9324
- // Key each node by its compositing-group ROOT: skip commandless wrappers
9325
- // only while they wrap a single child (e.g. a label <span> around text), then
9326
- // climb through contiguous painted ancestors. This keeps a face and its label
9327
- // together so stable sort draws the label after the face, while stopping at
9328
- // commandless multi-child 3D containers so sibling faces sort independently.
9329
- int root = node;
9330
- for (;;)
9331
- {
9332
- const int p = nodes[root].parent;
9333
- if (p < 0 || p >= nodeCount)
9334
- break;
9335
- if (!nodeHasCommands(p))
9336
- {
9337
- if (nodes[p].first_child != root || nodes[p].last_child != root)
9338
- break;
9339
- root = p;
9340
- continue;
9341
- }
9342
- root = p;
9343
- }
9344
- if (!nodeHasCommands(root))
9345
- root = node;
9346
- sortDepth[i] = ViewRenderer::transformedDepth(nodes[root], false);
9347
- sortZ[i] = outermostStackingZ(node);
9348
- }
9349
- for (int i = 1; i < dn; i++)
9350
- {
9704
+ // Reproduce the recorder's traversal order after transforms change. Comparing
9705
+ // the diverging branches preserves parent/child groups, paint phases,
9706
+ // stack levels, projected depth, and order-modified document ties.
9707
+ for (int i = 1; i < state.drawNodeOrderCount; ++i) {
9351
9708
  const int node = state.drawNodeOrder[i];
9352
- const int dp = sortDepth[i];
9353
- const int zi = sortZ[i];
9354
9709
  int j = i - 1;
9355
- while (j >= 0)
9356
- {
9357
- const bool pAfter = (sortZ[j] != zi) ? (sortZ[j] > zi) : (sortDepth[j] > dp);
9358
- if (!pAfter)
9359
- break;
9710
+ while (j >= 0 && PaintOrder::compareNodes(state.drawNodeOrder[j], node) > 0) {
9360
9711
  state.drawNodeOrder[j + 1] = state.drawNodeOrder[j];
9361
- sortDepth[j + 1] = sortDepth[j];
9362
- sortZ[j + 1] = sortZ[j];
9363
- j--;
9712
+ --j;
9364
9713
  }
9365
9714
  state.drawNodeOrder[j + 1] = node;
9366
- sortDepth[j + 1] = dp;
9367
- sortZ[j + 1] = zi;
9368
9715
  }
9369
9716
  return true;
9370
9717
  }
@@ -9407,6 +9754,18 @@ int gLastScrollUiFrame = -1000;
9407
9754
  }
9408
9755
  }
9409
9756
  }
9757
+ if (isDocumentCanvasRoot(nodes[id])) {
9758
+ // The UA canvas is below the root's opacity/visibility scope and is
9759
+ // independent of its layout box. Replaying it clears removed or
9760
+ // translucent propagated backgrounds across the whole viewport.
9761
+ if (DisplayCommand *cmd = append()) {
9762
+ cmd->type = DisplayCommandType::FillRect;
9763
+ cmd->bx = cmd->by = cmd->fill.x = cmd->fill.y = 0;
9764
+ cmd->bw = cmd->fill.w = cx1 + 1;
9765
+ cmd->bh = cmd->fill.h = cy1 + 1;
9766
+ cmd->fill.color = gea::framework::graphics::pixel::nativeColor(255, 255, 255);
9767
+ }
9768
+ }
9410
9769
  RenderRecorder::recordNode(id, parent_alpha, 0, 0, cx1, cy1, nullptr);
9411
9770
  }
9412
9771
 
@@ -9437,6 +9796,7 @@ int gLastScrollUiFrame = -1000;
9437
9796
 
9438
9797
  bool DisplayList::canReplayDirectDirtyRegions(int width, int height) const
9439
9798
  {
9799
+ if (hasTextClippedBackgrounds()) return false;
9440
9800
  Tree &tree = Tree::instance();
9441
9801
  Node *nodes = tree.nodes();
9442
9802
  if (state.drawNodeOrderCount <= 0)
@@ -9456,6 +9816,10 @@ int gLastScrollUiFrame = -1000;
9456
9816
  }
9457
9817
 
9458
9818
  int DisplayList::commandCount() const { return state.commandCount; }
9819
+ bool DisplayList::hasTextClippedBackgrounds() const
9820
+ {
9821
+ return state.textClippedBackgrounds;
9822
+ }
9459
9823
 
9460
9824
  void DisplayList::clearRetainedBackgroundRecolors()
9461
9825
  {
@@ -9689,21 +10053,16 @@ int gLastScrollUiFrame = -1000;
9689
10053
  int y0 = 0;
9690
10054
  int x1 = width - 1;
9691
10055
  int y1 = height - 1;
9692
- for (int a = nodes[id].parent; a >= 0 && a < nodeCount; a = nodes[a].parent)
10056
+ for (int a = LayoutEngine::isViewportFixed(nodes[id]) ? -1 : nodes[id].parent; a >= 0 && a < nodeCount; a = nodes[a].parent)
9693
10057
  {
9694
10058
  const Node &ancestor = nodes[a];
9695
10059
  if (!isViewLikeNodeType(ancestor.type) || ancestor.style.overflow == 0 || ancestor.first_child < 0)
10060
+ {
10061
+ if (LayoutEngine::isViewportFixed(ancestor)) break;
9696
10062
  continue;
9697
- const int ax1 = ancestor.layout.x + ancestor.layout.width - 1;
9698
- const int ay1 = ancestor.layout.y + ancestor.layout.height - 1;
9699
- if (ancestor.layout.x > x0)
9700
- x0 = ancestor.layout.x;
9701
- if (ancestor.layout.y > y0)
9702
- y0 = ancestor.layout.y;
9703
- if (ax1 < x1)
9704
- x1 = ax1;
9705
- if (ay1 < y1)
9706
- y1 = ay1;
10063
+ }
10064
+ ClipMath::clampToNode(ancestor, &x0, &y0, &x1, &y1);
10065
+ if (LayoutEngine::isViewportFixed(ancestor)) break;
9707
10066
  }
9708
10067
  *cx0 = x0;
9709
10068
  *cy0 = y0;
@@ -9763,7 +10122,7 @@ int gLastScrollUiFrame = -1000;
9763
10122
  continue;
9764
10123
  const Node &probe = nodes[id];
9765
10124
  const bool paintsBox =
9766
- (probe.style.has_bg && probe.style.bg_alpha > 0) ||
10125
+ (probe.style.has_bg && (probe.style.bg_alpha > 0 || styleHasBackgroundImage(probe.style))) ||
9767
10126
  rstyle(probe.style).box_shadow_alpha > 0;
9768
10127
  if (!paintsBox)
9769
10128
  continue;
@@ -10893,6 +11252,15 @@ int gLastScrollUiFrame = -1000;
10893
11252
  case DisplayCommandType::StrokeRoundedRect:
10894
11253
  {
10895
11254
  auto r = command.strokeRoundedRect;
11255
+ if (r.cssRadii) {
11256
+ TransformedRoundedRectCommand outer{}, inner{};
11257
+ cssRoundedBorderContours(r, outer, inner);
11258
+ const int samples = std::max(1, aaSamples);
11259
+ const int coverage = cssRoundedBorderCoverage(outer, inner, x, y, samples);
11260
+ *outColor = r.color;
11261
+ *outAlpha = combinedCoverageAlpha(255, coverage, samples * samples);
11262
+ return *outAlpha > 0;
11263
+ }
10896
11264
  const int maxRadius = std::min(r.w / 2, r.h / 2);
10897
11265
  r.tl = static_cast<std::int16_t>(std::clamp(static_cast<int>(r.tl), 0, maxRadius));
10898
11266
  r.tr = static_cast<std::int16_t>(std::clamp(static_cast<int>(r.tr), 0, maxRadius));
@@ -11713,6 +12081,7 @@ int gLastScrollUiFrame = -1000;
11713
12081
 
11714
12082
  bool DisplayList::recolorRetainedSolidBackground(int node, gea::framework::graphics::pixel::native_t oldColor, gea::framework::graphics::pixel::native_t newColor, int *x0, int *y0, int *x1, int *y1)
11715
12083
  {
12084
+ if (hasTextClippedBackgrounds()) return false;
11716
12085
  Tree &tree = Tree::instance();
11717
12086
  if (!state.commands || node < 0 || node >= tree.nodeCount())
11718
12087
  return false;
@@ -11884,8 +12253,8 @@ int gLastScrollUiFrame = -1000;
11884
12253
  // gate: without it a clipped-out node re-recorded a full background fill into
11885
12254
  // an empty range, which `old_len == 0 && tmp_len > 0` below (correctly) refuses
11886
12255
  // to splice — and the refusal cost the whole frame its retained display list.
11887
- const bool outsideRecordClip = nodeOutsideRecordClip(node, tree.mountedWidth(), tree.mountedHeight());
11888
- if (!outsideRecordClip &&
12256
+ const bool outsideRecordClip = !isDocumentCanvasRoot(*n) && nodeOutsideRecordClip(node, tree.mountedWidth(), tree.mountedHeight());
12257
+ if (!outsideRecordClip && n->style.visibility == 0 && !isCollapsedFlexSubtree(*n) && !ViewRenderer::backfaceSubtreeHidden(*n) &&
11889
12258
  n->style.display != 1 &&
11890
12259
  n->style.opacity != 0 &&
11891
12260
  !(n->style.blink_interval_ms > 0 && !n->style.blink_visible) &&
@@ -12018,6 +12387,10 @@ int gLastScrollUiFrame = -1000;
12018
12387
 
12019
12388
  void DisplayList::replaySimpleClippedDirtyRegion(int x0, int y0, int x1, int y1, int origin)
12020
12389
  {
12390
+ const auto &tree = Tree::instance();
12391
+ const int root = tree.mountedRoot();
12392
+ if (root >= 0 && root < tree.nodeCount() && isDocumentCanvasRoot(tree.nodes()[root]))
12393
+ DisplayCommandReplayer::restoreClearBaseInRegion(x0, y0, x1, y1);
12021
12394
  DisplayCommandReplayer::replaySimpleClippedDirtyRegion(x0, y0, x1, y1, origin);
12022
12395
  }
12023
12396