forge
Explore
Log in

Don't you dare go Hollow

tty: Throttle VBlanks on displays running faster than expected

Browse files at this commit

9d522ed51e75d1253793f9f5ec42b8faf36e47e7Ivan Molodetskikh and Christian Meissl, Scott McKendryParent: 8ef5cc2
src/backend/tty.rsmodified+16 -0
@@ -1633,6 +1633,22 @@ impl Tty {
1633 presentation_time1633 presentation_time
1634 };1634 };
1635 1635
1636+ if output_state
1637+ .vblank_throttle
1638+ .throttle(refresh_interval, time, move |state| {
1639+ let meta = DrmEventMetadata {
1640+ sequence: meta.sequence,
1641+ time: DrmEventTime::Monotonic(Duration::ZERO),
1642+ };
1643+
1644+ let tty = state.backend.tty();
1645+ tty.on_vblank(&mut state.niri, node, crtc, meta);
1646+ })
1647+ {
1648+ // Throttled.
1649+ return;
1650+ }
1651+
1636 let redraw_needed = match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) {1652 let redraw_needed = match mem::replace(&mut output_state.redraw_state, RedrawState::Idle) {
1637 RedrawState::WaitingForVBlank { redraw_needed } => redraw_needed,1653 RedrawState::WaitingForVBlank { redraw_needed } => redraw_needed,
1638 state @ (RedrawState::Idle1654 state @ (RedrawState::Idle
src/niri.rsmodified+3 -0
@@ -170,6 +170,7 @@ use crate::ui::screen_transition::{self, ScreenTransition};
170 use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRenderElement};170 use crate::ui::screenshot_ui::{OutputScreenshot, ScreenshotUi, ScreenshotUiRenderElement};
171 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};171 use crate::utils::scale::{closest_representable_scale, guess_monitor_scale};
172 use crate::utils::spawning::{CHILD_DISPLAY, CHILD_ENV};172 use crate::utils::spawning::{CHILD_DISPLAY, CHILD_ENV};
173+use crate::utils::vblank_throttle::VBlankThrottle;
173 use crate::utils::watcher::Watcher;174 use crate::utils::watcher::Watcher;
174 use crate::utils::xwayland::satellite::Satellite;175 use crate::utils::xwayland::satellite::Satellite;
175 use crate::utils::{176 use crate::utils::{
@@ -459,6 +460,7 @@ pub struct OutputState {
459 pub unfinished_animations_remain: bool,460 pub unfinished_animations_remain: bool,
460 /// Last sequence received in a vblank event.461 /// Last sequence received in a vblank event.
461 pub last_drm_sequence: Option<u32>,462 pub last_drm_sequence: Option<u32>,
463+ pub vblank_throttle: VBlankThrottle,
462 /// Sequence for frame callback throttling.464 /// Sequence for frame callback throttling.
463 ///465 ///
464 /// We want to send frame callbacks for each surface at most once per monitor refresh cycle.466 /// We want to send frame callbacks for each surface at most once per monitor refresh cycle.
@@ -3076,6 +3078,7 @@ impl Niri {
3076 unfinished_animations_remain: false,3078 unfinished_animations_remain: false,
3077 frame_clock: FrameClock::new(refresh_interval, vrr),3079 frame_clock: FrameClock::new(refresh_interval, vrr),
3078 last_drm_sequence: None,3080 last_drm_sequence: None,
3081+ vblank_throttle: VBlankThrottle::new(self.event_loop.clone(), name.connector.clone()),
3079 frame_callback_sequence: 0,3082 frame_callback_sequence: 0,
3080 backdrop_buffer: SolidColorBuffer::new(size, backdrop_color),3083 backdrop_buffer: SolidColorBuffer::new(size, backdrop_color),
3081 lock_render_state,3084 lock_render_state,
src/utils/mod.rsmodified+1 -0
@@ -38,6 +38,7 @@ pub mod scale;
38 pub mod signals;38 pub mod signals;
39 pub mod spawning;39 pub mod spawning;
40 pub mod transaction;40 pub mod transaction;
41+pub mod vblank_throttle;
41 pub mod watcher;42 pub mod watcher;
42 pub mod xwayland;43 pub mod xwayland;
43 44
src/utils/vblank_throttle.rsadded+71 -0
@@ -0,0 +1,71 @@
1+//! VBlank throttling.
2+//!
3+//! Some buggy drivers deliver VBlanks way earlier than necessary. This helper throttles the VBlank
4+//! in such cases to avoid tearing and to get more consistent timings.
5+
6+use std::time::Duration;
7+
8+use calloop::timer::{TimeoutAction, Timer};
9+use calloop::{LoopHandle, RegistrationToken};
10+
11+use crate::niri::State;
12+
13+#[derive(Debug)]
14+pub struct VBlankThrottle {
15+ event_loop: LoopHandle<'static, State>,
16+ last_vblank_timestamp: Option<Duration>,
17+ throttle_timer_token: Option<RegistrationToken>,
18+ printed_warning: bool,
19+ output_name: String,
20+}
21+
22+impl VBlankThrottle {
23+ pub fn new(event_loop: LoopHandle<'static, State>, output_name: String) -> Self {
24+ Self {
25+ event_loop,
26+ last_vblank_timestamp: None,
27+ throttle_timer_token: None,
28+ printed_warning: false,
29+ output_name,
30+ }
31+ }
32+
33+ pub fn throttle(
34+ &mut self,
35+ refresh_interval: Option<Duration>,
36+ timestamp: Duration,
37+ mut call_vblank: impl FnMut(&mut State) + 'static,
38+ ) -> bool {
39+ if let Some(token) = self.throttle_timer_token.take() {
40+ self.event_loop.remove(token);
41+ }
42+
43+ if let Some((last, refresh)) = Option::zip(self.last_vblank_timestamp, refresh_interval) {
44+ let passed = timestamp.saturating_sub(last);
45+ if passed < refresh / 2 {
46+ if !self.printed_warning {
47+ self.printed_warning = true;
48+ warn!(
49+ "output {} running faster than expected, throttling vblanks: \
50+ expected refresh {refresh:?}, got vblank after {passed:?}",
51+ self.output_name
52+ );
53+ }
54+
55+ let remaining = refresh - passed;
56+ let token = self
57+ .event_loop
58+ .insert_source(Timer::from_duration(remaining), move |_, _, state| {
59+ call_vblank(state);
60+ TimeoutAction::Drop
61+ })
62+ .unwrap();
63+ self.throttle_timer_token = Some(token);
64+ return true;
65+ }
66+ }
67+
68+ self.last_vblank_timestamp = Some(timestamp);
69+ false
70+ }
71+}