diff --git a/crates/engine/src/prep.rs b/crates/engine/src/prep.rs index bb53d244184..563fbb0df8f 100644 --- a/crates/engine/src/prep.rs +++ b/crates/engine/src/prep.rs @@ -214,12 +214,18 @@ pub struct PrepState { pub library: Option, /// The background build this preparation started. build: Option>, - /// The view (its signature) whose build was started, or that the user stopped. - started_for: Option, + /// The view (its signature) whose build was started, with the [`Self::edge`] it builds at: a + /// new size in the same view is built again (vangel76 on #4096). And the view the user stopped + /// it in, which holds whatever the size. + started_for: Option<(u64, usize)>, declined_for: Option, /// A build that couldn't start: the view (its signature), and when it may try again (not on /// every tick while the cause lasts; CodeRabbit on #3080). pub retry_at: Option<(u64, web_time::Instant)>, + /// The long edge (px) the frontend builds standard previews at (its Build Standard-Sized + /// Previews size; 0: [`crate::cmd::previews::standard_edge`]). The background build uses it + /// too, so a later build asked for skips what it made instead of rendering it again (#3947). + pub edge: usize, /// Tests: the next starts to fail, as a build thread that can't be spawned does. #[cfg(test)] pub(crate) fail_starts: usize, @@ -364,10 +370,17 @@ impl Session { if b.running() { if !wanted { b.cancel.store(true, Ordering::Relaxed); - } else if self.prep.started_for.is_some_and(|sig| sig != signature(&self.visible_cloned(), self, want.scope)) { - // another view (a folder, a filter, another library): this build's photos are - // not the ones shown; stopped, and the view now shown is prepared in its place - // (not counted as stopped by the user) + } else if b.cancel.load(Ordering::Relaxed) { + // stopping already (from its row, say): the view it was for is kept until it has, so + // a stop still holds for that view when the size changes meanwhile (CodeRabbit on #4096) + } else if self + .prep + .started_for + .is_some_and(|(sig, edge)| edge != self.prep.edge || sig != signature(&self.visible_cloned(), self, want.scope)) + { + // another view (a folder, a filter, another library) or another size: this + // build's previews are not the ones wanted; stopped, and the view now shown is + // prepared in its place (not counted as stopped by the user) b.cancel.store(true, Ordering::Relaxed); self.prep.started_for = None; } else if let Some(id) = at { @@ -380,7 +393,7 @@ impl Session { if b.cancel.load(Ordering::Relaxed) { if mine && wanted { // stopped from its row: not again for this view - self.prep.declined_for = self.prep.started_for; + self.prep.declined_for = self.prep.started_for.map(|(sig, _)| sig); } else { // replaced by a build that was asked for, or switched off: may start again later self.prep.started_for = None; @@ -405,7 +418,7 @@ impl Session { let busy = self.preview_build.as_ref().is_some_and(|b| b.running()); if busy || self.prep.steps < STEPS_BEFORE_BUILD - || self.prep.started_for == Some(sig) + || self.prep.started_for == Some((sig, self.prep.edge)) || self.prep.declined_for == Some(sig) || self.prep.retry_at.is_some_and(|(view, at)| view == sig && web_time::Instant::now() < at) || self.library.as_ref().is_none_or(|l| !l.on_disk) @@ -416,11 +429,14 @@ impl Session { Scope::Marked => ids.into_iter().filter(|id| marked(self, *id)).map(|id| id.0).collect(), _ => ids.into_iter().map(|id| id.0).collect(), }; - self.prep.started_for = Some(sig); + self.prep.started_for = Some((sig, self.prep.edge)); if ids.is_empty() { return; } - let p = json!({"ids": ids, "background": true, "from": at.0, "backward": backward}); + let mut p = json!({"ids": ids, "background": true, "from": at.0, "backward": backward}); + if let Some(o) = p.as_object_mut().filter(|_| self.prep.edge > 0) { + o.insert("edge".into(), json!(self.prep.edge)); + } // (tests: a start made to fail, as a build thread that can't be spawned fails) #[cfg(test)] let fail = self.prep.fail_starts.checked_sub(1).map(|left| self.prep.fail_starts = left).is_some(); diff --git a/crates/engine/src/tests/preview_prep.rs b/crates/engine/src/tests/preview_prep.rs index 0981c2cf960..ac8c2855898 100644 --- a/crates/engine/src/tests/preview_prep.rs +++ b/crates/engine/src/tests/preview_prep.rs @@ -291,6 +291,87 @@ fn the_background_preparation_starts_when_culling_and_can_be_stopped() { let _ = std::fs::remove_dir_all(root); } +/// #3947: the background build makes standard previews at the size the frontend's Build +/// Standard-Sized Previews asks for, so that build skips them instead of rendering every one again. +#[test] +fn the_background_build_makes_previews_at_the_frontends_standard_size() { + let (mut s, root, ids) = library("prep-edge", 3); + s.prep.edge = 300; + let mode = crate::prep::Mode::Library; + for id in &ids { + s.prep_tick(mode, Some(*id), false); + } + let b = s.preview_build.clone().expect("culling: the view is prepared"); + assert!(b.background); + assert!(wait_finished(&b), "built"); + assert_eq!(b.failed.load(Ordering::Relaxed), 0); + for id in &ids { + let p = s.catalog.photo(*id).unwrap().clone(); + let (w, h) = s.media.rendered.disk().expect("a disk cache").dims(s.view_key(&p, true)).expect("on disk"); + assert_eq!(w.max(h), 300, "built at the frontend's standard size"); + } + let r = s.execute("library.buildPreviews", &json!({"ids": ids_json(&ids), "edge": 300, "wait": true})).unwrap(); + assert_eq!(r["skipped"], 3, "the build asked for skips what the background made {r}"); + // vangel76 on #4096: a new size in the same view is prepared again, at that size + let first = s.preview_build.clone().expect("the build asked for"); + s.prep.edge = 500; + s.prep_tick(mode, Some(ids[2]), false); + let bigger = s.preview_build.clone().filter(|b| !std::sync::Arc::ptr_eq(b, &first)).expect("built again at the new size"); + assert!(bigger.background); + assert!(wait_finished(&bigger), "built"); + for id in &ids { + let p = s.catalog.photo(*id).unwrap().clone(); + let (w, h) = s.media.rendered.disk().expect("a disk cache").dims(s.view_key(&p, true)).expect("on disk"); + assert!(w.max(h) > 300, "bigger than the size before"); + } + // a stop from its row still holds for the view, whatever size comes next + let hold = crate::prep::TestHold::new(); + s.prep.edge = 400; + s.prep_tick(mode, Some(ids[2]), false); + let stopped = s.preview_build.clone().filter(|b| !std::sync::Arc::ptr_eq(b, &bigger)).expect("started at the next size"); + stopped.cancel.store(true, Ordering::Relaxed); + drop(hold); + assert!(wait_finished(&stopped), "a cancelled build stops"); + s.prep_tick(mode, Some(ids[2]), false); + s.prep.edge = 450; + s.prep_tick(mode, Some(ids[1]), false); + assert!(same_build(&s, &stopped), "not started again in the view it was stopped in"); + let _ = std::fs::remove_dir_all(root); +} + +/// CodeRabbit on #4096: a size that changes while a stopped build is still finishing its photos +/// doesn't undo the stop: the view it was stopped in isn't prepared again at the new size. +#[test] +fn a_stop_holds_when_the_size_changes_while_the_build_winds_down() { + use crate::cmd::previews::test_gate::{self, PreviewGate, ReleaseOnDrop}; + let (mut s, root, ids) = library("prep-edge-stop", 3); + s.prep.edge = 300; + let mode = crate::prep::Mode::Library; + // a lane parked at the gate keeps the build unfinished whatever `cancel` says (the foreground + // hold doesn't: its wait gives up once `cancel` is set), so the stop is still winding down + // when the size changes; the gate also opens if an assertion below fails + let gate = PreviewGate::new(); + let release = ReleaseOnDrop(gate.clone()); + test_gate::install(gate.clone()); + for id in &ids { + s.prep_tick(mode, Some(*id), false); + } + let b = s.preview_build.clone().expect("the view is prepared"); + assert!(gate.wait_entered(1), "a lane reached its first photo"); + b.cancel.store(true, Ordering::Relaxed); + s.prep.edge = 500; + s.prep_tick(mode, Some(ids[2]), false); + assert!(b.running(), "the size changed while the stopped build was still winding down"); + drop(release); + assert!(wait_finished(&b), "a cancelled build stops"); + for k in 0..3 { + s.prep_tick(mode, Some(ids[k]), false); + } + assert!(same_build(&s, &b), "not started again at the new size in the view it was stopped in"); + drop(s); + std::fs::remove_dir_all(&root).unwrap(); +} + /// While editing, the whole view's build follows the Library row (its switch and "which photos"); /// the Editing row's scope is only for the photos kept ready ahead. #[test] diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 3bfe7e05874..f0ae670355c 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -584,6 +584,8 @@ impl LightcraftApp { let loupe = self.ui.fullscreen || self.ui.view == state::ViewMode::Detail; let at = if loupe { self.session.active() } else { None }; let backward = self.look_ahead.backward(); + // the background build makes previews at Build Standard-Sized Previews' size (#3947) + self.session.prep.edge = self.ui.settings.standard_build_edge(self.session.loupe_edge) as usize; self.session.prep_tick(mode, at, backward); if self.session.preview_build.as_ref().is_some_and(|b| b.running()) { ctx.request_repaint_after(std::time::Duration::from_millis(500)); diff --git a/crates/ui-egui/src/menus/menubar.rs b/crates/ui-egui/src/menus/menubar.rs index 7ea62910bf9..8b4fd356d64 100644 --- a/crates/ui-egui/src/menus/menubar.rs +++ b/crates/ui-egui/src/menus/menubar.rs @@ -532,15 +532,12 @@ fn live_label(app: &LightcraftApp, id: &str, label: &str) -> String { } /// The parameterized submenus. -/// The params of Build Standard-Sized Previews: automatic builds as big as the loupe on this screen shows photos -/// (the engine's standard size, once a loupe has been drawn); before that, in a fresh session, the standard -/// 2560 px, not the engine's 2048 px minimum, so a typical fit view needn't render again; a chosen limit is used as is. +/// The params of Build Standard-Sized Previews: automatic builds at the standard 2560 px (not the engine's +/// 2048 px minimum, so a typical fit view needn't render again), or as big as the loupe on this screen shows +/// photos when that is more; a chosen limit is used as is. The background preparation builds at the same size +/// ([`crate::state::AppSettings::standard_build_edge`]), so this build skips what it made (#3947). pub(crate) fn standard_build_params(settings: &crate::state::AppSettings, loupe_edge: usize) -> Value { - match settings.preview_limit { - 0 if loupe_edge == 0 => json!({"size": "standard", "edge": settings.standard_preview_edge()}), - 0 => json!({"size": "standard"}), - edge => json!({"size": "standard", "edge": edge}), - } + json!({"size": "standard", "edge": settings.standard_build_edge(loupe_edge)}) } pub(crate) fn expanded(app: &LightcraftApp, name: &str) -> Option> { diff --git a/crates/ui-egui/src/state/settings.rs b/crates/ui-egui/src/state/settings.rs index 5f85ea9e6a4..b012608b06c 100644 --- a/crates/ui-egui/src/state/settings.rs +++ b/crates/ui-egui/src/state/settings.rs @@ -413,6 +413,18 @@ impl AppSettings { pub fn standard_preview_edge(&self) -> u32 { if self.preview_limit == 0 { STANDARD_PREVIEW_EDGE } else { self.preview_limit } } + + /// The long edge standard previews are built at, by Build Standard-Sized Previews and by the + /// background preparation alike: a chosen limit as is; on Automatic the standard size, or the + /// loupe's size on this screen (`loupe_edge`, 0: no loupe drawn yet) when that is bigger. The + /// same in every session, so a build skips the previews an earlier one made (#3947). + pub fn standard_build_edge(&self, loupe_edge: usize) -> u32 { + if self.preview_limit != 0 { + return self.preview_limit; + } + let loupe = u32::try_from(loupe_edge.min(8192)).unwrap_or(8192); + STANDARD_PREVIEW_EDGE.max(loupe) + } } /// How the Keywording box lays out the selection's keywords. diff --git a/crates/ui-egui/src/tests/lifecycle_037.rs b/crates/ui-egui/src/tests/lifecycle_037.rs index 052f410169a..84eef6fe82c 100644 --- a/crates/ui-egui/src/tests/lifecycle_037.rs +++ b/crates/ui-egui/src/tests/lifecycle_037.rs @@ -2366,6 +2366,20 @@ fn background_preparation_advances_while_the_window_is_hidden() { let _ = ctx.run_logic(&visible, |ctx| app.logic(ctx)); assert_eq!(app.prep_ticks, 3, "a shown pass advances it once"); } + +/// #3947: the background preparation builds previews at Build Standard-Sized Previews' size (a +/// chosen limit, else the standard 2560 px), so that build skips them instead of rendering them again. +#[test] +fn background_preparation_builds_at_the_standard_build_size() { + let mut app = app(); + let ctx = egui::Context::default(); + let hidden = viewport_raw(egui::vec2(800.0, 600.0), false); + let _ = ctx.run_logic(&hidden, |ctx| app.logic(ctx)); + assert_eq!(app.session.prep.edge, crate::state::STANDARD_PREVIEW_EDGE as usize); + app.ui.settings.preview_limit = 1600; + let _ = ctx.run_logic(&hidden, |ctx| app.logic(ctx)); + assert_eq!(app.session.prep.edge, 1600, "a chosen limit"); +} /// CodeRabbit on #3077/#3080: a background build that couldn't start asks for a frame at its retry /// time, so the retry happens with no input and no other timed frame. #[test] diff --git a/crates/ui-egui/src/tests/preview_limit.rs b/crates/ui-egui/src/tests/preview_limit.rs index d7dfb394754..8ac7232f063 100644 --- a/crates/ui-egui/src/tests/preview_limit.rs +++ b/crates/ui-egui/src/tests/preview_limit.rs @@ -1647,15 +1647,18 @@ mod in_the_loupe { } } -// Build Standard-Sized Previews on Automatic: the loupe's size once one was drawn (the engine's standard), the -// standard 2560 px in a fresh session, not the engine's 2048 px minimum a fit view would render again over -// (CodeRabbit on #3077); a chosen limit as is +// Build Standard-Sized Previews on Automatic: the standard 2560 px, not the engine's 2048 px minimum a fit +// view would render again over (CodeRabbit on #3077), or the loupe's size when bigger; a chosen limit as is. +// The same with or without a loupe drawn, so a fresh session's build skips the previews an earlier +// session's background preparation made (#3947) #[test] fn standard_build_keeps_the_standard_size_before_a_loupe_was_drawn() { use crate::menubar::standard_build_params; use serde_json::json; assert_eq!(standard_build_params(&with_limit(0), 0), json!({"size": "standard", "edge": STANDARD_PREVIEW_EDGE})); - assert_eq!(standard_build_params(&with_limit(0), 2400), json!({"size": "standard"})); + assert_eq!(standard_build_params(&with_limit(0), 2400), json!({"size": "standard", "edge": STANDARD_PREVIEW_EDGE})); + assert_eq!(standard_build_params(&with_limit(0), 3000), json!({"size": "standard", "edge": 3000})); + assert_eq!(standard_build_params(&with_limit(0), 50_000), json!({"size": "standard", "edge": 8192})); assert_eq!(standard_build_params(&with_limit(3840), 0), json!({"size": "standard", "edge": 3840})); assert_eq!(standard_build_params(&with_limit(3840), 2400), json!({"size": "standard", "edge": 3840})); } diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 02c309546c7..bb0da5dc1c0 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -727,7 +727,7 @@ From `08-lightroom-classic-extras.md` (Classic-only features) and part 2 of `06- | LRC-LIB-VISUALSEARCH | Find similar photos | P2 | ✅ | `cmd:library.findSimilar`, `crates/pipeline/src/cull.rs` (`signature`), `crates/engine/src/cmd/similar.rs`, `crates/ui-egui/src/find_similar.rs` | photo menu / Photo ▸ Find Similar Photos: look-alike photos (composition and tones), most similar first, as a filter (`only`); the app reads the photos on a worker thread under an activity row with ✕ | | LRC-LIB-MISSING | Missing files & relink | P1 | ✅ | `cmd:library.missing`, `cmd:photo.relink`, `cmd:library.findMissing`, `cmd:file.findMissing`, `cmd:photo.locate`, `crates/engine/src/cmd/missing.rs`, `crates/catalog/src/model.rs` (`Photo::library_file`), `crates/ui-egui/src/missing_sweep.rs`, `crates/ui-egui/src/panels/left.rs` (`locate_missing_item`), `crates/ui-egui/src/thumb.rs` | File → Find Missing Photos… (anywhere in a folder: same name, size and content hash; renamed files by size + content hash; same name and size alone only when unambiguous — look-alikes are skipped and reported; searched on a worker thread, one undo step), photo menu → Locate Missing File…; a folder or disk row's Locate Missing Files… chooses its new location and scopes the same recursive background finder to that folder and its subfolders. Unreadable files show a warning icon in the grid and the filmstrip (widgets `badge:failed:`, `film:failed:`) whose tooltip says why and where to go (Locate Missing File… for a missing original, else Reload from Disk), and a reason in the loupe; undo never moves files. Missing Photos in the sidebar (counted on a worker thread; the view, the Info panel, the grid's context menu and thumbnails read file availability from a cache a worker thread fills, so an offline NAS never stalls a frame); it covers library photos only — Local browse records are never checked, for the count, the view, `library.missing` and Find Missing alike | | LRC-LIB-CONVERT | Convert to DNG | P2 | ✅ | `cmd:photo.convertToDng`, `crates/engine/src/cmd/convert.rs` | Photo ▸ Convert to DNG: lossless DNG next to the raw with the settings embedded, photo (and its virtual copies) relinked; originals kept; undoable; the DNG is decoded and compared with the raw data, written to a temp file, synced, read back and only then given a free name (never replacing a file) — a failed write leaves no DNG and the photo on its raw (issue #106) | -| LRC-LIB-PREVIEWS | Build / discard previews | P1 | ✅ | `cmd:library.buildPreviews`, `cmd:library.previewProgress`, `cmd:library.cancelPreviews`, `cmd:library.clearPreviews`, `crates/engine/src/cmd/previews.rs`, `crates/preview/src/lib.rs`, `crates/engine/src/tests/preview_prep.rs` | File ▸ Previews: build standard-sized (as big as the loupe shows photos on this screen, at least 2048 px, or the Settings → Performance size) or 1:1 previews of the selected photos (more than one) or every photo in view, in the background (a row in the activity stack, ✕ to stop), discard the cache; disk thumbnail + view cache. The loupe shows a built preview that is big enough as the photo (fitted down, no render); a build skips photos whose previews are there and writes each to disk before it counts it; the disk cache's automatic size makes room for every photo of the library (about 1.5 MB a photo, no ceiling: a low drive is told once per session, see LR-PREF-LOCALSTORAGE), and memory for previews follows the memory budget | +| LRC-LIB-PREVIEWS | Build / discard previews | P1 | ✅ | `cmd:library.buildPreviews`, `cmd:library.previewProgress`, `cmd:library.cancelPreviews`, `cmd:library.clearPreviews`, `crates/engine/src/cmd/previews.rs`, `crates/preview/src/lib.rs`, `crates/engine/src/tests/preview_prep.rs` | File ▸ Previews: build standard-sized (as big as the loupe shows photos on this screen, at least 2560 px, or the Settings → Performance size; background preparation builds at the same size, so this build skips what it made, issue #3947) or 1:1 previews of the selected photos (more than one) or every photo in view, in the background (a row in the activity stack, ✕ to stop), discard the cache; disk thumbnail + view cache. The loupe shows a built preview that is big enough as the photo (fitted down, no render); a build skips photos whose previews are there and writes each to disk before it counts it; the disk cache's automatic size makes room for every photo of the library (about 1.5 MB a photo, no ceiling: a low drive is told once per session, see LR-PREF-LOCALSTORAGE), and memory for previews follows the memory budget | | LRC-PERF-PREP | Prepare photos in the background (beyond Lightroom) | P1 | 🟡 | `cmd:prefs.backgroundPrep`, `cmd:prefs.setBackgroundPrep`, `crates/engine/src/prep.rs`, `crates/ui-egui/src/dialogs/settings/performance.rs` | Settings ▸ Performance ▸ Prepare photos in the background, per mode (Culling, Library, Editing), app-wide or for one library: previews of every photo in view built in the spare time once the user steps through photos (yields to the photo on screen, nearest photos first in the direction of travel, one cancellable row), photos ahead rendered for the loupe (0 = off), eye focus strips ahead, which photos (every / picks, rated and labelled / only the one on screen); editing keeps the next photos decoded. Not yet: face detection, AI masks and AI Denoise as background jobs of these settings (they keep their own switches) | | LRC-LIB-SLIDESHOW-IMPROMPTU | Impromptu slideshow | P2 | ✅ | `cmd:view.slideshow` | see LR-VIEW-SLIDESHOW | | LRC-DEV-SNAPSHOTS | Named snapshots | P1 | ✅ | `cmd:version.create`, `cmd:version.restore` | = versions | diff --git a/docs/whats-new.md b/docs/whats-new.md index fdc89055fb6..7934c307a80 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -2,6 +2,11 @@ ## Unreleased +### Previews are built once +- Background preparation now builds previews at the same size as File ▸ Previews ▸ Build Standard-Sized Previews + (the Settings → Performance size, else at least 2560 px), so that build skips them instead of rendering every one + again (issue #3947). + ### Brush Feather works on strokes (process 16) - A brush stroke's edge now fades over the Feather you set. Overlapping dabs used to pile their soft edges up into an almost hard one: at Feather 100 and Flow 100, halfway out from a stroke's centre line was 90 % covered, and is