Debian Codegraph @

third_party/rust/wgpu-hal/src/metal/time.rs

raw
1 //! Handling of global timestamps.
2
3 #[repr(C)]
4 #[derive(Debug)]
5 struct MachTimebaseInfo {
6 numerator: u32,
7 denominator: u32,
8 }
9 extern "C" {
10 fn mach_timebase_info(out: *mut MachTimebaseInfo) -> u32;
11 fn mach_absolute_time() -> u64;
12 }
13
14 /// A timer which uses mach_absolute_time to get its time. This is what the metal callbacks use.
15 #[derive(Debug)]
16 pub struct PresentationTimer {
17 scale: MachTimebaseInfo,
18 }
19 impl PresentationTimer {
20 /// Generates a new timer.
21 pub fn new() -> Self {
22 // Default to 1 / 1 in case the call to timebase_info fails.
23 let mut scale = MachTimebaseInfo {
24 numerator: 1,
25 denominator: 1,
26 };
27 unsafe { mach_timebase_info(&mut scale) };
28
29 Self { scale }
30 }
31
32 /// Gets the current time in nanoseconds.
33 pub fn get_timestamp_ns(&self) -> u128 {
34 let time = unsafe { mach_absolute_time() };
35
36 (time as u128 * self.scale.numerator as u128) / self.scale.denominator as u128
37 }
38 }