diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 9b7ef6a0b..cebd5473b 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -257,6 +257,9 @@ pub struct Session { /// Each footage file's size and modification time when last seen (Settings ▸ Import ▸ /// Reload Footage Changed on Disk, [`footage_reload`]). pub footage_stamps: std::collections::HashMap, + /// Footage reloaded so far ([`Session::forget_decoded`]): part of the Project panel's + /// thumbnail keys ([`Session::thumbnail_key`]), which otherwise look at no file. + pub footage_reloads: std::sync::atomic::AtomicU64, /// The footage files effects read the channels of, as last given to the footage source /// ([`Session::refresh_aux_footage`]); `None` = not given yet. pub aux_footage: Option>, @@ -390,6 +393,7 @@ impl Default for Session { check_footage_on_open: false, stored_paths: Default::default(), footage_stamps: Default::default(), + footage_reloads: Default::default(), aux_footage: None, aux_needs: None, script_calls: ScriptCalls::default(), diff --git a/crates/engine/src/session/render.rs b/crates/engine/src/session/render.rs index fab28da00..dccafa070 100644 --- a/crates/engine/src/session/render.rs +++ b/crates/engine/src/session/render.rs @@ -122,6 +122,37 @@ impl Session { } } + /// What a Project panel thumbnail of `item` at `t` (longest side `max_side`) shows depends + /// on, as a key (#3271): a comp's content as the disk cache keys frames + /// ([`effectcraft_render::disk_cache::comp_content_key`]), footage's item and settings, the + /// time, the size, the render switches the thumbnail uses and the footage reloads so far. + /// An edit that doesn't change the picture (a selection, another comp, a marker) keeps it, so + /// the thumbnail isn't rendered again. `None` for items without pixels. + pub fn thumbnail_key(&self, item: ItemId, t: Tick, max_side: u32) -> Option { + use effectcraft_render::disk_cache::Hash128; + let it = self.project.item(item)?; + let mut h = Hash128::default(); + h.write(b"thumbnail-v1"); + match &it.kind { + effectcraft_project::ItemKind::Comp(_) => { + h.write_u64(1); + h.write(&effectcraft_render::disk_cache::comp_content_key(&self.project, item).to_le_bytes()); + h.write_u64(t.0 as u64); + h.write_u64(u64::from(self.prefs.general.switches_affect_nested_comps)); + h.write_u64(u64::from(self.prefs.three_d.realtime_shadows)); + } + effectcraft_project::ItemKind::Footage(f) if f.has_video => { + h.write_u64(2); + h.write_u64(item.0); + h.write(format!("{f:?}").as_bytes()); + } + _ => return None, + } + h.write_u64(u64::from(max_side)); + h.write_u64(self.footage_reloads.load(std::sync::atomic::Ordering::Relaxed)); + Some(h.finish()) + } + /// [`Session::render_rgba8`], or with `transparent` the frame's own straight alpha (what a /// render with RGB + Alpha channels writes) instead of compositing over the background. pub fn render_rgba8_alpha(&self, comp: ItemId, t: Tick, max_side: u32, transparent: bool) -> Result<(u32, u32, Vec)> { @@ -138,6 +169,7 @@ impl Session { /// Make the media layer and the layer cache let go of what they decoded from `footage`'s /// files (it keys frames by path, so an overwritten file would keep showing its old pixels). pub(crate) fn forget_decoded(&self, footage: &effectcraft_project::Footage) { + self.footage_reloads.fetch_add(1, std::sync::atomic::Ordering::Relaxed); for p in std::iter::once(&footage.path).chain(&footage.sequence) { self.footage.forget(p); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 19953053e..8576e4a9a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -64,3 +64,4 @@ mod timeline_interchange; pub(crate) mod track; mod vector_import; mod viewer; +mod thumbnails; diff --git a/crates/engine/src/tests/thumbnails.rs b/crates/engine/src/tests/thumbnails.rs new file mode 100644 index 000000000..34595d9c7 --- /dev/null +++ b/crates/engine/src/tests/thumbnails.rs @@ -0,0 +1,30 @@ +//! #3271: Project panel thumbnails are keyed by what they show, not by the project's revision: +//! an edit elsewhere keeps a comp's thumbnail, an edit of the comp, another time or size, and a +//! footage reload give it a new key. + +use effectcraft_time::Tick; +use serde_json::json; + +use crate::Session; + +#[test] +fn thumbnail_keys_follow_the_content() { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "A", "width": 64, "height": 32, "frameRate": 24, "duration": 2})).unwrap(); + let a = s.active_comp_id().unwrap(); + s.execute("layer.newSolid", json!({"name": "S", "color": "#ff8000"})).unwrap(); + let key = |s: &Session| s.thumbnail_key(a, Tick::ZERO, 192).unwrap(); + let k0 = key(&s); + // Another comp: the project changed, this comp's picture didn't. + s.execute("comp.new", json!({"name": "B", "width": 16, "height": 16, "frameRate": 24, "duration": 1})).unwrap(); + assert_eq!(key(&s), k0, "an edit elsewhere keeps the key"); + assert_ne!(s.thumbnail_key(a, Tick::from_seconds_f64(0.5), 192).unwrap(), k0, "another time"); + assert_ne!(s.thumbnail_key(a, Tick::ZERO, 96).unwrap(), k0, "another size"); + // An edit of the comp itself. + std::sync::Arc::make_mut(&mut s.project).comp_mut(a).unwrap().background = [0.2, 0.4, 0.6]; + assert_ne!(key(&s), k0, "an edit of the comp gives a new key"); + // A reload: files may have changed without the project knowing. + let before = key(&s); + s.forget_decoded(&effectcraft_project::Footage::default()); + assert_ne!(key(&s), before, "a reload gives a new key"); +} diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index 8d67fd9ee..a2b48d04d 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -119,17 +119,39 @@ pub fn drop_folder(project: &effectcraft_engine::project::Project, row: Option)>>>>); +/// A thumbnail being rendered off the UI thread: (content key, result slot). +type PendingThumb = (u128, Arc)>>>>); -/// Thumbnail of a comp (its current time) or footage (first frame), cached per item/revision. -/// Rendered on a background thread (inline on wasm32): until the new one lands the previous -/// thumbnail stays up, so selecting a big comp or editing it never stalls the panel. +/// The content key of item `it`'s thumbnail at `t` ([`effectcraft_engine::Session::thumbnail_key`]), +/// worked out again only when the project's revision or the time changed. +fn thumbnail_key(app: &EffectcraftApp, ctx: &egui::Context, it: &Item, t: effectcraft_engine::time::Tick) -> u128 { + let id = egui::Id::unique(("proj-thumb-key", it.id.0)); + let rev = app.session.revision; + if let Some((r, at, k)) = ctx.data(|d| d.get_temp::<(u64, i64, u128)>(id)) + && r == rev + && at == t.0 + { + return k; + } + // (no key: the revision stands in, as before) + let k = app.session.thumbnail_key(it.id, t, THUMB_SIDE).unwrap_or(u128::from(rev) | (1 << 127)); + ctx.data_mut(|d| d.insert_temp(id, (rev, t.0, k))); + k +} + +/// The longest side of a Project panel thumbnail, in pixels. +const THUMB_SIDE: u32 = 192; + +/// Thumbnail of a comp (its current time) or footage (first frame), cached per item by what it +/// shows ([`thumbnail_key`], #3271): an edit that doesn't change the picture doesn't render it +/// again. Rendered on a background thread (inline on wasm32): until the new one lands the +/// previous thumbnail stays up, so selecting a big comp or editing it never stalls the panel. fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option { let key = egui::Id::unique(("proj-thumb", it.id.0)); let pkey = egui::Id::unique(("proj-thumb-pending", it.id.0)); - let rev = app.session.revision; - let cached: Option<(u64, egui::TextureHandle)> = ctx.data(|d| d.get_temp(key)); + let t = app.session.state.times.get(&it.id).copied().unwrap_or_default(); + let rev = thumbnail_key(app, ctx, it, t); + let cached: Option<(u128, egui::TextureHandle)> = ctx.data(|d| d.get_temp(key)); let mut tex = cached.as_ref().map(|(_, t)| t.clone()); let have = cached.as_ref().map(|(r, _)| *r); // A finished render: upload it. @@ -162,8 +184,7 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option)>>>> = Arc::default(); if cfg!(target_arch = "wasm32") { *slot.lock().ok()? = Some(job());