Skip to main content

gpui/
present_feedback.rs

1//! vue-native fork: when each window present reached the screen, for an embedder that times its
2//! own frames against the display.
3//!
4//! A platform that reports it calls [`PresentListener::queued`] on the presenting thread for each
5//! present while feedback is wanted, then [`PresentListener::resolved`] once for that present, in
6//! present order per window. Times are the platform's monotonic clock in nanoseconds: QPC on
7//! Windows, `mach_absolute_time` on macOS.
8
9use std::sync::OnceLock;
10use std::sync::atomic::{AtomicBool, Ordering};
11
12/// What became of a window present.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub enum PresentOutcome {
15    /// First shown on a refresh.
16    Displayed {
17        /// When that refresh began.
18        at_ns: u64,
19        /// Its distance from the refresh before; the nominal period where that was not seen.
20        refresh_ns: u64,
21        /// False when `at_ns` was estimated.
22        exact: bool,
23    },
24    /// Replaced by a later present, or dropped with its swap chain, before any refresh showed it.
25    Discarded,
26}
27
28/// Hears window presents; see the module docs.
29pub trait PresentListener: Send + Sync {
30    /// On the presenting thread, after the present was queued and before it can be resolved.
31    /// `window` is the native handle: the HWND on Windows, the view's `CAMetalLayer` on macOS.
32    fn queued(&self, window: u64, present: u64);
33    /// Once per queued present, in present order per window.
34    fn resolved(&self, window: u64, present: u64, outcome: PresentOutcome);
35    /// On Windows, from the vsync thread once per refresh while feedback is wanted, after that
36    /// refresh's [`Self::resolved`] calls.
37    fn refresh(&self) {}
38}
39
40static LISTENER: OnceLock<Box<dyn PresentListener>> = OnceLock::new();
41static WANTED: AtomicBool = AtomicBool::new(false);
42static WAKER: OnceLock<fn()> = OnceLock::new();
43
44/// Installs the listener, once per process.
45pub fn set_present_listener(listener: Box<dyn PresentListener>) -> Result<(), &'static str> {
46    LISTENER
47        .set(listener)
48        .map_err(|_| "a present listener is already installed")
49}
50
51/// The installed listener.
52pub fn present_listener() -> Option<&'static dyn PresentListener> {
53    LISTENER.get().map(|l| &**l)
54}
55
56/// Whether presents are tracked and, on Windows, the vsync thread polls every refresh. Keep it
57/// off while nothing waits for an outcome, so an idle window costs nothing.
58pub fn want_present_feedback(on: bool) {
59    if WANTED.swap(on, Ordering::AcqRel) != on && on {
60        if let Some(wake) = WAKER.get() {
61            wake();
62        }
63    }
64}
65
66/// Whether a listener is installed and wants feedback.
67pub fn present_feedback_wanted() -> bool {
68    WANTED.load(Ordering::Acquire) && LISTENER.get().is_some()
69}
70
71/// For a platform: called when feedback becomes wanted, to wake a sleeping vsync thread.
72pub fn set_present_feedback_waker(wake: fn()) {
73    let _ = WAKER.set(wake);
74}