gpui-fast 0.1.9

gpui: GPUI with gpui-fast rendering and layout optimizations
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
// Modified by Longbridge for gpui-fast.
//! Identical real-div pixel regressions run by each GPU renderer.

use super::{assert_same_pixels, pixel, render_div_fixture};
use gpui::{Path, px};

/// The command-palette regression from zed#37480: a rounded palette has a
/// header above the input and list. Its radius belongs only to the palette's
/// outer corners, never to the input's or list row's own clipping rectangle.
#[gpui::test]
fn rounded_overflow_command_palette_rows_keep_square_corners(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};
    let (pixels, side) = render_div_fixture(cx, || {
        let row = |height, color| {
            gpui::div()
                .w_full()
                .h(px(height))
                .flex_shrink_0()
                .overflow_hidden()
                .bg(color)
        };
        gpui::div().size(px(64.)).bg(gpui::white()).child(
            gpui::div()
                .flex()
                .flex_col()
                .size(px(64.))
                .rounded(px(16.))
                .overflow_hidden()
                .child(row(16., gpui::black()))
                .child(row(16., gpui::red()))
                .child(row(8., gpui::blue()))
                .child(row(8., gpui::black()))
                .child(row(16., gpui::green())),
        )
    });
    let sample = |x, y| pixel(&pixels, side, x * side / 64, y * side / 64);
    // Check all four input and selected-row corners, not just their centres.
    for x in [0, 63] {
        for y in [16, 31] {
            assert_eq!(
                sample(x, y),
                [255, 0, 0, 255],
                "input corner acquired the palette radius at ({x}, {y})"
            );
        }
        for y in [32, 39] {
            assert_eq!(
                sample(x, y),
                [0, 0, 255, 255],
                "selected row acquired the palette radius at ({x}, {y})"
            );
        }
        assert_eq!(sample(x, 0), [255, 255, 255, 255]);
        assert_eq!(sample(x, 63), [255, 255, 255, 255]);
    }
    assert_eq!(sample(32, 0), [0, 0, 0, 255]);
    assert_eq!(sample(32, 63), [0, 128, 0, 255]);
}

#[gpui::test]
fn rounded_overflow_deep_stack_preserves_clips(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};
    let (pixels, side) = render_div_fixture(cx, || {
        let mut child = gpui::div().size(px(64.)).bg(gpui::black());
        // Distinct geometry prevents identical-scope deduplication. This also
        // exceeds Metal's 4096-byte inline fragment parameter limit.
        for depth in 0..100 {
            child = gpui::div()
                .size(px(64.))
                .rounded(px(20. + depth as f32 * 0.01))
                .overflow_hidden()
                .child(child);
        }
        gpui::div().size(px(64.)).bg(gpui::white()).child(child)
    });
    assert_eq!(pixel(&pixels, side, 0, 0), [255, 255, 255, 255]);
    assert_eq!(pixel(&pixels, side, side / 2, side / 2), [0, 0, 0, 255]);
}

#[gpui::test]
fn rounded_overflow_transparent_unequal_borders_clip_padding_edge(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};
    let (pixels, side) = render_div_fixture(cx, || {
        let mut parent = gpui::div().size(px(64.)).rounded(px(24.)).overflow_hidden();
        parent.style().border_widths.left = Some(px(8.).into());
        parent.style().border_widths.top = Some(px(4.).into());
        parent.style().border_widths.right = Some(px(2.).into());
        parent.style().border_widths.bottom = Some(px(6.).into());
        gpui::div()
            .size(px(64.))
            .bg(gpui::white())
            .child(parent.child(gpui::div().size(px(64.)).bg(gpui::black())))
    });
    let sample = |x, y| pixel(&pixels, side, x * side / 64, y * side / 64);
    assert_eq!(sample(9, 5), [255, 255, 255, 255]);
    assert_eq!(sample(24, 5), [0, 0, 0, 255]);
    assert_eq!(sample(9, 24), [0, 0, 0, 255]);
    assert_eq!(sample(60, 24), [0, 0, 0, 255]);
    assert_eq!(sample(63, 32), [255, 255, 255, 255]);
    assert_eq!(sample(32, 60), [255, 255, 255, 255]);
}

#[gpui::test]
fn rounded_overflow_empty_padding_edge_hides_descendants(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};
    let (pixels, _) = render_div_fixture(cx, || {
        let mut parent = gpui::div().size(px(64.)).rounded(px(24.)).overflow_hidden();
        parent.style().border_widths.left = Some(px(40.).into());
        parent.style().border_widths.right = Some(px(40.).into());
        gpui::div()
            .size(px(64.))
            .bg(gpui::white())
            .child(parent.child(gpui::div().size(px(64.)).bg(gpui::black())))
    });
    assert!(
        pixels
            .chunks_exact(4)
            .all(|pixel| pixel == [255, 255, 255, 255])
    );
}

/// Change only the ancestor's clip while the child's rendered output remains
/// retained. The replayed child must not bring its previous parent's clip back.
pub(super) fn retained_frames(
    cx: &mut gpui::TestAppContext,
    mut rasterize: impl FnMut(&gpui::Scene, gpui::Size<gpui::DevicePixels>) -> Vec<u8>,
) {
    use gpui::{
        AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, Styled as _,
    };
    use std::{cell::Cell, rc::Rc};
    struct Child(Rc<Cell<usize>>);
    impl Render for Child {
        fn render(&mut self, _: &mut gpui::Window, _: &mut Context<Self>) -> impl IntoElement {
            self.0.set(self.0.get() + 1);
            gpui::div().size(px(64.)).bg(gpui::black())
        }
    }
    struct Parent {
        radius: f32,
        child: Entity<Child>,
    }
    impl Render for Parent {
        fn render(&mut self, _: &mut gpui::Window, _: &mut Context<Self>) -> impl IntoElement {
            gpui::div().size(px(64.)).bg(gpui::white()).child(
                gpui::div()
                    .size(px(64.))
                    .rounded(px(self.radius))
                    .overflow_hidden()
                    .child(self.child.clone()),
            )
        }
    }
    let renders = Rc::new(Cell::new(0));
    let window = cx.update(|cx| {
        let child = cx.new(|_| Child(renders.clone()));
        cx.open_window(Default::default(), |_, cx| {
            cx.new(|_| Parent { radius: 24., child })
        })
        .unwrap()
    });
    for radius in [24., 0., 24.] {
        window
            .update(cx, |parent, _, cx| {
                parent.radius = radius;
                cx.notify();
            })
            .unwrap();
        cx.update_window(window.into(), |_, window, cx| {
            window.draw(cx).clear(cx);
            let side = (64. * window.scale_factor()) as usize;
            let extent = gpui::size(
                gpui::DevicePixels(side as i32),
                gpui::DevicePixels(side as i32),
            );
            let pixels = rasterize(window.painted_scene(), extent);
            let corner = if radius == 0. {
                [0, 0, 0, 255]
            } else {
                [255, 255, 255, 255]
            };
            assert_eq!(pixel(&pixels, side, 1, 1), corner);
            assert_eq!(pixel(&pixels, side, side / 2, side / 2), [0, 0, 0, 255]);
        })
        .unwrap();
    }
    assert_eq!(
        renders.get(),
        1,
        "child must remain retained while ancestor clip changes"
    );
}

pub(super) fn square_fixture() -> gpui::Div {
    use gpui::{ParentElement as _, Styled as _};
    gpui::div().size(px(64.)).bg(gpui::white()).child(
        gpui::div()
            .size(px(64.))
            .rounded(px(24.))
            .overflow_hidden()
            .child(gpui::div().size(px(64.)).bg(gpui::black())),
    )
}

pub(super) fn tiled_scene(
    source: &gpui::Scene,
    extent: gpui::Size<gpui::DevicePixels>,
) -> gpui::Scene {
    let mut content = gpui::Scene::default();
    content.replay(0..source.len(), source);
    content.finish();
    let frame = gpui::LayerFrame {
        key: gpui::LayerKey(123),
        generation: 1,
        background: gpui::rgba(0xffffffff),
        tile_size: 512,
        content: content.into(),
        dirty_tiles: Vec::new(),
    };
    let coord = gpui::TileCoord { x: 0, y: 0 };
    let bounds = frame.tile_bounds(coord);
    let mask = gpui::Bounds::new(
        gpui::point(gpui::ScaledPixels(0.), gpui::ScaledPixels(0.)),
        gpui::size(
            gpui::ScaledPixels(extent.width.0 as f32),
            gpui::ScaledPixels(extent.height.0 as f32),
        ),
    );
    let mut scene = gpui::Scene::default();
    scene.insert_primitive(gpui::PolychromeSprite {
        order: 0,
        pad: 0,
        grayscale: false.into(),
        opacity: 1.,
        bounds,
        content_mask: gpui::ContentMask { bounds: mask },
        corner_radii: Default::default(),
        tile: gpui::AtlasTile {
            texture_id: gpui::layer_tile_texture_id(frame.key),
            tile_id: gpui::layer_tile_id(coord),
            padding: 0,
            bounds: gpui::Bounds::new(
                gpui::point(gpui::DevicePixels(0), gpui::DevicePixels(0)),
                gpui::size(gpui::DevicePixels(512), gpui::DevicePixels(512)),
            ),
        },
    });
    scene.layers.frames.push(frame);
    scene.finish();
    scene
}

pub(super) fn sibling_fixture() -> gpui::Div {
    use gpui::{ParentElement as _, Styled as _};
    gpui::div()
        .flex()
        .flex_wrap()
        .size(px(64.))
        .bg(gpui::white())
        .children((0..64).map(|_| {
            gpui::div()
                .size(px(8.))
                .rounded(px(3.))
                .overflow_hidden()
                .child(gpui::div().size(px(8.)).bg(gpui::black()))
        }))
}

#[gpui::test]
fn rounded_overflow_many_siblings_remain_independent(cx: &mut gpui::TestAppContext) {
    let (pixels, side) = render_div_fixture(cx, sibling_fixture);
    let cell = side / 8;
    for row in 0..8 {
        for col in 0..8 {
            assert_eq!(
                pixel(&pixels, side, col * cell, row * cell),
                [255, 255, 255, 255]
            );
            assert_eq!(
                pixel(&pixels, side, col * cell + cell / 2, row * cell + cell / 2),
                [0, 0, 0, 255]
            );
        }
    }
}

/// Exercise div layout and painting before rasterizing the complete scene.
/// A square child must not cover the cutout of its rounded overflow container.
#[gpui::test]
fn rounded_overflow_clips_square_child(cx: &mut gpui::TestAppContext) {
    let (pixels, side) = render_div_fixture(cx, square_fixture);
    assert_eq!(pixel(&pixels, side, side / 2, side / 2), [0, 0, 0, 255]);
    assert_eq!(
        pixel(&pixels, side, 1, 1),
        [255, 255, 255, 255],
        "square child leaked outside rounded overflow container"
    );
}

#[gpui::test]
fn rounded_overflow_visible_preserves_square_child(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};

    let (pixels, side) = render_div_fixture(cx, || {
        gpui::div().size(px(64.)).bg(gpui::white()).child(
            gpui::div()
                .size(px(64.))
                .rounded(px(24.))
                .child(gpui::div().size(px(64.)).bg(gpui::black())),
        )
    });
    assert_eq!(pixel(&pixels, side, 1, 1), [0, 0, 0, 255]);
}

/// The regression in zed#37480: an inset rectangular clip must not inherit
/// the radius of its parent. Its own corner lies inside the parent's shape.
#[gpui::test]
fn rounded_overflow_does_not_round_inset_child(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};

    let (pixels, side) = render_div_fixture(cx, || {
        gpui::div().size(px(64.)).bg(gpui::white()).child(
            gpui::div()
                .size(px(64.))
                .rounded(px(24.))
                .overflow_hidden()
                .p(px(16.))
                .child(
                    gpui::div()
                        .size(px(32.))
                        .overflow_hidden()
                        .child(gpui::div().size(px(32.)).bg(gpui::black())),
                ),
        )
    });
    let inset = side / 4;
    assert_eq!(
        pixel(&pixels, side, inset + 1, inset + 1),
        [0, 0, 0, 255],
        "inset child acquired its ancestor's rounded corner"
    );
}

/// Cropping a tall rounded container must not move its bottom corners up
/// to the bottom edge of the shorter rectangular ancestor.
#[gpui::test]
fn rounded_overflow_cropped_child_keeps_original_corners(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};

    let (pixels, side) = render_div_fixture(cx, || {
        gpui::div().size(px(64.)).bg(gpui::white()).child(
            gpui::div().w(px(64.)).h(px(32.)).overflow_hidden().child(
                gpui::div()
                    .w(px(64.))
                    .min_h(px(128.))
                    .rounded(px(24.))
                    .overflow_hidden()
                    .child(gpui::div().w(px(64.)).min_h(px(128.)).bg(gpui::black())),
            ),
        )
    });
    assert_eq!(
        pixel(&pixels, side, 1, side / 2 - 2),
        [0, 0, 0, 255],
        "ancestor clipping moved the child's bottom rounded corner"
    );
    assert_eq!(pixel(&pixels, side, 1, side / 2 + 2), [255, 255, 255, 255]);
    assert_eq!(pixel(&pixels, side, 1, 1), [255, 255, 255, 255]);
}

/// These siblings overlap, but their rounded masks belong to separate
/// branches. The later sibling cannot mask away the earlier one's pixels.
#[gpui::test]
fn rounded_overflow_overlapping_siblings_keep_independent_clips(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};

    let (pixels, side) = render_div_fixture(cx, || {
        gpui::div()
            .relative()
            .size(px(64.))
            .bg(gpui::white())
            .child(
                gpui::div()
                    .absolute()
                    .size(px(48.))
                    .rounded(px(16.))
                    .overflow_hidden()
                    .child(gpui::div().size(px(48.)).bg(gpui::red())),
            )
            .child(
                gpui::div()
                    .absolute()
                    .left(px(24.))
                    .top(px(16.))
                    .size(px(32.))
                    .rounded(px(12.))
                    .overflow_hidden()
                    .child(gpui::div().size(px(32.)).bg(gpui::black())),
            )
    });
    assert_eq!(pixel(&pixels, side, side / 2, side / 2), [0, 0, 0, 255]);
    let exposed = pixel(&pixels, side, side * 3 / 8 + 1, side / 4 + 1);
    assert!(
        exposed[0] > 200 && exposed[1] < 100 && exposed[2] < 100 && exposed[3] == 255,
        "later sibling's corner must reveal the earlier red sibling: {exposed:?}"
    );
    assert_eq!(pixel(&pixels, side, 1, 1), [255, 255, 255, 255]);
}

pub(super) fn render_with(
    cx: &mut gpui::TestAppContext,
    build: fn() -> gpui::Div,
    mut rasterize: impl FnMut(&gpui::Scene, gpui::Size<gpui::DevicePixels>) -> Vec<u8>,
) -> (Vec<u8>, usize) {
    use gpui::{AppContext as _, Context, IntoElement, Render};

    struct View(fn() -> gpui::Div);
    impl Render for View {
        fn render(&mut self, _: &mut gpui::Window, _: &mut Context<Self>) -> impl IntoElement {
            (self.0)()
        }
    }
    let window = cx.update(|cx| {
        cx.open_window(Default::default(), |_, cx| cx.new(|_| View(build)))
            .unwrap()
    });
    window
        .update(cx, |_, window, cx| {
            window.draw(cx).clear(cx);
            let side = (64. * window.scale_factor()) as usize;
            let size = gpui::size(
                gpui::DevicePixels(side as i32),
                gpui::DevicePixels(side as i32),
            );
            (rasterize(window.painted_scene(), size), side)
        })
        .unwrap()
}

#[gpui::test]
fn rounded_overflow_identical_nested_clips_do_not_darken_edges(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};

    let (single, side) = render_div_fixture(cx, || {
        gpui::div().size(px(64.)).bg(gpui::white()).child(
            gpui::div()
                .size(px(64.))
                .rounded(px(24.))
                .overflow_hidden()
                .child(gpui::div().size(px(64.)).bg(gpui::black())),
        )
    });
    let (nested, _) = render_div_fixture(cx, || {
        gpui::div().size(px(64.)).bg(gpui::white()).child(
            gpui::div()
                .size(px(64.))
                .rounded(px(24.))
                .overflow_hidden()
                .child(
                    gpui::div()
                        .size(px(64.))
                        .rounded(px(24.))
                        .overflow_hidden()
                        .child(gpui::div().size(px(64.)).bg(gpui::black())),
                ),
        )
    });
    assert_same_pixels(&nested, &single, side);
}

#[gpui::test]
fn rounded_overflow_clips_paths(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};

    let (pixels, side) = render_div_fixture(cx, || {
        gpui::div().size(px(64.)).bg(gpui::white()).child(
            gpui::div()
                .size(px(64.))
                .rounded(px(24.))
                .overflow_hidden()
                .child(
                    gpui::canvas(
                        |_, _, _| (),
                        |bounds, (), window, _| {
                            let mut path = Path::new(bounds.origin);
                            path.line_to(bounds.top_right());
                            path.line_to(bounds.bottom_right());
                            path.line_to(bounds.bottom_left());
                            path.line_to(bounds.origin);
                            window.paint_path(path, gpui::black());
                        },
                    )
                    .size(px(64.)),
                ),
        )
    });
    assert_eq!(pixel(&pixels, side, side / 2, side / 2), [0, 0, 0, 255]);
    assert_eq!(pixel(&pixels, side, 1, 1), [255, 255, 255, 255]);
}

/// Disjoint path batches may share one intermediate rasterization group.
/// A clip mask drawn between them must not overwrite the later path's pixels
/// or leave that later path under the child's clip.
#[gpui::test]
fn rounded_overflow_between_path_batches_restores_path_pixels(cx: &mut gpui::TestAppContext) {
    use gpui::{ParentElement as _, Styled as _};

    let (pixels, side) = render_div_fixture(cx, || {
        gpui::div()
            .relative()
            .size(px(64.))
            .bg(gpui::white())
            .child(
                gpui::canvas(
                    |_, _, _| (),
                    |bounds, (), window, _| {
                        let start = bounds.origin;
                        let mut path = Path::new(start);
                        path.line_to(start + gpui::point(px(16.), px(0.)));
                        path.line_to(start + gpui::point(px(16.), px(16.)));
                        path.line_to(start + gpui::point(px(0.), px(16.)));
                        path.line_to(start);
                        window.paint_path(path, gpui::rgb(0xff0000));
                    },
                )
                .absolute()
                .size(px(64.)),
            )
            .child(
                gpui::div()
                    .absolute()
                    .size(px(64.))
                    .rounded(px(24.))
                    .overflow_hidden()
                    .child(gpui::div().size(px(64.)).bg(gpui::black())),
            )
            .child(
                gpui::canvas(
                    |_, _, _| (),
                    |bounds, (), window, _| {
                        let start = bounds.origin + gpui::point(px(48.), px(0.));
                        let mut path = Path::new(start);
                        path.line_to(start + gpui::point(px(16.), px(0.)));
                        path.line_to(start + gpui::point(px(16.), px(16.)));
                        path.line_to(start + gpui::point(px(0.), px(16.)));
                        path.line_to(start);
                        window.paint_path(path, gpui::rgb(0x00ff00));
                    },
                )
                .absolute()
                .size(px(64.)),
            )
    });
    assert_eq!(pixel(&pixels, side, 1, 1), [255, 0, 0, 255]);
    assert_eq!(pixel(&pixels, side, side - 2, 1), [0, 255, 0, 255]);
    assert_eq!(pixel(&pixels, side, side / 2, side / 2), [0, 0, 0, 255]);
}