Merge remote-tracking branch 'upstream/main' into onee/i786-psd-reader-craft-psd

This commit is contained in:
FredApps committed 2026-11-16 13:30:21 +01:00
commit f8aa110655
17 files changed
+629 -42

No files matched your search

+8
View File
@@ -167,6 +167,13 @@ pub struct Session {
pub journal: Vec<(String, Value)>,
/// Processed-layer pixels reused across frames and edits (content-keyed, never stale).
pub layer_cache: Arc<LayerCache>,
/// Set when this project closes ([`Session::replace_project`]), then replaced by a fresh flag
/// for the next one: work started for the project off the UI thread (Project panel
/// thumbnails) stops reading its footage and lets go of what it read, so it can't fill the
/// shared caches with a closed project's frames. Public only so that other crates can build a
/// `Session { .. }`.
#[doc(hidden)]
pub project_closed: Arc<std::sync::atomic::AtomicBool>,
/// The last Cryptomatte pick probe (see [`Session::crypto_probe_job`]). Public only so that
/// other crates can build a `Session { .. }`; its type is crate-private.
#[doc(hidden)]
@@ -351,6 +358,7 @@ impl Default for Session {
events: vec![],
journal: vec![],
layer_cache: Arc::new(LayerCache::default()),
project_closed: Default::default(),
crypto_probe: Default::default(),
disk_cache: None,
pending_disk_drops: Default::default(),
+12 -6
View File
@@ -196,6 +196,9 @@ impl Session {
/// Replace the whole project (open/new), resetting history and state.
pub fn replace_project(&mut self, mut p: Project, path: Option<String>) {
// Work still running for the closed project stops reading it (see `project_closed`).
self.project_closed.store(true, std::sync::atomic::Ordering::SeqCst);
self.project_closed = Arc::default();
if self.stop_render()
&& let Some(mut job) = self.render_job.take()
{
@@ -221,13 +224,13 @@ impl Session {
self.script_ui.project_epoch = self.script_ui.project_epoch.wrapping_add(1);
p.fix_next_id();
upgrade_effects(&mut p);
// The media layer keys decoded frames by path: files of the new project may have changed
// since an earlier project decoded them (#1070).
for it in p.items.values() {
if let effectcraft_project::ItemKind::Footage(f) = &it.kind {
self.forget_decoded(f);
}
// Let go of what the closed project's footage and proxies decoded (frames, decoders,
// models), and of what the new project's files decoded under an earlier project: the
// media layer keys them by path, and the files may have changed since (#1070).
for f in super::render::project_footage(&self.project).chain(super::render::project_footage(&p)) {
super::render::forget_files(self.footage.as_ref(), f);
}
self.drop_crypto_probe();
self.layer_cache.clear();
self.project = Arc::new(p);
// The set of footage effects read the channels of is sent again, even when it is empty.
@@ -237,6 +240,9 @@ impl Session {
self.path = path;
self.bump();
self.mark_saved();
// The viewers' RAM preview shows the closed project's frames, whose comp ids can collide
// with the new project's.
self.events.push(Event::PurgeCaches);
let first = self.project.comps().next().map(|(id, _)| *id);
if let Some(c) = first {
self.open_comp(c);
+48 -8
View File
@@ -1,3 +1,7 @@
use std::sync::atomic::Ordering;
use effectcraft_render::{CancellableFootage, FootageSource};
use super::{EngineError, Image, ItemId, RenderOpts, Renderer, Result, Session, ThumbnailJob, Tick, Value, commands};
impl Session {
@@ -77,9 +81,12 @@ impl Session {
/// A Project panel thumbnail of an item (a comp at `t`, footage at its first frame), longest
/// side at most `max_side`, as a job that owns everything it reads: frontends run it off the
/// UI thread (a big comp takes far longer than a frame). `None` for items without pixels.
/// Once the project closes ([`Session::replace_project`]) the job reads no more and answers
/// `None`, after letting go of what it read meanwhile: a thread that started late or decoded
/// through Close can't fill the shared caches with the closed project's frames.
pub fn thumbnail_job(&self, item: ItemId, t: Tick, max_side: u32) -> Option<ThumbnailJob> {
let it = self.project.item(item)?;
let footage = self.footage.clone();
let (footage, closed) = (self.footage.clone(), self.project_closed.clone());
match &it.kind {
effectcraft_project::ItemKind::Comp(c) => {
let (project, expr, cache) = (self.project.clone(), self.expr.clone(), self.layer_cache.clone());
@@ -93,21 +100,38 @@ impl Session {
};
let bg = c.background;
Some(Box::new(move || {
let mut r = Renderer::new(&project, footage.as_ref(), opts);
if closed.load(Ordering::SeqCst) {
return None;
}
let src = CancellableFootage { inner: footage.as_ref(), cancel: closed.clone() };
let mut r = Renderer::new(&project, &src, opts);
r.expr = expr.as_deref();
r.cache = Some(&cache);
let img = r.comp_frame(item, t);
if closed.load(Ordering::SeqCst) {
project_footage(&project).for_each(|f| forget_files(footage.as_ref(), f));
cache.clear();
return None;
}
Some((img.width, img.height, img.to_rgba8_over(bg)))
}))
}
effectcraft_project::ItemKind::Footage(f) if f.has_video => {
let f = f.clone();
Some(Box::new(move || {
if closed.load(Ordering::SeqCst) {
return None;
}
// Vector footage is drawn at the thumbnail's size: its declared size can be a
// 16384² canvas, gigabytes of pixels to point-sample down to a few dozen.
let scale = (max_side.max(1) as f64 / f.width.max(f.height).max(1) as f64).min(1.0);
let src = footage.vector_frame(item, &f, scale).or_else(|| footage.frame(item, &f, Tick(0)))?;
let img = effectcraft_render::cineon_frame(src, &f);
let read = CancellableFootage { inner: footage.as_ref(), cancel: closed.clone() };
let src = read.vector_frame(item, &f, scale).or_else(|| read.frame(item, &f, Tick(0)));
if closed.load(Ordering::SeqCst) {
forget_files(footage.as_ref(), &f);
return None;
}
let img = effectcraft_render::cineon_frame(src?, &f);
// Point-sample down to at most `max_side` on the long side (a drawn size one
// pixel over it, from rounding, is shrunk by that pixel, not halved).
let (long, max) = (u64::from(img.width.max(img.height)), u64::from(max_side.max(1)));
@@ -119,7 +143,7 @@ impl Session {
for x in 0..w {
let (sx, sy) = (u64::from(x) * u64::from(img.width) / u64::from(w), u64::from(y) * u64::from(img.height) / u64::from(h));
let i = ((sy * u64::from(img.width) + sx) * 4) as usize;
px.extend_from_slice(&full[i..i + 4]);
px.extend_from_slice(full.get(i..i + 4)?);
}
}
Some((w, h, px))
@@ -145,9 +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) {
for p in std::iter::once(&footage.path).chain(&footage.sequence) {
self.footage.forget(p);
}
forget_files(self.footage.as_ref(), footage);
self.layer_cache.clear();
self.drop_crypto_probe();
}
@@ -191,3 +213,21 @@ pub(crate) fn aux_footage(project: &effectcraft_project::Project) -> Vec<String>
paths.dedup();
paths
}
/// Make `src` let go of what it decoded from `footage`'s files (its file, or a sequence's).
pub(crate) fn forget_files(src: &dyn FootageSource, footage: &effectcraft_project::Footage) {
for p in std::iter::once(&footage.path).chain(&footage.sequence) {
src.forget(p);
}
}
/// Every footage `project` shows: its footage items and the proxies of its items.
pub(crate) fn project_footage(project: &effectcraft_project::Project) -> impl Iterator<Item = &effectcraft_project::Footage> {
project.items.values().flat_map(|it| {
let own = match &it.kind {
effectcraft_project::ItemKind::Footage(f) => Some(f),
_ => None,
};
own.into_iter().chain(it.proxy.as_deref().map(|px| &px.footage))
})
}
+1
View File
@@ -46,6 +46,7 @@ mod motion_blur_depth;
mod paint;
mod panels;
mod project_items;
mod project_release;
mod proxy;
mod quick_offset;
mod render_overwrite;
+161
View File
@@ -0,0 +1,161 @@
//! Close, New and Open Project let go of what the closed project decoded (#2042).
use std::sync::{Arc, Mutex};
use effectcraft_project::{Footage, ItemId, ItemKind, Project};
use effectcraft_time::Tick;
use serde_json::json;
use crate::render::{FootageSource, Image};
use crate::{Event, Session};
/// A footage source that records the paths it is told to forget.
#[derive(Default)]
struct Recording(Mutex<Vec<String>>);
impl FootageSource for Recording {
fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
None
}
fn forget(&self, path: &str) {
self.0.lock().unwrap().push(path.to_string());
}
}
fn with_footage(base: &Project, name: &str, path: &str) -> Project {
let mut p = base.clone();
p.add_item(name, Default::default(), None, ItemKind::Footage(Footage { path: path.into(), ..Default::default() }));
p
}
#[test]
fn replacing_project_releases_old_footage() {
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Main", "width": 640, "height": 360, "frameRate": 30, "duration": 10})).unwrap();
let src = Arc::new(Recording::default());
s.footage = src.clone();
let recorded = || src.0.lock().unwrap().clone();
let open_old = |s: &mut Session| {
let old = with_footage(&s.project, "old", "/old.mp4");
s.replace_project(old, None);
s.drain_events();
src.0.lock().unwrap().clear();
};
// Close Project lets go of the closed project's footage and clears the RAM preview.
open_old(&mut s);
s.execute("file.closeProject", json!({})).unwrap();
assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded());
assert!(s.drain_events().contains(&Event::PurgeCaches));
// Opening a project forgets the old project's files and the new project's.
open_old(&mut s);
s.replace_project(with_footage(&Project::default(), "new", "/new.mp4"), None);
let paths = recorded();
assert!(paths.contains(&"/old.mp4".to_string()) && paths.contains(&"/new.mp4".to_string()), "{paths:?}");
assert!(s.drain_events().contains(&Event::PurgeCaches));
// New Project does the same.
open_old(&mut s);
s.execute("file.newProject", json!({})).unwrap();
assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded());
assert!(s.drain_events().contains(&Event::PurgeCaches));
}
#[test]
fn closing_a_project_releases_its_proxies() {
let mut s = Session::default();
let src = Arc::new(Recording::default());
s.footage = src.clone();
let mut p = with_footage(&Project::default(), "old", "/old.mp4");
let item = p.items.values_mut().next().unwrap();
let footage = Footage { path: "/old-proxy.mov".into(), ..Default::default() };
item.proxy = Some(Box::new(effectcraft_project::Proxy { footage, enabled: true }));
s.replace_project(p, None);
src.0.lock().unwrap().clear();
s.execute("file.closeProject", json!({})).unwrap();
assert!(src.0.lock().unwrap().contains(&"/old-proxy.mov".to_string()), "{:?}", src.0.lock().unwrap());
}
/// A footage source with a memory cache, like the media pool: a frame read keeps its file's
/// entry until the file is forgotten. With a gate, the first read waits for the test.
#[derive(Default)]
struct Caching {
cached: Mutex<std::collections::HashSet<String>>,
gate: Mutex<Option<(std::sync::mpsc::Sender<()>, std::sync::mpsc::Receiver<()>)>>,
}
impl FootageSource for Caching {
fn frame(&self, _: ItemId, footage: &Footage, _: Tick) -> Option<Arc<Image>> {
let gate = self.gate.lock().unwrap().take();
if let Some((entered, go)) = gate {
entered.send(()).unwrap();
go.recv().unwrap();
}
self.cached.lock().unwrap().insert(footage.path.clone());
Some(Arc::new(Image::new(footage.width, footage.height)))
}
fn forget(&self, path: &str) {
self.cached.lock().unwrap().remove(path);
}
}
/// A session showing `/old.png` (a footage item, and a comp with a layer of it) through a
/// [`Caching`] source: (session, source, footage item, comp).
fn old_project() -> (Session, Arc<Caching>, ItemId, ItemId) {
let mut s = Session::default();
let src = Arc::new(Caching::default());
s.footage = src.clone();
let still = Footage { path: "/old.png".into(), has_video: true, width: 8, height: 8, ..Default::default() };
let mut p = Project::default();
let item = p.add_item("old", Default::default(), None, ItemKind::Footage(still));
s.replace_project(p, None);
s.execute("comp.new", json!({"name": "Main", "width": 8, "height": 8, "frameRate": 30, "duration": 1})).unwrap();
s.execute("layer.addItem", json!({"item": item.0})).unwrap();
let comp = s.active_comp_id().unwrap();
(s, src, item, comp)
}
#[test]
fn held_thumbnail_jobs_cannot_refill_the_closed_projects_caches() {
for close in ["file.closeProject", "file.newProject"] {
let (mut s, src, item, comp) = old_project();
// Project panel thumbnails, made while the project is open and run after it closed (a
// thread that started late).
let jobs = [s.thumbnail_job(item, Tick(0), 16).unwrap(), s.thumbnail_job(comp, Tick(0), 16).unwrap()];
s.execute(close, json!({})).unwrap();
let shown: Vec<_> = jobs.into_iter().map(|job| job()).collect();
assert!(src.cached.lock().unwrap().is_empty(), "{close}: the closed project's frames came back");
assert_eq!(s.layer_cache.stats().entries, 0, "{close}: the closed project's layers came back");
assert!(shown.iter().all(Option::is_none), "{close}: a closed project's thumbnail isn't shown");
}
}
#[test]
fn thumbnails_reading_through_close_let_go_of_what_they_read() {
for which in ["footage", "comp"] {
let (mut s, src, item, comp) = old_project();
let (entered, entered_rx) = std::sync::mpsc::channel();
let (go, go_rx) = std::sync::mpsc::channel();
*src.gate.lock().unwrap() = Some((entered, go_rx));
let job = s.thumbnail_job(if which == "comp" { comp } else { item }, Tick(0), 16).unwrap();
let running = std::thread::spawn(job);
// The job is decoding the closed project's still when the project closes.
entered_rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap();
s.execute("file.closeProject", json!({})).unwrap();
go.send(()).unwrap();
let shown = running.join().unwrap();
assert!(src.cached.lock().unwrap().is_empty(), "{which}: the frame decoded through Close was kept");
assert_eq!(s.layer_cache.stats().entries, 0, "{which}: the closed project's layers were kept");
assert!(shown.is_none(), "{which}: a closed project's thumbnail isn't shown");
}
}
#[test]
fn an_open_projects_thumbnails_still_render() {
let (s, src, item, comp) = old_project();
let (w, h, px) = s.thumbnail_job(item, Tick(0), 16).unwrap()().unwrap();
assert_eq!((w, h, px.len()), (8, 8, 8 * 8 * 4));
assert!(s.thumbnail_job(comp, Tick(0), 16).unwrap()().is_some());
assert!(src.cached.lock().unwrap().contains("/old.png"));
}
+54 -8
View File
@@ -208,6 +208,10 @@ struct CacheState {
last: HashMap<Arc<str>, i64>,
/// Movies whose frames can't be kept as 8-bit levels (more bits, or shown turned).
wide: HashSet<Arc<str>>,
/// Counts [`MediaPool::forget`]s and frame clears: a decode keeps its frame only when none
/// came while it ran, so one in flight when a file is forgotten (a closed project's, or one
/// changed on disk) can't bring the frame back.
forgets: u64,
}
/// A movie/audio source by path ([`Opened`]), opened by the first caller while the others wait
@@ -406,6 +410,7 @@ impl MediaPool {
let mut c = lock(&self.inner.cache);
c.lru = Lru::default();
c.last.clear();
c.forgets = c.forgets.wrapping_add(1);
drop(c);
// Decoded GIFs sit outside the frame budget; Purge frees them too.
lock(&self.inner.gifs).clear();
@@ -418,7 +423,9 @@ impl MediaPool {
lock(&self.inner.problems).clear();
}
/// Forget a file (after it changed on disk, or to retry a file that failed to open).
/// Forget a file (after it changed on disk or its project closed, or to retry a file that
/// failed to open). A decode of any file in flight meanwhile keeps nothing (see
/// [`CacheState::forgets`]).
pub fn forget(&self, path: &str) {
lock(&self.inner.sources).remove(path);
lock(&self.inner.models).remove(path);
@@ -431,6 +438,7 @@ impl MediaPool {
}
c.last.remove(path);
c.wide.remove(path);
c.forgets = c.forgets.wrapping_add(1);
drop(c);
crate::exr_channels::forget(path);
}
@@ -455,7 +463,7 @@ impl MediaPool {
if lock(&self.inner.cache).wide.contains(footage.path.as_str()) {
return None;
}
match Inner::frame_px(&self.inner, footage, t, true, None, float, true) {
match Inner::frame_px(&self.inner, footage, t, true, None, float, true, None) {
Ok(Pixels::Q8(f)) => Some(f),
_ => None,
}
@@ -850,7 +858,7 @@ impl Inner {
/// The frame of `footage` at `t`, resampled to `size` (see [`FootageSource::frame_at_size`]):
/// a movie's made at that size directly, a still's resampled once and kept at that size.
fn frame_at(self: &Arc<Self>, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve) -> Result<Arc<Image>> {
match Self::frame_px(self, footage, t, playback, size, float, false)? {
match Self::frame_px(self, footage, t, playback, size, float, false, None)? {
Pixels::F32(img) => Ok(img),
// (a float request's key never holds 8-bit levels)
Pixels::Q8(_) => Err(MediaError::Decode(format!("{}: 8-bit frame for a float request", footage.path))),
@@ -859,8 +867,20 @@ impl Inner {
/// [`Self::frame_at`], kept as 8-bit levels when `q8` is set and the frame has 8 bits (a
/// float frame under the float key otherwise, and the movie is remembered as `wide`).
/// `since`: the [`CacheState::forgets`] count the read was asked at (a read-ahead's, when it
/// was queued); a read that missed is not decoded once a forget came after it, and a frame
/// decoded while one came is not kept.
#[allow(clippy::too_many_arguments)]
fn frame_px(self: &Arc<Self>, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) -> Result<Pixels> {
fn frame_px(
self: &Arc<Self>,
footage: &Footage,
t: Tick,
playback: bool,
size: Option<(u32, u32)>,
float: FloatCurve,
q8: bool,
since: Option<u64>,
) -> Result<Pixels> {
if !footage.has_video {
return Err(MediaError::Unsupported(format!("{}: no video", footage.path)));
}
@@ -909,6 +929,10 @@ impl Inner {
}
c = self.done.wait(c).unwrap_or_else(std::sync::PoisonError::into_inner);
}
let since = since.unwrap_or(c.forgets);
if c.forgets != since {
return Err(MediaError::Cancelled);
}
self.misses.fetch_add(1, Ordering::Relaxed);
c.inflight.insert(loc.key.clone());
drop(c);
@@ -917,6 +941,9 @@ impl Inner {
reads::note_all(&files);
let img = img?;
let mut c = lock(&self.cache);
if c.forgets != since {
return Ok(img);
}
if q8 && let Pixels::F32(_) = img {
// not 8-bit: kept as the float frame it is, which the renderer asks for next
c.wide.insert(loc.key.path.clone());
@@ -935,23 +962,23 @@ impl Inner {
next.key.size = loc.key.size;
next.key.q8 = loc.key.q8;
if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) {
self.prefetch(footage, next_t, size, float, loc.key.q8);
self.prefetch(footage, next_t, size, float, loc.key.q8, c.forgets);
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn prefetch(self: &Arc<Self>, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) {
fn prefetch(self: &Arc<Self>, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool, since: u64) {
let (me, f) = (self.clone(), footage.clone());
self.prefetched.fetch_add(1, Ordering::Relaxed);
// A plain thread, not a rayon job: the decode below parallelises with rayon itself.
std::thread::spawn(move || {
let _ = me.frame_px(&f, t, false, size, float, q8);
let _ = me.frame_px(&f, t, false, size, float, q8, Some(since));
});
}
#[cfg(target_arch = "wasm32")]
fn prefetch(self: &Arc<Self>, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool) {}
fn prefetch(self: &Arc<Self>, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool, _since: u64) {}
/// A still (image file or sequence frame) at `loc.key.size` when one is set: resampled as the
/// renderer would ([`FootageSource::frame_at_size`]), so the pool keeps the reduced frame and a
@@ -1215,6 +1242,25 @@ fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
mod tests {
use super::*;
/// #2042: a read queued before its file was forgotten (as a read-ahead of a closed project's
/// movie is, by the read of the frame before) decodes nothing and keeps nothing; a read asked
/// after the forget decodes and keeps its frame. (A still stands in for the movie.)
#[test]
fn reads_queued_before_a_forget_keep_nothing() {
let mut png = std::io::Cursor::new(Vec::new());
image::RgbaImage::new(2, 2).write_to(&mut png, image::ImageFormat::Png).unwrap();
let pool = MediaPool::new();
pool.add_bytes("/a.png", png.into_inner().into());
let f = Footage { path: "/a.png".into(), kind: FootageKind::Still, width: 2, height: 2, has_video: true, ..Default::default() };
let queued = lock(&pool.inner.cache).forgets;
pool.forget("/a.png");
let read = Inner::frame_px(&pool.inner, &f, Tick::ZERO, false, None, FloatCurve::Srgb, false, Some(queued));
assert!(matches!(read, Err(MediaError::Cancelled)), "{:?}", read.err());
assert_eq!(pool.stats().frames, 0, "the forgotten file's frame came back");
pool.frame_at(&f, Tick::ZERO).unwrap();
assert_eq!(pool.stats().frames, 1);
}
/// Conforming a long file's audio reads every chunk at its own time: the sample index times
/// ticks per second overflowed `i64` past 12:36 at 48 kHz, and later chunks came out silent
/// (#172).
+4 -4
View File
@@ -73,9 +73,9 @@ pub trait FootageSource: Send + Sync {
/// Drop the decoded frames it keeps (Edit ▸ Purge ▸ All Memory / Image Cache Memory): the
/// next reads decode again. Sources without a cache ignore it.
fn purge(&self) {}
/// The file at `path` changed on disk (Reload Footage): drop what it keeps of it (decoded
/// frames, open decoders, parsed models), so the next reads see the new contents. Sources
/// without a cache ignore it.
/// The file at `path` changed on disk (Reload Footage), or the project showing it closed:
/// drop what it keeps of it (decoded frames, open decoders, parsed models), so the next reads
/// see the new contents. Sources without a cache ignore it.
fn forget(&self, _path: &str) {}
/// Settings ▸ Disk ▸ Conformed Audio Folder: where decoded audio is kept between reads
/// (`None` = off); sources that don't decode audio ignore it.
@@ -205,7 +205,7 @@ impl FootageSource for CancellableFootage<'_> {
self.inner.cache_budget()
}
fn vector_frame(&self, item: ItemId, footage: &Footage, scale: f64) -> Option<Arc<Image>> {
self.inner.vector_frame(item, footage, scale)
self.read(|| self.inner.vector_frame(item, footage, scale))
}
fn cancellable(&self, cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
self.inner.cancellable(cancel, f)
+1
View File
@@ -159,6 +159,7 @@ impl EffectcraftApp {
}
effectcraft_engine::Event::PurgeCaches => {
self.frames.clear();
crate::panels::project::forget_thumbnails(ctx);
// (frames made after it get keys of their own: see `Self::purge_stamp`)
self.purges = self.purges.wrapping_add(1);
}
+1
View File
@@ -9,6 +9,7 @@ pub mod crypto_pick;
pub mod delete_items;
pub mod dialogs;
pub mod dialogs_3d;
pub(crate) mod drag_preview;
pub mod effect_controls;
pub mod effects_presets;
pub mod essential;
+54
View File
@@ -0,0 +1,54 @@
//! Fast Previews ▸ Adaptive Resolution for drags outside the viewer: as in After Effects, a drag
//! that changes the frame on screen (scrubbing the time, moving or trimming layers, slipping,
//! dragging keys, scrubbing values) draws a draft until the button is released.
use std::sync::Arc;
use effectcraft_engine::project::{ItemId, Project};
use crate::EffectcraftApp;
/// The project, the active comp and its current frame index when a panel starts its pass.
/// [`DragWatch::end`] compares them with the comp and frame after the panel ran its gestures, so
/// every drag the panel handles is covered without a flag per gesture.
pub(crate) struct DragWatch {
before: Option<(ItemId, Arc<Project>, i64)>,
}
impl DragWatch {
pub(crate) fn begin(app: &EffectcraftApp) -> Self {
let project = app.session.project.clone();
let before = app.session.active_comp_id().and_then(|id| {
let frame = project.comp_arc(id)?.frame_rate.frame_at(app.session.time());
Some((id, project, frame))
});
Self { before }
}
/// While the primary button drags (moved past a click, or held past one), a change of the
/// current frame or of the active comp's pixels since [`Self::begin`] sets
/// `property_interacting`, which stays set until the button is up (`App::frame`). Pixels are
/// compared as the frame cache does: markers count when an expression reads them, a layer's
/// audio when something draws it, before or after the edit. A click, or a drag that draws
/// the same pixels (scrolling, zooming, the work area, selecting) keeps Full.
pub(crate) fn end(self, app: &mut EffectcraftApp, ctx: &egui::Context) {
let Some((id, project, frame)) = self.before else { return };
if app.ui.viewer.property_interacting || !ctx.input(|i| i.pointer.primary_down() && i.pointer.is_decidedly_dragging()) {
return;
}
if app.session.active_comp_id() != Some(id) {
return;
}
let (Some(was), Some(now)) = (project.comp_arc(id), app.session.project.comp_arc(id)) else { return };
let moved = was.frame_rate.frame_at(app.session.time()) != frame;
let edited = || {
let current = &app.session.project;
let heard = current.heard_layers().union(&project.heard_layers());
!now.same_pixels_with(&was, current.reads_markers(), &|l| heard.contains(id, l))
};
if moved || (!Arc::ptr_eq(&was, &now) && edited()) {
app.ui.viewer.property_interacting = true;
ctx.request_repaint();
}
}
}
+45 -7
View File
@@ -120,8 +120,27 @@ pub fn drop_folder(project: &effectcraft_engine::project::Project, row: Option<I
if it.is_folder() { Some(it.id) } else { it.parent }
}
/// A Project panel thumbnail: (the revision it shows, its texture).
#[derive(Clone)]
struct Thumb(u64, egui::TextureHandle);
/// A thumbnail being rendered off the UI thread: (revision, result slot).
type PendingThumb = (u64, Arc<Mutex<Option<Option<(u32, u32, Vec<u8>)>>>>);
#[derive(Clone)]
struct PendingThumb(u64, Arc<Mutex<Option<Option<(u32, u32, Vec<u8>)>>>>);
/// The Project panel's thumbnail texture of `item`, once its render has landed.
pub fn cached_thumbnail(ctx: &egui::Context, item: ItemId) -> Option<egui::TextureHandle> {
ctx.data(|d| d.get_temp::<Thumb>(egui::Id::unique(("proj-thumb", item.0)))).map(|Thumb(_, t)| t)
}
/// Let go of every Project panel thumbnail and the renders in flight for them (a cache purge, or
/// a project closed: the next project's item ids can be the closed one's).
pub fn forget_thumbnails(ctx: &egui::Context) {
ctx.data_mut(|d| {
d.remove_by_type::<Thumb>();
d.remove_by_type::<PendingThumb>();
});
}
/// 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
@@ -130,19 +149,19 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option<egu
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 mut tex = cached.as_ref().map(|(_, t)| t.clone());
let have = cached.as_ref().map(|(r, _)| *r);
let cached: Option<Thumb> = ctx.data(|d| d.get_temp(key));
let mut tex = cached.as_ref().map(|Thumb(_, t)| t.clone());
let have = cached.as_ref().map(|Thumb(r, _)| *r);
// A finished render: upload it.
let pending: Option<PendingThumb> = ctx.data(|d| d.get_temp(pkey));
if let Some((prev, slot)) = &pending {
if let Some(PendingThumb(prev, slot)) = &pending {
let done = slot.lock().ok().and_then(|mut s| s.take());
match done {
Some(Some((w, h, px))) => {
let ci = egui::ColorImage::from_rgba_unmultiplied([w as usize, h as usize], &px);
let t = ctx.load_texture(format!("proj-thumb-{}", it.id.0), ci, egui::TextureOptions::LINEAR);
ctx.data_mut(|d| {
d.insert_temp(key, (*prev, t.clone()));
d.insert_temp(key, Thumb(*prev, t.clone()));
d.remove::<PendingThumb>(pkey);
});
tex = Some(t);
@@ -180,7 +199,7 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option<egu
return tex;
}
}
ctx.data_mut(|d| d.insert_temp::<PendingThumb>(pkey, (rev, slot)));
ctx.data_mut(|d| d.insert_temp(pkey, PendingThumb(rev, slot)));
ctx.request_repaint();
tex
}
@@ -1512,6 +1531,25 @@ mod tests {
}
}
/// #2042: closing the project drops the Project panel's thumbnails (and renders in flight),
/// so their textures go with it and an item of the next project with a closed one's id never
/// shows the closed project's picture.
#[test]
fn closing_the_project_forgets_its_thumbnails() {
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
let ctx = egui::Context::default();
let (key, pkey) = (egui::Id::unique(("proj-thumb", 1u64)), egui::Id::unique(("proj-thumb-pending", 1u64)));
let tex = ctx.load_texture("t", egui::ColorImage::from_rgba_unmultiplied([1, 1], &[255; 4]), egui::TextureOptions::LINEAR);
ctx.data_mut(|d| {
d.insert_temp(key, Thumb(app.session.revision, tex));
d.insert_temp(pkey, PendingThumb(app.session.revision, Arc::default()));
});
app.session.execute("file.closeProject", serde_json::json!({})).unwrap();
app.handle_events(&ctx);
assert!(ctx.data(|d| d.get_temp::<Thumb>(key)).is_none(), "the closed project's thumbnail is kept");
assert!(ctx.data(|d| d.get_temp::<PendingThumb>(pkey)).is_none(), "a closed project's render is awaited");
}
/// Long names show in full: the Name column fits them (it stopped at 260 points with "…")
/// up to the panel's width less the Label column and, while optional columns show, room for
/// them; a width the user dragged wins.
+2
View File
@@ -67,6 +67,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
p.text(rect.center(), Align2::CENTER_CENTER, crate::i18n::tr("Select a layer to see its properties"), Tokens::ui(12.0), t.text_faint);
return;
};
let watch = super::drag_preview::DragWatch::begin(app);
let project = app.session.project.clone();
let expr = app.session.expr.clone();
let now = app.session.time();
@@ -190,6 +191,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
app.ui.status = e;
}
}
watch.end(app, &ctx);
}
fn section_header(app: &mut EffectcraftApp, p: &egui::Painter, x0: f32, w: f32, y: f32, title: &str, auto: &str) {
+4 -6
View File
@@ -1981,6 +1981,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
return;
};
let Some(comp) = app.session.project.comp_arc(cid) else { return };
let watch = super::drag_preview::DragWatch::begin(app);
let mut tl = layout(app, ui, rect, cid, &comp);
let g = &mut tl;
if !ui.input(|i| i.pointer.primary_down()) {
@@ -2040,7 +2041,8 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
drop_targets(app, ui, g);
let Rw { tl, ui_actions, .. } = rw;
apply_ui_acts(app, &tl.comp, ui_actions);
apply_actions(app, ui, &tl.ctx, tl.actions);
apply_actions(app, &tl.ctx, tl.actions);
watch.end(app, &tl.ctx);
}
/// Applies the UI-only changes the frame collected (twirls, time, ending an undo merge).
@@ -2099,11 +2101,7 @@ fn apply_ui_acts(app: &mut EffectcraftApp, comp: &Comp, ui_actions: Vec<UiAct>)
}
/// Runs the commands the frame collected through [`crate::menus::invoke`].
fn apply_actions(app: &mut EffectcraftApp, ui: &mut egui::Ui, ctx: &egui::Context, actions: Vec<(String, serde_json::Value)>) {
if ui.input(|i| i.pointer.primary_down()) && actions.iter().any(|(id, _)| id == "prop.set") {
app.ui.viewer.property_interacting = true;
ctx.request_repaint();
}
fn apply_actions(app: &mut EffectcraftApp, ctx: &egui::Context, actions: Vec<(String, serde_json::Value)>) {
for (id, params) in actions {
if id == "__endMerge" {
app.session.history.merge_key = None;
+3
View File
@@ -265,6 +265,9 @@ pub struct ViewerState {
/// The pointer is dragging in the viewer this frame (Adaptive Resolution).
#[serde(skip)]
pub interacting: bool,
/// A drag outside the viewer changed the frame on screen: a property scrub in Effect Controls,
/// or any Timeline or Properties drag ([`crate::panels::drag_preview`]). Adaptive Resolution
/// draws a draft until the primary button is up, which clears it.
#[serde(skip)]
pub property_interacting: bool,
/// Extended Viewer: the comp-space region the viewer renders this frame (comp frame plus
+1
View File
@@ -101,6 +101,7 @@ mod ui_time_zoom_controls;
mod ui_timeline_audio_levels;
mod ui_timeline_effect_label;
mod ui_timeline_group_select;
mod ui_timeline_interacting;
mod ui_timeline_marquee;
mod ui_timeline_pan;
mod ui_timeline_shapes;
+1 -3
View File
@@ -64,9 +64,7 @@ fn comp_thumbnail_renders_off_the_ui_thread() {
let mut landed = false;
for _ in 0..400 {
h.step();
if h.state().auto.find("project.thumbnail").is_some()
&& h.ctx.data(|d| d.get_temp::<(u64, egui::TextureHandle)>(egui::Id::unique(("proj-thumb", main.0)))).is_some()
{
if h.state().auto.find("project.thumbnail").is_some() && effectcraft_ui_egui::panels::project::cached_thumbnail(&h.ctx, main).is_some() {
landed = true;
break;
}
@@ -0,0 +1,229 @@
//! Fast Previews ▸ Adaptive Resolution while dragging outside the viewer (After Effects): any
//! Timeline drag that changes the frame on screen (the time, or the comp's pixels) draws a draft
//! until release, and the first frame after the release is full resolution again. A plain click,
//! or a drag that changes nothing on screen, keeps Full.
use effectcraft_engine::Session;
use effectcraft_ui_egui::EffectcraftApp;
use egui::{Event, Pos2, pos2, vec2};
use egui_kittest::Harness;
use serde_json::json;
/// The Timeline of a 10 s comp at 30 fps with a solid (opacity keys at 0 s and 1 s, revealed)
/// and a 20 s precomp trimmed to 3–7 s (its source runs past both ends, so it can slip), in
/// Adaptive Resolution.
struct Fixture {
h: Harness<'static, EffectcraftApp>,
solid: u64,
pre: u64,
opacity: u64,
}
fn fixture(mode: &str) -> Fixture {
let mut s = Session::default();
s.execute("comp.new", json!({"name": "Inner", "width": 320, "height": 180, "frameRate": 30, "duration": 20})).unwrap();
s.execute("comp.new", json!({"name": "Main", "width": 320, "height": 180, "frameRate": 30, "duration": 10})).unwrap();
let solid = s.execute("layer.newSolid", json!({"name": "Box", "color": "#e04020", "width": 80, "height": 80})).unwrap()["layer"].as_u64().unwrap();
for (time, value) in [(0.0, 0.0), (1.0, 100.0)] {
s.execute("prop.addKey", json!({"layer": solid, "path": "transform/opacity", "time": time, "value": value})).unwrap();
}
let pre = s.execute("layer.addItem", json!({"item": "Inner"})).unwrap()["layer"].as_u64().unwrap();
s.execute("layer.timing", json!({"layers": [pre], "in": 3.0, "out": 7.0})).unwrap();
let comp = s.active_comp().unwrap();
let opacity = comp.layers.iter().find(|l| l.id.0 == solid).unwrap().props.prop("transform/opacity").unwrap().uid;
s.execute("layer.select", json!({"layers": [solid]})).unwrap();
s.execute("view.fastPreviewMode", json!({"mode": mode})).unwrap();
let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(s));
h.run_steps(3);
let ctx = h.ctx.clone();
effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "timeline.reveal.opacity", json!({})).unwrap();
h.run_steps(4);
Fixture { h, solid, pre, opacity }
}
fn rect(h: &Harness<'_, EffectcraftApp>, id: &str) -> egui::Rect {
let e = h.state().auto.find(id).unwrap_or_else(|| panic!("no {id}"));
egui::Rect::from_min_size(pos2(e.rect[0], e.rect[1]), vec2(e.rect[2], e.rect[3]))
}
/// What a frame-changing drag must change: the project or the current time.
fn on_screen(h: &Harness<'_, EffectcraftApp>) -> (u64, effectcraft_time::Tick) {
(h.state().session.revision, h.state().session.time())
}
/// Presses at `from` and moves to `to` in ten steps, leaving the button down.
fn hold(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2) {
h.input_mut().events.push(Event::PointerMoved(from));
h.step();
h.input_mut().events.push(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
h.step();
for step in 1..=10 {
h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (step as f32 / 10.0)));
h.step();
}
}
/// Releases at `at` and runs exactly one frame: that frame must already be sharp.
fn release(h: &mut Harness<'_, EffectcraftApp>, at: Pos2) {
h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() });
h.step();
}
fn draft(h: &Harness<'_, EffectcraftApp>) -> bool {
h.state().viewer_scale(1.0, 1.0) < 1.0
}
/// Drags from `from` by `by`: a draft while held, Full on the first frame after release.
fn assert_draft_while_dragging(f: &mut Fixture, from: Pos2, by: egui::Vec2) {
let before = on_screen(&f.h);
hold(&mut f.h, from, from + by);
assert_ne!(on_screen(&f.h), before, "the drag changed nothing on screen");
assert!(f.h.state().ui.viewer.property_interacting, "not interacting while dragging");
assert!(draft(&f.h), "full resolution while dragging");
release(&mut f.h, from + by);
assert!(!f.h.state().ui.viewer.property_interacting, "still interacting after release");
assert!(!draft(&f.h), "the first frame after release is a draft");
}
#[test]
fn playhead_drag_draws_a_draft() {
let mut f = fixture("adaptive");
let ruler = rect(&f.h, "timeline.ruler");
assert_draft_while_dragging(&mut f, pos2(ruler.min.x + 40.0, ruler.max.y - 3.0), vec2(200.0, 0.0));
}
#[test]
fn timecode_drag_draws_a_draft() {
let mut f = fixture("adaptive");
let from = rect(&f.h, "timeline.timecode").center();
assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0));
}
#[test]
fn layer_bar_drag_draws_a_draft() {
let mut f = fixture("adaptive");
let from = rect(&f.h, &format!("timeline.layer.{}.bar", f.solid)).center();
assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0));
}
#[test]
fn in_point_trim_draws_a_draft() {
let mut f = fixture("adaptive");
let from = rect(&f.h, &format!("timeline.layer.{}.bar.in", f.pre)).center();
assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0));
}
#[test]
fn source_slip_draws_a_draft() {
let mut f = fixture("adaptive");
let from = rect(&f.h, &format!("timeline.layer.{}.sourcel", f.pre)).center();
assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0));
}
#[test]
fn key_drag_draws_a_draft() {
let mut f = fixture("adaptive");
let from = rect(&f.h, &format!("timeline.key.{}.0", f.opacity)).center();
assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0));
}
#[test]
fn in_out_and_duration_cell_drags_draw_a_draft() {
for kind in ["in", "out", "duration"] {
let mut f = fixture("adaptive");
effectcraft_ui_egui::panels::timeline::set_column(&mut f.h.state_mut().ui.timeline, kind, true).unwrap();
f.h.run_steps(3);
let from = rect(&f.h, &format!("timeline.layer.{}.{kind}", f.pre)).center();
assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0));
}
}
#[test]
fn properties_scrub_draws_a_draft() {
let mut f = fixture("adaptive");
let ctx = f.h.ctx.clone();
effectcraft_ui_egui::menus::invoke(f.h.state_mut(), &ctx, "window.panel", json!({"panel": "properties"})).unwrap();
f.h.run_steps(3);
let from = rect(&f.h, &format!("properties.prop.{}.value", f.opacity)).center();
assert_draft_while_dragging(&mut f, from, vec2(40.0, 0.0));
}
#[test]
fn work_area_drag_keeps_full_resolution() {
let mut f = fixture("adaptive");
let from = rect(&f.h, "timeline.workArea.end").center();
let before = f.h.state().session.active_comp().unwrap().work_area;
hold(&mut f.h, from, from + vec2(-80.0, 0.0));
assert_ne!(f.h.state().session.active_comp().unwrap().work_area, before, "the work area moved");
assert!(!draft(&f.h), "a work-area drag changes no pixels");
release(&mut f.h, from);
}
/// Scrubs the precomp's Audio Levels in the Timeline; `reads_audio` adds an expression that
/// reads audio. Returns whether the viewer drew a draft while held.
fn audio_levels_scrub(reads_audio: bool) -> bool {
let mut f = fixture("adaptive");
if reads_audio {
let rotation = json!({"layer": f.solid, "path": "transform/rotation", "expression": "thisComp.layer(1).audioLevels; value"});
f.h.state_mut().session.execute("prop.setExpression", rotation).unwrap();
}
let uid = f.h.state().session.active_comp().unwrap().layers.iter().find(|l| l.id.0 == f.pre).unwrap().props.prop("audio/levels").unwrap().uid;
let tl = &mut f.h.state_mut().ui.timeline;
tl.open_layers.insert(f.pre);
tl.layer_reveal.insert(f.pre, vec!["props".into()]);
tl.reveal_props.insert(uid);
f.h.run_steps(3);
let from = rect(&f.h, &format!("timeline.prop.{uid}.value.0")).center();
let before = on_screen(&f.h);
hold(&mut f.h, from, from + vec2(40.0, 0.0));
assert_ne!(on_screen(&f.h), before, "the scrub changed the levels");
let draft = draft(&f.h);
release(&mut f.h, from);
draft
}
/// A layer's audio is pixels only when something draws it (an expression reading audio here).
#[test]
fn audio_levels_scrub_draws_a_draft_only_when_audio_is_drawn() {
assert!(audio_levels_scrub(true), "an expression reads the audio");
assert!(!audio_levels_scrub(false), "nothing draws the audio");
}
/// Markers are pixels only to the expressions that read them (#1083): moving one keeps Full.
#[test]
fn marker_drag_keeps_full_resolution() {
let mut f = fixture("adaptive");
f.h.state_mut().session.execute("comp.addMarker", json!({"time": 2.0})).unwrap();
f.h.run_steps(3);
let from = rect(&f.h, "timeline.marker.0").center();
let before = f.h.state().session.active_comp().unwrap().markers[0].time;
hold(&mut f.h, from, from + vec2(80.0, 0.0));
assert_ne!(f.h.state().session.active_comp().unwrap().markers[0].time, before, "the marker moved");
assert!(!draft(&f.h), "a marker no expression reads changes no pixels");
release(&mut f.h, from);
}
#[test]
fn timeline_click_keeps_full_resolution() {
let mut f = fixture("adaptive");
let point = rect(&f.h, "timeline.ruler").center();
let before = f.h.state().session.time();
f.h.input_mut().events.push(Event::PointerMoved(point));
f.h.step();
f.h.input_mut().events.push(Event::PointerButton { pos: point, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() });
f.h.run_steps(3);
assert_ne!(f.h.state().session.time(), before, "the click moved the playhead");
assert!(!draft(&f.h));
release(&mut f.h, point);
}
/// The drag is still noted, but Fast Previews Off never lowers the resolution.
#[test]
fn fast_previews_off_keeps_full_resolution() {
let mut f = fixture("off");
let ruler = rect(&f.h, "timeline.ruler");
let from = pos2(ruler.min.x + 40.0, ruler.max.y - 3.0);
hold(&mut f.h, from, from + vec2(200.0, 0.0));
assert!(f.h.state().ui.viewer.property_interacting);
assert!(!draft(&f.h));
release(&mut f.h, from);
}