@geastack/engine 0.1.20 → 0.1.22

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
@@ -8,6 +8,7 @@
8
8
 
9
9
  #include <algorithm>
10
10
  #include <atomic>
11
+ #include <climits>
11
12
  #include <cmath>
12
13
  #include <cstdio>
13
14
  #include <cstdlib>
@@ -18,6 +19,9 @@
18
19
 
19
20
  // For the compile-gated GEA_EMBEDDED_TEXT_SPRITE_DIAG counters (see the diag
20
21
  // block above Canvas::drawRasterizedText). Includes must stay at file scope.
22
+ #ifndef GEA_EMBEDDED_TTF_RUNTIME_FONTS
23
+ #define GEA_EMBEDDED_TTF_RUNTIME_FONTS 0
24
+ #endif
21
25
  #ifndef GEA_EMBEDDED_TEXT_SPRITE_DIAG
22
26
  #define GEA_EMBEDDED_TEXT_SPRITE_DIAG 0
23
27
  #endif
@@ -1347,6 +1351,87 @@ void Canvas::writePixelAlpha(int x, int y, pixel::native_t fg, std::uint8_t alph
1347
1351
  writePixelNativeAlpha(x, y, fg, alpha);
1348
1352
  }
1349
1353
 
1354
+ void Canvas::blendCoverageRow(int y, int x0, int count, const std::uint8_t *coverage, pixel::native_t color,
1355
+ bool markRowDirty)
1356
+ {
1357
+ if (!pixels_ || !coverage || count <= 0 || globalAlpha_ == 0) return;
1358
+ const ClipRect &clip = clipStack_[clipDepth_];
1359
+ if (y < 0 || y >= height_ || y < clip.y0 || y > clip.y1) return;
1360
+ int begin = std::max({x0, clip.x0, 0});
1361
+ int end = std::min({x0 + count - 1, clip.x1, width_ - 1});
1362
+ while (begin <= end && coverage[begin - x0] == 0) ++begin;
1363
+ while (end >= begin && coverage[end - x0] == 0) --end;
1364
+ if (begin > end) return;
1365
+ const int phys = rowToPhysical(y);
1366
+ for (int x = begin; x <= end;) {
1367
+ // Runs of equal coverage: a shape's interior is one long run, its edges
1368
+ // short ones. Full runs are span fills; partial runs blend one colour at
1369
+ // one alpha, so the result for a given destination pixel repeats -- and
1370
+ // under a chart the destination is mostly one background colour.
1371
+ const std::uint8_t c = coverage[x - x0];
1372
+ int run = x + 1;
1373
+ while (run <= end && coverage[run - x0] == c) ++run;
1374
+ const int n = run - x;
1375
+ if (c == 255) {
1376
+ fillSpanGlobalAlpha(phys, x, n, color);
1377
+ } else if (c != 0) {
1378
+ const int a = globalAlpha_ == 255 ? c : (c * globalAlpha_) / 255;
1379
+ if (a > 0) blendSpanAlpha(y, phys, x, n, color, a);
1380
+ }
1381
+ x = run;
1382
+ }
1383
+ if (markRowDirty) markDirty(begin, y, end, y);
1384
+ }
1385
+
1386
+ void Canvas::blendCoverageSpan(int y, int x0, int count, std::uint8_t coverage, pixel::native_t color,
1387
+ bool markRowDirty)
1388
+ {
1389
+ if (!pixels_ || count <= 0 || coverage == 0 || globalAlpha_ == 0) return;
1390
+ const ClipRect &clip = clipStack_[clipDepth_];
1391
+ if (y < 0 || y >= height_ || y < clip.y0 || y > clip.y1) return;
1392
+ const int begin = std::max({x0, clip.x0, 0});
1393
+ const int end = std::min({x0 + count - 1, clip.x1, width_ - 1});
1394
+ if (begin > end) return;
1395
+ const int phys = rowToPhysical(y);
1396
+ if (coverage == 255) {
1397
+ fillSpanGlobalAlpha(phys, begin, end - begin + 1, color);
1398
+ } else {
1399
+ const int a = globalAlpha_ == 255 ? coverage : (coverage * globalAlpha_) / 255;
1400
+ if (a <= 0) return;
1401
+ blendSpanAlpha(y, phys, begin, end - begin + 1, color, a);
1402
+ }
1403
+ if (markRowDirty) markDirty(begin, y, end, y);
1404
+ }
1405
+
1406
+ // A run blends one colour at one alpha, so the result for a given destination
1407
+ // pixel repeats -- and under a chart the destination is mostly one colour.
1408
+ void Canvas::blendSpanAlpha(int y, int phys, int x, int n, pixel::native_t color, int a)
1409
+ {
1410
+ #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
1411
+ if (landscapeRot_) {
1412
+ for (int i = 0; i < n; ++i) rotBlend(x + i, y, color, a);
1413
+ return;
1414
+ }
1415
+ #endif
1416
+ (void)y;
1417
+ #if GEA_PIXEL_STORAGE_PACKED
1418
+ for (int i = 0; i < n; ++i) pixel::packed::blendPixel(packedRow(phys), x + i, color, a);
1419
+ #else
1420
+ pixel::native_t *p = &pixels_[phys * stride_ + x];
1421
+ pixel::native_t lastBg = p[0];
1422
+ pixel::native_t lastOut = pixel::blendNative(color, lastBg, a);
1423
+ p[0] = lastOut;
1424
+ for (int i = 1; i < n; ++i) {
1425
+ const pixel::native_t bg = p[i];
1426
+ if (bg != lastBg) {
1427
+ lastBg = bg;
1428
+ lastOut = pixel::blendNative(color, bg, a);
1429
+ }
1430
+ p[i] = lastOut;
1431
+ }
1432
+ #endif
1433
+ }
1434
+
1350
1435
  // Fill a horizontal span at physical row `phys` with a colour (converted once
1351
1436
  // to the native pixel). Native fill — no per-pixel conversion, no branch.
1352
1437
  void Canvas::fillSpanColor(int phys, int x0, int count, pixel::native_t color)
@@ -1860,19 +1945,23 @@ void Canvas::fillRect(int x, int y, int w, int h, pixel::native_t color)
1860
1945
  runStart = -1; // keep the shared tail below a no-op
1861
1946
  }
1862
1947
  #else
1863
- pixel::native_t *dst = &pixels_[rowToPhysical(row) * stride_ + x0];
1864
- int runStart = -1;
1865
- for (int i = 0; i < count; i++) {
1866
- if (dst[i] == c) {
1867
- if (runStart >= 0) {
1868
- markDirty(x0 + runStart, row, x0 + i - 1, row);
1869
- runStart = -1;
1870
- }
1871
- continue;
1872
- }
1873
- if (runStart < 0) runStart = i;
1874
- dst[i] = c;
1875
- }
1948
+ // Trim the already-correct pixels off both ends and fill what is
1949
+ // left as one span. Pixels inside the span that already match are
1950
+ // rewritten with the same value, so the pixels are exactly those of
1951
+ // a per-run fill; the dirty rect is the row's differing extent, a
1952
+ // superset of the per-run rects. Per-run marking cost a dirty-list
1953
+ // walk per run: a fill over freshly reused staging memory (the
1954
+ // fused flush's strip buffer) differs in scattered runs, and it
1955
+ // measured 15.8 cycles/px on an ESP32-S3.
1956
+ const pixel::native_t *dst = &pixels_[rowToPhysical(row) * stride_ + x0];
1957
+ const int runStart = -1;
1958
+ int a = 0;
1959
+ while (a < count && dst[a] == c) ++a;
1960
+ if (a == count) continue;
1961
+ int b = count - 1;
1962
+ while (dst[b] == c) --b;
1963
+ fillSpanColor(rowToPhysical(row), x0 + a, b - a + 1, c);
1964
+ markDirty(x0 + a, row, x0 + b, row);
1876
1965
  #endif
1877
1966
  if (runStart >= 0) markDirty(x0 + runStart, row, x1, row);
1878
1967
  }
@@ -3238,6 +3327,27 @@ void Canvas::drawRasterizedText(const char *text, int x, int y, pixel::native_t
3238
3327
  std::uint8_t *fbNib = packedRow(rowToPhysical(py));
3239
3328
  #else
3240
3329
  pixel::native_t *fb_row = &pixels_[rowToPhysical(py) * stride_ + gx];
3330
+ #if !GEA_EMBEDDED_TTF_RUNTIME_FONTS
3331
+ // Baked 8-bit atlas: read the glyph row directly. RasterizedFont::
3332
+ // coverage lives in another translation unit, so going through it
3333
+ // cost an out-of-line call plus its bounds checks per pixel — most
3334
+ // of a small label's draw time. row/col are already clipped inside
3335
+ // the glyph, which is all coverage() checks for.
3336
+ if (font.data()->atlasBits == 8) {
3337
+ const RasterizedFontData *fd = font.data();
3338
+ const std::uint8_t *covRow = fd->atlas + static_cast<std::size_t>(glyph.sourceY + row) * fd->atlasWidth + glyph.sourceX;
3339
+ const int ga = globalAlpha_;
3340
+ for (int col = col_start; col <= col_end; col++) {
3341
+ const int alpha = covRow[col];
3342
+ if (alpha == 0) continue;
3343
+ const std::uint8_t a = (ga == 255) ? alpha : (alpha * ga) / 255;
3344
+ if (a == 0) continue;
3345
+ pixel::native_t *pixel = &fb_row[col];
3346
+ *pixel = (a == 255) ? c : pixel::blendNative(c, *pixel, a);
3347
+ }
3348
+ continue;
3349
+ }
3350
+ #endif
3241
3351
  #endif
3242
3352
  for (int col = col_start; col <= col_end; col++) {
3243
3353
  int alpha = font.coverage(glyph, row, col);
@@ -3983,8 +4093,14 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
3983
4093
  for (int sy = row0; sy <= row1; sy++) {
3984
4094
  int sx0 = width_;
3985
4095
  int sx1 = -1;
4096
+ // The row's span before clipping: the antialiased edge band is measured
4097
+ // from the shape's own edges, not from where a clip happens to cut it.
4098
+ int ux0 = INT_MAX;
4099
+ int ux1 = INT_MIN;
3986
4100
  auto addSpan = [&](int left, int right) {
3987
4101
  if (left > right) return;
4102
+ if (left < ux0) ux0 = left;
4103
+ if (right > ux1) ux1 = right;
3988
4104
  if (left < clip->x0) left = clip->x0;
3989
4105
  if (right > clip->x1) right = clip->x1;
3990
4106
  if (left < 0) left = 0;
@@ -4036,10 +4152,12 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
4036
4152
  anyX0 = width_;
4037
4153
  anyX1 = -1;
4038
4154
  roundedRectAnyCoverageSpan(x, y, w, h, tl, tr, br, bl, sy, aaSamples, kernelWidth, &anyX0, &anyX1);
4039
- if (anyX0 < clip->x0) anyX0 = clip->x0;
4040
- if (anyX1 > clip->x1) anyX1 = clip->x1;
4041
4155
  if (anyX0 < x) anyX0 = x;
4042
4156
  if (anyX1 > x + w - 1) anyX1 = x + w - 1;
4157
+ if (ux0 < anyX0) ux0 = anyX0;
4158
+ if (ux1 > anyX1) ux1 = anyX1;
4159
+ if (anyX0 < clip->x0) anyX0 = clip->x0;
4160
+ if (anyX1 > clip->x1) anyX1 = clip->x1;
4043
4161
  if (anyX0 < 0) anyX0 = 0;
4044
4162
  if (anyX1 >= width_) anyX1 = width_ - 1;
4045
4163
  if (sx0 < anyX0) sx0 = anyX0;
@@ -4116,18 +4234,23 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
4116
4234
  for (int px = inX1 + 1; px <= anyX1; ++px) paintCoverageEdge(px);
4117
4235
  continue;
4118
4236
  } else if (useAntialias) {
4119
- if (sx0 <= sx1) {
4120
- const int leftEdgeEnd = std::min(anyX1, sx0 + edgePad);
4121
- const int rightEdgeStart = std::max(anyX0, sx1 - edgePad);
4122
- if (leftEdgeEnd >= rightEdgeStart) {
4123
- for (int px = anyX0; px <= anyX1; ++px) paintCoverageEdge(px);
4124
- } else {
4125
- for (int px = anyX0; px <= leftEdgeEnd; ++px) paintCoverageEdge(px);
4126
- for (int px = rightEdgeStart; px <= anyX1; ++px) paintCoverageEdge(px);
4127
- }
4128
- } else {
4237
+ // Coverage-test only the pixels within edgePad of the shape's edges;
4238
+ // the solid interior below takes the rest. Measuring the band from
4239
+ // the clipped span instead sampled coverage at both clip borders of
4240
+ // every row, though the shape covers those pixels fully: a card
4241
+ // background repainted under a dirty region inside it spent most
4242
+ // of its time there.
4243
+ const int solidX0 = ux0 + edgePad + 1;
4244
+ const int solidX1 = ux1 - edgePad - 1;
4245
+ if (sx0 > sx1 || solidX0 > solidX1) {
4129
4246
  for (int px = anyX0; px <= anyX1; ++px) paintCoverageEdge(px);
4247
+ } else {
4248
+ const int leftEdgeEnd = std::min(anyX1, solidX0 - 1);
4249
+ const int rightEdgeStart = std::max(anyX0, solidX1 + 1);
4250
+ for (int px = anyX0; px <= leftEdgeEnd; ++px) paintCoverageEdge(px);
4251
+ for (int px = rightEdgeStart; px <= anyX1; ++px) paintCoverageEdge(px);
4130
4252
  }
4253
+ if (sx0 > sx1) continue; // no solid span (and ux0/ux1 may be unset)
4131
4254
  } else if (sx0 > sx1) {
4132
4255
  continue;
4133
4256
  }
@@ -4136,8 +4259,8 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
4136
4259
  // full opaque shape — fill it directly, with no per-pixel edge coverage
4137
4260
  // (roundedRectSampleContains). AA on carves the antialiased edge band (edgePad)
4138
4261
  // out of the solid fill and leaves those edges to the coverage passes above.
4139
- int fillX0 = useAntialias ? sx0 + edgePad + 1 : sx0;
4140
- int fillX1 = useAntialias ? sx1 - edgePad - 1 : sx1;
4262
+ int fillX0 = useAntialias ? ux0 + edgePad + 1 : sx0;
4263
+ int fillX1 = useAntialias ? ux1 - edgePad - 1 : sx1;
4141
4264
  if (fillX0 < clip->x0) fillX0 = clip->x0;
4142
4265
  if (fillX1 > clip->x1) fillX1 = clip->x1;
4143
4266
  if (fillX0 < 0) fillX0 = 0;
package/image_store.cpp CHANGED
@@ -32,6 +32,17 @@
32
32
  #include "esp_jpeg_dec.h"
33
33
  #endif
34
34
 
35
+ // PNGs can stream through the S3 ROM's inflater straight into native pixels,
36
+ // so a decode needs the result plus ~45 KB rather than stb's whole inflated
37
+ // image and an RGBA copy (3.4 MB for an 800x480 picture).
38
+ #if !defined(GEA_EMBEDDED_ROM_PNG_STREAM) && defined(ESP_PLATFORM) && defined(CONFIG_IDF_TARGET_ESP32S3) && \
39
+ __has_include("miniz.h")
40
+ #define GEA_EMBEDDED_ROM_PNG_STREAM 1
41
+ #endif
42
+ #if defined(GEA_EMBEDDED_ROM_PNG_STREAM) && GEA_EMBEDDED_ROM_PNG_STREAM
43
+ #include "miniz.h"
44
+ #endif
45
+
35
46
  // Route stb_image's allocations through the framework allocator (PSRAM-first
36
47
  // on embedded targets). Plain malloc would land on the tiny SRAM newlib heap
37
48
  // on the rp2350 — where pico_malloc PANICS on failure — so decoding a large
@@ -260,7 +271,7 @@ int convertRgbaToNative(const std::uint8_t *rgba, pixel::native_t *out, std::uin
260
271
 
261
272
  ImageStore &ImageStore::instance()
262
273
  {
263
- static ImageStore store;
274
+ GEA_EXT_BSS static ImageStore store;
264
275
  return store;
265
276
  }
266
277
 
@@ -535,6 +546,265 @@ int ImageStore::decodeJpegTjpgd(ImageSlot &image, const std::uint8_t *data, int
535
546
  }
536
547
  #endif
537
548
 
549
+ #if defined(GEA_EMBEDDED_ROM_PNG_STREAM) && GEA_EMBEDDED_ROM_PNG_STREAM
550
+ namespace {
551
+
552
+ // Streaming PNG decode: the IDAT stream inflates through a 32 KB wrapping
553
+ // window, each scanline is unfiltered against the previous one as it
554
+ // completes and converted straight to native pixels. Peak memory is the
555
+ // result (plus an alpha plane when the image has transparency), two
556
+ // scanlines, the window and the inflater -- not the whole inflated image
557
+ // and an RGBA copy, which is what stb holds. Handles what assets are made
558
+ // of: 8-bit, non-interlaced gray, RGB, palette, gray+alpha and RGBA.
559
+ // Anything else returns kPngUnsupported and stays on stb.
560
+ constexpr int kPngUnsupported = -2;
561
+
562
+ std::uint32_t pngU32(const std::uint8_t *p)
563
+ {
564
+ return (std::uint32_t(p[0]) << 24) | (std::uint32_t(p[1]) << 16) | (std::uint32_t(p[2]) << 8) | p[3];
565
+ }
566
+
567
+ struct PngStream {
568
+ int width = 0;
569
+ int height = 0;
570
+ int colorType = 0;
571
+ int channels = 0;
572
+ std::size_t stride = 0; // bytes per scanline, filter byte excluded
573
+ std::uint8_t palette[256][4] = {};
574
+ bool opaqueHint = false;
575
+ pixel::native_t *pixels = nullptr;
576
+ std::uint8_t *alpha = nullptr;
577
+ bool hasZero = false;
578
+ std::uint8_t *prev = nullptr;
579
+ std::uint8_t *cur = nullptr; // filter byte + stride
580
+ std::size_t filled = 0;
581
+ int row = 0;
582
+ bool failed = false;
583
+
584
+ void emitPixel(int index, std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a)
585
+ {
586
+ pixels[index] = rgbaToNative(r, g, b, a);
587
+ // Same alpha plane convertRgbaToNative builds: opaque-hinted images
588
+ // keep only fully transparent pixels, others every non-255 value.
589
+ const bool keep = opaqueHint ? a == 0 : a != 255;
590
+ if (!keep) {
591
+ if (alpha) alpha[index] = 255;
592
+ return;
593
+ }
594
+ if (a == 0) hasZero = true;
595
+ if (!alpha) {
596
+ const std::size_t count = static_cast<std::size_t>(width) * height;
597
+ alpha = ImageMemory::allocate<std::uint8_t>(count);
598
+ if (!alpha) {
599
+ failed = true;
600
+ return;
601
+ }
602
+ std::memset(alpha, 255, count);
603
+ }
604
+ alpha[index] = opaqueHint ? 0 : a;
605
+ }
606
+
607
+ void finishRow()
608
+ {
609
+ const int bpp = channels;
610
+ const std::uint8_t filter = cur[0];
611
+ std::uint8_t *line = cur + 1;
612
+ const std::uint8_t *up = prev + 1;
613
+ for (std::size_t i = 0; i < stride; i++) {
614
+ const int left = i >= static_cast<std::size_t>(bpp) ? line[i - bpp] : 0;
615
+ const int above = up[i];
616
+ const int corner = i >= static_cast<std::size_t>(bpp) ? up[i - bpp] : 0;
617
+ int predictor = 0;
618
+ switch (filter) {
619
+ case 0: predictor = 0; break;
620
+ case 1: predictor = left; break;
621
+ case 2: predictor = above; break;
622
+ case 3: predictor = (left + above) >> 1; break;
623
+ case 4: {
624
+ const int p = left + above - corner;
625
+ const int pa = p > left ? p - left : left - p;
626
+ const int pb = p > above ? p - above : above - p;
627
+ const int pc = p > corner ? p - corner : corner - p;
628
+ predictor = (pa <= pb && pa <= pc) ? left : (pb <= pc ? above : corner);
629
+ break;
630
+ }
631
+ default: failed = true; return;
632
+ }
633
+ line[i] = static_cast<std::uint8_t>(line[i] + predictor);
634
+ }
635
+ const int base = row * width;
636
+ for (int x = 0; x < width && !failed; x++) {
637
+ const std::uint8_t *px = line + static_cast<std::size_t>(x) * channels;
638
+ switch (colorType) {
639
+ case 0: emitPixel(base + x, px[0], px[0], px[0], 255); break;
640
+ case 2: emitPixel(base + x, px[0], px[1], px[2], 255); break;
641
+ case 3: {
642
+ const std::uint8_t *e = palette[px[0]];
643
+ emitPixel(base + x, e[0], e[1], e[2], e[3]);
644
+ break;
645
+ }
646
+ case 4: emitPixel(base + x, px[0], px[0], px[0], px[1]); break;
647
+ default: emitPixel(base + x, px[0], px[1], px[2], px[3]); break;
648
+ }
649
+ }
650
+ std::uint8_t *swap = prev;
651
+ prev = cur;
652
+ cur = swap;
653
+ filled = 0;
654
+ row++;
655
+ }
656
+
657
+ void consume(const std::uint8_t *bytes, std::size_t count)
658
+ {
659
+ while (count > 0 && row < height && !failed) {
660
+ const std::size_t need = stride + 1 - filled;
661
+ const std::size_t take = count < need ? count : need;
662
+ std::memcpy(cur + filled, bytes, take);
663
+ filled += take;
664
+ bytes += take;
665
+ count -= take;
666
+ if (filled == stride + 1) finishRow();
667
+ }
668
+ }
669
+ };
670
+
671
+ // Offset of the next IDAT chunk at or after `pos`, or -1.
672
+ long nextIdat(const std::uint8_t *data, int length, long pos)
673
+ {
674
+ while (pos + 12 <= length) {
675
+ const std::uint32_t size = pngU32(data + pos);
676
+ if (size > static_cast<std::uint32_t>(length) || pos + 12 + static_cast<long>(size) > length) return -1;
677
+ if (std::memcmp(data + pos + 4, "IDAT", 4) == 0) return pos;
678
+ if (std::memcmp(data + pos + 4, "IEND", 4) == 0) return -1;
679
+ pos += 12 + static_cast<long>(size);
680
+ }
681
+ return -1;
682
+ }
683
+
684
+ int decodePngStreaming(ImageSlot &image, const std::uint8_t *data, int length, bool opaqueHint)
685
+ {
686
+ static const std::uint8_t kSignature[8] = {0x89, 'P', 'N', 'G', '\r', '\n', 0x1a, '\n'};
687
+ if (length < 33 || std::memcmp(data, kSignature, 8) != 0 || std::memcmp(data + 12, "IHDR", 4) != 0) return kPngUnsupported;
688
+ const std::uint32_t width = pngU32(data + 16);
689
+ const std::uint32_t height = pngU32(data + 20);
690
+ const int depth = data[24];
691
+ const int colorType = data[25];
692
+ const int interlace = data[28];
693
+ int channels = 0;
694
+ switch (colorType) {
695
+ case 0: channels = 1; break;
696
+ case 2: channels = 3; break;
697
+ case 3: channels = 1; break;
698
+ case 4: channels = 2; break;
699
+ case 6: channels = 4; break;
700
+ default: return kPngUnsupported;
701
+ }
702
+ if (depth != 8 || interlace != 0 || width == 0 || height == 0 || width > 16384 || height > 16384) return kPngUnsupported;
703
+
704
+ PngStream png;
705
+ png.width = static_cast<int>(width);
706
+ png.height = static_cast<int>(height);
707
+ png.colorType = colorType;
708
+ png.channels = channels;
709
+ png.stride = static_cast<std::size_t>(width) * channels;
710
+ png.opaqueHint = opaqueHint;
711
+
712
+ // Palette and tRNS precede the image data.
713
+ bool havePalette = false;
714
+ for (long pos = 33; pos + 12 <= length;) {
715
+ const std::uint32_t size = pngU32(data + pos);
716
+ if (pos + 12 + static_cast<long>(size) > length) return kPngUnsupported;
717
+ const std::uint8_t *body = data + pos + 8;
718
+ if (std::memcmp(data + pos + 4, "IDAT", 4) == 0 || std::memcmp(data + pos + 4, "IEND", 4) == 0) break;
719
+ if (std::memcmp(data + pos + 4, "PLTE", 4) == 0) {
720
+ for (std::uint32_t i = 0; i < size / 3 && i < 256; i++) {
721
+ png.palette[i][0] = body[i * 3];
722
+ png.palette[i][1] = body[i * 3 + 1];
723
+ png.palette[i][2] = body[i * 3 + 2];
724
+ png.palette[i][3] = 255;
725
+ }
726
+ havePalette = true;
727
+ } else if (std::memcmp(data + pos + 4, "tRNS", 4) == 0) {
728
+ // Colour-key transparency on gray/RGB is rare in assets; leave it to stb.
729
+ if (colorType != 3) return kPngUnsupported;
730
+ for (std::uint32_t i = 0; i < size && i < 256; i++) png.palette[i][3] = body[i];
731
+ }
732
+ pos += 12 + static_cast<long>(size);
733
+ }
734
+ if (colorType == 3 && !havePalette) return kPngUnsupported;
735
+ long idat = nextIdat(data, length, 33);
736
+ if (idat < 0) return kPngUnsupported;
737
+
738
+ const std::size_t pixelCount = static_cast<std::size_t>(width) * height;
739
+ png.pixels = ImageMemory::allocate<pixel::native_t>(pixelCount);
740
+ png.prev = ImageMemory::allocate<std::uint8_t>(png.stride + 1);
741
+ png.cur = ImageMemory::allocate<std::uint8_t>(png.stride + 1);
742
+ auto *inflator = ImageMemory::allocate<tinfl_decompressor>();
743
+ auto *window = ImageMemory::allocate<std::uint8_t>(TINFL_LZ_DICT_SIZE);
744
+ auto cleanup = [&](bool keepResult) {
745
+ ImageMemory::release(png.prev);
746
+ ImageMemory::release(png.cur);
747
+ ImageMemory::release(inflator);
748
+ ImageMemory::release(window);
749
+ if (!keepResult) {
750
+ ImageMemory::release(png.pixels);
751
+ ImageMemory::release(png.alpha);
752
+ }
753
+ };
754
+ if (!png.pixels || !png.prev || !png.cur || !inflator || !window) {
755
+ cleanup(false);
756
+ return -1;
757
+ }
758
+ std::memset(png.prev, 0, png.stride + 1);
759
+ tinfl_init(inflator);
760
+
761
+ std::size_t windowOffset = 0;
762
+ const std::uint8_t *in = data + idat + 8;
763
+ std::size_t inLeft = pngU32(data + idat);
764
+ long following = nextIdat(data, length, idat + 12 + static_cast<long>(inLeft));
765
+ tinfl_status status = TINFL_STATUS_NEEDS_MORE_INPUT;
766
+ while (!png.failed) {
767
+ std::size_t inSize = inLeft;
768
+ std::size_t outSize = TINFL_LZ_DICT_SIZE - windowOffset;
769
+ const mz_uint32 flags = TINFL_FLAG_PARSE_ZLIB_HEADER | (following >= 0 ? TINFL_FLAG_HAS_MORE_INPUT : 0);
770
+ status = tinfl_decompress(inflator, in, &inSize, window, window + windowOffset, &outSize, flags);
771
+ in += inSize;
772
+ inLeft -= inSize;
773
+ png.consume(window + windowOffset, outSize);
774
+ windowOffset = (windowOffset + outSize) & (TINFL_LZ_DICT_SIZE - 1);
775
+ if (status == TINFL_STATUS_DONE || png.row >= png.height) break;
776
+ if (status < 0) break;
777
+ if (status == TINFL_STATUS_NEEDS_MORE_INPUT && inLeft == 0) {
778
+ if (following < 0) break;
779
+ idat = following;
780
+ in = data + idat + 8;
781
+ inLeft = pngU32(data + idat);
782
+ following = nextIdat(data, length, idat + 12 + static_cast<long>(inLeft));
783
+ }
784
+ }
785
+ if (png.failed || png.row < png.height) {
786
+ cleanup(false);
787
+ return -1;
788
+ }
789
+ if (opaqueHint && png.alpha && !png.hasZero) {
790
+ ImageMemory::release(png.alpha);
791
+ png.alpha = nullptr;
792
+ }
793
+ cleanup(true);
794
+ image.width = png.width;
795
+ image.height = png.height;
796
+ image.frameCount = 1;
797
+ image.pixels = png.pixels;
798
+ image.ownsPixels = true;
799
+ image.alpha = png.alpha;
800
+ image.loopCount = 0;
801
+ image.playing = false;
802
+ return 0;
803
+ }
804
+
805
+ } // namespace
806
+ #endif
807
+
538
808
  int ImageStore::decodeStatic(ImageSlot &image, const std::uint8_t *data, int length, bool opaqueHint)
539
809
  {
540
810
  int width = 0;
@@ -638,6 +908,13 @@ int ImageStore::decodeStatic(ImageSlot &image, const std::uint8_t *data, int len
638
908
  }
639
909
  #endif
640
910
 
911
+ #if defined(GEA_EMBEDDED_ROM_PNG_STREAM) && GEA_EMBEDDED_ROM_PNG_STREAM
912
+ if (detectFormat(data, length) == ImageFormat::Png) {
913
+ const int rc = decodePngStreaming(image, data, length, opaqueHint);
914
+ if (rc != kPngUnsupported) return rc;
915
+ }
916
+ #endif
917
+
641
918
  stbi_uc *rgba = stbi_load_from_memory(data, length, &width, &height, &components, 4);
642
919
  if (!rgba) return -1;
643
920
 
@@ -761,9 +1038,39 @@ void ImageStore::materializeDeferred(int id) const
761
1038
  image->deferredData = nullptr;
762
1039
  image->deferredLength = 0;
763
1040
  image->deferredOpaque = false;
1041
+ image->sourceData = data;
1042
+ image->sourceLength = length;
1043
+ image->sourceOpaque = opaque;
764
1044
  self->decodeStatic(*image, data, length, opaque);
765
1045
  }
766
1046
 
1047
+ #ifndef GEA_IMAGE_IDLE_DECODE_RELEASE_BYTES
1048
+ #define GEA_IMAGE_IDLE_DECODE_RELEASE_BYTES (256 * 1024)
1049
+ #endif
1050
+
1051
+ void ImageStore::releaseIdleDecodes()
1052
+ {
1053
+ ImageStoreLock lock(slotMutex_);
1054
+ for (int id = 0; id < kImageMax; ++id) {
1055
+ ImageSlot &image = images_[id];
1056
+ if (!used_[id] || references_[id] != 0 || retired_[id]) continue;
1057
+ if (!image.sourceData || !image.ownsPixels || !image.pixels || image.mutablePixels) continue;
1058
+ const std::size_t bytes = static_cast<std::size_t>(image.width) * static_cast<std::size_t>(image.height) *
1059
+ sizeof(pixel::native_t);
1060
+ if (bytes < GEA_IMAGE_IDLE_DECODE_RELEASE_BYTES) continue;
1061
+ ImageMemory::release(image.pixels);
1062
+ ImageMemory::release(image.alpha);
1063
+ image.pixels = nullptr;
1064
+ image.alpha = nullptr;
1065
+ image.ownsPixels = false;
1066
+ image.deferredData = image.sourceData;
1067
+ image.deferredLength = image.sourceLength;
1068
+ image.deferredOpaque = image.sourceOpaque;
1069
+ image.sourceData = nullptr;
1070
+ image.sourceLength = 0;
1071
+ }
1072
+ }
1073
+
767
1074
  int ImageStore::decode(const std::uint8_t *data, int length, int preferredSlot)
768
1075
  {
769
1076
  return decodeWithHint(data, length, preferredSlot, false);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/engine",
3
- "version": "0.1.20",
3
+ "version": "0.1.22",
4
4
  "description": "GeaStack rendering engine, intrinsic JSX primitives, and native engine backends.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -259,3 +259,54 @@ with the preceding 164-byte candidate, this saves 648 B in TreeState and
259
259
  11,664 B of firmware. The 49-node host allocation fixture is unchanged because
260
260
  of allocator rounding; these record reductions are not a host heap saving.
261
261
  This remains a development checkpoint, not a 50-byte node or a 60 fps guarantee.
262
+
263
+
264
+ ## Inline SVG
265
+
266
+ `<svg>` and its shapes are ordinary nodes (`svg_element.h`): they take classes,
267
+ inline style, listeners and `:active`, and `getAttribute` returns full path
268
+ data. The `<svg>` box lays out like a view (its `width`/`height` attributes are
269
+ its default size; CSS wins) and records one `DrawSvg` display command. That
270
+ command rasterizes the svg's cached scene into the framebuffer with exact-area
271
+ anti-aliasing, honouring clip and opacity. Shapes are out of flow and zero-size.
272
+ The scene rebuilds once, on the next frame after anything in the subtree changes
273
+ (attribute, class, style, structure), and only the svg's box repaints.
274
+
275
+ Supported:
276
+
277
+ - Elements: `svg`, `g`, `path`, `polyline`, `polygon`, `line`, `circle`,
278
+ `ellipse` and `rect` (with `rx`/`ry`). `defs`, `title`, `desc` and `text`
279
+ are accepted and not painted.
280
+ - `viewBox` with `preserveAspectRatio` (all align values, `meet`/`slice`,
281
+ `none`).
282
+ - Path data: `M L H V C S Q T A Z`, absolute and relative, with implicit
283
+ repeats and compact arc flags. Parsing stops at the first error and keeps
284
+ what came before it, as browsers do.
285
+ - `transform` on shapes and groups: `matrix`, `translate`, `scale`,
286
+ `rotate(a [cx cy])`, `skewX` and `skewY`.
287
+ - Paint: `fill`, `stroke`, `stroke-width`, `stroke-linecap` (butt, round,
288
+ square), `stroke-linejoin` (miter, round, bevel), `stroke-miterlimit`,
289
+ `fill-rule`, `opacity`, `fill-opacity`, `stroke-opacity`, `color` with
290
+ `currentColor`, `visibility`, `display` and `pointer-events`. Colours are
291
+ hex (3, 4, 6 or 8 digits), `rgb[a]()`, `hsl[a]()`, named colours and
292
+ `transparent`.
293
+ - The same paint properties as CSS (`.slice { fill: #38bdf8; stroke-width: 3px }`,
294
+ `style={{ fill: color }}`). CSS beats presentation attributes, and the
295
+ inheritable ones inherit through `g`. Group `opacity` multiplies into each
296
+ shape; it is not composited as a layer.
297
+ - Touch: the hit test descends into the svg by geometry, either the fill's
298
+ winding or the stroke within 3px of slop. The topmost shape receives the
299
+ event, which bubbles through `g` and `svg`. `pointer-events: none` shapes
300
+ are transparent to touch.
301
+
302
+ Not supported: `<text>`, `<use>`, gradients and patterns, `clipPath`/`mask`,
303
+ markers, dashes (`stroke-dasharray`), filters, and units other than px.
304
+ `var()` in paint values is not supported either. A CSS `transform` on the
305
+ `<svg>` element itself moves its box, but the scene is not reprojected into the
306
+ transformed space.
307
+
308
+ Cost: the svg paint fields live in the pooled `RareStyle`, allocated only for
309
+ nodes that set a rare field. Ordinary nodes carry nothing new. The plugin
310
+ reports `node-svg` when the source can create an svg element (JSX `<svg>`,
311
+ `createElement('svg')`, `SVG*Element`, opaque markup). The CLI turns that into
312
+ `GEA_UI_SVG`, which compiles the whole feature out of apps that have no svg.