Skip to content
Open
36 changes: 26 additions & 10 deletions crates/engine/src/prep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,18 @@ pub struct PrepState {
pub library: Option<PrepSettings>,
/// The background build this preparation started.
build: Option<Arc<PreviewBuild>>,
/// The view (its signature) whose build was started, or that the user stopped.
started_for: Option<u64>,
/// 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<u64>,
/// 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,
Expand Down Expand Up @@ -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))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
// 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 {
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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();
Expand Down
81 changes: 81 additions & 0 deletions crates/engine/src/tests/preview_prep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions crates/ui-egui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. Refresh preparation when the requested preview size changes. The setting updates prep.edge, but the completed or running build is still keyed by the same view. Prepare a folder at 1600, switch to 3840, and stay in the folder: the next manual request can still rerender the old previews. Include the requested size in the build identity while preserving explicit Stop. Extend the same-size reuse regression with a size change in the same view.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 430443a: the started build is now remembered with the size it builds at, so a new size in the same view stops a running build and prepares the view again at that size. A stop from its row is still remembered by view alone, so it holds whatever size comes next. the_background_build_makes_previews_at_the_frontends_standard_size now changes 300 to 500 in the same view (fails without the fix), then checks that a stop holds through another size change.

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));
Expand Down
13 changes: 5 additions & 8 deletions crates/ui-egui/src/menus/menubar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<MenuNode>> {
Expand Down
12 changes: 12 additions & 0 deletions crates/ui-egui/src/state/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions crates/ui-egui/src/tests/lifecycle_037.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 7 additions & 4 deletions crates/ui-egui/src/tests/preview_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
}
Loading