cranpose-render-wgpu 0.9.21

GPU renderer for Cranpose scenes through wgpu
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
use std::rc::Rc;

use cranpose_render_common::graph::{
    DrawRunNode, PrimitiveEntry, PrimitiveNode, PrimitivePhase, ProjectiveTransform, RenderGraph,
    RenderNode, TextPrimitiveNode,
};
use cranpose_ui::{
    TextLayoutOptions,
    text::{AnnotatedString, SpanStyle, TextStyle, TextUnit},
};
use cranpose_ui_graphics::{
    Color, CompositingStrategy, DrawPrimitive, DrawTextStyle, GraphicsLayer, Rect, TextPrimitive,
};

use crate::{shared_test_support, support};

const LOGICAL_WIDTH: u32 = 96;
const LOGICAL_HEIGHT: u32 = 64;
const ROOT_SCALE: f32 = 1.25;
const PIXEL_WIDTH: u32 = 120;
const PIXEL_HEIGHT: u32 = 80;

fn hidden_text(clip: Option<Rect>) -> RenderNode {
    hidden_text_at(
        Rect {
            x: 4.0,
            y: 4.0,
            width: 20.0,
            height: 22.0,
        },
        clip,
    )
}

fn hidden_text_at(rect: Rect, clip: Option<Rect>) -> RenderNode {
    let annotated = AnnotatedString {
        text: "M".to_owned(),
        ..AnnotatedString::default()
    };
    RenderNode::Primitive(PrimitiveEntry {
        phase: PrimitivePhase::BeforeChildren,
        node: PrimitiveNode::Text(Box::new(TextPrimitiveNode {
            node_id: 51_001,
            rect,
            render_text: std::sync::Arc::new(annotated.render_string()),
            text: Rc::new(annotated),
            text_style: std::sync::Arc::new(TextStyle::from_span_style(SpanStyle {
                color: Some(Color::BLACK),
                font_size: TextUnit::Sp(18.0),
                ..Default::default()
            })),
            font_size: 18.0,
            layout_options: TextLayoutOptions::default(),
            clip,
            paint: Default::default(),
        })),
    })
}

fn visible_content() -> RenderNode {
    support::solid_rect(
        Rect {
            x: 8.35,
            y: 7.6,
            width: 19.25,
            height: 13.0,
        },
        Color(0.15, 0.35, 0.9, 1.0),
    )
}

fn sensitive_text_run() -> RenderNode {
    RenderNode::DrawRun(DrawRunNode::new(
        PrimitivePhase::BeforeChildren,
        vec![DrawPrimitive::Text(Box::new(TextPrimitive {
            rect: Rect {
                x: 5.0,
                y: 4.0,
                width: 22.0,
                height: 22.0,
            },
            text: Rc::from("M"),
            style: DrawTextStyle::new(18.0),
            color: Color::BLACK,
        }))],
    ))
}

fn graph_with_direct_clipped_text() -> RenderGraph {
    let wrapper = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 48.0,
            height: 36.0,
        },
        ProjectiveTransform::translation(1.35, 2.25),
        GraphicsLayer::default(),
        vec![hidden_text(Some(Rect {
            x: 0.0,
            y: 0.0,
            width: 0.0,
            height: 0.0,
        }))],
    );
    let outer = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 48.0,
            height: 36.0,
        },
        ProjectiveTransform::uniform_scale(1.1).then(ProjectiveTransform::translation(12.35, 9.65)),
        GraphicsLayer {
            compositing_strategy: CompositingStrategy::Offscreen,
            ..Default::default()
        },
        vec![visible_content(), RenderNode::Layer(Box::new(wrapper))],
    );
    support::page_graph(
        LOGICAL_WIDTH,
        LOGICAL_HEIGHT,
        vec![
            support::solid_rect(
                Rect {
                    x: 0.0,
                    y: 0.0,
                    width: LOGICAL_WIDTH as f32,
                    height: LOGICAL_HEIGHT as f32,
                },
                Color::WHITE,
            ),
            RenderNode::Layer(Box::new(outer)),
        ],
    )
}

fn replace_hidden_descendant(graph: &mut RenderGraph, descendant: RenderNode) {
    replace_hidden_descendant_with_transform(
        graph,
        descendant,
        ProjectiveTransform::translation(1.35, 2.25),
    );
}

fn replace_hidden_descendant_with_transform(
    graph: &mut RenderGraph,
    descendant: RenderNode,
    transform: ProjectiveTransform,
) {
    let Some(RenderNode::Layer(outer)) = graph.root.children.get_mut(1) else {
        panic!("expected the isolated outer layer");
    };
    let empty_clip = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 0.0,
            height: 0.0,
        },
        transform,
        GraphicsLayer {
            clip: true,
            ..Default::default()
        },
        vec![descendant],
    );
    let wrapper = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 48.0,
            height: 36.0,
        },
        ProjectiveTransform::translation(1.35, 2.25),
        GraphicsLayer::default(),
        vec![RenderNode::Layer(Box::new(empty_clip))],
    );
    outer.children = vec![visible_content(), RenderNode::Layer(Box::new(wrapper))];
}

fn replace_clipped_out_descendant(graph: &mut RenderGraph, descendant: RenderNode) {
    let Some(RenderNode::Layer(outer)) = graph.root.children.get_mut(1) else {
        panic!("expected the isolated outer layer");
    };
    outer.graphics_layer.clip = true;
    let mut child = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 20.0,
            height: 22.0,
        },
        ProjectiveTransform::translation(80.0, 4.0),
        GraphicsLayer::default(),
        vec![descendant],
    );
    child.draws_within_bounds = true;
    outer.children = vec![visible_content(), RenderNode::Layer(Box::new(child))];
}

fn replace_with_direct_translation(graph: &mut RenderGraph, descendant: RenderNode) {
    let Some(RenderNode::Layer(outer)) = graph.root.children.get_mut(1) else {
        panic!("expected the isolated outer layer");
    };
    let wrapper = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 48.0,
            height: 36.0,
        },
        ProjectiveTransform::identity(),
        GraphicsLayer::default(),
        vec![descendant],
    );
    outer.children = vec![visible_content(), RenderNode::Layer(Box::new(wrapper))];
}

fn capture(renderer: &mut support::LockedRenderer, graph: &RenderGraph) -> Vec<u8> {
    support::capture_settled(renderer, |renderer| {
        support::capture_graph_with_scale(
            renderer,
            graph.clone(),
            PIXEL_WIDTH,
            PIXEL_HEIGHT,
            ROOT_SCALE,
        )
    })
    .pixels
}

#[test]
fn nested_empty_clip_text_keeps_fractional_isolated_pixels_rigid_after_graph_edit() {
    let mut renderer = match support::headless_renderer() {
        Ok(renderer) => renderer,
        Err(error) => {
            eprintln!("skipping pixel-sensitive collection check: {error}");
            return;
        }
    };

    let mut graph = graph_with_direct_clipped_text();
    let direct_reference = capture(&mut renderer, &graph);

    replace_hidden_descendant(&mut graph, hidden_text(None));
    let nested = capture(&mut renderer, &graph);
    support::assert_same_bytes(
        "text below a direct-translation empty clip",
        PIXEL_WIDTH,
        &nested,
        &direct_reference,
    );

    replace_hidden_descendant(
        &mut graph,
        support::solid_rect(
            Rect {
                x: 4.0,
                y: 4.0,
                width: 20.0,
                height: 22.0,
            },
            Color::BLACK,
        ),
    );
    let nested_shape_only = capture(&mut renderer, &graph);
    assert!(
        nested_shape_only != nested,
        "removing the pixel-sensitive descendant must visibly remove the rigid snap phase"
    );

    let non_direct =
        ProjectiveTransform::uniform_scale(1.15).then(ProjectiveTransform::translation(1.35, 2.25));
    replace_hidden_descendant_with_transform(&mut graph, hidden_text(None), non_direct);
    let non_direct_text = capture(&mut renderer, &graph);
    replace_hidden_descendant_with_transform(
        &mut graph,
        support::solid_rect(
            Rect {
                x: 4.0,
                y: 4.0,
                width: 20.0,
                height: 22.0,
            },
            Color::BLACK,
        ),
        non_direct,
    );
    let non_direct_shape = capture(&mut renderer, &graph);
    support::assert_same_bytes(
        "non-direct transform blocks pixel sensitivity",
        PIXEL_WIDTH,
        &non_direct_text,
        &non_direct_shape,
    );

    let mut clipped_graph = graph_with_direct_clipped_text();
    let Some(RenderNode::Layer(outer)) = clipped_graph.root.children.get_mut(1) else {
        panic!("expected the isolated outer layer");
    };
    outer.graphics_layer.clip = true;
    outer.children = vec![
        visible_content(),
        hidden_text_at(
            Rect {
                x: 80.0,
                y: 4.0,
                width: 20.0,
                height: 22.0,
            },
            None,
        ),
    ];
    let clipped_direct_reference = capture(&mut renderer, &clipped_graph);

    let wrapper = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 20.0,
            height: 22.0,
        },
        ProjectiveTransform::identity(),
        GraphicsLayer::default(),
        vec![hidden_text(None)],
    );
    replace_clipped_out_descendant(&mut clipped_graph, RenderNode::Layer(Box::new(wrapper)));
    let clipped_nested = capture(&mut renderer, &clipped_graph);
    support::assert_same_bytes(
        "pixel-sensitive text in a layer culled by its parent clip",
        PIXEL_WIDTH,
        &clipped_nested,
        &clipped_direct_reference,
    );

    let mut run_graph = graph_with_direct_clipped_text();
    let Some(RenderNode::Layer(outer)) = run_graph.root.children.get_mut(1) else {
        panic!("expected the isolated outer layer");
    };
    outer.children = vec![visible_content(), sensitive_text_run()];
    let direct_run = capture(&mut renderer, &run_graph);

    replace_with_direct_translation(&mut run_graph, sensitive_text_run());
    let nested_run = capture(&mut renderer, &run_graph);
    support::assert_same_bytes(
        "recorded text run below a direct-translation layer",
        PIXEL_WIDTH,
        &nested_run,
        &direct_run,
    );
}

fn graph_with_nested_isolated(descendant: RenderNode) -> RenderGraph {
    let mut graph = graph_with_direct_clipped_text();
    let Some(RenderNode::Layer(outer)) = graph.root.children.get_mut(1) else {
        panic!("expected the isolated outer layer");
    };
    // The outer layer's content walks translated, so the inner isolated
    // layer snaps on its own and only the outer one asks about the text.
    outer.translated_content_context = true;
    let empty_clip = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 0.0,
            height: 0.0,
        },
        ProjectiveTransform::translation(1.35, 2.25),
        GraphicsLayer {
            clip: true,
            ..Default::default()
        },
        vec![descendant],
    );
    let inner = shared_test_support::layer_node(
        Rect {
            x: 0.0,
            y: 0.0,
            width: 48.0,
            height: 36.0,
        },
        ProjectiveTransform::translation(1.35, 2.25),
        GraphicsLayer {
            compositing_strategy: CompositingStrategy::Offscreen,
            ..Default::default()
        },
        vec![RenderNode::Layer(Box::new(empty_clip))],
    );
    outer.children = vec![visible_content(), RenderNode::Layer(Box::new(inner))];
    graph
}

#[test]
fn text_hidden_in_a_nested_isolated_layer_keeps_the_outer_layer_rigid() {
    let mut renderer = match support::headless_renderer() {
        Ok(renderer) => renderer,
        Err(error) => {
            eprintln!("skipping pixel-sensitive collection check: {error}");
            return;
        }
    };

    let with_text = capture(
        &mut renderer,
        &graph_with_nested_isolated(hidden_text(None)),
    );
    let with_shape = capture(
        &mut renderer,
        &graph_with_nested_isolated(support::solid_rect(
            Rect {
                x: 4.0,
                y: 4.0,
                width: 20.0,
                height: 22.0,
            },
            Color::BLACK,
        )),
    );
    assert!(
        with_text != with_shape,
        "text below the nested isolated layer must snap the outer layer"
    );
}