cranpose-render-wgpu 0.1.167

WGPU renderer backend for Cranpose
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
use cranpose_ui_graphics::RoundedCornerShape;

use super::*;

fn rect(x: f32, y: f32, width: f32, height: f32) -> Rect {
    Rect {
        x,
        y,
        width,
        height,
    }
}

fn corners(width: f32, height: f32, radius: f32) -> RoundedClipCorners {
    RoundedClipCorners::of(LayerRoundedClip {
        rect: rect(0.0, 0.0, width, height),
        radii: [radius; 4],
    })
}

#[test]
fn content_clear_of_every_corner_square_is_admitted() {
    assert!(corners(200.0, 100.0, 20.0).admits(rect(20.0, 20.0, 160.0, 60.0)));
}

#[test]
fn content_inside_the_corner_circle_is_admitted() {
    assert!(corners(200.0, 100.0, 20.0).admits(rect(14.0, 14.0, 60.0, 60.0)));
}

#[test]
fn content_reaching_the_corner_cut_is_refused() {
    assert!(!corners(200.0, 100.0, 20.0).admits(rect(2.0, 2.0, 60.0, 60.0)));
}

#[test]
fn content_touching_the_edge_between_corners_is_admitted() {
    assert!(corners(200.0, 100.0, 20.0).admits(rect(40.0, 0.0, 100.0, 100.0)));
}

#[test]
fn a_rounded_layer_whose_content_enters_a_corner_isolates() {
    let mut layer = LayerNode {
        local_bounds: rect(0.0, 0.0, 200.0, 100.0),
        graphics_layer: GraphicsLayer {
            clip: true,
            shape: LayerShape::Rounded(RoundedCornerShape::uniform(20.0)),
            ..Default::default()
        },
        ..Default::default()
    };
    layer.children.push(RenderNode::DrawRun(DrawRunNode::new(
        PrimitivePhase::BeforeChildren,
        vec![DrawPrimitive::Rect {
            rect: rect(0.0, 0.0, 200.0, 100.0),
            brush: cranpose_ui_graphics::Brush::solid(cranpose_ui_graphics::Color::WHITE),
            stroke: None,
        }],
    )));
    assert!(matches!(child_placement(&layer), Placement::Isolated));
    let RenderNode::DrawRun(run) = &mut layer.children[0] else {
        unreachable!()
    };
    *run = DrawRunNode::new(
        PrimitivePhase::BeforeChildren,
        vec![DrawPrimitive::Rect {
            rect: rect(14.0, 14.0, 172.0, 72.0),
            brush: cranpose_ui_graphics::Brush::solid(cranpose_ui_graphics::Color::WHITE),
            stroke: None,
        }],
    );
    assert!(matches!(child_placement(&layer), Placement::Direct(_)));
}

#[test]
fn an_animated_raster_scale_is_never_smaller_and_at_most_one_step_larger() {
    let step = 2f32.powf(1.0 / ANIMATED_RASTER_STEPS_PER_OCTAVE);
    for index in 0..400 {
        let scale = 0.25 + index as f32 * 0.01;
        let raster = animated_raster_scale(scale);
        assert!(raster >= scale, "{scale} rasterized at {raster}");
        assert!(
            raster <= scale * step * 1.0001,
            "{scale} rasterized at {raster}, more than one step up"
        );
    }
    assert_eq!(animated_raster_scale(0.9), animated_raster_scale(0.905));
    assert_eq!(animated_raster_scale(1.0), 1.0);
}

#[test]
fn layer_motion_steps_a_scale_only_while_it_changes_over_the_same_content() {
    let mut motion = LayerMotion::default();
    assert_eq!(
        motion.raster_scale(Some(1), 0.9, 7, true),
        0.9,
        "a layer seen for the first time rasterizes at its own scale"
    );
    motion.end_frame();
    let stepped = motion.raster_scale(Some(1), 0.91, 7, true);
    assert_eq!(stepped, animated_raster_scale(0.91));
    assert!(stepped > 0.91);
    motion.end_frame();
    assert_eq!(
        motion.raster_scale(Some(1), 0.91, 7, true),
        0.91,
        "a scale that holds for a frame rasterizes exactly again"
    );
    motion.end_frame();
    assert_eq!(
        motion.raster_scale(Some(1), 0.92, 8, true),
        0.92,
        "new content is drawn afresh anyway, so it rasterizes at its own scale"
    );
    motion.end_frame();
    assert_eq!(
        motion.raster_scale(Some(1), 0.93, 8, false),
        0.93,
        "a layer the cache cannot hold gains nothing from a stepped scale"
    );
    assert_eq!(motion.raster_scale(None, 0.93, 8, true), 0.93);
    motion.end_frame();
    motion.end_frame();
    assert_eq!(
        motion.raster_scale(Some(1), 0.95, 8, true),
        0.95,
        "a layer missing from the last frame starts over"
    );
}

#[test]
fn a_scaling_layer_keeps_its_raster_while_it_covers_the_scale_within_an_octave() {
    let mut motion = LayerMotion::default();
    let mut frame = |scale: f32| {
        let raster = motion.raster_scale(Some(1), scale, 7, true);
        motion.end_frame();
        raster
    };
    assert_eq!(frame(1.0), 1.0);
    for scale in [0.95, 0.8, 0.6, 0.51, 0.7, 0.99] {
        assert_eq!(frame(scale), 1.0, "{scale} draws from the raster at 1.0");
    }
    let shrunk = frame(0.45);
    assert_eq!(
        shrunk,
        animated_raster_scale(0.45),
        "past an octave down the raster steps down"
    );
    assert_eq!(frame(0.3), shrunk);
    let grown = frame(0.5);
    assert_eq!(
        grown,
        animated_raster_scale(0.5),
        "a scale the raster no longer covers steps up"
    );
    assert!(grown > shrunk);
}

fn turn(degrees: f32) -> ProjectiveTransform {
    let (sin, cos) = degrees.to_radians().sin_cos();
    ProjectiveTransform::from_rect_to_quad(
        rect(0.0, 0.0, 1.0, 1.0),
        [[0.0, 0.0], [cos, sin], [-sin, cos], [cos - sin, sin + cos]],
    )
}

fn solid(primitive_rect: Rect) -> DrawPrimitive {
    DrawPrimitive::Rect {
        rect: primitive_rect,
        brush: cranpose_ui_graphics::Brush::solid(cranpose_ui_graphics::Color::WHITE),
        stroke: None,
    }
}

fn run(primitives: Vec<DrawPrimitive>) -> RenderNode {
    RenderNode::DrawRun(DrawRunNode::new(PrimitivePhase::BeforeChildren, primitives))
}

fn turned_layer(
    transform: ProjectiveTransform,
    graphics_layer: GraphicsLayer,
    children: Vec<RenderNode>,
) -> LayerNode {
    LayerNode {
        local_bounds: rect(0.0, 0.0, 60.0, 40.0),
        transform_to_parent: transform,
        graphics_layer,
        children,
        ..Default::default()
    }
}

/// The isolated child `layer` collects into under an unturned root.
fn collected(layer: LayerNode) -> ChildLayer {
    let root = LayerNode {
        local_bounds: rect(0.0, 0.0, 200.0, 200.0),
        children: vec![RenderNode::Layer(Box::new(layer))],
        ..Default::default()
    };
    let mut scene = collect_root(
        &root,
        &mut crate::pipeline::UiTextLayoutResolver,
        &mut LayerMotion::default(),
        SceneCapacityHint::default(),
    );
    scene.children.pop().expect("a turned layer isolates")
}

#[test]
fn a_transform_that_only_turns_and_moves_is_rigid() {
    assert!(is_rigid(turn(33.0)));
    assert!(is_rigid(
        turn(-7.5).then(ProjectiveTransform::translation(12.0, -3.0))
    ));
    assert!(!is_rigid(ProjectiveTransform::uniform_scale(1.5)));
    assert!(!is_rigid(
        turn(20.0).then(ProjectiveTransform::uniform_scale(0.5))
    ));
    assert!(!is_rigid(ProjectiveTransform::from_rect_to_quad(
        rect(0.0, 0.0, 1.0, 1.0),
        [[0.0, 0.0], [1.0, 0.1], [0.0, 1.0], [0.8, 1.3]],
    )));
}

#[test]
fn a_layer_that_only_turns_draws_in_place() {
    let child = collected(turned_layer(
        turn(20.0),
        GraphicsLayer::default(),
        vec![run(vec![
            solid(rect(0.0, 0.0, 60.0, 40.0)),
            solid(rect(4.0, 4.0, 8.0, 8.0)),
        ])],
    ));
    assert!(child.in_place);
}

#[test]
fn a_turned_layer_that_needs_a_surface_for_more_than_its_turn_keeps_it() {
    let content = || vec![run(vec![solid(rect(0.0, 0.0, 60.0, 40.0))])];
    for graphics_layer in [
        GraphicsLayer {
            alpha: 0.5,
            ..Default::default()
        },
        GraphicsLayer {
            compositing_strategy: CompositingStrategy::Offscreen,
            ..Default::default()
        },
        GraphicsLayer {
            blend_mode: BlendMode::Multiply,
            ..Default::default()
        },
        GraphicsLayer {
            clip: true,
            shape: LayerShape::Rounded(RoundedCornerShape::uniform(8.0)),
            ..Default::default()
        },
    ] {
        let child = collected(turned_layer(turn(20.0), graphics_layer.clone(), content()));
        assert!(!child.in_place, "{graphics_layer:?} needs a surface");
    }
    let scaled = collected(turned_layer(
        turn(20.0).then(ProjectiveTransform::uniform_scale(1.5)),
        GraphicsLayer::default(),
        content(),
    ));
    assert!(
        !scaled.in_place,
        "a scaled layer would resample what it draws"
    );
}

#[test]
fn content_that_blends_through_to_the_page_keeps_its_surface() {
    let child = collected(turned_layer(
        turn(20.0),
        GraphicsLayer::default(),
        vec![run(vec![
            solid(rect(0.0, 0.0, 60.0, 40.0)),
            DrawPrimitive::Blend {
                primitive: Box::new(solid(rect(10.0, 10.0, 20.0, 20.0))),
                blend_mode: BlendMode::DstOut,
            },
        ])],
    ));
    assert!(
        !child.in_place,
        "a hole punched in place would reach the pixels beneath the layer"
    );
}

#[test]
fn a_turned_layer_whose_clip_would_cut_a_turned_child_keeps_its_surface() {
    let inner = turned_layer(
        turn(30.0),
        GraphicsLayer::default(),
        vec![run(vec![solid(rect(0.0, 0.0, 60.0, 40.0))])],
    );
    let clipping = collected(turned_layer(
        turn(-10.0),
        GraphicsLayer {
            clip: true,
            ..Default::default()
        },
        vec![RenderNode::Layer(Box::new(inner.clone()))],
    ));
    assert!(
        clipping.content.children[0].in_place,
        "the inner layer only turns"
    );
    assert!(
        !clipping.in_place,
        "a clip turned off the pixel grid is no scissor for the child it cuts"
    );
    let open = collected(turned_layer(
        turn(-10.0),
        GraphicsLayer::default(),
        vec![
            run(vec![solid(rect(0.0, 0.0, 60.0, 40.0))]),
            RenderNode::Layer(Box::new(inner)),
        ],
    ));
    assert!(open.in_place);
}

#[test]
fn a_turned_layer_with_an_image_keeps_its_surface() {
    let image = RenderNode::Primitive(PrimitiveEntry {
        phase: PrimitivePhase::BeforeChildren,
        node: PrimitiveNode::Draw(Box::new(cranpose_render_common::graph::DrawPrimitiveNode {
            primitive: DrawPrimitive::Image {
                rect: rect(0.0, 0.0, 60.0, 40.0),
                image: cranpose_ui_graphics::ImageBitmap::from_rgba8(1, 1, vec![255; 4])
                    .expect("a one-pixel bitmap"),
                alpha: 1.0,
                color_filter: None,
                sampling: cranpose_ui_graphics::ImageSampling::Nearest,
                src_rect: None,
            },
            clip: None,
        })),
    });
    let child = collected(turned_layer(
        turn(20.0),
        GraphicsLayer::default(),
        vec![image],
    ));
    assert!(
        !child.in_place,
        "an image drawn turned in place would have hard edges its filtered surface does not"
    );
}

fn glass_layer(transform: ProjectiveTransform) -> LayerNode {
    turned_layer(
        transform,
        GraphicsLayer {
            alpha: 0.5,
            backdrop_effect: Some(RenderEffect::blur(4.0)),
            clip: true,
            shape: LayerShape::Rounded(RoundedCornerShape::uniform(8.0)),
            ..Default::default()
        },
        Vec::new(),
    )
}

#[test]
fn a_turned_layer_reads_its_backdrop_in_its_own_space_under_its_turn() {
    let outer = collected(glass_layer(turn(20.0)));

    assert!(
        outer.backdrop.is_none(),
        "the turn carries no backdrop of its own"
    );
    assert!(outer.effect.is_none());
    assert!(outer.rounded_clip.is_none());
    assert_eq!(outer.alpha, GraphicsLayer::composite_alpha_8bit(0.5));
    let [inner] = outer.content.children.as_slice() else {
        panic!("the backdrop runs in one child in the layer's own space");
    };
    assert!(inner.backdrop.is_some());
    assert!(inner.rounded_clip.is_some());
    assert_eq!(inner.alpha, 1.0);
    assert_eq!(
        uniform_scale_translation(inner.transform),
        Some((1.0, Point::default())),
        "the inner child sits in the turned layer's own space"
    );
}

#[test]
fn a_moved_layer_resolves_its_backdrop_beside_its_surface() {
    let child = collected(glass_layer(ProjectiveTransform::translation(10.0, 20.0)));

    assert!(child.backdrop.is_some());
    assert!(child.rounded_clip.is_some());
    assert!(child.content.children.is_empty());
}

/// The draw ops a root clipped to 100×100 collects around `child`.
fn draw_ops_under_clip(child: LayerNode) -> usize {
    let root = LayerNode {
        local_bounds: rect(0.0, 0.0, 100.0, 100.0),
        clip_to_bounds: true,
        children: vec![RenderNode::Layer(Box::new(child))],
        ..Default::default()
    };
    collect_root(
        &root,
        &mut crate::pipeline::UiTextLayoutResolver,
        &mut LayerMotion::default(),
        SceneCapacityHint::default(),
    )
    .scene
    .draw_ops
    .len()
}

fn row_at(y: f32, draws_within_bounds: bool, shadow_elevation: f32) -> LayerNode {
    LayerNode {
        local_bounds: rect(0.0, 0.0, 100.0, 40.0),
        transform_to_parent: ProjectiveTransform::translation(0.0, y),
        graphics_layer: GraphicsLayer {
            shadow_elevation,
            ..Default::default()
        },
        draws_within_bounds,
        children: vec![run(vec![solid(rect(0.0, 0.0, 100.0, 40.0))])],
        ..Default::default()
    }
}

#[test]
fn a_contained_row_the_clip_leaves_out_is_not_collected() {
    assert!(
        draw_ops_under_clip(row_at(30.0, true, 0.0)) > 0,
        "a visible row draws"
    );
    assert_eq!(draw_ops_under_clip(row_at(300.0, true, 0.0)), 0);
}

#[test]
fn a_row_that_may_draw_past_its_bounds_is_collected_offscreen() {
    assert!(
        draw_ops_under_clip(row_at(300.0, false, 0.0)) > 0,
        "without the promise its draws may reach the clip"
    );
    assert!(
        draw_ops_under_clip(row_at(101.5, true, 4.0)) > 0,
        "a shadow reaches past the row's bounds"
    );
}

fn text_root(style: cranpose_ui::TextStyle) -> (LayerNode, std::sync::Arc<cranpose_ui::TextStyle>) {
    let style = std::sync::Arc::new(style);
    let text = cranpose_render_common::graph::TextPrimitiveNode {
        node_id: 5,
        rect: rect(0.0, 0.0, 80.0, 20.0),
        text: cranpose_ui::text::shared_plain_annotated_string("shared"),
        render_text: cranpose_ui::text::shared_plain_render_string("shared"),
        text_style: std::sync::Arc::clone(&style),
        font_size: 14.0,
        layout_options: cranpose_ui::TextLayoutOptions::default(),
        clip: None,
    };
    let root = LayerNode {
        local_bounds: rect(0.0, 0.0, 100.0, 100.0),
        children: vec![RenderNode::Primitive(PrimitiveEntry {
            phase: PrimitivePhase::BeforeChildren,
            node: PrimitiveNode::Text(Box::new(text)),
        })],
        ..Default::default()
    };
    (root, style)
}

#[test]
fn a_plain_texts_draw_carries_its_nodes_style() {
    let (root, style) = text_root(cranpose_ui::TextStyle::default());
    let scene = collect_root(
        &root,
        &mut crate::pipeline::UiTextLayoutResolver,
        &mut LayerMotion::default(),
        SceneCapacityHint::default(),
    )
    .scene;
    assert_eq!(scene.texts.len(), 1);
    assert!(std::sync::Arc::ptr_eq(&scene.texts[0].text_style, &style));
}