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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Runs a web app's work in the browser's idle periods: between frames, the
// lazy list items its frames left beyond their viewports
// (`AppShell::run_idle_prefetch`), as Android composes them while it waits
// for a frame, and while the page asks for no frame, the pipeline builds that
// wait for one (`Renderer::build_while_idle`). `requestIdleCallback` says how
// long the browser's idle period lasts. A browser without it (Safari) runs
// the work from a zero-delay timeout after a frame, within the frame's
// interval, and looks again for builds from a timeout.
use std::{
cell::{Cell, RefCell},
rc::{Rc, Weak},
time::Duration,
};
use cranpose_app_shell::{AppShell, FrameSchedule};
use cranpose_render_common::Renderer;
use cranpose_render_wgpu::WgpuRenderer;
use wasm_bindgen::{JsCast, closure::Closure};
use web_time::Instant;
// Where the browser has no idle callbacks: how long after a frame's start
// the passes may run, a 60 Hz interval less a margin for the next frame.
const TIMEOUT_IDLE_BUDGET: Duration = Duration::from_millis(12);
// Where the browser has no idle callbacks: when a build that waits for the
// GPU process to link the last one looks again, as Chrome's longest idle
// period comes again.
const TIMEOUT_BUILD_POLL_MS: i32 = 50;
pub(crate) struct IdleWork {
app: Weak<RefCell<AppShell<WgpuRenderer>>>,
// Whether the page has asked for its next frame.
frame_pending: Rc<Cell<bool>>,
// Whether the app asked for nothing at its last frame's end: no frame,
// no update and no timed event, so no animation waits behind a build.
stands_still: Cell<bool>,
queued: Cell<bool>,
}
impl IdleWork {
pub(crate) fn new(
app: &Rc<RefCell<AppShell<WgpuRenderer>>>,
frame_pending: &Rc<Cell<bool>>,
) -> Rc<Self> {
Rc::new(Self {
app: Rc::downgrade(app),
frame_pending: Rc::clone(frame_pending),
stands_still: Cell::new(false),
queued: Cell::new(false),
})
}
// Asks for an idle period after the frame that started at
// `frame_started`, when a lazy list waits for one or the app's
// `schedule` asks for nothing, which leaves builds no frame to run at.
// One is asked at a time.
pub(crate) fn queue_after_frame(
self: &Rc<Self>,
frame_started: Instant,
schedule: FrameSchedule,
) {
// A timed event that waits, as a trim at rest, leaves the app still.
self.stands_still
.set(!schedule.needs_frame && !schedule.needs_update);
if self.queued.get() || !(self.stands_still.get() || self.has_prefetch_work()) {
return;
}
let Some(window) = super::frame_host::window() else {
return;
};
self.queued.set(true);
if !self.ask_idle_period(&window) {
self.ask_timeout(&window, 0, Some(frame_started));
}
}
// Asks the browser for its next idle period. `false` where it has no
// idle callbacks.
fn ask_idle_period(self: &Rc<Self>, window: &web_sys::Window) -> bool {
let this = Rc::clone(self);
let idle = Closure::once_into_js(move |deadline: web_sys::IdleDeadline| {
let remaining = Duration::from_secs_f64(deadline.time_remaining().max(0.0) / 1000.0);
let builds_wait = this.run(Instant::now() + remaining);
// A list or a build still waiting gets the next idle period; a
// timeout runs passes only after a frame, whose start bounds it.
if (builds_wait || this.has_prefetch_work())
&& !this.queued.replace(true)
&& let Some(window) = super::frame_host::window()
&& !this.ask_idle_period(&window)
{
this.queued.set(false);
}
});
window.request_idle_callback(idle.unchecked_ref()).is_ok()
}
// Runs the work from a timeout `delay_ms` from now, the passes until
// 12 ms after the frame that started at `frame_started`, or with no
// frame, 12 ms after the timeout fires.
fn ask_timeout(
self: &Rc<Self>,
window: &web_sys::Window,
delay_ms: i32,
frame_started: Option<Instant>,
) {
let this = Rc::clone(self);
let timeout = Closure::once_into_js(move || {
let started = frame_started.unwrap_or_else(Instant::now);
if this.run(started + TIMEOUT_IDLE_BUDGET)
&& !this.queued.replace(true)
&& let Some(window) = super::frame_host::window()
{
this.ask_timeout(&window, TIMEOUT_BUILD_POLL_MS, None);
}
});
if window
.set_timeout_with_callback_and_timeout_and_arguments_0(
timeout.unchecked_ref(),
delay_ms,
)
.is_err()
{
self.queued.set(false);
}
}
// Runs the passes that fit before `deadline` and, while the app stands
// still, a build: whether builds still wait.
fn run(&self, deadline: Instant) -> bool {
self.queued.set(false);
let Some(app) = self.app.upgrade() else {
return false;
};
let Ok(mut app) = app.try_borrow_mut() else {
return false;
};
app.run_idle_prefetch(Some(deadline), |_| false);
// A frame asked for since runs first, and its end asks again.
let builds_wait = self.stands_still.get()
&& !self.frame_pending.get()
&& app.renderer().build_while_idle();
self.stands_still.set(builds_wait);
builds_wait
}
fn has_prefetch_work(&self) -> bool {
self.app.upgrade().is_some_and(|app| {
app.try_borrow()
.is_ok_and(|app| app.has_idle_prefetch_work())
})
}
}