@geastack/engine 0.1.14 → 0.1.16

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.
@@ -2,6 +2,7 @@
2
2
  #include "canvas_element.h"
3
3
 
4
4
  #include "display.h"
5
+ #include "display_present.h"
5
6
  #include "internal.h"
6
7
  #include "pixel.h"
7
8
  #include "tree_internal.h"
@@ -14,6 +15,7 @@
14
15
  #include <cmath>
15
16
  #include <cstdio>
16
17
  #include <cstdlib>
18
+ #include <unordered_map>
17
19
  #include <utility>
18
20
  #include <vector>
19
21
 
@@ -23,6 +25,179 @@ namespace gea::embedded::ui {
23
25
 
24
26
  namespace {
25
27
 
28
+ // One state per canvas node: every context for a node is a handle onto it.
29
+ // Contexts are made and used on the frame task only.
30
+ std::unordered_map<int, std::shared_ptr<CanvasContextState>> &contextStates()
31
+ {
32
+ static std::unordered_map<int, std::shared_ptr<CanvasContextState>> states;
33
+ return states;
34
+ }
35
+
36
+ } // namespace
37
+
38
+ std::shared_ptr<CanvasContextState> CanvasRenderingContext2D::stateFor(int nodeId)
39
+ {
40
+ // -1 is the direct-canvas display context (and an unattached default one).
41
+ auto &slot = contextStates()[nodeId < 0 ? -1 : nodeId];
42
+ if (!slot)
43
+ {
44
+ static std::uint32_t nextIdentity = 0;
45
+ slot = std::make_shared<CanvasContextState>();
46
+ slot->identity_ = ++nextIdentity;
47
+ }
48
+ return slot;
49
+ }
50
+
51
+ void CanvasRenderingContext2D::releaseState(int nodeId) { contextStates().erase(nodeId); }
52
+
53
+ namespace
54
+ {
55
+
56
+ bool retainedCanvasStyleEligible(const Node &node)
57
+ {
58
+ #if GEA_PIXEL_STORAGE_PACKED
59
+ return false;
60
+ #else
61
+ const auto *nodes = Tree::instance().nodes();
62
+ const int count = Tree::instance().nodeCount();
63
+ int id = static_cast<int>(&node - nodes);
64
+ for (int depth = 0; id >= 0 && id < count && depth < kMaxNodes; ++depth)
65
+ {
66
+ const auto &ancestor = nodes[id];
67
+ const auto &style = ancestor.computedStyle();
68
+ const auto &rare = rstyle(style);
69
+ if (style.opacity != 255 || style.mask_right_fade_width || rare.filter_present ||
70
+ rare.filter_blur_radius || rare.transform_present || rare.rotate_present ||
71
+ rare.translate_present || rare.scale_present || rare.perspective || rare.box_shadow_alpha)
72
+ return false;
73
+ for (int radius : style.border_radius)
74
+ if (radius != 0)
75
+ return false;
76
+ id = ancestor.parent;
77
+ }
78
+ if (id >= 0)
79
+ return false;
80
+ const auto &style = node.computedStyle();
81
+ for (int radius : style.border_radius)
82
+ if (radius != 0)
83
+ return false;
84
+ return true;
85
+ #endif
86
+ }
87
+
88
+ } // namespace
89
+
90
+ bool CanvasRenderingContext2D::hasRetainedCanvas(int nodeId)
91
+ {
92
+ const auto found = contextStates().find(nodeId);
93
+ return found != contextStates().end() && found->second->retainedFrame_ != nullptr;
94
+ }
95
+
96
+ bool CanvasRenderingContext2D::retainedCanvasDamage(int nodeId, int &x0, int &y0, int &x1, int &y1)
97
+ {
98
+ const auto found = contextStates().find(nodeId);
99
+ if (found == contextStates().end() || !found->second->retainedFrame_)
100
+ return false;
101
+ const auto &state = *found->second;
102
+ const auto &node = Tree::instance().node(nodeId);
103
+ if (!retainedCanvasStyleEligible(node) || node.layout.width != state.retainedWidth_ ||
104
+ node.layout.height != state.retainedHeight_ || state.retainedDamageX0_ > state.retainedDamageX1_ ||
105
+ state.retainedDamageY0_ > state.retainedDamageY1_)
106
+ return false;
107
+ x0 = state.retainedDamageX0_;
108
+ y0 = state.retainedDamageY0_;
109
+ x1 = state.retainedDamageX1_;
110
+ y1 = state.retainedDamageY1_;
111
+ return true;
112
+ }
113
+
114
+ void CanvasRenderingContext2D::clearRetainedCanvasDamage()
115
+ {
116
+ // Document refresh consumes display damage. Snapshot replay never calls this.
117
+ for (auto &[id, state] : contextStates())
118
+ {
119
+ state->retainedDamageX1_ = state->retainedDamageY1_ = -1;
120
+ state->retainedBatchChanged_ = false;
121
+ }
122
+ }
123
+
124
+ void CanvasRenderingContext2D::materializeRetainedCanvas(int nodeId)
125
+ {
126
+ const auto found = contextStates().find(nodeId);
127
+ if (found == contextStates().end() || !found->second->retainedFrame_)
128
+ return;
129
+ auto &state = *found->second;
130
+ auto frame = std::move(state.retainedFrame_);
131
+ // Clear the retained marker before ensureCanvas: its allocation hook calls
132
+ // this method too. Preserve the intrinsic size before any resize allocates.
133
+ auto *surface = Tree::instance().ensureCanvas(nodeId, state.retainedWidth_, state.retainedHeight_);
134
+ if (surface && surface->pixels())
135
+ {
136
+ gea::framework::display_present::rasterFrameRowsStrided(
137
+ surface->pixels(), surface->strideBytes() / sizeof(gea::framework::graphics::pixel::native_t), 0,
138
+ state.retainedWidth_, 0, state.retainedHeight_, state.retainedHeight_, *frame);
139
+ surface->markDirty(0, 0, state.retainedWidth_ - 1, state.retainedHeight_ - 1);
140
+ Tree::instance().markNodeDisplayCommandsDirty(nodeId);
141
+ state.retainedDamageX1_ = state.retainedDamageY1_ = -1;
142
+ }
143
+ else
144
+ {
145
+ state.retainedFrame_ = std::move(frame);
146
+ }
147
+ }
148
+
149
+ bool CanvasRenderingContext2D::replayRetainedCanvas(int nodeId, std::uint32_t identity, int x, int y,
150
+ int width, int height)
151
+ {
152
+ auto *destination = gea::platform::display::Display::canvas();
153
+ if (!destination || !destination->pixels())
154
+ return false;
155
+ const auto found = contextStates().find(nodeId);
156
+ if (found == contextStates().end() || found->second->identity_ != identity)
157
+ return false;
158
+ const auto &node = Tree::instance().node(nodeId);
159
+ if (found->second->retainedFrame_ &&
160
+ (!retainedCanvasStyleEligible(node) || node.layout.width != found->second->retainedWidth_ ||
161
+ node.layout.height != found->second->retainedHeight_))
162
+ materializeRetainedCanvas(nodeId);
163
+ if (destination->scrollRegionH() > 0)
164
+ materializeRetainedCanvas(nodeId);
165
+ #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
166
+ if (destination->isLandscapeRotated())
167
+ materializeRetainedCanvas(nodeId);
168
+ #endif
169
+ const auto &state = *found->second;
170
+ if (!state.retainedFrame_)
171
+ {
172
+ const auto *surface = Tree::instance().canvas(nodeId);
173
+ if (!surface || !surface->pixels())
174
+ return false;
175
+ gea::platform::display::Display::blitImage(surface->pixels(), nullptr, surface->width(),
176
+ surface->height(), x, y);
177
+ return true;
178
+ }
179
+ // Stage canvases retain logical screen coordinates even when their pixel
180
+ // pointer is biased into a small DMA strip. Intersect before taking a view.
181
+ int x0, y0, x1, y1;
182
+ gea::platform::display::Display::clip(&x0, &y0, &x1, &y1);
183
+ x0 = std::max({x0, x, 0});
184
+ y0 = std::max({y0, y, 0});
185
+ x1 = std::min({x1, x + width - 1, destination->width() - 1});
186
+ y1 = std::min({y1, y + height - 1, destination->height() - 1});
187
+ if (x0 > x1 || y0 > y1)
188
+ return true;
189
+ const int stride = destination->strideBytes() / sizeof(gea::framework::graphics::pixel::native_t);
190
+ auto *pixels = destination->pixels() + static_cast<std::size_t>(y0) * stride + x0;
191
+ gea::framework::display_present::rasterFrameRowsStrided(pixels, stride, x0 - x, x1 - x0 + 1, y0 - y,
192
+ y1 - y0 + 1, state.retainedHeight_,
193
+ *state.retainedFrame_);
194
+ destination->markDirty(x0, y0, x1, y1);
195
+ return true;
196
+ }
197
+
198
+ namespace
199
+ {
200
+
26
201
  CanvasPerfStats gCanvasPerfStats;
27
202
 
28
203
  // NEVER-RESET totals (the window stats above reset per perf interval).
@@ -178,7 +353,7 @@ float fontScaleFromCss(const std::string &font)
178
353
  return 1.0f;
179
354
  }
180
355
 
181
- } // namespace
356
+ } // namespace
182
357
 
183
358
  void canvasPerfStatsReset()
184
359
  {
@@ -211,21 +386,23 @@ void canvasTotalsRead(int *begin, int *end, int *fillRect, int *drawImage, int *
211
386
 
212
387
  CanvasPresentCommand &CanvasRenderingContext2D::appendPresentCommand(CanvasPresentCommandType type)
213
388
  {
214
- if (presentCommandCount_ < presentCommands_.size()) {
215
- CanvasPresentCommand &command = presentCommands_[presentCommandCount_++];
389
+ if (state_->presentCommandCount_ < state_->presentCommands_.size()) {
390
+ CanvasPresentCommand &command = state_->presentCommands_[state_->presentCommandCount_++];
216
391
  command.type = type;
217
392
  return command;
218
393
  }
219
- presentCommands_.emplace_back();
220
- presentCommandCount_ = presentCommands_.size();
221
- CanvasPresentCommand &command = presentCommands_.back();
394
+ state_->presentCommands_.emplace_back();
395
+ state_->presentCommandCount_ = state_->presentCommands_.size();
396
+ CanvasPresentCommand &command = state_->presentCommands_.back();
222
397
  command.type = type;
223
398
  return command;
224
399
  }
225
400
 
226
401
  void CanvasRenderingContext2D::resetPresentCommands()
227
402
  {
228
- presentCommandCount_ = 0;
403
+ for (std::size_t i = 0; i < state_->presentCommandCount_; ++i)
404
+ state_->presentCommands_[i].imageReference = gea::framework::graphics::ImageReference{};
405
+ state_->presentCommandCount_ = 0;
229
406
  }
230
407
 
231
408
  #if defined(GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT) && GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT
@@ -252,7 +429,7 @@ gea::framework::graphics::Canvas *CanvasRenderingContext2D::drawingCanvas()
252
429
 
253
430
  bool CanvasRenderingContext2D::recordingPresentBatch() const
254
431
  {
255
- return presentRecording_ && batchDepth_ > 0;
432
+ return state_->presentRecording_ && state_->batchDepth_ > 0;
256
433
  }
257
434
 
258
435
  void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixel::native_t color)
@@ -260,7 +437,7 @@ void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixe
260
437
  resetPresentCommands();
261
438
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::Clear);
262
439
  command.clearColor = color;
263
- batchDirty_ = true;
440
+ state_->batchDirty_ = true;
264
441
  }
265
442
 
266
443
  void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -272,8 +449,8 @@ void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h,
272
449
  command.w = w;
273
450
  command.h = h;
274
451
  command.color = color;
275
- command.alpha = globalAlpha_;
276
- batchDirty_ = true;
452
+ command.alpha = state_->globalAlpha_;
453
+ state_->batchDirty_ = true;
277
454
  }
278
455
 
279
456
  void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -285,8 +462,8 @@ void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int
285
462
  command.w = w;
286
463
  command.h = h;
287
464
  command.color = color;
288
- command.alpha = globalAlpha_;
289
- batchDirty_ = true;
465
+ command.alpha = state_->globalAlpha_;
466
+ state_->batchDirty_ = true;
290
467
  }
291
468
 
292
469
  void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
@@ -305,8 +482,8 @@ void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
305
482
  command.x2 = x2;
306
483
  command.y2 = y2;
307
484
  command.color = color;
308
- command.alpha = globalAlpha_;
309
- batchDirty_ = true;
485
+ command.alpha = state_->globalAlpha_;
486
+ state_->batchDirty_ = true;
310
487
  }
311
488
 
312
489
  void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -317,8 +494,8 @@ void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius,
317
494
  command.y = y;
318
495
  command.radius = radius;
319
496
  command.color = color;
320
- command.alpha = globalAlpha_;
321
- batchDirty_ = true;
497
+ command.alpha = state_->globalAlpha_;
498
+ state_->batchDirty_ = true;
322
499
  }
323
500
 
324
501
  void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -329,8 +506,8 @@ void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radiu
329
506
  command.y = y;
330
507
  command.radius = radius;
331
508
  command.color = color;
332
- command.alpha = globalAlpha_;
333
- batchDirty_ = true;
509
+ command.alpha = state_->globalAlpha_;
510
+ state_->batchDirty_ = true;
334
511
  }
335
512
 
336
513
  // Pointer + count forms: no temporary vector, and only the live `count`
@@ -346,11 +523,11 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
346
523
  const std::size_t limit = static_cast<std::size_t>(count);
347
524
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
348
525
  command.radius = radius;
349
- command.alpha = globalAlpha_;
526
+ command.alpha = state_->globalAlpha_;
350
527
  command.xs.assign(xs, xs + limit);
351
528
  command.ys.assign(ys, ys + limit);
352
529
  command.colors.assign(colors, colors + limit);
353
- batchDirty_ = true;
530
+ state_->batchDirty_ = true;
354
531
  }
355
532
 
356
533
  void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
@@ -363,12 +540,12 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
363
540
  const std::size_t limit = static_cast<std::size_t>(count);
364
541
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
365
542
  command.radius = radius;
366
- command.alpha = globalAlpha_;
543
+ command.alpha = state_->globalAlpha_;
367
544
  command.xs.assign(xs, xs + limit);
368
545
  command.ys.assign(ys, ys + limit);
369
546
  command.colors.resize(limit);
370
547
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
371
- batchDirty_ = true;
548
+ state_->batchDirty_ = true;
372
549
  }
373
550
 
374
551
  void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
@@ -384,11 +561,11 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
384
561
  if (limit == 0) return;
385
562
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
386
563
  command.radius = radius;
387
- command.alpha = globalAlpha_;
564
+ command.alpha = state_->globalAlpha_;
388
565
  command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(limit));
389
566
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
390
567
  command.colors.assign(colors.begin(), colors.begin() + static_cast<std::ptrdiff_t>(limit));
391
- batchDirty_ = true;
568
+ state_->batchDirty_ = true;
392
569
  }
393
570
 
394
571
  void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
@@ -409,8 +586,8 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
409
586
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
410
587
  command.colors.resize(limit);
411
588
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
412
- command.alpha = globalAlpha_;
413
- batchDirty_ = true;
589
+ command.alpha = state_->globalAlpha_;
590
+ state_->batchDirty_ = true;
414
591
  }
415
592
 
416
593
  void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std::uint16_t> &xs,
@@ -422,59 +599,51 @@ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std
422
599
  if (capped <= 0 || radius <= 0) return;
423
600
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
424
601
  command.radius = radius;
425
- command.alpha = globalAlpha_;
602
+ command.alpha = state_->globalAlpha_;
426
603
  command.xs.assign(xs.begin(), xs.begin() + capped);
427
604
  command.ys.assign(ys.begin(), ys.begin() + capped);
428
605
  command.colors.assign(static_cast<std::size_t>(capped), canvasRgb565(color));
429
- batchDirty_ = true;
606
+ state_->batchDirty_ = true;
430
607
  }
431
608
 
432
- void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
433
- const std::vector<std::int32_t> &y0s,
434
- const std::vector<std::int32_t> &x1s,
435
- const std::vector<std::int32_t> &y1s,
436
- const std::vector<std::int32_t> &x2s,
437
- const std::vector<std::int32_t> &y2s,
438
- const std::vector<std::uint32_t> &colors,
439
- const std::vector<std::int32_t> &order,
609
+ void CanvasRenderingContext2D::appendPresentTriangles(const CanvasNumericArrayView &x0s,
610
+ const CanvasNumericArrayView &y0s,
611
+ const CanvasNumericArrayView &x1s,
612
+ const CanvasNumericArrayView &y1s,
613
+ const CanvasNumericArrayView &x2s,
614
+ const CanvasNumericArrayView &y2s,
615
+ const CanvasNumericArrayView &colors,
616
+ const CanvasNumericArrayView &order,
440
617
  int count)
441
618
  {
442
- std::size_t limit = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size(), order.size()});
619
+ const std::size_t sourceSize = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size()});
620
+ std::size_t limit = std::min(sourceSize, order.size());
443
621
  if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
444
622
  if (limit == 0) return;
445
623
  using gea::framework::graphics::TriangleEntry;
446
624
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
447
- command.alpha = globalAlpha_;
625
+ command.alpha = state_->globalAlpha_;
448
626
  command.triangles.resize(limit);
449
- const std::int32_t *px0 = x0s.data();
450
- const std::int32_t *py0 = y0s.data();
451
- const std::int32_t *px1 = x1s.data();
452
- const std::int32_t *py1 = y1s.data();
453
- const std::int32_t *px2 = x2s.data();
454
- const std::int32_t *py2 = y2s.data();
455
- const std::uint32_t *pcolors = colors.data();
456
- const std::int32_t *porder = order.data();
457
627
  TriangleEntry *dst = command.triangles.data();
458
- const std::size_t sourceSize = x0s.size();
459
628
  for (std::size_t i = 0; i < limit; i++) {
460
- const std::int32_t rawIndex = porder[i];
629
+ const std::int64_t rawIndex = order.truncated(i);
461
630
  const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
462
631
  ? static_cast<std::size_t>(rawIndex)
463
632
  : 0;
464
633
  TriangleEntry &t = dst[i];
465
- t.x0 = static_cast<std::int16_t>(px0[o]);
466
- t.y0 = static_cast<std::int16_t>(py0[o]);
467
- t.x1 = static_cast<std::int16_t>(px1[o]);
468
- t.y1 = static_cast<std::int16_t>(py1[o]);
469
- t.x2 = static_cast<std::int16_t>(px2[o]);
470
- t.y2 = static_cast<std::int16_t>(py2[o]);
471
- t.color = gea::framework::graphics::pixel::nativeFromRrggbbaa(pcolors[o]);
634
+ t.x0 = static_cast<std::int16_t>(x0s[o]);
635
+ t.y0 = static_cast<std::int16_t>(y0s[o]);
636
+ t.x1 = static_cast<std::int16_t>(x1s[o]);
637
+ t.y1 = static_cast<std::int16_t>(y1s[o]);
638
+ t.x2 = static_cast<std::int16_t>(x2s[o]);
639
+ t.y2 = static_cast<std::int16_t>(y2s[o]);
640
+ t.color = gea::framework::graphics::pixel::nativeFromRrggbbaa(colors.truncated(o));
472
641
  const std::int16_t lo01 = t.y0 < t.y1 ? t.y0 : t.y1;
473
642
  const std::int16_t hi01 = t.y0 > t.y1 ? t.y0 : t.y1;
474
643
  t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
475
644
  t.rowY1 = hi01 > t.y2 ? hi01 : t.y2;
476
645
  }
477
- batchDirty_ = true;
646
+ state_->batchDirty_ = true;
478
647
  }
479
648
 
480
649
  void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::graphics::pixel::native_t *pixels,
@@ -486,14 +655,16 @@ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::grap
486
655
  {
487
656
  if (!pixels || srcWidth <= 0 || srcHeight <= 0) return;
488
657
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImage);
658
+ command.imageReference = gea::framework::graphics::ImageReference(
659
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
489
660
  command.pixels = pixels;
490
661
  command.alphaPixels = alphaPixels;
491
662
  command.srcWidth = srcWidth;
492
663
  command.srcHeight = srcHeight;
493
664
  command.x = x;
494
665
  command.y = y;
495
- command.alpha = globalAlpha_;
496
- batchDirty_ = true;
666
+ command.alpha = state_->globalAlpha_;
667
+ state_->batchDirty_ = true;
497
668
  }
498
669
 
499
670
  void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework::graphics::pixel::native_t *pixels,
@@ -508,6 +679,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
508
679
  {
509
680
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
510
681
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageScaled);
682
+ command.imageReference = gea::framework::graphics::ImageReference(
683
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
511
684
  command.pixels = pixels;
512
685
  command.alphaPixels = alphaPixels;
513
686
  command.srcWidth = srcWidth;
@@ -517,8 +690,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
517
690
  command.w = w;
518
691
  command.h = h;
519
692
  command.radius = radius;
520
- command.alpha = globalAlpha_;
521
- batchDirty_ = true;
693
+ command.alpha = state_->globalAlpha_;
694
+ state_->batchDirty_ = true;
522
695
  }
523
696
  void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::framework::graphics::pixel::native_t *, const std::uint8_t *, int, int, int, int, int, int) {}
524
697
  void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework::graphics::pixel::native_t *, const std::uint8_t *, int, int, int, int, int) {}
@@ -527,12 +700,12 @@ void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphi
527
700
 
528
701
  bool CanvasRenderingContext2D::presentBatch()
529
702
  {
530
- if (presentCommandCount_ == 0) return false;
703
+ if (state_->presentCommandCount_ == 0) return false;
531
704
  gTotalPresentBatchOk++;
532
705
  std::vector<gea::platform::display::DisplayPresentCommand> commands;
533
- commands.reserve(presentCommandCount_);
534
- for (std::size_t commandIndex = 0; commandIndex < presentCommandCount_; ++commandIndex) {
535
- const CanvasPresentCommand &command = presentCommands_[commandIndex];
706
+ commands.reserve(state_->presentCommandCount_);
707
+ for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex) {
708
+ const CanvasPresentCommand &command = state_->presentCommands_[commandIndex];
536
709
  gea::platform::display::DisplayPresentCommand displayCommand{};
537
710
  switch (command.type) {
538
711
  case CanvasPresentCommandType::Clear:
@@ -595,12 +768,12 @@ bool CanvasRenderingContext2D::presentBatch()
595
768
 
596
769
  void CanvasRenderingContext2D::applyDrawState(gea::framework::graphics::Canvas &surface) const
597
770
  {
598
- surface.setGlobalAlpha(globalAlpha_);
771
+ surface.setGlobalAlpha(state_->globalAlpha_);
599
772
  }
600
773
 
601
774
  void CanvasRenderingContext2D::markDirty()
602
775
  {
603
- if (batchDepth_ > 0) batchDirty_ = true;
776
+ if (state_->batchDepth_ > 0) state_->batchDirty_ = true;
604
777
  }
605
778
 
606
779
  void CanvasRenderingContext2D::markDrawDirty()
@@ -610,35 +783,35 @@ void CanvasRenderingContext2D::markDrawDirty()
610
783
 
611
784
  void CanvasRenderingContext2D::setFillStyle(const std::string &value)
612
785
  {
613
- if (fillStyleCached_ && value == fillStyleSource_) return;
614
- fillStyle_ = parseCanvasColor(value);
615
- fillStyleSource_ = value;
616
- fillStyleCached_ = true;
786
+ if (state_->fillStyleCached_ && value == state_->fillStyleSource_) return;
787
+ state_->fillStyle_ = parseCanvasColor(value);
788
+ state_->fillStyleSource_ = value;
789
+ state_->fillStyleCached_ = true;
617
790
  }
618
791
 
619
792
  void CanvasRenderingContext2D::setFillStyleRgb565(gea::framework::graphics::pixel::native_t color)
620
793
  {
621
- fillStyle_ = canvasRgb565(color);
622
- fillStyleCached_ = false;
794
+ state_->fillStyle_ = canvasRgb565(color);
795
+ state_->fillStyleCached_ = false;
623
796
  }
624
797
 
625
798
  void CanvasRenderingContext2D::setStrokeStyle(const std::string &value)
626
799
  {
627
- if (strokeStyleCached_ && value == strokeStyleSource_) return;
628
- strokeStyle_ = parseCanvasColor(value);
629
- strokeStyleSource_ = value;
630
- strokeStyleCached_ = true;
800
+ if (state_->strokeStyleCached_ && value == state_->strokeStyleSource_) return;
801
+ state_->strokeStyle_ = parseCanvasColor(value);
802
+ state_->strokeStyleSource_ = value;
803
+ state_->strokeStyleCached_ = true;
631
804
  }
632
805
 
633
806
  void CanvasRenderingContext2D::setStrokeStyleRgb565(gea::framework::graphics::pixel::native_t color)
634
807
  {
635
- strokeStyle_ = canvasRgb565(color);
636
- strokeStyleCached_ = false;
808
+ state_->strokeStyle_ = canvasRgb565(color);
809
+ state_->strokeStyleCached_ = false;
637
810
  }
638
811
 
639
- void CanvasRenderingContext2D::setGlobalAlpha(double alpha) { globalAlpha_ = alphaFromUnit(alpha); }
640
- void CanvasRenderingContext2D::setLineWidth(double width) { if (std::isfinite(width) && width > 0.0) lineWidth_ = width; }
641
- void CanvasRenderingContext2D::setFont(const std::string &font) { lastFontStr_ = font; fontScale_ = fontScaleFromCss(font); fontSizePx_ = fontPxFromCss(font); }
812
+ void CanvasRenderingContext2D::setGlobalAlpha(double alpha) { state_->globalAlpha_ = alphaFromUnit(alpha); }
813
+ void CanvasRenderingContext2D::setLineWidth(double width) { if (std::isfinite(width) && width > 0.0) state_->lineWidth_ = width; }
814
+ void CanvasRenderingContext2D::setFont(const std::string &font) { state_->lastFontStr_ = font; state_->fontScale_ = fontScaleFromCss(font); state_->fontSizePx_ = fontPxFromCss(font); }
642
815
 
643
816
  void CanvasRenderingContext2D::clear()
644
817
  {
@@ -647,10 +820,10 @@ void CanvasRenderingContext2D::clear()
647
820
 
648
821
  void CanvasRenderingContext2D::clearRect(int x, int y, int w, int h)
649
822
  {
650
- const std::uint8_t previousAlpha = globalAlpha_;
651
- globalAlpha_ = 255;
823
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
824
+ state_->globalAlpha_ = 255;
652
825
  fillRect(x, y, w, h);
653
- globalAlpha_ = previousAlpha;
826
+ state_->globalAlpha_ = previousAlpha;
654
827
  }
655
828
 
656
829
  void CanvasRenderingContext2D::clearRect(double x, double y, double w, double h) { clearRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
@@ -660,10 +833,10 @@ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
660
833
  if (w <= 0 || h <= 0) return;
661
834
  gTotalFillRect++;
662
835
  if (recordingPresentBatch()) {
663
- appendPresentFillRect(x, y, w, h, fillStyle_);
836
+ appendPresentFillRect(x, y, w, h, state_->fillStyle_);
664
837
  return;
665
838
  }
666
- gea::platform::display::Display::fillRect(x, y, w, h, fillStyle_);
839
+ gea::platform::display::Display::fillRect(x, y, w, h, state_->fillStyle_);
667
840
  }
668
841
 
669
842
  void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h) { fillRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
@@ -671,24 +844,24 @@ void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h)
671
844
  void CanvasRenderingContext2D::strokeRect(int x, int y, int w, int h)
672
845
  {
673
846
  if (recordingPresentBatch()) {
674
- appendPresentStrokeRect(x, y, w, h, strokeStyle_);
847
+ appendPresentStrokeRect(x, y, w, h, state_->strokeStyle_);
675
848
  return;
676
849
  }
677
- gea::platform::display::Display::strokeRect(x, y, w, h, strokeStyle_);
850
+ gea::platform::display::Display::strokeRect(x, y, w, h, state_->strokeStyle_);
678
851
  }
679
852
 
680
853
  void CanvasRenderingContext2D::strokeRect(double x, double y, double w, double h) { strokeRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
681
854
 
682
- void CanvasRenderingContext2D::fillCircle(int x, int y, int radius) { fillCircleRgb565(x, y, radius, fillStyle_); }
855
+ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius) { fillCircleRgb565(x, y, radius, state_->fillStyle_); }
683
856
  void CanvasRenderingContext2D::fillCircle(double x, double y, double radius) { fillCircle(rounded(x), rounded(y), rounded(radius)); }
684
857
 
685
858
  void CanvasRenderingContext2D::strokeCircle(int x, int y, int radius)
686
859
  {
687
860
  if (recordingPresentBatch()) {
688
- appendPresentStrokeCircle(x, y, radius, strokeStyle_);
861
+ appendPresentStrokeCircle(x, y, radius, state_->strokeStyle_);
689
862
  return;
690
863
  }
691
- gea::platform::display::Display::strokeCircle(x, y, radius, strokeStyle_);
864
+ gea::platform::display::Display::strokeCircle(x, y, radius, state_->strokeStyle_);
692
865
  }
693
866
 
694
867
  void CanvasRenderingContext2D::strokeCircle(double x, double y, double radius) { strokeCircle(rounded(x), rounded(y), rounded(radius)); }
@@ -721,14 +894,14 @@ void CanvasRenderingContext2D::fillTriangleRgb565(double x0, double y0, double x
721
894
  fillTriangleRgb565(rounded(x0), rounded(y0), rounded(x1), rounded(y1), rounded(x2), rounded(y2), color);
722
895
  }
723
896
 
724
- void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::int32_t> &x0s,
725
- const std::vector<std::int32_t> &y0s,
726
- const std::vector<std::int32_t> &x1s,
727
- const std::vector<std::int32_t> &y1s,
728
- const std::vector<std::int32_t> &x2s,
729
- const std::vector<std::int32_t> &y2s,
730
- const std::vector<std::uint32_t> &colors,
731
- const std::vector<std::int32_t> &order,
897
+ void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const CanvasNumericArrayView &x0s,
898
+ const CanvasNumericArrayView &y0s,
899
+ const CanvasNumericArrayView &x1s,
900
+ const CanvasNumericArrayView &y1s,
901
+ const CanvasNumericArrayView &x2s,
902
+ const CanvasNumericArrayView &y2s,
903
+ const CanvasNumericArrayView &colors,
904
+ const CanvasNumericArrayView &order,
732
905
  int count)
733
906
  {
734
907
  if (recordingPresentBatch()) appendPresentTriangles(x0s, y0s, x1s, y1s, x2s, y2s, colors, order, count);
@@ -742,13 +915,13 @@ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t
742
915
  void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::int32_t> &, const std::vector<std::int32_t> &, int, gea::framework::graphics::pixel::native_t) {}
743
916
  void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t> &xs, const std::vector<std::uint16_t> &ys, int radius, gea::framework::graphics::pixel::native_t color) { if (recordingPresentBatch()) appendPresentCirclesUniform(xs, ys, radius, color); }
744
917
 
745
- void CanvasRenderingContext2D::beginPath() { pathX_.clear(); pathY_.clear(); pathStarts_.clear(); hasArc_ = false; pathClosed_ = false; }
746
- void CanvasRenderingContext2D::arc(double x, double y, double radius, double, double) { arcX_ = x; arcY_ = y; arcRadius_ = radius; hasArc_ = true; }
747
- void CanvasRenderingContext2D::moveTo(double x, double y) { pathStarts_.push_back(static_cast<int>(pathX_.size())); pathX_.push_back(x); pathY_.push_back(y); }
748
- void CanvasRenderingContext2D::lineTo(double x, double y) { pathX_.push_back(x); pathY_.push_back(y); }
749
- void CanvasRenderingContext2D::closePath() { pathClosed_ = true; }
750
- void CanvasRenderingContext2D::fill() { if (hasArc_) fillCircle(arcX_, arcY_, arcRadius_); }
751
- void CanvasRenderingContext2D::stroke() { if (hasArc_) strokeCircle(arcX_, arcY_, arcRadius_); }
918
+ void CanvasRenderingContext2D::beginPath() { state_->pathX_.clear(); state_->pathY_.clear(); state_->pathStarts_.clear(); state_->hasArc_ = false; state_->pathClosed_ = false; }
919
+ void CanvasRenderingContext2D::arc(double x, double y, double radius, double, double) { state_->arcX_ = x; state_->arcY_ = y; state_->arcRadius_ = radius; state_->hasArc_ = true; }
920
+ void CanvasRenderingContext2D::moveTo(double x, double y) { state_->pathStarts_.push_back(static_cast<int>(state_->pathX_.size())); state_->pathX_.push_back(x); state_->pathY_.push_back(y); }
921
+ void CanvasRenderingContext2D::lineTo(double x, double y) { state_->pathX_.push_back(x); state_->pathY_.push_back(y); }
922
+ void CanvasRenderingContext2D::closePath() { state_->pathClosed_ = true; }
923
+ void CanvasRenderingContext2D::fill() { if (state_->hasArc_) fillCircle(state_->arcX_, state_->arcY_, state_->arcRadius_); }
924
+ void CanvasRenderingContext2D::stroke() { if (state_->hasArc_) strokeCircle(state_->arcX_, state_->arcY_, state_->arcRadius_); }
752
925
  void CanvasRenderingContext2D::fillText(const std::string &, int, int) {}
753
926
  void CanvasRenderingContext2D::fillText(const std::string &text, double x, double y) { fillText(text, rounded(x), rounded(y)); }
754
927
  // Images on the direct path record a present command like every other
@@ -771,6 +944,24 @@ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
771
944
  gCanvasPerfStats.drawImageUs += nowUs() - started;
772
945
  }
773
946
  void CanvasRenderingContext2D::drawImage(int imageId, double x, double y) { drawImage(imageId, rounded(x), rounded(y)); }
947
+ void CanvasRenderingContext2D::drawPixelRows(const gea::framework::graphics::pixel::native_t *pixels, int stride, int width, int height, int dx, int dy)
948
+ {
949
+ if (!pixels || width <= 0 || height <= 0 || stride < width) return;
950
+ if (!recordingPresentBatch()) return;
951
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
952
+ state_->globalAlpha_ = 255;
953
+ for (int row = 0; row < height; ++row)
954
+ appendPresentDrawImage(pixels + static_cast<std::size_t>(row) * static_cast<std::size_t>(stride), nullptr, width, 1, dx, dy + row);
955
+ state_->globalAlpha_ = previousAlpha;
956
+ }
957
+ void CanvasRenderingContext2D::putImage(int imageId, int dx, int dy)
958
+ {
959
+ // putImageData replaces the destination pixels: globalAlpha does not apply.
960
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
961
+ state_->globalAlpha_ = 255;
962
+ drawImage(imageId, dx, dy);
963
+ state_->globalAlpha_ = previousAlpha;
964
+ }
774
965
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, int dh)
775
966
  {
776
967
  gTotalDrawImage++;
@@ -794,7 +985,7 @@ double CanvasRenderingContext2D::measureTextInkCenter(const std::string &text)
794
985
  auto *surface = drawingCanvas();
795
986
  if (!surface) surface = canvas();
796
987
  if (!surface) return 0.0;
797
- return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
988
+ return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
798
989
  }
799
990
 
800
991
  double CanvasRenderingContext2D::measureText(const std::string &text)
@@ -802,7 +993,7 @@ double CanvasRenderingContext2D::measureText(const std::string &text)
802
993
  auto *surface = drawingCanvas();
803
994
  if (!surface) surface = canvas();
804
995
  if (!surface) return 0.0;
805
- return static_cast<double>(surface->measureTextFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
996
+ return static_cast<double>(surface->measureTextFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
806
997
  }
807
998
 
808
999
  void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int dw, int dh)
@@ -829,27 +1020,27 @@ void CanvasRenderingContext2D::beginBatch()
829
1020
  {
830
1021
  gTotalBeginBatch++;
831
1022
  gCanvasPerfStats.batchBeginCalls++;
832
- batchDepth_++;
833
- if (batchDepth_ != 1) return;
834
- batchDirty_ = false;
1023
+ state_->batchDepth_++;
1024
+ if (state_->batchDepth_ != 1) return;
1025
+ state_->batchDirty_ = false;
835
1026
  resetPresentCommands();
836
- presentRecording_ = true;
1027
+ state_->presentRecording_ = true;
837
1028
  }
838
1029
 
839
1030
  void CanvasRenderingContext2D::endBatch()
840
1031
  {
841
1032
  gTotalEndBatch++;
842
1033
  gCanvasPerfStats.batchEndCalls++;
843
- if (batchDepth_ <= 0) return;
844
- batchDepth_--;
845
- if (batchDepth_ > 0) return;
846
- if (batchDirty_) {
1034
+ if (state_->batchDepth_ <= 0) return;
1035
+ state_->batchDepth_--;
1036
+ if (state_->batchDepth_ > 0) return;
1037
+ if (state_->batchDirty_) {
847
1038
  gCanvasPerfStats.batchFlushCalls++;
848
1039
  (void)presentBatch();
849
1040
  }
850
1041
  resetPresentCommands();
851
- presentRecording_ = false;
852
- batchDirty_ = false;
1042
+ state_->presentRecording_ = false;
1043
+ state_->batchDirty_ = false;
853
1044
  }
854
1045
 
855
1046
  void CanvasRenderer::record(const Node &) {}
@@ -858,7 +1049,10 @@ void CanvasRenderer::record(const Node &) {}
858
1049
 
859
1050
  CanvasElement CanvasElement::create()
860
1051
  {
861
- return CanvasElement(Tree::instance().createCanvas());
1052
+ const int id = Tree::instance().createCanvas();
1053
+ // A recycled node id must not inherit the previous canvas's styles or batch.
1054
+ CanvasRenderingContext2D::releaseState(id);
1055
+ return CanvasElement(id);
862
1056
  }
863
1057
 
864
1058
  CanvasRenderingContext2D CanvasElement::getContext2D() const
@@ -871,8 +1065,8 @@ gea::framework::graphics::Canvas *CanvasRenderingContext2D::canvas()
871
1065
  const std::int64_t started = nowUs();
872
1066
  gCanvasPerfStats.canvasLookupCalls++;
873
1067
  gea::framework::graphics::Canvas *result = nullptr;
874
- if (batchCanvas_) {
875
- result = batchCanvas_;
1068
+ if (state_->batchCanvas_) {
1069
+ result = state_->batchCanvas_;
876
1070
  } else if (nodeId_ >= 0) {
877
1071
  result = Tree::instance().ensureCanvas(nodeId_);
878
1072
  }
@@ -882,24 +1076,24 @@ gea::framework::graphics::Canvas *CanvasRenderingContext2D::canvas()
882
1076
 
883
1077
  gea::framework::graphics::Canvas *CanvasRenderingContext2D::drawingCanvas()
884
1078
  {
885
- if (presentRecording_) {
1079
+ if (state_->presentRecording_) {
886
1080
  auto *surface = canvas();
887
1081
  if (surface) {
888
1082
  replayPresentBatchToCanvas(*surface);
889
1083
  resetPresentCommands();
890
- presentRecording_ = false;
891
- batchCanvas_ = surface;
892
- batchDirty_ = true;
1084
+ state_->presentRecording_ = false;
1085
+ state_->batchCanvas_ = surface;
1086
+ state_->batchDirty_ = true;
893
1087
  }
894
1088
  return surface;
895
1089
  }
896
- if (batchCanvas_) return batchCanvas_;
1090
+ if (state_->batchCanvas_) return state_->batchCanvas_;
897
1091
  return canvas();
898
1092
  }
899
1093
 
900
1094
  bool CanvasRenderingContext2D::recordingPresentBatch() const
901
1095
  {
902
- return presentRecording_ && batchDepth_ > 0 && nodeId_ >= 0;
1096
+ return state_->presentRecording_ && state_->batchDepth_ > 0 && nodeId_ >= 0;
903
1097
  }
904
1098
 
905
1099
  void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixel::native_t color)
@@ -907,7 +1101,7 @@ void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixe
907
1101
  resetPresentCommands();
908
1102
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::Clear);
909
1103
  command.clearColor = color;
910
- batchDirty_ = true;
1104
+ state_->batchDirty_ = true;
911
1105
  }
912
1106
 
913
1107
  void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -919,8 +1113,8 @@ void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h,
919
1113
  command.w = w;
920
1114
  command.h = h;
921
1115
  command.color = color;
922
- command.alpha = globalAlpha_;
923
- batchDirty_ = true;
1116
+ command.alpha = state_->globalAlpha_;
1117
+ state_->batchDirty_ = true;
924
1118
  }
925
1119
 
926
1120
  void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -932,8 +1126,8 @@ void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int
932
1126
  command.w = w;
933
1127
  command.h = h;
934
1128
  command.color = color;
935
- command.alpha = globalAlpha_;
936
- batchDirty_ = true;
1129
+ command.alpha = state_->globalAlpha_;
1130
+ state_->batchDirty_ = true;
937
1131
  }
938
1132
 
939
1133
  void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
@@ -952,8 +1146,8 @@ void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
952
1146
  command.x2 = x2;
953
1147
  command.y2 = y2;
954
1148
  command.color = color;
955
- command.alpha = globalAlpha_;
956
- batchDirty_ = true;
1149
+ command.alpha = state_->globalAlpha_;
1150
+ state_->batchDirty_ = true;
957
1151
  }
958
1152
 
959
1153
  void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -964,8 +1158,8 @@ void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius,
964
1158
  command.y = y;
965
1159
  command.radius = radius;
966
1160
  command.color = color;
967
- command.alpha = globalAlpha_;
968
- batchDirty_ = true;
1161
+ command.alpha = state_->globalAlpha_;
1162
+ state_->batchDirty_ = true;
969
1163
  }
970
1164
 
971
1165
  void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -976,8 +1170,8 @@ void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radiu
976
1170
  command.y = y;
977
1171
  command.radius = radius;
978
1172
  command.color = color;
979
- command.alpha = globalAlpha_;
980
- batchDirty_ = true;
1173
+ command.alpha = state_->globalAlpha_;
1174
+ state_->batchDirty_ = true;
981
1175
  }
982
1176
 
983
1177
  // Pointer + count forms (standard, non-direct-canvas variant). No temporary
@@ -993,11 +1187,11 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
993
1187
  const std::size_t limit = static_cast<std::size_t>(count);
994
1188
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
995
1189
  command.radius = radius;
996
- command.alpha = globalAlpha_;
1190
+ command.alpha = state_->globalAlpha_;
997
1191
  command.xs.assign(xs, xs + limit);
998
1192
  command.ys.assign(ys, ys + limit);
999
1193
  command.colors.assign(colors, colors + limit);
1000
- batchDirty_ = true;
1194
+ state_->batchDirty_ = true;
1001
1195
  }
1002
1196
 
1003
1197
  void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
@@ -1010,12 +1204,12 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
1010
1204
  const std::size_t limit = static_cast<std::size_t>(count);
1011
1205
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
1012
1206
  command.radius = radius;
1013
- command.alpha = globalAlpha_;
1207
+ command.alpha = state_->globalAlpha_;
1014
1208
  command.xs.assign(xs, xs + limit);
1015
1209
  command.ys.assign(ys, ys + limit);
1016
1210
  command.colors.resize(limit);
1017
1211
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
1018
- batchDirty_ = true;
1212
+ state_->batchDirty_ = true;
1019
1213
  }
1020
1214
 
1021
1215
  void CanvasRenderingContext2D::fillCirclesRgb565(const std::uint16_t *xs,
@@ -1073,8 +1267,8 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
1073
1267
  command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(count_));
1074
1268
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(count_));
1075
1269
  command.colors.assign(colors.begin(), colors.begin() + static_cast<std::ptrdiff_t>(count_));
1076
- command.alpha = globalAlpha_;
1077
- batchDirty_ = true;
1270
+ command.alpha = state_->globalAlpha_;
1271
+ state_->batchDirty_ = true;
1078
1272
  }
1079
1273
 
1080
1274
  void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
@@ -1095,8 +1289,8 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
1095
1289
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
1096
1290
  command.colors.resize(limit);
1097
1291
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
1098
- command.alpha = globalAlpha_;
1099
- batchDirty_ = true;
1292
+ command.alpha = state_->globalAlpha_;
1293
+ state_->batchDirty_ = true;
1100
1294
  }
1101
1295
 
1102
1296
  void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std::uint16_t> &xs,
@@ -1113,56 +1307,48 @@ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std
1113
1307
  command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(count));
1114
1308
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(count));
1115
1309
  command.colors.assign(count, color);
1116
- command.alpha = globalAlpha_;
1117
- batchDirty_ = true;
1118
- }
1119
-
1120
- void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
1121
- const std::vector<std::int32_t> &y0s,
1122
- const std::vector<std::int32_t> &x1s,
1123
- const std::vector<std::int32_t> &y1s,
1124
- const std::vector<std::int32_t> &x2s,
1125
- const std::vector<std::int32_t> &y2s,
1126
- const std::vector<std::uint32_t> &colors,
1127
- const std::vector<std::int32_t> &order,
1310
+ command.alpha = state_->globalAlpha_;
1311
+ state_->batchDirty_ = true;
1312
+ }
1313
+
1314
+ void CanvasRenderingContext2D::appendPresentTriangles(const CanvasNumericArrayView &x0s,
1315
+ const CanvasNumericArrayView &y0s,
1316
+ const CanvasNumericArrayView &x1s,
1317
+ const CanvasNumericArrayView &y1s,
1318
+ const CanvasNumericArrayView &x2s,
1319
+ const CanvasNumericArrayView &y2s,
1320
+ const CanvasNumericArrayView &colors,
1321
+ const CanvasNumericArrayView &order,
1128
1322
  int count)
1129
1323
  {
1130
- std::size_t limit = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size(), order.size()});
1324
+ const std::size_t sourceSize = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size()});
1325
+ std::size_t limit = std::min(sourceSize, order.size());
1131
1326
  if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
1132
1327
  if (limit == 0) return;
1133
1328
  using gea::framework::graphics::TriangleEntry;
1134
1329
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
1135
- command.alpha = globalAlpha_;
1330
+ command.alpha = state_->globalAlpha_;
1136
1331
  command.triangles.resize(limit);
1137
- const std::int32_t *px0 = x0s.data();
1138
- const std::int32_t *py0 = y0s.data();
1139
- const std::int32_t *px1 = x1s.data();
1140
- const std::int32_t *py1 = y1s.data();
1141
- const std::int32_t *px2 = x2s.data();
1142
- const std::int32_t *py2 = y2s.data();
1143
- const std::uint32_t *pcolors = colors.data();
1144
- const std::int32_t *porder = order.data();
1145
1332
  TriangleEntry *dst = command.triangles.data();
1146
- const std::size_t sourceSize = x0s.size();
1147
1333
  for (std::size_t i = 0; i < limit; i++) {
1148
- const std::int32_t rawIndex = porder[i];
1334
+ const std::int64_t rawIndex = order.truncated(i);
1149
1335
  const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
1150
1336
  ? static_cast<std::size_t>(rawIndex)
1151
1337
  : 0;
1152
1338
  TriangleEntry &t = dst[i];
1153
- t.x0 = static_cast<std::int16_t>(px0[o]);
1154
- t.y0 = static_cast<std::int16_t>(py0[o]);
1155
- t.x1 = static_cast<std::int16_t>(px1[o]);
1156
- t.y1 = static_cast<std::int16_t>(py1[o]);
1157
- t.x2 = static_cast<std::int16_t>(px2[o]);
1158
- t.y2 = static_cast<std::int16_t>(py2[o]);
1159
- t.color = gea::framework::graphics::pixel::nativeFromRrggbbaa(pcolors[o]);
1339
+ t.x0 = static_cast<std::int16_t>(x0s[o]);
1340
+ t.y0 = static_cast<std::int16_t>(y0s[o]);
1341
+ t.x1 = static_cast<std::int16_t>(x1s[o]);
1342
+ t.y1 = static_cast<std::int16_t>(y1s[o]);
1343
+ t.x2 = static_cast<std::int16_t>(x2s[o]);
1344
+ t.y2 = static_cast<std::int16_t>(y2s[o]);
1345
+ t.color = gea::framework::graphics::pixel::nativeFromRrggbbaa(colors.truncated(o));
1160
1346
  const std::int16_t lo01 = t.y0 < t.y1 ? t.y0 : t.y1;
1161
1347
  const std::int16_t hi01 = t.y0 > t.y1 ? t.y0 : t.y1;
1162
1348
  t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
1163
1349
  t.rowY1 = hi01 > t.y2 ? hi01 : t.y2;
1164
1350
  }
1165
- batchDirty_ = true;
1351
+ state_->batchDirty_ = true;
1166
1352
  }
1167
1353
 
1168
1354
  void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1174,14 +1360,16 @@ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::grap
1174
1360
  {
1175
1361
  if (!pixels || srcWidth <= 0 || srcHeight <= 0) return;
1176
1362
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImage);
1363
+ command.imageReference = gea::framework::graphics::ImageReference(
1364
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1177
1365
  command.pixels = pixels;
1178
1366
  command.alphaPixels = alphaPixels;
1179
1367
  command.srcWidth = srcWidth;
1180
1368
  command.srcHeight = srcHeight;
1181
1369
  command.x = x;
1182
1370
  command.y = y;
1183
- command.alpha = globalAlpha_;
1184
- batchDirty_ = true;
1371
+ command.alpha = state_->globalAlpha_;
1372
+ state_->batchDirty_ = true;
1185
1373
  }
1186
1374
 
1187
1375
  void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1196,6 +1384,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
1196
1384
  {
1197
1385
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
1198
1386
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageScaled);
1387
+ command.imageReference = gea::framework::graphics::ImageReference(
1388
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1199
1389
  command.pixels = pixels;
1200
1390
  command.alphaPixels = alphaPixels;
1201
1391
  command.srcWidth = srcWidth;
@@ -1205,8 +1395,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
1205
1395
  command.w = w;
1206
1396
  command.h = h;
1207
1397
  command.radius = radius;
1208
- command.alpha = globalAlpha_;
1209
- batchDirty_ = true;
1398
+ command.alpha = state_->globalAlpha_;
1399
+ state_->batchDirty_ = true;
1210
1400
  }
1211
1401
 
1212
1402
  void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1220,6 +1410,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::fram
1220
1410
  {
1221
1411
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
1222
1412
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageRotated90CW);
1413
+ command.imageReference = gea::framework::graphics::ImageReference(
1414
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1223
1415
  command.pixels = pixels;
1224
1416
  command.alphaPixels = alphaPixels;
1225
1417
  command.srcWidth = srcWidth;
@@ -1228,8 +1420,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::fram
1228
1420
  command.y = y;
1229
1421
  command.w = w;
1230
1422
  command.h = h;
1231
- command.alpha = globalAlpha_;
1232
- batchDirty_ = true;
1423
+ command.alpha = state_->globalAlpha_;
1424
+ state_->batchDirty_ = true;
1233
1425
  }
1234
1426
 
1235
1427
  void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1242,6 +1434,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework
1242
1434
  {
1243
1435
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0) return;
1244
1436
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageTiledX);
1437
+ command.imageReference = gea::framework::graphics::ImageReference(
1438
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1245
1439
  command.pixels = pixels;
1246
1440
  command.alphaPixels = alphaPixels;
1247
1441
  command.srcWidth = srcWidth;
@@ -1249,8 +1443,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework
1249
1443
  command.x = x;
1250
1444
  command.y = y;
1251
1445
  command.w = w;
1252
- command.alpha = globalAlpha_;
1253
- batchDirty_ = true;
1446
+ command.alpha = state_->globalAlpha_;
1447
+ state_->batchDirty_ = true;
1254
1448
  }
1255
1449
 
1256
1450
  void CanvasRenderingContext2D::appendPresentFillText(const std::string &text, int x, int y, gea::framework::graphics::pixel::native_t color)
@@ -1261,17 +1455,17 @@ void CanvasRenderingContext2D::appendPresentFillText(const std::string &text, in
1261
1455
  command.x = x;
1262
1456
  command.y = y;
1263
1457
  command.color = color;
1264
- command.scale = fontScale_;
1265
- command.fontFamilyId = fontFamilyId_;
1266
- command.fontSizePx = fontSizePx_;
1267
- command.alpha = globalAlpha_;
1268
- batchDirty_ = true;
1458
+ command.scale = state_->fontScale_;
1459
+ command.fontFamilyId = state_->fontFamilyId_;
1460
+ command.fontSizePx = state_->fontSizePx_;
1461
+ command.alpha = state_->globalAlpha_;
1462
+ state_->batchDirty_ = true;
1269
1463
  }
1270
1464
 
1271
1465
  void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphics::Canvas &surface)
1272
1466
  {
1273
- for (std::size_t commandIndex = 0; commandIndex < presentCommandCount_; ++commandIndex) {
1274
- const CanvasPresentCommand &command = presentCommands_[commandIndex];
1467
+ for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex) {
1468
+ const CanvasPresentCommand &command = state_->presentCommands_[commandIndex];
1275
1469
  surface.setGlobalAlpha(command.alpha);
1276
1470
  switch (command.type) {
1277
1471
  case CanvasPresentCommandType::Clear:
@@ -1340,44 +1534,49 @@ void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphi
1340
1534
  break;
1341
1535
  }
1342
1536
  }
1343
- surface.setGlobalAlpha(globalAlpha_);
1537
+ surface.setGlobalAlpha(state_->globalAlpha_);
1344
1538
  }
1345
1539
 
1346
- bool CanvasRenderingContext2D::presentBatch()
1540
+ std::vector<gea::platform::display::DisplayPresentCommand>
1541
+ CanvasRenderingContext2D::displayPresentCommands() const
1347
1542
  {
1348
- if (presentCommandCount_ == 0) return false;
1349
- gTotalPresentBatchOk++;
1350
1543
 
1351
1544
  std::vector<gea::platform::display::DisplayPresentCommand> commands;
1352
- commands.reserve(presentCommandCount_);
1353
- for (std::size_t commandIndex = 0; commandIndex < presentCommandCount_; ++commandIndex) {
1354
- const CanvasPresentCommand &command = presentCommands_[commandIndex];
1545
+ commands.reserve(state_->presentCommandCount_);
1546
+ for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex)
1547
+ {
1548
+ const CanvasPresentCommand &command = state_->presentCommands_[commandIndex];
1355
1549
  gea::platform::display::DisplayPresentCommand displayCommand{};
1356
- switch (command.type) {
1550
+ switch (command.type)
1551
+ {
1357
1552
  case CanvasPresentCommandType::Clear:
1358
1553
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::Clear;
1359
1554
  displayCommand.clear.color = command.clearColor;
1360
1555
  break;
1361
1556
  case CanvasPresentCommandType::FillRectRgb565:
1362
1557
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillRectRgb565;
1363
- displayCommand.fillRectRgb565 = {command.x, command.y, command.w, command.h, command.color, command.alpha};
1558
+ displayCommand.fillRectRgb565 = {command.x, command.y, command.w,
1559
+ command.h, command.color, command.alpha};
1364
1560
  break;
1365
1561
  case CanvasPresentCommandType::StrokeRectRgb565:
1366
1562
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeRectRgb565;
1367
- displayCommand.strokeRectRgb565 = {command.x, command.y, command.w, command.h, command.color, command.alpha};
1563
+ displayCommand.strokeRectRgb565 = {command.x, command.y, command.w,
1564
+ command.h, command.color, command.alpha};
1368
1565
  break;
1369
1566
  case CanvasPresentCommandType::FillTriangleRgb565:
1370
1567
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillTriangleRgb565;
1371
- displayCommand.fillTriangleRgb565 = {
1372
- command.x0, command.y0, command.x1, command.y1, command.x2, command.y2, command.color, command.alpha};
1568
+ displayCommand.fillTriangleRgb565 = {command.x0, command.y0, command.x1, command.y1,
1569
+ command.x2, command.y2, command.color, command.alpha};
1373
1570
  break;
1374
1571
  case CanvasPresentCommandType::FillCircleRgb565:
1375
1572
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCircleRgb565;
1376
- displayCommand.fillCircleRgb565 = {command.x, command.y, command.radius, command.color, command.alpha};
1573
+ displayCommand.fillCircleRgb565 = {command.x, command.y, command.radius, command.color,
1574
+ command.alpha};
1377
1575
  break;
1378
1576
  case CanvasPresentCommandType::StrokeCircleRgb565:
1379
1577
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeCircleRgb565;
1380
- displayCommand.strokeCircleRgb565 = {command.x, command.y, command.radius, command.color, command.alpha};
1578
+ displayCommand.strokeCircleRgb565 = {command.x, command.y, command.radius, command.color,
1579
+ command.alpha};
1381
1580
  break;
1382
1581
  case CanvasPresentCommandType::FillCirclesRgb565:
1383
1582
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCirclesRgb565;
@@ -1396,48 +1595,60 @@ bool CanvasRenderingContext2D::presentBatch()
1396
1595
  break;
1397
1596
  case CanvasPresentCommandType::DrawImage:
1398
1597
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImage;
1399
- displayCommand.drawImage = {
1400
- command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight, command.x, command.y, command.alpha};
1598
+ displayCommand.drawImage = {command.pixels, command.alphaPixels, command.srcWidth,
1599
+ command.srcHeight, command.x, command.y,
1600
+ command.alpha};
1401
1601
  break;
1402
1602
  case CanvasPresentCommandType::DrawImageScaled:
1403
1603
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageScaled;
1404
1604
  displayCommand.drawImageScaled = {
1405
- command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1406
- command.x, command.y, command.w, command.h, command.alpha, command.radius};
1605
+ command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight, command.x,
1606
+ command.y, command.w, command.h, command.alpha, command.radius};
1407
1607
  break;
1408
1608
  case CanvasPresentCommandType::DrawImageRotated90CW:
1409
1609
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageRotated90CW;
1410
- displayCommand.drawImageRotated90CW = {
1411
- command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1412
- command.x, command.y, command.w, command.h, command.alpha};
1610
+ displayCommand.drawImageRotated90CW = {command.pixels, command.alphaPixels, command.srcWidth,
1611
+ command.srcHeight, command.x, command.y,
1612
+ command.w, command.h, command.alpha};
1413
1613
  break;
1414
1614
  case CanvasPresentCommandType::DrawImageTiledX:
1415
1615
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageTiledX;
1416
- displayCommand.drawImageTiledX = {
1417
- command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight, command.x, command.y, command.w, command.alpha};
1616
+ displayCommand.drawImageTiledX = {command.pixels, command.alphaPixels, command.srcWidth,
1617
+ command.srcHeight, command.x, command.y,
1618
+ command.w, command.alpha};
1418
1619
  break;
1419
1620
  case CanvasPresentCommandType::FillText:
1420
1621
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillText;
1421
- displayCommand.fillText = {command.text.c_str(), command.x, command.y, command.color, command.scale,
1422
- command.fontFamilyId, command.fontSizePx, command.alpha};
1622
+ displayCommand.fillText = {command.text.c_str(), command.x, command.y,
1623
+ command.color, command.scale, command.fontFamilyId,
1624
+ command.fontSizePx, command.alpha};
1423
1625
  break;
1424
1626
  }
1425
1627
  commands.push_back(displayCommand);
1426
1628
  }
1629
+ return commands;
1630
+ }
1631
+
1632
+ bool CanvasRenderingContext2D::presentBatch()
1633
+ {
1634
+ if (state_->presentCommandCount_ == 0)
1635
+ return false;
1636
+ gTotalPresentBatchOk++;
1637
+ auto commands = displayPresentCommands();
1427
1638
  return gea::platform::display::Display::present(commands.data(), static_cast<int>(commands.size()));
1428
1639
  }
1429
1640
 
1430
1641
  void CanvasRenderingContext2D::applyDrawState(gea::framework::graphics::Canvas &surface) const
1431
1642
  {
1432
- surface.setGlobalAlpha(globalAlpha_);
1643
+ surface.setGlobalAlpha(state_->globalAlpha_);
1433
1644
  }
1434
1645
 
1435
1646
  void CanvasRenderingContext2D::markDirty()
1436
1647
  {
1437
1648
  const std::int64_t started = nowUs();
1438
1649
  gCanvasPerfStats.markDirtyCalls++;
1439
- if (batchDepth_ > 0) {
1440
- batchDirty_ = true;
1650
+ if (state_->batchDepth_ > 0) {
1651
+ state_->batchDirty_ = true;
1441
1652
  gCanvasPerfStats.markDirtyUs += nowUs() - started;
1442
1653
  return;
1443
1654
  }
@@ -1447,8 +1658,8 @@ void CanvasRenderingContext2D::markDirty()
1447
1658
 
1448
1659
  void CanvasRenderingContext2D::markDrawDirty()
1449
1660
  {
1450
- if (batchDepth_ > 0) {
1451
- batchDirty_ = true;
1661
+ if (state_->batchDepth_ > 0) {
1662
+ state_->batchDirty_ = true;
1452
1663
  return;
1453
1664
  }
1454
1665
  markDirty();
@@ -1456,59 +1667,59 @@ void CanvasRenderingContext2D::markDrawDirty()
1456
1667
 
1457
1668
  void CanvasRenderingContext2D::setFillStyle(const std::string &value)
1458
1669
  {
1459
- if (fillStyleCached_ && value == fillStyleSource_) return;
1460
- fillStyle_ = parseCanvasColor(value);
1461
- fillStyleSource_ = value;
1462
- fillStyleCached_ = true;
1670
+ if (state_->fillStyleCached_ && value == state_->fillStyleSource_) return;
1671
+ state_->fillStyle_ = parseCanvasColor(value);
1672
+ state_->fillStyleSource_ = value;
1673
+ state_->fillStyleCached_ = true;
1463
1674
  }
1464
1675
 
1465
1676
  void CanvasRenderingContext2D::setFillStyleRgb565(gea::framework::graphics::pixel::native_t color)
1466
1677
  {
1467
- fillStyle_ = canvasRgb565(color);
1468
- fillStyleCached_ = false;
1678
+ state_->fillStyle_ = canvasRgb565(color);
1679
+ state_->fillStyleCached_ = false;
1469
1680
  }
1470
1681
 
1471
1682
  void CanvasRenderingContext2D::setStrokeStyle(const std::string &value)
1472
1683
  {
1473
- if (strokeStyleCached_ && value == strokeStyleSource_) return;
1474
- strokeStyle_ = parseCanvasColor(value);
1475
- strokeStyleSource_ = value;
1476
- strokeStyleCached_ = true;
1684
+ if (state_->strokeStyleCached_ && value == state_->strokeStyleSource_) return;
1685
+ state_->strokeStyle_ = parseCanvasColor(value);
1686
+ state_->strokeStyleSource_ = value;
1687
+ state_->strokeStyleCached_ = true;
1477
1688
  }
1478
1689
 
1479
1690
  void CanvasRenderingContext2D::setStrokeStyleRgb565(gea::framework::graphics::pixel::native_t color)
1480
1691
  {
1481
- strokeStyle_ = canvasRgb565(color);
1482
- strokeStyleCached_ = false;
1692
+ state_->strokeStyle_ = canvasRgb565(color);
1693
+ state_->strokeStyleCached_ = false;
1483
1694
  }
1484
1695
 
1485
1696
  void CanvasRenderingContext2D::setGlobalAlpha(double alpha)
1486
1697
  {
1487
- globalAlpha_ = alphaFromUnit(alpha);
1698
+ state_->globalAlpha_ = alphaFromUnit(alpha);
1488
1699
  }
1489
1700
 
1490
1701
  void CanvasRenderingContext2D::setLineWidth(double width)
1491
1702
  {
1492
1703
  if (!std::isfinite(width) || width <= 0.0) return;
1493
- lineWidth_ = width;
1704
+ state_->lineWidth_ = width;
1494
1705
  }
1495
1706
 
1496
1707
  void CanvasRenderingContext2D::setFont(const std::string &font)
1497
1708
  {
1498
1709
  // Re-setting the same font (common: a per-frame FPS/HUD readout) is a no-op —
1499
1710
  // skip the CSS parse + FontRegistry family/atlas lookups entirely.
1500
- if (font == lastFontStr_) return;
1501
- lastFontStr_ = font;
1502
- fontScale_ = fontScaleFromCss(font);
1711
+ if (font == state_->lastFontStr_) return;
1712
+ state_->lastFontStr_ = font;
1713
+ state_->fontScale_ = fontScaleFromCss(font);
1503
1714
  const int px = fontPxFromCss(font);
1504
- if (px > 0) fontSizePx_ = px;
1715
+ if (px > 0) state_->fontSizePx_ = px;
1505
1716
  const std::string family = fontFamilyFromCss(font);
1506
- fontFamilyId_ = family.empty() ? -1 : gea::framework::graphics::FontRegistry::familyId(family.c_str());
1717
+ state_->fontFamilyId_ = family.empty() ? -1 : gea::framework::graphics::FontRegistry::familyId(family.c_str());
1507
1718
  // Only commit to the baked path when an atlas actually exists near this
1508
1719
  // size; otherwise stay on the scaled bitmap font.
1509
- if (fontFamilyId_ >= 0 &&
1510
- !gea::framework::graphics::FontRegistry::rasterizedFamily(fontFamilyId_, fontSizePx_).valid()) {
1511
- fontFamilyId_ = -1;
1720
+ if (state_->fontFamilyId_ >= 0 &&
1721
+ !gea::framework::graphics::FontRegistry::rasterizedFamily(state_->fontFamilyId_, state_->fontSizePx_).valid()) {
1722
+ state_->fontFamilyId_ = -1;
1512
1723
  }
1513
1724
  }
1514
1725
 
@@ -1526,10 +1737,10 @@ void CanvasRenderingContext2D::clear()
1526
1737
  void CanvasRenderingContext2D::clearRect(int x, int y, int w, int h)
1527
1738
  {
1528
1739
  if (recordingPresentBatch()) {
1529
- const std::uint8_t previousAlpha = globalAlpha_;
1530
- globalAlpha_ = 255;
1740
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
1741
+ state_->globalAlpha_ = 255;
1531
1742
  appendPresentFillRect(x, y, w, h, 0);
1532
- globalAlpha_ = previousAlpha;
1743
+ state_->globalAlpha_ = previousAlpha;
1533
1744
  return;
1534
1745
  }
1535
1746
  auto *surface = drawingCanvas();
@@ -1552,7 +1763,7 @@ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
1552
1763
  if (recordingPresentBatch()) {
1553
1764
  const std::int64_t started = nowUs();
1554
1765
  gCanvasPerfStats.fillRectCalls++;
1555
- appendPresentFillRect(x, y, w, h, fillStyle_);
1766
+ appendPresentFillRect(x, y, w, h, state_->fillStyle_);
1556
1767
  gCanvasPerfStats.fillRectUs += nowUs() - started;
1557
1768
  return;
1558
1769
  }
@@ -1561,7 +1772,7 @@ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
1561
1772
  const std::int64_t started = nowUs();
1562
1773
  gCanvasPerfStats.fillRectCalls++;
1563
1774
  applyDrawState(*surface);
1564
- surface->fillRect(x, y, w, h, fillStyle_);
1775
+ surface->fillRect(x, y, w, h, state_->fillStyle_);
1565
1776
  gCanvasPerfStats.fillRectUs += nowUs() - started;
1566
1777
  markDrawDirty();
1567
1778
  }
@@ -1574,13 +1785,13 @@ void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h)
1574
1785
  void CanvasRenderingContext2D::strokeRect(int x, int y, int w, int h)
1575
1786
  {
1576
1787
  if (recordingPresentBatch()) {
1577
- appendPresentStrokeRect(x, y, w, h, strokeStyle_);
1788
+ appendPresentStrokeRect(x, y, w, h, state_->strokeStyle_);
1578
1789
  return;
1579
1790
  }
1580
1791
  auto *surface = drawingCanvas();
1581
1792
  if (!surface) return;
1582
1793
  applyDrawState(*surface);
1583
- surface->strokeRect(x, y, w, h, strokeStyle_);
1794
+ surface->strokeRect(x, y, w, h, state_->strokeStyle_);
1584
1795
  markDrawDirty();
1585
1796
  }
1586
1797
 
@@ -1594,7 +1805,7 @@ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius)
1594
1805
  if (recordingPresentBatch()) {
1595
1806
  const std::int64_t started = nowUs();
1596
1807
  gCanvasPerfStats.fillCircleCalls++;
1597
- appendPresentFillCircle(x, y, radius, fillStyle_);
1808
+ appendPresentFillCircle(x, y, radius, state_->fillStyle_);
1598
1809
  gCanvasPerfStats.fillCircleUs += nowUs() - started;
1599
1810
  return;
1600
1811
  }
@@ -1603,7 +1814,7 @@ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius)
1603
1814
  const std::int64_t started = nowUs();
1604
1815
  gCanvasPerfStats.fillCircleCalls++;
1605
1816
  applyDrawState(*surface);
1606
- surface->fillCircle(x, y, radius, fillStyle_);
1817
+ surface->fillCircle(x, y, radius, state_->fillStyle_);
1607
1818
  gCanvasPerfStats.fillCircleUs += nowUs() - started;
1608
1819
  markDrawDirty();
1609
1820
  }
@@ -1616,13 +1827,13 @@ void CanvasRenderingContext2D::fillCircle(double x, double y, double radius)
1616
1827
  void CanvasRenderingContext2D::strokeCircle(int x, int y, int radius)
1617
1828
  {
1618
1829
  if (recordingPresentBatch()) {
1619
- appendPresentStrokeCircle(x, y, radius, strokeStyle_);
1830
+ appendPresentStrokeCircle(x, y, radius, state_->strokeStyle_);
1620
1831
  return;
1621
1832
  }
1622
1833
  auto *surface = drawingCanvas();
1623
1834
  if (!surface) return;
1624
1835
  applyDrawState(*surface);
1625
- surface->strokeCircle(x, y, radius, strokeStyle_);
1836
+ surface->strokeCircle(x, y, radius, state_->strokeStyle_);
1626
1837
  markDrawDirty();
1627
1838
  }
1628
1839
 
@@ -1691,17 +1902,18 @@ void CanvasRenderingContext2D::fillTriangleRgb565(double x0,
1691
1902
  fillTriangleRgb565(rounded(x0), rounded(y0), rounded(x1), rounded(y1), rounded(x2), rounded(y2), color);
1692
1903
  }
1693
1904
 
1694
- void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::int32_t> &x0s,
1695
- const std::vector<std::int32_t> &y0s,
1696
- const std::vector<std::int32_t> &x1s,
1697
- const std::vector<std::int32_t> &y1s,
1698
- const std::vector<std::int32_t> &x2s,
1699
- const std::vector<std::int32_t> &y2s,
1700
- const std::vector<std::uint32_t> &colors,
1701
- const std::vector<std::int32_t> &order,
1905
+ void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const CanvasNumericArrayView &x0s,
1906
+ const CanvasNumericArrayView &y0s,
1907
+ const CanvasNumericArrayView &x1s,
1908
+ const CanvasNumericArrayView &y1s,
1909
+ const CanvasNumericArrayView &x2s,
1910
+ const CanvasNumericArrayView &y2s,
1911
+ const CanvasNumericArrayView &colors,
1912
+ const CanvasNumericArrayView &order,
1702
1913
  int count)
1703
1914
  {
1704
- std::size_t limit = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size(), order.size()});
1915
+ const std::size_t sourceSize = std::min({x0s.size(), y0s.size(), x1s.size(), y1s.size(), x2s.size(), y2s.size(), colors.size()});
1916
+ std::size_t limit = std::min(sourceSize, order.size());
1705
1917
  if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
1706
1918
  if (limit == 0) return;
1707
1919
  if (recordingPresentBatch()) {
@@ -1716,14 +1928,13 @@ void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::
1716
1928
  const std::int64_t started = nowUs();
1717
1929
  gCanvasPerfStats.fillTriangleCalls += static_cast<int>(limit);
1718
1930
  applyDrawState(*surface);
1719
- const std::size_t sourceSize = x0s.size();
1720
1931
  for (std::size_t i = 0; i < limit; i++) {
1721
- const std::int32_t rawIndex = order[i];
1932
+ const std::int64_t rawIndex = order.truncated(i);
1722
1933
  const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
1723
1934
  ? static_cast<std::size_t>(rawIndex)
1724
1935
  : 0;
1725
1936
  surface->fillTriangle(x0s[o], y0s[o], x1s[o], y1s[o], x2s[o], y2s[o],
1726
- gea::framework::graphics::pixel::nativeFromRrggbbaa(colors[o]));
1937
+ gea::framework::graphics::pixel::nativeFromRrggbbaa(colors.truncated(o)));
1727
1938
  }
1728
1939
  gCanvasPerfStats.fillTriangleUs += nowUs() - started;
1729
1940
  markDrawDirty();
@@ -1853,47 +2064,48 @@ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t
1853
2064
 
1854
2065
  void CanvasRenderingContext2D::beginPath()
1855
2066
  {
1856
- hasArc_ = false;
1857
- pathX_.clear();
1858
- pathY_.clear();
1859
- pathStarts_.clear();
1860
- pathClosed_ = false;
2067
+ state_->hasArc_ = false;
2068
+ state_->pathX_.clear();
2069
+ state_->pathY_.clear();
2070
+ state_->pathStarts_.clear();
2071
+ state_->pathClosed_ = false;
1861
2072
  }
1862
2073
 
1863
2074
  void CanvasRenderingContext2D::arc(double x, double y, double radius, double, double)
1864
2075
  {
1865
- arcX_ = x;
1866
- arcY_ = y;
1867
- arcRadius_ = radius;
1868
- hasArc_ = true;
2076
+ state_->arcX_ = x;
2077
+ state_->arcY_ = y;
2078
+ state_->arcRadius_ = radius;
2079
+ state_->hasArc_ = true;
1869
2080
  }
1870
2081
 
1871
2082
  void CanvasRenderingContext2D::moveTo(double x, double y)
1872
2083
  {
1873
2084
  // Standard canvas semantics: moveTo starts a NEW subpath (it does not clear
1874
2085
  // the path — beginPath does). This lets one path hold many rings.
1875
- pathStarts_.push_back(static_cast<int>(pathX_.size()));
1876
- pathX_.push_back(x);
1877
- pathY_.push_back(y);
1878
- pathClosed_ = false;
2086
+ state_->pathStarts_.push_back(static_cast<int>(state_->pathX_.size()));
2087
+ state_->pathX_.push_back(x);
2088
+ state_->pathY_.push_back(y);
2089
+ state_->pathClosed_ = false;
1879
2090
  }
1880
2091
 
1881
2092
  void CanvasRenderingContext2D::lineTo(double x, double y)
1882
2093
  {
1883
- if (pathX_.empty()) moveTo(x, y);
2094
+ if (state_->pathX_.empty()) moveTo(x, y);
1884
2095
  else {
1885
- pathX_.push_back(x);
1886
- pathY_.push_back(y);
2096
+ state_->pathX_.push_back(x);
2097
+ state_->pathY_.push_back(y);
1887
2098
  }
1888
2099
  }
1889
2100
 
1890
2101
  void CanvasRenderingContext2D::closePath()
1891
2102
  {
1892
- pathClosed_ = true;
2103
+ state_->pathClosed_ = true;
1893
2104
  }
1894
2105
 
1895
2106
  void CanvasRenderingContext2D::fill()
1896
2107
  {
2108
+ prepareBitmapOnlyDraw();
1897
2109
  // Batch-aware (mirrors fillRect): when recording a present batch the spans
1898
2110
  // are appended as present commands so they actually reach the display; only
1899
2111
  // outside a batch do we draw straight to the surface. (The app composites
@@ -1902,48 +2114,48 @@ void CanvasRenderingContext2D::fill()
1902
2114
  auto *surface = rec ? canvas() : drawingCanvas();
1903
2115
  if (!surface) return;
1904
2116
  if (!rec) applyDrawState(*surface);
1905
- if (hasArc_) {
1906
- const int cx = rounded(arcX_);
1907
- const int cy = rounded(arcY_);
1908
- const int r = rounded(arcRadius_);
1909
- if (rec) appendPresentFillCircle(cx, cy, r, fillStyle_);
2117
+ if (state_->hasArc_) {
2118
+ const int cx = rounded(state_->arcX_);
2119
+ const int cy = rounded(state_->arcY_);
2120
+ const int r = rounded(state_->arcRadius_);
2121
+ if (rec) appendPresentFillCircle(cx, cy, r, state_->fillStyle_);
1910
2122
  else {
1911
- surface->fillCircle(cx, cy, r, fillStyle_);
2123
+ surface->fillCircle(cx, cy, r, state_->fillStyle_);
1912
2124
  markDrawDirty();
1913
2125
  }
1914
2126
  return;
1915
2127
  }
1916
- const std::size_t n = pathX_.size();
1917
- if (n < 3 || pathStarts_.empty()) return;
2128
+ const std::size_t n = state_->pathX_.size();
2129
+ if (n < 3 || state_->pathStarts_.empty()) return;
1918
2130
 
1919
2131
  // Even-odd scanline fill over all subpaths (each implicitly closed). Unlike
1920
2132
  // a triangle fan (convex only), this fills arbitrary concave shapes AND
1921
2133
  // holes — coastlines, lakes-with-islands, building courtyards. Inside spans
1922
2134
  // emit as 1px fillRects.
1923
- double minYf = pathY_[0];
1924
- double maxYf = pathY_[0];
2135
+ double minYf = state_->pathY_[0];
2136
+ double maxYf = state_->pathY_[0];
1925
2137
  for (std::size_t i = 1; i < n; i++) {
1926
- if (pathY_[i] < minYf) minYf = pathY_[i];
1927
- if (pathY_[i] > maxYf) maxYf = pathY_[i];
2138
+ if (state_->pathY_[i] < minYf) minYf = state_->pathY_[i];
2139
+ if (state_->pathY_[i] > maxYf) maxYf = state_->pathY_[i];
1928
2140
  }
1929
2141
  const int yTop = std::max(0, static_cast<int>(std::floor(minYf)));
1930
2142
  const int yBot = std::min(surface->height() - 1, static_cast<int>(std::ceil(maxYf)));
1931
- const std::size_t nStarts = pathStarts_.size();
2143
+ const std::size_t nStarts = state_->pathStarts_.size();
1932
2144
  static std::vector<double> nodes;
1933
2145
  for (int y = yTop; y <= yBot; y++) {
1934
2146
  const double yc = y + 0.5;
1935
2147
  nodes.clear();
1936
2148
  for (std::size_t s = 0; s < nStarts; s++) {
1937
- const std::size_t a = static_cast<std::size_t>(pathStarts_[s]);
1938
- const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(pathStarts_[s + 1]) : n;
2149
+ const std::size_t a = static_cast<std::size_t>(state_->pathStarts_[s]);
2150
+ const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(state_->pathStarts_[s + 1]) : n;
1939
2151
  if (b - a < 2) continue;
1940
2152
  for (std::size_t i = a; i < b; i++) {
1941
2153
  const std::size_t j = (i + 1 < b) ? i + 1 : a; // close the ring
1942
- const double yi = pathY_[i];
1943
- const double yj = pathY_[j];
2154
+ const double yi = state_->pathY_[i];
2155
+ const double yj = state_->pathY_[j];
1944
2156
  if ((yi < yc && yj >= yc) || (yj < yc && yi >= yc)) {
1945
2157
  const double t = (yc - yi) / (yj - yi);
1946
- nodes.push_back(pathX_[i] + t * (pathX_[j] - pathX_[i]));
2158
+ nodes.push_back(state_->pathX_[i] + t * (state_->pathX_[j] - state_->pathX_[i]));
1947
2159
  }
1948
2160
  }
1949
2161
  }
@@ -1953,8 +2165,8 @@ void CanvasRenderingContext2D::fill()
1953
2165
  const int xa = static_cast<int>(std::lround(nodes[k]));
1954
2166
  const int xb = static_cast<int>(std::lround(nodes[k + 1]));
1955
2167
  if (xb > xa) {
1956
- if (rec) appendPresentFillRect(xa, y, xb - xa, 1, fillStyle_);
1957
- else surface->fillRect(xa, y, xb - xa, 1, fillStyle_);
2168
+ if (rec) appendPresentFillRect(xa, y, xb - xa, 1, state_->fillStyle_);
2169
+ else surface->fillRect(xa, y, xb - xa, 1, state_->fillStyle_);
1958
2170
  }
1959
2171
  }
1960
2172
  }
@@ -1963,60 +2175,61 @@ void CanvasRenderingContext2D::fill()
1963
2175
 
1964
2176
  void CanvasRenderingContext2D::stroke()
1965
2177
  {
2178
+ prepareBitmapOnlyDraw();
1966
2179
  const bool rec = recordingPresentBatch();
1967
2180
  auto *surface = rec ? canvas() : drawingCanvas();
1968
2181
  if (!surface) return;
1969
2182
  if (!rec) applyDrawState(*surface);
1970
- if (hasArc_) {
1971
- const int cx = rounded(arcX_);
1972
- const int cy = rounded(arcY_);
1973
- const int r = rounded(arcRadius_);
1974
- if (rec) appendPresentStrokeCircle(cx, cy, r, strokeStyle_);
2183
+ if (state_->hasArc_) {
2184
+ const int cx = rounded(state_->arcX_);
2185
+ const int cy = rounded(state_->arcY_);
2186
+ const int r = rounded(state_->arcRadius_);
2187
+ if (rec) appendPresentStrokeCircle(cx, cy, r, state_->strokeStyle_);
1975
2188
  else {
1976
- surface->strokeCircle(cx, cy, r, strokeStyle_);
2189
+ surface->strokeCircle(cx, cy, r, state_->strokeStyle_);
1977
2190
  markDrawDirty();
1978
2191
  }
1979
2192
  return;
1980
2193
  }
1981
- const std::size_t n = pathX_.size();
1982
- if (n < 2 || pathStarts_.empty()) return;
2194
+ const std::size_t n = state_->pathX_.size();
2195
+ if (n < 2 || state_->pathStarts_.empty()) return;
1983
2196
  // Honor lineWidth: every segment is an oriented quad (two triangles), with a
1984
2197
  // round disc at each joint/cap for widths ≥ ~2px — roads, casings, rivers.
1985
2198
  // Always quad (not Bresenham) so it has a present-command representation.
1986
- const double half = std::max(0.5, lineWidth_ * 0.5);
1987
- const std::size_t nStarts = pathStarts_.size();
2199
+ const double half = std::max(0.5, state_->lineWidth_ * 0.5);
2200
+ const std::size_t nStarts = state_->pathStarts_.size();
1988
2201
  for (std::size_t s = 0; s < nStarts; s++) {
1989
- const std::size_t a = static_cast<std::size_t>(pathStarts_[s]);
1990
- const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(pathStarts_[s + 1]) : n;
2202
+ const std::size_t a = static_cast<std::size_t>(state_->pathStarts_[s]);
2203
+ const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(state_->pathStarts_[s + 1]) : n;
1991
2204
  if (b - a < 2) continue;
1992
- const std::size_t lastSeg = pathClosed_ ? b : b - 1;
2205
+ const std::size_t lastSeg = state_->pathClosed_ ? b : b - 1;
1993
2206
  for (std::size_t i = a; i < lastSeg; i++) {
1994
2207
  const std::size_t j = (i + 1 < b) ? i + 1 : a; // wrap when closed
1995
- const double dx = pathX_[j] - pathX_[i];
1996
- const double dy = pathY_[j] - pathY_[i];
2208
+ const double dx = state_->pathX_[j] - state_->pathX_[i];
2209
+ const double dy = state_->pathY_[j] - state_->pathY_[i];
1997
2210
  const double len = std::sqrt(dx * dx + dy * dy);
1998
2211
  if (len < 1e-6) continue;
1999
2212
  const double px = -dy / len * half; // perpendicular offset
2000
2213
  const double py = dx / len * half;
2001
- const int ax0 = rounded(pathX_[i] + px), ay0 = rounded(pathY_[i] + py);
2002
- const int ax1 = rounded(pathX_[i] - px), ay1 = rounded(pathY_[i] - py);
2003
- const int bx0 = rounded(pathX_[j] + px), by0 = rounded(pathY_[j] + py);
2004
- const int bx1 = rounded(pathX_[j] - px), by1 = rounded(pathY_[j] - py);
2214
+ const int ax0 = rounded(state_->pathX_[i] + px), ay0 = rounded(state_->pathY_[i] + py);
2215
+ const int ax1 = rounded(state_->pathX_[i] - px), ay1 = rounded(state_->pathY_[i] - py);
2216
+ const int bx0 = rounded(state_->pathX_[j] + px), by0 = rounded(state_->pathY_[j] + py);
2217
+ const int bx1 = rounded(state_->pathX_[j] - px), by1 = rounded(state_->pathY_[j] - py);
2005
2218
  if (rec) {
2006
- appendPresentFillTriangle(ax0, ay0, ax1, ay1, bx1, by1, strokeStyle_);
2007
- appendPresentFillTriangle(ax0, ay0, bx1, by1, bx0, by0, strokeStyle_);
2219
+ appendPresentFillTriangle(ax0, ay0, ax1, ay1, bx1, by1, state_->strokeStyle_);
2220
+ appendPresentFillTriangle(ax0, ay0, bx1, by1, bx0, by0, state_->strokeStyle_);
2008
2221
  } else {
2009
- surface->fillTriangle(ax0, ay0, ax1, ay1, bx1, by1, strokeStyle_);
2010
- surface->fillTriangle(ax0, ay0, bx1, by1, bx0, by0, strokeStyle_);
2222
+ surface->fillTriangle(ax0, ay0, ax1, ay1, bx1, by1, state_->strokeStyle_);
2223
+ surface->fillTriangle(ax0, ay0, bx1, by1, bx0, by0, state_->strokeStyle_);
2011
2224
  }
2012
2225
  }
2013
2226
  if (half >= 1.0) {
2014
2227
  const int r = rounded(half);
2015
2228
  for (std::size_t i = a; i < b; i++) {
2016
- const int cx = rounded(pathX_[i]);
2017
- const int cy = rounded(pathY_[i]);
2018
- if (rec) appendPresentFillCircle(cx, cy, r, strokeStyle_);
2019
- else surface->fillCircle(cx, cy, r, strokeStyle_);
2229
+ const int cx = rounded(state_->pathX_[i]);
2230
+ const int cy = rounded(state_->pathY_[i]);
2231
+ if (rec) appendPresentFillCircle(cx, cy, r, state_->strokeStyle_);
2232
+ else surface->fillCircle(cx, cy, r, state_->strokeStyle_);
2020
2233
  }
2021
2234
  }
2022
2235
  }
@@ -2025,10 +2238,11 @@ void CanvasRenderingContext2D::stroke()
2025
2238
 
2026
2239
  void CanvasRenderingContext2D::fillText(const std::string &text, int x, int y)
2027
2240
  {
2241
+ prepareBitmapOnlyDraw();
2028
2242
  if (recordingPresentBatch()) {
2029
2243
  const std::int64_t started = nowUs();
2030
2244
  gCanvasPerfStats.fillTextCalls++;
2031
- appendPresentFillText(text, x, y, fillStyle_);
2245
+ appendPresentFillText(text, x, y, state_->fillStyle_);
2032
2246
  gCanvasPerfStats.fillTextUs += nowUs() - started;
2033
2247
  return;
2034
2248
  }
@@ -2037,19 +2251,21 @@ void CanvasRenderingContext2D::fillText(const std::string &text, int x, int y)
2037
2251
  const std::int64_t started = nowUs();
2038
2252
  gCanvasPerfStats.fillTextCalls++;
2039
2253
  applyDrawState(*surface);
2040
- if (fontFamilyId_ >= 0) surface->drawTextFontFamily(text.c_str(), x, y, fillStyle_, fontFamilyId_, fontSizePx_);
2041
- else surface->drawText(text.c_str(), x, y, fillStyle_, fontScale_);
2254
+ if (state_->fontFamilyId_ >= 0) surface->drawTextFontFamily(text.c_str(), x, y, state_->fillStyle_, state_->fontFamilyId_, state_->fontSizePx_);
2255
+ else surface->drawText(text.c_str(), x, y, state_->fillStyle_, state_->fontScale_);
2042
2256
  gCanvasPerfStats.fillTextUs += nowUs() - started;
2043
2257
  markDrawDirty();
2044
2258
  }
2045
2259
 
2046
2260
  void CanvasRenderingContext2D::fillText(const std::string &text, double x, double y)
2047
2261
  {
2262
+ prepareBitmapOnlyDraw();
2048
2263
  fillText(text, rounded(x), rounded(y));
2049
2264
  }
2050
2265
 
2051
2266
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
2052
2267
  {
2268
+ prepareBitmapOnlyDraw();
2053
2269
  auto &images = gea::framework::graphics::ImageStore::instance();
2054
2270
  const int srcW = images.width(imageId);
2055
2271
  const int srcH = images.height(imageId);
@@ -2072,11 +2288,41 @@ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
2072
2288
 
2073
2289
  void CanvasRenderingContext2D::drawImage(int imageId, double dx, double dy)
2074
2290
  {
2291
+ prepareBitmapOnlyDraw();
2075
2292
  drawImage(imageId, rounded(dx), rounded(dy));
2076
2293
  }
2077
2294
 
2295
+ void CanvasRenderingContext2D::drawPixelRows(const gea::framework::graphics::pixel::native_t *pixels, int stride, int width, int height, int dx, int dy)
2296
+ {
2297
+ prepareBitmapOnlyDraw();
2298
+ if (!pixels || width <= 0 || height <= 0 || stride < width) return;
2299
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
2300
+ state_->globalAlpha_ = 255;
2301
+ if (recordingPresentBatch()) {
2302
+ for (int row = 0; row < height; ++row)
2303
+ appendPresentDrawImage(pixels + static_cast<std::size_t>(row) * static_cast<std::size_t>(stride), nullptr, width, 1, dx, dy + row);
2304
+ } else if (auto *surface = drawingCanvas()) {
2305
+ applyDrawState(*surface);
2306
+ for (int row = 0; row < height; ++row)
2307
+ surface->drawImage(pixels + static_cast<std::size_t>(row) * static_cast<std::size_t>(stride), nullptr, width, 1, dx, dy + row);
2308
+ markDrawDirty();
2309
+ }
2310
+ state_->globalAlpha_ = previousAlpha;
2311
+ }
2312
+
2313
+ void CanvasRenderingContext2D::putImage(int imageId, int dx, int dy)
2314
+ {
2315
+ prepareBitmapOnlyDraw();
2316
+ // putImageData replaces the destination pixels: globalAlpha does not apply.
2317
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
2318
+ state_->globalAlpha_ = 255;
2319
+ drawImage(imageId, dx, dy);
2320
+ state_->globalAlpha_ = previousAlpha;
2321
+ }
2322
+
2078
2323
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, int dh)
2079
2324
  {
2325
+ prepareBitmapOnlyDraw();
2080
2326
  gTotalDrawImage++;
2081
2327
  auto &images = gea::framework::graphics::ImageStore::instance();
2082
2328
  const int srcW = images.width(imageId);
@@ -2100,6 +2346,7 @@ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, in
2100
2346
 
2101
2347
  void CanvasRenderingContext2D::drawImage(int imageId, double dx, double dy, double dw, double dh)
2102
2348
  {
2349
+ prepareBitmapOnlyDraw();
2103
2350
  drawImage(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2104
2351
  }
2105
2352
 
@@ -2108,7 +2355,7 @@ double CanvasRenderingContext2D::measureTextInkCenter(const std::string &text)
2108
2355
  auto *surface = drawingCanvas();
2109
2356
  if (!surface) surface = canvas();
2110
2357
  if (!surface) return 0.0;
2111
- return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
2358
+ return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
2112
2359
  }
2113
2360
 
2114
2361
  double CanvasRenderingContext2D::measureText(const std::string &text)
@@ -2116,11 +2363,12 @@ double CanvasRenderingContext2D::measureText(const std::string &text)
2116
2363
  auto *surface = drawingCanvas();
2117
2364
  if (!surface) surface = canvas();
2118
2365
  if (!surface) return 0.0;
2119
- return static_cast<double>(surface->measureTextFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
2366
+ return static_cast<double>(surface->measureTextFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
2120
2367
  }
2121
2368
 
2122
2369
  void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int dw, int dh)
2123
2370
  {
2371
+ prepareBitmapOnlyDraw();
2124
2372
  gTotalDrawImage++;
2125
2373
  auto &images = gea::framework::graphics::ImageStore::instance();
2126
2374
  const int srcW = images.width(imageId);
@@ -2146,11 +2394,13 @@ void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int
2146
2394
 
2147
2395
  void CanvasRenderingContext2D::drawImageCircle(int imageId, double dx, double dy, double dw, double dh)
2148
2396
  {
2397
+ prepareBitmapOnlyDraw();
2149
2398
  drawImageCircle(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2150
2399
  }
2151
2400
 
2152
2401
  void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, int dx, int dy, int dw, int dh)
2153
2402
  {
2403
+ prepareBitmapOnlyDraw();
2154
2404
  gTotalDrawImage++;
2155
2405
  auto &images = gea::framework::graphics::ImageStore::instance();
2156
2406
  const int srcW = images.width(imageId);
@@ -2174,11 +2424,13 @@ void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, int dx, int dy,
2174
2424
 
2175
2425
  void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, double dx, double dy, double dw, double dh)
2176
2426
  {
2427
+ prepareBitmapOnlyDraw();
2177
2428
  drawImageRotated90CW(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2178
2429
  }
2179
2430
 
2180
2431
  void CanvasRenderingContext2D::drawImageTiledX(int imageId, int dx, int dy, int width)
2181
2432
  {
2433
+ prepareBitmapOnlyDraw();
2182
2434
  auto &images = gea::framework::graphics::ImageStore::instance();
2183
2435
  const int srcW = images.width(imageId);
2184
2436
  const int srcH = images.height(imageId);
@@ -2201,6 +2453,7 @@ void CanvasRenderingContext2D::drawImageTiledX(int imageId, int dx, int dy, int
2201
2453
 
2202
2454
  void CanvasRenderingContext2D::drawImageTiledX(int imageId, double dx, double dy, double width)
2203
2455
  {
2456
+ prepareBitmapOnlyDraw();
2204
2457
  drawImageTiledX(imageId, rounded(dx), rounded(dy), rounded(width));
2205
2458
  }
2206
2459
 
@@ -2214,84 +2467,211 @@ void CanvasRenderingContext2D::flush()
2214
2467
  gea::platform::display::Display::flush();
2215
2468
  }
2216
2469
 
2470
+ void CanvasRenderingContext2D::prepareBitmapOnlyDraw()
2471
+ {
2472
+ if (state_->presentRecording_ && nodeId_ >= 0 && !Tree::instance().isDisplayBackedCanvas(nodeId_))
2473
+ (void)drawingCanvas();
2474
+ }
2475
+
2476
+ bool CanvasRenderingContext2D::retainNumericBatch()
2477
+ {
2478
+ if (nodeId_ < 0 || state_->presentCommandCount_ == 0)
2479
+ return false;
2480
+ const auto &node = Tree::instance().node(nodeId_);
2481
+ if (!retainedCanvasStyleEligible(node) || node.layout.width <= 0 || node.layout.height <= 0)
2482
+ return false;
2483
+ const auto *destination = gea::platform::display::Display::canvas();
2484
+ #if GEA_PIXEL_STORAGE_PACKED
2485
+ return false;
2486
+ #else
2487
+ if (!destination || destination->scrollRegionH() > 0)
2488
+ return false;
2489
+ #if GEA_EMBEDDED_DISPLAY_ROTATE_LANDSCAPE
2490
+ if (destination->isLandscapeRotated())
2491
+ return false;
2492
+ #endif
2493
+ #endif
2494
+ for (std::size_t index = 0; index < state_->presentCommandCount_; ++index)
2495
+ {
2496
+ switch (state_->presentCommands_[index].type)
2497
+ {
2498
+ case CanvasPresentCommandType::Clear:
2499
+ case CanvasPresentCommandType::FillRectRgb565:
2500
+ case CanvasPresentCommandType::StrokeRectRgb565:
2501
+ case CanvasPresentCommandType::FillTriangleRgb565:
2502
+ case CanvasPresentCommandType::FillCircleRgb565:
2503
+ case CanvasPresentCommandType::StrokeCircleRgb565:
2504
+ case CanvasPresentCommandType::FillCirclesRgb565:
2505
+ case CanvasPresentCommandType::FillTrianglesRgb565:
2506
+ break;
2507
+ default:
2508
+ return false;
2509
+ }
2510
+ }
2511
+ auto commands = displayPresentCommands();
2512
+ if (!state_->pendingRetainedFrame_)
2513
+ state_->pendingRetainedFrame_ = std::make_shared<gea::framework::display_present::Frame>();
2514
+ auto frame = state_->pendingRetainedFrame_;
2515
+ if (!gea::framework::display_present::extractFrame(commands.data(), commands.size(), *frame, true) ||
2516
+ !gea::framework::display_present::frameHasOpaqueBase(*frame, node.layout.width, node.layout.height))
2517
+ return false;
2518
+ using namespace gea::framework::display_present;
2519
+ Rect damage;
2520
+ const Frame *previous =
2521
+ state_->retainedWidth_ == node.layout.width && state_->retainedHeight_ == node.layout.height
2522
+ ? state_->retainedFrame_.get()
2523
+ : nullptr;
2524
+ state_->retainedBatchChanged_ =
2525
+ dirtyRects(previous, *frame, &damage, 1, node.layout.width, node.layout.height) != 0;
2526
+ if (state_->retainedBatchChanged_)
2527
+ {
2528
+ // Keep one conservative pixel halo inside the canvas. Accumulate against
2529
+ // earlier batches that have not reached the display yet, even if a later
2530
+ // batch is identical to the most recently committed command frame.
2531
+ damage = clampAndAlign({damage.x0 - 1, damage.y0 - 1, damage.x1 + 1, damage.y1 + 1},
2532
+ node.layout.width, node.layout.height);
2533
+ const Rect pending{state_->retainedDamageX0_, state_->retainedDamageY0_, state_->retainedDamageX1_,
2534
+ state_->retainedDamageY1_};
2535
+ if (gea::framework::display_present::valid(pending))
2536
+ damage = unite(pending, damage);
2537
+ state_->retainedDamageX0_ = damage.x0;
2538
+ state_->retainedDamageY0_ = damage.y0;
2539
+ state_->retainedDamageX1_ = damage.x1;
2540
+ state_->retainedDamageY1_ = damage.y1;
2541
+ }
2542
+ state_->pendingRetainedFrame_ = std::move(state_->retainedFrame_);
2543
+ state_->retainedFrame_ = std::move(frame);
2544
+ state_->retainedWidth_ = node.layout.width;
2545
+ state_->retainedHeight_ = node.layout.height;
2546
+ return true;
2547
+ }
2548
+
2217
2549
  void CanvasRenderingContext2D::beginBatch()
2218
2550
  {
2219
2551
  gTotalBeginBatch++;
2220
2552
  gCanvasPerfStats.batchBeginCalls++;
2221
- batchDepth_++;
2222
- if (batchDepth_ != 1) return;
2223
- batchDirty_ = false;
2553
+ state_->batchDepth_++;
2554
+ if (state_->batchDepth_ != 1)
2555
+ return;
2556
+ state_->batchDirty_ = false;
2224
2557
  resetPresentCommands();
2225
2558
  // Re-evaluate display-backed eligibility every frame without binding the
2226
2559
  // display canvas: command-recorded batches do not need a framebuffer until
2227
2560
  // they fall back to replay. This avoids expensive target-side preservation
2228
2561
  // on double-buffered panels.
2229
- if (nodeId_ >= 0 && Tree::instance().markDisplayBackedCanvas(nodeId_)) {
2230
- presentRecording_ = true;
2231
- batchCanvas_ = nullptr;
2562
+ if (nodeId_ >= 0 && Tree::instance().markDisplayBackedCanvas(nodeId_))
2563
+ {
2564
+ state_->presentRecording_ = true;
2565
+ state_->batchCanvas_ = nullptr;
2232
2566
  return;
2233
2567
  }
2234
- if (nodeId_ >= 0) Tree::instance().ensureCanvas(nodeId_);
2235
- if (nodeId_ >= 0 && Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2236
- presentRecording_ = true;
2237
- batchCanvas_ = nullptr;
2238
- return;
2239
- }
2240
- presentRecording_ = false;
2241
- batchCanvas_ = canvas();
2568
+ // An old display-backed slot must leave that mode after eligibility changes.
2569
+ // Owned/retained slots need no pixels until an operation demands fallback.
2570
+ if (nodeId_ >= 0 && Tree::instance().isDisplayBackedCanvas(nodeId_))
2571
+ Tree::instance().ensureCanvas(nodeId_);
2572
+ state_->presentRecording_ = true;
2573
+ state_->batchCanvas_ = nullptr;
2242
2574
  }
2243
2575
 
2244
2576
  void CanvasRenderingContext2D::endBatch()
2245
2577
  {
2246
2578
  gTotalEndBatch++;
2247
2579
  gCanvasPerfStats.batchEndCalls++;
2248
- if (batchDepth_ <= 0) return;
2249
- batchDepth_--;
2250
- if (batchDepth_ > 0) return;
2251
- batchCanvas_ = nullptr;
2252
- if (batchDirty_ && nodeId_ >= 0) {
2253
- if (Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2580
+ if (state_->batchDepth_ <= 0)
2581
+ return;
2582
+ state_->batchDepth_--;
2583
+ if (state_->batchDepth_ > 0)
2584
+ return;
2585
+ state_->batchCanvas_ = nullptr;
2586
+ if (state_->batchDirty_ && nodeId_ >= 0)
2587
+ {
2588
+ if (Tree::instance().isDisplayBackedCanvas(nodeId_))
2589
+ {
2254
2590
  const std::int64_t started = nowUs();
2255
2591
  gCanvasPerfStats.batchFlushCalls++;
2256
2592
  bool presented = false;
2257
- if (presentRecording_) presented = presentBatch();
2258
- if (presented) {
2593
+ if (state_->presentRecording_)
2594
+ presented = presentBatch();
2595
+ if (presented)
2596
+ {
2259
2597
  Tree::instance().clearNodeDisplayCommandDirty(nodeId_);
2260
2598
  }
2261
- if (!presented) {
2599
+ if (!presented)
2600
+ {
2262
2601
  // Replay+flush paints the OFF-SCREEN composed buffer over the
2263
2602
  // scanout — when this interleaves with a double-buffered present
2264
2603
  // path the screen alternates fresh/stale frames (flicker).
2265
2604
  // Loud while diagnosing that exact symptom.
2266
2605
  static int replayFalls = 0;
2267
2606
  replayFalls++;
2268
- if ((replayFalls & 7) == 1) std::printf("[canvas] presentBatch REJECTED -> replay+flush (#%d)\n", replayFalls);
2269
- presentRecording_ = false;
2607
+ if ((replayFalls & 7) == 1)
2608
+ std::printf("[canvas] presentBatch REJECTED -> replay+flush (#%d)\n", replayFalls);
2609
+ state_->presentRecording_ = false;
2270
2610
  auto *surface = canvas();
2271
- if (surface) replayPresentBatchToCanvas(*surface);
2611
+ if (surface)
2612
+ replayPresentBatchToCanvas(*surface);
2272
2613
  gea::platform::display::Display::flush();
2273
2614
  }
2274
2615
  gCanvasPerfStats.batchFlushUs += nowUs() - started;
2275
- } else {
2276
- Tree::instance().markCanvasDirty(nodeId_);
2616
+ }
2617
+ else
2618
+ {
2619
+ const bool retained = state_->presentRecording_ && retainNumericBatch();
2620
+ if (state_->presentRecording_ && !retained)
2621
+ {
2622
+ auto *surface = canvas();
2623
+ if (surface)
2624
+ replayPresentBatchToCanvas(*surface);
2625
+ }
2626
+ if (!retained || state_->retainedBatchChanged_ ||
2627
+ state_->retainedDamageX0_ <= state_->retainedDamageX1_)
2628
+ Tree::instance().markCanvasDirty(nodeId_);
2277
2629
  }
2278
2630
  }
2279
2631
  resetPresentCommands();
2280
- presentRecording_ = false;
2281
- batchDirty_ = false;
2632
+ state_->presentRecording_ = false;
2633
+ state_->batchDirty_ = false;
2282
2634
  }
2283
2635
 
2284
2636
  void CanvasRenderer::record(const Node &node)
2285
2637
  {
2286
2638
  const Node *nodes = Tree::instance().nodes();
2287
2639
  const int id = static_cast<int>(&node - nodes);
2288
- if (Tree::instance().isDisplayBackedCanvas(id)) return;
2640
+ if (Tree::instance().isDisplayBackedCanvas(id))
2641
+ return;
2642
+
2643
+ if (CanvasRenderingContext2D::hasRetainedCanvas(id))
2644
+ {
2645
+ if (retainedCanvasStyleEligible(node) &&
2646
+ node.layout.width == CanvasRenderingContext2D::stateFor(id)->retainedWidth_ &&
2647
+ node.layout.height == CanvasRenderingContext2D::stateFor(id)->retainedHeight_)
2648
+ {
2649
+ DisplayCommand *cmd = DisplayList::instance().append();
2650
+ if (!cmd)
2651
+ return;
2652
+ cmd->type = DisplayCommandType::ReplayCanvasBatch;
2653
+ cmd->bx = node.layout.x;
2654
+ cmd->by = node.layout.y;
2655
+ cmd->bw = node.layout.width;
2656
+ cmd->bh = node.layout.height;
2657
+ cmd->canvasBatch = {static_cast<int16_t>(id),
2658
+ node.layout.x,
2659
+ node.layout.y,
2660
+ node.layout.width,
2661
+ node.layout.height,
2662
+ CanvasRenderingContext2D::stateFor(id)->identity_};
2663
+ return;
2664
+ }
2665
+ CanvasRenderingContext2D::materializeRetainedCanvas(id);
2666
+ }
2289
2667
 
2290
2668
  const auto *surface = Tree::instance().canvas(id);
2291
- if (!surface || !surface->pixels() || surface->width() <= 0 || surface->height() <= 0) return;
2669
+ if (!surface || !surface->pixels() || surface->width() <= 0 || surface->height() <= 0)
2670
+ return;
2292
2671
 
2293
2672
  DisplayCommand *cmd = DisplayList::instance().append();
2294
- if (!cmd) return;
2673
+ if (!cmd)
2674
+ return;
2295
2675
  cmd->type = DisplayCommandType::BlitImage;
2296
2676
  cmd->bx = node.layout.x;
2297
2677
  cmd->by = node.layout.y;
@@ -2311,4 +2691,4 @@ void CanvasRenderer::record(const Node &node)
2311
2691
 
2312
2692
  #endif
2313
2693
 
2314
- } // namespace gea::embedded::ui
2694
+ } // namespace gea::embedded::ui