@geastack/engine 0.1.14 → 0.1.16

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
@@ -1,13 +1,15 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- #include "renderer_features.h"
3
- #include "internal.h"
4
- #include "state_init.h"
5
2
  #include "canvas.h"
3
+ #include <image.h>
4
+ #include "canvas_element.h"
6
5
  #include "display.h"
7
6
  #include "graphics/font.h"
7
+ #include "internal.h"
8
8
  #include "memory.h"
9
- #include "tree_state.h"
9
+ #include "renderer_features.h"
10
+ #include "state_init.h"
10
11
  #include "style_values.h"
12
+ #include "tree_state.h"
11
13
  #include <pixel.h>
12
14
 
13
15
  #include <algorithm>
@@ -138,14 +140,13 @@ extern "C" __attribute__((weak)) void *gea_display_command_buffer(int *cap_comma
138
140
  return nullptr;
139
141
  }
140
142
 
141
- // Banded UI mode (GEA_EMBEDDED_DISPLAY_BANDED_UI) replays into a DMA staging
142
- // chunk the shared canvas is temporarily bound to; the worker core's canvas
143
- // still addresses the framebuffer, so no replay/fill may split across cores
144
- // while a band rasterizes. Constant true everywhere else.
143
+ // Offscreen snapshots and banded raster callbacks temporarily bind the primary
144
+ // canvas to another buffer. The worker canvas still addresses the framebuffer,
145
+ // so neither command replay nor individual fills may split across cores.
145
146
  #if GEA_EMBEDDED_DISPLAY_BANDED_UI
146
- #define GEA_RENDER_SPLIT_ALLOWED (!gea::embedded::ui::gBandedRasterActive)
147
+ #define GEA_RENDER_SPLIT_ALLOWED (!gea::embedded::ui::gSnapshotRasterActive && !gea::embedded::ui::gBandedRasterActive)
147
148
  #else
148
- #define GEA_RENDER_SPLIT_ALLOWED 1
149
+ #define GEA_RENDER_SPLIT_ALLOWED (!gea::embedded::ui::gSnapshotRasterActive)
149
150
  #endif
150
151
 
151
152
  // 2nd-core fill offload. The target may provide a worker pinned to the other CPU;
@@ -330,6 +331,10 @@ int gLastScrollUiFrame = -1000;
330
331
  return "BlitImage";
331
332
  case DisplayCommandType::BlitImageScaled:
332
333
  return "BlitImageScaled";
334
+ case DisplayCommandType::BlitImageProjected:
335
+ return "BlitImageProjected";
336
+ case DisplayCommandType::ReplayCanvasBatch:
337
+ return "ReplayCanvasBatch";
333
338
  case DisplayCommandType::BeginFilterBlur:
334
339
  return "BeginFilterBlur";
335
340
  case DisplayCommandType::ApplyFilterBlur:
@@ -476,6 +481,12 @@ int gLastScrollUiFrame = -1000;
476
481
  return cp;
477
482
  }
478
483
 
484
+ int followingProjectedTextCodepoint(const char *text, int textTransform, bool wordStart)
485
+ {
486
+ if (!text || !*text || *text == '\n') return 0;
487
+ return transformedTextCodepoint(nextUtf8Codepoint(text), textTransform, wordStart);
488
+ }
489
+
479
490
  struct FilterBlurCacheEntry
480
491
  {
481
492
  // Colour buffers are native pixels — the blur runs in this board's colour
@@ -528,6 +539,7 @@ int gLastScrollUiFrame = -1000;
528
539
  int commandCapacity = 0;
529
540
  bool commandsExternal = false;
530
541
  int commandCount = 0;
542
+ bool imageReferences[gea::framework::graphics::kImageMax]{};
531
543
  bool textClippedBackgrounds = false;
532
544
  // Sticky: an append() was dropped because the command buffer is full.
533
545
  // Reset by clear(); lets a caller detect a truncated record.
@@ -1588,6 +1600,25 @@ int gLastScrollUiFrame = -1000;
1588
1600
  out->brRy8 = scaleRadius8(r.brRy8, scaleY);
1589
1601
  out->blRx8 = scaleRadius8(r.blRx8, scaleX);
1590
1602
  out->blRy8 = scaleRadius8(r.blRy8, scaleY);
1603
+ // A scaled circle's box rounds to whole pixels independently of its
1604
+ // radii; rounding the box down leaves each radius up to a pixel past
1605
+ // half the box, and the Canvas primitive refused it -- about half the
1606
+ // frames of any scale animation then took the per-pixel shader. CSS
1607
+ // clamps such a radius to the box anyway; clamp the rounding slack.
1608
+ const int maxRx8 = std::min(out->w, out->h) * 4;
1609
+ const int maxRy8 = maxRx8;
1610
+ auto clampSlack = [](int &radius8, int max8) {
1611
+ if (radius8 > max8 && radius8 - max8 <= 8)
1612
+ radius8 = max8;
1613
+ };
1614
+ clampSlack(out->tlRx8, maxRx8);
1615
+ clampSlack(out->trRx8, maxRx8);
1616
+ clampSlack(out->brRx8, maxRx8);
1617
+ clampSlack(out->blRx8, maxRx8);
1618
+ clampSlack(out->tlRy8, maxRy8);
1619
+ clampSlack(out->trRy8, maxRy8);
1620
+ clampSlack(out->brRy8, maxRy8);
1621
+ clampSlack(out->blRy8, maxRy8);
1591
1622
  return true;
1592
1623
  }
1593
1624
 
@@ -1827,13 +1858,46 @@ int gLastScrollUiFrame = -1000;
1827
1858
 
1828
1859
  int fillQuadCoverage(const int *xs, const int *ys, int x, int y, int samples)
1829
1860
  {
1861
+ // Every sample lies within half a pixel of the pixel centre, so a pixel
1862
+ // whose centre clears each edge by that half-pixel reach (plus a bound on
1863
+ // the float rounding of the per-sample test) classifies all of its samples
1864
+ // at once. Exact integer math in half-pixel units; only the pixels an edge
1865
+ // actually crosses pay for the per-sample test, which is unchanged. Small
1866
+ // quads were ~7 us per pixel on the S31 doing every sample.
1867
+ int positive = 0, negative = 0;
1868
+ for (int i = 0; i < 4; ++i)
1869
+ {
1870
+ const int j = (i + 1) & 3;
1871
+ const std::int64_t dx = static_cast<std::int64_t>(xs[j]) - xs[i];
1872
+ const std::int64_t dy = static_cast<std::int64_t>(ys[j]) - ys[i];
1873
+ const std::int64_t ry = 2 * static_cast<std::int64_t>(y) + 1 - 2 * static_cast<std::int64_t>(ys[i]);
1874
+ const std::int64_t rx = 2 * static_cast<std::int64_t>(x) + 1 - 2 * static_cast<std::int64_t>(xs[i]);
1875
+ const std::int64_t adx = dx < 0 ? -dx : dx;
1876
+ const std::int64_t ady = dy < 0 ? -dy : dy;
1877
+ const std::int64_t centre = dx * ry - dy * rx;
1878
+ const std::int64_t reach = adx + ady;
1879
+ const std::int64_t rounding = ((adx * ((ry < 0 ? -ry : ry) + 1) + ady * ((rx < 0 ? -rx : rx) + 1)) >> 20) + 1;
1880
+ const std::int64_t margin = reach + rounding;
1881
+ if (centre > margin)
1882
+ positive |= 1 << i;
1883
+ else if (centre < -margin)
1884
+ negative |= 1 << i;
1885
+ }
1886
+ if (positive && negative)
1887
+ return 0;
1888
+ if (positive == 0xf || negative == 0xf)
1889
+ return samples * samples;
1890
+ float offsets[16];
1891
+ const int n = std::min(samples, 16);
1892
+ for (int i = 0; i < n; ++i)
1893
+ offsets[i] = antialiasOffset(i, samples);
1830
1894
  int coverage = 0;
1831
1895
  for (int iy = 0; iy < samples; ++iy)
1832
1896
  {
1833
- const float oy = antialiasOffset(iy, samples);
1897
+ const float oy = iy < 16 ? offsets[iy] : antialiasOffset(iy, samples);
1834
1898
  for (int ix = 0; ix < samples; ++ix)
1835
1899
  {
1836
- const float ox = antialiasOffset(ix, samples);
1900
+ const float ox = ix < 16 ? offsets[ix] : antialiasOffset(ix, samples);
1837
1901
  if (convexQuadContainsFast(xs, ys, static_cast<float>(x) + ox, static_cast<float>(y) + oy))
1838
1902
  ++coverage;
1839
1903
  }
@@ -1841,6 +1905,87 @@ int gLastScrollUiFrame = -1000;
1841
1905
  return coverage;
1842
1906
  }
1843
1907
 
1908
+ // Per-command preparation for fillQuadCoverage. With 2x2 or 4x4 sampling the
1909
+ // sample offsets are exact binary fractions, and while every edge's cross
1910
+ // product stays below 2^21 the float test in convexQuadContainsFast never
1911
+ // rounds — so the same signs come out of exact integers in 1/(2n) pixel
1912
+ // units. Each edge is then a linear function stepped across the sample grid:
1913
+ // a pixel it clears entirely costs two compares, and only the edges that
1914
+ // cross the pixel fill in per-sample sign bits. Thin rotated quads (dial
1915
+ // ticks) are almost all edge pixels; they were ~3 us per pixel on the S31.
1916
+ struct QuadCoverage
1917
+ {
1918
+ const int *xs;
1919
+ const int *ys;
1920
+ int samples;
1921
+ bool exact;
1922
+ int a[4], b[4];
1923
+ unsigned full;
1924
+
1925
+ QuadCoverage(const int *qx, const int *qy, int n, int x0, int y0, int x1, int y1)
1926
+ : xs(qx), ys(qy), samples(n), exact(false), full(0)
1927
+ {
1928
+ if (n != 2 && n != 4)
1929
+ return;
1930
+ std::int64_t worst = 0;
1931
+ for (int i = 0; i < 4; ++i)
1932
+ {
1933
+ const int j = (i + 1) & 3;
1934
+ const std::int64_t dx = static_cast<std::int64_t>(qx[j]) - qx[i];
1935
+ const std::int64_t dy = static_cast<std::int64_t>(qy[j]) - qy[i];
1936
+ const std::int64_t spanX = std::max(std::llabs(static_cast<std::int64_t>(x0) - qx[i]), std::llabs(static_cast<std::int64_t>(x1) - qx[i])) + 1;
1937
+ const std::int64_t spanY = std::max(std::llabs(static_cast<std::int64_t>(y0) - qy[i]), std::llabs(static_cast<std::int64_t>(y1) - qy[i])) + 1;
1938
+ worst = std::max<std::int64_t>(worst, std::llabs(dx) * spanY + std::llabs(dy) * spanX);
1939
+ a[i] = static_cast<int>(-dy);
1940
+ b[i] = static_cast<int>(dx);
1941
+ }
1942
+ if (worst >= (std::int64_t(1) << 20))
1943
+ return;
1944
+ exact = true;
1945
+ full = (1u << (n * n)) - 1u;
1946
+ }
1947
+
1948
+ int at(int x, int y) const
1949
+ {
1950
+ if (!exact)
1951
+ return fillQuadCoverage(xs, ys, x, y, samples);
1952
+ const int n = samples;
1953
+ const int unit = 2 * n;
1954
+ unsigned anyPositive = 0, anyNegative = 0;
1955
+ for (int i = 0; i < 4; ++i)
1956
+ {
1957
+ const int stepX = 2 * a[i], stepY = 2 * b[i];
1958
+ const int base = b[i] * (unit * (y - ys[i]) + 1) + a[i] * (unit * (x - xs[i]) + 1);
1959
+ const int reachX = stepX * (n - 1), reachY = stepY * (n - 1);
1960
+ const int lo = base + std::min(0, reachX) + std::min(0, reachY);
1961
+ const int hi = base + std::max(0, reachX) + std::max(0, reachY);
1962
+ if (lo > 0)
1963
+ anyPositive = full;
1964
+ else if (hi < 0)
1965
+ anyNegative = full;
1966
+ else if (lo != 0 || hi != 0)
1967
+ {
1968
+ unsigned bit = 1;
1969
+ int row = base;
1970
+ for (int iy = 0; iy < n; ++iy, row += stepY)
1971
+ {
1972
+ int v = row;
1973
+ for (int ix = 0; ix < n; ++ix, v += stepX, bit <<= 1)
1974
+ {
1975
+ if (v > 0)
1976
+ anyPositive |= bit;
1977
+ else if (v < 0)
1978
+ anyNegative |= bit;
1979
+ }
1980
+ }
1981
+ }
1982
+ if ((anyPositive & anyNegative) == full)
1983
+ return 0;
1984
+ }
1985
+ return __builtin_popcount(full & ~(anyPositive & anyNegative));
1986
+ }
1987
+ };
1988
+
1844
1989
  int combinedCoverageAlpha(int alpha, int coverage, int sampleCount)
1845
1990
  {
1846
1991
  if (coverage <= 0 || alpha <= 0)
@@ -2571,18 +2716,19 @@ int gLastScrollUiFrame = -1000;
2571
2716
  const int cp = transformedTextCodepoint(nextUtf8Codepoint(p), command.projectedText.textTransform, wordStart);
2572
2717
  if (!font.glyph(cp, &glyph))
2573
2718
  {
2574
- penX += font.sizePx() / 2;
2719
+ penX += font.advance(
2720
+ cp, followingProjectedTextCodepoint(p, command.projectedText.textTransform, wordStart));
2575
2721
  continue;
2576
2722
  }
2577
2723
 
2578
2724
  const int gx = penX + glyph.bearingX;
2579
2725
  const int gy = command.projectedText.srcY + font.ascender() - glyph.bearingY;
2580
- if (sourceX >= gx && sourceX < gx + glyph.width &&
2581
- sourceY >= gy && sourceY < gy + glyph.height)
2726
+ if (sourceX >= gx && sourceX < gx + glyph.width && sourceY >= gy && sourceY < gy + glyph.height)
2582
2727
  {
2583
2728
  return font.coverage(glyph, sourceY - gy, sourceX - gx);
2584
2729
  }
2585
- penX += glyph.advance;
2730
+ penX += font.advance(
2731
+ cp, followingProjectedTextCodepoint(p, command.projectedText.textTransform, wordStart));
2586
2732
  }
2587
2733
  return 0;
2588
2734
  }
@@ -2644,7 +2790,8 @@ int gLastScrollUiFrame = -1000;
2644
2790
  const int cp = transformedTextCodepoint(nextUtf8Codepoint(p), command.projectedText.textTransform, wordStart);
2645
2791
  if (!font.glyph(cp, &glyph))
2646
2792
  {
2647
- penX += font.sizePx() / 2;
2793
+ penX += font.advance(
2794
+ cp, followingProjectedTextCodepoint(p, command.projectedText.textTransform, wordStart));
2648
2795
  continue;
2649
2796
  }
2650
2797
  const int gx = penX + glyph.bearingX;
@@ -2675,7 +2822,8 @@ int gLastScrollUiFrame = -1000;
2675
2822
  }
2676
2823
  }
2677
2824
  }
2678
- penX += glyph.advance;
2825
+ penX += font.advance(
2826
+ cp, followingProjectedTextCodepoint(p, command.projectedText.textTransform, wordStart));
2679
2827
  }
2680
2828
  }
2681
2829
 
@@ -4218,6 +4366,7 @@ int gLastScrollUiFrame = -1000;
4218
4366
  const int coverageArea = (drawXMax - drawXMin + 1) * (drawY1 - drawY0 + 1);
4219
4367
  const int aaSamples = gea::framework::graphics::Canvas::antialiasSamples();
4220
4368
  const int aaSampleCount = aaSamples * aaSamples;
4369
+ const QuadCoverage quadCoverage(xs, ys, aaSamples, drawXMin, drawY0, drawXMax, drawY1);
4221
4370
  if (aaSamples >= 2 && drawXMin <= drawXMax && coverageArea > 0 && coverageArea <= kAntialiasFullCoverageAreaLimit)
4222
4371
  {
4223
4372
  int dirtyX0 = canvas->width();
@@ -4229,7 +4378,7 @@ int gLastScrollUiFrame = -1000;
4229
4378
  gea::framework::graphics::pixel::native_t *row = canvas->pixels() + canvas->rowToPhysical(y) * stride;
4230
4379
  for (int x = drawXMin; x <= drawXMax; ++x)
4231
4380
  {
4232
- const int coverage = fillQuadCoverage(xs, ys, x, y, aaSamples);
4381
+ const int coverage = quadCoverage.at(x, y);
4233
4382
  if (coverage <= 0)
4234
4383
  continue;
4235
4384
  paintCoveragePixel(row[x], command.quad.color, alpha, coverage, aaSampleCount);
@@ -4312,7 +4461,7 @@ int gLastScrollUiFrame = -1000;
4312
4461
 
4313
4462
  for (int x = aaLX0; x <= aaLX1; x++)
4314
4463
  {
4315
- const int coverage = fillQuadCoverage(xs, ys, x, y, aaSamples);
4464
+ const int coverage = quadCoverage.at(x, y);
4316
4465
  if (coverage > 0)
4317
4466
  paintCoveragePixel(row[x], quadColor, alpha, coverage, aaSampleCount);
4318
4467
  }
@@ -4333,7 +4482,7 @@ int gLastScrollUiFrame = -1000;
4333
4482
 
4334
4483
  for (int x = aaRX0; x <= aaRX1; x++)
4335
4484
  {
4336
- const int coverage = fillQuadCoverage(xs, ys, x, y, aaSamples);
4485
+ const int coverage = quadCoverage.at(x, y);
4337
4486
  if (coverage > 0)
4338
4487
  paintCoveragePixel(row[x], quadColor, alpha, coverage, aaSampleCount);
4339
4488
  }
@@ -4605,6 +4754,111 @@ int gLastScrollUiFrame = -1000;
4605
4754
  inner.blRx8 = std::max(0, s.rx8[3] - 8 * s.lineWidth); inner.blRy8 = std::max(0, s.ry8[3] - 8 * s.lineWidth);
4606
4755
  }
4607
4756
 
4757
+ // Whole-pixel answers for roundedRectContainsFast over a pixel's sample box.
4758
+ // Valid only while no two corner radii overlap along a side: the contained
4759
+ // set is then the convex rounded rect, so four inside corners mean every
4760
+ // sample is inside, and a box wholly in one corner's quadrant whose nearest
4761
+ // point lies outside that ellipse has every sample outside. The margins
4762
+ // dwarf the float rounding of the per-sample test; anything closer than
4763
+ // that is left to it.
4764
+ struct RoundedRectPixelClassifier
4765
+ {
4766
+ bool valid = false;
4767
+ bool empty = false;
4768
+ float left = 0, top = 0, right = 0, bottom = 0;
4769
+ float cx[4]{}, cy[4]{}, irx[4]{}, iry[4]{};
4770
+ bool rounded[4]{};
4771
+
4772
+ explicit RoundedRectPixelClassifier(const TransformedRoundedRectCommand &r)
4773
+ {
4774
+ if (r.lw <= 0 || r.lh <= 0)
4775
+ {
4776
+ valid = true;
4777
+ empty = true;
4778
+ return;
4779
+ }
4780
+ left = static_cast<float>(r.lx);
4781
+ top = static_cast<float>(r.ly);
4782
+ right = left + static_cast<float>(r.lw);
4783
+ bottom = top + static_cast<float>(r.lh);
4784
+ const int rx8[4] = {std::max(0, int(r.tlRx8)), std::max(0, int(r.trRx8)), std::max(0, int(r.brRx8)), std::max(0, int(r.blRx8))};
4785
+ const int ry8[4] = {std::max(0, int(r.tlRy8)), std::max(0, int(r.trRy8)), std::max(0, int(r.brRy8)), std::max(0, int(r.blRy8))};
4786
+ if (rx8[0] + rx8[1] > 8 * r.lw || rx8[3] + rx8[2] > 8 * r.lw ||
4787
+ ry8[0] + ry8[3] > 8 * r.lh || ry8[1] + ry8[2] > 8 * r.lh)
4788
+ return;
4789
+ for (int k = 0; k < 4; ++k)
4790
+ {
4791
+ rounded[k] = rx8[k] > 0 && ry8[k] > 0;
4792
+ if (!rounded[k])
4793
+ continue;
4794
+ // Below 2px the normalized margins shrink toward float error.
4795
+ if (rx8[k] < 16 || ry8[k] < 16)
4796
+ return;
4797
+ const float rx = rx8[k] * 0.125f, ry = ry8[k] * 0.125f;
4798
+ cx[k] = (k == 0 || k == 3) ? left + rx : right - rx;
4799
+ cy[k] = (k == 0 || k == 1) ? top + ry : bottom - ry;
4800
+ irx[k] = 1.0f / rx;
4801
+ iry[k] = 1.0f / ry;
4802
+ }
4803
+ valid = true;
4804
+ }
4805
+
4806
+ // The corner quadrant a point lies in: the region where the per-sample
4807
+ // test consults that ellipse.
4808
+ bool inQuadrant(int k, float px, float py, float m) const
4809
+ {
4810
+ const bool xSide = (k == 0 || k == 3) ? px < cx[k] - m : px > cx[k] + m;
4811
+ const bool ySide = (k == 0 || k == 1) ? py < cy[k] - m : py > cy[k] + m;
4812
+ return xSide && ySide;
4813
+ }
4814
+
4815
+ bool pointSurelyInside(float px, float py) const
4816
+ {
4817
+ const float m = 1e-3f;
4818
+ if (px < left + m || py < top + m || px >= right - m || py >= bottom - m)
4819
+ return false;
4820
+ for (int k = 0; k < 4; ++k)
4821
+ {
4822
+ if (!rounded[k])
4823
+ continue;
4824
+ const bool nearX = (k == 0 || k == 3) ? px < cx[k] + m : px > cx[k] - m;
4825
+ const bool nearY = (k == 0 || k == 1) ? py < cy[k] + m : py > cy[k] - m;
4826
+ if (!nearX || !nearY)
4827
+ continue;
4828
+ const float dx = (px - cx[k]) * irx[k], dy = (py - cy[k]) * iry[k];
4829
+ if (dx * dx + dy * dy > 1.0f - 1e-3f)
4830
+ return false;
4831
+ }
4832
+ return true;
4833
+ }
4834
+
4835
+ // 1: every sample inside, -1: every sample outside, 0: undecided.
4836
+ int box(float x0, float y0, float x1, float y1) const
4837
+ {
4838
+ if (empty)
4839
+ return -1;
4840
+ const float m = 1e-3f;
4841
+ if (x1 < left - m || y1 < top - m || x0 >= right + m || y0 >= bottom + m)
4842
+ return -1;
4843
+ for (int k = 0; k < 4; ++k)
4844
+ {
4845
+ if (!rounded[k])
4846
+ continue;
4847
+ const float nx = (k == 0 || k == 3) ? x1 : x0;
4848
+ const float ny = (k == 0 || k == 1) ? y1 : y0;
4849
+ if (!inQuadrant(k, nx, ny, m))
4850
+ continue;
4851
+ const float dx = (nx - cx[k]) * irx[k], dy = (ny - cy[k]) * iry[k];
4852
+ if (dx * dx + dy * dy > 1.0f + 1e-3f)
4853
+ return -1;
4854
+ }
4855
+ if (pointSurelyInside(x0, y0) && pointSurelyInside(x1, y0) &&
4856
+ pointSurelyInside(x0, y1) && pointSurelyInside(x1, y1))
4857
+ return 1;
4858
+ return 0;
4859
+ }
4860
+ };
4861
+
4608
4862
  int cssRoundedBorderCoverage(const TransformedRoundedRectCommand &outer,
4609
4863
  const TransformedRoundedRectCommand &inner,
4610
4864
  int x, int y, int samples)
@@ -4624,6 +4878,11 @@ int gLastScrollUiFrame = -1000;
4624
4878
  if (!canvas || !canvas->pixels() || s.w <= 0 || s.h <= 0 || s.lineWidth <= 0) return;
4625
4879
  TransformedRoundedRectCommand outer{}, inner{};
4626
4880
  cssRoundedBorderContours(s, outer, inner);
4881
+ // A circular 2px ring is all corner box: every pixel ran 2 x 16
4882
+ // containment tests (3.5 ms for an 88px ring on the S31). Settle whole
4883
+ // pixels first; only the ones a contour crosses are sampled.
4884
+ const RoundedRectPixelClassifier outerClass(outer), innerClass(inner);
4885
+ const bool classify = outerClass.valid && innerClass.valid;
4627
4886
  int x0, y0, x1, y1;
4628
4887
  gea::platform::display::Display::clip(&x0, &y0, &x1, &y1);
4629
4888
  x0 = std::max({x0, 0, int(s.x)}); y0 = std::max({y0, 0, int(s.y)});
@@ -4634,16 +4893,137 @@ int gLastScrollUiFrame = -1000;
4634
4893
  const int alpha = gea::platform::display::Display::alpha();
4635
4894
  if (!alpha) return;
4636
4895
  const int stride = canvas->strideBytes() / sizeof(gea::framework::graphics::pixel::native_t);
4896
+ const int full = samples * samples;
4897
+ // Only the corner boxes need sampling. Outside them both contours are
4898
+ // plain rectangles (the inner corners lie inside the outer corner
4899
+ // boxes), so a pixel is fully covered when it is outside the inner
4900
+ // rectangle and uncovered inside it -- exactly what sampling would
4901
+ // give. Rows between the corners only touch the two side bands.
4902
+ const int radiusX = (std::max({s.rx8[0], s.rx8[1], s.rx8[2], s.rx8[3]}) + 7) / 8;
4903
+ const int radiusY = (std::max({s.ry8[0], s.ry8[1], s.ry8[2], s.ry8[3]}) + 7) / 8;
4904
+ const int cornerX0 = s.x + radiusX, cornerX1 = s.x + s.w - radiusX;
4905
+ const int cornerY0 = s.y + radiusY, cornerY1 = s.y + s.h - radiusY;
4906
+ const int innerX0 = s.x + s.lineWidth, innerX1 = s.x + s.w - s.lineWidth;
4907
+ const int innerY0 = s.y + s.lineWidth, innerY1 = s.y + s.h - s.lineWidth;
4637
4908
  for (int y = y0; y <= y1; ++y) {
4638
4909
  auto *row = canvas->pixels() + canvas->rowToPhysical(y) * stride;
4910
+ const bool cornerRow = y < cornerY0 || y >= cornerY1;
4911
+ const bool innerRow = y >= innerY0 && y < innerY1;
4912
+ if (!cornerRow && innerRow) {
4913
+ for (int x = x0; x <= std::min(x1, innerX0 - 1); ++x) paintCoveragePixel(row[x], s.color, alpha, full, full);
4914
+ for (int x = std::max(x0, innerX1); x <= x1; ++x) paintCoveragePixel(row[x], s.color, alpha, full, full);
4915
+ continue;
4916
+ }
4639
4917
  for (int x = x0; x <= x1; ++x) {
4640
- const int coverage = cssRoundedBorderCoverage(outer, inner, x, y, samples);
4641
- if (coverage) paintCoveragePixel(row[x], s.color, alpha, coverage, samples * samples);
4918
+ int coverage;
4919
+ if (cornerRow && (x < cornerX0 || x >= cornerX1))
4920
+ {
4921
+ int decided = -1;
4922
+ if (classify)
4923
+ {
4924
+ const float o0 = 0.5f / samples, o1 = 1.0f - o0;
4925
+ const float bx0 = x + o0, by0 = y + o0, bx1 = x + o1, by1 = y + o1;
4926
+ const int out = outerClass.box(bx0, by0, bx1, by1);
4927
+ if (out < 0)
4928
+ decided = 0;
4929
+ else
4930
+ {
4931
+ const int in = innerClass.box(bx0, by0, bx1, by1);
4932
+ if (in > 0)
4933
+ decided = 0;
4934
+ else if (out > 0 && in < 0)
4935
+ decided = full;
4936
+ }
4937
+ }
4938
+ coverage = decided >= 0 ? decided : cssRoundedBorderCoverage(outer, inner, x, y, samples);
4939
+ }
4940
+ else
4941
+ coverage = innerRow && x >= innerX0 && x < innerX1 ? 0 : full;
4942
+ if (coverage) paintCoveragePixel(row[x], s.color, alpha, coverage, full);
4642
4943
  }
4643
4944
  }
4644
4945
  canvas->markDirty(x0, y0, x1, y1);
4645
4946
  }
4646
4947
 
4948
+ void drawProjectedImage(const DisplayCommand &command)
4949
+ {
4950
+ auto *canvas = gea::platform::display::Display::canvas();
4951
+ const auto &image = command.projectedBlit;
4952
+ if (!canvas || !image.pixels || image.sourceWidth <= 0 || image.sourceHeight <= 0 ||
4953
+ image.width <= 0 || image.height <= 0)
4954
+ return;
4955
+ if (image.backfaceHidden)
4956
+ {
4957
+ long area = 0;
4958
+ for (int i = 0; i < 4; ++i)
4959
+ {
4960
+ const int next = (i + 1) % 4;
4961
+ area += long(image.xs[i]) * image.ys[next] - long(image.xs[next]) * image.ys[i];
4962
+ }
4963
+ if (area <= 0)
4964
+ return;
4965
+ }
4966
+ double coefficients[8]{};
4967
+ if (!screenToLocalRectHomography(image.xs, image.ys, 0, 0, image.sourceWidth, image.sourceHeight,
4968
+ coefficients))
4969
+ return;
4970
+ float h[8];
4971
+ for (int i = 0; i < 8; ++i)
4972
+ h[i] = static_cast<float>(coefficients[i]);
4973
+ const bool affine = std::abs(h[6]) < 0.000000001f && std::abs(h[7]) < 0.000000001f;
4974
+ const bool rounded = image.tl || image.tr || image.br || image.bl;
4975
+ const float localScaleX = float(image.width) / image.sourceWidth;
4976
+ const float localScaleY = float(image.height) / image.sourceHeight;
4977
+ int x0, y0, x1, y1;
4978
+ gea::platform::display::Display::clip(&x0, &y0, &x1, &y1);
4979
+ x0 = std::max({x0, 0, int(command.bx)});
4980
+ y0 = std::max({y0, 0, int(command.by)});
4981
+ x1 = std::min({x1, canvas->width() - 1, command.bx + command.bw - 1});
4982
+ y1 = std::min({y1, canvas->height() - 1, command.by + command.bh - 1});
4983
+ const int globalAlpha = canvas->globalAlpha();
4984
+ for (int y = y0; y <= y1; ++y)
4985
+ {
4986
+ const float py = y + 0.5f;
4987
+ for (int x = x0; x <= x1; ++x)
4988
+ {
4989
+ const float px = x + 0.5f;
4990
+ const float divisor = affine ? 1.0f : h[6] * px + h[7] * py + 1.0f;
4991
+ if (std::abs(divisor) < 0.000001f)
4992
+ continue;
4993
+ const float inverse = affine ? 1.0f : 1.0f / divisor;
4994
+ const float sx = (h[0] * px + h[1] * py + h[2]) * inverse;
4995
+ const float sy = (h[3] * px + h[4] * py + h[5]) * inverse;
4996
+ if (!(sx >= 0 && sx < image.sourceWidth && sy >= 0 && sy < image.sourceHeight))
4997
+ continue;
4998
+ if (rounded)
4999
+ {
5000
+ const float lx = sx * localScaleX;
5001
+ const float ly = sy * localScaleY;
5002
+ const bool right = lx >= image.width / 2.0f;
5003
+ const bool bottom = ly >= image.height / 2.0f;
5004
+ const int radius =
5005
+ bottom ? (right ? image.br : image.bl) : (right ? image.tr : image.tl);
5006
+ const float r = std::min(float(radius), std::min(image.width, image.height) / 2.0f);
5007
+ const float cx = right ? image.width - lx : lx;
5008
+ const float cy = bottom ? image.height - ly : ly;
5009
+ if (r > 0 && cx < r && cy < r && (cx - r) * (cx - r) + (cy - r) * (cy - r) > r * r)
5010
+ continue;
5011
+ }
5012
+ const int source = int(sy) * image.sourceWidth + int(sx);
5013
+ const int alpha = ((image.alpha ? image.alpha[source] : 255) * globalAlpha) / 255;
5014
+ if (!alpha)
5015
+ continue;
5016
+ const auto color = alpha == 255
5017
+ ? image.pixels[source]
5018
+ : gea::framework::graphics::pixel::blendNative(
5019
+ image.pixels[source], canvas->readPixelNative(x, y), alpha);
5020
+ canvas->writePixelNativeExact(x, y, color);
5021
+ }
5022
+ }
5023
+ if (x0 <= x1 && y0 <= y1)
5024
+ canvas->markDirty(x0, y0, x1, y1);
5025
+ }
5026
+
4647
5027
  void drawTransformedRoundedRect(const DisplayCommand &command)
4648
5028
  {
4649
5029
  auto *canvas = gea::platform::display::Display::canvas();
@@ -5067,6 +5447,18 @@ int gLastScrollUiFrame = -1000;
5067
5447
  c->projectedText.x3 += dx;
5068
5448
  c->projectedText.y3 += dy;
5069
5449
  break;
5450
+ case DisplayCommandType::ReplayCanvasBatch:
5451
+ c->canvasBatch.x += dx;
5452
+ c->canvasBatch.y += dy;
5453
+ break;
5454
+ case DisplayCommandType::BlitImageProjected:
5455
+ c->projectedBlit.x += dx;
5456
+ c->projectedBlit.y += dy;
5457
+ for (int corner = 0; corner < 4; ++corner) {
5458
+ c->projectedBlit.xs[corner] += dx;
5459
+ c->projectedBlit.ys[corner] += dy;
5460
+ }
5461
+ break;
5070
5462
  case DisplayCommandType::BlitImage:
5071
5463
  c->blit.dx += dx;
5072
5464
  c->blit.dy += dy;
@@ -6463,6 +6855,13 @@ int gLastScrollUiFrame = -1000;
6463
6855
  gea::platform::display::Display::blitImage(c.blit.pixels, c.blit.alpha, c.blit.sourceWidth, c.blit.sourceHeight,
6464
6856
  c.blit.dx, c.blit.dy);
6465
6857
  break;
6858
+ case DisplayCommandType::ReplayCanvasBatch:
6859
+ CanvasRenderingContext2D::replayRetainedCanvas(c.canvasBatch.nodeId, c.canvasBatch.identity,
6860
+ c.canvasBatch.x, c.canvasBatch.y, c.canvasBatch.width, c.canvasBatch.height);
6861
+ break;
6862
+ case DisplayCommandType::BlitImageProjected:
6863
+ drawProjectedImage(c);
6864
+ break;
6466
6865
  case DisplayCommandType::BlitImageScaled:
6467
6866
  #if GEA_PIXEL_STORAGE_PACKED
6468
6867
  if (gScrollStripReplayActive) {
@@ -6554,6 +6953,8 @@ int gLastScrollUiFrame = -1000;
6554
6953
  GEA_REFRESH_PERF(__perf.treeReplayGradientUs += __replayDt);
6555
6954
  break;
6556
6955
  case DisplayCommandType::BlitImage:
6956
+ case DisplayCommandType::ReplayCanvasBatch:
6957
+ case DisplayCommandType::BlitImageProjected:
6557
6958
  case DisplayCommandType::BlitImageScaled:
6558
6959
  GEA_REFRESH_PERF(__perf.treeReplayImageUs += __replayDt);
6559
6960
  break;
@@ -6712,6 +7113,13 @@ int gLastScrollUiFrame = -1000;
6712
7113
  return;
6713
7114
  }
6714
7115
  Node *n = &Tree::instance().nodes()[id];
7116
+ // Plain leaf text fades through a retained alpha scope. Keeping the
7117
+ // scope at 255 and its glyph command at zero avoids a whole-list
7118
+ // rebuild whenever a label begins or ends a fade.
7119
+ const bool retainedTextAlpha = n->type == NodeType::Text && n->first_child < 0 &&
7120
+ !n->text.empty() && n->computedStyle().text_alpha == 255 &&
7121
+ n->layout.height >= std::max(16, static_cast<int>(n->computedStyle().font_size)) &&
7122
+ n->layout.height >= n->computedStyle().line_height;
6715
7123
  if (n->computedStyle().display == 1 || isCollapsedFlexSubtree(*n) || ViewRenderer::backfaceSubtreeHidden(*n))
6716
7124
  {
6717
7125
  state.clearNodeRange(id);
@@ -6745,7 +7153,7 @@ int gLastScrollUiFrame = -1000;
6745
7153
  }
6746
7154
  }
6747
7155
  }
6748
- if (n->computedStyle().opacity == 0 && !contributesTextMask)
7156
+ if (n->computedStyle().opacity == 0 && !contributesTextMask && !retainedTextAlpha)
6749
7157
  {
6750
7158
  state.clearNodeRange(id);
6751
7159
  return;
@@ -6781,7 +7189,7 @@ int gLastScrollUiFrame = -1000;
6781
7189
  uint8_t cur_alpha = parent_alpha;
6782
7190
  const Node *active_mask = mask_node;
6783
7191
 
6784
- if (n->computedStyle().opacity < 255)
7192
+ if (n->computedStyle().opacity < 255 || retainedTextAlpha)
6785
7193
  {
6786
7194
  cur_alpha = (parent_alpha * n->computedStyle().opacity) / 255;
6787
7195
  DisplayCommand *cmd = list.append();
@@ -6908,7 +7316,7 @@ int gLastScrollUiFrame = -1000;
6908
7316
  if (filtered && !nativeTextInput)
6909
7317
  recordFilterBlur(id, *n, DisplayCommandType::ApplyFilterBlur, cur_alpha);
6910
7318
 
6911
- if (n->computedStyle().opacity < 255)
7319
+ if (n->computedStyle().opacity < 255 || retainedTextAlpha)
6912
7320
  {
6913
7321
  DisplayCommand *cmd = list.append();
6914
7322
  if (cmd)
@@ -7649,7 +8057,44 @@ int gLastScrollUiFrame = -1000;
7649
8057
  command.clip.y + command.clip.h >= height;
7650
8058
  }
7651
8059
 
7652
- static bool canUseSimpleDirtyReplay(int width, int height)
8060
+ static bool simpleTextAlphaNode(int id, Node *nodes, int count)
8061
+ {
8062
+ if (id < 0 || id >= count || !state.hasNodeScratchFor(id)) return false;
8063
+ const Node &node = nodes[id];
8064
+ const int start = state.nodeDrawStart[id];
8065
+ const int end = state.nodeDrawEnd[id];
8066
+ return node.type == NodeType::Text && node.first_child < 0 &&
8067
+ node.computedStyle().text_alpha == 255 &&
8068
+ node.layout.height >= std::max(16, static_cast<int>(node.computedStyle().font_size)) &&
8069
+ node.layout.height >= node.computedStyle().line_height &&
8070
+ start >= 0 && end == start + 1 && end <= state.commandCount &&
8071
+ state.commands[start].type == DisplayCommandType::DrawText;
8072
+ }
8073
+
8074
+ // Only leaf text may carry partial opacity in simple replay. Its one
8075
+ // glyph command needs a local alpha, not ancestor/compositing traversal.
8076
+ struct SimpleTextAlphaScope
8077
+ {
8078
+ UniformRoundedRectBatch &batch;
8079
+ bool changed;
8080
+ SimpleTextAlphaScope(const Node &node, UniformRoundedRectBatch &rects)
8081
+ : batch(rects), changed(node.computedStyle().opacity != 255)
8082
+ {
8083
+ if (changed) {
8084
+ batch.flush();
8085
+ gea::platform::display::Display::setAlpha(node.computedStyle().opacity);
8086
+ }
8087
+ }
8088
+ ~SimpleTextAlphaScope()
8089
+ {
8090
+ if (changed) {
8091
+ batch.flush();
8092
+ gea::platform::display::Display::setAlpha(255);
8093
+ }
8094
+ }
8095
+ };
8096
+
8097
+ static bool canUseSimpleDirtyReplay(int width, int height, const DisplayReplayRegion *regions = nullptr, int count = 0)
7653
8098
  {
7654
8099
  if (DisplayList::instance().hasTextClippedBackgrounds()) return false;
7655
8100
  #if GEA_EMBEDDED_SIMPLE_REPLAY_DEBUG
@@ -7668,16 +8113,31 @@ int gLastScrollUiFrame = -1000;
7668
8113
  if ((GEA_CSS_FILTERS ? rstyle(node.computedStyle()).filter_blur_radius : 0) > 0)
7669
8114
  { SRDBG("blur node", i); return false; }
7670
8115
  if (node.computedStyle().opacity != 255 && node.computedStyle().opacity != 0)
7671
- { SRDBG("opacity node", i); return false; }
8116
+ if (!simpleTextAlphaNode(i, nodes, nodeCount))
8117
+ { SRDBG("opacity node", i); return false; }
7672
8118
  if (node.computedStyle().mask_right_fade_width > 0)
7673
8119
  { SRDBG("mask node", i); return false; }
7674
- if (hasRetainedTransformState(node))
7675
- { SRDBG("transform node", i); return false; }
8120
+ if (hasRetainedTransformState(node) || (regions && nodeHasTransformChain(i, nodes, nodeCount)))
8121
+ {
8122
+ if (!regions || count <= 0 || !state.hasNodeScratchFor(i))
8123
+ { SRDBG("transform node", i); return false; }
8124
+ // Projected command bounds include the transform of every
8125
+ // ancestor. A static transformed subtree outside all transfer
8126
+ // windows cannot affect their replay; intersecting ink still
8127
+ // requires the general path, even if its layout box is outside.
8128
+ for (int ri = 0; ri < count; ++ri)
8129
+ if (commandRangeBBoxOverlaps(state.nodeDrawStart[i], state.nodeDrawEnd[i],
8130
+ regions[ri].x0, regions[ri].y0, regions[ri].x1, regions[ri].y1))
8131
+ { SRDBG("transform region", i); return false; }
8132
+ }
7676
8133
  }
7677
8134
  for (int ci = 0; ci < state.commandCount; ci++)
7678
8135
  {
7679
8136
  const DisplayCommand &command = state.commands[ci];
7680
- if (command.type == DisplayCommandType::SetAlpha ||
8137
+ const bool plainTextAlpha = command.type == DisplayCommandType::SetAlpha &&
8138
+ command.alpha.recordParentAlpha == 255 &&
8139
+ simpleTextAlphaNode(command.alpha.nodeId, nodes, nodeCount);
8140
+ if ((command.type == DisplayCommandType::SetAlpha && command.alpha.alpha != 255 && !plainTextAlpha) ||
7681
8141
  command.type == DisplayCommandType::BeginFilterBlur ||
7682
8142
  command.type == DisplayCommandType::ApplyFilterBlur)
7683
8143
  { SRDBG("alpha/blur cmd type", (int)command.type); return false; }
@@ -7724,6 +8184,8 @@ int gLastScrollUiFrame = -1000;
7724
8184
  continue;
7725
8185
  const int start = state.nodeDrawStart[node_id];
7726
8186
  const int end = state.nodeDrawEnd[node_id];
8187
+ if (node.computedStyle().opacity == 0) continue;
8188
+ SimpleTextAlphaScope alpha(node, roundedRects);
7727
8189
  replaySimpleCommandRangeInRegions(start, end, regions, count, roundedRects);
7728
8190
  }
7729
8191
  roundedRects.flush();
@@ -7777,6 +8239,8 @@ int gLastScrollUiFrame = -1000;
7777
8239
  const int end = state.nodeDrawEnd[node_id];
7778
8240
  if (start < 0 || end <= start || end > state.commandCount)
7779
8241
  return;
8242
+ if (node.computedStyle().opacity == 0) return;
8243
+ SimpleTextAlphaScope alpha(node, roundedRects);
7780
8244
  for (int ci = start; ci < end; ci++)
7781
8245
  {
7782
8246
  DisplayCommand *c = &state.commands[ci];
@@ -7833,6 +8297,8 @@ int gLastScrollUiFrame = -1000;
7833
8297
  const int end = state.nodeDrawEnd[node_id];
7834
8298
  if (start < 0 || end <= start || end > state.commandCount)
7835
8299
  continue;
8300
+ if (node.computedStyle().opacity == 0) continue;
8301
+ SimpleTextAlphaScope alpha(node, roundedRects);
7836
8302
  for (int ci = start; ci < end; ci++)
7837
8303
  {
7838
8304
  DisplayCommand *c = &state.commands[ci];
@@ -8443,17 +8909,23 @@ int gLastScrollUiFrame = -1000;
8443
8909
  gea::platform::display::Display::fillRect(x0, y0, x1 - x0 + 1, y1 - y0 + 1, base);
8444
8910
  }
8445
8911
 
8446
- // Painter's-algorithm occlusion cull: the LAST node (in draw order) whose
8447
- // first command is an opaque fill or image covering the region occludes
8448
- // everything drawn before it — the replay walk can start there. A page-sized
8449
- // UI (e-reader page turn) stacks several full-bleed paper fills under the
8450
- // content; each is a full PSRAM write of the region, so skipping the covered
8451
- // ones removes ~100 ms/turn on a 540×960 GRAY4 panel. Conservative: the
8452
- // cover must be untransformed, at full effective alpha,
8453
- // its node's first command, never blinking, and not shrunk by any ancestor
8454
- // overflow clip smaller than the region.
8912
+ // Painter's-algorithm occlusion cull: the LAST node (in draw order) from
8913
+ // which the opaque fills or images that open the nodes drawn from there on
8914
+ // cover the region together occludes everything drawn before it — the
8915
+ // replay walk can start there. A page-sized UI (e-reader page turn) stacks
8916
+ // several full-bleed paper fills under the content; each is a full PSRAM
8917
+ // write of the region, so skipping the covered ones removes ~100 ms/turn on
8918
+ // a 540×960 GRAY4 panel. A carousel mid-slide shows two side-by-side
8919
+ // full-height images over a page background that neither covers alone but
8920
+ // both do together. Conservative: a cover must span the region's full
8921
+ // height, be untransformed, at full effective alpha, its node's first
8922
+ // command, never blinking, and is narrowed to every ancestor overflow clip.
8455
8923
  static int GEA_RENDER_HOT_SRAM occlusionCullStartIndex(int x0, int y0, int x1, int y1, Node *nodes, int nodeCount)
8456
8924
  {
8925
+ constexpr int kMaxCovers = 8;
8926
+ int coverX0[kMaxCovers];
8927
+ int coverX1[kMaxCovers];
8928
+ int coverCount = 0;
8457
8929
  for (int oi = state.drawNodeOrderCount - 1; oi > 0; --oi)
8458
8930
  {
8459
8931
  const int node_id = state.drawNodeOrder[oi];
@@ -8466,22 +8938,42 @@ int gLastScrollUiFrame = -1000;
8466
8938
  if (start < 0 || end <= start || end > state.commandCount)
8467
8939
  continue;
8468
8940
  const DisplayCommand &c = state.commands[start];
8469
- bool covers = false;
8941
+ int spanX0 = 0;
8942
+ int spanX1 = -1;
8470
8943
  if (c.type == DisplayCommandType::FillRect)
8471
- covers = c.fill.x <= x0 && c.fill.y <= y0 &&
8472
- c.fill.x + c.fill.w - 1 >= x1 && c.fill.y + c.fill.h - 1 >= y1;
8944
+ {
8945
+ if (c.fill.y <= y0 && c.fill.y + c.fill.h - 1 >= y1)
8946
+ {
8947
+ spanX0 = c.fill.x;
8948
+ spanX1 = c.fill.x + c.fill.w - 1;
8949
+ }
8950
+ }
8473
8951
  else if (c.type == DisplayCommandType::BlitImage)
8474
- covers = c.blit.pixels && !c.blit.alpha &&
8475
- c.blit.dx <= x0 && c.blit.dy <= y0 &&
8476
- c.blit.dx + c.blit.sourceWidth - 1 >= x1 &&
8477
- c.blit.dy + c.blit.sourceHeight - 1 >= y1;
8478
- if (!covers || c.textClipOwner >= 0)
8952
+ {
8953
+ if (c.blit.pixels && !c.blit.alpha && c.blit.dy <= y0 &&
8954
+ c.blit.dy + c.blit.sourceHeight - 1 >= y1)
8955
+ {
8956
+ spanX0 = c.blit.dx;
8957
+ spanX1 = c.blit.dx + c.blit.sourceWidth - 1;
8958
+ }
8959
+ }
8960
+ if (spanX0 < x0)
8961
+ spanX0 = x0;
8962
+ if (spanX1 > x1)
8963
+ spanX1 = x1;
8964
+ if (spanX0 > spanX1 || c.textClipOwner >= 0)
8479
8965
  continue;
8480
- // The recorded paint bbox must also cover the region (guards against
8966
+ // The recorded paint bbox must also cover the span (guards against
8481
8967
  // any recording that paints less than the command rect suggests).
8482
- if (c.bx > x0 || c.by > y0 || c.bx + c.bw - 1 < x1 || c.by + c.bh - 1 < y1)
8968
+ if (c.by > y0 || c.by + c.bh - 1 < y1)
8969
+ continue;
8970
+ if (spanX0 < c.bx)
8971
+ spanX0 = c.bx;
8972
+ if (spanX1 > c.bx + c.bw - 1)
8973
+ spanX1 = c.bx + c.bw - 1;
8974
+ if (spanX0 > spanX1)
8483
8975
  continue;
8484
- // Read node state only for a command large enough to cover the region.
8976
+ // Read node state only for a command that covers part of the region.
8485
8977
  Node *n = &nodes[node_id];
8486
8978
  if (n->computedStyle().display == 1)
8487
8979
  continue;
@@ -8495,9 +8987,16 @@ int gLastScrollUiFrame = -1000;
8495
8987
  if (!nodeRecordsOverflowClip(cursor, nodes, nodeCount))
8496
8988
  continue;
8497
8989
  const Node &clip = nodes[cursor];
8498
- if (clip.layout.x > x0 || clip.layout.y > y0 ||
8499
- clip.layout.x + clip.layout.width - 1 < x1 ||
8500
- clip.layout.y + clip.layout.height - 1 < y1)
8990
+ if (clip.layout.y > y0 || clip.layout.y + clip.layout.height - 1 < y1)
8991
+ {
8992
+ clipped = true;
8993
+ break;
8994
+ }
8995
+ if (spanX0 < clip.layout.x)
8996
+ spanX0 = clip.layout.x;
8997
+ if (spanX1 > clip.layout.x + clip.layout.width - 1)
8998
+ spanX1 = clip.layout.x + clip.layout.width - 1;
8999
+ if (spanX0 > spanX1)
8501
9000
  {
8502
9001
  clipped = true;
8503
9002
  break;
@@ -8505,7 +9004,29 @@ int gLastScrollUiFrame = -1000;
8505
9004
  }
8506
9005
  if (clipped)
8507
9006
  continue;
8508
- return oi;
9007
+ if (spanX0 == x0 && spanX1 == x1)
9008
+ return oi;
9009
+ if (coverCount == kMaxCovers)
9010
+ return 0;
9011
+ coverX0[coverCount] = spanX0;
9012
+ coverX1[coverCount] = spanX1;
9013
+ ++coverCount;
9014
+ // Covered when [x0, x1] can be walked left to right through the spans.
9015
+ int reach = x0;
9016
+ for (bool advanced = true; advanced && reach <= x1;)
9017
+ {
9018
+ advanced = false;
9019
+ for (int i = 0; i < coverCount; ++i)
9020
+ {
9021
+ if (coverX0[i] <= reach && coverX1[i] >= reach)
9022
+ {
9023
+ reach = coverX1[i] + 1;
9024
+ advanced = true;
9025
+ }
9026
+ }
9027
+ }
9028
+ if (reach > x1)
9029
+ return oi;
8509
9030
  }
8510
9031
  return 0;
8511
9032
  }
@@ -8764,7 +9285,7 @@ int gLastScrollUiFrame = -1000;
8764
9285
  auto *canvas = gea::platform::display::Display::canvas();
8765
9286
  const int width = canvas ? canvas->width() : 0;
8766
9287
  const int height = canvas ? canvas->height() : 0;
8767
- if (canUseSimpleDirtyReplay(width, height))
9288
+ if (canUseSimpleDirtyReplay(width, height, regions, count))
8768
9289
  {
8769
9290
  replaySimpleDirtyRegions(regions, count);
8770
9291
  return;
@@ -9267,6 +9788,34 @@ int gLastScrollUiFrame = -1000;
9267
9788
  return command;
9268
9789
  }
9269
9790
 
9791
+ void DisplayList::retainImage(int id)
9792
+ {
9793
+ auto &images = gea::framework::graphics::ImageStore::instance();
9794
+ if (id >= 0 && id < gea::framework::graphics::kImageMax && !state.imageReferences[id])
9795
+ state.imageReferences[id] = images.retain(id);
9796
+ }
9797
+
9798
+ void DisplayList::pruneImageReferences()
9799
+ {
9800
+ bool referenced[gea::framework::graphics::kImageMax]{};
9801
+ auto &images = gea::framework::graphics::ImageStore::instance();
9802
+ for (int i = 0; i < state.commandCount; ++i) {
9803
+ const auto &command = state.commands[i];
9804
+ const gea::framework::graphics::pixel::native_t *pixels = nullptr;
9805
+ if (command.type == DisplayCommandType::BlitImage) pixels = command.blit.pixels;
9806
+ else if (command.type == DisplayCommandType::BlitImageScaled) pixels = command.scaledBlit.pixels;
9807
+ else if (command.type == DisplayCommandType::BlitImageProjected) pixels = command.projectedBlit.pixels;
9808
+ const int id = images.idForPixels(pixels);
9809
+ if (id >= 0) referenced[id] = true;
9810
+ }
9811
+ for (int id = 0; id < gea::framework::graphics::kImageMax; ++id) {
9812
+ if (state.imageReferences[id] && !referenced[id]) {
9813
+ state.imageReferences[id] = false;
9814
+ images.release(id);
9815
+ }
9816
+ }
9817
+ }
9818
+
9270
9819
  void DisplayList::clear()
9271
9820
  {
9272
9821
  Tree &tree = Tree::instance();
@@ -9276,6 +9825,7 @@ int gLastScrollUiFrame = -1000;
9276
9825
  state.filterBlurCacheHits = 0;
9277
9826
  state.filterBlurCacheMisses = 0;
9278
9827
  state.commandCount = 0;
9828
+ pruneImageReferences();
9279
9829
  state.textClippedBackgrounds = false;
9280
9830
  state.commandOverflow = false;
9281
9831
  state.drawNodeOrderCount = 0;
@@ -9291,6 +9841,7 @@ int gLastScrollUiFrame = -1000;
9291
9841
  void DisplayList::resetStorage()
9292
9842
  {
9293
9843
  state.resetStorage();
9844
+ pruneImageReferences();
9294
9845
  invalidateStaticBackdrop();
9295
9846
  }
9296
9847
 
@@ -9688,6 +10239,15 @@ int gLastScrollUiFrame = -1000;
9688
10239
  c.quad.x3 = xs[3];
9689
10240
  c.quad.y3 = ys[3];
9690
10241
  }
10242
+ else if (c.type == DisplayCommandType::BlitImageProjected)
10243
+ {
10244
+ ViewRenderer::transformedRectCorners(n, false, c.projectedBlit.x, c.projectedBlit.y,
10245
+ c.projectedBlit.width, c.projectedBlit.height, xs, ys);
10246
+ for (int corner = 0; corner < 4; ++corner) {
10247
+ c.projectedBlit.xs[corner] = xs[corner];
10248
+ c.projectedBlit.ys[corner] = ys[corner];
10249
+ }
10250
+ }
9691
10251
  else if (c.type == DisplayCommandType::DrawProjectedText)
9692
10252
  {
9693
10253
  ViewRenderer::transformedRectCorners(n, false, c.projectedText.srcX, c.projectedText.srcY,
@@ -10047,6 +10607,12 @@ int gLastScrollUiFrame = -1000;
10047
10607
  c.bw = 0;
10048
10608
  c.bh = 0;
10049
10609
  }
10610
+ else if (c.type == DisplayCommandType::BlitImageProjected && c.projectedBlit.pixels == pixels)
10611
+ {
10612
+ c.projectedBlit.pixels = nullptr;
10613
+ c.projectedBlit.alpha = nullptr;
10614
+ c.bw = c.bh = 0;
10615
+ }
10050
10616
  else if (c.type == DisplayCommandType::BlitImageScaled && c.scaledBlit.pixels == pixels)
10051
10617
  {
10052
10618
  c.scaledBlit.pixels = nullptr;
@@ -11130,6 +11696,8 @@ int gLastScrollUiFrame = -1000;
11130
11696
  case DisplayCommandType::DrawProjectedText:
11131
11697
  case DisplayCommandType::DrawText:
11132
11698
  case DisplayCommandType::BlitImage:
11699
+ case DisplayCommandType::ReplayCanvasBatch:
11700
+ case DisplayCommandType::BlitImageProjected:
11133
11701
  case DisplayCommandType::BlitImageScaled:
11134
11702
  case DisplayCommandType::FillTransformedLinearGradient:
11135
11703
  case DisplayCommandType::FillTransformedRoundedRect:
@@ -12407,6 +12975,7 @@ int gLastScrollUiFrame = -1000;
12407
12975
  state.nodeDrawEnd[node] = -1;
12408
12976
  }
12409
12977
  state.shiftNodeDrawRangesAfter(end, delta, node);
12978
+ pruneImageReferences();
12410
12979
  return true;
12411
12980
  }
12412
12981
 
@@ -12495,6 +13064,11 @@ int gLastScrollUiFrame = -1000;
12495
13064
  return DisplayCommandReplayer::canReplaySimpleDirtyRegions(width, height);
12496
13065
  }
12497
13066
 
13067
+ bool DisplayList::canReplaySimpleDirtyRegions(int width, int height, const DisplayReplayRegion *regions, int count) const
13068
+ {
13069
+ return regions && count > 0 && DisplayCommandReplayer::canUseSimpleDirtyReplay(width, height, regions, count);
13070
+ }
13071
+
12498
13072
  void DisplayList::replaySimpleClippedDirtyRegion(int x0, int y0, int x1, int y1, int origin)
12499
13073
  {
12500
13074
  const auto &tree = Tree::instance();