@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/canvas.cpp CHANGED
@@ -11,6 +11,7 @@
11
11
  #include <cstdio>
12
12
  #include <cstdlib>
13
13
  #include <cstring>
14
+ #include <limits>
14
15
  #include <type_traits>
15
16
  #include <vector>
16
17
 
@@ -104,6 +105,11 @@ std::uint8_t gTextStampBackdrop = 15;
104
105
 
105
106
  namespace {
106
107
  int nextUtf8Codepoint(const char *&p); // defined below (same anon namespace); used by the text sprite cache
108
+ int peekUtf8Codepoint(const char *p)
109
+ {
110
+ if (!p || !*p || *p == '\n') return 0;
111
+ return nextUtf8Codepoint(p);
112
+ }
107
113
  #if GEA_PIXEL_STORAGE_PACKED_BITS == 4
108
114
  void glyphStampCacheReset(); // defined below (same anon namespace); used by setTextSolidBackdrop
109
115
  #endif
@@ -265,7 +271,6 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
265
271
  {
266
272
  const int lineH = font.lineHeight();
267
273
  const int ascender = font.ascender();
268
- const int fallbackAdvance = font.sizePx() / 2;
269
274
 
270
275
  // Pass 1: bbox of all glyph ink relative to the pen origin (0,0).
271
276
  int penX = 0, penY = 0;
@@ -274,7 +279,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
274
279
  const int cp = nextUtf8Codepoint(p);
275
280
  if (cp == '\n') { penX = 0; penY += lineH; continue; }
276
281
  Glyph glyph{};
277
- if (!font.glyph(cp, &glyph)) { penX += fallbackAdvance; continue; }
282
+ if (!font.glyph(cp, &glyph)) { penX += font.advance(cp, peekUtf8Codepoint(p)); continue; }
278
283
  if (glyph.width > 0 && glyph.height > 0) {
279
284
  const int gx = penX + glyph.bearingX;
280
285
  const int gy = penY + ascender - glyph.bearingY;
@@ -283,7 +288,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
283
288
  if (gx + glyph.width - 1 > maxLX) maxLX = gx + glyph.width - 1;
284
289
  if (gy + glyph.height - 1 > maxLY) maxLY = gy + glyph.height - 1;
285
290
  }
286
- penX += glyph.advance;
291
+ penX += font.advance(cp, peekUtf8Codepoint(p));
287
292
  }
288
293
  if (maxLX < minLX || maxLY < minLY) return false; // no ink
289
294
 
@@ -324,7 +329,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
324
329
  const int cp = nextUtf8Codepoint(p);
325
330
  if (cp == '\n') { penX = 0; penY += lineH; continue; }
326
331
  Glyph glyph{};
327
- if (!font.glyph(cp, &glyph)) { penX += fallbackAdvance; continue; }
332
+ if (!font.glyph(cp, &glyph)) { penX += font.advance(cp, peekUtf8Codepoint(p)); continue; }
328
333
  const int gx = penX + glyph.bearingX;
329
334
  const int gy = penY + ascender - glyph.bearingY;
330
335
  for (int row = 0; row < glyph.height; row++) {
@@ -341,7 +346,7 @@ bool buildTextSprite(TextSprite &sprite, const char *text, const RasterizedFont
341
346
  if (bx > sprite.rowX1[by]) sprite.rowX1[by] = static_cast<std::int16_t>(bx);
342
347
  }
343
348
  }
344
- penX += glyph.advance;
349
+ penX += font.advance(cp, peekUtf8Codepoint(p));
345
350
  }
346
351
 
347
352
  sprite.minLX = minLX;
@@ -547,75 +552,310 @@ static inline bool roundedRectSampleContains(int x,
547
552
  return true;
548
553
  }
549
554
 
550
- static inline int roundedRectCoverage(int x,
551
- int y,
552
- int w,
553
- int h,
554
- int tl,
555
- int tr,
556
- int br,
557
- int bl,
558
- int px,
559
- int py,
560
- int samples,
561
- float kernelWidth)
555
+ // `roundedRectSampleContains` over a pixel's whole sample grid, as a bit
556
+ // mask (bit iy * samples + ix). The same zone order and the same float
557
+ // expressions, but each corner's scaled x and y distances are taken once per
558
+ // sample column and row, not once per sample, and only for the corners the
559
+ // grid reaches.
560
+ static inline std::uint32_t roundedRectSampleMask(int x,
561
+ int y,
562
+ int w,
563
+ int h,
564
+ int tl,
565
+ int tr,
566
+ int br,
567
+ int bl,
568
+ int px,
569
+ int py,
570
+ int samples,
571
+ float kernelWidth)
562
572
  {
563
- int coverage = 0;
573
+ const float left = static_cast<float>(x);
574
+ const float top = static_cast<float>(y);
575
+ const float right = left + static_cast<float>(w);
576
+ const float bottom = top + static_cast<float>(h);
577
+ float sx[4], sy[4];
578
+ for (int i = 0; i < samples; ++i) {
579
+ sx[i] = static_cast<float>(px) + antialiasOffsetWithKernel(i, samples, kernelWidth);
580
+ sy[i] = static_cast<float>(py) + antialiasOffsetWithKernel(i, samples, kernelWidth);
581
+ }
582
+ // Corner order matches the sample test: tl, tr, br, bl.
583
+ const int radii[4]{tl, tr, br, bl};
584
+ const bool leftSide[4]{true, false, false, true};
585
+ const bool topSide[4]{true, true, false, false};
586
+ std::uint32_t zoneX[4]{}, zoneY[4]{};
587
+ float dxs[4][4], dys[4][4];
588
+ for (int c = 0; c < 4; ++c) {
589
+ const int radius = radii[c];
590
+ if (radius <= 0) continue;
591
+ const float rf = static_cast<float>(radius);
592
+ const float cx = leftSide[c] ? left + rf : right - rf;
593
+ const float cy = topSide[c] ? top + rf : bottom - rf;
594
+ for (int i = 0; i < samples; ++i) {
595
+ if (leftSide[c] ? sx[i] < cx : sx[i] >= cx) zoneX[c] |= 1u << i;
596
+ if (topSide[c] ? sy[i] < cy : sy[i] >= cy) zoneY[c] |= 1u << i;
597
+ }
598
+ if (!zoneX[c] || !zoneY[c]) {
599
+ zoneX[c] = zoneY[c] = 0;
600
+ continue;
601
+ }
602
+ for (int i = 0; i < samples; ++i) {
603
+ dxs[c][i] = (sx[i] - cx) / rf;
604
+ dys[c][i] = (sy[i] - cy) / rf;
605
+ }
606
+ }
607
+ std::uint32_t mask = 0;
564
608
  for (int iy = 0; iy < samples; ++iy) {
565
- const float oy = antialiasOffsetWithKernel(iy, samples, kernelWidth);
609
+ if (sy[iy] < top || sy[iy] >= bottom) continue;
566
610
  for (int ix = 0; ix < samples; ++ix) {
567
- const float ox = antialiasOffsetWithKernel(ix, samples, kernelWidth);
568
- if (roundedRectSampleContains(x, y, w, h, tl, tr, br, bl,
569
- static_cast<float>(px) + ox,
570
- static_cast<float>(py) + oy))
571
- ++coverage;
611
+ if (sx[ix] < left || sx[ix] >= right) continue;
612
+ bool inside = true;
613
+ for (int c = 0; c < 4; ++c) {
614
+ if (!((zoneX[c] >> ix) & 1u) || !((zoneY[c] >> iy) & 1u)) continue;
615
+ const float dx = dxs[c][ix];
616
+ const float dy = dys[c][iy];
617
+ inside = dx * dx + dy * dy <= 1.0f;
618
+ break;
619
+ }
620
+ if (inside) mask |= 1u << (iy * samples + ix);
572
621
  }
573
622
  }
574
- return coverage;
623
+ return mask;
624
+ }
625
+
626
+ static inline int roundedRectCoverage(int x, int y, int w, int h, int tl, int tr, int br, int bl,
627
+ int px, int py, int samples, float kernelWidth)
628
+ {
629
+ return __builtin_popcount(roundedRectSampleMask(x, y, w, h, tl, tr, br, bl, px, py, samples, kernelWidth));
575
630
  }
576
631
 
577
- static inline int roundedRectStrokeCoverage(int x,
578
- int y,
579
- int w,
580
- int h,
581
- int tl,
582
- int tr,
583
- int br,
584
- int bl,
585
- int lineWidth,
586
- int px,
587
- int py,
588
- int samples,
589
- float kernelWidth)
632
+ static inline int roundedRectStrokeCoverage(int x, int y, int w, int h, int tl, int tr, int br, int bl,
633
+ int lineWidth, int px, int py, int samples, float kernelWidth)
590
634
  {
591
- const int innerX = x + lineWidth;
592
- const int innerY = y + lineWidth;
635
+ std::uint32_t band = roundedRectSampleMask(x, y, w, h, tl, tr, br, bl, px, py, samples, kernelWidth);
593
636
  const int innerW = w - lineWidth * 2;
594
637
  const int innerH = h - lineWidth * 2;
595
- const int innerTl = std::max(0, tl - lineWidth);
596
- const int innerTr = std::max(0, tr - lineWidth);
597
- const int innerBr = std::max(0, br - lineWidth);
598
- const int innerBl = std::max(0, bl - lineWidth);
599
- const bool hasInner = innerW > 0 && innerH > 0;
600
- int coverage = 0;
601
- for (int iy = 0; iy < samples; ++iy) {
602
- const float oy = antialiasOffsetWithKernel(iy, samples, kernelWidth);
603
- for (int ix = 0; ix < samples; ++ix) {
604
- const float ox = antialiasOffsetWithKernel(ix, samples, kernelWidth);
605
- const float sx = static_cast<float>(px) + ox;
606
- const float sy = static_cast<float>(py) + oy;
607
- if (!roundedRectSampleContains(x, y, w, h, tl, tr, br, bl, sx, sy)) continue;
608
- if (hasInner &&
609
- roundedRectSampleContains(innerX, innerY, innerW, innerH,
610
- innerTl, innerTr, innerBr, innerBl,
611
- sx, sy))
612
- continue;
613
- ++coverage;
638
+ if (band && innerW > 0 && innerH > 0)
639
+ band &= ~roundedRectSampleMask(x + lineWidth, y + lineWidth, innerW, innerH, std::max(0, tl - lineWidth),
640
+ std::max(0, tr - lineWidth), std::max(0, br - lineWidth),
641
+ std::max(0, bl - lineWidth), px, py, samples, kernelWidth);
642
+ return __builtin_popcount(band);
643
+ }
644
+
645
+ // Whether every sample in [x0, x1] x [y0, y1] passes
646
+ // `roundedRectSampleContains` (1), none does (0), or it varies (-1). Outside
647
+ // the corner zones the shape is its bounds; a span wholly inside one corner
648
+ // zone is decided by its nearest and farthest distance from that corner's
649
+ // centre, with a margin far wider than the float rounding of the per-sample
650
+ // test. Zones are taken in the sample test's order, so overlapping zones
651
+ // resolve the same way.
652
+ static inline int roundedRectSamplesDecided(int x, int y, int w, int h, int tl, int tr, int br, int bl,
653
+ float x0, float y0, float x1, float y1)
654
+ {
655
+ const float left = static_cast<float>(x);
656
+ const float top = static_cast<float>(y);
657
+ const float right = left + static_cast<float>(w);
658
+ const float bottom = top + static_cast<float>(h);
659
+ if (x1 < left || x0 >= right || y1 < top || y0 >= bottom) return 0;
660
+ if (x0 < left || x1 >= right || y0 < top || y1 >= bottom) return -1;
661
+ auto circle = [&](float cx, float cy, int radius) {
662
+ const float r2 = static_cast<float>(radius) * static_cast<float>(radius);
663
+ const float nx = cx < x0 ? x0 - cx : cx > x1 ? cx - x1 : 0.0f;
664
+ const float ny = cy < y0 ? y0 - cy : cy > y1 ? cy - y1 : 0.0f;
665
+ const float fx = std::max(std::fabs(x0 - cx), std::fabs(x1 - cx));
666
+ const float fy = std::max(std::fabs(y0 - cy), std::fabs(y1 - cy));
667
+ if (fx * fx + fy * fy <= r2 * 0.999f) return 1;
668
+ if (nx * nx + ny * ny >= r2 * 1.001f) return 0;
669
+ return -1;
670
+ };
671
+ if (tl > 0 && x0 < left + static_cast<float>(tl) && y0 < top + static_cast<float>(tl)) {
672
+ if (x1 >= left + static_cast<float>(tl) || y1 >= top + static_cast<float>(tl)) return -1;
673
+ return circle(left + static_cast<float>(tl), top + static_cast<float>(tl), tl);
674
+ }
675
+ if (tr > 0 && x1 >= right - static_cast<float>(tr) && y0 < top + static_cast<float>(tr)) {
676
+ if (x0 < right - static_cast<float>(tr) || y1 >= top + static_cast<float>(tr)) return -1;
677
+ return circle(right - static_cast<float>(tr), top + static_cast<float>(tr), tr);
678
+ }
679
+ if (br > 0 && x1 >= right - static_cast<float>(br) && y1 >= bottom - static_cast<float>(br)) {
680
+ if (x0 < right - static_cast<float>(br) || y0 < bottom - static_cast<float>(br)) return -1;
681
+ return circle(right - static_cast<float>(br), bottom - static_cast<float>(br), br);
682
+ }
683
+ if (bl > 0 && x0 < left + static_cast<float>(bl) && y1 >= bottom - static_cast<float>(bl)) {
684
+ if (x1 >= left + static_cast<float>(bl) || y0 < bottom - static_cast<float>(bl)) return -1;
685
+ return circle(left + static_cast<float>(bl), bottom - static_cast<float>(bl), bl);
686
+ }
687
+ return 1;
688
+ }
689
+
690
+ // `roundedRectCoverage` without sampling where its answer is decided. -1 =
691
+ // sample it. `o0`/`o1` are the first and last sample offsets.
692
+ static inline int roundedRectCoverageDecided(int x, int y, int w, int h, int tl, int tr, int br, int bl,
693
+ int px, int py, float o0, float o1, int sampleCount)
694
+ {
695
+ const float x0 = static_cast<float>(px) + o0, x1 = static_cast<float>(px) + o1;
696
+ const float y0 = static_cast<float>(py) + o0, y1 = static_cast<float>(py) + o1;
697
+ const int decided = roundedRectSamplesDecided(x, y, w, h, tl, tr, br, bl, x0, y0, x1, y1);
698
+ return decided < 0 ? -1 : decided ? sampleCount : 0;
699
+ }
700
+
701
+ // `roundedRectStrokeCoverage` without sampling where its answer is decided:
702
+ // a pixel whose samples are all inside the outer contour and all outside the
703
+ // inner one is fully covered, one whose samples all miss the band is not.
704
+ // -1 = sample it. `o0`/`o1` are the first and last sample offsets.
705
+ static inline int roundedRectStrokeCoverageDecided(int x, int y, int w, int h, int tl, int tr, int br, int bl,
706
+ int lineWidth, int px, int py, float o0, float o1, int sampleCount)
707
+ {
708
+ const float x0 = static_cast<float>(px) + o0, x1 = static_cast<float>(px) + o1;
709
+ const float y0 = static_cast<float>(py) + o0, y1 = static_cast<float>(py) + o1;
710
+ const int outer = roundedRectSamplesDecided(x, y, w, h, tl, tr, br, bl, x0, y0, x1, y1);
711
+ if (outer <= 0) return outer;
712
+ const int innerW = w - lineWidth * 2;
713
+ const int innerH = h - lineWidth * 2;
714
+ if (innerW <= 0 || innerH <= 0) return sampleCount;
715
+ const int inner = roundedRectSamplesDecided(x + lineWidth, y + lineWidth, innerW, innerH, std::max(0, tl - lineWidth),
716
+ std::max(0, tr - lineWidth), std::max(0, br - lineWidth),
717
+ std::max(0, bl - lineWidth), x0, y0, x1, y1);
718
+ return inner < 0 ? -1 : inner ? 0 : sampleCount;
719
+ }
720
+
721
+ // The columns of row `py` whose every sample passes `roundedRectSampleContains`,
722
+ // as [*inX0, *inX1] (empty when *inX0 > *inX1). Conservative: a column it
723
+ // leaves out is merely sampled. Each corner insets the row by its chord at the
724
+ // sample row deepest into it, widened by a margin far beyond the per-sample
725
+ // test's float rounding, so whichever zone the test picks, the sample is
726
+ // inside that zone's circle.
727
+ static inline void roundedRectInsideSpan(int x, int y, int w, int h, int tl, int tr, int br, int bl,
728
+ int py, float o0, float o1, int *inX0, int *inX1)
729
+ {
730
+ *inX0 = 0;
731
+ *inX1 = -1;
732
+ const float left = static_cast<float>(x);
733
+ const float top = static_cast<float>(y);
734
+ const float right = left + static_cast<float>(w);
735
+ const float bottom = top + static_cast<float>(h);
736
+ const float y0 = static_cast<float>(py) + o0;
737
+ const float y1 = static_cast<float>(py) + o1;
738
+ if (y0 < top || y1 >= bottom) return;
739
+ auto inset = [](int radius, float depth) {
740
+ if (radius <= 0 || depth <= 0.0f) return 0.0f;
741
+ const float r = static_cast<float>(radius);
742
+ const float chord2 = r * r - depth * depth;
743
+ return r - (chord2 > 0.0f ? std::sqrt(chord2) : 0.0f) + 0.01f;
744
+ };
745
+ const float leftInset = std::max(inset(tl, top + static_cast<float>(tl) - y0),
746
+ inset(bl, y1 - (bottom - static_cast<float>(bl))));
747
+ const float rightInset = std::max(inset(tr, top + static_cast<float>(tr) - y0),
748
+ inset(br, y1 - (bottom - static_cast<float>(br))));
749
+ // px + o0 >= left + leftInset and px + o1 < right - rightInset.
750
+ *inX0 = static_cast<int>(std::ceil(left + leftInset - o0));
751
+ *inX1 = static_cast<int>(std::ceil(right - rightInset - o1)) - 1;
752
+ }
753
+
754
+ // Columns of row `py` whose samples ([px + o0, px + o1] squared) can reach the
755
+ // rounded rect [x, x + w) x [y, y + h); every other pixel has zero coverage.
756
+ // The widest the shape gets over the row's sample band is at the band edge
757
+ // nearest each corner's centre; a pixel of margin keeps it conservative.
758
+ static inline bool roundedRectReachSpan(int x, int y, int w, int h, int tl, int tr, int br, int bl,
759
+ int py, float o0, float o1, int *outX0, int *outX1)
760
+ {
761
+ const float left = static_cast<float>(x);
762
+ const float top = static_cast<float>(y);
763
+ const float right = left + static_cast<float>(w);
764
+ const float bottom = top + static_cast<float>(h);
765
+ const float y0 = static_cast<float>(py) + o0;
766
+ const float y1 = static_cast<float>(py) + o1;
767
+ if (y1 < top - 1.0f || y0 > bottom + 1.0f) return false;
768
+ auto inset = [&](int topRadius, int bottomRadius) {
769
+ const float rt = static_cast<float>(topRadius);
770
+ const float rb = static_cast<float>(bottomRadius);
771
+ if (y1 < top + rt) {
772
+ const float d = top + rt - y1;
773
+ return rt - std::sqrt(std::max(0.0f, rt * rt - d * d));
614
774
  }
775
+ if (y0 > bottom - rb) {
776
+ const float d = y0 - (bottom - rb);
777
+ return rb - std::sqrt(std::max(0.0f, rb * rb - d * d));
778
+ }
779
+ return 0.0f;
780
+ };
781
+ const float reachLeft = left + inset(tl, bl);
782
+ const float reachRight = right - inset(tr, br);
783
+ *outX0 = static_cast<int>(std::floor(reachLeft - o1)) - 1;
784
+ *outX1 = static_cast<int>(std::ceil(reachRight - o0)) + 1;
785
+ return *outX0 <= *outX1;
786
+ }
787
+
788
+ // Antialiased coverage of a rounded rect depends only on its size, radii and
789
+ // sample count and on the pixel's offset inside it: the sample offsets are
790
+ // exact binary fractions and every test works on exact differences, so the same
791
+ // shape at another integer position samples identically. A shape repainted every
792
+ // frame (Ember's glow discs under a pulsing ring, ~2500 edge pixels at ~4 us
793
+ // each on the S31) keeps its edge coverage here, filled lazily per pixel.
794
+ //
795
+ // The band rasterizer runs on both cores. The lock guards only lookup and
796
+ // eviction; an entry in use is pinned by its refcount, and two cores filling
797
+ // the same byte write the same value.
798
+ namespace {
799
+ struct RoundedCoverageTable {
800
+ int key[7] = {};
801
+ std::atomic<int> refs{0};
802
+ std::uint32_t stamp = 0;
803
+ std::uint8_t *cells = nullptr;
804
+ int capacity = 0;
805
+ };
806
+ constexpr int kRoundedCoverageTables = 4;
807
+ constexpr int kRoundedCoverageMaxCells = 160 * 160;
808
+ RoundedCoverageTable gRoundedCoverage[kRoundedCoverageTables];
809
+ std::atomic_flag gRoundedCoverageLock = ATOMIC_FLAG_INIT;
810
+ std::uint32_t gRoundedCoverageClock = 0;
811
+
812
+ void *allocRoundedCoverageCells(int bytes)
813
+ {
814
+ #ifdef ESP_PLATFORM
815
+ void *cells = heap_caps_malloc(static_cast<std::size_t>(bytes), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
816
+ if (cells) return cells;
817
+ #endif
818
+ return std::malloc(static_cast<std::size_t>(bytes));
819
+ }
820
+
821
+ RoundedCoverageTable *acquireRoundedCoverage(int w, int h, int tl, int tr, int br, int bl, int samples)
822
+ {
823
+ const int cells = w * h;
824
+ if (cells < 32 * 32 || cells > kRoundedCoverageMaxCells) return nullptr;
825
+ const int key[7] = {w, h, tl, tr, br, bl, samples};
826
+ while (gRoundedCoverageLock.test_and_set(std::memory_order_acquire)) {}
827
+ RoundedCoverageTable *found = nullptr;
828
+ RoundedCoverageTable *victim = nullptr;
829
+ for (auto &table : gRoundedCoverage) {
830
+ if (table.cells && std::memcmp(table.key, key, sizeof(key)) == 0) { found = &table; break; }
831
+ if (table.refs.load(std::memory_order_relaxed) == 0 && (!victim || table.stamp < victim->stamp)) victim = &table;
615
832
  }
616
- return coverage;
833
+ if (!found && victim) {
834
+ if (victim->capacity < cells) {
835
+ std::free(victim->cells);
836
+ victim->cells = static_cast<std::uint8_t *>(allocRoundedCoverageCells(cells));
837
+ victim->capacity = victim->cells ? cells : 0;
838
+ }
839
+ if (victim->cells) {
840
+ std::memset(victim->cells, 0xff, static_cast<std::size_t>(cells));
841
+ std::memcpy(victim->key, key, sizeof(key));
842
+ found = victim;
843
+ }
844
+ }
845
+ if (found) {
846
+ found->refs.fetch_add(1, std::memory_order_relaxed);
847
+ found->stamp = ++gRoundedCoverageClock;
848
+ }
849
+ gRoundedCoverageLock.clear(std::memory_order_release);
850
+ return found;
617
851
  }
618
852
 
853
+ void releaseRoundedCoverage(RoundedCoverageTable *table)
854
+ {
855
+ if (table) table->refs.fetch_sub(1, std::memory_order_release);
856
+ }
857
+ } // namespace
858
+
619
859
  static inline int coverageAlpha(int alpha, int coverage, int sampleCount)
620
860
  {
621
861
  if (alpha <= 0 || coverage <= 0) return 0;
@@ -1382,7 +1622,10 @@ void Canvas::clear(pixel::native_t color)
1382
1622
  #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
1383
1623
  if (landscapeRot_) clearPixels = static_cast<std::size_t>(stride_) * width_;
1384
1624
  #endif
1385
- std::fill_n(pixels_, clearPixels, color);
1625
+ if (clearPixels <= static_cast<std::size_t>(std::numeric_limits<int>::max()))
1626
+ pixel::fillNative(pixels_, static_cast<int>(clearPixels), color);
1627
+ else
1628
+ std::fill_n(pixels_, clearPixels, color);
1386
1629
  #endif
1387
1630
  markDirty(0, 0, width_ - 1, height_ - 1);
1388
1631
  }
@@ -1513,6 +1756,17 @@ void Canvas::fillRect(int x, int y, int w, int h, pixel::native_t color)
1513
1756
  }
1514
1757
  #endif
1515
1758
  #if !GEA_PIXEL_STORAGE_PACKED
1759
+ // Entire physical rows are one contiguous span. In particular, an owned
1760
+ // canvas background needs one PIE pattern setup rather than one per row.
1761
+ const std::size_t fillPixels = static_cast<std::size_t>(count) * (y1 - y0 + 1);
1762
+ if (x0 == 0 && count == stride_ && regionH_ <= 0 &&
1763
+ fillPixels >= static_cast<std::size_t>(width_) * height_ / 4 &&
1764
+ fillPixels <= static_cast<std::size_t>(std::numeric_limits<int>::max())) {
1765
+ pixel::fillNative(pixels_ + static_cast<std::size_t>(y0) * stride_,
1766
+ static_cast<int>(fillPixels), c);
1767
+ markDirty(x0, y0, x1, y1);
1768
+ return;
1769
+ }
1516
1770
  // Large opaque fills (e.g. a full-screen page/paper background) skip the
1517
1771
  // per-pixel occlusion scan below. That scan READS every pixel just to
1518
1772
  // minimize the dirty rect — worth it for a small changed region, but pure
@@ -1673,6 +1927,16 @@ void Canvas::fillRectOpaque(int x, int y, int w, int h, pixel::native_t color)
1673
1927
  #endif
1674
1928
 
1675
1929
  const int count = x1 - x0 + 1;
1930
+ #if !GEA_PIXEL_STORAGE_PACKED
1931
+ const std::size_t fillPixels = static_cast<std::size_t>(count) * (y1 - y0 + 1);
1932
+ if (x0 == 0 && count == stride_ && regionH_ <= 0 &&
1933
+ fillPixels <= static_cast<std::size_t>(std::numeric_limits<int>::max())) {
1934
+ pixel::fillNative(pixels_ + static_cast<std::size_t>(y0) * stride_,
1935
+ static_cast<int>(fillPixels), color);
1936
+ markDirty(x0, y0, x1, y1);
1937
+ return;
1938
+ }
1939
+ #endif
1676
1940
  for (int row = y0; row <= y1; row++) {
1677
1941
  fillSpanColor(rowToPhysical(row), x0, count, color);
1678
1942
  }
@@ -1975,14 +2239,127 @@ void Canvas::drawArc(int cx, int cy, int r, int start_deg, int end_deg, pixel::n
1975
2239
  }
1976
2240
  }
1977
2241
 
2242
+ namespace {
2243
+
2244
+ #ifndef GEA_EMBEDDED_TRIANGLE_RATIO_CACHE
2245
+ #ifdef ESP_PLATFORM
2246
+ #define GEA_EMBEDDED_TRIANGLE_RATIO_CACHE 1
2247
+ #else
2248
+ #define GEA_EMBEDDED_TRIANGLE_RATIO_CACHE 0
2249
+ #endif
2250
+ #endif
2251
+
2252
+ #if GEA_EMBEDDED_TRIANGLE_RATIO_CACHE
2253
+ // Float division is a software call on embedded targets. Cache the exact
2254
+ // quotient bits, not a reciprocal: changing the rounding changes edge pixels.
2255
+ // The triangular table is bounded to 32.8 KiB and never uses internal ESP RAM.
2256
+ constexpr int kTriangleRatioMaxDenominator = 128;
2257
+ constexpr std::size_t kTriangleRatioCount =
2258
+ kTriangleRatioMaxDenominator * (kTriangleRatioMaxDenominator + 3) / 2;
2259
+ // Publication states live in internal RAM. Published PSRAM rows are immutable;
2260
+ // no atomic operations (or concurrent writes) ever touch the PSRAM allocation.
2261
+ std::atomic<float *> s_triangleRatios{nullptr};
2262
+ std::atomic<unsigned> s_triangleRatioRows[kTriangleRatioMaxDenominator + 1]{};
2263
+ // 0: uninitialized, 1: initializing, 2: ready, 3: allocation unavailable.
2264
+ std::atomic<unsigned> s_triangleRatioState{0};
2265
+
2266
+ float *triangleRatioTable()
2267
+ {
2268
+ if (auto *table = s_triangleRatios.load(std::memory_order_acquire))
2269
+ return table;
2270
+ if (s_triangleRatioState.load(std::memory_order_relaxed) != 0)
2271
+ return nullptr;
2272
+ unsigned state = 0;
2273
+ if (!s_triangleRatioState.compare_exchange_strong(state, 1, std::memory_order_relaxed))
2274
+ return nullptr;
2275
+ void *storage = nullptr;
2276
+ #ifndef GEA_TEST_TRIANGLE_RATIO_ALLOCATION_FAILURE
2277
+ #ifdef ESP_PLATFORM
2278
+ storage =
2279
+ heap_caps_malloc(kTriangleRatioCount * sizeof(float), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
2280
+ #else
2281
+ storage = std::malloc(kTriangleRatioCount * sizeof(float));
2282
+ #endif
2283
+ #endif
2284
+ if (!storage)
2285
+ {
2286
+ s_triangleRatioState.store(3, std::memory_order_relaxed);
2287
+ return nullptr;
2288
+ }
2289
+ auto *table = static_cast<float *>(storage);
2290
+ s_triangleRatios.store(table, std::memory_order_release);
2291
+ s_triangleRatioState.store(2, std::memory_order_relaxed);
2292
+ return table;
2293
+ }
2294
+ #endif
2295
+
2296
+ float triangleRatio(int numerator, int denominator)
2297
+ {
2298
+ #if GEA_EMBEDDED_TRIANGLE_RATIO_CACHE
2299
+ if (denominator > 0 && denominator <= kTriangleRatioMaxDenominator && numerator >= 0 &&
2300
+ numerator <= denominator)
2301
+ {
2302
+ if (auto *table = triangleRatioTable())
2303
+ {
2304
+ const std::size_t offset = denominator * (denominator + 1) / 2 - 1;
2305
+ auto &state = s_triangleRatioRows[denominator];
2306
+ if (state.load(std::memory_order_acquire) == 2)
2307
+ return table[offset + numerator];
2308
+ unsigned empty = 0;
2309
+ if (state.compare_exchange_strong(empty, 1, std::memory_order_relaxed))
2310
+ {
2311
+ for (int n = 0; n <= denominator; ++n)
2312
+ table[offset + n] = static_cast<float>(n) / denominator;
2313
+ state.store(2, std::memory_order_release);
2314
+ return table[offset + numerator];
2315
+ }
2316
+ // Another raster thread is filling this row. It owns all writes;
2317
+ // retain the exact division until the immutable row is published.
2318
+ }
2319
+ }
2320
+ #endif
2321
+ return static_cast<float>(numerator) / denominator;
2322
+ }
2323
+
2324
+ // All triangle entry points share the public canvas pixel definition. Truncate
2325
+ // the interpolated displacement toward zero, rather than flooring an absolute
2326
+ // fixed-point position: those differ on descending/negative triangle edges.
2327
+ void triangleRowSpan(int x0, int y0, int x1, int y1, int x2, int y2, int y, int &ax, int &bx)
2328
+ {
2329
+ const bool second = y >= y1;
2330
+ const int segmentHeight = std::max(1, second ? y2 - y1 : y1 - y0);
2331
+ const float alpha = triangleRatio(y - y0, y2 - y0);
2332
+ const float beta = triangleRatio(y - (second ? y1 : y0), segmentHeight);
2333
+ ax = x0 + static_cast<int>((x2 - x0) * alpha);
2334
+ bx = second ? x1 + static_cast<int>((x2 - x1) * beta) : x0 + static_cast<int>((x1 - x0) * beta);
2335
+ if (ax > bx)
2336
+ std::swap(ax, bx);
2337
+ }
2338
+
2339
+ } // namespace
2340
+
1978
2341
  void Canvas::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, pixel::native_t color)
1979
2342
  {
1980
- if (!pixels_) return;
1981
- if (y0 > y1) { std::swap(x0, x1); std::swap(y0, y1); }
1982
- if (y0 > y2) { std::swap(x0, x2); std::swap(y0, y2); }
1983
- if (y1 > y2) { std::swap(x1, x2); std::swap(y1, y2); }
2343
+ if (!pixels_)
2344
+ return;
2345
+ if (y0 > y1)
2346
+ {
2347
+ std::swap(x0, x1);
2348
+ std::swap(y0, y1);
2349
+ }
2350
+ if (y0 > y2)
2351
+ {
2352
+ std::swap(x0, x2);
2353
+ std::swap(y0, y2);
2354
+ }
2355
+ if (y1 > y2)
2356
+ {
2357
+ std::swap(x1, x2);
2358
+ std::swap(y1, y2);
2359
+ }
1984
2360
  int total_h = y2 - y0;
1985
- if (total_h == 0) {
2361
+ if (total_h == 0)
2362
+ {
1986
2363
  int mn = x0 < x1 ? (x0 < x2 ? x0 : x2) : (x1 < x2 ? x1 : x2);
1987
2364
  int mx = x0 > x1 ? (x0 > x2 ? x0 : x2) : (x1 > x2 ? x1 : x2);
1988
2365
  fillRect(mn, y0, mx - mn + 1, 1, color);
@@ -1993,77 +2370,110 @@ void Canvas::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, pixel:
1993
2370
  const ClipRect *clip = &clipStack_[clipDepth_];
1994
2371
  int draw_y0 = y0;
1995
2372
  int draw_y1 = y2;
1996
- if (draw_y0 < clip->y0) draw_y0 = clip->y0;
1997
- if (draw_y1 > clip->y1) draw_y1 = clip->y1;
1998
- if (draw_y0 < 0) draw_y0 = 0;
1999
- if (draw_y1 >= height_) draw_y1 = height_ - 1;
2000
- if (draw_y0 > draw_y1) return;
2001
- if (globalAlpha_ != 255 && globalAlpha_ != 0) markDirtyClipped(min_x, y0, max_x, y2);
2002
- for (int y = draw_y0; y <= draw_y1; y++) {
2003
- int second = (y >= y1);
2004
- int seg_h = second ? (y2 - y1) : (y1 - y0);
2005
- if (seg_h == 0) seg_h = 1;
2006
- float al = (float)(y - y0) / total_h;
2007
- float beta = second ? (float)(y - y1) / seg_h : (float)(y - y0) / seg_h;
2008
- int ax = x0 + (int)((x2 - x0) * al);
2009
- int bx = second ? x1 + (int)((x2 - x1) * beta) : x0 + (int)((x1 - x0) * beta);
2010
- if (ax > bx) { int t = ax; ax = bx; bx = t; }
2011
- if (ax < clip->x0) ax = clip->x0;
2012
- if (bx > clip->x1) bx = clip->x1;
2013
- if (ax < 0) ax = 0;
2014
- if (bx >= width_) bx = width_ - 1;
2015
- if (ax > bx) continue;
2373
+ if (draw_y0 < clip->y0)
2374
+ draw_y0 = clip->y0;
2375
+ if (draw_y1 > clip->y1)
2376
+ draw_y1 = clip->y1;
2377
+ if (draw_y0 < 0)
2378
+ draw_y0 = 0;
2379
+ if (draw_y1 >= height_)
2380
+ draw_y1 = height_ - 1;
2381
+ if (draw_y0 > draw_y1)
2382
+ return;
2383
+ if (globalAlpha_ != 255 && globalAlpha_ != 0)
2384
+ markDirtyClipped(min_x, y0, max_x, y2);
2385
+ for (int y = draw_y0; y <= draw_y1; y++)
2386
+ {
2387
+ int ax, bx;
2388
+ triangleRowSpan(x0, y0, x1, y1, x2, y2, y, ax, bx);
2389
+ if (ax < clip->x0)
2390
+ ax = clip->x0;
2391
+ if (bx > clip->x1)
2392
+ bx = clip->x1;
2393
+ if (ax < 0)
2394
+ ax = 0;
2395
+ if (bx >= width_)
2396
+ bx = width_ - 1;
2397
+ if (ax > bx)
2398
+ continue;
2016
2399
  const pixel::native_t c = color;
2017
2400
  #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
2018
- if (landscapeRot_) {
2019
- if (globalAlpha_ == 255) {
2020
- for (int px = ax; px <= bx; px++) rotSet(px, y, c);
2401
+ if (landscapeRot_)
2402
+ {
2403
+ if (globalAlpha_ == 255)
2404
+ {
2405
+ for (int px = ax; px <= bx; px++)
2406
+ rotSet(px, y, c);
2021
2407
  markDirty(ax, y, bx, y);
2022
- } else if (globalAlpha_ != 0) {
2408
+ }
2409
+ else if (globalAlpha_ != 0)
2410
+ {
2023
2411
  const int a = globalAlpha_;
2024
- for (int px = ax; px <= bx; px++) rotBlend(px, y, c, a);
2412
+ for (int px = ax; px <= bx; px++)
2413
+ rotBlend(px, y, c, a);
2025
2414
  }
2026
2415
  continue;
2027
2416
  }
2028
2417
  #endif
2029
2418
  #if GEA_PIXEL_STORAGE_PACKED
2030
2419
  std::uint8_t *dnib = packedRow(rowToPhysical(y));
2031
- if (globalAlpha_ == 255) {
2420
+ if (globalAlpha_ == 255)
2421
+ {
2032
2422
  const int count = bx - ax + 1;
2033
2423
  int runStart = -1;
2034
- for (int i = 0; i < count; i++) {
2035
- if (pixel::packed::get(dnib, ax + i) == c) {
2036
- if (runStart >= 0) { markDirty(ax + runStart, y, ax + i - 1, y); runStart = -1; }
2424
+ for (int i = 0; i < count; i++)
2425
+ {
2426
+ if (pixel::packed::get(dnib, ax + i) == c)
2427
+ {
2428
+ if (runStart >= 0)
2429
+ {
2430
+ markDirty(ax + runStart, y, ax + i - 1, y);
2431
+ runStart = -1;
2432
+ }
2037
2433
  continue;
2038
2434
  }
2039
- if (runStart < 0) runStart = i;
2435
+ if (runStart < 0)
2436
+ runStart = i;
2040
2437
  pixel::packed::set(dnib, ax + i, c);
2041
2438
  }
2042
- if (runStart >= 0) markDirty(ax + runStart, y, bx, y);
2043
- } else if (globalAlpha_ != 0) {
2439
+ if (runStart >= 0)
2440
+ markDirty(ax + runStart, y, bx, y);
2441
+ }
2442
+ else if (globalAlpha_ != 0)
2443
+ {
2044
2444
  const int a = globalAlpha_;
2045
- for (int px = ax; px <= bx; px++) pixel::packed::blendPixel(dnib, px, c, a);
2445
+ for (int px = ax; px <= bx; px++)
2446
+ pixel::packed::blendPixel(dnib, px, c, a);
2046
2447
  }
2047
2448
  #else
2048
2449
  pixel::native_t *dst = &pixels_[rowToPhysical(y) * stride_ + ax];
2049
- if (globalAlpha_ == 255) {
2450
+ if (globalAlpha_ == 255)
2451
+ {
2050
2452
  const int count = bx - ax + 1;
2051
2453
  int runStart = -1;
2052
- for (int i = 0; i < count; i++) {
2053
- if (dst[i] == c) {
2054
- if (runStart >= 0) {
2454
+ for (int i = 0; i < count; i++)
2455
+ {
2456
+ if (dst[i] == c)
2457
+ {
2458
+ if (runStart >= 0)
2459
+ {
2055
2460
  markDirty(ax + runStart, y, ax + i - 1, y);
2056
2461
  runStart = -1;
2057
2462
  }
2058
2463
  continue;
2059
2464
  }
2060
- if (runStart < 0) runStart = i;
2465
+ if (runStart < 0)
2466
+ runStart = i;
2061
2467
  dst[i] = c;
2062
2468
  }
2063
- if (runStart >= 0) markDirty(ax + runStart, y, bx, y);
2064
- } else if (globalAlpha_ != 0) {
2469
+ if (runStart >= 0)
2470
+ markDirty(ax + runStart, y, bx, y);
2471
+ }
2472
+ else if (globalAlpha_ != 0)
2473
+ {
2065
2474
  const int a = globalAlpha_;
2066
- for (int px = ax; px <= bx; px++, dst++) *dst = pixel::blendNative(c, *dst, a);
2475
+ for (int px = ax; px <= bx; px++, dst++)
2476
+ *dst = pixel::blendNative(c, *dst, a);
2067
2477
  }
2068
2478
  #endif
2069
2479
  }
@@ -2072,15 +2482,23 @@ void Canvas::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, pixel:
2072
2482
  // Present-replay variant: each flush chunk starts from scratch, so fillTriangle's
2073
2483
  // persistent-surface bookkeeping (per-pixel dst-compare + run-based markDirty)
2074
2484
  // is pure overhead there — flat span fills, one bbox markDirty.
2075
- // Edges step incrementally in 48.16 fixed point: one divide per edge instead of
2076
- // two float divides per scanline. The present replay rasterizes thousands of
2077
- // triangle rows per frame and those divides dominated the raster time. 64-bit
2078
- // accumulators keep arbitrary int coordinates overflow-safe.
2079
- void Canvas::fillTriangleOpaque(int x0, int y0, int x1, int y1, int x2, int y2, pixel::native_t color)
2485
+ // Span geometry shares fillTriangle's floating interpolation and truncation;
2486
+ // opaque replay skips persistent-surface comparisons and dirty run tracking.
2487
+ void Canvas::fillTriangleOpaque(int x0, int y0, int x1, int y1, int x2, int y2,
2488
+ pixel::native_t color)
2080
2489
  {
2081
- if (!pixels_) return;
2082
- if (y0 > y1) { std::swap(x0, x1); std::swap(y0, y1); }
2083
- if (y0 > y2) { std::swap(x0, x2); std::swap(y0, y2); }
2490
+ if (!pixels_)
2491
+ return;
2492
+ if (y0 > y1)
2493
+ {
2494
+ std::swap(x0, x1);
2495
+ std::swap(y0, y1);
2496
+ }
2497
+ if (y0 > y2)
2498
+ {
2499
+ std::swap(x0, x2);
2500
+ std::swap(y0, y2);
2501
+ }
2084
2502
  if (y1 > y2) { std::swap(x1, x2); std::swap(y1, y2); }
2085
2503
  const int total_h = y2 - y0;
2086
2504
  if (total_h == 0) {
@@ -2101,43 +2519,15 @@ void Canvas::fillTriangleOpaque(int x0, int y0, int x1, int y1, int x2, int y2,
2101
2519
  int clip_x1 = clip->x1 >= width_ ? width_ - 1 : clip->x1;
2102
2520
  if (clip_x0 > clip_x1) return;
2103
2521
 
2104
- // Long edge y0→y2 spans both segments with one continuous accumulator, so
2105
- // the two mesh triangles sharing it rasterize identical columns (no cracks).
2106
- const std::int64_t stepA = ((static_cast<std::int64_t>(x2) - x0) << 16) / total_h;
2107
- std::int64_t accA = (static_cast<std::int64_t>(x0) << 16) + stepA * (draw_y0 - y0);
2108
-
2109
2522
  int dirty_x0 = width_;
2110
2523
  int dirty_x1 = -1;
2111
- auto rasterRows = [&](int yStart, int yEnd, std::int64_t accB, std::int64_t stepB) {
2112
- for (int y = yStart; y <= yEnd; y++) {
2113
- int ax = static_cast<int>(accA >> 16);
2114
- int bx = static_cast<int>(accB >> 16);
2115
- accA += stepA;
2116
- accB += stepB;
2117
- if (ax > bx) { const int t = ax; ax = bx; bx = t; }
2118
- if (ax < clip_x0) ax = clip_x0;
2119
- if (bx > clip_x1) bx = clip_x1;
2120
- if (ax > bx) continue;
2121
- fillSpanColor(rowToPhysical(y), ax, bx - ax + 1, color);
2122
- if (ax < dirty_x0) dirty_x0 = ax;
2123
- if (bx > dirty_x1) dirty_x1 = bx;
2124
- }
2125
- };
2126
-
2127
- if (draw_y0 < y1) {
2128
- // Upper segment rows [y0, y1): draw_y0 < y1 with draw_y0 >= y0 implies y1 > y0.
2129
- const int yEnd = draw_y1 < y1 - 1 ? draw_y1 : y1 - 1;
2130
- const std::int64_t stepB = ((static_cast<std::int64_t>(x1) - x0) << 16) / (y1 - y0);
2131
- const std::int64_t accB = (static_cast<std::int64_t>(x0) << 16) + stepB * (draw_y0 - y0);
2132
- rasterRows(draw_y0, yEnd, accB, stepB);
2133
- }
2134
- if (draw_y1 >= y1) {
2135
- // Lower segment rows [y1, y2]; accA is already positioned at yStart.
2136
- const int yStart = draw_y0 > y1 ? draw_y0 : y1;
2137
- const int segh = y2 - y1;
2138
- const std::int64_t stepB = segh > 0 ? ((static_cast<std::int64_t>(x2) - x1) << 16) / segh : 0;
2139
- const std::int64_t accB = (static_cast<std::int64_t>(x1) << 16) + stepB * (yStart - y1);
2140
- rasterRows(yStart, draw_y1, accB, stepB);
2524
+ for (int y = draw_y0; y <= draw_y1; ++y) {
2525
+ int ax, bx;
2526
+ triangleRowSpan(x0, y0, x1, y1, x2, y2, y, ax, bx);
2527
+ ax = std::max(ax, clip_x0); bx = std::min(bx, clip_x1);
2528
+ if (ax > bx) continue;
2529
+ fillSpanColor(rowToPhysical(y), ax, bx - ax + 1, color);
2530
+ dirty_x0 = std::min(dirty_x0, ax); dirty_x1 = std::max(dirty_x1, bx);
2141
2531
  }
2142
2532
  if (dirty_x1 >= dirty_x0) markDirty(dirty_x0, draw_y0, dirty_x1, draw_y1);
2143
2533
  }
@@ -2236,33 +2626,11 @@ void Canvas::fillTrianglesOpaqueOccluded(const TriangleEntry *tris, int count, i
2236
2626
  if (mn <= mx) fillCovered(y0, mn, mx, color);
2237
2627
  continue;
2238
2628
  }
2239
- const std::int64_t stepA = ((static_cast<std::int64_t>(x2) - x0) << 16) / total_h;
2240
- std::int64_t accA = (static_cast<std::int64_t>(x0) << 16) + stepA * (dy0 - y0);
2241
- auto walk = [&](int yStart, int yEnd, std::int64_t accB, std::int64_t stepB) {
2242
- for (int y = yStart; y <= yEnd; y++) {
2243
- int ax = static_cast<int>(accA >> 16);
2244
- int bx = static_cast<int>(accB >> 16);
2245
- accA += stepA;
2246
- accB += stepB;
2247
- if (ax > bx) { const int t = ax; ax = bx; bx = t; }
2248
- if (ax < clip_x0) ax = clip_x0;
2249
- if (bx > clip_x1) bx = clip_x1;
2250
- if (ax > bx) continue;
2251
- fillCovered(y, ax, bx, color);
2252
- }
2253
- };
2254
- if (dy0 < y1) {
2255
- const int yEnd = dy1 < y1 - 1 ? dy1 : y1 - 1;
2256
- const std::int64_t stepB = ((static_cast<std::int64_t>(x1) - x0) << 16) / (y1 - y0);
2257
- const std::int64_t accB = (static_cast<std::int64_t>(x0) << 16) + stepB * (dy0 - y0);
2258
- walk(dy0, yEnd, accB, stepB);
2259
- }
2260
- if (dy1 >= y1) {
2261
- const int yStart = dy0 > y1 ? dy0 : y1;
2262
- const int segh = y2 - y1;
2263
- const std::int64_t stepB = segh > 0 ? ((static_cast<std::int64_t>(x2) - x1) << 16) / segh : 0;
2264
- const std::int64_t accB = (static_cast<std::int64_t>(x1) << 16) + stepB * (yStart - y1);
2265
- walk(yStart, dy1, accB, stepB);
2629
+ for (int y = dy0; y <= dy1; ++y) {
2630
+ int ax, bx;
2631
+ triangleRowSpan(x0, y0, x1, y1, x2, y2, y, ax, bx);
2632
+ ax = std::max(ax, clip_x0); bx = std::min(bx, clip_x1);
2633
+ if (ax <= bx) fillCovered(y, ax, bx, color);
2266
2634
  }
2267
2635
  }
2268
2636
  if (dirty_x1 >= dirty_x0 && dirty_y1 >= dirty_y0) markDirty(dirty_x0, dirty_y0, dirty_x1, dirty_y1);
@@ -2391,7 +2759,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
2391
2759
  const int cp = nextUtf8Codepoint(p);
2392
2760
  if (cp == '\n') { pen_x = lx; pen_y += font.lineHeight(); continue; }
2393
2761
  Glyph glyph{};
2394
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
2762
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
2395
2763
  const int gx = pen_x + glyph.bearingX;
2396
2764
  const int gy = pen_y + font.ascender() - glyph.bearingY;
2397
2765
  const pixel::native_t c = color;
@@ -2415,7 +2783,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
2415
2783
  #endif
2416
2784
  }
2417
2785
  }
2418
- pen_x += glyph.advance;
2786
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
2419
2787
  }
2420
2788
  }
2421
2789
 
@@ -2451,7 +2819,7 @@ void Canvas::drawTextFontFamilyOnBackground(const char *text, int x, int y, pixe
2451
2819
 
2452
2820
  SolidGlyphCacheEntry *entry = solidGlyphCacheEntry(font, color, background, cp);
2453
2821
  if (!entry) {
2454
- penX += font.sizePx() / 2;
2822
+ penX += font.advance(cp, peekUtf8Codepoint(p));
2455
2823
  continue;
2456
2824
  }
2457
2825
 
@@ -2460,7 +2828,7 @@ void Canvas::drawTextFontFamilyOnBackground(const char *text, int x, int y, pixe
2460
2828
  const int gy = penY + font.ascender() - glyph.bearingY;
2461
2829
  if (gx > clipX1 || gy > clipY1 ||
2462
2830
  gx + glyph.width - 1 < clipX0 || gy + glyph.height - 1 < clipY0) {
2463
- penX += glyph.advance;
2831
+ penX += font.advance(cp, peekUtf8Codepoint(p));
2464
2832
  continue;
2465
2833
  }
2466
2834
 
@@ -2506,7 +2874,7 @@ void Canvas::drawTextFontFamilyOnBackground(const char *text, int x, int y, pixe
2506
2874
  }
2507
2875
 
2508
2876
  if (dirtyX0 <= dirtyX1 && dirtyY0 <= dirtyY1) markDirty(dirtyX0, dirtyY0, dirtyX1, dirtyY1);
2509
- penX += glyph.advance;
2877
+ penX += font.advance(cp, peekUtf8Codepoint(p));
2510
2878
  }
2511
2879
  }
2512
2880
 
@@ -2761,7 +3129,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
2761
3129
  const int cp = nextUtf8Codepoint(p);
2762
3130
  if (cp == '\n') { pen_x = x; pen_y += font.lineHeight(); continue; }
2763
3131
  Glyph glyph{};
2764
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
3132
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
2765
3133
 
2766
3134
  int gx = pen_x + glyph.bearingX;
2767
3135
  int gy = pen_y + font.ascender() - glyph.bearingY;
@@ -2769,7 +3137,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
2769
3137
  /* Skip glyph entirely if outside clip */
2770
3138
  if (gx > clip->x1 || gy > clip->y1 ||
2771
3139
  gx + glyph.width - 1 < clip->x0 || gy + glyph.height - 1 < clip->y0) {
2772
- pen_x += glyph.advance;
3140
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
2773
3141
  continue;
2774
3142
  }
2775
3143
 
@@ -2815,7 +3183,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
2815
3183
  stampPackedGlyphRow(packedRow(rowToPhysical(gy + row)), gx,
2816
3184
  src + static_cast<std::size_t>(row) * rb, glyph.width);
2817
3185
  markDirty(gx, gy, gx + glyph.width - 1, gy + glyph.height - 1);
2818
- pen_x += glyph.advance;
3186
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
2819
3187
  continue;
2820
3188
  }
2821
3189
  }
@@ -2858,7 +3226,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
2858
3226
  }
2859
3227
  markDirty(gx + col_start, gy + row_start,
2860
3228
  gx + col_end, gy + row_end);
2861
- pen_x += glyph.advance;
3229
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
2862
3230
  }
2863
3231
  }
2864
3232
 
@@ -3034,8 +3402,7 @@ void Canvas::measureTextFont(const char *text, int max_width, int font_id, int *
3034
3402
  lines++;
3035
3403
  continue;
3036
3404
  }
3037
- Glyph glyph{};
3038
- int adv = font.glyph(cp, &glyph) ? glyph.advance : (font.sizePx() / 2);
3405
+ int adv = font.advance(cp, peekUtf8Codepoint(p));
3039
3406
  int next_w = line_w + adv;
3040
3407
  if (next_w > max_width && line_w > 0) {
3041
3408
  if (line_w > max_line_w) max_line_w = line_w;
@@ -3073,7 +3440,7 @@ void Canvas::drawTextFontFamily(const char *text, int x, int y, pixel::native_t
3073
3440
  const int cp = nextUtf8Codepoint(p);
3074
3441
  if (cp == '\n') { pen_x = x; pen_y += font.lineHeight(); continue; }
3075
3442
  Glyph glyph{};
3076
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
3443
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
3077
3444
  const int gx = pen_x + glyph.bearingX;
3078
3445
  const int gy = pen_y + font.ascender() - glyph.bearingY;
3079
3446
  const pixel::native_t c = color;
@@ -3101,7 +3468,7 @@ void Canvas::drawTextFontFamily(const char *text, int x, int y, pixel::native_t
3101
3468
  #endif
3102
3469
  }
3103
3470
  }
3104
- pen_x += glyph.advance;
3471
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
3105
3472
  }
3106
3473
  return;
3107
3474
  }
@@ -3127,7 +3494,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
3127
3494
  const int cp = nextUtf8Codepoint(p);
3128
3495
  if (cp == '\n') { pen_x = lx; pen_y += font.lineHeight(); continue; }
3129
3496
  Glyph glyph{};
3130
- if (!font.glyph(cp, &glyph)) { pen_x += font.sizePx() / 2; continue; }
3497
+ if (!font.glyph(cp, &glyph)) { pen_x += font.advance(cp, peekUtf8Codepoint(p)); continue; }
3131
3498
  const int gx = pen_x + glyph.bearingX;
3132
3499
  const int gy = pen_y + font.ascender() - glyph.bearingY;
3133
3500
  const pixel::native_t c = color;
@@ -3151,7 +3518,7 @@ void Canvas::drawTextFontFamilyRotated90(const char *text, int lx, int ly, pixel
3151
3518
  #endif
3152
3519
  }
3153
3520
  }
3154
- pen_x += glyph.advance;
3521
+ pen_x += font.advance(cp, peekUtf8Codepoint(p));
3155
3522
  }
3156
3523
  }
3157
3524
 
@@ -3186,10 +3553,9 @@ int Canvas::measureTextFontFamily(const char *text, int family_id, int size_px)
3186
3553
  lineWidth = 0;
3187
3554
  continue;
3188
3555
  }
3189
- Glyph glyph{};
3190
3556
  // Same fallback the draw loop uses for a missing glyph, so measure and
3191
3557
  // draw agree rather than drifting apart on unmapped codepoints.
3192
- lineWidth += font.glyph(cp, &glyph) ? glyph.advance : font.sizePx() / 2;
3558
+ lineWidth += font.advance(cp, peekUtf8Codepoint(p));
3193
3559
  }
3194
3560
  return lineWidth > width ? lineWidth : width;
3195
3561
  }
@@ -3439,6 +3805,8 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
3439
3805
  const float kernelWidth = useAntialias ? roundedRectAntialiasKernelWidth(aaSamples, w, h, tl, tr, br, bl) : 1.0f;
3440
3806
  const int edgePad = useAntialias ? antialiasEdgePad(kernelWidth) : 0;
3441
3807
  const bool coverageSampleFill = useAntialias && roundedRectShouldCoverageSampleFill(w, h);
3808
+ const float firstOffset = useAntialias ? antialiasOffsetWithKernel(0, aaSamples, kernelWidth) : 0.0f;
3809
+ const float lastOffset = useAntialias ? antialiasOffsetWithKernel(aaSamples - 1, aaSamples, kernelWidth) : 0.0f;
3442
3810
 
3443
3811
  const ClipRect *clip = &clipStack_[clipDepth_];
3444
3812
  int row0 = y;
@@ -3472,8 +3840,10 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
3472
3840
  pixel::native_t *dst = &pixels_[rotPhysRow(sx) * stride_ + row0];
3473
3841
  #endif
3474
3842
  for (int sy = row0; sy <= row1; ++sy) {
3475
- const int coverage = roundedRectCoverage(
3476
- x, y, w, h, tl, tr, br, bl, sx, sy, aaSamples, kernelWidth);
3843
+ int coverage = roundedRectCoverageDecided(x, y, w, h, tl, tr, br, bl, sx, sy,
3844
+ firstOffset, lastOffset, aaSampleCount);
3845
+ if (coverage < 0)
3846
+ coverage = roundedRectCoverage(x, y, w, h, tl, tr, br, bl, sx, sy, aaSamples, kernelWidth);
3477
3847
  if (coverage <= 0) {
3478
3848
  #if !GEA_PIXEL_STORAGE_PACKED
3479
3849
  ++dst;
@@ -3573,6 +3943,8 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
3573
3943
  }
3574
3944
  #endif
3575
3945
 
3946
+ RoundedCoverageTable *coverageTable = useAntialias ? acquireRoundedCoverage(w, h, tl, tr, br, bl, aaSamples) : nullptr;
3947
+ struct ReleaseCoverage { RoundedCoverageTable *table; ~ReleaseCoverage() { releaseRoundedCoverage(table); } } releaseCoverage{coverageTable};
3576
3948
  for (int sy = row0; sy <= row1; sy++) {
3577
3949
  int sx0 = width_;
3578
3950
  int sx1 = -1;
@@ -3642,7 +4014,17 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
3642
4014
 
3643
4015
  auto paintCoverageEdge = [&](int px) {
3644
4016
  if (px < clip->x0 || px > clip->x1 || px < 0 || px >= width_) return;
3645
- const int coverage = useAntialias
4017
+ std::uint8_t *cell = coverageTable ? &coverageTable->cells[(sy - y) * w + (px - x)] : nullptr;
4018
+ int decided = cell && *cell != 0xff ? *cell
4019
+ : useAntialias ? roundedRectCoverageDecided(x, y, w, h, tl, tr, br, bl, px, sy,
4020
+ firstOffset, lastOffset, aaSampleCount)
4021
+ : -1;
4022
+ if (cell && *cell == 0xff && decided < 0)
4023
+ decided = roundedRectCoverage(x, y, w, h, tl, tr, br, bl, px, sy, aaSamples, kernelWidth);
4024
+ if (cell && *cell == 0xff) *cell = static_cast<std::uint8_t>(decided);
4025
+ const int coverage = decided >= 0
4026
+ ? decided
4027
+ : useAntialias
3646
4028
  ? roundedRectCoverage(x, y, w, h, tl, tr, br, bl, px, sy, aaSamples, kernelWidth)
3647
4029
  : (roundedRectSampleContains(x,
3648
4030
  y,
@@ -3680,7 +4062,23 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
3680
4062
  };
3681
4063
 
3682
4064
  if (coverageSampleFill) {
3683
- for (int px = anyX0; px <= anyX1; ++px) paintCoverageEdge(px);
4065
+ // Columns every sample covers take the whole fill alpha: one span,
4066
+ // not a coverage test per pixel.
4067
+ int inX0, inX1;
4068
+ roundedRectInsideSpan(x, y, w, h, tl, tr, br, bl, sy, firstOffset, lastOffset, &inX0, &inX1);
4069
+ if (inX0 < anyX0) inX0 = anyX0;
4070
+ if (inX1 > anyX1) inX1 = anyX1;
4071
+ if (inX0 > inX1) {
4072
+ for (int px = anyX0; px <= anyX1; ++px) paintCoverageEdge(px);
4073
+ continue;
4074
+ }
4075
+ for (int px = anyX0; px < inX0; ++px) paintCoverageEdge(px);
4076
+ fillSpanGlobalAlpha(rowToPhysical(sy), inX0, inX1 - inX0 + 1, color);
4077
+ if (inX0 < dirtyX0) dirtyX0 = inX0;
4078
+ if (sy < dirtyY0) dirtyY0 = sy;
4079
+ if (inX1 > dirtyX1) dirtyX1 = inX1;
4080
+ if (sy > dirtyY1) dirtyY1 = sy;
4081
+ for (int px = inX1 + 1; px <= anyX1; ++px) paintCoverageEdge(px);
3684
4082
  continue;
3685
4083
  } else if (useAntialias) {
3686
4084
  if (sx0 <= sx1) {
@@ -3779,14 +4177,56 @@ void Canvas::strokeRoundedRect(int x, int y, int w, int h, int tl, int tr, int b
3779
4177
  int dirtyY1 = -1;
3780
4178
  const int aaSampleCount = aaSamples * aaSamples;
3781
4179
  const int alpha = globalAlpha_;
4180
+ const float firstOffset = antialiasOffsetWithKernel(0, aaSamples, kernelWidth);
4181
+ const float lastOffset = antialiasOffsetWithKernel(aaSamples - 1, aaSamples, kernelWidth);
4182
+ const int innerW = w - lw * 2;
4183
+ const int innerH = h - lw * 2;
4184
+ const bool hasInner = innerW > 0 && innerH > 0;
3782
4185
  for (int sy = row0; sy <= row1; ++sy) {
3783
- for (int sx = col0; sx <= col1; ++sx) {
3784
- const int coverage = roundedRectStrokeCoverage(x, y, w, h,
3785
- tl, tr, br, bl,
3786
- lw,
3787
- sx, sy,
3788
- aaSamples,
3789
- kernelWidth);
4186
+ // Only the band needs a per-pixel test. Columns wholly inside the
4187
+ // inner shape are the hole, and on a row no inner sample reaches,
4188
+ // columns wholly inside the outer shape are the band at full alpha.
4189
+ // Pixels beyond the outer shape's reach have no coverage: an 88px
4190
+ // ring tested its ~1700 corner pixels one by one (3.5 ms on the S31).
4191
+ int reachX0 = 0, reachX1 = -1;
4192
+ if (!roundedRectReachSpan(x, y, w, h, tl, tr, br, bl, sy, firstOffset, lastOffset, &reachX0, &reachX1))
4193
+ continue;
4194
+ const int rowX0 = std::max(col0, reachX0);
4195
+ const int rowX1 = std::min(col1, reachX1);
4196
+ int holeX0 = 0, holeX1 = -1;
4197
+ if (hasInner)
4198
+ roundedRectInsideSpan(x + lw, y + lw, innerW, innerH, std::max(0, tl - lw), std::max(0, tr - lw),
4199
+ std::max(0, br - lw), std::max(0, bl - lw), sy, firstOffset, lastOffset,
4200
+ &holeX0, &holeX1);
4201
+ int solidX0 = 0, solidX1 = -1;
4202
+ const float rowY0 = static_cast<float>(sy) + firstOffset;
4203
+ const float rowY1 = static_cast<float>(sy) + lastOffset;
4204
+ if (!hasInner || rowY1 < static_cast<float>(y + lw) || rowY0 >= static_cast<float>(y + lw + innerH))
4205
+ roundedRectInsideSpan(x, y, w, h, tl, tr, br, bl, sy, firstOffset, lastOffset, &solidX0, &solidX1);
4206
+ for (int sx = rowX0; sx <= rowX1; ++sx) {
4207
+ if (sx >= holeX0 && sx <= holeX1) {
4208
+ sx = holeX1;
4209
+ continue;
4210
+ }
4211
+ if (sx >= solidX0 && sx <= solidX1) {
4212
+ const int end = std::min(solidX1, rowX1);
4213
+ fillSpanGlobalAlpha(rowToPhysical(sy), sx, end - sx + 1, color);
4214
+ if (sx < dirtyX0) dirtyX0 = sx;
4215
+ if (sy < dirtyY0) dirtyY0 = sy;
4216
+ if (end > dirtyX1) dirtyX1 = end;
4217
+ if (sy > dirtyY1) dirtyY1 = sy;
4218
+ sx = end;
4219
+ continue;
4220
+ }
4221
+ int coverage = roundedRectStrokeCoverageDecided(x, y, w, h, tl, tr, br, bl, lw, sx, sy,
4222
+ firstOffset, lastOffset, aaSampleCount);
4223
+ if (coverage < 0)
4224
+ coverage = roundedRectStrokeCoverage(x, y, w, h,
4225
+ tl, tr, br, bl,
4226
+ lw,
4227
+ sx, sy,
4228
+ aaSamples,
4229
+ kernelWidth);
3790
4230
  const int effectiveAlpha = coverageAlpha(alpha, coverage, aaSampleCount);
3791
4231
  if (effectiveAlpha <= 0) continue;
3792
4232
  const pixel::native_t c = color;