@geastack/engine 0.1.6 → 0.1.7

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/README.md CHANGED
@@ -24,6 +24,76 @@ The component prop types live in `@geastack/core`. The C++ sources are
24
24
  compiled into a target through `@geastack/core`'s source manifest. Vendored
25
25
  third-party code is listed in the repository's `THIRD_PARTY.md`.
26
26
 
27
+ ## Renderer cache reachability
28
+
29
+ ESP32 app builds derive renderer cache storage from the Gea compiler plugin's
30
+ source analysis. Circle operations, CSS transforms, linear gradients and radial
31
+ gradients each retain their own caches only when used. A video canvas that calls
32
+ `clearRect` and `drawImage`, for example, does not reserve circle tables. Ordinary
33
+ rounded corners continue to render without them.
34
+
35
+ The analysis follows source imports, imported CSS and CSS `@import`, JSX style
36
+ objects, style assignments and DOM style setters. Unknown styles or opaque
37
+ imports conservatively retain the corresponding features. The CLI passes the
38
+ result to both the app and the shared engine; no per-app switches are needed.
39
+ Position storage is selected independently for each physical edge and for
40
+ pixel versus percentage offsets. Unknown/logical insets retain every possible
41
+ edge. Native inputs and lists retain the edges they can mutate. The selected
42
+ fields remain embedded, aligned members; selection happens at compile time,
43
+ with no runtime accessor or extra pointer load. Cached style operations also
44
+ omit transform payloads when transforms are absent.
45
+
46
+ Text and border alpha bytes are omitted only when the complete source graph
47
+ proves every reachable color opaque. Literal CSS, custom-color definitions,
48
+ immutable local bindings and bounded palette/getter values participate in that
49
+ build-time proof. Unknown receivers, writes, escaping objects, palette mutation,
50
+ reflection, and native controls retain storage. Background alpha is independent.
51
+ The retained values stay inline; omitted fields read as the constant 255.
52
+ Node records reuse tail padding while preserving member assignment and reset
53
+ semantics, without a pointer lookup or packed-field decoding.
54
+
55
+ CSS animation support is likewise derived from whole-source reachability.
56
+ Animation/transition declarations, keyframes, `data-anim`, imperative animation
57
+ APIs and opaque styles/native UI calls retain it. Animation-free apps omit the
58
+ compiled animation handle in each CSS rule, active-list storage, priming and
59
+ per-frame animation polling. `requestAnimationFrame` is independent and remains
60
+ available. Older analysis versions retain the full animation engine; application
61
+ defines cannot override the proof. Enabled animation and frame-scheduling tests
62
+ run alongside the pruned geometry/pixel checks.
63
+
64
+ Generated `::before`/`::after` node support is also automatic. Literal selectors,
65
+ `content` writes and opaque CSS/native mutation retain it; older analyzers keep
66
+ support by default. First-line and animation buckets remain independent. Absent
67
+ families reserve no rule-plan buckets or cached bucket counters. CSS resets copy
68
+ an immutable default style directly, without constructing a temporary Node or
69
+ initializing unrelated text, links, layout and render state.
70
+
71
+ Cache-size definitions affect only retained features. Older analyzers and native
72
+ builds without feature analysis keep the full engine defaults.
73
+
74
+ Pruned caches have no persistent storage. The engine retains uncached rendering
75
+ paths for native or dynamically supplied instructions, preserving pixels and hit
76
+ testing even when such instructions are absent from the source analysis.
77
+ `packages/core/test/run-renderer-features.sh` compares enabled/pruned rendering
78
+ and verifies that pruned cache storage symbols are absent from the binary.
79
+
80
+ ## Native JPEG decoding
81
+
82
+ ESP targets that link Espressif's `esp_new_jpeg` component automatically use it
83
+ when its `esp_jpeg_dec.h` header is visible and the target stores RGB565 pixels.
84
+ The decoder writes directly to the aligned image buffer in the target's byte
85
+ order, preserving source dimensions. It does not allocate an intermediate RGBA
86
+ frame. Unsupported images fall back to the ESP32-S3 ROM decoder, then stb.
87
+ Targets without the component retain their existing decoder fallback.
88
+
89
+ The S3 ROM path also preserves full colour and source dimensions, converting
90
+ individual RGB888 MCU blocks into the final native image with an 8 KiB workspace.
91
+ `packages/core/test/run-native-jpeg.sh` checks the backend API contracts, native
92
+ pixels, byte order, cleanup and fallback using host mocks. Actual codec speed
93
+ and output must be checked on hardware. With `GEA_EMBEDDED_PERF=1`, the optional
94
+ Espressif backend reports its result and duration on the first and every tenth
95
+ decode.
96
+
27
97
  ## License
28
98
 
29
99
  Apache-2.0. See `LICENSE`.
package/canvas.cpp CHANGED
@@ -1,4 +1,5 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
+ #include "ui/renderer_features.h"
2
3
  #include "canvas.h"
3
4
  #include "display.h"
4
5
  #include "graphics/font.h"
@@ -833,6 +834,7 @@ SolidGlyphCacheEntry *solidGlyphCacheEntry(const RasterizedFont &font,
833
834
 
834
835
  class CanvasMath {
835
836
  public:
837
+ #if GEA_EMBEDDED_RENDERER_CIRCLES
836
838
  #ifndef GEA_EMBEDDED_CANVAS_CIRCLE_RADIUS_MAX
837
839
  #define GEA_EMBEDDED_CANVAS_CIRCLE_RADIUS_MAX 63
838
840
  #endif
@@ -853,6 +855,7 @@ public:
853
855
  static_assert(kCircleBoxSpanMax > 0 && kCircleBoxSpanMax <= 255);
854
856
  static_assert(kCircleBoxSpanSlots > 0);
855
857
 
858
+ #endif
856
859
  static int min(int a, int b) { return a < b ? a : b; }
857
860
 
858
861
  static int integerSqrt(int n)
@@ -867,33 +870,39 @@ public:
867
870
  return x;
868
871
  }
869
872
 
870
- const std::uint8_t (*circleSpans(int size))[2]
873
+ static const std::uint8_t (*circleSpans(int size))[2]
871
874
  {
875
+ #if GEA_EMBEDDED_RENDERER_CIRCLES
872
876
  if (size <= 0 || size > kCircleSpanMax || (size & 1)) return nullptr;
873
877
  int slot = size / 2;
874
878
  std::uint32_t bit = 1u << slot;
875
- if (!(circleSpanCacheReady_ & bit)) {
879
+ if (!(instance().circleSpanCacheReady_ & bit)) {
876
880
  int radius2 = size * size;
877
881
  for (int row = 0; row < size; row++) {
878
882
  int dy2 = row * 2 + 1 - size;
879
883
  int dx2 = integerSqrt(radius2 - dy2 * dy2);
880
- circleSpanCache_[slot][row][0] = (std::uint8_t)((size - dx2) / 2);
881
- circleSpanCache_[slot][row][1] = (std::uint8_t)((size + dx2 - 1) / 2);
884
+ instance().circleSpanCache_[slot][row][0] = (std::uint8_t)((size - dx2) / 2);
885
+ instance().circleSpanCache_[slot][row][1] = (std::uint8_t)((size + dx2 - 1) / 2);
882
886
  }
883
- circleSpanCacheReady_ |= bit;
887
+ instance().circleSpanCacheReady_ |= bit;
884
888
  }
885
- return circleSpanCache_[slot];
889
+ return instance().circleSpanCache_[slot];
890
+ #else
891
+ (void)size;
892
+ return nullptr;
893
+ #endif
886
894
  }
887
895
 
888
- const std::uint8_t (*circleBoxSpans(int size))[2]
896
+ static const std::uint8_t (*circleBoxSpans(int size))[2]
889
897
  {
898
+ #if GEA_EMBEDDED_RENDERER_CIRCLES
890
899
  if (size <= 0 || size > kCircleBoxSpanMax) return nullptr;
891
900
  for (int slot = 0; slot < kCircleBoxSpanSlots; ++slot) {
892
- if (circleBoxSpanSize_[slot] == size)
893
- return circleBoxSpanCache_[slot];
901
+ if (instance().circleBoxSpanSize_[slot] == size)
902
+ return instance().circleBoxSpanCache_[slot];
894
903
  }
895
- const int slot = circleBoxSpanNext_;
896
- circleBoxSpanNext_ = (circleBoxSpanNext_ + 1) % kCircleBoxSpanSlots;
904
+ const int slot = instance().circleBoxSpanNext_;
905
+ instance().circleBoxSpanNext_ = (instance().circleBoxSpanNext_ + 1) % kCircleBoxSpanSlots;
897
906
  const long long ss = static_cast<long long>(size) * static_cast<long long>(size);
898
907
  for (int row = 0; row < size; row++) {
899
908
  const long long dy2 = static_cast<long long>(row * 2 + 1 - size);
@@ -904,31 +913,41 @@ public:
904
913
  int sx1 = (size + dx2 - 1) / 2;
905
914
  if (sx0 < 0) sx0 = 0;
906
915
  if (sx1 >= size) sx1 = size - 1;
907
- circleBoxSpanCache_[slot][row][0] = static_cast<std::uint8_t>(sx0);
908
- circleBoxSpanCache_[slot][row][1] = static_cast<std::uint8_t>(sx1);
916
+ instance().circleBoxSpanCache_[slot][row][0] = static_cast<std::uint8_t>(sx0);
917
+ instance().circleBoxSpanCache_[slot][row][1] = static_cast<std::uint8_t>(sx1);
909
918
  }
910
- circleBoxSpanSize_[slot] = size;
911
- return circleBoxSpanCache_[slot];
919
+ instance().circleBoxSpanSize_[slot] = size;
920
+ return instance().circleBoxSpanCache_[slot];
921
+ #else
922
+ (void)size;
923
+ return nullptr;
924
+ #endif
912
925
  }
913
926
 
914
- const std::uint8_t (*circleRadiusSpans(int radius))[2]
927
+ static const std::uint8_t (*circleRadiusSpans(int radius))[2]
915
928
  {
929
+ #if GEA_EMBEDDED_RENDERER_CIRCLES
916
930
  if (radius <= 0 || radius > kCircleRadiusMax) return nullptr;
917
931
  std::uint64_t bit = 1ull << radius;
918
- if (!(circleRadiusSpanCacheReady_ & bit)) {
932
+ if (!(instance().circleRadiusSpanCacheReady_ & bit)) {
919
933
  const int diameter = radius * 2 + 1;
920
934
  const int rr = radius * radius;
921
935
  for (int row = 0; row < diameter; row++) {
922
936
  const int dy = row - radius;
923
937
  const int dx = integerSqrt(rr - dy * dy);
924
- circleRadiusSpanCache_[radius][row][0] = (std::uint8_t)(radius - dx);
925
- circleRadiusSpanCache_[radius][row][1] = (std::uint8_t)(radius + dx);
938
+ instance().circleRadiusSpanCache_[radius][row][0] = (std::uint8_t)(radius - dx);
939
+ instance().circleRadiusSpanCache_[radius][row][1] = (std::uint8_t)(radius + dx);
926
940
  }
927
- circleRadiusSpanCacheReady_ |= bit;
941
+ instance().circleRadiusSpanCacheReady_ |= bit;
928
942
  }
929
- return circleRadiusSpanCache_[radius];
943
+ return instance().circleRadiusSpanCache_[radius];
944
+ #else
945
+ (void)radius;
946
+ return nullptr;
947
+ #endif
930
948
  }
931
949
 
950
+ #if GEA_EMBEDDED_RENDERER_CIRCLES
932
951
  static CanvasMath &instance()
933
952
  {
934
953
  static CanvasMath math;
@@ -943,6 +962,7 @@ private:
943
962
  std::uint8_t circleBoxSpanCache_[kCircleBoxSpanSlots][kCircleBoxSpanMax][2]{};
944
963
  std::uint8_t circleBoxSpanSize_[kCircleBoxSpanSlots]{};
945
964
  int circleBoxSpanNext_ = 0;
965
+ #endif
946
966
  };
947
967
 
948
968
  pixel::native_t Canvas::readPixelNative(int x, int y) const
@@ -1658,7 +1678,7 @@ bool Canvas::fillCircleNoDirty(int cx, int cy, int r, pixel::native_t color)
1658
1678
  const ClipRect *clip = &clipStack_[clipDepth_];
1659
1679
  if (cx + r < clip->x0 || cx - r > clip->x1 || cy + r < clip->y0 || cy - r > clip->y1) return false;
1660
1680
 
1661
- const std::uint8_t (*spans)[2] = CanvasMath::instance().circleRadiusSpans(r);
1681
+ const std::uint8_t (*spans)[2] = CanvasMath::circleRadiusSpans(r);
1662
1682
  if (!spans) {
1663
1683
  const int rr = r * r;
1664
1684
  int dy0 = -r;
@@ -1807,7 +1827,7 @@ void Canvas::fillCirclesRgb565WorldYSorted(const CircleEntry *circles,
1807
1827
  int yOffset)
1808
1828
  {
1809
1829
  if (!pixels_ || !circles || count <= 0 || r <= 0) return;
1810
- const std::uint8_t (*spans)[2] = CanvasMath::instance().circleRadiusSpans(r);
1830
+ const std::uint8_t (*spans)[2] = CanvasMath::circleRadiusSpans(r);
1811
1831
  const int diameter = r * 2 + 1;
1812
1832
  const int w = width_;
1813
1833
  const int h = height_;
@@ -3226,8 +3246,8 @@ void Canvas::fillQuarterCircle(int cx, int cy, int r, int quadrant, pixel::nativ
3226
3246
  void Canvas::fillCircleBox(int x, int y, int size, pixel::native_t color)
3227
3247
  {
3228
3248
  if (!pixels_) return;
3229
- const std::uint8_t (*spans)[2] = CanvasMath::instance().circleSpans(size);
3230
- if (!spans) return;
3249
+ const std::uint8_t (*spans)[2] = CanvasMath::circleSpans(size);
3250
+ if (size <= 0 || size > 32 || (size & 1)) return;
3231
3251
  const ClipRect *clip = &clipStack_[clipDepth_];
3232
3252
 
3233
3253
  if (x > clip->x1 || y > clip->y1 || x + size - 1 < clip->x0 || y + size - 1 < clip->y0) return;
@@ -3242,8 +3262,10 @@ void Canvas::fillCircleBox(int x, int y, int size, pixel::native_t color)
3242
3262
  for (int row = row0; row <= row1; row++) {
3243
3263
  int sy = y + row;
3244
3264
 
3245
- int sx0 = x + spans[row][0];
3246
- int sx1 = x + spans[row][1];
3265
+ const int dy2 = row * 2 + 1 - size;
3266
+ const int dx2 = spans ? 0 : CanvasMath::integerSqrt(size * size - dy2 * dy2);
3267
+ int sx0 = x + (spans ? spans[row][0] : (size - dx2) / 2);
3268
+ int sx1 = x + (spans ? spans[row][1] : (size + dx2 - 1) / 2);
3247
3269
  if (sx0 < clip->x0) sx0 = clip->x0;
3248
3270
  if (sx1 > clip->x1) sx1 = clip->x1;
3249
3271
  if (sx0 < 0) sx0 = 0;
@@ -3275,7 +3297,7 @@ void Canvas::fillRoundedRectBoxesRgb565(const std::int16_t *xs,
3275
3297
  // four-corner rounded-rect raster.
3276
3298
  if (Canvas::antialiasSamples() < 2 && roundedRectIsCircleLike(w, h, tl, tr, br, bl) && h <= 256) {
3277
3299
  const std::uint8_t (*cachedSpans)[2] =
3278
- (w == h) ? CanvasMath::instance().circleBoxSpans(w) : nullptr;
3300
+ (w == h) ? CanvasMath::circleBoxSpans(w) : nullptr;
3279
3301
  std::uint16_t spanX0[256];
3280
3302
  std::uint16_t spanX1[256];
3281
3303
  if (!cachedSpans) {
@@ -3368,7 +3390,7 @@ void Canvas::fillRoundedRect(int x, int y, int w, int h, int tl, int tr, int br,
3368
3390
  // 62fps circle path (its poles are marginally rounder than the integer scanline).
3369
3391
  if (Canvas::antialiasSamples() < 2 && w == h &&
3370
3392
  tl == w / 2 && tr == w / 2 && br == w / 2 && bl == w / 2 &&
3371
- CanvasMath::instance().circleSpans(w)) {
3393
+ w > 0 && w <= 32 && !(w & 1)) {
3372
3394
  fillCircleBox(x, y, w, color);
3373
3395
  return;
3374
3396
  }
@@ -4378,7 +4400,7 @@ void Canvas::drawImageRounded(
4378
4400
  // lookup per row instead of an integer sqrt, and repeat draws of the
4379
4401
  // same radius (every full-size icon, every chunk that touches it) pay
4380
4402
  // nothing at all.
4381
- const std::uint8_t (*spans)[2] = CanvasMath::instance().circleRadiusSpans(rr);
4403
+ const std::uint8_t (*spans)[2] = CanvasMath::circleRadiusSpans(rr);
4382
4404
  for (int py = y0; py <= y1; py++) {
4383
4405
  const int ly = py - dy;
4384
4406
  int lo, hi;
package/css/declarative.h CHANGED
@@ -34,6 +34,7 @@ public:
34
34
  // Call once after an app mounts; nowMs is the current frame clock.
35
35
  static void scanAndStart(uint32_t nowMs)
36
36
  {
37
+ #if GEA_CSS_ANIMATIONS
37
38
  gea::embedded::ui::Tree &tree = gea::embedded::ui::Tree::instance();
38
39
  const int count = tree.nodeCount();
39
40
  for (int id = 0; id < count; ++id) {
@@ -41,9 +42,13 @@ public:
41
42
  if (!kind || kind[0] == '\0') continue;
42
43
  startForNode(tree, id, kind, nowMs);
43
44
  }
45
+ #else
46
+ (void)nowMs;
47
+ #endif
44
48
  }
45
49
 
46
50
  private:
51
+ #if GEA_CSS_ANIMATIONS
47
52
  static int attrInt(gea::embedded::ui::Tree &tree, int id, const char *name, int def)
48
53
  {
49
54
  const char *v = tree.getAttribute(id, name);
@@ -126,6 +131,7 @@ private:
126
131
  startOne(Property::Height, from, to);
127
132
  }
128
133
  }
134
+ #endif
129
135
  };
130
136
 
131
137
  } // namespace gea::css
package/css/engine.h CHANGED
@@ -25,6 +25,7 @@
25
25
 
26
26
  namespace gea::css {
27
27
 
28
+ #if GEA_CSS_ANIMATIONS
28
29
  class AnimationEngine {
29
30
  public:
30
31
  static AnimationEngine &instance()
@@ -225,4 +226,22 @@ private:
225
226
  int nextHandle_ = 1;
226
227
  };
227
228
 
229
+ #else
230
+ // The shared runtime still calls tick/clear in an animation-free app. These
231
+ // compile away without allocating an active-list singleton. A native producer
232
+ // must retain support; accidental use fails at compile time rather than dropping
233
+ // an animation silently.
234
+ class AnimationEngine {
235
+ public:
236
+ static AnimationEngine &instance() { static AnimationEngine engine; return engine; }
237
+ int start(Animation, uint32_t) = delete;
238
+ void cancel(int) {}
239
+ void cancelNode(int) {}
240
+ void clear() {}
241
+ bool active() const { return false; }
242
+ std::size_t count() const { return 0; }
243
+ void tick(uint32_t) {}
244
+ };
245
+ #endif
246
+
228
247
  } // namespace gea::css
package/image_store.cpp CHANGED
@@ -2,8 +2,35 @@
2
2
  #include "image.h"
3
3
  #include "memory.h"
4
4
  #include "pixel.h"
5
+ #include "gea_perf_config.h"
5
6
 
6
7
  #include <cstring>
8
+ #include <limits>
9
+
10
+ #if GEA_EMBEDDED_PERF && defined(ESP_PLATFORM)
11
+ #include "esp_timer.h"
12
+ #include <atomic>
13
+ #include <cstdio>
14
+ #endif
15
+
16
+ // Baseline JPEGs can stream from the S3 ROM decoder directly into native
17
+ // pixels. Keep unsupported JPEG variants on stb's general-purpose path.
18
+ #ifdef ESP_PLATFORM
19
+ #include "sdkconfig.h"
20
+ #endif
21
+ #if !defined(GEA_EMBEDDED_ROM_TJPGD) && defined(ESP_PLATFORM) && defined(CONFIG_IDF_TARGET_ESP32S3)
22
+ #define GEA_EMBEDDED_ROM_TJPGD 1
23
+ #endif
24
+
25
+ // A linked esp_new_jpeg component supplies the S3 SIMD decoder. It writes
26
+ // straight to native RGB565; other pixel formats keep the ROM/stb paths.
27
+ #if !defined(GEA_EMBEDDED_ESP_JPEG) && defined(ESP_PLATFORM) && \
28
+ GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565 && __has_include("esp_jpeg_dec.h")
29
+ #define GEA_EMBEDDED_ESP_JPEG 1
30
+ #endif
31
+ #if defined(GEA_EMBEDDED_ESP_JPEG) && GEA_EMBEDDED_ESP_JPEG && GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565
32
+ #include "esp_jpeg_dec.h"
33
+ #endif
7
34
 
8
35
  // Route stb_image's allocations through the framework allocator (PSRAM-first
9
36
  // on embedded targets). Plain malloc would land on the tiny SRAM newlib heap
@@ -50,12 +77,16 @@ void *gea_stbi_realloc_sized(void *ptr, std::size_t oldSize, std::size_t newSize
50
77
  #include "stb_image.h"
51
78
 
52
79
  #if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
53
- // ESP32 ROM TinyJPEG decompressor. Block-streaming with a small work pool and
54
- // 1/2–1/8 descaling, so a full-page cover decodes in a few KB of scratch instead
55
- // of stb_image's several MB of large contiguous buffers (which the reader's
56
- // fragmented PSRAM cannot satisfy).
80
+ // The ROM emits one RGB888 MCU at a time, not a full-frame RGB(A) buffer.
81
+ #if defined(CONFIG_IDF_TARGET_ESP32S3)
82
+ #include "esp32s3/rom/tjpgd.h"
83
+ #else
57
84
  #include "esp32/rom/tjpgd.h"
58
85
  #endif
86
+ #ifdef ESP_PLATFORM
87
+ #include "esp_heap_caps.h"
88
+ #endif
89
+ #endif
59
90
 
60
91
  #include "AnimatedGIF.h"
61
92
 
@@ -339,6 +370,73 @@ int ImageStore::decodeGif(ImageSlot &image, const std::uint8_t *data, int length
339
370
  return -1;
340
371
  }
341
372
 
373
+ #if defined(GEA_EMBEDDED_ESP_JPEG) && GEA_EMBEDDED_ESP_JPEG && GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565
374
+ int ImageStore::decodeJpegEsp(ImageSlot &image, const std::uint8_t *data, int length)
375
+ {
376
+ #if GEA_EMBEDDED_PERF && defined(ESP_PLATFORM)
377
+ static std::atomic<unsigned> sampleSequence{0};
378
+ const bool measure = sampleSequence.fetch_add(1, std::memory_order_relaxed) % 10 == 0;
379
+ const std::int64_t started = measure ? esp_timer_get_time() : 0;
380
+ #endif
381
+ const auto result = [&](jpeg_error_t rc, const char *stage) {
382
+ #if GEA_EMBEDDED_PERF && defined(ESP_PLATFORM)
383
+ if (measure) std::printf("[jpeg-esp] rc=%d stage=%s size=%dx%d total_us=%lld fallback=%s\n",
384
+ static_cast<int>(rc), stage, image.width, image.height,
385
+ static_cast<long long>(esp_timer_get_time() - started), rc == JPEG_ERR_OK ? "no" : "yes");
386
+ #else
387
+ (void)stage;
388
+ #endif
389
+ return rc == JPEG_ERR_OK ? 0 : -1;
390
+ };
391
+ struct Decoder {
392
+ jpeg_dec_handle_t handle = nullptr;
393
+ ~Decoder() { if (handle) jpeg_dec_close(handle); }
394
+ } decoder;
395
+ jpeg_dec_config_t config = DEFAULT_JPEG_DEC_CONFIG();
396
+ #if GEA_EMBEDDED_PIXEL_PANEL_ENDIAN
397
+ config.output_type = JPEG_PIXEL_FORMAT_RGB565_BE;
398
+ #else
399
+ config.output_type = JPEG_PIXEL_FORMAT_RGB565_LE;
400
+ #endif
401
+ // Plain whole-image decode supports arbitrary dimensions, including 410x502.
402
+ // Block mode, rotation and resize have stricter alignment requirements.
403
+ jpeg_error_t rc = jpeg_dec_open(&config, &decoder.handle);
404
+ if (rc != JPEG_ERR_OK) return result(rc, "open");
405
+ jpeg_dec_io_t io{};
406
+ io.inbuf = const_cast<std::uint8_t *>(data);
407
+ io.inbuf_len = length;
408
+ jpeg_dec_header_info_t info{};
409
+ rc = jpeg_dec_parse_header(decoder.handle, &io, &info);
410
+ if (rc != JPEG_ERR_OK) return result(rc, "header");
411
+ if (!info.width || !info.height || static_cast<std::size_t>(info.width) >
412
+ static_cast<std::size_t>(std::numeric_limits<int>::max()) / sizeof(pixel::native_t) / info.height)
413
+ return result(JPEG_ERR_INVALID_PARAM, "dimensions");
414
+ const std::size_t count = static_cast<std::size_t>(info.width) * info.height;
415
+ const int byteCount = static_cast<int>(count * sizeof(pixel::native_t));
416
+ int outputBytes = 0;
417
+ rc = jpeg_dec_get_outbuf_len(decoder.handle, &outputBytes);
418
+ if (rc != JPEG_ERR_OK) return result(rc, "buffer-size");
419
+ if (outputBytes != byteCount) return result(JPEG_ERR_INVALID_PARAM, "buffer-size");
420
+ auto *pixels = ImageMemory::allocate<pixel::native_t>(count);
421
+ if (!pixels) return result(JPEG_ERR_NO_MEM, "allocation");
422
+ io.outbuf = reinterpret_cast<std::uint8_t *>(pixels);
423
+ rc = jpeg_dec_process(decoder.handle, &io);
424
+ if (rc != JPEG_ERR_OK || io.out_size != byteCount) {
425
+ ImageMemory::release(pixels);
426
+ return result(rc == JPEG_ERR_OK ? JPEG_ERR_BAD_DATA : rc, "decode");
427
+ }
428
+ image.width = info.width;
429
+ image.height = info.height;
430
+ image.frameCount = 1;
431
+ image.pixels = pixels;
432
+ image.ownsPixels = true;
433
+ image.alpha = nullptr;
434
+ image.loopCount = 0;
435
+ image.playing = false;
436
+ return result(JPEG_ERR_OK, "decode");
437
+ }
438
+ #endif
439
+
342
440
  #if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
343
441
  namespace {
344
442
  // Shared input+output context for one ROM-tjpgd session (jd->device carries it
@@ -363,10 +461,13 @@ UINT geaTjpgdInput(JDEC *jd, BYTE *buf, UINT nbyte)
363
461
  return static_cast<UINT>(take);
364
462
  }
365
463
 
366
- // Receive a decoded RGB888 block and write it (as luma) into the native buffer.
464
+ // Convert each MCU directly into the final image. Preserve colour and source
465
+ // dimensions: image loading must not silently resize a 502px-high video frame.
367
466
  UINT geaTjpgdOutput(JDEC *jd, void *bitmap, JRECT *rect)
368
467
  {
369
468
  auto *c = static_cast<TjpgdCtx *>(jd->device);
469
+ if (!bitmap || rect->left > rect->right || rect->top > rect->bottom ||
470
+ rect->right >= c->outW || rect->bottom >= c->outH) return 0;
370
471
  const std::uint8_t *rgb = static_cast<const std::uint8_t *>(bitmap);
371
472
  for (int y = rect->top; y <= rect->bottom; y++) {
372
473
  for (int x = rect->left; x <= rect->right; x++) {
@@ -374,48 +475,49 @@ UINT geaTjpgdOutput(JDEC *jd, void *bitmap, JRECT *rect)
374
475
  const std::uint8_t g = rgb[1];
375
476
  const std::uint8_t b = rgb[2];
376
477
  rgb += 3;
377
- if (x < c->outW && y < c->outH) {
378
- const std::uint8_t lum = static_cast<std::uint8_t>((r * 77 + g * 150 + b * 29) >> 8);
379
- c->out[y * c->outW + x] = rgbaToNative(lum, lum, lum, 255);
380
- }
478
+ c->out[static_cast<std::size_t>(y) * c->outW + x] = rgbaToNative(r, g, b, 255);
381
479
  }
382
480
  }
383
481
  return 1;
384
482
  }
385
483
  } // namespace
386
484
 
387
- // Decode a (large) JPEG with the ROM tinyjpeg decompressor, descaling so the
388
- // output fits the panel. Returns 0 on success (fills `image`), -1 otherwise.
485
+ // Decode at the source dimensions with bounded scratch plus the native image.
389
486
  int ImageStore::decodeJpegTjpgd(ImageSlot &image, const std::uint8_t *data, int length)
390
487
  {
391
488
  TjpgdCtx ctx{data, static_cast<std::size_t>(length), 0, nullptr, 0, 0};
392
- // Work pool for the huffman/quant tables + one-MCU scratch. Generous and
393
- // PSRAM-backed so jd_prepare never fails for lack of pool.
394
- const std::size_t poolSize = 64 * 1024;
395
- void *pool = memory::Allocator::allocatePreferSpiram(poolSize, alignof(std::max_align_t));
489
+
490
+ // Keep the frequently accessed Huffman/quant tables in internal memory when
491
+ // available. Allocation is per decode and falls back to PSRAM under pressure.
492
+ constexpr std::size_t poolSize = 8 * 1024;
493
+ void *pool = nullptr;
494
+ #ifdef ESP_PLATFORM
495
+ pool = heap_caps_malloc(poolSize, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
496
+ #endif
497
+ if (!pool) pool = memory::Allocator::allocatePreferSpiram(poolSize, alignof(std::max_align_t));
396
498
  if (!pool) return -1;
397
- JDEC jd;
398
- if (jd_prepare(&jd, geaTjpgdInput, pool, poolSize, &ctx) != JDR_OK) {
499
+ JDEC jd{};
500
+ const JRESULT prepareRc = jd_prepare(&jd, geaTjpgdInput, pool, poolSize, &ctx);
501
+ if (prepareRc != JDR_OK) {
399
502
  memory::Allocator::free(pool);
400
503
  return -1;
401
504
  }
402
- // Descale by powers of two until both axes sit at/under the panel's long
403
- // side; a grayscale e-paper contain-fitting the result shows no visible loss.
404
- int scale = 0;
405
- while (((static_cast<int>(jd.width) >> scale) > 480 || (static_cast<int>(jd.height) >> scale) > 480) && scale < 3) scale++;
406
- ctx.outW = static_cast<int>(jd.width) >> scale;
407
- ctx.outH = static_cast<int>(jd.height) >> scale;
408
- if (ctx.outW <= 0 || ctx.outH <= 0) {
505
+ if (jd.width == 0 || jd.height == 0 ||
506
+ jd.width > static_cast<UINT>(std::numeric_limits<int>::max()) ||
507
+ jd.height > static_cast<UINT>(std::numeric_limits<int>::max()) ||
508
+ static_cast<std::size_t>(jd.width) > std::numeric_limits<std::size_t>::max() / sizeof(pixel::native_t) / jd.height) {
409
509
  memory::Allocator::free(pool);
410
510
  return -1;
411
511
  }
512
+ ctx.outW = static_cast<int>(jd.width);
513
+ ctx.outH = static_cast<int>(jd.height);
412
514
  auto *px = ImageMemory::allocate<pixel::native_t>(static_cast<std::size_t>(ctx.outW) * ctx.outH);
413
515
  if (!px) {
414
516
  memory::Allocator::free(pool);
415
517
  return -1;
416
518
  }
417
519
  ctx.out = px;
418
- const JRESULT rc = jd_decomp(&jd, geaTjpgdOutput, static_cast<BYTE>(scale));
520
+ const JRESULT rc = jd_decomp(&jd, geaTjpgdOutput, 0);
419
521
  memory::Allocator::free(pool);
420
522
  if (rc != JDR_OK) {
421
523
  ImageMemory::release(px);
@@ -462,16 +564,17 @@ int ImageStore::decodeStatic(ImageSlot &image, const std::uint8_t *data, int len
462
564
  return 0;
463
565
  }
464
566
 
567
+ #if defined(GEA_EMBEDDED_ESP_JPEG) && GEA_EMBEDDED_ESP_JPEG && GEA_EMBEDDED_PIXEL_FORMAT == GEA_PIXEL_RGB565
568
+ if (detectFormat(data, length) == ImageFormat::Jpeg) {
569
+ if (decodeJpegEsp(image, data, length) == 0) return 0;
570
+ }
571
+ #endif
572
+
465
573
  #if defined(GEA_EMBEDDED_ROM_TJPGD) && GEA_EMBEDDED_ROM_TJPGD
466
- // Large JPEGs (covers/photos) go through the low-memory ROM decoder. Small
467
- // JPEGs keep the stb path (its fixed cost is fine and avoids the pool alloc).
574
+ // JPEG has no alpha; both loadBytes and loadBytesOpaque can use the native
575
+ // streaming path. Unsupported encodings (e.g. progressive) fall back to stb.
468
576
  if (detectFormat(data, length) == ImageFormat::Jpeg) {
469
- int jw = 0, jh = 0, jc = 0;
470
- if (stbi_info_from_memory(data, length, &jw, &jh, &jc) && static_cast<long long>(jw) * jh > 200000) {
471
- const int rc = decodeJpegTjpgd(image, data, length);
472
- if (rc == 0) return 0;
473
- // Fall through to stb on tjpgd failure (best-effort).
474
- }
577
+ if (decodeJpegTjpgd(image, data, length) == 0) return 0;
475
578
  }
476
579
  #endif
477
580
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/engine",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
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.26"
60
+ "@geastack/core": "^0.1.27"
61
61
  }
62
62
  }