@geastack/engine 0.1.15 → 0.1.17

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"
@@ -32,22 +33,170 @@ std::unordered_map<int, std::shared_ptr<CanvasContextState>> &contextStates()
32
33
  return states;
33
34
  }
34
35
 
35
- } // namespace
36
+ } // namespace
36
37
 
37
38
  std::shared_ptr<CanvasContextState> CanvasRenderingContext2D::stateFor(int nodeId)
38
39
  {
39
40
  // -1 is the direct-canvas display context (and an unattached default one).
40
41
  auto &slot = contextStates()[nodeId < 0 ? -1 : nodeId];
41
- if (!slot) slot = std::make_shared<CanvasContextState>();
42
+ if (!slot)
43
+ {
44
+ static std::uint32_t nextIdentity = 0;
45
+ slot = std::make_shared<CanvasContextState>();
46
+ slot->identity_ = ++nextIdentity;
47
+ }
42
48
  return slot;
43
49
  }
44
50
 
45
- void CanvasRenderingContext2D::releaseState(int nodeId)
51
+ void CanvasRenderingContext2D::releaseState(int nodeId) { contextStates().erase(nodeId); }
52
+
53
+ namespace
54
+ {
55
+
56
+ bool retainedCanvasStyleEligible(const Node &node)
46
57
  {
47
- contextStates().erase(nodeId);
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
48
86
  }
49
87
 
50
- namespace {
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
+ {
51
200
 
52
201
  CanvasPerfStats gCanvasPerfStats;
53
202
 
@@ -204,7 +353,7 @@ float fontScaleFromCss(const std::string &font)
204
353
  return 1.0f;
205
354
  }
206
355
 
207
- } // namespace
356
+ } // namespace
208
357
 
209
358
  void canvasPerfStatsReset()
210
359
  {
@@ -251,6 +400,8 @@ CanvasPresentCommand &CanvasRenderingContext2D::appendPresentCommand(CanvasPrese
251
400
 
252
401
  void CanvasRenderingContext2D::resetPresentCommands()
253
402
  {
403
+ for (std::size_t i = 0; i < state_->presentCommandCount_; ++i)
404
+ state_->presentCommands_[i].imageReference = gea::framework::graphics::ImageReference{};
254
405
  state_->presentCommandCount_ = 0;
255
406
  }
256
407
 
@@ -455,46 +606,38 @@ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std
455
606
  state_->batchDirty_ = true;
456
607
  }
457
608
 
458
- void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
459
- const std::vector<std::int32_t> &y0s,
460
- const std::vector<std::int32_t> &x1s,
461
- const std::vector<std::int32_t> &y1s,
462
- const std::vector<std::int32_t> &x2s,
463
- const std::vector<std::int32_t> &y2s,
464
- const std::vector<std::uint32_t> &colors,
465
- 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,
466
617
  int count)
467
618
  {
468
- 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());
469
621
  if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
470
622
  if (limit == 0) return;
471
623
  using gea::framework::graphics::TriangleEntry;
472
624
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
473
625
  command.alpha = state_->globalAlpha_;
474
626
  command.triangles.resize(limit);
475
- const std::int32_t *px0 = x0s.data();
476
- const std::int32_t *py0 = y0s.data();
477
- const std::int32_t *px1 = x1s.data();
478
- const std::int32_t *py1 = y1s.data();
479
- const std::int32_t *px2 = x2s.data();
480
- const std::int32_t *py2 = y2s.data();
481
- const std::uint32_t *pcolors = colors.data();
482
- const std::int32_t *porder = order.data();
483
627
  TriangleEntry *dst = command.triangles.data();
484
- const std::size_t sourceSize = x0s.size();
485
628
  for (std::size_t i = 0; i < limit; i++) {
486
- const std::int32_t rawIndex = porder[i];
629
+ const std::int64_t rawIndex = order.truncated(i);
487
630
  const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
488
631
  ? static_cast<std::size_t>(rawIndex)
489
632
  : 0;
490
633
  TriangleEntry &t = dst[i];
491
- t.x0 = static_cast<std::int16_t>(px0[o]);
492
- t.y0 = static_cast<std::int16_t>(py0[o]);
493
- t.x1 = static_cast<std::int16_t>(px1[o]);
494
- t.y1 = static_cast<std::int16_t>(py1[o]);
495
- t.x2 = static_cast<std::int16_t>(px2[o]);
496
- t.y2 = static_cast<std::int16_t>(py2[o]);
497
- 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));
498
641
  const std::int16_t lo01 = t.y0 < t.y1 ? t.y0 : t.y1;
499
642
  const std::int16_t hi01 = t.y0 > t.y1 ? t.y0 : t.y1;
500
643
  t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
@@ -512,6 +655,8 @@ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::grap
512
655
  {
513
656
  if (!pixels || srcWidth <= 0 || srcHeight <= 0) return;
514
657
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImage);
658
+ command.imageReference = gea::framework::graphics::ImageReference(
659
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
515
660
  command.pixels = pixels;
516
661
  command.alphaPixels = alphaPixels;
517
662
  command.srcWidth = srcWidth;
@@ -534,6 +679,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
534
679
  {
535
680
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
536
681
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageScaled);
682
+ command.imageReference = gea::framework::graphics::ImageReference(
683
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
537
684
  command.pixels = pixels;
538
685
  command.alphaPixels = alphaPixels;
539
686
  command.srcWidth = srcWidth;
@@ -747,14 +894,14 @@ void CanvasRenderingContext2D::fillTriangleRgb565(double x0, double y0, double x
747
894
  fillTriangleRgb565(rounded(x0), rounded(y0), rounded(x1), rounded(y1), rounded(x2), rounded(y2), color);
748
895
  }
749
896
 
750
- void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::int32_t> &x0s,
751
- const std::vector<std::int32_t> &y0s,
752
- const std::vector<std::int32_t> &x1s,
753
- const std::vector<std::int32_t> &y1s,
754
- const std::vector<std::int32_t> &x2s,
755
- const std::vector<std::int32_t> &y2s,
756
- const std::vector<std::uint32_t> &colors,
757
- 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,
758
905
  int count)
759
906
  {
760
907
  if (recordingPresentBatch()) appendPresentTriangles(x0s, y0s, x1s, y1s, x2s, y2s, colors, order, count);
@@ -1164,46 +1311,38 @@ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std
1164
1311
  state_->batchDirty_ = true;
1165
1312
  }
1166
1313
 
1167
- void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
1168
- const std::vector<std::int32_t> &y0s,
1169
- const std::vector<std::int32_t> &x1s,
1170
- const std::vector<std::int32_t> &y1s,
1171
- const std::vector<std::int32_t> &x2s,
1172
- const std::vector<std::int32_t> &y2s,
1173
- const std::vector<std::uint32_t> &colors,
1174
- const std::vector<std::int32_t> &order,
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,
1175
1322
  int count)
1176
1323
  {
1177
- 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());
1178
1326
  if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
1179
1327
  if (limit == 0) return;
1180
1328
  using gea::framework::graphics::TriangleEntry;
1181
1329
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
1182
1330
  command.alpha = state_->globalAlpha_;
1183
1331
  command.triangles.resize(limit);
1184
- const std::int32_t *px0 = x0s.data();
1185
- const std::int32_t *py0 = y0s.data();
1186
- const std::int32_t *px1 = x1s.data();
1187
- const std::int32_t *py1 = y1s.data();
1188
- const std::int32_t *px2 = x2s.data();
1189
- const std::int32_t *py2 = y2s.data();
1190
- const std::uint32_t *pcolors = colors.data();
1191
- const std::int32_t *porder = order.data();
1192
1332
  TriangleEntry *dst = command.triangles.data();
1193
- const std::size_t sourceSize = x0s.size();
1194
1333
  for (std::size_t i = 0; i < limit; i++) {
1195
- const std::int32_t rawIndex = porder[i];
1334
+ const std::int64_t rawIndex = order.truncated(i);
1196
1335
  const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
1197
1336
  ? static_cast<std::size_t>(rawIndex)
1198
1337
  : 0;
1199
1338
  TriangleEntry &t = dst[i];
1200
- t.x0 = static_cast<std::int16_t>(px0[o]);
1201
- t.y0 = static_cast<std::int16_t>(py0[o]);
1202
- t.x1 = static_cast<std::int16_t>(px1[o]);
1203
- t.y1 = static_cast<std::int16_t>(py1[o]);
1204
- t.x2 = static_cast<std::int16_t>(px2[o]);
1205
- t.y2 = static_cast<std::int16_t>(py2[o]);
1206
- 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));
1207
1346
  const std::int16_t lo01 = t.y0 < t.y1 ? t.y0 : t.y1;
1208
1347
  const std::int16_t hi01 = t.y0 > t.y1 ? t.y0 : t.y1;
1209
1348
  t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
@@ -1221,6 +1360,8 @@ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::grap
1221
1360
  {
1222
1361
  if (!pixels || srcWidth <= 0 || srcHeight <= 0) return;
1223
1362
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImage);
1363
+ command.imageReference = gea::framework::graphics::ImageReference(
1364
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1224
1365
  command.pixels = pixels;
1225
1366
  command.alphaPixels = alphaPixels;
1226
1367
  command.srcWidth = srcWidth;
@@ -1243,6 +1384,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
1243
1384
  {
1244
1385
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
1245
1386
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageScaled);
1387
+ command.imageReference = gea::framework::graphics::ImageReference(
1388
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1246
1389
  command.pixels = pixels;
1247
1390
  command.alphaPixels = alphaPixels;
1248
1391
  command.srcWidth = srcWidth;
@@ -1267,6 +1410,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::fram
1267
1410
  {
1268
1411
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0 || h <= 0) return;
1269
1412
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageRotated90CW);
1413
+ command.imageReference = gea::framework::graphics::ImageReference(
1414
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1270
1415
  command.pixels = pixels;
1271
1416
  command.alphaPixels = alphaPixels;
1272
1417
  command.srcWidth = srcWidth;
@@ -1289,6 +1434,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework
1289
1434
  {
1290
1435
  if (!pixels || srcWidth <= 0 || srcHeight <= 0 || w <= 0) return;
1291
1436
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::DrawImageTiledX);
1437
+ command.imageReference = gea::framework::graphics::ImageReference(
1438
+ gea::framework::graphics::ImageStore::instance().idForPixels(pixels));
1292
1439
  command.pixels = pixels;
1293
1440
  command.alphaPixels = alphaPixels;
1294
1441
  command.srcWidth = srcWidth;
@@ -1390,41 +1537,46 @@ void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphi
1390
1537
  surface.setGlobalAlpha(state_->globalAlpha_);
1391
1538
  }
1392
1539
 
1393
- bool CanvasRenderingContext2D::presentBatch()
1540
+ std::vector<gea::platform::display::DisplayPresentCommand>
1541
+ CanvasRenderingContext2D::displayPresentCommands() const
1394
1542
  {
1395
- if (state_->presentCommandCount_ == 0) return false;
1396
- gTotalPresentBatchOk++;
1397
1543
 
1398
1544
  std::vector<gea::platform::display::DisplayPresentCommand> commands;
1399
1545
  commands.reserve(state_->presentCommandCount_);
1400
- for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex) {
1546
+ for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex)
1547
+ {
1401
1548
  const CanvasPresentCommand &command = state_->presentCommands_[commandIndex];
1402
1549
  gea::platform::display::DisplayPresentCommand displayCommand{};
1403
- switch (command.type) {
1550
+ switch (command.type)
1551
+ {
1404
1552
  case CanvasPresentCommandType::Clear:
1405
1553
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::Clear;
1406
1554
  displayCommand.clear.color = command.clearColor;
1407
1555
  break;
1408
1556
  case CanvasPresentCommandType::FillRectRgb565:
1409
1557
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillRectRgb565;
1410
- 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};
1411
1560
  break;
1412
1561
  case CanvasPresentCommandType::StrokeRectRgb565:
1413
1562
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeRectRgb565;
1414
- 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};
1415
1565
  break;
1416
1566
  case CanvasPresentCommandType::FillTriangleRgb565:
1417
1567
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillTriangleRgb565;
1418
- displayCommand.fillTriangleRgb565 = {
1419
- 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};
1420
1570
  break;
1421
1571
  case CanvasPresentCommandType::FillCircleRgb565:
1422
1572
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCircleRgb565;
1423
- 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};
1424
1575
  break;
1425
1576
  case CanvasPresentCommandType::StrokeCircleRgb565:
1426
1577
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::StrokeCircleRgb565;
1427
- 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};
1428
1580
  break;
1429
1581
  case CanvasPresentCommandType::FillCirclesRgb565:
1430
1582
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillCirclesRgb565;
@@ -1443,34 +1595,46 @@ bool CanvasRenderingContext2D::presentBatch()
1443
1595
  break;
1444
1596
  case CanvasPresentCommandType::DrawImage:
1445
1597
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImage;
1446
- displayCommand.drawImage = {
1447
- 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};
1448
1601
  break;
1449
1602
  case CanvasPresentCommandType::DrawImageScaled:
1450
1603
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageScaled;
1451
1604
  displayCommand.drawImageScaled = {
1452
- command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1453
- 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};
1454
1607
  break;
1455
1608
  case CanvasPresentCommandType::DrawImageRotated90CW:
1456
1609
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageRotated90CW;
1457
- displayCommand.drawImageRotated90CW = {
1458
- command.pixels, command.alphaPixels, command.srcWidth, command.srcHeight,
1459
- 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};
1460
1613
  break;
1461
1614
  case CanvasPresentCommandType::DrawImageTiledX:
1462
1615
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::DrawImageTiledX;
1463
- displayCommand.drawImageTiledX = {
1464
- 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};
1465
1619
  break;
1466
1620
  case CanvasPresentCommandType::FillText:
1467
1621
  displayCommand.type = gea::platform::display::DisplayPresentCommandType::FillText;
1468
- displayCommand.fillText = {command.text.c_str(), command.x, command.y, command.color, command.scale,
1469
- 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};
1470
1625
  break;
1471
1626
  }
1472
1627
  commands.push_back(displayCommand);
1473
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();
1474
1638
  return gea::platform::display::Display::present(commands.data(), static_cast<int>(commands.size()));
1475
1639
  }
1476
1640
 
@@ -1738,17 +1902,18 @@ void CanvasRenderingContext2D::fillTriangleRgb565(double x0,
1738
1902
  fillTriangleRgb565(rounded(x0), rounded(y0), rounded(x1), rounded(y1), rounded(x2), rounded(y2), color);
1739
1903
  }
1740
1904
 
1741
- void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::int32_t> &x0s,
1742
- const std::vector<std::int32_t> &y0s,
1743
- const std::vector<std::int32_t> &x1s,
1744
- const std::vector<std::int32_t> &y1s,
1745
- const std::vector<std::int32_t> &x2s,
1746
- const std::vector<std::int32_t> &y2s,
1747
- const std::vector<std::uint32_t> &colors,
1748
- 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,
1749
1913
  int count)
1750
1914
  {
1751
- 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());
1752
1917
  if (count >= 0) limit = std::min(limit, static_cast<std::size_t>(count));
1753
1918
  if (limit == 0) return;
1754
1919
  if (recordingPresentBatch()) {
@@ -1763,14 +1928,13 @@ void CanvasRenderingContext2D::fillTrianglesRgb565Sorted(const std::vector<std::
1763
1928
  const std::int64_t started = nowUs();
1764
1929
  gCanvasPerfStats.fillTriangleCalls += static_cast<int>(limit);
1765
1930
  applyDrawState(*surface);
1766
- const std::size_t sourceSize = x0s.size();
1767
1931
  for (std::size_t i = 0; i < limit; i++) {
1768
- const std::int32_t rawIndex = order[i];
1932
+ const std::int64_t rawIndex = order.truncated(i);
1769
1933
  const std::size_t o = rawIndex >= 0 && static_cast<std::size_t>(rawIndex) < sourceSize
1770
1934
  ? static_cast<std::size_t>(rawIndex)
1771
1935
  : 0;
1772
1936
  surface->fillTriangle(x0s[o], y0s[o], x1s[o], y1s[o], x2s[o], y2s[o],
1773
- gea::framework::graphics::pixel::nativeFromRrggbbaa(colors[o]));
1937
+ gea::framework::graphics::pixel::nativeFromRrggbbaa(colors.truncated(o)));
1774
1938
  }
1775
1939
  gCanvasPerfStats.fillTriangleUs += nowUs() - started;
1776
1940
  markDrawDirty();
@@ -1941,6 +2105,7 @@ void CanvasRenderingContext2D::closePath()
1941
2105
 
1942
2106
  void CanvasRenderingContext2D::fill()
1943
2107
  {
2108
+ prepareBitmapOnlyDraw();
1944
2109
  // Batch-aware (mirrors fillRect): when recording a present batch the spans
1945
2110
  // are appended as present commands so they actually reach the display; only
1946
2111
  // outside a batch do we draw straight to the surface. (The app composites
@@ -2010,6 +2175,7 @@ void CanvasRenderingContext2D::fill()
2010
2175
 
2011
2176
  void CanvasRenderingContext2D::stroke()
2012
2177
  {
2178
+ prepareBitmapOnlyDraw();
2013
2179
  const bool rec = recordingPresentBatch();
2014
2180
  auto *surface = rec ? canvas() : drawingCanvas();
2015
2181
  if (!surface) return;
@@ -2072,6 +2238,7 @@ void CanvasRenderingContext2D::stroke()
2072
2238
 
2073
2239
  void CanvasRenderingContext2D::fillText(const std::string &text, int x, int y)
2074
2240
  {
2241
+ prepareBitmapOnlyDraw();
2075
2242
  if (recordingPresentBatch()) {
2076
2243
  const std::int64_t started = nowUs();
2077
2244
  gCanvasPerfStats.fillTextCalls++;
@@ -2092,11 +2259,13 @@ void CanvasRenderingContext2D::fillText(const std::string &text, int x, int y)
2092
2259
 
2093
2260
  void CanvasRenderingContext2D::fillText(const std::string &text, double x, double y)
2094
2261
  {
2262
+ prepareBitmapOnlyDraw();
2095
2263
  fillText(text, rounded(x), rounded(y));
2096
2264
  }
2097
2265
 
2098
2266
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
2099
2267
  {
2268
+ prepareBitmapOnlyDraw();
2100
2269
  auto &images = gea::framework::graphics::ImageStore::instance();
2101
2270
  const int srcW = images.width(imageId);
2102
2271
  const int srcH = images.height(imageId);
@@ -2119,11 +2288,13 @@ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
2119
2288
 
2120
2289
  void CanvasRenderingContext2D::drawImage(int imageId, double dx, double dy)
2121
2290
  {
2291
+ prepareBitmapOnlyDraw();
2122
2292
  drawImage(imageId, rounded(dx), rounded(dy));
2123
2293
  }
2124
2294
 
2125
2295
  void CanvasRenderingContext2D::drawPixelRows(const gea::framework::graphics::pixel::native_t *pixels, int stride, int width, int height, int dx, int dy)
2126
2296
  {
2297
+ prepareBitmapOnlyDraw();
2127
2298
  if (!pixels || width <= 0 || height <= 0 || stride < width) return;
2128
2299
  const std::uint8_t previousAlpha = state_->globalAlpha_;
2129
2300
  state_->globalAlpha_ = 255;
@@ -2141,6 +2312,7 @@ void CanvasRenderingContext2D::drawPixelRows(const gea::framework::graphics::pix
2141
2312
 
2142
2313
  void CanvasRenderingContext2D::putImage(int imageId, int dx, int dy)
2143
2314
  {
2315
+ prepareBitmapOnlyDraw();
2144
2316
  // putImageData replaces the destination pixels: globalAlpha does not apply.
2145
2317
  const std::uint8_t previousAlpha = state_->globalAlpha_;
2146
2318
  state_->globalAlpha_ = 255;
@@ -2150,6 +2322,7 @@ void CanvasRenderingContext2D::putImage(int imageId, int dx, int dy)
2150
2322
 
2151
2323
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, int dh)
2152
2324
  {
2325
+ prepareBitmapOnlyDraw();
2153
2326
  gTotalDrawImage++;
2154
2327
  auto &images = gea::framework::graphics::ImageStore::instance();
2155
2328
  const int srcW = images.width(imageId);
@@ -2173,6 +2346,7 @@ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, in
2173
2346
 
2174
2347
  void CanvasRenderingContext2D::drawImage(int imageId, double dx, double dy, double dw, double dh)
2175
2348
  {
2349
+ prepareBitmapOnlyDraw();
2176
2350
  drawImage(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2177
2351
  }
2178
2352
 
@@ -2194,6 +2368,7 @@ double CanvasRenderingContext2D::measureText(const std::string &text)
2194
2368
 
2195
2369
  void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int dw, int dh)
2196
2370
  {
2371
+ prepareBitmapOnlyDraw();
2197
2372
  gTotalDrawImage++;
2198
2373
  auto &images = gea::framework::graphics::ImageStore::instance();
2199
2374
  const int srcW = images.width(imageId);
@@ -2219,11 +2394,13 @@ void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int
2219
2394
 
2220
2395
  void CanvasRenderingContext2D::drawImageCircle(int imageId, double dx, double dy, double dw, double dh)
2221
2396
  {
2397
+ prepareBitmapOnlyDraw();
2222
2398
  drawImageCircle(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2223
2399
  }
2224
2400
 
2225
2401
  void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, int dx, int dy, int dw, int dh)
2226
2402
  {
2403
+ prepareBitmapOnlyDraw();
2227
2404
  gTotalDrawImage++;
2228
2405
  auto &images = gea::framework::graphics::ImageStore::instance();
2229
2406
  const int srcW = images.width(imageId);
@@ -2247,11 +2424,13 @@ void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, int dx, int dy,
2247
2424
 
2248
2425
  void CanvasRenderingContext2D::drawImageRotated90CW(int imageId, double dx, double dy, double dw, double dh)
2249
2426
  {
2427
+ prepareBitmapOnlyDraw();
2250
2428
  drawImageRotated90CW(imageId, rounded(dx), rounded(dy), rounded(dw), rounded(dh));
2251
2429
  }
2252
2430
 
2253
2431
  void CanvasRenderingContext2D::drawImageTiledX(int imageId, int dx, int dy, int width)
2254
2432
  {
2433
+ prepareBitmapOnlyDraw();
2255
2434
  auto &images = gea::framework::graphics::ImageStore::instance();
2256
2435
  const int srcW = images.width(imageId);
2257
2436
  const int srcH = images.height(imageId);
@@ -2274,6 +2453,7 @@ void CanvasRenderingContext2D::drawImageTiledX(int imageId, int dx, int dy, int
2274
2453
 
2275
2454
  void CanvasRenderingContext2D::drawImageTiledX(int imageId, double dx, double dy, double width)
2276
2455
  {
2456
+ prepareBitmapOnlyDraw();
2277
2457
  drawImageTiledX(imageId, rounded(dx), rounded(dy), rounded(width));
2278
2458
  }
2279
2459
 
@@ -2287,66 +2467,165 @@ void CanvasRenderingContext2D::flush()
2287
2467
  gea::platform::display::Display::flush();
2288
2468
  }
2289
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
+
2290
2549
  void CanvasRenderingContext2D::beginBatch()
2291
2550
  {
2292
2551
  gTotalBeginBatch++;
2293
2552
  gCanvasPerfStats.batchBeginCalls++;
2294
2553
  state_->batchDepth_++;
2295
- if (state_->batchDepth_ != 1) return;
2554
+ if (state_->batchDepth_ != 1)
2555
+ return;
2296
2556
  state_->batchDirty_ = false;
2297
2557
  resetPresentCommands();
2298
2558
  // Re-evaluate display-backed eligibility every frame without binding the
2299
2559
  // display canvas: command-recorded batches do not need a framebuffer until
2300
2560
  // they fall back to replay. This avoids expensive target-side preservation
2301
2561
  // on double-buffered panels.
2302
- if (nodeId_ >= 0 && Tree::instance().markDisplayBackedCanvas(nodeId_)) {
2303
- state_->presentRecording_ = true;
2304
- state_->batchCanvas_ = nullptr;
2305
- return;
2306
- }
2307
- if (nodeId_ >= 0) Tree::instance().ensureCanvas(nodeId_);
2308
- if (nodeId_ >= 0 && Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2562
+ if (nodeId_ >= 0 && Tree::instance().markDisplayBackedCanvas(nodeId_))
2563
+ {
2309
2564
  state_->presentRecording_ = true;
2310
2565
  state_->batchCanvas_ = nullptr;
2311
2566
  return;
2312
2567
  }
2313
- state_->presentRecording_ = false;
2314
- state_->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;
2315
2574
  }
2316
2575
 
2317
2576
  void CanvasRenderingContext2D::endBatch()
2318
2577
  {
2319
2578
  gTotalEndBatch++;
2320
2579
  gCanvasPerfStats.batchEndCalls++;
2321
- if (state_->batchDepth_ <= 0) return;
2580
+ if (state_->batchDepth_ <= 0)
2581
+ return;
2322
2582
  state_->batchDepth_--;
2323
- if (state_->batchDepth_ > 0) return;
2583
+ if (state_->batchDepth_ > 0)
2584
+ return;
2324
2585
  state_->batchCanvas_ = nullptr;
2325
- if (state_->batchDirty_ && nodeId_ >= 0) {
2326
- if (Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2586
+ if (state_->batchDirty_ && nodeId_ >= 0)
2587
+ {
2588
+ if (Tree::instance().isDisplayBackedCanvas(nodeId_))
2589
+ {
2327
2590
  const std::int64_t started = nowUs();
2328
2591
  gCanvasPerfStats.batchFlushCalls++;
2329
2592
  bool presented = false;
2330
- if (state_->presentRecording_) presented = presentBatch();
2331
- if (presented) {
2593
+ if (state_->presentRecording_)
2594
+ presented = presentBatch();
2595
+ if (presented)
2596
+ {
2332
2597
  Tree::instance().clearNodeDisplayCommandDirty(nodeId_);
2333
2598
  }
2334
- if (!presented) {
2599
+ if (!presented)
2600
+ {
2335
2601
  // Replay+flush paints the OFF-SCREEN composed buffer over the
2336
2602
  // scanout — when this interleaves with a double-buffered present
2337
2603
  // path the screen alternates fresh/stale frames (flicker).
2338
2604
  // Loud while diagnosing that exact symptom.
2339
2605
  static int replayFalls = 0;
2340
2606
  replayFalls++;
2341
- if ((replayFalls & 7) == 1) std::printf("[canvas] presentBatch REJECTED -> replay+flush (#%d)\n", replayFalls);
2607
+ if ((replayFalls & 7) == 1)
2608
+ std::printf("[canvas] presentBatch REJECTED -> replay+flush (#%d)\n", replayFalls);
2342
2609
  state_->presentRecording_ = false;
2343
2610
  auto *surface = canvas();
2344
- if (surface) replayPresentBatchToCanvas(*surface);
2611
+ if (surface)
2612
+ replayPresentBatchToCanvas(*surface);
2345
2613
  gea::platform::display::Display::flush();
2346
2614
  }
2347
2615
  gCanvasPerfStats.batchFlushUs += nowUs() - started;
2348
- } else {
2349
- 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_);
2350
2629
  }
2351
2630
  }
2352
2631
  resetPresentCommands();
@@ -2358,13 +2637,41 @@ void CanvasRenderer::record(const Node &node)
2358
2637
  {
2359
2638
  const Node *nodes = Tree::instance().nodes();
2360
2639
  const int id = static_cast<int>(&node - nodes);
2361
- 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
+ }
2362
2667
 
2363
2668
  const auto *surface = Tree::instance().canvas(id);
2364
- if (!surface || !surface->pixels() || surface->width() <= 0 || surface->height() <= 0) return;
2669
+ if (!surface || !surface->pixels() || surface->width() <= 0 || surface->height() <= 0)
2670
+ return;
2365
2671
 
2366
2672
  DisplayCommand *cmd = DisplayList::instance().append();
2367
- if (!cmd) return;
2673
+ if (!cmd)
2674
+ return;
2368
2675
  cmd->type = DisplayCommandType::BlitImage;
2369
2676
  cmd->bx = node.layout.x;
2370
2677
  cmd->by = node.layout.y;
@@ -2384,4 +2691,4 @@ void CanvasRenderer::record(const Node &node)
2384
2691
 
2385
2692
  #endif
2386
2693
 
2387
- } // namespace gea::embedded::ui
2694
+ } // namespace gea::embedded::ui