sk-tree 0.5.0

Skathi's retained node tree and the renderer's host functions
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//! The element side of `<sk-surface>`: per node, the [`LiveTile`] the surface's frame is written
//! into, the painted size reported to the producer and to JS, and the events.

use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::Arc;

use gpui::{App, RenderImage, Window};

use super::{display, feedback, Frame, SurfaceDisplay, SurfaceFormat, SurfaceHandle, SurfaceInfo};
use crate::live::{self, LiveTile, Shown};
use crate::tree;
use crate::windows::{self, WinKey};
use crate::EventPayload;

struct Node {
    surface: SurfaceHandle,
    window: WinKey,
    tile: Option<LiveTile>,
    /// The surface's `seq` the tile holds (or that cleared it).
    seen: u64,
    /// The id of the frame the tile holds.
    frame: u64,
    /// The content box, physical px, and the scale, from the last paint.
    painted: (u32, u32, f32),
    reported: (u32, u32),
    visible: bool,
    ready: bool,
    /// The display last reported to JS.
    display: Option<SurfaceDisplay>,
    /// The brightest the frame may be painted: `dynamic-range-limit` applied to the display.
    headroom: f32,
    /// The tile holds the frame's half-float values, painted up to `headroom`.
    hdr: bool,
    attached: bool,
    /// Why the last GPU frame was not shown, once logged.
    #[cfg_attr(not(any(windows, target_os = "macos")), allow(dead_code))]
    refused: Option<String>,
}

thread_local! {
    static NODES: RefCell<HashMap<u32, Node>> = RefCell::new(HashMap::new());
    /// Events a pop-out's draw raised, for the next primary tick to dispatch.
    static DEFERRED: RefCell<Vec<(u32, Event)>> = const { RefCell::new(Vec::new()) };
    /// Each window's native handle, as present feedback names it.
    static PRESENT_KEYS: RefCell<HashMap<WinKey, u64>> = RefCell::new(HashMap::new());
}

#[cfg(windows)]
enum Gpu {
    D3D11(super::d3d11::Texture),
    D3D12(super::d3d12::Ready),
}

#[cfg(target_os = "macos")]
enum Gpu {
    Metal(super::metal::Ready),
}

enum Event {
    Resize(u32, u32, f32),
    Ready(u32, u32),
    Lost(&'static str),
    Display(SurfaceDisplay),
}

/// Called from the build: binds node `id` to the surface `name` and returns the picture to paint.
pub(crate) fn note_build(id: u32, win: WinKey, name: &str) -> Option<Arc<RenderImage>> {
    NODES.with(|m| {
        let mut m = m.borrow_mut();
        if m.get(&id).is_some_and(|n| n.surface.name() != name) {
            if let Some(old) = m.remove(&id) {
                forget(old);
            }
        }
        if name.is_empty() {
            return None;
        }
        let node = m.entry(id).or_insert_with(|| Node {
            surface: super::surface(name),
            window: win,
            tile: None,
            seen: 0,
            frame: 0,
            painted: (0, 0, 1.0),
            reported: (0, 0),
            visible: false,
            ready: false,
            display: None,
            headroom: 1.0,
            hdr: false,
            attached: false,
            refused: None,
        });
        node.window = win;
        node.tile
            .as_ref()
            .filter(|t| t.window() == win && node.ready)
            .map(LiveTile::image)
    })
}

fn paint_headroom(id: u32) -> f32 {
    NODES.with(|m| {
        m.borrow()
            .get(&id)
            .filter(|n| n.hdr)
            .map_or(1.0, |n| n.headroom)
    })
}

/// Node `id`'s picture, painted with the headroom its frame may reach.
pub(crate) struct HdrPicture(pub(crate) u32, pub(crate) gpui::AnyElement);

impl gpui::Element for HdrPicture {
    type RequestLayoutState = ();
    type PrepaintState = ();

    fn id(&self) -> Option<gpui::ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _id: Option<&gpui::GlobalElementId>,
        _inspector_id: Option<&gpui::InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (gpui::LayoutId, ()) {
        (self.1.request_layout(window, cx), ())
    }

    fn prepaint(
        &mut self,
        _id: Option<&gpui::GlobalElementId>,
        _inspector_id: Option<&gpui::InspectorElementId>,
        _bounds: gpui::Bounds<gpui::Pixels>,
        _request_layout: &mut (),
        window: &mut Window,
        cx: &mut App,
    ) {
        self.1.prepaint(window, cx);
    }

    fn paint(
        &mut self,
        _id: Option<&gpui::GlobalElementId>,
        _inspector_id: Option<&gpui::InspectorElementId>,
        _bounds: gpui::Bounds<gpui::Pixels>,
        _request_layout: &mut (),
        _prepaint: &mut (),
        window: &mut Window,
        cx: &mut App,
    ) {
        let headroom = paint_headroom(self.0);
        window.with_image_headroom(headroom, |window| self.1.paint(window, cx));
    }
}

/// Called from the paint with the content box in window pixels.
pub(crate) fn note_painted(id: u32, width: f32, height: f32, scale: f32) {
    NODES.with(|m| {
        if let Some(node) = m.borrow_mut().get_mut(&id) {
            node.painted = (
                (width * scale).round().max(0.0) as u32,
                (height * scale).round().max(0.0) as u32,
                scale,
            );
        }
    });
}

fn attached(id: u32) -> bool {
    tree::read_tree(|t| t.connected(id))
}

fn forget(node: Node) {
    if let Some(tile) = node.tile {
        tile.retire();
    }
}

pub(crate) fn gc(live: impl Fn(u32) -> bool) {
    let gone: Vec<Node> = NODES.with(|m| {
        let mut m = m.borrow_mut();
        let ids: Vec<u32> = m.keys().copied().filter(|&id| !live(id)).collect();
        ids.into_iter().filter_map(|id| m.remove(&id)).collect()
    });
    for node in gone {
        forget(node);
    }
}

pub(crate) fn forget_window(key: WinKey) {
    display::forget(key);
    if let Some(window) = PRESENT_KEYS.with(|k| k.borrow_mut().remove(&key)) {
        feedback::with(|t| t.window_closed(window));
    }
    NODES.with(|m| {
        for node in m.borrow_mut().values_mut() {
            if node.tile.as_ref().is_some_and(|t| t.window() == key) {
                node.tile = None;
                node.seen = 0;
            }
        }
    });
}

/// Per frame, before the draw: writes new frames into visible nodes' tiles, reports sizes and
/// visibility, and fires the element events. Returns `true` when the window must draw.
pub(crate) fn tick(window: &mut Window, cx: &mut App) -> bool {
    if !super::any() {
        return false;
    }
    #[cfg(target_os = "macos")]
    super::metal::watch_device();
    let here = windows::key(window);
    let page_hidden = live::page_hidden();
    let here_shown = window.is_visible();
    let mut events = DEFERRED.with(|d| std::mem::take(&mut *d.borrow_mut()));
    let mut touch = Vec::new();
    let mut draw = false;
    let mut infos: HashMap<*const super::Shared, SurfaceInfo> = HashMap::new();
    let mut shown = Shown::Nowhere;
    NODES.with(|m| {
        for (&id, node) in m.borrow_mut().iter_mut() {
            let attached = attached(id);
            node.visible = attached
                && !page_hidden
                && (node.window != here || here_shown)
                && node.painted.0 > 0
                && node.painted.1 > 0
                && live::visible(id, node.window);
            if attached && node.painted.0 > 0 && (node.painted.0, node.painted.1) != node.reported {
                node.reported = (node.painted.0, node.painted.1);
                events.push((
                    id,
                    Event::Resize(node.painted.0, node.painted.1, node.painted.2),
                ));
            }
            if attached {
                follow_headroom(id, node, &mut touch);
            }
            node.attached = attached;
            if node.window != here {
                draw |= node.visible && node.surface.lock_state().seq != node.seen;
            }
        }
    });
    // After the switch, so the display reported says whether this window now draws HDR.
    let hdr = update_output(window, here);
    NODES.with(|m| {
        for (&id, node) in m.borrow_mut().iter_mut() {
            if node.attached {
                let on = display::of(node.window);
                if on.known && node.display.is_none_or(|d| on.differs(&d)) {
                    node.display = Some(on);
                    events.push((id, Event::Display(on)));
                }
                let limit = crate::inherit::dynamic_range_limit(id);
                let info = infos.entry(Arc::as_ptr(node.surface.shared())).or_default();
                // The largest visible element sets the size; a hidden one's last size only stands in.
                let rank = |visible: bool, w: u32, h: u32| (visible, u64::from(w) * u64::from(h));
                if rank(node.visible, node.painted.0, node.painted.1)
                    > rank(info.visible, info.width, info.height)
                {
                    info.width = node.painted.0;
                    info.height = node.painted.1;
                    info.scale = node.painted.2;
                    info.display = on;
                    info.dynamic_range_limit = limit;
                } else if !info.display.known {
                    info.display = on;
                }
                info.visible |= node.visible;
            }
            if node.window == here {
                let (s, event) = write(window, here, id, node, hdr, &mut touch);
                shown = shown.max(s);
                events.extend(event.map(|e| (id, e)));
            }
        }
    });
    live::note(shown);
    display::note_tick(infos.values().any(|info| info.visible));
    finish(touch, &mut draw);
    for surface in super::all() {
        let info = infos
            .get(&Arc::as_ptr(surface.shared()))
            .copied()
            .unwrap_or_default();
        surface.set_info(info);
    }
    carry(window, here);
    dispatch(events, window, cx);
    draw
}

/// Tells present feedback which frame of each surface `here`'s next present shows.
fn carry(window: &Window, here: WinKey) {
    if !feedback::watching() {
        return;
    }
    let Some(key) = PRESENT_KEYS.with(|k| {
        let mut keys = k.borrow_mut();
        if let Some(&key) = keys.get(&here) {
            return Some(key);
        }
        let key = feedback::window_key(window, here)?;
        keys.insert(here, key);
        Some(key)
    }) else {
        return;
    };
    let carrying = NODES.with(|m| {
        m.borrow()
            .values()
            .filter(|n| n.window == here && n.visible && n.ready && n.tile.is_some())
            .map(|n| (feedback::key(n.surface.shared()), n.frame))
            .collect()
    });
    feedback::with(|t| t.carry(key, carrying));
    #[cfg(not(any(windows, target_os = "macos")))]
    feedback::ticked(key, display::of(here).refresh_hz);
}

/// Samples `window`'s display; every tick, so `dynamic-range` answers without a surface.
pub(crate) fn sample_display(window: &Window) {
    display::sample(window, windows::key(window));
}

/// Moves a node to the headroom it may be painted with now, from the display's live reading.
fn follow_headroom(id: u32, node: &mut Node, touch: &mut Vec<u32>) {
    let limit = crate::inherit::dynamic_range_limit(id);
    let output = display::live(node.window).output_headroom;
    note_headroom(id, node, limit.headroom(output), touch);
}

/// A picture shown in HDR is repainted when its headroom moves.
fn note_headroom(id: u32, node: &mut Node, headroom: f32, touch: &mut Vec<u32>) {
    if (headroom - node.headroom).abs() > 1e-3 * headroom {
        node.headroom = headroom;
        if node.hdr && node.ready {
            touch.push(id);
        }
    }
}

/// Puts `here` in HDR while a visible half-float surface in it may be brighter than SDR white,
/// asking for the most headroom such a surface's `dynamic-range-limit` allows on its display.
fn update_output(window: &Window, here: WinKey) -> bool {
    let reach = display::reach(here);
    let headroom = NODES.with(|m| {
        m.borrow()
            .iter()
            .filter(|(_, n)| {
                n.window == here && n.visible && n.surface.format() == SurfaceFormat::Rgba16Float
            })
            .map(|(&id, _)| crate::inherit::dynamic_range_limit(id).headroom(reach))
            .fold(1.0, f32::max)
    });
    display::update_output(window, here, headroom)
}

/// A pop-out's half of [`tick`], from its own draw: its nodes' tiles live in its atlas.
pub(crate) fn window_tick(window: &mut Window) {
    if !super::any() {
        return;
    }
    let here = windows::key(window);
    let hdr = update_output(window, here);
    let mut touch = Vec::new();
    NODES.with(|m| {
        for (&id, node) in m.borrow_mut().iter_mut() {
            if node.window == here {
                if let (_, Some(event)) = write(window, here, id, node, hdr, &mut touch) {
                    DEFERRED.with(|d| d.borrow_mut().push((id, event)));
                }
            }
        }
    });
    let mut draw = false;
    finish(touch, &mut draw);
    carry(window, here);
}

fn finish(touch: Vec<u32>, draw: &mut bool) {
    if touch.is_empty() {
        return;
    }
    *draw = true;
    tree::with_tree(|t| {
        for id in touch {
            t.touch(id);
        }
    });
}

fn write(
    window: &mut Window,
    here: WinKey,
    id: u32,
    node: &mut Node,
    window_hdr: bool,
    touch: &mut Vec<u32>,
) -> (Shown, Option<Event>) {
    // A half-float frame goes to an HDR window as it is, to a standard one mapped to 8-bit: a
    // switch rewrites the frame already shown.
    let hdr = window_hdr && node.surface.format() == SurfaceFormat::Rgba16Float;
    if hdr != node.hdr {
        node.hdr = hdr;
        node.seen = 0;
    }
    #[cfg(windows)]
    let mut unmapped = None;
    // One lock: a present on another thread may resize the surface's texture in between.
    let locked = {
        let mut st = node.surface.lock_state();
        #[cfg(windows)]
        let texture = match st.frame {
            Some(Frame::D3D12 { .. }) => st.gpu12.ready().map(Gpu::D3D12),
            // Only a visible node's tick may draw a half-float frame's mapping.
            Some(Frame::Gpu { .. }) if node.visible && st.seq != node.seen => {
                match st.gpu_texture(hdr) {
                    Ok(texture) => texture.map(Gpu::D3D11),
                    Err(error) => {
                        unmapped = Some(error);
                        None
                    }
                }
            }
            _ => None,
        };
        #[cfg(target_os = "macos")]
        let texture = match st.frame {
            Some(Frame::Metal { .. }) => st.metal.ready().map(Gpu::Metal),
            _ => None,
        };
        #[cfg(not(any(windows, target_os = "macos")))]
        let texture = ();
        let copied = copied(&texture);
        let showable = match copied {
            Some((done, (_, frame, _))) => done.then_some(frame),
            None => st.frame.is_some().then_some(st.frame_id),
        };
        if let Some(frame) = showable {
            st.settle(frame);
            if node.visible && st.seq != node.seen {
                st.pin(frame);
            }
        }
        (
            st.seq,
            st.frame_id,
            st.frame.clone(),
            st.lost,
            texture,
            copied,
        )
    };
    let (mut seq, mut frame_id, frame, lost, texture, copied) = locked;
    #[cfg(windows)]
    if let Some(error) = unmapped {
        refused(
            node,
            format!("its half-float frame could not be mapped for the window: {error}"),
        );
    }
    if seq == node.seen {
        return (Shown::Nowhere, None);
    }
    // The copy into the surface's texture waits on the producer's fence on the GPU; handing it to
    // the atlas before it ran would stall the renderer's queue on the producer. While the newest
    // frame copies, the newest copied one is shown.
    let mut size = None;
    if let Some((done, (ready_seq, ready_frame, ready_size))) = copied {
        if node.visible {
            if !done {
                sk_runtime::frames::wake();
                return (Shown::Nowhere, None);
            }
            if ready_seq != seq {
                sk_runtime::frames::wake();
                if ready_seq <= node.seen {
                    return (Shown::Nowhere, None);
                }
                (seq, frame_id) = (ready_seq, ready_frame);
            }
            size = Some(ready_size);
        }
    }
    let Some(frame) = frame else {
        node.seen = seq;
        let was = std::mem::replace(&mut node.ready, false);
        if let Some(tile) = node.tile.take() {
            tile.retire();
            touch.push(id);
        }
        return (Shown::Nowhere, was.then_some(Event::Lost(lost)));
    };
    if !node.visible {
        return (Shown::Nowhere, None);
    }
    let size = size.unwrap_or_else(|| frame.size());
    let fits = node
        .tile
        .as_ref()
        .is_some_and(|t| t.window() == here && t.size() == size);
    if !fits {
        if let Some(old) = node.tile.take() {
            old.retire();
        }
        node.tile = Some(LiveTile::new(here, size.0, size.1));
    }
    let tile = node.tile.as_mut().expect("just made");
    #[cfg(not(any(windows, target_os = "macos")))]
    let () = texture;
    let wrote = match frame {
        Frame::Cpu {
            bgra,
            halves: Some(halves),
            ..
        } if hdr => tile
            .write_rgba16f(window, &halves, true)
            .unwrap_or_else(|| tile.write_bgra(window, bgra.to_vec(), true)),
        Frame::Cpu { bgra, .. } => tile.write_bgra(window, bgra.to_vec(), true),
        #[cfg(any(windows, target_os = "macos"))]
        _ => {
            let written = match texture {
                #[cfg(windows)]
                Some(Gpu::D3D11(texture)) => {
                    tile.write_gpu(window, gpui::GpuTextureHandle(texture.as_raw()), true)
                }
                #[cfg(windows)]
                Some(Gpu::D3D12(ready)) if hdr && ready.is_half_float() => {
                    let handle = ready.handle();
                    tile.write_d3d12(window, handle, Arc::new(ready), true)
                }
                #[cfg(windows)]
                Some(Gpu::D3D12(ready)) => match ready.for_atlas() {
                    Ok(ready) => {
                        let handle = ready.handle();
                        tile.write_d3d12(window, handle, Arc::new(ready), true)
                    }
                    Err(error) => Err(format!(
                        "its half-float frame could not be mapped for the window: {error}"
                    )),
                },
                #[cfg(target_os = "macos")]
                Some(Gpu::Metal(ready)) if hdr && ready.is_half_float() => {
                    let (handle, wait) = (ready.handle(), ready.ready());
                    tile.write_metal(window, handle, wait, Arc::new(ready), true)
                }
                #[cfg(target_os = "macos")]
                Some(Gpu::Metal(ready)) => match ready.for_atlas() {
                    Ok(ready) => {
                        let (handle, wait) = (ready.handle(), ready.ready());
                        tile.write_metal(window, handle, wait, Arc::new(ready), true)
                    }
                    Err(error) => Err(format!(
                        "its half-float frame could not be mapped for the window: {error}"
                    )),
                },
                None => return (Shown::Nowhere, None),
            };
            match written {
                Ok(shown) => {
                    node.refused = None;
                    shown
                }
                Err(why) => {
                    node.seen = seq;
                    refused(node, why);
                    return (Shown::Nowhere, None);
                }
            }
        }
    };
    node.seen = seq;
    node.frame = frame_id;
    node.surface.lock_state().note_shown(seq);
    let first = !std::mem::replace(&mut node.ready, true);
    let shown = if !fits || first || wrote == Shown::Draw {
        touch.push(id);
        Shown::Draw
    } else {
        wrote
    };
    (shown, first.then_some(Event::Ready(size.0, size.1)))
}

/// For a frame copied on the surface's own queue: whether the copy has run, and the frame's `seq`,
/// id and size.
#[cfg(any(windows, target_os = "macos"))]
fn copied(texture: &Option<Gpu>) -> Option<(bool, (u64, u64, (u32, u32)))> {
    match texture {
        #[cfg(windows)]
        Some(Gpu::D3D12(ready)) => Some((ready.done(), ready.frame())),
        #[cfg(target_os = "macos")]
        Some(Gpu::Metal(ready)) => Some((ready.done(), ready.frame())),
        _ => None,
    }
}

#[cfg(not(any(windows, target_os = "macos")))]
fn copied(_: &()) -> Option<(bool, (u64, u64, (u32, u32)))> {
    None
}

/// Logged once per surface node and reason.
#[cfg(any(windows, target_os = "macos"))]
fn refused(node: &mut Node, why: String) {
    if node.refused.as_deref() == Some(why.as_str()) {
        return;
    }
    let msg = format!(
        "<sk-surface> \"{}\": a GPU frame is not shown: {why}",
        node.surface.name()
    );
    log::warn!("{msg}");
    crate::log::record("window", msg.clone());
    crate::render::warn_compat("surface-gpu-upload", &msg);
    node.refused = Some(why);
}

/// `surfacedisplaychange`'s `key`: "hdr" when the display is in HDR, "output-hdr" when the window
/// draws HDR, and "vrr" or "fixed" when its refresh is known to be variable or not.
fn display_flags(on: &SurfaceDisplay) -> String {
    let refresh = match on.variable_refresh {
        None => None,
        Some(false) => Some("fixed"),
        Some(true) => Some("vrr"),
    };
    [
        on.hdr.then_some("hdr"),
        on.output_hdr.then_some("output-hdr"),
        refresh,
    ]
    .into_iter()
    .flatten()
    .collect::<Vec<_>>()
    .join(" ")
}

fn dispatch(events: Vec<(u32, Event)>, window: &mut Window, cx: &mut App) {
    for (id, event) in events {
        let listeners = tree::read_tree(|t| t.node(id).map(|n| n.listeners)).unwrap_or(0);
        let flags;
        let (code, payload) = match event {
            Event::Resize(w, h, scale) => (
                tree::EV_SURFACERESIZE,
                EventPayload {
                    a: w as f64,
                    b: h as f64,
                    c: scale as f64,
                    d: 0.0,
                    key: None,
                },
            ),
            Event::Ready(w, h) => (
                tree::EV_SURFACEREADY,
                EventPayload {
                    a: w as f64,
                    b: h as f64,
                    c: 0.0,
                    d: 0.0,
                    key: None,
                },
            ),
            Event::Lost(reason) => (
                tree::EV_SURFACELOST,
                EventPayload {
                    a: 0.0,
                    b: 0.0,
                    c: 0.0,
                    d: 0.0,
                    key: Some(reason),
                },
            ),
            Event::Display(on) => (
                tree::EV_SURFACEDISPLAYCHANGE,
                EventPayload {
                    a: on.headroom as f64,
                    b: on.potential_headroom as f64,
                    c: on.refresh_hz.unwrap_or(0.0) as f64,
                    d: on.sdr_white_nits.unwrap_or(0.0) as f64,
                    key: Some({
                        flags = display_flags(&on);
                        &flags
                    }),
                },
            ),
        };
        if listeners & (1u64 << code) != 0 {
            crate::render::js_dispatch_app(id, code, &payload, window, cx);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_node_follows_its_name_and_reports_physical_pixels() {
        let id = 0x7ffe_0001;
        assert!(note_build(id, 7, "tests:node-a").is_none(), "no frame yet");
        note_painted(id, 100.5, 50.0, 1.5);
        let painted = NODES.with(|m| m.borrow()[&id].painted);
        assert_eq!(painted, (151, 75, 1.5));
        note_build(id, 7, "tests:node-b");
        let (name, painted) = NODES.with(|m| {
            let n = &m.borrow()[&id];
            (n.surface.name().to_string(), n.painted)
        });
        assert_eq!(name, "tests:node-b");
        assert_eq!(painted, (0, 0, 1.0), "a new surface starts unmeasured");
        note_build(id, 7, "");
        assert_eq!(count_of(id), 0, "no name, no node");
    }

    #[test]
    fn a_surface_in_the_document_is_attached_in_every_feature_set() {
        let (inside, outside) = tree::with_tree(|t| {
            let root = t.create_root();
            let tag = t.intern("sk-surface");
            let inside = t.create_element(tag);
            t.insert(inside, root, 0);
            (inside, t.create_element(tag))
        });
        assert!(attached(inside));
        assert!(!attached(outside));
    }

    #[test]
    fn an_hdr_picture_follows_a_headroom_ramp_every_frame_while_reports_stay_spaced() {
        let (id, win) = (0x7ffe_0003, 0x7ffe);
        note_build(id, win, "tests:ramp");
        NODES.with(|m| {
            let mut m = m.borrow_mut();
            let node = m.get_mut(&id).expect("built");
            (node.hdr, node.ready) = (true, true);
        });
        let start = std::time::Instant::now();
        let frames = 192;
        let mut reports = Vec::new();
        for f in 0..=frames {
            let headroom = 1.0 + 5.11 * f as f64 / frames as f64;
            let at = start + std::time::Duration::from_micros(f * 1_000_000 / 120);
            display::sample_edr(win, headroom, at);
            let mut touch = Vec::new();
            NODES.with(|m| follow_headroom(id, m.borrow_mut().get_mut(&id).unwrap(), &mut touch));
            let painted = paint_headroom(id);
            assert!(
                (painted - headroom as f32).abs() <= 1e-3 * painted,
                "frame {f}: painted at {painted}, the screen allows {headroom}"
            );
            assert!(f == 0 || touch == [id], "frame {f} is not repainted");
            let reported = display::of(win).output_headroom;
            if reports.last() != Some(&reported) {
                reports.push(reported);
            }
        }
        assert!(reports.len() <= 10, "{reports:?}");
        display::forget(win);
        note_build(id, win, "");
    }

    fn count_of(id: u32) -> usize {
        NODES.with(|m| usize::from(m.borrow().contains_key(&id)))
    }
}