aboutsummaryrefslogtreecommitdiff
path: root/src/utils/vblank_throttle.rs
blob: ecf2053f6b7d4b7e7ce19bad6f3e2f1aa68b2d73 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! VBlank throttling.
//!
//! Some buggy drivers deliver VBlanks way earlier than necessary. This helper throttles the VBlank
//! in such cases to avoid tearing and to get more consistent timings.

use std::time::Duration;

use calloop::timer::{TimeoutAction, Timer};
use calloop::{LoopHandle, RegistrationToken};

use crate::niri::State;

#[derive(Debug)]
pub struct VBlankThrottle {
    event_loop: LoopHandle<'static, State>,
    last_vblank_timestamp: Option<Duration>,
    throttle_timer_token: Option<RegistrationToken>,
    printed_warning: bool,
    output_name: String,
}

impl VBlankThrottle {
    pub fn new(event_loop: LoopHandle<'static, State>, output_name: String) -> Self {
        Self {
            event_loop,
            last_vblank_timestamp: None,
            throttle_timer_token: None,
            printed_warning: false,
            output_name,
        }
    }

    pub fn throttle(
        &mut self,
        refresh_interval: Option<Duration>,
        timestamp: Duration,
        mut call_vblank: impl FnMut(&mut State) + 'static,
    ) -> bool {
        if let Some(token) = self.throttle_timer_token.take() {
            self.event_loop.remove(token);
        }

        if let Some((last, refresh)) = Option::zip(self.last_vblank_timestamp, refresh_interval) {
            let passed = timestamp.saturating_sub(last);
            if passed < refresh / 2 {
                if !self.printed_warning {
                    self.printed_warning = true;
                    warn!(
                        "output {} running faster than expected, throttling vblanks: \
                         expected refresh {refresh:?}, got vblank after {passed:?}",
                        self.output_name
                    );
                }

                let remaining = refresh - passed;
                let token = self
                    .event_loop
                    .insert_source(Timer::from_duration(remaining), move |_, _, state| {
                        call_vblank(state);
                        TimeoutAction::Drop
                    })
                    .unwrap();
                self.throttle_timer_token = Some(token);
                return true;
            }
        }

        self.last_vblank_timestamp = Some(timestamp);
        false
    }
}