@geastack/engine 0.1.21 → 0.1.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ui/render.cpp CHANGED
@@ -16,6 +16,7 @@
16
16
  #include <algorithm>
17
17
  #include <array>
18
18
  #include <cctype>
19
+ #include <climits>
19
20
  #include <cmath>
20
21
  #include <cstdint>
21
22
  #include <cstdio>
@@ -186,6 +187,31 @@ extern "C" __attribute__((weak)) int gea_current_render_core() { return 0; }
186
187
  extern "C" __attribute__((weak)) int gea_replay_stage_rows() { return 0; }
187
188
  extern "C" __attribute__((weak)) bool gea_replay_stage_begin(int, int, int, int) { return false; }
188
189
  extern "C" __attribute__((weak)) void gea_replay_stage_end() {}
190
+ // True while this core has a stage open: its draws already land in SRAM.
191
+ extern "C" __attribute__((weak)) bool gea_replay_stage_active() { return false; }
192
+ extern "C" __attribute__((weak)) int gea_bulk_replay_stage_rows() { return 0; }
193
+ // Rows a staged window `width` pixels wide may span; a display packing narrow
194
+ // windows tighter returns more than gea_bulk_replay_stage_rows().
195
+ extern "C" __attribute__((weak)) int gea_bulk_replay_stage_rows_for_width(int) { return gea_bulk_replay_stage_rows(); }
196
+ extern "C" __attribute__((weak)) void gea_replay_stage_skip_seed(bool) {}
197
+ extern "C" __attribute__((weak)) void gea_replay_stage_pipeline(bool) {}
198
+ // A replay is about to draw straight into the framebuffer (no stage open), so
199
+ // its rows sit dirty in the cache until the publish writes them back.
200
+ extern "C" __attribute__((weak)) void gea_display_note_direct_draw() {}
201
+ namespace {
202
+ // A repaint this large (a quarter of an 800x480 panel) is a burst: a tab's
203
+ // first scroll, a content swap. Raster it through the display's SRAM strips
204
+ // even where per-frame repaints go direct -- unstaged, its PSRAM writes starve
205
+ // a bounce-buffered panel's scanout refill (a visible tear). Steady per-frame
206
+ // repaints stay below it, so the per-strip list walk never taxes animation.
207
+ constexpr long kBulkStagePixels = 96000;
208
+ int replayStageRowsForArea(long area)
209
+ {
210
+ const int rows = gea_replay_stage_rows();
211
+ if (rows > 0) return rows;
212
+ return area >= kBulkStagePixels ? gea_bulk_replay_stage_rows() : 0;
213
+ }
214
+ } // namespace
189
215
 
190
216
  // Share (permille of the split region's rows) the SUBMITTING core keeps in the
191
217
  // coarse 2-core replay split. 500 = even split. A target whose render core pays a
@@ -254,6 +280,7 @@ extern "C" __attribute__((weak)) void gea_rgb565_lut_blend_const_alpha_span(std:
254
280
  namespace gea::embedded::ui
255
281
  {
256
282
 
283
+
257
284
  bool gScrollStripReplayActive = false;
258
285
  bool gScrollRanThisFrame = false;
259
286
  bool gScrollImageHoldPending = false;
@@ -544,6 +571,18 @@ int gLastScrollUiFrame = -1000;
544
571
  int commandCount = 0;
545
572
  bool imageReferences[gea::framework::graphics::kImageMax]{};
546
573
  bool textClippedBackgrounds = false;
574
+ // Scrollbar thumbs in the command stream (recorded after their
575
+ // container's children, so outside every node's command range). Direct
576
+ // dirty-region replay walks node ranges and never draws them; a frame
577
+ // whose dirty rects touch one must take the full-list path instead.
578
+ static constexpr int kMaxScrollbarBoxes = 16;
579
+ std::int16_t scrollbarBoxes[kMaxScrollbarBoxes][4]{};
580
+ // Each thumb's own command, so direct replay can repaint it.
581
+ int scrollbarCommands[kMaxScrollbarBoxes]{};
582
+ // Each thumb's scroll container, so a subtree translate can move it.
583
+ int scrollbarNodes[kMaxScrollbarBoxes]{};
584
+ int scrollbarBoxCount = 0;
585
+ bool scrollbarBoxOverflow = false;
547
586
  // Sticky: an append() was dropped because the command buffer is full.
548
587
  // Reset by clear(); lets a caller detect a truncated record.
549
588
  bool commandOverflow = false;
@@ -804,6 +843,8 @@ int gLastScrollUiFrame = -1000;
804
843
  commandsExternal = false;
805
844
  commandCount = 0;
806
845
  textClippedBackgrounds = false;
846
+ scrollbarBoxCount = 0;
847
+ scrollbarBoxOverflow = false;
807
848
  appendOverride = nullptr;
808
849
  appendOverrideCount = nullptr;
809
850
  appendOverrideCapacity = 0;
@@ -854,6 +895,11 @@ int gLastScrollUiFrame = -1000;
854
895
  if (nodeDrawEnd[i] >= old_end)
855
896
  nodeDrawEnd[i] += delta;
856
897
  }
898
+ // Scrollbar thumbs sit outside every node range but are addressed by
899
+ // command index too (scrollbarThumbsPaintLast, replayScrollbarThumbsInRegion).
900
+ for (int i = 0; i < scrollbarBoxCount; i++)
901
+ if (scrollbarCommands[i] >= old_end)
902
+ scrollbarCommands[i] += delta;
857
903
  }
858
904
  };
859
905
 
@@ -5348,9 +5394,9 @@ int gLastScrollUiFrame = -1000;
5348
5394
  // Memoize per node per refresh. The child z-sort runs at every tree level,
5349
5395
  // so an ancestor's sort recurses a subtree that a descendant's sort then
5350
5396
  // re-walks. Caching collapses those repeated O(subtree) walks to O(1).
5351
- static int memo[kMaxNodes];
5352
- static uint64_t memoSerial[kMaxNodes];
5353
- static int memoContextRoot[kMaxNodes];
5397
+ GEA_EXT_BSS static int memo[kMaxNodes];
5398
+ GEA_EXT_BSS static uint64_t memoSerial[kMaxNodes];
5399
+ GEA_EXT_BSS static int memoContextRoot[kMaxNodes];
5354
5400
  const uint64_t serial = tree.refreshSerial();
5355
5401
  if (node < kMaxNodes && memoSerial[node] == serial && memoContextRoot[node] == contextRoot)
5356
5402
  return memo[node];
@@ -7869,6 +7915,22 @@ int gLastScrollUiFrame = -1000;
7869
7915
  }
7870
7916
  };
7871
7917
 
7918
+ // The commands one region's strips replay (DisplayCommandReplayer::
7919
+ // buildRegionIndex). The list itself lives in PSRAM, and a strip-by-strip
7920
+ // replay walked all of it per 24-row strip: ~800 commands, ~1.1 ms a strip,
7921
+ // about a third of a CrowPanel chart scroll frame.
7922
+ // Sized to the list on demand (a static of kMaxCommands took 32 KB of
7923
+ // PSRAM, which the CrowPanel app could not spare).
7924
+ static std::uint16_t *gRegionIndex = nullptr;
7925
+ static int gRegionIndexCapacity = 0;
7926
+ // Per thread: the render worker replays bands on the other core while the
7927
+ // list is active, and must walk the whole list as before.
7928
+ static thread_local int gRegionIndexCount = -1;
7929
+ // The clip the list was filtered under: a replay under any clip inside it
7930
+ // may use the list, any other walks the whole list.
7931
+ static thread_local int gRegionX0, gRegionY0, gRegionX1, gRegionY1;
7932
+ static thread_local bool gRegionCovered = false;
7933
+
7872
7934
  struct DisplayCommandReplayer
7873
7935
  {
7874
7936
  static bool transformedRoundedRectPaintsRegion(const DisplayCommand &command, int x0, int y0, int x1, int y1)
@@ -7966,6 +8028,75 @@ int gLastScrollUiFrame = -1000;
7966
8028
  y1);
7967
8029
  }
7968
8030
 
8031
+ // What state.drawNodeBBox (one packed box per draw-order slot) holds. The
8032
+ // simple dirty replay keeps node layout boxes there; the region replays
8033
+ // below keep the union of each node's command boxes there instead, for one
8034
+ // replay scope (beginSimpleReplayScope..endSimpleReplayScope, inside which
8035
+ // the tree and the display list hold still). A frame takes one path or the
8036
+ // other, and whichever fills the mirror marks the other's contents stale.
8037
+ // No new memory: PSRAM on the 800x480 panels has no 10 KB to spare.
8038
+ static inline std::uint64_t gDrawNodeBBoxLayoutSerial = ~0ull; // serial whose layout boxes it holds
8039
+ static inline bool gDrawNodeBBoxCommandScope = false; // inside a replay scope
8040
+ static inline bool gDrawNodeBBoxHoldsCommandUnions = false; // filled for this scope
8041
+
8042
+ static void fillDrawNodeCommandUnions()
8043
+ {
8044
+ for (int oi = 0; oi < state.drawNodeOrderCount; oi++)
8045
+ {
8046
+ // Empty marker: x0 = INT16_MAX, x1 = INT16_MIN.
8047
+ int bx0 = 32767, by0 = 32767, bx1 = -32768, by1 = -32768;
8048
+ const int id = state.drawNodeOrder[oi];
8049
+ if (state.hasNodeScratchFor(id))
8050
+ {
8051
+ const int start = state.nodeDrawStart[id];
8052
+ const int end = state.nodeDrawEnd[id];
8053
+ if (start >= 0 && end > start && end <= state.commandCount)
8054
+ {
8055
+ bx0 = by0 = INT_MAX;
8056
+ bx1 = by1 = INT_MIN;
8057
+ for (int ci = start; ci < end; ci++)
8058
+ {
8059
+ // Same box arithmetic as commandBBoxOverlapsRegion, degenerate
8060
+ // boxes included, so the union passes every box that test passes.
8061
+ const DisplayCommand &c = state.commands[ci];
8062
+ bx0 = std::min(bx0, static_cast<int>(c.bx));
8063
+ by0 = std::min(by0, static_cast<int>(c.by));
8064
+ bx1 = std::max(bx1, c.bx + c.bw - 1);
8065
+ by1 = std::max(by1, c.by + c.bh - 1);
8066
+ }
8067
+ }
8068
+ }
8069
+ int16_t *bb = &state.drawNodeBBox[oi * 4];
8070
+ bb[0] = static_cast<int16_t>(std::clamp(bx0, -32768, 32767));
8071
+ bb[1] = static_cast<int16_t>(std::clamp(by0, -32768, 32767));
8072
+ bb[2] = static_cast<int16_t>(std::clamp(bx1, -32768, 32767));
8073
+ bb[3] = static_cast<int16_t>(std::clamp(by1, -32768, 32767));
8074
+ }
8075
+ gDrawNodeBBoxHoldsCommandUnions = true;
8076
+ gDrawNodeBBoxLayoutSerial = ~0ull;
8077
+ }
8078
+
8079
+ // Whether any of the node in draw slot `oi` has a recorded command whose box
8080
+ // reaches the region. The region loops ask it of every node in draw order,
8081
+ // once per region; with every tab page mounted that is ~400 nodes, nearly
8082
+ // all far from the region, so inside a replay scope the answer comes from
8083
+ // the packed union rather than the node's commands scattered through PSRAM.
8084
+ // A miss is exact; a hit on the union may still be a near miss, which
8085
+ // replayNodeCommandRange then turns away command by command.
8086
+ static bool GEA_RENDER_HOT_SRAM nodeCommandsReachRegion(int oi, int node_id, int x0, int y0, int x1, int y1)
8087
+ {
8088
+ if (!state.hasNodeScratchFor(node_id))
8089
+ return false;
8090
+ if (!gDrawNodeBBoxCommandScope || !state.drawNodeBBox)
8091
+ return commandRangeBBoxOverlaps(state.nodeDrawStart[node_id], state.nodeDrawEnd[node_id], x0, y0, x1, y1);
8092
+ if (!gDrawNodeBBoxHoldsCommandUnions)
8093
+ fillDrawNodeCommandUnions();
8094
+ const int16_t *bb = &state.drawNodeBBox[oi * 4];
8095
+ if (bb[0] == 32767 && bb[2] == -32768)
8096
+ return false;
8097
+ return ClipMath::rectsOverlap(bb[0], bb[1], bb[2], bb[3], x0, y0, x1, y1);
8098
+ }
8099
+
7969
8100
  static bool commandRangeOverlaps(int start, int end, int x0, int y0, int x1, int y1)
7970
8101
  {
7971
8102
  if (start < 0 || end <= start || end > state.commandCount)
@@ -8004,6 +8135,58 @@ int gLastScrollUiFrame = -1000;
8004
8135
  return false;
8005
8136
  }
8006
8137
 
8138
+ // nodeHasTransformChain for every node of one pass: each node's own state is
8139
+ // tested once and each answer reused by its descendants, where asking per node
8140
+ // walks every ancestor chain again (N x depth tests of the 20-field state).
8141
+ // Bit 0: some node on the chain to the root (self included) holds transform
8142
+ // state; bit 1: this node itself does. Valid only while the tree is unchanged.
8143
+ struct TransformChainMemo
8144
+ {
8145
+ std::vector<std::int8_t> memo;
8146
+ std::vector<int> path;
8147
+ // With no transform anywhere this frame or last (the cached whole-tree scan
8148
+ // every transform path consults), every answer is 0 without a node read.
8149
+ bool none = false;
8150
+ void reset(int nodeCount)
8151
+ {
8152
+ none = !ViewRenderer::anyTransformActive();
8153
+ if (!none)
8154
+ memo.assign(static_cast<std::size_t>(nodeCount), -1);
8155
+ }
8156
+ int at(int node_id, Node *nodes, int nodeCount)
8157
+ {
8158
+ if (none)
8159
+ return 0;
8160
+ path.clear();
8161
+ int cursor = node_id;
8162
+ int above = 0;
8163
+ while (cursor >= 0 && cursor < nodeCount)
8164
+ {
8165
+ if (memo[cursor] >= 0)
8166
+ {
8167
+ above = memo[cursor] & 1;
8168
+ break;
8169
+ }
8170
+ path.push_back(cursor);
8171
+ cursor = nodes[cursor].parent;
8172
+ }
8173
+ for (int k = static_cast<int>(path.size()) - 1; k >= 0; --k)
8174
+ {
8175
+ const int self = hasRetainedTransformState(nodes[path[k]]) ? 1 : 0;
8176
+ above |= self;
8177
+ memo[path[k]] = static_cast<std::int8_t>(above | (self << 1));
8178
+ }
8179
+ return memo[node_id];
8180
+ }
8181
+ bool retained(int node_id, Node *nodes, int nodeCount) { return (at(node_id, nodes, nodeCount) & 2) != 0; }
8182
+ bool chain(int node_id, Node *nodes, int nodeCount) { return (at(node_id, nodes, nodeCount) & 1) != 0; }
8183
+ };
8184
+ static TransformChainMemo &transformChainMemo()
8185
+ {
8186
+ static TransformChainMemo memo;
8187
+ return memo;
8188
+ }
8189
+
8007
8190
  static bool nodeRecordsOverflowClip(int node_id, Node *nodes, int nodeCount)
8008
8191
  {
8009
8192
  if (node_id < 0 || node_id >= nodeCount)
@@ -8064,6 +8247,93 @@ int gLastScrollUiFrame = -1000;
8064
8247
  return pushed;
8065
8248
  }
8066
8249
 
8250
+ // The ancestor chain of the node replayNodeCommandRange last resolved, root
8251
+ // first, with what pushNodeReplayClips and effectiveAlphaForNode read at
8252
+ // each level. Those walk every ancestor's style for each node replayed, and
8253
+ // draw order is depth-first, so the next node's chain is almost all this
8254
+ // one: only the levels below the shared ancestor are read again. The tree
8255
+ // holds still inside a replay scope, the only place this is used (it is
8256
+ // emptied when one opens); the answers match the two walks exactly.
8257
+ struct ReplayChainMemo
8258
+ {
8259
+ int depth; // zero-initialized (static storage)
8260
+ int id[kScratchDepth];
8261
+ std::uint8_t alpha[kScratchDepth]; // opacity product, root..level, as effectiveAlphaForNode folds it
8262
+ bool fixed[kScratchDepth]; // LayoutEngine::isViewportFixed
8263
+ bool clips[kScratchDepth]; // nodeRecordsOverflowClip
8264
+ int clip[kScratchDepth][4]; // its overflowClipBounds
8265
+ };
8266
+ static inline ReplayChainMemo gReplayChain;
8267
+
8268
+ // Brings gReplayChain to `node_id`'s chain. False when the chain is deeper
8269
+ // than the walks allow (they give up there); the caller then uses them.
8270
+ static bool GEA_RENDER_HOT_SRAM resolveReplayChain(int node_id, Node *nodes, int nodeCount)
8271
+ {
8272
+ auto &m = gReplayChain;
8273
+ int below[kScratchDepth]; // node_id and the ancestors not in the memo, leaf first
8274
+ int belowCount = 0;
8275
+ int keep = 0; // memo levels that stay: the shared ancestor's chain
8276
+ for (int cursor = node_id; cursor >= 0 && cursor < nodeCount; cursor = nodes[cursor].parent)
8277
+ {
8278
+ int level = -1;
8279
+ for (int k = m.depth - 1; k >= 0; --k)
8280
+ if (m.id[k] == cursor)
8281
+ {
8282
+ level = k;
8283
+ break;
8284
+ }
8285
+ if (level >= 0)
8286
+ {
8287
+ keep = level + 1;
8288
+ break;
8289
+ }
8290
+ if (belowCount >= kScratchDepth)
8291
+ return false;
8292
+ below[belowCount++] = cursor;
8293
+ }
8294
+ if (keep + belowCount > kScratchDepth)
8295
+ return false;
8296
+ m.depth = keep;
8297
+ for (int i = belowCount - 1; i >= 0; --i)
8298
+ {
8299
+ const int id = below[i];
8300
+ const int k = m.depth++;
8301
+ const int parentAlpha = k > 0 ? m.alpha[k - 1] : 255;
8302
+ m.id[k] = id;
8303
+ m.alpha[k] = static_cast<std::uint8_t>((parentAlpha * static_cast<int>(nodes[id].computedStyle().opacity)) / 255);
8304
+ m.fixed[k] = LayoutEngine::isViewportFixed(nodes[id]);
8305
+ m.clips[k] = nodeRecordsOverflowClip(id, nodes, nodeCount);
8306
+ if (m.clips[k])
8307
+ {
8308
+ overflowClipBounds(nodes[id], m.clip[k][0], m.clip[k][1], m.clip[k][2], m.clip[k][3]);
8309
+ }
8310
+ }
8311
+ return m.depth > 0;
8312
+ }
8313
+
8314
+ // pushNodeReplayClips from the resolved chain: the clipping ancestors from
8315
+ // the nearest viewport-fixed level (the node's own included) down to its parent.
8316
+ static int GEA_RENDER_HOT_SRAM pushReplayChainClips()
8317
+ {
8318
+ const auto &m = gReplayChain;
8319
+ int first = 0;
8320
+ for (int k = m.depth - 1; k >= 0; --k)
8321
+ if (m.fixed[k])
8322
+ {
8323
+ first = k;
8324
+ break;
8325
+ }
8326
+ int pushed = 0;
8327
+ for (int k = first; k < m.depth - 1; ++k)
8328
+ {
8329
+ if (!m.clips[k])
8330
+ continue;
8331
+ gea::platform::display::Display::pushClip(m.clip[k][0], m.clip[k][1], m.clip[k][2], m.clip[k][3]);
8332
+ pushed++;
8333
+ }
8334
+ return pushed;
8335
+ }
8336
+
8067
8337
  static void replayCommandRange(int start, int end, int x0, int y0, int x1, int y1, UniformRoundedRectBatch &roundedRects)
8068
8338
  {
8069
8339
  if (start < 0 || end <= start || end > state.commandCount)
@@ -8139,11 +8409,13 @@ int gLastScrollUiFrame = -1000;
8139
8409
  // content-only frames (markNodeDisplayCommandsDirty patches a node's commands
8140
8410
  // without a full re-record), so a moved absolute leaf would carry a stale bbox
8141
8411
  // here and get wrongly AABB-skipped in the chunk it actually occupies.
8142
- static std::uint64_t bboxRebuildSerial = ~0ull;
8412
+ // The region replays may have left command unions in the mirror
8413
+ // (gDrawNodeBBoxLayoutSerial says whose boxes it holds).
8143
8414
  const std::uint64_t serial = tree.refreshSerial();
8144
- if (bboxRebuildSerial == serial)
8415
+ if (gDrawNodeBBoxLayoutSerial == serial)
8145
8416
  return;
8146
- bboxRebuildSerial = serial;
8417
+ gDrawNodeBBoxLayoutSerial = serial;
8418
+ gDrawNodeBBoxHoldsCommandUnions = false;
8147
8419
  for (int oi = 0; oi < state.drawNodeOrderCount; oi++)
8148
8420
  {
8149
8421
  const int id = state.drawNodeOrder[oi];
@@ -8204,7 +8476,151 @@ int gLastScrollUiFrame = -1000;
8204
8476
  }
8205
8477
  };
8206
8478
 
8479
+ // The simple-replay verdict walks every node and every command, and the
8480
+ // unified flush asks it once per dirty region: on a busy screen (Analytics,
8481
+ // 238 nodes, 8 regions) that walk was ~5 ms a region, most of the frame's
8482
+ // replay. Only the transformed-ink test depends on the region, so inside a
8483
+ // replay scope -- the display list and tree held still -- the rest is
8484
+ // computed once and the region test runs over the transformed nodes alone.
8485
+ struct SimpleReplayVerdictCache
8486
+ {
8487
+ static constexpr int kMaxTransformed = 64;
8488
+ int scopeDepth = 0;
8489
+ bool valid = false;
8490
+ int width = 0;
8491
+ int height = 0;
8492
+ bool baseOk = false; // every region-independent check passed
8493
+ bool anyRetainedTransform = false; // fails a region-less query
8494
+ bool transformOverflow = false; // too many transformed nodes to list
8495
+ bool transformMissingScratch = false;
8496
+ int transformedCount = 0;
8497
+ int transformed[kMaxTransformed];
8498
+ };
8499
+ static SimpleReplayVerdictCache &simpleReplayVerdictCache()
8500
+ {
8501
+ static SimpleReplayVerdictCache cache;
8502
+ return cache;
8503
+ }
8504
+
8505
+ static void beginSimpleReplayScope()
8506
+ {
8507
+ auto &cache = simpleReplayVerdictCache();
8508
+ if (cache.scopeDepth++ == 0)
8509
+ {
8510
+ cache.valid = false;
8511
+ gDrawNodeBBoxCommandScope = true;
8512
+ gDrawNodeBBoxHoldsCommandUnions = false;
8513
+ gReplayChain.depth = 0;
8514
+ }
8515
+ }
8516
+
8517
+ static void endSimpleReplayScope()
8518
+ {
8519
+ auto &cache = simpleReplayVerdictCache();
8520
+ if (cache.scopeDepth > 0 && --cache.scopeDepth == 0)
8521
+ {
8522
+ cache.valid = false;
8523
+ gDrawNodeBBoxCommandScope = false;
8524
+ gDrawNodeBBoxHoldsCommandUnions = false;
8525
+ gReplayChain.depth = 0;
8526
+ }
8527
+ }
8528
+
8207
8529
  static bool canUseSimpleDirtyReplay(int width, int height, const DisplayReplayRegion *regions = nullptr, int count = 0)
8530
+ {
8531
+ auto &cache = simpleReplayVerdictCache();
8532
+ if (cache.scopeDepth <= 0)
8533
+ return canUseSimpleDirtyReplayUncached(width, height, regions, count);
8534
+ if (!cache.valid || cache.width != width || cache.height != height)
8535
+ fillSimpleReplayVerdictCache(width, height);
8536
+ if (cache.transformOverflow)
8537
+ return canUseSimpleDirtyReplayUncached(width, height, regions, count);
8538
+ if (!cache.baseOk)
8539
+ return false;
8540
+ if (cache.transformedCount == 0)
8541
+ return true;
8542
+ // The uncached walk: any retained transform fails a region-less query;
8543
+ // a transform chain only counts once regions are given.
8544
+ if (!regions || count <= 0)
8545
+ return !cache.anyRetainedTransform;
8546
+ if (cache.transformMissingScratch)
8547
+ return false;
8548
+ for (int t = 0; t < cache.transformedCount; ++t)
8549
+ {
8550
+ const int i = cache.transformed[t];
8551
+ for (int ri = 0; ri < count; ++ri)
8552
+ if (commandRangeBBoxOverlaps(state.nodeDrawStart[i], state.nodeDrawEnd[i],
8553
+ regions[ri].x0, regions[ri].y0, regions[ri].x1, regions[ri].y1))
8554
+ return false;
8555
+ }
8556
+ return true;
8557
+ }
8558
+
8559
+ // Region-independent half of canUseSimpleDirtyReplayUncached, plus the list
8560
+ // of nodes its transformed-ink test would visit.
8561
+ static void fillSimpleReplayVerdictCache(int width, int height)
8562
+ {
8563
+ auto &cache = simpleReplayVerdictCache();
8564
+ cache.valid = true;
8565
+ cache.width = width;
8566
+ cache.height = height;
8567
+ cache.baseOk = false;
8568
+ cache.anyRetainedTransform = false;
8569
+ cache.transformOverflow = false;
8570
+ cache.transformMissingScratch = false;
8571
+ cache.transformedCount = 0;
8572
+ if (DisplayList::instance().hasTextClippedBackgrounds())
8573
+ return;
8574
+ Tree &tree = Tree::instance();
8575
+ Node *nodes = tree.nodes();
8576
+ const int nodeCount = tree.nodeCount();
8577
+ auto &chains = transformChainMemo();
8578
+ chains.reset(nodeCount);
8579
+ for (int i = 0; i < nodeCount; i++)
8580
+ {
8581
+ const Node &node = nodes[i];
8582
+ if ((GEA_CSS_FILTERS ? rstyle(node.computedStyle()).filter_blur_radius : 0) > 0)
8583
+ return;
8584
+ if (node.computedStyle().opacity != 255 && node.computedStyle().opacity != 0)
8585
+ if (!simpleTextAlphaNode(i, nodes, nodeCount))
8586
+ return;
8587
+ if (node.computedStyle().mask_right_fade_width > 0)
8588
+ return;
8589
+ const bool retained = chains.retained(i, nodes, nodeCount);
8590
+ if (chains.chain(i, nodes, nodeCount))
8591
+ {
8592
+ if (retained)
8593
+ cache.anyRetainedTransform = true;
8594
+ if (!state.hasNodeScratchFor(i))
8595
+ cache.transformMissingScratch = true;
8596
+ else if (cache.transformedCount < SimpleReplayVerdictCache::kMaxTransformed)
8597
+ cache.transformed[cache.transformedCount++] = i;
8598
+ else
8599
+ cache.transformOverflow = true;
8600
+ }
8601
+ }
8602
+ // A transformed node without scratch fails every query, region-less ones
8603
+ // included (it is listed so the region-less path sees a transform).
8604
+ if (cache.transformMissingScratch && cache.transformedCount == 0)
8605
+ cache.transformed[cache.transformedCount++] = 0;
8606
+ for (int ci = 0; ci < state.commandCount; ci++)
8607
+ {
8608
+ const DisplayCommand &command = state.commands[ci];
8609
+ const bool plainTextAlpha = command.type == DisplayCommandType::SetAlpha &&
8610
+ command.alpha.recordParentAlpha == 255 &&
8611
+ simpleTextAlphaNode(command.alpha.nodeId, nodes, nodeCount);
8612
+ if ((command.type == DisplayCommandType::SetAlpha && command.alpha.alpha != 255 && !plainTextAlpha) ||
8613
+ command.type == DisplayCommandType::BeginFilterBlur ||
8614
+ command.type == DisplayCommandType::ApplyFilterBlur)
8615
+ return;
8616
+ if (command.type == DisplayCommandType::PushClip &&
8617
+ !clipCoversViewport(command, width, height))
8618
+ return;
8619
+ }
8620
+ cache.baseOk = true;
8621
+ }
8622
+
8623
+ static bool canUseSimpleDirtyReplayUncached(int width, int height, const DisplayReplayRegion *regions, int count)
8208
8624
  {
8209
8625
  if (DisplayList::instance().hasTextClippedBackgrounds()) return false;
8210
8626
  #if GEA_EMBEDDED_SIMPLE_REPLAY_DEBUG
@@ -8217,6 +8633,8 @@ int gLastScrollUiFrame = -1000;
8217
8633
  Tree &tree = Tree::instance();
8218
8634
  Node *nodes = tree.nodes();
8219
8635
  const int nodeCount = tree.nodeCount();
8636
+ auto &chains = transformChainMemo();
8637
+ chains.reset(nodeCount);
8220
8638
  for (int i = 0; i < nodeCount; i++)
8221
8639
  {
8222
8640
  const Node &node = nodes[i];
@@ -8227,7 +8645,7 @@ int gLastScrollUiFrame = -1000;
8227
8645
  { SRDBG("opacity node", i); return false; }
8228
8646
  if (node.computedStyle().mask_right_fade_width > 0)
8229
8647
  { SRDBG("mask node", i); return false; }
8230
- if (hasRetainedTransformState(node) || (regions && nodeHasTransformChain(i, nodes, nodeCount)))
8648
+ if (chains.retained(i, nodes, nodeCount) || (regions && chains.chain(i, nodes, nodeCount)))
8231
8649
  {
8232
8650
  if (!regions || count <= 0 || !state.hasNodeScratchFor(i))
8233
8651
  { SRDBG("transform node", i); return false; }
@@ -8635,19 +9053,37 @@ int gLastScrollUiFrame = -1000;
8635
9053
  const int nodeCount = tree.nodeCount();
8636
9054
  if (node_id < 0 || node_id >= nodeCount)
8637
9055
  return;
8638
- if (retainedBackgroundNodeCanBeSkippedInRegion(node_id, x0, y0, x1, y1))
8639
- return;
8640
9056
  if (!state.hasNodeScratchFor(node_id))
8641
9057
  return;
8642
9058
  const int start = state.nodeDrawStart[node_id];
8643
9059
  const int end = state.nodeDrawEnd[node_id];
8644
9060
  if (start < 0 || end <= start || end > state.commandCount)
8645
9061
  return;
8646
-
8647
- const int pushedClips = pushNodeReplayClips(node_id, nodes, nodeCount);
8648
- if (pushedClips < 0)
9062
+ // A node none of whose commands reach the region paints nothing in it
9063
+ // (the loop below filters every draw by the same box), so skip its
9064
+ // ancestor clip and opacity walks too, as the multi-region path does.
9065
+ // With every tab page mounted the draw order holds ~400 nodes, and
9066
+ // those walks for each of them in each of a frame's ~7 small regions
9067
+ // were most of the CrowPanel Analytics meters' replay time.
9068
+ if (!commandRangeBBoxOverlaps(start, end, x0, y0, x1, y1))
8649
9069
  return;
8650
- const std::uint8_t alpha = effectiveAlphaForNode(node_id, nodes, nodeCount);
9070
+ if (retainedBackgroundNodeCanBeSkippedInRegion(node_id, x0, y0, x1, y1))
9071
+ return;
9072
+
9073
+ int pushedClips;
9074
+ std::uint8_t alpha;
9075
+ if (gDrawNodeBBoxCommandScope && resolveReplayChain(node_id, nodes, nodeCount))
9076
+ {
9077
+ pushedClips = pushReplayChainClips();
9078
+ alpha = gReplayChain.alpha[gReplayChain.depth - 1];
9079
+ }
9080
+ else
9081
+ {
9082
+ pushedClips = pushNodeReplayClips(node_id, nodes, nodeCount);
9083
+ if (pushedClips < 0)
9084
+ return;
9085
+ alpha = effectiveAlphaForNode(node_id, nodes, nodeCount);
9086
+ }
8651
9087
  const bool stateChanged = pushedClips > 0 || alpha != 255;
8652
9088
  if (stateChanged)
8653
9089
  {
@@ -8764,6 +9200,12 @@ int gLastScrollUiFrame = -1000;
8764
9200
  int node_id = state.drawNodeOrder[oi];
8765
9201
  if (node_id < 0 || node_id >= tree.nodeCount())
8766
9202
  continue;
9203
+ // The command boxes first: they are what replayNodeCommandRange filters
9204
+ // by, ancestors included (one that reaches no pixel of the region paints
9205
+ // none), and most of the ~400 nodes reach none, so they are turned away
9206
+ // before their Node is read.
9207
+ if (!nodeCommandsReachRegion(oi, node_id, x0, y0, x1, y1))
9208
+ continue;
8767
9209
  Node *n = &nodes[node_id];
8768
9210
  if (n->computedStyle().display == 1)
8769
9211
  continue;
@@ -8778,11 +9220,6 @@ int gLastScrollUiFrame = -1000;
8778
9220
  !nodeOrAncestorRenderDirty(node_id, nodes, tree.nodeCount()))
8779
9221
  continue;
8780
9222
 
8781
- const int start = state.nodeDrawStart[node_id];
8782
- const int end = state.nodeDrawEnd[node_id];
8783
- const bool isAncestor = nodeInList(node_id, ancestors, ancestorCount);
8784
- if (!isAncestor && !commandRangeBBoxOverlaps(start, end, x0, y0, x1, y1))
8785
- continue;
8786
9223
  replayNodeCommandRange(node_id, x0, y0, x1, y1, roundedRects);
8787
9224
  }
8788
9225
  roundedRects.flush();
@@ -8813,6 +9250,12 @@ int gLastScrollUiFrame = -1000;
8813
9250
  const int savedH = canvas->height();
8814
9251
  const int savedStride = canvas->strideBytes() / static_cast<int>(sizeof(gea::framework::graphics::pixel::native_t));
8815
9252
  const std::uint8_t savedAlpha = gea::platform::display::Display::alpha();
9253
+ // bindPixels clears the scroll region; a ring-scrolling display keeps its
9254
+ // framebuffer rows rotated across frames, so drawing on after the bake
9255
+ // without the region would land in the wrong rows.
9256
+ const int savedRegionY = canvas->scrollRegionY();
9257
+ const int savedRegionH = canvas->scrollRegionH();
9258
+ const int savedRegionOffset = canvas->scrollOffsetY();
8816
9259
 
8817
9260
  gBackdropBaking = true;
8818
9261
  canvas->bindPixels(bg, width, height, width);
@@ -8849,6 +9292,8 @@ int gLastScrollUiFrame = -1000;
8849
9292
  gea::platform::display::Display::popClip();
8850
9293
 
8851
9294
  canvas->bindPixels(savedPixels, savedW, savedH, savedStride);
9295
+ if (savedRegionH > 0)
9296
+ canvas->setScrollRegion(savedRegionY, savedRegionH, savedRegionOffset);
8852
9297
  canvas->resetDirty();
8853
9298
  gea::platform::display::Display::resetClip();
8854
9299
  gea::platform::display::Display::setAlpha(savedAlpha);
@@ -8871,6 +9316,11 @@ int gLastScrollUiFrame = -1000;
8871
9316
  constexpr int kMinPixelsToSplitFrame = GEA_EMBEDDED_RENDER_PARALLEL_MIN_PIXELS;
8872
9317
  const int regionW = x1 - x0 + 1;
8873
9318
  const int regionH = y1 - y0 + 1;
9319
+ // Staging runs on this core's strip; a band handed to the other core
9320
+ // could not stage, so a bulk-staged region is not split.
9321
+ const int stageRows = gea_replay_stage_active() ? 0 : replayStageRowsForArea(static_cast<long>(regionW) * regionH);
9322
+ if (stageRows > 0 && gea_replay_stage_rows() <= 0)
9323
+ allowSplit = false;
8874
9324
  if (GEA_EMBEDDED_RENDER_PARALLEL_DIRTY_REPLAY && GEA_RENDER_SPLIT_ALLOWED && allowSplit && regionW > 0 && regionH >= kMinRowsToSplitFrame &&
8875
9325
  regionW * regionH >= kMinPixelsToSplitFrame)
8876
9326
  {
@@ -8907,7 +9357,6 @@ int gLastScrollUiFrame = -1000;
8907
9357
  // byte-identical to the direct path for any scene. Composes with the 2-core
8908
9358
  // band split above: each band re-enters here with allowSplit=false and
8909
9359
  // strip-stages its own rows on its own core.
8910
- const int stageRows = gea_replay_stage_rows();
8911
9360
  if (stageRows > 0)
8912
9361
  {
8913
9362
  for (int sy0 = y0; sy0 <= y1; sy0 += stageRows)
@@ -9043,6 +9492,10 @@ int gLastScrollUiFrame = -1000;
9043
9492
  continue;
9044
9493
  if (!state.hasNodeScratchFor(node_id))
9045
9494
  continue;
9495
+ // A node whose commands reach none of the region covers none of it;
9496
+ // inside a replay scope the packed union says so without the command.
9497
+ if (gDrawNodeBBoxCommandScope && !nodeCommandsReachRegion(oi, node_id, x0, y0, x1, y1))
9498
+ continue;
9046
9499
  const int start = state.nodeDrawStart[node_id];
9047
9500
  const int end = state.nodeDrawEnd[node_id];
9048
9501
  if (start < 0 || end <= start || end > state.commandCount)
@@ -9067,6 +9520,18 @@ int gLastScrollUiFrame = -1000;
9067
9520
  spanX1 = c.blit.dx + c.blit.sourceWidth - 1;
9068
9521
  }
9069
9522
  }
9523
+ // A rounded card covers a region that lies inside its corners (one
9524
+ // pixel of slack: an edge pixel can still be antialiased), the same
9525
+ // rule regionHasOpaqueBase uses. Without it every dirty region on a
9526
+ // card replayed the page backgrounds underneath it as well.
9527
+ else if (c.type == DisplayCommandType::FillRoundedRect)
9528
+ {
9529
+ if (regionInsideRetainedBackground(c, x0 - 1, y0 - 1, x1 + 1, y1 + 1))
9530
+ {
9531
+ spanX0 = x0;
9532
+ spanX1 = x1;
9533
+ }
9534
+ }
9070
9535
  if (spanX0 < x0)
9071
9536
  spanX0 = x0;
9072
9537
  if (spanX1 > x1)
@@ -9168,6 +9633,8 @@ int gLastScrollUiFrame = -1000;
9168
9633
  int node_id = state.drawNodeOrder[oi];
9169
9634
  if (node_id < 0 || node_id >= tree.nodeCount())
9170
9635
  continue;
9636
+ if (!nodeCommandsReachRegion(oi, node_id, x0, y0, x1, y1)) // before the Node is read
9637
+ continue;
9171
9638
  Node *n = &nodes[node_id];
9172
9639
  if (n->computedStyle().display == 1)
9173
9640
  continue;
@@ -9387,7 +9854,7 @@ int gLastScrollUiFrame = -1000;
9387
9854
  gea::platform::display::Display::setAlpha(255);
9388
9855
  }
9389
9856
 
9390
- static void GEA_RENDER_HOT_SRAM replayDirectDirtyRegions(const DisplayReplayRegion *regions, int count, bool allowSplit = true)
9857
+ static void GEA_RENDER_HOT_SRAM replayDirectDirtyRegions(const DisplayReplayRegion *regions, int count, bool allowSplit = true, bool allowStage = true)
9391
9858
  {
9392
9859
  GEA_REFRESH_PERF(refreshPerfStatsMutable().treeReplayDirectRegionsCalls++);
9393
9860
  if (!regions || count <= 0)
@@ -9409,6 +9876,74 @@ int gLastScrollUiFrame = -1000;
9409
9876
  regions[0].origin);
9410
9877
  return;
9411
9878
  }
9879
+ // SRAM-staged strips, as replayDirectDirtyRegion does for one region:
9880
+ // several regions (a scroll's exposed band plus ticking content) otherwise
9881
+ // draw straight into a framebuffer the panel is scanning, and their fills
9882
+ // at full speed, from both cores, starved the RGB panel's bounce refills.
9883
+ // Each strip stages the bbox of the regions clipped to it and replays them
9884
+ // there -- the same per-band clipping the 2-core split below applies, so
9885
+ // the pixels are the same. Inside a strip the stage is active, so the
9886
+ // nested calls draw into it.
9887
+ constexpr int kMaxStagedRegions = 32;
9888
+ long stageArea = 0;
9889
+ for (int i = 0; i < count; i++)
9890
+ if (regions[i].x0 <= regions[i].x1 && regions[i].y0 <= regions[i].y1)
9891
+ stageArea += static_cast<long>(regions[i].x1 - regions[i].x0 + 1) * (regions[i].y1 - regions[i].y0 + 1);
9892
+ const int stageRows = replayStageRowsForArea(stageArea);
9893
+ if (allowStage && stageRows > 0 && count <= kMaxStagedRegions && !gea_replay_stage_active())
9894
+ {
9895
+ int minY = 0x7fffffff, maxY = -0x7fffffff;
9896
+ for (int i = 0; i < count; i++)
9897
+ {
9898
+ if (regions[i].x0 > regions[i].x1 || regions[i].y0 > regions[i].y1)
9899
+ continue;
9900
+ minY = std::min(minY, regions[i].y0);
9901
+ maxY = std::max(maxY, regions[i].y1);
9902
+ }
9903
+ for (int sy0 = minY; sy0 <= maxY; sy0 += stageRows)
9904
+ {
9905
+ const int sy1 = std::min(maxY, sy0 + stageRows - 1);
9906
+ DisplayReplayRegion strip[kMaxStagedRegions];
9907
+ int n = 0;
9908
+ int bx0 = 0x7fffffff, by0 = 0x7fffffff, bx1 = -0x7fffffff, by1 = -0x7fffffff;
9909
+ for (int i = 0; i < count; i++)
9910
+ {
9911
+ DisplayReplayRegion r = regions[i];
9912
+ if (r.x0 > r.x1)
9913
+ continue;
9914
+ r.y0 = std::max(r.y0, sy0);
9915
+ r.y1 = std::min(r.y1, sy1);
9916
+ if (r.y0 > r.y1)
9917
+ continue;
9918
+ strip[n++] = r;
9919
+ bx0 = std::min(bx0, r.x0);
9920
+ by0 = std::min(by0, r.y0);
9921
+ bx1 = std::max(bx1, r.x1);
9922
+ by1 = std::max(by1, r.y1);
9923
+ }
9924
+ if (n == 0)
9925
+ continue;
9926
+ if (!gea_replay_stage_begin(bx0, by0, bx1, by1))
9927
+ {
9928
+ // No stage: the rest goes straight to the framebuffer, as before.
9929
+ DisplayReplayRegion rest[kMaxStagedRegions];
9930
+ int m = 0;
9931
+ for (int i = 0; i < count; i++)
9932
+ {
9933
+ DisplayReplayRegion r = regions[i];
9934
+ r.y0 = std::max(r.y0, sy0);
9935
+ if (r.x0 <= r.x1 && r.y0 <= r.y1)
9936
+ rest[m++] = r;
9937
+ }
9938
+ if (m > 0)
9939
+ replayDirectDirtyRegions(rest, m, /*allowSplit=*/false, /*allowStage=*/false);
9940
+ return;
9941
+ }
9942
+ replayDirectDirtyRegions(strip, n, /*allowSplit=*/false, /*allowStage=*/false);
9943
+ gea_replay_stage_end();
9944
+ }
9945
+ return;
9946
+ }
9412
9947
  constexpr int kMaxRegionSplit = 32;
9413
9948
  if (count <= kMaxRegionSplit)
9414
9949
  {
@@ -9552,22 +10087,11 @@ int gLastScrollUiFrame = -1000;
9552
10087
  roundedRects.flush();
9553
10088
  }
9554
10089
 
9555
- static void GEA_RENDER_HOT_SRAM replay()
10090
+ // Occlusion-cull start for the clip now in force (see replay()).
10091
+ static int GEA_RENDER_HOT_SRAM cullStartIndex()
9556
10092
  {
9557
- int cx0 = 0, cy0 = 0, cx1 = 0, cy1 = 0;
9558
- int clip_dirty = 1;
9559
- int clip_stack_depth = 0;
9560
- UniformRoundedRectBatch roundedRects;
9561
-
9562
- // Occlusion cull against the CALLER's clip (the conservative tree_render
9563
- // path pushes each dirty rect around a full replay): start at the last
9564
- // opaque full-cover fill's first command. Unlike the node walks, this
9565
- // stream carries clip state INLINE, and an orphan PopClip at depth 0
9566
- // pops the caller's region clip — so only cull when the cover has no
9567
- // clipping ancestors (whose Pop would run unmatched), the skipped
9568
- // prefix is Push/Pop balanced, and no filter-blur pair could straddle
9569
- // the cut.
9570
10093
  int startCi = 0;
10094
+ cullCovered = false;
9571
10095
  {
9572
10096
  int rx0 = 0, ry0 = 0, rx1 = -1, ry1 = -1;
9573
10097
  gea::platform::display::Display::clip(&rx0, &ry0, &rx1, &ry1);
@@ -9593,6 +10117,7 @@ int gLastScrollUiFrame = -1000;
9593
10117
  if (depth >= kCullClipDepth)
9594
10118
  {
9595
10119
  startCi = 0;
10120
+ cullCovered = false;
9596
10121
  break;
9597
10122
  }
9598
10123
  clipX0[depth] = curX0;
@@ -9610,6 +10135,7 @@ int gLastScrollUiFrame = -1000;
9610
10135
  if (--depth < 0)
9611
10136
  {
9612
10137
  startCi = 0;
10138
+ cullCovered = false;
9613
10139
  break;
9614
10140
  }
9615
10141
  curX0 = clipX0[depth];
@@ -9625,6 +10151,7 @@ int gLastScrollUiFrame = -1000;
9625
10151
  c.type == DisplayCommandType::ApplyFilterBlur)
9626
10152
  {
9627
10153
  startCi = 0;
10154
+ cullCovered = false;
9628
10155
  break;
9629
10156
  }
9630
10157
  else if (c.type == DisplayCommandType::FillRect && alpha == 255 &&
@@ -9635,13 +10162,282 @@ int gLastScrollUiFrame = -1000;
9635
10162
  c.bx + c.bw - 1 >= rx1 && c.by + c.bh - 1 >= ry1)
9636
10163
  {
9637
10164
  startCi = i;
10165
+ cullCovered = true;
9638
10166
  }
9639
10167
  }
9640
10168
  }
9641
10169
  }
9642
10170
 
9643
- for (int i = 0; i < state.commandCount; i++)
10171
+ return startCi;
10172
+ }
10173
+
10174
+ // One walk under the clip now in force keeps the clip/alpha commands and
10175
+ // the draws that can land inside it, drops clip scopes that miss it whole,
10176
+ // and finds the occlusion cull start as cullStartIndex() does (a cover
10177
+ // drops the draws kept before it). False (nothing filtered) on a filter
10178
+ // blur, whose backdrop capture depends on draws outside the clip.
10179
+ static bool buildRegionIndex()
10180
+ {
10181
+ gRegionIndexCount = -1;
10182
+ cullCovered = false;
10183
+ int rx0 = 0, ry0 = 0, rx1 = -1, ry1 = -1;
10184
+ gea::platform::display::Display::clip(&rx0, &ry0, &rx1, &ry1);
10185
+ if (rx1 < rx0 || ry1 < ry0) return false;
10186
+ gRegionX0 = rx0; gRegionY0 = ry0; gRegionX1 = rx1; gRegionY1 = ry1;
10187
+ if (gRegionIndexCapacity < state.commandCount)
10188
+ {
10189
+ const int capacity = (state.commandCount + 511) & ~511;
10190
+ void *grown = gea::framework::memory::Allocator::reallocatePreferSpiram(gRegionIndex, sizeof(std::uint16_t) * capacity);
10191
+ if (!grown) return false;
10192
+ gRegionIndex = static_cast<std::uint16_t *>(grown);
10193
+ gRegionIndexCapacity = capacity;
10194
+ }
10195
+ constexpr int kDepth = 64;
10196
+ int clipX0[kDepth], clipY0[kDepth], clipX1[kDepth], clipY1[kDepth], pushCmd[kDepth];
10197
+ int depth = 0, skip = 0, alpha = 255, n = 0, startCi = 0;
10198
+ int pendingAlpha = -1, emittedAlpha = -1;
10199
+ int curX0 = rx0, curY0 = ry0, curX1 = rx1, curY1 = ry1;
10200
+ for (int i = 0; i < state.commandCount; ++i)
9644
10201
  {
10202
+ const DisplayCommand &c = state.commands[i];
10203
+ switch (c.type)
10204
+ {
10205
+ case DisplayCommandType::PushClip:
10206
+ {
10207
+ if (skip) { skip++; continue; }
10208
+ const int nx0 = std::max(curX0, static_cast<int>(c.clip.x));
10209
+ const int ny0 = std::max(curY0, static_cast<int>(c.clip.y));
10210
+ const int nx1 = std::min(curX1, static_cast<int>(c.clip.x) + static_cast<int>(c.clip.w) - 1);
10211
+ const int ny1 = std::min(curY1, static_cast<int>(c.clip.y) + static_cast<int>(c.clip.h) - 1);
10212
+ if (nx0 > nx1 || ny0 > ny1) { skip = 1; continue; } // nothing in the scope can show
10213
+ if (depth >= kDepth) return false;
10214
+ clipX0[depth] = curX0; clipY0[depth] = curY0; clipX1[depth] = curX1; clipY1[depth] = curY1;
10215
+ pushCmd[depth] = i;
10216
+ depth++;
10217
+ curX0 = nx0; curY0 = ny0; curX1 = nx1; curY1 = ny1;
10218
+ gRegionIndex[n++] = static_cast<std::uint16_t>(i);
10219
+ continue;
10220
+ }
10221
+ case DisplayCommandType::PopClip:
10222
+ if (skip) { skip--; continue; }
10223
+ if (depth > 0)
10224
+ {
10225
+ depth--;
10226
+ curX0 = clipX0[depth]; curY0 = clipY0[depth]; curX1 = clipX1[depth]; curY1 = clipY1[depth];
10227
+ // A scope that kept no draw is dropped whole (most clip scopes
10228
+ // overlap the region yet draw nothing inside it).
10229
+ if (n > 0 && gRegionIndex[n - 1] == pushCmd[depth]) { n--; continue; }
10230
+ }
10231
+ gRegionIndex[n++] = static_cast<std::uint16_t>(i); // at depth 0 it pops the caller's clip
10232
+ continue;
10233
+ case DisplayCommandType::SetAlpha:
10234
+ // Alpha is one value, not a stack: only the last one before each
10235
+ // kept draw matters (most scopes hold no draw in the region).
10236
+ alpha = c.alpha.alpha;
10237
+ pendingAlpha = i;
10238
+ continue;
10239
+ case DisplayCommandType::BeginFilterBlur:
10240
+ case DisplayCommandType::ApplyFilterBlur:
10241
+ cullCovered = false;
10242
+ return false;
10243
+ default:
10244
+ break;
10245
+ }
10246
+ if (skip) continue;
10247
+ if (c.bx > curX1 || c.by > curY1 || c.bx + c.bw - 1 < curX0 || c.by + c.bh - 1 < curY0) continue;
10248
+ if (c.type == DisplayCommandType::FillRect && alpha == 255 &&
10249
+ curX0 <= rx0 && curY0 <= ry0 && curX1 >= rx1 && curY1 >= ry1 &&
10250
+ c.fill.x <= rx0 && c.fill.y <= ry0 &&
10251
+ c.fill.x + c.fill.w - 1 >= rx1 && c.fill.y + c.fill.h - 1 >= ry1 &&
10252
+ c.bx <= rx0 && c.by <= ry0 && c.bx + c.bw - 1 >= rx1 && c.by + c.bh - 1 >= ry1)
10253
+ {
10254
+ // Everything drawn so far is hidden under this fill: keep only the
10255
+ // clip/alpha state kept before it.
10256
+ int kept = 0;
10257
+ for (int k = 0; k < n; ++k)
10258
+ {
10259
+ const DisplayCommandType t = state.commands[gRegionIndex[k]].type;
10260
+ if (t == DisplayCommandType::PushClip || t == DisplayCommandType::PopClip || t == DisplayCommandType::SetAlpha)
10261
+ gRegionIndex[kept++] = gRegionIndex[k];
10262
+ }
10263
+ n = kept;
10264
+ startCi = i;
10265
+ cullCovered = true;
10266
+ emittedAlpha = -1;
10267
+ }
10268
+ if (pendingAlpha != emittedAlpha)
10269
+ {
10270
+ gRegionIndex[n++] = static_cast<std::uint16_t>(pendingAlpha);
10271
+ emittedAlpha = pendingAlpha;
10272
+ }
10273
+ gRegionIndex[n++] = static_cast<std::uint16_t>(i);
10274
+ }
10275
+ // Leave alpha where the whole list leaves it: the next strip starts there.
10276
+ if (pendingAlpha != emittedAlpha) gRegionIndex[n++] = static_cast<std::uint16_t>(pendingAlpha);
10277
+ (void)startCi;
10278
+ gRegionCovered = cullCovered;
10279
+ gRegionIndexCount = n;
10280
+ return true;
10281
+ }
10282
+
10283
+ // Per row of the viewport (vx, vy, vw, vh): a signature of what a
10284
+ // horizontal scroll cannot change, or 0. A row that only full-width plain
10285
+ // fills touch (a background, a horizontal rule, a rounded rect's straight
10286
+ // middle) looks the same at every scroll offset, and its signature hashes
10287
+ // those fills in paint order; any other draw reaching the row, or a fill
10288
+ // cut short of either edge, makes it 0. False when it cannot tell (a
10289
+ // filter blur, a clip stack too deep).
10290
+ static bool scrollRowSignatures(int vx, int vy, int vw, int vh, std::uint32_t *sig)
10291
+ {
10292
+ const int vx1 = vx + vw - 1, vy1 = vy + vh - 1;
10293
+ for (int r = 0; r < vh; ++r) sig[r] = 2166136261u;
10294
+ constexpr int kDepth = 64;
10295
+ int clipX0[kDepth], clipY0[kDepth], clipX1[kDepth], clipY1[kDepth];
10296
+ int depth = 0, alpha = 255;
10297
+ int curX0 = vx, curY0 = vy, curX1 = vx1, curY1 = vy1;
10298
+ auto vary = [&](int y0, int y1) {
10299
+ y0 = std::max(y0, curY0); y1 = std::min(y1, curY1);
10300
+ for (int y = y0; y <= y1; ++y) sig[y - vy] = 0;
10301
+ };
10302
+ auto band = [&](int y0, int y1, std::uint32_t color) {
10303
+ y0 = std::max(y0, curY0); y1 = std::min(y1, curY1);
10304
+ const std::uint32_t mix = (color << 8) ^ static_cast<std::uint32_t>(alpha);
10305
+ for (int y = y0; y <= y1; ++y)
10306
+ if (sig[y - vy]) sig[y - vy] = ((sig[y - vy] ^ mix) * 16777619u) | 1u;
10307
+ };
10308
+ for (int i = 0; i < state.commandCount; ++i)
10309
+ {
10310
+ const DisplayCommand &c = state.commands[i];
10311
+ switch (c.type)
10312
+ {
10313
+ case DisplayCommandType::PushClip:
10314
+ if (depth >= kDepth) return false;
10315
+ clipX0[depth] = curX0; clipY0[depth] = curY0; clipX1[depth] = curX1; clipY1[depth] = curY1;
10316
+ depth++;
10317
+ curX0 = std::max(curX0, static_cast<int>(c.clip.x));
10318
+ curY0 = std::max(curY0, static_cast<int>(c.clip.y));
10319
+ curX1 = std::min(curX1, static_cast<int>(c.clip.x) + static_cast<int>(c.clip.w) - 1);
10320
+ curY1 = std::min(curY1, static_cast<int>(c.clip.y) + static_cast<int>(c.clip.h) - 1);
10321
+ continue;
10322
+ case DisplayCommandType::PopClip:
10323
+ if (depth > 0)
10324
+ {
10325
+ depth--;
10326
+ curX0 = clipX0[depth]; curY0 = clipY0[depth]; curX1 = clipX1[depth]; curY1 = clipY1[depth];
10327
+ }
10328
+ continue;
10329
+ case DisplayCommandType::SetAlpha:
10330
+ alpha = c.alpha.alpha;
10331
+ continue;
10332
+ case DisplayCommandType::BeginFilterBlur:
10333
+ case DisplayCommandType::ApplyFilterBlur:
10334
+ return false;
10335
+ default:
10336
+ break;
10337
+ }
10338
+ if (curX0 > curX1 || curY0 > curY1) continue;
10339
+ if (c.bx > curX1 || c.by > curY1 || c.bx + c.bw - 1 < curX0 || c.by + c.bh - 1 < curY0) continue;
10340
+ // Only a fill spanning the whole viewport width, with the clip
10341
+ // leaving it whole, is the same at every offset.
10342
+ const bool spans = curX0 <= vx && curX1 >= vx1;
10343
+ if (spans && c.type == DisplayCommandType::FillRect && c.textClipOwner < 0 &&
10344
+ c.fill.x <= vx && c.fill.x + c.fill.w - 1 >= vx1)
10345
+ {
10346
+ band(c.fill.y, c.fill.y + c.fill.h - 1, c.fill.color);
10347
+ continue;
10348
+ }
10349
+ if (spans && c.type == DisplayCommandType::FillRoundedRect && c.textClipOwner < 0 &&
10350
+ c.fillRoundedRect.x <= vx && c.fillRoundedRect.x + c.fillRoundedRect.w - 1 >= vx1)
10351
+ {
10352
+ const auto &f = c.fillRoundedRect;
10353
+ const int x1 = f.x + f.w - 1, y1 = f.y + f.h - 1;
10354
+ // Corners clear of the viewport leave every row straight; else
10355
+ // only the rows between the corners are.
10356
+ const int top = std::max(f.tl, f.tr), bottom = std::max(f.bl, f.br);
10357
+ const bool cornersOut = f.x + std::max(f.tl, f.bl) <= vx && x1 - std::max(f.tr, f.br) >= vx1;
10358
+ if (cornersOut || f.y + top > y1 - bottom)
10359
+ {
10360
+ if (cornersOut) band(f.y, y1, f.color);
10361
+ else vary(f.y, y1);
10362
+ }
10363
+ else
10364
+ {
10365
+ vary(f.y, f.y + top - 1);
10366
+ band(f.y + top, y1 - bottom, f.color);
10367
+ vary(y1 - bottom + 1, y1);
10368
+ }
10369
+ continue;
10370
+ }
10371
+ // A border whose side edges (corners included) lie outside the
10372
+ // viewport paints only its top and bottom bands inside it: a card's
10373
+ // outline around a scroller leaves the rows between untouched.
10374
+ if (c.type == DisplayCommandType::StrokeRect || c.type == DisplayCommandType::StrokeRoundedRect)
10375
+ {
10376
+ int sx, sy, sw, sh, reach = 1;
10377
+ if (c.type == DisplayCommandType::StrokeRect)
10378
+ {
10379
+ sx = c.stroke.x; sy = c.stroke.y; sw = c.stroke.w; sh = c.stroke.h;
10380
+ }
10381
+ else
10382
+ {
10383
+ const auto &st = c.strokeRoundedRect;
10384
+ sx = st.x; sy = st.y; sw = st.w; sh = st.h;
10385
+ reach = std::max<int>({st.tl, st.tr, st.br, st.bl, 0}) + std::max<int>(st.lineWidth, 1);
10386
+ if (st.cssRadii)
10387
+ for (int k = 0; k < 4; ++k) reach = std::max(reach, (std::max(st.rx8[k], st.ry8[k]) + 7) / 8 + std::max<int>(st.lineWidth, 1));
10388
+ }
10389
+ if (sx + reach < vx && sx + sw - 1 - reach > vx1 && sh > 2 * reach)
10390
+ {
10391
+ vary(std::min<int>(sy, c.by), sy + reach);
10392
+ vary(sy + sh - 1 - reach, std::max<int>(sy + sh - 1, c.by + c.bh - 1));
10393
+ continue;
10394
+ }
10395
+ }
10396
+ vary(c.by, c.by + c.bh - 1);
10397
+ }
10398
+ return true;
10399
+ }
10400
+
10401
+ // A strip-by-strip replay of one region computes the cull once for the
10402
+ // whole region (a cover of the region covers every strip of it) instead
10403
+ // of re-scanning the list per strip.
10404
+ static inline int cullStartOverride = -1;
10405
+ // Whether the last cullStartIndex() found an opaque fill covering the clip.
10406
+ static inline bool cullCovered = false;
10407
+
10408
+ static void GEA_RENDER_HOT_SRAM replay()
10409
+ {
10410
+ int cx0 = 0, cy0 = 0, cx1 = 0, cy1 = 0;
10411
+ int clip_dirty = 1;
10412
+ int clip_stack_depth = 0;
10413
+ UniformRoundedRectBatch roundedRects;
10414
+
10415
+ // Occlusion cull against the CALLER's clip (the conservative tree_render
10416
+ // path pushes each dirty rect around a full replay): start at the last
10417
+ // opaque full-cover fill's first command. Unlike the node walks, this
10418
+ // stream carries clip state INLINE, and an orphan PopClip at depth 0
10419
+ // pops the caller's region clip — so only cull when the cover has no
10420
+ // clipping ancestors (whose Pop would run unmatched), the skipped
10421
+ // prefix is Push/Pop balanced, and no filter-blur pair could straddle
10422
+ // the cut.
10423
+ // A filtered region list already dropped the culled draws; replay()
10424
+ // below decides whether the clip lets it use the list.
10425
+ int startCi = -1;
10426
+
10427
+ int listed = gRegionIndexCount;
10428
+ if (listed >= 0)
10429
+ {
10430
+ int qx0 = 0, qy0 = 0, qx1 = -1, qy1 = -1;
10431
+ gea::platform::display::Display::clip(&qx0, &qy0, &qx1, &qy1);
10432
+ if (qx1 >= qx0 && qy1 >= qy0 &&
10433
+ (qx0 < gRegionX0 || qy0 < gRegionY0 || qx1 > gRegionX1 || qy1 > gRegionY1))
10434
+ listed = -1;
10435
+ }
10436
+ const int total = listed >= 0 ? listed : state.commandCount;
10437
+ if (startCi < 0) startCi = listed >= 0 ? 0 : cullStartOverride >= 0 ? cullStartOverride : cullStartIndex();
10438
+ for (int k = 0; k < total; k++)
10439
+ {
10440
+ const int i = listed >= 0 ? gRegionIndex[k] : k;
9645
10441
  DisplayCommand *c = &state.commands[i];
9646
10442
 
9647
10443
  // Occlusion cull: draws before the covering fill are invisible; only
@@ -9794,7 +10590,7 @@ int gLastScrollUiFrame = -1000;
9794
10590
  {
9795
10591
  // This is also the recorder's traversal, so retained draws cannot invent a
9796
10592
  // second hierarchy or lose document-order ties after a depth crossing.
9797
- static int ranks[kMaxNodes];
10593
+ GEA_EXT_BSS static int ranks[kMaxNodes];
9798
10594
  static uint64_t serial = ~0ull;
9799
10595
  static uint32_t recordSerial = 0;
9800
10596
  auto &tree = Tree::instance();
@@ -9911,6 +10707,12 @@ int gLastScrollUiFrame = -1000;
9911
10707
 
9912
10708
  void DisplayList::pruneImageReferences()
9913
10709
  {
10710
+ // Runs after every re-recorded node: with no image held there is nothing
10711
+ // to release, and only blit commands can hold one.
10712
+ bool anyHeld = false;
10713
+ for (int id = 0; id < gea::framework::graphics::kImageMax && !anyHeld; ++id)
10714
+ anyHeld = state.imageReferences[id];
10715
+ if (!anyHeld) return;
9914
10716
  bool referenced[gea::framework::graphics::kImageMax]{};
9915
10717
  auto &images = gea::framework::graphics::ImageStore::instance();
9916
10718
  for (int i = 0; i < state.commandCount; ++i) {
@@ -9919,6 +10721,7 @@ int gLastScrollUiFrame = -1000;
9919
10721
  if (command.type == DisplayCommandType::BlitImage) pixels = command.blit.pixels;
9920
10722
  else if (command.type == DisplayCommandType::BlitImageScaled) pixels = command.scaledBlit.pixels;
9921
10723
  else if (command.type == DisplayCommandType::BlitImageProjected) pixels = command.projectedBlit.pixels;
10724
+ else continue;
9922
10725
  const int id = images.idForPixels(pixels);
9923
10726
  if (id >= 0) referenced[id] = true;
9924
10727
  }
@@ -9932,6 +10735,7 @@ int gLastScrollUiFrame = -1000;
9932
10735
 
9933
10736
  void DisplayList::clear()
9934
10737
  {
10738
+ gRegionIndexCount = -1; // its indices name the old list
9935
10739
  Tree &tree = Tree::instance();
9936
10740
  state.displayListSerial++;
9937
10741
  if (state.displayListSerial == 0)
@@ -9941,6 +10745,8 @@ int gLastScrollUiFrame = -1000;
9941
10745
  state.commandCount = 0;
9942
10746
  pruneImageReferences();
9943
10747
  state.textClippedBackgrounds = false;
10748
+ state.scrollbarBoxCount = 0;
10749
+ state.scrollbarBoxOverflow = false;
9944
10750
  state.commandOverflow = false;
9945
10751
  state.drawNodeOrderCount = 0;
9946
10752
  if (!state.ensureNodeScratchCapacity(tree.nodeCount()))
@@ -10018,6 +10824,14 @@ int gLastScrollUiFrame = -1000;
10018
10824
  Tree &weldTree = Tree::instance();
10019
10825
  Node *weldNodes = weldTree.nodes();
10020
10826
  const int weldNodeCount = weldTree.nodeCount();
10827
+ // No transform-dirty node, no dynamic subtree: nothing below can weld. One
10828
+ // pass over a flag instead of an ancestor walk per node, on every refresh
10829
+ // of every screen without a transform animation.
10830
+ bool anyTransformDirty = false;
10831
+ for (int id = 0; id < weldNodeCount && !anyTransformDirty; id++)
10832
+ anyTransformDirty = weldNodes[id].render.transform_dirty;
10833
+ if (!anyTransformDirty)
10834
+ return;
10021
10835
  for (int id = 0; id < weldNodeCount && wn + 4 <= kMaxWeld; id++)
10022
10836
  {
10023
10837
  bool nodeInDynamicSubtree = false;
@@ -10568,9 +11382,11 @@ int gLastScrollUiFrame = -1000;
10568
11382
  {
10569
11383
  if ((GEA_CSS_FILTERS ? rstyle(nodes[i].computedStyle()).filter_blur_radius : 0) > 0)
10570
11384
  return false;
10571
- if (nodes[i].computedStyle().overflow == 2)
10572
- return false;
10573
11385
  }
11386
+ // Scroll containers replay like overflow:hidden ones (their clips come
11387
+ // from overflowClipBounds); only their scrollbar thumbs sit outside the
11388
+ // node ranges, and tree_render keeps frames that touch a thumb on the
11389
+ // full-list path (scrollbarOverlapsRegion).
10574
11390
  (void)width;
10575
11391
  (void)height;
10576
11392
 
@@ -10583,6 +11399,81 @@ int gLastScrollUiFrame = -1000;
10583
11399
  return state.textClippedBackgrounds;
10584
11400
  }
10585
11401
 
11402
+ void DisplayList::noteScrollbarBox(int node, int x, int y, int w, int h)
11403
+ {
11404
+ if (state.scrollbarBoxCount >= DisplayListState::kMaxScrollbarBoxes)
11405
+ {
11406
+ state.scrollbarBoxOverflow = true;
11407
+ return;
11408
+ }
11409
+ // Called right after the thumb's append(), so it is the last command.
11410
+ state.scrollbarCommands[state.scrollbarBoxCount] = state.commandCount - 1;
11411
+ state.scrollbarNodes[state.scrollbarBoxCount] = node;
11412
+ std::int16_t *box = state.scrollbarBoxes[state.scrollbarBoxCount++];
11413
+ box[0] = static_cast<std::int16_t>(x);
11414
+ box[1] = static_cast<std::int16_t>(y);
11415
+ box[2] = static_cast<std::int16_t>(x + w - 1);
11416
+ box[3] = static_cast<std::int16_t>(y + h - 1);
11417
+ }
11418
+
11419
+ bool DisplayList::scrollbarOverlapsRegion(int x0, int y0, int x1, int y1) const
11420
+ {
11421
+ if (state.scrollbarBoxOverflow)
11422
+ return true;
11423
+ for (int i = 0; i < state.scrollbarBoxCount; ++i)
11424
+ {
11425
+ const std::int16_t *box = state.scrollbarBoxes[i];
11426
+ if (box[0] <= x1 && box[2] >= x0 && box[1] <= y1 && box[3] >= y0)
11427
+ return true;
11428
+ }
11429
+ return false;
11430
+ }
11431
+
11432
+ // Direct replay walks node command ranges, which a thumb sits outside. It can
11433
+ // still serve a frame that touches a thumb by painting the thumb after the
11434
+ // nodes -- correct only while nothing recorded after the thumb paints over it
11435
+ // (an overlay above the scroll container would otherwise end up under it).
11436
+ bool DisplayList::scrollbarThumbsPaintLast() const
11437
+ {
11438
+ if (state.scrollbarBoxOverflow)
11439
+ return false;
11440
+ for (int i = 0; i < state.scrollbarBoxCount; ++i)
11441
+ {
11442
+ const int index = state.scrollbarCommands[i];
11443
+ if (index < 0 || index >= state.commandCount)
11444
+ return false;
11445
+ const std::int16_t *box = state.scrollbarBoxes[i];
11446
+ for (int ci = index + 1; ci < state.commandCount; ++ci)
11447
+ {
11448
+ const DisplayCommand &c = state.commands[ci];
11449
+ if (c.type == DisplayCommandType::PushClip || c.type == DisplayCommandType::PopClip)
11450
+ continue;
11451
+ if (c.bw <= 0 || c.bh <= 0)
11452
+ continue;
11453
+ if (ClipMath::rectsOverlap(c.bx, c.by, c.bx + c.bw - 1, c.by + c.bh - 1, box[0], box[1], box[2], box[3]))
11454
+ return false;
11455
+ }
11456
+ }
11457
+ return true;
11458
+ }
11459
+
11460
+ void DisplayList::replayScrollbarThumbsInRegion(int x0, int y0, int x1, int y1)
11461
+ {
11462
+ for (int i = 0; i < state.scrollbarBoxCount; ++i)
11463
+ {
11464
+ const std::int16_t *box = state.scrollbarBoxes[i];
11465
+ if (!(box[0] <= x1 && box[2] >= x0 && box[1] <= y1 && box[3] >= y0))
11466
+ continue;
11467
+ const int index = state.scrollbarCommands[i];
11468
+ if (index < 0 || index >= state.commandCount)
11469
+ continue;
11470
+ gea::platform::display::Display::setAlpha(255);
11471
+ gea::platform::display::Display::pushClip(x0, y0, x1 - x0 + 1, y1 - y0 + 1);
11472
+ DisplayCommandDrawer::replay(state.commands[index]);
11473
+ gea::platform::display::Display::popClip();
11474
+ }
11475
+ }
11476
+
10586
11477
  void DisplayList::clearRetainedBackgroundRecolors()
10587
11478
  {
10588
11479
  state.retainedBackgroundRecolorCount = 0;
@@ -10771,23 +11662,65 @@ int gLastScrollUiFrame = -1000;
10771
11662
  return;
10772
11663
  Tree &tree = Tree::instance();
10773
11664
  Node *nodes = tree.nodes();
10774
- auto belongsToSubtree = [&](int id) {
10775
- for (int cursor = id; cursor >= 0 && cursor < tree.nodeCount(); cursor = nodes[cursor].parent)
10776
- if (cursor == node)
10777
- return true;
10778
- return false;
10779
- };
10780
- for (int id = 0; id < tree.nodeCount(); id++)
11665
+ const int count = tree.nodeCount();
11666
+ // Mark the subtree in one walk down its child links. Testing every node's
11667
+ // ancestor chain instead cost O(nodes x depth) on every scroll frame: 4-12
11668
+ // ms of a CrowPanel chart scroll frame, on a tab of ~600 commands.
11669
+ static std::vector<std::uint32_t> subtreeMark;
11670
+ static std::uint32_t subtreeStamp = 0;
11671
+ if (subtreeMark.size() < static_cast<std::size_t>(count))
11672
+ subtreeMark.resize(static_cast<std::size_t>(count), 0);
11673
+ if (++subtreeStamp == 0)
10781
11674
  {
10782
- if (!belongsToSubtree(id) || !state.hasNodeScratchFor(id))
10783
- continue;
10784
- const int start = state.nodeDrawStart[id];
10785
- const int end = state.nodeDrawEnd[id];
10786
- if (start >= 0 && end > start && end <= state.commandCount)
11675
+ std::fill(subtreeMark.begin(), subtreeMark.end(), 0);
11676
+ subtreeStamp = 1;
11677
+ }
11678
+ for (int id = node; id >= 0;)
11679
+ {
11680
+ subtreeMark[id] = subtreeStamp;
11681
+ if (state.hasNodeScratchFor(id))
10787
11682
  {
10788
- for (int ci = start; ci < end; ci++)
10789
- DisplayCommandTranslator::translate(&state.commands[ci], dx, dy);
11683
+ const int start = state.nodeDrawStart[id];
11684
+ const int end = state.nodeDrawEnd[id];
11685
+ if (start >= 0 && end > start && end <= state.commandCount)
11686
+ {
11687
+ for (int ci = start; ci < end; ci++)
11688
+ DisplayCommandTranslator::translate(&state.commands[ci], dx, dy);
11689
+ }
11690
+ }
11691
+ if (nodes[id].first_child >= 0)
11692
+ {
11693
+ id = nodes[id].first_child;
11694
+ continue;
10790
11695
  }
11696
+ while (id != node && nodes[id].next_sibling < 0)
11697
+ id = nodes[id].parent;
11698
+ if (id == node)
11699
+ break;
11700
+ id = nodes[id].next_sibling;
11701
+ }
11702
+ auto belongsToSubtree = [&](int id) {
11703
+ return id >= 0 && id < count && subtreeMark[id] == subtreeStamp;
11704
+ };
11705
+ // A scroll container's thumb sits outside every node paint range too. The
11706
+ // thumbs of containers inside the subtree move with it (the root's own
11707
+ // thumb stays: it is viewport-fixed like the root's box, and its caller
11708
+ // repositions it -- patchScrollbarThumb). Left behind, a scrolling page
11709
+ // replayed a nested list's thumb at its old place into every revealed
11710
+ // strip that crossed it, and the blit then dragged those stale pieces up
11711
+ // the screen with the content.
11712
+ for (int i = 0; i < state.scrollbarBoxCount; ++i)
11713
+ {
11714
+ const int owner = state.scrollbarNodes[i];
11715
+ const int ci = state.scrollbarCommands[i];
11716
+ if (owner == node || !belongsToSubtree(owner) || ci < 0 || ci >= state.commandCount)
11717
+ continue;
11718
+ DisplayCommandTranslator::translate(&state.commands[ci], dx, dy);
11719
+ std::int16_t *box = state.scrollbarBoxes[i];
11720
+ box[0] = static_cast<std::int16_t>(box[0] + dx);
11721
+ box[1] = static_cast<std::int16_t>(box[1] + dy);
11722
+ box[2] = static_cast<std::int16_t>(box[2] + dx);
11723
+ box[3] = static_cast<std::int16_t>(box[3] + dy);
10791
11724
  }
10792
11725
  // Clip scopes sit outside node paint ranges. Scan the command buffer once
10793
11726
  // after translating all paint ranges, rather than once per descendant.
@@ -10823,10 +11756,14 @@ int gLastScrollUiFrame = -1000;
10823
11756
  if (ViewRenderer::anyTransformActive())
10824
11757
  return false;
10825
11758
  Node *nodes = tree.nodes();
10826
- int x0 = 0;
10827
- int y0 = 0;
10828
- int x1 = width - 1;
10829
- int y1 = height - 1;
11759
+ // Built inside out, so the screen bound goes on last: recordNode hands
11760
+ // the children of an expanded-clip node (recordNodeWithExpandedClip) the
11761
+ // coverage window in place of everything above it, screen included.
11762
+ int x0 = INT_MIN / 2;
11763
+ int y0 = INT_MIN / 2;
11764
+ int x1 = INT_MAX / 2;
11765
+ int y1 = INT_MAX / 2;
11766
+ bool expanded = false;
10830
11767
  for (int a = LayoutEngine::isViewportFixed(nodes[id]) ? -1 : nodes[id].parent; a >= 0 && a < nodeCount; a = nodes[a].parent)
10831
11768
  {
10832
11769
  const Node &ancestor = nodes[a];
@@ -10835,9 +11772,25 @@ int gLastScrollUiFrame = -1000;
10835
11772
  if (LayoutEngine::isViewportFixed(ancestor)) break;
10836
11773
  continue;
10837
11774
  }
11775
+ if (a == state.recordExpandedClipNode)
11776
+ {
11777
+ x0 = std::max(x0, state.recordExpandedClipX0);
11778
+ y0 = std::max(y0, state.recordExpandedClipY0);
11779
+ x1 = std::min(x1, state.recordExpandedClipX1);
11780
+ y1 = std::min(y1, state.recordExpandedClipY1);
11781
+ expanded = true;
11782
+ break;
11783
+ }
10838
11784
  ClipMath::clampToNode(ancestor, &x0, &y0, &x1, &y1);
10839
11785
  if (LayoutEngine::isViewportFixed(ancestor)) break;
10840
11786
  }
11787
+ if (!expanded)
11788
+ {
11789
+ x0 = std::max(x0, 0);
11790
+ y0 = std::max(y0, 0);
11791
+ x1 = std::min(x1, width - 1);
11792
+ y1 = std::min(y1, height - 1);
11793
+ }
10841
11794
  *cx0 = x0;
10842
11795
  *cy0 = y0;
10843
11796
  *cx1 = x1;
@@ -12406,7 +13359,7 @@ int gLastScrollUiFrame = -1000;
12406
13359
  const std::int64_t totalStartUs = refreshPerfNowUs();
12407
13360
  FillBandDirty dMain{canvas->width(), canvas->height(), -1, -1};
12408
13361
  FillBandDirty dWork{canvas->width(), canvas->height(), -1, -1};
12409
- static RecolorPixelCache protectedPixelCaches[2];
13362
+ GEA_EXT_BSS static RecolorPixelCache protectedPixelCaches[2];
12410
13363
  if (protectForegroundDuringFill)
12411
13364
  {
12412
13365
  protectedPixelCaches[0].clear();
@@ -13023,17 +13976,30 @@ int gLastScrollUiFrame = -1000;
13023
13976
  return false;
13024
13977
  int start = state.nodeDrawStart[node];
13025
13978
  int end = state.nodeDrawEnd[node];
13979
+ // recordNode culls a node outside its ancestors' overflow clips to the
13980
+ // empty range (-1, -1). While it stays outside, a full record would cull it
13981
+ // again, so a change to it (a ticking legend just below a scroll view's
13982
+ // edge) leaves the list exactly as it is -- refusing here instead threw the
13983
+ // whole retained list away and re-recorded the screen every frame.
13984
+ if (start < 0 && end < 0 && !isDocumentCanvasRoot(nodes[node]) &&
13985
+ nodeOutsideRecordClip(node, tree.mountedWidth(), tree.mountedHeight()))
13986
+ return true;
13026
13987
  if (start < 0 || end < start || end > state.commandCount)
13027
13988
  return false;
13028
13989
 
13029
- DisplayCommand tmp[96];
13990
+ // Raw storage, like the list's own buffer: append() hands out slots the
13991
+ // recorders fill, so constructing 96 commands per re-record (a few KB of
13992
+ // zeroing, every changed node, every frame) bought nothing.
13993
+ constexpr int kTmpCommands = 96;
13994
+ alignas(DisplayCommand) unsigned char tmpStorage[sizeof(DisplayCommand) * kTmpCommands];
13995
+ DisplayCommand *tmp = reinterpret_cast<DisplayCommand *>(tmpStorage);
13030
13996
  int tmp_len = 0;
13031
13997
  DisplayCommand *prev_override = state.appendOverride;
13032
13998
  int *prev_override_len = state.appendOverrideCount;
13033
13999
  int prev_override_cap = state.appendOverrideCapacity;
13034
14000
  state.appendOverride = tmp;
13035
14001
  state.appendOverrideCount = &tmp_len;
13036
- state.appendOverrideCapacity = (int)(sizeof(tmp) / sizeof(tmp[0]));
14002
+ state.appendOverrideCapacity = kTmpCommands;
13037
14003
 
13038
14004
  Node *n = &nodes[node];
13039
14005
  // recordNode gates every paint on `overlaps_clip` — a node outside its
@@ -13072,6 +14038,15 @@ int gLastScrollUiFrame = -1000;
13072
14038
  int delta = tmp_len - old_len;
13073
14039
  if (old_len == 0 && tmp_len > 0)
13074
14040
  return false;
14041
+ // Only a replaced blit can drop the last reference to an image; new ones
14042
+ // retain theirs while recording.
14043
+ bool replacedBlit = false;
14044
+ for (int ci = start; ci < end && !replacedBlit; ++ci)
14045
+ {
14046
+ const DisplayCommandType type = state.commands[ci].type;
14047
+ replacedBlit = type == DisplayCommandType::BlitImage || type == DisplayCommandType::BlitImageScaled ||
14048
+ type == DisplayCommandType::BlitImageProjected;
14049
+ }
13075
14050
  if (delta > 0 && state.commandCount + delta > DisplayListState::kMaxCommands)
13076
14051
  return false;
13077
14052
  if (delta != 0 && end < state.commandCount)
@@ -13094,10 +14069,27 @@ int gLastScrollUiFrame = -1000;
13094
14069
  state.nodeDrawEnd[node] = -1;
13095
14070
  }
13096
14071
  state.shiftNodeDrawRangesAfter(end, delta, node);
13097
- pruneImageReferences();
14072
+ if (replacedBlit)
14073
+ pruneImageReferences();
13098
14074
  return true;
13099
14075
  }
13100
14076
 
14077
+ bool DisplayList::rerecordNodeCommandsWithExpandedClip(int node, int clipNode, int clipX0, int clipY0, int clipX1, int clipY1)
14078
+ {
14079
+ state.recordExpandedClipNode = clipNode;
14080
+ state.recordExpandedClipX0 = clipX0;
14081
+ state.recordExpandedClipY0 = clipY0;
14082
+ state.recordExpandedClipX1 = clipX1;
14083
+ state.recordExpandedClipY1 = clipY1;
14084
+ const bool ok = rerecordNodeCommands(node);
14085
+ state.recordExpandedClipNode = -1;
14086
+ state.recordExpandedClipX0 = 0;
14087
+ state.recordExpandedClipY0 = 0;
14088
+ state.recordExpandedClipX1 = -1;
14089
+ state.recordExpandedClipY1 = -1;
14090
+ return ok;
14091
+ }
14092
+
13101
14093
  std::uint32_t DisplayList::recordSerial() const
13102
14094
  {
13103
14095
  return state.displayListSerial;
@@ -13115,6 +14107,10 @@ int gLastScrollUiFrame = -1000;
13115
14107
  Node *nodes = tree.nodes();
13116
14108
  if (!state.commands || node < 0 || node >= tree.nodeCount())
13117
14109
  return false;
14110
+ // Untracked thumbs (more than the box table holds) cannot follow a
14111
+ // translate (translateSubtreeCommands): have the caller re-record.
14112
+ if (state.scrollbarBoxOverflow)
14113
+ return false;
13118
14114
  const Node *n = &nodes[node];
13119
14115
 
13120
14116
  // Mirror of ViewRenderer::recordScrollbar's geometry. Cases where no
@@ -13141,7 +14137,7 @@ int gLastScrollUiFrame = -1000;
13141
14137
  };
13142
14138
  const int oldY = thumbY(n->layout.previous_scroll_y);
13143
14139
  const int newY = thumbY(n->layout.scroll_y);
13144
- const int thumbX = n->layout.x + n->layout.width - 7;
14140
+ const int thumbX = n->layout.x + n->layout.width - 6 - 4;
13145
14141
  if (oldY == newY)
13146
14142
  return true;
13147
14143
 
@@ -13150,10 +14146,17 @@ int gLastScrollUiFrame = -1000;
13150
14146
  DisplayCommand &c = state.commands[ci];
13151
14147
  if (c.type != DisplayCommandType::FillRoundedRect)
13152
14148
  continue;
13153
- if (c.bx != thumbX || c.bw != 3 || c.by != oldY || c.bh != thumb_h)
14149
+ if (c.bx != thumbX || c.bw != 4 || c.by != oldY || c.bh != thumb_h)
13154
14150
  continue;
13155
14151
  c.by = static_cast<std::int16_t>(newY);
13156
14152
  c.fillRoundedRect.y = static_cast<std::int16_t>(newY);
14153
+ // The thumb's box steers which repaints replay it; keep it on the thumb.
14154
+ for (int i = 0; i < state.scrollbarBoxCount; ++i)
14155
+ if (state.scrollbarCommands[i] == ci)
14156
+ {
14157
+ state.scrollbarBoxes[i][1] = static_cast<std::int16_t>(newY);
14158
+ state.scrollbarBoxes[i][3] = static_cast<std::int16_t>(newY + thumb_h - 1);
14159
+ }
13157
14160
  return true;
13158
14161
  }
13159
14162
  return false;
@@ -13169,6 +14172,7 @@ int gLastScrollUiFrame = -1000;
13169
14172
  // background re-blend over themselves and drift outward every replay.
13170
14173
  DisplayCommandReplayer::restoreClearBaseInRegion(x0, y0, x1, y1);
13171
14174
  DisplayCommandReplayer::replayDirectDirtyRegion(x0, y0, x1, y1, origin);
14175
+ replayScrollbarThumbsInRegion(x0, y0, x1, y1);
13172
14176
  }
13173
14177
 
13174
14178
  void DisplayList::replayDirectDirtyRegions(const DisplayReplayRegion *regions, int count)
@@ -13176,6 +14180,18 @@ int gLastScrollUiFrame = -1000;
13176
14180
  for (int i = 0; i < count; ++i)
13177
14181
  DisplayCommandReplayer::restoreClearBaseInRegion(regions[i].x0, regions[i].y0, regions[i].x1, regions[i].y1);
13178
14182
  DisplayCommandReplayer::replayDirectDirtyRegions(regions, count);
14183
+ for (int i = 0; i < count; ++i)
14184
+ replayScrollbarThumbsInRegion(regions[i].x0, regions[i].y0, regions[i].x1, regions[i].y1);
14185
+ }
14186
+
14187
+ void DisplayList::beginSimpleReplayScope()
14188
+ {
14189
+ DisplayCommandReplayer::beginSimpleReplayScope();
14190
+ }
14191
+
14192
+ void DisplayList::endSimpleReplayScope()
14193
+ {
14194
+ DisplayCommandReplayer::endSimpleReplayScope();
13179
14195
  }
13180
14196
 
13181
14197
  bool DisplayList::canReplaySimpleDirtyRegions(int width, int height) const
@@ -13198,14 +14214,16 @@ int gLastScrollUiFrame = -1000;
13198
14214
  // chunk. The worker canvas has neither that binding nor the clip, so this
13199
14215
  // already-clipped entry must stay on the submitting canvas.
13200
14216
  DisplayCommandReplayer::replaySimpleClippedDirtyRegion(x0, y0, x1, y1, origin, /*allowSplit=*/false);
14217
+ replayScrollbarThumbsInRegion(x0, y0, x1, y1);
13201
14218
  }
13202
14219
 
13203
14220
  void DisplayList::maybeBakeStaticBackdrop(int width, int height, bool stableThisFrame, bool directReplay,
13204
14221
  bool bakeEligibleThisFrame)
13205
14222
  {
13206
14223
  // The backdrop cache only helps the direct dirty-region replay path; without it
13207
- // we render the full list anyway, so don't bake.
13208
- if (!directReplay)
14224
+ // we render the full list anyway, so don't bake. Nor with scrollbar thumbs:
14225
+ // the bake would capture a thumb that direct replay repaints elsewhere.
14226
+ if (!directReplay || state.scrollbarBoxCount > 0)
13209
14227
  {
13210
14228
  gBackdropCacheValid = false;
13211
14229
  gBackdropStableFrames = 0;
@@ -13268,8 +14286,52 @@ int gLastScrollUiFrame = -1000;
13268
14286
  *misses = state.filterBlurCacheMisses;
13269
14287
  }
13270
14288
 
14289
+ int DisplayList::replayCullStart()
14290
+ {
14291
+ return DisplayCommandReplayer::cullStartIndex();
14292
+ }
14293
+
14294
+ bool DisplayList::replayCullCovered()
14295
+ {
14296
+ return DisplayCommandReplayer::cullCovered;
14297
+ }
14298
+
14299
+ bool DisplayList::scrollRowSignatures(int vx, int vy, int vw, int vh, std::uint32_t *sig)
14300
+ {
14301
+ return DisplayCommandReplayer::scrollRowSignatures(vx, vy, vw, vh, sig);
14302
+ }
14303
+
14304
+ bool DisplayList::beginRegionReplay()
14305
+ {
14306
+ return DisplayCommandReplayer::buildRegionIndex();
14307
+ }
14308
+
14309
+ bool DisplayList::regionReplayActive()
14310
+ {
14311
+ return gRegionIndexCount >= 0;
14312
+ }
14313
+
14314
+ bool DisplayList::regionReplayHolds(int x, int y, int w, int h, bool *covered)
14315
+ {
14316
+ if (gRegionIndexCount < 0 || x < gRegionX0 || y < gRegionY0 || x + w - 1 > gRegionX1 || y + h - 1 > gRegionY1)
14317
+ return false;
14318
+ if (covered) *covered = gRegionCovered;
14319
+ return true;
14320
+ }
14321
+
14322
+ void DisplayList::endRegionReplay()
14323
+ {
14324
+ gRegionIndexCount = -1;
14325
+ }
14326
+
14327
+ void DisplayList::setReplayCullStart(int start)
14328
+ {
14329
+ DisplayCommandReplayer::cullStartOverride = start;
14330
+ }
14331
+
13271
14332
  void DisplayList::replay()
13272
14333
  {
14334
+ if (!gea_replay_stage_active()) gea_display_note_direct_draw();
13273
14335
  DisplayCommandReplayer::replay();
13274
14336
  }
13275
14337