@geastack/engine 0.1.13 → 0.1.15

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.
@@ -14,6 +14,7 @@
14
14
  #include <cmath>
15
15
  #include <cstdio>
16
16
  #include <cstdlib>
17
+ #include <unordered_map>
17
18
  #include <utility>
18
19
  #include <vector>
19
20
 
@@ -23,6 +24,31 @@ namespace gea::embedded::ui {
23
24
 
24
25
  namespace {
25
26
 
27
+ // One state per canvas node: every context for a node is a handle onto it.
28
+ // Contexts are made and used on the frame task only.
29
+ std::unordered_map<int, std::shared_ptr<CanvasContextState>> &contextStates()
30
+ {
31
+ static std::unordered_map<int, std::shared_ptr<CanvasContextState>> states;
32
+ return states;
33
+ }
34
+
35
+ } // namespace
36
+
37
+ std::shared_ptr<CanvasContextState> CanvasRenderingContext2D::stateFor(int nodeId)
38
+ {
39
+ // -1 is the direct-canvas display context (and an unattached default one).
40
+ auto &slot = contextStates()[nodeId < 0 ? -1 : nodeId];
41
+ if (!slot) slot = std::make_shared<CanvasContextState>();
42
+ return slot;
43
+ }
44
+
45
+ void CanvasRenderingContext2D::releaseState(int nodeId)
46
+ {
47
+ contextStates().erase(nodeId);
48
+ }
49
+
50
+ namespace {
51
+
26
52
  CanvasPerfStats gCanvasPerfStats;
27
53
 
28
54
  // NEVER-RESET totals (the window stats above reset per perf interval).
@@ -211,21 +237,21 @@ void canvasTotalsRead(int *begin, int *end, int *fillRect, int *drawImage, int *
211
237
 
212
238
  CanvasPresentCommand &CanvasRenderingContext2D::appendPresentCommand(CanvasPresentCommandType type)
213
239
  {
214
- if (presentCommandCount_ < presentCommands_.size()) {
215
- CanvasPresentCommand &command = presentCommands_[presentCommandCount_++];
240
+ if (state_->presentCommandCount_ < state_->presentCommands_.size()) {
241
+ CanvasPresentCommand &command = state_->presentCommands_[state_->presentCommandCount_++];
216
242
  command.type = type;
217
243
  return command;
218
244
  }
219
- presentCommands_.emplace_back();
220
- presentCommandCount_ = presentCommands_.size();
221
- CanvasPresentCommand &command = presentCommands_.back();
245
+ state_->presentCommands_.emplace_back();
246
+ state_->presentCommandCount_ = state_->presentCommands_.size();
247
+ CanvasPresentCommand &command = state_->presentCommands_.back();
222
248
  command.type = type;
223
249
  return command;
224
250
  }
225
251
 
226
252
  void CanvasRenderingContext2D::resetPresentCommands()
227
253
  {
228
- presentCommandCount_ = 0;
254
+ state_->presentCommandCount_ = 0;
229
255
  }
230
256
 
231
257
  #if defined(GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT) && GEA_EMBEDDED_DIRECT_CANVAS_CONTEXT
@@ -252,7 +278,7 @@ gea::framework::graphics::Canvas *CanvasRenderingContext2D::drawingCanvas()
252
278
 
253
279
  bool CanvasRenderingContext2D::recordingPresentBatch() const
254
280
  {
255
- return presentRecording_ && batchDepth_ > 0;
281
+ return state_->presentRecording_ && state_->batchDepth_ > 0;
256
282
  }
257
283
 
258
284
  void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixel::native_t color)
@@ -260,7 +286,7 @@ void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixe
260
286
  resetPresentCommands();
261
287
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::Clear);
262
288
  command.clearColor = color;
263
- batchDirty_ = true;
289
+ state_->batchDirty_ = true;
264
290
  }
265
291
 
266
292
  void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -272,8 +298,8 @@ void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h,
272
298
  command.w = w;
273
299
  command.h = h;
274
300
  command.color = color;
275
- command.alpha = globalAlpha_;
276
- batchDirty_ = true;
301
+ command.alpha = state_->globalAlpha_;
302
+ state_->batchDirty_ = true;
277
303
  }
278
304
 
279
305
  void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -285,8 +311,8 @@ void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int
285
311
  command.w = w;
286
312
  command.h = h;
287
313
  command.color = color;
288
- command.alpha = globalAlpha_;
289
- batchDirty_ = true;
314
+ command.alpha = state_->globalAlpha_;
315
+ state_->batchDirty_ = true;
290
316
  }
291
317
 
292
318
  void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
@@ -305,8 +331,8 @@ void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
305
331
  command.x2 = x2;
306
332
  command.y2 = y2;
307
333
  command.color = color;
308
- command.alpha = globalAlpha_;
309
- batchDirty_ = true;
334
+ command.alpha = state_->globalAlpha_;
335
+ state_->batchDirty_ = true;
310
336
  }
311
337
 
312
338
  void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -317,8 +343,8 @@ void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius,
317
343
  command.y = y;
318
344
  command.radius = radius;
319
345
  command.color = color;
320
- command.alpha = globalAlpha_;
321
- batchDirty_ = true;
346
+ command.alpha = state_->globalAlpha_;
347
+ state_->batchDirty_ = true;
322
348
  }
323
349
 
324
350
  void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -329,8 +355,8 @@ void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radiu
329
355
  command.y = y;
330
356
  command.radius = radius;
331
357
  command.color = color;
332
- command.alpha = globalAlpha_;
333
- batchDirty_ = true;
358
+ command.alpha = state_->globalAlpha_;
359
+ state_->batchDirty_ = true;
334
360
  }
335
361
 
336
362
  // Pointer + count forms: no temporary vector, and only the live `count`
@@ -346,11 +372,11 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
346
372
  const std::size_t limit = static_cast<std::size_t>(count);
347
373
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
348
374
  command.radius = radius;
349
- command.alpha = globalAlpha_;
375
+ command.alpha = state_->globalAlpha_;
350
376
  command.xs.assign(xs, xs + limit);
351
377
  command.ys.assign(ys, ys + limit);
352
378
  command.colors.assign(colors, colors + limit);
353
- batchDirty_ = true;
379
+ state_->batchDirty_ = true;
354
380
  }
355
381
 
356
382
  void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
@@ -363,12 +389,12 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
363
389
  const std::size_t limit = static_cast<std::size_t>(count);
364
390
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
365
391
  command.radius = radius;
366
- command.alpha = globalAlpha_;
392
+ command.alpha = state_->globalAlpha_;
367
393
  command.xs.assign(xs, xs + limit);
368
394
  command.ys.assign(ys, ys + limit);
369
395
  command.colors.resize(limit);
370
396
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
371
- batchDirty_ = true;
397
+ state_->batchDirty_ = true;
372
398
  }
373
399
 
374
400
  void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
@@ -384,11 +410,11 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
384
410
  if (limit == 0) return;
385
411
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
386
412
  command.radius = radius;
387
- command.alpha = globalAlpha_;
413
+ command.alpha = state_->globalAlpha_;
388
414
  command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(limit));
389
415
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
390
416
  command.colors.assign(colors.begin(), colors.begin() + static_cast<std::ptrdiff_t>(limit));
391
- batchDirty_ = true;
417
+ state_->batchDirty_ = true;
392
418
  }
393
419
 
394
420
  void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
@@ -409,8 +435,8 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
409
435
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
410
436
  command.colors.resize(limit);
411
437
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
412
- command.alpha = globalAlpha_;
413
- batchDirty_ = true;
438
+ command.alpha = state_->globalAlpha_;
439
+ state_->batchDirty_ = true;
414
440
  }
415
441
 
416
442
  void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std::uint16_t> &xs,
@@ -422,11 +448,11 @@ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std
422
448
  if (capped <= 0 || radius <= 0) return;
423
449
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
424
450
  command.radius = radius;
425
- command.alpha = globalAlpha_;
451
+ command.alpha = state_->globalAlpha_;
426
452
  command.xs.assign(xs.begin(), xs.begin() + capped);
427
453
  command.ys.assign(ys.begin(), ys.begin() + capped);
428
454
  command.colors.assign(static_cast<std::size_t>(capped), canvasRgb565(color));
429
- batchDirty_ = true;
455
+ state_->batchDirty_ = true;
430
456
  }
431
457
 
432
458
  void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
@@ -444,7 +470,7 @@ void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int
444
470
  if (limit == 0) return;
445
471
  using gea::framework::graphics::TriangleEntry;
446
472
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
447
- command.alpha = globalAlpha_;
473
+ command.alpha = state_->globalAlpha_;
448
474
  command.triangles.resize(limit);
449
475
  const std::int32_t *px0 = x0s.data();
450
476
  const std::int32_t *py0 = y0s.data();
@@ -474,7 +500,7 @@ void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int
474
500
  t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
475
501
  t.rowY1 = hi01 > t.y2 ? hi01 : t.y2;
476
502
  }
477
- batchDirty_ = true;
503
+ state_->batchDirty_ = true;
478
504
  }
479
505
 
480
506
  void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::graphics::pixel::native_t *pixels,
@@ -492,8 +518,8 @@ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::grap
492
518
  command.srcHeight = srcHeight;
493
519
  command.x = x;
494
520
  command.y = y;
495
- command.alpha = globalAlpha_;
496
- batchDirty_ = true;
521
+ command.alpha = state_->globalAlpha_;
522
+ state_->batchDirty_ = true;
497
523
  }
498
524
 
499
525
  void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework::graphics::pixel::native_t *pixels,
@@ -517,8 +543,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
517
543
  command.w = w;
518
544
  command.h = h;
519
545
  command.radius = radius;
520
- command.alpha = globalAlpha_;
521
- batchDirty_ = true;
546
+ command.alpha = state_->globalAlpha_;
547
+ state_->batchDirty_ = true;
522
548
  }
523
549
  void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::framework::graphics::pixel::native_t *, const std::uint8_t *, int, int, int, int, int, int) {}
524
550
  void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework::graphics::pixel::native_t *, const std::uint8_t *, int, int, int, int, int) {}
@@ -527,12 +553,12 @@ void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphi
527
553
 
528
554
  bool CanvasRenderingContext2D::presentBatch()
529
555
  {
530
- if (presentCommandCount_ == 0) return false;
556
+ if (state_->presentCommandCount_ == 0) return false;
531
557
  gTotalPresentBatchOk++;
532
558
  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];
559
+ commands.reserve(state_->presentCommandCount_);
560
+ for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex) {
561
+ const CanvasPresentCommand &command = state_->presentCommands_[commandIndex];
536
562
  gea::platform::display::DisplayPresentCommand displayCommand{};
537
563
  switch (command.type) {
538
564
  case CanvasPresentCommandType::Clear:
@@ -595,12 +621,12 @@ bool CanvasRenderingContext2D::presentBatch()
595
621
 
596
622
  void CanvasRenderingContext2D::applyDrawState(gea::framework::graphics::Canvas &surface) const
597
623
  {
598
- surface.setGlobalAlpha(globalAlpha_);
624
+ surface.setGlobalAlpha(state_->globalAlpha_);
599
625
  }
600
626
 
601
627
  void CanvasRenderingContext2D::markDirty()
602
628
  {
603
- if (batchDepth_ > 0) batchDirty_ = true;
629
+ if (state_->batchDepth_ > 0) state_->batchDirty_ = true;
604
630
  }
605
631
 
606
632
  void CanvasRenderingContext2D::markDrawDirty()
@@ -610,35 +636,35 @@ void CanvasRenderingContext2D::markDrawDirty()
610
636
 
611
637
  void CanvasRenderingContext2D::setFillStyle(const std::string &value)
612
638
  {
613
- if (fillStyleCached_ && value == fillStyleSource_) return;
614
- fillStyle_ = parseCanvasColor(value);
615
- fillStyleSource_ = value;
616
- fillStyleCached_ = true;
639
+ if (state_->fillStyleCached_ && value == state_->fillStyleSource_) return;
640
+ state_->fillStyle_ = parseCanvasColor(value);
641
+ state_->fillStyleSource_ = value;
642
+ state_->fillStyleCached_ = true;
617
643
  }
618
644
 
619
645
  void CanvasRenderingContext2D::setFillStyleRgb565(gea::framework::graphics::pixel::native_t color)
620
646
  {
621
- fillStyle_ = canvasRgb565(color);
622
- fillStyleCached_ = false;
647
+ state_->fillStyle_ = canvasRgb565(color);
648
+ state_->fillStyleCached_ = false;
623
649
  }
624
650
 
625
651
  void CanvasRenderingContext2D::setStrokeStyle(const std::string &value)
626
652
  {
627
- if (strokeStyleCached_ && value == strokeStyleSource_) return;
628
- strokeStyle_ = parseCanvasColor(value);
629
- strokeStyleSource_ = value;
630
- strokeStyleCached_ = true;
653
+ if (state_->strokeStyleCached_ && value == state_->strokeStyleSource_) return;
654
+ state_->strokeStyle_ = parseCanvasColor(value);
655
+ state_->strokeStyleSource_ = value;
656
+ state_->strokeStyleCached_ = true;
631
657
  }
632
658
 
633
659
  void CanvasRenderingContext2D::setStrokeStyleRgb565(gea::framework::graphics::pixel::native_t color)
634
660
  {
635
- strokeStyle_ = canvasRgb565(color);
636
- strokeStyleCached_ = false;
661
+ state_->strokeStyle_ = canvasRgb565(color);
662
+ state_->strokeStyleCached_ = false;
637
663
  }
638
664
 
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); }
665
+ void CanvasRenderingContext2D::setGlobalAlpha(double alpha) { state_->globalAlpha_ = alphaFromUnit(alpha); }
666
+ void CanvasRenderingContext2D::setLineWidth(double width) { if (std::isfinite(width) && width > 0.0) state_->lineWidth_ = width; }
667
+ void CanvasRenderingContext2D::setFont(const std::string &font) { state_->lastFontStr_ = font; state_->fontScale_ = fontScaleFromCss(font); state_->fontSizePx_ = fontPxFromCss(font); }
642
668
 
643
669
  void CanvasRenderingContext2D::clear()
644
670
  {
@@ -647,10 +673,10 @@ void CanvasRenderingContext2D::clear()
647
673
 
648
674
  void CanvasRenderingContext2D::clearRect(int x, int y, int w, int h)
649
675
  {
650
- const std::uint8_t previousAlpha = globalAlpha_;
651
- globalAlpha_ = 255;
676
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
677
+ state_->globalAlpha_ = 255;
652
678
  fillRect(x, y, w, h);
653
- globalAlpha_ = previousAlpha;
679
+ state_->globalAlpha_ = previousAlpha;
654
680
  }
655
681
 
656
682
  void CanvasRenderingContext2D::clearRect(double x, double y, double w, double h) { clearRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
@@ -660,10 +686,10 @@ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
660
686
  if (w <= 0 || h <= 0) return;
661
687
  gTotalFillRect++;
662
688
  if (recordingPresentBatch()) {
663
- appendPresentFillRect(x, y, w, h, fillStyle_);
689
+ appendPresentFillRect(x, y, w, h, state_->fillStyle_);
664
690
  return;
665
691
  }
666
- gea::platform::display::Display::fillRect(x, y, w, h, fillStyle_);
692
+ gea::platform::display::Display::fillRect(x, y, w, h, state_->fillStyle_);
667
693
  }
668
694
 
669
695
  void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h) { fillRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
@@ -671,24 +697,24 @@ void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h)
671
697
  void CanvasRenderingContext2D::strokeRect(int x, int y, int w, int h)
672
698
  {
673
699
  if (recordingPresentBatch()) {
674
- appendPresentStrokeRect(x, y, w, h, strokeStyle_);
700
+ appendPresentStrokeRect(x, y, w, h, state_->strokeStyle_);
675
701
  return;
676
702
  }
677
- gea::platform::display::Display::strokeRect(x, y, w, h, strokeStyle_);
703
+ gea::platform::display::Display::strokeRect(x, y, w, h, state_->strokeStyle_);
678
704
  }
679
705
 
680
706
  void CanvasRenderingContext2D::strokeRect(double x, double y, double w, double h) { strokeRect(rounded(x), rounded(y), rounded(w), rounded(h)); }
681
707
 
682
- void CanvasRenderingContext2D::fillCircle(int x, int y, int radius) { fillCircleRgb565(x, y, radius, fillStyle_); }
708
+ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius) { fillCircleRgb565(x, y, radius, state_->fillStyle_); }
683
709
  void CanvasRenderingContext2D::fillCircle(double x, double y, double radius) { fillCircle(rounded(x), rounded(y), rounded(radius)); }
684
710
 
685
711
  void CanvasRenderingContext2D::strokeCircle(int x, int y, int radius)
686
712
  {
687
713
  if (recordingPresentBatch()) {
688
- appendPresentStrokeCircle(x, y, radius, strokeStyle_);
714
+ appendPresentStrokeCircle(x, y, radius, state_->strokeStyle_);
689
715
  return;
690
716
  }
691
- gea::platform::display::Display::strokeCircle(x, y, radius, strokeStyle_);
717
+ gea::platform::display::Display::strokeCircle(x, y, radius, state_->strokeStyle_);
692
718
  }
693
719
 
694
720
  void CanvasRenderingContext2D::strokeCircle(double x, double y, double radius) { strokeCircle(rounded(x), rounded(y), rounded(radius)); }
@@ -742,13 +768,13 @@ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t
742
768
  void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::int32_t> &, const std::vector<std::int32_t> &, int, gea::framework::graphics::pixel::native_t) {}
743
769
  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
770
 
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_); }
771
+ void CanvasRenderingContext2D::beginPath() { state_->pathX_.clear(); state_->pathY_.clear(); state_->pathStarts_.clear(); state_->hasArc_ = false; state_->pathClosed_ = false; }
772
+ void CanvasRenderingContext2D::arc(double x, double y, double radius, double, double) { state_->arcX_ = x; state_->arcY_ = y; state_->arcRadius_ = radius; state_->hasArc_ = true; }
773
+ 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); }
774
+ void CanvasRenderingContext2D::lineTo(double x, double y) { state_->pathX_.push_back(x); state_->pathY_.push_back(y); }
775
+ void CanvasRenderingContext2D::closePath() { state_->pathClosed_ = true; }
776
+ void CanvasRenderingContext2D::fill() { if (state_->hasArc_) fillCircle(state_->arcX_, state_->arcY_, state_->arcRadius_); }
777
+ void CanvasRenderingContext2D::stroke() { if (state_->hasArc_) strokeCircle(state_->arcX_, state_->arcY_, state_->arcRadius_); }
752
778
  void CanvasRenderingContext2D::fillText(const std::string &, int, int) {}
753
779
  void CanvasRenderingContext2D::fillText(const std::string &text, double x, double y) { fillText(text, rounded(x), rounded(y)); }
754
780
  // Images on the direct path record a present command like every other
@@ -771,6 +797,24 @@ void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy)
771
797
  gCanvasPerfStats.drawImageUs += nowUs() - started;
772
798
  }
773
799
  void CanvasRenderingContext2D::drawImage(int imageId, double x, double y) { drawImage(imageId, rounded(x), rounded(y)); }
800
+ void CanvasRenderingContext2D::drawPixelRows(const gea::framework::graphics::pixel::native_t *pixels, int stride, int width, int height, int dx, int dy)
801
+ {
802
+ if (!pixels || width <= 0 || height <= 0 || stride < width) return;
803
+ if (!recordingPresentBatch()) return;
804
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
805
+ state_->globalAlpha_ = 255;
806
+ for (int row = 0; row < height; ++row)
807
+ appendPresentDrawImage(pixels + static_cast<std::size_t>(row) * static_cast<std::size_t>(stride), nullptr, width, 1, dx, dy + row);
808
+ state_->globalAlpha_ = previousAlpha;
809
+ }
810
+ void CanvasRenderingContext2D::putImage(int imageId, int dx, int dy)
811
+ {
812
+ // putImageData replaces the destination pixels: globalAlpha does not apply.
813
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
814
+ state_->globalAlpha_ = 255;
815
+ drawImage(imageId, dx, dy);
816
+ state_->globalAlpha_ = previousAlpha;
817
+ }
774
818
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, int dh)
775
819
  {
776
820
  gTotalDrawImage++;
@@ -794,7 +838,7 @@ double CanvasRenderingContext2D::measureTextInkCenter(const std::string &text)
794
838
  auto *surface = drawingCanvas();
795
839
  if (!surface) surface = canvas();
796
840
  if (!surface) return 0.0;
797
- return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
841
+ return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
798
842
  }
799
843
 
800
844
  double CanvasRenderingContext2D::measureText(const std::string &text)
@@ -802,7 +846,7 @@ double CanvasRenderingContext2D::measureText(const std::string &text)
802
846
  auto *surface = drawingCanvas();
803
847
  if (!surface) surface = canvas();
804
848
  if (!surface) return 0.0;
805
- return static_cast<double>(surface->measureTextFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
849
+ return static_cast<double>(surface->measureTextFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
806
850
  }
807
851
 
808
852
  void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int dw, int dh)
@@ -829,27 +873,27 @@ void CanvasRenderingContext2D::beginBatch()
829
873
  {
830
874
  gTotalBeginBatch++;
831
875
  gCanvasPerfStats.batchBeginCalls++;
832
- batchDepth_++;
833
- if (batchDepth_ != 1) return;
834
- batchDirty_ = false;
876
+ state_->batchDepth_++;
877
+ if (state_->batchDepth_ != 1) return;
878
+ state_->batchDirty_ = false;
835
879
  resetPresentCommands();
836
- presentRecording_ = true;
880
+ state_->presentRecording_ = true;
837
881
  }
838
882
 
839
883
  void CanvasRenderingContext2D::endBatch()
840
884
  {
841
885
  gTotalEndBatch++;
842
886
  gCanvasPerfStats.batchEndCalls++;
843
- if (batchDepth_ <= 0) return;
844
- batchDepth_--;
845
- if (batchDepth_ > 0) return;
846
- if (batchDirty_) {
887
+ if (state_->batchDepth_ <= 0) return;
888
+ state_->batchDepth_--;
889
+ if (state_->batchDepth_ > 0) return;
890
+ if (state_->batchDirty_) {
847
891
  gCanvasPerfStats.batchFlushCalls++;
848
892
  (void)presentBatch();
849
893
  }
850
894
  resetPresentCommands();
851
- presentRecording_ = false;
852
- batchDirty_ = false;
895
+ state_->presentRecording_ = false;
896
+ state_->batchDirty_ = false;
853
897
  }
854
898
 
855
899
  void CanvasRenderer::record(const Node &) {}
@@ -858,7 +902,10 @@ void CanvasRenderer::record(const Node &) {}
858
902
 
859
903
  CanvasElement CanvasElement::create()
860
904
  {
861
- return CanvasElement(Tree::instance().createCanvas());
905
+ const int id = Tree::instance().createCanvas();
906
+ // A recycled node id must not inherit the previous canvas's styles or batch.
907
+ CanvasRenderingContext2D::releaseState(id);
908
+ return CanvasElement(id);
862
909
  }
863
910
 
864
911
  CanvasRenderingContext2D CanvasElement::getContext2D() const
@@ -871,8 +918,8 @@ gea::framework::graphics::Canvas *CanvasRenderingContext2D::canvas()
871
918
  const std::int64_t started = nowUs();
872
919
  gCanvasPerfStats.canvasLookupCalls++;
873
920
  gea::framework::graphics::Canvas *result = nullptr;
874
- if (batchCanvas_) {
875
- result = batchCanvas_;
921
+ if (state_->batchCanvas_) {
922
+ result = state_->batchCanvas_;
876
923
  } else if (nodeId_ >= 0) {
877
924
  result = Tree::instance().ensureCanvas(nodeId_);
878
925
  }
@@ -882,24 +929,24 @@ gea::framework::graphics::Canvas *CanvasRenderingContext2D::canvas()
882
929
 
883
930
  gea::framework::graphics::Canvas *CanvasRenderingContext2D::drawingCanvas()
884
931
  {
885
- if (presentRecording_) {
932
+ if (state_->presentRecording_) {
886
933
  auto *surface = canvas();
887
934
  if (surface) {
888
935
  replayPresentBatchToCanvas(*surface);
889
936
  resetPresentCommands();
890
- presentRecording_ = false;
891
- batchCanvas_ = surface;
892
- batchDirty_ = true;
937
+ state_->presentRecording_ = false;
938
+ state_->batchCanvas_ = surface;
939
+ state_->batchDirty_ = true;
893
940
  }
894
941
  return surface;
895
942
  }
896
- if (batchCanvas_) return batchCanvas_;
943
+ if (state_->batchCanvas_) return state_->batchCanvas_;
897
944
  return canvas();
898
945
  }
899
946
 
900
947
  bool CanvasRenderingContext2D::recordingPresentBatch() const
901
948
  {
902
- return presentRecording_ && batchDepth_ > 0 && nodeId_ >= 0;
949
+ return state_->presentRecording_ && state_->batchDepth_ > 0 && nodeId_ >= 0;
903
950
  }
904
951
 
905
952
  void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixel::native_t color)
@@ -907,7 +954,7 @@ void CanvasRenderingContext2D::appendPresentClear(gea::framework::graphics::pixe
907
954
  resetPresentCommands();
908
955
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::Clear);
909
956
  command.clearColor = color;
910
- batchDirty_ = true;
957
+ state_->batchDirty_ = true;
911
958
  }
912
959
 
913
960
  void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -919,8 +966,8 @@ void CanvasRenderingContext2D::appendPresentFillRect(int x, int y, int w, int h,
919
966
  command.w = w;
920
967
  command.h = h;
921
968
  command.color = color;
922
- command.alpha = globalAlpha_;
923
- batchDirty_ = true;
969
+ command.alpha = state_->globalAlpha_;
970
+ state_->batchDirty_ = true;
924
971
  }
925
972
 
926
973
  void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int h, gea::framework::graphics::pixel::native_t color)
@@ -932,8 +979,8 @@ void CanvasRenderingContext2D::appendPresentStrokeRect(int x, int y, int w, int
932
979
  command.w = w;
933
980
  command.h = h;
934
981
  command.color = color;
935
- command.alpha = globalAlpha_;
936
- batchDirty_ = true;
982
+ command.alpha = state_->globalAlpha_;
983
+ state_->batchDirty_ = true;
937
984
  }
938
985
 
939
986
  void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
@@ -952,8 +999,8 @@ void CanvasRenderingContext2D::appendPresentFillTriangle(int x0,
952
999
  command.x2 = x2;
953
1000
  command.y2 = y2;
954
1001
  command.color = color;
955
- command.alpha = globalAlpha_;
956
- batchDirty_ = true;
1002
+ command.alpha = state_->globalAlpha_;
1003
+ state_->batchDirty_ = true;
957
1004
  }
958
1005
 
959
1006
  void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -964,8 +1011,8 @@ void CanvasRenderingContext2D::appendPresentFillCircle(int x, int y, int radius,
964
1011
  command.y = y;
965
1012
  command.radius = radius;
966
1013
  command.color = color;
967
- command.alpha = globalAlpha_;
968
- batchDirty_ = true;
1014
+ command.alpha = state_->globalAlpha_;
1015
+ state_->batchDirty_ = true;
969
1016
  }
970
1017
 
971
1018
  void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radius, gea::framework::graphics::pixel::native_t color)
@@ -976,8 +1023,8 @@ void CanvasRenderingContext2D::appendPresentStrokeCircle(int x, int y, int radiu
976
1023
  command.y = y;
977
1024
  command.radius = radius;
978
1025
  command.color = color;
979
- command.alpha = globalAlpha_;
980
- batchDirty_ = true;
1026
+ command.alpha = state_->globalAlpha_;
1027
+ state_->batchDirty_ = true;
981
1028
  }
982
1029
 
983
1030
  // Pointer + count forms (standard, non-direct-canvas variant). No temporary
@@ -993,11 +1040,11 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
993
1040
  const std::size_t limit = static_cast<std::size_t>(count);
994
1041
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
995
1042
  command.radius = radius;
996
- command.alpha = globalAlpha_;
1043
+ command.alpha = state_->globalAlpha_;
997
1044
  command.xs.assign(xs, xs + limit);
998
1045
  command.ys.assign(ys, ys + limit);
999
1046
  command.colors.assign(colors, colors + limit);
1000
- batchDirty_ = true;
1047
+ state_->batchDirty_ = true;
1001
1048
  }
1002
1049
 
1003
1050
  void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
@@ -1010,12 +1057,12 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::uint16_t *xs,
1010
1057
  const std::size_t limit = static_cast<std::size_t>(count);
1011
1058
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillCirclesRgb565);
1012
1059
  command.radius = radius;
1013
- command.alpha = globalAlpha_;
1060
+ command.alpha = state_->globalAlpha_;
1014
1061
  command.xs.assign(xs, xs + limit);
1015
1062
  command.ys.assign(ys, ys + limit);
1016
1063
  command.colors.resize(limit);
1017
1064
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
1018
- batchDirty_ = true;
1065
+ state_->batchDirty_ = true;
1019
1066
  }
1020
1067
 
1021
1068
  void CanvasRenderingContext2D::fillCirclesRgb565(const std::uint16_t *xs,
@@ -1073,8 +1120,8 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
1073
1120
  command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(count_));
1074
1121
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(count_));
1075
1122
  command.colors.assign(colors.begin(), colors.begin() + static_cast<std::ptrdiff_t>(count_));
1076
- command.alpha = globalAlpha_;
1077
- batchDirty_ = true;
1123
+ command.alpha = state_->globalAlpha_;
1124
+ state_->batchDirty_ = true;
1078
1125
  }
1079
1126
 
1080
1127
  void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint16_t> &xs,
@@ -1095,8 +1142,8 @@ void CanvasRenderingContext2D::appendPresentCircles(const std::vector<std::uint1
1095
1142
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(limit));
1096
1143
  command.colors.resize(limit);
1097
1144
  for (std::size_t i = 0; i < limit; ++i) command.colors[i] = colors[i].value;
1098
- command.alpha = globalAlpha_;
1099
- batchDirty_ = true;
1145
+ command.alpha = state_->globalAlpha_;
1146
+ state_->batchDirty_ = true;
1100
1147
  }
1101
1148
 
1102
1149
  void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std::uint16_t> &xs,
@@ -1113,8 +1160,8 @@ void CanvasRenderingContext2D::appendPresentCirclesUniform(const std::vector<std
1113
1160
  command.xs.assign(xs.begin(), xs.begin() + static_cast<std::ptrdiff_t>(count));
1114
1161
  command.ys.assign(ys.begin(), ys.begin() + static_cast<std::ptrdiff_t>(count));
1115
1162
  command.colors.assign(count, color);
1116
- command.alpha = globalAlpha_;
1117
- batchDirty_ = true;
1163
+ command.alpha = state_->globalAlpha_;
1164
+ state_->batchDirty_ = true;
1118
1165
  }
1119
1166
 
1120
1167
  void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int32_t> &x0s,
@@ -1132,7 +1179,7 @@ void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int
1132
1179
  if (limit == 0) return;
1133
1180
  using gea::framework::graphics::TriangleEntry;
1134
1181
  CanvasPresentCommand &command = appendPresentCommand(CanvasPresentCommandType::FillTrianglesRgb565);
1135
- command.alpha = globalAlpha_;
1182
+ command.alpha = state_->globalAlpha_;
1136
1183
  command.triangles.resize(limit);
1137
1184
  const std::int32_t *px0 = x0s.data();
1138
1185
  const std::int32_t *py0 = y0s.data();
@@ -1162,7 +1209,7 @@ void CanvasRenderingContext2D::appendPresentTriangles(const std::vector<std::int
1162
1209
  t.rowY0 = lo01 < t.y2 ? lo01 : t.y2;
1163
1210
  t.rowY1 = hi01 > t.y2 ? hi01 : t.y2;
1164
1211
  }
1165
- batchDirty_ = true;
1212
+ state_->batchDirty_ = true;
1166
1213
  }
1167
1214
 
1168
1215
  void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1180,8 +1227,8 @@ void CanvasRenderingContext2D::appendPresentDrawImage(const gea::framework::grap
1180
1227
  command.srcHeight = srcHeight;
1181
1228
  command.x = x;
1182
1229
  command.y = y;
1183
- command.alpha = globalAlpha_;
1184
- batchDirty_ = true;
1230
+ command.alpha = state_->globalAlpha_;
1231
+ state_->batchDirty_ = true;
1185
1232
  }
1186
1233
 
1187
1234
  void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1205,8 +1252,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageScaled(const gea::framework
1205
1252
  command.w = w;
1206
1253
  command.h = h;
1207
1254
  command.radius = radius;
1208
- command.alpha = globalAlpha_;
1209
- batchDirty_ = true;
1255
+ command.alpha = state_->globalAlpha_;
1256
+ state_->batchDirty_ = true;
1210
1257
  }
1211
1258
 
1212
1259
  void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1228,8 +1275,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageRotated90CW(const gea::fram
1228
1275
  command.y = y;
1229
1276
  command.w = w;
1230
1277
  command.h = h;
1231
- command.alpha = globalAlpha_;
1232
- batchDirty_ = true;
1278
+ command.alpha = state_->globalAlpha_;
1279
+ state_->batchDirty_ = true;
1233
1280
  }
1234
1281
 
1235
1282
  void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework::graphics::pixel::native_t *pixels,
@@ -1249,8 +1296,8 @@ void CanvasRenderingContext2D::appendPresentDrawImageTiledX(const gea::framework
1249
1296
  command.x = x;
1250
1297
  command.y = y;
1251
1298
  command.w = w;
1252
- command.alpha = globalAlpha_;
1253
- batchDirty_ = true;
1299
+ command.alpha = state_->globalAlpha_;
1300
+ state_->batchDirty_ = true;
1254
1301
  }
1255
1302
 
1256
1303
  void CanvasRenderingContext2D::appendPresentFillText(const std::string &text, int x, int y, gea::framework::graphics::pixel::native_t color)
@@ -1261,17 +1308,17 @@ void CanvasRenderingContext2D::appendPresentFillText(const std::string &text, in
1261
1308
  command.x = x;
1262
1309
  command.y = y;
1263
1310
  command.color = color;
1264
- command.scale = fontScale_;
1265
- command.fontFamilyId = fontFamilyId_;
1266
- command.fontSizePx = fontSizePx_;
1267
- command.alpha = globalAlpha_;
1268
- batchDirty_ = true;
1311
+ command.scale = state_->fontScale_;
1312
+ command.fontFamilyId = state_->fontFamilyId_;
1313
+ command.fontSizePx = state_->fontSizePx_;
1314
+ command.alpha = state_->globalAlpha_;
1315
+ state_->batchDirty_ = true;
1269
1316
  }
1270
1317
 
1271
1318
  void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphics::Canvas &surface)
1272
1319
  {
1273
- for (std::size_t commandIndex = 0; commandIndex < presentCommandCount_; ++commandIndex) {
1274
- const CanvasPresentCommand &command = presentCommands_[commandIndex];
1320
+ for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex) {
1321
+ const CanvasPresentCommand &command = state_->presentCommands_[commandIndex];
1275
1322
  surface.setGlobalAlpha(command.alpha);
1276
1323
  switch (command.type) {
1277
1324
  case CanvasPresentCommandType::Clear:
@@ -1340,18 +1387,18 @@ void CanvasRenderingContext2D::replayPresentBatchToCanvas(gea::framework::graphi
1340
1387
  break;
1341
1388
  }
1342
1389
  }
1343
- surface.setGlobalAlpha(globalAlpha_);
1390
+ surface.setGlobalAlpha(state_->globalAlpha_);
1344
1391
  }
1345
1392
 
1346
1393
  bool CanvasRenderingContext2D::presentBatch()
1347
1394
  {
1348
- if (presentCommandCount_ == 0) return false;
1395
+ if (state_->presentCommandCount_ == 0) return false;
1349
1396
  gTotalPresentBatchOk++;
1350
1397
 
1351
1398
  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];
1399
+ commands.reserve(state_->presentCommandCount_);
1400
+ for (std::size_t commandIndex = 0; commandIndex < state_->presentCommandCount_; ++commandIndex) {
1401
+ const CanvasPresentCommand &command = state_->presentCommands_[commandIndex];
1355
1402
  gea::platform::display::DisplayPresentCommand displayCommand{};
1356
1403
  switch (command.type) {
1357
1404
  case CanvasPresentCommandType::Clear:
@@ -1429,15 +1476,15 @@ bool CanvasRenderingContext2D::presentBatch()
1429
1476
 
1430
1477
  void CanvasRenderingContext2D::applyDrawState(gea::framework::graphics::Canvas &surface) const
1431
1478
  {
1432
- surface.setGlobalAlpha(globalAlpha_);
1479
+ surface.setGlobalAlpha(state_->globalAlpha_);
1433
1480
  }
1434
1481
 
1435
1482
  void CanvasRenderingContext2D::markDirty()
1436
1483
  {
1437
1484
  const std::int64_t started = nowUs();
1438
1485
  gCanvasPerfStats.markDirtyCalls++;
1439
- if (batchDepth_ > 0) {
1440
- batchDirty_ = true;
1486
+ if (state_->batchDepth_ > 0) {
1487
+ state_->batchDirty_ = true;
1441
1488
  gCanvasPerfStats.markDirtyUs += nowUs() - started;
1442
1489
  return;
1443
1490
  }
@@ -1447,8 +1494,8 @@ void CanvasRenderingContext2D::markDirty()
1447
1494
 
1448
1495
  void CanvasRenderingContext2D::markDrawDirty()
1449
1496
  {
1450
- if (batchDepth_ > 0) {
1451
- batchDirty_ = true;
1497
+ if (state_->batchDepth_ > 0) {
1498
+ state_->batchDirty_ = true;
1452
1499
  return;
1453
1500
  }
1454
1501
  markDirty();
@@ -1456,59 +1503,59 @@ void CanvasRenderingContext2D::markDrawDirty()
1456
1503
 
1457
1504
  void CanvasRenderingContext2D::setFillStyle(const std::string &value)
1458
1505
  {
1459
- if (fillStyleCached_ && value == fillStyleSource_) return;
1460
- fillStyle_ = parseCanvasColor(value);
1461
- fillStyleSource_ = value;
1462
- fillStyleCached_ = true;
1506
+ if (state_->fillStyleCached_ && value == state_->fillStyleSource_) return;
1507
+ state_->fillStyle_ = parseCanvasColor(value);
1508
+ state_->fillStyleSource_ = value;
1509
+ state_->fillStyleCached_ = true;
1463
1510
  }
1464
1511
 
1465
1512
  void CanvasRenderingContext2D::setFillStyleRgb565(gea::framework::graphics::pixel::native_t color)
1466
1513
  {
1467
- fillStyle_ = canvasRgb565(color);
1468
- fillStyleCached_ = false;
1514
+ state_->fillStyle_ = canvasRgb565(color);
1515
+ state_->fillStyleCached_ = false;
1469
1516
  }
1470
1517
 
1471
1518
  void CanvasRenderingContext2D::setStrokeStyle(const std::string &value)
1472
1519
  {
1473
- if (strokeStyleCached_ && value == strokeStyleSource_) return;
1474
- strokeStyle_ = parseCanvasColor(value);
1475
- strokeStyleSource_ = value;
1476
- strokeStyleCached_ = true;
1520
+ if (state_->strokeStyleCached_ && value == state_->strokeStyleSource_) return;
1521
+ state_->strokeStyle_ = parseCanvasColor(value);
1522
+ state_->strokeStyleSource_ = value;
1523
+ state_->strokeStyleCached_ = true;
1477
1524
  }
1478
1525
 
1479
1526
  void CanvasRenderingContext2D::setStrokeStyleRgb565(gea::framework::graphics::pixel::native_t color)
1480
1527
  {
1481
- strokeStyle_ = canvasRgb565(color);
1482
- strokeStyleCached_ = false;
1528
+ state_->strokeStyle_ = canvasRgb565(color);
1529
+ state_->strokeStyleCached_ = false;
1483
1530
  }
1484
1531
 
1485
1532
  void CanvasRenderingContext2D::setGlobalAlpha(double alpha)
1486
1533
  {
1487
- globalAlpha_ = alphaFromUnit(alpha);
1534
+ state_->globalAlpha_ = alphaFromUnit(alpha);
1488
1535
  }
1489
1536
 
1490
1537
  void CanvasRenderingContext2D::setLineWidth(double width)
1491
1538
  {
1492
1539
  if (!std::isfinite(width) || width <= 0.0) return;
1493
- lineWidth_ = width;
1540
+ state_->lineWidth_ = width;
1494
1541
  }
1495
1542
 
1496
1543
  void CanvasRenderingContext2D::setFont(const std::string &font)
1497
1544
  {
1498
1545
  // Re-setting the same font (common: a per-frame FPS/HUD readout) is a no-op —
1499
1546
  // skip the CSS parse + FontRegistry family/atlas lookups entirely.
1500
- if (font == lastFontStr_) return;
1501
- lastFontStr_ = font;
1502
- fontScale_ = fontScaleFromCss(font);
1547
+ if (font == state_->lastFontStr_) return;
1548
+ state_->lastFontStr_ = font;
1549
+ state_->fontScale_ = fontScaleFromCss(font);
1503
1550
  const int px = fontPxFromCss(font);
1504
- if (px > 0) fontSizePx_ = px;
1551
+ if (px > 0) state_->fontSizePx_ = px;
1505
1552
  const std::string family = fontFamilyFromCss(font);
1506
- fontFamilyId_ = family.empty() ? -1 : gea::framework::graphics::FontRegistry::familyId(family.c_str());
1553
+ state_->fontFamilyId_ = family.empty() ? -1 : gea::framework::graphics::FontRegistry::familyId(family.c_str());
1507
1554
  // Only commit to the baked path when an atlas actually exists near this
1508
1555
  // size; otherwise stay on the scaled bitmap font.
1509
- if (fontFamilyId_ >= 0 &&
1510
- !gea::framework::graphics::FontRegistry::rasterizedFamily(fontFamilyId_, fontSizePx_).valid()) {
1511
- fontFamilyId_ = -1;
1556
+ if (state_->fontFamilyId_ >= 0 &&
1557
+ !gea::framework::graphics::FontRegistry::rasterizedFamily(state_->fontFamilyId_, state_->fontSizePx_).valid()) {
1558
+ state_->fontFamilyId_ = -1;
1512
1559
  }
1513
1560
  }
1514
1561
 
@@ -1526,10 +1573,10 @@ void CanvasRenderingContext2D::clear()
1526
1573
  void CanvasRenderingContext2D::clearRect(int x, int y, int w, int h)
1527
1574
  {
1528
1575
  if (recordingPresentBatch()) {
1529
- const std::uint8_t previousAlpha = globalAlpha_;
1530
- globalAlpha_ = 255;
1576
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
1577
+ state_->globalAlpha_ = 255;
1531
1578
  appendPresentFillRect(x, y, w, h, 0);
1532
- globalAlpha_ = previousAlpha;
1579
+ state_->globalAlpha_ = previousAlpha;
1533
1580
  return;
1534
1581
  }
1535
1582
  auto *surface = drawingCanvas();
@@ -1552,7 +1599,7 @@ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
1552
1599
  if (recordingPresentBatch()) {
1553
1600
  const std::int64_t started = nowUs();
1554
1601
  gCanvasPerfStats.fillRectCalls++;
1555
- appendPresentFillRect(x, y, w, h, fillStyle_);
1602
+ appendPresentFillRect(x, y, w, h, state_->fillStyle_);
1556
1603
  gCanvasPerfStats.fillRectUs += nowUs() - started;
1557
1604
  return;
1558
1605
  }
@@ -1561,7 +1608,7 @@ void CanvasRenderingContext2D::fillRect(int x, int y, int w, int h)
1561
1608
  const std::int64_t started = nowUs();
1562
1609
  gCanvasPerfStats.fillRectCalls++;
1563
1610
  applyDrawState(*surface);
1564
- surface->fillRect(x, y, w, h, fillStyle_);
1611
+ surface->fillRect(x, y, w, h, state_->fillStyle_);
1565
1612
  gCanvasPerfStats.fillRectUs += nowUs() - started;
1566
1613
  markDrawDirty();
1567
1614
  }
@@ -1574,13 +1621,13 @@ void CanvasRenderingContext2D::fillRect(double x, double y, double w, double h)
1574
1621
  void CanvasRenderingContext2D::strokeRect(int x, int y, int w, int h)
1575
1622
  {
1576
1623
  if (recordingPresentBatch()) {
1577
- appendPresentStrokeRect(x, y, w, h, strokeStyle_);
1624
+ appendPresentStrokeRect(x, y, w, h, state_->strokeStyle_);
1578
1625
  return;
1579
1626
  }
1580
1627
  auto *surface = drawingCanvas();
1581
1628
  if (!surface) return;
1582
1629
  applyDrawState(*surface);
1583
- surface->strokeRect(x, y, w, h, strokeStyle_);
1630
+ surface->strokeRect(x, y, w, h, state_->strokeStyle_);
1584
1631
  markDrawDirty();
1585
1632
  }
1586
1633
 
@@ -1594,7 +1641,7 @@ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius)
1594
1641
  if (recordingPresentBatch()) {
1595
1642
  const std::int64_t started = nowUs();
1596
1643
  gCanvasPerfStats.fillCircleCalls++;
1597
- appendPresentFillCircle(x, y, radius, fillStyle_);
1644
+ appendPresentFillCircle(x, y, radius, state_->fillStyle_);
1598
1645
  gCanvasPerfStats.fillCircleUs += nowUs() - started;
1599
1646
  return;
1600
1647
  }
@@ -1603,7 +1650,7 @@ void CanvasRenderingContext2D::fillCircle(int x, int y, int radius)
1603
1650
  const std::int64_t started = nowUs();
1604
1651
  gCanvasPerfStats.fillCircleCalls++;
1605
1652
  applyDrawState(*surface);
1606
- surface->fillCircle(x, y, radius, fillStyle_);
1653
+ surface->fillCircle(x, y, radius, state_->fillStyle_);
1607
1654
  gCanvasPerfStats.fillCircleUs += nowUs() - started;
1608
1655
  markDrawDirty();
1609
1656
  }
@@ -1616,13 +1663,13 @@ void CanvasRenderingContext2D::fillCircle(double x, double y, double radius)
1616
1663
  void CanvasRenderingContext2D::strokeCircle(int x, int y, int radius)
1617
1664
  {
1618
1665
  if (recordingPresentBatch()) {
1619
- appendPresentStrokeCircle(x, y, radius, strokeStyle_);
1666
+ appendPresentStrokeCircle(x, y, radius, state_->strokeStyle_);
1620
1667
  return;
1621
1668
  }
1622
1669
  auto *surface = drawingCanvas();
1623
1670
  if (!surface) return;
1624
1671
  applyDrawState(*surface);
1625
- surface->strokeCircle(x, y, radius, strokeStyle_);
1672
+ surface->strokeCircle(x, y, radius, state_->strokeStyle_);
1626
1673
  markDrawDirty();
1627
1674
  }
1628
1675
 
@@ -1853,43 +1900,43 @@ void CanvasRenderingContext2D::fillCirclesRgb565(const std::vector<std::uint16_t
1853
1900
 
1854
1901
  void CanvasRenderingContext2D::beginPath()
1855
1902
  {
1856
- hasArc_ = false;
1857
- pathX_.clear();
1858
- pathY_.clear();
1859
- pathStarts_.clear();
1860
- pathClosed_ = false;
1903
+ state_->hasArc_ = false;
1904
+ state_->pathX_.clear();
1905
+ state_->pathY_.clear();
1906
+ state_->pathStarts_.clear();
1907
+ state_->pathClosed_ = false;
1861
1908
  }
1862
1909
 
1863
1910
  void CanvasRenderingContext2D::arc(double x, double y, double radius, double, double)
1864
1911
  {
1865
- arcX_ = x;
1866
- arcY_ = y;
1867
- arcRadius_ = radius;
1868
- hasArc_ = true;
1912
+ state_->arcX_ = x;
1913
+ state_->arcY_ = y;
1914
+ state_->arcRadius_ = radius;
1915
+ state_->hasArc_ = true;
1869
1916
  }
1870
1917
 
1871
1918
  void CanvasRenderingContext2D::moveTo(double x, double y)
1872
1919
  {
1873
1920
  // Standard canvas semantics: moveTo starts a NEW subpath (it does not clear
1874
1921
  // 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;
1922
+ state_->pathStarts_.push_back(static_cast<int>(state_->pathX_.size()));
1923
+ state_->pathX_.push_back(x);
1924
+ state_->pathY_.push_back(y);
1925
+ state_->pathClosed_ = false;
1879
1926
  }
1880
1927
 
1881
1928
  void CanvasRenderingContext2D::lineTo(double x, double y)
1882
1929
  {
1883
- if (pathX_.empty()) moveTo(x, y);
1930
+ if (state_->pathX_.empty()) moveTo(x, y);
1884
1931
  else {
1885
- pathX_.push_back(x);
1886
- pathY_.push_back(y);
1932
+ state_->pathX_.push_back(x);
1933
+ state_->pathY_.push_back(y);
1887
1934
  }
1888
1935
  }
1889
1936
 
1890
1937
  void CanvasRenderingContext2D::closePath()
1891
1938
  {
1892
- pathClosed_ = true;
1939
+ state_->pathClosed_ = true;
1893
1940
  }
1894
1941
 
1895
1942
  void CanvasRenderingContext2D::fill()
@@ -1902,48 +1949,48 @@ void CanvasRenderingContext2D::fill()
1902
1949
  auto *surface = rec ? canvas() : drawingCanvas();
1903
1950
  if (!surface) return;
1904
1951
  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_);
1952
+ if (state_->hasArc_) {
1953
+ const int cx = rounded(state_->arcX_);
1954
+ const int cy = rounded(state_->arcY_);
1955
+ const int r = rounded(state_->arcRadius_);
1956
+ if (rec) appendPresentFillCircle(cx, cy, r, state_->fillStyle_);
1910
1957
  else {
1911
- surface->fillCircle(cx, cy, r, fillStyle_);
1958
+ surface->fillCircle(cx, cy, r, state_->fillStyle_);
1912
1959
  markDrawDirty();
1913
1960
  }
1914
1961
  return;
1915
1962
  }
1916
- const std::size_t n = pathX_.size();
1917
- if (n < 3 || pathStarts_.empty()) return;
1963
+ const std::size_t n = state_->pathX_.size();
1964
+ if (n < 3 || state_->pathStarts_.empty()) return;
1918
1965
 
1919
1966
  // Even-odd scanline fill over all subpaths (each implicitly closed). Unlike
1920
1967
  // a triangle fan (convex only), this fills arbitrary concave shapes AND
1921
1968
  // holes — coastlines, lakes-with-islands, building courtyards. Inside spans
1922
1969
  // emit as 1px fillRects.
1923
- double minYf = pathY_[0];
1924
- double maxYf = pathY_[0];
1970
+ double minYf = state_->pathY_[0];
1971
+ double maxYf = state_->pathY_[0];
1925
1972
  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];
1973
+ if (state_->pathY_[i] < minYf) minYf = state_->pathY_[i];
1974
+ if (state_->pathY_[i] > maxYf) maxYf = state_->pathY_[i];
1928
1975
  }
1929
1976
  const int yTop = std::max(0, static_cast<int>(std::floor(minYf)));
1930
1977
  const int yBot = std::min(surface->height() - 1, static_cast<int>(std::ceil(maxYf)));
1931
- const std::size_t nStarts = pathStarts_.size();
1978
+ const std::size_t nStarts = state_->pathStarts_.size();
1932
1979
  static std::vector<double> nodes;
1933
1980
  for (int y = yTop; y <= yBot; y++) {
1934
1981
  const double yc = y + 0.5;
1935
1982
  nodes.clear();
1936
1983
  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;
1984
+ const std::size_t a = static_cast<std::size_t>(state_->pathStarts_[s]);
1985
+ const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(state_->pathStarts_[s + 1]) : n;
1939
1986
  if (b - a < 2) continue;
1940
1987
  for (std::size_t i = a; i < b; i++) {
1941
1988
  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];
1989
+ const double yi = state_->pathY_[i];
1990
+ const double yj = state_->pathY_[j];
1944
1991
  if ((yi < yc && yj >= yc) || (yj < yc && yi >= yc)) {
1945
1992
  const double t = (yc - yi) / (yj - yi);
1946
- nodes.push_back(pathX_[i] + t * (pathX_[j] - pathX_[i]));
1993
+ nodes.push_back(state_->pathX_[i] + t * (state_->pathX_[j] - state_->pathX_[i]));
1947
1994
  }
1948
1995
  }
1949
1996
  }
@@ -1953,8 +2000,8 @@ void CanvasRenderingContext2D::fill()
1953
2000
  const int xa = static_cast<int>(std::lround(nodes[k]));
1954
2001
  const int xb = static_cast<int>(std::lround(nodes[k + 1]));
1955
2002
  if (xb > xa) {
1956
- if (rec) appendPresentFillRect(xa, y, xb - xa, 1, fillStyle_);
1957
- else surface->fillRect(xa, y, xb - xa, 1, fillStyle_);
2003
+ if (rec) appendPresentFillRect(xa, y, xb - xa, 1, state_->fillStyle_);
2004
+ else surface->fillRect(xa, y, xb - xa, 1, state_->fillStyle_);
1958
2005
  }
1959
2006
  }
1960
2007
  }
@@ -1967,56 +2014,56 @@ void CanvasRenderingContext2D::stroke()
1967
2014
  auto *surface = rec ? canvas() : drawingCanvas();
1968
2015
  if (!surface) return;
1969
2016
  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_);
2017
+ if (state_->hasArc_) {
2018
+ const int cx = rounded(state_->arcX_);
2019
+ const int cy = rounded(state_->arcY_);
2020
+ const int r = rounded(state_->arcRadius_);
2021
+ if (rec) appendPresentStrokeCircle(cx, cy, r, state_->strokeStyle_);
1975
2022
  else {
1976
- surface->strokeCircle(cx, cy, r, strokeStyle_);
2023
+ surface->strokeCircle(cx, cy, r, state_->strokeStyle_);
1977
2024
  markDrawDirty();
1978
2025
  }
1979
2026
  return;
1980
2027
  }
1981
- const std::size_t n = pathX_.size();
1982
- if (n < 2 || pathStarts_.empty()) return;
2028
+ const std::size_t n = state_->pathX_.size();
2029
+ if (n < 2 || state_->pathStarts_.empty()) return;
1983
2030
  // Honor lineWidth: every segment is an oriented quad (two triangles), with a
1984
2031
  // round disc at each joint/cap for widths ≥ ~2px — roads, casings, rivers.
1985
2032
  // 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();
2033
+ const double half = std::max(0.5, state_->lineWidth_ * 0.5);
2034
+ const std::size_t nStarts = state_->pathStarts_.size();
1988
2035
  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;
2036
+ const std::size_t a = static_cast<std::size_t>(state_->pathStarts_[s]);
2037
+ const std::size_t b = (s + 1 < nStarts) ? static_cast<std::size_t>(state_->pathStarts_[s + 1]) : n;
1991
2038
  if (b - a < 2) continue;
1992
- const std::size_t lastSeg = pathClosed_ ? b : b - 1;
2039
+ const std::size_t lastSeg = state_->pathClosed_ ? b : b - 1;
1993
2040
  for (std::size_t i = a; i < lastSeg; i++) {
1994
2041
  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];
2042
+ const double dx = state_->pathX_[j] - state_->pathX_[i];
2043
+ const double dy = state_->pathY_[j] - state_->pathY_[i];
1997
2044
  const double len = std::sqrt(dx * dx + dy * dy);
1998
2045
  if (len < 1e-6) continue;
1999
2046
  const double px = -dy / len * half; // perpendicular offset
2000
2047
  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);
2048
+ const int ax0 = rounded(state_->pathX_[i] + px), ay0 = rounded(state_->pathY_[i] + py);
2049
+ const int ax1 = rounded(state_->pathX_[i] - px), ay1 = rounded(state_->pathY_[i] - py);
2050
+ const int bx0 = rounded(state_->pathX_[j] + px), by0 = rounded(state_->pathY_[j] + py);
2051
+ const int bx1 = rounded(state_->pathX_[j] - px), by1 = rounded(state_->pathY_[j] - py);
2005
2052
  if (rec) {
2006
- appendPresentFillTriangle(ax0, ay0, ax1, ay1, bx1, by1, strokeStyle_);
2007
- appendPresentFillTriangle(ax0, ay0, bx1, by1, bx0, by0, strokeStyle_);
2053
+ appendPresentFillTriangle(ax0, ay0, ax1, ay1, bx1, by1, state_->strokeStyle_);
2054
+ appendPresentFillTriangle(ax0, ay0, bx1, by1, bx0, by0, state_->strokeStyle_);
2008
2055
  } else {
2009
- surface->fillTriangle(ax0, ay0, ax1, ay1, bx1, by1, strokeStyle_);
2010
- surface->fillTriangle(ax0, ay0, bx1, by1, bx0, by0, strokeStyle_);
2056
+ surface->fillTriangle(ax0, ay0, ax1, ay1, bx1, by1, state_->strokeStyle_);
2057
+ surface->fillTriangle(ax0, ay0, bx1, by1, bx0, by0, state_->strokeStyle_);
2011
2058
  }
2012
2059
  }
2013
2060
  if (half >= 1.0) {
2014
2061
  const int r = rounded(half);
2015
2062
  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_);
2063
+ const int cx = rounded(state_->pathX_[i]);
2064
+ const int cy = rounded(state_->pathY_[i]);
2065
+ if (rec) appendPresentFillCircle(cx, cy, r, state_->strokeStyle_);
2066
+ else surface->fillCircle(cx, cy, r, state_->strokeStyle_);
2020
2067
  }
2021
2068
  }
2022
2069
  }
@@ -2028,7 +2075,7 @@ void CanvasRenderingContext2D::fillText(const std::string &text, int x, int y)
2028
2075
  if (recordingPresentBatch()) {
2029
2076
  const std::int64_t started = nowUs();
2030
2077
  gCanvasPerfStats.fillTextCalls++;
2031
- appendPresentFillText(text, x, y, fillStyle_);
2078
+ appendPresentFillText(text, x, y, state_->fillStyle_);
2032
2079
  gCanvasPerfStats.fillTextUs += nowUs() - started;
2033
2080
  return;
2034
2081
  }
@@ -2037,8 +2084,8 @@ void CanvasRenderingContext2D::fillText(const std::string &text, int x, int y)
2037
2084
  const std::int64_t started = nowUs();
2038
2085
  gCanvasPerfStats.fillTextCalls++;
2039
2086
  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_);
2087
+ if (state_->fontFamilyId_ >= 0) surface->drawTextFontFamily(text.c_str(), x, y, state_->fillStyle_, state_->fontFamilyId_, state_->fontSizePx_);
2088
+ else surface->drawText(text.c_str(), x, y, state_->fillStyle_, state_->fontScale_);
2042
2089
  gCanvasPerfStats.fillTextUs += nowUs() - started;
2043
2090
  markDrawDirty();
2044
2091
  }
@@ -2075,6 +2122,32 @@ void CanvasRenderingContext2D::drawImage(int imageId, double dx, double dy)
2075
2122
  drawImage(imageId, rounded(dx), rounded(dy));
2076
2123
  }
2077
2124
 
2125
+ void CanvasRenderingContext2D::drawPixelRows(const gea::framework::graphics::pixel::native_t *pixels, int stride, int width, int height, int dx, int dy)
2126
+ {
2127
+ if (!pixels || width <= 0 || height <= 0 || stride < width) return;
2128
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
2129
+ state_->globalAlpha_ = 255;
2130
+ if (recordingPresentBatch()) {
2131
+ for (int row = 0; row < height; ++row)
2132
+ appendPresentDrawImage(pixels + static_cast<std::size_t>(row) * static_cast<std::size_t>(stride), nullptr, width, 1, dx, dy + row);
2133
+ } else if (auto *surface = drawingCanvas()) {
2134
+ applyDrawState(*surface);
2135
+ for (int row = 0; row < height; ++row)
2136
+ surface->drawImage(pixels + static_cast<std::size_t>(row) * static_cast<std::size_t>(stride), nullptr, width, 1, dx, dy + row);
2137
+ markDrawDirty();
2138
+ }
2139
+ state_->globalAlpha_ = previousAlpha;
2140
+ }
2141
+
2142
+ void CanvasRenderingContext2D::putImage(int imageId, int dx, int dy)
2143
+ {
2144
+ // putImageData replaces the destination pixels: globalAlpha does not apply.
2145
+ const std::uint8_t previousAlpha = state_->globalAlpha_;
2146
+ state_->globalAlpha_ = 255;
2147
+ drawImage(imageId, dx, dy);
2148
+ state_->globalAlpha_ = previousAlpha;
2149
+ }
2150
+
2078
2151
  void CanvasRenderingContext2D::drawImage(int imageId, int dx, int dy, int dw, int dh)
2079
2152
  {
2080
2153
  gTotalDrawImage++;
@@ -2108,7 +2181,7 @@ double CanvasRenderingContext2D::measureTextInkCenter(const std::string &text)
2108
2181
  auto *surface = drawingCanvas();
2109
2182
  if (!surface) surface = canvas();
2110
2183
  if (!surface) return 0.0;
2111
- return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
2184
+ return static_cast<double>(surface->measureTextInkCenterFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
2112
2185
  }
2113
2186
 
2114
2187
  double CanvasRenderingContext2D::measureText(const std::string &text)
@@ -2116,7 +2189,7 @@ double CanvasRenderingContext2D::measureText(const std::string &text)
2116
2189
  auto *surface = drawingCanvas();
2117
2190
  if (!surface) surface = canvas();
2118
2191
  if (!surface) return 0.0;
2119
- return static_cast<double>(surface->measureTextFontFamily(text.c_str(), fontFamilyId_, fontSizePx_));
2192
+ return static_cast<double>(surface->measureTextFontFamily(text.c_str(), state_->fontFamilyId_, state_->fontSizePx_));
2120
2193
  }
2121
2194
 
2122
2195
  void CanvasRenderingContext2D::drawImageCircle(int imageId, int dx, int dy, int dw, int dh)
@@ -2218,43 +2291,43 @@ void CanvasRenderingContext2D::beginBatch()
2218
2291
  {
2219
2292
  gTotalBeginBatch++;
2220
2293
  gCanvasPerfStats.batchBeginCalls++;
2221
- batchDepth_++;
2222
- if (batchDepth_ != 1) return;
2223
- batchDirty_ = false;
2294
+ state_->batchDepth_++;
2295
+ if (state_->batchDepth_ != 1) return;
2296
+ state_->batchDirty_ = false;
2224
2297
  resetPresentCommands();
2225
2298
  // Re-evaluate display-backed eligibility every frame without binding the
2226
2299
  // display canvas: command-recorded batches do not need a framebuffer until
2227
2300
  // they fall back to replay. This avoids expensive target-side preservation
2228
2301
  // on double-buffered panels.
2229
2302
  if (nodeId_ >= 0 && Tree::instance().markDisplayBackedCanvas(nodeId_)) {
2230
- presentRecording_ = true;
2231
- batchCanvas_ = nullptr;
2303
+ state_->presentRecording_ = true;
2304
+ state_->batchCanvas_ = nullptr;
2232
2305
  return;
2233
2306
  }
2234
2307
  if (nodeId_ >= 0) Tree::instance().ensureCanvas(nodeId_);
2235
2308
  if (nodeId_ >= 0 && Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2236
- presentRecording_ = true;
2237
- batchCanvas_ = nullptr;
2309
+ state_->presentRecording_ = true;
2310
+ state_->batchCanvas_ = nullptr;
2238
2311
  return;
2239
2312
  }
2240
- presentRecording_ = false;
2241
- batchCanvas_ = canvas();
2313
+ state_->presentRecording_ = false;
2314
+ state_->batchCanvas_ = canvas();
2242
2315
  }
2243
2316
 
2244
2317
  void CanvasRenderingContext2D::endBatch()
2245
2318
  {
2246
2319
  gTotalEndBatch++;
2247
2320
  gCanvasPerfStats.batchEndCalls++;
2248
- if (batchDepth_ <= 0) return;
2249
- batchDepth_--;
2250
- if (batchDepth_ > 0) return;
2251
- batchCanvas_ = nullptr;
2252
- if (batchDirty_ && nodeId_ >= 0) {
2321
+ if (state_->batchDepth_ <= 0) return;
2322
+ state_->batchDepth_--;
2323
+ if (state_->batchDepth_ > 0) return;
2324
+ state_->batchCanvas_ = nullptr;
2325
+ if (state_->batchDirty_ && nodeId_ >= 0) {
2253
2326
  if (Tree::instance().isDisplayBackedCanvas(nodeId_)) {
2254
2327
  const std::int64_t started = nowUs();
2255
2328
  gCanvasPerfStats.batchFlushCalls++;
2256
2329
  bool presented = false;
2257
- if (presentRecording_) presented = presentBatch();
2330
+ if (state_->presentRecording_) presented = presentBatch();
2258
2331
  if (presented) {
2259
2332
  Tree::instance().clearNodeDisplayCommandDirty(nodeId_);
2260
2333
  }
@@ -2266,7 +2339,7 @@ void CanvasRenderingContext2D::endBatch()
2266
2339
  static int replayFalls = 0;
2267
2340
  replayFalls++;
2268
2341
  if ((replayFalls & 7) == 1) std::printf("[canvas] presentBatch REJECTED -> replay+flush (#%d)\n", replayFalls);
2269
- presentRecording_ = false;
2342
+ state_->presentRecording_ = false;
2270
2343
  auto *surface = canvas();
2271
2344
  if (surface) replayPresentBatchToCanvas(*surface);
2272
2345
  gea::platform::display::Display::flush();
@@ -2277,8 +2350,8 @@ void CanvasRenderingContext2D::endBatch()
2277
2350
  }
2278
2351
  }
2279
2352
  resetPresentCommands();
2280
- presentRecording_ = false;
2281
- batchDirty_ = false;
2353
+ state_->presentRecording_ = false;
2354
+ state_->batchDirty_ = false;
2282
2355
  }
2283
2356
 
2284
2357
  void CanvasRenderer::record(const Node &node)