sk-tree 0.4.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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! Which surface frames reached the screen ([`super::SurfaceHandle::on_frame`]).
//!
//! The tick records, per window, the newest frame of each surface its next present carries; gpui
//! reports each window present it queued and, once the display has shown or skipped it, what
//! became of it (`gpui::present_feedback`). A frame is presented by the first window present
//! shown that carried it. One that no window, present or surface still holds, never shown, was
//! superseded. Events wait for their elders, so each surface hears them in frame order.

use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock, Weak};
use std::time::Duration;

use gpui::present_feedback::{PresentListener, PresentOutcome};

use super::{lock, FrameDropReason, FrameEvent, FramePrecision, Shared};

/// A surface, by its shared state's address.
pub(crate) type Key = usize;

pub(crate) fn key(shared: &Arc<Shared>) -> Key {
    Arc::as_ptr(shared) as Key
}

#[derive(Default)]
struct Track {
    /// Frames not yet delivered, with their event once known.
    open: BTreeMap<u64, Option<FrameEvent>>,
    /// The surface's own frame.
    current: Option<u64>,
    /// The time of the last frame shown, for the display clock.
    last_shown: Option<u64>,
}

#[derive(Default)]
struct Win {
    carrying: HashMap<Key, u64>,
    records: VecDeque<(u64, Vec<(Key, u64)>)>,
}

#[derive(Default)]
pub(crate) struct Tracker {
    surfaces: HashMap<Key, Track>,
    windows: HashMap<u64, Win>,
    page_hidden: bool,
    events: Vec<(Key, FrameEvent)>,
}

impl Tracker {
    pub(crate) fn track(&mut self, s: Key) {
        self.surfaces.entry(s).or_default();
    }

    pub(crate) fn untrack(&mut self, s: Key) {
        self.surfaces.remove(&s);
        self.events.retain(|(k, _)| *k != s);
    }

    /// A present of frame `frame`; `visible` is the surface's `info().visible`.
    pub(crate) fn put(&mut self, s: Key, frame: u64, visible: bool) {
        let Some(track) = self.surfaces.get_mut(&s) else {
            return;
        };
        track.open.insert(frame, None);
        let old = track.current.replace(frame);
        if !visible || self.page_hidden {
            self.resolve(
                s,
                frame,
                FrameEvent::dropped(frame, FrameDropReason::Hidden),
            );
        }
        if let Some(old) = old {
            self.release(s, old);
        }
    }

    /// The surface's frame was withdrawn.
    pub(crate) fn cleared(&mut self, s: Key) {
        if let Some(old) = self.surfaces.get_mut(&s).and_then(|t| t.current.take()) {
            self.release(s, old);
        }
    }

    /// What `window`'s next present shows of each surface, from its tick.
    pub(crate) fn carry(&mut self, window: u64, carrying: HashMap<Key, u64>) {
        let carrying: HashMap<Key, u64> = carrying
            .into_iter()
            .filter(|(s, _)| self.surfaces.contains_key(s))
            .collect();
        if carrying.is_empty() && !self.windows.contains_key(&window) {
            return;
        }
        let win = self.windows.entry(window).or_default();
        let old = std::mem::replace(&mut win.carrying, carrying);
        for (s, frame) in old {
            if self.windows[&window].carrying.get(&s) != Some(&frame) {
                self.release(s, frame);
            }
        }
    }

    pub(crate) fn queued(&mut self, window: u64, present: u64) {
        let Some(win) = self.windows.get(&window) else {
            return;
        };
        let frames: Vec<(Key, u64)> = win
            .carrying
            .iter()
            .map(|(&s, &f)| (s, f))
            .filter(|&(s, f)| self.unresolved(s, f))
            .collect();
        if !frames.is_empty() {
            let win = self.windows.get_mut(&window).expect("looked up");
            win.records.push_back((present, frames));
        }
    }

    pub(crate) fn resolved(
        &mut self,
        window: u64,
        present: u64,
        outcome: PresentOutcome,
        nominal: impl Fn(Key) -> Option<Duration>,
    ) {
        loop {
            let Some(win) = self.windows.get_mut(&window) else {
                return;
            };
            let Some(&(first, _)) = win.records.front() else {
                return;
            };
            if first > present {
                return;
            }
            let (_, frames) = win.records.pop_front().expect("looked at");
            match outcome {
                PresentOutcome::Displayed {
                    at_ns,
                    refresh_ns,
                    exact,
                } if first == present => {
                    for (s, frame) in frames {
                        let refresh = match refresh_ns {
                            0 => nominal(s).unwrap_or_default(),
                            ns => Duration::from_nanos(ns),
                        };
                        self.shown(s, frame, at_ns, refresh, exact);
                    }
                }
                _ => {
                    for (s, frame) in frames {
                        self.release(s, frame);
                    }
                }
            }
        }
    }

    /// The surface's `info().visible`, each tick.
    pub(crate) fn visibility(&mut self, s: Key, visible: bool) {
        if !visible {
            self.drop_all(s, FrameDropReason::Hidden);
        }
    }

    pub(crate) fn set_page_hidden(&mut self, hidden: bool) {
        self.page_hidden = hidden;
        if hidden {
            for s in self.keys() {
                self.drop_all(s, FrameDropReason::Hidden);
            }
        }
    }

    pub(crate) fn device_lost(&mut self) {
        for s in self.keys() {
            self.drop_all(s, FrameDropReason::DeviceLost);
        }
    }

    pub(crate) fn window_closed(&mut self, window: u64) {
        let Some(win) = self.windows.remove(&window) else {
            return;
        };
        let mut held: Vec<Key> = win.carrying.keys().copied().collect();
        held.extend(
            win.records
                .iter()
                .flat_map(|(_, f)| f.iter().map(|(s, _)| *s)),
        );
        held.sort_unstable();
        held.dedup();
        for s in held {
            self.drop_all(s, FrameDropReason::Closed);
        }
    }

    pub(crate) fn closed(&mut self) {
        for s in self.keys() {
            self.drop_all(s, FrameDropReason::Closed);
        }
    }

    /// Whether anything waits on a present outcome or a delivery.
    pub(crate) fn busy(&self) -> bool {
        !self.events.is_empty() || self.surfaces.values().any(|t| !t.open.is_empty())
    }

    pub(crate) fn take_events(&mut self) -> Vec<(Key, FrameEvent)> {
        std::mem::take(&mut self.events)
    }

    pub(crate) fn last_shown(&self, s: Key) -> Option<u64> {
        self.surfaces.get(&s)?.last_shown
    }

    fn keys(&self) -> Vec<Key> {
        self.surfaces.keys().copied().collect()
    }

    fn unresolved(&self, s: Key, frame: u64) -> bool {
        self.surfaces
            .get(&s)
            .is_some_and(|t| matches!(t.open.get(&frame), Some(None)))
    }

    fn drop_all(&mut self, s: Key, reason: FrameDropReason) {
        let Some(track) = self.surfaces.get(&s) else {
            return;
        };
        let frames: Vec<u64> = track
            .open
            .iter()
            .filter(|(_, e)| e.is_none())
            .map(|(&f, _)| f)
            .collect();
        for frame in frames {
            self.resolve(s, frame, FrameEvent::dropped(frame, reason));
        }
    }

    fn shown(&mut self, s: Key, frame: u64, at: u64, refresh: Duration, exact: bool) {
        if !self.unresolved(s, frame) {
            return;
        }
        let track = self.surfaces.get_mut(&s).expect("unresolved");
        track.last_shown = Some(at);
        let older: Vec<u64> = track
            .open
            .range(..frame)
            .filter(|(_, e)| e.is_none())
            .map(|(&f, _)| f)
            .collect();
        for f in older {
            self.resolve(s, f, FrameEvent::dropped(f, FrameDropReason::Superseded));
        }
        let precision = if exact {
            FramePrecision::Exact
        } else {
            FramePrecision::Estimated
        };
        self.resolve(
            s,
            frame,
            FrameEvent::Presented {
                frame,
                shown_at: at,
                refresh_interval: refresh,
                precision,
            },
        );
    }

    /// `frame` lost one holder; with none left and never shown, it was superseded.
    fn release(&mut self, s: Key, frame: u64) {
        if !self.unresolved(s, frame) {
            return;
        }
        let held = self.surfaces[&s].current == Some(frame)
            || self.windows.values().any(|w| {
                w.carrying.get(&s) == Some(&frame)
                    || w.records.iter().any(|(_, f)| f.contains(&(s, frame)))
            });
        if !held {
            self.resolve(
                s,
                frame,
                FrameEvent::dropped(frame, FrameDropReason::Superseded),
            );
        }
    }

    fn resolve(&mut self, s: Key, frame: u64, event: FrameEvent) {
        let Some(track) = self.surfaces.get_mut(&s) else {
            return;
        };
        match track.open.get_mut(&frame) {
            Some(slot @ None) => *slot = Some(event),
            _ => return,
        }
        while let Some(entry) = track.open.first_entry() {
            let Some(event) = *entry.get() else {
                break;
            };
            entry.remove();
            self.events.push((s, event));
        }
    }
}

struct Global {
    tracker: Mutex<Tracker>,
    /// Held while events are handed out, so each surface hears them in order.
    delivering: Mutex<()>,
    surfaces: Mutex<HashMap<Key, Weak<Shared>>>,
    #[cfg(not(windows))]
    wake: std::sync::Condvar,
}

fn global() -> &'static Global {
    static GLOBAL: OnceLock<Global> = OnceLock::new();
    GLOBAL.get_or_init(|| Global {
        tracker: Mutex::new(Tracker::default()),
        delivering: Mutex::new(()),
        surfaces: Mutex::new(HashMap::new()),
        #[cfg(not(windows))]
        wake: std::sync::Condvar::new(),
    })
}

/// Runs `f` on the tracker, then lets gpui poll while anything waits, and (off Windows) wakes the
/// delivery thread.
pub(crate) fn with<R>(f: impl FnOnce(&mut Tracker) -> R) -> R {
    let g = global();
    let mut t = lock(&g.tracker);
    let r = f(&mut t);
    settle(&t);
    r
}

fn settle(t: &MutexGuard<'_, Tracker>) {
    gpui::present_feedback::want_present_feedback(t.busy());
    #[cfg(not(windows))]
    if !t.events.is_empty() {
        global().wake.notify_all();
    }
}

/// Starts tracking `shared`'s frames: the first watcher installs the listener.
pub(crate) fn watch(shared: &Arc<Shared>) {
    static INSTALL: std::sync::Once = std::sync::Once::new();
    INSTALL.call_once(|| {
        let _ = gpui::present_feedback::set_present_listener(Box::new(Listener));
        #[cfg(not(windows))]
        let _ = std::thread::Builder::new()
            .name("sk-frame-feedback".into())
            .spawn(|| loop {
                let g = global();
                let mut t = lock(&g.tracker);
                while t.events.is_empty() {
                    t = g.wake.wait(t).unwrap_or_else(|p| p.into_inner());
                }
                drop(t);
                deliver();
            });
    });
    WATCHING.store(true, Ordering::Relaxed);
    let k = key(shared);
    lock(&global().surfaces).insert(k, Arc::downgrade(shared));
    with(|t| t.track(k));
}

pub(crate) fn unwatch(shared: &Arc<Shared>) {
    with(|t| t.untrack(key(shared)));
}

pub(crate) fn set_page_hidden(hidden: bool) {
    with(|t| t.set_page_hidden(hidden));
}

/// The app is quitting: every frame still waiting is dropped, and told here, since the vsync
/// thread may already be gone.
pub(crate) fn app_closed() {
    if watching() {
        with(|t| t.closed());
        deliver();
    }
}

/// Hands out the events resolved so far.
pub(crate) fn deliver() {
    let g = global();
    let _order = lock(&g.delivering);
    let events = {
        let mut t = lock(&g.tracker);
        let events = t.take_events();
        settle(&t);
        events
    };
    if events.is_empty() {
        return;
    }
    let surfaces = lock(&g.surfaces).clone();
    for (s, event) in events {
        if let Some(shared) = surfaces.get(&s).and_then(Weak::upgrade) {
            shared.frame_event(event);
        }
    }
}

struct Listener;

impl PresentListener for Listener {
    fn queued(&self, window: u64, present: u64) {
        with(|t| t.queued(window, present));
    }

    fn resolved(&self, window: u64, present: u64, outcome: PresentOutcome) {
        // Read before the tracker's lock: a present takes the surface's state, then the tracker.
        let mut nominal = HashMap::new();
        if matches!(outcome, PresentOutcome::Displayed { refresh_ns: 0, .. }) {
            let surfaces = lock(&global().surfaces).clone();
            for (s, shared) in &surfaces {
                let hz = shared
                    .upgrade()
                    .and_then(|shared| lock(&shared.state).info.display.refresh_hz);
                if let Some(hz) = hz.filter(|hz| *hz > 0.0) {
                    nominal.insert(*s, Duration::from_secs_f64(1.0 / f64::from(hz)));
                }
            }
        }
        with(|t| t.resolved(window, present, outcome, |s| nominal.get(&s).copied()));
    }

    fn refresh(&self) {
        deliver();
    }
}

static WATCHING: AtomicBool = AtomicBool::new(false);

/// Whether any surface was ever watched; the tick skips its bookkeeping until then.
pub(crate) fn watching() -> bool {
    WATCHING.load(Ordering::Relaxed)
}

/// The window's native handle as gpui's presents name it: the HWND, or the `NSView`'s layer.
/// Elsewhere gpui reports no presents and the window's key stands in.
pub(crate) fn window_key(window: &gpui::Window, fallback: u64) -> Option<u64> {
    use raw_window_handle::{HasWindowHandle, RawWindowHandle};
    let handle = <gpui::Window as HasWindowHandle>::window_handle(window).ok()?;
    match handle.as_raw() {
        RawWindowHandle::Win32(w) => Some(w.hwnd.get() as u64),
        #[cfg(target_os = "macos")]
        RawWindowHandle::AppKit(h) => {
            let view = h.ns_view.as_ptr().cast::<objc2::runtime::AnyObject>();
            // SAFETY: the view lives as long as its window, which is ticking.
            let layer: *mut objc2::runtime::AnyObject = unsafe { objc2::msg_send![&*view, layer] };
            (!layer.is_null()).then_some(layer as u64)
        }
        _ => Some(fallback),
    }
}

/// Where gpui reports no presents: the frames a window carries count as shown about a refresh
/// after its tick.
#[cfg(not(any(windows, target_os = "macos")))]
pub(crate) fn ticked(window: u64, refresh_hz: Option<f32>) {
    static PRESENTS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let period = refresh_hz
        .filter(|hz| *hz > 0.0)
        .map_or(16_666_667, |hz| (1e9 / f64::from(hz)) as u64);
    let present = PRESENTS.fetch_add(1, Ordering::Relaxed) + 1;
    with(|t| {
        t.queued(window, present);
        t.resolved(
            window,
            present,
            PresentOutcome::Displayed {
                at_ns: monotonic_ns() + period,
                refresh_ns: period,
                exact: false,
            },
            |_| None,
        );
    });
}

/// Now, in the clock frames are timed in: QPC on Windows, `mach_absolute_time` on macOS,
/// `CLOCK_MONOTONIC` elsewhere, in nanoseconds.
pub fn monotonic_ns() -> u64 {
    platform::now_ns()
}

/// The display's last vblank and nominal period, for `info().display`.
pub(crate) fn display_clock(s: Key, refresh_hz: Option<f32>) -> (Option<u64>, Option<Duration>) {
    let nominal = refresh_hz
        .filter(|hz| *hz > 0.0)
        .map(|hz| Duration::from_secs_f64(1.0 / f64::from(hz)));
    if let Some((vblank, period)) = platform::compositor_clock() {
        let agrees = nominal.is_none_or(|n| {
            (n.as_secs_f64() - period.as_secs_f64()).abs() <= 0.01 * n.as_secs_f64()
        });
        if agrees {
            return (Some(vblank), Some(period));
        }
    }
    let Some(period) = nominal else {
        return (None, None);
    };
    let last = lock(&global().tracker).last_shown(s);
    let vblank = last.map(|last| {
        let p = period.as_nanos().max(1) as u64;
        let now = monotonic_ns();
        last + now.saturating_sub(last) / p * p
    });
    (vblank, Some(period))
}

#[cfg(windows)]
mod platform {
    use std::sync::OnceLock;
    use std::time::Duration;

    use windows::Win32::Foundation::HWND;
    use windows::Win32::Graphics::Dwm::{DwmGetCompositionTimingInfo, DWM_TIMING_INFO};
    use windows::Win32::System::Performance::{QueryPerformanceCounter, QueryPerformanceFrequency};

    fn frequency() -> i64 {
        static F: OnceLock<i64> = OnceLock::new();
        *F.get_or_init(|| {
            let mut f = 0;
            // SAFETY: always succeeds on XP and later.
            unsafe { QueryPerformanceFrequency(&mut f).ok() };
            f.max(1)
        })
    }

    /// As mpv's `mp_raw_time_ns` scales QPC.
    fn ns(ticks: i64) -> u64 {
        let (f, t) = (frequency(), ticks.max(0));
        ((t / f) * 1_000_000_000 + (t % f) * 1_000_000_000 / f) as u64
    }

    pub(super) fn now_ns() -> u64 {
        let mut now = 0;
        // SAFETY: always succeeds on XP and later.
        unsafe { QueryPerformanceCounter(&mut now).ok() };
        ns(now)
    }

    pub(super) fn compositor_clock() -> Option<(u64, Duration)> {
        let mut timing = DWM_TIMING_INFO {
            cbSize: std::mem::size_of::<DWM_TIMING_INFO>() as u32,
            ..Default::default()
        };
        // SAFETY: a sized struct; a null window asks for the compositor's own timing.
        unsafe { DwmGetCompositionTimingInfo(HWND::default(), &mut timing) }.ok()?;
        let period = timing.qpcRefreshPeriod as i64;
        (period > 0 && timing.qpcVBlank > 0).then(|| {
            (
                ns(timing.qpcVBlank as i64),
                Duration::from_nanos(ns(period)),
            )
        })
    }
}

#[cfg(target_os = "macos")]
mod platform {
    use std::sync::OnceLock;
    use std::time::Duration;

    #[repr(C)]
    #[derive(Default)]
    struct Timebase {
        numer: u32,
        denom: u32,
    }

    extern "C" {
        fn mach_absolute_time() -> u64;
        fn mach_timebase_info(info: *mut Timebase) -> i32;
    }

    pub(super) fn now_ns() -> u64 {
        static BASE: OnceLock<(u64, u64)> = OnceLock::new();
        let (numer, denom) = *BASE.get_or_init(|| {
            let mut tb = Timebase::default();
            // SAFETY: writes the struct it is given.
            unsafe { mach_timebase_info(&mut tb) };
            (u64::from(tb.numer.max(1)), u64::from(tb.denom.max(1)))
        });
        // SAFETY: no arguments.
        let ticks = unsafe { mach_absolute_time() };
        (u128::from(ticks) * u128::from(numer) / u128::from(denom)) as u64
    }

    pub(super) fn compositor_clock() -> Option<(u64, Duration)> {
        None
    }
}

#[cfg(not(any(windows, target_os = "macos")))]
mod platform {
    use std::time::Duration;

    #[repr(C)]
    struct Timespec {
        sec: i64,
        nsec: i64,
    }

    extern "C" {
        fn clock_gettime(clock: i32, out: *mut Timespec) -> i32;
    }

    pub(super) fn now_ns() -> u64 {
        let mut t = Timespec { sec: 0, nsec: 0 };
        // SAFETY: CLOCK_MONOTONIC (1) into a timespec.
        unsafe { clock_gettime(1, &mut t) };
        (t.sec.max(0) as u64) * 1_000_000_000 + t.nsec.max(0) as u64
    }

    pub(super) fn compositor_clock() -> Option<(u64, Duration)> {
        None
    }
}

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

    const W: u64 = 1;
    const A: Key = 10;
    const B: Key = 20;

    fn shown(at: u64) -> PresentOutcome {
        PresentOutcome::Displayed {
            at_ns: at,
            refresh_ns: 1000,
            exact: true,
        }
    }

    fn presented(frame: u64, at: u64) -> FrameEvent {
        FrameEvent::Presented {
            frame,
            shown_at: at,
            refresh_interval: Duration::from_nanos(1000),
            precision: FramePrecision::Exact,
        }
    }

    fn dropped(frame: u64, reason: FrameDropReason) -> FrameEvent {
        FrameEvent::dropped(frame, reason)
    }

    fn tracker(surfaces: &[Key]) -> Tracker {
        let mut t = Tracker::default();
        for &s in surfaces {
            t.track(s);
        }
        t
    }

    fn frames(t: &mut Tracker, s: Key) -> Vec<FrameEvent> {
        let (mine, rest): (Vec<_>, Vec<_>) =
            t.take_events().into_iter().partition(|(k, _)| *k == s);
        t.events = rest;
        mine.into_iter().map(|(_, e)| e).collect()
    }

    /// One tick: `carrying` is written, then the window presents as `present`.
    fn tick(t: &mut Tracker, carrying: &[(Key, u64)], present: u64) {
        t.carry(W, carrying.iter().copied().collect());
        t.queued(W, present);
    }

    fn outcome(t: &mut Tracker, present: u64, o: PresentOutcome) {
        t.resolved(W, present, o, |_| None);
    }

    #[test]
    fn a_frame_is_presented_by_the_first_shown_present_that_carried_it() {
        let mut t = tracker(&[A]);
        t.put(A, 1, true);
        tick(&mut t, &[(A, 1)], 1);
        tick(&mut t, &[(A, 1)], 2);
        outcome(&mut t, 1, PresentOutcome::Discarded);
        assert!(
            frames(&mut t, A).is_empty(),
            "a later present still carries it"
        );
        outcome(&mut t, 2, shown(5000));
        assert_eq!(frames(&mut t, A), [presented(1, 5000)]);
        assert!(!t.busy());
    }

    #[test]
    fn a_frame_replaced_before_any_present_showed_it_is_superseded_and_events_keep_order() {
        let mut t = tracker(&[A]);
        t.put(A, 1, true);
        tick(&mut t, &[(A, 1)], 1);
        t.put(A, 2, true);
        t.put(A, 3, true);
        assert!(
            frames(&mut t, A).is_empty(),
            "2 is superseded at once, but waits for 1"
        );
        tick(&mut t, &[(A, 3)], 2);
        outcome(&mut t, 1, shown(1000));
        assert_eq!(
            frames(&mut t, A),
            [presented(1, 1000), dropped(2, FrameDropReason::Superseded)]
        );
        // Present 2 was discarded and 4 replaced 3 meanwhile.
        t.put(A, 4, true);
        tick(&mut t, &[(A, 4)], 3);
        outcome(&mut t, 2, PresentOutcome::Discarded);
        outcome(&mut t, 3, shown(3000));
        assert_eq!(
            frames(&mut t, A),
            [dropped(3, FrameDropReason::Superseded), presented(4, 3000)]
        );
    }

    #[test]
    fn a_frame_shown_in_another_window_first_supersedes_older_ones() {
        const W2: u64 = 2;
        let mut t = tracker(&[A]);
        t.put(A, 1, true);
        tick(&mut t, &[(A, 1)], 1);
        t.put(A, 2, true);
        t.carry(W2, [(A, 2)].into());
        t.queued(W2, 1);
        t.resolved(W2, 1, shown(2000), |_| None);
        assert_eq!(
            frames(&mut t, A),
            [dropped(1, FrameDropReason::Superseded), presented(2, 2000)]
        );
        outcome(&mut t, 1, shown(3000));
        assert!(frames(&mut t, A).is_empty(), "each frame resolves once");
    }

    #[test]
    fn surfaces_in_one_window_resolve_on_their_own() {
        let mut t = tracker(&[A, B]);
        t.put(A, 1, true);
        t.put(B, 1, true);
        tick(&mut t, &[(A, 1), (B, 1)], 1);
        t.put(B, 2, true);
        tick(&mut t, &[(A, 1), (B, 2)], 2);
        outcome(&mut t, 1, PresentOutcome::Discarded);
        outcome(&mut t, 2, shown(2000));
        assert_eq!(frames(&mut t, A), [presented(1, 2000)]);
        assert_eq!(
            frames(&mut t, B),
            [dropped(1, FrameDropReason::Superseded), presented(2, 2000)]
        );
    }

    #[test]
    fn hidden_surfaces_drop_what_waits_and_every_present_until_shown() {
        let mut t = tracker(&[A]);
        t.put(A, 1, true);
        tick(&mut t, &[(A, 1)], 1);
        t.visibility(A, false);
        t.put(A, 2, false);
        outcome(&mut t, 1, shown(1000));
        assert_eq!(
            frames(&mut t, A),
            [
                dropped(1, FrameDropReason::Hidden),
                dropped(2, FrameDropReason::Hidden)
            ]
        );
        t.set_page_hidden(true);
        t.put(A, 3, true);
        assert_eq!(frames(&mut t, A), [dropped(3, FrameDropReason::Hidden)]);
        t.set_page_hidden(false);
        t.put(A, 4, true);
        tick(&mut t, &[(A, 4)], 2);
        outcome(&mut t, 2, shown(2000));
        assert_eq!(frames(&mut t, A), [presented(4, 2000)]);
    }

    #[test]
    fn a_lost_device_or_a_closed_window_drops_every_waiting_frame() {
        let mut t = tracker(&[A, B]);
        t.put(A, 1, true);
        tick(&mut t, &[(A, 1)], 1);
        t.put(A, 2, true);
        t.device_lost();
        outcome(&mut t, 1, shown(1000));
        assert_eq!(
            frames(&mut t, A),
            [
                dropped(1, FrameDropReason::DeviceLost),
                dropped(2, FrameDropReason::DeviceLost)
            ]
        );
        t.put(A, 3, true);
        t.put(B, 1, true);
        tick(&mut t, &[(A, 3), (B, 1)], 2);
        t.window_closed(W);
        assert_eq!(frames(&mut t, A), [dropped(3, FrameDropReason::Closed)]);
        assert_eq!(frames(&mut t, B), [dropped(1, FrameDropReason::Closed)]);
        outcome(&mut t, 2, shown(2000));
        assert!(t.take_events().is_empty());
        t.put(A, 4, true);
        t.closed();
        assert_eq!(frames(&mut t, A), [dropped(4, FrameDropReason::Closed)]);
        assert!(!t.busy());
    }
}