repose-render-wgpu 0.31.21

Repose wgpu handler
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
//! Headless offscreen probes for `VectorMesh` rendering.
//!
//! Skips (does not fail) without a WGPU adapter, so adapter-less CI stays
//! green. Catches silent mesh-pipeline regressions: a mesh scene must produce
//! covered pixels.

use repose_core::{
    BlendMode, Brush, Color, PaintDesc, Px, Rect, Scene, SceneNode, Transform, VectorMeshData,
    VectorVertex,
};
use repose_render_wgpu::offscreen::OffscreenRenderer;
use std::sync::Arc;

fn quad(x0: f32, y0: f32, x1: f32, y1: f32, color: [f32; 4]) -> Arc<VectorMeshData> {
    let vertices: Arc<[VectorVertex]> = [
        ([x0, y0], [0.0, 0.0]),
        ([x1, y0], [1.0, 0.0]),
        ([x1, y1], [1.0, 1.0]),
        ([x0, y1], [0.0, 1.0]),
    ]
    .iter()
    .map(|(pos, uv)| VectorVertex {
        pos: *pos,
        color,
        uv: *uv,
    })
    .collect();
    Arc::new(VectorMeshData {
        vertices,
        indices: [0u32, 1, 2, 0, 2, 3].into(),
    })
}

fn try_offscreen(w: u32, h: u32) -> Option<OffscreenRenderer> {
    match OffscreenRenderer::new_blocking(w, h, 1) {
        Ok(o) => Some(o),
        Err(e) => {
            eprintln!("SKIP: no WGPU adapter for mesh offscreen probe ({e:#})");
            None
        }
    }
}

fn covered_of(nodes: Vec<SceneNode>) -> Option<Vec<u8>> {
    let Some(mut off) = try_offscreen(64, 64) else {
        return None;
    };
    let scene = Scene {
        clear_color: Color::from_rgba(0, 0, 0, 0),
        nodes,
    };
    Some(off.render_rgba(&scene, None).expect("render"))
}

fn ink_bbox(px: &[u8]) -> Option<(u32, u32, u32, u32)> {
    let (mut x0, mut y0, mut x1, mut y1) = (64u32, 64u32, 0u32, 0u32);
    for (i, p) in px.as_chunks::<4>().0.iter().enumerate() {
        if p[3] > 8 {
            let (x, y) = ((i % 64) as u32, (i / 64) as u32);
            x0 = x0.min(x);
            y0 = y0.min(y);
            x1 = x1.max(x);
            y1 = y1.max(y);
        }
    }
    (x1 >= x0).then_some((x0, y0, x1, y1))
}

fn white_rect(x: f32, y: f32, w: f32, h: f32) -> SceneNode {
    SceneNode::Rect {
        rect: Rect { x, y, w, h },
        brush: repose_core::Brush::Solid(Color::from_rgba(255, 255, 255, 255)),
        radius: [Px::ZERO; 4],
    }
}

#[test]
fn bisect_layer_coordinate_space() {
    // Layers expect layer-local children (the established producer
    // contract: repose-ui pushes a `-rect` shift after `BeginLayer`).
    // A world-space child double-offsets to (20,20); a layer-local child
    // composites exactly at the layer origin (10,10).
    for (label, cx, cy, expect) in [
        ("world-child", 10.0f32, 10.0f32, (20, 20, 39, 39)),
        ("local-child", 0.0f32, 0.0f32, (10, 10, 29, 29)),
    ] {
        let Some(px) = covered_of(vec![
            SceneNode::BeginLayer {
                rect: Rect {
                    x: 10.0,
                    y: 10.0,
                    w: 44.0,
                    h: 44.0,
                },
                layer_id: 7,
                alpha: 1.0,
                blur_radius_x: Px(0.0),
                blur_radius_y: Px(0.0),
                rectangle_edge: true,
            },
            white_rect(cx, cy, 20.0, 20.0),
            SceneNode::EndLayer { layer_id: 7 },
        ]) else {
            return;
        };
        assert_eq!(ink_bbox(&px), Some(expect), "{label} landed wrong");
    }
}

#[test]
fn vector_mesh_covers_pixels_offscreen() {
    let Some(mut off) = try_offscreen(64, 64) else {
        return;
    };
    let scene = Scene {
        clear_color: Color::from_rgba(0, 0, 0, 0),
        nodes: vec![SceneNode::VectorMesh {
            mesh: quad(10.0, 10.0, 54.0, 54.0, [1.0, 1.0, 1.0, 1.0]),
            // Documented 2x3 identity `[m00, m01, m10, m11, tx, ty]`; the
            // transposed `[1, 0, 0, 0, 1, 0]` collapses to a line (zero
            // pixels) and must never be emitted by producers.
            transform: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
            paint: PaintDesc::Solid,
            clip: None,
            blend: BlendMode::Alpha,
        }],
    };
    let px = off.render_rgba(&scene, None).expect("render");
    let covered = px.as_chunks::<4>().0.iter().filter(|p| p[3] > 8).count();
    assert!(covered > 1000, "mesh must cover pixels, got {covered}");
    // Centre of the quad must be opaque white.
    let c = (32 * 64 + 32) * 4;
    assert!(px[c + 3] > 200, "centre alpha {}", px[c + 3]);
}

/// Tilt about the horizontal axis through the rect centre (focal 200px),
/// matching the hand computation in `geometry::projective_tests`.
fn tilt_about_y32() -> Transform {
    let (cy, f) = (32.0f32, 200.0f32);
    let th = 0.3f32;
    let (s, c) = (th.sin(), th.cos());
    Transform::from_projective_rows(
        [1.0, 0.0, 0.0],
        [0.0, c, cy * (1.0 - c)],
        [0.0, s / f, 1.0 - cy * s / f],
    )
}

fn tilt_scene() -> Vec<SceneNode> {
    vec![
        SceneNode::PushTransform {
            transform: tilt_about_y32(),
        },
        SceneNode::Rect {
            rect: Rect {
                x: 10.0,
                y: 10.0,
                w: 44.0,
                h: 44.0,
            },
            brush: Brush::Solid(Color::from_rgba(255, 255, 255, 255)),
            radius: [Px::ZERO; 4],
        },
        SceneNode::PopTransform,
    ]
}

#[test]
fn perspective_flatten_projects_rect() {
    let Some(px) = covered_of(tilt_scene()) else {
        return;
    };
    let (x0, y0, x1, y1) = ink_bbox(&px).expect("tilted rect must cover pixels");
    // Hand-computed projection (see `tilt_about_y32`): x spans ~9.7..55.8
    // (x scales with the y-dependent w), y spans ~11.4..51.4. ±2px for
    // rasterization.
    assert!(
        (8..=12).contains(&x0) && (54..=58).contains(&x1),
        "projected x-range wrong: {x0}..{x1}"
    );
    assert!(
        (9..=13).contains(&y0) && (49..=53).contains(&y1),
        "projected y-range wrong: {y0}..{y1}"
    );
}

#[test]
fn affine_push_transform_is_unaffected() {
    // Control: the same rect under a plain translate lands exactly.
    let Some(px) = covered_of(vec![
        SceneNode::PushTransform {
            transform: Transform::translate(5.0, 7.0),
        },
        SceneNode::Rect {
            rect: Rect {
                x: 10.0,
                y: 10.0,
                w: 44.0,
                h: 44.0,
            },
            brush: Brush::Solid(Color::from_rgba(255, 255, 255, 255)),
            radius: [Px::ZERO; 4],
        },
        SceneNode::PopTransform,
    ]) else {
        return;
    };
    assert_eq!(
        ink_bbox(&px),
        Some((15, 17, 58, 60)),
        "affine path must stay exact"
    );
}

fn clip_scene(op: repose_core::ClipOp) -> Vec<SceneNode> {
    vec![
        SceneNode::PushVectorClip {
            mesh: quad(20.0, 20.0, 44.0, 44.0, [1.0, 1.0, 1.0, 1.0]),
            op,
        },
        white_rect(0.0, 0.0, 64.0, 64.0),
        SceneNode::PopVectorClip,
    ]
}

#[test]
fn vector_clip_intersect_masks_rect() {
    let Some(px) = covered_of(clip_scene(repose_core::ClipOp::Intersect)) else {
        return;
    };
    // Only the 24x24 mask area survives.
    assert_eq!(ink_bbox(&px), Some((20, 20, 43, 43)));
}

#[test]
fn vector_clip_difference_cuts_rect() {
    let Some(px) = covered_of(clip_scene(repose_core::ClipOp::Difference)) else {
        return;
    };
    let ink = ink_bbox(&px).expect("inverse clip must leave content");
    // Full frame minus the mask: edges survive, centre is empty.
    assert_eq!((ink.0, ink.1), (0, 0));
    assert_eq!((ink.2, ink.3), (63, 63));
    let centre = (32 * 64 + 32) * 4;
    assert_eq!(px[centre + 3], 0, "mask interior must be cut out");
    assert!(px[3] > 200, "frame corner must survive");
}

#[test]
fn coverage_tile_composites_tinted() {
    let Some(mut off) = try_offscreen(64, 64) else {
        return;
    };
    // 10x10 full-coverage tile composited red at (5,5).
    let data = vec![255u8; 10 * 10];
    let h = off.renderer_mut().register_coverage_a8(10, 10, &data);
    assert_ne!(h, 0, "registration must return a handle");
    let scene = Scene {
        clear_color: Color::from_rgba(0, 0, 0, 0),
        nodes: vec![SceneNode::Coverage {
            rect: Rect {
                x: 5.0,
                y: 5.0,
                w: 10.0,
                h: 10.0,
            },
            handle: h,
            color: Color::from_rgba(255, 0, 0, 255),
        }],
    };
    let px = off.render_rgba(&scene, None).expect("render");
    let at = |x: usize, y: usize| (y * 64 + x) * 4;
    let c = at(10, 10);
    assert!(px[c + 3] > 200, "tile interior alpha {}", px[c + 3]);
    assert!(px[c] > 200, "tile interior red {}", px[c]);
    assert_eq!(px[at(0, 0) + 3], 0, "outside tile must be empty");
    assert_eq!(px[at(20, 20) + 3], 0, "outside tile must be empty");

    // Half-coverage tile: alpha must track coverage (~128).
    let half = vec![128u8; 8 * 8];
    let h2 = off.renderer_mut().register_coverage_a8(8, 8, &half);
    let scene = Scene {
        clear_color: Color::from_rgba(0, 0, 0, 0),
        nodes: vec![SceneNode::Coverage {
            rect: Rect {
                x: 30.0,
                y: 30.0,
                w: 8.0,
                h: 8.0,
            },
            handle: h2,
            color: Color::from_rgba(255, 255, 255, 255),
        }],
    };
    let px = off.render_rgba(&scene, None).expect("render");
    let c = at(34, 34);
    assert!(
        (100..=160).contains(&px[c + 3]),
        "half coverage alpha {}",
        px[c + 3]
    );

    // Unknown handles are skipped, not fatal.
    off.renderer_mut().remove_coverage(h2);
    let scene = Scene {
        clear_color: Color::from_rgba(0, 0, 0, 0),
        nodes: vec![SceneNode::Coverage {
            rect: Rect {
                x: 30.0,
                y: 30.0,
                w: 8.0,
                h: 8.0,
            },
            handle: h2,
            color: Color::from_rgba(255, 255, 255, 255),
        }],
    };
    let px = off.render_rgba(&scene, None).expect("render");
    assert_eq!(px[at(34, 34) + 3], 0, "removed tile must not draw");
}

fn covered_of_sized(w: u32, h: u32, nodes: Vec<SceneNode>) -> Option<Vec<u8>> {
    let Some(mut off) = try_offscreen(w, h) else {
        return None;
    };
    let scene = Scene {
        clear_color: Color::from_rgba(0, 0, 0, 0),
        nodes,
    };
    Some(off.render_rgba(&scene, None).expect("render"))
}

fn ink_bbox_wh(px: &[u8], w: usize) -> Option<(u32, u32, u32, u32)> {
    let (mut x0, mut y0, mut x1, mut y1) = (w as u32, 100000u32, 0u32, 0u32);
    for (i, p) in px.as_chunks::<4>().0.iter().enumerate() {
        if p[3] > 8 {
            let (x, y) = ((i % w) as u32, (i / w) as u32);
            x0 = x0.min(x);
            y0 = y0.min(y);
            x1 = x1.max(x);
            y1 = y1.max(y);
        }
    }
    (x1 >= x0).then_some((x0, y0, x1, y1))
}

#[test]
fn flatten_with_tiny_tilt() {
    // Systematic placement check, independent of map math: `w = 1 + 1e-4*y`
    // shrinks the far (bottom) edge hardest, so the quad lands at the
    // analytic projection of its corners, not at the input rect.
    // `project_rect` puts the corners in x 209.455..419.049, y 140.012..212.391,
    // and the rasterizer keeps a pixel only when its centre is covered, so
    // the ink lands one short of both far edges: (209, 140, 418, 211).
    let mut t = Transform::identity();
    t.perspective = [0.0, 0.0001, 1.0];
    let Some(px) = covered_of_sized(
        640,
        360,
        vec![
            SceneNode::PushTransform { transform: t },
            SceneNode::Rect {
                rect: Rect {
                    x: 214.0,
                    y: 142.0,
                    w: 211.0,
                    h: 75.0,
                },
                brush: Brush::Solid(Color::from_rgba(255, 255, 255, 255)),
                radius: [Px::ZERO; 4],
            },
            SceneNode::PopTransform,
        ],
    ) else {
        return;
    };
    let got = ink_bbox_wh(&px, 640);
    let (x0, y0, x1, y1) = got.expect("tilted rect must cover pixels");
    // ±1px for cross-driver coverage differences at the AA fringe.
    let expect = (209u32, 140u32, 418u32, 211u32);
    let (ex0, ey0, ex1, ey1) = expect;
    assert!(
        x0.abs_diff(ex0) <= 1
            && y0.abs_diff(ey0) <= 1
            && x1.abs_diff(ex1) <= 1
            && y1.abs_diff(ey1) <= 1,
        "tiny-tilt rect at {got:?}, want {expect:?}"
    );
}

#[test]
fn flatten_places_content_by_map() {
    // Near-affine perspective: the map rules placement. With an (almost)
    // identity map the rect lands exactly; with a gentle tilt it lands on
    // the hand-computed projection (±2px rasterization).
    let mut t = Transform::identity();
    t.perspective = [0.0, 1e-9, 1.0];
    let Some(px) = covered_of_sized(
        640,
        360,
        vec![
            SceneNode::PushTransform { transform: t },
            SceneNode::Rect {
                rect: Rect {
                    x: 214.0,
                    y: 142.0,
                    w: 211.0,
                    h: 75.0,
                },
                brush: Brush::Solid(Color::from_rgba(255, 255, 255, 255)),
                radius: [Px::ZERO; 4],
            },
            SceneNode::PopTransform,
        ],
    ) else {
        return;
    };
    assert_eq!(ink_bbox_wh(&px, 640), Some((214, 142, 424, 216)));

    let mut t = Transform::identity();
    t.perspective = [0.0, 0.0001, 1.0];
    for (rect, expect) in [
        (
            Rect {
                x: 10.0,
                y: 10.0,
                w: 44.0,
                h: 44.0,
            },
            (10, 10, 53, 53),
        ),
        (
            Rect {
                x: 500.0,
                y: 280.0,
                w: 60.0,
                h: 40.0,
            },
            (485, 272, 544, 309),
        ),
    ] {
        let Some(px) = covered_of_sized(
            640,
            360,
            vec![
                SceneNode::PushTransform { transform: t },
                SceneNode::Rect {
                    rect,
                    brush: Brush::Solid(Color::from_rgba(255, 255, 255, 255)),
                    radius: [Px::ZERO; 4],
                },
                SceneNode::PopTransform,
            ],
        ) else {
            return;
        };
        let got = ink_bbox_wh(&px, 640);
        let (x0, y0, x1, y1) = got.expect("tilted rect must cover pixels");
        let (ex0, ey0, ex1, ey1) = expect;
        assert!(
            x0.abs_diff(ex0) <= 2
                && y0.abs_diff(ey0) <= 2
                && x1.abs_diff(ex1) <= 2
                && y1.abs_diff(ey1) <= 2,
            "tilted rect at {got:?}, want {expect:?}"
        );
    }
}

#[test]
fn flatten_with_ass_frx45_map() {
    // Exact transform ass-rs emits for `\frx45` (from the parity debug
    // dump): the rect must land where the map says, center ~(320,179).
    let t = Transform {
        translate_x: 130.33391,
        translate_y: 126.03361,
        scale_x: 0.87359774,
        scale_y: 0.61428744,
        rotate: 0.5082492,
        shear_x: -0.79221636,
        shear_y: -0.5570625,
        origin_x: 0.5,
        origin_y: 0.5,
        perspective: [0.0, -0.0022627416, 1.4072934],
    };
    let Some(px) = covered_of_sized(
        640,
        360,
        vec![
            SceneNode::PushTransform { transform: t },
            SceneNode::Rect {
                rect: Rect {
                    x: 214.0,
                    y: 142.0,
                    w: 211.0,
                    h: 75.0,
                },
                brush: Brush::Solid(Color::from_rgba(255, 255, 255, 255)),
                radius: [Px::ZERO; 4],
            },
            SceneNode::PopTransform,
        ],
    ) else {
        return;
    };
    // Hand-computed projection of the rect through the map. Note the
    // left edge slants (204 at the bottom vs 222 at the top): the libass
    // `offs` coupling shears x with y, exactly as measured on libass
    // output itself.
    assert_eq!(ink_bbox_wh(&px, 640), Some((204, 155, 434, 208)));
}

fn center_px(nodes: Vec<SceneNode>) -> Option<[u8; 4]> {
    let px = covered_of(nodes)?;
    let c = (32 * 64 + 32) * 4;
    Some([px[c], px[c + 1], px[c + 2], px[c + 3]])
}

// Blending happens in linear space and reads back through sRGB, so
// expectations are the sRGB re-encoding of the linear math. Mesh vertex
// colors skip the sRGB decode, so source grey is linear 0.5 ~= sRGB 188.
fn blend_quad(blend: BlendMode) -> Vec<SceneNode> {
    vec![
        // Opaque red backdrop over the whole probe.
        SceneNode::Rect {
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 64.0,
                h: 64.0,
            },
            brush: Brush::Solid(Color::from_rgba(255, 0, 0, 255)),
            radius: [Px::ZERO; 4],
        },
        SceneNode::VectorMesh {
            // Opaque 50% grey source quad in the centre. Mesh vertex
            // colors are shader-linear (no sRGB decode), so 0.5 here is
            // linear 0.5 ~= sRGB 188.
            mesh: quad(16.0, 16.0, 48.0, 48.0, [0.5, 0.5, 0.5, 1.0]),
            transform: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
            paint: PaintDesc::Solid,
            clip: None,
            blend,
        },
    ]
}

#[test]
fn blend_multiply_darkens_backdrop() {
    // red * grey(0.5) = (0.5, 0, 0) ~= sRGB (188, 0, 0).
    let Some(c) = center_px(blend_quad(BlendMode::Multiply)) else {
        return;
    };
    assert!(
        (175..=200).contains(&c[0]) && c[1] < 30 && c[2] < 30,
        "multiply of red over grey wrong: {c:?}"
    );
}

#[test]
fn blend_screen_lightens_backdrop() {
    // screen(red, grey) = (1, 0.5, 0.5) ~= sRGB (255, 188, 188).
    let Some(c) = center_px(blend_quad(BlendMode::Screen)) else {
        return;
    };
    assert!(
        c[0] > 200 && (175..=200).contains(&c[1]) && (175..=200).contains(&c[2]),
        "screen of red over grey wrong: {c:?}"
    );
}

#[test]
fn blend_darken_takes_minimum() {
    // min(red, grey) = (0.5, 0, 0) ~= sRGB (188, 0, 0).
    let Some(c) = center_px(blend_quad(BlendMode::Darken)) else {
        return;
    };
    assert!(
        (175..=200).contains(&c[0]) && c[1] < 30 && c[2] < 30,
        "darken of red over grey wrong: {c:?}"
    );
}

#[test]
fn blend_lighten_takes_maximum() {
    // max(red, grey) = (1, 0.5, 0.5) ~= sRGB (255, 188, 188).
    let Some(c) = center_px(blend_quad(BlendMode::Lighten)) else {
        return;
    };
    assert!(
        c[0] > 200 && (175..=200).contains(&c[1]) && (175..=200).contains(&c[2]),
        "lighten of red over grey wrong: {c:?}"
    );
}

#[test]
fn blend_overlay_matches_screen_on_red() {
    // Backdrop red is light in R (1.0) and dark in G/B (0.0): overlay ==
    // screen on R, multiply on G/B. Source grey is linear 0.5, so expect
    // R = screen(0.5, 1.0) = 1.0 and G/B = 2*0.5*0 = 0: (255, 0, 0).
    let Some(c) = center_px(blend_quad(BlendMode::Overlay)) else {
        return;
    };
    assert!(
        c[0] > 200 && c[1] < 40 && c[2] < 40,
        "overlay of red over grey wrong: {c:?}"
    );
}

#[test]
fn blend_difference_subtracts_backdrop() {
    // |red - grey| = (0.5, 0.5, 0.5) ~= sRGB (188, 188, 188).
    let Some(c) = center_px(blend_quad(BlendMode::Difference)) else {
        return;
    };
    assert!(
        (175..=200).contains(&c[0]) && (175..=200).contains(&c[1]) && (175..=200).contains(&c[2]),
        "difference of red over grey wrong: {c:?}"
    );
}

#[test]
fn blend_overlay_inside_layer_composites() {
    // Isolated blend with a layer parent at the surface origin: the mesh
    // bbox maps 1:1, so overlay must match the surface-parent result
    // (255, 0, 0).
    let nodes = vec![
        SceneNode::BeginLayer {
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 64.0,
                h: 64.0,
            },
            layer_id: 21,
            alpha: 1.0,
            blur_radius_x: Px(0.0),
            blur_radius_y: Px(0.0),
            rectangle_edge: true,
        },
        SceneNode::Rect {
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 64.0,
                h: 64.0,
            },
            brush: Brush::Solid(Color::from_rgba(255, 0, 0, 255)),
            radius: [Px::ZERO; 4],
        },
        SceneNode::VectorMesh {
            mesh: quad(16.0, 16.0, 48.0, 48.0, [0.5, 0.5, 0.5, 1.0]),
            transform: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
            paint: PaintDesc::Solid,
            clip: None,
            blend: BlendMode::Overlay,
        },
        SceneNode::EndLayer { layer_id: 21 },
    ];
    let Some(px) = covered_of(nodes) else {
        return;
    };
    let c = (32 * 64 + 32) * 4;
    let (r, g, b) = (px[c], px[c + 1], px[c + 2]);
    assert!(
        r > 200 && g < 40 && b < 40,
        "overlay inside origin layer wrong: [{r}, {g}, {b}, {}]",
        px[c + 3]
    );
}

#[test]
fn blend_overlay_inside_offset_layer_composites() {
    // Layer parent at a non-zero surface origin: the mesh bbox maps into
    // layer pixels, so overlay must still match (255, 0, 0).
    let nodes = vec![
        SceneNode::BeginLayer {
            rect: Rect {
                x: 10.0,
                y: 8.0,
                w: 64.0,
                h: 64.0,
            },
            layer_id: 22,
            alpha: 1.0,
            blur_radius_x: Px(0.0),
            blur_radius_y: Px(0.0),
            rectangle_edge: true,
        },
        SceneNode::Rect {
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 64.0,
                h: 64.0,
            },
            brush: Brush::Solid(Color::from_rgba(255, 0, 0, 255)),
            radius: [Px::ZERO; 4],
        },
        SceneNode::VectorMesh {
            mesh: quad(16.0, 16.0, 48.0, 48.0, [0.5, 0.5, 0.5, 1.0]),
            transform: [1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
            paint: PaintDesc::Solid,
            clip: None,
            blend: BlendMode::Overlay,
        },
        SceneNode::EndLayer { layer_id: 22 },
    ];
    let Some(mut off) = try_offscreen(96, 96) else {
        return;
    };
    let scene = Scene {
        clear_color: Color::from_rgba(0, 0, 0, 0),
        nodes,
    };
    let px = off.render_rgba(&scene, None).expect("render");
    // Mesh centre in surface pixels: layer origin + (32, 32).
    let c = ((8 + 32) * 96 + (10 + 32)) * 4;
    let (r, g, b) = (px[c], px[c + 1], px[c + 2]);
    assert!(
        r > 200 && g < 40 && b < 40,
        "overlay inside offset layer wrong: [{r}, {g}, {b}, {}]",
        px[c + 3]
    );
}