@geastack/engine 0.1.28 → 0.1.30

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/css/animation.h CHANGED
@@ -15,6 +15,7 @@
15
15
  #include "../ui/style.h"
16
16
  #include "easing.h"
17
17
  #include "interpolate.h"
18
+ #include "pixel.h"
18
19
 
19
20
  namespace gea::css {
20
21
 
@@ -218,15 +219,23 @@ inline double sampleSegment(const Animation &a, const Keyframe &k0, const Keyfra
218
219
  const double eased = a.easing.kind() == EasingKind::Linear ? localT : a.easing(localT);
219
220
 
220
221
  if (a.kind == ValueKind::Color) {
221
- return static_cast<double>(
222
- lerpColor565(static_cast<int>(std::llround(k0.value)), static_cast<int>(std::llround(k1.value)), eased));
222
+ // Color keyframes hold native style values, which are 32-bit colors on
223
+ // RGBA8888 and ARGB8888 builds.
224
+ const int from = static_cast<int>(std::llround(k0.value));
225
+ const int to = static_cast<int>(std::llround(k1.value));
226
+ #if GEA_PIXEL_FORMAT_IS_8888
227
+ return static_cast<double>(lerpColor8888(from, to, eased));
228
+ #else
229
+ return static_cast<double>(lerpColor565(from, to, eased));
230
+ #endif
223
231
  }
224
232
  return lerp(k0.value, k1.value, eased);
225
233
  }
226
234
 
227
235
  // Sample the property value at timeline progress p (0..1 within one iteration):
228
236
  // find the bracketing keyframes, ease the in-segment progress, interpolate per
229
- // kind. Returns the value to apply (degrees for Angle, RGB565 for Color).
237
+ // kind. Returns the value to apply (degrees for Angle, the native style value
238
+ // for Color).
230
239
  inline double sampleTrack(const Animation &a, double p)
231
240
  {
232
241
  const auto &kf = a.keyframes;
package/css/declarative.h CHANGED
@@ -110,14 +110,24 @@ private:
110
110
  AnimationEngine::instance().start(a, nowMs);
111
111
  };
112
112
 
113
+ // Color attributes are RGB565; 8888 style values hold 8 bits per channel.
114
+ auto color = [](int rgb565) {
115
+ #if GEA_PIXEL_FORMAT_IS_8888
116
+ namespace pixel = gea::framework::graphics::pixel;
117
+ return static_cast<int>(pixel::nativeStyleValueFromRgba8888(pixel::rgba8888FromRgb565(static_cast<std::uint16_t>(rgb565))));
118
+ #else
119
+ return rgb565;
120
+ #endif
121
+ };
122
+
113
123
  if (!std::strcmp(kind, "rotate"))
114
124
  startOne(Property::TransformRotate, from, to);
115
125
  else if (!std::strcmp(kind, "opacity"))
116
126
  startOne(Property::Opacity, from, to);
117
127
  else if (!std::strcmp(kind, "color"))
118
- startOne(Property::Color, from, to);
128
+ startOne(Property::Color, color(from), color(to));
119
129
  else if (!std::strcmp(kind, "bg") || !std::strcmp(kind, "background"))
120
- startOne(Property::BackgroundColor, from, to);
130
+ startOne(Property::BackgroundColor, color(from), color(to));
121
131
  else if (!std::strcmp(kind, "width"))
122
132
  startOne(Property::Width, from, to);
123
133
  else if (!std::strcmp(kind, "height"))
package/css/interpolate.h CHANGED
@@ -6,6 +6,7 @@
6
6
  // (inline). Part of the gea CSS animation engine. See docs/geaos-grand-vision.md (M1).
7
7
 
8
8
  #include <cmath>
9
+ #include <cstdint>
9
10
 
10
11
  namespace gea::css {
11
12
 
@@ -35,4 +36,19 @@ inline int lerpColor565(int fromRgb565, int toRgb565, double t)
35
36
  return (r << 11) | (g << 5) | b;
36
37
  }
37
38
 
39
+ // Interpolate two 32-bit colors one byte at a time. Each byte is one channel in
40
+ // both RGBA8888 and ARGB8888, so the channel order does not matter here.
41
+ inline int lerpColor8888(int from, int to, double t)
42
+ {
43
+ const auto a = static_cast<std::uint32_t>(from);
44
+ const auto b = static_cast<std::uint32_t>(to);
45
+ std::uint32_t out = 0;
46
+ for (int shift = 0; shift < 32; shift += 8) {
47
+ const int ca = static_cast<int>((a >> shift) & 0xFFu);
48
+ const int cb = static_cast<int>((b >> shift) & 0xFFu);
49
+ out |= static_cast<std::uint32_t>(detail::clampi(detail::roundi(lerp(ca, cb, t)), 0, 0xFF)) << shift;
50
+ }
51
+ return static_cast<int>(out);
52
+ }
53
+
38
54
  } // namespace gea::css
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.28",
3
+ "version": "0.1.30",
4
4
  "description": "GeaStack rendering engine, intrinsic JSX primitives, and native engine backends.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -57,6 +57,6 @@
57
57
  },
58
58
  "sideEffects": false,
59
59
  "dependencies": {
60
- "@geastack/core": "^0.1.46"
60
+ "@geastack/core": "^0.1.63"
61
61
  }
62
62
  }
@@ -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();
package/ui/canvas_store.h CHANGED
@@ -20,6 +20,7 @@ struct CanvasSurfaceState {
20
20
  gea::framework::graphics::pixel::native_t *pixels = nullptr;
21
21
  int width = 0;
22
22
  int height = 0;
23
+ std::uint32_t redraws = 0;
23
24
  CanvasSurfaceState *next = nullptr;
24
25
 
25
26
  bool ownsPixels() const;
package/ui/document.cpp CHANGED
@@ -318,6 +318,7 @@ void Document::addEventListener(const char *type, gea::framework::events::EventL
318
318
  namespace
319
319
  {
320
320
  gea::framework::display::Underlay::Paint underlayPaint = nullptr;
321
+ gea::framework::display::Underlay::Describe underlayDescribe = nullptr;
321
322
  void *underlayUser = nullptr;
322
323
  int underlayX0 = 0, underlayY0 = 0, underlayX1 = -1, underlayY1 = -1;
323
324
  bool underlayOpaque = false;
@@ -328,9 +329,10 @@ bool gea::framework::display::Underlay::composited()
328
329
  return !gea::embedded::ui::Document::directCanvasContextUsed() && gea::embedded::ui::Tree::instance().mountedRoot() >= 0;
329
330
  }
330
331
 
331
- void gea::framework::display::Underlay::set(Paint paint, void *user, int x0, int y0, int x1, int y1)
332
+ void gea::framework::display::Underlay::set(Paint paint, void *user, int x0, int y0, int x1, int y1, Describe describe)
332
333
  {
333
334
  underlayPaint = paint;
335
+ underlayDescribe = describe;
334
336
  underlayUser = user;
335
337
  underlayX0 = x0;
336
338
  underlayY0 = y0;
@@ -352,10 +354,19 @@ bool gea::framework::display::Underlay::covers(int x0, int y0, int x1, int y1)
352
354
  void gea::framework::display::Underlay::clear()
353
355
  {
354
356
  underlayPaint = nullptr;
357
+ underlayDescribe = nullptr;
355
358
  underlayUser = nullptr;
356
359
  underlayOpaque = false;
357
360
  }
358
361
 
362
+ bool gea::framework::display::Underlay::describe(Sink &sink)
363
+ {
364
+ if (!underlayPaint || !underlayDescribe) return false;
365
+ sink.area(underlayX0, underlayY0, underlayX1, underlayY1);
366
+ underlayDescribe(sink, underlayUser);
367
+ return true;
368
+ }
369
+
359
370
  bool gea::framework::display::Underlay::active()
360
371
  {
361
372
  return underlayPaint != nullptr;
package/ui/internal.h CHANGED
@@ -454,6 +454,7 @@ void rootScrollImageHoldTick();
454
454
  bool reprojectDirtyRect(int *x0, int *y0, int *x1, int *y1) const;
455
455
  void filterBlurCacheStats(int *hits, int *misses) const;
456
456
  int commandCount() const;
457
+ const DisplayCommand *commandAt(int index) const;
457
458
  bool hasTextClippedBackgrounds() const;
458
459
  // Scrollbar thumbs are recorded outside every node's command range;
459
460
  // see DisplayListState::scrollbarBoxes.
@@ -7,6 +7,10 @@
7
7
  #include <cstdlib>
8
8
  #include <new>
9
9
  #include <utility>
10
+
11
+ #ifndef GEA_EMBEDDED_DEFAULT_FONT_FAMILY_ID
12
+ #define GEA_EMBEDDED_DEFAULT_FONT_FAMILY_ID -1
13
+ #endif
10
14
  #if defined(ESP_PLATFORM)
11
15
  #include "esp_heap_caps.h"
12
16
  #endif
@@ -194,7 +198,7 @@ constexpr ComputedStyle makeDefaultStyle()
194
198
  #if GEA_CSS_FONT_STRETCH
195
199
  style.font_stretch = 1000;
196
200
  #endif
197
- style.font_id = -1;
201
+ style.font_id = GEA_EMBEDDED_DEFAULT_FONT_FAMILY_ID;
198
202
  style.font_size = 0;
199
203
  #if GEA_CSS_FONT_WEIGHT
200
204
  style.font_weight = 400;
package/ui/render.cpp CHANGED
@@ -11323,6 +11323,10 @@ int gLastScrollUiFrame = -1000;
11323
11323
  }
11324
11324
 
11325
11325
  int DisplayList::commandCount() const { return state.commandCount; }
11326
+ const DisplayCommand *DisplayList::commandAt(int index) const
11327
+ {
11328
+ return state.commands && index >= 0 && index < state.commandCount ? &state.commands[index] : nullptr;
11329
+ }
11326
11330
  bool DisplayList::hasTextClippedBackgrounds() const
11327
11331
  {
11328
11332
  return state.textClippedBackgrounds;
package/ui/svg_element.h CHANGED
@@ -89,6 +89,23 @@ void validateScenes();
89
89
  void record(const Node &svgNode);
90
90
  void paint(const DisplayCommand &command);
91
91
 
92
+ struct Path;
93
+ struct StrokeStyle;
94
+
95
+ // One paint of a scene item, for a target that draws scenes itself: the fill
96
+ // of `geometry` (svg-local pixels), or with `stroke` set its stroke outline.
97
+ // A gradient paint is only flagged.
98
+ struct ScenePaint {
99
+ const Path *geometry;
100
+ const StrokeStyle *stroke;
101
+ bool evenOdd;
102
+ bool gradient;
103
+ std::uint32_t color; // native
104
+ float alpha;
105
+ };
106
+ // The paints of an svg root's scene as last recorded, in paint order.
107
+ void visitScene(int root, void (*visit)(void *user, const ScenePaint &paint), void *user);
108
+
92
109
  // Hit test inside an svg root whose box contains (x, y): the topmost shape
93
110
  // under the point, or -1 when no shape paints there.
94
111
  int hitTest(int rootId, int x, int y);
@@ -185,6 +185,7 @@ struct Item {
185
185
  bool evenOdd = false;
186
186
  bool hittable = true;
187
187
  float halfStroke = 0.0f;
188
+ StrokeStyle strokeStyle;
188
189
  Bounds fillBounds, strokeBounds;
189
190
  // Everything the geometry and edges depend on (attributes, transform,
190
191
  // stroke style): an unchanged item keeps them across scene rebuilds, so a
@@ -783,6 +784,7 @@ void walk(int id, const Context &parent, std::vector<Item> &items, ItemCache &ca
783
784
  // distance to the path), so it goes straight to edges.
784
785
  strokePathEdges(item.geometry, style, item.strokeEdges, item.strokeBounds);
785
786
  item.halfStroke = width / 2;
787
+ item.strokeStyle = style;
786
788
  }
787
789
  if (item.fill.on) {
788
790
  item.fillBounds.add(item.geometry.contours);
@@ -1568,6 +1570,18 @@ void paint(const DisplayCommand &c)
1568
1570
  paintRows(&band, cy0, cy1);
1569
1571
  }
1570
1572
 
1573
+ void visitScene(int root, void (*visit)(void *user, const ScenePaint &paint), void *user)
1574
+ {
1575
+ auto it = g_scenes.find(root);
1576
+ if (it == g_scenes.end()) return;
1577
+ for (const Item &item : it->second.items) {
1578
+ if (item.fill.on)
1579
+ visit(user, {&item.geometry, nullptr, item.evenOdd, item.fill.gradient, item.fill.color, std::min(1.0f, item.fill.alpha)});
1580
+ if (!item.strokeEdges.empty())
1581
+ visit(user, {&item.geometry, &item.strokeStyle, false, item.stroke.gradient, item.stroke.color, std::min(1.0f, item.stroke.alpha)});
1582
+ }
1583
+ }
1584
+
1571
1585
  int hitTest(int rootId, int x, int y)
1572
1586
  {
1573
1587
  auto it = g_scenes.find(rootId);
package/ui/text.cpp CHANGED
@@ -267,6 +267,8 @@ void appendAlphaCommand(uint8_t alpha, int bx, int by, int bw, int bh)
267
267
  cmd->type = DisplayCommandType::SetAlpha;
268
268
  cmd->bx = bx; cmd->by = by; cmd->bw = bw; cmd->bh = bh;
269
269
  cmd->alpha.alpha = alpha;
270
+ cmd->alpha.recordParentAlpha = alpha;
271
+ cmd->alpha.nodeId = -1;
270
272
  }
271
273
 
272
274
  void boundsFromCorners(const int16_t *xs, const int16_t *ys, int *x0, int *y0, int *x1, int *y1)
@@ -103,6 +103,9 @@ public:
103
103
  void displayBackedFailDebug(int *reason, int *extra, int *count) const;
104
104
  void canvasSlotDebug(int id, int *mode, int *rebinds) const;
105
105
  void markCanvasDirty(int node);
106
+ // The <canvas> node whose owned bitmap is `pixels` (-1: none), and how many
107
+ // times it has been redrawn (markCanvasDirty) since its surface was created.
108
+ int canvasForPixels(const gea::framework::graphics::pixel::native_t *pixels, std::uint32_t *redraws) const;
106
109
 
107
110
  // A <camera> leaf reuses the same per-node RGB565 surface machinery as a
108
111
  // canvas, but the pixels are produced by the platform camera backend each
package/ui/tree_nodes.cpp CHANGED
@@ -523,6 +523,20 @@ void Tree::canvasSlotDebug(int id, int *mode, int *rebinds) const
523
523
  *rebinds = gCanvasRebindCount;
524
524
  }
525
525
 
526
+ int Tree::canvasForPixels(const gea::framework::graphics::pixel::native_t *pixels, std::uint32_t *redraws) const
527
+ {
528
+ const auto &state = treeState();
529
+ int found = -1;
530
+ state.canvases.forEach([&](const CanvasSurfaceState &surface) {
531
+ if (found >= 0 || !surface.ownsPixels() || surface.pixels != pixels || surface.nodeId < 0 || surface.nodeId >= state.nodeCount ||
532
+ state.nodes[surface.nodeId].type != NodeType::Canvas)
533
+ return;
534
+ found = surface.nodeId;
535
+ *redraws = surface.redraws;
536
+ });
537
+ return found;
538
+ }
539
+
526
540
  bool Tree::isDisplayBackedCanvas(int id) const
527
541
  {
528
542
  const auto &state = treeState();
@@ -535,8 +549,9 @@ void Tree::markCanvasDirty(int id)
535
549
  {
536
550
  auto &state = treeState();
537
551
  if (id < 0 || id >= state.nodeCount) return;
538
- const auto *surface = state.canvases.find(id);
552
+ auto *surface = state.canvases.find(id);
539
553
  if (surface && surface->displayBacked()) return;
554
+ if (surface) ++surface->redraws;
540
555
  // Pixels only: drawing never changes the element's box, so no layout_dirty.
541
556
  // It used to be set here, and a canvas redrawn every frame (Ember's heat
542
557
  // arc) then cost a full relayout of the whole screen per frame (~5.5 ms of
package/ui/view.cpp CHANGED
@@ -75,6 +75,8 @@ void appendAlphaCommand(uint8_t alpha, int bx, int by, int bw, int bh)
75
75
  cmd->type = DisplayCommandType::SetAlpha;
76
76
  cmd->bx = bx; cmd->by = by; cmd->bw = bw; cmd->bh = bh;
77
77
  cmd->alpha.alpha = alpha;
78
+ cmd->alpha.recordParentAlpha = alpha;
79
+ cmd->alpha.nodeId = -1;
78
80
  }
79
81
 
80
82
  uint8_t interpolateAlpha(uint8_t from, uint8_t to, int permille)