use std::time::Instant;
use winit::event_loop::ControlFlow;
pub(crate) fn paint_blocked(minimized: Option<bool>, surface_configured: bool) -> bool {
minimized == Some(true) && !surface_configured
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct LoopState {
pub(crate) capture_pending: bool,
pub(crate) continuous: bool,
pub(crate) app_wants_draw: bool,
pub(crate) paint_blocked: bool,
pub(crate) surface_retry_at: Option<Instant>,
pub(crate) app_deadline: Option<Instant>,
pub(crate) now: Instant,
}
pub(crate) fn next_control_flow(s: &LoopState) -> ControlFlow {
if let Some(retry) = s.surface_retry_at.filter(|&at| at > s.now) {
let wake = s.app_deadline.map_or(retry, |app| app.min(retry));
return ControlFlow::WaitUntil(wake);
}
let poll = s.capture_pending || (!s.paint_blocked && (s.continuous || s.app_wants_draw));
if poll {
ControlFlow::Poll
} else if let Some(t) = s.app_deadline {
ControlFlow::WaitUntil(t)
} else {
ControlFlow::Wait
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
fn idle(now: Instant) -> LoopState {
LoopState {
capture_pending: false,
continuous: false,
app_wants_draw: false,
paint_blocked: false,
surface_retry_at: None,
app_deadline: None,
now,
}
}
#[test]
fn only_a_minimized_window_with_an_unconfigured_surface_blocks_the_paint() {
assert!(paint_blocked(Some(true), false));
assert!(!paint_blocked(Some(false), false));
assert!(!paint_blocked(None, false), "unknown state paints as usual");
assert!(!paint_blocked(Some(false), true));
assert!(!paint_blocked(None, true));
}
#[test]
fn minimized_window_with_a_configured_surface_paints_and_polls_as_before() {
assert!(!paint_blocked(Some(true), true));
let s = LoopState {
continuous: true,
paint_blocked: paint_blocked(Some(true), true),
..idle(Instant::now())
};
assert_eq!(next_control_flow(&s), ControlFlow::Poll);
}
#[test]
fn surface_backoff_waits_instead_of_polling() {
let now = Instant::now();
let retry = now + Duration::from_millis(400);
let s = LoopState {
capture_pending: true,
continuous: true,
app_wants_draw: true,
surface_retry_at: Some(retry),
..idle(now)
};
assert_eq!(next_control_flow(&s), ControlFlow::WaitUntil(retry));
}
#[test]
fn earlier_app_deadline_still_wakes_during_backoff() {
let now = Instant::now();
let app = now + Duration::from_millis(100);
let s = LoopState {
continuous: true,
surface_retry_at: Some(now + Duration::from_millis(800)),
app_deadline: Some(app),
..idle(now)
};
assert_eq!(next_control_flow(&s), ControlFlow::WaitUntil(app));
}
#[test]
fn a_due_retry_no_longer_holds_the_loop() {
let now = Instant::now();
let s = LoopState {
continuous: true,
surface_retry_at: Some(now),
..idle(now)
};
assert_eq!(next_control_flow(&s), ControlFlow::Poll);
}
#[test]
fn blocked_paint_does_not_poll_for_frames_it_cannot_paint() {
let now = Instant::now();
let continuous = LoopState {
continuous: true,
app_wants_draw: true,
paint_blocked: paint_blocked(Some(true), false),
..idle(now)
};
assert_eq!(next_control_flow(&continuous), ControlFlow::Wait);
let capture = LoopState {
capture_pending: true,
paint_blocked: paint_blocked(Some(true), false),
..idle(now)
};
assert_eq!(next_control_flow(&capture), ControlFlow::Poll);
}
#[test]
fn normal_ladder_is_unchanged() {
let now = Instant::now();
assert_eq!(next_control_flow(&idle(now)), ControlFlow::Wait);
let wants = LoopState {
app_wants_draw: true,
..idle(now)
};
assert_eq!(next_control_flow(&wants), ControlFlow::Poll);
let continuous = LoopState {
continuous: true,
..idle(now)
};
assert_eq!(next_control_flow(&continuous), ControlFlow::Poll);
let capture = LoopState {
capture_pending: true,
..idle(now)
};
assert_eq!(next_control_flow(&capture), ControlFlow::Poll);
let t = now + Duration::from_secs(1);
let deadline = LoopState {
app_deadline: Some(t),
..idle(now)
};
assert_eq!(next_control_flow(&deadline), ControlFlow::WaitUntil(t));
}
}