use std::time::{Duration, Instant};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Redraw {
Now,
After(Duration),
Scheduled,
}
#[derive(Clone, Debug)]
pub struct FrameThrottle {
interval: Duration,
last_frame: Option<Instant>,
pending: bool,
}
impl FrameThrottle {
pub fn new(fps: u32) -> FrameThrottle {
FrameThrottle::with_interval(Duration::from_secs_f64(1.0 / fps.max(1) as f64))
}
pub fn with_interval(interval: Duration) -> FrameThrottle {
FrameThrottle { interval, last_frame: None, pending: false }
}
pub fn interval(&self) -> Duration {
self.interval
}
pub fn is_pending(&self) -> bool {
self.pending
}
pub fn request(&mut self, now: Instant) -> Redraw {
if self.pending {
return Redraw::Scheduled;
}
match self.last_frame {
Some(last) if now.saturating_duration_since(last) < self.interval => {
self.pending = true;
Redraw::After(self.interval - now.saturating_duration_since(last))
}
_ => {
self.last_frame = Some(now);
Redraw::Now
}
}
}
pub fn fire(&mut self, now: Instant) {
self.pending = false;
self.last_frame = Some(now);
}
pub fn frame_drawn(&mut self, now: Instant) {
self.last_frame = Some(now);
}
pub fn poll(&mut self, now: Instant) -> bool {
if self.pending && self.timeout(now) == Some(Duration::ZERO) {
self.fire(now);
true
} else {
false
}
}
pub fn timeout(&self, now: Instant) -> Option<Duration> {
if !self.pending {
return None;
}
let elapsed = self.last_frame.map_or(self.interval, |l| now.saturating_duration_since(l));
Some(self.interval.saturating_sub(elapsed))
}
}
impl Default for FrameThrottle {
fn default() -> Self {
FrameThrottle::new(60)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn coalesces_bursts() {
let t0 = Instant::now();
let ms = Duration::from_millis;
let mut th = FrameThrottle::with_interval(ms(16));
assert_eq!(th.request(t0), Redraw::Now);
assert_eq!(th.request(t0 + ms(4)), Redraw::After(ms(12)));
for i in 5..15 {
assert_eq!(th.request(t0 + ms(i)), Redraw::Scheduled);
}
assert_eq!(th.timeout(t0 + ms(10)), Some(ms(6)));
assert!(!th.poll(t0 + ms(10)));
assert!(th.poll(t0 + ms(16)));
assert!(!th.is_pending());
assert_eq!(th.request(t0 + ms(40)), Redraw::Now);
}
#[test]
fn fps_interval() {
assert_eq!(FrameThrottle::new(50).interval(), Duration::from_millis(20));
assert_eq!(FrameThrottle::new(0).interval(), Duration::from_secs(1));
}
}