@geastack/engine 0.1.27 → 0.1.29

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
@@ -144,11 +144,29 @@ void glyphStampCacheReset(); // defined below (same anon namespace); used by se
144
144
  #if GEA_EMBEDDED_TEXT_SPRITE_CACHE
145
145
  namespace {
146
146
 
147
- constexpr int kTextSpriteSlots = 4;
147
+ // With PSRAM, small text is cached too: re-rasterizing a label from the flash
148
+ // atlas misses the cache on every glyph row (~40 cycles/px measured on the
149
+ // ESP32-S31 for 17-28px labels, 3 ms/frame for a weather screen's 20 labels),
150
+ // while a sprite's RLE runs are contiguous. A dynamic list with more distinct
151
+ // strings than slots never reaches a second sighting, so it never builds.
152
+ // Without PSRAM the sprites would eat internal RAM, so only large text caches.
153
+ #ifndef GEA_EMBEDDED_TEXT_SPRITE_SLOTS
154
+ #if defined(CONFIG_SPIRAM) && CONFIG_SPIRAM
155
+ #define GEA_EMBEDDED_TEXT_SPRITE_SLOTS 32
156
+ #else
157
+ #define GEA_EMBEDDED_TEXT_SPRITE_SLOTS 4
158
+ #endif
159
+ #endif
160
+ #ifndef GEA_EMBEDDED_TEXT_SPRITE_MIN_SIZE_PX
161
+ #if defined(CONFIG_SPIRAM) && CONFIG_SPIRAM
162
+ #define GEA_EMBEDDED_TEXT_SPRITE_MIN_SIZE_PX 12
163
+ #else
164
+ #define GEA_EMBEDDED_TEXT_SPRITE_MIN_SIZE_PX 40
165
+ #endif
166
+ #endif
167
+ constexpr int kTextSpriteSlots = GEA_EMBEDDED_TEXT_SPRITE_SLOTS;
148
168
  constexpr int kTextSpriteMaxLen = 63;
149
- // Only cache genuinely large text. The per-frame re-raster of small UI text is
150
- // cheap, and caching it would risk churn (alloc/blit overhead) on dynamic lists.
151
- constexpr int kTextSpriteMinSizePx = 40;
169
+ constexpr int kTextSpriteMinSizePx = GEA_EMBEDDED_TEXT_SPRITE_MIN_SIZE_PX;
152
170
  // Skip caching anything whose sprite would be larger than this (a runaway title).
153
171
  constexpr std::size_t kTextSpriteMaxBytes = 768u * 1024u;
154
172
 
@@ -266,7 +284,7 @@ void adoptHotTextSprite(const TextSprite &s)
266
284
 
267
285
  TextSprite &textSpriteFor(const char *text, const RasterizedFont &font)
268
286
  {
269
- // Search all four slots: a direct-mapped hash makes alternating labels
287
+ // Search every slot: a direct-mapped hash makes alternating labels
270
288
  // such as "FPS: 59" and "FPS: 60" evict each other. Hits stay read-only;
271
289
  // a four-byte replacement cursor advances only for a new cache key.
272
290
  for (TextSprite &candidate : gTextSprites) {
@@ -1403,6 +1421,18 @@ void Canvas::blendCoverageSpan(int y, int x0, int count, std::uint8_t coverage,
1403
1421
  if (markRowDirty) markDirty(begin, y, end, y);
1404
1422
  }
1405
1423
 
1424
+ #if !GEA_PIXEL_FORMAT_IS_8888 && !GEA_PIXEL_FORMAT_IS_GRAY && !GEA_PIXEL_STORAGE_PACKED
1425
+ #define GEA_CANVAS_CONST_BLEND 1
1426
+ // Spans this long amortize building the per-channel tables (128 entries).
1427
+ constexpr int kConstBlendMinSpan = 48;
1428
+ static inline void blendSpanTable(pixel::native_t *dst, int count, const pixel::ConstBlend565 &table)
1429
+ {
1430
+ for (int i = 0; i < count; i++) dst[i] = table.apply(dst[i]);
1431
+ }
1432
+ #else
1433
+ #define GEA_CANVAS_CONST_BLEND 0
1434
+ #endif
1435
+
1406
1436
  // A run blends one colour at one alpha, so the result for a given destination
1407
1437
  // pixel repeats -- and under a chart the destination is mostly one colour.
1408
1438
  void Canvas::blendSpanAlpha(int y, int phys, int x, int n, pixel::native_t color, int a)
@@ -1418,6 +1448,14 @@ void Canvas::blendSpanAlpha(int y, int phys, int x, int n, pixel::native_t color
1418
1448
  for (int i = 0; i < n; ++i) pixel::packed::blendPixel(packedRow(phys), x + i, color, a);
1419
1449
  #else
1420
1450
  pixel::native_t *p = &pixels_[phys * stride_ + x];
1451
+ #if GEA_CANVAS_CONST_BLEND
1452
+ if (n >= kConstBlendMinSpan) {
1453
+ pixel::ConstBlend565 table;
1454
+ table.prepare(color, a);
1455
+ blendSpanTable(p, n, table);
1456
+ return;
1457
+ }
1458
+ #endif
1421
1459
  pixel::native_t lastBg = p[0];
1422
1460
  pixel::native_t lastOut = pixel::blendNative(color, lastBg, a);
1423
1461
  p[0] = lastOut;
@@ -1474,6 +1512,14 @@ void Canvas::fillSpanGlobalAlpha(int phys, int x0, int count, pixel::native_t co
1474
1512
  for (int i = 0; i < count; i++) pixel::packed::blendPixel(row, x0 + i, c, a);
1475
1513
  #else
1476
1514
  pixel::native_t *dst = &pixels_[phys * stride_ + x0];
1515
+ #if GEA_CANVAS_CONST_BLEND
1516
+ if (count >= kConstBlendMinSpan) {
1517
+ pixel::ConstBlend565 table;
1518
+ table.prepare(c, a);
1519
+ blendSpanTable(dst, count, table);
1520
+ return;
1521
+ }
1522
+ #endif
1477
1523
  for (int i = 0; i < count; i++) dst[i] = pixel::blendNative(c, dst[i], a);
1478
1524
  #endif
1479
1525
  }
@@ -1982,6 +2028,17 @@ void Canvas::fillRect(int x, int y, int w, int h, pixel::native_t color)
1982
2028
  markDirty(x0, y0, x1, y1);
1983
2029
  return;
1984
2030
  }
2031
+ #endif
2032
+ #if GEA_CANVAS_CONST_BLEND
2033
+ // Every pixel blends the same colour at the same alpha: one table per call.
2034
+ if ((x1 - x0 + 1) * (y1 - y0 + 1) >= kConstBlendMinSpan) {
2035
+ pixel::ConstBlend565 table;
2036
+ table.prepare(c, a);
2037
+ for (int row = y0; row <= y1; row++)
2038
+ blendSpanTable(&pixels_[rowToPhysical(row) * stride_ + x0], x1 - x0 + 1, table);
2039
+ markDirty(x0, y0, x1, y1);
2040
+ return;
2041
+ }
1985
2042
  #endif
1986
2043
  for (int row = y0; row <= y1; row++) {
1987
2044
  #if GEA_PIXEL_STORAGE_PACKED
@@ -2070,6 +2127,24 @@ bool Canvas::fillCircleNoDirty(int cx, int cy, int r, pixel::native_t color)
2070
2127
  const ClipRect *clip = &clipStack_[clipDepth_];
2071
2128
  if (cx + r < clip->x0 || cx - r > clip->x1 || cy + r < clip->y0 || cy - r > clip->y1) return false;
2072
2129
 
2130
+ #if GEA_CANVAS_CONST_BLEND
2131
+ // A translucent disc blends one colour at one alpha: build the tables once
2132
+ // for the whole circle rather than per row.
2133
+ const bool tabled = globalAlpha_ != 255 && globalAlpha_ != 0 && r >= 8
2134
+ #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
2135
+ && !landscapeRot_
2136
+ #endif
2137
+ ;
2138
+ pixel::ConstBlend565 table;
2139
+ if (tabled) table.prepare(color, globalAlpha_);
2140
+ auto span = [&](int py, int x0, int x1) {
2141
+ if (tabled) blendSpanTable(&pixels_[rowToPhysical(py) * stride_ + x0], x1 - x0 + 1, table);
2142
+ else fillSpanGlobalAlpha(rowToPhysical(py), x0, x1 - x0 + 1, color);
2143
+ };
2144
+ #else
2145
+ auto span = [&](int py, int x0, int x1) { fillSpanGlobalAlpha(rowToPhysical(py), x0, x1 - x0 + 1, color); };
2146
+ #endif
2147
+
2073
2148
  const std::uint8_t (*spans)[2] = CanvasMath::circleRadiusSpans(r);
2074
2149
  if (!spans) {
2075
2150
  const int rr = r * r;
@@ -2091,7 +2166,7 @@ bool Canvas::fillCircleNoDirty(int cx, int cy, int r, pixel::native_t color)
2091
2166
  if (x1 >= width_) x1 = width_ - 1;
2092
2167
  if (x0 > x1) continue;
2093
2168
 
2094
- fillSpanGlobalAlpha(rowToPhysical(py), x0, x1 - x0 + 1, color);
2169
+ span(py, x0, x1);
2095
2170
  }
2096
2171
  return true;
2097
2172
  }
@@ -2116,7 +2191,7 @@ bool Canvas::fillCircleNoDirty(int cx, int cy, int r, pixel::native_t color)
2116
2191
  if (x1 >= width_) x1 = width_ - 1;
2117
2192
  if (x0 > x1) continue;
2118
2193
 
2119
- fillSpanGlobalAlpha(rowToPhysical(py), x0, x1 - x0 + 1, color);
2194
+ span(py, x0, x1);
2120
2195
  }
2121
2196
  return true;
2122
2197
  }
@@ -3174,7 +3249,10 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
3174
3249
  const std::uint16_t *bLen = slot.rleValid ? slot.rleLen : nullptr;
3175
3250
  const std::int32_t *bStart = slot.rleValid ? slot.rleRowStart : nullptr;
3176
3251
  #if GEA_EMBEDDED_TEXT_SPRITE_HOT_STAGE_BYTES > 0
3177
- if (slot.rleValid) {
3252
+ // Only large text stages: with small labels cached too, several
3253
+ // sprites blit per frame and would steal the one stage from
3254
+ // each other, re-copying every frame.
3255
+ if (slot.rleValid && slot.sizePx >= 40) {
3178
3256
  if (gHotTextSprite.owner != &slot) adoptHotTextSprite(slot);
3179
3257
  if (gHotTextSprite.owner == &slot) {
3180
3258
  bRowX0 = gHotTextSprite.rowX0;
@@ -3227,6 +3305,26 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
3227
3305
  #if GEA_EMBEDDED_TEXT_SPRITE_DIAG
3228
3306
  gTsDiag.seen++;
3229
3307
  #endif
3308
+ // Build and blit on the first sighting too: a text drawn once
3309
+ // by the glyph loop and from then on from the sprite changes
3310
+ // its pixels between those two frames (a one-frame flash of a
3311
+ // whole text row every time a full cache evicts it).
3312
+ #if GEA_EMBEDDED_TEXT_SPRITE_HOT_STAGE_BYTES > 0
3313
+ if (gHotTextSprite.owner == &slot) gHotTextSprite.owner = nullptr;
3314
+ #endif
3315
+ if (buildTextSprite(slot, text, font)) {
3316
+ #if GEA_EMBEDDED_TEXT_SPRITE_DIAG
3317
+ gTsDiag.built++;
3318
+ #endif
3319
+ slot.state = TextSprite::Cached;
3320
+ blitGlyphSprite(slot.cov, slot.rowX0, slot.rowX1,
3321
+ slot.rleValid ? slot.rleVal : nullptr,
3322
+ slot.rleValid ? slot.rleLen : nullptr,
3323
+ slot.rleValid ? slot.rleRowStart : nullptr,
3324
+ slot.minLX, slot.minLY, slot.w, slot.h, x, y, color);
3325
+ return;
3326
+ }
3327
+ slot.state = TextSprite::Empty;
3230
3328
  }
3231
3329
  }
3232
3330
  }
package/image_store.cpp CHANGED
@@ -1053,7 +1053,9 @@ void ImageStore::releaseIdleDecodes()
1053
1053
  ImageStoreLock lock(slotMutex_);
1054
1054
  for (int id = 0; id < kImageMax; ++id) {
1055
1055
  ImageSlot &image = images_[id];
1056
- if (!used_[id] || references_[id] != 0 || retired_[id]) continue;
1056
+ const bool touched = touched_[id];
1057
+ touched_[id] = false;
1058
+ if (touched || !used_[id] || references_[id] != 0 || retired_[id]) continue;
1057
1059
  if (!image.sourceData || !image.ownsPixels || !image.pixels || image.mutablePixels) continue;
1058
1060
  const std::size_t bytes = static_cast<std::size_t>(image.width) * static_cast<std::size_t>(image.height) *
1059
1061
  sizeof(pixel::native_t);
@@ -1102,8 +1104,22 @@ bool ImageStore::pixelsMayChange(const pixel::native_t *pixels) const
1102
1104
  return false;
1103
1105
  }
1104
1106
 
1105
- const pixel::native_t *ImageStore::currentPixels(int id) const { materializeDeferred(id); const ImageSlot *image = slot(id); return image ? image->pixels : nullptr; }
1106
- const std::uint8_t *ImageStore::currentAlpha(int id) const { materializeDeferred(id); const ImageSlot *image = slot(id); return image ? image->alpha : nullptr; }
1107
+ const pixel::native_t *ImageStore::currentPixels(int id) const
1108
+ {
1109
+ materializeDeferred(id);
1110
+ const ImageSlot *image = slot(id);
1111
+ if (!image) return nullptr;
1112
+ touched_[id] = true;
1113
+ return image->pixels;
1114
+ }
1115
+ const std::uint8_t *ImageStore::currentAlpha(int id) const
1116
+ {
1117
+ materializeDeferred(id);
1118
+ const ImageSlot *image = slot(id);
1119
+ if (!image) return nullptr;
1120
+ touched_[id] = true;
1121
+ return image->alpha;
1122
+ }
1107
1123
 
1108
1124
  void ImageStore::setPlaying(int id, bool playing)
1109
1125
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/engine",
3
- "version": "0.1.27",
3
+ "version": "0.1.29",
4
4
  "description": "GeaStack rendering engine, intrinsic JSX primitives, and native engine backends.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -139,7 +139,21 @@ constexpr int kRuntimeExtraGlyphs = sizeof(kRuntimeExtraCodepoints)/sizeof(int);
139
139
  constexpr int kRuntimeExtraCodepoints[] = {0x00b0, 0x2022};
140
140
  constexpr int kRuntimeExtraGlyphs = sizeof(kRuntimeExtraCodepoints) / sizeof(int);
141
141
  #endif
142
- constexpr int kRuntimeGlyphCapacity = kRuntimeAsciiCount + kRuntimeExtraGlyphs;
142
+ // Room for the glyphs an app declares beyond the fixed set above: the build's
143
+ // font generator emits generated::runtimeTtfDeclaredCodepoints() -- the raster
144
+ // baseline plus every `--gea-font-charset` the app's CSS names -- so a runtime
145
+ // TTF slot covers the same characters a baked atlas would. Each entry costs a
146
+ // Glyph in every cache slot, so the default stays 0 where there is no FreeType
147
+ // (small boards); desktop builds raise it.
148
+ #ifndef GEA_EMBEDDED_TTF_RUNTIME_DECLARED_GLYPHS
149
+ #if GEA_EMBEDDED_TTF_FREETYPE
150
+ #define GEA_EMBEDDED_TTF_RUNTIME_DECLARED_GLYPHS 128
151
+ #else
152
+ #define GEA_EMBEDDED_TTF_RUNTIME_DECLARED_GLYPHS 0
153
+ #endif
154
+ #endif
155
+ constexpr int kRuntimeDeclaredGlyphCapacity = GEA_EMBEDDED_TTF_RUNTIME_DECLARED_GLYPHS;
156
+ constexpr int kRuntimeGlyphCapacity = kRuntimeAsciiCount + kRuntimeExtraGlyphs + kRuntimeDeclaredGlyphCapacity;
143
157
  constexpr int kRuntimeFontCacheSlots = 64;
144
158
  #if GEA_EMBEDDED_TTF_FREETYPE
145
159
  constexpr int kRuntimeMaxAtlasWidth = 4096;
@@ -179,11 +193,28 @@ struct RuntimeRasterizedFontSlot {
179
193
  int atlasRowHeight = 0;
180
194
  };
181
195
 
196
+ bool runtimeFixedCodepoint(int codepoint)
197
+ {
198
+ if (codepoint >= kRuntimeAsciiFirst && codepoint <= kRuntimeAsciiLast) return true;
199
+ for (int i = 0; i < kRuntimeExtraGlyphs; ++i)
200
+ if (kRuntimeExtraCodepoints[i] == codepoint) return true;
201
+ return false;
202
+ }
203
+
182
204
  int runtimeCodepointAt(int index)
183
205
  {
184
206
  if (index >= 0 && index < kRuntimeAsciiCount) return kRuntimeAsciiFirst + index;
185
- if (index >= kRuntimeAsciiCount && index < kRuntimeGlyphCapacity) return kRuntimeExtraCodepoints[index - kRuntimeAsciiCount];
186
- return -1;
207
+ index -= kRuntimeAsciiCount;
208
+ if (index >= 0 && index < kRuntimeExtraGlyphs) return kRuntimeExtraCodepoints[index];
209
+ index -= kRuntimeExtraGlyphs;
210
+ if (index < 0 || index >= kRuntimeDeclaredGlyphCapacity) return -1;
211
+ // Declared codepoints the fixed set already covers leave a gap (-1) rather
212
+ // than a duplicate entry.
213
+ int count = 0;
214
+ const int *declared = generated::runtimeTtfDeclaredCodepoints(&count);
215
+ if (!declared || index >= count) return -1;
216
+ const int codepoint = declared[index];
217
+ return codepoint > 0 && !runtimeFixedCodepoint(codepoint) ? codepoint : -1;
187
218
  }
188
219
 
189
220
  int roundToInt(float value)
@@ -744,6 +775,12 @@ __attribute__((weak)) const std::uint8_t *lookupRuntimeTtfFontForFamily(int fami
744
775
  return nullptr;
745
776
  }
746
777
 
778
+ __attribute__((weak)) const int *runtimeTtfDeclaredCodepoints(int *count)
779
+ {
780
+ if (count) *count = 0;
781
+ return nullptr;
782
+ }
783
+
747
784
  } // namespace generated
748
785
 
749
786
  } // namespace gea::framework::graphics
@@ -14,6 +14,98 @@
14
14
  #include <chrono>
15
15
  #include <cmath>
16
16
  #include <cstdio>
17
+ #if defined(ESP_PLATFORM)
18
+ // Async-present displays fill each draw buffer with the frame's backdrop off the
19
+ // app's thread (targets/esp32/display.cpp); absent elsewhere.
20
+ extern "C" bool gea_display_backdrop(int imageId, const std::uint16_t *pixels, int width, int height, int dx, int dy)
21
+ __attribute__((weak));
22
+ extern "C" bool gea_display_backdrop_overlay(int index, int x, int y, int w, int h, std::uint16_t color, int alpha)
23
+ __attribute__((weak));
24
+ extern "C" bool gea_display_backdrop_prefix_end(int matched) __attribute__((weak));
25
+ #endif
26
+
27
+ namespace {
28
+ // The frame's backdrop prefix: the opaque full-panel image, then any
29
+ // translucent full-colour rectangles drawn before anything else (a dusk tint,
30
+ // a glass band). The display can hand the frame a draw buffer that already
31
+ // holds the image with those rectangles blended in; the rectangles it holds
32
+ // are skipped here. The prefix ends at the first other draw: if the buffer
33
+ // held more rectangles than the frame asked for, the image is blitted again
34
+ // and the skipped rectangles drawn, which is exact because nothing else has
35
+ // been drawn yet.
36
+ struct BackdropPrefix {
37
+ struct Rect { int x, y, w, h; gea::framework::graphics::pixel::native_t color; std::uint8_t alpha; };
38
+ static constexpr int kMaxOverlays = 4;
39
+ bool active = false;
40
+ bool imageInBuffer = false;
41
+ bool inOverlay = false; // an overlay candidate is drawing: do not end the prefix
42
+ int count = 0; // overlays seen
43
+ int skipped = 0; // leading overlays found in the buffer
44
+ Rect rects[kMaxOverlays];
45
+ gea::framework::graphics::Canvas *surface = nullptr;
46
+ const gea::framework::graphics::pixel::native_t *pixels = nullptr;
47
+ int srcW = 0, srcH = 0, dx = 0, dy = 0;
48
+ };
49
+ BackdropPrefix gBackdropPrefix;
50
+
51
+ // From here on the draw buffer must hold exactly the rectangles skipped so far.
52
+ void settleBackdropBuffer()
53
+ {
54
+ BackdropPrefix &prefix = gBackdropPrefix;
55
+ if (!prefix.imageInBuffer) return;
56
+ prefix.imageInBuffer = false;
57
+ #if defined(ESP_PLATFORM)
58
+ if (!gea_display_backdrop_prefix_end || gea_display_backdrop_prefix_end(prefix.skipped)) return;
59
+ // The buffer held rectangles this frame did not draw: start it over.
60
+ auto &surface = *prefix.surface;
61
+ const std::uint8_t alpha = surface.globalAlpha();
62
+ surface.setGlobalAlpha(255);
63
+ surface.drawImage(prefix.pixels, nullptr, prefix.srcW, prefix.srcH, prefix.dx, prefix.dy);
64
+ for (int i = 0; i < prefix.skipped; i++) {
65
+ const BackdropPrefix::Rect &r = prefix.rects[i];
66
+ surface.setGlobalAlpha(r.alpha);
67
+ surface.fillRect(r.x, r.y, r.w, r.h, r.color);
68
+ }
69
+ surface.setGlobalAlpha(alpha);
70
+ #endif
71
+ }
72
+
73
+ void endBackdropPrefix()
74
+ {
75
+ BackdropPrefix &prefix = gBackdropPrefix;
76
+ if (!prefix.active || prefix.inOverlay) return;
77
+ settleBackdropBuffer();
78
+ prefix.active = false;
79
+ }
80
+ } // namespace
81
+
82
+ // 0: not part of the prefix (it ends here); 1: part of it, draw it; 2: part
83
+ // of it and already in the buffer, skip it.
84
+ static int backdropOverlay(int x, int y, int w, int h,
85
+ gea::framework::graphics::pixel::native_t color, std::uint8_t alpha)
86
+ {
87
+ BackdropPrefix &prefix = gBackdropPrefix;
88
+ #if defined(ESP_PLATFORM) && !GEA_PIXEL_FORMAT_IS_8888 && !GEA_PIXEL_FORMAT_IS_GRAY
89
+ if (!prefix.active || !gea_display_backdrop_overlay || alpha == 0 || alpha == 255 || w <= 0 || h <= 0 ||
90
+ prefix.count >= BackdropPrefix::kMaxOverlays)
91
+ return 0;
92
+ int cx0, cy0, cx1, cy1;
93
+ prefix.surface->currentClip(&cx0, &cy0, &cx1, &cy1);
94
+ if (cx0 > 0 || cy0 > 0 || cx1 < prefix.surface->width() - 1 || cy1 < prefix.surface->height() - 1) return 0;
95
+ const int index = prefix.count++;
96
+ prefix.rects[index] = BackdropPrefix::Rect{x, y, w, h, color, alpha};
97
+ const bool held = gea_display_backdrop_overlay(index, x, y, w, h, color, alpha);
98
+ if (held && prefix.imageInBuffer && prefix.skipped == index) {
99
+ prefix.skipped++;
100
+ return 2;
101
+ }
102
+ settleBackdropBuffer();
103
+ return 1;
104
+ #else
105
+ (void)prefix; (void)x; (void)y; (void)w; (void)h; (void)color; (void)alpha;
106
+ return 0;
107
+ #endif
108
+ }
17
109
  #include <cstdlib>
18
110
  #include <unordered_map>
19
111
  #include <utility>
@@ -1074,6 +1166,7 @@ gea::framework::graphics::Canvas *CanvasRenderingContext2D::canvas()
1074
1166
 
1075
1167
  gea::framework::graphics::Canvas *CanvasRenderingContext2D::drawingCanvas()
1076
1168
  {
1169
+ if (gBackdropPrefix.active) endBackdropPrefix();
1077
1170
  if (state_->presentRecording_) {
1078
1171
  auto *surface = canvas();
1079
1172
  if (surface) {
@@ -1765,7 +1858,15 @@ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
1765
1858
  gCanvasPerfStats.fillRectUs += nowUs() - started;
1766
1859
  return;
1767
1860
  }
1861
+ const int overlay = gBackdropPrefix.active ? backdropOverlay(x, y, w, h, state_->fillStyle_, state_->globalAlpha_) : 0;
1862
+ if (overlay == 2) {
1863
+ gBackdropPrefix.surface->markDirty(x, y, x + w - 1, y + h - 1);
1864
+ markDrawDirty();
1865
+ return;
1866
+ }
1867
+ gBackdropPrefix.inOverlay = overlay == 1;
1768
1868
  auto *surface = drawingCanvas();
1869
+ gBackdropPrefix.inOverlay = false;
1769
1870
  if (!surface) return;
1770
1871
  const std::int64_t started = nowUs();
1771
1872
  gCanvasPerfStats.fillRectCalls++;
@@ -2261,6 +2362,34 @@ void CanvasRenderingContext2D::fillText(const std::string &text, double x, doubl
2261
2362
  fillText(text, rounded(x), rounded(y));
2262
2363
  }
2263
2364
 
2365
+
2366
+ // A frame that starts with one opaque image covering the whole display canvas
2367
+ // (a scene illustration under the UI) may find it already in the draw buffer:
2368
+ // true when the display put it there, so the blit can be skipped. Images whose
2369
+ // pixels can change under the same pointer (app buffers, animations) never are.
2370
+ static bool backdropAlreadyDrawn(gea::framework::graphics::Canvas &surface, int imageId,
2371
+ const gea::framework::graphics::pixel::native_t *pixels,
2372
+ const std::uint8_t *alpha, int srcW, int srcH, int dx, int dy,
2373
+ bool *candidate)
2374
+ {
2375
+ *candidate = false;
2376
+ #if defined(ESP_PLATFORM) && !GEA_PIXEL_FORMAT_IS_8888 && !GEA_PIXEL_FORMAT_IS_GRAY
2377
+ if (!gea_display_backdrop) return false;
2378
+ if (alpha || surface.globalAlpha() != 255) return false;
2379
+ if (&surface != gea::platform::display::Display::canvas()) return false;
2380
+ if (dx > 0 || dy > 0 || dx + srcW < surface.width() || dy + srcH < surface.height()) return false;
2381
+ int x0, y0, x1, y1;
2382
+ if (surface.dirty(&x0, &y0, &x1, &y1)) return false; // not the frame's first draw
2383
+ auto &images = gea::framework::graphics::ImageStore::instance();
2384
+ if (images.isAnimated(imageId) || images.pixelsMayChange(pixels)) return false;
2385
+ *candidate = true;
2386
+ return gea_display_backdrop(imageId, pixels, srcW, srcH, dx, dy);
2387
+ #else
2388
+ (void)surface; (void)imageId; (void)pixels; (void)alpha; (void)srcW; (void)srcH; (void)dx; (void)dy; (void)candidate;
2389
+ return false;
2390
+ #endif
2391
+ }
2392
+
2264
2393
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
2265
2394
  {
2266
2395
  prepareBitmapOnlyDraw();
@@ -2279,7 +2408,31 @@ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
2279
2408
  auto *surface = drawingCanvas();
2280
2409
  if (!surface) return;
2281
2410
  applyDrawState(*surface);
2282
- surface->drawImage(pixels, images.currentAlpha(imageId), srcW, srcH, dx, dy);
2411
+ const std::uint8_t *alpha = images.currentAlpha(imageId);
2412
+ bool backdropCandidate = false;
2413
+ if (backdropAlreadyDrawn(*surface, imageId, pixels, alpha, srcW, srcH, dx, dy, &backdropCandidate)) {
2414
+ surface->markDirty(0, 0, surface->width() - 1, surface->height() - 1);
2415
+ gBackdropPrefix = BackdropPrefix{};
2416
+ gBackdropPrefix.active = true;
2417
+ gBackdropPrefix.imageInBuffer = true;
2418
+ gBackdropPrefix.surface = surface;
2419
+ gBackdropPrefix.pixels = pixels;
2420
+ gBackdropPrefix.srcW = srcW;
2421
+ gBackdropPrefix.srcH = srcH;
2422
+ gBackdropPrefix.dx = dx;
2423
+ gBackdropPrefix.dy = dy;
2424
+ gCanvasPerfStats.drawImageUs += nowUs() - started;
2425
+ markDrawDirty();
2426
+ return;
2427
+ }
2428
+ if (backdropCandidate) {
2429
+ // Drawn here, but its rectangles are still reported so the display can
2430
+ // build the composite for later frames.
2431
+ gBackdropPrefix = BackdropPrefix{};
2432
+ gBackdropPrefix.active = true;
2433
+ gBackdropPrefix.surface = surface;
2434
+ }
2435
+ surface->drawImage(pixels, alpha, srcW, srcH, dx, dy);
2283
2436
  gCanvasPerfStats.drawImageUs += nowUs() - started;
2284
2437
  markDrawDirty();
2285
2438
  }
@@ -2455,6 +2608,7 @@ void CanvasRenderingContext2D::drawImageTiledX(int imageId, double dx, double dy
2455
2608
 
2456
2609
  void CanvasRenderingContext2D::flush()
2457
2610
  {
2611
+ if (gBackdropPrefix.active) endBackdropPrefix();
2458
2612
  if (nodeId_ < 0) return;
2459
2613
  if (!Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2460
2614
  markDirty();