gpui-fast 0.1.12

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
// Modified by Longbridge for gpui-fast.
//! Carrying a layer's non-scene records (hitboxes, listeners, dispatch
//! nodes) through frames that composite it (M5).
//!
//! On a frame that composites a layer, its content is neither prepainted nor
//! painted, but what prepainting and painting it added to the last frame
//! besides the scene is still wanted: its hitboxes, for hit testing, its
//! dispatch nodes, for focus and key and action dispatch, and its mouse
//! listeners, cursor styles and tab stops. [`carry_prepaint`] and
//! [`carry_paint`] copy them from the rendered frame where the container's
//! children would add them, as [`Window::reuse_prepaint`] and
//! [`Window::reuse_paint`] do for a reused view, but without replaying the
//! scene, which the layer's tiles stand for, and with the hitboxes moved by
//! the scroll since the content was painted and clipped to the viewport.

use std::{ops::Range, rc::Rc};

use crate::{
    App, Bounds, EntityId, GlobalElementId, Hitbox, LayoutId, PaintIndex, Pixels, Point,
    PrepaintStateIndex, Window,
    fast::{dependencies::RenderDependencies, layers::input::LayerInput, retained::RetainedLayout},
};

/// Carries the prepaint records of the content of the layer of the
/// container `id`, composited at `scroll_offset` in `viewport`, into the
/// frame being drawn, where prepainting the content would add them.
pub(crate) fn carry_prepaint(
    window: &mut Window,
    id: &GlobalElementId,
    viewport: Bounds<Pixels>,
    scroll_offset: Point<Pixels>,
) {
    let Some(layer) = window.fast_layers.layers.get_mut(id) else {
        return;
    };
    let Some(record) = layer.record.as_mut() else {
        return;
    };
    let delta = scroll_offset - record.scroll_offset;
    let range = record.prepaint_range.clone();
    let a11y = record.a11y.clone();
    let input = &mut layer.input;
    let moved = delta != input.stale;
    input.scrolled = delta - input.stale;
    input.stale = delta;
    input.viewport = viewport;
    input.handle_offset.set(delta);
    // The hitboxes are the layer's, lent to the carry while it runs.
    let hitboxes = std::mem::take(&mut input.hitboxes);
    let carried = carry_prepaint_records(
        window,
        &range,
        &hitboxes,
        a11y.as_deref(),
        delta,
        viewport,
        !moved,
        true,
    );
    if let Some(layer) = window.fast_layers.layers.get_mut(id) {
        layer.input.hitboxes = hitboxes;
        if let Some(record) = layer.record.as_mut() {
            record.prepaint_range = carried;
        }
    }
}

/// Carries the prepaint records the rendered frame holds over `range` into
/// the frame being drawn, where prepainting what added them would add them
/// again, returning where they lie in it: `hitboxes`, which stand for those
/// of the range, moved by `delta` and clipped to `viewport`; its tooltips if
/// `tooltips`, a tooltip showing where its element was when it was
/// requested; its element states if `element_states`, which are dropped
/// otherwise; its line layouts and dispatch nodes; and the accessibility
/// nodes `a11y` kept of it, moved by `delta`.
#[allow(clippy::too_many_arguments)]
#[inline]
pub(crate) fn carry_prepaint_records(
    window: &mut Window,
    range: &Range<PrepaintStateIndex>,
    hitboxes: &[Hitbox],
    a11y: Option<&crate::fast::a11y::A11yStretch>,
    delta: Point<Pixels>,
    viewport: Bounds<Pixels>,
    tooltips: bool,
    element_states: bool,
) -> Range<PrepaintStateIndex> {
    let start = window.prepaint_index();
    let next = &mut window.next_frame;
    let rendered = &mut window.rendered_frame;
    next.hitboxes
        .extend(LayerInput::hitboxes_at(hitboxes, delta, viewport));
    // A scroll hides tooltips (spec §7, rule 5).
    let requests =
        &mut rendered.tooltip_requests[range.start.tooltips_index..range.end.tooltips_index];
    if tooltips {
        next.tooltip_requests
            .extend(requests.iter_mut().map(|request| request.take()));
    }
    if element_states {
        next.accessed_element_states.extend(
            rendered.accessed_element_states[range.start.accessed_element_states_index
                ..range.end.accessed_element_states_index]
                .iter()
                .cloned(),
        );
    }
    window
        .text_system
        .reuse_layouts(range.start.line_layout_index.clone()..range.end.line_layout_index.clone());
    let subtree = next.dispatch_tree.reuse_subtree(
        range.start.dispatch_tree_index..range.end.dispatch_tree_index,
        &mut rendered.dispatch_tree,
        window.focus,
    );
    if subtree.contains_focus() {
        next.focus = window.focus;
    }
    crate::fast::a11y::carry(window, a11y, delta);
    // Content that deferred draws is never composited (spec §6.5).
    debug_assert_eq!(
        range.start.deferred_draws_index,
        range.end.deferred_draws_index
    );
    start..window.prepaint_index()
}

/// Carries the paint records of the content of the layer of the container
/// `id` into the frame being drawn, where painting the content would add
/// them: what [`carry_prepaint`] carried the prepaint records of.
pub(crate) fn carry_paint(window: &mut Window, id: &GlobalElementId) {
    let frame = window.fast_layers.frame;
    let Some(layer) = window.fast_layers.layers.get_mut(id) else {
        return;
    };
    let Some(record) = layer.record.as_ref() else {
        return;
    };
    let range = record.paint_range.clone();
    let delta = layer.input.stale;
    let scrolled = layer.input.scrolled;
    let viewport = layer.input.viewport;
    let mut carried = carry_paint_records(window, &range, delta, scrolled, viewport, true);
    let Some(layer) = window.fast_layers.layers.get_mut(id) else {
        return;
    };
    if let Some(record) = layer.record.as_mut() {
        // The content's scene is the layer's, which is not carried.
        carried.start.scene_index = record.paint_range.start.scene_index;
        carried.end.scene_index = record.paint_range.end.scene_index;
        record.paint_range = carried;
    }
    layer.input.ranges_frame = Some(frame);
}

/// Carries the paint records the rendered frame holds over `range` into
/// the frame being drawn, where painting what added them would add them
/// again, returning where they lie in it: its window control hitboxes,
/// moved by `delta` and clipped to `viewport`, cursor styles, input
/// handlers, mouse listeners, element states if `element_states`, tab stops,
/// line layouts and, for tests, debug bounds, moved by `scrolled`: the
/// rendered frame holds them where it showed what added them, which has
/// scrolled by as much since. The scene is not carried.
#[inline]
pub(crate) fn carry_paint_records(
    window: &mut Window,
    range: &Range<PaintIndex>,
    delta: Point<Pixels>,
    scrolled: Point<Pixels>,
    viewport: Bounds<Pixels>,
    element_states: bool,
) -> Range<PaintIndex> {
    let start = window.paint_index();
    let next = &mut window.next_frame;
    let rendered = &mut window.rendered_frame;
    next.window_control_hitboxes.extend(
        rendered.window_control_hitboxes
            [range.start.fast.window_control_hitboxes..range.end.fast.window_control_hitboxes]
            .iter()
            .map(|(area, hitbox)| {
                let moved = LayerInput::hitboxes_at(std::slice::from_ref(hitbox), delta, viewport)
                    .next()
                    .expect("one hitbox");
                (*area, moved)
            }),
    );
    next.cursor_styles.extend(
        rendered.cursor_styles[range.start.cursor_styles_index..range.end.cursor_styles_index]
            .iter()
            .cloned(),
    );
    next.input_handlers.extend(
        rendered.input_handlers[range.start.input_handlers_index..range.end.input_handlers_index]
            .iter_mut()
            .map(|handler| handler.take()),
    );
    next.mouse_listeners.extend(
        rendered.mouse_listeners
            [range.start.mouse_listeners_index..range.end.mouse_listeners_index]
            .iter_mut()
            .map(|listener| listener.take()),
    );
    if element_states {
        next.accessed_element_states.extend(
            rendered.accessed_element_states[range.start.accessed_element_states_index
                ..range.end.accessed_element_states_index]
                .iter()
                .cloned(),
        );
    }
    next.tab_stops.replay(
        &rendered.tab_stops.insertion_history
            [range.start.tab_handle_index..range.end.tab_handle_index],
    );
    crate::fast::a11y::reuse_paint(window, range);
    let next = &mut window.next_frame;
    let rendered = &mut window.rendered_frame;
    #[cfg(any(test, feature = "test-support"))]
    for (selector, bounds) in
        &rendered.debug_bounds_records[range.start.debug_bounds_index..range.end.debug_bounds_index]
    {
        let moved = Bounds::new(bounds.origin + scrolled, bounds.size);
        next.record_debug_bounds(selector.clone(), moved);
    }
    let _ = scrolled;
    window
        .text_system
        .reuse_layouts(range.start.line_layout_index.clone()..range.end.line_layout_index.clone());
    start..window.paint_index()
}

/// Follows, in the records of the virtual lists' layers whose rows lie
/// inside a retained subtree the frame being drawn copies from the last
/// one, the copy of its prepaint records: those the rendered frame held over
/// `from` land from `to` on. A frame drawing the view holding a list from
/// the last one neither prepaints nor paints the list, but carries its rows'
/// records along, so that the next frame can carry them in turn.
///
/// A `div`'s layer is painted again after such a frame instead: whether its
/// content's hovers changed is told from the last frame's hit test, which a
/// scroll since leaves behind, while a list's rows foretell theirs (see
/// [`crate::fast::layers::lists`]).
pub(crate) fn follow_prepaint(
    window: &mut Window,
    from: &Range<PrepaintStateIndex>,
    to: &PrepaintStateIndex,
) {
    if window.fast_layers.layers.is_empty() {
        return;
    }
    let frame = window.fast_layers.frame;
    let inside = |range: &Range<PrepaintStateIndex>| {
        let (a, b) = (&range.start, &range.end);
        let (c, d) = (&from.start, &from.end);
        a.hitboxes_index >= c.hitboxes_index
            && b.hitboxes_index <= d.hitboxes_index
            && a.tooltips_index >= c.tooltips_index
            && b.tooltips_index <= d.tooltips_index
            && a.deferred_draws_index >= c.deferred_draws_index
            && b.deferred_draws_index <= d.deferred_draws_index
            && a.dispatch_tree_index >= c.dispatch_tree_index
            && b.dispatch_tree_index <= d.dispatch_tree_index
            && a.accessed_element_states_index >= c.accessed_element_states_index
            && b.accessed_element_states_index <= d.accessed_element_states_index
            && a.line_layout_index.lines_index >= c.line_layout_index.lines_index
            && b.line_layout_index.lines_index <= d.line_layout_index.lines_index
    };
    let shift = |range: &Range<PrepaintStateIndex>| {
        range.start.shifted(&from.start, to)..range.end.shifted(&from.start, to)
    };
    for layer in window.fast_layers.layers.values_mut() {
        let current = layer
            .input
            .ranges_frame
            .is_some_and(|painted| painted + 1 == frame);
        let Some(record) = layer.record.as_mut() else {
            continue;
        };
        if !current || !layer.rows.list || !inside(&record.prepaint_range) {
            continue;
        }
        record.prepaint_range = shift(&record.prepaint_range);
        layer.rows.follow_prepaint(&shift);
        layer.input.prepaint_followed = Some(frame);
    }
}

/// Follows, as [`follow_prepaint`] does, the copy of the paint records the
/// rendered frame held over `from` to `to` on. The scene index of a layer's
/// paint range is its own scene's, which is not copied.
pub(crate) fn follow_paint(window: &mut Window, from: &Range<PaintIndex>, to: &PaintIndex) {
    if window.fast_layers.layers.is_empty() {
        return;
    }
    let frame = window.fast_layers.frame;
    let inside = |range: &Range<PaintIndex>| {
        let (a, b) = (&range.start, &range.end);
        let (c, d) = (&from.start, &from.end);
        a.fast.window_control_hitboxes >= c.fast.window_control_hitboxes
            && b.fast.window_control_hitboxes <= d.fast.window_control_hitboxes
            && a.mouse_listeners_index >= c.mouse_listeners_index
            && b.mouse_listeners_index <= d.mouse_listeners_index
            && a.input_handlers_index >= c.input_handlers_index
            && b.input_handlers_index <= d.input_handlers_index
            && a.cursor_styles_index >= c.cursor_styles_index
            && b.cursor_styles_index <= d.cursor_styles_index
            && a.accessed_element_states_index >= c.accessed_element_states_index
            && b.accessed_element_states_index <= d.accessed_element_states_index
            && a.tab_handle_index >= c.tab_handle_index
            && b.tab_handle_index <= d.tab_handle_index
            && a.line_layout_index.lines_index >= c.line_layout_index.lines_index
            && b.line_layout_index.lines_index <= d.line_layout_index.lines_index
    };
    // A layer's scene index counts from its own scene, so it can lie before
    // the window's, where shifting it would underflow: it is shifted by none.
    let from_start = PaintIndex {
        scene_index: 0,
        ..from.start.clone()
    };
    let to = PaintIndex {
        scene_index: 0,
        ..to.clone()
    };
    let shift = |range: &Range<PaintIndex>| {
        range.start.shifted(&from_start, &to)..range.end.shifted(&from_start, &to)
    };
    for layer in window.fast_layers.layers.values_mut() {
        if layer.input.prepaint_followed != Some(frame) {
            continue;
        }
        let Some(record) = layer.record.as_mut() else {
            continue;
        };
        let mut paint_range = record.paint_range.clone();
        // Only its scene index can lie outside what was copied.
        paint_range.start.scene_index = from.start.scene_index;
        paint_range.end.scene_index = from.start.scene_index;
        if !inside(&paint_range) {
            continue;
        }
        record.paint_range = shift(&record.paint_range);
        layer.rows.follow_paint(&shift);
        layer.input.ranges_frame = Some(frame);
    }
}

/// How a view drawn inside a layer's content was laid out when the content
/// was painted, for frames that composite the layer to lay it out again
/// without rendering it.
///
/// Views inside a layer's content keep no retained record of their own (the
/// layer is their retention, see [`crate::fast::layers::paint::inside_layer`]),
/// and a frame that composites the layer neither prepaints nor paints them;
/// but the view holding the container renders it again, and lays out its
/// children, a child view among them (pattern A). Without a record to reuse,
/// the child view would render every frame; its layer keeps its layout
/// instead.
pub(crate) struct KeptLayout {
    pub(crate) layout: Rc<RetainedLayout>,
    /// Everything laying the view out and prepainting it read.
    pub(crate) dependencies: RenderDependencies,
}

/// Keeps how the view `id`, prepainted inside the content of the layer being
/// painted, was laid out, and what laying it out and prepainting it read.
pub(crate) fn keep_view_layout(
    window: &mut Window,
    id: GlobalElementId,
    layout: Rc<RetainedLayout>,
    dependencies: RenderDependencies,
) {
    if let Some(painting) = window.fast_layers.painting.as_mut() {
        painting.view_layouts.insert(
            id,
            KeptLayout {
                layout,
                dependencies,
            },
        );
    }
}

/// Lays out the view `id`, of entity `entity`, which is not dirty, as the
/// layer whose content it is drawn in kept it, if one kept it and nothing it
/// read changed since, returning its node and the layout: the frame is then
/// expected to composite the layer, and if it paints the content afresh
/// after all, the view is rendered at that layout where it is prepainted.
pub(crate) fn reuse_kept_layout(
    window: &mut Window,
    id: &GlobalElementId,
    entity: EntityId,
    cx: &mut App,
) -> Option<(LayoutId, Rc<RetainedLayout>)> {
    if window.fast_layers.layers.is_empty()
        || crate::fast::layers::paint::inside_layer(window)
        || !crate::fast::layers::active(window, cx)
        || window.retained_state.notified_entities.contains(&entity)
        || window.retained_state.dirty_subtrees.contains(id)
    {
        return None;
    }
    let kept = window.fast_layers.layers.values().find_map(|layer| {
        let record = layer.record.as_ref()?;
        record.view_layouts.get(id)
    })?;
    let layout = kept.layout.clone();
    if layout.rem_size != window.rem_size()
        || layout.text_style != window.text_style()
        || cx.dependencies_changed(&kept.dependencies, window.inside_notified_view())
        || crate::fast::layers::invalidate::offset_read_changed(window, &kept.dependencies)
    {
        return None;
    }
    let dependencies = kept.dependencies.clone();
    if !window
        .layout_engine
        .as_mut()
        .unwrap()
        .try_keep_retained(&layout.keys)
    {
        return None;
    }
    window
        .next_frame
        .accessed_element_states
        .extend(layout.element_states.iter().cloned());
    cx.replay_dependencies(&dependencies);
    Some((layout.root, layout))
}