rlvgl-platform 0.2.5

Platform backends, blitters, and hardware integration for rlvgl.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
//! Basic graphics types and blitter traits for platform backends.
//!
//! These types describe pixel surfaces and operations that can be
//! accelerated by different platform implementations.

#[cfg(any(
    feature = "canvas",
    feature = "gif",
    feature = "apng",
    feature = "nes",
    feature = "png",
    feature = "jpeg",
    feature = "qrcode",
    feature = "lottie",
    feature = "fontdue",
    test,
))]
use alloc::vec::Vec;
#[cfg(feature = "fontdue")]
use alloc::{collections::BTreeMap, vec};
use bitflags::bitflags;
use heapless::Vec as HVec;
use rlvgl_core::cmd::{BlendMode, Cmd, CommandList};
use rlvgl_core::font::{FontMetrics, ShapedText};
#[cfg(feature = "fontdue")]
use rlvgl_core::fontdue::{Metrics, line_metrics, rasterize_glyph};
use rlvgl_core::renderer::Renderer;
use rlvgl_core::widget::{Color, Rect as WidgetRect};

#[cfg(feature = "fontdue")]
const FONT_DATA: &[u8] = include_bytes!("../assets/fonts/DejaVuSans.ttf");

#[cfg(feature = "fontdue")]
fn round_to_i32(value: f32) -> i32 {
    if value.is_nan() {
        0
    } else if value >= 0.0 {
        (value + 0.5) as i32
    } else {
        (value - 0.5) as i32
    }
}

#[cfg(feature = "fontdue")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
/// Key identifying a cached glyph by font, size, and character.
struct GlyphKey {
    /// Pointer to the font data used to rasterize the glyph.
    font: *const u8,
    /// Font size in pixels, stored as raw bits for ordering.
    size: u32,
    /// Unicode codepoint of the glyph.
    ch: char,
}

/// Supported pixel formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PixelFmt {
    /// 32-bit ARGB8888 format.
    Argb8888,
    /// 16-bit RGB565 format.
    Rgb565,
    /// 8-bit grayscale format.
    L8,
    /// 8-bit alpha-only format.
    A8,
    /// 4-bit alpha-only format.
    A4,
}

/// Rectangular region within a surface.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rect {
    /// Left coordinate of the rectangle.
    pub x: i32,
    /// Top coordinate of the rectangle.
    pub y: i32,
    /// Width of the rectangle in pixels.
    pub w: u32,
    /// Height of the rectangle in pixels.
    pub h: u32,
}

/// A pixel buffer with dimension and format metadata.
pub struct Surface<'a> {
    /// Underlying pixel storage.
    pub buf: &'a mut [u8],
    /// Number of bytes between consecutive lines.
    pub stride: usize,
    /// Pixel format used by the buffer.
    pub format: PixelFmt,
    /// Width of the surface in pixels.
    pub width: u32,
    /// Height of the surface in pixels.
    pub height: u32,
}

impl<'a> Surface<'a> {
    /// Create a new surface from raw parts.
    pub fn new(
        buf: &'a mut [u8],
        stride: usize,
        format: PixelFmt,
        width: u32,
        height: u32,
    ) -> Self {
        Self {
            buf,
            stride,
            format,
            width,
            height,
        }
    }
}

bitflags! {
    /// Capabilities supported by a blitter implementation.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct BlitCaps: u32 {
        /// Ability to fill regions with a solid color.
        const FILL = 0b0001;
        /// Ability to copy pixels between surfaces.
        const BLIT = 0b0010;
        /// Ability to blend a source over a destination.
        const BLEND = 0b0100;
        /// Ability to convert between pixel formats.
        const PFC = 0b1000;
    }
}

/// Trait implemented by types capable of transferring pixel data.
pub trait Blitter {
    /// Return the capabilities supported by this blitter.
    fn caps(&self) -> BlitCaps;

    /// Fill `area` within `dst` with a solid `color`.
    fn fill(&mut self, dst: &mut Surface, area: Rect, color: u32);

    /// Copy pixels from `src` within `src_area` to `dst` at `dst_pos`.
    fn blit(&mut self, src: &Surface, src_area: Rect, dst: &mut Surface, dst_pos: (i32, i32));

    /// Blend pixels from `src` over `dst`.
    fn blend(&mut self, src: &Surface, src_area: Rect, dst: &mut Surface, dst_pos: (i32, i32));
}

/// Collects dirty rectangles for a frame and optionally coalesces them.
///
/// The planner stores up to `N` rectangles in a stack-allocated buffer. Call
/// [`Self::add`] to register a region that changed during rendering and
/// [`Self::rects`] to obtain the batched list for flushing. After presenting
/// the frame, call [`Self::clear`] to reuse the planner for the next frame.
///
/// Adds beyond `N` are silently dropped, but [`Self::overflowed`] flips to
/// `true` so callers driving a dirty-rect present path know the rect set is
/// incomplete and can fall back to a full-frame repaint for that frame.
pub struct BlitPlanner<const N: usize> {
    rects: HVec<Rect, N>,
    overflowed: bool,
}

impl<const N: usize> BlitPlanner<N> {
    /// Create an empty planner.
    pub fn new() -> Self {
        Self {
            rects: HVec::new(),
            overflowed: false,
        }
    }

    /// Record a dirty rectangle.
    pub fn add(&mut self, rect: Rect) {
        if self.rects.push(rect).is_err() {
            self.overflowed = true;
        }
    }

    /// Return all accumulated rectangles.
    pub fn rects(&self) -> &[Rect] {
        &self.rects
    }

    /// Whether [`Self::add`] dropped a rect because the planner was full.
    pub fn overflowed(&self) -> bool {
        self.overflowed
    }

    /// Remove all stored rectangles.
    pub fn clear(&mut self) {
        self.rects.clear();
        self.overflowed = false;
    }
}

impl<const N: usize> Default for BlitPlanner<N> {
    fn default() -> Self {
        Self::new()
    }
}

/// Renderer implementation backed by a [`Blitter`].
///
/// A `BlitterRenderer` owns a target [`Surface`] and batches dirty regions
/// using a [`BlitPlanner`]. Widgets interact with the generic [`Renderer`] trait
/// without being aware of the underlying blitter.
pub struct BlitterRenderer<'a, B: Blitter, const N: usize> {
    blitter: &'a mut B,
    surface: Surface<'a>,
    planner: BlitPlanner<N>,
    #[cfg(any(
        feature = "canvas",
        feature = "gif",
        feature = "apng",
        feature = "nes",
        all(feature = "png", not(target_os = "none")),
        all(feature = "jpeg", not(target_os = "none")),
        all(feature = "qrcode", not(target_os = "none")),
        feature = "lottie",
        test,
    ))]
    scratch: Option<Vec<u8>>,
    #[cfg(feature = "fontdue")]
    glyph_cache: BTreeMap<GlyphKey, (Metrics, Vec<u8>)>,
}

impl<'a, B: Blitter, const N: usize> BlitterRenderer<'a, B, N> {
    /// Create a new renderer targeting `surface` using `blitter`.
    pub fn new(blitter: &'a mut B, surface: Surface<'a>) -> Self {
        Self {
            blitter,
            surface,
            planner: BlitPlanner::new(),
            #[cfg(any(
                feature = "canvas",
                feature = "gif",
                feature = "apng",
                feature = "nes",
                all(feature = "png", not(target_os = "none")),
                all(feature = "jpeg", not(target_os = "none")),
                all(feature = "qrcode", not(target_os = "none")),
                feature = "lottie",
                test,
            ))]
            scratch: None,
            #[cfg(feature = "fontdue")]
            glyph_cache: BTreeMap::new(),
        }
    }

    /// Access the internal dirty-rectangle planner.
    pub fn planner(&mut self) -> &mut BlitPlanner<N> {
        &mut self.planner
    }

    #[cfg(any(
        feature = "canvas",
        feature = "gif",
        feature = "apng",
        feature = "nes",
        all(feature = "png", not(target_os = "none")),
        all(feature = "jpeg", not(target_os = "none")),
        all(feature = "qrcode", not(target_os = "none")),
        feature = "lottie",
        test,
    ))]
    fn blit_colors(&mut self, position: (i32, i32), pixels: &[Color], w: u32, h: u32) {
        let required = (w * h * 4) as usize;
        let buf = self.scratch.get_or_insert_with(Vec::new);
        if buf.len() < required {
            buf.resize(required, 0);
        }
        for (i, c) in pixels.iter().enumerate() {
            buf[i * 4..i * 4 + 4].copy_from_slice(&c.to_argb8888().to_le_bytes());
        }
        let src = Surface::new(
            &mut buf[..required],
            (w * 4) as usize,
            PixelFmt::Argb8888,
            w,
            h,
        );
        self.blitter
            .blit(&src, Rect { x: 0, y: 0, w, h }, &mut self.surface, position);
        self.planner.add(Rect {
            x: position.0,
            y: position.1,
            w,
            h,
        });
    }

    #[cfg(all(feature = "png", not(target_os = "none")))]
    /// Decode a PNG image and blit it onto the target surface.
    pub fn draw_png(
        &mut self,
        position: (i32, i32),
        data: &[u8],
    ) -> Result<(), rlvgl_core::png::DecodingError> {
        let (pixels, w, h) = rlvgl_core::png::decode(data)?;
        self.blit_colors(position, &pixels, w, h);
        Ok(())
    }

    #[cfg(all(feature = "jpeg", not(target_os = "none")))]
    /// Decode a JPEG image and blit it onto the target surface.
    pub fn draw_jpeg(
        &mut self,
        position: (i32, i32),
        data: &[u8],
    ) -> Result<(), rlvgl_core::jpeg::Error> {
        let (pixels, w, h) = rlvgl_core::jpeg::decode(data)?;
        self.blit_colors(position, &pixels, w as u32, h as u32);
        Ok(())
    }

    #[cfg(all(feature = "qrcode", not(target_os = "none")))]
    /// Generate a QR code from `data` and blit it onto the target surface.
    pub fn draw_qr(
        &mut self,
        position: (i32, i32),
        data: &[u8],
    ) -> Result<(), rlvgl_core::qrcode::QrError> {
        let (pixels, w, h) = rlvgl_core::qrcode::generate(data)?;
        self.blit_colors(position, &pixels, w, h);
        Ok(())
    }

    #[cfg(feature = "lottie")]
    /// Render a Lottie JSON animation frame and blit it onto the target surface.
    ///
    /// Returns an error if the JSON data is invalid.
    pub fn draw_lottie_frame(
        &mut self,
        position: (i32, i32),
        json: &str,
        frame: usize,
        width: u32,
        height: u32,
    ) -> Result<(), rlvgl_core::lottie::Error> {
        let pixels =
            rlvgl_core::lottie::render_lottie_frame(json, frame, width as usize, height as usize)?;
        self.blit_colors(position, &pixels, width, height);
        Ok(())
    }

    #[cfg(feature = "canvas")]
    /// Blit an [`rlvgl_core::canvas::Canvas`] onto the target surface.
    pub fn draw_canvas(&mut self, position: (i32, i32), canvas: &rlvgl_core::canvas::Canvas) {
        let (w, h) = canvas.size();
        let pixels = canvas.pixels();
        self.blit_colors(position, &pixels, w, h);
    }

    #[cfg(feature = "gif")]
    /// Decode a GIF and blit the selected frame onto the target surface.
    pub fn draw_gif_frame(
        &mut self,
        position: (i32, i32),
        data: &[u8],
        frame: usize,
    ) -> Result<(), rlvgl_core::gif::DecodingError> {
        let (frames, w, h) = rlvgl_core::gif::decode(data)?;
        if let Some(f) = frames.get(frame) {
            self.blit_colors(position, &f.pixels, w as u32, h as u32);
        }
        Ok(())
    }

    #[cfg(feature = "apng")]
    /// Decode an APNG and blit the selected frame onto the target surface.
    pub fn draw_apng_frame(
        &mut self,
        position: (i32, i32),
        data: &[u8],
        frame: usize,
    ) -> Result<(), image::ImageError> {
        let (frames, w, h) = rlvgl_core::apng::decode(data)?;
        if let Some(f) = frames.get(frame) {
            self.blit_colors(position, &f.pixels, w, h);
        }
        Ok(())
    }

    #[cfg(all(feature = "pinyin", feature = "fontdue"))]
    /// Render Pinyin IME candidate characters via the blitter.
    ///
    /// Returns `true` if any candidates were rendered for `input`.
    pub fn draw_pinyin_candidates(
        &mut self,
        position: (i32, i32),
        ime: &rlvgl_core::pinyin::PinyinInputMethod,
        input: &str,
        color: Color,
    ) -> bool {
        if let Some(chars) = ime.candidates(input) {
            // Determine remaining space on the surface.
            let max_w = self.surface.width as i32 - position.0;
            let max_h = self.surface.height as i32 - position.1;
            if max_w <= 0 || max_h < 16 {
                return false;
            }

            // Truncate the candidate string to fit within the surface width.
            let text: alloc::string::String = chars.into_iter().collect();
            let max_chars = (max_w / 16) as usize;
            let clipped: alloc::string::String = text.chars().take(max_chars).collect();
            if clipped.is_empty() {
                return false;
            }
            Renderer::draw_text(self, position, &clipped, color);
            true
        } else {
            false
        }
    }

    #[cfg(all(feature = "fatfs", feature = "fontdue"))]
    /// List a FAT directory and render the entries line by line.
    pub fn draw_fatfs_dir<T>(
        &mut self,
        position: (i32, i32),
        image: &mut T,
        dir: &str,
        color: Color,
    ) -> Result<(), std::io::Error>
    where
        T: std::io::Read + std::io::Write + std::io::Seek,
    {
        let max_w = self.surface.width as i32 - position.0;
        let max_h = self.surface.height as i32 - position.1;
        if max_w <= 0 || max_h <= 0 {
            return Ok(());
        }
        let line_h = 16;
        let max_lines = (max_h / line_h) as usize;
        let max_chars = (max_w / 16) as usize;
        let names = rlvgl_core::fatfs::list_dir(image, dir)?;
        for (i, name) in names.iter().take(max_lines).enumerate() {
            let y = position.1 + (i as i32) * line_h;
            let clipped: alloc::string::String = name.chars().take(max_chars).collect();
            if clipped.is_empty() {
                break;
            }
            Renderer::draw_text(self, (position.0, y), &clipped, color);
        }
        Ok(())
    }

    #[cfg(feature = "nes")]
    /// Blit an NES frame represented as ARGB8888 [`Color`] pixels.
    pub fn draw_nes_frame(
        &mut self,
        position: (i32, i32),
        pixels: &[Color],
        width: u32,
        height: u32,
    ) {
        self.blit_colors(position, pixels, width, height);
    }

    #[cfg(feature = "fontdue")]
    /// Draw UTF-8 text using the supplied font and size.
    pub fn draw_text(
        &mut self,
        position: (i32, i32),
        text: &str,
        color: Color,
        font_data: &[u8],
        px: f32,
    ) {
        let vm = line_metrics(font_data, px).unwrap();
        let ascent = round_to_i32(vm.ascent);
        let baseline = position.1 + ascent;
        let mut x_cursor = position.0;
        for ch in text.chars() {
            let key = GlyphKey {
                font: font_data.as_ptr(),
                size: px.to_bits(),
                ch,
            };
            let (metrics, bitmap) = {
                let entry = self
                    .glyph_cache
                    .entry(key)
                    .or_insert_with(|| rasterize_glyph(font_data, ch, px).unwrap());
                (entry.0, entry.1.clone())
            };
            let w = metrics.width as i32;
            let h = metrics.height as i32;
            if w == 0 || h == 0 {
                x_cursor += round_to_i32(metrics.advance_width);
                continue;
            }
            let mut argb = vec![0u8; (w * h * 4) as usize];
            for y in 0..h {
                for x in 0..w {
                    let alpha = bitmap[(y) as usize * metrics.width + x as usize];
                    let idx = ((y * w + x) * 4) as usize;
                    argb[idx] = (color.0 as u16 * alpha as u16 / 255) as u8;
                    argb[idx + 1] = (color.1 as u16 * alpha as u16 / 255) as u8;
                    argb[idx + 2] = (color.2 as u16 * alpha as u16 / 255) as u8;
                    argb[idx + 3] = alpha;
                }
            }
            let src = Surface::new(
                argb.as_mut_slice(),
                (w * 4) as usize,
                PixelFmt::Argb8888,
                w as u32,
                h as u32,
            );
            let dst_pos = (
                x_cursor + metrics.xmin,
                baseline - ascent - metrics.ymin - (h - 1),
            );
            self.blitter.blend(
                &src,
                Rect {
                    x: 0,
                    y: 0,
                    w: w as u32,
                    h: h as u32,
                },
                &mut self.surface,
                dst_pos,
            );
            self.planner.add(Rect {
                x: dst_pos.0,
                y: dst_pos.1,
                w: w as u32,
                h: h as u32,
            });
            x_cursor += round_to_i32(metrics.advance_width);
        }
    }

    #[cfg(not(feature = "fontdue"))]
    /// Stub text renderer when fontdue is disabled.
    pub fn draw_text(
        &mut self,
        position: (i32, i32),
        text: &str,
        color: Color,
        _font_data: &[u8],
        _px: f32,
    ) {
        let _ = (position, text, color);
    }
}

impl<B: Blitter, const N: usize> Renderer for BlitterRenderer<'_, B, N> {
    fn blend_rect(&mut self, rect: WidgetRect, color: Color) {
        let alpha = color.3 as u16;
        if alpha == 0 {
            return;
        }
        if alpha == 255 {
            self.fill_rect(rect, color);
            return;
        }
        // Inline source-over blending for ARGB8888 surfaces.
        if self.surface.format == PixelFmt::Argb8888 {
            let sw = self.surface.width as i32;
            let sh = self.surface.height as i32;
            let stride = self.surface.stride;
            let inv = 255 - alpha;
            let x0 = rect.x.max(0);
            let y0 = rect.y.max(0);
            let x1 = (rect.x + rect.width).min(sw);
            let y1 = (rect.y + rect.height).min(sh);
            for y in y0..y1 {
                for x in x0..x1 {
                    let off = y as usize * stride + x as usize * 4;
                    let bg_b = self.surface.buf[off] as u16;
                    let bg_g = self.surface.buf[off + 1] as u16;
                    let bg_r = self.surface.buf[off + 2] as u16;
                    self.surface.buf[off] = ((color.2 as u16 * alpha + bg_b * inv) / 255) as u8;
                    self.surface.buf[off + 1] = ((color.1 as u16 * alpha + bg_g * inv) / 255) as u8;
                    self.surface.buf[off + 2] = ((color.0 as u16 * alpha + bg_r * inv) / 255) as u8;
                    self.surface.buf[off + 3] = 0xff;
                }
            }
            self.planner.add(Rect {
                x: rect.x,
                y: rect.y,
                w: rect.width as u32,
                h: rect.height as u32,
            });
        } else {
            // Fallback for non-ARGB8888 surfaces: just overwrite.
            self.fill_rect(rect, color);
        }
    }

    fn fill_rect(&mut self, rect: WidgetRect, color: Color) {
        let r = Rect {
            x: rect.x,
            y: rect.y,
            w: rect.width as u32,
            h: rect.height as u32,
        };
        self.planner.add(r);
        self.blitter.fill(&mut self.surface, r, color.to_argb8888());
    }

    fn draw_text(&mut self, position: (i32, i32), text: &str, color: Color) {
        #[cfg(feature = "fontdue")]
        {
            const PX: f32 = 16.0;
            BlitterRenderer::draw_text(self, position, text, color, FONT_DATA, PX);
        }
        #[cfg(not(feature = "fontdue"))]
        {
            let _ = (position, text, color);
        }
    }

    fn draw_pixels(&mut self, position: (i32, i32), pixels: &[Color], width: u32, height: u32) {
        // Fast path: write ARGB8888 pixels directly into the surface buffer,
        // then record the dirty rectangle. Avoids per-pixel fill_rect overhead.
        if self.surface.format == PixelFmt::Argb8888 {
            let sw = self.surface.width as i32;
            let sh = self.surface.height as i32;
            let stride = self.surface.stride;
            for y in 0..height as i32 {
                let dy = position.1 + y;
                if dy < 0 || dy >= sh {
                    continue;
                }
                for x in 0..width as i32 {
                    let dx = position.0 + x;
                    if dx < 0 || dx >= sw {
                        continue;
                    }
                    let src_idx = (y as u32 * width + x as u32) as usize;
                    if let Some(&c) = pixels.get(src_idx) {
                        let off = dy as usize * stride + dx as usize * 4;
                        self.surface.buf[off..off + 4]
                            .copy_from_slice(&c.to_argb8888().to_le_bytes());
                    }
                }
            }
            self.planner.add(Rect {
                x: position.0,
                y: position.1,
                w: width,
                h: height,
            });
        } else {
            // Fallback: per-pixel fill_rect for non-ARGB8888 surfaces
            for y in 0..height as i32 {
                for x in 0..width as i32 {
                    let idx = (y as u32 * width + x as u32) as usize;
                    if let Some(&c) = pixels.get(idx) {
                        self.fill_rect(
                            WidgetRect {
                                x: position.0 + x,
                                y: position.1 + y,
                                width: 1,
                                height: 1,
                            },
                            c,
                        );
                    }
                }
            }
        }
    }

    fn blend_row(&mut self, x: i32, y: i32, color: Color, coverage: &[u8]) {
        // AA inner-loop primitive. Direct ARGB8888 source-over with per-pixel
        // alpha = (color.alpha * coverage[i]) / 255. One planner.add per row
        // keeps DMA2D refresh granularity efficient versus per-pixel adds.
        if color.3 == 0 || coverage.is_empty() {
            return;
        }
        if self.surface.format != PixelFmt::Argb8888 {
            // Non-ARGB8888 surfaces: fall back through the trait default
            // (per-pixel blend_rect). This branch is rare on disco.
            for (i, &cov) in coverage.iter().enumerate() {
                if cov == 0 {
                    continue;
                }
                let alpha = ((color.3 as u16 * cov as u16) / 255) as u8;
                if alpha == 0 {
                    continue;
                }
                self.blend_rect(
                    WidgetRect {
                        x: x + i as i32,
                        y,
                        width: 1,
                        height: 1,
                    },
                    Color(color.0, color.1, color.2, alpha),
                );
            }
            return;
        }
        let sw = self.surface.width as i32;
        let sh = self.surface.height as i32;
        if y < 0 || y >= sh {
            return;
        }
        let stride = self.surface.stride;
        let src_a = color.3 as u16;
        let row_off = y as usize * stride;
        let mut written_x0 = i32::MAX;
        let mut written_x1 = i32::MIN;
        for (i, &cov) in coverage.iter().enumerate() {
            if cov == 0 {
                continue;
            }
            let px = x + i as i32;
            if px < 0 || px >= sw {
                continue;
            }
            let alpha = (src_a * cov as u16) / 255;
            if alpha == 0 {
                continue;
            }
            let inv = 255 - alpha;
            let off = row_off + (px as usize) * 4;
            let bg_b = self.surface.buf[off] as u16;
            let bg_g = self.surface.buf[off + 1] as u16;
            let bg_r = self.surface.buf[off + 2] as u16;
            self.surface.buf[off] = ((color.2 as u16 * alpha + bg_b * inv) / 255) as u8;
            self.surface.buf[off + 1] = ((color.1 as u16 * alpha + bg_g * inv) / 255) as u8;
            self.surface.buf[off + 2] = ((color.0 as u16 * alpha + bg_r * inv) / 255) as u8;
            self.surface.buf[off + 3] = 0xff;
            if px < written_x0 {
                written_x0 = px;
            }
            if px > written_x1 {
                written_x1 = px;
            }
        }
        if written_x0 <= written_x1 {
            self.planner.add(Rect {
                x: written_x0,
                y,
                w: (written_x1 - written_x0 + 1) as u32,
                h: 1,
            });
        }
    }

    fn submit(&mut self, list: &CommandList) {
        submit_with_occlusion(self, list);
    }
}

/// `CommandSink` impl with occlusion pre-pass.
///
/// Backend-specialized submit path: walks the [`CommandList`] and, for
/// each cmd, checks whether any *later* opaque [`Cmd::FillRect`] fully
/// covers its AABB. If so, the cmd is skipped entirely — it would be
/// overwritten anyway. Survivors are dispatched via the normal
/// [`Renderer`] trait methods on `self` (existing
/// [`BlitterRenderer::blend_row`] override applies, so AA cmds get the
/// hardware-blend fast path).
///
/// The check is O(n²) but cmd counts are small (a typical clock frame
/// is 50–100 cmds). Only `FillRect` with `alpha == 255` qualifies as an
/// occluder — geometric primitives (OBB, disc, arc, line) don't fully
/// cover their AABBs and are never occluders even at full alpha.
///
/// Two-pass dispatch:
///
/// 1. **Occlusion pre-pass.** For each cmd, scan forward looking for a
///    later opaque [`Cmd::FillRect`] whose AABB fully covers it; if
///    found, drop the cmd. Markers (`Barrier`, `SetClip`) terminate
///    the search range — a `Barrier` declares ordering that must be
///    preserved (compositor sync, swap hold), and a `SetClip` change
///    means later cmds render under a different clip and can't be
///    trusted as occluders of earlier pixels.
///
/// 2. **Adjacent-FillRect coalescing.** Survivor cmds are dispatched
///    one at a time, but consecutive [`Cmd::FillRect`]s with identical
///    `color` + `blend` and edge-adjacent rectangles are merged into a
///    single wider FillRect. Cuts down on the number of DMA2D fill
///    ops and reduces dirty-rect tracking pressure when widgets
///    happen to emit sequential strips of the same fill (toolbars,
///    background bands, table cells).
///
/// The coalescing is conservative — only horizontal or vertical edge
/// adjacency is detected (no L-shapes, no overlap-into-union). For
/// the common case of a row of cells fixed-width and same-color, this
/// reduces N rects to 1.
fn submit_with_occlusion<B: Blitter, const N: usize>(
    r: &mut BlitterRenderer<'_, B, N>,
    list: &CommandList,
) {
    let mut pending: Option<(WidgetRect, Color, BlendMode)> = None;

    for (i, cmd) in list.iter().enumerate() {
        // Occlusion pre-pass.
        let occluded = match cmd.aabb() {
            None => false,
            Some(bb_i) => {
                let mut hit = false;
                for later in list.iter().skip(i + 1) {
                    if matches!(later, Cmd::Barrier | Cmd::SetClip { .. }) {
                        break;
                    }
                    if !is_opaque_filler(later) {
                        continue;
                    }
                    let Some(bb_j) = later.aabb() else { continue };
                    if rect_contains(bb_j, bb_i) {
                        hit = true;
                        break;
                    }
                }
                hit
            }
        };
        if occluded {
            // Skip the cmd entirely. Don't flush pending — the next cmd
            // may still coalesce with what's pending across this gap.
            continue;
        }

        // Coalescing dispatch.
        if let Cmd::FillRect { rect, color, blend } = cmd {
            if let Some((prect, pcolor, pblend)) = pending.as_ref() {
                if pcolor == color
                    && pblend == blend
                    && let Some(merged) = merge_rects_axis_aligned(*prect, *rect)
                {
                    pending = Some((merged, *color, *blend));
                    continue;
                }
                // Can't merge — flush pending and start a new pending.
                flush_pending(r, pending.take());
            }
            pending = Some((*rect, *color, *blend));
        } else {
            // Markers and non-FillRect cmds break the coalescing run.
            flush_pending(r, pending.take());
            cmd.dispatch_to(r);
        }
    }
    flush_pending(r, pending.take());
}

#[inline]
fn flush_pending<B: Blitter, const N: usize>(
    r: &mut BlitterRenderer<'_, B, N>,
    pending: Option<(WidgetRect, Color, BlendMode)>,
) {
    if let Some((rect, color, blend)) = pending {
        Cmd::FillRect { rect, color, blend }.dispatch_to(r);
    }
}

/// Try to merge two rectangles into a single rectangle representing
/// their union. Returns `Some(union)` only when one of the four
/// edge-adjacent cases applies; returns `None` for overlap, gap, or
/// L-shapes (which would require a multi-rect representation).
#[inline]
fn merge_rects_axis_aligned(a: WidgetRect, b: WidgetRect) -> Option<WidgetRect> {
    if a.y == b.y && a.height == b.height {
        if a.x + a.width == b.x {
            return Some(WidgetRect {
                x: a.x,
                y: a.y,
                width: a.width + b.width,
                height: a.height,
            });
        }
        if b.x + b.width == a.x {
            return Some(WidgetRect {
                x: b.x,
                y: b.y,
                width: a.width + b.width,
                height: a.height,
            });
        }
    }
    if a.x == b.x && a.width == b.width {
        if a.y + a.height == b.y {
            return Some(WidgetRect {
                x: a.x,
                y: a.y,
                width: a.width,
                height: a.height + b.height,
            });
        }
        if b.y + b.height == a.y {
            return Some(WidgetRect {
                x: b.x,
                y: b.y,
                width: a.width,
                height: a.height + b.height,
            });
        }
    }
    None
}

#[inline]
fn is_opaque_filler(cmd: &Cmd) -> bool {
    if let Cmd::FillRect { color, .. } = cmd {
        color.3 == 255
    } else {
        false
    }
}

#[inline]
fn rect_contains(outer: WidgetRect, inner: WidgetRect) -> bool {
    outer.x <= inner.x
        && outer.y <= inner.y
        && outer.x + outer.width >= inner.x + inner.width
        && outer.y + outer.height >= inner.y + inner.height
}

/// Renderer wrapper that applies 90° CCW rotation for platforms where the
/// physical display is landscape but the framebuffer is portrait.
///
/// Maps logical coordinates (800×480 landscape) to framebuffer coordinates
/// (480×800 portrait):
///   fb_x = fb_width - logical_y - logical_height
///   fb_y = logical_x
///   fb_w = logical_height
///   fb_h = logical_width
pub struct RotatedRenderer<'a> {
    inner: &'a mut dyn Renderer,
    /// Portrait framebuffer width (the short dimension, e.g. 480).
    fb_width: i32,
}

impl<'a> RotatedRenderer<'a> {
    /// Create a rotated renderer wrapping `inner`.
    ///
    /// `fb_width` is the portrait framebuffer width (480 on STM32H747I-DISCO).
    pub fn new(inner: &'a mut dyn Renderer, fb_width: u32) -> Self {
        Self {
            inner,
            fb_width: fb_width as i32,
        }
    }

    /// FONT-04 §8.B: rotate a glyph's A8 coverage once into a bounded scratch
    /// buffer and emit it as physical (portrait) rows through
    /// `inner.blend_row`, instead of dispatching every coverage pixel through
    /// the rotation as a 1×1 `blend_rect`.
    ///
    /// `extent` is the glyph's tight bitmap rect in *landscape* coordinates.
    /// The rotation mirrors [`Self::draw_pixels`] / the DMA2D
    /// `draw_glyph_rotated` reference: landscape `(col,row)` →
    /// `scratch[col*h + (h-1-row)]`, physical size `h` wide × `w` tall, placed
    /// at `(fb_width - extent.y - h, extent.x)`. Because `inner.blend_row`
    /// performs the same per-pixel source-over (`alpha = color.a·cov/255`) the
    /// per-pixel path would, the rendered pixels are identical — this is a
    /// throughput change only (§8.A).
    ///
    /// Returns `false` when `font` has no coverage for `ch` (the caller falls
    /// back to an extent rect), matching the core `draw_glyph` contract.
    fn blit_glyph_coverage_rotated(
        &mut self,
        font: &dyn FontMetrics,
        ch: char,
        extent: WidgetRect,
        color: Color,
    ) -> bool {
        let w = extent.width;
        let h = extent.height;
        if w <= 0 || h <= 0 {
            return true; // zero-area glyph (e.g. space) — nothing to draw
        }
        let (wu, hu) = (w as usize, h as usize);

        // Bounded scratch (§8.C): no per-glyph heap in the render loop.
        const BOUND: usize = 64 * 64;
        if wu <= 64 && hu <= 64 && wu * hu <= BOUND {
            // 2026-06-15 (FONT-04): A8 coverage scratch, sibling to the Color
            // SCRATCH in `draw_pixels`. Same `.rlvgl_blit_scratch` placement so
            // consumers' linker scripts keep it off the MSP stack-growth path
            // (see the draw_pixels SCRATCH comment + disco ERRATA-005).
            #[cfg_attr(target_os = "none", unsafe(link_section = ".rlvgl_blit_scratch"))]
            static mut SCRATCH_COV: [u8; BOUND] = [0u8; BOUND]; // rlvgl-discipline: allow(static_mut)

            // SAFETY: single-core, single-threaded — `RotatedRenderer` is used
            // only from the main render loop, never an ISR. The scratch is
            // unique to this call site, not borrowed across calls; we fully
            // write the `[..wu*hu]` prefix before reading it back below.
            let scratch = unsafe { &mut SCRATCH_COV[..wu * hu] };

            // Gather each landscape coverage row and scatter it into the
            // rotated (portrait) layout in one pass.
            let mut row_buf = [0u8; 64];
            for row in 0..hu {
                let cov_row = &mut row_buf[..wu];
                if !font.glyph_coverage_row(ch, row as u16, 0, cov_row) {
                    return false;
                }
                for (col, &cov) in cov_row.iter().enumerate() {
                    scratch[col * hu + (hu - 1 - row)] = cov;
                }
            }

            // Physical placement mirrors `fill_rect`/`draw_pixels`.
            let fb_x = self.fb_width - extent.y - h;
            let fb_y = extent.x;
            // Emit `w` physical rows of `h` coverage bytes each; inner clips x
            // and blends source-over (preserving cov-0 holes).
            for py in 0..wu {
                let run = &scratch[py * hu..py * hu + hu];
                self.inner.blend_row(fb_x, fb_y + py as i32, color, run);
            }
            true
        } else {
            // Oversize fallback: the correct per-pixel rotated path
            // (`self.blend_row` default → rotated 1×1 `blend_rect`). Rare — the
            // disco widget path draws no glyphs this large.
            let mut chunk = [0u8; 64];
            for row in 0..hu {
                let mut x_off = 0usize;
                while x_off < wu {
                    let n = (wu - x_off).min(chunk.len());
                    let part = &mut chunk[..n];
                    if !font.glyph_coverage_row(ch, row as u16, x_off as u16, part) {
                        return false;
                    }
                    self.blend_row(extent.x + x_off as i32, extent.y + row as i32, color, part);
                    x_off += n;
                }
            }
            true
        }
    }
}

impl Renderer for RotatedRenderer<'_> {
    fn fill_rect(&mut self, rect: WidgetRect, color: Color) {
        let mut fb_x = self.fb_width - rect.y - rect.height;
        let fb_y = rect.x;
        let mut fb_w = rect.height;
        let fb_h = rect.width;

        if fb_w <= 0 || fb_h <= 0 {
            return;
        }

        // Clamp left edge: rect extends off-screen left
        if fb_x < 0 {
            fb_w += fb_x; // shrink width by the overshoot
            fb_x = 0;
        }
        // Clamp right edge
        if fb_x + fb_w > self.fb_width {
            fb_w = self.fb_width - fb_x;
        }
        if fb_w <= 0 {
            return;
        }

        self.inner.fill_rect(
            WidgetRect {
                x: fb_x,
                y: fb_y,
                width: fb_w,
                height: fb_h,
            },
            color,
        );
    }

    fn blend_rect(&mut self, rect: WidgetRect, color: Color) {
        let mut fb_x = self.fb_width - rect.y - rect.height;
        let fb_y = rect.x;
        let mut fb_w = rect.height;
        let fb_h = rect.width;

        if fb_w <= 0 || fb_h <= 0 {
            return;
        }

        if fb_x < 0 {
            fb_w += fb_x;
            fb_x = 0;
        }
        if fb_x + fb_w > self.fb_width {
            fb_w = self.fb_width - fb_x;
        }
        if fb_w <= 0 {
            return;
        }

        self.inner.blend_rect(
            WidgetRect {
                x: fb_x,
                y: fb_y,
                width: fb_w,
                height: fb_h,
            },
            color,
        );
    }

    fn draw_text(&mut self, position: (i32, i32), text: &str, color: Color) {
        let fx = self.fb_width - 1 - position.1;
        if fx >= 0 {
            self.inner.draw_text((fx, position.0), text, color);
        }
    }

    fn draw_glyph(&mut self, font: &dyn FontMetrics, ch: char, origin: (i32, i32), color: Color) {
        // FONT-04 §8.B: rotate the glyph's coverage once and blit rows through
        // `inner.blend_row`, rather than the per-pixel `blend_rect` the
        // trait-default `draw_glyph` → `blend_row` would dispatch on this
        // rotated wrapper. `origin` is the baseline pen position (same anchor
        // as the core default); derive the landscape extent from the metrics.
        let Some(info) = font.glyph_metrics(ch) else {
            return;
        };
        let extent = WidgetRect {
            x: origin.0 + info.bearing_x as i32,
            y: origin.1 - info.bearing_y as i32,
            width: info.width as i32,
            height: info.height as i32,
        };
        if extent.width <= 0 || extent.height <= 0 {
            return;
        }
        if !self.blit_glyph_coverage_rotated(font, ch, extent, color) {
            // No coverage for this glyph: deterministic extent rect (rotated),
            // matching the core `draw_glyph` fallback.
            self.blend_rect(extent, color);
        }
    }

    fn draw_text_shaped(&mut self, shaped: &ShapedText<'_>, origin: (i32, i32), color: Color) {
        // Mirror the core default's per-glyph structure, but route coverage
        // through the rotate-then-blit helper (FONT-04 §8.B). Reached by
        // callers that draw onto the rotated renderer without an intervening
        // clip wrapper; clip-wrapped widgets (e.g. `Label`) still funnel
        // through `blend_row` and are unaffected.
        let font = shaped.font;
        for glyph in &shaped.glyphs {
            let mut extent = glyph.extent();
            extent.x += origin.0;
            extent.y += origin.1;
            if extent.width <= 0 || extent.height <= 0 {
                continue;
            }
            let drawn = match font {
                Some(font) => self.blit_glyph_coverage_rotated(font, glyph.ch, extent, color),
                None => false,
            };
            if !drawn {
                self.blend_rect(extent, color);
            }
        }
    }

    fn draw_pixels(&mut self, position: (i32, i32), pixels: &[Color], width: u32, height: u32) {
        // 2026-05-17: replace the per-pixel `fill_rect` slow path with a
        // rotate-then-delegate pattern. The previous implementation made
        // W*H calls to self.fill_rect (each a 1×1 logical rect) through
        // the full rotation + clip + blitter pipeline. For a 60×60 icon
        // that's 3600 single-pixel fill_rects per draw, and IconStrip
        // draws 3 icons per frame — pinning CM7 inside the blitter for
        // seconds at a time and making poll_joystick effectively
        // unreachable (probe-rs halt evidence: 3 sequential halts ~1s
        // apart all landed at byte-identical PC/LR/SP inside
        // CpuBlitter::fill's row loop, called from this site).
        //
        // The inner `BlitterRenderer::draw_pixels` (line 617) already has
        // an Argb8888 fast path that writes pixels directly into the
        // surface buffer + adds one dirty rect — H+W work per call, no
        // per-pixel fill_rect dispatch. Rotating the source buffer once
        // (W*H copies) and calling inner.draw_pixels once collapses
        // 3 × 60 × 60 = 10,800 fill_rect dispatches down to 3 fast blits.
        if width == 0 || height == 0 {
            return;
        }

        // 90° rotation mapping (mirrors `fill_rect` above):
        // physical (fb_x, fb_y) = (fb_width - rect.y - rect.height, rect.x)
        // physical (width, height) = (rect.height, rect.width)
        let fb_x = self.fb_width - position.1 - height as i32;
        let fb_y = position.0;
        let phys_w = height; // physical width = logical height
        let phys_h = width; // physical height = logical width

        // Re-lay-out the pixel buffer in physical orientation.
        // For each logical (lx, ly), the rotated buffer index is
        // (lx * phys_w) + (height - 1 - ly).
        //
        // 2026-05-17 (second pass): use a static scratch buffer instead
        // of allocating a Vec per call. The bench-9-snapshot setup
        // draws ~10-14 icons per render through this path
        // (IconStrip + Wing + spectrum overlay); at 9 Hz that was
        // ~200 KiB/sec of alloc/free through the 64 KiB linked-list
        // heap. After a few seconds of operation the heap fragments
        // and a subsequent alloc panics — observed as a "lock up after
        // navigating into the wing menu, recover via reset button"
        // pattern.
        //
        // The scratch buffer is sized for the worst case we draw in
        // practice (icons up to 64×64). If a caller requests a larger
        // pixels rect, we fall back to allocating one Vec for that
        // specific call — large icons are rare and the alloc still
        // makes progress.
        const SCRATCH_PIXELS: usize = 64 * 64;
        // 2026-05-25 — Place SCRATCH in a named link section so consumers
        // can map it to RAM that is NOT adjacent to MSP stack growth.
        //
        // Why this matters: on Cortex-M with cortex-m-rt's default link.x,
        // MSP starts at `ORIGIN(RAM) + LENGTH(RAM)` and grows DOWN through
        // `.bss`. This 16 KiB array (= `[Color; 64*64]` = 16384 bytes) in
        // `.bss` ends up near MSP's growth path on boards with modest RAM.
        // On STM32H747I-DISCO (DTCM = 128 KiB), the first render frame's
        // call chain consumes ~14.5 KiB of MSP via the widget-tree walk,
        // putting SP INSIDE this array. Inner-loop writes to `scratch[N]`
        // for large enough N then overwrite the function's own stack
        // frame — including the slice's fat pointer — leading to seemingly
        // random hard faults at addresses that look unrelated to the bug.
        //
        // Consumers MUST add a SECTIONS block mapping `.rlvgl_blit_scratch`
        // to a non-stack-adjacent RAM region in their linker script.
        // Example for STM32H747 (place in D1 AXI SRAM):
        //
        //   SECTIONS {
        //     .rlvgl_blit_scratch (NOLOAD) : ALIGN(4) {
        //       *(.rlvgl_blit_scratch .rlvgl_blit_scratch.*)
        //     } > D1_CM7
        //   } INSERT AFTER .uninit;
        //
        // Diagnosed in disco-analyzer ERRATA-005 bench-45 (2026-05-25):
        // SCRATCH lived at DTCM 0x2001_A459..0x2001_E459; SP at fault was
        // 0x2001_C5F8 (= inside the array); the slice's fat pointer slot
        // at sp+176 = scratch[2196] was overwritten by an icon pixel and
        // the next iteration's load of scratch.data_ptr returned the
        // trampled bytes (0xFFFFFF59) — a precise data-bus error in the
        // PPB region followed. Per-iteration capture of scratch.as_ptr()
        // confirmed the corruption point matched the icon pixel pattern
        // byte-for-byte.
        // CRATES-CI-01: the ELF-style section name is only meaningful for
        // the firmware linker script above; on Mach-O hosts (macOS dev
        // boxes consuming rlvgl-platform from crates.io) it is rejected at
        // LLVM codegen ("mach-o section specifier requires a segment").
        // Gate the attribute to bare-metal targets so host builds place
        // SCRATCH in .bss as usual.
        #[cfg_attr(target_os = "none", unsafe(link_section = ".rlvgl_blit_scratch"))]
        static mut SCRATCH: [Color; SCRATCH_PIXELS] = [Color(0, 0, 0, 0); SCRATCH_PIXELS]; // rlvgl-discipline: allow(static_mut)

        let len = (width * height) as usize;
        if len <= SCRATCH_PIXELS {
            // SAFETY: single-core, single-threaded — `draw_pixels` is
            // called only from the main render loop and not from any
            // ISR. The scratch buffer is unique to this call site and
            // not borrowed across calls.
            let scratch = unsafe { &mut SCRATCH[..len] };
            // Clear only the prefix we'll actually fill; pixels that
            // aren't written (the `pixels.get(src_idx)` None branch)
            // retain their prior value, but we touch every index in
            // the inner loop so that's safe.
            for ly in 0..height as i32 {
                for lx in 0..width as i32 {
                    let src_idx = (ly as u32 * width + lx as u32) as usize;
                    let dst_idx = (lx as u32 * phys_w + (height as i32 - 1 - ly) as u32) as usize;
                    scratch[dst_idx] = pixels.get(src_idx).copied().unwrap_or(Color(0, 0, 0, 0));
                }
            }
            self.inner
                .draw_pixels((fb_x, fb_y), scratch, phys_w, phys_h);
        } else {
            // Oversize fallback: allocate once for this call. Rare.
            let mut rotated: alloc::vec::Vec<Color> = alloc::vec::Vec::with_capacity(len);
            rotated.resize(len, Color(0, 0, 0, 0));
            for ly in 0..height as i32 {
                for lx in 0..width as i32 {
                    let src_idx = (ly as u32 * width + lx as u32) as usize;
                    let dst_idx = (lx as u32 * phys_w + (height as i32 - 1 - ly) as u32) as usize;
                    if let Some(&c) = pixels.get(src_idx) {
                        rotated[dst_idx] = c;
                    }
                }
            }
            self.inner
                .draw_pixels((fb_x, fb_y), &rotated, phys_w, phys_h);
        }
    }

    fn submit(&mut self, list: &CommandList) {
        // Delegate to inner — preserves any backend-specific
        // optimizations (e.g. BlitterRenderer's occlusion pre-pass)
        // through the rotation wrapper.
        self.inner.submit(list);
    }
}

#[cfg(test)]
mod scratch_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;

    #[test]
    fn blit_colors_reuses_scratch_buffer() {
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        let pixels = [Color(0, 0, 0, 0)];
        renderer.blit_colors((0, 0), &pixels, 1, 1);
        let first_ptr = renderer.scratch.as_ref().unwrap().as_ptr();
        renderer.blit_colors((1, 1), &pixels, 1, 1);
        let second_ptr = renderer.scratch.as_ref().unwrap().as_ptr();
        assert_eq!(first_ptr, second_ptr);
    }
}

#[cfg(all(test, feature = "fontdue"))]
mod text_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;

    #[test]
    fn blitter_draws_text() {
        let mut buf = [0u8; 64 * 64 * 4];
        let surface = Surface::new(&mut buf, 64 * 4, PixelFmt::Argb8888, 64, 64);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        Renderer::draw_text(&mut renderer, (0, 32), "A", Color(255, 255, 255, 255));
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn cache_accounts_for_size() {
        let mut buf = [0u8; 64 * 64 * 4];
        let surface = Surface::new(&mut buf, 64 * 4, PixelFmt::Argb8888, 64, 64);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_text((0, 32), "Hi", Color(255, 255, 255, 255), FONT_DATA, 16.0);
        let len_after_small = renderer.glyph_cache.len();
        renderer.draw_text((0, 32), "Hi", Color(255, 255, 255, 255), FONT_DATA, 16.0);
        assert_eq!(len_after_small, renderer.glyph_cache.len());
        renderer.draw_text((0, 32), "Hi", Color(255, 255, 255, 255), FONT_DATA, 24.0);
        assert!(renderer.glyph_cache.len() > len_after_small);
    }
}

#[cfg(all(test, feature = "png", not(target_os = "none")))]
mod png_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use base64::Engine;
    use rlvgl_core::png::DecodingError;

    const RED_DOT_PNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC";

    #[test]
    fn blitter_draws_png() {
        let data = base64::engine::general_purpose::STANDARD
            .decode(RED_DOT_PNG)
            .unwrap();
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_png((0, 0), &data).unwrap();
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn blitter_rejects_invalid_png() {
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        let err = renderer.draw_png((0, 0), b"not a png").unwrap_err();
        assert!(matches!(err, DecodingError::Format(_)));
    }
}

#[cfg(all(test, feature = "jpeg", not(target_os = "none")))]
mod jpeg_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use base64::Engine;
    use rlvgl_core::jpeg::Error as JpegError;

    const RED_DOT_JPEG: &str = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAABAAEDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDi6KKK+ZP3E//Z";

    #[test]
    fn blitter_draws_jpeg() {
        let data = base64::engine::general_purpose::STANDARD
            .decode(RED_DOT_JPEG)
            .unwrap();
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_jpeg((0, 0), &data).unwrap();
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn blitter_rejects_invalid_jpeg() {
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        let err = renderer.draw_jpeg((0, 0), b"not a jpeg").unwrap_err();
        assert!(matches!(err, JpegError::Format(_)));
    }
}

#[cfg(all(test, feature = "gif"))]
mod gif_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use base64::Engine;
    use rlvgl_core::gif::DecodingError as GifDecodingError;

    const RED_DOT_GIF: &str = "R0lGODdhAQABAPAAAP8AAP///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==";

    #[test]
    fn blitter_draws_gif() {
        let data = base64::engine::general_purpose::STANDARD
            .decode(RED_DOT_GIF)
            .unwrap();
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_gif_frame((0, 0), &data, 0).unwrap();
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn blitter_rejects_invalid_gif() {
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        let err = renderer
            .draw_gif_frame((0, 0), b"not a gif", 0)
            .unwrap_err();
        assert!(matches!(err, GifDecodingError::Format(_)));
    }
}

#[cfg(all(test, feature = "apng"))]
mod apng_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use base64::Engine;

    const RED_DOT_APNG: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACGFjVEwAAAABAAAAALQt6aAAAAAaZmNUTAAAAAAAAAABAAAAAQAAAAAAAAAAAGQD6AEAqmVSjAAAAA1JREFUeJxj+M/A8B8ABQAB/4mZPR0AAAAASUVORK5CYII=";

    #[test]
    fn blitter_draws_apng() {
        let data = base64::engine::general_purpose::STANDARD
            .decode(RED_DOT_APNG)
            .unwrap();
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_apng_frame((0, 0), &data, 0).unwrap();
        assert!(buf.iter().any(|&p| p != 0));
    }
}

#[cfg(all(test, feature = "canvas"))]
mod canvas_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use embedded_graphics::prelude::Point;
    use rlvgl_core::canvas::Canvas;

    #[test]
    fn blitter_draws_canvas() {
        let mut canvas = Canvas::new(1, 1);
        canvas.draw_pixel(Point::new(0, 0), Color(255, 0, 0, 255));
        let mut buf = [0u8; 4];
        let surface = Surface::new(&mut buf, 4, PixelFmt::Argb8888, 1, 1);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_canvas((0, 0), &canvas);
        assert!(buf.iter().any(|&p| p != 0));
    }
}

#[cfg(all(test, feature = "qrcode", not(target_os = "none")))]
mod qrcode_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use rlvgl_core::qrcode::QrError;

    #[test]
    fn blitter_draws_qr() {
        let mut buf = [0u8; 64 * 64 * 4];
        let surface = Surface::new(&mut buf, 64 * 4, PixelFmt::Argb8888, 64, 64);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_qr((0, 0), b"hi").unwrap();
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn blitter_rejects_invalid_qr_data() {
        let mut buf = [0u8; 64 * 64 * 4];
        let surface = Surface::new(&mut buf, 64 * 4, PixelFmt::Argb8888, 64, 64);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        let data = vec![0u8; 3000];
        let err = renderer.draw_qr((0, 0), &data).unwrap_err();
        assert!(matches!(err, QrError::DataTooLong));
    }
}

#[cfg(all(test, feature = "lottie"))]
mod lottie_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;

    const SIMPLE_JSON: &str =
        "{\"v\":\"5.7\",\"fr\":30,\"ip\":0,\"op\":0,\"w\":1,\"h\":1,\"layers\":[]}";

    #[test]
    fn blitter_draws_lottie() {
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer
            .draw_lottie_frame((0, 0), SIMPLE_JSON, 0, 1, 1)
            .unwrap();
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn blitter_rejects_invalid_lottie() {
        let mut buf = [0u8; 4 * 4 * 4];
        let surface = Surface::new(&mut buf, 4 * 4, PixelFmt::Argb8888, 4, 4);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        assert!(
            renderer
                .draw_lottie_frame((0, 0), "not json", 0, 1, 1)
                .is_err()
        );
    }
}

#[cfg(all(test, feature = "pinyin", feature = "fontdue"))]
mod pinyin_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use rlvgl_core::pinyin::PinyinInputMethod;

    #[test]
    fn blitter_draws_pinyin() {
        let mut buf = [0u8; 64 * 64 * 4];
        let surface = Surface::new(&mut buf, 64 * 4, PixelFmt::Argb8888, 64, 64);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        let ime = PinyinInputMethod;
        assert!(renderer.draw_pinyin_candidates((0, 0), &ime, "zhong", Color(255, 255, 255, 255)));
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn pinyin_candidates_clipped_to_surface() {
        let mut buf = [0u8; 32 * 16 * 4];
        let surface = Surface::new(&mut buf, 32 * 4, PixelFmt::Argb8888, 32, 16);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        let ime = PinyinInputMethod;
        assert!(renderer.draw_pinyin_candidates((0, 0), &ime, "zhong", Color(255, 255, 255, 255)));

        let mut expected = [0u8; 32 * 16 * 4];
        let surface_e = Surface::new(&mut expected, 32 * 4, PixelFmt::Argb8888, 32, 16);
        let mut blit_e = CpuBlitter;
        let mut renderer_e: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit_e, surface_e);
        let chars = ime.candidates("zhong").unwrap();
        let text: alloc::string::String = chars.into_iter().collect();
        let clipped: alloc::string::String = text.chars().take(2).collect();
        Renderer::draw_text(&mut renderer_e, (0, 0), &clipped, Color(255, 255, 255, 255));
        assert_eq!(buf[..], expected[..]);
    }
}

#[cfg(all(test, feature = "fatfs", feature = "fontdue"))]
mod fatfs_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;
    use fatfs::{FileSystem, FormatVolumeOptions, FsOptions};
    use fscommon::BufStream;
    use std::io::{Cursor, Seek, SeekFrom, Write};

    #[test]
    fn blitter_draws_fatfs_listing() {
        let mut img = Cursor::new(vec![0u8; 1024 * 512]);
        fatfs::format_volume(&mut img, FormatVolumeOptions::new()).unwrap();
        img.seek(SeekFrom::Start(0)).unwrap();
        {
            let buf_stream = BufStream::new(&mut img);
            let fs = FileSystem::new(buf_stream, FsOptions::new()).unwrap();
            fs.root_dir()
                .create_file("foo.txt")
                .unwrap()
                .write_all(b"hi")
                .unwrap();
        }
        img.seek(SeekFrom::Start(0)).unwrap();
        let mut buf = [0u8; 64 * 64 * 4];
        let surface = Surface::new(&mut buf, 64 * 4, PixelFmt::Argb8888, 64, 64);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer
            .draw_fatfs_dir((0, 0), &mut img, "/", Color(255, 255, 255, 255))
            .unwrap();
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn fatfs_listing_clipped_to_surface() {
        let mut img = Cursor::new(vec![0u8; 1024 * 512]);
        fatfs::format_volume(&mut img, FormatVolumeOptions::new()).unwrap();
        img.seek(SeekFrom::Start(0)).unwrap();
        {
            let buf_stream = BufStream::new(&mut img);
            let fs = FileSystem::new(buf_stream, FsOptions::new()).unwrap();
            fs.root_dir().create_file("first_long_name.txt").unwrap();
            fs.root_dir().create_file("second_long_name.txt").unwrap();
            fs.root_dir().create_file("third_long_name.txt").unwrap();
        }
        img.seek(SeekFrom::Start(0)).unwrap();
        let image_vec = img.get_ref().clone();
        let mut img_expected = Cursor::new(image_vec.clone());
        let mut img_actual = Cursor::new(image_vec);

        let mut buf = [0u8; 32 * 32 * 4];
        let surface = Surface::new(&mut buf, 32 * 4, PixelFmt::Argb8888, 32, 32);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer
            .draw_fatfs_dir((0, 0), &mut img_actual, "/", Color(255, 255, 255, 255))
            .unwrap();

        let names = rlvgl_core::fatfs::list_dir(&mut img_expected, "/").unwrap();
        let mut expected = [0u8; 32 * 32 * 4];
        let surface_e = Surface::new(&mut expected, 32 * 4, PixelFmt::Argb8888, 32, 32);
        let mut blit_e = CpuBlitter;
        let mut renderer_e: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit_e, surface_e);
        for (i, name) in names.iter().take(2).enumerate() {
            let clipped: alloc::string::String = name.chars().take(2).collect();
            Renderer::draw_text(
                &mut renderer_e,
                (0, (i as i32) * 16),
                &clipped,
                Color(255, 255, 255, 255),
            );
        }
        assert_eq!(buf[..], expected[..]);
    }
}

#[cfg(all(test, feature = "nes"))]
mod nes_tests {
    use super::*;
    use crate::cpu_blitter::CpuBlitter;

    #[test]
    fn blitter_draws_nes_frame() {
        let pixels = [Color(255, 0, 0, 255)];
        let mut buf = [0u8; 4];
        let surface = Surface::new(&mut buf, 4, PixelFmt::Argb8888, 1, 1);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_nes_frame((0, 0), &pixels, 1, 1);
        assert!(buf.iter().any(|&p| p != 0));
    }

    #[test]
    fn blitter_draws_full_nes_frame() {
        let mut pixels = [Color(0, 0, 0, 255); 256 * 240];
        for y in 0..240 {
            for x in 0..256 {
                pixels[y * 256 + x] = Color(x as u8, y as u8, 0, 255);
            }
        }
        let mut buf = [0u8; 256 * 240 * 4];
        let surface = Surface::new(&mut buf, 256 * 4, PixelFmt::Argb8888, 256, 240);
        let mut blit = CpuBlitter;
        let mut renderer: BlitterRenderer<'_, CpuBlitter, 4> =
            BlitterRenderer::new(&mut blit, surface);
        renderer.draw_nes_frame((0, 0), &pixels, 256, 240);
        let x = 128usize;
        let y = 120usize;
        let idx = (y * 256 + x) * 4;
        let actual = u32::from_le_bytes(buf[idx..idx + 4].try_into().unwrap());
        let expected = Color(x as u8, y as u8, 0, 255).to_argb8888();
        assert_eq!(actual, expected);
    }
}