@geastack/engine 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +70 -0
  2. package/canvas.cpp +86 -30
  3. package/css/animation.h +64 -0
  4. package/css/declarative.h +6 -0
  5. package/css/engine.h +25 -1
  6. package/image_store.cpp +136 -33
  7. package/package.json +2 -2
  8. package/ui/CSS_FEATURES.md +261 -0
  9. package/ui/absolute_leaf_refresh.cpp +42 -39
  10. package/ui/core.cpp +32 -1
  11. package/ui/css_atom.cpp +6 -0
  12. package/ui/css_atom.h +2 -0
  13. package/ui/css_features.h +282 -0
  14. package/ui/display_invalidation.cpp +29 -0
  15. package/ui/flex_balance.h +73 -0
  16. package/ui/image_node.cpp +33 -28
  17. package/ui/input.cpp +88 -38
  18. package/ui/input_render.cpp +17 -13
  19. package/ui/internal.h +51 -5
  20. package/ui/layout.cpp +2601 -441
  21. package/ui/layout_snapshot.cpp +49 -6
  22. package/ui/node_features.h +18 -0
  23. package/ui/node_lifecycle.cpp +204 -71
  24. package/ui/node_lifecycle.h +1 -0
  25. package/ui/node_model.h +930 -70
  26. package/ui/node_text.h +82 -0
  27. package/ui/render.cpp +793 -352
  28. package/ui/renderer_features.h +19 -0
  29. package/ui/root_scroll_refresh.cpp +46 -24
  30. package/ui/style.cpp +4155 -1073
  31. package/ui/style.h +143 -6
  32. package/ui/style_values.h +16 -0
  33. package/ui/text.cpp +401 -106
  34. package/ui/tree_events.cpp +138 -95
  35. package/ui/tree_events.h +28 -19
  36. package/ui/tree_inspection.h +15 -0
  37. package/ui/tree_internal.h +19 -0
  38. package/ui/tree_nodes.cpp +38 -4
  39. package/ui/tree_render.cpp +119 -40
  40. package/ui/tree_scroll_mirror.cpp +26 -0
  41. package/ui/tree_state.cpp +261 -114
  42. package/ui/tree_state.h +121 -16
  43. package/ui/tree_style.cpp +609 -82
  44. package/ui/view.cpp +720 -299
  45. package/ui/virtual_keyboard.cpp +10 -3
package/ui/render.cpp CHANGED
@@ -1,4 +1,5 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
+ #include "renderer_features.h"
2
3
  #include "internal.h"
3
4
  #include "state_init.h"
4
5
  #include "canvas.h"
@@ -6,6 +7,7 @@
6
7
  #include "graphics/font.h"
7
8
  #include "memory.h"
8
9
  #include "tree_state.h"
10
+ #include "style_values.h"
9
11
  #include <pixel.h>
10
12
 
11
13
  #include <algorithm>
@@ -267,14 +269,14 @@ int gLastScrollUiFrame = -1000;
267
269
  #define GEA_EMBEDDED_RECOLOR_PIXEL_CACHE_SIZE 512
268
270
  #endif
269
271
 
270
- constexpr int kTransformedGradientLutSlots = GEA_EMBEDDED_TRANSFORMED_GRADIENT_LUT_SLOTS;
272
+ constexpr int kTransformedGradientLutSlots = (GEA_EMBEDDED_RENDERER_TRANSFORMS && GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS) ? GEA_EMBEDDED_TRANSFORMED_GRADIENT_LUT_SLOTS : 1;
271
273
  static_assert(kTransformedGradientLutSlots > 0);
272
- constexpr int kTransformedGradientLutBanks = GEA_EMBEDDED_TRANSFORMED_GRADIENT_LUT_BANKS;
274
+ constexpr int kTransformedGradientLutBanks = (GEA_EMBEDDED_RENDERER_TRANSFORMS && GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS) ? GEA_EMBEDDED_TRANSFORMED_GRADIENT_LUT_BANKS : 1;
273
275
  static_assert(kTransformedGradientLutBanks > 0);
274
276
  constexpr bool kTransformedGradientA5Mirror = GEA_EMBEDDED_TRANSFORMED_GRADIENT_A5_MIRROR != 0;
275
277
  static_assert(kTransformedGradientA5Mirror || GEA_EMBEDDED_PIE_BLEND_SPAN8_GATHER == 0,
276
278
  "GEA_EMBEDDED_PIE_BLEND_SPAN8_GATHER requires the transformed-gradient a5 mirror");
277
- constexpr int kProjectedTextCacheBanks = GEA_EMBEDDED_PROJECTED_TEXT_CACHE_BANKS;
279
+ constexpr int kProjectedTextCacheBanks = GEA_EMBEDDED_RENDERER_TRANSFORMS ? GEA_EMBEDDED_PROJECTED_TEXT_CACHE_BANKS : 1;
278
280
  static_assert(kProjectedTextCacheBanks > 0);
279
281
  constexpr int kScratchDepth = 16;
280
282
  constexpr int kFilterBlurPasses = 5;
@@ -516,6 +518,7 @@ int gLastScrollUiFrame = -1000;
516
518
  int commandCapacity = 0;
517
519
  bool commandsExternal = false;
518
520
  int commandCount = 0;
521
+ bool textClippedBackgrounds = false;
519
522
  // Sticky: an append() was dropped because the command buffer is full.
520
523
  // Reset by clear(); lets a caller detect a truncated record.
521
524
  bool commandOverflow = false;
@@ -775,6 +778,7 @@ int gLastScrollUiFrame = -1000;
775
778
  commandCapacity = 0;
776
779
  commandsExternal = false;
777
780
  commandCount = 0;
781
+ textClippedBackgrounds = false;
778
782
  appendOverride = nullptr;
779
783
  appendOverrideCount = nullptr;
780
784
  appendOverrideCapacity = 0;
@@ -1344,7 +1348,7 @@ int gLastScrollUiFrame = -1000;
1344
1348
  return node.first_child < 0 &&
1345
1349
  node.type == NodeType::View &&
1346
1350
  node.style.has_bg &&
1347
- node.style.bg_fill == 0 &&
1351
+ !styleHasBackgroundImage(node.style) && rstyle(node.style).bg_clip == 0 &&
1348
1352
  !hasAnyBorder(node.style);
1349
1353
  }
1350
1354
 
@@ -1353,10 +1357,10 @@ int gLastScrollUiFrame = -1000;
1353
1357
  const int halfMin = std::min(node.layout.width, node.layout.height) / 2;
1354
1358
  const int minRadius = std::max(1, halfMin - 1);
1355
1359
  return halfMin > 0 &&
1356
- node.style.border_radius[0] >= minRadius &&
1357
- node.style.border_radius[1] >= minRadius &&
1358
- node.style.border_radius[2] >= minRadius &&
1359
- node.style.border_radius[3] >= minRadius;
1360
+ node.style.border_radius[GEA_CSS_RADIUS_INDEX(0)] >= minRadius &&
1361
+ node.style.border_radius[GEA_CSS_RADIUS_INDEX(1)] >= minRadius &&
1362
+ node.style.border_radius[GEA_CSS_RADIUS_INDEX(2)] >= minRadius &&
1363
+ node.style.border_radius[GEA_CSS_RADIUS_INDEX(3)] >= minRadius;
1360
1364
  }
1361
1365
 
1362
1366
  bool screenToNodeRectHomography(const Node &node, double out[8])
@@ -1577,8 +1581,13 @@ int gLastScrollUiFrame = -1000;
1577
1581
  return true;
1578
1582
  }
1579
1583
 
1580
- bool roundedRectScreenSpanHasCircularRadii(const RoundedRectScreenSpan &r)
1584
+ bool roundedRectScreenSpanFitsCanvas(const RoundedRectScreenSpan &r)
1581
1585
  {
1586
+ // Both direct replay and batching call a Canvas primitive that clamps
1587
+ // radii to half the box. Larger CSS curves require the exact shader.
1588
+ const int maxRadius8 = std::min(r.w, r.h) * 4;
1589
+ if (r.tlRx8 > maxRadius8 || r.trRx8 > maxRadius8 ||
1590
+ r.brRx8 > maxRadius8 || r.blRx8 > maxRadius8) return false;
1582
1591
  return std::abs(r.tlRx8 - r.tlRy8) <= 1 &&
1583
1592
  std::abs(r.trRx8 - r.trRy8) <= 1 &&
1584
1593
  std::abs(r.brRx8 - r.brRy8) <= 1 &&
@@ -1643,7 +1652,7 @@ int gLastScrollUiFrame = -1000;
1643
1652
  RoundedRectScreenSpan span{};
1644
1653
  if (!transformedRoundedRectToScreenSpan(r, &span))
1645
1654
  return false;
1646
- if (!roundedRectScreenSpanHasCircularRadii(span))
1655
+ if (!roundedRectScreenSpanFitsCanvas(span))
1647
1656
  return false;
1648
1657
 
1649
1658
  if (drawCircleLikeScreenSpan(span, r.color))
@@ -2002,8 +2011,8 @@ int gLastScrollUiFrame = -1000;
2002
2011
  return nx * nx + ny * ny <= 1.0;
2003
2012
  }
2004
2013
 
2005
- const double radius = static_cast<double>(std::min(std::min(node.style.border_radius[0], node.style.border_radius[1]),
2006
- std::min(node.style.border_radius[2], node.style.border_radius[3])));
2014
+ const double radius = static_cast<double>(std::min(std::min(node.style.border_radius[GEA_CSS_RADIUS_INDEX(0)], node.style.border_radius[GEA_CSS_RADIUS_INDEX(1)]),
2015
+ std::min(node.style.border_radius[GEA_CSS_RADIUS_INDEX(2)], node.style.border_radius[GEA_CSS_RADIUS_INDEX(3)])));
2007
2016
  const double r = std::max(0.0, std::min(radius, std::min(w, h) * 0.5));
2008
2017
  if (r <= 0.0)
2009
2018
  return true;
@@ -2117,7 +2126,7 @@ int gLastScrollUiFrame = -1000;
2117
2126
  const double ry = h * 0.5;
2118
2127
  if (rx <= 0.0 || ry <= 0.0)
2119
2128
  return 0;
2120
- const double blur = std::max(1.0, static_cast<double>(rstyle(node.style).filter_blur_radius));
2129
+ const double blur = std::max(1.0, static_cast<double>((GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0)));
2121
2130
  const double dx = std::fabs(sx - (x + rx));
2122
2131
  const double dy = std::fabs(sy - (y + ry));
2123
2132
  const double alphaX = 1.0 - smoothStep(rx, rx + blur * 2.0, dx);
@@ -2660,14 +2669,22 @@ int gLastScrollUiFrame = -1000;
2660
2669
  }
2661
2670
  }
2662
2671
 
2663
- GEA_RENDER_HOT_SRAM void drawProjectedText(const DisplayCommand &command)
2672
+ GEA_RENDER_HOT_SRAM void drawProjectedText(const DisplayCommand &authoredCommand,
2673
+ uint8_t *coverageRow = nullptr, int coverageX = 0)
2664
2674
  {
2675
+ // Keep deferred commands pointing to authored node storage. Normalize
2676
+ // locally on replay, using the same content as layout and cache hashing.
2677
+ std::string preparedText;
2678
+ DisplayCommand command = authoredCommand;
2679
+ command.projectedText.text = TextRenderer::prepareText(command.projectedText.text,
2680
+ command.projectedText.textTransform, command.projectedText.whiteSpace, preparedText);
2681
+ command.projectedText.textTransform = 0;
2665
2682
  auto *canvas = gea::platform::display::Display::canvas();
2666
2683
  if (!canvas || !canvas->pixels() || canvas->width() <= 0 || canvas->height() <= 0)
2667
2684
  return;
2668
2685
  if (!command.projectedText.text || !command.projectedText.text[0])
2669
2686
  return;
2670
- if (command.projectedText.alpha == 0 || command.projectedText.srcW <= 0 || command.projectedText.srcH <= 0)
2687
+ if ((!coverageRow && command.projectedText.alpha == 0) || command.projectedText.srcW <= 0 || command.projectedText.srcH <= 0)
2671
2688
  return;
2672
2689
 
2673
2690
  // Back-face cull (CSS backface-visibility:hidden) — same winding test as the gradient
@@ -2704,7 +2721,7 @@ int gLastScrollUiFrame = -1000;
2704
2721
  return;
2705
2722
 
2706
2723
  const int parentAlpha = gea::platform::display::Display::alpha();
2707
- if (parentAlpha == 0)
2724
+ if (!coverageRow && parentAlpha == 0)
2708
2725
  return;
2709
2726
 
2710
2727
  const int stride = canvas->strideBytes() / static_cast<int>(sizeof(gea::framework::graphics::pixel::native_t));
@@ -2727,9 +2744,10 @@ int gLastScrollUiFrame = -1000;
2727
2744
  // thread). Falls back to the per-pixel scan if the label is implausibly large.
2728
2745
  const int srcW = command.projectedText.srcW;
2729
2746
  const int srcH = command.projectedText.srcH;
2730
- const bool useBuffer = static_cast<long long>(srcW) * static_cast<long long>(srcH) <= (1LL << 19);
2731
2747
  const std::uint8_t *cb = nullptr;
2732
2748
  int inkX0 = 0, inkY0 = 0, inkX1 = srcW - 1, inkY1 = srcH - 1;
2749
+ const bool useBuffer = static_cast<long long>(srcW) * static_cast<long long>(srcH) <= (1LL << 19);
2750
+ #if GEA_EMBEDDED_RENDERER_TRANSFORMS
2733
2751
  if (useBuffer)
2734
2752
  {
2735
2753
  // Coverage buffer is text-space (independent of the 3D projection), so it's
@@ -2885,6 +2903,21 @@ int gLastScrollUiFrame = -1000;
2885
2903
  inkY1 = hit->ink[3];
2886
2904
  }
2887
2905
  else
2906
+ #else
2907
+ // A native/dynamic instruction missed by source analysis still uses the
2908
+ // identical coverage rasterization and Q8 sampling. Only this call owns
2909
+ // the scratch; no transform-free app reserves a bank or retains a bitmap.
2910
+ std::vector<std::uint8_t> uncachedCoverage;
2911
+ if (useBuffer) {
2912
+ uncachedCoverage.resize(static_cast<std::size_t>(srcW) * srcH);
2913
+ int ink[4];
2914
+ buildProjectedTextAlpha(command, font, uncachedCoverage.data(), srcW, srcH, ink);
2915
+ if (ink[2] < ink[0]) return;
2916
+ cb = uncachedCoverage.data();
2917
+ inkX0 = ink[0]; inkY0 = ink[1]; inkX1 = ink[2]; inkY1 = ink[3];
2918
+ }
2919
+ else
2920
+ #endif
2888
2921
  {
2889
2922
  refreshPerfStatsMutable().projectedTextCacheFallbacks++;
2890
2923
  }
@@ -3067,6 +3100,10 @@ int gLastScrollUiFrame = -1000;
3067
3100
  if (coverage <= 0)
3068
3101
  continue;
3069
3102
 
3103
+ if (coverageRow) {
3104
+ coverageRow[xi - coverageX] = std::max<int>(coverageRow[xi - coverageX], coverage);
3105
+ continue;
3106
+ }
3070
3107
  int alpha = (coverage * static_cast<int>(command.projectedText.alpha) + 127) / 255;
3071
3108
  if (parentAlpha != 255) // parentAlpha==255 makes the second scale an identity
3072
3109
  alpha = (alpha * parentAlpha + 127) / 255;
@@ -3364,6 +3401,7 @@ int gLastScrollUiFrame = -1000;
3364
3401
  // so an app that never fills a transformed gradient reserves ZERO RAM for
3365
3402
  // this ~26 KB LUT bank scratch — it used to sit in .bss unconditionally for
3366
3403
  // every app. Allocated on the first transformed-gradient fill; never freed.
3404
+ #if GEA_EMBEDDED_RENDERER_TRANSFORMS && GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS
3367
3405
  using FaceLutBank = FaceLutSlot[kTransformedGradientLutSlots];
3368
3406
  static FaceLutBank *const slotsByBank = []() -> FaceLutBank *
3369
3407
  {
@@ -3393,6 +3431,16 @@ int gLastScrollUiFrame = -1000;
3393
3431
  auto &slots = slotsByBank[lutBank];
3394
3432
  int &slotRR = slotRRByBank[lutBank];
3395
3433
  std::uint8_t *const slotConstAlpha = slotConstAlphaByBank[lutBank];
3434
+
3435
+ #else
3436
+ FaceLutSlot slots[1]{};
3437
+ int slotRR = 0;
3438
+ std::uint8_t slotConstAlpha[1]{};
3439
+ #if GEA_EMBEDDED_TRANSFORMED_GRADIENT_LUT_LOCK
3440
+ volatile unsigned char slotLockByBank[1]{};
3441
+ constexpr int lutBank = 0;
3442
+ #endif
3443
+ #endif
3396
3444
  #if GEA_EMBEDDED_TRANSFORMED_GRADIENT_LUT_LOCK
3397
3445
  volatile unsigned char &slotLock = slotLockByBank[lutBank];
3398
3446
  auto lockSlots = [&slotLock]()
@@ -4515,6 +4563,64 @@ int gLastScrollUiFrame = -1000;
4515
4563
  return true;
4516
4564
  }
4517
4565
 
4566
+ void cssRoundedBorderContours(const decltype(DisplayCommand{}.strokeRoundedRect) &s,
4567
+ TransformedRoundedRectCommand &outer,
4568
+ TransformedRoundedRectCommand &inner)
4569
+ {
4570
+ outer.lx = s.x; outer.ly = s.y; outer.lw = s.w; outer.lh = s.h;
4571
+ outer.tlRx8 = s.rx8[0]; outer.tlRy8 = s.ry8[0];
4572
+ outer.trRx8 = s.rx8[1]; outer.trRy8 = s.ry8[1];
4573
+ outer.brRx8 = s.rx8[2]; outer.brRy8 = s.ry8[2];
4574
+ outer.blRx8 = s.rx8[3]; outer.blRy8 = s.ry8[3];
4575
+ inner = outer;
4576
+ inner.lx += s.lineWidth; inner.ly += s.lineWidth;
4577
+ inner.lw = std::max(0, s.w - 2 * s.lineWidth);
4578
+ inner.lh = std::max(0, s.h - 2 * s.lineWidth);
4579
+ inner.tlRx8 = std::max(0, s.rx8[0] - 8 * s.lineWidth); inner.tlRy8 = std::max(0, s.ry8[0] - 8 * s.lineWidth);
4580
+ inner.trRx8 = std::max(0, s.rx8[1] - 8 * s.lineWidth); inner.trRy8 = std::max(0, s.ry8[1] - 8 * s.lineWidth);
4581
+ inner.brRx8 = std::max(0, s.rx8[2] - 8 * s.lineWidth); inner.brRy8 = std::max(0, s.ry8[2] - 8 * s.lineWidth);
4582
+ inner.blRx8 = std::max(0, s.rx8[3] - 8 * s.lineWidth); inner.blRy8 = std::max(0, s.ry8[3] - 8 * s.lineWidth);
4583
+ }
4584
+
4585
+ int cssRoundedBorderCoverage(const TransformedRoundedRectCommand &outer,
4586
+ const TransformedRoundedRectCommand &inner,
4587
+ int x, int y, int samples)
4588
+ {
4589
+ int coverage = 0;
4590
+ for (int iy = 0; iy < samples; ++iy) for (int ix = 0; ix < samples; ++ix) {
4591
+ const float px = x + (ix + 0.5f) / samples, py = y + (iy + 0.5f) / samples;
4592
+ coverage += roundedRectContainsFast(outer, px, py) && !roundedRectContainsFast(inner, px, py);
4593
+ }
4594
+ return coverage;
4595
+ }
4596
+
4597
+ void drawCssRoundedBorder(const DisplayCommand &command)
4598
+ {
4599
+ const auto &s = command.strokeRoundedRect;
4600
+ auto *canvas = gea::platform::display::Display::canvas();
4601
+ if (!canvas || !canvas->pixels() || s.w <= 0 || s.h <= 0 || s.lineWidth <= 0) return;
4602
+ TransformedRoundedRectCommand outer{}, inner{};
4603
+ cssRoundedBorderContours(s, outer, inner);
4604
+ int x0, y0, x1, y1;
4605
+ gea::platform::display::Display::clip(&x0, &y0, &x1, &y1);
4606
+ x0 = std::max({x0, 0, int(s.x)}); y0 = std::max({y0, 0, int(s.y)});
4607
+ x1 = std::min({x1, canvas->width() - 1, s.x + s.w - 1});
4608
+ y1 = std::min({y1, canvas->height() - 1, s.y + s.h - 1});
4609
+ if (x1 < x0 || y1 < y0) return;
4610
+ const int samples = std::max(1, gea::framework::graphics::Canvas::antialiasSamples());
4611
+ const int alpha = gea::platform::display::Display::alpha();
4612
+ if (!alpha) return;
4613
+ const int stride = canvas->strideBytes() / sizeof(gea::framework::graphics::pixel::native_t);
4614
+ for (int y = y0; y <= y1; ++y) {
4615
+ auto *row = canvas->pixels() + canvas->rowToPhysical(y) * stride;
4616
+ for (int x = x0; x <= x1; ++x) {
4617
+ const int coverage = cssRoundedBorderCoverage(outer, inner, x, y, samples);
4618
+ if (coverage) paintCoveragePixel(row[x], s.color, alpha, coverage, samples * samples);
4619
+ }
4620
+ }
4621
+ canvas->markDirty(x0, y0, x1, y1);
4622
+ }
4623
+
4518
4624
  void drawTransformedRoundedRect(const DisplayCommand &command)
4519
4625
  {
4520
4626
  auto *canvas = gea::platform::display::Display::canvas();
@@ -4631,28 +4737,20 @@ int gLastScrollUiFrame = -1000;
4631
4737
  bool hasRetainedTransformState(const Node &node)
4632
4738
  {
4633
4739
  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;
4740
+ const bool currentTransformable = ViewRenderer::isTransformableBox(node);
4741
+ return (currentTransformable && hasIndividualLinearTransform(rs)) ||
4742
+ (node.render.previous_transformable_box && hadIndividualLinearTransform(node.render)) ||
4743
+ (currentTransformable && (rs.transform_rotate != 0 || rs.transform_rotate_x != 0 ||
4744
+ rs.transform_rotate_y != 0 || composedTranslateX(rs) != 0 || composedTranslateY(rs) != 0 ||
4745
+ composedTranslateZ(rs) != 0 || composedTranslateXPercent(rs) != 0 ||
4746
+ composedTranslateYPercent(rs) != 0 || rs.transform_scale_x != 1000 ||
4747
+ rs.transform_scale_y != 1000 || rs.transform_scale_z != 1000 || rs.perspective > 0)) ||
4748
+ (node.render.previous_transformable_box && (node.render.previous_transform_rotate != 0 ||
4749
+ node.render.previous_transform_rotate_x != 0 || node.render.previous_transform_rotate_y != 0 ||
4750
+ node.render.previous_transform_translate_x != 0 || node.render.previous_transform_translate_y != 0 ||
4751
+ node.render.previous_transform_translate_z != 0 || node.render.previous_transform_translate_x_percent != 0 ||
4752
+ node.render.previous_transform_translate_y_percent != 0 || node.render.previous_transform_scale_x != 1000 ||
4753
+ node.render.previous_transform_scale_y != 1000 || node.render.previous_transform_scale_z != 1000 || node.render.previous_perspective > 0));
4656
4754
  }
4657
4755
 
4658
4756
  struct ClipMath
@@ -4671,10 +4769,9 @@ int gLastScrollUiFrame = -1000;
4671
4769
 
4672
4770
  static void clampToNode(const Node &node, int *cx0, int *cy0, int *cx1, int *cy1)
4673
4771
  {
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;
4772
+ int nx0, ny0, w, h;
4773
+ overflowClipBounds(node, nx0, ny0, w, h);
4774
+ int nx1 = nx0 + w - 1, ny1 = ny0 + h - 1;
4678
4775
  if (*cx0 < nx0)
4679
4776
  *cx0 = nx0;
4680
4777
  if (*cy0 < ny0)
@@ -4688,71 +4785,129 @@ int gLastScrollUiFrame = -1000;
4688
4785
 
4689
4786
  struct ChildZSorter
4690
4787
  {
4788
+ struct Key { int phase, level; };
4789
+ static bool effectGroup(const Node &node)
4790
+ {
4791
+ const auto &s = node.style;
4792
+ const auto &rs = rstyle(s);
4793
+ const bool transformable = ViewRenderer::isTransformableBox(node);
4794
+ return s.opacity < 255 || s.mask_right_fade_width > 0 || rs.filter_present || rs.filter_blur_radius ||
4795
+ (transformable && (rs.translate_present || rs.transform_present || rs.rotate_present || rs.scale_present ||
4796
+ rs.transform_preserve_3d || rs.perspective || rs.transform_rotate || rs.transform_rotate_x || rs.transform_rotate_y ||
4797
+ composedTranslateX(rs) || composedTranslateY(rs) || composedTranslateZ(rs) ||
4798
+ composedTranslateXPercent(rs) || composedTranslateYPercent(rs) ||
4799
+ rs.transform_scale_x != 1000 || rs.transform_scale_y != 1000 || rs.transform_scale_z != 1000)) || (rs.containment & (4 | 16));
4800
+ }
4801
+ static Key key(int id)
4802
+ {
4803
+ const Node *nodes = Tree::instance().nodes();
4804
+ const Node &n = nodes[id];
4805
+ const auto &s = n.style;
4806
+ const bool positioned = s.position != 0;
4807
+ const bool item = n.parent >= 0 && !isOutOfFlowPosition(s.position) &&
4808
+ (nodes[n.parent].style.display == kDisplayFlex || isDisplayGrid(nodes[n.parent].style));
4809
+ const int level = !s.z_index_auto && (positioned || item) ? s.z_index : 0;
4810
+ if (level < 0) return {0, level};
4811
+ if (level > 0) return {5, level};
4812
+ if (positioned || effectGroup(n) || (item && !s.z_index_auto)) return {4, 0};
4813
+ if (item) return {3, 0};
4814
+ if (s.float_side) return {2, 0};
4815
+ return {LayoutEngine::isInlineLevelNode(n) ? 3 : 1, 0};
4816
+ }
4817
+
4818
+ static int compareKeys(int first, int second)
4819
+ {
4820
+ const auto a = key(first), b = key(second);
4821
+ return a.phase != b.phase ? a.phase - b.phase : a.level - b.level;
4822
+ }
4823
+
4824
+ static bool participatesIn3DContext(int node, int contextRoot)
4825
+ {
4826
+ const Node *nodes = Tree::instance().nodes();
4827
+ if (contextRoot < 0 || !ViewRenderer::isTransformableBox(nodes[contextRoot]) || !preserves3D(nodes[contextRoot].style))
4828
+ return false;
4829
+ // A 3D rendering context follows DOM ancestry, not containing-block
4830
+ // ancestry. Every parent between a participant and the context root
4831
+ // must preserve 3D; a flat parent composites its descendants as a
4832
+ // single plane and prevents abs/fixed descendants from being hoisted
4833
+ // into this context.
4834
+ for (int parent = nodes[node].parent; parent >= 0 && parent != contextRoot; parent = nodes[parent].parent)
4835
+ if (!ViewRenderer::isTransformableBox(nodes[parent]) || !preserves3D(nodes[parent].style))
4836
+ return false;
4837
+ for (int parent = nodes[node].parent; parent >= 0; parent = nodes[parent].parent)
4838
+ if (parent == contextRoot)
4839
+ return true;
4840
+ return false;
4841
+ }
4842
+
4691
4843
  static int depth(int node)
4692
4844
  {
4693
4845
  Node *nodes = Tree::instance().nodes();
4694
4846
  return ViewRenderer::transformedDepth(nodes[node], false);
4695
4847
  }
4696
4848
 
4697
- static int subtreeDepth(int node)
4849
+ static int subtreeDepth(int node, int contextRoot)
4698
4850
  {
4699
4851
  Tree &tree = Tree::instance();
4700
4852
  Node *nodes = tree.nodes();
4701
- if (node < 0 || node >= tree.nodeCount())
4853
+ if (node < 0 || node >= tree.nodeCount() || !participatesIn3DContext(node, contextRoot))
4702
4854
  return 0;
4703
4855
  // Memoize per node per refresh. The child z-sort runs at every tree level,
4704
4856
  // so an ancestor's sort recurses a subtree that a descendant's sort then
4705
4857
  // re-walks. Caching collapses those repeated O(subtree) walks to O(1).
4706
4858
  static int memo[kMaxNodes];
4707
4859
  static uint64_t memoSerial[kMaxNodes];
4860
+ static int memoContextRoot[kMaxNodes];
4708
4861
  const uint64_t serial = tree.refreshSerial();
4709
- if (node < kMaxNodes && memoSerial[node] == serial)
4862
+ if (node < kMaxNodes && memoSerial[node] == serial && memoContextRoot[node] == contextRoot)
4710
4863
  return memo[node];
4711
4864
  int maxDepth = depth(node);
4712
4865
  for (int child = nodes[node].first_child; child >= 0 && child < tree.nodeCount(); child = nodes[child].next_sibling)
4713
4866
  {
4714
- maxDepth = std::max(maxDepth, subtreeDepth(child));
4867
+ if (participatesIn3DContext(child, contextRoot))
4868
+ maxDepth = std::max(maxDepth, subtreeDepth(child, contextRoot));
4715
4869
  }
4716
4870
  if (node < kMaxNodes)
4717
4871
  {
4718
4872
  memo[node] = maxDepth;
4719
4873
  memoSerial[node] = serial;
4874
+ memoContextRoot[node] = contextRoot;
4720
4875
  }
4721
4876
  return maxDepth;
4722
4877
  }
4723
4878
 
4724
4879
  static bool afterByDepth(int existing, int existingDepth, int candidate, int candidateDepth)
4725
4880
  {
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;
4881
+ const int order = compareKeys(existing, candidate);
4882
+ if (order) return order > 0;
4729
4883
  return existingDepth > candidateDepth;
4730
4884
  }
4731
4885
 
4732
- static void sort(int *children, int child_count)
4886
+ static void sort(int *children, int child_count, int contextRoot)
4733
4887
  {
4734
4888
  if (child_count < 2)
4735
4889
  return;
4736
4890
  // 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
4891
+ // sort entirely when every sibling has the same paint phase and stack
4892
+ // level and no transform/3D depth is active — the common 2D case
4739
4893
  // (typography, breakout). subtreeDepth() recurses the whole subtree and
4740
4894
  // each node costs 5 double-precision projections, so this avoids it
4741
4895
  // entirely for 2D trees.
4742
- const bool transforms = ViewRenderer::anyTransformActive();
4743
- Node *nodes = Tree::instance().nodes();
4896
+ const bool transforms = contextRoot >= 0 && ViewRenderer::isTransformableBox(Tree::instance().nodes()[contextRoot]) &&
4897
+ preserves3D(Tree::instance().nodes()[contextRoot].style) &&
4898
+ ViewRenderer::anyTransformActive();
4744
4899
  if (!transforms)
4745
4900
  {
4746
- bool anyZ = false;
4747
- for (int i = 0; i < child_count; i++)
4901
+ bool mixed = false;
4902
+ for (int i = 1; i < child_count; i++)
4748
4903
  {
4749
- if (nodes[children[i]].style.z_index != 0)
4904
+ if (compareKeys(children[0], children[i]))
4750
4905
  {
4751
- anyZ = true;
4906
+ mixed = true;
4752
4907
  break;
4753
4908
  }
4754
4909
  }
4755
- if (!anyZ)
4910
+ if (!mixed)
4756
4911
  return;
4757
4912
  }
4758
4913
  // Precompute each child's painter's-depth key ONCE. Recomputing
@@ -4761,12 +4916,12 @@ int gLastScrollUiFrame = -1000;
4761
4916
  // css-animation-showcase: 21 children → ~440 comparisons, each doing a
4762
4917
  // recursive subtree walk of 5 projections/node). Computing it up front is
4763
4918
  // 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];
4919
+ // active transform; without one it's constant, so skip the walk. A
4920
+ // context may include hoisted descendants beyond the immediate-child
4921
+ // scratch limit, so size this buffer to its actual participant count.
4922
+ std::vector<int> depthKey(child_count);
4768
4923
  for (int i = 0; i < child_count; i++)
4769
- depthKey[i] = transforms ? subtreeDepth(children[i]) : 0;
4924
+ depthKey[i] = transforms ? subtreeDepth(children[i], contextRoot) : 0;
4770
4925
  for (int i = 1; i < child_count; i++)
4771
4926
  {
4772
4927
  const int childKey = children[i];
@@ -4794,10 +4949,15 @@ int gLastScrollUiFrame = -1000;
4794
4949
  c->by += dy;
4795
4950
  switch (c->type)
4796
4951
  {
4797
- case DisplayCommandType::PushClip:
4798
- c->clip.x += dx;
4799
- c->clip.y += dy;
4952
+ case DisplayCommandType::PushClip: {
4953
+ const int id = c->clip.nodeId;
4954
+ const Node *owner = id >= 0 && id < Tree::instance().nodeCount() ? &Tree::instance().nodes()[id] : nullptr;
4955
+ if (!owner || overflowX(owner->style)) c->clip.x += dx;
4956
+ else c->bx -= dx;
4957
+ if (!owner || overflowY(owner->style)) c->clip.y += dy;
4958
+ else c->by -= dy;
4800
4959
  break;
4960
+ }
4801
4961
  case DisplayCommandType::FillRect:
4802
4962
  c->fill.x += dx;
4803
4963
  c->fill.y += dy;
@@ -4936,8 +5096,6 @@ int gLastScrollUiFrame = -1000;
4936
5096
  if (rowX0 > rowX1)
4937
5097
  continue;
4938
5098
  int spanW = rowX1 - rowX0 + 1;
4939
- if (spanW > gea::platform::display::kWidth)
4940
- spanW = gea::platform::display::kWidth;
4941
5099
  const long long off = static_cast<long long>(y) * scrW + rowX0;
4942
5100
  gea::platform::display::Display::blitImage(color + off, alpha + off, spanW, 1, rowX0, y);
4943
5101
  }
@@ -5155,6 +5313,29 @@ int gLastScrollUiFrame = -1000;
5155
5313
  gea::framework::graphics::pixel::native_t color;
5156
5314
  };
5157
5315
 
5316
+ static DitherStop ditherStop(int r, int g, int b, uint8_t alpha)
5317
+ {
5318
+ DitherStop stop{};
5319
+ auto split = [](int value, int levels, uint8_t &q, uint8_t &rem) {
5320
+ const int scaled = std::clamp(value, 0, 255) * levels;
5321
+ q = static_cast<uint8_t>(scaled / 255);
5322
+ rem = static_cast<uint8_t>(scaled % 255);
5323
+ };
5324
+ split(r, 31, stop.qR, stop.remR);
5325
+ split(g, 63, stop.qG, stop.remG);
5326
+ split(b, 31, stop.qB, stop.remB);
5327
+ stop.color = gea::framework::graphics::pixel::packNative8(r, g, b);
5328
+ stop.alpha = alpha;
5329
+ return stop;
5330
+ }
5331
+
5332
+ static DitherStop ditherAt(const DisplayCommand &c, int p)
5333
+ {
5334
+ int r, g, b;
5335
+ rgb888At(c, p, &r, &g, &b);
5336
+ return ditherStop(r, g, b, alphaAt(c, p));
5337
+ }
5338
+
5158
5339
  // Un-dithered RGB888 color at a gradient position. Carries every integer
5159
5340
  // divide in the gradient (mid-stop remap, RGB565->888 unpack, channel
5160
5341
  // interpolation). The S3 has no fast integer divide, so calling this per
@@ -5327,6 +5508,7 @@ int gLastScrollUiFrame = -1000;
5327
5508
  // flush's expected per-row dirty pattern and hung the panel TX. Going through
5328
5509
  // blitImage() keeps the cache provably equivalent to the stable path.) Gated on
5329
5510
  // opaque + large + stable so animated/small/alpha gradients render normally.
5511
+ #if GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS
5330
5512
  auto *bgCanvas = gea::platform::display::Display::canvas();
5331
5513
  int bgCap = 0;
5332
5514
  gea::framework::graphics::pixel::native_t *bgBuf = gea_bg_cache(&bgCap);
@@ -5340,12 +5522,14 @@ int gLastScrollUiFrame = -1000;
5340
5522
  static_cast<long long>(scrW) * scrH;
5341
5523
  const bool bgCacheable = bgBuf && bgCanvas && bgCanvas->pixels() && bgOpaque && bgLarge &&
5342
5524
  scrW > 0 && scrH > 0 && bgCap >= scrW * scrH;
5343
- const std::uint16_t bgKeyNow[10] = {
5525
+ const std::uint16_t bgKeyNow[14] = {
5344
5526
  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
5527
  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];
5528
+ c.gradient.midAlpha, c.gradient.toAlpha, c.gradient.hasMid,
5529
+ static_cast<std::uint16_t>(c.gradient.x), static_cast<std::uint16_t>(c.gradient.y),
5530
+ static_cast<std::uint16_t>(c.gradient.w), static_cast<std::uint16_t>(c.gradient.h)};
5531
+ static std::uint16_t bgKey[14];
5532
+ static std::uint16_t bgLastKey[14];
5349
5533
  static int bgStable = 0;
5350
5534
  static bool bgValid = false;
5351
5535
  static int bgKW = 0, bgKH = 0;
@@ -5359,8 +5543,6 @@ int gLastScrollUiFrame = -1000;
5359
5543
  if (rowX0 > rowX1)
5360
5544
  continue;
5361
5545
  int spanW = rowX1 - rowX0 + 1;
5362
- if (spanW > gea::platform::display::kWidth)
5363
- spanW = gea::platform::display::kWidth;
5364
5546
  gea::platform::display::Display::blitImage(
5365
5547
  bgBuf + static_cast<long long>(y) * scrW + rowX0, nullptr, spanW, 1, rowX0, y);
5366
5548
  }
@@ -5390,6 +5572,8 @@ int gLastScrollUiFrame = -1000;
5390
5572
  if (transCacheable && transCache.blit(transKey, 14, x0, y0, x1, y1, scrW, scrH, transSpan))
5391
5573
  return;
5392
5574
 
5575
+ #endif
5576
+
5393
5577
  const double angleRadians = (static_cast<double>(c.gradient.angle) * kPi) / 1800.0;
5394
5578
  double dx = std::sin(angleRadians);
5395
5579
  double dy = -std::cos(angleRadians);
@@ -5456,6 +5640,7 @@ int gLastScrollUiFrame = -1000;
5456
5640
  #ifndef GEA_EMBEDDED_LAZY_GRADIENT_LUTS
5457
5641
  #define GEA_EMBEDDED_LAZY_GRADIENT_LUTS 0
5458
5642
  #endif
5643
+ #if GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS
5459
5644
  struct LutSlot
5460
5645
  {
5461
5646
  DitherStop lut[1001];
@@ -5511,32 +5696,7 @@ int gLastScrollUiFrame = -1000;
5511
5696
  {
5512
5697
  for (int p = 0; p <= 1000; p++)
5513
5698
  {
5514
- int r, g, b;
5515
- rgb888At(c, p, &r, &g, &b);
5516
- auto split = [](int value, int levels, uint8_t *q, uint8_t *rem)
5517
- {
5518
- if (value <= 0)
5519
- {
5520
- *q = 0;
5521
- *rem = 0;
5522
- return;
5523
- }
5524
- if (value >= 255)
5525
- {
5526
- *q = static_cast<uint8_t>(levels);
5527
- *rem = 0;
5528
- return;
5529
- }
5530
- const int scaled = value * levels;
5531
- const int qq = scaled / 255;
5532
- *q = static_cast<uint8_t>(qq);
5533
- *rem = static_cast<uint8_t>(scaled - qq * 255);
5534
- };
5535
- split(r, 31, &lut[p].qR, &lut[p].remR);
5536
- split(g, 63, &lut[p].qG, &lut[p].remG);
5537
- split(b, 31, &lut[p].qB, &lut[p].remB);
5538
- lut[p].color = gea::framework::graphics::pixel::packNative8(r, g, b);
5539
- lut[p].alpha = alphaAt(c, p);
5699
+ lut[p] = ditherAt(c, p);
5540
5700
  }
5541
5701
  lutKey[0] = c.gradient.fromColor;
5542
5702
  lutKey[1] = c.gradient.midColor;
@@ -5548,6 +5708,13 @@ int gLastScrollUiFrame = -1000;
5548
5708
  lutKeyA[2] = c.gradient.toAlpha;
5549
5709
  lutKeyA[3] = c.gradient.hasMid;
5550
5710
  }
5711
+ #else
5712
+ struct UncachedLut {
5713
+ const DisplayCommand &command;
5714
+ DitherStop operator[](int p) const { return ditherAt(command, p); }
5715
+ } lut{c};
5716
+ #endif
5717
+
5551
5718
  static constexpr uint8_t bayer4[4][4] = {
5552
5719
  {0, 8, 2, 10},
5553
5720
  {12, 4, 14, 6},
@@ -5572,8 +5739,14 @@ int gLastScrollUiFrame = -1000;
5572
5739
  const float stepY = static_cast<float>(dy);
5573
5740
  const float fMinProjection = static_cast<float>(minProjection);
5574
5741
  const float invSpanPermille = 1000.0f / static_cast<float>(projectionSpan);
5575
- static gea::framework::graphics::pixel::native_t rowColor[gea::platform::display::kWidth];
5576
- static uint8_t rowAlpha[gea::platform::display::kWidth];
5742
+ #if GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS
5743
+ static
5744
+ #endif
5745
+ gea::framework::graphics::pixel::native_t rowColor[gea::platform::display::kWidth];
5746
+ #if GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS
5747
+ static
5748
+ #endif
5749
+ uint8_t rowAlpha[gea::platform::display::kWidth];
5577
5750
  for (int y = y0; y <= y1; y++)
5578
5751
  {
5579
5752
  int rowX0 = x0;
@@ -5582,64 +5755,67 @@ int gLastScrollUiFrame = -1000;
5582
5755
  if (rowX0 > rowX1)
5583
5756
  continue;
5584
5757
  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)
5758
+ // Keep the device-sized buffer, but cover wider simulator viewports in chunks.
5759
+ for (int chunkX = rowX0; chunkX <= rowX1; chunkX += gea::platform::display::kWidth)
5591
5760
  {
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++)
5761
+ const int spanW = std::min(gea::platform::display::kWidth, rowX1 - chunkX + 1);
5762
+ float projection = (static_cast<float>(chunkX) + 0.5f) * stepX + (static_cast<float>(y) + 0.5f) * stepY;
5763
+ bool rowOpaque = true;
5764
+ if (stepX == 0.0f)
5602
5765
  {
5766
+ // Vertical gradient (the common `to bottom` case): the projection —
5767
+ // hence the permille bucket, LUT entry, and alpha — is constant
5768
+ // across the row; only the 4-periodic bayer COLUMN pattern varies.
5769
+ // Compute the 4-pixel pattern once with exactly the per-pixel math
5770
+ // (bit-identical) and fill the row at store speed instead of paying
5771
+ // the DDA + clamp + LUT + dither chain per pixel.
5772
+ const int permille = clampPermille(static_cast<int>((projection - fMinProjection) * invSpanPermille + 0.5f));
5773
+ const DitherStop &e = lut[permille];
5774
+ gea::framework::graphics::pixel::native_t pattern[4];
5775
+ for (int k = 0; k < 4; k++)
5776
+ {
5603
5777
  #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);
5778
+ const int threshold = bayerRow[k] * 16 + 8;
5779
+ const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
5780
+ const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
5781
+ const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
5782
+ pattern[k] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
5609
5783
  #else
5610
- pattern[k] = e.color;
5784
+ pattern[k] = e.color;
5611
5785
  #endif
5786
+ }
5787
+ for (int i = 0; i < spanW; i++)
5788
+ rowColor[i] = pattern[(chunkX + i) & 3];
5789
+ std::memset(rowAlpha, e.alpha, static_cast<std::size_t>(spanW));
5790
+ rowOpaque = e.alpha == 255;
5612
5791
  }
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];
5792
+ else
5793
+ for (int i = 0; i < spanW; i++, projection += stepX)
5794
+ {
5795
+ const int permille = clampPermille(static_cast<int>((projection - fMinProjection) * invSpanPermille + 0.5f));
5796
+ const DitherStop &e = lut[permille];
5623
5797
  #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);
5798
+ const int threshold = bayerRow[(chunkX + i) & 3] * 16 + 8;
5799
+ const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
5800
+ const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
5801
+ const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
5802
+ rowColor[i] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
5629
5803
  #else
5630
- rowColor[i] = e.color; // full colour: no dither (8888 doesn't band)
5804
+ rowColor[i] = e.color; // full colour: no dither (8888 doesn't band)
5631
5805
  #endif
5632
- rowAlpha[i] = e.alpha;
5633
- if (e.alpha != 255)
5634
- rowOpaque = false;
5806
+ rowAlpha[i] = e.alpha;
5807
+ if (e.alpha != 255)
5808
+ rowOpaque = false;
5809
+ }
5810
+ // alpha=nullptr -> blitImage's opaque memcpy (baseAlpha==255) or whole-row
5811
+ // global blend (baseAlpha<255); the rowAlpha mask -> per-pixel combine
5812
+ // with baseAlpha. Equivalent to the old fillRun(combineAlpha(baseAlpha,
5813
+ // localAlpha)). baseAlpha is read from Display::alpha() inside blitImage.
5814
+ gea::platform::display::Display::blitImage(rowColor, rowOpaque ? nullptr : rowAlpha, spanW, 1, chunkX, y);
5635
5815
  }
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
5816
  }
5642
5817
 
5818
+ #if GEA_EMBEDDED_RENDERER_LINEAR_GRADIENTS
5643
5819
  // Once the gradient has been identical for 2 frames, render it in full into
5644
5820
  // the cache buffer (reusing the LUT/projection just computed); subsequent
5645
5821
  // frames hit the blit fast-path above. One-time ~full-screen render; writes
@@ -5763,6 +5939,7 @@ int gLastScrollUiFrame = -1000;
5763
5939
  }
5764
5940
  });
5765
5941
  }
5942
+ #endif
5766
5943
  }
5767
5944
  };
5768
5945
 
@@ -5857,6 +6034,16 @@ int gLastScrollUiFrame = -1000;
5857
6034
  localPermille(c, permille));
5858
6035
  }
5859
6036
 
6037
+ static LinearGradientDrawer::DitherStop ditherAt(const DisplayCommand &c, int p)
6038
+ {
6039
+ const int lp = localPermille(c, p);
6040
+ int r, g, b;
6041
+ LinearGradientDrawer::interpolatePremultipliedRgb888(c.radialGradient.fromColor, c.radialGradient.fromAlpha,
6042
+ c.radialGradient.toColor, c.radialGradient.toAlpha, lp, &r, &g, &b);
6043
+ return LinearGradientDrawer::ditherStop(r, g, b,
6044
+ LinearGradientDrawer::interpolateAlpha(c.radialGradient.fromAlpha, c.radialGradient.toAlpha, lp));
6045
+ }
6046
+
5860
6047
  static void replay(const DisplayCommand &c)
5861
6048
  {
5862
6049
  int cx0, cy0, cx1, cy1;
@@ -5883,6 +6070,7 @@ int gLastScrollUiFrame = -1000;
5883
6070
  // warm-glow radial is static — fixed colors + box, identical across themes
5884
6071
  // (only the base linear layer changes) — so cache it once and blit instead of
5885
6072
  // recomputing sqrt + dither per pixel. Gated to reasonably large boxes.
6073
+ #if GEA_EMBEDDED_RENDERER_RADIAL_GRADIENTS
5886
6074
  auto *rcanvas = gea::platform::display::Display::canvas();
5887
6075
  const int scrW = rcanvas ? rcanvas->width() : 0;
5888
6076
  const int scrH = rcanvas ? rcanvas->height() : 0;
@@ -5904,9 +6092,12 @@ int gLastScrollUiFrame = -1000;
5904
6092
  if (cacheable && cache.blit(keyNow, 17, x0, y0, x1, y1, scrW, scrH, span))
5905
6093
  return;
5906
6094
 
6095
+ #endif
6096
+
5907
6097
  // Divide-free dither LUT indexed by local permille (0..1000), cached across
5908
6098
  // calls keyed by the color stops (mirrors LinearGradientDrawer). The per-pixel
5909
6099
  // dither becomes a table read + 3 threshold compares — no divides.
6100
+ #if GEA_EMBEDDED_RENDERER_RADIAL_GRADIENTS
5910
6101
  #if GEA_EMBEDDED_LAZY_GRADIENT_LUTS
5911
6102
  static auto lutStorage = std::make_unique<std::array<LinearGradientDrawer::DitherStop, 1001>>();
5912
6103
  auto &lut = *lutStorage;
@@ -5926,34 +6117,7 @@ int gLastScrollUiFrame = -1000;
5926
6117
  // clamps to 1000, which localPermille also maps to 1000).
5927
6118
  for (int p = 0; p <= 1000; ++p)
5928
6119
  {
5929
- const int lp = localPermille(c, p);
5930
- int r = 0, g = 0, b = 0;
5931
- LinearGradientDrawer::interpolatePremultipliedRgb888(c.radialGradient.fromColor, c.radialGradient.fromAlpha,
5932
- c.radialGradient.toColor, c.radialGradient.toAlpha, lp, &r, &g, &b);
5933
- auto split = [](int value, int levels, uint8_t *q, uint8_t *rem)
5934
- {
5935
- if (value <= 0)
5936
- {
5937
- *q = 0;
5938
- *rem = 0;
5939
- return;
5940
- }
5941
- if (value >= 255)
5942
- {
5943
- *q = static_cast<uint8_t>(levels);
5944
- *rem = 0;
5945
- return;
5946
- }
5947
- const int scaled = value * levels;
5948
- const int qq = scaled / 255;
5949
- *q = static_cast<uint8_t>(qq);
5950
- *rem = static_cast<uint8_t>(scaled - qq * 255);
5951
- };
5952
- split(r, 31, &lut[p].qR, &lut[p].remR);
5953
- split(g, 63, &lut[p].qG, &lut[p].remG);
5954
- split(b, 31, &lut[p].qB, &lut[p].remB);
5955
- lut[p].color = gea::framework::graphics::pixel::packNative8(r, g, b);
5956
- lut[p].alpha = LinearGradientDrawer::interpolateAlpha(c.radialGradient.fromAlpha, c.radialGradient.toAlpha, lp);
6120
+ lut[p] = ditherAt(c, p);
5957
6121
  }
5958
6122
  lutKeyFrom = c.radialGradient.fromColor;
5959
6123
  lutKeyTo = c.radialGradient.toColor;
@@ -5961,6 +6125,13 @@ int gLastScrollUiFrame = -1000;
5961
6125
  lutKeyToA = c.radialGradient.toAlpha;
5962
6126
  lutKeyStop = c.radialGradient.stopPermille;
5963
6127
  }
6128
+ #else
6129
+ struct UncachedLut {
6130
+ const DisplayCommand &command;
6131
+ LinearGradientDrawer::DitherStop operator[](int p) const { return ditherAt(command, p); }
6132
+ } lut{c};
6133
+ #endif
6134
+
5964
6135
  static constexpr uint8_t bayer4[4][4] = {
5965
6136
  {0, 8, 2, 10},
5966
6137
  {12, 4, 14, 6},
@@ -5972,8 +6143,14 @@ int gLastScrollUiFrame = -1000;
5972
6143
  // row into ~1px writes (each paying a clip + framebuffer read-back + dirty-list
5973
6144
  // scan) — the radial's real cost, just like the linear drawer. blitImage clips
5974
6145
  // and marks dirty once per row. (baseAlpha is read from Display::alpha() inside.)
5975
- static gea::framework::graphics::pixel::native_t rowColor[gea::platform::display::kWidth];
5976
- static uint8_t rowAlpha[gea::platform::display::kWidth];
6146
+ #if GEA_EMBEDDED_RENDERER_RADIAL_GRADIENTS
6147
+ static
6148
+ #endif
6149
+ gea::framework::graphics::pixel::native_t rowColor[gea::platform::display::kWidth];
6150
+ #if GEA_EMBEDDED_RENDERER_RADIAL_GRADIENTS
6151
+ static
6152
+ #endif
6153
+ uint8_t rowAlpha[gea::platform::display::kWidth];
5977
6154
  // Mirror gate: a pixel xx and its mirror (mirrorSum - xx) have dx values
5978
6155
  // that are exact float negations of each other when 2*centerX lands on an
5979
6156
  // integer grid (e.g. the shell glow's `at 50%`), so dx*dx — and therefore
@@ -5983,7 +6160,10 @@ int gLastScrollUiFrame = -1000;
5983
6160
  const float mirrorSumF = 2.0f * centerX - 1.0f;
5984
6161
  const int mirrorSum = static_cast<int>(mirrorSumF);
5985
6162
  const bool mirrorable = mirrorSumF == static_cast<float>(mirrorSum);
5986
- static std::uint16_t rowPermille[gea::platform::display::kWidth];
6163
+ #if GEA_EMBEDDED_RENDERER_RADIAL_GRADIENTS
6164
+ static
6165
+ #endif
6166
+ std::uint16_t rowPermille[gea::platform::display::kWidth];
5987
6167
  for (int y = y0; y <= y1; ++y)
5988
6168
  {
5989
6169
  int rowX0 = x0;
@@ -5993,42 +6173,45 @@ int gLastScrollUiFrame = -1000;
5993
6173
  continue;
5994
6174
  const float dy = (static_cast<float>(y) + 0.5f - centerY) * invRadiusY;
5995
6175
  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)
6176
+ // Keep the device-sized buffer, but cover wider simulator viewports in chunks.
6177
+ for (int chunkX = rowX0; chunkX <= rowX1; chunkX += gea::platform::display::kWidth)
6001
6178
  {
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)
6006
- {
6007
- permille = rowPermille[mi];
6008
- }
6009
- else
6179
+ const int spanW = std::min(gea::platform::display::kWidth, rowX1 - chunkX + 1);
6180
+ bool rowOpaque = true;
6181
+ for (int i = 0; i < spanW; ++i)
6010
6182
  {
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]];
6183
+ const int xx = chunkX + i;
6184
+ const int mi = mirrorSum - xx - chunkX; // index of xx's mirror in this span
6185
+ int permille;
6186
+ if (mirrorable && mi >= 0 && mi < i)
6187
+ {
6188
+ permille = rowPermille[mi];
6189
+ }
6190
+ else
6191
+ {
6192
+ const float dx = (static_cast<float>(xx) + 0.5f - centerX) * invRadiusX;
6193
+ permille = static_cast<int>(std::sqrt(dx * dx + dy * dy) * 1000.0f + 0.5f);
6194
+ }
6195
+ rowPermille[i] = static_cast<std::uint16_t>(permille > 1000 ? 1000 : permille);
6196
+ const LinearGradientDrawer::DitherStop &e = lut[rowPermille[i]];
6016
6197
  #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);
6198
+ const int threshold = bayerRow[xx & 3] * 16 + 8;
6199
+ const int r5 = e.qR + ((e.remR > threshold) ? 1 : 0);
6200
+ const int g6 = e.qG + ((e.remG > threshold) ? 1 : 0);
6201
+ const int b5 = e.qB + ((e.remB > threshold) ? 1 : 0);
6202
+ rowColor[i] = gea::framework::graphics::pixel::packRgb565Components(r5, g6, b5);
6022
6203
  #else
6023
- rowColor[i] = e.color; // full colour: no dither
6204
+ rowColor[i] = e.color; // full colour: no dither
6024
6205
  #endif
6025
- rowAlpha[i] = e.alpha;
6026
- if (e.alpha != 255)
6027
- rowOpaque = false;
6206
+ rowAlpha[i] = e.alpha;
6207
+ if (e.alpha != 255)
6208
+ rowOpaque = false;
6209
+ }
6210
+ gea::platform::display::Display::blitImage(rowColor, rowOpaque ? nullptr : rowAlpha, spanW, 1, chunkX, y);
6028
6211
  }
6029
- gea::platform::display::Display::blitImage(rowColor, rowOpaque ? nullptr : rowAlpha, spanW, 1, rowX0, y);
6030
6212
  }
6031
6213
 
6214
+ #if GEA_EMBEDDED_RENDERER_RADIAL_GRADIENTS
6032
6215
  // Bake the full box into the cache once params are stable for 2 frames.
6033
6216
  if (cacheable)
6034
6217
  {
@@ -6050,11 +6233,99 @@ int gLastScrollUiFrame = -1000;
6050
6233
  *alp = e.alpha;
6051
6234
  });
6052
6235
  }
6236
+ #endif
6053
6237
  }
6054
6238
  };
6055
6239
 
6056
6240
  struct DisplayCommandDrawer
6057
6241
  {
6242
+ struct TextInkCommand {
6243
+ const DisplayCommand *command;
6244
+ int x0, y0, x1, y1;
6245
+ };
6246
+
6247
+ static void collectTextInk(int id, int owner, int x0, int y0, int x1, int y1,
6248
+ std::vector<TextInkCommand> &ink)
6249
+ {
6250
+ auto &tree = Tree::instance();
6251
+ if (id < 0 || id >= tree.nodeCount()) return;
6252
+ const auto &node = tree.nodes()[id];
6253
+ if (node.style.display == 1 || (id != owner && isOutOfFlowPosition(node.style.position))) return;
6254
+ if (state.hasNodeScratchFor(id) && node.style.visibility == 0) {
6255
+ const int begin = state.nodeDrawStart[id], end = state.nodeDrawEnd[id];
6256
+ for (int ci = std::max(0, begin); ci < end && ci < state.commandCount; ++ci) {
6257
+ const auto &c = state.commands[ci];
6258
+ if (c.type == DisplayCommandType::DrawText || c.type == DisplayCommandType::DrawProjectedText || c.textDecorationInk)
6259
+ ink.push_back({&c, x0, y0, x1, y1});
6260
+ }
6261
+ }
6262
+ // Match the overflow clip recorded for descendants of this node.
6263
+ // Positioned in-flow and stacking-context descendants still contribute;
6264
+ // their opacity does not change the shape of the mask.
6265
+ const int clipIndex = state.hasNodeScratchFor(id) ? state.nodeDrawEnd[id] : -1;
6266
+ if (clipIndex >= 0 && clipIndex < state.commandCount) {
6267
+ const auto &clip = state.commands[clipIndex];
6268
+ if (clip.type == DisplayCommandType::PushClip && clip.clip.nodeId == id) {
6269
+ x0 = std::max<int>(x0, clip.clip.x); y0 = std::max<int>(y0, clip.clip.y);
6270
+ x1 = std::min<int>(x1, clip.clip.x+clip.clip.w-1); y1 = std::min<int>(y1, clip.clip.y+clip.clip.h-1);
6271
+ }
6272
+ }
6273
+ if (x0 > x1 || y0 > y1) return;
6274
+ for (int child = node.first_child; child >= 0; child = tree.nodes()[child].next_sibling)
6275
+ collectTextInk(child, owner, x0, y0, x1, y1, ink);
6276
+ }
6277
+
6278
+ static void replayTextClipped(const DisplayCommand &command)
6279
+ {
6280
+ using gea::platform::display::Display;
6281
+ using namespace gea::framework::graphics;
6282
+ auto *canvas = Display::canvas();
6283
+ if (!canvas) return;
6284
+ int x0, y0, x1, y1;
6285
+ Display::clip(&x0, &y0, &x1, &y1);
6286
+ x0 = std::max({x0, 0, static_cast<int>(command.bx)});
6287
+ y0 = std::max({y0, 0, static_cast<int>(command.by)});
6288
+ x1 = std::min({x1, canvas->width()-1, command.bx+command.bw-1});
6289
+ y1 = std::min({y1, canvas->height()-1, command.by+command.bh-1});
6290
+ if (x0 > x1 || y0 > y1) return;
6291
+ std::vector<TextInkCommand> ink;
6292
+ collectTextInk(command.textClipOwner, command.textClipOwner, x0, y0, x1, y1, ink);
6293
+ if (ink.empty()) return;
6294
+ DisplayCommand paint = command;
6295
+ paint.textClipOwner = -1;
6296
+ // Stack-local, bounded scratch remains safe if independent dirty rows
6297
+ // are replayed on separate workers. No viewport-sized backing store.
6298
+ constexpr int chunkSize = 128;
6299
+ pixel::native_t before[chunkSize];
6300
+ uint8_t coverage[chunkSize];
6301
+ for (int y = y0; y <= y1; ++y) for (int x = x0; x <= x1; x += chunkSize) {
6302
+ const int width = std::min(chunkSize, x1-x+1);
6303
+ std::fill_n(coverage, width, 0);
6304
+ Display::pushClip(x, y, width, 1);
6305
+ for (const auto &entry : ink) {
6306
+ if (y < entry.y0 || y > entry.y1 || x > entry.x1 || x+width <= entry.x0) continue;
6307
+ const auto &c = *entry.command;
6308
+ Display::pushClip(entry.x0, entry.y0, entry.x1-entry.x0+1, entry.y1-entry.y0+1);
6309
+ if (c.type == DisplayCommandType::DrawText)
6310
+ TextRenderer::unionCoverageRow(c, y, x, width, coverage);
6311
+ else if (c.type == DisplayCommandType::DrawProjectedText)
6312
+ drawProjectedText(c, coverage, x);
6313
+ else if (c.textDecorationInk && c.type == DisplayCommandType::FillRect && y >= c.fill.y && y < c.fill.y+c.fill.h)
6314
+ 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)
6315
+ coverage[px-x] = 255;
6316
+ Display::popClip();
6317
+ }
6318
+ if (std::any_of(coverage, coverage+width, [](uint8_t a) { return a != 0; })) {
6319
+ for (int i = 0; i < width; ++i) before[i] = canvas->readPixelNative(x+i, y);
6320
+ replay(paint);
6321
+ for (int i = 0; i < width; ++i) if (coverage[i] != 255)
6322
+ canvas->writePixelNativeExact(x+i, y, coverage[i] == 0 ? before[i] :
6323
+ pixel::blendNative(canvas->readPixelNative(x+i, y), before[i], coverage[i]));
6324
+ }
6325
+ Display::popClip();
6326
+ }
6327
+ }
6328
+
6058
6329
  static void noteReplayCommand(const DisplayCommand &c)
6059
6330
  {
6060
6331
  auto &perf = refreshPerfStatsMutable();
@@ -6084,6 +6355,11 @@ int gLastScrollUiFrame = -1000;
6084
6355
 
6085
6356
  static void GEA_RENDER_HOT_SRAM replay(const DisplayCommand &c)
6086
6357
  {
6358
+ if (c.textClipOwner >= 0 && c.type != DisplayCommandType::SetAlpha &&
6359
+ c.type != DisplayCommandType::PushClip && c.type != DisplayCommandType::PopClip) {
6360
+ replayTextClipped(c);
6361
+ return;
6362
+ }
6087
6363
  noteReplayCommand(c);
6088
6364
  const std::int64_t __replayT0 = refreshPerfNowUs();
6089
6365
  switch (c.type)
@@ -6130,6 +6406,7 @@ int gLastScrollUiFrame = -1000;
6130
6406
  gea::platform::display::Display::strokeRect(c.stroke.x, c.stroke.y, c.stroke.w, c.stroke.h, c.stroke.color);
6131
6407
  break;
6132
6408
  case DisplayCommandType::StrokeRoundedRect:
6409
+ if (c.strokeRoundedRect.cssRadii) { drawCssRoundedBorder(c); break; }
6133
6410
  gea::platform::display::Display::strokeRoundedRect(c.strokeRoundedRect.x, c.strokeRoundedRect.y,
6134
6411
  c.strokeRoundedRect.w, c.strokeRoundedRect.h,
6135
6412
  c.strokeRoundedRect.tl, c.strokeRoundedRect.tr, c.strokeRoundedRect.br, c.strokeRoundedRect.bl,
@@ -6311,7 +6588,7 @@ int gLastScrollUiFrame = -1000;
6311
6588
  const bool simpleBackgroundLeaf = node.first_child < 0 &&
6312
6589
  node.type == NodeType::View &&
6313
6590
  node.style.has_bg &&
6314
- node.style.bg_fill == 0 &&
6591
+ !styleHasBackgroundImage(node.style) && rstyle(node.style).bg_clip == 0 &&
6315
6592
  !hasAnyBorder(node.style);
6316
6593
  if (!simpleBackgroundLeaf)
6317
6594
  return -1;
@@ -6320,12 +6597,12 @@ int gLastScrollUiFrame = -1000;
6320
6597
 
6321
6598
  static void recordFilterBlur(int id, const Node &node, DisplayCommandType type, uint8_t parentAlpha)
6322
6599
  {
6323
- if (rstyle(node.style).filter_blur_radius <= 0)
6600
+ if ((GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0) <= 0)
6324
6601
  return;
6325
6602
  if (node.layout.width <= 0 || node.layout.height <= 0)
6326
6603
  return;
6327
6604
  const bool sourceLayerBlur = filterBlurSourceAlphaCap(node, parentAlpha) >= 0;
6328
- const int localSpread = sourceLayerBlur ? std::max(0, static_cast<int>(rstyle(node.style).filter_blur_radius)) * kFilterBlurPasses : 0;
6605
+ const int localSpread = sourceLayerBlur ? std::max(0, static_cast<int>((GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0))) * kFilterBlurPasses : 0;
6329
6606
  int16_t xs[4] = {};
6330
6607
  int16_t ys[4] = {};
6331
6608
  ViewRenderer::transformedRectCorners(node, false,
@@ -6351,9 +6628,9 @@ int gLastScrollUiFrame = -1000;
6351
6628
  }
6352
6629
  if (x0 > x1 || y0 > y1)
6353
6630
  return;
6354
- int radiusX = rstyle(node.style).filter_blur_radius;
6355
- int radiusY = rstyle(node.style).filter_blur_radius;
6356
- filterBlurScreenRadii(node, rstyle(node.style).filter_blur_radius, &radiusX, &radiusY);
6631
+ int radiusX = (GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0);
6632
+ int radiusY = (GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0);
6633
+ filterBlurScreenRadii(node, (GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0), &radiusX, &radiusY);
6357
6634
  const int spreadX = sourceLayerBlur ? 0 : std::max(0, radiusX) * kFilterBlurPasses;
6358
6635
  const int spreadY = sourceLayerBlur ? 0 : std::max(0, radiusY) * kFilterBlurPasses;
6359
6636
  x0 -= spreadX;
@@ -6369,13 +6646,34 @@ int gLastScrollUiFrame = -1000;
6369
6646
  cmd->bw = static_cast<int16_t>(clampInt(x1 - x0 + 1, 0, 32767));
6370
6647
  cmd->bh = static_cast<int16_t>(clampInt(y1 - y0 + 1, 0, 32767));
6371
6648
  cmd->filterBlur.nodeId = static_cast<int16_t>(id);
6372
- cmd->filterBlur.radius = rstyle(node.style).filter_blur_radius;
6649
+ cmd->filterBlur.radius = (GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0);
6373
6650
  cmd->filterBlur.radiusX = static_cast<int16_t>(clampInt(radiusX, 0, 32767));
6374
6651
  cmd->filterBlur.radiusY = static_cast<int16_t>(clampInt(radiusY, 0, 32767));
6375
6652
  cmd->filterBlur.sourceAlphaCap = static_cast<int16_t>(filterBlurSourceAlphaCap(node, parentAlpha));
6376
6653
  }
6377
6654
 
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)
6655
+ struct RecordClipFrame {
6656
+ const Node &node;
6657
+ const RecordClipFrame *parent;
6658
+ };
6659
+
6660
+ // Fixed descendants are painted in place in z-order, but their viewport
6661
+ // containing block is outside the ordinary ancestors' overflow clips.
6662
+ struct SuspendedRecordClips {
6663
+ const RecordClipFrame *clips;
6664
+ explicit SuspendedRecordClips(const RecordClipFrame *value) : clips(value) {
6665
+ for (auto *frame = clips; frame; frame = frame->parent)
6666
+ ViewRenderer::recordClipEnd(frame->node);
6667
+ }
6668
+ static void restore(const RecordClipFrame *frame) {
6669
+ if (!frame) return;
6670
+ restore(frame->parent);
6671
+ ViewRenderer::recordClipBegin(frame->node);
6672
+ }
6673
+ ~SuspendedRecordClips() { restore(clips); }
6674
+ };
6675
+
6676
+ 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
6677
  {
6380
6678
  if (!state.hasNodeScratchFor(id))
6381
6679
  return;
@@ -6391,7 +6689,7 @@ int gLastScrollUiFrame = -1000;
6391
6689
  return;
6392
6690
  }
6393
6691
  Node *n = &Tree::instance().nodes()[id];
6394
- if (n->style.display == 1)
6692
+ if (n->style.display == 1 || isCollapsedFlexSubtree(*n) || ViewRenderer::backfaceSubtreeHidden(*n))
6395
6693
  {
6396
6694
  state.clearNodeRange(id);
6397
6695
  return;
@@ -6413,14 +6711,35 @@ int gLastScrollUiFrame = -1000;
6413
6711
  // guarantees the parent's background fill is what replays in the
6414
6712
  // dirty rect — same semantics as display:none, without forcing app
6415
6713
  // code to choose between the two patterns.
6416
- if (n->style.opacity == 0)
6714
+ bool contributesTextMask = false;
6715
+ if (n->style.opacity == 0) {
6716
+ const auto *nodes = Tree::instance().nodes();
6717
+ for (int ancestor = id; ancestor >= 0; ancestor = nodes[ancestor].parent) {
6718
+ if (isOutOfFlowPosition(nodes[ancestor].style.position)) break;
6719
+ const int parent = nodes[ancestor].parent;
6720
+ if (parent >= 0 && StyleValues::hasTextBackgroundClip(nodes[parent].style)) {
6721
+ contributesTextMask = true; break;
6722
+ }
6723
+ }
6724
+ }
6725
+ if (n->style.opacity == 0 && !contributesTextMask)
6417
6726
  {
6418
6727
  state.clearNodeRange(id);
6419
6728
  return;
6420
6729
  }
6421
6730
 
6422
- int overlaps_clip = ClipMath::nodeOverlapsClip(*n, cx0, cy0, cx1, cy1);
6423
- if (!overlaps_clip && (!isViewLikeNodeType(n->type) || n->style.overflow != 0))
6731
+ const bool viewportFixed = LayoutEngine::isViewportFixed(*n);
6732
+ SuspendedRecordClips suspended(viewportFixed ? clips : nullptr);
6733
+ if (viewportFixed) {
6734
+ clips = nullptr;
6735
+ cx0 = cy0 = 0;
6736
+ const auto *canvas = gea::platform::display::Display::canvas();
6737
+ cx1 = (canvas ? canvas->width() : treeState().mountedWidth) - 1;
6738
+ cy1 = (canvas ? canvas->height() : treeState().mountedHeight) - 1;
6739
+ }
6740
+ int overlaps_clip = isDocumentCanvasRoot(*n) || ClipMath::nodeOverlapsClip(*n, cx0, cy0, cx1, cy1);
6741
+ if (!overlaps_clip && (!isViewLikeNodeType(n->type) || (overflowX(n->style) && overflowY(n->style))) &&
6742
+ !LayoutEngine::containsViewportFixed(id))
6424
6743
  {
6425
6744
  state.clearNodeRange(id);
6426
6745
  return;
@@ -6432,7 +6751,10 @@ int gLastScrollUiFrame = -1000;
6432
6751
  int h = n->layout.height;
6433
6752
 
6434
6753
  DisplayList &list = DisplayList::instance();
6435
- int pushed_clip = overlaps_clip ? ViewRenderer::recordClipBegin(*n) : 0;
6754
+ // A culled ancestor may still be traversed to reach a fixed child.
6755
+ // Keep its clip active for ordinary children; the fixed child alone
6756
+ // suspends that clip when it enters viewport coordinates.
6757
+ int pushed_clip = 0;
6436
6758
  uint8_t cur_alpha = parent_alpha;
6437
6759
  const Node *active_mask = mask_node;
6438
6760
 
@@ -6457,8 +6779,9 @@ int gLastScrollUiFrame = -1000;
6457
6779
  active_mask = n;
6458
6780
 
6459
6781
  int draw_start = state.commandCount;
6782
+ const bool paintsOwnBox = overlaps_clip && n->style.visibility == 0;
6460
6783
  const bool nativeTextInput = isNativeTextInputView(*n);
6461
- const bool filtered = overlaps_clip && rstyle(n->style).filter_blur_radius > 0;
6784
+ const bool filtered = overlaps_clip && (GEA_CSS_FILTERS ? rstyle(n->style).filter_blur_radius : 0) > 0;
6462
6785
  uint8_t draw_alpha = cur_alpha;
6463
6786
  bool pushed_mask_alpha = false;
6464
6787
  if (active_mask && active_mask->style.mask_right_fade_width > 0)
@@ -6474,26 +6797,26 @@ int gLastScrollUiFrame = -1000;
6474
6797
  if (filtered)
6475
6798
  recordFilterBlur(id, *n, DisplayCommandType::BeginFilterBlur, draw_alpha);
6476
6799
 
6477
- if (overlaps_clip && !nativeTextInput)
6800
+ if (paintsOwnBox && !nativeTextInput)
6478
6801
  ViewRenderer::recordBox(*n, draw_alpha);
6479
6802
 
6480
- if (overlaps_clip && n->type == NodeType::Text)
6803
+ if (paintsOwnBox && n->type == NodeType::Text)
6481
6804
  {
6482
6805
  TextRenderer::record(*n, draw_alpha);
6483
6806
  }
6484
- else if (overlaps_clip && n->type == NodeType::Image)
6807
+ else if (paintsOwnBox && n->type == NodeType::Image)
6485
6808
  {
6486
6809
  ImageRenderer::record(*n);
6487
6810
  }
6488
- else if (overlaps_clip && n->type == NodeType::Canvas)
6811
+ else if (paintsOwnBox && n->type == NodeType::Canvas)
6489
6812
  {
6490
6813
  CanvasRenderer::record(*n);
6491
6814
  }
6492
- else if (overlaps_clip && n->type == NodeType::Camera)
6815
+ else if (paintsOwnBox && n->type == NodeType::Camera)
6493
6816
  {
6494
6817
  CameraRenderer::record(*n);
6495
6818
  }
6496
- else if (overlaps_clip && n->type == NodeType::View)
6819
+ else if (paintsOwnBox && n->type == NodeType::View)
6497
6820
  {
6498
6821
  // `<input>` JSX elements lower to a View node with tag_name "input".
6499
6822
  // On native text-input targets, UIKit/AppKit owns the full visual
@@ -6509,14 +6832,10 @@ int gLastScrollUiFrame = -1000;
6509
6832
  if (draw_end > draw_start && state.drawNodeOrderCount < state.nodeScratchCapacity)
6510
6833
  state.drawNodeOrder[state.drawNodeOrderCount++] = id;
6511
6834
 
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
- }
6835
+ // A box's own background, border and outline are outside its content clip.
6836
+ pushed_clip = ViewRenderer::recordClipBegin(*n);
6837
+ ++state.recordDepth;
6838
+ const auto children = PaintOrder::collectChildren(id, groupRoot, includePositioned);
6520
6839
 
6521
6840
  int child_cx0 = cx0, child_cy0 = cy0, child_cx1 = cx1, child_cy1 = cy1;
6522
6841
  if (pushed_clip && id == state.recordExpandedClipNode)
@@ -6529,12 +6848,38 @@ int gLastScrollUiFrame = -1000;
6529
6848
  else if (pushed_clip)
6530
6849
  ClipMath::clampToNode(*n, &child_cx0, &child_cy0, &child_cx1, &child_cy1);
6531
6850
 
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);
6851
+ const RecordClipFrame ownClip{*n, clips};
6852
+ for (int child : children) {
6853
+ // A positioned descendant can paint in this context while its
6854
+ // intervening non-stacking ancestors still supply overflow clips.
6855
+ Node *nodes = Tree::instance().nodes();
6856
+ std::vector<int> ancestors;
6857
+ bool dirty = insideDirty || n->render.dirty;
6858
+ for (int p = nodes[child].parent; p >= 0 && p != id; p = nodes[p].parent) {
6859
+ dirty |= nodes[p].render.dirty;
6860
+ if (nodes[p].style.overflow != 0) ancestors.push_back(p);
6861
+ }
6862
+ std::vector<RecordClipFrame> frames;
6863
+ frames.reserve(ancestors.size());
6864
+ const RecordClipFrame *childClips = pushed_clip ? &ownClip : clips;
6865
+ int x0 = child_cx0, y0 = child_cy0, x1 = child_cx1, y1 = child_cy1;
6866
+ for (auto p = ancestors.rbegin(); p != ancestors.rend(); ++p) {
6867
+ const Node &ancestor = nodes[*p];
6868
+ if (ViewRenderer::recordClipBegin(ancestor)) {
6869
+ frames.push_back({ancestor, childClips}); childClips = &frames.back();
6870
+ if (*p == state.recordExpandedClipNode) {
6871
+ x0 = state.recordExpandedClipX0; y0 = state.recordExpandedClipY0;
6872
+ x1 = state.recordExpandedClipX1; y1 = state.recordExpandedClipY1;
6873
+ } else ClipMath::clampToNode(ancestor, &x0, &y0, &x1, &y1);
6874
+ }
6875
+ }
6876
+ recordNode(child, cur_alpha, x0, y0, x1, y1, active_mask, dirty, childClips, PaintOrder::isGroup(child), PaintOrder::isContext(child));
6877
+ for (auto frame = frames.rbegin(); frame != frames.rend(); ++frame) ViewRenderer::recordClipEnd(frame->node);
6878
+ }
6534
6879
 
6535
6880
  state.recordDepth--;
6536
6881
 
6537
- if (overlaps_clip && !nativeTextInput)
6882
+ if (paintsOwnBox && !nativeTextInput)
6538
6883
  ViewRenderer::recordScrollbar(*n);
6539
6884
 
6540
6885
  if (filtered && !nativeTextInput)
@@ -6650,6 +6995,7 @@ int gLastScrollUiFrame = -1000;
6650
6995
 
6651
6996
  bool append(const DisplayCommand &c)
6652
6997
  {
6998
+ if (c.textClipOwner >= 0) return false;
6653
6999
  if (c.type == DisplayCommandType::FillRoundedRect)
6654
7000
  return appendBox(c.fillRoundedRect.x,
6655
7001
  c.fillRoundedRect.y,
@@ -6677,7 +7023,7 @@ int gLastScrollUiFrame = -1000;
6677
7023
 
6678
7024
  RoundedRectScreenSpan span{};
6679
7025
  if (!transformedRoundedRectToScreenSpan(r, &span) ||
6680
- !roundedRectScreenSpanHasCircularRadii(span))
7026
+ !roundedRectScreenSpanFitsCanvas(span))
6681
7027
  return false;
6682
7028
  if (roundedRectScreenSpanIsCircle(span))
6683
7029
  {
@@ -7160,21 +7506,18 @@ int gLastScrollUiFrame = -1000;
7160
7506
  if (count >= kScratchDepth)
7161
7507
  return -1;
7162
7508
  chain[count++] = cursor;
7509
+ if (LayoutEngine::isViewportFixed(nodes[cursor])) break;
7163
7510
  }
7164
7511
  int pushed = 0;
7165
- for (int i = count - 1; i >= 0; i--)
7512
+ for (int i = count - 1; i > 0; i--)
7166
7513
  {
7167
7514
  const int id = chain[i];
7168
7515
  if (!nodeRecordsOverflowClip(id, nodes, nodeCount))
7169
7516
  continue;
7170
7517
  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);
7518
+ int x, y, w, h;
7519
+ overflowClipBounds(clip, x, y, w, h);
7520
+ gea::platform::display::Display::pushClip(x, y, w, h);
7178
7521
  pushed++;
7179
7522
  }
7180
7523
  return pushed;
@@ -7285,6 +7628,7 @@ int gLastScrollUiFrame = -1000;
7285
7628
 
7286
7629
  static bool canUseSimpleDirtyReplay(int width, int height)
7287
7630
  {
7631
+ if (DisplayList::instance().hasTextClippedBackgrounds()) return false;
7288
7632
  #if GEA_EMBEDDED_SIMPLE_REPLAY_DEBUG
7289
7633
  static int dbgN = 0;
7290
7634
  const bool dbg = (dbgN++ % 600) == 0;
@@ -7298,7 +7642,7 @@ int gLastScrollUiFrame = -1000;
7298
7642
  for (int i = 0; i < nodeCount; i++)
7299
7643
  {
7300
7644
  const Node &node = nodes[i];
7301
- if (rstyle(node.style).filter_blur_radius > 0)
7645
+ if ((GEA_CSS_FILTERS ? rstyle(node.style).filter_blur_radius : 0) > 0)
7302
7646
  { SRDBG("blur node", i); return false; }
7303
7647
  if (node.style.opacity != 255 && node.style.opacity != 0)
7304
7648
  { SRDBG("opacity node", i); return false; }
@@ -7598,6 +7942,14 @@ int gLastScrollUiFrame = -1000;
7598
7942
  for (int id = origin; id >= 0 && chainLen < 32; id = nodes[id].parent)
7599
7943
  chain[chainLen++] = id;
7600
7944
  const int originOrder = drawOrderIndexForSimpleReplay(origin);
7945
+ // An opaque image can cover the origin's ancestors completely.
7946
+ // Use the ordered cull walk instead of repainting those ancestors
7947
+ // immediately before the image overwrites them.
7948
+ if (occlusionCullStartIndex(x0, y0, x1, y1, nodes, nodeCount) > 0)
7949
+ {
7950
+ replaySimpleClippedDirtyRegionFast(x0, y0, x1, y1);
7951
+ return;
7952
+ }
7601
7953
  if (!simpleOriginReplayCanSkipEarlierNodes(chain, chainLen, originOrder, x0, y0, x1, y1))
7602
7954
  {
7603
7955
  replaySimpleClippedDirtyRegionFast(x0, y0, x1, y1);
@@ -8053,7 +8405,8 @@ int gLastScrollUiFrame = -1000;
8053
8405
  // fully paint, so a replay of that region reproduces the full replay instead of
8054
8406
  // re-blending antialiased edges over their own previous blend. Display::clear()
8055
8407
  // leaves 0 on every backend (see each target's clearNoFlush), and Tree::mount()
8056
- // is the only place it runs.
8408
+ // is the only place it runs. HTML documents instead use the white UA
8409
+ // canvas recorded below their root background.
8057
8410
  static void GEA_RENDER_HOT_SRAM restoreClearBaseInRegion(int x0, int y0, int x1, int y1)
8058
8411
  {
8059
8412
  if (x0 > x1 || y0 > y1)
@@ -8061,16 +8414,19 @@ int gLastScrollUiFrame = -1000;
8061
8414
  Tree &tree = Tree::instance();
8062
8415
  if (regionHasOpaqueBase(x0, y0, x1, y1, tree.nodes(), tree.nodeCount()))
8063
8416
  return;
8064
- gea::platform::display::Display::fillRect(x0, y0, x1 - x0 + 1, y1 - y0 + 1, 0x0000);
8417
+ const int root = tree.mountedRoot();
8418
+ const auto base = root >= 0 && root < tree.nodeCount() && isDocumentCanvasRoot(tree.nodes()[root])
8419
+ ? gea::framework::graphics::pixel::nativeColor(255, 255, 255) : 0;
8420
+ gea::platform::display::Display::fillRect(x0, y0, x1 - x0 + 1, y1 - y0 + 1, base);
8065
8421
  }
8066
8422
 
8067
8423
  // Painter's-algorithm occlusion cull: the LAST node (in draw order) whose
8068
- // first command is an opaque FillRect fully covering the region occludes
8424
+ // first command is an opaque fill or image covering the region occludes
8069
8425
  // everything drawn before it — the replay walk can start there. A page-sized
8070
8426
  // UI (e-reader page turn) stacks several full-bleed paper fills under the
8071
8427
  // content; each is a full PSRAM write of the region, so skipping the covered
8072
8428
  // ones removes ~100 ms/turn on a 540×960 GRAY4 panel. Conservative: the
8073
- // cover must be untransformed (a plain FillRect), at full effective alpha,
8429
+ // cover must be untransformed, at full effective alpha,
8074
8430
  // its node's first command, never blinking, and not shrunk by any ancestor
8075
8431
  // overflow clip smaller than the region.
8076
8432
  static int GEA_RENDER_HOT_SRAM occlusionCullStartIndex(int x0, int y0, int x1, int y1, Node *nodes, int nodeCount)
@@ -8092,10 +8448,16 @@ int gLastScrollUiFrame = -1000;
8092
8448
  if (start < 0 || end <= start || end > state.commandCount)
8093
8449
  continue;
8094
8450
  const DisplayCommand &c = state.commands[start];
8095
- if (c.type != DisplayCommandType::FillRect)
8096
- continue;
8097
- if (c.fill.x > x0 || c.fill.y > y0 ||
8098
- c.fill.x + c.fill.w - 1 < x1 || c.fill.y + c.fill.h - 1 < y1)
8451
+ bool covers = false;
8452
+ if (c.type == DisplayCommandType::FillRect)
8453
+ covers = c.fill.x <= x0 && c.fill.y <= y0 &&
8454
+ c.fill.x + c.fill.w - 1 >= x1 && c.fill.y + c.fill.h - 1 >= y1;
8455
+ else if (c.type == DisplayCommandType::BlitImage)
8456
+ covers = c.blit.pixels && !c.blit.alpha &&
8457
+ c.blit.dx <= x0 && c.blit.dy <= y0 &&
8458
+ c.blit.dx + c.blit.sourceWidth - 1 >= x1 &&
8459
+ c.blit.dy + c.blit.sourceHeight - 1 >= y1;
8460
+ if (!covers || c.textClipOwner >= 0)
8099
8461
  continue;
8100
8462
  // The recorded paint bbox must also cover the region (guards against
8101
8463
  // any recording that paints less than the command rect suggests).
@@ -8223,6 +8585,7 @@ int gLastScrollUiFrame = -1000;
8223
8585
 
8224
8586
  static bool replayFillRectInRegion(const DisplayCommand &c, const DisplayReplayRegion &region)
8225
8587
  {
8588
+ if (c.textClipOwner >= 0) return false;
8226
8589
  if (c.type != DisplayCommandType::FillRect || !commandOverlapsRegion(c, region))
8227
8590
  return false;
8228
8591
  int x0 = c.fill.x;
@@ -8246,6 +8609,7 @@ int gLastScrollUiFrame = -1000;
8246
8609
 
8247
8610
  static bool replayContainedRoundedRectFillInRegion(const DisplayCommand &c, int x0, int y0, int x1, int y1)
8248
8611
  {
8612
+ if (c.textClipOwner >= 0) return false;
8249
8613
  if (c.type != DisplayCommandType::FillRoundedRect)
8250
8614
  return false;
8251
8615
  if (!regionInsideRetainedBackground(c, x0, y0, x1, y1))
@@ -8703,19 +9067,119 @@ int gLastScrollUiFrame = -1000;
8703
9067
 
8704
9068
  } // namespace
8705
9069
 
9070
+ void PaintOrder::sortChildren(int *children, int count, int contextRoot)
9071
+ {
9072
+ ChildZSorter::sort(children, count, contextRoot);
9073
+ }
9074
+
9075
+ bool PaintOrder::isContext(int id)
9076
+ {
9077
+ const auto *nodes = Tree::instance().nodes();
9078
+ const auto &n = nodes[id];
9079
+ const bool item = n.parent >= 0 && !isOutOfFlowPosition(n.style.position) &&
9080
+ (nodes[n.parent].style.display == kDisplayFlex || isDisplayGrid(nodes[n.parent].style));
9081
+ return n.parent < 0 || n.style.position == kPositionFixed || ChildZSorter::effectGroup(n) ||
9082
+ (!n.style.z_index_auto && (n.style.position != 0 || item));
9083
+ }
9084
+
9085
+ bool PaintOrder::isGroup(int id)
9086
+ {
9087
+ const auto *nodes = Tree::instance().nodes();
9088
+ const auto &n = nodes[id];
9089
+ const bool item = n.parent >= 0 &&
9090
+ (nodes[n.parent].style.display == kDisplayFlex || isDisplayGrid(nodes[n.parent].style));
9091
+ return isContext(id) || n.style.position != 0 || n.style.float_side || item;
9092
+ }
9093
+
9094
+ std::vector<int> PaintOrder::collectChildren(int root, bool groupRoot, bool includePositioned)
9095
+ {
9096
+ const auto *nodes = Tree::instance().nodes();
9097
+ std::vector<int> result;
9098
+ if (!groupRoot) return result; // Ordinary boxes contribute only their own paint.
9099
+ // CSS painting follows stacking contexts rather than DOM parentage.
9100
+ // Float, flex/grid-item and positioned-auto groups keep their ordinary
9101
+ // contents together, while their positioned descendants participate in
9102
+ // the enclosing real context. Ordinary block/inline wrappers contribute
9103
+ // separate entries so block backgrounds remain below floats and text.
9104
+ auto walk = [&](auto &&self, int parent, bool withinGroup) -> void {
9105
+ std::vector<int> children;
9106
+ for (int child = nodes[parent].first_child; child >= 0; child = nodes[child].next_sibling)
9107
+ if (!isDisplayNone(nodes[child].style)) children.push_back(child);
9108
+ if (nodes[parent].style.display == kDisplayFlex || isDisplayGrid(nodes[parent].style)) {
9109
+ std::stable_sort(children.begin(), children.end(), [&](int a, int b) {
9110
+ const int oa = isOutOfFlowPosition(nodes[a].style.position) ? 0 : nodes[a].style.order;
9111
+ const int ob = isOutOfFlowPosition(nodes[b].style.position) ? 0 : nodes[b].style.order;
9112
+ return oa < ob;
9113
+ });
9114
+ }
9115
+ for (int child : children) {
9116
+ const auto &n = nodes[child];
9117
+ if (n.style.blink_interval_ms > 0 && !n.style.blink_visible) continue;
9118
+ const bool context = isContext(child), positioned = n.style.position != 0 || context;
9119
+ const bool group = isGroup(child);
9120
+ if (positioned) {
9121
+ if (includePositioned) result.push_back(child);
9122
+ if (!context && includePositioned) self(self, child, true);
9123
+ } else {
9124
+ if (!withinGroup) result.push_back(child);
9125
+ self(self, child, withinGroup || group);
9126
+ }
9127
+ }
9128
+ };
9129
+ walk(walk, root, false);
9130
+ sortChildren(result.data(), static_cast<int>(result.size()), root);
9131
+ return result;
9132
+ }
9133
+
9134
+ int PaintOrder::compareNodes(int first, int second)
9135
+ {
9136
+ // This is also the recorder's traversal, so retained draws cannot invent a
9137
+ // second hierarchy or lose document-order ties after a depth crossing.
9138
+ static int ranks[kMaxNodes];
9139
+ static uint64_t serial = ~0ull;
9140
+ static uint32_t recordSerial = 0;
9141
+ auto &tree = Tree::instance();
9142
+ if (serial != tree.refreshSerial() || recordSerial != state.displayListSerial) {
9143
+ std::fill(std::begin(ranks), std::end(ranks), -1);
9144
+ int next = 0;
9145
+ auto visit = [&](auto &&self, int node, bool groupRoot, bool positioned) -> void {
9146
+ ranks[node] = next++;
9147
+ for (int child : collectChildren(node, groupRoot, positioned))
9148
+ self(self, child, isGroup(child), isContext(child));
9149
+ };
9150
+ for (int id = 0; id < tree.nodeCount(); ++id)
9151
+ if (tree.nodes()[id].parent < 0) visit(visit, id, true, true);
9152
+ serial = tree.refreshSerial(); recordSerial = state.displayListSerial;
9153
+ }
9154
+ return ranks[first] - ranks[second];
9155
+ }
9156
+
8706
9157
  DisplayList &DisplayList::instance()
8707
9158
  {
8708
9159
  static DisplayList displayList;
8709
9160
  return displayList;
8710
9161
  }
8711
9162
 
9163
+ static int recordingTextClipOwner = -1;
9164
+
9165
+ int DisplayList::setRecordingTextClipOwner(int owner)
9166
+ {
9167
+ const int previous = recordingTextClipOwner;
9168
+ recordingTextClipOwner = owner;
9169
+ return previous;
9170
+ }
9171
+
8712
9172
  DisplayCommand *DisplayList::append()
8713
9173
  {
9174
+ if (recordingTextClipOwner >= 0) state.textClippedBackgrounds = true;
8714
9175
  if (state.appendOverride)
8715
9176
  {
8716
9177
  if (!state.appendOverrideCount || *state.appendOverrideCount >= state.appendOverrideCapacity)
8717
9178
  return nullptr;
8718
- return &state.appendOverride[(*state.appendOverrideCount)++];
9179
+ auto *command = &state.appendOverride[(*state.appendOverrideCount)++];
9180
+ command->textClipOwner = recordingTextClipOwner;
9181
+ command->textDecorationInk = false;
9182
+ return command;
8719
9183
  }
8720
9184
  if (!state.commands)
8721
9185
  {
@@ -8773,7 +9237,10 @@ int gLastScrollUiFrame = -1000;
8773
9237
  state.commandOverflow = true;
8774
9238
  return nullptr;
8775
9239
  }
8776
- return &state.commands[state.commandCount++];
9240
+ auto *command = &state.commands[state.commandCount++];
9241
+ command->textClipOwner = recordingTextClipOwner;
9242
+ command->textDecorationInk = false;
9243
+ return command;
8777
9244
  }
8778
9245
 
8779
9246
  void DisplayList::clear()
@@ -8785,6 +9252,7 @@ int gLastScrollUiFrame = -1000;
8785
9252
  state.filterBlurCacheHits = 0;
8786
9253
  state.filterBlurCacheMisses = 0;
8787
9254
  state.commandCount = 0;
9255
+ state.textClippedBackgrounds = false;
8788
9256
  state.commandOverflow = false;
8789
9257
  state.drawNodeOrderCount = 0;
8790
9258
  if (!state.ensureNodeScratchCapacity(tree.nodeCount()))
@@ -9032,6 +9500,18 @@ int gLastScrollUiFrame = -1000;
9032
9500
  return false;
9033
9501
  }
9034
9502
 
9503
+ // A flat hidden face can remove its entire paint subtree. Rebuild when
9504
+ // its visibility changes; an unchanged hidden group needs no commands,
9505
+ // and an unchanged visible group can still use the reprojection path.
9506
+ // The recorder gives even an empty visible container a valid range.
9507
+ for (int i = 0; i < state.drawNodeOrderCount; ++i)
9508
+ if (state.drawNodeOrder[i] < 0 || state.drawNodeOrder[i] >= nodeCount) return false;
9509
+ for (int id = 0; id < nodeCount; ++id) {
9510
+ if (!nodes[id].style.backface_hidden || !ViewRenderer::isTransformableBox(nodes[id]) || preserves3D(nodes[id].style)) continue;
9511
+ const bool wasRecorded = state.nodeDrawStart[id] >= 0;
9512
+ if (wasRecorded == ViewRenderer::backfaceSubtreeHidden(nodes[id])) return false;
9513
+ }
9514
+
9035
9515
  // Validate first (no partial patching): every recorded command's screen geometry
9036
9516
  // must be recomputable from a stored local rect. Anything else — clips, plain
9037
9517
  // fills/strokes, images, blur, raw text — means the structure isn't a pure
@@ -9285,86 +9765,17 @@ int gLastScrollUiFrame = -1000;
9285
9765
  state.reprojectDirtyX1 = rdX1;
9286
9766
  state.reprojectDirtyY1 = rdY1;
9287
9767
 
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
- {
9768
+ // Reproduce the recorder's traversal order after transforms change. Comparing
9769
+ // the diverging branches preserves parent/child groups, paint phases,
9770
+ // stack levels, projected depth, and order-modified document ties.
9771
+ for (int i = 1; i < state.drawNodeOrderCount; ++i) {
9351
9772
  const int node = state.drawNodeOrder[i];
9352
- const int dp = sortDepth[i];
9353
- const int zi = sortZ[i];
9354
9773
  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;
9774
+ while (j >= 0 && PaintOrder::compareNodes(state.drawNodeOrder[j], node) > 0) {
9360
9775
  state.drawNodeOrder[j + 1] = state.drawNodeOrder[j];
9361
- sortDepth[j + 1] = sortDepth[j];
9362
- sortZ[j + 1] = sortZ[j];
9363
- j--;
9776
+ --j;
9364
9777
  }
9365
9778
  state.drawNodeOrder[j + 1] = node;
9366
- sortDepth[j + 1] = dp;
9367
- sortZ[j + 1] = zi;
9368
9779
  }
9369
9780
  return true;
9370
9781
  }
@@ -9407,6 +9818,18 @@ int gLastScrollUiFrame = -1000;
9407
9818
  }
9408
9819
  }
9409
9820
  }
9821
+ if (isDocumentCanvasRoot(nodes[id])) {
9822
+ // The UA canvas is below the root's opacity/visibility scope and is
9823
+ // independent of its layout box. Replaying it clears removed or
9824
+ // translucent propagated backgrounds across the whole viewport.
9825
+ if (DisplayCommand *cmd = append()) {
9826
+ cmd->type = DisplayCommandType::FillRect;
9827
+ cmd->bx = cmd->by = cmd->fill.x = cmd->fill.y = 0;
9828
+ cmd->bw = cmd->fill.w = cx1 + 1;
9829
+ cmd->bh = cmd->fill.h = cy1 + 1;
9830
+ cmd->fill.color = gea::framework::graphics::pixel::nativeColor(255, 255, 255);
9831
+ }
9832
+ }
9410
9833
  RenderRecorder::recordNode(id, parent_alpha, 0, 0, cx1, cy1, nullptr);
9411
9834
  }
9412
9835
 
@@ -9437,6 +9860,7 @@ int gLastScrollUiFrame = -1000;
9437
9860
 
9438
9861
  bool DisplayList::canReplayDirectDirtyRegions(int width, int height) const
9439
9862
  {
9863
+ if (hasTextClippedBackgrounds()) return false;
9440
9864
  Tree &tree = Tree::instance();
9441
9865
  Node *nodes = tree.nodes();
9442
9866
  if (state.drawNodeOrderCount <= 0)
@@ -9444,7 +9868,7 @@ int gLastScrollUiFrame = -1000;
9444
9868
 
9445
9869
  for (int i = 0; i < tree.nodeCount(); i++)
9446
9870
  {
9447
- if (rstyle(nodes[i].style).filter_blur_radius > 0)
9871
+ if ((GEA_CSS_FILTERS ? rstyle(nodes[i].style).filter_blur_radius : 0) > 0)
9448
9872
  return false;
9449
9873
  if (nodes[i].style.overflow == 2)
9450
9874
  return false;
@@ -9456,6 +9880,10 @@ int gLastScrollUiFrame = -1000;
9456
9880
  }
9457
9881
 
9458
9882
  int DisplayList::commandCount() const { return state.commandCount; }
9883
+ bool DisplayList::hasTextClippedBackgrounds() const
9884
+ {
9885
+ return state.textClippedBackgrounds;
9886
+ }
9459
9887
 
9460
9888
  void DisplayList::clearRetainedBackgroundRecolors()
9461
9889
  {
@@ -9689,21 +10117,16 @@ int gLastScrollUiFrame = -1000;
9689
10117
  int y0 = 0;
9690
10118
  int x1 = width - 1;
9691
10119
  int y1 = height - 1;
9692
- for (int a = nodes[id].parent; a >= 0 && a < nodeCount; a = nodes[a].parent)
10120
+ for (int a = LayoutEngine::isViewportFixed(nodes[id]) ? -1 : nodes[id].parent; a >= 0 && a < nodeCount; a = nodes[a].parent)
9693
10121
  {
9694
10122
  const Node &ancestor = nodes[a];
9695
10123
  if (!isViewLikeNodeType(ancestor.type) || ancestor.style.overflow == 0 || ancestor.first_child < 0)
10124
+ {
10125
+ if (LayoutEngine::isViewportFixed(ancestor)) break;
9696
10126
  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;
10127
+ }
10128
+ ClipMath::clampToNode(ancestor, &x0, &y0, &x1, &y1);
10129
+ if (LayoutEngine::isViewportFixed(ancestor)) break;
9707
10130
  }
9708
10131
  *cx0 = x0;
9709
10132
  *cy0 = y0;
@@ -9763,8 +10186,8 @@ int gLastScrollUiFrame = -1000;
9763
10186
  continue;
9764
10187
  const Node &probe = nodes[id];
9765
10188
  const bool paintsBox =
9766
- (probe.style.has_bg && probe.style.bg_alpha > 0) ||
9767
- rstyle(probe.style).box_shadow_alpha > 0;
10189
+ (probe.style.has_bg && (probe.style.bg_alpha > 0 || styleHasBackgroundImage(probe.style))) ||
10190
+ (GEA_CSS_BOX_SHADOW ? rstyle(probe.style).box_shadow_alpha : 0) > 0;
9768
10191
  if (!paintsBox)
9769
10192
  continue;
9770
10193
  }
@@ -10893,6 +11316,15 @@ int gLastScrollUiFrame = -1000;
10893
11316
  case DisplayCommandType::StrokeRoundedRect:
10894
11317
  {
10895
11318
  auto r = command.strokeRoundedRect;
11319
+ if (r.cssRadii) {
11320
+ TransformedRoundedRectCommand outer{}, inner{};
11321
+ cssRoundedBorderContours(r, outer, inner);
11322
+ const int samples = std::max(1, aaSamples);
11323
+ const int coverage = cssRoundedBorderCoverage(outer, inner, x, y, samples);
11324
+ *outColor = r.color;
11325
+ *outAlpha = combinedCoverageAlpha(255, coverage, samples * samples);
11326
+ return *outAlpha > 0;
11327
+ }
10896
11328
  const int maxRadius = std::min(r.w / 2, r.h / 2);
10897
11329
  r.tl = static_cast<std::int16_t>(std::clamp(static_cast<int>(r.tl), 0, maxRadius));
10898
11330
  r.tr = static_cast<std::int16_t>(std::clamp(static_cast<int>(r.tr), 0, maxRadius));
@@ -11713,6 +12145,7 @@ int gLastScrollUiFrame = -1000;
11713
12145
 
11714
12146
  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
12147
  {
12148
+ if (hasTextClippedBackgrounds()) return false;
11716
12149
  Tree &tree = Tree::instance();
11717
12150
  if (!state.commands || node < 0 || node >= tree.nodeCount())
11718
12151
  return false;
@@ -11884,8 +12317,8 @@ int gLastScrollUiFrame = -1000;
11884
12317
  // gate: without it a clipped-out node re-recorded a full background fill into
11885
12318
  // an empty range, which `old_len == 0 && tmp_len > 0` below (correctly) refuses
11886
12319
  // 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 &&
12320
+ const bool outsideRecordClip = !isDocumentCanvasRoot(*n) && nodeOutsideRecordClip(node, tree.mountedWidth(), tree.mountedHeight());
12321
+ if (!outsideRecordClip && n->style.visibility == 0 && !isCollapsedFlexSubtree(*n) && !ViewRenderer::backfaceSubtreeHidden(*n) &&
11889
12322
  n->style.display != 1 &&
11890
12323
  n->style.opacity != 0 &&
11891
12324
  !(n->style.blink_interval_ms > 0 && !n->style.blink_visible) &&
@@ -11947,6 +12380,7 @@ int gLastScrollUiFrame = -1000;
11947
12380
 
11948
12381
  bool DisplayList::patchScrollbarThumb(int node)
11949
12382
  {
12383
+ #if GEA_CSS_SCROLLING
11950
12384
  Tree &tree = Tree::instance();
11951
12385
  Node *nodes = tree.nodes();
11952
12386
  if (!state.commands || node < 0 || node >= tree.nodeCount())
@@ -11993,7 +12427,10 @@ int gLastScrollUiFrame = -1000;
11993
12427
  return true;
11994
12428
  }
11995
12429
  return false;
11996
- }
12430
+ #else
12431
+ (void)node; return true;
12432
+ #endif
12433
+ }
11997
12434
 
11998
12435
  void DisplayList::replayDirectDirtyRegion(int x0, int y0, int x1, int y1, int origin)
11999
12436
  {
@@ -12018,6 +12455,10 @@ int gLastScrollUiFrame = -1000;
12018
12455
 
12019
12456
  void DisplayList::replaySimpleClippedDirtyRegion(int x0, int y0, int x1, int y1, int origin)
12020
12457
  {
12458
+ const auto &tree = Tree::instance();
12459
+ const int root = tree.mountedRoot();
12460
+ if (root >= 0 && root < tree.nodeCount() && isDocumentCanvasRoot(tree.nodes()[root]))
12461
+ DisplayCommandReplayer::restoreClearBaseInRegion(x0, y0, x1, y1);
12021
12462
  DisplayCommandReplayer::replaySimpleClippedDirtyRegion(x0, y0, x1, y1, origin);
12022
12463
  }
12023
12464