fret-core 0.1.0

Core contracts, IDs, geometry, events, and shared data types for the Fret framework.
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
use crate::{
    Px, SvgFit, ViewportFit,
    geometry::{Corners, Edges, Point, Rect, Transform2D},
    ids::{EffectId, ImageId, PathId, RenderTargetId, SvgId, TextBlobId},
};
use serde::{Deserialize, Serialize};
use slotmap::Key;

mod composite;
mod fingerprint;
mod image_object_fit;
mod mask;
mod paint;
mod replay;
mod shadow;
mod stroke;
mod validate;

pub use composite::{BlendMode, CompositeGroupDesc};
use fingerprint::mix_scene_op;
pub use image_object_fit::{ImageObjectFitMapped, map_image_object_fit};
pub use mask::Mask;
pub use paint::{
    ColorSpace, GradientStop, LinearGradient, MAX_STOPS, MaterialParams, Paint, PaintBindingV1,
    PaintEvalSpaceV1, RadialGradient, SweepGradient, TileMode,
};
pub use shadow::{ShadowRRectFallbackSpec, shadow_rrect_fallback_quads};
pub use stroke::{DashPatternV1, StrokeStyleV1};
pub use validate::{SceneValidationError, SceneValidationErrorKind};

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ImageSamplingHint {
    /// Renderer-chosen default (typically linear filtering for UI content).
    #[default]
    Default,
    Linear,
    Nearest,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DrawOrder(pub u32);

// `DrawOrder` is intentionally non-semantic for compositing. Scene operation order is authoritative.
// See `docs/adr/0081-draworder-is-non-semantic.md`.

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Color {
    pub const TRANSPARENT: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 0.0,
        a: 0.0,
    };

    /// Convert an sRGB `0xRRGGBB` hex color into a linear `Color` (alpha = 1.0).
    pub fn from_srgb_hex_rgb(hex: u32) -> Self {
        let r = ((hex >> 16) & 0xff) as u8;
        let g = ((hex >> 8) & 0xff) as u8;
        let b = (hex & 0xff) as u8;
        Self {
            r: srgb_u8_to_linear(r),
            g: srgb_u8_to_linear(g),
            b: srgb_u8_to_linear(b),
            a: 1.0,
        }
    }

    /// Convert a linear `Color` to an sRGB `0xRRGGBB` hex value (alpha ignored).
    pub fn to_srgb_hex_rgb(self) -> u32 {
        let r = linear_to_srgb_u8(self.r) as u32;
        let g = linear_to_srgb_u8(self.g) as u32;
        let b = linear_to_srgb_u8(self.b) as u32;
        (r << 16) | (g << 8) | b
    }
}

fn srgb_f32_to_linear(c: f32) -> f32 {
    if c <= 0.04045 {
        c / 12.92
    } else {
        ((c + 0.055) / 1.055).powf(2.4)
    }
}

fn linear_f32_to_srgb(c: f32) -> f32 {
    if c <= 0.0031308 {
        12.92 * c
    } else {
        1.055 * c.powf(1.0 / 2.4) - 0.055
    }
}

fn srgb_u8_to_linear(u: u8) -> f32 {
    srgb_f32_to_linear(u as f32 / 255.0)
}

fn linear_to_srgb_u8(c: f32) -> u8 {
    let srgb = linear_f32_to_srgb(c.clamp(0.0, 1.0)).clamp(0.0, 1.0);
    (srgb * 255.0).round() as u8
}

/// A bounded, portable text shadow surface (v1).
///
/// This is intentionally minimal (single layer, no blur) so it remains viable across wasm/mobile
/// backends. Higher-level shadow recipes (multi-layer elevation, blur, color management) remain
/// policy in ecosystem crates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextShadowV1 {
    /// Baseline-origin offset in logical pixels (pre-scale-factor).
    pub offset: Point,
    pub color: Color,
}

impl TextShadowV1 {
    pub const fn new(offset: Point, color: Color) -> Self {
        Self { offset, color }
    }
}

/// A bounded, portable text outline/stroke surface (v1).
///
/// This is intentionally minimal so it can be implemented deterministically across wasm/mobile
/// backends. More advanced strategies (e.g. SDF/MSDF atlases, multi-layer outlines) remain v2+.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextOutlineV1 {
    pub paint: PaintBindingV1,
    /// Outline width in logical pixels (pre-scale-factor).
    pub width_px: crate::Px,
}

impl TextOutlineV1 {
    pub const MAX_WIDTH_PX: crate::Px = crate::Px(8.0);

    pub fn sanitize(self) -> Option<Self> {
        if !self.width_px.0.is_finite() {
            return None;
        }
        let width_px = crate::Px(self.width_px.0.clamp(0.0, Self::MAX_WIDTH_PX.0));
        if width_px.0 <= 0.0 {
            return None;
        }
        Some(Self {
            paint: self.paint.sanitize(),
            width_px,
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EffectMode {
    /// Render children to an offscreen intermediate, then filter and composite the result.
    FilterContent,
    /// Sample already-rendered backdrop behind the group, filter it, then draw children on top.
    Backdrop,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EffectQuality {
    /// Renderer-chosen quality within budgets (ADR 0118).
    Auto,
    Low,
    Medium,
    High,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DitherMode {
    Bayer4x4,
}

/// Fixed-size custom effect parameters (v1).
///
/// This is intentionally a small, bounded payload for capability-gated custom effects.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EffectParamsV1 {
    pub vec4s: [[f32; 4]; 4],
}

impl EffectParamsV1 {
    pub const ZERO: Self = Self {
        vec4s: [[0.0; 4]; 4],
    };

    pub fn sanitize(self) -> Self {
        let mut out = self;
        for v in &mut out.vec4s {
            for x in v {
                if !x.is_finite() {
                    *x = 0.0;
                }
            }
        }
        out
    }

    pub fn is_finite(self) -> bool {
        self.vec4s.iter().flatten().all(|&x| x.is_finite())
    }
}

/// Bounded procedural noise parameters (v1).
///
/// This is a mechanism-level surface intended to enable authored “grain” layers that are useful
/// for acrylic/glass recipes (e.g. subtle noise after backdrop blur) while remaining deterministic
/// (no hidden time dependency).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NoiseV1 {
    /// Additive noise magnitude in linear space. Recommended range is ~[0, 0.1].
    pub strength: f32,
    /// Spatial scale for the noise field in logical pixels (pre-scale-factor).
    pub scale_px: crate::Px,
    /// Deterministic phase/seed value (no hidden time dependency).
    pub phase: f32,
}

impl NoiseV1 {
    pub const MAX_STRENGTH: f32 = 1.0;
    pub const MIN_SCALE_PX: crate::Px = crate::Px(1.0);
    pub const MAX_SCALE_PX: crate::Px = crate::Px(1024.0);

    pub fn sanitize(self) -> Self {
        let strength = if self.strength.is_finite() {
            self.strength.clamp(0.0, Self::MAX_STRENGTH)
        } else {
            0.0
        };
        let scale_px = if self.scale_px.0.is_finite() {
            crate::Px(
                self.scale_px
                    .0
                    .clamp(Self::MIN_SCALE_PX.0, Self::MAX_SCALE_PX.0),
            )
        } else {
            Self::MIN_SCALE_PX
        };
        let phase = if self.phase.is_finite() {
            self.phase
        } else {
            0.0
        };

        Self {
            strength,
            scale_px,
            phase,
        }
    }
}

/// Bounded backdrop warp parameters (v1).
///
/// This is a mechanism-level surface intended to enable refraction-like liquid glass effects by
/// sampling the already-rendered backdrop with a deterministic UV displacement. Higher-level
/// recipes (normal-map assets, interaction curves, multi-layer stacks) remain ecosystem policy.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BackdropWarpV1 {
    /// Displacement strength in logical pixels (pre-scale-factor).
    pub strength_px: crate::Px,
    /// Spatial scale for the warp field in logical pixels.
    pub scale_px: crate::Px,
    /// Deterministic phase/seed value (no hidden time dependency).
    pub phase: f32,
    /// Optional chromatic aberration magnitude in logical pixels.
    pub chromatic_aberration_px: crate::Px,
    pub kind: BackdropWarpKindV1,
}

impl BackdropWarpV1 {
    pub const MAX_STRENGTH_PX: crate::Px = crate::Px(24.0);
    pub const MIN_SCALE_PX: crate::Px = crate::Px(1.0);
    pub const MAX_SCALE_PX: crate::Px = crate::Px(1024.0);
    pub const MAX_CHROMATIC_ABERRATION_PX: crate::Px = crate::Px(8.0);

    pub fn sanitize(self) -> Self {
        let strength_px = if self.strength_px.0.is_finite() {
            crate::Px(self.strength_px.0.clamp(0.0, Self::MAX_STRENGTH_PX.0))
        } else {
            crate::Px(0.0)
        };

        let scale_px = if self.scale_px.0.is_finite() {
            crate::Px(
                self.scale_px
                    .0
                    .clamp(Self::MIN_SCALE_PX.0, Self::MAX_SCALE_PX.0),
            )
        } else {
            Self::MIN_SCALE_PX
        };

        let phase = if self.phase.is_finite() {
            self.phase
        } else {
            0.0
        };

        let chromatic_aberration_px = if self.chromatic_aberration_px.0.is_finite() {
            crate::Px(
                self.chromatic_aberration_px
                    .0
                    .clamp(0.0, Self::MAX_CHROMATIC_ABERRATION_PX.0),
            )
        } else {
            crate::Px(0.0)
        };

        Self {
            strength_px,
            scale_px,
            phase,
            chromatic_aberration_px,
            kind: self.kind,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WarpMapEncodingV1 {
    /// Decode displacement from RG in [0, 1] mapped to [-1, 1].
    ///
    /// This is a good default for authored displacement maps.
    RgSigned,
    /// Decode a normal from RGB in [0, 1] mapped to [-1, 1], and use XY as displacement.
    ///
    /// This is convenient when the warp field is stored as a normal map.
    NormalRgb,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BackdropWarpFieldV2 {
    /// Use the procedural v1 warp field (`BackdropWarpV1`).
    Procedural,
    /// Use an image-driven displacement/normal map as the warp field.
    ImageDisplacementMap {
        image: ImageId,
        uv: UvRect,
        sampling: ImageSamplingHint,
        encoding: WarpMapEncodingV1,
    },
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BackdropWarpV2 {
    /// The v1 parameters are retained as the portable base (and deterministic fallback).
    pub base: BackdropWarpV1,
    pub field: BackdropWarpFieldV2,
}

impl BackdropWarpV2 {
    pub fn sanitize(self) -> Self {
        let base = self.base.sanitize();
        let field = match self.field {
            BackdropWarpFieldV2::Procedural => BackdropWarpFieldV2::Procedural,
            BackdropWarpFieldV2::ImageDisplacementMap {
                image,
                uv,
                sampling,
                encoding,
            } => {
                let uv = if uv.u0.is_finite()
                    && uv.v0.is_finite()
                    && uv.u1.is_finite()
                    && uv.v1.is_finite()
                {
                    uv
                } else {
                    UvRect::FULL
                };
                BackdropWarpFieldV2::ImageDisplacementMap {
                    image,
                    uv,
                    sampling,
                    encoding,
                }
            }
        };

        Self { base, field }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackdropWarpKindV1 {
    Wave,
    /// Reserved for a lens-like warp in a future v1.x/v2. Renderers may treat this as `Wave`.
    LensReserved,
}

/// Bounded drop shadow parameters (v1).
///
/// This is a mechanism-level, blur-based shadow surface intended for general UI content (cards,
/// popovers, overlays). It is explicitly bounded and deterministic so it remains viable on
/// wasm/WebGPU and mobile GPUs.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DropShadowV1 {
    /// Shadow offset in logical pixels (pre-scale-factor).
    pub offset_px: Point,
    /// Blur radius in logical pixels (pre-scale-factor).
    pub blur_radius_px: crate::Px,
    /// Downsample hint (1–4). Renderers may degrade deterministically under budgets.
    pub downsample: u32,
    /// Solid shadow color (unpremultiplied RGBA in [0, 1]).
    pub color: Color,
}

impl DropShadowV1 {
    pub const MAX_BLUR_RADIUS_PX: crate::Px = crate::Px(64.0);

    pub fn sanitize(self) -> Self {
        let offset_px = Point::new(
            crate::Px(if self.offset_px.x.0.is_finite() {
                self.offset_px.x.0
            } else {
                0.0
            }),
            crate::Px(if self.offset_px.y.0.is_finite() {
                self.offset_px.y.0
            } else {
                0.0
            }),
        );
        let blur_radius_px = if self.blur_radius_px.0.is_finite() {
            crate::Px(self.blur_radius_px.0.clamp(0.0, Self::MAX_BLUR_RADIUS_PX.0))
        } else {
            crate::Px(0.0)
        };
        let downsample = self.downsample.clamp(1, 4);
        let color = Color {
            r: if self.color.r.is_finite() {
                self.color.r.clamp(0.0, 1.0)
            } else {
                0.0
            },
            g: if self.color.g.is_finite() {
                self.color.g.clamp(0.0, 1.0)
            } else {
                0.0
            },
            b: if self.color.b.is_finite() {
                self.color.b.clamp(0.0, 1.0)
            } else {
                0.0
            },
            a: if self.color.a.is_finite() {
                self.color.a.clamp(0.0, 1.0)
            } else {
                0.0
            },
        };

        Self {
            offset_px,
            blur_radius_px,
            downsample,
            color,
        }
    }
}

/// Optional user image input for bounded custom effects (v2).
///
/// This is intentionally small and portable: it references an `ImageId` registered through the
/// existing image service and uses the existing `ImageSamplingHint` vocabulary.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CustomEffectImageInputV1 {
    pub image: ImageId,
    pub uv: UvRect,
    pub sampling: ImageSamplingHint,
}

impl CustomEffectImageInputV1 {
    pub const fn new(image: ImageId) -> Self {
        Self {
            image,
            uv: UvRect::FULL,
            sampling: ImageSamplingHint::Default,
        }
    }
}

/// Custom effect source selection for CustomV3.
///
/// This stays bounded and deterministic: callers can request a distinct `src_raw` source and an
/// optional bounded pyramid, but backends may degrade by aliasing sources under budgets or
/// unsupported capabilities.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct CustomEffectSourcesV3 {
    pub want_raw: bool,
    pub pyramid: Option<CustomEffectPyramidRequestV1>,
}

/// Bounded request for a renderer-owned blur pyramid derived from `src_raw` (v3).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CustomEffectPyramidRequestV1 {
    pub max_levels: u8,
    pub max_radius_px: crate::Px,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EffectStep {
    GaussianBlur {
        radius_px: crate::Px,
        downsample: u32,
    },
    DropShadowV1(DropShadowV1),
    BackdropWarpV1(BackdropWarpV1),
    BackdropWarpV2(BackdropWarpV2),
    NoiseV1(NoiseV1),
    ColorAdjust {
        saturation: f32,
        brightness: f32,
        contrast: f32,
    },
    ColorMatrix {
        m: [f32; 20],
    },
    AlphaThreshold {
        cutoff: f32,
        soft: f32,
    },
    Pixelate {
        scale: u32,
    },
    Dither {
        mode: DitherMode,
    },
    CustomV1 {
        id: EffectId,
        params: EffectParamsV1,
        /// Maximum sampling offset (in logical px) that the custom effect may use when reading
        /// from its source texture.
        ///
        /// This is a bounded contract input used by renderers to deterministically allocate
        /// enough context ("padding") for effect chains (e.g. blur -> custom refraction) without
        /// introducing edge artifacts near clipped/scissored bounds.
        ///
        /// Backends may clamp or degrade behavior under tight budgets.
        max_sample_offset_px: crate::Px,
    },
    CustomV2 {
        id: EffectId,
        params: EffectParamsV1,
        /// Maximum sampling offset (in logical px) that the custom effect may use when reading
        /// from its source texture.
        ///
        /// This preserves the deterministic chain padding story from v1.
        max_sample_offset_px: crate::Px,
        /// Optional user-provided image input (v2 ceiling bump).
        input_image: Option<CustomEffectImageInputV1>,
    },
    CustomV3 {
        id: EffectId,
        params: EffectParamsV1,
        /// Maximum sampling offset (in logical px) that the custom effect may use when reading
        /// from its source textures.
        ///
        /// This preserves the deterministic chain padding story from v1/v2.
        max_sample_offset_px: crate::Px,
        /// Optional user-provided image input 0 (v2-compatible).
        user0: Option<CustomEffectImageInputV1>,
        /// Optional user-provided image input 1 (v3 ceiling bump).
        user1: Option<CustomEffectImageInputV1>,
        /// Renderer-provided sources request (raw + optional pyramid).
        sources: CustomEffectSourcesV3,
    },
}

impl EffectStep {
    pub fn sanitize(self) -> Self {
        match self {
            EffectStep::ColorMatrix { mut m } => {
                for v in &mut m {
                    if !v.is_finite() {
                        *v = 0.0;
                    }
                }
                EffectStep::ColorMatrix { m }
            }
            EffectStep::BackdropWarpV1(w) => EffectStep::BackdropWarpV1(w.sanitize()),
            EffectStep::BackdropWarpV2(w) => EffectStep::BackdropWarpV2(w.sanitize()),
            EffectStep::DropShadowV1(s) => EffectStep::DropShadowV1(s.sanitize()),
            EffectStep::NoiseV1(n) => EffectStep::NoiseV1(n.sanitize()),
            EffectStep::CustomV1 {
                id,
                params,
                max_sample_offset_px,
            } => EffectStep::CustomV1 {
                id,
                params: params.sanitize(),
                max_sample_offset_px: if max_sample_offset_px.0.is_finite() {
                    crate::Px(max_sample_offset_px.0.max(0.0))
                } else {
                    crate::Px(0.0)
                },
            },
            EffectStep::CustomV2 {
                id,
                params,
                max_sample_offset_px,
                input_image,
            } => EffectStep::CustomV2 {
                id,
                params: params.sanitize(),
                max_sample_offset_px: if max_sample_offset_px.0.is_finite() {
                    crate::Px(max_sample_offset_px.0.max(0.0))
                } else {
                    crate::Px(0.0)
                },
                input_image: input_image.map(|mut input| {
                    if !input.uv.u0.is_finite() {
                        input.uv.u0 = 0.0;
                    }
                    if !input.uv.v0.is_finite() {
                        input.uv.v0 = 0.0;
                    }
                    if !input.uv.u1.is_finite() {
                        input.uv.u1 = 1.0;
                    }
                    if !input.uv.v1.is_finite() {
                        input.uv.v1 = 1.0;
                    }
                    input
                }),
            },
            EffectStep::CustomV3 {
                id,
                params,
                max_sample_offset_px,
                user0,
                user1,
                mut sources,
            } => {
                let sanitize_input = |input: Option<CustomEffectImageInputV1>| {
                    input.map(|mut input| {
                        if !input.uv.u0.is_finite() {
                            input.uv.u0 = 0.0;
                        }
                        if !input.uv.v0.is_finite() {
                            input.uv.v0 = 0.0;
                        }
                        if !input.uv.u1.is_finite() {
                            input.uv.u1 = 1.0;
                        }
                        if !input.uv.v1.is_finite() {
                            input.uv.v1 = 1.0;
                        }
                        input
                    })
                };

                sources.pyramid = sources.pyramid.map(|req| {
                    let max_levels = req.max_levels.clamp(1, 7);
                    let max_radius_px = if req.max_radius_px.0.is_finite() {
                        crate::Px(req.max_radius_px.0.max(0.0))
                    } else {
                        crate::Px(0.0)
                    };
                    CustomEffectPyramidRequestV1 {
                        max_levels,
                        max_radius_px,
                    }
                });

                EffectStep::CustomV3 {
                    id,
                    params: params.sanitize(),
                    max_sample_offset_px: if max_sample_offset_px.0.is_finite() {
                        crate::Px(max_sample_offset_px.0.max(0.0))
                    } else {
                        crate::Px(0.0)
                    },
                    user0: sanitize_input(user0),
                    user1: sanitize_input(user1),
                    sources,
                }
            }
            EffectStep::AlphaThreshold { cutoff, soft } => EffectStep::AlphaThreshold {
                cutoff: if cutoff.is_finite() { cutoff } else { 0.0 },
                soft: if soft.is_finite() { soft.max(0.0) } else { 0.0 },
            },
            other => other,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EffectChain {
    steps: [Option<EffectStep>; 4],
}

impl EffectChain {
    pub const MAX_STEPS: usize = 4;
    pub const EMPTY: Self = Self {
        steps: [None, None, None, None],
    };

    pub fn from_steps(steps: &[EffectStep]) -> Self {
        assert!(
            steps.len() <= Self::MAX_STEPS,
            "EffectChain supports up to {} steps",
            Self::MAX_STEPS
        );
        let mut out = Self::EMPTY;
        for (idx, step) in steps.iter().copied().enumerate() {
            out.steps[idx] = Some(step);
        }
        out
    }

    pub fn is_empty(&self) -> bool {
        self.steps.iter().all(|s| s.is_none())
    }

    pub fn iter(&self) -> impl Iterator<Item = EffectStep> + '_ {
        self.steps.iter().copied().flatten()
    }

    pub fn sanitize(self) -> Self {
        let mut out = self;
        for step in &mut out.steps {
            *step = step.map(EffectStep::sanitize);
        }
        out
    }
}

impl Default for EffectChain {
    fn default() -> Self {
        Self::EMPTY
    }
}

#[derive(Debug, Default, Clone)]
pub struct SceneRecording {
    ops: Vec<SceneOp>,
    fingerprint: u64,
    #[cfg(debug_assertions)]
    storage_swapped_since_clear: bool,
}

pub type Scene = SceneRecording;

impl SceneRecording {
    pub fn clear(&mut self) {
        self.ops.clear();
        self.fingerprint = 0;
        #[cfg(debug_assertions)]
        {
            self.storage_swapped_since_clear = false;
        }
    }

    pub fn push(&mut self, op: SceneOp) {
        // Clamp quad corner radii to the local rect size (CSS-style effective border radius).
        //
        // Browsers constrain border radii to half the corresponding box dimension. Many shadcn
        // components use `rounded-full`, which maps to an arbitrarily large radius that becomes
        // `min(width, height) / 2` in practice. Keeping this normalization at the scene layer makes
        // renderer backends and scripted tests agree on the effective shape.
        let op = match op {
            SceneOp::Quad {
                order,
                rect,
                background,
                border,
                border_paint,
                mut corner_radii,
            } => {
                let max = rect.size.width.0.min(rect.size.height.0) * 0.5;
                let max = if max.is_finite() { max.max(0.0) } else { 0.0 };
                corner_radii.top_left = Px(corner_radii.top_left.0.min(max));
                corner_radii.top_right = Px(corner_radii.top_right.0.min(max));
                corner_radii.bottom_left = Px(corner_radii.bottom_left.0.min(max));
                corner_radii.bottom_right = Px(corner_radii.bottom_right.0.min(max));

                SceneOp::Quad {
                    order,
                    rect,
                    background: background.sanitize(),
                    border,
                    border_paint: border_paint.sanitize(),
                    corner_radii,
                }
            }
            SceneOp::ShadowRRect {
                order,
                rect,
                mut corner_radii,
                offset,
                spread,
                blur_radius,
                color,
            } => {
                let max = rect.size.width.0.min(rect.size.height.0) * 0.5;
                let max = if max.is_finite() { max.max(0.0) } else { 0.0 };
                corner_radii.top_left = Px(corner_radii.top_left.0.max(0.0).min(max));
                corner_radii.top_right = Px(corner_radii.top_right.0.max(0.0).min(max));
                corner_radii.bottom_left = Px(corner_radii.bottom_left.0.max(0.0).min(max));
                corner_radii.bottom_right = Px(corner_radii.bottom_right.0.max(0.0).min(max));

                let blur_radius = if blur_radius.0.is_finite() {
                    Px(blur_radius
                        .0
                        .clamp(0.0, SHADOW_RRECT_V1_MAX_BLUR_RADIUS_PX.0))
                } else {
                    Px(0.0)
                };

                SceneOp::ShadowRRect {
                    order,
                    rect,
                    corner_radii,
                    offset,
                    spread,
                    blur_radius,
                    color,
                }
            }
            SceneOp::PushEffect {
                bounds,
                mode,
                chain,
                quality,
            } => SceneOp::PushEffect {
                bounds,
                mode,
                chain: chain.sanitize(),
                quality,
            },
            SceneOp::Text {
                order,
                origin,
                text,
                paint,
                outline,
                shadow,
            } => SceneOp::Text {
                order,
                origin,
                text,
                paint: paint.sanitize(),
                outline: outline.and_then(|o| o.sanitize()),
                shadow,
            },
            other => other,
        };

        self.fingerprint = mix_scene_op(self.fingerprint, op);
        self.ops.push(op);
    }

    pub fn with_transform<T>(
        &mut self,
        transform: Transform2D,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.push(SceneOp::PushTransform { transform });
        let out = f(self);
        self.push(SceneOp::PopTransform);
        out
    }

    pub fn with_opacity<T>(&mut self, opacity: f32, f: impl FnOnce(&mut Self) -> T) -> T {
        self.push(SceneOp::PushOpacity { opacity });
        let out = f(self);
        self.push(SceneOp::PopOpacity);
        out
    }

    pub fn with_layer<T>(&mut self, layer: u32, f: impl FnOnce(&mut Self) -> T) -> T {
        self.push(SceneOp::PushLayer { layer });
        let out = f(self);
        self.push(SceneOp::PopLayer);
        out
    }

    pub fn with_clip_rect<T>(&mut self, rect: Rect, f: impl FnOnce(&mut Self) -> T) -> T {
        self.push(SceneOp::PushClipRect { rect });
        let out = f(self);
        self.push(SceneOp::PopClip);
        out
    }

    pub fn with_clip_rrect<T>(
        &mut self,
        rect: Rect,
        corner_radii: Corners,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.push(SceneOp::PushClipRRect { rect, corner_radii });
        let out = f(self);
        self.push(SceneOp::PopClip);
        out
    }

    pub fn with_clip_path<T>(
        &mut self,
        bounds: Rect,
        origin: Point,
        path: PathId,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.push(SceneOp::PushClipPath {
            bounds,
            origin,
            path,
        });
        let out = f(self);
        self.push(SceneOp::PopClip);
        out
    }

    pub fn with_mask<T>(&mut self, bounds: Rect, mask: Mask, f: impl FnOnce(&mut Self) -> T) -> T {
        self.push(SceneOp::PushMask { bounds, mask });
        let out = f(self);
        self.push(SceneOp::PopMask);
        out
    }

    pub fn with_effect<T>(
        &mut self,
        bounds: Rect,
        mode: EffectMode,
        chain: EffectChain,
        quality: EffectQuality,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.push(SceneOp::PushEffect {
            bounds,
            mode,
            chain,
            quality,
        });
        let out = f(self);
        self.push(SceneOp::PopEffect);
        out
    }

    pub fn with_composite_group<T>(
        &mut self,
        desc: CompositeGroupDesc,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.push(SceneOp::PushCompositeGroup { desc });
        let out = f(self);
        self.push(SceneOp::PopCompositeGroup);
        out
    }

    pub fn with_backdrop_source_group_v1<T>(
        &mut self,
        bounds: Rect,
        pyramid: Option<CustomEffectPyramidRequestV1>,
        quality: EffectQuality,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.push(SceneOp::PushBackdropSourceGroupV1 {
            bounds,
            pyramid,
            quality,
        });
        let out = f(self);
        self.push(SceneOp::PopBackdropSourceGroup);
        out
    }

    pub fn ops(&self) -> &[SceneOp] {
        &self.ops
    }

    pub fn ops_len(&self) -> usize {
        self.ops.len()
    }

    /// Swap the internal op storage with an external buffer.
    ///
    /// This is a performance-oriented API used by subsystems like the UI paint cache to "take"
    /// the previous frame's ops without copying.
    ///
    /// In debug builds, this asserts if called more than once without an intervening `clear()`,
    /// because repeated swaps typically indicate multiple paint-cache ingestions from the same scene.
    pub fn swap_storage(&mut self, other_ops: &mut Vec<SceneOp>, other_fingerprint: &mut u64) {
        #[cfg(debug_assertions)]
        debug_assert!(
            !self.storage_swapped_since_clear,
            "Scene::swap_storage() was called more than once without an intervening Scene::clear(); \
this is not supported because swap_storage() is destructive and typically indicates multiple paint-cache ingestions"
        );
        std::mem::swap(&mut self.ops, other_ops);
        std::mem::swap(&mut self.fingerprint, other_fingerprint);
        #[cfg(debug_assertions)]
        {
            self.storage_swapped_since_clear = true;
        }
    }

    pub fn fingerprint(&self) -> u64 {
        self.fingerprint
    }
}

#[derive(Debug, Clone, Copy)]
pub enum SceneOp {
    PushTransform {
        transform: Transform2D,
    },
    PopTransform,

    /// Opacity multiplier applied to subsequent draw ops.
    ///
    /// The opacity stack composes multiplicatively (parent * child).
    PushOpacity {
        opacity: f32,
    },
    PopOpacity,

    /// Reserved layer stack marker (ADR 0019).
    PushLayer {
        layer: u32,
    },
    PopLayer,

    PushClipRect {
        rect: Rect,
    },
    PushClipRRect {
        rect: Rect,
        corner_radii: Corners,
    },
    /// Push a path-based clip entry (clip-path).
    ///
    /// `bounds` is a computation bound (not an implicit clip) used to bound GPU work and enable
    /// deterministic budgeting/degradation. The clip geometry is given by a prepared path handle.
    ///
    /// v1 note: renderers may implement this via an offscreen intermediate + mask composite.
    PushClipPath {
        bounds: Rect,
        origin: Point,
        path: PathId,
    },
    PopClip,

    PushMask {
        /// Computation bounds (not an implicit clip), see ADR 0239.
        bounds: Rect,
        mask: Mask,
    },
    PopMask,

    PushEffect {
        /// Computation bounds (not an implicit clip), see ADR 0117.
        bounds: Rect,
        mode: EffectMode,
        chain: EffectChain,
        quality: EffectQuality,
    },
    PopEffect,

    /// Backdrop source group (v1): a mechanism-level scope that enables renderers to share a raw
    /// backdrop snapshot (and optional pyramid) across multiple CustomV3 “liquid glass” surfaces.
    ///
    /// `bounds` are computation bounds (not an implicit clip). `pyramid` is an optional bounded
    /// request for a shared renderer-owned blur pyramid derived from the group snapshot.
    ///
    /// See ADR 0302.
    PushBackdropSourceGroupV1 {
        /// Computation bounds (not an implicit clip).
        bounds: Rect,
        /// Optional bounded pyramid request shared by the group (upper bound).
        pyramid: Option<CustomEffectPyramidRequestV1>,
        /// Quality hint used for deterministic budgeting/degradation.
        quality: EffectQuality,
    },
    PopBackdropSourceGroup,

    PushCompositeGroup {
        desc: CompositeGroupDesc,
    },
    PopCompositeGroup,

    Quad {
        order: DrawOrder,
        rect: Rect,
        background: PaintBindingV1,
        border: Edges,
        border_paint: PaintBindingV1,
        corner_radii: Corners,
    },

    StrokeRRect {
        order: DrawOrder,
        rect: Rect,
        stroke: Edges,
        stroke_paint: PaintBindingV1,
        corner_radii: Corners,
        style: StrokeStyleV1,
    },

    /// Draw a single rounded-rect box shadow layer.
    ///
    /// This is a first-class geometric shadow primitive for container chrome. Unlike
    /// `EffectStep::DropShadowV1`, it is not content-derived and does not require a FilterContent
    /// intermediate.
    ShadowRRect {
        order: DrawOrder,
        rect: Rect,
        corner_radii: Corners,
        offset: Point,
        spread: Px,
        blur_radius: Px,
        color: Color,
    },

    Image {
        order: DrawOrder,
        rect: Rect,
        image: ImageId,
        fit: ViewportFit,
        sampling: ImageSamplingHint,
        opacity: f32,
    },

    ImageRegion {
        order: DrawOrder,
        rect: Rect,
        image: ImageId,
        uv: UvRect,
        sampling: ImageSamplingHint,
        opacity: f32,
    },

    /// Draw an alpha mask image tinted with a solid color.
    ///
    /// The referenced `image` is expected to store coverage in the red channel (e.g. `R8Unorm`).
    MaskImage {
        order: DrawOrder,
        rect: Rect,
        image: ImageId,
        uv: UvRect,
        sampling: ImageSamplingHint,
        color: Color,
        opacity: f32,
    },

    /// Draw an SVG as a monochrome icon: rasterize to an alpha mask, then tint with a solid color.
    SvgMaskIcon {
        order: DrawOrder,
        rect: Rect,
        svg: SvgId,
        fit: SvgFit,
        color: Color,
        opacity: f32,
    },

    /// Draw an SVG as an RGBA image: rasterize and upload as an image texture.
    SvgImage {
        order: DrawOrder,
        rect: Rect,
        svg: SvgId,
        fit: SvgFit,
        opacity: f32,
    },

    Text {
        order: DrawOrder,
        origin: Point,
        text: TextBlobId,
        paint: PaintBindingV1,
        outline: Option<TextOutlineV1>,
        shadow: Option<TextShadowV1>,
    },

    Path {
        order: DrawOrder,
        origin: Point,
        path: PathId,
        paint: PaintBindingV1,
    },

    ViewportSurface {
        order: DrawOrder,
        rect: Rect,
        target: RenderTargetId,
        opacity: f32,
    },
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct UvRect {
    pub u0: f32,
    pub v0: f32,
    pub u1: f32,
    pub v1: f32,
}

impl UvRect {
    pub const FULL: Self = Self {
        u0: 0.0,
        v0: 0.0,
        u1: 1.0,
        v1: 1.0,
    };
}

pub const SHADOW_RRECT_V1_MAX_BLUR_RADIUS_PX: crate::Px = crate::Px(64.0);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::{Px, Size};

    #[test]
    fn replay_ops_translated_wraps_in_transform_stack() {
        let ops = [SceneOp::Quad {
            order: DrawOrder(0),
            rect: Rect::new(Point::new(Px(0.0), Px(0.0)), Size::new(Px(10.0), Px(10.0))),
            background: Paint::Solid(Color::TRANSPARENT).into(),
            border: Edges::all(Px(0.0)),
            border_paint: Paint::Solid(Color::TRANSPARENT).into(),
            corner_radii: Corners::all(Px(0.0)),
        }];

        let mut scene = Scene::default();
        scene.replay_ops_translated(&ops, Point::new(Px(2.0), Px(3.0)));

        assert_eq!(scene.ops_len(), 3);
        assert!(matches!(scene.ops()[0], SceneOp::PushTransform { .. }));
        assert!(matches!(scene.ops()[1], SceneOp::Quad { .. }));
        assert!(matches!(scene.ops()[2], SceneOp::PopTransform));
    }

    #[test]
    fn validate_rejects_transform_underflow() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PopTransform);
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::TransformUnderflow,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_unbalanced_clip_stack() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PushClipRect {
            rect: Rect::new(Point::new(Px(0.0), Px(0.0)), Size::new(Px(1.0), Px(1.0))),
        });
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::UnbalancedClipStack { remaining: 1 },
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_effect_underflow() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PopEffect);
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::EffectUnderflow,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_unbalanced_effect_stack() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PushEffect {
            bounds: Rect::new(Point::new(Px(0.0), Px(0.0)), Size::new(Px(1.0), Px(1.0))),
            mode: EffectMode::Backdrop,
            chain: EffectChain::from_steps(&[EffectStep::GaussianBlur {
                radius_px: Px(2.0),
                downsample: 2,
            }]),
            quality: EffectQuality::Auto,
        });
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::UnbalancedEffectStack { remaining: 1 },
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_opacity_underflow() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PopOpacity);
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::OpacityUnderflow,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_layer_underflow() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PopLayer);
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::LayerUnderflow,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_clip_underflow() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PopClip);
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::ClipUnderflow,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_nonfinite_opacity() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PushOpacity { opacity: f32::NAN });
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::NonFiniteOpacity,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_nonfinite_transform() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PushTransform {
            transform: Transform2D {
                a: f32::NAN,
                ..Transform2D::IDENTITY
            },
        });
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::NonFiniteTransform,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_nonfinite_draw_op_data() {
        let mut scene = Scene::default();
        scene.push(SceneOp::Quad {
            order: DrawOrder(0),
            rect: Rect::new(
                Point::new(Px(f32::NAN), Px(0.0)),
                Size::new(Px(10.0), Px(10.0)),
            ),
            background: Paint::Solid(Color::TRANSPARENT).into(),
            border: Edges::all(Px(0.0)),
            border_paint: Paint::Solid(Color::TRANSPARENT).into(),
            corner_radii: Corners::all(Px(0.0)),
        });
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::NonFiniteOpData,
                ..
            })
        ));
    }

    #[test]
    fn push_shadow_rrect_clamps_blur_and_preserves_base_corner_radii() {
        let mut scene = Scene::default();
        scene.push(SceneOp::ShadowRRect {
            order: DrawOrder(0),
            rect: Rect::new(Point::new(Px(0.0), Px(0.0)), Size::new(Px(20.0), Px(12.0))),
            corner_radii: Corners::all(Px(9999.0)),
            offset: Point::new(Px(0.0), Px(4.0)),
            spread: Px(-4.0),
            blur_radius: Px(4096.0),
            color: Color::TRANSPARENT,
        });

        let SceneOp::ShadowRRect {
            corner_radii,
            blur_radius,
            ..
        } = scene.ops()[0]
        else {
            panic!("expected shadow rrect");
        };

        assert_eq!(blur_radius, SHADOW_RRECT_V1_MAX_BLUR_RADIUS_PX);
        assert_eq!(corner_radii, Corners::all(Px(6.0)));
    }

    #[test]
    fn validate_rejects_nonfinite_shadow_rrect_data() {
        let mut scene = Scene::default();
        scene.push(SceneOp::ShadowRRect {
            order: DrawOrder(0),
            rect: Rect::new(Point::new(Px(0.0), Px(0.0)), Size::new(Px(10.0), Px(10.0))),
            corner_radii: Corners::all(Px(4.0)),
            offset: Point::new(Px(f32::NAN), Px(0.0)),
            spread: Px(0.0),
            blur_radius: Px(8.0),
            color: Color::TRANSPARENT,
        });

        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::NonFiniteOpData,
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_unbalanced_opacity_stack() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PushOpacity { opacity: 0.5 });
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::UnbalancedOpacityStack { remaining: 1 },
                ..
            })
        ));
    }

    #[test]
    fn validate_rejects_unbalanced_layer_stack() {
        let mut scene = Scene::default();
        scene.push(SceneOp::PushLayer { layer: 1 });
        assert!(matches!(
            scene.validate(),
            Err(SceneValidationError {
                kind: SceneValidationErrorKind::UnbalancedLayerStack { remaining: 1 },
                ..
            })
        ));
    }
}