summaryrefslogtreecommitdiffstats
path: root/third_party/rust/wgpu-hal/src/metal/time.rs
blob: 5c6bec10cdce73533ffd9cd1ae901ad89e460930 (plain)
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
//! Handling of global timestamps.

#[repr(C)]
#[derive(Debug)]
struct MachTimebaseInfo {
    numerator: u32,
    denominator: u32,
}
extern "C" {
    fn mach_timebase_info(out: *mut MachTimebaseInfo) -> u32;
    fn mach_absolute_time() -> u64;
}

/// A timer which uses mach_absolute_time to get its time. This is what the metal callbacks use.
#[derive(Debug)]
pub struct PresentationTimer {
    scale: MachTimebaseInfo,
}
impl PresentationTimer {
    /// Generates a new timer.
    pub fn new() -> Self {
        // Default to 1 / 1 in case the call to timebase_info fails.
        let mut scale = MachTimebaseInfo {
            numerator: 1,
            denominator: 1,
        };
        unsafe { mach_timebase_info(&mut scale) };

        Self { scale }
    }

    /// Gets the current time in nanoseconds.
    pub fn get_timestamp_ns(&self) -> u128 {
        let time = unsafe { mach_absolute_time() };

        (time as u128 * self.scale.numerator as u128) / self.scale.denominator as u128
    }
}