From e291eef6d6e7753a52fdf0afb1a56a91c2e5acd7 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Tue, 3 Nov 2026 14:03:59 +0100 Subject: [PATCH] Viewport pages: free a document's pages when no page fits the budget With a canvas budget too small for one page, show() returned to the CPU canvas after creating or invalidating the stream but before pruning, so the stream's GPU pages and its jobs stayed held. Check the page budget first and drop the stream and its pages, as a document that fits does. Addresses CodeRabbit review r4453118176 on #5332. --- .../src/canvas/background_redraw/tests.rs | 3 +++ crates/ui-egui/src/canvas/viewport_pages.rs | 24 ++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/canvas/background_redraw/tests.rs b/crates/ui-egui/src/canvas/background_redraw/tests.rs index a5397c77ddd..a669b18a38c 100644 --- a/crates/ui-egui/src/canvas/background_redraw/tests.rs +++ b/crates/ui-egui/src/canvas/background_redraw/tests.rs @@ -687,6 +687,9 @@ fn a_canvas_over_the_texture_budget_draws_its_viewport_pages_on_the_gpu() { gpu.set_canvas_budget(1); g.harness.run_steps(3); assert_eq!(render_path_of(&g), (Some("canvasTooLarge"), Some("CPU Rendering"))); + // Its pages and their work are freed meanwhile (#5332 review). + assert!(gpu.pages(key).0.is_empty(), "pages freed"); + assert!(!g.harness.state().streams.contains_key(&doc.id), "stream dropped"); gpu.set_canvas_budget(set - 1); settle_pages(&mut g); // Closing the document cancels its work and frees its pages. diff --git a/crates/ui-egui/src/canvas/viewport_pages.rs b/crates/ui-egui/src/canvas/viewport_pages.rs index 01c7d791cf1..410a7d4dfc8 100644 --- a/crates/ui-egui/src/canvas/viewport_pages.rs +++ b/crates/ui-egui/src/canvas/viewport_pages.rs @@ -597,6 +597,20 @@ pub(super) fn show(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, xf: } return false; } + let key = stream_key(id.0); + // Half the per-document canvas budget: the rest is headroom for what the compositor needs. + let limit = gpu.canvas_budget() / 2; + let page_bytes = gpu.page_texture_bytes(doc.depth, size, PAGE_SIDE + 2, PAGE_SIDE + 2).max(1); + let max_pages = usize::try_from(limit / page_bytes).unwrap_or(usize::MAX); + // No room for a single page: the CPU canvas draws the document, as main does for every + // document over the budget, and the pages and work held for it are freed. + if max_pages == 0 { + if let Some(s) = app.streams.remove(&id) { + drop(s); + gpu.retain_pages(key, &[]); + } + return false; + } let (display, _) = canvas_display(app, &doc, None); let display_key = texture_key(display.as_deref()); let now = Identity { revision, preview: raw_key, display: display_key, size }; @@ -607,11 +621,6 @@ pub(super) fn show(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, xf: .and_then(|s| damage_since(app, idx, (s.revision, s.preview ^ display_key), (revision, raw_key), display_key, last_damage)); app.streams.entry(id).or_default().invalidate(now, damage); } - let key = stream_key(id.0); - // Half the per-document canvas budget: the rest is headroom for what the compositor needs. - let limit = gpu.canvas_budget() / 2; - let page_bytes = gpu.page_texture_bytes(doc.depth, size, PAGE_SIDE + 2, PAGE_SIDE + 2).max(1); - let max_pages = usize::try_from(limit / page_bytes).unwrap_or(usize::MAX); let wanted = pages(size, visible_doc_rect(xf), zoom, max_pages); if gpu.texture_info(id.0).is_some() { // Switching from the texture set: free it, the pages are budgeted against that memory. @@ -619,9 +628,8 @@ pub(super) fn show(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, xf: } let stream = app.streams.entry(id).or_default(); stream.retry_after((photocraft_raster::spill::read_error_generation(), photocraft_raster::memory::configuration_generation())); - // No room for a single page, or a page that failed: the CPU canvas draws the document, as - // main does for every document over the budget. - if max_pages == 0 || stream.stuck(&wanted) { + // A page that failed: the CPU canvas draws the document until it is retried. + if stream.stuck(&wanted) { return false; } let faithful = photocraft_compose::proxy::proxy_faithful(&doc);