use std::{
collections::VecDeque,
f32::consts::TAU,
num::NonZeroIsize,
sync::{Arc, Mutex},
time::{Duration, Instant},
};
use raw_window_handle::{
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, Win32WindowHandle, WindowHandle, WindowsDisplayHandle,
};
use winit::{dpi::PhysicalPosition, event::WindowEvent};
use super::{
frame_pacing::{FramePacing, PresentedFrame, frame_due},
loop_wait::{LoopNeeds, LoopWait},
};
use crate::{
animation::{AnimatableProperty, Animation, Easing, Keyframes, KeyframesId, StepPosition},
app::{
App, Tree,
component::Component,
ctx::{Ctx, Interval},
events::MouseButton,
render_engine::RenderEngine,
},
components::{Rect, Row},
layout::render_list::RenderList,
node::{Element, transform::Decomposed},
};
const REFRESH: Duration = Duration::from_nanos(16_666_667);
const TURN: KeyframesId = KeyframesId::new(41);
const SPINNER: (f32, f32) = (20.0, 20.0);
struct TestSurface;
impl HasWindowHandle for TestSurface {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
let handle = Win32WindowHandle::new(NonZeroIsize::new(1).expect("1 is not zero"));
Ok(unsafe { WindowHandle::borrow_raw(handle.into()) })
}
}
impl HasDisplayHandle for TestSurface {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
Ok(unsafe { DisplayHandle::borrow_raw(WindowsDisplayHandle::new().into()) })
}
}
#[derive(Clone, Copy, Debug)]
struct Frame {
pass_at: Instant,
angle: f32,
}
struct Display {
epoch: Instant,
last_present: Option<Instant>,
pass_at: Instant,
render_cost: Duration,
frames: Vec<Frame>,
vblanks: Vec<u128>,
}
impl Display {
fn vblank_index(&self, at: Instant) -> u128 {
vblank_index(self.epoch, REFRESH, at)
}
fn vblank_after(&self, at: Instant) -> Instant {
let refreshes = self.vblank_index(at);
self.epoch + REFRESH * u32::try_from(refreshes).expect("a test runs for less than two years")
}
}
fn vblank_index(epoch: Instant, refresh: Duration, at: Instant) -> u128 {
(at - epoch).as_nanos() / refresh.as_nanos() + 1
}
fn skipped_vblanks(vblanks: &[u128]) -> u128 {
vblanks.windows(2).map(|pair| pair[1] - pair[0] - 1).sum()
}
struct VsyncEngine(Arc<Mutex<Display>>);
impl RenderEngine for VsyncEngine {
fn resize(&mut self, _width: u32, _height: u32) {}
fn render(&mut self, list: &RenderList, _window: WindowHandle<'_>, _display: DisplayHandle<'_>) -> bool {
let mut display = self.0.lock().expect("the display is used on one thread");
if let Some(previous) = display.last_present {
sleep_until(display.vblank_after(previous));
}
std::thread::sleep(display.render_cost);
let presented = Instant::now();
display.last_present = Some(presented);
let vblank = display.vblank_index(presented);
display.vblanks.push(vblank);
let [a, b, _, _] = list.rects.first().map_or([1.0, 0.0, 0.0, 1.0], |rect| rect.transform);
let frame = Frame {
pass_at: display.pass_at,
angle: b.atan2(a).rem_euclid(TAU),
};
display.frames.push(frame);
true
}
}
fn sleep_until(at: Instant) {
let now = Instant::now();
if at > now {
std::thread::sleep(at - now);
}
}
struct Shell {
app: App,
tree: Tree,
pacing: FramePacing,
display: Arc<Mutex<Display>>,
clicks: VecDeque<Instant>,
timer_lateness: Vec<Duration>,
sleeps: usize,
}
impl Shell {
fn new(tree: Tree) -> Self {
let display = Arc::new(Mutex::new(Display {
epoch: Instant::now(),
last_present: None,
pass_at: Instant::now(),
render_cost: Duration::ZERO,
frames: Vec::new(),
vblanks: Vec::new(),
}));
let mut shell = Self {
app: App::new(),
tree,
pacing: FramePacing::default(),
display: display.clone(),
clicks: VecDeque::new(),
timer_lateness: Vec::new(),
sleeps: 0,
};
shell.tree.resize(200, 100);
shell
.tree
.set_render_engine_factory(move || Box::new(VsyncEngine(display.clone())));
shell.tree.request_redraw();
shell
}
fn run_for(&mut self, duration: Duration) {
let end = Instant::now() + duration;
loop {
let now = Instant::now();
if now >= end {
return;
}
self.turn(now, end);
}
}
fn turn(&mut self, now: Instant, end: Instant) {
while self.clicks.front().is_some_and(|at| *at <= now) {
self.clicks.pop_front();
self.tree.mouse_down(SPINNER.0, SPINNER.1, MouseButton::Left);
self.tree.mouse_up(SPINNER.0, SPINNER.1, MouseButton::Left);
}
let timer_due = self.tree.next_timer_at().filter(|due| *due <= now);
self.tree.tick_timers();
if let Some(due) = timer_due {
self.timer_lateness.push(Instant::now() - due);
}
self.tree.tick_futures();
self.pacing.request_frame(&mut self.tree, || REFRESH, now);
self.tree.tick_scheduled_redraw(now);
if self.tree.needs_redraw() {
let started = Instant::now();
self.display.lock().expect("one thread").pass_at = started;
if self.tree.pass(&mut self.app, &TestSurface).rendered {
self.pacing.presented(PresentedFrame {
started,
presented: Instant::now(),
});
}
return;
}
let needs = LoopNeeds {
polling: self.tree.needs_polling(),
next_redraw: self.tree.next_scheduled_redraw(),
next_timer: self.tree.next_timer_at(),
..LoopNeeds::default()
};
let wake = match LoopWait::of(needs, now) {
LoopWait::Poll => return,
LoopWait::WaitUntil(at) => at.min(end),
LoopWait::Wait => end,
};
let wake = self.clicks.front().map_or(wake, |click| wake.min(*click));
self.sleeps += 1;
sleep_until(wake);
}
fn frames(&self) -> Vec<Frame> {
self.display.lock().expect("one thread").frames.clone()
}
fn set_render_cost(&mut self, cost: Duration) {
self.display.lock().expect("one thread").render_cost = cost;
}
fn vblanks(&self) -> Vec<u128> {
self.display.lock().expect("one thread").vblanks.clone()
}
}
fn turn_keyframes() -> Keyframes {
Keyframes::new(TURN)
.frame(0.0, |f| {
f.set(AnimatableProperty::Transform, Decomposed::IDENTITY.with_rotate(0.0));
})
.frame(1.0, |f| {
f.set(AnimatableProperty::Transform, Decomposed::IDENTITY.with_rotate(TAU));
})
}
fn steps(count: u32) -> Easing {
Easing::Steps {
count,
position: StepPosition::JumpEnd,
}
}
fn spinner(easing: Easing) -> Tree {
let mut tree = Tree::new();
tree.register_keyframes(turn_keyframes());
tree.set_root(
Row::new().child(
Rect::new(40.0, 40.0)
.background("#2563eb")
.animation(Animation::new(TURN).duration_ms(1000).easing(easing).infinite()),
),
);
tree
}
fn pose(angle: f32, count: u32) -> u32 {
((angle / (TAU / count as f32)).round() as u32) % count
}
#[test]
fn a_spinner_of_twelve_steps_a_second_presents_about_twelve_frames_a_second() {
let mut shell = Shell::new(spinner(steps(12)));
shell.run_for(Duration::from_secs(1));
let frames = shell.frames();
assert!(
(11..=14).contains(&frames.len()),
"a 12-step turn per second draws a frame per step, not one per refresh: {} frames in 1 s",
frames.len()
);
for pair in frames.windows(2) {
assert_ne!(
pose(pair[0].angle, 12),
pose(pair[1].angle, 12),
"every frame shows the next step: {frames:?}"
);
}
}
#[test]
fn a_continuous_spinner_advances_every_refresh_through_paced_frames() {
let mut shell = Shell::new(spinner(Easing::Linear));
shell.run_for(Duration::from_millis(500));
let frames = shell.frames();
assert!(
(24..=32).contains(&frames.len()),
"a continuous animation runs at the display's rate: {} frames in 0.5 s",
frames.len()
);
let advanced = frames.windows(2).filter(|pair| pair[0].angle != pair[1].angle).count();
assert_eq!(advanced, frames.len() - 1, "every frame turns the spinner: {frames:?}");
assert!(
shell.sleeps >= frames.len() - 1,
"the loop sleeps until each frame is due instead of waiting for the swapchain: {} sleeps for {} frames",
shell.sleeps,
frames.len()
);
}
#[test]
fn the_loop_waits_for_an_event_once_an_animation_ends() {
let mut tree = Tree::new();
tree.register_keyframes(turn_keyframes());
tree.set_root(
Row::new().child(Rect::new(40.0, 40.0).animation(Animation::new(TURN).duration_ms(300).easing(steps(3)))),
);
let mut shell = Shell::new(tree);
shell.run_for(Duration::from_millis(500));
let frames = shell.frames().len();
assert!((3..=5).contains(&frames), "three steps and the end: {frames} frames");
assert!(!shell.tree.has_active_timeline());
assert!(!shell.tree.needs_redraw());
let needs = LoopNeeds {
polling: shell.tree.needs_polling(),
next_redraw: shell.tree.next_scheduled_redraw(),
next_timer: shell.tree.next_timer_at(),
..LoopNeeds::default()
};
assert_eq!(
LoopWait::of(needs, Instant::now()),
LoopWait::Wait,
"nothing is left to wake for"
);
}
#[test]
fn a_minimized_window_draws_no_animation_frames_and_resumes_on_time_when_restored() {
let mut shell = Shell::new(spinner(steps(12)));
shell.run_for(Duration::from_millis(300));
let started_at = shell.frames()[0].pass_at;
shell.tree.window().set_minimized(true);
let frames_before = shell.frames().len();
shell.run_for(Duration::from_millis(500));
assert_eq!(
shell.frames().len(),
frames_before,
"a minimized window draws no animation frames"
);
shell.tree.window().set_minimized(false);
let restored_at = Instant::now();
shell.run_for(Duration::from_millis(400));
let resumed = &shell.frames()[frames_before..];
assert!(
(4..=7).contains(&resumed.len()),
"the spinner turns again at its pace after the restore: {} frames in 0.4 s",
resumed.len()
);
assert!(
resumed[0].pass_at - restored_at < REFRESH,
"the first frame comes at once"
);
for frame in resumed {
let elapsed_steps = ((frame.pass_at - started_at).as_secs_f64() * 12.0).floor() as u32 % 12;
let drawn = pose(frame.angle, 12);
let off = (drawn + 12 - elapsed_steps) % 12;
assert!(
off == 0 || off == 11,
"the animation kept its time while minimized: step {drawn} drawn at step {elapsed_steps} of the clock"
);
}
}
#[derive(Clone, Default)]
struct ClickLog(Arc<Mutex<Vec<Instant>>>);
impl PartialEq for ClickLog {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for ClickLog {
fn write_info(&self, _buffer: &mut Vec<crate::app::component::ComponentInfo>) {}
}
struct ClickableSpinner {
clicks: ClickLog,
_interval: Interval,
}
impl Component for ClickableSpinner {
type Props = ClickLog;
fn create(ctx: &mut Ctx) -> Self {
let interval = ctx.create_interval(Duration::from_millis(25), || {});
interval.start();
Self {
clicks: ctx.props::<ClickLog>().clone(),
_interval: interval,
}
}
fn render(&self, _ctx: &mut Ctx) -> impl Into<Element> {
let clicks = self.clicks.clone();
Row::new().child(
Rect::new(40.0, 40.0)
.background("#2563eb")
.animation(Animation::new(TURN).duration_ms(1000).easing(steps(12)).infinite())
.on_click(move |_| clicks.0.lock().expect("one thread").push(Instant::now())),
)
}
}
#[test]
fn input_and_timers_are_handled_at_once_while_a_spinner_turns() {
let mut app = App::new();
let mut tree = Tree::new();
tree.register_keyframes(turn_keyframes());
let log = ClickLog::default();
tree.mount_root::<ClickableSpinner>(&mut app, log.clone());
let mut shell = Shell::new(tree);
shell.run_for(Duration::from_millis(100));
let first_click = Instant::now() + Duration::from_millis(7);
let clicks: Vec<Instant> = (0..12).map(|i| first_click + Duration::from_millis(37) * i).collect();
shell.clicks = clicks.iter().copied().collect();
shell.timer_lateness.clear();
shell.run_for(Duration::from_millis(600));
let clicked_at = log.0.lock().expect("one thread").clone();
assert_eq!(clicked_at.len(), clicks.len(), "every click is handled");
let mut click_latency: Vec<Duration> = clicks
.iter()
.zip(&clicked_at)
.map(|(sent, handled)| *handled - *sent)
.collect();
click_latency.sort();
let mut timer_lateness = shell.timer_lateness.clone();
timer_lateness.sort();
assert!(
timer_lateness.len() >= 15,
"the 25 ms interval keeps firing: {timer_lateness:?}"
);
let median = |sorted: &[Duration]| sorted[sorted.len() / 2];
let on_time = Duration::from_millis(3);
assert!(
median(&click_latency) < on_time && median(&timer_lateness) < on_time,
"clicks are handled and timers fire when they are due, not after the next frame:\n click latency {click_latency:?}\n timer lateness {timer_lateness:?}"
);
}
#[test]
fn a_continuous_spinner_presents_at_every_vblank_whatever_its_render_cost() {
for cost_ms in 0..=3 {
let mut shell = Shell::new(spinner(Easing::Linear));
shell.set_render_cost(Duration::from_millis(cost_ms));
shell.run_for(Duration::from_millis(600));
let vblanks = shell.vblanks();
let skipped = skipped_vblanks(&vblanks[1..]);
assert_eq!(
skipped,
0,
"with {cost_ms} ms of render work, every vblank shows a new frame: {} presents, {skipped} vblanks without one",
vblanks.len()
);
assert!(
(34..=39).contains(&vblanks.len()),
"60 presents a second: {} in 0.6 s with {cost_ms} ms of work",
vblanks.len()
);
}
}
#[test]
fn a_twelve_step_spinner_draws_only_its_steps_whatever_its_render_cost() {
let mut shell = Shell::new(spinner(steps(12)));
shell.set_render_cost(Duration::from_millis(3));
shell.run_for(Duration::from_secs(1));
let frames = shell.frames().len();
assert!(
(11..=14).contains(&frames),
"12 steps a second draw 12 frames a second: {frames} in 1 s"
);
}
#[test]
fn a_minimized_window_draws_no_perf_overlay_frames() {
let mut tree = Tree::new();
tree.set_root(Row::new().child(Rect::new(40.0, 40.0).background("#2563eb")));
tree.draw_perf_overlay();
let mut shell = Shell::new(tree);
shell.run_for(Duration::from_millis(100));
shell.tree.window().set_minimized(true);
let frames_before = shell.frames().len();
for _ in 0..24 {
shell.run_for(Duration::from_millis(50));
}
assert_eq!(
shell.frames().len(),
frames_before,
"the overlay's samples draw nothing while the window is minimized"
);
}
#[test]
fn moving_the_window_reads_its_refresh_rate_again() {
let mut app = App::new();
let mut tree = spinner(Easing::Linear);
tree.pass_headless(&mut app);
let mut pacing = FramePacing::default();
let mut reads = 0;
let mut request = |pacing: &mut FramePacing, tree: &mut Tree| {
pacing.request_frame(
tree,
|| {
reads += 1;
REFRESH
},
Instant::now(),
);
};
request(&mut pacing, &mut tree);
request(&mut pacing, &mut tree);
pacing.handle_event(&WindowEvent::Moved(PhysicalPosition::new(1920, 0)));
request(&mut pacing, &mut tree);
assert_eq!(reads, 2, "the rate is read once, then again when the window moves");
}
fn virtual_presents(
epoch: Instant,
refresh: Duration,
cost: Duration,
count: usize,
changes: impl Fn(Instant) -> Instant,
) -> Vec<Instant> {
let vblank_after = |at: Instant| {
let refreshes = vblank_index(epoch, refresh, at);
epoch + refresh * u32::try_from(refreshes).expect("a short simulation")
};
let mut seed = 0x2545_f491_4f6c_dd1d_u64;
let mut wake_latency = || {
seed = seed
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
Duration::from_micros((seed >> 33) % 1000)
};
let mut now = epoch;
let mut last: Option<PresentedFrame> = None;
let mut presents: Vec<Instant> = Vec::with_capacity(count);
while presents.len() < count {
if let Some(frame) = last {
now = now.max(frame_due(changes(frame.started), last, refresh)) + wake_latency();
}
let started = now;
now += cost;
if let Some(previous) = presents.last() {
now = now.max(vblank_after(*previous));
}
now += cost;
presents.push(now);
last = Some(PresentedFrame {
started,
presented: now,
});
}
presents
}
#[test]
fn on_a_virtual_clock_continuous_frames_take_every_vblank() {
let cases = [(REFRESH, [0, 1000, 2000, 3000]), (REFRESH / 2, [0, 500, 1000, 1500])];
for (refresh, costs_us) in cases {
for cost in costs_us.map(Duration::from_micros) {
let epoch = Instant::now();
let presents = virtual_presents(epoch, refresh, cost / 2, 600, |sampled| sampled);
let vblanks: Vec<u128> = presents
.iter()
.map(|present| vblank_index(epoch, refresh, *present))
.collect();
assert_eq!(
skipped_vblanks(&vblanks[1..]),
0,
"at {refresh:?} a refresh and {cost:?} of work per frame, every vblank shows a new frame"
);
}
}
}
#[test]
fn on_a_virtual_clock_continuous_frames_take_every_vblank_at_144_hz() {
let refresh = Duration::from_nanos(6_944_444);
for cost_us in [2000, 2250, 2500] {
let cost = Duration::from_micros(cost_us);
let epoch = Instant::now();
let presents = virtual_presents(epoch, refresh, cost / 2, 1200, |sampled| sampled);
let vblanks: Vec<u128> = presents
.iter()
.map(|present| vblank_index(epoch, refresh, *present))
.collect();
assert_eq!(
skipped_vblanks(&vblanks[1..]),
0,
"at 144 Hz and {cost:?} of work per frame, every vblank shows a new frame"
);
}
}
#[test]
fn on_a_virtual_clock_twelve_steps_draw_twelve_frames_a_second() {
let step = Duration::from_secs(1) / 12;
let epoch = Instant::now();
let next_step = |sampled: Instant| {
let steps = (sampled - epoch).as_nanos() / step.as_nanos() + 1;
epoch + step * u32::try_from(steps).expect("a short simulation") + Duration::from_micros(100)
};
let presents = virtual_presents(epoch, REFRESH, Duration::from_millis(1), 40, next_step);
let in_first_second = presents
.iter()
.filter(|present| **present - epoch < Duration::from_secs(1))
.count();
assert!(
(12..=13).contains(&in_first_second),
"a frame per step: {in_first_second} frames in the first second"
);
}