From 7ed0c1e92fd69e3442b093a8131a783302edbcdc Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 20 Nov 2026 12:21:08 +0100 Subject: [PATCH] Fix multiwindow repaint starvation and schedule idle redraws --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/pdf-presenter/src/native.rs | 133 +++++++++++++++++++++- docs/changelog.md | 11 ++ docs/development/native-implementation.md | 23 ++++ 5 files changed, 165 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0fd0dbb..4d8ad64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2245,7 +2245,7 @@ dependencies = [ [[package]] name = "pdf-presenter" -version = "0.1.72" +version = "0.1.73" dependencies = [ "bytemuck", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 12a1580..d95ff40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/crates/pdf-presenter/src/native.rs b/crates/pdf-presenter/src/native.rs index 4462433..fe7e3b2 100644 --- a/crates/pdf-presenter/src/native.rs +++ b/crates/pdf-presenter/src/native.rs @@ -37,6 +37,12 @@ fn navigation_direction(key: &Key, modifiers: egui::Modifiers) -> Option { _ => None, } } +// Bound zero-delay animation requests; idle windows have no repaint deadline. +fn repaint_deadline(now: Instant, delay: Duration) -> Option { + (delay != Duration::MAX) + .then(|| now.checked_add(delay.max(Duration::from_millis(16)))) + .flatten() +} type Failure = Box; #[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, display_action: Option, zoom_change: Option, 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(); diff --git a/docs/changelog.md b/docs/changelog.md index ea206f6..c944c54 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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 diff --git a/docs/development/native-implementation.md b/docs/development/native-implementation.md index 7eea175..3502ff3 100644 --- a/docs/development/native-implementation.md +++ b/docs/development/native-implementation.md @@ -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.