Fix multiwindow repaint starvation and schedule idle redraws

This commit is contained in:
Dandaedre 2026-11-20 12:21:08 +01:00
parent 5294e083ad
commit 7ed0c1e92f
5 changed files with 165 additions and 6 deletions

2
Cargo.lock generated
View File

@ -2245,7 +2245,7 @@ dependencies = [
[[package]]
name = "pdf-presenter"
version = "0.1.72"
version = "0.1.73"
dependencies = [
"bytemuck",
"chrono",

View File

@ -4,7 +4,7 @@ default-members = ["crates/pdf-presenter"]
resolver = "3"
[workspace.package]
version = "0.1.72"
version = "0.1.73"
edition = "2024"
license = "MIT"
repository = "https://proxy.dandaedre.com/gitea/idle-org/pdf-presenter"

View File

@ -37,6 +37,12 @@ fn navigation_direction(key: &Key, modifiers: egui::Modifiers) -> Option<i32> {
_ => None,
}
}
// Bound zero-delay animation requests; idle windows have no repaint deadline.
fn repaint_deadline(now: Instant, delay: Duration) -> Option<Instant> {
(delay != Duration::MAX)
.then(|| now.checked_add(delay.max(Duration::from_millis(16))))
.flatten()
}
type Failure = Box<dyn std::error::Error>;
#[derive(Clone, Copy, PartialEq, Eq)]
enum Role {
@ -56,6 +62,7 @@ struct NativeWindow {
target: Option<(String, bool)>,
transition: Option<(Instant, bool)>,
frames: usize,
repaint_at: Option<Instant>,
display_action: Option<DisplayAction>,
zoom_change: Option<crate::zoom::View>,
zoom_watch: crate::zoom::PerformanceWatch,
@ -111,6 +118,7 @@ impl NativeWindow {
target: None,
transition: None,
frames: 0,
repaint_at: None,
display_action: None,
zoom_change: None,
zoom_watch: Default::default(),
@ -119,6 +127,12 @@ impl NativeWindow {
browser_parent: None,
})
}
fn repaint_if_due(&mut self, now: Instant) {
if self.repaint_at.is_some_and(|deadline| now >= deadline) {
self.repaint_at = None;
self.window.request_redraw();
}
}
fn place(&mut self, handle: &MonitorHandle, id: &str, fullscreen: bool, x11: bool) {
let target = (id.to_owned(), fullscreen);
if self.target.as_ref() == Some(&target) {
@ -290,6 +304,13 @@ impl NativeWindow {
}
}
});
self.repaint_at = repaint_deadline(
Instant::now(),
output
.viewport_output
.get(&egui::ViewportId::ROOT)
.map_or(Duration::MAX, |viewport| viewport.repaint_delay),
);
self.input
.handle_platform_output(&self.window, output.platform_output);
let primitives = ctx.tessellate(output.shapes, output.pixels_per_point);
@ -1181,13 +1202,14 @@ impl App {
s.new_output = true;
}
}
if repaint {
self.redraw();
}
if let Err(e) = self.sync_windows(el) {
self.error = Some(e.to_string());
el.exit();
}
// Include newly created mirrors in the same state update.
if repaint {
self.redraw();
}
}
}
impl ApplicationHandler for App {
@ -1225,7 +1247,10 @@ impl ApplicationHandler for App {
let n = &mut self.windows[index];
n.input.on_window_event(&n.window, &event).repaint
};
if repaint {
// egui-winit reports repaint=true for RedrawRequested itself. The
// current render satisfies that request: requeueing it creates an idle
// paint loop and can starve later Windows WM_PAINT messages.
if repaint && !matches!(event, WindowEvent::RedrawRequested) {
self.windows[index].window.request_redraw();
}
match event {
@ -1413,6 +1438,7 @@ impl ApplicationHandler for App {
}
}
for window in &mut self.windows {
window.repaint_if_due(now);
window.transition(&self.handles, now);
}
if self.smoke
@ -1558,6 +1584,105 @@ pub fn run_output() -> Result<(), Failure> {
#[cfg(test)]
mod buffer_tests {
use super::*;
#[test]
fn repaint_deadlines_bound_animation_and_leave_idle_windows_asleep() {
let now = Instant::now();
assert_eq!(repaint_deadline(now, Duration::MAX), None);
assert_eq!(
repaint_deadline(now, Duration::ZERO),
Some(now + Duration::from_millis(16))
);
assert_eq!(
repaint_deadline(now, Duration::from_secs(2)),
Some(now + Duration::from_secs(2))
);
}
#[cfg(target_os = "linux")]
#[test]
fn three_native_windows_render_without_clicks_and_then_stop_repainting() {
if std::env::var_os("PDF_PRESENTER_GUI_TESTS").is_none() {
return;
}
use winit::platform::x11::EventLoopBuilderExtX11;
struct Harness {
app: App,
started: Instant,
}
impl ApplicationHandler for Harness {
fn resumed(&mut self, el: &ActiveEventLoop) {
for index in 0..3 {
let window =
NativeWindow::create(el, Role::Presentation, format!("test-{index}"))
.unwrap();
let _ = window
.window
.request_inner_size(PhysicalSize::new(180, 120));
window
.window
.set_outer_position(PhysicalPosition::new(20 + index * 200, 20));
window.window.set_visible(true);
window.window.request_redraw();
self.app.windows.push(window);
}
}
fn window_event(&mut self, el: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
self.app.window_event(el, id, event);
}
fn about_to_wait(&mut self, el: &ActiveEventLoop) {
let now = Instant::now();
for window in &mut self.app.windows {
window.repaint_if_due(now);
}
if now.duration_since(self.started) >= Duration::from_secs(1) {
el.exit();
}
el.set_control_flow(ControlFlow::WaitUntil(now + Duration::from_millis(8)));
}
}
let now = Instant::now();
let mut harness = Harness {
started: now,
app: App {
studio: None,
host: None,
output: None,
lease: None,
windows: vec![],
handles: vec![],
next_poll: now,
next_clock: now,
next_monitors: now,
x11: true,
error: None,
smoke: false,
smoke_stage: 0,
started: now,
windowed: true,
override_presenter: None,
override_output: None,
navigation_hold: None,
},
};
EventLoop::builder()
.with_x11()
.with_any_thread(true)
.build()
.unwrap()
.run_app(&mut harness)
.unwrap();
assert!(harness.app.error.is_none(), "{:?}", harness.app.error);
assert_eq!(harness.app.windows.len(), 3);
for window in &harness.app.windows {
assert!(window.frames > 0, "Every mirror must paint without a click");
assert!(
window.frames < 30,
"Idle window repainted {} times",
window.frames
);
}
}
#[test]
fn space_navigation_respects_shift_focus_roles_and_blanking() {
let now = Instant::now();

View File

@ -2,6 +2,17 @@
## Unreleased
### 0.1.73
- Stop self-perpetuating native repaint requests that can starve other selected
displays until their windows receive a click. Honor each window's egui repaint
deadline with a bounded animation rate, and include new mirrors in state redraws.
Add a three-window regression exercising rendering without clicks and idle repaint
limits. Linux workspace/native tests, 13 Python tests, formatting and warnings-
denied Clippy/rustdoc pass; whole-application coverage is 84.57% (75% floor).
The Linux release binary is rebuilt. Physical three-monitor and Windows/macOS
verification remain open.
### 0.1.72
- Preserve the live show when Escape closes All slides, including focused controls

View File

@ -377,3 +377,26 @@ mouse holds. Skipped uncached destinations defer new rendering until input settl
for 180 ms or ends; existing workers are not restarted on each repeat. Release can
cancel one obsolete foreground job so the final destination gets priority. The
last ready audience frame remains visible; RAM-ready destinations can show directly.
## 0.1.73 multiwindow repaint scheduling
A native RedrawRequested event satisfies egui-winit's repaint response for that
same event; it must not enqueue itself again. The old feedback loop continuously
repainted idle windows and could starve later WM_PAINT delivery on Windows, where
winit returns to its event loop after each paint. Each window now keeps one egui
repaint deadline, consumed once when due; zero-delay animation requests are bounded
to a 16 ms minimum. Input, slide changes and window transitions still request
immediate redraws, regardless of focus. Window reconciliation precedes shared-state
redraw requests so newly created mirrors participate too.
The Linux native regression opens three windows without clicking or explicitly
focusing any of them, checks that each paints, and rejects continuous idle painting.
This exercises the event path but does not prove physical three-monitor routing or
Windows/macOS compositor behavior.
Restoring the old repaint feedback made the new regression fail with 1,520 idle
paints in one second. The fixed version passes alongside the full Linux workspace
and native tests, 13 Python helper tests, formatting, and warnings-denied Clippy
and rustdoc. Whole-application coverage is 84.57% against the 75% floor. The Linux
release binary is rebuilt; no Windows/macOS builds or publication were performed.