forge
Explore
Log in

Don't you dare go Hollow

c65a4f162405d1b8c780925b723feeb481c87919Ivan MolodetskikhParent: 529a24c
Tracks the presentation time and allows querying the next presentation
time.
src/frame_clock.rsadded+60 -0
@@ -0,0 +1,60 @@
1+use std::num::NonZeroU64;
2+use std::time::Duration;
3+
4+use crate::utils::get_monotonic_time;
5+
6+#[derive(Debug)]
7+pub struct FrameClock {
8+ last_presentation_time: Option<Duration>,
9+ refresh_interval_ns: Option<NonZeroU64>,
10+}
11+
12+impl FrameClock {
13+ pub fn new(refresh_interval: Option<Duration>) -> Self {
14+ let refresh_interval_ns = if let Some(interval) = &refresh_interval {
15+ assert_eq!(interval.as_secs(), 0);
16+ Some(NonZeroU64::new(interval.subsec_nanos().into()).unwrap())
17+ } else {
18+ None
19+ };
20+
21+ Self {
22+ last_presentation_time: None,
23+ refresh_interval_ns,
24+ }
25+ }
26+
27+ pub fn presented(&mut self, presentation_time: Duration) {
28+ if presentation_time.is_zero() {
29+ // Not interested in these.
30+ return;
31+ }
32+
33+ self.last_presentation_time = Some(presentation_time);
34+ }
35+
36+ pub fn next_presentation_time(&self) -> Duration {
37+ let mut now = get_monotonic_time();
38+
39+ let Some(refresh_interval_ns) = self.refresh_interval_ns else {
40+ return now;
41+ };
42+ let Some(last_presentation_time) = self.last_presentation_time else {
43+ return now;
44+ };
45+
46+ let refresh_interval_ns = refresh_interval_ns.get();
47+
48+ if now <= last_presentation_time {
49+ // Got an early VBlank.
50+ now += Duration::from_nanos(refresh_interval_ns);
51+ // Assume two-frame early VBlanks don't happen. Overflow checks will catch them.
52+ }
53+
54+ let since_last = now - last_presentation_time;
55+ let since_last_ns =
56+ since_last.as_secs() * 1_000_000_000 + u64::from(since_last.subsec_nanos());
57+ let to_next_ns = (since_last_ns / refresh_interval_ns + 1) * refresh_interval_ns;
58+ last_presentation_time + Duration::from_nanos(to_next_ns)
59+ }
60+}
src/main.rsmodified+2 -0
@@ -4,11 +4,13 @@ extern crate tracing;
4 mod handlers;4 mod handlers;
5 5
6 mod backend;6 mod backend;
7+mod frame_clock;
7 mod grabs;8 mod grabs;
8 mod input;9 mod input;
9 mod layout;10 mod layout;
10 mod niri;11 mod niri;
11 mod tty;12 mod tty;
13+mod utils;
12 mod winit;14 mod winit;
13 15
14 use std::env;16 use std::env;
src/niri.rsmodified+5 -1
@@ -1,6 +1,7 @@
1 use std::collections::HashMap;1 use std::collections::HashMap;
2 use std::os::unix::io::AsRawFd;2 use std::os::unix::io::AsRawFd;
3 use std::sync::Arc;3 use std::sync::Arc;
4+use std::time::Duration;
4 5
5 use smithay::backend::renderer::element::render_elements;6 use smithay::backend::renderer::element::render_elements;
6 use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};7 use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
@@ -27,6 +28,7 @@ use smithay::wayland::shm::ShmState;
27 use smithay::wayland::socket::ListeningSocketSource;28 use smithay::wayland::socket::ListeningSocketSource;
28 29
29 use crate::backend::Backend;30 use crate::backend::Backend;
31+use crate::frame_clock::FrameClock;
30 use crate::layout::MonitorSet;32 use crate::layout::MonitorSet;
31 use crate::LoopData;33 use crate::LoopData;
32 34
@@ -71,6 +73,7 @@ pub struct OutputState {
71 // Set to `true` when the output was redrawn and is waiting for a VBlank. Upon VBlank a redraw73 // Set to `true` when the output was redrawn and is waiting for a VBlank. Upon VBlank a redraw
72 // will always be queued, so you cannot queue a redraw while waiting for a VBlank.74 // will always be queued, so you cannot queue a redraw while waiting for a VBlank.
73 pub waiting_for_vblank: bool,75 pub waiting_for_vblank: bool,
76+ pub frame_clock: FrameClock,
74 }77 }
75 78
76 impl Niri {79 impl Niri {
@@ -164,7 +167,7 @@ impl Niri {
164 }167 }
165 }168 }
166 169
167- pub fn add_output(&mut self, output: Output) {170+ pub fn add_output(&mut self, output: Output, refresh_interval: Option<Duration>) {
168 let x = self171 let x = self
169 .global_space172 .global_space
170 .outputs()173 .outputs()
@@ -180,6 +183,7 @@ impl Niri {
180 global: output.create_global::<Niri>(&self.display_handle),183 global: output.create_global::<Niri>(&self.display_handle),
181 queued_redraw: None,184 queued_redraw: None,
182 waiting_for_vblank: false,185 waiting_for_vblank: false,
186+ frame_clock: FrameClock::new(refresh_interval),
183 };187 };
184 let rv = self.output_state.insert(output, state);188 let rv = self.output_state.insert(output, state);
185 assert!(rv.is_none(), "output was already tracked");189 assert!(rv.is_none(), "output was already tracked");
src/tty.rsmodified+43 -8
@@ -1,13 +1,14 @@
1 use std::collections::{HashMap, HashSet};1 use std::collections::{HashMap, HashSet};
2 use std::os::fd::FromRawFd;2 use std::os::fd::FromRawFd;
3 use std::path::{Path, PathBuf};3 use std::path::{Path, PathBuf};
4+use std::time::Duration;
4 5
5 use anyhow::anyhow;6 use anyhow::anyhow;
6 use smithay::backend::allocator::dmabuf::Dmabuf;7 use smithay::backend::allocator::dmabuf::Dmabuf;
7 use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice};8 use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice};
8 use smithay::backend::allocator::{Format as DrmFormat, Fourcc};9 use smithay::backend::allocator::{Format as DrmFormat, Fourcc};
9 use smithay::backend::drm::compositor::DrmCompositor;10 use smithay::backend::drm::compositor::DrmCompositor;
10-use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent};11+use smithay::backend::drm::{DrmDevice, DrmDeviceFd, DrmEvent, DrmEventTime};
11 use smithay::backend::egl::{EGLContext, EGLDisplay};12 use smithay::backend::egl::{EGLContext, EGLDisplay};
12 use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface};13 use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface};
13 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};14 use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
@@ -17,7 +18,9 @@ use smithay::backend::session::{Event as SessionEvent, Session};
17 use smithay::backend::udev::{self, UdevBackend, UdevEvent};18 use smithay::backend::udev::{self, UdevBackend, UdevEvent};
18 use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpixel};19 use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties, Subpixel};
19 use smithay::reexports::calloop::{LoopHandle, RegistrationToken};20 use smithay::reexports::calloop::{LoopHandle, RegistrationToken};
20-use smithay::reexports::drm::control::{connector, crtc, ModeTypeFlags};21+use smithay::reexports::drm::control::{
22+ connector, crtc, Mode as DrmMode, ModeFlags, ModeTypeFlags,
23+};
21 use smithay::reexports::input::Libinput;24 use smithay::reexports::input::Libinput;
22 use smithay::reexports::nix::fcntl::OFlag;25 use smithay::reexports::nix::fcntl::OFlag;
23 use smithay::reexports::nix::libc::dev_t;26 use smithay::reexports::nix::libc::dev_t;
@@ -248,6 +251,16 @@ impl Tty {
248 error!("error marking frame as submitted: {err}");251 error!("error marking frame as submitted: {err}");
249 }252 }
250 253
254+ let presentation_time = match metadata.as_mut().unwrap().time {
255+ DrmEventTime::Monotonic(time) => time,
256+ DrmEventTime::Realtime(_) => {
257+ // Not supported.
258+
259+ // This value will be ignored in the frame clock code.
260+ Duration::ZERO
261+ }
262+ };
263+
251 // Send presentation time feedback.264 // Send presentation time feedback.
252 // catacomb265 // catacomb
253 // .windows266 // .windows
@@ -263,11 +276,9 @@ impl Tty {
263 })276 })
264 .unwrap()277 .unwrap()
265 .clone();278 .clone();
266- data.niri279+ let output_state = data.niri.output_state.get_mut(&output).unwrap();
267- .output_state280+ output_state.waiting_for_vblank = false;
268- .get_mut(&output)281+ output_state.frame_clock.presented(presentation_time);
269- .unwrap()
270- .waiting_for_vblank = false;
271 data.niri.queue_redraw(output);282 data.niri.queue_redraw(output);
272 }283 }
273 DrmEvent::Error(error) => error!("DRM error: {error}"),284 DrmEvent::Error(error) => error!("DRM error: {error}"),
@@ -425,7 +436,7 @@ impl Tty {
425 let res = device.surfaces.insert(crtc, compositor);436 let res = device.surfaces.insert(crtc, compositor);
426 assert!(res.is_none(), "crtc must not have already existed");437 assert!(res.is_none(), "crtc must not have already existed");
427 438
428- niri.add_output(output.clone());439+ niri.add_output(output.clone(), Some(refresh_interval(*mode)));
429 niri.queue_redraw(output);440 niri.queue_redraw(output);
430 441
431 Ok(())442 Ok(())
@@ -464,3 +475,27 @@ impl Tty {
464 }475 }
465 }476 }
466 }477 }
478+
479+fn refresh_interval(mode: DrmMode) -> Duration {
480+ let clock = mode.clock() as u64;
481+ let htotal = mode.hsync().2 as u64;
482+ let vtotal = mode.vsync().2 as u64;
483+
484+ let mut numerator = htotal * vtotal * 1_000_000;
485+ let mut denominator = clock;
486+
487+ if mode.flags().contains(ModeFlags::INTERLACE) {
488+ denominator *= 2;
489+ }
490+
491+ if mode.flags().contains(ModeFlags::DBLSCAN) {
492+ numerator *= 2;
493+ }
494+
495+ if mode.vscan() > 1 {
496+ numerator *= mode.vscan() as u64;
497+ }
498+
499+ let refresh_interval = (numerator + denominator / 2) / denominator;
500+ Duration::from_nanos(refresh_interval)
501+}
src/utils.rsadded+7 -0
@@ -0,0 +1,7 @@
1+use std::time::Duration;
2+
3+use smithay::reexports::nix::time::{clock_gettime, ClockId};
4+
5+pub fn get_monotonic_time() -> Duration {
6+ Duration::from(clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap())
7+}
src/winit.rsmodified+1 -1
@@ -110,7 +110,7 @@ impl Winit {
110 {110 {
111 warn!("error binding renderer wl_display: {err}");111 warn!("error binding renderer wl_display: {err}");
112 }112 }
113- niri.add_output(self.output.clone());113+ niri.add_output(self.output.clone(), None);
114 }114 }
115 115
116 fn dispatch(&mut self, niri: &mut Niri) {116 fn dispatch(&mut self, niri: &mut Niri) {