@geastack/engine 0.1.32 → 0.1.34
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 +197 -18
- package/image_store.cpp +75 -2
- package/package.json +2 -2
- package/rasterized_font.cpp +45 -6
- package/ui/canvas_element.cpp +5 -0
- package/ui/canvas_element.h +1 -0
- package/ui/document.cpp +114 -0
- package/ui/document.h +23 -0
- package/ui/internal.h +1 -1
- package/ui/node.cpp +239 -0
- package/ui/node.h +49 -0
- package/ui/render.cpp +32 -2
- package/ui/style.cpp +87 -2
- package/ui/style.h +11 -0
- package/ui/tree_internal.h +3 -0
- package/ui/tree_nodes.cpp +3 -0
- package/ui/tree_render.cpp +41 -0
- package/ui/tree_state.h +6 -0
- package/ui/view.cpp +36 -2
- package/vendor/nanosvg/LICENSE.txt +18 -0
- package/vendor/nanosvg/nanosvg.h +3278 -0
- package/vendor/nanosvg/nanosvgrast.h +1472 -0
package/canvas.cpp
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
#include <cstdlib>
|
|
15
15
|
#include <cstring>
|
|
16
16
|
#include <limits>
|
|
17
|
+
#include <new>
|
|
17
18
|
#include <type_traits>
|
|
18
19
|
#include <vector>
|
|
19
20
|
|
|
@@ -208,7 +209,27 @@ struct TextSprite {
|
|
|
208
209
|
bool rleValid = false; // true only when the current sprite has a valid RLE
|
|
209
210
|
};
|
|
210
211
|
|
|
211
|
-
|
|
212
|
+
// One bank per render core. The display's 2-core band split replays a label
|
|
213
|
+
// that straddles the split line on BOTH cores at once; with one shared bank
|
|
214
|
+
// both built, reallocated and freed the same slot concurrently. On the mosaico
|
|
215
|
+
// that corrupted the heap: a later launch faulted in blitGlyphSprite on a
|
|
216
|
+
// garbage pointer, and GEADEV MEM's heap walk faulted. The projected-text
|
|
217
|
+
// cache in render.cpp is split the same way for the same race.
|
|
218
|
+
// Heap-allocated on a core's first label (PSRAM where malloc prefers it):
|
|
219
|
+
// static, the two banks were 8 KB of internal RAM. A bank is created only by
|
|
220
|
+
// its own core, so creation needs no lock.
|
|
221
|
+
TextSprite *gTextSprites[2] = {nullptr, nullptr};
|
|
222
|
+
|
|
223
|
+
inline int textSpriteBank()
|
|
224
|
+
{
|
|
225
|
+
#if GEA_CANVAS_HAS_FREERTOS
|
|
226
|
+
return xPortGetCoreID() & 1;
|
|
227
|
+
#elif GEA_CANVAS_HAS_PICO
|
|
228
|
+
return get_core_num() & 1;
|
|
229
|
+
#else
|
|
230
|
+
return 0;
|
|
231
|
+
#endif
|
|
232
|
+
}
|
|
212
233
|
|
|
213
234
|
// Internal-SRAM staging for the sprite currently being blitted. The RLE blit walks
|
|
214
235
|
// rowX0/rowX1/rleRowStart/rleVal/rleLen several times per frame (once per DMA chunk
|
|
@@ -252,7 +273,7 @@ void adoptHotTextSprite(const TextSprite &s)
|
|
|
252
273
|
// cached sprite blitted through the hot stage). The S3, where this staging
|
|
253
274
|
// was built, has byte-accessible IRAM and never showed it.
|
|
254
275
|
hot.buf = static_cast<std::uint8_t *>(
|
|
255
|
-
heap_caps_malloc(GEA_EMBEDDED_TEXT_SPRITE_HOT_STAGE_BYTES, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
|
|
276
|
+
heap_caps_malloc(GEA_EMBEDDED_TEXT_SPRITE_HOT_STAGE_BYTES, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA | MALLOC_CAP_8BIT));
|
|
256
277
|
#else
|
|
257
278
|
hot.buf = static_cast<std::uint8_t *>(std::malloc(GEA_EMBEDDED_TEXT_SPRITE_HOT_STAGE_BYTES));
|
|
258
279
|
#endif
|
|
@@ -282,18 +303,25 @@ void adoptHotTextSprite(const TextSprite &s)
|
|
|
282
303
|
}
|
|
283
304
|
#endif // GEA_EMBEDDED_TEXT_SPRITE_HOT_STAGE_BYTES > 0
|
|
284
305
|
|
|
285
|
-
TextSprite
|
|
306
|
+
TextSprite *textSpriteBankSlots(int bank)
|
|
307
|
+
{
|
|
308
|
+
if (!gTextSprites[bank]) gTextSprites[bank] = new (std::nothrow) TextSprite[kTextSpriteSlots];
|
|
309
|
+
return gTextSprites[bank];
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
TextSprite &textSpriteFor(TextSprite *slots, const char *text, const RasterizedFont &font, int bank)
|
|
286
313
|
{
|
|
287
314
|
// Search every slot: a direct-mapped hash makes alternating labels
|
|
288
315
|
// such as "FPS: 59" and "FPS: 60" evict each other. Hits stay read-only;
|
|
289
316
|
// a four-byte replacement cursor advances only for a new cache key.
|
|
290
|
-
for (
|
|
317
|
+
for (int i = 0; i < kTextSpriteSlots; i++) {
|
|
318
|
+
TextSprite &candidate = slots[i];
|
|
291
319
|
if (candidate.fontData == font.data() && candidate.fontGeneration==font.data()->generation && candidate.sizePx == font.sizePx() &&
|
|
292
320
|
std::strcmp(candidate.text, text) == 0)
|
|
293
321
|
return candidate;
|
|
294
322
|
}
|
|
295
|
-
static std::atomic<unsigned> nextSlot{
|
|
296
|
-
return
|
|
323
|
+
static std::atomic<unsigned> nextSlot[2]{};
|
|
324
|
+
return slots[nextSlot[bank].fetch_add(1, std::memory_order_relaxed) % kTextSpriteSlots];
|
|
297
325
|
}
|
|
298
326
|
|
|
299
327
|
// Rasterize `text` into `sprite`'s coverage buffer in text-relative coordinates
|
|
@@ -530,6 +558,66 @@ bool Canvas::textRasterCacheEnabled()
|
|
|
530
558
|
return gTextRasterCacheEnabled;
|
|
531
559
|
}
|
|
532
560
|
|
|
561
|
+
namespace {
|
|
562
|
+
void freeRoundedCoverageTables();
|
|
563
|
+
} // namespace
|
|
564
|
+
|
|
565
|
+
void Canvas::releaseShapeCaches()
|
|
566
|
+
{
|
|
567
|
+
freeRoundedCoverageTables();
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
#ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
|
|
571
|
+
namespace {
|
|
572
|
+
void releaseSolidGlyphCache();
|
|
573
|
+
} // namespace
|
|
574
|
+
#endif
|
|
575
|
+
|
|
576
|
+
void Canvas::invalidateTextRasterCache()
|
|
577
|
+
{
|
|
578
|
+
#ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
|
|
579
|
+
releaseSolidGlyphCache();
|
|
580
|
+
#endif
|
|
581
|
+
// Runs when an app unmounts: its sprites are keyed by its fonts, and a
|
|
582
|
+
// program's fonts leave with it, so the buffers go too rather than holding
|
|
583
|
+
// up to every slot's largest label for the next app.
|
|
584
|
+
#if GEA_EMBEDDED_TEXT_SPRITE_CACHE
|
|
585
|
+
for (TextSprite *bank : gTextSprites)
|
|
586
|
+
for (int i = 0; bank && i < kTextSpriteSlots; i++) {
|
|
587
|
+
TextSprite &sprite = bank[i];
|
|
588
|
+
sprite.text[0] = '\0';
|
|
589
|
+
sprite.fontData = nullptr;
|
|
590
|
+
sprite.sizePx = -1;
|
|
591
|
+
sprite.state = TextSprite::Empty;
|
|
592
|
+
sprite.rleValid = false;
|
|
593
|
+
std::free(sprite.cov);
|
|
594
|
+
std::free(sprite.rowX0);
|
|
595
|
+
std::free(sprite.rowX1);
|
|
596
|
+
std::free(sprite.rleVal);
|
|
597
|
+
std::free(sprite.rleLen);
|
|
598
|
+
std::free(sprite.rleRowStart);
|
|
599
|
+
sprite.cov = nullptr;
|
|
600
|
+
sprite.rowX0 = nullptr;
|
|
601
|
+
sprite.rowX1 = nullptr;
|
|
602
|
+
sprite.rleVal = nullptr;
|
|
603
|
+
sprite.rleLen = nullptr;
|
|
604
|
+
sprite.rleRowStart = nullptr;
|
|
605
|
+
sprite.covCap = 0;
|
|
606
|
+
sprite.extCap = 0;
|
|
607
|
+
sprite.rleCap = 0;
|
|
608
|
+
sprite.rleRowCap = 0;
|
|
609
|
+
}
|
|
610
|
+
#if GEA_EMBEDDED_TEXT_SPRITE_HOT_STAGE_BYTES > 0
|
|
611
|
+
// The hot stage is 12 KB of internal RAM, allocated the first time a large
|
|
612
|
+
// label blits; the launcher an unloaded app returns to has none to spare.
|
|
613
|
+
gHotTextSprite.owner = nullptr;
|
|
614
|
+
std::free(gHotTextSprite.buf);
|
|
615
|
+
gHotTextSprite.buf = nullptr;
|
|
616
|
+
gHotTextSprite.allocFailed = false;
|
|
617
|
+
#endif
|
|
618
|
+
#endif // GEA_EMBEDDED_TEXT_SPRITE_CACHE
|
|
619
|
+
}
|
|
620
|
+
|
|
533
621
|
void Canvas::setTextSolidBackdrop(int rrggbb)
|
|
534
622
|
{
|
|
535
623
|
#if GEA_PIXEL_STORAGE_PACKED_BITS == 4
|
|
@@ -841,6 +929,18 @@ RoundedCoverageTable gRoundedCoverage[kRoundedCoverageTables];
|
|
|
841
929
|
std::atomic_flag gRoundedCoverageLock = ATOMIC_FLAG_INIT;
|
|
842
930
|
std::uint32_t gRoundedCoverageClock = 0;
|
|
843
931
|
|
|
932
|
+
void freeRoundedCoverageTables()
|
|
933
|
+
{
|
|
934
|
+
while (gRoundedCoverageLock.test_and_set(std::memory_order_acquire)) {}
|
|
935
|
+
for (auto &table : gRoundedCoverage) {
|
|
936
|
+
if (table.refs.load(std::memory_order_relaxed) != 0) continue;
|
|
937
|
+
std::free(table.cells);
|
|
938
|
+
table.cells = nullptr;
|
|
939
|
+
table.capacity = 0;
|
|
940
|
+
}
|
|
941
|
+
gRoundedCoverageLock.clear(std::memory_order_release);
|
|
942
|
+
}
|
|
943
|
+
|
|
844
944
|
void *allocRoundedCoverageCells(int bytes)
|
|
845
945
|
{
|
|
846
946
|
#ifdef ESP_PLATFORM
|
|
@@ -1040,8 +1140,25 @@ struct SolidGlyphCacheEntry {
|
|
|
1040
1140
|
};
|
|
1041
1141
|
|
|
1042
1142
|
constexpr int kSolidGlyphCacheSize = 48;
|
|
1043
|
-
|
|
1044
|
-
|
|
1143
|
+
// One bank per render core, heap-allocated by its own core on first use: the
|
|
1144
|
+
// 2-core band split draws the same label on both cores at once, and one shared
|
|
1145
|
+
// bank rebuilt an entry's vectors under the other core's reads.
|
|
1146
|
+
struct SolidGlyphBank {
|
|
1147
|
+
SolidGlyphCacheEntry entries[kSolidGlyphCacheSize];
|
|
1148
|
+
std::uint32_t clock = 1;
|
|
1149
|
+
};
|
|
1150
|
+
SolidGlyphBank *gSolidGlyphBanks[2] = {nullptr, nullptr};
|
|
1151
|
+
|
|
1152
|
+
inline int solidGlyphBank()
|
|
1153
|
+
{
|
|
1154
|
+
#if GEA_CANVAS_HAS_FREERTOS
|
|
1155
|
+
return xPortGetCoreID() & 1;
|
|
1156
|
+
#elif GEA_CANVAS_HAS_PICO
|
|
1157
|
+
return get_core_num() & 1;
|
|
1158
|
+
#else
|
|
1159
|
+
return 0;
|
|
1160
|
+
#endif
|
|
1161
|
+
}
|
|
1045
1162
|
|
|
1046
1163
|
SolidGlyphCacheEntry *solidGlyphCacheEntry(const RasterizedFont &font,
|
|
1047
1164
|
pixel::native_t color,
|
|
@@ -1050,11 +1167,15 @@ SolidGlyphCacheEntry *solidGlyphCacheEntry(const RasterizedFont &font,
|
|
|
1050
1167
|
{
|
|
1051
1168
|
const RasterizedFontData *data = font.data();
|
|
1052
1169
|
if (!data) return nullptr;
|
|
1170
|
+
SolidGlyphBank *&bankSlot = gSolidGlyphBanks[solidGlyphBank()];
|
|
1171
|
+
if (!bankSlot) bankSlot = new (std::nothrow) SolidGlyphBank();
|
|
1172
|
+
if (!bankSlot) return nullptr;
|
|
1173
|
+
SolidGlyphBank &bank = *bankSlot;
|
|
1053
1174
|
|
|
1054
|
-
for (auto &entry :
|
|
1175
|
+
for (auto &entry : bank.entries) {
|
|
1055
1176
|
if (entry.font == data && entry.color == color && entry.background == background &&
|
|
1056
1177
|
entry.codepoint == codepoint) {
|
|
1057
|
-
entry.lastUsed = ++
|
|
1178
|
+
entry.lastUsed = ++bank.clock;
|
|
1058
1179
|
return &entry;
|
|
1059
1180
|
}
|
|
1060
1181
|
}
|
|
@@ -1062,8 +1183,8 @@ SolidGlyphCacheEntry *solidGlyphCacheEntry(const RasterizedFont &font,
|
|
|
1062
1183
|
Glyph glyph{};
|
|
1063
1184
|
if (!font.glyph(codepoint, &glyph)) return nullptr;
|
|
1064
1185
|
|
|
1065
|
-
SolidGlyphCacheEntry *victim = &
|
|
1066
|
-
for (auto &entry :
|
|
1186
|
+
SolidGlyphCacheEntry *victim = &bank.entries[0];
|
|
1187
|
+
for (auto &entry : bank.entries) {
|
|
1067
1188
|
if (!entry.font) {
|
|
1068
1189
|
victim = &entry;
|
|
1069
1190
|
break;
|
|
@@ -1109,10 +1230,20 @@ SolidGlyphCacheEntry *solidGlyphCacheEntry(const RasterizedFont &font,
|
|
|
1109
1230
|
}
|
|
1110
1231
|
}
|
|
1111
1232
|
|
|
1112
|
-
victim->lastUsed = ++
|
|
1233
|
+
victim->lastUsed = ++bank.clock;
|
|
1113
1234
|
return victim;
|
|
1114
1235
|
}
|
|
1115
1236
|
|
|
1237
|
+
// An unloaded program's fonts go with it, and the next program's can land at
|
|
1238
|
+
// the same address: its glyphs must not come back from this cache.
|
|
1239
|
+
void releaseSolidGlyphCache()
|
|
1240
|
+
{
|
|
1241
|
+
for (SolidGlyphBank *&bank : gSolidGlyphBanks) {
|
|
1242
|
+
delete bank;
|
|
1243
|
+
bank = nullptr;
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1116
1247
|
} // namespace
|
|
1117
1248
|
#endif
|
|
1118
1249
|
|
|
@@ -3229,8 +3360,12 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
|
|
|
3229
3360
|
#if GEA_EMBEDDED_TEXT_SPRITE_DIAG
|
|
3230
3361
|
if (len == 0 || len > static_cast<std::size_t>(kTextSpriteMaxLen)) gTsDiag.tooLong++;
|
|
3231
3362
|
#endif
|
|
3232
|
-
|
|
3233
|
-
|
|
3363
|
+
const int bank = textSpriteBank();
|
|
3364
|
+
TextSprite *slots = textSpriteBankSlots(bank);
|
|
3365
|
+
if (slots && len > 0 && len <= static_cast<std::size_t>(kTextSpriteMaxLen)) {
|
|
3366
|
+
// The hot stage is one internal-RAM buffer: only the render core's
|
|
3367
|
+
// bank uses it, so the worker core never touches it.
|
|
3368
|
+
TextSprite &slot = textSpriteFor(slots, text, font, bank);
|
|
3234
3369
|
const bool idMatch = slot.fontData == font.data() && slot.fontGeneration==font.data()->generation &&
|
|
3235
3370
|
slot.sizePx == font.sizePx() &&
|
|
3236
3371
|
std::strcmp(slot.text, text) == 0;
|
|
@@ -3252,7 +3387,7 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
|
|
|
3252
3387
|
// Only large text stages: with small labels cached too, several
|
|
3253
3388
|
// sprites blit per frame and would steal the one stage from
|
|
3254
3389
|
// each other, re-copying every frame.
|
|
3255
|
-
if (slot.rleValid && slot.sizePx >= 40) {
|
|
3390
|
+
if (bank == 0 && slot.rleValid && slot.sizePx >= 40) {
|
|
3256
3391
|
if (gHotTextSprite.owner != &slot) adoptHotTextSprite(slot);
|
|
3257
3392
|
if (gHotTextSprite.owner == &slot) {
|
|
3258
3393
|
bRowX0 = gHotTextSprite.rowX0;
|
|
@@ -4188,6 +4323,22 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
|
|
|
4188
4323
|
|
|
4189
4324
|
RoundedCoverageTable *coverageTable = useAntialias ? acquireRoundedCoverage(w, h, tl, tr, br, bl, aaSamples) : nullptr;
|
|
4190
4325
|
struct ReleaseCoverage { RoundedCoverageTable *table; ~ReleaseCoverage() { releaseRoundedCoverage(table); } } releaseCoverage{coverageTable};
|
|
4326
|
+
// Edge coverage on the straight sides, keyed by distance from each side
|
|
4327
|
+
// (-1 = not yet computed). Without it every row of an antialiased box
|
|
4328
|
+
// re-sampled its edge band, through a coverage table that a box whose size
|
|
4329
|
+
// changes every frame (a cover flow) never hits.
|
|
4330
|
+
constexpr int kStraightEdgeSlots = 16;
|
|
4331
|
+
std::int8_t straightLeft[kStraightEdgeSlots];
|
|
4332
|
+
std::int8_t straightRight[kStraightEdgeSlots];
|
|
4333
|
+
std::memset(straightLeft, -1, sizeof(straightLeft));
|
|
4334
|
+
std::memset(straightRight, -1, sizeof(straightRight));
|
|
4335
|
+
const float straightTop = static_cast<float>(y) + static_cast<float>(top_r);
|
|
4336
|
+
const float straightBottom = static_cast<float>(y) + static_cast<float>(h) - static_cast<float>(bot_r);
|
|
4337
|
+
// The top and bottom edges are the transpose: a pixel whose samples all lie
|
|
4338
|
+
// between the left and right corner bands has a coverage that depends on
|
|
4339
|
+
// its row alone, so an edge row computes it once instead of per pixel.
|
|
4340
|
+
const float straightLeftX = static_cast<float>(x) + static_cast<float>(tl > bl ? tl : bl);
|
|
4341
|
+
const float straightRightX = static_cast<float>(x) + static_cast<float>(w) - static_cast<float>(tr > br ? tr : br);
|
|
4191
4342
|
for (int sy = row0; sy <= row1; sy++) {
|
|
4192
4343
|
int sx0 = width_;
|
|
4193
4344
|
int sx1 = -1;
|
|
@@ -4263,10 +4414,38 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
|
|
|
4263
4414
|
}
|
|
4264
4415
|
if (anyX0 > anyX1) continue;
|
|
4265
4416
|
|
|
4417
|
+
// Every sample of this row lies between the corner bands, so no corner
|
|
4418
|
+
// arc reaches it: edge coverage here is a function of px only.
|
|
4419
|
+
const bool straightRow = useAntialias &&
|
|
4420
|
+
static_cast<float>(sy) + firstOffset >= straightTop &&
|
|
4421
|
+
static_cast<float>(sy) + lastOffset < straightBottom;
|
|
4422
|
+
std::int8_t straightRowCoverage = -1;
|
|
4423
|
+
|
|
4266
4424
|
auto paintCoverageEdge = [&](int px) {
|
|
4267
4425
|
if (px < clip->x0 || px > clip->x1 || px < 0 || px >= width_) return;
|
|
4268
|
-
|
|
4269
|
-
|
|
4426
|
+
// Straight-side row: the answer depends on px alone, so it is
|
|
4427
|
+
// computed once per shape and reused down the whole side.
|
|
4428
|
+
std::int8_t *straightSlot = nullptr;
|
|
4429
|
+
if (straightRow) {
|
|
4430
|
+
const int fromLeft = px - x;
|
|
4431
|
+
const int fromRight = x + w - 1 - px;
|
|
4432
|
+
straightSlot = fromLeft >= 0 && fromLeft < kStraightEdgeSlots ? &straightLeft[fromLeft]
|
|
4433
|
+
: fromRight >= 0 && fromRight < kStraightEdgeSlots ? &straightRight[fromRight]
|
|
4434
|
+
: nullptr;
|
|
4435
|
+
} else if (useAntialias &&
|
|
4436
|
+
static_cast<float>(px) + firstOffset >= straightLeftX &&
|
|
4437
|
+
static_cast<float>(px) + lastOffset < straightRightX) {
|
|
4438
|
+
straightSlot = &straightRowCoverage;
|
|
4439
|
+
}
|
|
4440
|
+
if (straightSlot && *straightSlot < 0) {
|
|
4441
|
+
const int known = roundedRectCoverageDecided(x, y, w, h, tl, tr, br, bl, px, sy,
|
|
4442
|
+
firstOffset, lastOffset, aaSampleCount);
|
|
4443
|
+
*straightSlot = static_cast<std::int8_t>(
|
|
4444
|
+
known >= 0 ? known : roundedRectCoverage(x, y, w, h, tl, tr, br, bl, px, sy, aaSamples, kernelWidth));
|
|
4445
|
+
}
|
|
4446
|
+
std::uint8_t *cell = !straightSlot && coverageTable ? &coverageTable->cells[(sy - y) * w + (px - x)] : nullptr;
|
|
4447
|
+
int decided = straightSlot ? *straightSlot
|
|
4448
|
+
: cell && *cell != 0xff ? *cell
|
|
4270
4449
|
: useAntialias ? roundedRectCoverageDecided(x, y, w, h, tl, tr, br, bl, px, sy,
|
|
4271
4450
|
firstOffset, lastOffset, aaSampleCount)
|
|
4272
4451
|
: -1;
|
package/image_store.cpp
CHANGED
|
@@ -4,7 +4,11 @@
|
|
|
4
4
|
#include "pixel.h"
|
|
5
5
|
#include "gea_perf_config.h"
|
|
6
6
|
|
|
7
|
+
#include <cmath>
|
|
7
8
|
#include <cstring>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <string_view>
|
|
8
12
|
#include <limits>
|
|
9
13
|
|
|
10
14
|
#if GEA_EMBEDDED_PERF && defined(ESP_PLATFORM)
|
|
@@ -87,6 +91,60 @@ void *gea_stbi_realloc_sized(void *ptr, std::size_t oldSize, std::size_t newSize
|
|
|
87
91
|
#define STB_IMAGE_IMPLEMENTATION
|
|
88
92
|
#include "stb_image.h"
|
|
89
93
|
|
|
94
|
+
#if defined(__APPLE__) || defined(_WIN32) || (defined(__linux__) && !defined(ESP_PLATFORM))
|
|
95
|
+
#define GEA_HOST_SVG_IMAGES 1
|
|
96
|
+
#define NANOSVG_IMPLEMENTATION
|
|
97
|
+
#include "vendor/nanosvg/nanosvg.h"
|
|
98
|
+
#define NANOSVGRAST_IMPLEMENTATION
|
|
99
|
+
#include "vendor/nanosvg/nanosvgrast.h"
|
|
100
|
+
|
|
101
|
+
namespace {
|
|
102
|
+
bool isSvgImage(const std::uint8_t *data, int length)
|
|
103
|
+
{
|
|
104
|
+
std::string_view xml(reinterpret_cast<const char *>(data), length);
|
|
105
|
+
if (xml.starts_with("\xef\xbb\xbf")) xml.remove_prefix(3);
|
|
106
|
+
while (true) {
|
|
107
|
+
const auto start = xml.find_first_not_of(" \t\n\r");
|
|
108
|
+
if (start == xml.npos) return false;
|
|
109
|
+
xml.remove_prefix(start);
|
|
110
|
+
const bool declaration = xml.starts_with("<?");
|
|
111
|
+
if (!declaration && !xml.starts_with("<!--")) break;
|
|
112
|
+
const auto close = declaration ? std::string_view("?>") : std::string_view("-->");
|
|
113
|
+
const auto end = xml.find(close);
|
|
114
|
+
if (end == xml.npos) return false;
|
|
115
|
+
xml.remove_prefix(end + close.size());
|
|
116
|
+
}
|
|
117
|
+
return xml.starts_with("<svg") && xml.size() > 4 &&
|
|
118
|
+
std::string_view(" \t\n\r>/").find(xml[4]) != xml.npos;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
stbi_uc *rasterizeSvgImage(const std::uint8_t *data, int length, int &width, int &height)
|
|
122
|
+
{
|
|
123
|
+
constexpr int maxPixels = 4 * 1024 * 1024;
|
|
124
|
+
if (length > 1024 * 1024) return nullptr;
|
|
125
|
+
std::string markup(reinterpret_cast<const char *>(data), length);
|
|
126
|
+
std::unique_ptr<NSVGimage, decltype(&nsvgDelete)> image(nsvgParse(markup.data(), "px", 96), nsvgDelete);
|
|
127
|
+
if (!image || !(image->width > 0 && image->width <= 8192 &&
|
|
128
|
+
image->height > 0 && image->height <= 8192)) return nullptr;
|
|
129
|
+
|
|
130
|
+
width = static_cast<int>(std::ceil(image->width));
|
|
131
|
+
height = static_cast<int>(std::ceil(image->height));
|
|
132
|
+
const int pixels = width * height;
|
|
133
|
+
if (pixels > maxPixels) return nullptr;
|
|
134
|
+
|
|
135
|
+
std::unique_ptr<NSVGrasterizer, decltype(&nsvgDeleteRasterizer)> rasterizer(
|
|
136
|
+
nsvgCreateRasterizer(), nsvgDeleteRasterizer);
|
|
137
|
+
if (!rasterizer) return nullptr;
|
|
138
|
+
|
|
139
|
+
auto *rgba = static_cast<stbi_uc *>(gea_stbi_malloc(static_cast<std::size_t>(pixels) * 4));
|
|
140
|
+
if (!rgba) return nullptr;
|
|
141
|
+
nsvgRasterize(rasterizer.get(), image.get(), 0, 0, 1, rgba, width, height, width * 4);
|
|
142
|
+
return rgba;
|
|
143
|
+
}
|
|
144
|
+
} // namespace
|
|
145
|
+
#endif
|
|
146
|
+
|
|
147
|
+
|
|
90
148
|
#if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
|
|
91
149
|
// The ROM emits one RGB888 MCU at a time, not a full-frame RGB(A) buffer.
|
|
92
150
|
#if defined(CONFIG_IDF_TARGET_ESP32S3)
|
|
@@ -293,6 +351,9 @@ ImageFormat ImageStore::detectFormat(const std::uint8_t *data, int length) const
|
|
|
293
351
|
if (data[0] == 0xFF && data[1] == 0xD8) return ImageFormat::Jpeg;
|
|
294
352
|
if (data[0] == 0x89 && data[1] == 'P' && data[2] == 'N' && data[3] == 'G') return ImageFormat::Png;
|
|
295
353
|
if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') return ImageFormat::Gif;
|
|
354
|
+
#if GEA_HOST_SVG_IMAGES
|
|
355
|
+
if (isSvgImage(data, length)) return ImageFormat::Svg;
|
|
356
|
+
#endif
|
|
296
357
|
return ImageFormat::Unknown;
|
|
297
358
|
}
|
|
298
359
|
|
|
@@ -503,7 +564,8 @@ int ImageStore::decodeJpegTjpgd(ImageSlot &image, const std::uint8_t *data, int
|
|
|
503
564
|
constexpr std::size_t poolSize = 8 * 1024;
|
|
504
565
|
void *pool = nullptr;
|
|
505
566
|
#ifdef ESP_PLATFORM
|
|
506
|
-
|
|
567
|
+
// MALLOC_CAP_DMA keeps it out of the S31/P4 LP RAM, slower than PSRAM.
|
|
568
|
+
pool = heap_caps_malloc(poolSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA | MALLOC_CAP_8BIT);
|
|
507
569
|
#endif
|
|
508
570
|
if (!pool) pool = memory::Allocator::allocatePreferSpiram(poolSize, alignof(std::max_align_t));
|
|
509
571
|
if (!pool) return -1;
|
|
@@ -915,7 +977,11 @@ int ImageStore::decodeStatic(ImageSlot &image, const std::uint8_t *data, int len
|
|
|
915
977
|
}
|
|
916
978
|
#endif
|
|
917
979
|
|
|
918
|
-
stbi_uc *rgba =
|
|
980
|
+
stbi_uc *rgba =
|
|
981
|
+
#if GEA_HOST_SVG_IMAGES
|
|
982
|
+
image.format == ImageFormat::Svg ? rasterizeSvgImage(data, length, width, height) :
|
|
983
|
+
#endif
|
|
984
|
+
stbi_load_from_memory(data, length, &width, &height, &components, 4);
|
|
919
985
|
if (!rgba) return -1;
|
|
920
986
|
|
|
921
987
|
const int pixelCount = width * height;
|
|
@@ -1255,6 +1321,13 @@ void ImageStore::disposeAll()
|
|
|
1255
1321
|
}
|
|
1256
1322
|
}
|
|
1257
1323
|
|
|
1324
|
+
void ImageStore::disposeEverything()
|
|
1325
|
+
{
|
|
1326
|
+
ImageStoreLock lock(slotMutex_);
|
|
1327
|
+
for (int i = 0; i < kImageMax; i++)
|
|
1328
|
+
if (used_[i]) disposeLocked(i);
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1258
1331
|
int ImageStore::registerBuffer(pixel::native_t *pixels, int width, int height, int preferredSlot, bool takeOwnership)
|
|
1259
1332
|
{
|
|
1260
1333
|
if (!pixels || width <= 0 || height <= 0) return -1;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geastack/engine",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.34",
|
|
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.
|
|
60
|
+
"@geastack/core": "^0.1.67"
|
|
61
61
|
}
|
|
62
62
|
}
|
package/rasterized_font.cpp
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
#include "graphics/font.h"
|
|
3
|
+
#include "program.h"
|
|
3
4
|
|
|
4
5
|
namespace {
|
|
5
6
|
const gea::framework::graphics::RuntimeFontProvider *runtimeFontProvider = nullptr;
|
|
@@ -103,11 +104,43 @@ namespace {
|
|
|
103
104
|
|
|
104
105
|
void ensureGeneratedFontsLinked()
|
|
105
106
|
{
|
|
107
|
+
if (const auto *loaded = program::loaded()) {
|
|
108
|
+
if (loaded->fontsEnsureLinked) loaded->fontsEnsureLinked();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
106
111
|
#ifdef GEA_EMBEDDED_HAS_GENERATED_FONTS
|
|
107
112
|
generated::ensureLinked();
|
|
108
113
|
#endif
|
|
109
114
|
}
|
|
110
115
|
|
|
116
|
+
// A loaded program (program.h) brings its own font registry; otherwise the
|
|
117
|
+
// firmware's generated one answers.
|
|
118
|
+
const RasterizedFontData *programLookupFont(int fontId)
|
|
119
|
+
{
|
|
120
|
+
if (const auto *loaded = program::loaded()) return loaded->lookupFont ? loaded->lookupFont(fontId) : nullptr;
|
|
121
|
+
return generated::lookupFont(fontId);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const RasterizedFontData *programLookupFontForFamily(int familyId, int sizePx)
|
|
125
|
+
{
|
|
126
|
+
if (const auto *loaded = program::loaded())
|
|
127
|
+
return loaded->lookupFontForFamily ? loaded->lookupFontForFamily(familyId, sizePx) : nullptr;
|
|
128
|
+
return generated::lookupFontForFamily(familyId, sizePx);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
int programLookupFontFamily(const char *family)
|
|
132
|
+
{
|
|
133
|
+
if (const auto *loaded = program::loaded()) return loaded->lookupFontFamily ? loaded->lookupFontFamily(family) : -1;
|
|
134
|
+
return generated::lookupFontFamily(family);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const char *programLookupFontFamilyName(int familyId)
|
|
138
|
+
{
|
|
139
|
+
if (const auto *loaded = program::loaded())
|
|
140
|
+
return loaded->lookupFontFamilyName ? loaded->lookupFontFamilyName(familyId) : nullptr;
|
|
141
|
+
return generated::lookupFontFamilyName(familyId);
|
|
142
|
+
}
|
|
143
|
+
|
|
111
144
|
#if GEA_EMBEDDED_TTF_RUNTIME_FONTS
|
|
112
145
|
|
|
113
146
|
#if GEA_EMBEDDED_TTF_RUNTIME_TRACE
|
|
@@ -211,7 +244,9 @@ int runtimeCodepointAt(int index)
|
|
|
211
244
|
// Declared codepoints the fixed set already covers leave a gap (-1) rather
|
|
212
245
|
// than a duplicate entry.
|
|
213
246
|
int count = 0;
|
|
214
|
-
const
|
|
247
|
+
const auto *loaded = program::loaded();
|
|
248
|
+
const int *declared = loaded ? (loaded->runtimeTtfDeclaredCodepoints ? loaded->runtimeTtfDeclaredCodepoints(&count) : nullptr)
|
|
249
|
+
: generated::runtimeTtfDeclaredCodepoints(&count);
|
|
215
250
|
if (!declared || index >= count) return -1;
|
|
216
251
|
const int codepoint = declared[index];
|
|
217
252
|
return codepoint > 0 && !runtimeFixedCodepoint(codepoint) ? codepoint : -1;
|
|
@@ -271,7 +306,11 @@ bool buildRuntimeFontSlot(RuntimeRasterizedFontSlot &slot, int familyId, int siz
|
|
|
271
306
|
|
|
272
307
|
unsigned long fontLength = 0;
|
|
273
308
|
const std::uint8_t *fontBytes = runtimeFontProvider ? runtimeFontProvider->fontBytes(familyId, &fontLength) : nullptr;
|
|
274
|
-
if (!fontBytes)
|
|
309
|
+
if (!fontBytes) {
|
|
310
|
+
const auto *loaded = program::loaded();
|
|
311
|
+
fontBytes = loaded ? (loaded->lookupRuntimeTtfFontForFamily ? loaded->lookupRuntimeTtfFontForFamily(familyId, &fontLength) : nullptr)
|
|
312
|
+
: generated::lookupRuntimeTtfFontForFamily(familyId, &fontLength);
|
|
313
|
+
}
|
|
275
314
|
runtimeTtfTrace("slot.lookup family=%d bytes=%p len=%lu", familyId, static_cast<const void *>(fontBytes), fontLength);
|
|
276
315
|
if (!fontBytes || fontLength == 0) return false;
|
|
277
316
|
|
|
@@ -683,7 +722,7 @@ std::uint8_t RasterizedFont::coverage(const Glyph &glyph, int row, int col) cons
|
|
|
683
722
|
RasterizedFont FontRegistry::rasterized(int fontId)
|
|
684
723
|
{
|
|
685
724
|
ensureGeneratedFontsLinked();
|
|
686
|
-
return RasterizedFont(
|
|
725
|
+
return RasterizedFont(programLookupFont(fontId));
|
|
687
726
|
}
|
|
688
727
|
|
|
689
728
|
RasterizedFont FontRegistry::rasterizedFamily(int familyId, int sizePx)
|
|
@@ -699,7 +738,7 @@ RasterizedFont FontRegistry::rasterizedFamily(int familyId, int sizePx,int weigh
|
|
|
699
738
|
return RasterizedFont(data);
|
|
700
739
|
}
|
|
701
740
|
#endif
|
|
702
|
-
return RasterizedFont(
|
|
741
|
+
return RasterizedFont(programLookupFontForFamily(familyId, sizePx));
|
|
703
742
|
}
|
|
704
743
|
|
|
705
744
|
void FontRegistry::invalidateRuntimeFonts()
|
|
@@ -729,7 +768,7 @@ int FontRegistry::familyId(const char *family)
|
|
|
729
768
|
const int id = runtimeFontProvider->lookupFamily(family);
|
|
730
769
|
if (id >= 0) return id;
|
|
731
770
|
}
|
|
732
|
-
return
|
|
771
|
+
return programLookupFontFamily(family);
|
|
733
772
|
}
|
|
734
773
|
|
|
735
774
|
const char *FontRegistry::familyName(int familyId)
|
|
@@ -738,7 +777,7 @@ const char *FontRegistry::familyName(int familyId)
|
|
|
738
777
|
if (runtimeFontProvider) {
|
|
739
778
|
if (const char *name = runtimeFontProvider->familyName(familyId)) return name;
|
|
740
779
|
}
|
|
741
|
-
return
|
|
780
|
+
return programLookupFontFamilyName(familyId);
|
|
742
781
|
}
|
|
743
782
|
|
|
744
783
|
namespace generated {
|
package/ui/canvas_element.cpp
CHANGED
|
@@ -142,6 +142,11 @@ std::shared_ptr<CanvasContextState> CanvasRenderingContext2D::stateFor(int nodeI
|
|
|
142
142
|
|
|
143
143
|
void CanvasRenderingContext2D::releaseState(int nodeId) { contextStates().erase(nodeId); }
|
|
144
144
|
|
|
145
|
+
// An unmounting app's canvases are gone; their states are freed with the map's
|
|
146
|
+
// buckets, which would otherwise stay in internal RAM, in the middle of the
|
|
147
|
+
// block the next app allocates its large buffers from.
|
|
148
|
+
void CanvasRenderingContext2D::releaseAllStates() { std::unordered_map<int, std::shared_ptr<CanvasContextState>>().swap(contextStates()); }
|
|
149
|
+
|
|
145
150
|
namespace
|
|
146
151
|
{
|
|
147
152
|
|
package/ui/canvas_element.h
CHANGED
|
@@ -202,6 +202,7 @@ public:
|
|
|
202
202
|
static std::shared_ptr<CanvasContextState> stateFor(int nodeId);
|
|
203
203
|
// Drops canvas `nodeId`'s state, so a node id reused later starts clean.
|
|
204
204
|
static void releaseState(int nodeId);
|
|
205
|
+
static void releaseAllStates();
|
|
205
206
|
static void materializeRetainedCanvas(int nodeId);
|
|
206
207
|
static bool replayRetainedCanvas(int nodeId, std::uint32_t identity, int x, int y, int width, int height);
|
|
207
208
|
static bool hasRetainedCanvas(int nodeId);
|