From ba04ab70a0274c022c5139dd08b554f83f387235 Mon Sep 17 00:00:00 2001 From: ded Date: Tue, 20 Oct 2026 17:47:39 +0300 Subject: [PATCH 001/350] 3D text and shapes rasterise only the part the output shows (#1388) A 3D text or shape layer close to the camera was rasterised whole at its on-screen scale, up to ~16 million pixels (a quarter gigabyte as f32) per frame worker, though the frame showed a small part of it; behind the camera it was rasterised and not drawn. With 16 preview workers one such layer took the app to 8-14 GB. Renderer::raster_clip projects the output frame back onto the layer's plane (as the Classic 3D compositor draws it) and rasterises only those cells of an 8 x 8 grid over the content, in a window of the whole raster's pixel grid; nothing when the content is behind the camera. Cells keep a moving camera on one cached raster. Layers whose off-frame pixels can matter (effects, styles, masks, motion blur, depth of field, lights, Advanced 3D, per-character 3D, collapsed 2D transforms) are rasterised whole as before. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/lib.rs | 89 +++++++- crates/render/src/shapes.rs | 10 +- crates/render/src/tests_raster_clip.rs | 281 +++++++++++++++++++++++++ crates/render/src/text.rs | 42 +++- crates/render/src/three_d/compose.rs | 46 ++++ 5 files changed, 454 insertions(+), 14 deletions(-) create mode 100644 crates/render/src/tests_raster_clip.rs diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index eccaed2f4..47e301666 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -620,6 +620,9 @@ pub struct Renderer<'a> { through: [Option>; MAX_THROUGH], /// More collapsed levels than [`MAX_THROUGH`]: their motion is not blurred. through_lost: bool, + /// Rasterise 3D text and shapes whole, never only the part the output shows + /// ([`Renderer::raster_clip`]; tests compare against it). + pub(crate) whole_raster: bool, } /// One collapsed precomp layer a nested comp is drawn through. @@ -706,6 +709,7 @@ impl<'a> Renderer<'a> { top: true, through: [None; MAX_THROUGH], through_lost: false, + whole_raster: false, } } @@ -887,8 +891,9 @@ impl<'a> Renderer<'a> { } let (scale, draft, blur) = (self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)); let prev = ctx.time - ctx.comp.frame_rate.frame_duration(); - let now = cache::layer_key(ctx, layer, scale, draft, blur); - now.is_some() && now == cache::layer_key(&ctx.at(prev), layer, scale, draft, blur) + let before = ctx.at(prev); + let now = self.clipped_key(ctx, layer, cache::layer_key(ctx, layer, scale, draft, blur)); + now.is_some() && now == self.clipped_key(&before, layer, cache::layer_key(&before, layer, scale, draft, blur)) } /// Rasterisation scale of a layer's source: the output scale, or — for text and shape layers, @@ -921,14 +926,73 @@ impl<'a> Renderer<'a> { // a whole number maps the unit vectors to lengths a hair over 1, which must stay 1 (#390: // rounded up to 2^¼, unscaled text was drawn larger and resampled, softer). let mut k = 2f64.powf((k.log2() * 4.0 - 1e-6).ceil() / 4.0).clamp(1.0 / 16.0, 64.0); - // Keep the buffer within about 16 million pixels. + // Keep the buffer within about 16 million pixels (of the part the output shows, when + // only that is rasterised: see `raster_clip`). if let Some(b) = content_bounds(ctx, layer) { + let b = match self.raster_clip(ctx, layer) { + Some(c) => { + let r = kurbo::Rect::new(b[0], b[1], b[2], b[3]).intersect(c); + [r.x0, r.y0, r.x1, r.y1] + } + None => b, + }; let area = ((b[2] - b[0]) * (b[3] - b[1])).max(1.0) * s * s; k = k.min((16.0e6 / area).sqrt().max(1.0)); } s * k } + /// The layer-space part of a 3D text or shape layer the output shows, when only that part + /// needs rasterising (#1388: text close to the camera was rasterised whole at up to 16 + /// million pixels, a quarter gigabyte per frame worker, though the frame showed a fraction + /// of it; behind the camera it was rasterised and not drawn). `None` rasterises it whole: + /// whenever pixels outside the frame can matter — effects, layer styles, masks, motion blur, + /// depth of field, lights (shadows and transmission), Advanced 3D, per-character 3D, a + /// collapsed 2D transform — or the visible part can't be bounded (see + /// [`three_d::compose::visible_clip`]). + pub(crate) fn raster_clip(&self, ctx: &EvalCtx, layer: &Layer) -> Option { + if self.whole_raster || !layer.is_3d() || !matches!(layer.source, LayerSource::Text | LayerSource::Shape) || self.outer.is_some() { + return None; + } + if (layer.switches.effects && layer.effects().is_some_and(|fx| !fx.children.is_empty())) + || layer.masks().is_some_and(|m| !m.children.is_empty()) + || styles::active(ctx, layer) + || ctx.comp.layers.iter().any(|l| matches!(l.source, LayerSource::Light { .. })) + || self.is_adv3d(ctx) + || (matches!(layer.source, LayerSource::Text) && text::per_char_3d(ctx, layer)) + { + return None; + } + let v = three_d::compose::visible_clip(self, ctx, layer)?; + if v.area() <= 0.0 { + // Behind the camera. + return Some(v); + } + let b = content_bounds(ctx, layer).map(|b| kurbo::Rect::new(b[0], b[1], b[2], b[3]))?; + if v.x0 <= b.x0 && v.y0 <= b.y0 && v.x1 >= b.x1 && v.y1 >= b.y1 { + return None; + } + // Whole cells of a layer-space grid whose cell is about a quarter of the part that shows, + // rounded down to a power of two: a moving camera keeps one raster (and its cache entry) + // while that part stays within the same cells, and a dolly changes the grid only at each + // doubling. The cells cover at most about 2.25 times the part that shows. Worth it only + // when it leaves out at least half of the content. + let c = 2f64.powf(((v.width().max(v.height()) / 4.0).max(1e-6)).log2().floor()); + let snap = |v: f64, up: bool| if up { (v / c).ceil() * c } else { (v / c).floor() * c }; + // (Not cut to the content bounds: a stroke or a glyph's padding reaches past them.) + let cells = kurbo::Rect::new(snap(v.x0, false), snap(v.y0, false), snap(v.x1, true), snap(v.y1, true)); + (cells.is_finite() && cells.intersect(b).area() <= b.area() * 0.5).then_some(cells) + } + + /// A layer content cache key that also names the part rasterised ([`Self::raster_clip`]). + fn clipped_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option) -> Option { + let k = key?; + Some(match self.raster_clip(ctx, layer) { + Some(c) => [c.x0, c.y0, c.x1, c.y1].iter().fold(cache::derive(k, 0xc11b), |d, v| cache::derive(d, (v * 16.0).round() as i64 as u64)), + None => k, + }) + } + /// Draw a Wireframe-quality layer: its bounds as a one-pixel outline. fn draw_wireframe(&self, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) { for (x, y) in self.wireframe_pixels(ctx, layer, (canvas.width, canvas.height)) { @@ -1341,8 +1405,11 @@ impl<'a> Renderer<'a> { let img = self.footage_frame(ctx, layer, *item, pf, lt, size)?; Some(self.footage_buf(&img, pf, f.width, f.height)) } - LayerSource::Text => Some(self.authored(text::render(ctx, layer, self.raster_scale(ctx, layer)))), - LayerSource::Shape => layer.props.sub("contents").map(|c| self.authored(shapes::render(ctx, layer, c, self.raster_scale(ctx, layer)))), + LayerSource::Text => Some(self.authored(text::render_in(ctx, layer, self.raster_scale(ctx, layer), self.raster_clip(ctx, layer)))), + LayerSource::Shape => layer + .props + .sub("contents") + .map(|c| self.authored(shapes::render_in(ctx, layer, c, self.raster_scale(ctx, layer), self.raster_clip(ctx, layer)))), _ => None, } } @@ -1516,7 +1583,7 @@ impl<'a> Renderer<'a> { fn content_buf_timed(&self, ctx: &EvalCtx, layer: &Layer, mut timing: Option<&mut LayerTiming>) -> Option<(Arc, Option)> { let key = self.cache.and_then(|_| cache::layer_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer))); - let key = self.content_key(ctx, layer, key); + let key = self.content_key(ctx, layer, self.clipped_key(ctx, layer, key)); if let (Some(c), Some(k)) = (self.cache, key) && let Some(b) = c.get(k) { @@ -1539,7 +1606,7 @@ impl<'a> Renderer<'a> { /// so scrubbing reuses frames rendered for earlier output frames. pub fn layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option> { let key = self.cache.and_then(|_| cache::input_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer), effects)); - let key = self.content_key(ctx, layer, key); + let key = self.content_key(ctx, layer, self.clipped_key(ctx, layer, key)); if let (Some(c), Some(k)) = (self.cache, key) && let Some(b) = c.get(k) { @@ -2206,7 +2273,11 @@ impl<'a> Renderer<'a> { /// its effects show. `None` = the layer changes nothing. pub fn adjustment_footprint(&self, ctx: &EvalCtx, layer: &Layer) -> Option> { // Cached, so an accelerator sees the same buffer (and uploads it once) every frame. - let key = self.cache.and_then(|_| cache::footprint_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer))); + let key = self.clipped_key( + ctx, + layer, + self.cache.and_then(|_| cache::footprint_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer))), + ); if let (Some(c), Some(k)) = (self.cache, key) && let Some(b) = c.get(k) { @@ -2424,4 +2495,6 @@ mod tests_nested; #[cfg(test)] mod tests_paint; #[cfg(test)] +mod tests_raster_clip; +#[cfg(test)] mod tests_time; diff --git a/crates/render/src/shapes.rs b/crates/render/src/shapes.rs index f8de5a290..2808f283b 100644 --- a/crates/render/src/shapes.rs +++ b/crates/render/src/shapes.rs @@ -592,6 +592,12 @@ const BATCH_PIXELS: usize = 8 << 20; /// Each draw rasterises and composites only its own pixel rectangle; coverage masks of /// consecutive draws are rasterised in parallel, then composited in stacking order. pub fn render(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64) -> Buf { + render_in(ctx, layer, contents, s, None) +} + +/// [`render`], only the part of the layer inside `clip` (layer space; the part the output shows, +/// see `Renderer::raster_clip`). +pub fn render_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64, clip: Option) -> Buf { let mut arena = Vec::new(); let (draws, _) = collect(ctx, layer, contents, &mut arena); // Bottom-most draw first. @@ -608,9 +614,7 @@ pub fn render(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64) -> Buf let Some(b) = resolved.iter().map(|r| r.2).reduce(|a, b| a.union(b)) else { return Buf { img: Image::new(1, 1), offset: [0.0, 0.0], scale: s } }; // Keep buffers sane for runaway geometry. let b = b.intersect(kurbo::Rect::new(-20000.0, -20000.0, 20000.0, 20000.0)); - let w = ((b.width() * s).ceil() as u32 + 4).clamp(1, 16384); - let h = ((b.height() * s).ceil() as u32 + 4).clamp(1, 16384); - let offset = [-b.x0 * s + 2.0, -b.y0 * s + 2.0]; + let Some((w, h, offset)) = crate::text::raster_window(b, s, clip) else { return Buf { img: Image::new(1, 1), offset: [0.0, 0.0], scale: s } }; let m = Mat3::translate(vec2(offset[0], offset[1])) * Mat3::scale(vec2(s, s)); let px_rect = |r: kurbo::Rect| -> Option { let x0 = (r.x0 * s + offset[0]).floor().max(0.0); diff --git a/crates/render/src/tests_raster_clip.rs b/crates/render/src/tests_raster_clip.rs new file mode 100644 index 000000000..74da0e616 --- /dev/null +++ b/crates/render/src/tests_raster_clip.rs @@ -0,0 +1,281 @@ +//! #1388: 3D text and shape layers rasterise only the part of the layer the output shows. Where +//! the whole raster fits its pixel budget the frame is identical to rasterising the whole layer; +//! close to the camera the raster stays small instead of growing to ~16 million pixels. + +use effectcraft_color::Label; +use effectcraft_keyframe::{TextDoc, Value}; +use effectcraft_project::build::{self, Ids}; +use effectcraft_project::{Comp, ItemId, ItemKind, Layer, LayerSource, Project}; +use effectcraft_time::{FrameRate, Tick}; + +use crate::{NoFootage, RenderOpts, Renderer}; + +const W: u32 = 400; +const H: u32 = 300; + +/// The default camera's distance to the z = 0 plane. +fn zoom() -> f64 { + W as f64 * 50.0 / 36.0 +} + +fn setup() -> (Project, ItemId, Comp) { + let mut p = Project::default(); + p.settings.bit_depth = effectcraft_project::BitDepth::Bpc32; + let comp = Comp::new(W, H, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into())); + (p, cid, comp) +} + +fn set(l: &mut Layer, path: &str, v: Value) { + l.props.prop_mut(path).unwrap_or_else(|| panic!("no {path}")).value = v; +} + +/// A 3D text layer at `pos`, turned `rot_y` degrees about Y. +fn text3(p: &mut Project, comp: &Comp, pos: [f64; 3], rot_y: f64) -> Layer { + let mut l = build::layer(p, comp, "Text", LayerSource::Text, (W, H), None); + l.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "NEAR CAMERA TEXT".into(), size: 60.0, ..Default::default() })); + l.switches.three_d = true; + set(&mut l, "transform/position", Value::Vec3(pos)); + set(&mut l, "transform/rotationY", Value::Scalar(rot_y)); + l +} + +/// A 3D shape layer (a 300×120 rectangle with a stroke) at `pos`. +fn shape3(p: &mut Project, comp: &Comp, pos: [f64; 3]) -> Layer { + let mut l = build::layer(p, comp, "Shape", LayerSource::Shape, (W, H), None); + let mut next = p.next_id; + { + let mut ids = Ids(&mut next); + let r = build::shape_rect(&mut ids, [300.0, 120.0], [0.0, 0.0], 0.0); + let f = build::shape_fill(&mut ids, [0.9, 0.4, 0.1, 1.0]); + let s = build::shape_stroke(&mut ids, [1.0, 1.0, 1.0, 1.0], 6.0); + let g = build::shape_group(&mut ids, "Box", vec![r, f, s]); + l.props.sub_mut("contents").unwrap().children.push(g.into()); + } + p.next_id = next; + l.switches.three_d = true; + set(&mut l, "transform/position", Value::Vec3(pos)); + l +} + +fn scene(l: impl FnOnce(&mut Project, &Comp) -> Layer) -> (Project, ItemId) { + let (mut p, cid, comp) = setup(); + let layer = l(&mut p, &comp); + p.comp_mut(cid).unwrap().layers.push(layer); + (p, cid) +} + +fn frame(p: &Project, cid: ItemId, whole: bool) -> effectcraft_raster::Image { + let mut r = Renderer::new(p, &NoFootage, RenderOpts::default()); + r.whole_raster = whole; + r.comp_frame(cid, Tick::ZERO) +} + +/// Pixels of the layer's processed buffer. +fn raster_px(p: &Project, cid: ItemId, whole: bool) -> u64 { + let mut r = Renderer::new(p, &NoFootage, RenderOpts::default()); + r.whole_raster = whole; + let c = p.comp(cid).unwrap(); + let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap(); + let b = r.layer_buf(&ctx, &c.layers[0]).unwrap(); + b.img.width as u64 * b.img.height as u64 +} + +#[test] +fn the_visible_part_renders_the_same_frame_as_the_whole_layer() { + let cx = W as f64 / 2.0; + let cy = H as f64 / 2.0; + let cases: Vec<(&str, (Project, ItemId))> = vec![ + ("far", scene(|p, c| text3(p, c, [cx, cy, 300.0], 0.0))), + ("in the frame plane", scene(|p, c| text3(p, c, [cx, cy, 0.0], 0.0))), + ("past the frame edge", scene(|p, c| text3(p, c, [60.0, cy, -200.0], 0.0))), + ("turned", scene(|p, c| text3(p, c, [cx, cy, -150.0], 55.0))), + ("close to the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() + 40.0], 0.0))), + ("partly behind the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() + 30.0], 80.0))), + ("behind the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() - 200.0], 0.0))), + ("shape", scene(|p, c| shape3(p, c, [cx + 150.0, cy, -250.0]))), + ]; + for (label, (p, cid)) in &cases { + let clipped = frame(p, *cid, false); + let whole = frame(p, *cid, true); + assert_eq!(clipped.data, whole.data, "{label}"); + } +} + +/// Close to the camera only part of the text shows: its raster is a fraction of the whole one +/// (millions of pixels), and the frame still shows text. +#[test] +fn text_close_to_the_camera_rasterises_what_shows() { + let (p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0)); + let clipped = raster_px(&p, cid, false); + let whole = raster_px(&p, cid, true); + assert!(whole > 5_000_000, "the whole raster is huge ({whole} px)"); + eprintln!("near camera: {clipped} px rasterised, {whole} px whole"); + assert!(clipped * 10 < whole, "{clipped} px of {whole}"); + let img = frame(&p, cid, false); + let covered = img.data.iter().filter(|px| px[3] > 0.5).count(); + assert!(covered > (W * H / 20) as usize, "text shows ({covered} px)"); + // Behind the camera there is nothing to rasterise. + let (p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() - 200.0], 0.0)); + assert!(raster_px(&p, cid, false) <= 1); +} + +/// The #1388 case at full size: 200 pt text in a 1920 × 1080 comp, close to the camera. Rasterised +/// whole it fills the ~16 million pixel cap; clipped, it stays within a few frames' worth of pixels +/// (and the text still shows). +#[test] +fn hd_text_close_to_the_camera_stays_within_a_few_frames() { + let mut p = Project::default(); + let comp = Comp::new(1920, 1080, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into())); + let mut l = build::layer(&mut p, &comp, "Text", LayerSource::Text, (1920, 1080), None); + l.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "NEAR CAMERA".into(), size: 200.0, ..Default::default() })); + l.switches.three_d = true; + set(&mut l, "transform/position", Value::Vec3([960.0, 540.0, -1920.0 * 50.0 / 36.0 + 60.0])); + p.comp_mut(cid).unwrap().layers.push(l); + let (clipped, whole) = (raster_px(&p, cid, false), raster_px(&p, cid, true)); + eprintln!("HD near camera: {clipped} px rasterised, {whole} px whole"); + assert!(whole > 12_000_000, "{whole}"); + assert!(clipped <= 4 * 1920 * 1080, "{clipped} px"); + assert!(frame(&p, cid, false).data.iter().any(|px| px[3] > 0.5), "the text shows"); +} + +/// A slow move of the layer against the camera (a camera move does the same) keeps the part +/// rasterised within the same grid cells for several frames: they reuse the cached raster instead +/// of drawing the text again each frame. +#[test] +fn a_slow_move_reuses_the_clipped_raster() { + let cache = crate::LayerCache::default(); + let at = |x: f64| { + let (p, cid) = scene(|p, c| text3(p, c, [x, H as f64 / 2.0, -zoom() + 40.0], 0.0)); + let mut r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + r.cache = Some(&cache); + r.comp_frame(cid, Tick::ZERO); + }; + at(W as f64 / 2.0); + let misses = cache.stats().misses; + // 16 frames, a quarter of a layer pixel apart (here about three output pixels). + for i in 1..=16 { + at(W as f64 / 2.0 + i as f64 * 0.25); + } + let rasters = cache.stats().misses - misses; + assert!(rasters <= 2, "{rasters} new rasters over 16 frames"); +} + +/// One keyframed motion: (seconds, value) pairs. +type Keys<'a> = &'a [(f64, [f64; 3])]; + +fn keyed(l: &mut Layer, path: &str, keys: Keys) { + l.props.prop_mut(path).unwrap_or_else(|| panic!("no {path}")).keys = + keys.iter().map(|(t, v)| effectcraft_keyframe::Keyframe::new(Tick::from_seconds_f64(*t), Value::Vec3(*v))).collect(); +} + +/// A camera keyed through `pos` / `poi`, and the text keyed through `text_pos` (and turning about +/// Y through `text_rot`): every frame of a second, rendered with the clipped raster and a layer +/// cache shared across the frames, matches rasterising the whole layer without a cache (or differs +/// by an anti-aliasing sub-sample or two on a few edge pixels) — except where the whole raster is held at +/// its pixel cap (softer than the clipped one, which isn't): there the text shows and the clipped +/// raster stays small. +fn moving(label: &str, pos: Keys, poi: Keys, text_pos: Keys, text_rot: &[(f64, f64)]) { + let (mut p, cid, comp) = setup(); + let mut text = text3(&mut p, &comp, [W as f64 / 2.0, H as f64 / 2.0, 0.0], 0.0); + if !text_pos.is_empty() { + keyed(&mut text, "transform/position", text_pos); + } + if !text_rot.is_empty() { + text.props.prop_mut("transform/rotationY").unwrap().keys = + text_rot.iter().map(|(t, v)| effectcraft_keyframe::Keyframe::new(Tick::from_seconds_f64(*t), Value::Scalar(*v))).collect(); + } + let mut cam = build::layer(&mut p, &comp, "Camera", LayerSource::Camera, (W, H), None); + set(&mut cam, "cameraOptions/zoom", Value::Scalar(zoom())); + keyed(&mut cam, "transform/position", pos); + keyed(&mut cam, "transform/poi", poi); + p.comp_mut(cid).unwrap().layers = vec![cam, text]; + let cache = crate::LayerCache::default(); + let (mut exact, mut near, mut capped) = (0, 0, 0); + for f in 0..30 { + let t = Tick::from_seconds_f64(f as f64 / 30.0); + let mut clipped = Renderer::new(&p, &NoFootage, RenderOpts::default()); + clipped.cache = Some(&cache); + let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default()); + whole.whole_raster = true; + let ctx = whole.eval_ctx(cid, t).unwrap(); + let layer = &p.comp(cid).unwrap().layers[1]; + let a = clipped.comp_frame(cid, t); + if clipped.raster_scale(&ctx, layer) == whole.raster_scale(&ctx, layer) { + let b = whole.comp_frame(cid, t); + if a.data == b.data { + exact += 1; + } else { + // The clipped raster places the glyphs at other coordinates (smaller ones: the whole + // raster can be thousands of pixels wide), and tiny-skia transforms them in f32: its + // 4 × 4 anti-aliasing may land a sub-sample or two differently on a few edge pixels. + let (n, worst) = a.data.iter().zip(&b.data).fold((0usize, 0.0f32), |(n, w), (p, q)| { + let d = (0..4).map(|i| (p[i] - q[i]).abs()).fold(0.0f32, f32::max); + (n + usize::from(d > 0.0), w.max(d)) + }); + assert!(worst <= 2.0 / 16.0 + 1e-4 && n * 1000 <= (W * H) as usize, "{label}: frame {f}: {n} px differ, by up to {worst}"); + near += 1; + } + } else { + // Held at the cap whole: the clipped raster is sharper, and small. + let buf = clipped.layer_buf(&ctx, layer).unwrap(); + assert!(buf.img.width as u64 * buf.img.height as u64 <= 16_500_000, "{label}: frame {f}"); + let shows = |img: &effectcraft_raster::Image| img.data.iter().filter(|px| px[3] > 0.5).count(); + let (sa, sb) = (shows(&a), shows(&whole.comp_frame(cid, t))); + // The same text shows (or none, the camera past it); coverage within a few percent. + assert!(sa.abs_diff(sb) * 50 <= sb.max(1) + 50, "{label}: frame {f}: {sa} px of text, {sb} rasterised whole"); + capped += 1; + } + } + eprintln!("{label}: {exact} frames identical, {near} within anti-aliasing noise, {capped} sharper than the capped whole raster"); + assert!(exact + near >= 10, "{label}: {exact} identical, {near} close, {capped} at the cap"); +} + +#[test] +fn a_camera_moving_any_way_renders_every_frame_as_the_whole_layer() { + let (cx, cy, z) = (W as f64 / 2.0, H as f64 / 2.0, -zoom()); + let still = [(0.0, [cx, cy, 0.0])]; + // In towards the text, through it and past it; and back out. + moving("dolly in through the text", &[(0.0, [cx, cy, 2.0 * z]), (1.0, [cx, cy, 150.0])], &[(0.0, [cx, cy, 400.0]), (1.0, [cx, cy, 700.0])], &[], &[]); + moving("dolly out", &[(0.0, [cx, cy, -60.0]), (1.0, [cx, cy, 3.0 * z])], &still, &[], &[]); + // Sideways and up and down close to the text, so it leaves the frame on both sides. + moving( + "pan x", + &[(0.0, [cx - 500.0, cy, -120.0]), (1.0, [cx + 500.0, cy, -120.0])], + &[(0.0, [cx - 500.0, cy, 0.0]), (1.0, [cx + 500.0, cy, 0.0])], + &[], + &[], + ); + moving("pan y", &[(0.0, [cx, cy - 300.0, -90.0]), (1.0, [cx, cy + 300.0, -90.0])], &[(0.0, [cx, cy - 300.0, 0.0]), (1.0, [cx, cy + 300.0, 0.0])], &[], &[]); + // Around the text, close, looking at it. + let orbit: Vec<(f64, [f64; 3])> = (0..=8) + .map(|i| { + let a = i as f64 / 8.0 * std::f64::consts::TAU * 0.4 - 0.8; + (i as f64 / 8.0, [cx + 150.0 * a.sin(), cy, -150.0 * a.cos()]) + }) + .collect(); + moving("orbit", &orbit, &still, &[], &[]); + // The text moves instead: towards the camera, past it, and across. + moving( + "text towards and past the camera", + &[(0.0, [cx, cy, z])], + &still, + &[(0.0, [cx + 200.0, cy - 80.0, 400.0]), (1.0, [cx - 150.0, cy + 60.0, z - 100.0])], + &[], + ); + // Turning close to the camera. + moving("text turning near the camera", &[(0.0, [cx, cy, z])], &still, &[(0.0, [cx, cy, z + 70.0])], &[(0.0, -70.0), (1.0, 70.0)]); +} + +/// A layer whose pixels outside the frame can matter (an effect here) is rasterised whole. +#[test] +fn layers_with_effects_rasterise_whole() { + let (mut p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0)); + let spec = effectcraft_effects::find("ec.blur.gaussian").unwrap(); + let mut next = p.next_id; + let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [W as f64, H as f64]); + p.next_id = next; + p.comp_mut(cid).unwrap().layers[0].props.sub_mut("effects").unwrap().children.push(g.into()); + assert_eq!(raster_px(&p, cid, false), raster_px(&p, cid, true)); +} diff --git a/crates/render/src/text.rs b/crates/render/src/text.rs index fbb0feb24..2aa93ecfa 100644 --- a/crates/render/src/text.rs +++ b/crates/render/src/text.rs @@ -943,8 +943,46 @@ fn draw_glyphs(geom: &TextGeom, img: &mut Image, to_px: &Mat3, s: f64, only: Opt } } +/// Buffer pixels kept around a clip (see [`raster_window`]). +const CLIP_PAD: f64 = 24.0; + +/// The buffer for content bounds `b` (layer space) at scale `s`: (width, height, layer → buffer +/// offset), two pixels of padding around it. With `clip`, only the pixels of that grid inside +/// `clip`, padded by [`CLIP_PAD`]: a whole-pixel window of the whole buffer's grid. Its pixels are +/// the whole buffer's, except that a glyph cut by the window is rasterised from the window's edge: +/// tiny-skia's 4 × 4 anti-aliasing then lands a sub-sample differently on a few edge pixels, up to +/// a dozen rows in from that edge — inside the padding, out of the frame. `None` when `clip` +/// misses the content. +pub(crate) fn raster_window(b: kurbo::Rect, s: f64, clip: Option) -> Option<(u32, u32, [f64; 2])> { + let (full_w, full_h) = ((b.width() * s).ceil() + 4.0, (b.height() * s).ceil() + 4.0); + let full = [-b.x0 * s + 2.0, -b.y0 * s + 2.0]; + let (x0, y0, x1, y1) = match clip { + None => (0.0, 0.0, full_w, full_h), + // Nothing shows (behind the camera). + Some(c) if c.area() <= 0.0 => return None, + Some(c) => ( + (c.x0 * s + full[0] - CLIP_PAD).floor().max(0.0), + (c.y0 * s + full[1] - CLIP_PAD).floor().max(0.0), + (c.x1 * s + full[0] + CLIP_PAD).ceil().min(full_w), + (c.y1 * s + full[1] + CLIP_PAD).ceil().min(full_h), + ), + }; + if !(x1 > x0 && y1 > y0) { + return None; + } + let w = ((x1 - x0) as u32).clamp(1, 16384); + let h = ((y1 - y0) as u32).clamp(1, 16384); + Some((w, h, [full[0] - x0, full[1] - y0])) +} + /// Render a text layer into a layer-space buffer at scale `s`. pub fn render(ctx: &EvalCtx, layer: &Layer, s: f64) -> Buf { + render_in(ctx, layer, s, None) +} + +/// [`render`], only the part of the layer inside `clip` (layer space; the part the output shows, +/// see `Renderer::raster_clip`). +pub fn render_in(ctx: &EvalCtx, layer: &Layer, s: f64, clip: Option) -> Buf { let empty = || Buf { img: Image::new(1, 1), offset: [0.0; 2], scale: s }; let Some(geom) = text_geom(ctx, layer) else { return empty() }; let mut bounds: Option = None; @@ -957,9 +995,7 @@ pub fn render(ctx: &EvalCtx, layer: &Layer, s: f64) -> Buf { } let Some(b) = bounds else { return empty() }; let b = b.intersect(kurbo::Rect::new(-20000.0, -20000.0, 20000.0, 20000.0)); - let w = ((b.width() * s).ceil() as u32 + 4).clamp(1, 16384); - let h = ((b.height() * s).ceil() as u32 + 4).clamp(1, 16384); - let offset = [-b.x0 * s + 2.0, -b.y0 * s + 2.0]; + let Some((w, h, offset)) = raster_window(b, s, clip) else { return empty() }; let mut img = Image::new(w, h); let base = Mat3::translate(vec2(offset[0], offset[1])) * Mat3::scale(vec2(s, s)); draw_glyphs(&geom, &mut img, &base, s, None); diff --git a/crates/render/src/three_d/compose.rs b/crates/render/src/three_d/compose.rs index 6cf87e70e..1f17e3314 100644 --- a/crates/render/src/three_d/compose.rs +++ b/crates/render/src/three_d/compose.rs @@ -185,6 +185,52 @@ pub(crate) fn view_size(r: &Renderer, ctx: &EvalCtx) -> (f64, f64) { (c.width as f64, c.height as f64) } +/// The layer-space rectangle of a 3D layer that this render's output can show: the output frame +/// (padded by two pixels) projected back onto the layer's plane, the way [`geo`] draws it. `None` +/// when that can't be bounded: an orthographic camera, depth of field (its blur reads past the +/// frame), motion blur (each sub-sample sees another part), or a frame corner whose ray misses the +/// plane in front of the camera (a plane seen towards its horizon). An empty rectangle when the +/// whole content is behind the camera. +pub(crate) fn visible_clip(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> Option { + let cam = camera_for(r, ctx); + if cam.ortho || cam.dof.is_some() || mb_samples(r, ctx, layer) > 1 { + return None; + } + let comp = view_size(r, ctx); + let s = r.opts.scale; + let outer = r.collapse3d.map_or(Mat4::IDENTITY, |c| c.world); + let world = outer * ctx.world_matrix(layer); + let (off, w, h) = match r.roi_offset() { + Some((x, y, w, h)) => ((x, y), w as f64, h as f64), + None => ((0.0, 0.0), (comp.0 * s).round(), (comp.1 * s).round()), + }; + let proj = Mat4::translate(vec3(-off.0, -off.1, 0.0)) * Mat4::scale(vec3(s, s, 1.0)) * cam.projection(comp.0, comp.1); + let hinv = (proj * world).plane_to_mat3().inverse()?; + let cv = cam.view * world; + let depth = |u: f64, v: f64| cv.0[2][0] * u + cv.0[2][1] * v + cv.0[2][3]; + if let Some(b) = crate::content_bounds(ctx, layer) + && [(b[0], b[1]), (b[2], b[1]), (b[2], b[3]), (b[0], b[3])].iter().all(|&(u, v)| depth(u, v) < NEAR) + { + return Some(kurbo::Rect::ZERO); + } + const PAD: f64 = 2.0; + let mut rect: Option = None; + for (sx, sy) in [(-PAD, -PAD), (w + PAD, -PAD), (w + PAD, h + PAD), (-PAD, h + PAD)] { + let m = &hinv.0; + let qz = m[2][0] * sx + m[2][1] * sy + m[2][2]; + if qz <= 1e-300 { + return None; + } + let (u, v) = ((m[0][0] * sx + m[0][1] * sy + m[0][2]) / qz, (m[1][0] * sx + m[1][1] * sy + m[1][2]) / qz); + if !u.is_finite() || !v.is_finite() || depth(u, v) < NEAR { + return None; + } + let p = kurbo::Rect::new(u, v, u, v); + rect = Some(rect.map_or(p, |r| r.union(p))); + } + rect +} + /// Number of motion-blur sub-samples for a layer. fn mb_samples(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> usize { if r.opts.motion_blur && ctx.comp.enable_motion_blur && r.layer_motion_blur(layer) { From 3ac9a82378898f2fe05ec9fa454425f95d798b51 Mon Sep 17 00:00:00 2001 From: ded Date: Tue, 20 Oct 2026 18:32:28 +0300 Subject: [PATCH 002/350] Layer-space reads of a 3D text or shape layer get the whole layer (#1388) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review: effects that take another layer as a parameter (Set Matte, Displacement Map, time effects…) read it through Renderer::nested in layer space, and the motion tracker reads layer_source; both could get only the part of the layer the frame shows. The nested renderer and layer_source now rasterise whole. Co-Authored-By: Claude Opus 5.5 --- crates/render/src/lib.rs | 6 +++++- crates/render/src/tests_raster_clip.rs | 28 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 47e301666..54948c8dd 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -766,6 +766,9 @@ impl<'a> Renderer<'a> { top: false, through: [None; MAX_THROUGH], through_lost: false, + // Effects read other layers through it in layer space (layer parameters, Echo…): + // they need the whole layer, not the part this frame shows (see `raster_clip`). + whole_raster: true, ..*self } } @@ -1573,7 +1576,8 @@ impl<'a> Renderer<'a> { /// (what the motion tracker analyses, like After Effects' Layer panel). `None` for layers /// without pixels. pub fn layer_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option { - self.source(ctx, layer) + // The whole layer, not the part the frame shows (see `raster_clip`). + Renderer { whole_raster: true, ..*self }.source(ctx, layer) } /// Layer pixels after source → masks → effects (no layer styles). diff --git a/crates/render/src/tests_raster_clip.rs b/crates/render/src/tests_raster_clip.rs index 74da0e616..da0b40884 100644 --- a/crates/render/src/tests_raster_clip.rs +++ b/crates/render/src/tests_raster_clip.rs @@ -268,6 +268,34 @@ fn a_camera_moving_any_way_renders_every_frame_as_the_whole_layer() { moving("text turning near the camera", &[(0.0, [cx, cy, z])], &still, &[(0.0, [cx, cy, z + 70.0])], &[(0.0, -70.0), (1.0, 70.0)]); } +/// Reads of a layer in its own space get the whole layer, not the part the frame shows: an effect +/// taking another layer as a parameter (Set Matte, stretched to fit, from hidden text close to the +/// camera), and the source the motion tracker analyses. +#[test] +fn layer_space_reads_get_the_whole_layer() { + let (mut p, cid, comp) = setup(); + let mut text = text3(&mut p, &comp, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0); + text.switches.video = false; + let text_id = text.id; + let sid = + p.add_item("Solid", Label::Red, None, ItemKind::Solid(effectcraft_project::Solid { color: [0.2, 0.6, 1.0], width: W, height: H, pixel_aspect: 1.0 })); + let mut solid = build::layer(&mut p, &comp, "Solid", LayerSource::Solid { item: sid }, (W, H), None); + let spec = effectcraft_effects::find("ec.channel.setmatte").unwrap(); + let mut next = p.next_id; + let mut g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [W as f64, H as f64]); + p.next_id = next; + g.prop_mut("takeMatteFromLayer").unwrap().value = Value::Layer(Some(text_id.0)); + solid.props.sub_mut("effects").unwrap().children.push(g.into()); + p.comp_mut(cid).unwrap().layers = vec![text, solid]; + assert_eq!(frame(&p, cid, false).data, frame(&p, cid, true).data, "Set Matte from the text"); + let r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap(); + let layer = &p.comp(cid).unwrap().layers[0]; + let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default()); + whole.whole_raster = true; + assert_eq!(r.layer_source(&ctx, layer).unwrap().img, whole.layer_source(&ctx, layer).unwrap().img, "the tracker's source"); +} + /// A layer whose pixels outside the frame can matter (an effect here) is rasterised whole. #[test] fn layers_with_effects_rasterise_whole() { From d383a1e8bb67aed08b3e4da1c0df0f6e994affe5 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Thu, 22 Oct 2026 21:03:41 +0200 Subject: [PATCH 003/350] Make the audio preview follow mute changes made during playback (#2104) --- crates/ui-egui/src/audio.rs | 160 +++++++++++++++++++++++++++++++----- crates/ui-egui/src/lib.rs | 4 + 2 files changed, 142 insertions(+), 22 deletions(-) diff --git a/crates/ui-egui/src/audio.rs b/crates/ui-egui/src/audio.rs index 7972e6942..1ddff0c5b 100644 --- a/crates/ui-egui/src/audio.rs +++ b/crates/ui-egui/src/audio.rs @@ -23,7 +23,7 @@ use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; -use effectcraft_engine::project::ItemId; +use effectcraft_engine::project::{ItemId, Project}; use effectcraft_engine::time::{FrameRate, Tick}; use crate::frames::RenderSource; @@ -179,12 +179,24 @@ pub struct AudioPlayback { device: Box, stop: Arc, thread: Option>, - /// wasm32 (no threads): what the feeder thread would own, run by [`AudioPlayback::pump`]. - feeder: Option<(RenderSource, ItemId, i64, u32)>, + /// What the mixer reads and advances: the feeder thread, or [`AudioPlayback::pump`] on wasm32 + /// (no threads). + state: Arc>, + comp: ItemId, + mix: u32, pub rate: u32, pub span: PlaySpan, } +/// The mixer's position and its view of the project. +struct Feeder { + src: RenderSource, + /// The next comp sample index to mix. + cursor: i64, + /// Bumped when the project changes: a block mixed from an older project is dropped. + generation: u64, +} + /// Audio kept queued ahead of the device: ~200 ms (a feeder thread tops it up continuously); /// wasm32 tops it up once per UI frame, so it keeps ~350 ms to ride out slow frames. fn queue_target(rate: u32) -> usize { @@ -211,9 +223,10 @@ impl AudioPlayback { let span = PlaySpan::new(start, wa, wb, looping, rate); let feed = Arc::new(AudioFeed::default()); let stop = Arc::new(AtomicBool::new(false)); + let state = Arc::new(Mutex::new(Feeder { src, cursor: span.start, generation: 0 })); if cfg!(target_arch = "wasm32") { // The device takes samples only in `pump`, which fills the queue first. - let mut p = AudioPlayback { feed: feed.clone(), device, stop, thread: None, feeder: Some((src, comp, span.start, mix)), rate, span }; + let mut p = AudioPlayback { feed: feed.clone(), device, stop, thread: None, state, comp, mix, rate, span }; p.device.start(feed)?; p.pump(); return Ok(p); @@ -223,15 +236,14 @@ impl AudioPlayback { // as that took (#408). The device plays silence until the first block arrives, and the // clock doesn't count it. let thread = { - let (feed, stop) = (feed.clone(), stop.clone()); + let (feed, stop, state) = (feed.clone(), stop.clone(), state.clone()); std::thread::Builder::new() .name("ec-audio-feed".into()) .spawn(move || { - let mut cursor = span.start; let target = queue_target(rate); while !stop.load(Ordering::Relaxed) { if feed.queued_frames() < target && !feed.finished.load(Ordering::Relaxed) { - cursor = feed_block(&src, comp, &feed, &span, cursor, rate, mix); + feed_block(&state, comp, &feed, &span, rate, mix); } else { std::thread::sleep(std::time::Duration::from_millis(4)); } @@ -240,21 +252,39 @@ impl AudioPlayback { .map_err(|e| e.to_string())? }; device.start(feed.clone())?; - Ok(AudioPlayback { feed, device, stop, thread: Some(thread), feeder: None, rate, span }) + Ok(AudioPlayback { feed, device, stop, thread: Some(thread), state, comp, mix, rate, span }) } /// Every UI frame while playing: on wasm32 mix ahead into the feed (what the feeder thread /// does elsewhere), then let the device take what it needs. pub fn pump(&mut self) { - if let Some((src, comp, cursor, mix)) = &mut self.feeder { + if cfg!(target_arch = "wasm32") { let target = queue_target(self.rate); while self.feed.queued_frames() < target && !self.feed.finished.load(Ordering::Relaxed) { - *cursor = feed_block(src, *comp, &self.feed, &self.span, *cursor, self.rate, *mix); + feed_block(&self.state, self.comp, &self.feed, &self.span, self.rate, self.mix); } } self.device.pump(); } + /// The project the sound is mixed from. + pub fn project(&self) -> Option> { + self.state.lock().ok().map(|s| s.src.project.clone()) + } + + /// Mix from `project` from now on (the project was edited while playing, e.g. a layer's Audio + /// switch, #2104): what is queued came from the old project, so it is dropped and mixing + /// resumes at the first sample the device has not taken yet (also when the feeder had + /// already mixed up to the end of a non-looping span). + pub fn set_project(&mut self, project: Arc) { + let Ok(mut s) = self.state.lock() else { return }; + s.src.project = project; + s.generation += 1; + self.feed.clear(); + self.feed.finished.store(false, Ordering::Relaxed); + s.cursor = clock_sample(&self.span, self.feed.consumed(), 0); + } + /// The comp sample index audible now. pub fn clock(&self) -> i64 { clock_sample(&self.span, self.feed.consumed(), self.device.latency_frames()) @@ -279,20 +309,33 @@ impl Drop for AudioPlayback { } } -/// Mix one block at `cursor` into the feed; returns the next cursor (wrapping or finishing at -/// the work-area end). -fn feed_block(src: &RenderSource, comp: ItemId, feed: &AudioFeed, span: &PlaySpan, cursor: i64, rate: u32, mix: u32) -> i64 { - let mut cursor = cursor; - if cursor >= span.wb { - if !span.looping { - feed.finished.store(true, Ordering::Relaxed); - return cursor; - } +/// Mix one block at the feeder's cursor into the feed and move the cursor on (wrapping or +/// finishing at the work-area end). The block is mixed without holding the lock, so a project +/// change never waits for a slow mix; a block mixed from the old project is dropped. +fn feed_block(state: &Mutex, comp: ItemId, feed: &AudioFeed, span: &PlaySpan, rate: u32, mix: u32) { + let Ok((src, mut cursor, generation)) = state.lock().map(|s| (s.src.clone(), s.cursor, s.generation)) else { + // Poisoned: nothing more can be mixed. + feed.finished.store(true, Ordering::Relaxed); + return; + }; + let mut block = None; + if cursor >= span.wb && span.looping { cursor = span.wa; } - let n = ((span.wb - cursor) as usize).min(BLOCK); - feed.push(&mix_block(src, comp, sample_time(cursor, rate), n, rate, mix)); - cursor + n as i64 + if cursor < span.wb { + let n = ((span.wb - cursor) as usize).min(BLOCK); + block = Some(mix_block(&src, comp, sample_time(cursor, rate), n, rate, mix)); + cursor += n as i64; + } + let Ok(mut s) = state.lock() else { return }; + if s.generation != generation { + return; + } + match block { + Some(b) => feed.push(&b), + None => feed.finished.store(true, Ordering::Relaxed), + } + s.cursor = cursor; } /// `n` stereo frames of `comp`'s mix from comp time `t` at the device `rate`, mixed at `mix` Hz @@ -559,6 +602,79 @@ mod tests { playback.stop(); } + /// #2104: muting a layer while the preview plays silences it: the queued sound is dropped + /// and everything mixed afterwards comes from the edited project. + #[test] + fn muting_a_layer_while_playing_silences_the_preview() { + use effectcraft_engine::color::Label; + use effectcraft_engine::project::{Comp, Footage, FootageKind, ItemKind, LayerSource, build}; + use effectcraft_engine::render::FootageSource; + use effectcraft_engine::render::cache::LayerCache; + use std::time::{Duration, Instant}; + + /// Footage audio that is a constant 0.5. + struct Steady; + impl FootageSource for Steady { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, _: u32) -> Option> { + Some(vec![0.5; frames * 2]) + } + } + struct Device; + impl AudioDevice for Device { + fn sample_rate(&self) -> u32 { + 48000 + } + fn start(&mut self, _: Arc) -> Result<(), String> { + Ok(()) + } + fn stop(&mut self) {} + } + + let mut p = Project::default(); + let footage = + Footage { path: "tone.flac".into(), kind: FootageKind::Audio, duration: Tick::from_seconds_f64(60.0), has_audio: true, ..Default::default() }; + let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); + let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); + comp.layers = vec![layer]; + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let mut project = Arc::new(p); + let src = RenderSource { + project: project.clone(), + footage: Arc::new(Steady), + expr: None, + layer_cache: Arc::new(LayerCache::default()), + gpu: None, + gpu_display: false, + disk: None, + }; + let mut playback = + AudioPlayback::start(Box::new(Device), src, cid, Tick::ZERO, Tick::ZERO, Tick::from_seconds_f64(10.0), true, 0).expect("the preview starts"); + let t = Instant::now(); + while playback.feed.queued_frames() == 0 { + assert!(t.elapsed() < Duration::from_secs(30), "the feeder never delivered"); + std::thread::sleep(Duration::from_millis(10)); + } + // Mute the layer while the sound plays. + if let Some(c) = Arc::make_mut(&mut project).comp_mut(cid) { + c.layers[0].switches.audio = false; + } + playback.set_project(project.clone()); + assert!(playback.project().is_some_and(|p| Arc::ptr_eq(&p, &project))); + // The feeder tops the queue up again, now from the muted project. + while playback.feed.queued_frames() == 0 { + assert!(t.elapsed() < Duration::from_secs(30), "the feeder never delivered"); + std::thread::sleep(Duration::from_millis(10)); + } + let mut out = vec![1.0f32; playback.feed.queued_frames() * 2]; + playback.feed.pull(&mut out); + assert!(out.iter().all(|v| *v == 0.0), "the muted layer still plays"); + playback.stop(); + } + #[test] fn meter_decays_and_holds_peaks() { let mut m = Meter::default(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 72016cc53..e76788ef2 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1263,6 +1263,10 @@ impl EffectcraftApp { } } if let Some(a) = &mut self.audio { + // An edit while playing (mute, unmute, solo, levels): the sound follows it (#2104). + if a.project().is_some_and(|p| !Arc::ptr_eq(&p, &self.session.project)) { + a.set_project(self.session.project.clone()); + } a.pump(); self.meter.update(a.feed.take_peaks(), now); } else if let Some(s) = &mut self.scrub { From 2301fd6e84675996159a297172399b61eda003fe Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Fri, 23 Oct 2026 16:09:16 +0200 Subject: [PATCH 004/350] Keep the audio queue when an edit during playback does not change the sound (#2104) --- crates/ui-egui/src/audio.rs | 90 ++++++++++++++++++++++++++++++++++++- crates/ui-egui/src/lib.rs | 2 +- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/audio.rs b/crates/ui-egui/src/audio.rs index 1ddff0c5b..029c7ca6b 100644 --- a/crates/ui-egui/src/audio.rs +++ b/crates/ui-egui/src/audio.rs @@ -23,7 +23,7 @@ use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; -use effectcraft_engine::project::{ItemId, Project}; +use effectcraft_engine::project::{ItemId, ItemKind, LayerSource, Project}; use effectcraft_engine::time::{FrameRate, Tick}; use crate::frames::RenderSource; @@ -285,6 +285,18 @@ impl AudioPlayback { s.cursor = clock_sample(&self.span, self.feed.consumed(), 0); } + /// Follow an edited `project`: mixing continues from it, and the queued sound is dropped only + /// when the edit changed what is heard ([`audio_changed`]); a transform or colour edit leaves + /// the queue alone, so playback does not drop out. + pub fn follow_project(&mut self, project: Arc) { + let Some(old) = self.project() else { return }; + if audio_changed(&old, &project, self.comp) { + self.set_project(project); + } else if let Ok(mut s) = self.state.lock() { + s.src.project = project; + } + } + /// The comp sample index audible now. pub fn clock(&self) -> i64 { clock_sample(&self.span, self.feed.consumed(), self.device.latency_frames()) @@ -309,6 +321,46 @@ impl Drop for AudioPlayback { } } +/// Whether `new` sounds different from `old` for `comp`: a layer's timing, source, Audio / Solo / +/// Effects switch, Audio Levels or effects, or the same for a nested comp. Other edits (video +/// properties, names, labels) do not change the mix. +fn audio_changed(old: &Project, new: &Project, comp: ItemId) -> bool { + fn comps_differ(old: &Project, new: &Project, comp: ItemId, depth: usize) -> bool { + let (Some(a), Some(b)) = (old.comp(comp), new.comp(comp)) else { return old.comp(comp).is_some() != new.comp(comp).is_some() }; + if depth >= 16 { + return false; + } + a.layers.len() != b.layers.len() + || a.layers.iter().zip(&b.layers).any(|(x, y)| { + x.id != y.id + || x.source != y.source + || x.start_time != y.start_time + || x.in_point != y.in_point + || x.out_point != y.out_point + || x.stretch != y.stretch + || x.switches.audio != y.switches.audio + || x.switches.solo != y.switches.solo + || x.switches.effects != y.switches.effects + || x.props.sub("audio") != y.props.sub("audio") + || x.effects() != y.effects() + || match &x.source { + LayerSource::Footage { item } => footage_differs(old, new, *item), + LayerSource::Comp { item } => comps_differ(old, new, *item, depth + 1), + _ => false, + } + }) + } + /// The footage item's audio availability changed (relinked, missing, no audio). + fn footage_differs(old: &Project, new: &Project, item: ItemId) -> bool { + let f = |p: &Project| match p.item(item).map(|i| &i.kind) { + Some(ItemKind::Footage(f)) => Some((f.has_audio, f.missing, f.path.clone())), + _ => None, + }; + f(old) != f(new) + } + comps_differ(old, new, comp, 0) +} + /// Mix one block at the feeder's cursor into the feed and move the cursor on (wrapping or /// finishing at the work-area end). The block is mixed without holding the lock, so a project /// change never waits for a slow mix; a block mixed from the old project is dropped. @@ -675,6 +727,42 @@ mod tests { playback.stop(); } + /// #2104: only edits that change the mix drop the queued sound. + #[test] + fn only_audible_edits_change_the_audio() { + use effectcraft_engine::color::Label; + use effectcraft_engine::project::{Comp, Footage, FootageKind, build}; + + let mut p = Project::default(); + let footage = + Footage { path: "tone.flac".into(), kind: FootageKind::Audio, duration: Tick::from_seconds_f64(60.0), has_audio: true, ..Default::default() }; + let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); + let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); + comp.layers = vec![layer]; + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let old = p; + // A video-only edit: the layer is hidden, its name and label change. + let mut video = old.clone(); + if let Some(l) = video.comp_mut(cid).and_then(|c| c.layers.first_mut()) { + l.switches.video = false; + l.name = "renamed".into(); + } + assert!(!audio_changed(&old, &video, cid), "a video edit must not drop the sound"); + // Muting, solo and a moved in point do change it. + for edit in 0..3 { + let mut edited = old.clone(); + if let Some(l) = edited.comp_mut(cid).and_then(|c| c.layers.first_mut()) { + match edit { + 0 => l.switches.audio = false, + 1 => l.switches.solo = true, + _ => l.in_point = Tick::from_seconds_f64(1.0), + } + } + assert!(audio_changed(&old, &edited, cid), "audible edit {edit} must be followed"); + } + } + #[test] fn meter_decays_and_holds_peaks() { let mut m = Meter::default(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e76788ef2..c3a17fa30 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1265,7 +1265,7 @@ impl EffectcraftApp { if let Some(a) = &mut self.audio { // An edit while playing (mute, unmute, solo, levels): the sound follows it (#2104). if a.project().is_some_and(|p| !Arc::ptr_eq(&p, &self.session.project)) { - a.set_project(self.session.project.clone()); + a.follow_project(self.session.project.clone()); } a.pump(); self.meter.update(a.feed.take_peaks(), now); From 55040de3d139f97a516c107cdb82ace687619ea2 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Sun, 1 Nov 2026 07:42:06 +0100 Subject: [PATCH 005/350] Build the #2104 audio tests' layer lists with into(), as Comp::layers is now a Layers (#2104) --- crates/ui-egui/src/audio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/audio.rs b/crates/ui-egui/src/audio.rs index eb7828a18..f8b52fc88 100644 --- a/crates/ui-egui/src/audio.rs +++ b/crates/ui-egui/src/audio.rs @@ -691,7 +691,7 @@ mod tests { let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); - comp.layers = vec![layer]; + comp.layers = vec![layer].into(); let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); let mut project = Arc::new(p); let src = RenderSource { @@ -739,7 +739,7 @@ mod tests { let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); - comp.layers = vec![layer]; + comp.layers = vec![layer].into(); let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); let old = p; // A video-only edit: the layer is hidden, its name and label change. From faf1d99e4a0650b5c660daa908a657743e2e4fac Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Mon, 2 Nov 2026 21:55:19 +0530 Subject: [PATCH 006/350] Fix #2869: disclose selected effects with arrow keys --- crates/ui-egui/src/menus/shortcuts.rs | 4 + crates/ui-egui/src/panels/effect_controls.rs | 1 + .../src/panels/effect_controls/panel.rs | 22 ++++ crates/ui-egui/tests/it/ui_effect_controls.rs | 102 ++++++++++++++++++ 4 files changed, 129 insertions(+) diff --git a/crates/ui-egui/src/menus/shortcuts.rs b/crates/ui-egui/src/menus/shortcuts.rs index 4ef1e64fb..65542e56f 100644 --- a/crates/ui-egui/src/menus/shortcuts.rs +++ b/crates/ui-egui/src/menus/shortcuts.rs @@ -403,6 +403,10 @@ fn handle_shortcuts_impl(app: &mut EffectcraftApp, ctx: &egui::Context, tab_only if app.dialog.is_some() { continue; } + if !mods.any() && crate::panels::effect_controls::handle_disclosure_key(app, key) { + ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, key)); + continue; + } // Enter in the Project panel renames the selected item (handled by the panel). // In Effects & Presets and the Render Queue Enter applies or opens the row (handled by the // panel), not the layer rename. diff --git a/crates/ui-egui/src/panels/effect_controls.rs b/crates/ui-egui/src/panels/effect_controls.rs index d9706f147..6e71c6850 100644 --- a/crates/ui-egui/src/panels/effect_controls.rs +++ b/crates/ui-egui/src/panels/effect_controls.rs @@ -36,6 +36,7 @@ mod viewer; pub(crate) use common::gesture_key; pub use common::{choose_files_after_apply, reveal_applied}; +pub(crate) use panel::handle_disclosure_key; pub use panel::{show, start_rename}; pub(crate) use rows::toggle_pick; pub use viewer::viewer_hook; diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 122a6abf7..0d81029e6 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -3,6 +3,28 @@ use super::editors::{curves_editor, editor_height, histogram_only, levels_editor use super::rows::{FxScope, group_rows, toggle_closed}; use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, PropGroup, Rect, Sense, Stroke, Tokens, json, pos2, vec2, widgets}; +/// Plain Left/Right disclose selected effect headers before global layer nudges run. +pub(crate) fn handle_disclosure_key(app: &mut EffectcraftApp, key: egui::Key) -> bool { + if app.ui.focused != crate::dock::PanelKind::EffectControls || !matches!(key, egui::Key::ArrowLeft | egui::Key::ArrowRight) { + return false; + } + let Some(comp) = app.session.active_comp() else { return false }; + let Some(layer) = app.session.state.selected_layers.first().and_then(|id| comp.layer(*id)) else { return false }; + let Some(effects) = layer.effects() else { return false }; + let mut handled = false; + for g in effects.groups() { + if matches!(&g.kind, GroupKind::Effect { .. }) && app.session.state.selected_props.contains(&(layer.id, g.uid)) { + if key == egui::Key::ArrowLeft { + app.ui.fx_closed.insert(g.uid); + } else { + app.ui.fx_closed.remove(&g.uid); + } + handled = true; + } + } + handled +} + // --------------------------------------------------------------------------------------------- /// Open the rename field on effect `uid` (Enter with the effect selected), name selected. diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index d6138ba5c..3de502a5b 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -90,6 +90,108 @@ fn click(h: &mut Harness<'_, EffectcraftApp>, pos: egui::Pos2) { h.step(); } +fn header_arrow_harness() -> (Harness<'static, EffectcraftApp>, LayerId, [(u64, u64); 3]) { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Disclosure", "width": 32, "height": 18, "frameRate": 30, "duration": 2})).unwrap(); + let lid = s.execute("layer.newSolid", json!({"name": "Controls", "color": "#40b080"})).unwrap()["layer"].as_u64().unwrap(); + let mut effects = [(0, 0); 3]; + for pair in &mut effects { + let fx = s.execute("effect.apply", json!({"layers": [lid], "effect": "Point Control"})).unwrap()["effects"][0].as_u64().unwrap(); + let prop = layer(&s, lid).props.find_group(fx).unwrap().get("point").unwrap().uid; + *pair = (fx, prop); + } + s.state.selected_props.clear(); + let mut app = EffectcraftApp::new(s); + app.show_panel(PanelKind::EffectControls); + app.toggle_maximize(PanelKind::EffectControls); + let mut h = Harness::builder().with_size(egui::vec2(1000.0, 700.0)).with_step_dt(1.0 / 60.0).build_eframe(|_| app); + h.run_steps(4); + (h, LayerId(lid), effects) +} + +fn header_arrow_key(h: &mut Harness<'_, EffectcraftApp>, key: egui::Key, modifiers: egui::Modifiers) { + h.key_press_modifiers(modifiers, key); + h.run_steps(2); +} + +#[test] +fn effect_header_arrows_collapse_and_expand_without_editing_the_document() { + let (mut h, lid, [(fx, prop), (_, other), _]) = header_arrow_harness(); + let at = rect_of(&h, &format!("effectControls.effect.{fx}.name")).center(); + click(&mut h, at); + assert_eq!(h.state().ui.focused, PanelKind::EffectControls); + assert_eq!(h.state().session.state.selected_props, vec![(lid, fx)]); + let project = serde_json::to_value(&h.state().session.project).unwrap(); + let time = h.state().session.time(); + let undo = h.state().session.history.undo.len(); + let redo = h.state().session.history.redo.len(); + for key in [egui::Key::ArrowLeft, egui::Key::ArrowLeft, egui::Key::ArrowRight, egui::Key::ArrowRight] { + header_arrow_key(&mut h, key, egui::Modifiers::NONE); + assert_eq!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some(), key == egui::Key::ArrowRight); + assert!(h.state().auto.find(&format!("effectControls.row.{other}")).is_some(), "unselected effect stays open"); + assert_eq!(h.state().session.state.selected_props, vec![(lid, fx)]); + assert_eq!(serde_json::to_value(&h.state().session.project).unwrap(), project); + assert_eq!(h.state().session.time(), time); + assert_eq!((h.state().session.history.undo.len(), h.state().session.history.redo.len()), (undo, redo)); + } +} + +#[test] +fn effect_header_arrows_only_disclose_selected_headers_on_the_displayed_layer() { + let (mut h, lid, [(first, _), (second, _), (third, prop)]) = header_arrow_harness(); + h.state_mut().session.state.selected_props = vec![(lid, first), (lid, second), (LayerId(0), third), (lid, u64::MAX)]; + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert_eq!(h.state().ui.fx_closed.iter().copied().collect::>(), vec![first, second]); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some()); + header_arrow_key(&mut h, egui::Key::ArrowRight, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty()); + + // A parameter selection, stale header, or header on another layer must still fall through. + h.state_mut().session.state.selected_props = vec![(lid, prop), (LayerId(0), first), (lid, u64::MAX)]; + let before = serde_json::to_value(&h.state().session.project).unwrap(); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty()); + assert_ne!(serde_json::to_value(&h.state().session.project).unwrap(), before, "the existing layer nudge still runs"); +} + +#[test] +fn effect_header_arrows_preserve_modified_shortcuts_and_keyboard_ownership() { + let (mut h, lid, [(fx, _), _, _]) = header_arrow_harness(); + h.state_mut().session.state.selected_props = vec![(lid, fx)]; + let time = h.state().session.time(); + header_arrow_key(&mut h, egui::Key::ArrowRight, egui::Modifiers::COMMAND); + assert!(h.state().session.time() > time, "Cmd/Ctrl+Right still advances a frame"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::COMMAND); + assert_eq!(h.state().session.time(), time); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::SHIFT); + assert!(h.state().ui.fx_closed.is_empty(), "Shift+Left does not collapse effects"); + h.state_mut().ui.focused = PanelKind::Composition; + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "another panel owns the arrows"); + h.state_mut().ui.focused = PanelKind::EffectControls; + h.state_mut().dialog = Some(effectcraft_ui_egui::Dialog::About); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "a dialog owns the arrows"); + h.state_mut().dialog = None; + h.run_steps(3); + let at = rect_of(&h, &format!("effectControls.effect.{fx}.name")).center(); + h.event(egui::Event::PointerMoved(at)); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: egui::Modifiers::NONE }); + h.step(); + } + h.run_steps(2); + assert!(egui::Popup::is_any_open(&h.ctx), "the effect context menu is open"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "the context menu owns the arrows"); + header_arrow_key(&mut h, egui::Key::Escape, egui::Modifiers::NONE); + double_click(&mut h, at); + assert!(h.ctx.egui_wants_keyboard_input(), "the rename field owns the keyboard"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "Left moves the text caret instead of collapsing"); + assert!(h.state().auto.find(&format!("effectControls.effect.{fx}.nameEdit")).is_some()); +} + #[test] fn effect_controls_widgets_register_automation_ids() { let (app, x) = app(); From c5bf611909d5647cb1d45218310a6b860b1d1873 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Mon, 2 Nov 2026 22:10:35 +0530 Subject: [PATCH 007/350] #2817: allow reopening manually drawn paths --- crates/engine/src/cmd.rs | 2 +- crates/engine/src/cmd/mask.rs | 2 +- crates/engine/src/cmd/paths.rs | 4 + crates/engine/src/cmd/paths/closed.rs | 125 ++++++++++++++++++ crates/engine/src/menus.rs | 3 +- crates/engine/src/menus/tests.rs | 38 ++++++ crates/engine/src/tests/viewer.rs | 179 ++++++++++++++++++++++++++ crates/ui-egui/src/i18n.rs | 30 +++-- 8 files changed, 367 insertions(+), 16 deletions(-) create mode 100644 crates/engine/src/cmd/paths/closed.rs diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 1168c431b..acd991fb2 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -47,7 +47,7 @@ mod nav_more; pub mod paint; pub mod panels_cmds; pub(crate) mod path_nulls; -mod paths; +pub(crate) mod paths; mod project_items; pub(crate) mod prop; mod prop_groups; diff --git a/crates/engine/src/cmd/mask.rs b/crates/engine/src/cmd/mask.rs index 778dfb54c..d7a1c1dbe 100644 --- a/crates/engine/src/cmd/mask.rs +++ b/crates/engine/src/cmd/mask.rs @@ -644,7 +644,7 @@ pub fn specs() -> Vec { cmd!("mask.new", "New Mask from Points", [], None, "{layer?, vertices: [[x,y]…], inTangents?, outTangents?, closed?, mode?}", has_layers, new_mask), cmd!("mask.addVertex", "Add Mask Vertex", [], None, "{layer?, mask, point: [x,y], in?, out?, index?}", has_layers, add_vertex), cmd!("mask.setVertex", "Set Mask Vertex", [], None, "{layer?, mask, index, point?, in?, out?, merge?}", has_layers, set_vertex), - cmd!("mask.setClosed", "Closed", ["Layer", "Mask and Shape Path"], None, "{layer?, mask, closed?}", has_layers, set_closed), + cmd!("mask.setClosed", "Closed", [], None, "{layer?, mask, closed?}", has_layers, set_closed), cmd!("mask.selectVertices", "Select Mask Vertices", [], None, "{vertices: [{layer, mask, index}], add?, toggle?}", has_comp, select_vertices), cmd!("mask.moveVertices", "Move Mask Vertices", [], None, "{vertices?: [{layer, mask, index}], delta: [dx,dy], merge?}", has_vertices, move_vertices), cmd!("mask.deleteVertices", "Delete Mask Vertices", [], None, "{vertices?}", has_vertices, delete_vertices), diff --git a/crates/engine/src/cmd/paths.rs b/crates/engine/src/cmd/paths.rs index 8364f67ec..2f1e1051b 100644 --- a/crates/engine/src/cmd/paths.rs +++ b/crates/engine/src/cmd/paths.rs @@ -12,6 +12,9 @@ use super::layer_menu::target_masks; use super::{CommandSpec, b_p, bad, f_p, has_comp, has_layers, layer_mut, layer_p, merge_p, str_p}; use crate::{EngineError, Result, Session, VertexRef, cmd}; +mod closed; +pub(crate) use closed::selected_paths_closed; + // ---------------------------------------------------------------- RotoBezier /// Automatic (RotoBezier) tangents: each vertex's tangents follow the line through its @@ -547,6 +550,7 @@ fn ungroup_shapes(s: &mut Session, p: &Value) -> Result { pub fn specs() -> Vec { vec![ + cmd!("path.toggleClosed", "Closed", ["Layer", "Mask and Shape Path"], None, "{}", closed::has_paths, closed::toggle_closed), cmd!("path.rotoBezier", "RotoBezier", ["Layer", "Mask and Shape Path"], None, "{layer?, mask?, value?}", has_layers, roto_bezier), cmd!("path.convertToBezier", "Convert To Bezier Path", ["Layer", "Mask and Shape Path"], None, "{layer?, mask?}", has_layers, convert_to_bezier), cmd!("path.groupShapes", "Group Shapes", ["Layer"], Some("Cmd+G"), "{layer?, items?: [uid]}", has_shape_items, group_shapes), diff --git a/crates/engine/src/cmd/paths/closed.rs b/crates/engine/src/cmd/paths/closed.rs new file mode 100644 index 000000000..d3cd8c1ef --- /dev/null +++ b/crates/engine/src/cmd/paths/closed.rs @@ -0,0 +1,125 @@ +//! The selection-aware Closed menu action. Explicit edits retain `mask.setClosed`'s API. + +use effectcraft_keyframe::Value as KV; +use effectcraft_project::{GroupKind, Layer, LayerId, PropGroup, Uid}; +use effectcraft_time::Tick; +use serde_json::{Value, json}; + +use super::super::{bad, layer_mut}; +use crate::{EngineError, Result, Session, VertexRef}; + +const MAX_DEPTH: usize = 128; + +struct Target { + layer: LayerId, + route: Vec, + closed: bool, +} + +struct Selection { + vertices: Vec, + props: Vec<(LayerId, Uid)>, + time: Tick, +} + +/// A route uses checked child access and never recurses through an input-controlled tree. +fn group_at_mut<'a>(mut group: &'a mut PropGroup, route: &[usize]) -> Option<&'a mut PropGroup> { + for &index in route { + group = group.children.get_mut(index)?.as_group_mut()?; + } + Some(group) +} + +fn targets(s: &Session) -> Result> { + let comp = s.active_comp().ok_or(EngineError::NoComp)?; + // Layer navigation and insertion can retain point/property selections on other layers. + let selection = Selection { + vertices: s.state.selected_vertices.iter().filter(|v| s.state.selected_layers.contains(&v.layer)).copied().collect(), + props: s.state.selected_props.iter().filter(|(lid, _)| s.state.selected_layers.contains(lid)).copied().collect(), + time: s.time(), + }; + let mut targets = Vec::new(); + for layer in comp.layers.iter() { + let selected_layer = if !selection.vertices.is_empty() { + selection.vertices.iter().any(|v| v.layer == layer.id) + } else if !selection.props.is_empty() { + selection.props.iter().any(|(lid, _)| *lid == layer.id) + } else { + s.state.selected_layers.contains(&layer.id) + }; + if !selected_layer || layer.switches.locked { + continue; + } + collect(&selection, layer, &layer.props, &mut Vec::new(), selection.props.is_empty(), &mut targets)?; + } + if targets.is_empty() { + return Err(bad("path.toggleClosed", "select an unlocked mask or Bezier shape path")); + } + Ok(targets) +} + +fn collect(selection: &Selection, layer: &Layer, group: &PropGroup, route: &mut Vec, inherited: bool, targets: &mut Vec) -> Result<()> { + if route.len() > MAX_DEPTH { + return Err(bad("path.toggleClosed", "path group nesting exceeds 128 levels")); + } + if matches!(group.kind, GroupKind::Mask { locked: true, .. }) { + return Ok(()); + } + let selected = inherited || selection.props.contains(&(layer.id, group.uid)); + if super::super::mask::is_path_group(group) + && let Some(prop) = group.get("path") + && let KV::Path(path) = prop.value_at(layer.layer_time(selection.time)) + { + let selected = if selection.vertices.is_empty() { + selected || selection.props.contains(&(layer.id, prop.uid)) + } else { + selection.vertices.iter().any(|v| v.layer == layer.id && v.mask == group.uid && v.index < path.vertices.len()) + }; + if selected { + targets.push(Target { layer: layer.id, route: route.clone(), closed: path.closed }); + } + return Ok(()); + } + for (index, child) in group.children.iter().enumerate() { + let Some(child) = child.as_group() else { continue }; + route.push(index); + collect(selection, layer, child, route, selected, targets)?; + route.pop(); + } + Ok(()) +} + +pub(crate) fn selected_paths_closed(s: &Session) -> Option { + targets(s).ok().map(|targets| targets.iter().all(|target| target.closed)) +} + +pub(super) fn has_paths(s: &Session) -> std::result::Result<(), String> { + targets(s).map(|_| ()).map_err(|error| error.to_string()) +} + +pub(super) fn toggle_closed(s: &mut Session, _: &Value) -> Result { + let targets = targets(s)?; + let closed = !targets.iter().all(|target| target.closed); + let cid = s.active_comp_id().ok_or(EngineError::NoComp)?; + let time = s.time(); + s.edit("Closed", None, |project, _| { + for target in &targets { + let layer = layer_mut(project, cid, target.layer)?; + let time = layer.layer_time(time); + let group = group_at_mut(&mut layer.props, &target.route).ok_or_else(|| bad("path.toggleClosed", "the selected path no longer exists"))?; + let roto = super::is_roto(group); + let prop = group.get_mut("path").ok_or_else(|| bad("path.toggleClosed", "the selected group has no path"))?; + let KV::Path(mut path) = prop.value_at(time) else { return Err(bad("path.toggleClosed", "the selected value is not a path")) }; + if path.closed == closed { + continue; + } + path.closed = closed; + if roto { + super::roto_smooth(&mut path); + } + prop.set_value_at(time, KV::Path(path)); + } + Ok(()) + })?; + Ok(json!({"paths": targets.len(), "closed": closed})) +} diff --git a/crates/engine/src/menus.rs b/crates/engine/src/menus.rs index a7418ae58..813097828 100644 --- a/crates/engine/src/menus.rs +++ b/crates/engine/src/menus.rs @@ -314,6 +314,7 @@ pub fn checked(s: &Session, command: &str, params: &Value) -> Option { let layer = s.state.selected_layers.first().and_then(|l| comp.layer(*l)); let pstr = |k: &str| params.get(k).and_then(Value::as_str); match command { + "path.toggleClosed" => crate::cmd::paths::selected_paths_closed(s), "layer.setBlendMode" => { let want = effectcraft_color::BlendMode::from_name(pstr("mode")?)?; Some(layer?.blend_mode == want) @@ -820,7 +821,7 @@ Layer Hide Locked Masks | layer.mask.hideLocked Mask and Shape Path RotoBezier | path.rotoBezier - Closed | mask.setClosed + Closed | path.toggleClosed Convert To Bezier Path | path.convertToBezier Set First Vertex | path.setFirstVertex Free Transform Points | path.freeTransform diff --git a/crates/engine/src/menus/tests.rs b/crates/engine/src/menus/tests.rs index 913a0e92e..02c825bf9 100644 --- a/crates/engine/src/menus/tests.rs +++ b/crates/engine/src/menus/tests.rs @@ -140,6 +140,44 @@ fn checked_reflects_layer_state() { assert_eq!(checked(&s, "file.save", &serde_json::json!({})), None); } +#[test] +fn closed_menu_opens_and_recloses_a_manual_shape() { + use serde_json::json; + let mut s = crate::Session::default(); + s.execute("comp.new", json!({"width": 320, "height": 180})).unwrap(); + let r = s + .execute( + "shape.newPath", + json!({"vertices": [[0, 0], [100, 0], [100, 100]], "closed": true, + "inTangents": [[0, -12], [-8, 0], [0, -6]], "outTangents": [[12, 0], [0, 8], [-6, 0]]}), + ) + .unwrap(); + let lid = effectcraft_project::LayerId(r["layer"].as_u64().unwrap()); + let uid = r["path"].as_u64().unwrap(); + let path = + |s: &crate::Session| s.active_comp().unwrap().layer(lid).unwrap().props.find_group(uid).unwrap().get("path").unwrap().value.as_path().unwrap().clone(); + let original = path(&s); + for mac in [true, false] { + let (_, entry) = + platform_entries(mac).into_iter().find(|(p, e)| p.iter().map(String::as_str).eq(["Layer", "Mask and Shape Path"]) && e.label == "Closed").unwrap(); + assert!(s.is_enabled(&entry.command)); + assert_eq!(checked(&s, &entry.command, &entry.params), Some(true)); + s.execute(&entry.command, entry.params.clone()).unwrap(); + let mut opened = original.clone(); + opened.closed = false; + assert_eq!(path(&s), opened); + assert!(!effectcraft_path::to_kurbo(&path(&s)).elements().contains(&effectcraft_path::PathEl::ClosePath)); + assert_eq!(checked(&s, &entry.command, &entry.params), Some(false)); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(path(&s), original); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(path(&s), opened); + s.execute(&entry.command, entry.params).unwrap(); + assert_eq!(path(&s), original); + assert!(effectcraft_path::to_kurbo(&path(&s)).elements().contains(&effectcraft_path::PathEl::ClosePath)); + } +} + /// `cargo test -p effectcraft-engine dump_specs -- --ignored --nocapture`: every command as TSV. #[test] #[ignore] diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index ef2bd8acc..36e3b0537 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -286,6 +286,185 @@ fn shape_path(s: &Session, lid: u64, uid: u64) -> effectcraft_keyframe::ShapePat l.props.find_group(uid).unwrap().get("path").unwrap().value.as_path().unwrap().clone() } +#[test] +fn toggle_closed_respects_path_and_vertex_selection() { + let mut s = comp(); + let a = s.execute("shape.newPath", json!({"vertices": [[0, 0], [100, 0], [100, 100]], "closed": true})).unwrap(); + let lid = a["layer"].as_u64().unwrap(); + let a = a["path"].as_u64().unwrap(); + let b = s.execute("shape.newPath", json!({"layer": lid, "vertices": [[10, 10], [90, 10], [90, 90]], "closed": true})).unwrap()["path"].as_u64().unwrap(); + let leaf = layer(&s, lid).props.find_group(a).unwrap().get("path").unwrap().uid; + s.state.selected_vertices.clear(); + s.state.selected_props = vec![(LayerId(lid), leaf)]; + s.execute("path.toggleClosed", json!({})).unwrap(); + assert!(!shape_path(&s, lid, a).closed); + assert!(shape_path(&s, lid, b).closed, "an unselected sibling stays closed"); + s.state.selected_props = vec![(LayerId(lid), b)]; + s.state.selected_vertices = vec![crate::VertexRef { layer: LayerId(lid), mask: a, index: 1 }]; + s.execute("path.toggleClosed", json!({})).unwrap(); + assert!(shape_path(&s, lid, a).closed, "vertices take precedence over property selection"); + assert!(shape_path(&s, lid, b).closed); + s.state.selected_vertices.clear(); + s.state.selected_props = vec![(LayerId(lid), a), (LayerId(lid), leaf), (LayerId(lid), b)]; + let r = s.execute("path.toggleClosed", json!({})).unwrap(); + assert_eq!(r["paths"], json!(2)); + assert!(!shape_path(&s, lid, a).closed); + assert!(!shape_path(&s, lid, b).closed); + s.execute("edit.undo", json!({})).unwrap(); + assert!(shape_path(&s, lid, a).closed); + assert!(shape_path(&s, lid, b).closed, "all paths undo in one step"); + s.execute("mask.setClosed", json!({"layer": lid, "mask": a, "closed": false})).unwrap(); + s.execute("path.toggleClosed", json!({})).unwrap(); + assert!(shape_path(&s, lid, a).closed); + assert!(shape_path(&s, lid, b).closed, "a mixed selection closes all paths"); + s.state.selected_props.clear(); + s.execute("path.toggleClosed", json!({})).unwrap(); + assert!(!shape_path(&s, lid, a).closed); + assert!(!shape_path(&s, lid, b).closed, "layer selection addresses all editable paths"); +} + +#[test] +fn toggle_closed_ignores_selections_retained_on_other_layers() { + let mut s = comp(); + let a = s.execute("shape.newPath", json!({"vertices": [[0, 0], [100, 0], [100, 100]], "closed": true})).unwrap(); + let (a_layer, a_path) = (a["layer"].as_u64().unwrap(), a["path"].as_u64().unwrap()); + let b_layer = s.execute("layer.newShape", json!({"kind": "none"})).unwrap()["layer"].as_u64().unwrap(); + assert!(s.state.selected_vertices.iter().any(|v| v.layer == LayerId(a_layer)), "insertion retains old vertices"); + assert!(!s.is_enabled("path.toggleClosed")); + assert!(s.execute("path.toggleClosed", json!({})).is_err()); + assert!(shape_path(&s, a_layer, a_path).closed); + + let b_path = + s.execute("shape.newPath", json!({"layer": b_layer, "vertices": [[0, 0], [50, 0], [50, 50]], "closed": true})).unwrap()["path"].as_u64().unwrap(); + s.execute("prop.select", json!({"layer": b_layer, "prop": b_path})).unwrap(); + s.execute("layer.selectNext", json!({})).unwrap(); + assert_eq!(s.state.selected_layers, vec![LayerId(a_layer)]); + assert!(s.state.selected_vertices.iter().any(|v| v.layer == LayerId(b_layer)), "navigation retains old vertices"); + assert!(s.state.selected_props.contains(&(LayerId(b_layer), b_path))); + assert_eq!(crate::menus::checked(&s, "path.toggleClosed", &json!({})), Some(true)); + s.execute("path.toggleClosed", json!({})).unwrap(); + assert!(!shape_path(&s, a_layer, a_path).closed); + assert!(shape_path(&s, b_layer, b_path).closed, "the deselected layer stays unchanged"); + s.execute("layer.selectPrevious", json!({})).unwrap(); + assert_eq!(s.state.selected_layers, vec![LayerId(b_layer)]); + s.execute("prop.select", json!({"layer": b_layer, "path": "transform/position"})).unwrap(); + assert!(!s.is_enabled("path.toggleClosed"), "a non-path property does not select sibling paths"); + s.execute("layer.selectNext", json!({})).unwrap(); + s.execute("path.toggleClosed", json!({})).unwrap(); + assert!(shape_path(&s, a_layer, a_path).closed, "a property retained on another layer cannot block this layer"); + assert!(shape_path(&s, b_layer, b_path).closed); +} + +#[test] +fn toggle_closed_rejects_locked_stale_and_nonpath_selection() { + let mut s = comp(); + let r = s.execute("shape.newPath", json!({"vertices": [[0, 0], [100, 0], [100, 100]], "closed": true})).unwrap(); + let (lid, uid) = (r["layer"].as_u64().unwrap(), r["path"].as_u64().unwrap()); + let before = s.project.clone(); + s.state.selected_vertices.clear(); + s.state.selected_props = vec![(LayerId(lid), u64::MAX)]; + assert!(!s.is_enabled("path.toggleClosed")); + assert!(s.execute("path.toggleClosed", json!({})).is_err()); + assert!(s.project.same_content(&before)); + s.state.selected_props.clear(); + s.state.selected_vertices = vec![crate::VertexRef { layer: LayerId(lid), mask: uid, index: usize::MAX }]; + assert!(!s.is_enabled("path.toggleClosed")); + assert!(s.execute("path.toggleClosed", json!({})).is_err()); + s.state.selected_vertices.clear(); + let cid = s.active_comp_id().unwrap(); + std::sync::Arc::make_mut(&mut s.project).comp_mut(cid).unwrap().layer_mut(LayerId(lid)).unwrap().switches.locked = true; + assert!(!s.is_enabled("path.toggleClosed")); + assert!(s.execute("path.toggleClosed", json!({})).is_err()); + assert!(shape_path(&s, lid, uid).closed); + s.execute("layer.newShape", json!({"kind": "rect"})).unwrap(); + s.state.selected_props.clear(); + assert!(!s.is_enabled("path.toggleClosed")); + assert!(s.execute("path.toggleClosed", json!({})).is_err()); +} + +#[test] +fn toggle_closed_handles_masks_and_preserves_explicit_set_closed() { + let mut s = comp(); + let lid = s.execute("layer.newSolid", json!({})).unwrap()["layer"].as_u64().unwrap(); + let uid = s.execute("mask.new", json!({"layer": lid, "vertices": [[0, 0], [100, 0], [100, 100]], "closed": true})).unwrap()["mask"].as_u64().unwrap(); + let path = |s: &Session| layer(s, lid).props.find_group(uid).unwrap().get("path").unwrap().value.as_path().unwrap().clone(); + let original = path(&s); + s.state.selected_props = vec![(LayerId(lid), uid)]; + s.execute("path.toggleClosed", json!({})).unwrap(); + assert!(!path(&s).closed); + s.execute("mask.setClosed", json!({"layer": lid, "mask": uid})).unwrap(); + assert_eq!(path(&s), original, "explicit API still defaults to closed"); + s.execute("mask.setClosed", json!({"layer": lid, "mask": uid, "closed": false})).unwrap(); + assert!(!path(&s).closed); + s.execute("path.toggleClosed", json!({})).unwrap(); + assert_eq!(path(&s), original); + let cid = s.active_comp_id().unwrap(); + let g = std::sync::Arc::make_mut(&mut s.project).comp_mut(cid).unwrap().layer_mut(LayerId(lid)).unwrap().props.find_group_mut(uid).unwrap(); + if let effectcraft_project::GroupKind::Mask { locked, .. } = &mut g.kind { + *locked = true; + } + assert!(!s.is_enabled("path.toggleClosed")); + assert!(s.execute("path.toggleClosed", json!({})).is_err()); + assert_eq!(path(&s), original); +} + +#[test] +fn toggle_closed_edits_layer_time_and_roundtrips() { + use effectcraft_keyframe::Keyframe; + let mut s = comp(); + let r = s.execute("shape.newPath", json!({"vertices": [[0, 0], [100, 0], [100, 100]], "closed": true})).unwrap(); + let (lid, uid) = (r["layer"].as_u64().unwrap(), r["path"].as_u64().unwrap()); + let cid = s.active_comp_id().unwrap(); + let original = shape_path(&s, lid, uid); + let second = Tick::from_seconds_f64(1.0); + let layer = std::sync::Arc::make_mut(&mut s.project).comp_mut(cid).unwrap().layer_mut(LayerId(lid)).unwrap(); + layer.start_time = second; + layer.stretch = 200.0; + let prop = layer.props.find_group_mut(uid).unwrap().get_mut("path").unwrap(); + prop.keys = vec![Keyframe::new(Tick::ZERO, KV::Path(original.clone())), Keyframe::new(second, KV::Path(original.clone()))]; + let keys = prop.keys.clone(); + s.execute("time.set", json!({"time": 3.0})).unwrap(); + s.execute("path.toggleClosed", json!({})).unwrap(); + let prop = s.project.comp(cid).unwrap().layer(LayerId(lid)).unwrap().props.find_group(uid).unwrap().get("path").unwrap(); + assert_eq!(prop.keys.len(), 2); + assert_eq!(prop.keys[0], keys[0], "the other keyframe is untouched"); + let mut opened = original; + opened.closed = false; + assert_eq!(prop.value_at(second), KV::Path(opened.clone())); + assert!(prop.value_at(Tick::from_seconds_f64(0.5)).as_path().unwrap().closed, "the earlier segment keeps its closedness"); + let bytes = serde_json::to_vec(&*s.project).unwrap(); + let restored: effectcraft_project::Project = serde_json::from_slice(&bytes).unwrap(); + let prop = restored.comp(cid).unwrap().layer(LayerId(lid)).unwrap().props.find_group(uid).unwrap().get("path").unwrap(); + assert_eq!(prop.value_at(second), KV::Path(opened)); + s.execute("time.set", json!({"time": 2.0})).unwrap(); + assert_eq!(crate::menus::checked(&s, "path.toggleClosed", &json!({})), Some(true)); + s.execute("time.set", json!({"time": 3.0})).unwrap(); + assert_eq!(crate::menus::checked(&s, "path.toggleClosed", &json!({})), Some(false)); +} + +#[test] +fn toggle_closed_rejects_excessive_group_nesting_without_an_edit() { + use effectcraft_project::build::{self, Ids}; + let mut s = comp(); + let r = s.execute("shape.newPath", json!({"vertices": [[0, 0], [100, 0], [100, 100]], "closed": true})).unwrap(); + let lid = LayerId(r["layer"].as_u64().unwrap()); + let cid = s.active_comp_id().unwrap(); + let project = std::sync::Arc::make_mut(&mut s.project); + let mut next = project.next_id; + let contents = project.comp_mut(cid).unwrap().layer_mut(lid).unwrap().props.sub_mut("contents").unwrap(); + let node = contents.children.first_mut().unwrap(); + let mut nested = node.as_group().unwrap().clone(); + for _ in 0..130 { + nested = build::shape_group(&mut Ids(&mut next), "Nested", vec![nested]); + } + *node = effectcraft_project::Node::Group(nested); + project.next_id = next; + let before = s.project.clone(); + assert!(!s.is_enabled("path.toggleClosed")); + assert!(s.execute("path.toggleClosed", json!({})).is_err()); + assert!(s.project.same_content(&before)); +} + #[test] fn shape_pen_builds_shape_group_and_renders() { let mut s = comp(); diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index fe676163e..18ffe2725 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -699,7 +699,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "隐藏锁定的蒙版"), ("", "Mask and Shape Path", "蒙版和形状路径"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "已关闭"), + ("path.toggleClosed", "Closed", "已关闭"), ("path.convertToBezier", "Convert To Bezier Path", "转换为贝塞尔曲线路径"), ("path.setFirstVertex", "Set First Vertex", "设置第一个顶点"), ("path.freeTransform", "Free Transform Points", "自由变换点"), @@ -1333,7 +1333,7 @@ const SWEDISH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Dölj låsta masker"), ("", "Mask and Shape Path", "Mask- och formbana"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Sluten"), + ("path.toggleClosed", "Closed", "Sluten"), ("path.convertToBezier", "Convert To Bezier Path", "Konvertera till Bezierbana"), ("path.setFirstVertex", "Set First Vertex", "Ange första hörnpunkt"), ("path.freeTransform", "Free Transform Points", "Fri transformering av punkter"), @@ -1973,7 +1973,7 @@ const TRADITIONAL_CHINESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "隱藏鎖定的遮色片"), ("", "Mask and Shape Path", "遮色片和形狀路徑"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "封閉"), + ("path.toggleClosed", "Closed", "封閉"), ("path.convertToBezier", "Convert To Bezier Path", "轉換為貝茲路徑"), ("path.setFirstVertex", "Set First Vertex", "設定第一個頂點"), ("path.freeTransform", "Free Transform Points", "自由變換點"), @@ -2608,7 +2608,7 @@ const JAPANESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "ロックしたマスクを非表示"), ("", "Mask and Shape Path", "マスクとシェイプのパス"), ("path.rotoBezier", "RotoBezier", "ロトベジェ"), - ("mask.setClosed", "Closed", "閉じたパス"), + ("path.toggleClosed", "Closed", "閉じたパス"), ("path.convertToBezier", "Convert To Bezier Path", "ベジェパスに変換"), ("path.setFirstVertex", "Set First Vertex", "最初の頂点を設定"), ("path.freeTransform", "Free Transform Points", "ポイントを自由変形"), @@ -3246,7 +3246,7 @@ const UKRAINIAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Приховати заблоковані маски"), ("", "Mask and Shape Path", "Контур маски та фігури"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Замкнений"), + ("path.toggleClosed", "Closed", "Замкнений"), ("path.convertToBezier", "Convert To Bezier Path", "Перетворити на контур Безьє"), ("path.setFirstVertex", "Set First Vertex", "Задати першу вершину"), ("path.freeTransform", "Free Transform Points", "Вільне трансформування точок"), @@ -3885,7 +3885,7 @@ const SPANISH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Ocultar máscaras bloqueadas"), ("", "Mask and Shape Path", "Trazado de máscara y de forma"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Cerrado"), + ("path.toggleClosed", "Closed", "Cerrado"), ("path.convertToBezier", "Convert To Bezier Path", "Convertir en trazado Bézier"), ("path.setFirstVertex", "Set First Vertex", "Establecer primer vértice"), ("path.freeTransform", "Free Transform Points", "Transformar puntos libremente"), @@ -4522,7 +4522,7 @@ const PORTUGUESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Ocultar máscaras bloqueadas"), ("", "Mask and Shape Path", "Caminho da máscara e da forma"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Fechado"), + ("path.toggleClosed", "Closed", "Fechado"), ("path.convertToBezier", "Convert To Bezier Path", "Converter em caminho Bézier"), ("path.setFirstVertex", "Set First Vertex", "Definir primeiro vértice"), ("path.freeTransform", "Free Transform Points", "Pontos de transformação livre"), @@ -5166,7 +5166,7 @@ const RUSSIAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Скрыть заблокированные маски"), ("", "Mask and Shape Path", "Контур маски и фигуры"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Замкнутый"), + ("path.toggleClosed", "Closed", "Замкнутый"), ("path.convertToBezier", "Convert To Bezier Path", "Преобразовать в кривую Безье"), ("path.setFirstVertex", "Set First Vertex", "Задать первую вершину"), ("path.freeTransform", "Free Transform Points", "Свободное трансформирование точек"), @@ -5803,7 +5803,7 @@ const FRENCH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Masquer les masques verrouillés"), ("", "Mask and Shape Path", "Trajectoire de masque et de forme"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Fermé"), + ("path.toggleClosed", "Closed", "Fermé"), ("path.convertToBezier", "Convert To Bezier Path", "Convertir en trajectoire Bézier"), ("path.setFirstVertex", "Set First Vertex", "Définir le premier sommet"), ("path.freeTransform", "Free Transform Points", "Transformation libre des points"), @@ -6440,7 +6440,7 @@ const GERMAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Gesperrte Masken ausblenden"), ("", "Mask and Shape Path", "Masken- und Formpfad"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Geschlossen"), + ("path.toggleClosed", "Closed", "Geschlossen"), ("path.convertToBezier", "Convert To Bezier Path", "In Bézier-Pfad konvertieren"), ("path.setFirstVertex", "Set First Vertex", "Ersten Scheitelpunkt festlegen"), ("path.freeTransform", "Free Transform Points", "Punkte frei transformieren"), @@ -7076,7 +7076,7 @@ const POLISH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Ukryj zablokowane maski"), ("", "Mask and Shape Path", "Ścieżka maski i kształtu"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Zamknięta"), + ("path.toggleClosed", "Closed", "Zamknięta"), ("path.convertToBezier", "Convert To Bezier Path", "Konwertuj na ścieżkę Béziera"), ("path.setFirstVertex", "Set First Vertex", "Ustaw pierwszy wierzchołek"), ("path.freeTransform", "Free Transform Points", "Swobodne przekształcanie punktów"), @@ -7712,7 +7712,7 @@ const CZECH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Skrýt zamknuté masky"), ("", "Mask and Shape Path", "Cesta masky a tvaru"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Uzavřená"), + ("path.toggleClosed", "Closed", "Uzavřená"), ("path.convertToBezier", "Convert To Bezier Path", "Převést na Bézierovu cestu"), ("path.setFirstVertex", "Set First Vertex", "Nastavit první vrchol"), ("path.freeTransform", "Free Transform Points", "Volně transformovat body"), @@ -8345,7 +8345,7 @@ const GEORGIAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "ჩაკეტილი ნიღბების დამალვა"), ("", "Mask and Shape Path", "ნიღაბი და ფიგურის ტრაექტორია"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "შეკრული"), + ("path.toggleClosed", "Closed", "შეკრული"), ("path.convertToBezier", "Convert To Bezier Path", "ბეზიეს ტრაექტორიად გარდაქმნა"), ("path.setFirstVertex", "Set First Vertex", "პირველი წვეროს დაყენება"), ("path.freeTransform", "Free Transform Points", "წერტილების თავისუფალი გარდაქმნა"), @@ -9215,12 +9215,16 @@ mod tests { alt_shortcuts: Vec::new(), }; let new_comp = menu_entry("comp.new", "New Composition..."); + let closed_path = menu_entry("path.toggleClosed", "Closed"); let undo = menu_entry("edit.undo", "Undo"); for code in CATALOG_CODES { app.session.execute("prefs.set", json!({"key":"general.language", "value":code})).unwrap(); let drawn = entry(&app, &new_comp, new_comp.label.clone()); assert_eq!(drawn, label(&app, "comp.new", "New Composition..."), "{code}"); assert_ne!(drawn, new_comp.label, "{code}: the menu entry is drawn in English"); + let closed_label = entry(&app, &closed_path, closed_path.label.clone()); + assert_eq!(closed_label, label(&app, "path.toggleClosed", "Closed"), "{code}"); + assert_ne!(closed_label, closed_path.label, "{code}: Closed is drawn in English"); } app.session.execute("prefs.set", json!({"key":"general.language", "value":"ru"})).unwrap(); assert_eq!(entry(&app, &undo, "Can't Undo".into()), "Нечего отменять"); From 81d585e381d9aabec87ebdfb4ecb77963972c26d Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Mon, 2 Nov 2026 22:14:13 +0530 Subject: [PATCH 008/350] Fix #2869: preserve arrows when Effect Controls is hidden --- .../src/panels/effect_controls/panel.rs | 9 ++++- crates/ui-egui/tests/it/ui_effect_controls.rs | 35 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 0d81029e6..4932ddd21 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -2,10 +2,17 @@ use super::common::{Actions, PARAM_NAME_X, ROW, effect_active, selected_layer}; use super::editors::{curves_editor, editor_height, histogram_only, levels_editor, object_matte_editor, roto_editor, warp_editor}; use super::rows::{FxScope, group_rows, toggle_closed}; use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, PropGroup, Rect, Sense, Stroke, Tokens, json, pos2, vec2, widgets}; +use crate::dock::{PanelKind, WorkspaceExt as _}; /// Plain Left/Right disclose selected effect headers before global layer nudges run. pub(crate) fn handle_disclosure_key(app: &mut EffectcraftApp, key: egui::Key) -> bool { - if app.ui.focused != crate::dock::PanelKind::EffectControls || !matches!(key, egui::Key::ArrowLeft | egui::Key::ArrowRight) { + let panel = PanelKind::EffectControls; + if app.ui.focused != panel + || app.ui.start_screen + || !app.ui.dock.is_visible(panel) + || (app.ui.dock.is_docked(panel) && app.ui.maximized.is_some_and(|m| m != panel && app.ui.dock.is_docked(m))) + || !matches!(key, egui::Key::ArrowLeft | egui::Key::ArrowRight) + { return false; } let Some(comp) = app.session.active_comp() else { return false }; diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index 3de502a5b..6710b6603 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -154,6 +154,41 @@ fn effect_header_arrows_only_disclose_selected_headers_on_the_displayed_layer() assert_ne!(serde_json::to_value(&h.state().session.project).unwrap(), before, "the existing layer nudge still runs"); } +#[test] +fn effect_header_arrows_ignore_hidden_panels_with_stale_focus() { + for hidden in ["closed", "maximized", "home"] { + let (mut h, lid, [(fx, _), _, _]) = header_arrow_harness(); + h.state_mut().session.state.selected_props = vec![(lid, fx)]; + match hidden { + "closed" => h.state_mut().close_panel(PanelKind::EffectControls), + "maximized" => h.state_mut().toggle_maximize(PanelKind::Composition), + _ => h.state_mut().ui.start_screen = true, + } + h.run_steps(2); + assert_eq!(h.state().ui.focused, PanelKind::EffectControls, "{hidden}: stale focus is preserved"); + assert!(h.state().auto.find("panel.EffectControls").is_none(), "{hidden}: Effect Controls is hidden"); + let x = |h: &Harness<'_, EffectcraftApp>| layer(&h.state().session, lid.0).props.prop("transform/position").unwrap().value.as_vec3()[0]; + let before = x(&h); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "{hidden}: hidden effects keep their disclosure state"); + assert!(x(&h) < before, "{hidden}: Left still nudges the layer"); + } +} + +#[test] +fn effect_header_arrows_work_in_floating_panel_beside_maximized_panel() { + let (mut h, lid, [(fx, prop), _, _]) = header_arrow_harness(); + h.state_mut().apply_dock(craft_ui::docking::Action::Float { panel: PanelKind::EffectControls, rect: [40.0, 40.0, 600.0, 400.0] }).unwrap(); + h.state_mut().toggle_maximize(PanelKind::Composition); + h.state_mut().session.state.selected_props = vec![(lid, fx)]; + h.run_steps(3); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some()); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_none()); + header_arrow_key(&mut h, egui::Key::ArrowRight, egui::Modifiers::NONE); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some()); +} + #[test] fn effect_header_arrows_preserve_modified_shortcuts_and_keyboard_ownership() { let (mut h, lid, [(fx, _), _, _]) = header_arrow_harness(); From 2b1b68855798f23ec473408ad01fc144f313f3cd Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Mon, 2 Nov 2026 22:16:19 +0530 Subject: [PATCH 009/350] #2817: honor combined path and shape group selections --- crates/engine/src/cmd/paths/closed.rs | 20 +++++++++----------- crates/engine/src/tests/viewer.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/paths/closed.rs b/crates/engine/src/cmd/paths/closed.rs index d3cd8c1ef..268e68843 100644 --- a/crates/engine/src/cmd/paths/closed.rs +++ b/crates/engine/src/cmd/paths/closed.rs @@ -38,19 +38,18 @@ fn targets(s: &Session) -> Result> { props: s.state.selected_props.iter().filter(|(lid, _)| s.state.selected_layers.contains(lid)).copied().collect(), time: s.time(), }; + let whole_layers = selection.vertices.is_empty() && selection.props.is_empty(); let mut targets = Vec::new(); for layer in comp.layers.iter() { - let selected_layer = if !selection.vertices.is_empty() { - selection.vertices.iter().any(|v| v.layer == layer.id) - } else if !selection.props.is_empty() { - selection.props.iter().any(|(lid, _)| *lid == layer.id) - } else { + let selected_layer = if whole_layers { s.state.selected_layers.contains(&layer.id) + } else { + selection.vertices.iter().any(|v| v.layer == layer.id) || selection.props.iter().any(|(lid, _)| *lid == layer.id) }; if !selected_layer || layer.switches.locked { continue; } - collect(&selection, layer, &layer.props, &mut Vec::new(), selection.props.is_empty(), &mut targets)?; + collect(&selection, layer, &layer.props, &mut Vec::new(), whole_layers, &mut targets)?; } if targets.is_empty() { return Err(bad("path.toggleClosed", "select an unlocked mask or Bezier shape path")); @@ -70,11 +69,10 @@ fn collect(selection: &Selection, layer: &Layer, group: &PropGroup, route: &mut && let Some(prop) = group.get("path") && let KV::Path(path) = prop.value_at(layer.layer_time(selection.time)) { - let selected = if selection.vertices.is_empty() { - selected || selection.props.contains(&(layer.id, prop.uid)) - } else { - selection.vertices.iter().any(|v| v.layer == layer.id && v.mask == group.uid && v.index < path.vertices.len()) - }; + // Property selection also selects its points; both describe the selected paths. + let selected = selected + || selection.props.contains(&(layer.id, prop.uid)) + || selection.vertices.iter().any(|v| v.layer == layer.id && v.mask == group.uid && v.index < path.vertices.len()); if selected { targets.push(Target { layer: layer.id, route: route.clone(), closed: path.closed }); } diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index 36e3b0537..a9cd5c646 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -302,7 +302,7 @@ fn toggle_closed_respects_path_and_vertex_selection() { s.state.selected_props = vec![(LayerId(lid), b)]; s.state.selected_vertices = vec![crate::VertexRef { layer: LayerId(lid), mask: a, index: 1 }]; s.execute("path.toggleClosed", json!({})).unwrap(); - assert!(shape_path(&s, lid, a).closed, "vertices take precedence over property selection"); + assert!(shape_path(&s, lid, a).closed, "selected points and properties both describe target paths"); assert!(shape_path(&s, lid, b).closed); s.state.selected_vertices.clear(); s.state.selected_props = vec![(LayerId(lid), a), (LayerId(lid), leaf), (LayerId(lid), b)]; @@ -323,6 +323,31 @@ fn toggle_closed_respects_path_and_vertex_selection() { assert!(!shape_path(&s, lid, b).closed, "layer selection addresses all editable paths"); } +#[test] +fn toggle_closed_combines_property_and_shape_group_selections() { + let mut s = comp(); + let a = s.execute("shape.newPath", json!({"vertices": [[0, 0], [100, 0], [100, 100]], "closed": true})).unwrap(); + let (lid, a_path) = (a["layer"].as_u64().unwrap(), a["path"].as_u64().unwrap()); + let b = s.execute("shape.newPath", json!({"layer": lid, "vertices": [[10, 10], [90, 10], [90, 90]], "closed": true})).unwrap(); + let (b_path, b_group) = (b["path"].as_u64().unwrap(), b["group"].as_u64().unwrap()); + let c_path = + s.execute("shape.newPath", json!({"layer": lid, "vertices": [[20, 20], [80, 20], [80, 80]], "closed": true})).unwrap()["path"].as_u64().unwrap(); + s.execute("prop.select", json!({"layer": lid, "prop": a_path})).unwrap(); + s.execute("prop.select", json!({"layer": lid, "prop": b_group, "add": true})).unwrap(); + assert!(s.state.selected_vertices.iter().all(|v| v.mask == a_path), "only the path property implies selected vertices"); + assert!(!s.state.selected_vertices.is_empty()); + assert!(s.state.selected_props.contains(&(LayerId(lid), b_group))); + let result = s.execute("path.toggleClosed", json!({})).unwrap(); + assert_eq!(result["paths"], json!(2)); + assert!(!shape_path(&s, lid, a_path).closed); + assert!(!shape_path(&s, lid, b_path).closed, "the added shape group is included alongside implied vertices"); + assert!(shape_path(&s, lid, c_path).closed, "unselected sibling stays unchanged"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(shape_path(&s, lid, a_path).closed); + assert!(shape_path(&s, lid, b_path).closed, "both selected paths undo together"); + assert!(shape_path(&s, lid, c_path).closed); +} + #[test] fn toggle_closed_ignores_selections_retained_on_other_layers() { let mut s = comp(); From 4966534c8274e2349dc75c3cd26e9200b69b01b0 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Wed, 4 Nov 2026 07:55:33 +0100 Subject: [PATCH 010/350] Make the audio preview follow edits that change the sound only, and start it when a silent preview gains sound (#2104) --- crates/ui-egui/src/app/playback.rs | 1 + crates/ui-egui/src/app/prefetch.rs | 16 ++++++++++++ crates/ui-egui/src/audio.rs | 40 ++++++++++++++++++++++++++++-- crates/ui-egui/src/lib.rs | 2 ++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/app/playback.rs b/crates/ui-egui/src/app/playback.rs index d40bc69ff..5bb860d5b 100644 --- a/crates/ui-egui/src/app/playback.rs +++ b/crates/ui-egui/src/app/playback.rs @@ -40,6 +40,7 @@ impl EffectcraftApp { restore_max, audio_frame: (!plan.video).then_some(plan.first), audio_held: false, + silent_project: None, starting: false, starting_since: now, first_cached: None, diff --git a/crates/ui-egui/src/app/prefetch.rs b/crates/ui-egui/src/app/prefetch.rs index c18bb2415..2277589de 100644 --- a/crates/ui-egui/src/app/prefetch.rs +++ b/crates/ui-egui/src/app/prefetch.rs @@ -70,6 +70,7 @@ impl EffectcraftApp { let step = plan.map_or(1, |p| p.step); let fit_ahead = self.frames_that_fit(&c, scale) - 1; self.prefetch_playback(&pass, fit_ahead); + self.wake_silent_preview(); self.pump_meters(ctx, now); if !self.playback.playing { self.cache_when_idle(ctx, &c, &series, now, input_active, (wa, wb)); @@ -254,6 +255,21 @@ impl EffectcraftApp { false } + /// A silent preview whose project was edited so that it has sound now (a layer unmuted while + /// playing, #2104): hold the sound for [`Self::start_held_audio`], which starts it from the + /// current frame once the frames ahead are cached. Checked once per project edit. + fn wake_silent_preview(&mut self) { + let pb = &self.playback; + if self.audio.is_some() || !pb.playing || pb.caching || pb.starting || pb.audio_held || !pb.plan.is_some_and(|p| p.video) { + return; + } + if pb.silent_project.as_ref().is_some_and(|p| std::sync::Arc::ptr_eq(p, &self.session.project)) { + return; + } + self.playback.silent_project = Some(self.session.project.clone()); + self.playback.audio_held = self.audio_plays(); + } + /// Held sound starts once the frames ahead are cached again after a stall (see /// `Playback::audio_held`): the prefetch window when the silent preview keeps real time, else /// the rest of the preview. diff --git a/crates/ui-egui/src/audio.rs b/crates/ui-egui/src/audio.rs index f8b52fc88..eb54822d1 100644 --- a/crates/ui-egui/src/audio.rs +++ b/crates/ui-egui/src/audio.rs @@ -23,7 +23,7 @@ use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; -use effectcraft_engine::project::{ItemId, ItemKind, LayerSource, Project}; +use effectcraft_engine::project::{GroupKind, ItemId, ItemKind, Layer, LayerSource, Project, PropGroup}; use effectcraft_engine::time::{FrameRate, Tick}; use crate::frames::RenderSource; @@ -342,7 +342,7 @@ fn audio_changed(old: &Project, new: &Project, comp: ItemId) -> bool { || x.switches.solo != y.switches.solo || x.switches.effects != y.switches.effects || x.props.sub("audio") != y.props.sub("audio") - || x.effects() != y.effects() + || audio_effects(x) != audio_effects(y) || match &x.source { LayerSource::Footage { item } => footage_differs(old, new, *item), LayerSource::Comp { item } => comps_differ(old, new, *item, depth + 1), @@ -350,6 +350,12 @@ fn audio_changed(old: &Project, new: &Project, comp: ItemId) -> bool { } }) } + /// The layer's audio effects (Effect > Audio), the only effects the mixer applies; a video effect + /// edit leaves them as they were. + fn audio_effects(l: &Layer) -> Vec<&PropGroup> { + let is_audio = |g: &&PropGroup| matches!(&g.kind, GroupKind::Effect { effect } if effectcraft_engine::effects::audio_fx::is_audio_effect(effect)); + l.effects().into_iter().flat_map(|fx| fx.groups()).filter(is_audio).collect() + } /// The footage item's audio availability changed (relinked, missing, no audio). fn footage_differs(old: &Project, new: &Project, item: ItemId) -> bool { let f = |p: &Project| match p.item(item).map(|i| &i.kind) { @@ -763,6 +769,36 @@ mod tests { } } + /// #2104: a video effect edit leaves the mix alone, an audio effect changes it. + #[test] + fn only_audio_effects_change_the_audio() { + use effectcraft_engine::color::Label; + use effectcraft_engine::project::build::{self, Ids}; + use effectcraft_engine::project::{Comp, Footage, FootageKind}; + + let mut p = Project::default(); + let footage = + Footage { path: "tone.flac".into(), kind: FootageKind::Audio, duration: Tick::from_seconds_f64(60.0), has_audio: true, ..Default::default() }; + let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); + let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); + comp.layers = vec![layer].into(); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let with_effect = |id: &str| { + let mut edited = p.clone(); + let spec = effectcraft_engine::effects::find(id).expect("the effect exists"); + let mut next = edited.next_id; + let g = effectcraft_engine::effects::instantiate(spec, &mut Ids(&mut next), spec.name, [64.0, 36.0]); + edited.next_id = next; + if let Some(fx) = edited.comp_mut(cid).and_then(|c| c.layers.first_mut()).and_then(|l| l.props.sub_mut("effects")) { + fx.children.push(g.into()); + } + edited + }; + assert!(!audio_changed(&p, &with_effect("ec.blur.gaussian"), cid), "a video effect must not drop the sound"); + assert!(audio_changed(&p, &with_effect("ec.audio.backwards"), cid), "an audio effect must be followed"); + } + #[test] fn meter_decays_and_holds_peaks() { let mut m = Meter::default(); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e237eeaa6..cd39b2ba7 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -196,6 +196,8 @@ pub struct Playback { /// fits in the RAM preview) is cached, so it never plays over skipped frames (#103), or /// sooner once the silent preview keeps real time with the frames ahead cached (#208). pub audio_held: bool, + /// The project last checked for a silent preview that became audible (#2104). + pub silent_project: Option>, /// Starting a preview with sound (#947): the playhead stays on the first frame while the /// frames ahead render (the prefetch window, at most what fits in the RAM preview), then /// sound and picture start together from it. Without this the picture played on silently From 29619dfe6749cd977f005a6b73e4ef7bcf12d228 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:38:57 +0100 Subject: [PATCH 011/350] Preview: render at most two frames on the GPU at once so video memory stays flat (#3028) --- crates/ui-egui/src/frames.rs | 59 ++++++++++++++++++++++++++++-- crates/ui-egui/src/frames/tests.rs | 22 +++++++++++ docs/architecture.md | 4 +- 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index 9aad2a81c..c4cc1eee1 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -26,7 +26,7 @@ use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Condvar, Mutex, PoisonError}; use effectcraft_engine::project::{Comp, ItemId, ItemKind, LayerSource, Node, Project, PropGroup}; use effectcraft_engine::render::disk_cache::{self, DiskCache}; @@ -47,6 +47,46 @@ const MIN_GPU_BUDGET: usize = 64 << 20; const GPU_PRESSURE_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(5); const GPU_RECOVERY_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30); +/// GPU frames that render at once, however many frame threads there are. Each one holds its own +/// working textures, so 16 at once ran an RTX 3090 out of video memory (#3028). +const MAX_GPU_RENDERS: usize = 2; + +/// Counts the GPU renders in flight; the rest of the frame threads wait their turn. +struct GpuSlots { + busy: Mutex, + freed: Condvar, +} + +/// One GPU render slot, given back on drop. +struct GpuSlot<'a>(&'a GpuSlots); + +static GPU_SLOTS: GpuSlots = GpuSlots::new(); + +impl GpuSlots { + const fn new() -> Self { + Self { busy: Mutex::new(0), freed: Condvar::new() } + } + + /// Wait until fewer than `max` renders are in flight, then take a slot. The browser has no + /// threads to wait on: it never blocks. + fn acquire(&self, max: usize) -> GpuSlot<'_> { + let mut busy = self.busy.lock().unwrap_or_else(PoisonError::into_inner); + while !cfg!(target_arch = "wasm32") && *busy >= max { + busy = self.freed.wait(busy).unwrap_or_else(PoisonError::into_inner); + } + *busy += 1; + GpuSlot(self) + } +} + +impl Drop for GpuSlot<'_> { + fn drop(&mut self) { + let mut busy = self.0.busy.lock().unwrap_or_else(PoisonError::into_inner); + *busy = busy.saturating_sub(1); + self.0.freed.notify_one(); + } +} + /// A rendered viewer frame: CPU pixels or a GPU texture. #[derive(Clone)] pub enum FrameImage { @@ -1458,7 +1498,10 @@ impl Worker { r.cache = Some(&job.src.layer_cache); r.accel = Some(g as &dyn effectcraft_engine::render::Accelerator); r.active_accel()?; - let f = g.within_memory(|| g.render_display(&r, job.comp, job.t)); + let f = { + let _slot = GPU_SLOTS.acquire(MAX_GPU_RENDERS); + g.within_memory(|| g.render_display(&r, job.comp, job.t)) + }; effectcraft_engine::render::Accelerator::frame_end(g); match f { Ok(f) => { @@ -1497,7 +1540,11 @@ impl Worker { let auto = if cfg!(target_arch = "wasm32") { None } else { r.frame_auto(a, job.comp, true) }; if auto.is_none_or(|(p, key)| p.choose(key)) { let t0 = web_time::Instant::now(); - match g.within_memory(|| g.render_display(&r, job.comp, job.t)) { + let shown = { + let _slot = GPU_SLOTS.acquire(MAX_GPU_RENDERS); + g.within_memory(|| g.render_display(&r, job.comp, job.t)) + }; + match shown { Ok(Some(f)) => { self.gpu_succeeded(); if let Some((p, key)) = auto { @@ -1530,7 +1577,11 @@ impl Worker { } if let Some(g) = gpu { // The GPU composites and the frame is read back. - return match g.within_memory(|| r.comp_frame(job.comp, job.t)) { + let composited = { + let _slot = GPU_SLOTS.acquire(MAX_GPU_RENDERS); + g.within_memory(|| r.comp_frame(job.comp, job.t)) + }; + return match composited { Ok(img) => { self.gpu_succeeded(); FrameImage::Cpu(Arc::new(to_color_image(&img))) diff --git a/crates/ui-egui/src/frames/tests.rs b/crates/ui-egui/src/frames/tests.rs index ace51b0bb..dde9225c5 100644 --- a/crates/ui-egui/src/frames/tests.rs +++ b/crates/ui-egui/src/frames/tests.rs @@ -1113,3 +1113,25 @@ fn timing_markers_and_expressions_still_change_the_frames() { s.execute("layer.addMarker", json!({"comp": outer.0, "layers": [solid], "time": 0.75})).unwrap(); assert_ne!(at(&s), o2, "an expression can read the marker, so the frames change"); } + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn gpu_renders_in_flight_stay_within_the_cap() { + use std::sync::atomic::AtomicUsize; + let slots = GpuSlots::new(); + let now = AtomicUsize::new(0); + let peak = AtomicUsize::new(0); + std::thread::scope(|s| { + for _ in 0..16 { + s.spawn(|| { + let _slot = slots.acquire(2); + let n = now.fetch_add(1, Ordering::SeqCst) + 1; + peak.fetch_max(n, Ordering::SeqCst); + std::thread::sleep(std::time::Duration::from_millis(5)); + now.fetch_sub(1, Ordering::SeqCst); + }); + } + }); + assert!(peak.load(Ordering::SeqCst) <= 2); + assert_eq!(*slots.busy.lock().unwrap_or_else(PoisonError::into_inner), 0); +} diff --git a/docs/architecture.md b/docs/architecture.md index c4bebd9ed..7637dbc28 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -362,7 +362,9 @@ as NaNs (a kernel that skips pixels fails the oracle) and validation errors pani (`ui-egui::frames`); headless renders, the CLI (unless `--gpu`) and CI use the CPU. Outside tests, device errors are logged rather than fatal. A viewer frame that runs out of video memory (`Gpu::within_memory`) renders on the CPU, the uploaded layers and pooled textures are freed, and -the RAM preview keeps half as many GPU frames from then on. The same guard catches a frame's +the RAM preview keeps half as many GPU frames from then on. However many frame threads the viewer +runs, at most two of them render on the GPU at once and the rest wait their turn, so their working +textures don't add up to more video memory than the GPU has (#3028). The same guard catches a frame's validation errors (an invalid texture, often left by a failed allocation): that frame renders on the CPU and the kept textures are freed, but the compositor stays on and, with no allocation failure before it, the RAM preview keeps its GPU frames (#1319). A frame whose render panics is From d10991eff3b405aa8f1e3171ece47cde0bd764e2 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 14:45:39 +0100 Subject: [PATCH 012/350] AEP import: map HDR Highlight Compression, Echo, Linear Wipe, Offset and Color Stabilizer The importer reported these five effects as lost. They now become EffectCraft's effects of the same name with every parameter After Effects stores (ranges, defaults and popup items from our own After Effects 2026 project, documented in docs/aep-format.md 3.7): - ADBE HDR ToneMap -> ec.utility.hdrcompression (Amount) - ADBE Echo -> ec.time.echo (Echo Time, Number Of Echoes, Starting Intensity, Decay, Echo Operator by item name) - ADBE Linear Wipe -> ec.transition.linearwipe - ADBE Offset -> ec.distort.offset (Blend With Original is a stored 0..1 fraction, a percentage here) - ADBE Deflicker -> ec.color.colorstabilizer (fresh Stabilize default Levels, points, Sample Size) A table row can name the range EffectCraft's effect works in (ParamMap::limit): Echo values or keys beyond 255 echoes or a Decay of 1 are imported as stored and reported. Tests build each effect into the committed synthetic.aep (fx_inject.rs, which also checks its pard layout reads back). On the sample project the effect losses go from 75 to 3 (the out-of-range Echo values). Co-Authored-By: Claude Opus 5.5 --- crates/aep/src/container/effect_map.rs | 62 +++- crates/aep/src/import/effects.rs | 16 +- crates/aep/tests/it/import.rs | 2 + crates/aep/tests/it/import/effects.rs | 2 +- .../aep/tests/it/import/effects_quickwins.rs | 149 ++++++++ crates/aep/tests/it/import/fx_inject.rs | 322 ++++++++++++++++++ docs/aep-format.md | 14 + docs/aep-import.md | 17 + 8 files changed, 581 insertions(+), 3 deletions(-) create mode 100644 crates/aep/tests/it/import/effects_quickwins.rs create mode 100644 crates/aep/tests/it/import/fx_inject.rs diff --git a/crates/aep/src/container/effect_map.rs b/crates/aep/src/container/effect_map.rs index a7c9bbd48..5b872450f 100644 --- a/crates/aep/src/container/effect_map.rs +++ b/crates/aep/src/container/effect_map.rs @@ -120,6 +120,10 @@ pub struct ParamMap { /// The EffectCraft parameter id. pub target: &'static str, pub conv: Conv, + /// The range EffectCraft's effect works in when it is narrower than the range After Effects + /// accepts (the effect limits the value there): a stored value or key outside it is imported + /// as it is and reported. `None`: no narrower range. + pub limit: Option<(f64, f64)>, } /// Values of an effect that are not ordinary parameters. @@ -147,7 +151,12 @@ pub struct EffectMap { } const fn pm(index: usize, kind: ParamKind, target: &'static str, conv: Conv) -> ParamMap { - ParamMap { index, kind, target, conv } + ParamMap { index, kind, target, conv, limit: None } +} + +/// [`pm`] for a parameter EffectCraft's effect limits to `lo..=hi` (see [`ParamMap::limit`]). +const fn pm_limited(index: usize, kind: ParamKind, target: &'static str, conv: Conv, lo: f64, hi: f64) -> ParamMap { + ParamMap { index, kind, target, conv, limit: Some((lo, hi)) } } const fn fx(match_name: &'static str, effect: &'static str, params: &'static [ParamMap]) -> EffectMap { @@ -339,6 +348,13 @@ pub const MATTE_USE: &[&str] = &["Red Channel", "Green Channel", "Blue Channel", /// 903. pub const SHUTTER_CONTROL: &[&str] = &["Manual", "Automatic"]; +// After Effects' popup items of the #3110 effects as the 2026-11-05 importer block stores them +// (`pdnm`; the `echo-*` and `colstab-*` compositions). +/// Echo's Echo Operator (`echo-05` ... `echo-10`). +pub const ECHO_OPERATOR: &[&str] = &["Add", "Maximum", "Minimum", "Screen", "Composite In Back", "Composite In Front", "Blend"]; +/// Color Stabilizer's Stabilize (`colstab-02`, `colstab-03`; the fresh default is Levels). +pub const STABILIZE: &[&str] = &["Brightness", "Levels", "Curves"]; + /// The effects the importer converts. pub const EFFECTS: &[EffectMap] = &[ // Tint (356-364): Map Black To, Map White To, Amount to Tint; 4 = Swap Colors (a button). @@ -1006,6 +1022,50 @@ pub const EFFECTS: &[EffectMap] = &[ pm(4, K::FloatSlider, "vectorDetail", Conv::Plain { valid_max: 100.0 }), ], ), + // HDR Highlight Compression (importer block 2026-11-05, `hdr-*`): Amount a float slider in + // percent (valid 0..100, default 100). + fx("ADBE HDR ToneMap", "ec.utility.hdrcompression", &[pm(1, K::FloatSlider, "amount", Conv::Plain { valid_max: 100.0 })]), + // Echo (`echo-*`): Echo Time in seconds (valid -30 000..30 000), Number Of Echoes an integer + // (valid 0..30 000), Starting Intensity 0..1, Decay (valid 0..100, slider 0..1), Echo + // Operator. EffectCraft's Echo uses at most 255 echoes and a Decay up to 1. + fx( + "ADBE Echo", + "ec.time.echo", + &[ + pm(1, K::FloatSlider, "echoTime", Conv::Plain { valid_max: 30000.0 }), + pm_limited(2, K::IntSlider, "numberOfEchoes", Conv::Plain { valid_max: 30000.0 }, 0.0, 255.0), + pm(3, K::FloatSlider, "startingIntensity", Conv::Plain { valid_max: 1.0 }), + pm_limited(4, K::FloatSlider, "decay", Conv::Plain { valid_max: 100.0 }, 0.0, 1.0), + pm(5, K::Popup, "echoOperator", Conv::Popup { default: 1, items: ECHO_OPERATOR }), + ], + ), + // Linear Wipe (`linwipe-*`): Transition Completion and Feather fixed sliders (valid 100 and + // 32 000), Wipe Angle in degrees. + fx( + "ADBE Linear Wipe", + "ec.transition.linearwipe", + &[ + pm(1, K::FixedSlider, "completion", Conv::Plain { valid_max: 100.0 }), + pm(2, K::Angle, "angle", Conv::Angle), + pm(3, K::FixedSlider, "feather", Conv::Plain { valid_max: 32000.0 }), + ], + ), + // Offset (`offset-*`): Blend With Original is a 0..1 fraction, a percentage here. + fx("ADBE Offset", "ec.distort.offset", &[pm(1, K::Point, "shift", Conv::Point), pm(2, K::FixedSlider, "blend", Conv::Percent { valid_max: 1.0 })]), + // Color Stabilizer (`colstab-*`): Stabilize (fresh default Levels), the three sample points, + // Sample Size a fixed slider (valid 50, default 3). The reference frame (Set Frame) is not a + // parameter in the file and is not imported. + fx( + "ADBE Deflicker", + "ec.color.colorstabilizer", + &[ + pm(1, K::Popup, "stabilize", Conv::Popup { default: 2, items: STABILIZE }), + pm(2, K::Point, "blackPoint", Conv::Point), + pm(3, K::Point, "midPoint", Conv::Point), + pm(4, K::Point, "whitePoint", Conv::Point), + pm(5, K::FixedSlider, "sampleSize", Conv::Plain { valid_max: 50.0 }), + ], + ), // Layer Control (076, 220, 320, 321): the default is the owning layer, None is unset. fx("ADBE Layer Control", "ec.control.layer", &[pm(1, K::Layer, "layer", Conv::LayerRef { own: true })]), fx("ADBE Slider Control", "ec.control.slider", &[pm(1, K::FloatSlider, "slider", Conv::Plain { valid_max: 1_000_000.0 })]), diff --git a/crates/aep/src/import/effects.rs b/crates/aep/src/import/effects.rs index 477a25df4..1d20a9dd1 100644 --- a/crates/aep/src/import/effects.rs +++ b/crates/aep/src/import/effects.rs @@ -266,6 +266,7 @@ fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_> if let Some(map) = &popup_map { remap_popup_keys(prop, map, &here, &label, col); } + report_beyond_limit(m.limit, prop.keys.iter().map(|k| &k.value), &here, &label, col); } return; } @@ -298,11 +299,24 @@ fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_> _ => convert(value, kind), }; match converted { - Some(v) => prop.value = v, + Some(v) => { + report_beyond_limit(m.limit, std::iter::once(&v), &here, &label, col); + prop.value = v; + } None => col.lose(LossCategory::DamagedData, format!("{here}: parameter {label} holds a value that is not valid ({value:?}), default used")), } } +/// Reports a value After Effects accepts but EffectCraft's effect limits (`ParamMap::limit`): it is +/// imported as it is, and the effect renders it as the nearest end of its range. +fn report_beyond_limit<'v>(limit: Option<(f64, f64)>, values: impl Iterator, here: &str, label: &str, col: &mut Collector) { + let Some((lo, hi)) = limit else { return }; + let beyond: Vec = values.filter_map(|v| if let Value::Scalar(x) = v { Some(*x) } else { None }).filter(|x| *x < lo || *x > hi).collect(); + if let Some(x) = beyond.first() { + col.lose(LossCategory::Effects, format!("{here}: parameter {label} is {x}, EffectCraft's effect uses {lo} to {hi} (rendered as the nearest of them)")); + } +} + /// A layer parameter: None stays unset, the owning layer is itself (`own`) or unset, another layer /// is resolved by [`resolve_layer_refs`] once the composition's layers exist. /// `names` is `(here, label)`, the effect and the parameter for the report. diff --git a/crates/aep/tests/it/import.rs b/crates/aep/tests/it/import.rs index 95dced570..178dd2bb3 100644 --- a/crates/aep/tests/it/import.rs +++ b/crates/aep/tests/it/import.rs @@ -11,8 +11,10 @@ mod combined; mod ease_round2; mod effects; mod effects_batch5; +mod effects_quickwins; mod effects_round2; mod effects_round3; +mod fx_inject; mod markers; mod masks; mod mattes; diff --git a/crates/aep/tests/it/import/effects.rs b/crates/aep/tests/it/import/effects.rs index be1293b9a..aa4180567 100644 --- a/crates/aep/tests/it/import/effects.rs +++ b/crates/aep/tests/it/import/effects.rs @@ -72,7 +72,7 @@ fn the_table_names_tint_gradient_ramp_and_exposure() { ] { assert!(t.contains(&want), "{want:?}"); } - assert_eq!(t.len(), 57); + assert_eq!(t.len(), 62); for (_, id) in t { assert!(effectcraft_effects::find(id).is_some(), "{id}"); } diff --git a/crates/aep/tests/it/import/effects_quickwins.rs b/crates/aep/tests/it/import/effects_quickwins.rs new file mode 100644 index 000000000..792e08d74 --- /dev/null +++ b/crates/aep/tests/it/import/effects_quickwins.rs @@ -0,0 +1,149 @@ +//! HDR Highlight Compression, Echo, Linear Wipe, Offset and Color Stabilizer (#3110) arrive as +//! EffectCraft's effects of the same name. Each is built into `synthetic.aep` (`fx_inject`) with +//! the definitions After Effects 2026 wrote for it in the importer block of 2026-11-05 (valid +//! ranges, defaults, popup items) and the values of that block's compositions. + +use crate::import::fx_inject::{Default, Fx, losses, near, num, other, popup, pt, run, slider, v}; +use effectcraft_aep::ParamKind as K; +use effectcraft_aep::import::LossCategory; +use effectcraft_keyframe::Value; + +const ECHO_OPS: &[&str] = &["Add", "Maximum", "Minimum", "Screen", "Composite In Back", "Composite In Front", "Blend"]; + +fn echo(values: Vec<(usize, f64)>) -> Fx { + Fx { + match_name: "ADBE Echo", + name: "Echo", + defs: vec![ + slider(K::FloatSlider, "Echo Time (seconds)", -1.0 / 30.0, (-30000.0, 30000.0)), + slider(K::IntSlider, "Number Of Echoes", 1.0, (0.0, 30000.0)), + slider(K::FloatSlider, "Starting Intensity", 1.0, (0.0, 1.0)), + slider(K::FloatSlider, "Decay", 1.0, (0.0, 100.0)), + popup("Echo Operator", 1, ECHO_OPS), + ], + values, + data: vec![], + } +} + +#[test] +fn hdr_highlight_compression_amount() { + let fx = |values| Fx { + match_name: "ADBE HDR ToneMap", + name: "HDR Highlight Compression", + defs: vec![slider(K::FloatSlider, "Amount", 100.0, (0.0, 100.0))], + values, + data: vec![], + }; + // `hdr-01-set-all`: 90; `hdr-00-defaults`: no record, 100. + for (values, want) in [(vec![(1, 90.0)], 90.0), (vec![], 100.0)] { + let (g, r) = run(&fx(values), "ec.utility.hdrcompression"); + assert_eq!(losses(&r, LossCategory::Effects, "ADBE HDR ToneMap"), Vec::::new()); + near(&g.expect("imported"), "amount", want); + } +} + +#[test] +fn echo_values_and_operator() { + // `echo-01-set-all` (without its Decay 20, below) and `echo-09-pop5-6`. + let (g, r) = run(&echo(vec![(1, 9.967), (2, 14.0), (3, 0.0), (5, 6.0)]), "ec.time.echo"); + assert_eq!(losses(&r, LossCategory::Effects, "ADBE Echo"), Vec::::new()); + let g = g.expect("imported"); + near(&g, "echoTime", 9.967); + near(&g, "numberOfEchoes", 14.0); + near(&g, "startingIntensity", 0.0); + near(&g, "decay", 1.0); + assert_eq!(v(&g, "echoOperator"), Value::Enum(5), "Composite In Front"); + // `echo-00-defaults`: one echo a frame back (1/30 s), Add. + let (g, _) = run(&echo(vec![]), "ec.time.echo"); + let g = g.expect("imported"); + assert!((num(&g, "echoTime") + 1.0 / 30.0).abs() < 1e-6); + assert_eq!((num(&g, "numberOfEchoes"), v(&g, "echoOperator")), (1.0, Value::Enum(0))); +} + +#[test] +fn echo_values_beyond_effectcrafts_range_are_reported() { + // `echo-03-max`: 30 000 echoes and Decay 100; EffectCraft's Echo uses at most 255 and 1. + let (g, r) = run(&echo(vec![(2, 30000.0), (4, 100.0)]), "ec.time.echo"); + let found = losses(&r, LossCategory::Effects, "ADBE Echo"); + assert_eq!(found.len(), 2, "{found:?}"); + assert!(found.iter().any(|d| d.contains("Number Of Echoes") && d.contains("0 to 255")), "{found:?}"); + assert!(found.iter().any(|d| d.contains("Decay") && d.contains("0 to 1")), "{found:?}"); + // Kept as stored. + let g = g.expect("still imported"); + near(&g, "numberOfEchoes", 30000.0); + near(&g, "decay", 100.0); +} + +#[test] +fn linear_wipe_values() { + let fx = |values| Fx { + match_name: "ADBE Linear Wipe", + name: "Linear Wipe", + defs: vec![ + slider(K::FixedSlider, "Transition Completion", 0.0, (0.0, 100.0)), + other(K::Angle, "Wipe Angle", Default::Number(90.0)), + slider(K::FixedSlider, "Feather", 0.0, (0.0, 32000.0)), + ], + values, + data: vec![], + }; + // `linwipe-01-set-all`. + let (g, r) = run(&fx(vec![(1, 10.0), (2, 77.0), (3, 16.0)]), "ec.transition.linearwipe"); + assert_eq!(losses(&r, LossCategory::Effects, "ADBE Linear Wipe"), Vec::::new()); + let g = g.expect("imported"); + near(&g, "completion", 10.0); + near(&g, "angle", 77.0); + near(&g, "feather", 16.0); + // `linwipe-00-defaults`: the Wipe Angle default 90. + near(&run(&fx(vec![]), "ec.transition.linearwipe").0.expect("imported"), "angle", 90.0); +} + +#[test] +fn offset_blend_is_a_fraction_and_the_centre_a_point() { + let fx = |values| Fx { + match_name: "ADBE Offset", + name: "Offset", + defs: vec![other(K::Point, "Shift Center To", Default::Point(0.5, 0.5)), slider(K::FixedSlider, "Blend With Original", 0.0, (0.0, 1.0))], + values, + data: vec![], + }; + // `offset-02-chk-toggled`: Blend With Original stored 1.0 is 100 %. + let (g, r) = run(&fx(vec![(2, 1.0)]), "ec.distort.offset"); + assert_eq!(losses(&r, LossCategory::Effects, "ADBE Offset"), Vec::::new()); + let g = g.expect("imported"); + near(&g, "blend", 100.0); + // The default centre is the layer's centre (synthetic.aep's layer is 8x8). + pt(&g, "shift", [4.0, 4.0]); + near(&run(&fx(vec![(2, 0.25)]), "ec.distort.offset").0.expect("imported"), "blend", 25.0); +} + +#[test] +fn color_stabilizer_points_sample_size_and_stabilize() { + let fx = |values| Fx { + match_name: "ADBE Deflicker", + name: "Color Stabilizer", + defs: vec![ + popup("Stabilize", 2, &["Brightness", "Levels", "Curves"]), + other(K::Point, "Black Point", Default::Point(0.2, 0.5)), + other(K::Point, "Mid Point", Default::Point(0.5, 0.5)), + other(K::Point, "White Point", Default::Point(0.8, 0.5)), + slider(K::FixedSlider, "Sample Size", 3.0, (0.0, 50.0)), + ], + values, + data: vec![], + }; + // `colstab-00-defaults`: Levels, the points at 20 %, 50 % and 80 % of the width. + let (g, r) = run(&fx(vec![]), "ec.color.colorstabilizer"); + assert_eq!(losses(&r, LossCategory::Effects, "ADBE Deflicker"), Vec::::new()); + let g = g.expect("imported"); + assert_eq!(v(&g, "stabilize"), Value::Enum(1), "Levels"); + pt(&g, "blackPoint", [1.6, 4.0]); + pt(&g, "whitePoint", [6.4, 4.0]); + near(&g, "sampleSize", 3.0); + // `colstab-03-pop1-3` and `colstab-27-pop5-25`. + let (g, _) = run(&fx(vec![(1, 3.0), (5, 25.0)]), "ec.color.colorstabilizer"); + let g = g.expect("imported"); + assert_eq!(v(&g, "stabilize"), Value::Enum(2), "Curves"); + near(&g, "sampleSize", 25.0); +} diff --git a/crates/aep/tests/it/import/fx_inject.rs b/crates/aep/tests/it/import/fx_inject.rs new file mode 100644 index 000000000..9687e2d6c --- /dev/null +++ b/crates/aep/tests/it/import/fx_inject.rs @@ -0,0 +1,322 @@ +//! Builds an effect instance into the committed `synthetic.aep` in place of its Glow (the instance +//! on the layer and the project's shared definitions), so an effect mapping can be tested without +//! the external corpus. The parameter definitions are written in the `pard` layout of +//! docs/aep-format.md 3.7 (type at byte 15, name at 16, default at 56, a float slider's valid +//! range as `f32` at 104 / 108 and its slider range at 112 / 116, a fixed or integer slider's valid +//! maximum at 128, a popup's item count at 60); `pard_layout_decodes_as_written` reads them back. +//! The ranges and defaults the tests use are the ones After Effects 2026 wrote in the importer +//! block of 2026-11-05 (`1100-quickwins.aep`, `1300-thirdparty.aep`, own projects). + +use effectcraft_aep::import::{ImportOptions, ImportResult, LossCategory, import}; +use effectcraft_aep::{Body, Chunk, ParamKind, ParamValue, Project as AepProject, parse, write}; +use effectcraft_keyframe::Value; +use effectcraft_project::{Project, PropGroup}; + +const SYNTHETIC: &[u8] = include_bytes!("../../fixtures/synthetic.aep"); + +/// A parameter definition. +#[derive(Clone)] +pub struct Def { + pub kind: ParamKind, + pub name: &'static str, + pub default: Default, + /// A float slider's valid range, a fixed or integer slider's valid maximum (`.1`). + pub valid: (f64, f64), + /// A popup's items. + pub items: &'static [&'static str], +} + +/// The default a definition holds at byte 56. +#[derive(Clone, Copy)] +pub enum Default { + None, + Number(f64), + Index(u32), + Point(f64, f64), + /// ARGB 0..255. + Color([u8; 4]), +} + +fn kind_code(k: ParamKind) -> u8 { + match k { + ParamKind::Layer => 0, + ParamKind::IntSlider => 1, + ParamKind::FixedSlider => 2, + ParamKind::Angle => 3, + ParamKind::Checkbox => 4, + ParamKind::Color => 5, + ParamKind::Point => 6, + ParamKind::Popup => 7, + ParamKind::NoData => 9, + ParamKind::FloatSlider => 10, + ParamKind::ArbitraryData => 11, + ParamKind::GroupStart => 13, + ParamKind::GroupEnd => 14, + ParamKind::Button => 15, + _ => 9, + } +} + +fn fixed(v: f64) -> [u8; 4] { + ((v * 65536.0).round() as i32).to_be_bytes() +} + +pub fn pard(d: &Def) -> Chunk<'static> { + let mut b = vec![0u8; 148]; + b[15] = kind_code(d.kind); + b[16..16 + d.name.len()].copy_from_slice(d.name.as_bytes()); + match (d.kind, d.default) { + (ParamKind::FloatSlider, Default::Number(v)) => b[56..64].copy_from_slice(&v.to_be_bytes()), + (ParamKind::FixedSlider | ParamKind::Angle, Default::Number(v)) => b[56..60].copy_from_slice(&fixed(v)), + (ParamKind::IntSlider | ParamKind::Checkbox, Default::Number(v)) => b[56..60].copy_from_slice(&(v as i32).to_be_bytes()), + (ParamKind::Popup, Default::Index(i)) => b[56..60].copy_from_slice(&i.to_be_bytes()), + (ParamKind::Point, Default::Point(x, y)) => { + b[56..60].copy_from_slice(&fixed(x)); + b[60..64].copy_from_slice(&fixed(y)); + } + (ParamKind::Color, Default::Color(c)) => b[56..60].copy_from_slice(&c), + _ => {} + } + match d.kind { + ParamKind::FloatSlider => { + b[104..108].copy_from_slice(&(d.valid.0 as f32).to_be_bytes()); + b[108..112].copy_from_slice(&(d.valid.1 as f32).to_be_bytes()); + b[112..116].copy_from_slice(&(d.valid.0 as f32).to_be_bytes()); + b[116..120].copy_from_slice(&(d.valid.1 as f32).to_be_bytes()); + } + ParamKind::FixedSlider => b[128..132].copy_from_slice(&fixed(d.valid.1)), + ParamKind::IntSlider => b[128..132].copy_from_slice(&(d.valid.1 as i32).to_be_bytes()), + ParamKind::Popup => b[60..62].copy_from_slice(&(d.items.len() as u16).to_be_bytes()), + _ => {} + } + Chunk::data(*b"pard", b) +} + +/// A match name chunk (`tdmn`, 40 bytes). +pub fn tdmn(name: &str) -> Chunk<'static> { + let mut b = name.as_bytes().to_vec(); + b.resize(40, 0); + Chunk::data(*b"tdmn", b) +} + +fn utf8(s: &str) -> Chunk<'static> { + Chunk::data(*b"Utf8", s.as_bytes().to_vec()) +} + +/// A chunk whose body is chunks without being a `LIST` (`pdnm`, `fnam`: `NESTED_IDS`). +fn nested(id: [u8; 4], children: Vec>) -> Chunk<'static> { + Chunk { id, body: Body::Chunks(children), pad: None } +} + +/// A static value record holding `v`, after the one After Effects wrote for Glow's A & B Midpoint +/// (its header; without the expression). +fn record(template: &Chunk<'_>, v: f64) -> Chunk<'static> { + let mut kids = Vec::new(); + for c in template.children() { + match &c.body { + Body::Data(d) if c.id == *b"tdsb" || c.id == *b"tdb4" => kids.push(Chunk::data(c.id, d.to_vec())), + Body::Data(_) if c.id == *b"cdat" => { + let mut d = vec![0u8; 40]; + d[..8].copy_from_slice(&v.to_be_bytes()); + kids.push(Chunk::data(*b"cdat", d)); + } + _ => {} + } + } + Chunk::list(*b"tdbs", kids) +} + +/// An effect instance: its match name, display name, parameter definitions (after the input +/// layer, before the built-in entry), the 1-D values stored (`parT` index, value) and the +/// arbitrary data stored (`parT` index, bytes). +pub struct Fx { + pub match_name: &'static str, + pub name: &'static str, + pub defs: Vec, + pub values: Vec<(usize, f64)>, + pub data: Vec<(usize, Vec)>, +} + +fn param_name(fx: &Fx, i: usize) -> String { + format!("{}-{i:04}", fx.match_name) +} + +fn sspc(fx: &Fx, template: &Chunk<'_>) -> Chunk<'static> { + let n = fx.defs.len() + 2; + let mut part = vec![ + Chunk::data(*b"parn", (n as u32).to_be_bytes().to_vec()), + tdmn(¶m_name(fx, 0)), + pard(&Def { kind: ParamKind::Layer, name: "", default: Default::None, valid: (0.0, 0.0), items: &[] }), + ]; + for (i, d) in fx.defs.iter().enumerate() { + part.push(tdmn(¶m_name(fx, i + 1))); + part.push(pard(d)); + if d.kind == ParamKind::Popup { + part.push(nested(*b"pdnm", vec![utf8(&d.items.join("|"))])); + } + } + part.push(tdmn("ADBE Effect Built In Params")); + part.push(pard(&Def { kind: ParamKind::NoData, name: "", default: Default::None, valid: (0.0, 0.0), items: &[] })); + let mut values = vec![Chunk::data(*b"tdsb", 1u32.to_be_bytes().to_vec())]; + for (i, v) in &fx.values { + values.push(tdmn(¶m_name(fx, *i))); + values.push(record(template, *v)); + if let Some((_, d)) = fx.data.iter().find(|(j, _)| j == i) { + values.push(Chunk::list(*b"aRbs", vec![Chunk::data(*b"aRbp", d.clone())])); + } + } + Chunk::list(*b"sspc", vec![nested(*b"fnam", vec![utf8(fx.name)]), Chunk::list(*b"parT", part), Chunk::list(*b"tdgp", values)]) +} + +/// The value record of Glow's A & B Midpoint (`ADBE Glo2-0011`): a 1-D static record. +fn find_glow_template(chunks: &[Chunk<'_>]) -> Option> { + for c in chunks { + let kids = c.children(); + if let Some(at) = kids.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.starts_with(b"ADBE Glo2-0011\0"))) + && let Some(record) = kids.get(at + 1).filter(|k| k.is_list(b"tdbs")) + { + return Some(Chunk::list( + *b"tdbs", + record + .children() + .iter() + .map(|k| match &k.body { + Body::Data(d) => Chunk::data(k.id, d.to_vec()), + _ => Chunk::data(k.id, Vec::new()), + }) + .collect(), + )); + } + if let Some(t) = find_glow_template(kids) { + return Some(t); + } + } + None +} + +fn replace_glow(chunks: &mut [Chunk<'_>], fx: &Fx, template: &Chunk<'_>) -> usize { + let mut n = 0; + for ch in chunks { + let kids = ch.children_mut(); + if let Some(at) = kids.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.starts_with(b"ADBE Glo2\0"))) + && kids.get(at + 1).is_some_and(|k| k.is_list(b"sspc")) + { + kids[at] = tdmn(fx.match_name); + kids[at + 1] = sspc(fx, template); + n += 1; + } + n += replace_glow(kids, fx, template); + } + n +} + +/// `synthetic.aep` with `fx` in place of the Glow effect. +pub fn bytes_with(fx: &Fx) -> Vec { + let file = parse(SYNTHETIC).unwrap(); + let template = find_glow_template(&file.chunks).expect("Glow's A & B Midpoint record"); + let mut file = parse(SYNTHETIC).unwrap(); + assert_eq!(replace_glow(&mut file.chunks, fx, &template), 2, "the Glow effect and its shared definitions"); + write(&file).unwrap() +} + +/// Imports `synthetic.aep` with `fx` in place of its Glow: the EffectCraft effect `effect` (if +/// imported) and the result. +pub fn run(fx: &Fx, effect: &str) -> (Option, ImportResult) { + let bytes = bytes_with(fx); + let mut project = Project::default(); + let none = |_: &str| false; + let r = import(&mut project, &bytes, &ImportOptions { allow_losses: true, ..ImportOptions::new(&none) }).unwrap(); + let g = project + .comps() + .flat_map(|(_, c)| c.layers.iter()) + .find_map(|l| l.props.sub("effects").and_then(|fx| fx.children.iter().filter_map(|n| n.as_group()).find(|g| g.match_id == effect).cloned())); + (g, r) +} + +/// The losses of `category` that name the effect `match_name`. +pub fn losses(r: &ImportResult, category: LossCategory, match_name: &str) -> Vec { + let tag = format!("({match_name})"); + r.report.losses.iter().filter(|l| l.category == category).flat_map(|l| l.details.clone()).filter(|d| d.contains(&tag)).collect() +} + +pub fn v(g: &PropGroup, id: &str) -> Value { + g.prop(id).unwrap_or_else(|| panic!("no {id} in {}", g.match_id)).value.clone() +} + +pub fn num(g: &PropGroup, id: &str) -> f64 { + match v(g, id) { + Value::Scalar(x) => x, + other => panic!("{id}: {other:?}"), + } +} + +pub fn near(g: &PropGroup, id: &str, want: f64) { + let got = num(g, id); + assert!((got - want).abs() <= want.abs().max(1.0) * 1e-6, "{} {id}: {got} != {want}", g.match_id); +} + +pub fn pt(g: &PropGroup, id: &str, want: [f64; 2]) { + let Value::Vec2(a) = v(g, id) else { panic!("{id}") }; + assert!((a[0] - want[0]).abs() < 1e-2 && (a[1] - want[1]).abs() < 1e-2, "{} {id}: {a:?} != {want:?}", g.match_id); +} + +/// A slider definition. +pub fn slider(kind: ParamKind, name: &'static str, default: f64, valid: (f64, f64)) -> Def { + Def { kind, name, default: Default::Number(default), valid, items: &[] } +} + +pub fn popup(name: &'static str, default: u32, items: &'static [&'static str]) -> Def { + Def { kind: ParamKind::Popup, name, default: Default::Index(default), valid: (0.0, 0.0), items } +} + +pub fn other(kind: ParamKind, name: &'static str, default: Default) -> Def { + Def { kind, name, default, valid: (0.0, 0.0), items: &[] } +} + +#[test] +fn pard_layout_decodes_as_written() { + // Echo's Decay (float, valid 0..100, default 1), Linear Wipe's Feather (fixed, valid max + // 32 000), Echo's Number Of Echoes (integer, max 30 000, default 1), Offset's centre point, + // Color Stabilizer's Stabilize (default 2 of 3): what the sample dump of 2026-11-05 read. + let fx = Fx { + match_name: "Test FX", + name: "Test FX", + defs: vec![ + slider(ParamKind::FloatSlider, "Decay", 1.0, (0.0, 100.0)), + slider(ParamKind::FixedSlider, "Feather", 0.0, (0.0, 32000.0)), + slider(ParamKind::IntSlider, "Number Of Echoes", 1.0, (0.0, 30000.0)), + other(ParamKind::Point, "Shift Center To", Default::Point(0.5, 0.5)), + popup("Stabilize", 2, &["Brightness", "Levels", "Curves"]), + other(ParamKind::Color, "Shadows Unbalance", Default::Color([0, 0, 0, 0])), + ], + values: vec![(1, 20.0)], + data: vec![], + }; + let bytes = bytes_with(&fx); + let file = parse(&bytes).unwrap(); + let proj = AepProject::new(&file).unwrap(); + let defs = proj.effect_definition_index(); + let found = proj + .all_items() + .into_iter() + .filter_map(|i| i.comp()) + .flat_map(|c| c.layers().collect::>()) + .flat_map(|l| l.effects()) + .find(|e| e.match_name() == "Test FX") + .expect("the effect"); + let p = defs.params(&found); + let by = |i: usize| p.iter().find(|q| q.index == i).expect("parameter").clone(); + let decay = by(1); + assert_eq!( + (decay.kind, decay.name.as_str(), decay.valid_min(), decay.valid_max(), decay.pard_default()), + (ParamKind::FloatSlider, "Decay", Some(0.0), Some(100.0), Some(ParamValue::Number(1.0))) + ); + assert_eq!(decay.stored_value(), Some(ParamValue::Number(20.0))); + assert_eq!((by(2).kind, by(2).valid_max(), by(2).pard_default()), (ParamKind::FixedSlider, Some(32000.0), Some(ParamValue::Number(0.0)))); + assert_eq!((by(3).kind, by(3).valid_max(), by(3).pard_default()), (ParamKind::IntSlider, Some(30000.0), Some(ParamValue::Number(1.0)))); + assert_eq!(by(4).pard_default(), Some(ParamValue::Point([0.5, 0.5]))); + let stab = by(5); + assert_eq!((stab.popup_item_count(), stab.items.clone()), (Some(3), Some(vec!["Brightness".to_string(), "Levels".into(), "Curves".into()]))); + assert_eq!(found.display_name().as_deref(), Some("Test FX")); + assert_eq!(by(6).pard_default(), Some(ParamValue::Argb([0.0, 0.0, 0.0, 0.0]))); +} diff --git a/docs/aep-format.md b/docs/aep-format.md index fcc8f53be..32ec74854 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -519,6 +519,20 @@ popup_selected_item, borrowed_definition}`, `Effect::has_own_definitions`, (356, 357, 361, 363, 364, 366, 368, 369, 371, 374, 377, 378, 380, 382, 383), `tests/effects_exposure.rs` (389-391, 393-397). +**Importer block 2026-11-05, #3110 (`1100-quickwins.aep`, After Effects 26.2.1; V for every +row: defaults from the defaults composition, the valid ranges from the minimum and maximum +compositions, the items from one composition per popup item).** Each set value is stored as set +(Decay 20 is `20.0`, Blend With Original 100 % is `1.0`); a value set to the default leaves no record +(HDR Amount 100): + +| Effect (match name) | Parameters (`parT` index: type, valid range, default) | +|---|---| +| `ADBE HDR ToneMap` (HDR Highlight Compression) | 1 Amount: float, 0..100 (slider 0..100), default 100 | +| `ADBE Echo` | 1 Echo Time (seconds): float, -30 000..30 000 (slider -5..5), default -1/30 (float32); 2 Number Of Echoes: integer, max 30 000, default 1; 3 Starting Intensity: float, 0..1, default 1; 4 Decay: float, **valid 0..100, slider 0..1**, default 1; 5 Echo Operator: popup (Add, Maximum, Minimum, Screen, Composite In Back, Composite In Front, Blend), default 1 | +| `ADBE Linear Wipe` | 1 Transition Completion: fixed, max 100, default 0; 2 Wipe Angle: angle, default 90; 3 Feather: fixed, max 32 000, default 0 | +| `ADBE Offset` | 1 Shift Center To: point, default the layer centre; 2 Blend With Original: fixed, **max 1 (a 0..1 fraction)**, default 0 | +| `ADBE Deflicker` (Color Stabilizer) | 1 Stabilize: popup (Brightness, Levels, Curves), **default 2 (Levels)**; 2 Black Point, 3 Mid Point, 4 White Point: points, defaults 0.2 / 0.5 / 0.8 of the width at half the height; 5 Sample Size: fixed, max 50, default 3. The reference frame the Set Frame button picks is not among the parameters | + **Curves and Mesh Warp** (961-965, 985; applied from presets, the data is not scriptable): their arbitrary-data parameter (type 11) always has a record (`tdbs` with a 4-byte `cdat`) and a `LIST aRbs` / `aRbp`, also at the default. Curves (`ADBE CurvesCustom-0001`, 1644 bytes): `u16` 1, 1; diff --git a/docs/aep-import.md b/docs/aep-import.md index baeb8d0e1..1926274f0 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -178,6 +178,7 @@ parameter gets the file's value: | `ADBE HUE SATURATION` 1 Channel Control, 2 Channel Range data, 6 Colorize, 7 Colorize Hue, 8 Colorize Saturation, 9 Colorize Lightness | `channelControl`; `hue`, `saturation`, `lightness` from the data; `colorize`, `colorizeHue`, `colorizeSaturation`, `colorizeLightness` | popup by name; the master hue (degrees), saturation and lightness are the `i32` of the arbitrary data (the visible Master parameters keep no record); the colour ranges are reported when they are not at their defaults (not sampled) | 443-450 | | `ADBE Easy Levels2` 1 Channel, 2 Histogram data, 8 / 9 Clip To Output Black / White | `channel`; `inBlack`, `inWhite`, `gamma`, `outBlack`, `outWhite` from the data; `clipToOutputBlack`, `clipToOutputWhite` | popups by name (After Effects' items On, Off, Off for 32 bpc Color are in another order here); the RGB values are the `f32` of the arbitrary data; the per-channel values are reported when they are not at their defaults (not sampled) | 511-516 | | Batch 6 (903 defaults, 904 / 905 set values): `ADBE Brightness & Contrast 2`, `ADBE Turbulent Displace`, `ADBE Fractal Noise`, `ADBE Laser` (Beam), `CC Radial Fast Blur`, `ADBE Noise2`, `ADBE Pro Levels2` (Levels (Individual Controls)), `ADBE Corner Pin`, `ADBE Grid`, `ADBE Sharpen`, `ADBE Linear Color Key2`, `ADBE Radial Wipe`, `ADBE Remove Color Matting`, `CC Bend It`, `CC Power Pin`, `ADBE 4ColorGradient`, `ADBE Change Color`, `ADBE Extract`, `ADBE Noise HLS2`, `ADBE Scatter`, `CC Light Sweep`, `ADBE Compound Blur`, `ADBE Displacement Map`, `ADBE Set Matte3`, `ADBE Gaussian Blur` (legacy), `ADBE OFMotionBlur` (Pixel Motion Blur), `ADBE Layer Control` | the EffectCraft effect of the same name, every parameter EffectCraft has | sliders as stored (each checked against its valid maximum); Beam Length, Time and Softness and CC Power Pin Perspective are 0..1 fractions, percentages here; points as layer pixels; popups by item name (Set Matte's `Red Channel` ... is `Red` ...); twirl-down group markers and the Levels histogram carry nothing; **layer parameters** (below) | 903, 904, 905; 220, 320, 321 | +| Importer block 2026-11-05 (`1100-quickwins.aep`: per effect defaults, set values, minimum and maximum, every popup item, a keyed and an expression parameter): `ADBE HDR ToneMap` (HDR Highlight Compression) 1 Amount; `ADBE Echo` 1 Echo Time, 2 Number Of Echoes, 3 Starting Intensity, 4 Decay, 5 Echo Operator; `ADBE Linear Wipe` 1 Transition Completion, 2 Wipe Angle, 3 Feather; `ADBE Offset` 1 Shift Center To, 2 Blend With Original; `ADBE Deflicker` (Color Stabilizer) 1 Stabilize, 2-4 Black / Mid / White Point, 5 Sample Size | `ec.utility.hdrcompression` `amount`; `ec.time.echo` `echoTime`, `numberOfEchoes`, `startingIntensity`, `decay`, `echoOperator`; `ec.transition.linearwipe` `completion`, `angle`, `feather`; `ec.distort.offset` `shift`, `blend`; `ec.color.colorstabilizer` `stabilize`, `blackPoint`, `midPoint`, `whitePoint`, `sampleSize` | percent (float, valid 0..100, default 100); seconds (float, valid ±30 000), integer (valid 30 000), 0..1 (float), Decay as stored (float, valid 0..100), popup by name: EffectCraft's Echo uses at most 255 echoes and a Decay up to 1, a value or key beyond that is imported as stored and **reported**; percent (fixed, 100), degrees, pixels (fixed, 32 000); point in layer pixels, Blend 0..1 x 100 = percent; popup by name (**fresh default Levels**), points in layer pixels, Sample Size as stored (fixed, valid 50, default 3; its unit, pixels here, is not verified). Color Stabilizer's reference frame (Set Frame) is not a parameter in the file and is not imported (EffectCraft's Reference Frame stays 0 s) | `tests/it/import/effects_quickwins.rs`; render comparison below | | `ADBE Slider Control`, `ADBE Angle Control`, `ADBE Checkbox Control`, `ADBE Color Control`, `ADBE Point Control` (parameter 1) | `ec.control.slider` `slider`, `ec.control.angle` `angle`, `ec.control.checkbox` `checkbox`, `ec.control.color` `color`, `ec.control.point` `point` | float (valid max 1 000 000) as stored; degrees; checkbox; colour; point in layer pixels | 071, 073, 217-219, 221, 304, 310 | - **Nothing to import:** the implicit input-layer parameter (index 0), the Swap Colors buttons @@ -308,6 +309,22 @@ frame blending, 3D material options, audio levels, guides, the mask colour, lock between them (363: 50 at 1 s; 364: grey at 1 s; 382: [950, 500] at 1 s), the defaults equal to EffectCraft's, stack order with a reported Gaussian Blur (366), the second instance (383), and lossless imports without `allowLosses`. +- `crates/aep/tests/it/import/effects_quickwins.rs` (#3110): HDR Highlight Compression, Echo, + Linear Wipe, Offset and Color Stabilizer, each built into `synthetic.aep` (`fx_inject.rs`, which + also checks that its `pard` layout reads back) with the definitions and values of the importer + block of 2026-11-05. Render check of that block's 75 compositions (320x180, 32 bpc, working space + None, 0.5 s) against After Effects' frames (its `saveFrameToPng` of a 32 bpc project writes + `floor(value x 25)` per channel, so values are compared at that quantisation): Offset (5), Linear + Wipe defaults / min / max, Echo with the Add, Maximum, Minimum, Screen operators and the keyed and + expression variants, Color Stabilizer with every Stabilize item and Sample Size up to 25 match + within one level. Differences, all in EffectCraft's effects and not in the mapping: HDR + Highlight Compression's curve (up to 6 of 25 levels on 19 % of the pixels at Amount 100, equal + at Amount 0); Echo's Blend operator (After Effects' frame is as bright as Add's, EffectCraft + averages) and the edge of the moving square with Composite In Back / Front (0.5 % of the + pixels); Linear Wipe's feathered edge (0.6 % of the pixels); Color Stabilizer with a Sample Size + over 25 (EffectCraft's samples drift, After Effects' frame stays unchanged) and with keyed + sample points. The renders do not exercise a Decay above 1 (After Effects' set value has + Starting Intensity 0) or what Color Stabilizer corrects (the test card does not flicker). - `crates/aep/tests/it/import/combined.rs`: masks and effects on the same layer, an effect on a text layer, and the same through `.aepx` (built in-test from the fixtures: the effect parade of one fixture grafted onto the layer of another). From 77e91199e88ff4aa353990f08b8b725e601fca47 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 14:55:01 +0100 Subject: [PATCH 013/350] AEP import: map CC Color Neutralizer, EXtractoR and IDentifier The importer reported these three plug-ins as lost effects. They now become EffectCraft's effects of the same name (parameter lists and data layouts from our own After Effects 2026 project, docs/aep-format.md 3.7): - CS Color Neutralizer -> ec.color.cccolorneutralizer: Pinning and Blend w. Original. After Effects' correction is the per-range Red / Green / Blue sliders (the Unbalance colours are pickers that keep no record), plus View and Black / White Point; EffectCraft's effect has none of them, so each is reported when stored. - EXtractoR -> ec.3d.extractor: Black / White Point, UnMult, and the default Channel Info (R, G, B, (copy)) as EffectCraft's R, G, B, A (ExtractorChannels). Other channels are reported: only the default layout is sampled. - IDentifier -> ec.3d.identifier: Display by item name, ID. Its Channel Info names only the channel type (Object ID, identifier_channel_type), not the channel After Effects reads; After Effects left the sampled EXR unchanged where EffectCraft draws the footage's object ID channel, so every instance is reported. On the sample project no effect is lost any more (29 before); the parameters above are reported instead. Co-Authored-By: Claude Opus 5.5 --- crates/aep/src/container.rs | 2 +- crates/aep/src/container/arbitrary.rs | 51 +++++ crates/aep/src/container/effect_map.rs | 42 ++++ crates/aep/src/import/effects.rs | 38 +++- crates/aep/src/lib.rs | 14 +- crates/aep/tests/it/import.rs | 1 + crates/aep/tests/it/import/effects.rs | 2 +- .../aep/tests/it/import/effects_thirdparty.rs | 179 ++++++++++++++++++ docs/aep-format.md | 16 ++ docs/aep-import.md | 13 ++ 10 files changed, 347 insertions(+), 11 deletions(-) create mode 100644 crates/aep/tests/it/import/effects_thirdparty.rs diff --git a/crates/aep/src/container.rs b/crates/aep/src/container.rs index 4aa0f393a..0a4746f68 100644 --- a/crates/aep/src/container.rs +++ b/crates/aep/src/container.rs @@ -36,7 +36,7 @@ mod values; mod write; pub use aepx::{is_aepx, parse_any, read_aepx, read_aepx_with_limits}; -pub use arbitrary::{CURVE_IDENTITY, CurvesData, HueSaturationData, LevelsData, MeshData, curve_lut}; +pub use arbitrary::{CURVE_IDENTITY, CurvesData, ExtractorChannels, HueSaturationData, LevelsData, MeshData, curve_lut, identifier_channel_type}; pub use camera::{CameraPosition, DEFAULT_CAMERA_ZOOM}; pub use chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS, Walk}; pub use effects::{DefinitionSource, Effect, EffectDefinitions, EffectParam, LayerRef, ParamKind, ParamValue}; diff --git a/crates/aep/src/container/arbitrary.rs b/crates/aep/src/container/arbitrary.rs index dea70d22c..7b47e83c7 100644 --- a/crates/aep/src/container/arbitrary.rs +++ b/crates/aep/src/container/arbitrary.rs @@ -307,3 +307,54 @@ impl MeshData { Some(out) } } + +/// EXtractoR's Channel Info data (`EXtractoR-0013`, 688 bytes, the plug-in's own little-endian +/// layout; read from the default instances of the importer block of 2026-11-05, where no channel +/// was changed): a `u32` 1 and 28 bytes, then four records of 164 bytes (Red, Green, Blue, Alpha), +/// each a `u32` kind (1 a named channel, 2 the input's channel: `(copy)` on Alpha) and the channel +/// name from byte 35, NUL-terminated. Only the default (`R`, `G`, `B`, `(copy)`) is sampled. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ExtractorChannels { + /// Per output channel (red, green, blue, alpha): the kind and the name. + pub channels: [(u32, String); 4], +} + +impl ExtractorChannels { + /// The data's length. + pub const LEN: usize = 688; + const RECORD: usize = 164; + + /// `None` when the data does not have this layout. + pub fn parse(data: &[u8]) -> Option { + if data.len() != Self::LEN || data.get(..4)? != 1u32.to_le_bytes() { + return None; + } + let mut channels: [(u32, String); 4] = Default::default(); + for (k, slot) in channels.iter_mut().enumerate() { + let at = 32usize.checked_add(k.checked_mul(Self::RECORD)?)?; + let rec = data.get(at..at.checked_add(Self::RECORD)?)?; + let kind = u32::from_le_bytes(*rec.first_chunk::<4>()?); + let name = rec.get(35..)?; + let end = name.iter().position(|b| *b == 0)?; + let name = std::str::from_utf8(name.get(..end)?).ok()?; + *slot = (kind, name.to_string()); + } + Some(Self { channels }) + } + + /// The channels of a fresh EXtractoR: Red, Green and Blue from the channels of those names, + /// Alpha copied from the input. + pub fn is_default(&self) -> bool { + let want = [(1, "R"), (1, "G"), (1, "B"), (2, "(copy)")]; + self.channels.iter().zip(want).all(|((k, n), (wk, wn))| *k == wk && n == wn) + } +} + +/// IDentifier's Channel Info data (`IDentifier-0005`, 148 bytes): the channel type's name, +/// NUL-terminated from byte 19 (`Object ID` in every sample of the importer block of 2026-11-05; +/// the bytes around it differ between instances with the same settings and are not read). +pub fn identifier_channel_type(data: &[u8]) -> Option<&str> { + let s = data.get(19..)?; + let end = s.iter().position(|b| *b == 0)?; + std::str::from_utf8(s.get(..end)?).ok() +} diff --git a/crates/aep/src/container/effect_map.rs b/crates/aep/src/container/effect_map.rs index 5b872450f..46fcf39ea 100644 --- a/crates/aep/src/container/effect_map.rs +++ b/crates/aep/src/container/effect_map.rs @@ -138,6 +138,10 @@ pub enum Extra { Curves, /// Mesh Warp's Distortion Mesh (965, 985, `MeshData`). MeshWarp, + /// EXtractoR's Channel Info (`ExtractorChannels`): the default channels only. + Extractor, + /// IDentifier's Channel Info (`identifier_channel_type`): Object ID only. + Identifier, } /// One mapped effect. @@ -354,6 +358,8 @@ pub const SHUTTER_CONTROL: &[&str] = &["Manual", "Automatic"]; pub const ECHO_OPERATOR: &[&str] = &["Add", "Maximum", "Minimum", "Screen", "Composite In Back", "Composite In Front", "Blend"]; /// Color Stabilizer's Stabilize (`colstab-02`, `colstab-03`; the fresh default is Levels). pub const STABILIZE: &[&str] = &["Brightness", "Levels", "Curves"]; +/// IDentifier's Display (`identifier-04` ... `identifier-06`, `1300-thirdparty.aep`). +pub const IDENTIFIER_DISPLAY: &[&str] = &["Colors", "Luma Matte", "Alpha Matte", "Raw"]; /// The effects the importer converts. pub const EFFECTS: &[EffectMap] = &[ @@ -1066,6 +1072,40 @@ pub const EFFECTS: &[EffectMap] = &[ pm(5, K::FixedSlider, "sampleSize", Conv::Plain { valid_max: 50.0 }), ], ), + // CC Color Neutralizer (importer block 2026-11-05, `ccneutral-*`): Pinning and Blend w. + // Original are float sliders in percent (valid 0..100). After Effects' correction is the Red / + // Green / Blue slider of each range (3-5, 9-11, 15-17; the Unbalance colours are pickers that + // set them and keep no record), then View (22) and Black / White Point (23, 24): + // EffectCraft's CC Color Neutralizer has none of them, so they are reported when stored. + fx( + "CS Color Neutralizer", + "ec.color.cccolorneutralizer", + &[pm(19, K::FloatSlider, "pinning", Conv::Plain { valid_max: 100.0 }), pm(20, K::FloatSlider, "blendWOriginal", Conv::Plain { valid_max: 100.0 })], + ), + // EXtractoR (`extractor-*`): 1 Channel Info (arbitrary data, `Extra::Extractor`), 2 / 6 the + // Process group, Black and White Point float sliders (valid ±3.4e38, the float32 range), + // UnMult. EffectCraft's Clip has no counterpart and stays off. + EffectMap { + match_name: "EXtractoR", + effect: "ec.3d.extractor", + params: &[ + pm(3, K::FloatSlider, "blackPoint", Conv::Plain { valid_max: F32_MAX }), + pm(4, K::FloatSlider, "whitePoint", Conv::Plain { valid_max: F32_MAX }), + pm(5, K::Checkbox, "unMult", Conv::Checkbox), + ], + extra: Extra::Extractor, + }, + // IDentifier (`identifier-*`): 1 Channel Info (arbitrary data, `Extra::Identifier`), 2 Display, + // 3 ID an integer slider (valid 0..2 147 483 647). + EffectMap { + match_name: "IDentifier", + effect: "ec.3d.identifier", + params: &[ + pm(2, K::Popup, "display", Conv::Popup { default: 1, items: IDENTIFIER_DISPLAY }), + pm(3, K::IntSlider, "id", Conv::Plain { valid_max: 2_147_483_647.0 }), + ], + extra: Extra::Identifier, + }, // Layer Control (076, 220, 320, 321): the default is the owning layer, None is unset. fx("ADBE Layer Control", "ec.control.layer", &[pm(1, K::Layer, "layer", Conv::LayerRef { own: true })]), fx("ADBE Slider Control", "ec.control.slider", &[pm(1, K::FloatSlider, "slider", Conv::Plain { valid_max: 1_000_000.0 })]), @@ -1079,6 +1119,8 @@ pub const EFFECTS: &[EffectMap] = &[ pub const EXPOSURE: Conv = Conv::Plain { valid_max: 100.0 }; pub const OFFSET: Conv = Conv::Plain { valid_max: 2.0 }; pub const GAMMA: Conv = Conv::Plain { valid_max: 10.0 }; +/// The largest float32, the valid maximum of EXtractoR's Black and White Point. +pub const F32_MAX: f64 = f32::MAX as f64; /// The valid maximum of Glow's A & B Midpoint (16.16 of 0.99, 427). pub const GLOW_MIDPOINT_MAX: f64 = 0.990_005_493_164_062_5; diff --git a/crates/aep/src/import/effects.rs b/crates/aep/src/import/effects.rs index 1d20a9dd1..a9fa3c2c7 100644 --- a/crates/aep/src/import/effects.rs +++ b/crates/aep/src/import/effects.rs @@ -36,8 +36,8 @@ use crate::effect_map::{Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, popup_key}; use crate::{ - CurvesData, DefinitionSource, Effect as AepEffect, EffectDefinitions, EffectParam, HueSaturationData, Layer as AepLayer, LayerRef, LevelsData, MeshData, - ParamKind, ParamValue, + CurvesData, DefinitionSource, Effect as AepEffect, EffectDefinitions, EffectParam, ExtractorChannels, HueSaturationData, Layer as AepLayer, LayerRef, + LevelsData, MeshData, ParamKind, ParamValue, }; use effectcraft_keyframe::Value; use effectcraft_project::build::Ids; @@ -465,6 +465,40 @@ fn import_extra(fx: &AepEffect<'_, '_>, extra: Extra, p: &EffectParam<'_, '_>, h Some(m) => import_mesh(&m, here, size, group, col), None => col.lose(LossCategory::DamagedData, format!("{here}: the Distortion Mesh is unreadable, defaults used")), }, + Extra::Extractor => match ExtractorChannels::parse(data) { + // The fresh channels: EffectCraft's own R, G, B, A (its A is the input's alpha, After + // Effects' `(copy)`). + Some(c) if c.is_default() => { + for (id, name) in [("red", "R"), ("green", "G"), ("blue", "B"), ("alpha", "A")] { + if let Some(prop) = group.prop_mut(id) { + prop.value = Value::Str(name.into()); + } + } + } + Some(c) => { + let names: Vec<&str> = c.channels.iter().map(|(_, n)| n.as_str()).collect(); + col.lose( + LossCategory::Effects, + format!("{here}: the channels {} are not imported (only the default channels are sampled), R, G, B, A used", names.join(", ")), + ); + } + None => col.lose(LossCategory::Effects, format!("{here}: the Channel Info data is not in the sampled layout, R, G, B, A used")), + }, + Extra::Identifier => match crate::identifier_channel_type(data) { + Some("Object ID") => { + if let Some(prop) = group.prop_mut("channelType") { + prop.value = Value::Enum(0); + } + // The data names the type, not the channel After Effects reads: with this Channel + // Info After Effects left the sampled EXR unchanged, where EffectCraft draws the + // footage's object ID channel. + col.lose( + LossCategory::Effects, + format!("{here}: the channel IDentifier reads (Channel Info) is not decoded; EffectCraft uses the footage's object ID channel"), + ); + } + other => col.lose(LossCategory::Effects, format!("{here}: the channel type {other:?} is not imported (only Object ID is sampled), Object ID used")), + }, Extra::None => {} } } diff --git a/crates/aep/src/lib.rs b/crates/aep/src/lib.rs index c8b37d716..dba3136ee 100644 --- a/crates/aep/src/lib.rs +++ b/crates/aep/src/lib.rs @@ -14,11 +14,11 @@ pub mod import; pub use container::{ AepError, AepFile, AutoKern, BlendMode, Body, CURVE_IDENTITY, CameraPosition, Caps, Chunk, Comp, CompHeader, CueType, CurvesData, DEFAULT_CAMERA_ZOOM, - DefinitionSource, Effect, EffectDefinitions, EffectParam, ExpressionFlags, FontBaseline, Footage, GradientType, HueSaturationData, Interpolation, Item, - ItemKind, Justification, KernRun, KeyEase, KeyLayout, KeyList, Keyframe, KeyframeKind, LIST, Layer, LayerHeader, LayerRef, LayerType, LevelsData, - LightType, Limits, ListHeader, Marker, Mask, MaskAtom, MaskMode, MatteMode, MeshData, NESTED_IDS, Orientation, OrientationKey, ParagraphRun, ParamKind, - ParamValue, PathVertex, PolystarType, Project, ProjectSettings, PropEntry, PropGroup, PropNode, Property, Quality, Rational, Renderer, SequenceInfo, - ShapeDetail, ShapeGroup, ShapeItem, ShapePath, SpatialKey, Stroke, StrokeCap, StrokeJoin, StyleRun, TextBox, TextDocument, TextValue, TimeBase, - TimeDisplay, TimeRemap, TimeRemapKey, TrackMatte, Tracked, Transform, ValueSource, Walk, WorkingSpace, curve_lut, effect_map, is_aepx, parse, parse_any, - parse_text_dict, parse_with_limits, read_aepx, read_aepx_with_limits, style_map, write, write_to, + DefinitionSource, Effect, EffectDefinitions, EffectParam, ExpressionFlags, ExtractorChannels, FontBaseline, Footage, GradientType, HueSaturationData, + Interpolation, Item, ItemKind, Justification, KernRun, KeyEase, KeyLayout, KeyList, Keyframe, KeyframeKind, LIST, Layer, LayerHeader, LayerRef, LayerType, + LevelsData, LightType, Limits, ListHeader, Marker, Mask, MaskAtom, MaskMode, MatteMode, MeshData, NESTED_IDS, Orientation, OrientationKey, ParagraphRun, + ParamKind, ParamValue, PathVertex, PolystarType, Project, ProjectSettings, PropEntry, PropGroup, PropNode, Property, Quality, Rational, Renderer, + SequenceInfo, ShapeDetail, ShapeGroup, ShapeItem, ShapePath, SpatialKey, Stroke, StrokeCap, StrokeJoin, StyleRun, TextBox, TextDocument, TextValue, + TimeBase, TimeDisplay, TimeRemap, TimeRemapKey, TrackMatte, Tracked, Transform, ValueSource, Walk, WorkingSpace, curve_lut, effect_map, + identifier_channel_type, is_aepx, parse, parse_any, parse_text_dict, parse_with_limits, read_aepx, read_aepx_with_limits, style_map, write, write_to, }; diff --git a/crates/aep/tests/it/import.rs b/crates/aep/tests/it/import.rs index 178dd2bb3..a24f9af26 100644 --- a/crates/aep/tests/it/import.rs +++ b/crates/aep/tests/it/import.rs @@ -14,6 +14,7 @@ mod effects_batch5; mod effects_quickwins; mod effects_round2; mod effects_round3; +mod effects_thirdparty; mod fx_inject; mod markers; mod masks; diff --git a/crates/aep/tests/it/import/effects.rs b/crates/aep/tests/it/import/effects.rs index aa4180567..fbb28b406 100644 --- a/crates/aep/tests/it/import/effects.rs +++ b/crates/aep/tests/it/import/effects.rs @@ -72,7 +72,7 @@ fn the_table_names_tint_gradient_ramp_and_exposure() { ] { assert!(t.contains(&want), "{want:?}"); } - assert_eq!(t.len(), 62); + assert_eq!(t.len(), 65); for (_, id) in t { assert!(effectcraft_effects::find(id).is_some(), "{id}"); } diff --git a/crates/aep/tests/it/import/effects_thirdparty.rs b/crates/aep/tests/it/import/effects_thirdparty.rs new file mode 100644 index 000000000..20f0b8230 --- /dev/null +++ b/crates/aep/tests/it/import/effects_thirdparty.rs @@ -0,0 +1,179 @@ +//! CC Color Neutralizer, EXtractoR and IDentifier (#3112) arrive as EffectCraft's effects of the +//! same name. Each is built into `synthetic.aep` (`fx_inject`) with the definitions After Effects +//! 2026 wrote for it in the importer block of 2026-11-05 (`1300-thirdparty.aep`) and the values +//! of that block's compositions; the Channel Info data is laid out as those instances store it. + +use crate::import::fx_inject::{Default, Fx, losses, near, other, popup, run, slider, v}; +use effectcraft_aep::import::LossCategory; +use effectcraft_aep::{ExtractorChannels, ParamKind as K, identifier_channel_type}; +use effectcraft_keyframe::Value; + +fn neutralizer(values: Vec<(usize, f64)>) -> Fx { + let range = |name| slider(K::FloatSlider, name, 0.0, (-100.0, 100.0)); + let group = |name| other(K::GroupStart, name, Default::None); + let end = || other(K::GroupEnd, "", Default::None); + Fx { + match_name: "CS Color Neutralizer", + name: "CC Color Neutralizer", + defs: vec![ + other(K::Color, "Shadows Unbalance", Default::Color([0, 0, 0, 0])), + group("Shadows"), + range("Red - Shadows"), + range("Green - Shadows"), + range("Blue - Shadows"), + end(), + other(K::Color, "Midtones Unbalance", Default::Color([255, 127, 127, 127])), + group("Midtones"), + range("Red - Midtones"), + range("Green - Midtones"), + range("Blue - Midtones"), + end(), + other(K::Color, "Highlights Unbalance", Default::Color([255, 255, 255, 255])), + group("Highlights"), + range("Red - Highlights"), + range("Green - Highlights"), + range("Blue - Highlights"), + end(), + slider(K::FloatSlider, "Pinning", 0.0, (0.0, 100.0)), + slider(K::FloatSlider, "Blend w. Original", 0.0, (0.0, 100.0)), + group("Special"), + popup("View", 1, &["Final Result", "Correction B to W", "Correction Stair"]), + slider(K::FloatSlider, "Black Point", 0.0, (0.0, 49.9)), + slider(K::FloatSlider, "White Point", 100.0, (50.1, 100.0)), + end(), + ], + values, + data: vec![], + } +} + +#[test] +fn cc_color_neutralizer_pinning_and_blend() { + // `ccneutral-00-defaults` and the Pinning / Blend of `ccneutral-01-set-all`. + let (g, r) = run(&neutralizer(vec![(19, 16.0), (20, 19.0)]), "ec.color.cccolorneutralizer"); + assert_eq!(losses(&r, LossCategory::Effects, "CS Color Neutralizer"), Vec::::new()); + let g = g.expect("imported"); + near(&g, "pinning", 16.0); + near(&g, "blendWOriginal", 19.0); + // EffectCraft's own (identity) balance stays. + assert_eq!(v(&g, "midtonesUnbalance"), v(&g, "midtonesBalance")); +} + +#[test] +fn cc_color_neutralizer_corrections_without_a_counterpart_are_reported() { + // `ccneutral-03-sec-shadows`, `ccneutral-09-pop22-2`, `ccneutral-06-sec-special`. + let (g, r) = run(&neutralizer(vec![(3, 13.0), (4, 16.0), (5, 19.0), (22, 2.0), (23, 10.0), (24, 87.0)]), "ec.color.cccolorneutralizer"); + let found = losses(&r, LossCategory::Effects, "CS Color Neutralizer"); + for name in ["Red - Shadows", "Green - Shadows", "Blue - Shadows", "View", "Black Point", "White Point"] { + assert!(found.iter().any(|d| d.contains(&format!("parameter {name} (")) && d.contains("not imported")), "{name}: {found:?}"); + } + assert_eq!(found.len(), 6, "{found:?}"); + assert!(g.is_some(), "the effect itself is imported"); +} + +/// EXtractoR's Channel Info data with these record kinds and names. +fn channels(recs: [(u32, &str); 4]) -> Vec { + let mut d = vec![0u8; ExtractorChannels::LEN]; + d[..4].copy_from_slice(&1u32.to_le_bytes()); + for (k, (kind, name)) in recs.iter().enumerate() { + let at = 32 + k * 164; + d[at..at + 4].copy_from_slice(&kind.to_le_bytes()); + d[at + 35..at + 35 + name.len()].copy_from_slice(name.as_bytes()); + } + d +} + +const DEFAULT_CHANNELS: [(u32, &str); 4] = [(1, "R"), (1, "G"), (1, "B"), (2, "(copy)")]; + +fn extractor(values: Vec<(usize, f64)>, data: Vec) -> Fx { + let mut values = values; + values.insert(0, (1, 0.0)); + Fx { + match_name: "EXtractoR", + name: "EXtractoR", + defs: vec![ + other(K::ArbitraryData, "Channel Info", Default::None), + other(K::GroupStart, "Process", Default::None), + slider(K::FloatSlider, "Black Point", 0.0, (-f64::from(f32::MAX), f64::from(f32::MAX))), + slider(K::FloatSlider, "White Point", 1.0, (-f64::from(f32::MAX), f64::from(f32::MAX))), + other(K::Checkbox, "", Default::Number(0.0)), + other(K::GroupEnd, "", Default::None), + ], + values, + data: vec![(1, data)], + } +} + +#[test] +fn extractor_channel_info_layout() { + let c = ExtractorChannels::parse(&channels(DEFAULT_CHANNELS)).expect("the default layout"); + assert!(c.is_default()); + let c = ExtractorChannels::parse(&channels([(1, "Z"), (1, "G"), (1, "B"), (2, "(copy)")])).expect("parses"); + assert!(!c.is_default()); + assert_eq!(c.channels[0], (1, "Z".to_string())); + assert_eq!(ExtractorChannels::parse(&[0u8; 10]), None); +} + +#[test] +fn extractor_points_unmult_and_default_channels() { + // `extractor-01-set-all`: Black Point 10, White Point 14, UnMult on. + let (g, r) = run(&extractor(vec![(3, 10.0), (4, 14.0), (5, 1.0)], channels(DEFAULT_CHANNELS)), "ec.3d.extractor"); + assert_eq!(losses(&r, LossCategory::Effects, "EXtractoR"), Vec::::new()); + let g = g.expect("imported"); + near(&g, "blackPoint", 10.0); + near(&g, "whitePoint", 14.0); + assert_eq!((v(&g, "unMult"), v(&g, "clip")), (Value::Bool(true), Value::Bool(false))); + // After Effects' `(copy)` Alpha is the input's alpha: EffectCraft's A. + for (id, want) in [("red", "R"), ("green", "G"), ("blue", "B"), ("alpha", "A")] { + assert_eq!(v(&g, id), Value::Str(want.into()), "{id}"); + } +} + +#[test] +fn extractor_channels_other_than_the_default_are_reported() { + let (g, r) = run(&extractor(vec![], channels([(1, "Z"), (1, "Z"), (1, "Z"), (2, "(copy)")])), "ec.3d.extractor"); + let found = losses(&r, LossCategory::Effects, "EXtractoR"); + assert!(found.len() == 1 && found[0].contains("the channels Z, Z, Z, (copy) are not imported"), "{found:?}"); + assert_eq!(v(&g.expect("imported"), "red"), Value::Str("R".into())); +} + +/// IDentifier's Channel Info: the channel type's name at 19 among bytes that are not read. +fn id_info(name: &str) -> Vec { + let mut d: Vec = (0..148u32).map(|i| (i * 37 % 251) as u8 | 1).collect(); + d[19..19 + name.len()].copy_from_slice(name.as_bytes()); + d[19 + name.len()] = 0; + d +} + +fn identifier(values: Vec<(usize, f64)>, data: Vec) -> Fx { + let mut values = values; + values.insert(0, (1, 0.0)); + Fx { + match_name: "IDentifier", + name: "IDentifier", + defs: vec![ + other(K::ArbitraryData, "Channel Info", Default::None), + popup("Display", 1, &["Colors", "Luma Matte", "Alpha Matte", "Raw"]), + slider(K::IntSlider, "ID", 0.0, (0.0, 2_147_483_647.0)), + ], + values, + data: vec![(1, data)], + } +} + +#[test] +fn identifier_display_id_and_channel_type() { + assert_eq!(identifier_channel_type(&id_info("Object ID")), Some("Object ID")); + // `identifier-01-set-all` (ID 10) and `identifier-05-pop2-3` (Alpha Matte). + let (g, r) = run(&identifier(vec![(2, 3.0), (3, 10.0)], id_info("Object ID")), "ec.3d.identifier"); + // Which channel After Effects reads is not decoded (it left the sampled EXR unchanged): reported. + let found = losses(&r, LossCategory::Effects, "IDentifier"); + assert!(found.len() == 1 && found[0].contains("is not decoded"), "{found:?}"); + let g = g.expect("imported"); + assert_eq!((v(&g, "display"), v(&g, "channelType")), (Value::Enum(2), Value::Enum(0))); + near(&g, "id", 10.0); + // A channel type that is not sampled is reported. + let (_, r) = run(&identifier(vec![], id_info("Material ID")), "ec.3d.identifier"); + let found = losses(&r, LossCategory::Effects, "IDentifier"); + assert!(found.len() == 1 && found[0].contains("\"Material ID\""), "{found:?}"); +} diff --git a/docs/aep-format.md b/docs/aep-format.md index 32ec74854..29ee51963 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -533,6 +533,22 @@ compositions, the items from one composition per popup item).** Each set value i | `ADBE Offset` | 1 Shift Center To: point, default the layer centre; 2 Blend With Original: fixed, **max 1 (a 0..1 fraction)**, default 0 | | `ADBE Deflicker` (Color Stabilizer) | 1 Stabilize: popup (Brightness, Levels, Curves), **default 2 (Levels)**; 2 Black Point, 3 Mid Point, 4 White Point: points, defaults 0.2 / 0.5 / 0.8 of the width at half the height; 5 Sample Size: fixed, max 50, default 3. The reference frame the Set Frame button picks is not among the parameters | +**Importer block 2026-11-05, #3112 (`1300-thirdparty.aep`; V for the parameter lists, H for the +data layouts, read from default instances only):** + +| Effect (match name, plug-in version) | Parameters (`parT` index: type, valid range, default) | +|---|---| +| `CS Color Neutralizer` (CC Color Neutralizer, 1.3) | 1 / 7 / 13 Shadows / Midtones / Highlights Unbalance: colours (black, 127 grey, white; pickers: setting one sets the range's sliders, e.g. [0.27, 0.55, 0.85] gives Red 42.94, Green 1.18, Blue -44.12 = (mean - channel) x 150 / 255 on 8-bit values, and leaves no colour record); 2-6, 8-12, 14-18 a group per range (start, Red, Green, Blue, end) with float sliders -100..100 (slider -50..50), default 0; 19 Pinning: float 0..100 (slider 0..50); 20 Blend w. Original: float 0..100; 21-25 the Special group: 22 View popup (Final Result, Correction B to W, Correction Stair), default 1, 23 Black Point float 0..49.9, default 0, 24 White Point float 50.1..100, default 100 | +| `EXtractoR` (2.6.6) | 1 Channel Info (`EXtractoR-0013`): arbitrary data, 688 bytes, below; 2-6 the Process group: 3 Black Point and 4 White Point float sliders, valid ±3.4028235e38 (the float32 range; slider ±1 000), default 0 / 1; 5 UnMult: checkbox (empty `pard` name, the label in `pdnm`), default off | +| `IDentifier` (2.6.6) | 1 Channel Info (`IDentifier-0005`): arbitrary data, 148 bytes, below; 2 Display: popup (Colors, Luma Matte, Alpha Matte, Raw), default 1; 3 ID: integer slider, max 2 147 483 647, default 0 | + +EXtractoR's Channel Info is the plug-in's own **little-endian** layout: `u32` 1 and 28 zero bytes, +then four 164-byte records (Red, Green, Blue, Alpha), each a `u32` kind (1 in R, G, B; 2 in Alpha, +whose name is `(copy)`) and the channel name NUL-terminated from record byte 35 (`R`, `G`, `B`). +The data is the same in every composition of the block. IDentifier's data differs between +instances with the same settings except for the channel type's name, NUL-terminated from byte 19 +(`Object ID`); the other bytes are not read. API: `ExtractorChannels`, `identifier_channel_type`. + **Curves and Mesh Warp** (961-965, 985; applied from presets, the data is not scriptable): their arbitrary-data parameter (type 11) always has a record (`tdbs` with a 4-byte `cdat`) and a `LIST aRbs` / `aRbp`, also at the default. Curves (`ADBE CurvesCustom-0001`, 1644 bytes): `u16` 1, 1; diff --git a/docs/aep-import.md b/docs/aep-import.md index 1926274f0..f14141156 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -179,6 +179,7 @@ parameter gets the file's value: | `ADBE Easy Levels2` 1 Channel, 2 Histogram data, 8 / 9 Clip To Output Black / White | `channel`; `inBlack`, `inWhite`, `gamma`, `outBlack`, `outWhite` from the data; `clipToOutputBlack`, `clipToOutputWhite` | popups by name (After Effects' items On, Off, Off for 32 bpc Color are in another order here); the RGB values are the `f32` of the arbitrary data; the per-channel values are reported when they are not at their defaults (not sampled) | 511-516 | | Batch 6 (903 defaults, 904 / 905 set values): `ADBE Brightness & Contrast 2`, `ADBE Turbulent Displace`, `ADBE Fractal Noise`, `ADBE Laser` (Beam), `CC Radial Fast Blur`, `ADBE Noise2`, `ADBE Pro Levels2` (Levels (Individual Controls)), `ADBE Corner Pin`, `ADBE Grid`, `ADBE Sharpen`, `ADBE Linear Color Key2`, `ADBE Radial Wipe`, `ADBE Remove Color Matting`, `CC Bend It`, `CC Power Pin`, `ADBE 4ColorGradient`, `ADBE Change Color`, `ADBE Extract`, `ADBE Noise HLS2`, `ADBE Scatter`, `CC Light Sweep`, `ADBE Compound Blur`, `ADBE Displacement Map`, `ADBE Set Matte3`, `ADBE Gaussian Blur` (legacy), `ADBE OFMotionBlur` (Pixel Motion Blur), `ADBE Layer Control` | the EffectCraft effect of the same name, every parameter EffectCraft has | sliders as stored (each checked against its valid maximum); Beam Length, Time and Softness and CC Power Pin Perspective are 0..1 fractions, percentages here; points as layer pixels; popups by item name (Set Matte's `Red Channel` ... is `Red` ...); twirl-down group markers and the Levels histogram carry nothing; **layer parameters** (below) | 903, 904, 905; 220, 320, 321 | | Importer block 2026-11-05 (`1100-quickwins.aep`: per effect defaults, set values, minimum and maximum, every popup item, a keyed and an expression parameter): `ADBE HDR ToneMap` (HDR Highlight Compression) 1 Amount; `ADBE Echo` 1 Echo Time, 2 Number Of Echoes, 3 Starting Intensity, 4 Decay, 5 Echo Operator; `ADBE Linear Wipe` 1 Transition Completion, 2 Wipe Angle, 3 Feather; `ADBE Offset` 1 Shift Center To, 2 Blend With Original; `ADBE Deflicker` (Color Stabilizer) 1 Stabilize, 2-4 Black / Mid / White Point, 5 Sample Size | `ec.utility.hdrcompression` `amount`; `ec.time.echo` `echoTime`, `numberOfEchoes`, `startingIntensity`, `decay`, `echoOperator`; `ec.transition.linearwipe` `completion`, `angle`, `feather`; `ec.distort.offset` `shift`, `blend`; `ec.color.colorstabilizer` `stabilize`, `blackPoint`, `midPoint`, `whitePoint`, `sampleSize` | percent (float, valid 0..100, default 100); seconds (float, valid ±30 000), integer (valid 30 000), 0..1 (float), Decay as stored (float, valid 0..100), popup by name: EffectCraft's Echo uses at most 255 echoes and a Decay up to 1, a value or key beyond that is imported as stored and **reported**; percent (fixed, 100), degrees, pixels (fixed, 32 000); point in layer pixels, Blend 0..1 x 100 = percent; popup by name (**fresh default Levels**), points in layer pixels, Sample Size as stored (fixed, valid 50, default 3; its unit, pixels here, is not verified). Color Stabilizer's reference frame (Set Frame) is not a parameter in the file and is not imported (EffectCraft's Reference Frame stays 0 s) | `tests/it/import/effects_quickwins.rs`; render comparison below | +| Importer block 2026-11-05 (`1300-thirdparty.aep`), plug-ins that ship with After Effects: `CS Color Neutralizer` (CC Color Neutralizer) 19 Pinning, 20 Blend w. Original; `EXtractoR` 1 Channel Info data, 3 Black Point, 4 White Point, 5 UnMult; `IDentifier` 1 Channel Info data, 2 Display, 3 ID | `ec.color.cccolorneutralizer` `pinning`, `blendWOriginal`; `ec.3d.extractor` `red`, `green`, `blue`, `alpha`, `blackPoint`, `whitePoint`, `unMult`; `ec.3d.identifier` `channelType`, `display`, `id` | CC Color Neutralizer: percent (float, valid 100); its correction is the Red / Green / Blue slider of each range (the Unbalance colours are pickers that set them and keep no record), with View and Black / White Point: **EffectCraft's effect has no counterpart, each is reported when stored** (EffectCraft's Unbalance / Balance stay at their identity defaults). EXtractoR: the default channels (R, G, B, `(copy)`) are EffectCraft's R, G, B, A, other channels are reported (`ExtractorChannels`; only the default is sampled), Black and White Point as stored (float, valid ±3.4e38; a value beyond ±1e9 is reported as damaged data, as for every number), UnMult a checkbox, Clip stays off. IDentifier: Display by name, ID as stored (integer, valid 2 147 483 647); the Channel Info names only the channel type (Object ID, `identifier_channel_type`; another is reported), not the channel After Effects reads, so **every instance is reported**: EffectCraft draws the footage's object ID channel where After Effects left the sampled EXR unchanged | `tests/it/import/effects_thirdparty.rs`; render comparison below | | `ADBE Slider Control`, `ADBE Angle Control`, `ADBE Checkbox Control`, `ADBE Color Control`, `ADBE Point Control` (parameter 1) | `ec.control.slider` `slider`, `ec.control.angle` `angle`, `ec.control.checkbox` `checkbox`, `ec.control.color` `color`, `ec.control.point` `point` | float (valid max 1 000 000) as stored; degrees; checkbox; colour; point in layer pixels | 071, 073, 217-219, 221, 304, 310 | - **Nothing to import:** the implicit input-layer parameter (index 0), the Swap Colors buttons @@ -325,6 +326,18 @@ frame blending, 3D material options, audio levels, guides, the mask colour, lock over 25 (EffectCraft's samples drift, After Effects' frame stays unchanged) and with keyed sample points. The renders do not exercise a Decay above 1 (After Effects' set value has Starting Intensity 0) or what Color Stabilizer corrects (the test card does not flicker). +- `crates/aep/tests/it/import/effects_thirdparty.rs` (#3112): CC Color Neutralizer, EXtractoR and + IDentifier built into `synthetic.aep` like the #3110 effects. Render check of the 29 compositions + of `1300-thirdparty.aep` (as above): CC Color Neutralizer at its defaults and the minimum and + maximum compositions match within one level (no composition isolates Pinning: the maximum has + Blend w. Original 100, which shows the original), the compositions with reported Red / + Green / Blue corrections or View differ as reported (up to 5 levels, View 25); EXtractoR at its + defaults and with UnMult within 3 levels on 6 % of the pixels, keyed and expression Black Point + within one level, the ±3.4e38 compositions differ (reported as damaged data). IDentifier: After + Effects' frame is the unchanged EXR in every variant (its default Object ID finds no channel in + that EXR), while EffectCraft's IDentifier finds the EXR's `ID` channel and draws it; the import + reports this for every IDentifier. No sample changes EXtractoR's channels or IDentifier's channel + type (the presets for that were missing from the block). - `crates/aep/tests/it/import/combined.rs`: masks and effects on the same layer, an effect on a text layer, and the same through `.aepx` (built in-test from the fixtures: the effect parade of one fixture grafted onto the layer of another). From ce3a5b3673a17dd0c766c322c19f551ad9eae9a3 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 15:23:55 +0100 Subject: [PATCH 014/350] AEP import: map Lumetri Color (ADBE Lumetri) to EffectCraft's Lumetri Color The importer now converts After Effects' Lumetri Color. Its parameters are found by match-name suffix, not by parT index: After Effects inserted -0127 ... -0130 mid-list and five tone sliders share one valid range. - Values are read from every value record, also those in the default state (tdsb 3), where Lumetri keeps High Dynamic Range, HDR Specular and the second Look popup (EffectParam::recorded_value; stored_value is unchanged). - A switched-off section, and HSL Secondary without a picked key colour, draws nothing in After Effects: its set values are reported in one line and not imported. - The HSL key data is decoded into hue / saturation / lightness ranges. - LUTs and Looks (by name), controls without a counterpart, eyedropper colours and undecoded curves, wheels and Split Toning are reported. - EffectCraft's Lumetri sliders take After Effects' valid ranges. - import_param is split into lookup plus import_mapped for reuse. Evidence: 1200 samples (After Effects 2026). The 62 read-backs of mapped controls in sections that act equal the imported values. The other 61 lie in sections that do nothing and are reported. Renders of identity grades are within 1/255. Part of #3111 Co-Authored-By: Claude Opus 5.5 --- crates/aep/src/container/effect_map.rs | 6 + crates/aep/src/container/effects.rs | 15 +- crates/aep/src/import.rs | 1 + crates/aep/src/import/effects.rs | 56 ++- crates/aep/src/import/lumetri.rs | 642 +++++++++++++++++++++++++ crates/aep/tests/it/import.rs | 1 + crates/aep/tests/it/import/effects.rs | 2 +- crates/aep/tests/it/import/lumetri.rs | 448 +++++++++++++++++ crates/effects/src/color3.rs | 41 +- docs/aep-format.md | 38 ++ docs/aep-import.md | 29 +- docs/file-format-parity.md | 2 +- 12 files changed, 1244 insertions(+), 37 deletions(-) create mode 100644 crates/aep/src/import/lumetri.rs create mode 100644 crates/aep/tests/it/import/lumetri.rs diff --git a/crates/aep/src/container/effect_map.rs b/crates/aep/src/container/effect_map.rs index a7c9bbd48..312332e8f 100644 --- a/crates/aep/src/container/effect_map.rs +++ b/crates/aep/src/container/effect_map.rs @@ -134,6 +134,9 @@ pub enum Extra { Curves, /// Mesh Warp's Distortion Mesh (965, 985, `MeshData`). MeshWarp, + /// Lumetri Color: every parameter goes through its own table, keyed by match name + /// (`import/lumetri.rs`, 1200). + Lumetri, } /// One mapped effect. @@ -509,6 +512,9 @@ pub const EFFECTS: &[EffectMap] = &[ ], extra: Extra::MeshWarp, }, + // Lumetri Color (1200): the parameters are mapped by match name, not by `parT` index + // (`import/lumetri.rs`). + EffectMap { match_name: "ADBE Lumetri", effect: "ec.color.lumetri", params: &[], extra: Extra::Lumetri }, // Expression controls (070-077, 217-221, 310). // Directional Blur (711): Direction in degrees, Blur Length in pixels (valid max 1 000). fx( diff --git a/crates/aep/src/container/effects.rs b/crates/aep/src/container/effects.rs index df1c2b9c1..597710587 100644 --- a/crates/aep/src/container/effects.rs +++ b/crates/aep/src/container/effects.rs @@ -375,11 +375,24 @@ impl<'f, 'a> EffectParam<'f, 'a> { /// colours, popups and layer controls. A record counts when its `tdsb` is 1 and it has no /// keyframes; a layer control reads the `tdpi` chunk; a button has no value. pub fn stored_value(&self) -> Option { + let p = self.stored?; + if self.kind != ParamKind::Layer && p.flags() != Some(1) { + return None; + } + self.recorded_value() + } + + /// The value in the value record whatever the record's state (`tdsb`): what + /// [`EffectParam::stored_value`] reads, also from a record in the default state (`tdsb` 3). + /// Lumetri Color writes the values of its hidden parameters so (High Dynamic Range, HDR + /// Specular, Look: 1200 `lumetri-01-set-all`, `-02-sec-top`, `-06-sec-light`; After Effects + /// reads back the recorded values). `None` with keyframes or without a record. + pub fn recorded_value(&self) -> Option { let p = self.stored?; if self.kind == ParamKind::Layer { return be_u32(p.chunk().child(b"tdpi")?.bytes()?, 0).map(ParamValue::Layer); } - if p.flags() != Some(1) || p.is_animated() { + if p.is_animated() { return None; } let v = p.static_value()?; diff --git a/crates/aep/src/import.rs b/crates/aep/src/import.rs index 1ffe3ec48..eb4eee8c8 100644 --- a/crates/aep/src/import.rs +++ b/crates/aep/src/import.rs @@ -27,6 +27,7 @@ mod build; mod camera; mod effects; +mod lumetri; mod markers; mod masks; mod props; diff --git a/crates/aep/src/import/effects.rs b/crates/aep/src/import/effects.rs index 477a25df4..2ef912e00 100644 --- a/crates/aep/src/import/effects.rs +++ b/crates/aep/src/import/effects.rs @@ -34,7 +34,7 @@ //! import. Hue/Saturation's master values and Levels' values are arbitrary data, decoded by the //! reader (444-446, 512-516). -use crate::effect_map::{Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, popup_key}; +use crate::effect_map::{Conv, EFFECTS, EffectMap, Extra, HUE_SATURATION_MASTER, LEVELS_RGB, ParamMap, popup_key}; use crate::{ CurvesData, DefinitionSource, Effect as AepEffect, EffectDefinitions, EffectParam, HueSaturationData, Layer as AepLayer, LayerRef, LevelsData, MeshData, ParamKind, ParamValue, @@ -99,8 +99,12 @@ pub(crate) fn import_effects<'f, 'a>( None => col.lose(LossCategory::DamagedData, format!("{}: '{}': the fx switch of '{shown}' is unreadable, imported switched on", cx.comp, cx.layer)), } let params = defs.params(&fx); - for p in ¶ms { - import_param(&fx, map, p, cx, &shown, ParamOut { group: &mut group, refs: &mut *refs, col: &mut *col }); + if map.extra == Extra::Lumetri { + crate::import::lumetri::import_lumetri(&fx, ¶ms, cx, &shown, ParamOut { group: &mut group, refs: &mut *refs, col: &mut *col }); + } else { + for p in ¶ms { + import_param(&fx, map, p, cx, &shown, ParamOut { group: &mut group, refs: &mut *refs, col: &mut *col }); + } } col.imported.effects = col.imported.effects.saturating_add(1); target.children.push(Node::Group(group)); @@ -108,7 +112,7 @@ pub(crate) fn import_effects<'f, 'a>( } /// Parameters that never carry a value to import. -fn carries_nothing(p: &EffectParam<'_, '_>) -> bool { +pub(crate) fn carries_nothing(p: &EffectParam<'_, '_>) -> bool { matches!(p.kind, ParamKind::Button | ParamKind::NoData | ParamKind::GroupStart | ParamKind::GroupEnd) || (p.index == 0 && p.kind == ParamKind::Layer) } @@ -127,10 +131,15 @@ pub(crate) struct PendingLayerRef { /// Where a parameter is imported to: the effect's group, the layer parameters still to resolve /// and the report. -struct ParamOut<'a> { - group: &'a mut PropGroup, - refs: &'a mut Vec, - col: &'a mut Collector, +pub(crate) struct ParamOut<'a> { + pub group: &'a mut PropGroup, + pub refs: &'a mut Vec, + pub col: &'a mut Collector, +} + +/// How a parameter is named in the report: its name and match name. +pub(crate) fn param_label(p: &EffectParam<'_, '_>) -> String { + if p.name.trim().is_empty() { p.match_name.clone() } else { format!("{} ({})", p.name.trim(), p.match_name) } } fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_>, cx: &LayerCtx<'_>, shown: &str, out: ParamOut<'_>) { @@ -138,7 +147,7 @@ fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_> if carries_nothing(p) { return; } - let label = if p.name.is_empty() { p.match_name.clone() } else { format!("{} ({})", p.name, p.match_name) }; + let label = param_label(p); let here = format!("{}: '{}' effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()); if p.kind == ParamKind::ArbitraryData && map.extra != Extra::None { import_extra(fx, map.extra, p, &here, cx.size, group, col); @@ -152,6 +161,27 @@ fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_> } return; }; + import_mapped(p, m, cx, (&here, &label), ParamOut { group, refs, col }, ValueRecord::Written); +} + +/// Which value records count. +#[derive(Clone, Copy, PartialEq, Eq)] +pub(crate) enum ValueRecord { + /// Only written records (`tdsb` 1, [`EffectParam::stored_value`]). + Written, + /// Every record, also one in the default state (Lumetri Color, [`EffectParam::recorded_value`]). + Any, +} + +/// Imports parameter `p` into the property `m` names, converted by `m.conv`. `names` is +/// `(here, label)`, the effect and the parameter for the report. +pub(crate) fn import_mapped(p: &EffectParam<'_, '_>, m: &ParamMap, cx: &LayerCtx<'_>, names: (&str, &str), out: ParamOut<'_>, records: ValueRecord) { + let (here, label) = names; + let ParamOut { group, refs, col } = out; + let read = |p: &EffectParam<'_, '_>| match records { + ValueRecord::Written => p.stored_value(), + ValueRecord::Any => p.recorded_value(), + }; if p.kind != m.kind { col.lose(LossCategory::Effects, format!("{here}: parameter {label} has type {:?}, expected {:?}; not imported", p.kind, m.kind)); return; @@ -159,7 +189,7 @@ fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_> match m.conv { Conv::Skip => return, Conv::LayerRef { own } => { - import_layer_ref(p, m.target, own, cx, (&here, &label), ParamOut { group, refs, col }); + import_layer_ref(p, m.target, own, cx, (here, label), ParamOut { group, refs, col }); return; } _ => {} @@ -264,12 +294,12 @@ fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_> col.lose(LossCategory::DamagedData, format!("{here}: parameter {label} has no readable keyframe, default used")); } if let Some(map) = &popup_map { - remap_popup_keys(prop, map, &here, &label, col); + remap_popup_keys(prop, map, here, label, col); } } return; } - let value = match (p.stored(), p.stored_value()) { + let value = match (p.stored(), read(p)) { (Some(_), Some(v)) => Some(v), (Some(_), None) => { col.lose(LossCategory::DamagedData, format!("{here}: parameter {label} holds an unreadable value, default used")); @@ -451,7 +481,7 @@ fn import_extra(fx: &AepEffect<'_, '_>, extra: Extra, p: &EffectParam<'_, '_>, h Some(m) => import_mesh(&m, here, size, group, col), None => col.lose(LossCategory::DamagedData, format!("{here}: the Distortion Mesh is unreadable, defaults used")), }, - Extra::None => {} + Extra::None | Extra::Lumetri => {} } } diff --git a/crates/aep/src/import/lumetri.rs b/crates/aep/src/import/lumetri.rs new file mode 100644 index 000000000..22f28b1aa --- /dev/null +++ b/crates/aep/src/import/lumetri.rs @@ -0,0 +1,642 @@ +//! Lumetri Color (`ADBE Lumetri`) to EffectCraft's Lumetri Color (`ec.color.lumetri`). +//! +//! The parameters are found by their match name (`ADBE Lumetri-0011` is Exposure), not by their +//! place in `parT`: After Effects has added parameters in the middle of the list over the +//! versions (`-0127` ... `-0130` sit between older ones), and several sliders share a valid range +//! (Contrast, Highlights, Shadows, Whites and Blacks are all -150..150), so a shifted index +//! would map silently to the wrong control. Evidence: the 1200 samples (After Effects 2026, +//! `docs/aep-format.md` 3.7). +//! +//! - **Values in any record state.** Lumetri writes the values of its hidden controls (High +//! Dynamic Range, HDR Specular, the second Look popup, ...) in records in the default state +//! (`tdsb` 3); After Effects reads them back as set, so every record counts here +//! ([`EffectParam::recorded_value`]). +//! - **Section switches.** Each section (Basic Correction, Creative, Curves, RGB Curves, the five +//! hue / saturation curves, Color Wheels, HSL Secondary, Vignette) has an on / off checkbox in +//! After Effects; EffectCraft's Lumetri has none. A section that is off does nothing in After +//! Effects (1200 `lumetri-03-sec-basic-correction`, `-17-sec-vignette` render as `-00-defaults`), +//! so its values are not imported (the effect looks the same) and are listed in the report. +//! - **Arbitrary data** (`ParamKind::ArbitraryData`, every instance stores it): the hue / +//! saturation curves (a curve type, a point count, points as `f64` x, y with y an offset from +//! neutral) are imported when neutral and reported otherwise (no sample moves a point); the +//! HSL Secondary key ([`HslKey`]) is imported; the RGB curves, the colour wheels, Split Toning +//! and the correction wheels are reported when they are not the default (not sampled). +//! - **LUTs and looks.** The Input LUT and the Look are files After Effects names (the Look's name +//! is UTF-16 text in `-0096`, the popups only index a list of the installed ones); they are not +//! in the project, so a set one is reported by name and EffectCraft's popup stays None. +//! - **Eyedropper colours** (White Balance, the hue curve selectors, the key's Set / Add / +//! Remove colours) only set other controls in After Effects; they are reported when set. +//! - The `Blob` (`-0001`, an XML copy of the grade After Effects writes when a Look changes and does +//! not keep current: 1200 `-01-set-all` holds the defaults of controls set after the Look) and +//! the Auto Tone analysis (`-0121`, `-0122`) are derived data and not read. + +use crate::effect_map::{Conv, ParamMap}; +use crate::{Effect as AepEffect, EffectParam, ParamKind, ParamValue}; +use effectcraft_keyframe::Value; + +use crate::import::effects::{LayerCtx, ParamOut, ValueRecord, carries_nothing, import_mapped, param_label}; +use crate::import::report::LossCategory; +use ParamKind as K; + +/// After Effects' Show Mask styles (`-0075`, `pdnm`), EffectCraft's in the same order. +pub const MASK_STYLE: &[&str] = &["Color/Gray", "Color/Black", "White/Black"]; + +/// A part of the effect with its own on / off switch. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Section { + /// No switch (High Dynamic Range and the controls outside the sections). + Always, + BasicCorrection, + Creative, + RgbCurves, + /// The five hue / saturation curves (0 Hue vs Sat ... 4 Sat vs Sat). + HueSat(u8), + ColorWheels, + HslSecondary, + Vignette, + SpeedGradeCustom, +} + +impl Section { + fn name(self) -> &'static str { + match self { + Section::Always => "", + Section::BasicCorrection => "Basic Correction", + Section::Creative => "Creative", + Section::RgbCurves => "RGB Curves", + Section::HueSat(0) => "Hue vs Sat", + Section::HueSat(1) => "Hue vs Hue", + Section::HueSat(2) => "Hue vs Luma", + Section::HueSat(3) => "Luma vs Sat", + Section::HueSat(_) => "Sat vs Sat", + Section::ColorWheels => "Color Wheels", + Section::HslSecondary => "HSL Secondary", + Section::Vignette => "Vignette", + Section::SpeedGradeCustom => "SpeedGrade Custom", + } + } +} + +/// Arbitrary data the importer reads. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Data { + /// The four RGB curves (4 x 520 bytes). + RgbCurves, + /// One hue / saturation curve (520 bytes); the EffectCraft parameter it belongs to. + HueSatCurve, + /// Color Wheels (4 x 3 `f64`, each wheel 0, 0.5, 0 by default). + Wheels, + /// HSL Secondary's correction wheels (8 x 3 `f64`, the same default). + CorrectionWheels, + /// Split Toning (32 zero bytes by default). + SplitToning, + /// HSL Secondary's key ([`HslKey`]). + HslKey, + /// Data without a counterpart, empty by default (`fe fe` or zero bytes). + Other, +} + +/// What a Lumetri parameter is. +#[derive(Clone, Copy, Debug)] +enum Role { + /// An EffectCraft parameter and the conversion. + To(&'static str, Conv), + /// The on / off switch of a section. + Switch, + /// No counterpart: reported when not at the default. + Missing, + /// An eyedropper colour: reported when set. + Eyedropper, + /// The Input LUT (`lut` true) or the Look: a popup, a name or an asset. + Lut { + lut: bool, + }, + /// The Look's colour space (its popup and the space's id): goes with the Look. + LookSpace, + Data(Data), + /// Derived data, not read. + Derived, +} + +struct Row { + suffix: u16, + section: Section, + kind: Option, + role: Role, +} + +const fn row(suffix: u16, section: Section, kind: Option, role: Role) -> Row { + Row { suffix, section, kind, role } +} + +const fn to(suffix: u16, section: Section, kind: ParamKind, target: &'static str, conv: Conv) -> Row { + row(suffix, section, Some(kind), Role::To(target, conv)) +} + +const fn plain(valid_max: f64) -> Conv { + Conv::Plain { valid_max } +} + +use Section as S; + +/// Every Lumetri parameter by match-name suffix (1200: `dump.props` and the `pard` of +/// `lumetri-00-defaults`). The valid maxima are After Effects 2026's; the stored numbers are the +/// values After Effects shows (read back in every variant). +const ROWS: &[Row] = &[ + to(62, S::Always, K::Checkbox, "highDynamicRange", Conv::Checkbox), + // Basic Correction: 3 the switch; 4 / 95 the Input LUT's name data, 5 / 127 its popups. + row(3, S::BasicCorrection, Some(K::Checkbox), Role::Switch), + row(4, S::BasicCorrection, Some(K::ArbitraryData), Role::Lut { lut: true }), + row(95, S::BasicCorrection, Some(K::ArbitraryData), Role::Lut { lut: true }), + row(5, S::BasicCorrection, Some(K::Popup), Role::Lut { lut: true }), + row(127, S::BasicCorrection, Some(K::Popup), Role::Lut { lut: true }), + to(63, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/hdrWhite", plain(1000.0)), + // The Auto tone's Intensity (next to the Auto and Reset buttons 120, 119). + row(123, S::BasicCorrection, Some(K::FloatSlider), Role::Missing), + row(94, S::BasicCorrection, Some(K::Color), Role::Eyedropper), + to(7, S::BasicCorrection, K::FloatSlider, "basicCorrection/whiteBalance/temperature", plain(300.0)), + to(8, S::BasicCorrection, K::FloatSlider, "basicCorrection/whiteBalance/tint", plain(300.0)), + to(20, S::BasicCorrection, K::FloatSlider, "basicCorrection/saturation", plain(300.0)), + to(11, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/exposure", plain(7.0)), + to(12, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/contrast", plain(150.0)), + to(13, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/highlights", plain(150.0)), + to(14, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/shadows", plain(150.0)), + to(15, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/whites", plain(150.0)), + to(16, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/blacks", plain(150.0)), + to(64, S::BasicCorrection, K::FloatSlider, "basicCorrection/tone/hdrSpecular", plain(150.0)), + // Creative: 23 the switch; 24 / 96 the Look's name, 25 / 128 its popups, 129 / 130 its colour + // space. + row(23, S::Creative, Some(K::Checkbox), Role::Switch), + row(24, S::Creative, Some(K::ArbitraryData), Role::Lut { lut: false }), + row(96, S::Creative, Some(K::ArbitraryData), Role::Lut { lut: false }), + row(25, S::Creative, Some(K::Popup), Role::Lut { lut: false }), + row(128, S::Creative, Some(K::Popup), Role::Lut { lut: false }), + row(129, S::Creative, Some(K::ArbitraryData), Role::LookSpace), + row(130, S::Creative, Some(K::Popup), Role::LookSpace), + to(26, S::Creative, K::FloatSlider, "creative/lookIntensity", plain(200.0)), + to(28, S::Creative, K::FloatSlider, "creative/adjustments/fadedFilm", plain(150.0)), + to(29, S::Creative, K::FloatSlider, "creative/adjustments/sharpen", plain(100.0)), + to(30, S::Creative, K::FloatSlider, "creative/adjustments/vibrance", plain(100.0)), + to(31, S::Creative, K::FloatSlider, "creative/adjustments/creativeSaturation", plain(300.0)), + row(32, S::Creative, Some(K::ArbitraryData), Role::Data(Data::SplitToning)), + to(33, S::Creative, K::FloatSlider, "creative/adjustments/tintBalance", plain(150.0)), + // Curves: 37 switches all curves, 118 the RGB curves, 104 ... 111 each hue / saturation curve. + row(37, S::RgbCurves, Some(K::Checkbox), Role::Switch), + row(118, S::RgbCurves, Some(K::Checkbox), Role::Switch), + row(65, S::RgbCurves, Some(K::FloatSlider), Role::Missing), + row(39, S::RgbCurves, Some(K::ArbitraryData), Role::Data(Data::RgbCurves)), + row(104, S::HueSat(0), Some(K::Checkbox), Role::Switch), + row(113, S::HueSat(0), Some(K::Color), Role::Eyedropper), + row(42, S::HueSat(0), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(105, S::HueSat(1), Some(K::Checkbox), Role::Switch), + row(114, S::HueSat(1), Some(K::Color), Role::Eyedropper), + row(106, S::HueSat(1), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(107, S::HueSat(2), Some(K::Checkbox), Role::Switch), + row(115, S::HueSat(2), Some(K::Color), Role::Eyedropper), + row(108, S::HueSat(2), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(109, S::HueSat(3), Some(K::Checkbox), Role::Switch), + row(116, S::HueSat(3), Some(K::Color), Role::Eyedropper), + row(110, S::HueSat(3), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(111, S::HueSat(4), Some(K::Checkbox), Role::Switch), + row(117, S::HueSat(4), Some(K::Color), Role::Eyedropper), + row(112, S::HueSat(4), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + // Color Wheels: 46 the switch, 102 Face Detection (on by default), 66 HDR White, 47 the wheels. + row(46, S::ColorWheels, Some(K::Checkbox), Role::Switch), + row(102, S::ColorWheels, Some(K::Checkbox), Role::Missing), + row(66, S::ColorWheels, Some(K::FloatSlider), Role::Missing), + row(47, S::ColorWheels, Some(K::ArbitraryData), Role::Data(Data::Wheels)), + // HSL Secondary: 68 the switch; the key (70-76), Refine (81, 82), Correction (85-90). + row(68, S::HslSecondary, Some(K::Checkbox), Role::Switch), + row(70, S::HslSecondary, Some(K::Color), Role::Eyedropper), + row(71, S::HslSecondary, Some(K::Color), Role::Eyedropper), + row(72, S::HslSecondary, Some(K::Color), Role::Eyedropper), + row(73, S::HslSecondary, Some(K::ArbitraryData), Role::Data(Data::HslKey)), + to(74, S::HslSecondary, K::Checkbox, "hslSecondary/key/showMask", Conv::Checkbox), + to(75, S::HslSecondary, K::Popup, "hslSecondary/key/maskStyle", Conv::Popup { default: 1, items: MASK_STYLE }), + to(76, S::HslSecondary, K::Checkbox, "hslSecondary/key/invertMask", Conv::Checkbox), + to(81, S::HslSecondary, K::FloatSlider, "hslSecondary/refine/denoise", plain(100.0)), + to(82, S::HslSecondary, K::FloatSlider, "hslSecondary/refine/blur", plain(1000.0)), + row(85, S::HslSecondary, Some(K::ArbitraryData), Role::Data(Data::CorrectionWheels)), + // The correction wheel's mode (4 zero bytes by default). + row(93, S::HslSecondary, Some(K::ArbitraryData), Role::Data(Data::Other)), + to(86, S::HslSecondary, K::FloatSlider, "hslSecondary/correction/temperature", plain(300.0)), + to(87, S::HslSecondary, K::FloatSlider, "hslSecondary/correction/tint", plain(300.0)), + to(88, S::HslSecondary, K::FloatSlider, "hslSecondary/correction/contrast", plain(150.0)), + to(89, S::HslSecondary, K::FloatSlider, "hslSecondary/correction/sharpen", plain(100.0)), + to(90, S::HslSecondary, K::FloatSlider, "hslSecondary/correction/saturation", plain(300.0)), + // Vignette: 50 the switch. + row(50, S::Vignette, Some(K::Checkbox), Role::Switch), + to(51, S::Vignette, K::FloatSlider, "vignette/vignetteAmount", plain(5.0)), + to(52, S::Vignette, K::FloatSlider, "vignette/vignetteMidpoint", plain(100.0)), + to(53, S::Vignette, K::FloatSlider, "vignette/vignetteRoundness", plain(100.0)), + to(54, S::Vignette, K::FloatSlider, "vignette/vignetteFeather", plain(100.0)), + // SpeedGrade Custom: 57 Custom Layer (on by default), 60 its data. + row(57, S::SpeedGradeCustom, Some(K::Checkbox), Role::Switch), + row(60, S::SpeedGradeCustom, Some(K::ArbitraryData), Role::Data(Data::Other)), + // Outside the sections: Legacy Tone And Saturation, the embedded LUTs, Auto Tone, Auto Color, + // the LUT and Look assets. + row(97, S::Always, Some(K::Checkbox), Role::Missing), + row(98, S::Always, Some(K::ArbitraryData), Role::Data(Data::Other)), + row(99, S::Always, Some(K::ArbitraryData), Role::Data(Data::Other)), + row(100, S::Always, Some(K::ArbitraryData), Role::Data(Data::Other)), + row(124, S::Always, Some(K::Checkbox), Role::Missing), + row(125, S::Always, Some(K::ArbitraryData), Role::Lut { lut: true }), + row(126, S::Always, Some(K::ArbitraryData), Role::Lut { lut: false }), + row(1, S::Always, Some(K::ArbitraryData), Role::Derived), + row(121, S::Always, Some(K::FloatSlider), Role::Derived), + row(122, S::Always, Some(K::ArbitraryData), Role::Derived), +]; + +/// The names After Effects shows for the parameters whose `pard` name is blank (`dump.props`). +fn blank_name(suffix: u16) -> Option<&'static str> { + Some(match suffix { + 62 => "High Dynamic Range", + 3 | 23 | 37 | 118 | 104 | 105 | 107 | 109 | 111 | 46 | 68 | 50 => "Active", + 4 | 95 => "Input LUT data", + 24 | 96 => "Look name", + 102 => "Face Detection", + 74 => "Show Mask", + 75 => "Mask style", + 76 => "Invert Mask", + 97 => "Legacy Tone And Saturation", + 124 => "Auto Color Active", + _ => return None, + }) +} + +/// The parameter's name for the report: its own, else the one After Effects shows. +fn name_of(p: &EffectParam<'_, '_>) -> String { + match (p.name.trim(), suffix(&p.match_name).and_then(blank_name)) { + ("", Some(n)) => n.to_string(), + ("", None) => p.match_name.clone(), + (n, _) => n.to_string(), + } +} + +/// The match-name suffix of a Lumetri parameter (`ADBE Lumetri-0011` is 11). +fn suffix(match_name: &str) -> Option { + match_name.strip_prefix("ADBE Lumetri-")?.parse().ok() +} + +/// The parameter's value: the recorded one, else the `pard` default. +fn value_of(p: &EffectParam<'_, '_>) -> Option { + p.recorded_value().or_else(|| p.pard_default()) +} + +/// True when the parameter has keyframes, an expression, or a recorded value other than its +/// default (a record without a readable default counts as set). +fn is_set(p: &EffectParam<'_, '_>) -> bool { + if p.is_animated() || p.stored().is_some_and(|s| s.expression().is_some()) { + return true; + } + match (p.recorded_value(), p.pard_default()) { + (Some(v), Some(d)) => !same(v, d), + (Some(_), None) => true, + (None, _) => false, + } +} + +/// Equal within the float noise of a stored `f64` / `f32`. +fn same(a: ParamValue, b: ParamValue) -> bool { + let close = |x: f64, y: f64| (x - y).abs() <= 1e-6 * x.abs().max(y.abs()).max(1.0); + match (a, b) { + (ParamValue::Number(x), ParamValue::Number(y)) => close(x, y), + (ParamValue::Argb(x), ParamValue::Argb(y)) => x.iter().zip(y).skip(1).all(|(x, y)| close(*x, y)), + _ => a == b, + } +} + +/// A value as the report shows it. +fn shown_value(v: Option) -> String { + match v { + Some(ParamValue::Number(n)) => { + let s = format!("{n:.3}"); + s.trim_end_matches('0').trim_end_matches('.').to_string() + } + Some(ParamValue::Bool(b)) => if b { "on" } else { "off" }.to_string(), + Some(ParamValue::Index(i)) => format!("item {i}"), + Some(ParamValue::Argb([_, r, g, b])) => format!("RGB {:.3} {:.3} {:.3}", r, g, b), + Some(other) => format!("{other:?}"), + None => "set".to_string(), + } +} + +/// Empty data: absent, `fe fe` (no text, no asset) or only zero bytes. +fn is_empty_data(d: Option<&[u8]>) -> bool { + match d { + None => true, + Some(d) => d == [0xfe, 0xfe] || d.iter().all(|b| *b == 0), + } +} + +fn le_u32(d: &[u8], at: usize) -> Option { + Some(u32::from_le_bytes(*d.get(at..at.checked_add(4)?)?.first_chunk::<4>()?)) +} + +fn le_f32(d: &[u8], at: usize) -> Option { + Some(f32::from_le_bytes(*d.get(at..at.checked_add(4)?)?.first_chunk::<4>()?)) +} + +fn le_f64(d: &[u8], at: usize) -> Option { + Some(f64::from_le_bytes(*d.get(at..at.checked_add(8)?)?.first_chunk::<8>()?)) +} + +/// A UTF-16LE text up to its NUL (the Look's name, `-0096`: `SL BLUE ICE` in 1200 `-01-set-all`); +/// `None` for empty data. +fn utf16_text(d: &[u8]) -> Option { + if is_empty_data(Some(d)) { + return None; + } + let units: Vec = d.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).take_while(|u| *u != 0).collect(); + let s = String::from_utf16_lossy(&units); + let s = s.trim(); + (!s.is_empty()).then(|| s.to_string()) +} + +/// A Lumetri curve (520 bytes): `i32` curve type at 0 (3 for the hue curves, which wrap; 2 for +/// Luma vs Sat and Sat vs Sat; 0 for an RGB curve), the `u32` point count at 4, then the points +/// as little-endian `f64` pairs x, y from byte 8 (at most 32). `None` when the count does not fit. +fn curve_points(d: &[u8]) -> Option> { + let n = usize::try_from(le_u32(d, 4)?).ok()?; + if n > d.len().saturating_sub(8) / 16 { + return None; + } + (0..n).map(|i| Some((le_f64(d, 8 + 16 * i)?, le_f64(d, 16 + 16 * i)?))).collect() +} + +/// HSL Secondary's key (`-0073`, 40 bytes): a flag byte (1 once a key colour was picked, 0 for a +/// fresh effect), then hue, saturation and lightness, each three little-endian `f32` (centre, +/// range, tolerance; fractions of the full hue circle / of 0..1) and an enabled byte. A fresh effect +/// stores flag 0 and centre 0.5, range 0, tolerance 0, enabled for each (1200 `-00-defaults`); +/// After Effects' own copy in `-0001` names the same fields (`middleHLS`, `rangeHLS`, +/// `toleranceHLS`, `enableHLS`, hue in degrees: centre 180). Picking Set color RGB 0.02 0.55 0.85 +/// gives hue centre 0.5605 (that colour's hue, 201.8 degrees) and lightness centre 0.435 (its +/// HSL lightness) with range 0.05 and tolerance 0.1 (`-01-set-all`). +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct HslKey { + pub picked: bool, + /// Hue, saturation, lightness: centre, range, tolerance, enabled. + pub channels: [(f32, f32, f32, bool); 3], +} + +impl HslKey { + pub fn parse(d: &[u8]) -> Option { + if d.len() != 40 { + return None; + } + let picked = match d.first()? { + 0 => false, + 1 => true, + _ => return None, + }; + let ch = |at: usize| -> Option<(f32, f32, f32, bool)> { + let (c, r, t) = (le_f32(d, at)?, le_f32(d, at + 4)?, le_f32(d, at + 8)?); + let on = match d.get(at + 12)? { + 0 => false, + 1 => true, + _ => return None, + }; + (c.is_finite() && r.is_finite() && t.is_finite()).then_some((c, r, t, on)) + }; + Some(HslKey { picked, channels: [ch(1)?, ch(14)?, ch(27)?] }) + } + + fn is_default(&self) -> bool { + !self.picked && self.channels.iter().all(|c| *c == (0.5, 0.0, 0.0, true)) + } +} + +/// The colour wheel data: triples of `f64`, each wheel `0, 0.5, 0` by default. +fn wheels_default(d: &[u8], wheels: usize) -> bool { + d.len() == wheels * 24 && (0..wheels).all(|w| le_f64(d, w * 24) == Some(0.0) && le_f64(d, w * 24 + 8) == Some(0.5) && le_f64(d, w * 24 + 16) == Some(0.0)) +} + +/// Is the data of `data` at After Effects' default (as far as it can be told)? +fn data_default(data: Data, d: Option<&[u8]>) -> bool { + let Some(d) = d else { return true }; + match data { + Data::RgbCurves => { + d.len() == 4 * 520 && d.as_chunks::<520>().0.iter().all(|c| curve_points(c).is_some_and(|pts| pts.iter().all(|(x, y)| (x - y).abs() < 1e-9))) + } + Data::HueSatCurve => curve_points(d).is_some_and(|pts| pts.iter().all(|(_, y)| *y == 0.0)), + Data::Wheels => wheels_default(d, 4), + Data::CorrectionWheels => wheels_default(d, 8), + Data::SplitToning | Data::Other => is_empty_data(Some(d)), + Data::HslKey => HslKey::parse(d).is_some_and(|k| k.is_default()), + } +} + +/// What the Input LUT or the Look holds: names, a popup item, an asset. +#[derive(Default)] +struct LutState { + names: Vec, + item: Option, + asset: bool, +} + +/// How a set parameter of a section that does nothing is listed in the report; `None` when it is +/// at its default. +fn off_text(p: &EffectParam<'_, '_>, role: Role, data: Option<&[u8]>) -> Option { + match role { + Role::Data(d) => (!data_default(d, data)).then(|| name_of(p)), + // A LUT by its name (the popups index the same choice). + Role::Lut { lut } if p.kind == K::ArbitraryData => { + let what = if lut { "Input LUT" } else { "Look" }; + data.and_then(utf16_text).map(|n| format!("{what} '{n}'")).or_else(|| (!is_empty_data(data)).then(|| what.to_string())) + } + // The Look's colour space goes with the Look. + Role::Lut { .. } | Role::LookSpace | Role::Derived => None, + _ => is_set(p).then(|| format!("{} {}", name_of(p), shown_value(p.recorded_value()))), + } +} + +/// Notes what an Input LUT or Look parameter holds: a popup item, an asset or a name. +fn note_lut(p: &EffectParam<'_, '_>, suffix: u16, data: Option<&[u8]>, st: &mut LutState) { + if p.kind == K::Popup { + // The popup indexes the LUTs installed with After Effects; item 1 is None. + if let Some(ParamValue::Index(i)) = p.recorded_value() + && i > 1 + { + st.item = usize::try_from(i).ok().and_then(|i| p.items.as_ref()?.get(i - 1).cloned()).or(Some(format!("item {i}"))); + } + } else if suffix == 125 || suffix == 126 { + st.asset |= !is_empty_data(data); + } else if let Some(name) = data.and_then(utf16_text) + && !st.names.contains(&name) + { + st.names.push(name); + } +} + +/// Imports a Lumetri Color instance's parameters into `out.group` and reports what does not arrive. +pub(crate) fn import_lumetri(fx: &AepEffect<'_, '_>, params: &[EffectParam<'_, '_>], cx: &LayerCtx<'_>, shown: &str, out: ParamOut<'_>) { + let ParamOut { group, refs, col } = out; + let here = format!("{}: '{}' effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name()); + let find = |s: u16| params.iter().find(|p| suffix(&p.match_name) == Some(s)); + // The switches: on unless a record says off. Keys or an expression on a switch are not + // imported (the static value counts). + let switch_on = |s: u16| -> bool { + match find(s) { + Some(p) => !matches!(value_of(p), Some(ParamValue::Bool(false))), + None => true, + } + }; + // HSL Secondary without a picked key colour selects nothing: After Effects applies none of + // its correction (1200 `-16-sec-correction`, `-15-sec-refine` render as `-00-defaults`); + // EffectCraft's default key is the whole image, so the values are left out too. + // Unreadable or missing key data does not count as unpicked (it is reported or older). + let key_picked = find(73).and_then(|p| fx.arbitrary_data(&p.match_name)).and_then(HslKey::parse).is_none_or(|k| k.picked); + let section_on = |sec: Section| -> bool { + match sec { + Section::Always => true, + Section::BasicCorrection => switch_on(3), + Section::Creative => switch_on(23), + Section::RgbCurves => switch_on(37) && switch_on(118), + Section::HueSat(i) => switch_on(37) && switch_on([104, 105, 107, 109, 111].get(usize::from(i)).copied().unwrap_or(104)), + Section::ColorWheels => switch_on(46), + Section::HslSecondary => switch_on(68) && key_picked, + Section::Vignette => switch_on(50), + // Custom Layer off is itself reported (a switch without counterpart). + Section::SpeedGradeCustom => true, + } + }; + let mut off: Vec<(Section, Vec)> = Vec::new(); + let mut eyedroppers: Vec = Vec::new(); + let mut luts = [LutState::default(), LutState::default()]; + for p in params { + if carries_nothing(p) || p.match_name == "ADBE Effect Built In Params" { + continue; + } + let label = if p.name.trim().is_empty() { format!("{} ({})", name_of(p), p.match_name) } else { param_label(p) }; + let Some(row) = suffix(&p.match_name).and_then(|s| ROWS.iter().find(|r| r.suffix == s)) else { + // A parameter this table does not know (a newer version): lost when set. + if p.stored().is_some() + && (p.kind != K::ArbitraryData && is_set(p) || p.kind == K::ArbitraryData && !is_empty_data(fx.arbitrary_data(&p.match_name))) + { + col.lose(LossCategory::Effects, format!("{here}: parameter {label} not imported")); + } + continue; + }; + if row.kind.is_some_and(|k| k != p.kind) { + if p.stored().is_some() { + col.lose(LossCategory::Effects, format!("{here}: parameter {label} has type {:?}, expected {:?}; not imported", p.kind, row.kind)); + } + continue; + } + let data = || fx.arbitrary_data(&p.match_name); + // A switched-off section: its set values are listed, not imported. + if !matches!(row.role, Role::Switch) && !section_on(row.section) { + let text = off_text(p, row.role, data()); + if let Some(text) = text { + match off.iter_mut().find(|(s, _)| *s == row.section) { + Some((_, list)) => { + if !list.contains(&text) { + list.push(text); + } + } + None => off.push((row.section, vec![text])), + } + } + continue; + } + match row.role { + Role::To(target, conv) => { + let m = ParamMap { index: p.index, kind: p.kind, target, conv }; + import_mapped(p, &m, cx, (&here, &label), ParamOut { group: &mut *group, refs: &mut *refs, col: &mut *col }, ValueRecord::Any); + } + Role::Switch => { + if p.is_animated() || p.stored().is_some_and(|s| s.expression().is_some()) { + col.lose(LossCategory::Effects, format!("{here}: keyframes or an expression on the switch {label} are not imported")); + } + if row.section == Section::SpeedGradeCustom && !switch_on(row.suffix) { + col.lose(LossCategory::Effects, format!("{here}: parameter {label} is off and has no counterpart")); + } + } + Role::Missing => { + if is_set(p) { + col.lose( + LossCategory::Effects, + format!("{here}: parameter {label} is {} and has no counterpart, not imported", shown_value(p.recorded_value())), + ); + } + } + Role::Eyedropper => { + if is_set(p) { + eyedroppers.push(name_of(p)); + } + } + Role::Lut { lut } => note_lut(p, row.suffix, data(), &mut luts[usize::from(!lut)]), + // The Look's colour space only applies to a Look, which is reported. + Role::LookSpace => {} + Role::Data(Data::HslKey) => import_hsl_key(data(), &here, &mut *group, &mut *col), + Role::Data(d) => { + if p.is_animated() || p.stored().is_some_and(|s| s.expression().is_some()) { + col.lose(LossCategory::Effects, format!("{here}: keyframes or an expression on {label} are not imported")); + } + if !data_default(d, data()) { + let why = match d { + Data::HueSatCurve => "the curve's points are moved; no sample tells their scale", + Data::RgbCurves | Data::Wheels | Data::CorrectionWheels | Data::SplitToning => "the data is not decoded (not sampled)", + Data::HslKey | Data::Other => "no counterpart", + }; + col.lose(LossCategory::Effects, format!("{here}: parameter {label} is not imported ({why})")); + } + } + Role::Derived => {} + } + } + for (sec, list) in off { + let why = if sec == Section::HslSecondary && switch_on(68) { + "has no key colour picked (After Effects applies it to nothing)" + } else { + "is switched off (EffectCraft's Lumetri has no section switch)" + }; + col.lose(LossCategory::Effects, format!("{here}: section {} {why}, its values are not imported: {}", sec.name(), list.join(", "))); + } + if !eyedroppers.is_empty() { + col.lose(LossCategory::Effects, format!("{here}: the eyedropper colours {} are not imported (they only set other controls)", eyedroppers.join(", "))); + } + for (st, what) in luts.iter().zip(["Input LUT", "Look"]) { + let name = st.names.first().cloned().or_else(|| st.item.clone()); + if name.is_some() || st.asset { + let name = name.map(|n| format!(" '{n}'")).unwrap_or_default(); + col.lose( + LossCategory::Effects, + format!("{here}: the {what}{name} is not imported (the LUT file is not in the project; EffectCraft's {what} stays None)"), + ); + } + } +} + +/// HSL Secondary's key as EffectCraft's hue centre / range / softness (degrees) and saturation and +/// lightness bands (percent): range is taken as the half width, tolerance as the softness. A key +/// that was never picked keeps EffectCraft's default (the whole image). +fn import_hsl_key(d: Option<&[u8]>, here: &str, group: &mut effectcraft_project::PropGroup, col: &mut crate::import::report::Collector) { + let Some(d) = d else { return }; + let Some(key) = HslKey::parse(d) else { + col.lose(LossCategory::DamagedData, format!("{here}: the HSL Secondary key data is unreadable, the default key used")); + return; + }; + if !key.picked { + if !key.is_default() { + col.lose(LossCategory::Effects, format!("{here}: the HSL Secondary key holds values without a picked colour, not imported")); + } + return; + } + let mut set = |id: &str, v: Value| { + if let Some(prop) = group.prop_mut(&format!("hslSecondary/key/{id}")) { + prop.value = v; + } + }; + let [h, s, l] = key.channels.map(|(c, r, t, on)| (f64::from(c), f64::from(r), f64::from(t), on)); + set("hueEnabled", Value::Bool(h.3)); + set("hueCenter", Value::Scalar((h.0 * 360.0).rem_euclid(360.0))); + set("hueRange", Value::Scalar((h.1 * 360.0).clamp(0.0, 180.0))); + set("hueSoftness", Value::Scalar((h.2 * 360.0).clamp(0.0, 180.0))); + for ((c, r, t, on), id) in [(s.0, s.1, s.2, s.3), (l.0, l.1, l.2, l.3)].into_iter().zip(["saturation", "lightness"]) { + set(&format!("{id}Enabled"), Value::Bool(on)); + set(&format!("{id}Low"), Value::Scalar(((c - r) * 100.0).clamp(0.0, 100.0))); + set(&format!("{id}High"), Value::Scalar(((c + r) * 100.0).clamp(0.0, 100.0))); + set(&format!("{id}Softness"), Value::Scalar((t * 100.0).clamp(0.0, 100.0))); + } +} diff --git a/crates/aep/tests/it/import.rs b/crates/aep/tests/it/import.rs index 95dced570..122d18c2b 100644 --- a/crates/aep/tests/it/import.rs +++ b/crates/aep/tests/it/import.rs @@ -13,6 +13,7 @@ mod effects; mod effects_batch5; mod effects_round2; mod effects_round3; +mod lumetri; mod markers; mod masks; mod mattes; diff --git a/crates/aep/tests/it/import/effects.rs b/crates/aep/tests/it/import/effects.rs index be1293b9a..010df44d5 100644 --- a/crates/aep/tests/it/import/effects.rs +++ b/crates/aep/tests/it/import/effects.rs @@ -72,7 +72,7 @@ fn the_table_names_tint_gradient_ramp_and_exposure() { ] { assert!(t.contains(&want), "{want:?}"); } - assert_eq!(t.len(), 57); + assert_eq!(t.len(), 58); for (_, id) in t { assert!(effectcraft_effects::find(id).is_some(), "{id}"); } diff --git a/crates/aep/tests/it/import/lumetri.rs b/crates/aep/tests/it/import/lumetri.rs new file mode 100644 index 000000000..526f43648 --- /dev/null +++ b/crates/aep/tests/it/import/lumetri.rs @@ -0,0 +1,448 @@ +//! Lumetri Color (`ADBE Lumetri`) arrives as EffectCraft's Lumetri Color (`ec.color.lumetri`), +//! its parameters found by match name. The effect is built into the committed `synthetic.aep` in +//! place of its Glow, in the layout of the 1200 samples (After Effects 2026, docs/aep-format.md +//! 3.7): the definitions are a subset in an order of our own, so a mapping by `parT` index fails. + +use effectcraft_aep::import::{ImportOptions, ImportResult, LossCategory, import}; +use effectcraft_aep::{Body, Chunk, parse, write}; +use effectcraft_keyframe::Value; +use effectcraft_project::{Project, PropGroup}; + +const SYNTHETIC: &[u8] = include_bytes!("../../fixtures/synthetic.aep"); + +fn tdmn(name: &str) -> Chunk<'static> { + let mut b = name.as_bytes().to_vec(); + b.resize(40, 0); + Chunk::data(*b"tdmn", b) +} + +/// A chunk holding chunks (`pdnm`, `fnam`: `NESTED_IDS`) with one `Utf8`. +fn nested(id: [u8; 4], text: &[u8]) -> Chunk<'static> { + Chunk { id, body: Body::Chunks(vec![Chunk::data(*b"Utf8", text.to_vec())]), pad: None } +} + +fn mn(suffix: u16) -> String { + format!("ADBE Lumetri-{suffix:04}") +} + +/// A parameter definition (`pard`, 148 bytes): the type at byte 15, the name at 16, the default at +/// 56 (a float slider's `f64`, a checkbox's or popup's `u32`), a popup's item count at 60, a float +/// slider's valid range at 104 / 108 and slider range at 112 / 116 (`f32`). +#[derive(Clone)] +enum Def { + Float { name: &'static str, default: f64, min: f32, max: f32 }, + Check { default: bool }, + Popup { name: &'static str, items: &'static [&'static str] }, + Data { name: &'static str }, + Group { name: &'static str, end: bool }, +} + +fn pard(d: &Def) -> Vec> { + let mut b = vec![0u8; 148]; + let name = |b: &mut Vec, n: &str| b[16..16 + n.len()].copy_from_slice(n.as_bytes()); + let mut extra = Vec::new(); + match d { + Def::Float { name: n, default, min, max } => { + b[15] = 10; + name(&mut b, n); + b[56..64].copy_from_slice(&default.to_be_bytes()); + b[104..108].copy_from_slice(&min.to_be_bytes()); + b[108..112].copy_from_slice(&max.to_be_bytes()); + b[112..116].copy_from_slice(&min.to_be_bytes()); + b[116..120].copy_from_slice(&max.to_be_bytes()); + } + Def::Check { default } => { + b[15] = 4; + name(&mut b, " "); + b[56..60].copy_from_slice(&u32::from(*default).to_be_bytes()); + } + Def::Popup { name: n, items } => { + b[15] = 7; + name(&mut b, n); + b[56..60].copy_from_slice(&1u32.to_be_bytes()); + b[60..62].copy_from_slice(&u16::try_from(items.len()).unwrap().to_be_bytes()); + extra.push(nested(*b"pdnm", items.join("|").as_bytes())); + } + Def::Data { name: n } => { + b[15] = 11; + name(&mut b, n); + } + Def::Group { name: n, end } => { + b[15] = if *end { 14 } else { 13 }; + name(&mut b, n); + } + } + let mut v = vec![Chunk::data(*b"pard", b)]; + v.extend(extra); + v +} + +/// A stored value. +#[derive(Clone)] +enum Rec { + /// A number in a record of state `tdsb` (1 written, 3 the default state). + Num(f64, u32), + /// Arbitrary data. + Data(Vec), +} + +/// A static value record after the one After Effects wrote for Glow's A & B Midpoint (its header, +/// one component). +fn record(template: &Chunk<'_>, tdsb: u32, v: f64) -> Chunk<'static> { + let mut kids = Vec::new(); + for c in template.children() { + match &c.body { + Body::Data(_) if c.id == *b"tdsb" => kids.push(Chunk::data(*b"tdsb", tdsb.to_be_bytes().to_vec())), + Body::Data(d) if c.id == *b"tdb4" => kids.push(Chunk::data(c.id, d.to_vec())), + Body::Data(_) if c.id == *b"cdat" => { + let mut d = vec![0u8; 40]; + d[..8].copy_from_slice(&v.to_be_bytes()); + kids.push(Chunk::data(*b"cdat", d)); + } + _ => {} + } + } + Chunk::list(*b"tdbs", kids) +} + +/// The defaults of a fresh instance's data (1200 `lumetri-00-defaults`). +fn empty() -> Vec { + vec![0xfe, 0xfe] +} + +/// A Lumetri curve: type, point count, points (`f64` x, y), padded to 520 bytes. +fn curve(kind: i32, pts: &[(f64, f64)]) -> Vec { + let mut d = kind.to_le_bytes().to_vec(); + d.extend(u32::try_from(pts.len()).unwrap().to_le_bytes()); + for (x, y) in pts { + d.extend(x.to_le_bytes()); + d.extend(y.to_le_bytes()); + } + d.resize(520, 0); + d +} + +fn rgb_curves() -> Vec { + (0..4).flat_map(|_| curve(0, &[(0.0, 0.0), (1.0, 1.0)])).collect() +} + +fn wheels(n: usize) -> Vec { + (0..n).flat_map(|_| [0.0f64, 0.5, 0.0].into_iter().flat_map(f64::to_le_bytes)).collect() +} + +fn utf16(s: &str) -> Vec { + s.encode_utf16().chain([0]).flat_map(u16::to_le_bytes).collect() +} + +/// HSL Secondary's key data as 1200 `lumetri-01-set-all` stores it (Set color RGB 0.02 0.55 0.85). +const HSL_PICKED: &str = "01327f0f3fcdcc4c3dcdcccc3d01d519683f2e57c93dcade173e01dfdede3ecdcc4c3dcdcccc3d01"; +/// A fresh instance's key data (`-00-defaults`). +const HSL_FRESH: &str = "000000003f0000000000000000010000003f0000000000000000010000003f000000000000000001"; + +fn hex(s: &str) -> Vec { + (0..s.len()).step_by(2).map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap()).collect() +} + +const MASK_STYLE: &[&str] = &["Color/Gray", "Color/Black", "White/Black"]; +const LOOKS: &[&str] = &["None", "[Custom]", "Browse...", "(-", "SL BIG", "SL BLUE ICE"]; + +/// The definitions, an order of our own (not After Effects' `parT` order) and without the ones +/// After Effects 2026 added late (-0127 ... -0130). +fn defs() -> Vec<(u16, Def)> { + let f = |name, default, min, max| Def::Float { name, default, min, max }; + vec![ + (1, Def::Data { name: "Blob" }), + (62, Def::Check { default: false }), + (2, Def::Group { name: "Basic Correction", end: false }), + (3, Def::Check { default: true }), + (95, Def::Data { name: " " }), + (64, f("HDR Specular", 0.0, -150.0, 150.0)), + (16, f("Blacks", 0.0, -150.0, 150.0)), + (11, f("Exposure", 0.0, -7.0, 7.0)), + (12, f("Contrast", 0.0, -150.0, 150.0)), + (7, f("Temperature", 0.0, -300.0, 300.0)), + (21, Def::Group { name: "", end: true }), + (22, Def::Group { name: "Creative", end: false }), + (23, Def::Check { default: true }), + (96, Def::Data { name: " " }), + (25, Def::Popup { name: "Look", items: LOOKS }), + (26, f("Intensity", 100.0, 0.0, 200.0)), + (32, Def::Data { name: "Split Toning" }), + (35, Def::Group { name: "", end: true }), + (39, Def::Data { name: "RGB Curve" }), + (104, Def::Check { default: true }), + (42, Def::Data { name: "Hue vs Sat" }), + (47, Def::Data { name: "Wheels" }), + (68, Def::Check { default: true }), + (73, Def::Data { name: "HSL Sliders" }), + (74, Def::Check { default: false }), + (75, Def::Popup { name: " ", items: MASK_STYLE }), + (85, Def::Data { name: "Color Wheels" }), + (86, f("Temperature", 0.0, -300.0, 300.0)), + (50, Def::Check { default: true }), + (51, f("Amount", 0.0, -5.0, 5.0)), + (54, f("Feather", 50.0, 0.0, 100.0)), + (97, Def::Check { default: false }), + ] +} + +/// The data every instance stores, at the defaults. +fn default_data() -> Vec<(u16, Rec)> { + vec![ + (1, Rec::Data(empty())), + (95, Rec::Data(empty())), + (96, Rec::Data(empty())), + (32, Rec::Data(vec![0; 32])), + (39, Rec::Data(rgb_curves())), + (42, Rec::Data(curve(3, &[]))), + (47, Rec::Data(wheels(4))), + (73, Rec::Data(hex(HSL_FRESH))), + (85, Rec::Data(wheels(8))), + ] +} + +/// The Lumetri instance: the definitions, then the stored values `recs` (data replaces the +/// default data of the same parameter). +fn sspc(defs: &[(u16, Def)], recs: &[(u16, Rec)], template: &Chunk<'_>) -> Chunk<'static> { + let mut part = vec![Chunk::data(*b"parn", u32::try_from(defs.len() + 2).unwrap().to_be_bytes().to_vec()), tdmn(&mn(0))]; + let mut layer = vec![0u8; 148]; + layer[15] = 0; + part.push(Chunk::data(*b"pard", layer)); + for (s, d) in defs { + part.push(tdmn(&mn(*s))); + part.extend(pard(d)); + } + part.push(tdmn("ADBE Effect Built In Params")); + let mut end = vec![0u8; 148]; + end[15] = 9; + part.push(Chunk::data(*b"pard", end)); + let mut all: Vec<(u16, Rec)> = default_data().into_iter().filter(|(s, _)| !recs.iter().any(|(r, _)| r == s)).collect(); + all.extend(recs.iter().cloned()); + let mut values = vec![Chunk::data(*b"tdsb", 1u32.to_be_bytes().to_vec())]; + for (s, r) in all { + values.push(tdmn(&mn(s))); + match r { + Rec::Num(v, tdsb) => values.push(record(template, tdsb, v)), + Rec::Data(d) => { + values.push(record(template, 1, 0.0)); + values.push(Chunk::list(*b"aRbs", vec![Chunk::data(*b"aRbp", d)])); + } + } + } + Chunk::list(*b"sspc", vec![nested(*b"fnam", b"Lumetri Color"), Chunk::list(*b"parT", part), Chunk::list(*b"tdgp", values)]) +} + +fn find_glow_template<'a>(chunks: &'a [Chunk<'a>]) -> Option> { + for c in chunks { + let kids = c.children(); + if let Some(at) = kids.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.starts_with(b"ADBE Glo2-0011\0"))) + && let Some(record) = kids.get(at + 1).filter(|k| k.is_list(b"tdbs")) + { + return Some(record.clone()); + } + if let Some(t) = find_glow_template(kids) { + return Some(t); + } + } + None +} + +/// Puts the instance in place of the Glow effect (in the effect parade and the project's shared +/// definitions); the number of places. +fn replace_glow(chunks: &mut [Chunk<'_>], instance: &Chunk<'static>) -> usize { + let mut n = 0; + for ch in chunks { + let kids = ch.children_mut(); + if let Some(at) = kids.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.starts_with(b"ADBE Glo2\0"))) + && kids.get(at + 1).is_some_and(|k| k.is_list(b"sspc")) + { + kids[at] = tdmn("ADBE Lumetri"); + kids[at + 1] = instance.clone(); + n += 1; + } + n += replace_glow(kids, instance); + } + n +} + +fn run_with(defs: &[(u16, Def)], recs: &[(u16, Rec)]) -> (PropGroup, ImportResult) { + let file = parse(SYNTHETIC).unwrap(); + let template = find_glow_template(&file.chunks).expect("Glow's A & B Midpoint record"); + let instance = sspc(defs, recs, &template); + let mut file = parse(SYNTHETIC).unwrap(); + assert_eq!(replace_glow(&mut file.chunks, &instance), 2, "the Glow effect and its shared definitions"); + let bytes = write(&file).unwrap(); + let mut project = Project::default(); + let none = |_: &str| false; + let r = import(&mut project, &bytes, &ImportOptions { allow_losses: true, ..ImportOptions::new(&none) }).unwrap(); + let g = project + .comps() + .flat_map(|(_, c)| c.layers.iter()) + .find_map(|l| l.props.sub("effects").and_then(|fx| fx.children.iter().filter_map(|n| n.as_group()).find(|g| g.match_id == "ec.color.lumetri").cloned())) + .expect("an EffectCraft Lumetri Color"); + (g, r) +} + +fn run(recs: &[(u16, Rec)]) -> (PropGroup, ImportResult) { + run_with(&defs(), recs) +} + +fn losses(r: &ImportResult) -> Vec { + r.report + .losses + .iter() + .filter(|l| matches!(l.category, LossCategory::Effects | LossCategory::DamagedData)) + .flat_map(|l| l.details.clone()) + .filter(|d| d.contains("Lumetri")) + .collect() +} + +fn num(g: &PropGroup, id: &str) -> f64 { + g.prop(id).unwrap_or_else(|| panic!("no {id}")).value.as_f64() +} + +fn val(g: &PropGroup, id: &str) -> Value { + g.prop(id).unwrap_or_else(|| panic!("no {id}")).value.clone() +} + +#[test] +fn a_fresh_lumetri_arrives_without_losses() { + let (g, r) = run(&[]); + assert_eq!(losses(&r), Vec::::new()); + assert_eq!((num(&g, "basicCorrection/tone/exposure"), num(&g, "creative/lookIntensity"), num(&g, "vignette/vignetteFeather")), (0.0, 100.0, 50.0)); + assert_eq!(val(&g, "creative/look"), Value::Enum(0)); +} + +#[test] +fn lumetri_values_arrive_by_match_name_also_from_default_state_records() { + // 1200 `lumetri-01-set-all` / `-06-sec-light`: High Dynamic Range and HDR Specular are + // recorded in the default state (`tdsb` 3) and read back as set. + let (g, r) = run(&[ + (62, Rec::Num(1.0, 3)), + (64, Rec::Num(22.0, 3)), + (11, Rec::Num(2.589_999_914_169_311_5, 1)), + (12, Rec::Num(37.0, 1)), + (16, Rec::Num(-150.0, 1)), + (7, Rec::Num(300.0, 1)), + (26, Rec::Num(134.0, 1)), + (51, Rec::Num(1.85, 1)), + (74, Rec::Num(1.0, 1)), + (75, Rec::Num(3.0, 1)), + (73, Rec::Data(hex(HSL_PICKED))), + ]); + assert_eq!(losses(&r), Vec::::new()); + assert_eq!(val(&g, "highDynamicRange"), Value::Bool(true)); + for (id, want) in [ + ("basicCorrection/tone/hdrSpecular", 22.0), + ("basicCorrection/tone/exposure", 2.589_999_914_169_311_5), + ("basicCorrection/tone/contrast", 37.0), + ("basicCorrection/tone/blacks", -150.0), + // Past EffectCraft's old slider range: kept (the range is After Effects' now). + ("basicCorrection/whiteBalance/temperature", 300.0), + ("creative/lookIntensity", 134.0), + ("vignette/vignetteAmount", 1.85), + ] { + assert_eq!(num(&g, id), want, "{id}"); + } + assert_eq!((val(&g, "hslSecondary/key/showMask"), val(&g, "hslSecondary/key/maskStyle")), (Value::Bool(true), Value::Enum(2))); +} + +#[test] +fn a_switched_off_section_is_reported_and_not_applied() { + // 1200 `-03-sec-basic-correction`, `-17-sec-vignette`: a section that is off does nothing. + let (g, r) = run(&[(3, Rec::Num(0.0, 1)), (11, Rec::Num(2.0, 1)), (50, Rec::Num(0.0, 1)), (51, Rec::Num(-2.0, 1)), (12, Rec::Num(37.0, 1))]); + assert_eq!((num(&g, "basicCorrection/tone/exposure"), num(&g, "basicCorrection/tone/contrast"), num(&g, "vignette/vignetteAmount")), (0.0, 0.0, 0.0)); + let l = losses(&r); + assert_eq!(l.len(), 2, "{l:?}"); + assert!(l[0].contains("section Basic Correction is switched off") && l[0].contains("Exposure 2, Contrast 37"), "{l:?}"); + assert!(l[1].contains("section Vignette is switched off") && l[1].contains("Amount -2"), "{l:?}"); +} + +#[test] +fn a_look_is_reported_by_name() { + // 1200 `-01-set-all`: the Look's name in -0096, the popup's record in the default state. + let (g, r) = run(&[(96, Rec::Data(utf16("SL BLUE ICE"))), (25, Rec::Num(6.0, 3))]); + assert_eq!(val(&g, "creative/look"), Value::Enum(0)); + let l = losses(&r); + assert_eq!(l.len(), 1, "{l:?}"); + assert!(l[0].contains("the Look 'SL BLUE ICE' is not imported"), "{l:?}"); + // Only the popup: its item. + let (_, r) = run(&[(25, Rec::Num(5.0, 1))]); + assert!(losses(&r).iter().any(|d| d.contains("the Look 'SL BIG' is not imported")), "{:?}", losses(&r)); +} + +#[test] +fn hue_saturation_curves_with_neutral_points_arrive_moved_ones_are_reported() { + // 1200 `-11-sec-hue-saturation-curves`: the selector put three neutral points on the curve. + let neutral = curve(3, &[(0.742_483, 0.0), (0.798_038, 0.0), (0.853_594, 0.0)]); + let (g, r) = run(&[(42, Rec::Data(neutral))]); + assert_eq!(losses(&r), Vec::::new()); + assert_eq!(val(&g, "curves/hueSaturationCurves/hueVsSat"), Value::Str(String::new())); + let (_, r) = run(&[(42, Rec::Data(curve(3, &[(0.5, 0.25)])))]); + let l = losses(&r); + assert!(l.len() == 1 && l[0].contains("Hue vs Sat (ADBE Lumetri-0042) is not imported"), "{l:?}"); + // A point count past the data: not a default, reported, no panic. + let mut bad = curve(3, &[]); + bad[4..8].copy_from_slice(&1000u32.to_le_bytes()); + let (_, r) = run(&[(42, Rec::Data(bad))]); + assert_eq!(losses(&r).len(), 1); +} + +#[test] +fn the_picked_hsl_key_becomes_hue_saturation_and_lightness_ranges() { + let (g, r) = run(&[(73, Rec::Data(hex(HSL_PICKED)))]); + assert_eq!(losses(&r), Vec::::new()); + let close = |id: &str, want: f64| assert!((num(&g, id) - want).abs() < 0.01, "{id}: {} != {want}", num(&g, id)); + // Hue centre 0.5605 of the circle, range 0.05, tolerance 0.1. + close("hslSecondary/key/hueCenter", 201.79); + close("hslSecondary/key/hueRange", 18.0); + close("hslSecondary/key/hueSoftness", 36.0); + // Lightness centre 0.435 +- 0.05, tolerance 0.1, as percent. + close("hslSecondary/key/lightnessLow", 38.53); + close("hslSecondary/key/lightnessHigh", 48.53); + close("hslSecondary/key/lightnessSoftness", 10.0); + assert_eq!(val(&g, "hslSecondary/key/hueEnabled"), Value::Bool(true)); + // With the key picked the correction applies; without, After Effects applies it to nothing + // (1200 `-16-sec-correction` renders as `-00-defaults`): left out and reported. + let (g, _) = run(&[(73, Rec::Data(hex(HSL_PICKED))), (86, Rec::Num(19.0, 1))]); + assert_eq!(num(&g, "hslSecondary/correction/temperature"), 19.0); + let (g, r) = run(&[(86, Rec::Num(19.0, 1))]); + assert_eq!(num(&g, "hslSecondary/correction/temperature"), 0.0); + let l = losses(&r); + assert!(l.len() == 1 && l[0].contains("HSL Secondary has no key colour picked") && l[0].contains("Temperature 19"), "{l:?}"); + // Damaged key data: reported, the default key kept. + let (g, r) = run(&[(73, Rec::Data(vec![7; 40]))]); + assert!(losses(&r).iter().any(|d| d.contains("HSL Secondary key data is unreadable")), "{:?}", losses(&r)); + assert_eq!(num(&g, "hslSecondary/key/hueRange"), 180.0); +} + +#[test] +fn undecoded_data_and_controls_without_counterpart_are_reported_when_set() { + let mut split = vec![0u8; 32]; + split[0] = 1; + let mut w = wheels(4); + w[8] = 1; + let (_, r) = run(&[(32, Rec::Data(split)), (47, Rec::Data(w)), (97, Rec::Num(1.0, 3)), (95, Rec::Data(utf16("my.cube")))]); + let l = losses(&r); + assert_eq!(l.len(), 4, "{l:?}"); + assert!(l.iter().any(|d| d.contains("Split Toning (ADBE Lumetri-0032) is not imported")), "{l:?}"); + assert!(l.iter().any(|d| d.contains("Wheels (ADBE Lumetri-0047) is not imported")), "{l:?}"); + assert!(l.iter().any(|d| d.contains("Legacy Tone And Saturation (ADBE Lumetri-0097) is on")), "{l:?}"); + assert!(l.iter().any(|d| d.contains("the Input LUT 'my.cube' is not imported")), "{l:?}"); +} + +#[test] +fn an_unknown_scale_or_parameter_is_reported_not_guessed() { + let mut d = defs(); + // Exposure with another valid range (an older or newer version): not guessed at. + if let Some((_, Def::Float { min, max, .. })) = d.iter_mut().find(|(s, _)| *s == 11) { + (*min, *max) = (-100.0, 100.0); + } + // A parameter this importer does not know, set. + d.push((200, Def::Float { name: "Future", default: 0.0, min: -1.0, max: 1.0 })); + let (g, r) = run_with(&d, &[(11, Rec::Num(3.0, 1)), (200, Rec::Num(0.5, 1))]); + assert_eq!(num(&g, "basicCorrection/tone/exposure"), 0.0); + let l = losses(&r); + assert!(l.iter().any(|x| x.contains("Exposure (ADBE Lumetri-0011) has the valid maximum Some(100.0), expected 7")), "{l:?}"); + assert!(l.iter().any(|x| x.contains("Future (ADBE Lumetri-0200) not imported")), "{l:?}"); +} diff --git a/crates/effects/src/color3.rs b/crates/effects/src/color3.rs index 4850710ab..d4cdedd98 100644 --- a/crates/effects/src/color3.rs +++ b/crates/effects/src/color3.rs @@ -705,32 +705,37 @@ fn lumetri(ctx: &EffectCtx, mut b: Buf) -> Buf { pub fn specs() -> Vec { let grey = || col(0.5, 0.5, 0.5); + // After Effects' valid ranges (a value past the slider's drag range is still kept: .aep + // import, 1200), the slider over the range After Effects' slider covers. + let wb = || slider(-300.0, 300.0, -100.0, 100.0, 1); + let tone = || slider(-150.0, 150.0, -100.0, 100.0, 1); + let sat = || slider(0.0, 300.0, 0.0, 200.0, 1); let mut lum = vec![ p("highDynamicRange", "High Dynamic Range", Value::Bool(false), ParamUi::Checkbox), p("basicCorrection/inputLut", "Input LUT", Value::Enum(0), popup(&["None", "Custom"])), // A .cube file path (or the file's text) for Input LUT = Custom. p("basicCorrection/inputLutFile", "Input LUT File", Value::Str(String::new()), ParamUi::Text), - p("basicCorrection/whiteBalance/temperature", "Temperature", num(0.0), bipolar()), - p("basicCorrection/whiteBalance/tint", "Tint", num(0.0), bipolar()), - p("basicCorrection/tone/exposure", "Exposure", num(0.0), slider(-5.0, 5.0, -5.0, 5.0, 2)), - p("basicCorrection/tone/contrast", "Contrast", num(0.0), bipolar()), - p("basicCorrection/tone/highlights", "Highlights", num(0.0), bipolar()), - p("basicCorrection/tone/shadows", "Shadows", num(0.0), bipolar()), - p("basicCorrection/tone/whites", "Whites", num(0.0), bipolar()), - p("basicCorrection/tone/blacks", "Blacks", num(0.0), bipolar()), + p("basicCorrection/whiteBalance/temperature", "Temperature", num(0.0), wb()), + p("basicCorrection/whiteBalance/tint", "Tint", num(0.0), wb()), + p("basicCorrection/tone/exposure", "Exposure", num(0.0), slider(-7.0, 7.0, -5.0, 5.0, 2)), + p("basicCorrection/tone/contrast", "Contrast", num(0.0), tone()), + p("basicCorrection/tone/highlights", "Highlights", num(0.0), tone()), + p("basicCorrection/tone/shadows", "Shadows", num(0.0), tone()), + p("basicCorrection/tone/whites", "Whites", num(0.0), tone()), + p("basicCorrection/tone/blacks", "Blacks", num(0.0), tone()), p("basicCorrection/tone/hdrWhite", "HDR White", num(100.0), slider(100.0, 10000.0, 100.0, 4000.0, 0)), - p("basicCorrection/tone/hdrSpecular", "HDR Specular", num(0.0), bipolar()), - p("basicCorrection/saturation", "Saturation", num(100.0), slider(0.0, 200.0, 0.0, 200.0, 1)), + p("basicCorrection/tone/hdrSpecular", "HDR Specular", num(0.0), tone()), + p("basicCorrection/saturation", "Saturation", num(100.0), sat()), p("creative/look", "Look", Value::Enum(0), popup(&LUMETRI_LOOKS)), p("creative/lookFile", "Look File", Value::Str(String::new()), ParamUi::Text), p("creative/lookIntensity", "Intensity", num(100.0), slider(0.0, 200.0, 0.0, 200.0, 1)), - p("creative/adjustments/fadedFilm", "Faded Film", num(0.0), pct()), + p("creative/adjustments/fadedFilm", "Faded Film", num(0.0), slider(0.0, 150.0, 0.0, 100.0, 1)), p("creative/adjustments/sharpen", "Sharpen", num(0.0), bipolar()), p("creative/adjustments/vibrance", "Vibrance", num(0.0), bipolar()), - p("creative/adjustments/creativeSaturation", "Saturation", num(100.0), slider(0.0, 200.0, 0.0, 200.0, 1)), + p("creative/adjustments/creativeSaturation", "Saturation", num(100.0), sat()), p("creative/adjustments/shadowTint", "Shadow Tint", grey(), ParamUi::Color), p("creative/adjustments/highlightTint", "Highlight Tint", grey(), ParamUi::Color), - p("creative/adjustments/tintBalance", "Tint Balance", num(0.0), bipolar()), + p("creative/adjustments/tintBalance", "Tint Balance", num(0.0), tone()), ]; const CURVES: [(&str, &str); 12] = [ ("curves/curveMasterShadows", "RGB Curve Shadows"), @@ -786,13 +791,13 @@ pub fn specs() -> Vec { p("hslSecondary/key/maskStyle", "Mask Style", Value::Enum(0), popup(&HSL_MASK_STYLES)), p("hslSecondary/key/invertMask", "Invert Mask", Value::Bool(false), ParamUi::Checkbox), p("hslSecondary/refine/denoise", "Denoise", num(0.0), pct()), - p("hslSecondary/refine/blur", "Blur", num(0.0), pct()), + p("hslSecondary/refine/blur", "Blur", num(0.0), slider(0.0, 1000.0, 0.0, 100.0, 1)), p("hslSecondary/correction/correctionWheel", "Correction", grey(), ParamUi::Color), - p("hslSecondary/correction/temperature", "Temperature", num(0.0), bipolar()), - p("hslSecondary/correction/tint", "Tint", num(0.0), bipolar()), - p("hslSecondary/correction/contrast", "Contrast", num(0.0), bipolar()), + p("hslSecondary/correction/temperature", "Temperature", num(0.0), wb()), + p("hslSecondary/correction/tint", "Tint", num(0.0), wb()), + p("hslSecondary/correction/contrast", "Contrast", num(0.0), tone()), p("hslSecondary/correction/sharpen", "Sharpen", num(0.0), bipolar()), - p("hslSecondary/correction/saturation", "Saturation", num(100.0), slider(0.0, 200.0, 0.0, 200.0, 1)), + p("hslSecondary/correction/saturation", "Saturation", num(100.0), sat()), p("vignette/vignetteAmount", "Amount", num(0.0), slider(-5.0, 5.0, -5.0, 5.0, 2)), p("vignette/vignetteMidpoint", "Midpoint", num(50.0), pct()), p("vignette/vignetteRoundness", "Roundness", num(0.0), bipolar()), diff --git a/docs/aep-format.md b/docs/aep-format.md index fcc8f53be..9a37b384f 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -534,6 +534,44 @@ suffix is a parameter id, not the `parT` position (Curves -0002 Channel before - -0005 Quality before -0004). API: `CurvesData`, `MeshData`, `curve_lut`. [V; details: corpus FORMAT S] +**Lumetri Color** (`ADBE Lumetri`, 1200: 58 compositions made by script in After Effects 2026, 32 bpc, +working space None; defaults, all set, one per section, valid minima and maxima, every popup item the +script can set, checkboxes toggled, keys, an expression): 131 `parT` entries. The `-NNNN` suffix is a +parameter id, not the position (`-0127` ... `-0130` sit between `-0005` and `-0026`), so the importer +keys by suffix. Float sliders (type 10) store the value After Effects shows; the valid ranges (`pard` +104 / 108) are wider than the slider's (112 / 116): Temperature / Tint -300..300 (slider -100..100), +Saturation 0..300 (0..200), Exposure -7..7 (-5..5), Contrast, Highlights, Shadows, Whites, Blacks, +HDR Specular, Tint Balance -150..150 (-100..100), Faded Film 0..150 (0..100), HDR White 100..1000, +HDR Range 100..10000, Refine Blur 0..1000 (0..30), Vignette Amount -5..5 (-3..3); the read-backs of +`-19-min` / `-20-max` equal the stored numbers. **Hidden controls are recorded in the default state** +(`tdsb` 3) and still hold the value After Effects reads back: High Dynamic Range `-0062` (1.0), +HDR Specular `-0064` (22), the second Look popup `-0128` (26.16 as set, read back 26), Legacy Tone And +Saturation `-0097`, Auto Color `-0124`, Face Detection `-0102`, the Auto Intensity `-0123` +(`EffectParam::recorded_value`). Each section has an Active checkbox (blank `pard` name; `-0003` Basic +Correction, `-0023` Creative, `-0037` Curves, `-0118` RGB Curves, `-0104` / `-0105` / `-0107` / `-0109` / +`-0111` the hue / saturation curves, `-0046` Color Wheels, `-0068` HSL Secondary, `-0050` Vignette, on +by default); off, the section does nothing (`-03-sec-basic-correction`, `-17-sec-vignette` render as +`-00-defaults`). Arbitrary data (type 11, a record and `aRbs` in every instance; empty is `fe fe`): +`-0001` an XML copy of the grade written only when the Look changes (not kept current: `-01-set-all` +holds defaults for controls set after the Look); `-0096` the Look's name, UTF-16LE (`SL BLUE ICE`; the +popup `-0025` itself has no record, `-0128` can disagree with it: `-07-sec-creative` names item 20 +while `-0128` holds 23), `-0095` likewise for the Input LUT (H, by analogy: no sample sets a LUT); +`-0129` the Look colour space's 16-byte id (the GUID `LookColorSpace` of `-0001`, byte-swapped; zero +for None; the Color Space popup `-0130` writes no record of its own, `-22-pop38-1` ... `-53-pop38-33`); +`-0032` Split Toning (32 zero bytes); `-0039` the four RGB curves and `-0042`, `-0106`, `-0108`, +`-0110`, `-0112` the hue / saturation curves, 520 bytes each: `i32` type (3 for the hue curves, 2 for +Luma vs Sat and Sat vs Sat, 0 for an RGB curve), `u32` point count, then little-endian `f64` pairs x, y +(RGB: (0, 0), (1, 1) by default; hue / saturation: no points, and the selector colours `-0113` ... +`-0117` add three points at y = 0, the hue picked +- 0.0556; no sample moves a point, so y's scale is +H); `-0047` Color Wheels (4 x `f64` triples, 0, 0.5, 0 each) and `-0085` the HSL correction wheels (8 +triples, the same); `-0073` the HSL key, 40 bytes: a flag byte (1 once a colour was picked), then hue, +saturation, lightness, each centre, range, tolerance (`f32` LE, fractions) and an enabled byte (fresh: +0.5, 0, 0, on; picking RGB 0.02 0.55 0.85 gives hue centre 0.5605 = that colour's hue and lightness +centre 0.435 = its HSL lightness, range 0.05, tolerance 0.1: `-01-set-all`; whether range is the half +or full width is H); `-0122` the Auto Tone analysis (UTF-16 JSON). Curves, LUTs and wheels are not +scriptable: the presets for them are still to be made. [V for the sliders, checkboxes, popups and the +`tdsb` 3 values; H where marked] + ### 3.8 Masks and bezier paths (`tdgp[ADBE Mask Parade]`) | Field | Chunk path | Offset | Type | Meaning | Evidence | Conf. | diff --git a/docs/aep-import.md b/docs/aep-import.md index baeb8d0e1..07c089782 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -1,6 +1,6 @@ # After Effects project import -> **Last reviewed:** 2026-10-19 · **Last updated:** 2026-10-19 · **Change:** Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-05 · **Change:** Lumetri Color is imported (1200: every slider, checkbox and popup with a counterpart, by match name, also from records in the default state; switched-off sections, LUTs, looks, undecoded curves and wheels are reported); before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 File ▸ Import ▸ After Effects Project… (`file.importAep`) reads an `.aep` or its XML form `.aepx` and adds its items to the open project, in a folder named after the file (as After Effects does when it imports a project). @@ -58,7 +58,7 @@ outside the sampled ones keeps the current value and is reported as `project set | Text | the Source Text of a text layer: the text (paragraphs separated by CR; a hard line break is a CR too; U+0003 is laid out by After Effects exactly like a CR (386 against 387) and becomes a CR), per style run the font (PostScript name), size, fill and stroke colour, whether fill and stroke are drawn, stroke width, stroke over fill (one switch for the text), tracking, leading (automatic or explicit), faux bold, faux italic, all caps, small caps, superscript and subscript (526-528), horizontal and vertical scale, baseline shift, automatic kerning (metrics, optical, none), ligatures, tsume (538-578); per paragraph the justification (left, centre, right and the four justified variants), first-line / start / end indent, space before / after, direction, composer, hanging punctuation (in a right-to-left paragraph the stored justification is relative to the direction: After Effects lays 0 and 3 out flush right and 1 and 4 flush left, read-back test t7, so they arrive as right / left); point text or box text (box size and top-left in layer pixels); vertical right-to-left text (575) | | Text animators | every animator with its name and its range selectors (Start, End, Offset, or the Index range when Units is Index; Units, Based On, Mode, Amount, Shape, Smoothness, Ease High / Low, Randomize Order, Random Seed) and the properties Anchor Point, Position, Scale, Skew, Skew Axis, Rotation, Opacity, Fill Color, Stroke Color, Stroke Width, Tracking Amount, Line Spacing, Blur, Character Offset and Character Range, with keys and expressions (579-594, 114, 728). Values are stored as After Effects shows them; popups are 1-based in the file | | Masks | every mask of an imported layer, in stack order (on text and shape layers, which have no source, the stored mask box is in layer pixels: 705, 706): name, path (vertices and tangents in layer pixels, open or closed), mode (None, Add, Subtract, Intersect, Lighten, Darken, Difference), inverted, feather (x, y in pixels), opacity (percent), expansion (pixels); path keyframes (time, linear or hold sides, roving, label), feather / opacity / expansion keyframes with ease | -| Effects | **Curves** (961-964: the five curves' points from the arbitrary data as EffectCraft's `x,y` points in 0..1; render within 6 levels: EffectCraft interpolates monotone, After Effects stores a natural spline) and **Mesh Warp** (965, 985: Rows, Columns, Quality and the Distortion Mesh as per-vertex offsets in layer pixels; Bezier handles moved on their own are kept as eight extra numbers of the vertex for the export and reported, EffectCraft draws the mesh bilinear; the header is rows + 1, columns + 1; render 985: 1.4 % of pixels > 2 levels around the moved vertex), **Tint**, **Gradient Ramp**, **Exposure**, **Invert**, **Fast Box Blur**, **Gaussian Blur**, **Drop Shadow**, **Glow**, **Fill**, **Hue/Saturation**, **Levels**, **Timewarp** (Method, Adjust Time By, Speed with keys, Source Frame; 529-533), **Directional Blur**, **Radial Blur**, **CC Radial Blur**, **Venetian Blinds**, **Channel Blur**, **Black & White**, **Tritone**, **Luma Key**, **Simple Choker**, **Matte Choker**, **Transform** (all parameters; 711-723) and the expression controls **Slider**, **Angle**, **Checkbox**, **Color** and **Point Control** (table below) on any imported layer (also text and shape layers and layers with masks), in stack order, with the instance name, every parameter (static value or default) and its keyframes; also the second and later instances of a type, whose parameter definitions are only in the project's shared definition list. Other effects are reported (below) | +| Effects | **Lumetri Color** (1200; see *Lumetri Color* below), **Curves** (961-964: the five curves' points from the arbitrary data as EffectCraft's `x,y` points in 0..1; render within 6 levels: EffectCraft interpolates monotone, After Effects stores a natural spline) and **Mesh Warp** (965, 985: Rows, Columns, Quality and the Distortion Mesh as per-vertex offsets in layer pixels; Bezier handles moved on their own are kept as eight extra numbers of the vertex for the export and reported, EffectCraft draws the mesh bilinear; the header is rows + 1, columns + 1; render 985: 1.4 % of pixels > 2 levels around the moved vertex), **Tint**, **Gradient Ramp**, **Exposure**, **Invert**, **Fast Box Blur**, **Gaussian Blur**, **Drop Shadow**, **Glow**, **Fill**, **Hue/Saturation**, **Levels**, **Timewarp** (Method, Adjust Time By, Speed with keys, Source Frame; 529-533), **Directional Blur**, **Radial Blur**, **CC Radial Blur**, **Venetian Blinds**, **Channel Blur**, **Black & White**, **Tritone**, **Luma Key**, **Simple Choker**, **Matte Choker**, **Transform** (all parameters; 711-723) and the expression controls **Slider**, **Angle**, **Checkbox**, **Color** and **Point Control** (table below) on any imported layer (also text and shape layers and layers with masks), in stack order, with the instance name, every parameter (static value or default) and its keyframes; also the second and later instances of a type, whose parameter definitions are only in the project's shared definition list. Other effects are reported (below) | | Layer styles | Drop Shadow, Inner Shadow, Outer Glow, Inner Glow, Bevel and Emboss, Satin, Color Overlay, Gradient Overlay and Stroke when applied (switch group `tdsb` 1): every parameter, starting from After Effects' defaults (Use Global Light is off there), with keys on scalars and colours and expressions; Blending Options: Fill Opacity, the channel switches, Blend Interior Styles as Group, Use Blend Ranges, and Global Light Angle / Altitude, which the composition adopts (one setting per composition here). Units and popups through `effectcraft_aep::style_map` (aep-format 3.12b). Render check against After Effects (`proof\styles`): drop shadow, inner shadow, satin, colour overlay, stroke within a few levels; glow and bevel intensities and the Gradient Overlay direction differ in EffectCraft's renderer | | Shape layers | groups (nested, with anchor, position, scale, skew, skew axis, rotation, opacity), rectangle (size, position, roundness), ellipse (size, position), polystar (star or polygon, points, position, rotation, radii, roundness), bezier path (vertices and tangents in layer pixels, path keys), fill (colour, opacity, fill rule), stroke (colour, opacity, width, cap, join, miter limit), gradient fill and stroke (linear or radial, start and end point; the default white-to-black colours), trim paths (start, end, offset, trim multiple shapes), round corners (radius), merge paths (mode; 707, 708), offset paths (amount, line join, miter limit, copies, copy offset; 710; After Effects offsets copy *i* by Amount x (*i* + Copy Offset), default 1, EffectCraft by Amount x (*i* + 1 + copyOffset), so the copy offset arrives 1 less, also its keys; an expression on it is kept and reported; 826-828, 828 renders equal to After Effects on every grid point), repeater (copies, offset, composite, transform; 709), in file order, with keyframes and expressions, the operators' defaults as After Effects reads them back (731); the layer anchor in pixels; 238-242, 330-333, 451-484, 519, 705-710, 731 | | Markers | layer and composition markers: time (layer time for layer markers, exact frames), duration (1/600 s units, 509, 510), comment, chapter, URL, frame target, label, cue point (name, event or navigation, parameters); 122, 123, 249, 347-349, 509, 510 | @@ -179,6 +179,7 @@ parameter gets the file's value: | `ADBE Easy Levels2` 1 Channel, 2 Histogram data, 8 / 9 Clip To Output Black / White | `channel`; `inBlack`, `inWhite`, `gamma`, `outBlack`, `outWhite` from the data; `clipToOutputBlack`, `clipToOutputWhite` | popups by name (After Effects' items On, Off, Off for 32 bpc Color are in another order here); the RGB values are the `f32` of the arbitrary data; the per-channel values are reported when they are not at their defaults (not sampled) | 511-516 | | Batch 6 (903 defaults, 904 / 905 set values): `ADBE Brightness & Contrast 2`, `ADBE Turbulent Displace`, `ADBE Fractal Noise`, `ADBE Laser` (Beam), `CC Radial Fast Blur`, `ADBE Noise2`, `ADBE Pro Levels2` (Levels (Individual Controls)), `ADBE Corner Pin`, `ADBE Grid`, `ADBE Sharpen`, `ADBE Linear Color Key2`, `ADBE Radial Wipe`, `ADBE Remove Color Matting`, `CC Bend It`, `CC Power Pin`, `ADBE 4ColorGradient`, `ADBE Change Color`, `ADBE Extract`, `ADBE Noise HLS2`, `ADBE Scatter`, `CC Light Sweep`, `ADBE Compound Blur`, `ADBE Displacement Map`, `ADBE Set Matte3`, `ADBE Gaussian Blur` (legacy), `ADBE OFMotionBlur` (Pixel Motion Blur), `ADBE Layer Control` | the EffectCraft effect of the same name, every parameter EffectCraft has | sliders as stored (each checked against its valid maximum); Beam Length, Time and Softness and CC Power Pin Perspective are 0..1 fractions, percentages here; points as layer pixels; popups by item name (Set Matte's `Red Channel` ... is `Red` ...); twirl-down group markers and the Levels histogram carry nothing; **layer parameters** (below) | 903, 904, 905; 220, 320, 321 | | `ADBE Slider Control`, `ADBE Angle Control`, `ADBE Checkbox Control`, `ADBE Color Control`, `ADBE Point Control` (parameter 1) | `ec.control.slider` `slider`, `ec.control.angle` `angle`, `ec.control.checkbox` `checkbox`, `ec.control.color` `color`, `ec.control.point` `point` | float (valid max 1 000 000) as stored; degrees; checkbox; colour; point in layer pixels | 071, 073, 217-219, 221, 304, 310 | +| `ADBE Lumetri` by match-name suffix (1200), not by index: -0062 High Dynamic Range; -0063 HDR White; -0007 / -0008 Temperature / Tint; -0020 Saturation; -0011 Exposure; -0012 ... -0016 Contrast, Highlights, Shadows, Whites, Blacks; -0064 HDR Specular; -0026 Look Intensity; -0028 Faded Film; -0029 Sharpen; -0030 Vibrance; -0031 Saturation; -0033 Tint Balance; -0074 Show Mask; -0075 mask style; -0076 Invert Mask; -0081 Denoise; -0082 Blur; -0086 ... -0090 the HSL correction's Temperature, Tint, Contrast, Sharpen, Saturation; -0051 ... -0054 Vignette Amount, Midpoint, Roundness, Feather; -0073 the HSL key data | `highDynamicRange`, `basicCorrection/tone/hdrWhite`, `basicCorrection/whiteBalance/temperature` / `tint`, `basicCorrection/saturation`, `basicCorrection/tone/exposure`, `.../contrast`, `highlights`, `shadows`, `whites`, `blacks`, `hdrSpecular`, `creative/lookIntensity`, `creative/adjustments/fadedFilm`, `sharpen`, `vibrance`, `creativeSaturation`, `tintBalance`, `hslSecondary/key/showMask`, `maskStyle`, `invertMask`, `hslSecondary/refine/denoise`, `blur`, `hslSecondary/correction/*`, `vignette/*`; `hslSecondary/key/hue*`, `saturation*`, `lightness*` | sliders: the stored number x 1 (valid maxima 1000, 300, 300, 7, 150, 200, 150, 100, 100, 300, 150, 100, 1000, 5, 100 as in `pard`); checkboxes and the popup as elsewhere, **also from records in the default state** (`tdsb` 3); the key: hue centre x 360 degrees, range x 360 as the half width, tolerance x 360 as the softness, saturation and lightness centre +- range x 100 percent, tolerance x 100, once a colour was picked | 1200: all 62 read-backs of mapped controls in sections that act equal after import (min, max, sections, popups); the other 61 lie in sections that do nothing and are reported; keys and the expression on HDR White (`-56-keys`, `-57-expr`) | - **Nothing to import:** the implicit input-layer parameter (index 0), the Swap Colors buttons (Tint 4, Ramp 8: they always have a record and hold no value), Exposure's twirl-down group @@ -230,6 +231,28 @@ parameter gets the file's value: column just outside the layer: After Effects' Glow draws a dark halo about 10 px wide outside the layer (red 0.55, 0.65, 0.74, 0.92 at 0, 1, 2, 5 px), EffectCraft's Glow none (an effect difference, recorded for a separate fix). +- **Lumetri Color** (1200, `crates/aep/src/import/lumetri.rs`). The parameters are found by match + name (After Effects inserted `-0127` ... `-0130` mid-list; five tone sliders share one valid range, + so an index shift would not be caught). Values come from every value record, also those in the + default state (`tdsb` 3), where Lumetri keeps High Dynamic Range, HDR Specular and the second Look + popup. EffectCraft's Lumetri has no section switches: a section that is off in After Effects + (Basic Correction, Creative, Curves / RGB Curves, each hue / saturation curve, Color Wheels, HSL + Secondary, Vignette), and HSL Secondary without a picked key colour, does nothing there, so its set + values are listed in one report line and not imported (the frame stays as After Effects draws it). + The Input LUT and the Look are reported by name (LUT files are not in the project). Controls + without a counterpart (the Auto tone's Intensity, RGB Curves' HDR Range, Color Wheels' HDR White and + Face Detection, Legacy Tone And Saturation, Auto Color, SpeedGrade Custom Layer), the eyedropper + colours, and the RGB curves, colour wheels, Split Toning, HSL correction wheels and moved hue / + saturation curve points (not sampled) are reported when set. Render check at 0.5 s against After + Effects' 32 bpc PNGs (stored as value x 6553.5 in 16 bits, so read as raw / 6553.5): every + composition whose grade After Effects draws as identity (defaults, set-all with every section off, + the switched-off sections, the popups, keys, expression) is within 1/255 in EffectCraft; the + graded ones differ by EffectCraft's Lumetri formulas, not by the import: White Balance (sec-color: + After Effects warms and brightens, max 0.10 above the default; EffectCraft darkens, mean error + 0.015), Exposure / tone (sec-light: After Effects rolls highlights off and lifts the blacks to 0.18, + EffectCraft clips at 1 and keeps black at 0; mean 0.12), Creative adjustments (sec-adjustments: + Faded Film lifts black to 0.07 in After Effects, 0.035 here) and the extremes (min / max: mean 0.063 / + 0.454; max: After Effects' vignette at +5 and the tone sliders give mid grey, EffectCraft white). - **Colour.** After Effects stores the colour its picker shows as ARGB doubles 0..255 in the project's working-space encoding; EffectCraft's parameter colours are straight RGBA 0..1 that the render pipeline hands to the effect as stored (`crates/render/src/color.rs`: authored colours are @@ -389,5 +412,5 @@ crate yet. ## Not done yet -Effects outside the table (Lumetri; EXtractoR: its channel choice is arbitrary data; Stroke: its path is a mask reference), environment lights, the ease of bezier Orientation keys, wiggly and expression selectors and the other text animator properties (Character Value, Character Alignment other than its default, the Hue / Saturation / Brightness / Opacity variants, 3D rotations, variable font axes), manual kerning, No Break, the stroke line join, the Japanese leading model, Source Text keyframes, layer-style gradients, Pattern Overlay and the switch state 3, Mesh Warp handles moved on their own, +Lumetri Color's section switches (a switched-off section is reported and its values left out), its LUTs and looks (files After Effects names: reported by name), its RGB curves, colour wheels, Split Toning and HSL correction wheels (not sampled: reported when set) and moved hue / saturation curve points (the scale of y is not sampled); effects outside the table (EXtractoR: its channel choice is arbitrary data; Stroke: its path is a mask reference), environment lights, the ease of bezier Orientation keys, wiggly and expression selectors and the other text animator properties (Character Value, Character Alignment other than its default, the Hue / Saturation / Brightness / Opacity variants, 3D rotations, variable font axes), manual kerning, No Break, the stroke line join, the Japanese leading model, Source Text keyframes, layer-style gradients, Pattern Overlay and the switch state 3, Mesh Warp handles moved on their own, separated dimensions and footage interpretation need verified layouts in the reader first. diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 5ef21511d..f94fc3789 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -22,7 +22,7 @@ trips and renders, checked by tests, not against After Effects), **partial** (na | Format | After Effects | EffectCraft read | Write | Fidelity / notes | Tested by | |---|---|---|---|---|---| -| Project `.aep` | R/W (native) | partial | none | Main file format; the clean-room method is in [aep-format.md](aep-format.md), the maintainers' decision on it is open (#190). `crates/aep` reads and writes the RIFX container losslessly (byte-identical round trip) with a typed view of the decoded fields, from our own generated files only ([aep-format.md](aep-format.md)). File ▸ Import ▸ After Effects Project… (`file.importAep`, `effectcraft_aep::import`): folders, project settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text with animators, shapes, cameras, lights, track mattes, markers, time remapping, layer styles, the built-in effects of a table (Curves, Levels, Glow, Fractal Noise, Gradient Ramp, … and the expression controls), footage and image sequences; everything else is reported ([aep-import.md](aep-import.md)). Export to `.aep` is not built yet. Effects outside the table (plug-ins, Lumetri, …) are not converted | `crates/aep` tests (`tests/it`: `container`, `import`) | +| Project `.aep` | R/W (native) | partial | none | Main file format; the clean-room method is in [aep-format.md](aep-format.md), the maintainers' decision on it is open (#190). `crates/aep` reads and writes the RIFX container losslessly (byte-identical round trip) with a typed view of the decoded fields, from our own generated files only ([aep-format.md](aep-format.md)). File ▸ Import ▸ After Effects Project… (`file.importAep`, `effectcraft_aep::import`): folders, project settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text with animators, shapes, cameras, lights, track mattes, markers, time remapping, layer styles, the built-in effects of a table (Lumetri Color, Curves, Levels, Glow, Fractal Noise, Gradient Ramp, … and the expression controls), footage and image sequences; everything else is reported ([aep-import.md](aep-import.md)). Export to `.aep` is not built yet. Effects outside the table (plug-ins, …) are not converted | `crates/aep` tests (`tests/it`: `container`, `import`) | | XML project `.aepx` | R/W | partial | none | Read into the same chunk tree as `.aep` by `crates/aep` (`read_aepx`; mapping in [aep-format.md](aep-format.md) section 1), so `file.importAep` imports it with the same coverage as `.aep`; not written | `crates/aep` `aepx` tests (container and import) | | Templates `.aet` / `.aetx` | R/W | none | none | Own `.ectemplate` (Home ▸ Templates, Save as Template) | `templates.rs` tests | | Motion Graphics template `.mogrt` | W (and R in Premiere) | none | none | Own `.ectemplate` Essential Graphics templates; FilmCraft rejects our `.fcgt` export (#611) | `essential.rs` tests | From 9c25944c700ab7ac8463462b006f34425b8e6a39 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 17:45:19 +0100 Subject: [PATCH 015/350] AEP import: EXtractoR's and IDentifier's chosen channels by name Presets of our own (blocks 1300 and 1310) show what the Channel Info stores for a chosen channel. - EXtractoR: each record is a kind in byte 0 and the channel name. Named channels are imported by name (Z, Z, Z renders equal to After Effects) and `(copy)` as the input's own channel. The kind is byte 0 only: one definition default has `02 00 00 32`. - IDentifier: byte 19 holds the chosen channel's name, `Object ID` when none is chosen. After Effects lists only UINT channels and leaves the layer unchanged without one, which the loss now says. A chosen `ObjectID` / `MaterialID` channel (any case) is the one EffectCraft reads itself, so it imports without a loss; other names are reported. Co-Authored-By: Claude Opus 5.5 --- crates/aep/src/container/arbitrary.rs | 39 +++++++++--- crates/aep/src/container/effect_map.rs | 4 +- crates/aep/src/import/effects.rs | 49 +++++++++++---- .../aep/tests/it/import/effects_thirdparty.rs | 62 +++++++++++++++---- docs/aep-format.md | 24 +++++-- docs/aep-import.md | 14 ++++- 6 files changed, 149 insertions(+), 43 deletions(-) diff --git a/crates/aep/src/container/arbitrary.rs b/crates/aep/src/container/arbitrary.rs index 7b47e83c7..cbcc56c53 100644 --- a/crates/aep/src/container/arbitrary.rs +++ b/crates/aep/src/container/arbitrary.rs @@ -309,10 +309,14 @@ impl MeshData { } /// EXtractoR's Channel Info data (`EXtractoR-0013`, 688 bytes, the plug-in's own little-endian -/// layout; read from the default instances of the importer block of 2026-11-05, where no channel -/// was changed): a `u32` 1 and 28 bytes, then four records of 164 bytes (Red, Green, Blue, Alpha), -/// each a `u32` kind (1 a named channel, 2 the input's channel: `(copy)` on Alpha) and the channel -/// name from byte 35, NUL-terminated. Only the default (`R`, `G`, `B`, `(copy)`) is sampled. +/// layout; read from the instances of the importer block of 2026-11-05: the defaults and a preset +/// of our own that reads `Z` into Red, Green and Blue): a `u32` 1 and 28 bytes, then four records +/// of 164 bytes (Red, Green, Blue, Alpha), each a kind in byte 0 (1 a channel of the footage by +/// name, 2 the input's own channel: `(copy)`; bytes 1..4 are not always zero, `0x32` at 3 in one +/// definition's default, so only byte 0 is read) and the channel name from byte 35, +/// NUL-terminated. The other bytes are not read (they differ between instances with the same +/// channels; the `u32` at 4 of a chosen channel looks like its position in the footage's sorted +/// channel list, 13 of 14 for `Z`, where the defaults hold 0). Only the name is read. #[derive(Debug, Clone, PartialEq, Eq)] pub struct ExtractorChannels { /// Per output channel (red, green, blue, alpha): the kind and the name. @@ -333,7 +337,7 @@ impl ExtractorChannels { for (k, slot) in channels.iter_mut().enumerate() { let at = 32usize.checked_add(k.checked_mul(Self::RECORD)?)?; let rec = data.get(at..at.checked_add(Self::RECORD)?)?; - let kind = u32::from_le_bytes(*rec.first_chunk::<4>()?); + let kind = u32::from(*rec.first()?); let name = rec.get(35..)?; let end = name.iter().position(|b| *b == 0)?; let name = std::str::from_utf8(name.get(..end)?).ok()?; @@ -348,11 +352,30 @@ impl ExtractorChannels { let want = [(1, "R"), (1, "G"), (1, "B"), (2, "(copy)")]; self.channels.iter().zip(want).all(|((k, n), (wk, wn))| *k == wk && n == wn) } + + /// Per output channel (red, green, blue, alpha) the channel it reads, `Err` with the + /// position of the first record of another kind or without a name: a named channel by its + /// name, `(copy)` as the input's own channel of the same position (`R`, `G`, `B`, `A`). + pub fn sources(&self) -> Result<[String; 4], usize> { + const OWN: [&str; 4] = ["R", "G", "B", "A"]; + let mut out: [String; 4] = Default::default(); + for (k, ((kind, name), slot)) in self.channels.iter().zip(out.iter_mut()).enumerate() { + *slot = match (kind, name.trim()) { + (1, n) if !n.is_empty() => n.to_string(), + (2, _) => OWN.get(k).ok_or(k)?.to_string(), + _ => return Err(k), + }; + } + Ok(out) + } } -/// IDentifier's Channel Info data (`IDentifier-0005`, 148 bytes): the channel type's name, -/// NUL-terminated from byte 19 (`Object ID` in every sample of the importer block of 2026-11-05; -/// the bytes around it differ between instances with the same settings and are not read). +/// IDentifier's Channel Info data (`IDentifier-0005`, 148 bytes): the chosen channel's name, +/// NUL-terminated from byte 19, or `Object ID` when none is chosen (importer blocks 1300 and 1310 +/// of 2026-11-05: `objectId` in a preset of our own that chose that UINT channel, `Object ID` in +/// every other instance). The bytes around it differ between instances with the same settings +/// and are not read (a chosen channel has 15 at byte 4, which looks like its position in the +/// EXR's sorted channel list, as for EXtractoR). pub fn identifier_channel_type(data: &[u8]) -> Option<&str> { let s = data.get(19..)?; let end = s.iter().position(|b| *b == 0)?; diff --git a/crates/aep/src/container/effect_map.rs b/crates/aep/src/container/effect_map.rs index 46fcf39ea..7f426bcd1 100644 --- a/crates/aep/src/container/effect_map.rs +++ b/crates/aep/src/container/effect_map.rs @@ -138,9 +138,9 @@ pub enum Extra { Curves, /// Mesh Warp's Distortion Mesh (965, 985, `MeshData`). MeshWarp, - /// EXtractoR's Channel Info (`ExtractorChannels`): the default channels only. + /// EXtractoR's Channel Info (`ExtractorChannels`): the channels by name. Extractor, - /// IDentifier's Channel Info (`identifier_channel_type`): Object ID only. + /// IDentifier's Channel Info (`identifier_channel_type`): the chosen channel by name. Identifier, } diff --git a/crates/aep/src/import/effects.rs b/crates/aep/src/import/effects.rs index a9fa3c2c7..5a8a60285 100644 --- a/crates/aep/src/import/effects.rs +++ b/crates/aep/src/import/effects.rs @@ -465,44 +465,67 @@ fn import_extra(fx: &AepEffect<'_, '_>, extra: Extra, p: &EffectParam<'_, '_>, h Some(m) => import_mesh(&m, here, size, group, col), None => col.lose(LossCategory::DamagedData, format!("{here}: the Distortion Mesh is unreadable, defaults used")), }, - Extra::Extractor => match ExtractorChannels::parse(data) { - // The fresh channels: EffectCraft's own R, G, B, A (its A is the input's alpha, After - // Effects' `(copy)`). - Some(c) if c.is_default() => { - for (id, name) in [("red", "R"), ("green", "G"), ("blue", "B"), ("alpha", "A")] { + Extra::Extractor => match ExtractorChannels::parse(data).map(|c| (c.sources(), c)) { + // Each output channel reads the footage's channel of the stored name, which + // EffectCraft's EXtractoR finds by the same name (`Z`); After Effects' `(copy)` is the + // input's own channel, EffectCraft's R, G, B or A (the fresh channels are R, G, B, A). + Some((Ok(names), _)) => { + for (id, name) in ["red", "green", "blue", "alpha"].into_iter().zip(names) { if let Some(prop) = group.prop_mut(id) { - prop.value = Value::Str(name.into()); + prop.value = Value::Str(name); } } } - Some(c) => { + Some((Err(k), c)) => { let names: Vec<&str> = c.channels.iter().map(|(_, n)| n.as_str()).collect(); col.lose( LossCategory::Effects, - format!("{here}: the channels {} are not imported (only the default channels are sampled), R, G, B, A used", names.join(", ")), + format!( + "{here}: the channels {} are not imported (record {} is of an unknown kind or names no channel), R, G, B, A used", + names.join(", "), + k.saturating_add(1) + ), ); } None => col.lose(LossCategory::Effects, format!("{here}: the Channel Info data is not in the sampled layout, R, G, B, A used")), }, Extra::Identifier => match crate::identifier_channel_type(data) { + // No channel chosen (the type's label): After Effects lists only the footage's integer + // (UINT) channels and, with none chosen, leaves the layer unchanged, where EffectCraft + // draws the footage's object ID channel. Some("Object ID") => { if let Some(prop) = group.prop_mut("channelType") { prop.value = Value::Enum(0); } - // The data names the type, not the channel After Effects reads: with this Channel - // Info After Effects left the sampled EXR unchanged, where EffectCraft draws the - // footage's object ID channel. col.lose( LossCategory::Effects, - format!("{here}: the channel IDentifier reads (Channel Info) is not decoded; EffectCraft uses the footage's object ID channel"), + format!("{here}: no ID channel is chosen (Channel Info), so After Effects leaves the layer unchanged; EffectCraft draws the footage's object ID channel"), ); } - other => col.lose(LossCategory::Effects, format!("{here}: the channel type {other:?} is not imported (only Object ID is sampled), Object ID used")), + // A chosen channel, by name. EffectCraft's IDentifier has no channel of its own: it + // reads the footage's first `ObjectID` (`MaterialID`) channel, names compared without + // regard to case, so a channel of that name is the one After Effects reads. + Some(name) => match IDENTIFIER_OWN.iter().position(|own| own.eq_ignore_ascii_case(name.trim())) { + Some(kind) => { + if let Some(prop) = group.prop_mut("channelType") { + prop.value = Value::Enum(u32::try_from(kind).unwrap_or(0)); + } + } + None => col.lose( + LossCategory::Effects, + format!("{here}: the ID channel '{name}' (Channel Info) is not imported; EffectCraft reads the footage's object ID channel (ObjectID, object_id, objectId, id)"), + ), + }, + None => col.lose(LossCategory::Effects, format!("{here}: the Channel Info data is not in the sampled layout, the object ID channel used")), }, Extra::None => {} } } +/// The channels EffectCraft's IDentifier reads first for its Channel Type Object ID and Material +/// ID (`AuxChannels::object_id`, `material_id`). +const IDENTIFIER_OWN: [&str; 2] = ["ObjectID", "MaterialID"]; + /// Curves' EffectCraft parameters, in the stored order (RGB, Red, Green, Blue, Alpha). const CURVE_IDS: [&str; 5] = ["rgb", "red", "green", "blue", "alpha"]; diff --git a/crates/aep/tests/it/import/effects_thirdparty.rs b/crates/aep/tests/it/import/effects_thirdparty.rs index 20f0b8230..7bf1ad3c8 100644 --- a/crates/aep/tests/it/import/effects_thirdparty.rs +++ b/crates/aep/tests/it/import/effects_thirdparty.rs @@ -112,6 +112,11 @@ fn extractor_channel_info_layout() { assert!(!c.is_default()); assert_eq!(c.channels[0], (1, "Z".to_string())); assert_eq!(ExtractorChannels::parse(&[0u8; 10]), None); + // The kind is byte 0 of a record: the definition default of `extractor-00-defaults` has + // `02 00 00 32` on Alpha, still the input's own channel. + let c = ExtractorChannels::parse(&channels([(1, "R"), (1, "G"), (1, "B"), (0x3200_0002, "(copy)")])).expect("parses"); + assert!(c.is_default()); + assert_eq!(c.sources(), Ok(["R", "G", "B", "A"].map(String::from))); } #[test] @@ -129,12 +134,32 @@ fn extractor_points_unmult_and_default_channels() { } } +/// `extractor-07-preset-channel-z` (a preset of our own): `Z` into Red, Green and Blue, Alpha +/// `(copy)`. EffectCraft's EXtractoR reads the footage's channel by the same name. #[test] -fn extractor_channels_other_than_the_default_are_reported() { +fn extractor_channels_are_imported_by_name() { let (g, r) = run(&extractor(vec![], channels([(1, "Z"), (1, "Z"), (1, "Z"), (2, "(copy)")])), "ec.3d.extractor"); - let found = losses(&r, LossCategory::Effects, "EXtractoR"); - assert!(found.len() == 1 && found[0].contains("the channels Z, Z, Z, (copy) are not imported"), "{found:?}"); - assert_eq!(v(&g.expect("imported"), "red"), Value::Str("R".into())); + assert_eq!(losses(&r, LossCategory::Effects, "EXtractoR"), Vec::::new()); + let g = g.expect("imported"); + for (id, want) in [("red", "Z"), ("green", "Z"), ("blue", "Z"), ("alpha", "A")] { + assert_eq!(v(&g, id), Value::Str(want.into()), "{id}"); + } + // `(copy)` on another channel is that channel of the input. + let (g, _) = run(&extractor(vec![], channels([(2, "(copy)"), (1, "CryptoObject00.R"), (2, "(copy)"), (1, "Z")])), "ec.3d.extractor"); + let g = g.expect("imported"); + for (id, want) in [("red", "R"), ("green", "CryptoObject00.R"), ("blue", "B"), ("alpha", "Z")] { + assert_eq!(v(&g, id), Value::Str(want.into()), "{id}"); + } +} + +#[test] +fn extractor_channels_of_an_unknown_kind_are_reported() { + for recs in [[(1, "Z"), (3, "Z"), (1, "Z"), (2, "(copy)")], [(1, "Z"), (1, ""), (1, "Z"), (2, "(copy)")]] { + let (g, r) = run(&extractor(vec![], channels(recs)), "ec.3d.extractor"); + let found = losses(&r, LossCategory::Effects, "EXtractoR"); + assert!(found.len() == 1 && found[0].contains("are not imported (record 2 is of an unknown kind or names no channel)"), "{recs:?}: {found:?}"); + assert_eq!(v(&g.expect("imported"), "red"), Value::Str("R".into()), "{recs:?}"); + } } /// IDentifier's Channel Info: the channel type's name at 19 among bytes that are not read. @@ -164,16 +189,31 @@ fn identifier(values: Vec<(usize, f64)>, data: Vec) -> Fx { #[test] fn identifier_display_id_and_channel_type() { assert_eq!(identifier_channel_type(&id_info("Object ID")), Some("Object ID")); - // `identifier-01-set-all` (ID 10) and `identifier-05-pop2-3` (Alpha Matte). + // `identifier-01-set-all` (ID 10) and `identifier-05-pop2-3` (Alpha Matte): no channel chosen + // (After Effects found none or none was picked), so After Effects leaves the layer unchanged. let (g, r) = run(&identifier(vec![(2, 3.0), (3, 10.0)], id_info("Object ID")), "ec.3d.identifier"); - // Which channel After Effects reads is not decoded (it left the sampled EXR unchanged): reported. let found = losses(&r, LossCategory::Effects, "IDentifier"); - assert!(found.len() == 1 && found[0].contains("is not decoded"), "{found:?}"); + assert!(found.len() == 1 && found[0].contains("no ID channel is chosen"), "{found:?}"); let g = g.expect("imported"); assert_eq!((v(&g, "display"), v(&g, "channelType")), (Value::Enum(2), Value::Enum(0))); near(&g, "id", 10.0); - // A channel type that is not sampled is reported. - let (_, r) = run(&identifier(vec![], id_info("Material ID")), "ec.3d.identifier"); - let found = losses(&r, LossCategory::Effects, "IDentifier"); - assert!(found.len() == 1 && found[0].contains("\"Material ID\""), "{found:?}"); +} + +/// `identifier-09-preset-channel-objectid-id2` (1310, a preset of our own): the UINT channel +/// `objectId` chosen and ID 2. EffectCraft's IDentifier reads that channel itself (`ObjectID` +/// without regard to case), so nothing is lost; another name is reported. +#[test] +fn identifier_chosen_channels_by_name() { + for (name, kind) in [("objectId", 0), ("ObjectID", 0), ("materialid", 1)] { + let (g, r) = run(&identifier(vec![(3, 2.0)], id_info(name)), "ec.3d.identifier"); + assert_eq!(losses(&r, LossCategory::Effects, "IDentifier"), Vec::::new(), "{name}"); + let g = g.expect("imported"); + assert_eq!(v(&g, "channelType"), Value::Enum(kind), "{name}"); + near(&g, "id", 2.0); + } + for name in ["IndexOB.X", "ID", "Material ID"] { + let (_, r) = run(&identifier(vec![], id_info(name)), "ec.3d.identifier"); + let found = losses(&r, LossCategory::Effects, "IDentifier"); + assert!(found.len() == 1 && found[0].contains(&format!("the ID channel '{name}' (Channel Info) is not imported")), "{name}: {found:?}"); + } } diff --git a/docs/aep-format.md b/docs/aep-format.md index 29ee51963..7b9e67981 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -542,12 +542,24 @@ data layouts, read from default instances only):** | `EXtractoR` (2.6.6) | 1 Channel Info (`EXtractoR-0013`): arbitrary data, 688 bytes, below; 2-6 the Process group: 3 Black Point and 4 White Point float sliders, valid ±3.4028235e38 (the float32 range; slider ±1 000), default 0 / 1; 5 UnMult: checkbox (empty `pard` name, the label in `pdnm`), default off | | `IDentifier` (2.6.6) | 1 Channel Info (`IDentifier-0005`): arbitrary data, 148 bytes, below; 2 Display: popup (Colors, Luma Matte, Alpha Matte, Raw), default 1; 3 ID: integer slider, max 2 147 483 647, default 0 | -EXtractoR's Channel Info is the plug-in's own **little-endian** layout: `u32` 1 and 28 zero bytes, -then four 164-byte records (Red, Green, Blue, Alpha), each a `u32` kind (1 in R, G, B; 2 in Alpha, -whose name is `(copy)`) and the channel name NUL-terminated from record byte 35 (`R`, `G`, `B`). -The data is the same in every composition of the block. IDentifier's data differs between -instances with the same settings except for the channel type's name, NUL-terminated from byte 19 -(`Object ID`); the other bytes are not read. API: `ExtractorChannels`, `identifier_channel_type`. +EXtractoR's Channel Info is the plug-in's own **little-endian** layout: `u32` 1 and 28 bytes, then +four 164-byte records (Red, Green, Blue, Alpha), each a kind in byte 0 (1 a channel of the footage by +name: `R`, `G`, `B` by default; 2 the input's own channel, named `(copy)`: Alpha by default) and the +channel name NUL-terminated from record byte 35. The rest is not read: it differs between instances +with the same channels (uninitialised-looking values, also in bytes 1..4 of a record: `02 00 00 32` +on Alpha in one definition's default, which is why only byte 0 is the kind). **A chosen channel** +(`extractor-07-preset-channel-z`, a preset of our own that reads `Z` into Red, Green and Blue) is kind +1 with its name (`Z`), and the `u32` at record byte 4 is then 13, which looks like the channel's +position in the EXR's sorted channel list of 14 (the defaults hold 0 there: H). IDentifier's data +differs between instances with the same settings except for **the chosen channel's name**, +NUL-terminated from byte 19, which is `Object ID` when no channel is chosen. After Effects lists +only integer (UINT) channels: in 1300's EXR (a FLOAT `ID` channel) it found none ("No ID channels +available"), so that block's preset `identifier-09-preset-channel-id` only sets ID 2; in 1310's +EXR (UINT `objectId` and `IndexOB.X` added) the preset `identifier-09-preset-channel-objectid-id2` +stores `objectId`, with 15 at byte 4 (the channel's position in the sorted list of 16, as for +EXtractoR: H). Without a chosen channel After Effects leaves the layer unchanged (1300 and 1310 +defaults). The other bytes are not read. API: `ExtractorChannels`, +`identifier_channel_type`. **Curves and Mesh Warp** (961-965, 985; applied from presets, the data is not scriptable): their arbitrary-data parameter (type 11) always has a record (`tdbs` with a 4-byte `cdat`) and a `LIST diff --git a/docs/aep-import.md b/docs/aep-import.md index f14141156..cb61cd2ab 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -179,7 +179,7 @@ parameter gets the file's value: | `ADBE Easy Levels2` 1 Channel, 2 Histogram data, 8 / 9 Clip To Output Black / White | `channel`; `inBlack`, `inWhite`, `gamma`, `outBlack`, `outWhite` from the data; `clipToOutputBlack`, `clipToOutputWhite` | popups by name (After Effects' items On, Off, Off for 32 bpc Color are in another order here); the RGB values are the `f32` of the arbitrary data; the per-channel values are reported when they are not at their defaults (not sampled) | 511-516 | | Batch 6 (903 defaults, 904 / 905 set values): `ADBE Brightness & Contrast 2`, `ADBE Turbulent Displace`, `ADBE Fractal Noise`, `ADBE Laser` (Beam), `CC Radial Fast Blur`, `ADBE Noise2`, `ADBE Pro Levels2` (Levels (Individual Controls)), `ADBE Corner Pin`, `ADBE Grid`, `ADBE Sharpen`, `ADBE Linear Color Key2`, `ADBE Radial Wipe`, `ADBE Remove Color Matting`, `CC Bend It`, `CC Power Pin`, `ADBE 4ColorGradient`, `ADBE Change Color`, `ADBE Extract`, `ADBE Noise HLS2`, `ADBE Scatter`, `CC Light Sweep`, `ADBE Compound Blur`, `ADBE Displacement Map`, `ADBE Set Matte3`, `ADBE Gaussian Blur` (legacy), `ADBE OFMotionBlur` (Pixel Motion Blur), `ADBE Layer Control` | the EffectCraft effect of the same name, every parameter EffectCraft has | sliders as stored (each checked against its valid maximum); Beam Length, Time and Softness and CC Power Pin Perspective are 0..1 fractions, percentages here; points as layer pixels; popups by item name (Set Matte's `Red Channel` ... is `Red` ...); twirl-down group markers and the Levels histogram carry nothing; **layer parameters** (below) | 903, 904, 905; 220, 320, 321 | | Importer block 2026-11-05 (`1100-quickwins.aep`: per effect defaults, set values, minimum and maximum, every popup item, a keyed and an expression parameter): `ADBE HDR ToneMap` (HDR Highlight Compression) 1 Amount; `ADBE Echo` 1 Echo Time, 2 Number Of Echoes, 3 Starting Intensity, 4 Decay, 5 Echo Operator; `ADBE Linear Wipe` 1 Transition Completion, 2 Wipe Angle, 3 Feather; `ADBE Offset` 1 Shift Center To, 2 Blend With Original; `ADBE Deflicker` (Color Stabilizer) 1 Stabilize, 2-4 Black / Mid / White Point, 5 Sample Size | `ec.utility.hdrcompression` `amount`; `ec.time.echo` `echoTime`, `numberOfEchoes`, `startingIntensity`, `decay`, `echoOperator`; `ec.transition.linearwipe` `completion`, `angle`, `feather`; `ec.distort.offset` `shift`, `blend`; `ec.color.colorstabilizer` `stabilize`, `blackPoint`, `midPoint`, `whitePoint`, `sampleSize` | percent (float, valid 0..100, default 100); seconds (float, valid ±30 000), integer (valid 30 000), 0..1 (float), Decay as stored (float, valid 0..100), popup by name: EffectCraft's Echo uses at most 255 echoes and a Decay up to 1, a value or key beyond that is imported as stored and **reported**; percent (fixed, 100), degrees, pixels (fixed, 32 000); point in layer pixels, Blend 0..1 x 100 = percent; popup by name (**fresh default Levels**), points in layer pixels, Sample Size as stored (fixed, valid 50, default 3; its unit, pixels here, is not verified). Color Stabilizer's reference frame (Set Frame) is not a parameter in the file and is not imported (EffectCraft's Reference Frame stays 0 s) | `tests/it/import/effects_quickwins.rs`; render comparison below | -| Importer block 2026-11-05 (`1300-thirdparty.aep`), plug-ins that ship with After Effects: `CS Color Neutralizer` (CC Color Neutralizer) 19 Pinning, 20 Blend w. Original; `EXtractoR` 1 Channel Info data, 3 Black Point, 4 White Point, 5 UnMult; `IDentifier` 1 Channel Info data, 2 Display, 3 ID | `ec.color.cccolorneutralizer` `pinning`, `blendWOriginal`; `ec.3d.extractor` `red`, `green`, `blue`, `alpha`, `blackPoint`, `whitePoint`, `unMult`; `ec.3d.identifier` `channelType`, `display`, `id` | CC Color Neutralizer: percent (float, valid 100); its correction is the Red / Green / Blue slider of each range (the Unbalance colours are pickers that set them and keep no record), with View and Black / White Point: **EffectCraft's effect has no counterpart, each is reported when stored** (EffectCraft's Unbalance / Balance stay at their identity defaults). EXtractoR: the default channels (R, G, B, `(copy)`) are EffectCraft's R, G, B, A, other channels are reported (`ExtractorChannels`; only the default is sampled), Black and White Point as stored (float, valid ±3.4e38; a value beyond ±1e9 is reported as damaged data, as for every number), UnMult a checkbox, Clip stays off. IDentifier: Display by name, ID as stored (integer, valid 2 147 483 647); the Channel Info names only the channel type (Object ID, `identifier_channel_type`; another is reported), not the channel After Effects reads, so **every instance is reported**: EffectCraft draws the footage's object ID channel where After Effects left the sampled EXR unchanged | `tests/it/import/effects_thirdparty.rs`; render comparison below | +| Importer block 2026-11-05 (`1300-thirdparty.aep`), plug-ins that ship with After Effects: `CS Color Neutralizer` (CC Color Neutralizer) 19 Pinning, 20 Blend w. Original; `EXtractoR` 1 Channel Info data, 3 Black Point, 4 White Point, 5 UnMult; `IDentifier` 1 Channel Info data, 2 Display, 3 ID | `ec.color.cccolorneutralizer` `pinning`, `blendWOriginal`; `ec.3d.extractor` `red`, `green`, `blue`, `alpha`, `blackPoint`, `whitePoint`, `unMult`; `ec.3d.identifier` `channelType`, `display`, `id` | CC Color Neutralizer: percent (float, valid 100); its correction is the Red / Green / Blue slider of each range (the Unbalance colours are pickers that set them and keep no record), with View and Black / White Point: **EffectCraft's effect has no counterpart, each is reported when stored** (EffectCraft's Unbalance / Balance stay at their identity defaults). EXtractoR: each output channel by the footage channel's name, which EffectCraft's EXtractoR reads by the same name (the default R, G, B; `Z` from our preset), and `(copy)` as the input's own channel of that position (EffectCraft's R, G, B or A; the default Alpha is A); a record of another kind or without a name is reported and R, G, B, A used (`ExtractorChannels`), Black and White Point as stored (float, valid ±3.4e38; a value beyond ±1e9 is reported as damaged data, as for every number), UnMult a checkbox, Clip stays off. IDentifier: Display by name, ID as stored (integer, valid 2 147 483 647); the Channel Info names the chosen channel (`identifier_channel_type`): After Effects lists only the footage's integer (UINT) channels, and with none chosen (the data then says `Object ID`) it leaves the layer unchanged, where EffectCraft draws the footage's object ID channel: **reported**; a chosen channel named `ObjectID` or `MaterialID` (without regard to case, `objectId` in our preset) is the one EffectCraft's IDentifier reads itself (Channel Type Object ID or Material ID), any other name is reported (EffectCraft's IDentifier has no channel choice) | `tests/it/import/effects_thirdparty.rs`; render comparison below | | `ADBE Slider Control`, `ADBE Angle Control`, `ADBE Checkbox Control`, `ADBE Color Control`, `ADBE Point Control` (parameter 1) | `ec.control.slider` `slider`, `ec.control.angle` `angle`, `ec.control.checkbox` `checkbox`, `ec.control.color` `color`, `ec.control.point` `point` | float (valid max 1 000 000) as stored; degrees; checkbox; colour; point in layer pixels | 071, 073, 217-219, 221, 304, 310 | - **Nothing to import:** the implicit input-layer parameter (index 0), the Swap Colors buttons @@ -336,8 +336,16 @@ frame blending, 3D material options, audio levels, guides, the mask colour, lock within one level, the ±3.4e38 compositions differ (reported as damaged data). IDentifier: After Effects' frame is the unchanged EXR in every variant (its default Object ID finds no channel in that EXR), while EffectCraft's IDentifier finds the EXR's `ID` channel and draws it; the import - reports this for every IDentifier. No sample changes EXtractoR's channels or IDentifier's channel - type (the presets for that were missing from the block). + reports this for every IDentifier. The follow-up presets (our own): `extractor-07-preset-channel-z` + (`Z` into Red, Green and Blue) imports without a loss and renders equal to After Effects (within + 3e-5 at After Effects' 1/10 PNG scale; the background's Z of 100 clips there in both). IDentifier + needs an integer channel: block 1310 (`1310-identifier-uint.aep`, the EXR with UINT channels + `objectId` and `IndexOB.X` added) with the preset `identifier-09-preset-channel-objectid-id2` + (`objectId`, ID 2) imports without a loss, and EffectCraft's IDentifier finds the same channel: + the ID regions are the same pixels (8 449, 2 400, 1 515, 1 256, 780), the colours differ (After + Effects draws flat colours per ID and does not highlight the chosen ID, EffectCraft draws its + own ID colours with the chosen ID white). At its defaults (no channel chosen) After Effects + leaves the EXR unchanged in 1310 too. - `crates/aep/tests/it/import/combined.rs`: masks and effects on the same layer, an effect on a text layer, and the same through `.aepx` (built in-test from the fixtures: the effect parade of one fixture grafted onto the layer of another). From 6a67b4dac1f989d3ae4be3ef01656404b7144f11 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 17:48:29 +0100 Subject: [PATCH 016/350] AEP import: Lumetri Color's master RGB curve, Hue vs Sat and colour wheels After Effects 2026 presets made by hand (1200 lumetri-58 ... -62) decode the data the scripted samples could not set. - RGB curve (-0039): the first of the four curves is the master curve and arrives as curves/rgbCurves/master. A moved red, green or blue curve is reported: no sample tells which slot is which channel. - Hue vs Sat (-0042): y is the offset from neutral, 1 doubling the saturation (in linear light), so EffectCraft's y is 0.5 + y / 2. The other four hue / saturation curves stay reported when moved. - Color Wheels (-0047): four triples of hue (degrees), brightness slider (0.5 neutral) and distance. Shadows, Midtones and Highlights become EffectCraft's wheel colours (HSL of that hue, saturation = distance, lightness 0.5); the brightness sliders and the fourth wheel are reported. - Damaged curve or wheel data is reported as damaged. - Legacy Tone And Saturation is reported when a record says on: its pard default turns from off to on when the project is saved again (the 25.x / 24.x / 26 copies of 1200), so comparing with it missed it. - The Input LUT and Look presets confirm -0095 / -0096 hold the names; the LUT files are After Effects' own and are not embedded. Evidence: the 62 read-backs still equal the imported values in the new 63-composition 1200 file. The version copies store the same Lumetri definitions, ranges and values and give the same report. Renders: master curve within 0.024 (After Effects uses a natural spline, EffectCraft's Lumetri a monotone one), Hue vs Sat mean 0.008, wheels mean 0.042. Part of #3111 Co-Authored-By: Claude Opus 5.5 --- crates/aep/src/import/lumetri.rs | 190 +++++++++++++++++++++++--- crates/aep/tests/it/import/lumetri.rs | 129 +++++++++++++++-- docs/aep-format.md | 52 ++++--- docs/aep-import.md | 26 +++- 4 files changed, 348 insertions(+), 49 deletions(-) diff --git a/crates/aep/src/import/lumetri.rs b/crates/aep/src/import/lumetri.rs index 22f28b1aa..9369155f4 100644 --- a/crates/aep/src/import/lumetri.rs +++ b/crates/aep/src/import/lumetri.rs @@ -16,11 +16,18 @@ //! After Effects; EffectCraft's Lumetri has none. A section that is off does nothing in After //! Effects (1200 `lumetri-03-sec-basic-correction`, `-17-sec-vignette` render as `-00-defaults`), //! so its values are not imported (the effect looks the same) and are listed in the report. -//! - **Arbitrary data** (`ParamKind::ArbitraryData`, every instance stores it): the hue / -//! saturation curves (a curve type, a point count, points as `f64` x, y with y an offset from -//! neutral) are imported when neutral and reported otherwise (no sample moves a point); the -//! HSL Secondary key ([`HslKey`]) is imported; the RGB curves, the colour wheels, Split Toning -//! and the correction wheels are reported when they are not the default (not sampled). +//! - **Arbitrary data** (`ParamKind::ArbitraryData`, every instance stores it). Curves are a +//! curve type, a point count and points as `f64` x, y ([`curve_points`]). +//! - The RGB curve (`-0039`) holds four curves; the first is the master (white) curve (1200 +//! `lumetri-60-preset-curves-rgb`), imported. The other three are reported when moved: no +//! sample tells which is which channel. +//! - Hue vs Sat (`-0042`): y is an offset from neutral, 1 doubling the saturation (`-61`), so +//! EffectCraft's y is `0.5 + y / 2`. The other four hue / saturation curves have no sample +//! of their scale and are reported when moved. +//! - Color Wheels (`-0047`): Shadows, Midtones, Highlights and a fourth wheel, each hue, +//! brightness slider, distance ([`import_wheels`], `-62`). +//! - The HSL Secondary key ([`HslKey`]) is imported; Split Toning and the correction wheels are +//! reported when they are not the default (not sampled). //! - **LUTs and looks.** The Input LUT and the Look are files After Effects names (the Look's name //! is UTF-16 text in `-0096`, the popups only index a list of the installed ones); they are not //! in the project, so a set one is reported by name and EffectCraft's popup stays None. @@ -82,8 +89,9 @@ impl Section { enum Data { /// The four RGB curves (4 x 520 bytes). RgbCurves, - /// One hue / saturation curve (520 bytes); the EffectCraft parameter it belongs to. - HueSatCurve, + /// One hue / saturation curve (520 bytes); the EffectCraft parameter when the scale of its y + /// is known (Hue vs Sat). + HueSatCurve(Option<&'static str>), /// Color Wheels (4 x 3 `f64`, each wheel 0, 0.5, 0 by default). Wheels, /// HSL Secondary's correction wheels (8 x 3 `f64`, the same default). @@ -105,6 +113,11 @@ enum Role { Switch, /// No counterpart: reported when not at the default. Missing, + /// A checkbox without counterpart whose `pard` default is not reliable: reported when a + /// record says on. Legacy Tone And Saturation (`-0097`) is off in a fresh instance, but its + /// `pard` default reads off in 1200 as saved by the script and on after the project was + /// opened and saved again (`versions/1200-lumetri-26`, `-25x`, `-24x`). + MissingWhenOn, /// An eyedropper colour: reported when set. Eyedropper, /// The Input LUT (`lut` true) or the Look: a popup, a name or an asset. @@ -187,19 +200,19 @@ const ROWS: &[Row] = &[ row(39, S::RgbCurves, Some(K::ArbitraryData), Role::Data(Data::RgbCurves)), row(104, S::HueSat(0), Some(K::Checkbox), Role::Switch), row(113, S::HueSat(0), Some(K::Color), Role::Eyedropper), - row(42, S::HueSat(0), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(42, S::HueSat(0), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve(Some("curves/hueSaturationCurves/hueVsSat")))), row(105, S::HueSat(1), Some(K::Checkbox), Role::Switch), row(114, S::HueSat(1), Some(K::Color), Role::Eyedropper), - row(106, S::HueSat(1), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(106, S::HueSat(1), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve(None))), row(107, S::HueSat(2), Some(K::Checkbox), Role::Switch), row(115, S::HueSat(2), Some(K::Color), Role::Eyedropper), - row(108, S::HueSat(2), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(108, S::HueSat(2), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve(None))), row(109, S::HueSat(3), Some(K::Checkbox), Role::Switch), row(116, S::HueSat(3), Some(K::Color), Role::Eyedropper), - row(110, S::HueSat(3), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(110, S::HueSat(3), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve(None))), row(111, S::HueSat(4), Some(K::Checkbox), Role::Switch), row(117, S::HueSat(4), Some(K::Color), Role::Eyedropper), - row(112, S::HueSat(4), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve)), + row(112, S::HueSat(4), Some(K::ArbitraryData), Role::Data(Data::HueSatCurve(None))), // Color Wheels: 46 the switch, 102 Face Detection (on by default), 66 HDR White, 47 the wheels. row(46, S::ColorWheels, Some(K::Checkbox), Role::Switch), row(102, S::ColorWheels, Some(K::Checkbox), Role::Missing), @@ -235,7 +248,7 @@ const ROWS: &[Row] = &[ row(60, S::SpeedGradeCustom, Some(K::ArbitraryData), Role::Data(Data::Other)), // Outside the sections: Legacy Tone And Saturation, the embedded LUTs, Auto Tone, Auto Color, // the LUT and Look assets. - row(97, S::Always, Some(K::Checkbox), Role::Missing), + row(97, S::Always, Some(K::Checkbox), Role::MissingWhenOn), row(98, S::Always, Some(K::ArbitraryData), Role::Data(Data::Other)), row(99, S::Always, Some(K::ArbitraryData), Role::Data(Data::Other)), row(100, S::Always, Some(K::ArbitraryData), Role::Data(Data::Other)), @@ -355,7 +368,11 @@ fn utf16_text(d: &[u8]) -> Option { /// A Lumetri curve (520 bytes): `i32` curve type at 0 (3 for the hue curves, which wrap; 2 for /// Luma vs Sat and Sat vs Sat; 0 for an RGB curve), the `u32` point count at 4, then the points -/// as little-endian `f64` pairs x, y from byte 8 (at most 32). `None` when the count does not fit. +/// as little-endian `f64` pairs x, y from byte 8 (at most 32), sorted by x. Bytes after the +/// counted points are stale (a fresh hue curve has count 0 and old points behind it). An RGB +/// curve's x and y are 0..1 (2 points 0,0 and 1,1 by default); a hue / saturation curve's x is +/// the hue (0 red, 1/3 green) or the luma / saturation, 0..1, and y its offset from neutral. +/// `None` when the count does not fit. fn curve_points(d: &[u8]) -> Option> { let n = usize::try_from(le_u32(d, 4)?).ok()?; if n > d.len().saturating_sub(8) / 16 { @@ -418,7 +435,7 @@ fn data_default(data: Data, d: Option<&[u8]>) -> bool { Data::RgbCurves => { d.len() == 4 * 520 && d.as_chunks::<520>().0.iter().all(|c| curve_points(c).is_some_and(|pts| pts.iter().all(|(x, y)| (x - y).abs() < 1e-9))) } - Data::HueSatCurve => curve_points(d).is_some_and(|pts| pts.iter().all(|(_, y)| *y == 0.0)), + Data::HueSatCurve(_) => curve_points(d).is_some_and(|pts| pts.iter().all(|(_, y)| *y == 0.0)), Data::Wheels => wheels_default(d, 4), Data::CorrectionWheels => wheels_default(d, 8), Data::SplitToning | Data::Other => is_empty_data(Some(d)), @@ -552,6 +569,11 @@ pub(crate) fn import_lumetri(fx: &AepEffect<'_, '_>, params: &[EffectParam<'_, ' col.lose(LossCategory::Effects, format!("{here}: parameter {label} is off and has no counterpart")); } } + Role::MissingWhenOn => { + if p.is_animated() || matches!(p.recorded_value(), Some(ParamValue::Bool(true))) { + col.lose(LossCategory::Effects, format!("{here}: parameter {label} is on and has no counterpart, not imported")); + } + } Role::Missing => { if is_set(p) { col.lose( @@ -575,8 +597,20 @@ pub(crate) fn import_lumetri(fx: &AepEffect<'_, '_>, params: &[EffectParam<'_, ' } if !data_default(d, data()) { let why = match d { - Data::HueSatCurve => "the curve's points are moved; no sample tells their scale", - Data::RgbCurves | Data::Wheels | Data::CorrectionWheels | Data::SplitToning => "the data is not decoded (not sampled)", + Data::RgbCurves => { + import_rgb_curves(data(), (&here, &label), &mut *group, &mut *col); + continue; + } + Data::HueSatCurve(Some(target)) => { + import_hue_sat_curve(data(), target, (&here, &label), &mut *group, &mut *col); + continue; + } + Data::Wheels => { + import_wheels(data(), (&here, &label), &mut *group, &mut *col); + continue; + } + Data::HueSatCurve(None) => "the curve's points are moved; no sample tells their scale", + Data::CorrectionWheels | Data::SplitToning => "the data is not decoded (not sampled)", Data::HslKey | Data::Other => "no counterpart", }; col.lose(LossCategory::Effects, format!("{here}: parameter {label} is not imported ({why})")); @@ -640,3 +674,125 @@ fn import_hsl_key(d: Option<&[u8]>, here: &str, group: &mut effectcraft_project: set(&format!("{id}Softness"), Value::Scalar((t * 100.0).clamp(0.0, 100.0))); } } + +/// A parameter of `group` set to `v` (ids the spec does not have are skipped). +fn set_prop(group: &mut effectcraft_project::PropGroup, id: &str, v: Value) { + if let Some(prop) = group.prop_mut(id) { + prop.value = v; + } +} + +/// Curve points as EffectCraft's `x,y x,y ...` text, x and y clamped to 0..1; `None` when a value +/// is not finite. +fn points_text(pts: impl IntoIterator) -> Option { + let mut out: Vec = Vec::new(); + for (x, y) in pts { + if !x.is_finite() || !y.is_finite() { + return None; + } + out.push(format!("{},{}", x.clamp(0.0, 1.0), y.clamp(0.0, 1.0))); + } + Some(out.join(" ")) +} + +/// The RGB curve (`-0039`, four curves of 520 bytes). The first is the master curve (1200 +/// `lumetri-60-preset-curves-rgb`: its midpoint raised to 0.787 lifts R, G and B alike) and +/// becomes `curves/rgbCurves/master`. The other three are the channel curves, but no sample moves +/// one, so which is red, green or blue is not known: a moved one is reported. +fn import_rgb_curves(d: Option<&[u8]>, (here, label): (&str, &str), group: &mut effectcraft_project::PropGroup, col: &mut crate::import::report::Collector) { + let Some(d) = d else { return }; + let curves: Option>> = if d.len() == 4 * 520 { d.as_chunks::<520>().0.iter().map(|c| curve_points(c)).collect() } else { None }; + let Some(curves) = curves else { + col.lose(LossCategory::DamagedData, format!("{here}: the data of {label} is unreadable, the curves are not imported")); + return; + }; + let identity = |pts: &[(f64, f64)]| pts.iter().all(|(x, y)| (x - y).abs() < 1e-9); + if let Some(master) = curves.first().filter(|c| !identity(c)) { + match points_text(master.iter().copied()) { + Some(text) => set_prop(group, "curves/rgbCurves/master", Value::Str(text)), + None => col.lose(LossCategory::DamagedData, format!("{here}: the master curve of {label} has unreadable points, not imported")), + } + } + let moved = curves.iter().skip(1).filter(|c| !identity(c)).count(); + if moved > 0 { + col.lose( + LossCategory::Effects, + format!("{here}: {moved} channel curve(s) of {label} are not imported (no sample tells which curve is red, green or blue)"), + ); + } +} + +/// Hue vs Sat (`-0042`): After Effects' y is an offset from neutral where 1 doubles the +/// saturation (1200 `lumetri-61-preset-curves-huesat`: a point at hue 0.0155, y 0.97 scales pure +/// red's saturation by about 1.96 in linear light; pins at -0.03 and -0.02 scale green, blue and +/// cyan by 0.97). EffectCraft's y = 1 doubles it and 0.5 is neutral, so y becomes `0.5 + y / 2`. +/// x is the hue in both (0 red, 1/3 green). +fn import_hue_sat_curve( + d: Option<&[u8]>, + target: &str, + (here, label): (&str, &str), + group: &mut effectcraft_project::PropGroup, + col: &mut crate::import::report::Collector, +) { + let Some(d) = d else { return }; + match curve_points(d).and_then(|pts| points_text(pts.into_iter().map(|(x, y)| (x, 0.5 + y / 2.0)))) { + Some(text) => set_prop(group, target, Value::Str(text)), + None => col.lose(LossCategory::DamagedData, format!("{here}: the data of {label} is unreadable, the curve is not imported")), + } +} + +/// The colour EffectCraft's wheel takes for After Effects' hue (degrees, 0 red, 120 green) and +/// distance from the centre (0..1): the HSL colour of that hue with saturation = distance and +/// lightness 0.5 (grey 0.5 at distance 0). EffectCraft's wheel adds that colour's offset from its +/// grey, so the direction is After Effects' and the strength grows with the distance. +fn wheel_colour(hue: f64, distance: f64) -> [f64; 4] { + let c = distance.clamp(0.0, 1.0); + let h = hue.rem_euclid(360.0) / 60.0; + let x = c * (1.0 - (h.rem_euclid(2.0) - 1.0).abs()); + let (r, g, b) = match h { + h if h < 1.0 => (c, x, 0.0), + h if h < 2.0 => (x, c, 0.0), + h if h < 3.0 => (0.0, c, x), + h if h < 4.0 => (0.0, x, c), + h if h < 5.0 => (x, 0.0, c), + _ => (c, 0.0, x), + }; + let m = 0.5 - c / 2.0; + [r + m, g + m, b + m, 1.0] +} + +/// Color Wheels (`-0047`, 96 bytes): four wheels of three little-endian `f64`, the hue in degrees, +/// the brightness slider (0..1, 0.5 neutral) and the distance from the centre (0..1); `0, 0.5, 0` +/// each by default. The first three are Shadows, Midtones and Highlights (1200 +/// `lumetri-62-preset-wheels`: Shadows 229 degrees (blue), slider 0.37, distance 0.55; Midtones +/// 0.1 degrees (red), 0.5, 0.39; Highlights 93 degrees (yellow-green), 0.73, 0.32); the fourth is +/// not known. Hue and distance become EffectCraft's wheel colour ([`wheel_colour`]); the brightness +/// sliders and a set fourth wheel have no counterpart and are reported. +fn import_wheels(d: Option<&[u8]>, (here, label): (&str, &str), group: &mut effectcraft_project::PropGroup, col: &mut crate::import::report::Collector) { + let Some(d) = d else { return }; + let wheels: Option> = if d.len() == 4 * 24 { + (0..4).map(|w| Some([le_f64(d, w * 24)?, le_f64(d, w * 24 + 8)?, le_f64(d, w * 24 + 16)?]).filter(|v| v.iter().all(|x| x.is_finite()))).collect() + } else { + None + }; + let Some(wheels) = wheels else { + col.lose(LossCategory::DamagedData, format!("{here}: the data of {label} is unreadable, the colour wheels are not imported")); + return; + }; + let mut lost: Vec = Vec::new(); + for (w, (id, name)) in wheels.iter().zip([("shadowsWheel", "Shadows"), ("midtonesWheel", "Midtones"), ("highlightsWheel", "Highlights")]) { + let [hue, brightness, distance] = *w; + if distance != 0.0 { + set_prop(group, &format!("colorWheels/{id}"), Value::Color(wheel_colour(hue, distance))); + } + if (brightness - 0.5).abs() > 1e-9 { + lost.push(format!("{name} brightness {brightness:.3} (0.5 is neutral)")); + } + } + if wheels.get(3).is_some_and(|w| *w != [0.0, 0.5, 0.0]) { + lost.push("the fourth wheel (its role is not known)".to_string()); + } + if !lost.is_empty() { + col.lose(LossCategory::Effects, format!("{here}: {label}: {} not imported (no counterpart)", lost.join(", "))); + } +} diff --git a/crates/aep/tests/it/import/lumetri.rs b/crates/aep/tests/it/import/lumetri.rs index 526f43648..a3ec9138b 100644 --- a/crates/aep/tests/it/import/lumetri.rs +++ b/crates/aep/tests/it/import/lumetri.rs @@ -172,6 +172,7 @@ fn defs() -> Vec<(u16, Def)> { (39, Def::Data { name: "RGB Curve" }), (104, Def::Check { default: true }), (42, Def::Data { name: "Hue vs Sat" }), + (106, Def::Data { name: "Hue vs Hue" }), (47, Def::Data { name: "Wheels" }), (68, Def::Check { default: true }), (73, Def::Data { name: "HSL Sliders" }), @@ -195,6 +196,7 @@ fn default_data() -> Vec<(u16, Rec)> { (32, Rec::Data(vec![0; 32])), (39, Rec::Data(rgb_curves())), (42, Rec::Data(curve(3, &[]))), + (106, Rec::Data(curve(3, &[]))), (47, Rec::Data(wheels(4))), (73, Rec::Data(hex(HSL_FRESH))), (85, Rec::Data(wheels(8))), @@ -372,20 +374,112 @@ fn a_look_is_reported_by_name() { } #[test] -fn hue_saturation_curves_with_neutral_points_arrive_moved_ones_are_reported() { +fn hue_vs_sat_arrives_with_its_y_scaled_other_moved_curves_are_reported() { // 1200 `-11-sec-hue-saturation-curves`: the selector put three neutral points on the curve. let neutral = curve(3, &[(0.742_483, 0.0), (0.798_038, 0.0), (0.853_594, 0.0)]); let (g, r) = run(&[(42, Rec::Data(neutral))]); assert_eq!(losses(&r), Vec::::new()); assert_eq!(val(&g, "curves/hueSaturationCurves/hueVsSat"), Value::Str(String::new())); - let (_, r) = run(&[(42, Rec::Data(curve(3, &[(0.5, 0.25)])))]); + // 1200 `-61-preset-curves-huesat`: red raised to 0.97 (After Effects nearly doubles its + // saturation), the neighbours pinned near 0. EffectCraft's y: 0.5 + y / 2. + let preset = curve(3, &[(0.015_463_917_525_773_196, 0.97), (0.262_886_597_938_144_34, -0.03), (0.764_604_810_996_563_5, -0.02)]); + let (g, r) = run(&[(42, Rec::Data(preset))]); + assert_eq!(losses(&r), Vec::::new()); + let Value::Str(text) = val(&g, "curves/hueSaturationCurves/hueVsSat") else { panic!("a point list") }; + let pts: Vec<(f64, f64)> = text + .split(' ') + .map(|p| { + let (x, y) = p.split_once(',').unwrap(); + (x.parse().unwrap(), y.parse().unwrap()) + }) + .collect(); + let want = [(0.015_464, 0.985), (0.262_887, 0.485), (0.764_605, 0.49)]; + assert_eq!(pts.len(), 3, "{text}"); + for ((x, y), (wx, wy)) in pts.iter().zip(want) { + assert!((x - wx).abs() < 1e-5 && (y - wy).abs() < 1e-9, "{text}"); + } + // Hue vs Hue moved: no sample tells its scale, reported and left neutral. + let (g, r) = run(&[(106, Rec::Data(curve(3, &[(0.5, 0.25)])))]); let l = losses(&r); - assert!(l.len() == 1 && l[0].contains("Hue vs Sat (ADBE Lumetri-0042) is not imported"), "{l:?}"); - // A point count past the data: not a default, reported, no panic. + assert!(l.len() == 1 && l[0].contains("Hue vs Hue (ADBE Lumetri-0106) is not imported"), "{l:?}"); + assert_eq!(val(&g, "curves/hueSaturationCurves/hueVsHue"), Value::Str(String::new())); + // A point count past the data: not a default, reported as damaged, no panic. let mut bad = curve(3, &[]); bad[4..8].copy_from_slice(&1000u32.to_le_bytes()); - let (_, r) = run(&[(42, Rec::Data(bad))]); - assert_eq!(losses(&r).len(), 1); + let (g, r) = run(&[(42, Rec::Data(bad))]); + let l = losses(&r); + assert!(l.len() == 1 && l[0].contains("Hue vs Sat (ADBE Lumetri-0042) is unreadable"), "{l:?}"); + assert_eq!(val(&g, "curves/hueSaturationCurves/hueVsSat"), Value::Str(String::new())); +} + +#[test] +fn the_master_rgb_curve_arrives_moved_channel_curves_are_reported() { + // 1200 `-60-preset-curves-rgb`: the first of the four curves, its midpoint raised. + let master = curve(0, &[(0.0, 0.0), (0.491_408_934_707_903_76, 0.786_885_245_901_639_3), (1.0, 1.0)]); + let identity = curve(0, &[(0.0, 0.0), (1.0, 1.0)]); + let mut d = master.clone(); + (0..3).for_each(|_| d.extend(&identity)); + let (g, r) = run(&[(39, Rec::Data(d))]); + assert_eq!(losses(&r), Vec::::new()); + assert_eq!(val(&g, "curves/rgbCurves/master"), Value::Str("0,0 0.49140893470790376,0.7868852459016393 1,1".into())); + assert_eq!(val(&g, "curves/rgbCurves/red"), Value::Str("0,0 1,1".into())); + // A moved channel curve: which channel it is is not known, reported. + let mut d = identity.clone(); + d.extend(&master); + (0..2).for_each(|_| d.extend(&identity)); + let (g, r) = run(&[(39, Rec::Data(d))]); + let l = losses(&r); + assert!(l.len() == 1 && l[0].contains("1 channel curve(s) of RGB Curve (ADBE Lumetri-0039) are not imported"), "{l:?}"); + assert_eq!(val(&g, "curves/rgbCurves/master"), Value::Str("0,0 1,1".into())); + // Damaged: the wrong length. + let (_, r) = run(&[(39, Rec::Data(master))]); + assert!(losses(&r).iter().any(|x| x.contains("RGB Curve (ADBE Lumetri-0039) is unreadable")), "{:?}", losses(&r)); +} + +#[test] +fn colour_wheels_arrive_as_colours_their_brightness_is_reported() { + // 1200 `-62-preset-wheels`: Shadows towards blue with the slider down, Midtones towards red, + // Highlights towards yellow-green with the slider up; the fourth wheel at its default. + let w: Vec = [ + 229.119_895_976_548_18, + 0.369_892_467_494_294_2, + 0.547_209_348_004_638, + 0.104_745_366_878_020_72, + 0.5, + 0.394_961_840_132_529_44, + 93.374_076_744_668_57, + 0.726_881_718_523_399_9, + 0.318_976_840_675_999_35, + 0.0, + 0.5, + 0.0, + ] + .into_iter() + .flat_map(f64::to_le_bytes) + .collect(); + let (g, r) = run(&[(47, Rec::Data(w.clone()))]); + let colour = |id: &str| match val(&g, id) { + Value::Color(c) => c, + other => panic!("{id}: {other:?}"), + }; + // The hue's direction: blue strongest in Shadows, red in Midtones, green in Highlights. + let s = colour("colorWheels/shadowsWheel"); + assert!(s[2] > 0.77 && s[0] < 0.23 && (s[0] + s[2] - 1.0).abs() < 1e-9, "{s:?}"); + let m = colour("colorWheels/midtonesWheel"); + assert!((m[0] - 0.697_480_920_066_264_7).abs() < 1e-9 && m[1] > m[2], "{m:?}"); + let h = colour("colorWheels/highlightsWheel"); + assert!(h[1] > h[0] && h[0] > h[2], "{h:?}"); + let l = losses(&r); + assert!(l.len() == 1 && l[0].contains("Shadows brightness 0.370") && l[0].contains("Highlights brightness 0.727"), "{l:?}"); + assert!(!l[0].contains("Midtones") && !l[0].contains("fourth"), "{l:?}"); + // The fourth wheel set: reported; damaged data: reported, the wheels stay grey. + let mut w4 = w; + w4[88..96].copy_from_slice(&0.3f64.to_le_bytes()); + let (_, r) = run(&[(47, Rec::Data(w4))]); + assert!(losses(&r).iter().any(|x| x.contains("the fourth wheel")), "{:?}", losses(&r)); + let (g, r) = run(&[(47, Rec::Data(vec![1; 95]))]); + assert!(losses(&r).iter().any(|x| x.contains("Wheels (ADBE Lumetri-0047) is unreadable")), "{:?}", losses(&r)); + assert_eq!(val(&g, "colorWheels/shadowsWheel"), Value::Color([0.5, 0.5, 0.5, 1.0])); } #[test] @@ -420,17 +514,30 @@ fn the_picked_hsl_key_becomes_hue_saturation_and_lightness_ranges() { fn undecoded_data_and_controls_without_counterpart_are_reported_when_set() { let mut split = vec![0u8; 32]; split[0] = 1; - let mut w = wheels(4); - w[8] = 1; - let (_, r) = run(&[(32, Rec::Data(split)), (47, Rec::Data(w)), (97, Rec::Num(1.0, 3)), (95, Rec::Data(utf16("my.cube")))]); + let (_, r) = run(&[(32, Rec::Data(split)), (97, Rec::Num(1.0, 3)), (95, Rec::Data(utf16("my.cube")))]); let l = losses(&r); - assert_eq!(l.len(), 4, "{l:?}"); + assert_eq!(l.len(), 3, "{l:?}"); assert!(l.iter().any(|d| d.contains("Split Toning (ADBE Lumetri-0032) is not imported")), "{l:?}"); - assert!(l.iter().any(|d| d.contains("Wheels (ADBE Lumetri-0047) is not imported")), "{l:?}"); assert!(l.iter().any(|d| d.contains("Legacy Tone And Saturation (ADBE Lumetri-0097) is on")), "{l:?}"); assert!(l.iter().any(|d| d.contains("the Input LUT 'my.cube' is not imported")), "{l:?}"); } +#[test] +fn legacy_tone_on_is_reported_whatever_the_pard_default_says() { + // After the project is opened and saved again, the `pard` default of Legacy Tone And + // Saturation reads on (versions/1200-lumetri-26 / -25x / -24x; off in 1200 as the script + // saved it): a record that says on is still reported, a missing record is not. + let mut d = defs(); + if let Some((_, Def::Check { default })) = d.iter_mut().find(|(s, _)| *s == 97) { + *default = true; + } + let (_, r) = run_with(&d, &[(97, Rec::Num(1.0, 3))]); + let l = losses(&r); + assert!(l.len() == 1 && l[0].contains("Legacy Tone And Saturation (ADBE Lumetri-0097) is on"), "{l:?}"); + let (_, r) = run_with(&d, &[]); + assert_eq!(losses(&r), Vec::::new()); +} + #[test] fn an_unknown_scale_or_parameter_is_reported_not_guessed() { let mut d = defs(); diff --git a/docs/aep-format.md b/docs/aep-format.md index 9a37b384f..639c0a013 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -534,9 +534,10 @@ suffix is a parameter id, not the `parT` position (Curves -0002 Channel before - -0005 Quality before -0004). API: `CurvesData`, `MeshData`, `curve_lut`. [V; details: corpus FORMAT S] -**Lumetri Color** (`ADBE Lumetri`, 1200: 58 compositions made by script in After Effects 2026, 32 bpc, -working space None; defaults, all set, one per section, valid minima and maxima, every popup item the -script can set, checkboxes toggled, keys, an expression): 131 `parT` entries. The `-NNNN` suffix is a +**Lumetri Color** (`ADBE Lumetri`, 1200: 63 compositions in After Effects 2026, 32 bpc, working space +None; by script: defaults, all set, one per section, valid minima and maxima, every popup item the script +can set, checkboxes toggled, keys, an expression; from presets made by hand, `-58` ... `-62`: an Input +LUT, a Look, the RGB curve, Hue vs Sat, the colour wheels): 131 `parT` entries. The `-NNNN` suffix is a parameter id, not the position (`-0127` ... `-0130` sit between `-0005` and `-0026`), so the importer keys by suffix. Float sliders (type 10) store the value After Effects shows; the valid ranges (`pard` 104 / 108) are wider than the slider's (112 / 116): Temperature / Tint -300..300 (slider -100..100), @@ -546,31 +547,50 @@ HDR Range 100..10000, Refine Blur 0..1000 (0..30), Vignette Amount -5..5 (-3..3) `-19-min` / `-20-max` equal the stored numbers. **Hidden controls are recorded in the default state** (`tdsb` 3) and still hold the value After Effects reads back: High Dynamic Range `-0062` (1.0), HDR Specular `-0064` (22), the second Look popup `-0128` (26.16 as set, read back 26), Legacy Tone And -Saturation `-0097`, Auto Color `-0124`, Face Detection `-0102`, the Auto Intensity `-0123` +Saturation `-0097` (its `pard` default reads off as the script saved the project and on once the +project was opened and saved again, `versions/1200-lumetri-26`, `-25x`, `-24x`; a fresh instance reads +back off), Auto Color `-0124`, Face Detection `-0102`, the Auto Intensity `-0123` (`EffectParam::recorded_value`). Each section has an Active checkbox (blank `pard` name; `-0003` Basic Correction, `-0023` Creative, `-0037` Curves, `-0118` RGB Curves, `-0104` / `-0105` / `-0107` / `-0109` / `-0111` the hue / saturation curves, `-0046` Color Wheels, `-0068` HSL Secondary, `-0050` Vignette, on by default); off, the section does nothing (`-03-sec-basic-correction`, `-17-sec-vignette` render as `-00-defaults`). Arbitrary data (type 11, a record and `aRbs` in every instance; empty is `fe fe`): `-0001` an XML copy of the grade written only when the Look changes (not kept current: `-01-set-all` -holds defaults for controls set after the Look); `-0096` the Look's name, UTF-16LE (`SL BLUE ICE`; the -popup `-0025` itself has no record, `-0128` can disagree with it: `-07-sec-creative` names item 20 -while `-0128` holds 23), `-0095` likewise for the Input LUT (H, by analogy: no sample sets a LUT); -`-0129` the Look colour space's 16-byte id (the GUID `LookColorSpace` of `-0001`, byte-swapped; zero -for None; the Color Space popup `-0130` writes no record of its own, `-22-pop38-1` ... `-53-pop38-33`); +holds defaults for controls set after the Look; it names the LUT file, `Fuji F125 Kodak 2393 (by +Adobe).cube`); `-0096` the Look's name, UTF-16LE (`SL BLUE ICE`, `Fuji F125 Kodak 2393 (by Adobe)` in +`-59-preset-look-lut`; the popup `-0025` itself has no record, `-0128` can disagree with it: +`-07-sec-creative` names item 20 while `-0128` holds 23), `-0095` likewise for the Input LUT +(`ALEXA_Default_LogC2Rec709`, `-58-preset-input-lut`; the popups `-0005` / `-0127` keep no record, +`-0004`, `-0024` and the assets `-0125` / `-0126` stay `fe fe`: the LUTs are After Effects' own files, +named, not embedded); `-0129` the Look colour space's 16-byte id (the GUID `LookColorSpace` of `-0001` +in Windows layout: the first 4, 2 and 2 bytes little-endian; zero for None; the Color Space popup +`-0130` writes no record of its own, `-22-pop38-1` ... `-53-pop38-33`); `-0032` Split Toning (32 zero bytes); `-0039` the four RGB curves and `-0042`, `-0106`, `-0108`, `-0110`, `-0112` the hue / saturation curves, 520 bytes each: `i32` type (3 for the hue curves, 2 for Luma vs Sat and Sat vs Sat, 0 for an RGB curve), `u32` point count, then little-endian `f64` pairs x, y -(RGB: (0, 0), (1, 1) by default; hue / saturation: no points, and the selector colours `-0113` ... -`-0117` add three points at y = 0, the hue picked +- 0.0556; no sample moves a point, so y's scale is -H); `-0047` Color Wheels (4 x `f64` triples, 0, 0.5, 0 each) and `-0085` the HSL correction wheels (8 -triples, the same); `-0073` the HSL key, 40 bytes: a flag byte (1 once a colour was picked), then hue, +sorted by x (bytes after the counted points are stale). RGB: x and y 0..1, (0, 0), (1, 1) by default; +the first of the four is the master curve (`-60-preset-curves-rgb`: (0.491, 0.787) lifts R, G and B +alike, and the render follows a natural cubic spline through the points within 0.003); the order of +the other three is H (the `-0001` XML names `ToneCurvesLuma`, `Red`, `Green`, `Blue`; no sample moves +one). Hue / saturation: x the hue (0 red, 1/3 green) or the luma / saturation 0..1, y the offset from +neutral 0; a fresh curve has no points, the selector colours `-0113` ... `-0117` add three at y = 0, +the hue picked +- 0.0556. Hue vs Sat's y scales the saturation by 1 + y in linear light +(`-61-preset-curves-huesat`: (0.0155, 0.97) takes pure red's linear saturation to 1.96 times, (0.263, +-0.03) and (0.765, -0.02) green, cyan and blue to 0.97); the other four curves' scale is H. `-0047` +Color Wheels, 4 `f64` triples: hue in degrees (0 red, 120 green), brightness slider 0..1 (0.5 +neutral), distance from the centre 0..1; 0, 0.5, 0 each by default; Shadows, Midtones, Highlights, then +a fourth wheel of unknown role (`-62-preset-wheels`: Shadows 229.1, 0.370, 0.547 towards blue with the +slider down; Midtones 0.1, 0.5, 0.395 towards red; Highlights 93.4, 0.727, 0.319 towards yellow-green +with the slider up; the fourth at its default); `-0085` the HSL correction wheels (8 triples, the same +default; layout H); `-0073` the HSL key, 40 bytes: a flag byte (1 once a colour was picked), then hue, saturation, lightness, each centre, range, tolerance (`f32` LE, fractions) and an enabled byte (fresh: 0.5, 0, 0, on; picking RGB 0.02 0.55 0.85 gives hue centre 0.5605 = that colour's hue and lightness centre 0.435 = its HSL lightness, range 0.05, tolerance 0.1: `-01-set-all`; whether range is the half -or full width is H); `-0122` the Auto Tone analysis (UTF-16 JSON). Curves, LUTs and wheels are not -scriptable: the presets for them are still to be made. [V for the sliders, checkboxes, popups and the -`tdsb` 3 values; H where marked] +or full width is H); `-0122` the Auto Tone analysis (UTF-16 JSON). The project saved as 25.x and 24.x +copies (`versions/1200-lumetri-25x`, `-24x`: the 58 scripted compositions, saved by After Effects +2026) stores every Lumetri definition, valid range and value as the 26 save does. [V for the sliders, +checkboxes, popups, the `tdsb` 3 values, the master RGB curve, Hue vs Sat, the colour wheels and the +LUT names; H where marked] ### 3.8 Masks and bezier paths (`tdgp[ADBE Mask Parade]`) diff --git a/docs/aep-import.md b/docs/aep-import.md index 07c089782..f09f0ba1a 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -1,6 +1,6 @@ # After Effects project import -> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-05 · **Change:** Lumetri Color is imported (1200: every slider, checkbox and popup with a counterpart, by match name, also from records in the default state; switched-off sections, LUTs, looks, undecoded curves and wheels are reported); before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-05 · **Change:** Lumetri Color's master RGB curve, Hue vs Sat curve and colour wheels are imported (1200 presets made by hand); before that: Lumetri Color is imported (1200: every slider, checkbox and popup with a counterpart, by match name, also from records in the default state; switched-off sections, LUTs, looks, undecoded curves and wheels are reported); before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 File ▸ Import ▸ After Effects Project… (`file.importAep`) reads an `.aep` or its XML form `.aepx` and adds its items to the open project, in a folder named after the file (as After Effects does when it imports a project). @@ -179,7 +179,7 @@ parameter gets the file's value: | `ADBE Easy Levels2` 1 Channel, 2 Histogram data, 8 / 9 Clip To Output Black / White | `channel`; `inBlack`, `inWhite`, `gamma`, `outBlack`, `outWhite` from the data; `clipToOutputBlack`, `clipToOutputWhite` | popups by name (After Effects' items On, Off, Off for 32 bpc Color are in another order here); the RGB values are the `f32` of the arbitrary data; the per-channel values are reported when they are not at their defaults (not sampled) | 511-516 | | Batch 6 (903 defaults, 904 / 905 set values): `ADBE Brightness & Contrast 2`, `ADBE Turbulent Displace`, `ADBE Fractal Noise`, `ADBE Laser` (Beam), `CC Radial Fast Blur`, `ADBE Noise2`, `ADBE Pro Levels2` (Levels (Individual Controls)), `ADBE Corner Pin`, `ADBE Grid`, `ADBE Sharpen`, `ADBE Linear Color Key2`, `ADBE Radial Wipe`, `ADBE Remove Color Matting`, `CC Bend It`, `CC Power Pin`, `ADBE 4ColorGradient`, `ADBE Change Color`, `ADBE Extract`, `ADBE Noise HLS2`, `ADBE Scatter`, `CC Light Sweep`, `ADBE Compound Blur`, `ADBE Displacement Map`, `ADBE Set Matte3`, `ADBE Gaussian Blur` (legacy), `ADBE OFMotionBlur` (Pixel Motion Blur), `ADBE Layer Control` | the EffectCraft effect of the same name, every parameter EffectCraft has | sliders as stored (each checked against its valid maximum); Beam Length, Time and Softness and CC Power Pin Perspective are 0..1 fractions, percentages here; points as layer pixels; popups by item name (Set Matte's `Red Channel` ... is `Red` ...); twirl-down group markers and the Levels histogram carry nothing; **layer parameters** (below) | 903, 904, 905; 220, 320, 321 | | `ADBE Slider Control`, `ADBE Angle Control`, `ADBE Checkbox Control`, `ADBE Color Control`, `ADBE Point Control` (parameter 1) | `ec.control.slider` `slider`, `ec.control.angle` `angle`, `ec.control.checkbox` `checkbox`, `ec.control.color` `color`, `ec.control.point` `point` | float (valid max 1 000 000) as stored; degrees; checkbox; colour; point in layer pixels | 071, 073, 217-219, 221, 304, 310 | -| `ADBE Lumetri` by match-name suffix (1200), not by index: -0062 High Dynamic Range; -0063 HDR White; -0007 / -0008 Temperature / Tint; -0020 Saturation; -0011 Exposure; -0012 ... -0016 Contrast, Highlights, Shadows, Whites, Blacks; -0064 HDR Specular; -0026 Look Intensity; -0028 Faded Film; -0029 Sharpen; -0030 Vibrance; -0031 Saturation; -0033 Tint Balance; -0074 Show Mask; -0075 mask style; -0076 Invert Mask; -0081 Denoise; -0082 Blur; -0086 ... -0090 the HSL correction's Temperature, Tint, Contrast, Sharpen, Saturation; -0051 ... -0054 Vignette Amount, Midpoint, Roundness, Feather; -0073 the HSL key data | `highDynamicRange`, `basicCorrection/tone/hdrWhite`, `basicCorrection/whiteBalance/temperature` / `tint`, `basicCorrection/saturation`, `basicCorrection/tone/exposure`, `.../contrast`, `highlights`, `shadows`, `whites`, `blacks`, `hdrSpecular`, `creative/lookIntensity`, `creative/adjustments/fadedFilm`, `sharpen`, `vibrance`, `creativeSaturation`, `tintBalance`, `hslSecondary/key/showMask`, `maskStyle`, `invertMask`, `hslSecondary/refine/denoise`, `blur`, `hslSecondary/correction/*`, `vignette/*`; `hslSecondary/key/hue*`, `saturation*`, `lightness*` | sliders: the stored number x 1 (valid maxima 1000, 300, 300, 7, 150, 200, 150, 100, 100, 300, 150, 100, 1000, 5, 100 as in `pard`); checkboxes and the popup as elsewhere, **also from records in the default state** (`tdsb` 3); the key: hue centre x 360 degrees, range x 360 as the half width, tolerance x 360 as the softness, saturation and lightness centre +- range x 100 percent, tolerance x 100, once a colour was picked | 1200: all 62 read-backs of mapped controls in sections that act equal after import (min, max, sections, popups); the other 61 lie in sections that do nothing and are reported; keys and the expression on HDR White (`-56-keys`, `-57-expr`) | +| `ADBE Lumetri` by match-name suffix (1200), not by index: -0062 High Dynamic Range; -0063 HDR White; -0007 / -0008 Temperature / Tint; -0020 Saturation; -0011 Exposure; -0012 ... -0016 Contrast, Highlights, Shadows, Whites, Blacks; -0064 HDR Specular; -0026 Look Intensity; -0028 Faded Film; -0029 Sharpen; -0030 Vibrance; -0031 Saturation; -0033 Tint Balance; -0074 Show Mask; -0075 mask style; -0076 Invert Mask; -0081 Denoise; -0082 Blur; -0086 ... -0090 the HSL correction's Temperature, Tint, Contrast, Sharpen, Saturation; -0051 ... -0054 Vignette Amount, Midpoint, Roundness, Feather; -0073 the HSL key data; -0039 the first of the four RGB curves; -0042 Hue vs Sat; -0047 the Shadows, Midtones and Highlights wheels | `highDynamicRange`, `basicCorrection/tone/hdrWhite`, `basicCorrection/whiteBalance/temperature` / `tint`, `basicCorrection/saturation`, `basicCorrection/tone/exposure`, `.../contrast`, `highlights`, `shadows`, `whites`, `blacks`, `hdrSpecular`, `creative/lookIntensity`, `creative/adjustments/fadedFilm`, `sharpen`, `vibrance`, `creativeSaturation`, `tintBalance`, `hslSecondary/key/showMask`, `maskStyle`, `invertMask`, `hslSecondary/refine/denoise`, `blur`, `hslSecondary/correction/*`, `vignette/*`; `hslSecondary/key/hue*`, `saturation*`, `lightness*`; `curves/rgbCurves/master`; `curves/hueSaturationCurves/hueVsSat`; `colorWheels/shadowsWheel`, `midtonesWheel`, `highlightsWheel` | sliders: the stored number x 1 (valid maxima 1000, 300, 300, 7, 150, 200, 150, 100, 100, 300, 150, 100, 1000, 5, 100 as in `pard`); checkboxes and the popup as elsewhere, **also from records in the default state** (`tdsb` 3); the key: hue centre x 360 degrees, range x 360 as the half width, tolerance x 360 as the softness, saturation and lightness centre +- range x 100 percent, tolerance x 100, once a colour was picked; the master curve's points as `x,y` (0..1); Hue vs Sat's points with y as `0.5 + y / 2` (After Effects' y 1 doubles the saturation, EffectCraft's y 1 too); a wheel's hue (degrees) and distance as the HSL colour of that hue with saturation = distance and lightness 0.5 | 1200: all 62 read-backs of mapped controls in sections that act equal after import (min, max, sections, popups); the other 61 lie in sections that do nothing and are reported; keys and the expression on HDR White (`-56-keys`, `-57-expr`); the presets `-60-preset-curves-rgb`, `-61-preset-curves-huesat`, `-62-preset-wheels` | - **Nothing to import:** the implicit input-layer parameter (index 0), the Swap Colors buttons (Tint 4, Ramp 8: they always have a record and hold no value), Exposure's twirl-down group @@ -242,8 +242,15 @@ parameter gets the file's value: The Input LUT and the Look are reported by name (LUT files are not in the project). Controls without a counterpart (the Auto tone's Intensity, RGB Curves' HDR Range, Color Wheels' HDR White and Face Detection, Legacy Tone And Saturation, Auto Color, SpeedGrade Custom Layer), the eyedropper - colours, and the RGB curves, colour wheels, Split Toning, HSL correction wheels and moved hue / - saturation curve points (not sampled) are reported when set. Render check at 0.5 s against After + colours, the red, green and blue curves (only the master curve is sampled, so which is which is + not known), the colour wheels' brightness sliders and their fourth wheel, Split Toning, the HSL + correction wheels and moved points of the other four hue / saturation curves (not sampled) are + reported when set. The master RGB curve arrives as its points; Hue vs Sat with y as `0.5 + y / 2` + (After Effects' y is the offset from neutral, 1 doubling the saturation; EffectCraft's y = 1 + doubles it); a colour wheel's hue and distance as the HSL colour of that hue with saturation = + distance, lightness 0.5 (EffectCraft's wheels have no brightness slider). Legacy Tone And + Saturation is reported when a record says on: its `pard` default turns from off to on when the + project is saved again. Render check at 0.5 s against After Effects' 32 bpc PNGs (stored as value x 6553.5 in 16 bits, so read as raw / 6553.5): every composition whose grade After Effects draws as identity (defaults, set-all with every section off, the switched-off sections, the popups, keys, expression) is within 1/255 in EffectCraft; the @@ -253,6 +260,15 @@ parameter gets the file's value: EffectCraft clips at 1 and keeps black at 0; mean 0.12), Creative adjustments (sec-adjustments: Faded Film lifts black to 0.07 in After Effects, 0.035 here) and the extremes (min / max: mean 0.063 / 0.454; max: After Effects' vignette at +5 and the tone sliders give mid grey, EffectCraft white). + The 32 bpc PNGs hold the pixel value with no transfer curve (the defaults' ramp reads x / 320 + exactly), so values above 1 survive up to about 10; the comparison clips After Effects at 1 as + EffectCraft's 8-bit frame does. The hand-made presets: the master RGB curve within 0.024 (mean + 0.009; After Effects' curve is a natural cubic spline through the points, within 0.003, while + EffectCraft's Lumetri interpolates monotone); Hue vs Sat mean 0.008, max 0.18 (After Effects + scales the saturation in linear light, red reaching 1.26; EffectCraft in display values, 8.3 % of + pixels > 2 levels); the colour wheels mean 0.042, max 0.13 (the hues go the same way: Shadows + lift blue at black by 0.13 in After Effects, 0.21 here; Highlights add green; the brightness + sliders are not imported); the Input LUT and the Look are not imported (mean 0.088 / 0.076). - **Colour.** After Effects stores the colour its picker shows as ARGB doubles 0..255 in the project's working-space encoding; EffectCraft's parameter colours are straight RGBA 0..1 that the render pipeline hands to the effect as stored (`crates/render/src/color.rs`: authored colours are @@ -412,5 +428,5 @@ crate yet. ## Not done yet -Lumetri Color's section switches (a switched-off section is reported and its values left out), its LUTs and looks (files After Effects names: reported by name), its RGB curves, colour wheels, Split Toning and HSL correction wheels (not sampled: reported when set) and moved hue / saturation curve points (the scale of y is not sampled); effects outside the table (EXtractoR: its channel choice is arbitrary data; Stroke: its path is a mask reference), environment lights, the ease of bezier Orientation keys, wiggly and expression selectors and the other text animator properties (Character Value, Character Alignment other than its default, the Hue / Saturation / Brightness / Opacity variants, 3D rotations, variable font axes), manual kerning, No Break, the stroke line join, the Japanese leading model, Source Text keyframes, layer-style gradients, Pattern Overlay and the switch state 3, Mesh Warp handles moved on their own, +Lumetri Color's section switches (a switched-off section is reported and its values left out), its LUTs and looks (files After Effects names: reported by name), its red, green and blue curves (no sample tells which is which), the colour wheels' brightness sliders and the fourth wheel, Split Toning and the HSL correction wheels (not sampled: reported when set) and moved points of the four hue / saturation curves other than Hue vs Sat (the scale of y is not sampled); effects outside the table (EXtractoR: its channel choice is arbitrary data; Stroke: its path is a mask reference), environment lights, the ease of bezier Orientation keys, wiggly and expression selectors and the other text animator properties (Character Value, Character Alignment other than its default, the Hue / Saturation / Brightness / Opacity variants, 3D rotations, variable font axes), manual kerning, No Break, the stroke line join, the Japanese leading model, Source Text keyframes, layer-style gradients, Pattern Overlay and the switch state 3, Mesh Warp handles moved on their own, separated dimensions and footage interpretation need verified layouts in the reader first. From 337299ddf6eb9af6b9644a1ef21698171b7048c0 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 17:50:28 +0100 Subject: [PATCH 017/350] AEP import docs: Lumetri wheel render figure measured at the same pixel The EffectCraft value for the Shadows wheel's blue lift at black was read at input 0.05; at input 0 it is 0.17 (After Effects 0.13). Part of #3111 Co-Authored-By: Claude Opus 5.5 --- docs/aep-import.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/aep-import.md b/docs/aep-import.md index f09f0ba1a..7ad0efb83 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -267,7 +267,7 @@ parameter gets the file's value: EffectCraft's Lumetri interpolates monotone); Hue vs Sat mean 0.008, max 0.18 (After Effects scales the saturation in linear light, red reaching 1.26; EffectCraft in display values, 8.3 % of pixels > 2 levels); the colour wheels mean 0.042, max 0.13 (the hues go the same way: Shadows - lift blue at black by 0.13 in After Effects, 0.21 here; Highlights add green; the brightness + lift blue at black by 0.13 in After Effects, 0.17 here; Highlights add green; the brightness sliders are not imported); the Input LUT and the Look are not imported (mean 0.088 / 0.076). - **Colour.** After Effects stores the colour its picker shows as ARGB doubles 0..255 in the project's working-space encoding; EffectCraft's parameter colours are straight RGBA 0..1 that the From 495221aac67f1b8491455fe8bd1bd1fe3f40428f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 18:36:56 +0100 Subject: [PATCH 018/350] AEP import: doc comments on the quick-win test helpers and tests Co-Authored-By: Claude Opus 5.5 --- crates/aep/src/import/effects.rs | 2 ++ crates/aep/tests/it/import/effects_quickwins.rs | 7 +++++++ crates/aep/tests/it/import/fx_inject.rs | 15 +++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/crates/aep/src/import/effects.rs b/crates/aep/src/import/effects.rs index 1d20a9dd1..c078f7b76 100644 --- a/crates/aep/src/import/effects.rs +++ b/crates/aep/src/import/effects.rs @@ -133,6 +133,8 @@ struct ParamOut<'a> { col: &'a mut Collector, } +/// Imports one parameter of `fx` into its EffectCraft property: its keyframes, the value in its +/// record or the `pard` default, with a loss for what cannot be read or mapped. fn import_param(fx: &AepEffect<'_, '_>, map: &EffectMap, p: &EffectParam<'_, '_>, cx: &LayerCtx<'_>, shown: &str, out: ParamOut<'_>) { let ParamOut { group, refs, col } = out; if carries_nothing(p) { diff --git a/crates/aep/tests/it/import/effects_quickwins.rs b/crates/aep/tests/it/import/effects_quickwins.rs index 792e08d74..0d4ed61ec 100644 --- a/crates/aep/tests/it/import/effects_quickwins.rs +++ b/crates/aep/tests/it/import/effects_quickwins.rs @@ -10,6 +10,7 @@ use effectcraft_keyframe::Value; const ECHO_OPS: &[&str] = &["Add", "Maximum", "Minimum", "Screen", "Composite In Back", "Composite In Front", "Blend"]; +/// Echo with the definitions After Effects 2026 wrote and the stored `values`. fn echo(values: Vec<(usize, f64)>) -> Fx { Fx { match_name: "ADBE Echo", @@ -26,6 +27,7 @@ fn echo(values: Vec<(usize, f64)>) -> Fx { } } +/// HDR Highlight Compression's Amount, set and default. #[test] fn hdr_highlight_compression_amount() { let fx = |values| Fx { @@ -43,6 +45,7 @@ fn hdr_highlight_compression_amount() { } } +/// Echo's values and operator, set and default. #[test] fn echo_values_and_operator() { // `echo-01-set-all` (without its Decay 20, below) and `echo-09-pop5-6`. @@ -61,6 +64,7 @@ fn echo_values_and_operator() { assert_eq!((num(&g, "numberOfEchoes"), v(&g, "echoOperator")), (1.0, Value::Enum(0))); } +/// Echo values beyond EffectCraft's ranges are imported as stored and reported. #[test] fn echo_values_beyond_effectcrafts_range_are_reported() { // `echo-03-max`: 30 000 echoes and Decay 100; EffectCraft's Echo uses at most 255 and 1. @@ -75,6 +79,7 @@ fn echo_values_beyond_effectcrafts_range_are_reported() { near(&g, "decay", 100.0); } +/// Linear Wipe's values, set and default. #[test] fn linear_wipe_values() { let fx = |values| Fx { @@ -99,6 +104,7 @@ fn linear_wipe_values() { near(&run(&fx(vec![]), "ec.transition.linearwipe").0.expect("imported"), "angle", 90.0); } +/// Offset's Blend With Original as a percentage and its centre as a point. #[test] fn offset_blend_is_a_fraction_and_the_centre_a_point() { let fx = |values| Fx { @@ -118,6 +124,7 @@ fn offset_blend_is_a_fraction_and_the_centre_a_point() { near(&run(&fx(vec![(2, 0.25)]), "ec.distort.offset").0.expect("imported"), "blend", 25.0); } +/// Color Stabilizer's points, Sample Size and Stabilize, default and set. #[test] fn color_stabilizer_points_sample_size_and_stabilize() { let fx = |values| Fx { diff --git a/crates/aep/tests/it/import/fx_inject.rs b/crates/aep/tests/it/import/fx_inject.rs index 9687e2d6c..bf9f08ea2 100644 --- a/crates/aep/tests/it/import/fx_inject.rs +++ b/crates/aep/tests/it/import/fx_inject.rs @@ -37,6 +37,7 @@ pub enum Default { Color([u8; 4]), } +/// The parameter type code `pard` byte 15 holds for `k`. fn kind_code(k: ParamKind) -> u8 { match k { ParamKind::Layer => 0, @@ -57,10 +58,12 @@ fn kind_code(k: ParamKind) -> u8 { } } +/// `v` as a big-endian 16.16 fixed-point value. fn fixed(v: f64) -> [u8; 4] { ((v * 65536.0).round() as i32).to_be_bytes() } +/// The 148-byte parameter definition (`pard`) of `d`: type, name, default and range. pub fn pard(d: &Def) -> Chunk<'static> { let mut b = vec![0u8; 148]; b[15] = kind_code(d.kind); @@ -99,6 +102,7 @@ pub fn tdmn(name: &str) -> Chunk<'static> { Chunk::data(*b"tdmn", b) } +/// A UTF-8 string chunk (`Utf8`). fn utf8(s: &str) -> Chunk<'static> { Chunk::data(*b"Utf8", s.as_bytes().to_vec()) } @@ -137,10 +141,13 @@ pub struct Fx { pub data: Vec<(usize, Vec)>, } +/// The match name of parameter `i` of `fx` (`-NNNN`). fn param_name(fx: &Fx, i: usize) -> String { format!("{}-{i:04}", fx.match_name) } +/// The effect's `sspc`: its name, the parameter definitions (`parT`) and the stored values +/// (`tdgp`), each value record copied from `template`. fn sspc(fx: &Fx, template: &Chunk<'_>) -> Chunk<'static> { let n = fx.defs.len() + 2; let mut part = vec![ @@ -194,6 +201,7 @@ fn find_glow_template(chunks: &[Chunk<'_>]) -> Option> { None } +/// Replaces every Glow (`ADBE Glo2` with its `sspc`) below `chunks` with `fx`; the number replaced. fn replace_glow(chunks: &mut [Chunk<'_>], fx: &Fx, template: &Chunk<'_>) -> usize { let mut n = 0; for ch in chunks { @@ -239,10 +247,12 @@ pub fn losses(r: &ImportResult, category: LossCategory, match_name: &str) -> Vec r.report.losses.iter().filter(|l| l.category == category).flat_map(|l| l.details.clone()).filter(|d| d.contains(&tag)).collect() } +/// The value of property `id` of `g` (panics when it is missing). pub fn v(g: &PropGroup, id: &str) -> Value { g.prop(id).unwrap_or_else(|| panic!("no {id} in {}", g.match_id)).value.clone() } +/// The scalar value of property `id` of `g`. pub fn num(g: &PropGroup, id: &str) -> f64 { match v(g, id) { Value::Scalar(x) => x, @@ -250,11 +260,13 @@ pub fn num(g: &PropGroup, id: &str) -> f64 { } } +/// Asserts that property `id` of `g` is `want` (relative 1e-6). pub fn near(g: &PropGroup, id: &str, want: f64) { let got = num(g, id); assert!((got - want).abs() <= want.abs().max(1.0) * 1e-6, "{} {id}: {got} != {want}", g.match_id); } +/// Asserts that the point property `id` of `g` is `want` (within 0.01). pub fn pt(g: &PropGroup, id: &str, want: [f64; 2]) { let Value::Vec2(a) = v(g, id) else { panic!("{id}") }; assert!((a[0] - want[0]).abs() < 1e-2 && (a[1] - want[1]).abs() < 1e-2, "{} {id}: {a:?} != {want:?}", g.match_id); @@ -265,14 +277,17 @@ pub fn slider(kind: ParamKind, name: &'static str, default: f64, valid: (f64, f6 Def { kind, name, default: Default::Number(default), valid, items: &[] } } +/// A popup definition: default item (1-based) and items. pub fn popup(name: &'static str, default: u32, items: &'static [&'static str]) -> Def { Def { kind: ParamKind::Popup, name, default: Default::Index(default), valid: (0.0, 0.0), items } } +/// A definition of another kind (angle, point, colour, ...) with `default`. pub fn other(kind: ParamKind, name: &'static str, default: Default) -> Def { Def { kind, name, default, valid: (0.0, 0.0), items: &[] } } +/// The definitions written here read back as the reader decodes After Effects' `pard`. #[test] fn pard_layout_decodes_as_written() { // Echo's Decay (float, valid 0..100, default 1), Linear Wipe's Feather (fixed, valid max From c32aa7cfbc1c63d1f8b9cba1c5e34419fd498d7e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 23:58:35 +0100 Subject: [PATCH 019/350] AEP import: Echo keyframe regression for the range loss Co-Authored-By: Claude Sonnet 5.5 --- .../aep/tests/it/import/effects_quickwins.rs | 23 +++++++++++++++ crates/aep/tests/it/import/fx_inject.rs | 29 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/crates/aep/tests/it/import/effects_quickwins.rs b/crates/aep/tests/it/import/effects_quickwins.rs index 0d4ed61ec..f2b0594b6 100644 --- a/crates/aep/tests/it/import/effects_quickwins.rs +++ b/crates/aep/tests/it/import/effects_quickwins.rs @@ -24,6 +24,7 @@ fn echo(values: Vec<(usize, f64)>) -> Fx { ], values, data: vec![], + keys: vec![], } } @@ -36,6 +37,7 @@ fn hdr_highlight_compression_amount() { defs: vec![slider(K::FloatSlider, "Amount", 100.0, (0.0, 100.0))], values, data: vec![], + keys: vec![], }; // `hdr-01-set-all`: 90; `hdr-00-defaults`: no record, 100. for (values, want) in [(vec![(1, 90.0)], 90.0), (vec![], 100.0)] { @@ -79,6 +81,24 @@ fn echo_values_beyond_effectcrafts_range_are_reported() { near(&g, "decay", 100.0); } +/// A keyframed Echo parameter keeps its keys as stored and reports the range loss. +#[test] +fn echo_keys_beyond_effectcrafts_range_are_kept_and_reported() { + // Number Of Echoes keyed 10 -> 30 000: the second key is beyond EffectCraft's 255. + let mut fx = echo(vec![]); + fx.keys = vec![(2, vec![(0, 10.0), (1000, 30000.0)])]; + let (g, r) = run(&fx, "ec.time.echo"); + let found = losses(&r, LossCategory::Effects, "ADBE Echo"); + assert_eq!(found.len(), 1, "{found:?}"); + assert!(found[0].contains("Number Of Echoes") && found[0].contains("30000") && found[0].contains("0 to 255"), "{found:?}"); + assert!(losses(&r, LossCategory::DamagedData, "ADBE Echo").is_empty()); + // Both keys arrive with their values. + let g = g.expect("still imported"); + let keys = &g.prop("numberOfEchoes").expect("numberOfEchoes").keys; + let values: Vec = keys.iter().map(|k| k.value.clone()).collect(); + assert_eq!(values, [Value::Scalar(10.0), Value::Scalar(30000.0)]); +} + /// Linear Wipe's values, set and default. #[test] fn linear_wipe_values() { @@ -92,6 +112,7 @@ fn linear_wipe_values() { ], values, data: vec![], + keys: vec![], }; // `linwipe-01-set-all`. let (g, r) = run(&fx(vec![(1, 10.0), (2, 77.0), (3, 16.0)]), "ec.transition.linearwipe"); @@ -113,6 +134,7 @@ fn offset_blend_is_a_fraction_and_the_centre_a_point() { defs: vec![other(K::Point, "Shift Center To", Default::Point(0.5, 0.5)), slider(K::FixedSlider, "Blend With Original", 0.0, (0.0, 1.0))], values, data: vec![], + keys: vec![], }; // `offset-02-chk-toggled`: Blend With Original stored 1.0 is 100 %. let (g, r) = run(&fx(vec![(2, 1.0)]), "ec.distort.offset"); @@ -139,6 +161,7 @@ fn color_stabilizer_points_sample_size_and_stabilize() { ], values, data: vec![], + keys: vec![], }; // `colstab-00-defaults`: Levels, the points at 20 %, 50 % and 80 % of the width. let (g, r) = run(&fx(vec![]), "ec.color.colorstabilizer"); diff --git a/crates/aep/tests/it/import/fx_inject.rs b/crates/aep/tests/it/import/fx_inject.rs index bf9f08ea2..81e6a821a 100644 --- a/crates/aep/tests/it/import/fx_inject.rs +++ b/crates/aep/tests/it/import/fx_inject.rs @@ -130,6 +130,27 @@ fn record(template: &Chunk<'_>, v: f64) -> Chunk<'static> { Chunk::list(*b"tdbs", kids) } +/// A keyframed value record: `record`'s header with a key list of linear one-component keys. +fn keyed_record(template: &Chunk<'_>, keys: &[(i32, f64)]) -> Chunk<'static> { + let Body::List { children, .. } = record(template, keys.first().map_or(0.0, |k| k.1)).body else { unreachable!("record builds a LIST") }; + let mut children = children; + let mut lhd3 = vec![0u8; 52]; + lhd3[0..4].copy_from_slice(&0x00d0_0beeu32.to_be_bytes()); + lhd3[8..12].copy_from_slice(&(keys.len() as u32).to_be_bytes()); + lhd3[16..20].copy_from_slice(&48u32.to_be_bytes()); + let mut ldat = Vec::new(); + for (time, value) in keys { + let mut rec = vec![0u8; 48]; + rec[0..4].copy_from_slice(&time.to_be_bytes()); + rec[4] = 1; + rec[5] = 1; + rec[8..16].copy_from_slice(&value.to_be_bytes()); + ldat.extend(rec); + } + children.push(Chunk::list(*b"list", vec![Chunk::data(*b"lhd3", lhd3), Chunk::data(*b"ldat", ldat)])); + Chunk::list(*b"tdbs", children) +} + /// An effect instance: its match name, display name, parameter definitions (after the input /// layer, before the built-in entry), the 1-D values stored (`parT` index, value) and the /// arbitrary data stored (`parT` index, bytes). @@ -139,6 +160,9 @@ pub struct Fx { pub defs: Vec, pub values: Vec<(usize, f64)>, pub data: Vec<(usize, Vec)>, + /// Keyframed 1-D parameters (`parT` index, (time units, value) per key), linear keys; the + /// parameter then has no entry in `values`. + pub keys: Vec<(usize, Vec<(i32, f64)>)>, } /// The match name of parameter `i` of `fx` (`-NNNN`). @@ -172,6 +196,10 @@ fn sspc(fx: &Fx, template: &Chunk<'_>) -> Chunk<'static> { values.push(Chunk::list(*b"aRbs", vec![Chunk::data(*b"aRbp", d.clone())])); } } + for (i, keys) in &fx.keys { + values.push(tdmn(¶m_name(fx, *i))); + values.push(keyed_record(template, keys)); + } Chunk::list(*b"sspc", vec![nested(*b"fnam", vec![utf8(fx.name)]), Chunk::list(*b"parT", part), Chunk::list(*b"tdgp", values)]) } @@ -306,6 +334,7 @@ fn pard_layout_decodes_as_written() { ], values: vec![(1, 20.0)], data: vec![], + keys: vec![], }; let bytes = bytes_with(&fx); let file = parse(&bytes).unwrap(); From 0f882368846d75bd30212abfe9ae5b4e11bb3eb9 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Fri, 6 Nov 2026 00:05:54 +0100 Subject: [PATCH 020/350] AEP import: negative cases for EXtractoR and IDentifier channel names and a missing Channel Info payload Co-Authored-By: Claude Sonnet 5.5 --- .../aep/tests/it/import/effects_thirdparty.rs | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/crates/aep/tests/it/import/effects_thirdparty.rs b/crates/aep/tests/it/import/effects_thirdparty.rs index 7bf1ad3c8..7c7ec190c 100644 --- a/crates/aep/tests/it/import/effects_thirdparty.rs +++ b/crates/aep/tests/it/import/effects_thirdparty.rs @@ -44,6 +44,7 @@ fn neutralizer(values: Vec<(usize, f64)>) -> Fx { ], values, data: vec![], + keys: vec![], } } @@ -101,6 +102,7 @@ fn extractor(values: Vec<(usize, f64)>, data: Vec) -> Fx { ], values, data: vec![(1, data)], + keys: vec![], } } @@ -183,6 +185,7 @@ fn identifier(values: Vec<(usize, f64)>, data: Vec) -> Fx { ], values, data: vec![(1, data)], + keys: vec![], } } @@ -217,3 +220,71 @@ fn identifier_chosen_channels_by_name() { assert!(found.len() == 1 && found[0].contains(&format!("the ID channel '{name}' (Channel Info) is not imported")), "{name}: {found:?}"); } } + +/// EXtractoR's data with the name of record `k` from byte 35 replaced by `name` (no NUL added; the +/// rest of the record's name field is left as `fill`). +fn channels_with_raw_name(k: usize, name: &[u8], fill: u8) -> Vec { + let mut d = channels(DEFAULT_CHANNELS); + let at = 32 + k * 164 + 35; + d[at..at + 129].fill(fill); + d[at..at + name.len()].copy_from_slice(name); + d +} + +/// A channel name that never ends (no NUL up to the end of its record) or is not UTF-8 is +/// refused by the decoder and reported at the importer; the defaults R, G, B, A stay. +#[test] +fn extractor_names_without_a_terminator_or_in_invalid_utf8_are_refused() { + let unterminated = channels_with_raw_name(2, b"Z", b'Z'); + let invalid = channels_with_raw_name(1, &[0x5a, 0xff, 0xfe, 0x00], 0); + for (what, data) in [("unterminated", unterminated), ("invalid UTF-8", invalid)] { + assert_eq!(ExtractorChannels::parse(&data), None, "{what}"); + let (g, r) = run(&extractor(vec![], data), "ec.3d.extractor"); + let found = losses(&r, LossCategory::Effects, "EXtractoR"); + assert!(found.len() == 1 && found[0].contains("not in the sampled layout"), "{what}: {found:?}"); + let g = g.expect("imported"); + for (id, want) in [("red", "R"), ("green", "G"), ("blue", "B"), ("alpha", "A")] { + assert_eq!(v(&g, id), Value::Str(want.into()), "{what}: {id}"); + } + } +} + +/// The same for IDentifier: a channel name without NUL up to the end of the data, or in invalid +/// UTF-8, is refused and reported; the Object ID channel stays. +#[test] +fn identifier_names_without_a_terminator_or_in_invalid_utf8_are_refused() { + let mut unterminated = id_info("objectId"); + unterminated[19..].fill(b'x'); + let mut invalid = id_info("objectId"); + invalid[19..22].copy_from_slice(&[b'o', 0xff, 0]); + for (what, data) in [("unterminated", unterminated), ("invalid UTF-8", invalid)] { + assert_eq!(identifier_channel_type(&data), None, "{what}"); + let (g, r) = run(&identifier(vec![], data), "ec.3d.identifier"); + let found = losses(&r, LossCategory::Effects, "IDentifier"); + assert!(found.len() == 1 && found[0].contains("not in the sampled layout"), "{what}: {found:?}"); + assert_eq!(v(&g.expect("imported"), "channelType"), Value::Enum(0), "{what}"); + } + // Data too short to reach the name. + assert_eq!(identifier_channel_type(&[1; 10]), None); +} + +/// A Channel Info value record without its arbitrary data (no `aRbs`): the effect still imports, +/// with the defaults, and nothing panics. +#[test] +fn a_missing_channel_info_payload_leaves_the_defaults() { + let mut fx = extractor(vec![], vec![]); + fx.data.clear(); + let (g, r) = run(&fx, "ec.3d.extractor"); + assert_eq!(losses(&r, LossCategory::Effects, "EXtractoR"), Vec::::new()); + let g = g.expect("imported"); + for (id, want) in [("red", "R"), ("green", "G"), ("blue", "B"), ("alpha", "A")] { + assert_eq!(v(&g, id), Value::Str(want.into()), "{id}"); + } + let mut fx = identifier(vec![(3, 5.0)], vec![]); + fx.data.clear(); + let (g, r) = run(&fx, "ec.3d.identifier"); + assert_eq!(losses(&r, LossCategory::Effects, "IDentifier"), Vec::::new()); + let g = g.expect("imported"); + assert_eq!(v(&g, "channelType"), Value::Enum(0)); + near(&g, "id", 5.0); +} From 692cc5228903ce421f79a51fa1b65789cd3d8ee7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Fri, 6 Nov 2026 00:15:17 +0100 Subject: [PATCH 021/350] AEP import: Lumetri value records without their arbitrary data are reported as damaged Co-Authored-By: Claude Sonnet 5.5 --- crates/aep/src/import/lumetri.rs | 5 +++++ crates/aep/tests/it/import/lumetri.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/aep/src/import/lumetri.rs b/crates/aep/src/import/lumetri.rs index 9369155f4..194afe15f 100644 --- a/crates/aep/src/import/lumetri.rs +++ b/crates/aep/src/import/lumetri.rs @@ -590,6 +590,11 @@ pub(crate) fn import_lumetri(fx: &AepEffect<'_, '_>, params: &[EffectParam<'_, ' Role::Lut { lut } => note_lut(p, row.suffix, data(), &mut luts[usize::from(!lut)]), // The Look's colour space only applies to a Look, which is reported. Role::LookSpace => {} + Role::Data(_) if p.stored().is_some() && data().is_none() => { + // The value record is there but its aRbs/aRbp payload is not: damaged, not the + // default (a parameter without a record is the default). + col.lose(LossCategory::DamagedData, format!("{here}: the data of {label} is missing, not imported (the default used)")); + } Role::Data(Data::HslKey) => import_hsl_key(data(), &here, &mut *group, &mut *col), Role::Data(d) => { if p.is_animated() || p.stored().is_some_and(|s| s.expression().is_some()) { diff --git a/crates/aep/tests/it/import/lumetri.rs b/crates/aep/tests/it/import/lumetri.rs index a3ec9138b..d31960cef 100644 --- a/crates/aep/tests/it/import/lumetri.rs +++ b/crates/aep/tests/it/import/lumetri.rs @@ -84,6 +84,8 @@ enum Rec { Num(f64, u32), /// Arbitrary data. Data(Vec), + /// A value record whose arbitrary data (`aRbs`) is missing. + NoData, } /// A static value record after the one After Effects wrote for Glow's A & B Midpoint (its header, @@ -229,6 +231,7 @@ fn sspc(defs: &[(u16, Def)], recs: &[(u16, Rec)], template: &Chunk<'_>) -> Chunk values.push(record(template, 1, 0.0)); values.push(Chunk::list(*b"aRbs", vec![Chunk::data(*b"aRbp", d)])); } + Rec::NoData => values.push(record(template, 1, 0.0)), } } Chunk::list(*b"sspc", vec![nested(*b"fnam", b"Lumetri Color"), Chunk::list(*b"parT", part), Chunk::list(*b"tdgp", values)]) @@ -436,6 +439,21 @@ fn the_master_rgb_curve_arrives_moved_channel_curves_are_reported() { assert!(losses(&r).iter().any(|x| x.contains("RGB Curve (ADBE Lumetri-0039) is unreadable")), "{:?}", losses(&r)); } +/// A value record without its arbitrary data is damaged, not the default: reported for the RGB +/// curve and the HSL key, the neighbours stay as they were. +#[test] +fn a_value_record_without_its_data_is_reported_as_damaged() { + for (suffix, name) in [(39, "RGB Curve (ADBE Lumetri-0039)"), (73, "HSL Sliders (ADBE Lumetri-0073)")] { + let (g, r) = run(&[(suffix, Rec::NoData)]); + let l = losses(&r); + assert!(l.len() == 1 && l[0].contains(name) && l[0].contains("is missing"), "{suffix}: {l:?}"); + assert!(r.report.losses.iter().any(|x| x.category == LossCategory::DamagedData), "{suffix}"); + // The grade stays at the default. + assert_eq!(val(&g, "curves/rgbCurves/master"), Value::Str("0,0 1,1".into())); + assert_eq!(num(&g, "hslSecondary/key/hueRange"), 180.0); + } +} + #[test] fn colour_wheels_arrive_as_colours_their_brightness_is_reported() { // 1200 `-62-preset-wheels`: Shadows towards blue with the slider down, Midtones towards red, From afdcf956fbe4a328c1714846a46420565a2d0e47 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:13:18 +0100 Subject: [PATCH 022/350] Effects: Grid, Checkerboard and Circle keep the layer's mask with Blending Mode None Masks are applied to a layer before its effects, but with the default Blending Mode None these generators replaced each pixel with the generated pattern, so the pattern drew over the whole layer and the mask was lost. With None the generated alpha is now scaled by the layer's alpha, as 4-Color Gradient already did (#1381). A shared gen_alpha helper (CPU) and its WGSL twin hold the rule for Grid, Checkerboard, Circle and 4-Color Gradient. Only None keeps the layer's alpha: the other modes composite the pattern onto the layer as After Effects' blending modes do, and their alpha outside the layer is not verified against After Effects, so they are unchanged. Fixes #3121 --- crates/effects/src/generate.rs | 71 ++++++++++++++++++++++--- crates/gpu/src/shaders/fx_extra.wgsl | 5 +- crates/gpu/src/shaders/fx_generate.wgsl | 18 +++++-- crates/render/src/tests.rs | 24 +++++++++ 4 files changed, 104 insertions(+), 14 deletions(-) diff --git a/crates/effects/src/generate.rs b/crates/effects/src/generate.rs index ae0d61861..2c772219a 100644 --- a/crates/effects/src/generate.rs +++ b/crates/effects/src/generate.rs @@ -53,7 +53,8 @@ fn gradient_ramp(ctx: &EffectCtx, mut b: Buf) -> Buf { } /// Blending Mode options of the shape generators (Checkerboard, Grid, Circle): "None" shows the -/// generated pattern only; the others composite it onto the layer like layer blending modes. +/// generated pattern only, inside the layer's alpha (see [`gen_alpha`]); the others composite it +/// onto the layer like layer blending modes. pub(crate) const GEN_BLEND_MODES: [&str; 20] = [ "None", "Normal", @@ -103,7 +104,8 @@ pub fn gen_mode(i: u32) -> Option { } /// Composite a generated premultiplied pixel `g` onto the original `o` with blending mode -/// index `mode` (into [`GEN_BLEND_MODES`]). +/// index `mode` (into [`GEN_BLEND_MODES`]). None returns `g` as is: the caller keeps the layer's +/// alpha in `g` with [`gen_alpha`]. #[inline] pub(crate) fn gen_blend(o: Px, g: Px, mode: u32) -> Px { match gen_mode(mode) { @@ -112,6 +114,16 @@ pub(crate) fn gen_blend(o: Px, g: Px, mode: u32) -> Px { } } +/// The generated alpha `a` with Blending Mode None (`keep_alpha`, `gen_mode(mode).is_none()`): +/// scaled by the layer's alpha `layer_a`, so the pattern stays inside the layer's shape and its +/// masks, which are applied before effects (#1381, #3121). The other modes composite onto the +/// layer as After Effects' blending modes do and keep `a`; their alpha outside the layer is not +/// verified against After Effects. Mirrored by `gen_alpha` in `fx_generate.wgsl`. +#[inline] +pub(crate) fn gen_alpha(a: f32, layer_a: f32, keep_alpha: bool) -> f32 { + if keep_alpha { a * layer_a } else { a } +} + /// 4-Color Gradient's weights are inverse squared distances softened by Blend²: each point /// weighs 1 / (d² + Blend²) (pixels). Fitted to After Effects 2026 renders at Blend 10, 50, 100 /// and 300 (power 2.00 at every Blend, softening 0 / 2367 / 9817 / 90127 ≈ Blend²; mean error @@ -128,6 +140,7 @@ fn four_color(ctx: &EffectCtx, mut b: Buf) -> Buf { let jitter = (ctx.params.f("jitter") / 100.0).clamp(0.0, 1.0) as f32; let op = ctx.params.f("opacity") as f32 / 100.0; let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); let seed = ctx.seed; b.img.rows_mut().for_each(|(y, row)| { for (x, px) in row.iter_mut().enumerate() { @@ -149,7 +162,7 @@ fn four_color(ctx: &EffectCtx, mut b: Buf) -> Buf { } // None keeps the layer's alpha (#1381), as the Gradient Ramp does: the gradient fills // a shape or text, not its bounding box. - let a = if gen_mode(mode).is_none() { op * px[3] } else { op }; + let a = gen_alpha(op, px[3], keep_alpha); *px = gen_blend(*px, [c[0] * a, c[1] * a, c[2] * a, a], mode); } }); @@ -179,6 +192,7 @@ fn checkerboard(ctx: &EffectCtx, mut b: Buf) -> Buf { let c = ctx.params.color("color"); let op = ctx.params.f("opacity") as f32 / 100.0; let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); // Signed distance (in feather units) to the nearest cell edge, positive in even cells. let axis = |p: f64, size: f64, f: f64| -> f64 { let u = p / size; @@ -191,8 +205,8 @@ fn checkerboard(ctx: &EffectCtx, mut b: Buf) -> Buf { for (x, px) in row.iter_mut().enumerate() { let sx = axis(x as f64 + 0.5 - anchor.0, w, fw); let sy = axis(y as f64 + 0.5 - anchor.1, h, fh); - let cov = ((0.5 + 0.5 * sx * sy) as f32).clamp(0.0, 1.0) * op; - *px = gen_blend(*px, [c[0] * cov, c[1] * cov, c[2] * cov, cov], mode); + let a = gen_alpha(((0.5 + 0.5 * sx * sy) as f32).clamp(0.0, 1.0) * op, px[3], keep_alpha); + *px = gen_blend(*px, [c[0] * a, c[1] * a, c[2] * a, a], mode); } }); b @@ -208,6 +222,7 @@ fn grid(ctx: &EffectCtx, mut b: Buf) -> Buf { let c = ctx.params.color("color"); let op = ctx.params.f("opacity") as f32 / 100.0; let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); b.img.rows_mut().for_each(|(y, row)| { for (x, px) in row.iter_mut().enumerate() { let gx = (x as f64 + 0.5 - anchor.0).rem_euclid(w); @@ -219,7 +234,7 @@ fn grid(ctx: &EffectCtx, mut b: Buf) -> Buf { if invert { cov = 1.0 - cov; } - let a = cov * op; + let a = gen_alpha(cov * op, px[3], keep_alpha); *px = gen_blend(*px, [c[0] * a, c[1] * a, c[2] * a, a], mode); } }); @@ -256,6 +271,7 @@ fn circle(ctx: &EffectCtx, mut b: Buf) -> Buf { let op = ctx.params.f("opacity") as f32 / 100.0; let invert = ctx.params.b("invert"); let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); b.img.rows_mut().for_each(|(y, row)| { for (x, px) in row.iter_mut().enumerate() { let d = ((x as f64 + 0.5 - c.0).powi(2) + (y as f64 + 0.5 - c.1).powi(2)).sqrt(); @@ -266,7 +282,7 @@ fn circle(ctx: &EffectCtx, mut b: Buf) -> Buf { if invert { cov = 1.0 - cov; } - let a = cov * op; + let a = gen_alpha(cov * op, px[3], keep_alpha); *px = gen_blend(*px, [color[0] * a, color[1] * a, color[2] * a, a], mode); } }); @@ -572,6 +588,47 @@ mod generator_tests { assert!(o.img.get(0, 5)[0] > 0.9 && o.img.get(5, 5)[3] < 0.01); } + /// With Blending Mode None, a shape generator's pattern shows only where the layer has alpha + /// (#1381, #3121: masks come before effects). `set` makes the pattern cover (5, 5) and (35, 5). + fn assert_none_stays_inside_the_layer_alpha(id: &str, set: &[(&'static str, Value)]) { + // Three bands: transparent, half and fully opaque, premultiplied. + let mut img = Image::filled(40, 24, [1.0, 0.0, 0.0, 1.0]); + for (i, p) in img.data.iter_mut().enumerate() { + let a = [0.0, 0.5, 1.0][i % 40 / 14]; + *p = p.map(|c| c * a); + } + for op in [100.0, 50.0] { + let mut set = set.to_vec(); + set.push(("opacity", num(op))); + let o = run_fx(id, &set, img.clone(), 0.0, EffectEnv::default()); + let k = op as f32 / 100.0; + for (src, out) in img.data.iter().zip(&o.img.data) { + assert!(out[3] <= src[3] * k + 1e-6, "{id} opacity {op}: alpha {} became {}", src[3], out[3]); + assert!(out[..3].iter().all(|&c| c <= out[3] + 1e-6), "{id} opacity {op}: premultiplied colour above alpha"); + } + assert!(o.img.get(5, 5)[3] < 1e-6, "{id} opacity {op}: the pattern shows where the layer is transparent"); + assert!(o.img.get(35, 5)[3] > 0.9 * k, "{id} opacity {op}: the pattern shows on the opaque layer: {:?}", o.img.get(35, 5)); + } + } + + #[test] + fn grid_none_stays_inside_the_layer_alpha() { + // Inverted, the grid covers nearly every pixel, wherever the lines fall. + assert_none_stays_inside_the_layer_alpha("ec.generate.grid", &[("width", num(10.0)), ("border", num(2.0)), ("invertGrid", Value::Bool(true))]); + } + + #[test] + fn checkerboard_none_stays_inside_the_layer_alpha() { + // 8-pixel squares from the anchor at 0: (5, 5) and (35, 5) lie in on squares. + assert_none_stays_inside_the_layer_alpha("ec.generate.checkerboard", &[("width", num(8.0))]); + } + + #[test] + fn circle_none_stays_inside_the_layer_alpha() { + // A disk larger than the image covers every pixel. + assert_none_stays_inside_the_layer_alpha("ec.generate.circle", &[("center", Value::Vec2([20.0, 12.0])), ("radius", num(100.0))]); + } + #[test] fn circle_edge_makes_a_ring() { let c = ("center", Value::Vec2([16.0, 16.0])); diff --git a/crates/gpu/src/shaders/fx_extra.wgsl b/crates/gpu/src/shaders/fx_extra.wgsl index e1425ddcd..35170b24c 100644 --- a/crates/gpu/src/shaders/fx_extra.wgsl +++ b/crates/gpu/src/shaders/fx_extra.wgsl @@ -29,8 +29,9 @@ fn fxe_circle(@builtin(global_invocation_id) gid: vec3) { if (P.u[0].z != 0u) { cov = 1.0 - cov; } - let a = cov * P.f[1].z; - textureStore(out, p, gen_blend(textureLoad(src, p, 0), vec4(P.f[2].xyz * a, a), P.u[0].x)); + let o = textureLoad(src, p, 0); + let a = gen_alpha(cov * P.f[1].z, o.w, P.u[0].x); + textureStore(out, p, gen_blend(o, vec4(P.f[2].xyz * a, a), P.u[0].x)); } // ---------------------------------------------------------------- Ellipse (generate2::ellipse) diff --git a/crates/gpu/src/shaders/fx_generate.wgsl b/crates/gpu/src/shaders/fx_generate.wgsl index 7a2a026bd..876428435 100644 --- a/crates/gpu/src/shaders/fx_generate.wgsl +++ b/crates/gpu/src/shaders/fx_generate.wgsl @@ -32,6 +32,12 @@ fn gen_blend(o: vec4, g: vec4, mode: u32) -> vec4 { return blend_pixel(mode, o, g, 0.5); } +// generate::gen_alpha: with None (mode 255) the generated alpha `a` is scaled by the layer's +// alpha, so the pattern stays inside the layer and its masks; other modes keep `a`. +fn gen_alpha(a: f32, layer_a: f32, mode: u32) -> f32 { + return select(a, a * layer_a, mode == 255u); +} + // A (hi, lo) pair from `data`. fn gen_hl(i: u32) -> vec2 { return vec2(data[i], data[i + 1u]); @@ -466,9 +472,10 @@ fn gen_checker(@builtin(global_invocation_id) gid: vec3) { } let sx = gen_axis(f32(p.x) + P.f[0].x, P.f[0].z, P.f[1].x); let sy = gen_axis(f32(p.y) + P.f[0].y, P.f[0].w, P.f[1].y); - let cov = clamp(0.5 + 0.5 * sx * sy, 0.0, 1.0) * P.f[1].z; + let o = textureLoad(src, p, 0); + let a = gen_alpha(clamp(0.5 + 0.5 * sx * sy, 0.0, 1.0) * P.f[1].z, o.w, P.u[0].x); let c = P.f[2].xyz; - textureStore(out, p, gen_blend(textureLoad(src, p, 0), vec4(c * cov, cov), P.u[0].x)); + textureStore(out, p, gen_blend(o, vec4(c * a, a), P.u[0].x)); } // f32::rem_euclid. @@ -502,8 +509,9 @@ fn gen_grid(@builtin(global_invocation_id) gid: vec3) { if (P.u[0].y != 0u) { cov = 1.0 - cov; } - let a = cov * P.f[1].z; - textureStore(out, p, gen_blend(textureLoad(src, p, 0), vec4(P.f[2].xyz * a, a), P.u[0].x)); + let o = textureLoad(src, p, 0); + let a = gen_alpha(cov * P.f[1].z, o.w, P.u[0].x); + textureStore(out, p, gen_blend(o, vec4(P.f[2].xyz * a, a), P.u[0].x)); } // u[0] = (mode, seed); f[0..4] = points (x, y); f[4..8] = colours; @@ -535,7 +543,7 @@ fn gen_fourcolor(@builtin(global_invocation_id) gid: vec3) { } let o = textureLoad(src, p, 0); // None keeps the layer's alpha, as the CPU path does (#1381). - let op = select(P.f[8].z, P.f[8].z * o.w, P.u[0].x == 255u); + let op = gen_alpha(P.f[8].z, o.w, P.u[0].x); textureStore(out, p, gen_blend(o, vec4(c * op, op), P.u[0].x)); } diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index c77fc2d94..ab38651a3 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -784,6 +784,30 @@ fn effects_see_layer_masks() { assert!(img.get(150, 50)[3] > 0.99, "mode None mask does not cut the layer"); } +/// Masks come before effects: Grid (Blending Mode None) draws only inside a masked solid (#3121). +#[test] +fn grid_stays_inside_the_layer_mask() { + let (mut p, cid, comp) = setup(); + let mut l = solid(&mut p, &comp, [1.0, 1.0, 1.0], 200, 100); + let mut next = p.next_id; + let m = build::mask(&mut Ids(&mut next), "Mask 1", ShapePath::rect([50.0, 50.0], 40.0, 40.0), MaskMode::Add, [255, 255, 0]); + p.next_id = next; + l.props.sub_mut("masks").unwrap().children.push(m.into()); + // Inverted, the grid covers nearly every pixel, so the check does not depend on where the + // lines fall. + let set = [("width", Value::Scalar(10.0)), ("border", Value::Scalar(2.0)), ("invertGrid", Value::Bool(true))]; + add_effect(&mut p, &mut l, "ec.generate.grid", [200.0, 100.0], &set); + p.comp_mut(cid).unwrap().layers.push(l); + let img = render_frame(&p, cid, Tick::ZERO, 1.0); + for y in 0..100 { + for x in 100..200 { + assert!(img.get(x, y)[3] < 0.01, "grid outside the mask at ({x}, {y}): {:?}", img.get(x, y)); + } + } + assert!(img.get(15, 15)[3] < 0.01, "grid left of and above the mask: {:?}", img.get(15, 15)); + assert!(img.get(55, 55)[3] > 0.5, "grid inside the mask: {:?}", img.get(55, 55)); +} + /// An open mask (the Pen still drawing it) leaves the layer whole; closed, it masks (#290). #[test] fn open_masks_make_no_transparency() { From af50ad43f32a1bd55643da276a6deaa250b0f9a0 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:22:14 +0100 Subject: [PATCH 023/350] Effect Controls: renaming an effect opens with the whole name selected Enter on a selected effect (in Effect Controls, or in the Timeline while Effect Controls shows the effect) focused the effect's rename field before the field existed. The field was then drawn in the same pass as that Enter, took it and gave the keyboard straight back, so it stayed open without the keyboard and with the caret after the name: typing didn't replace the name. The field now takes the keyboard and selects its whole name the pass it is first drawn, as a double-click or the header menu's Rename already opened it, and that pass doesn't commit it. It also always opens on the name the header draws: Enter opened it on the English registry name, so in a translated interface the field showed a different name than the header, and committing it untouched counted as a rename. Fixes #3198 --- .../src/panels/effect_controls/panel.rs | 30 +++++++---- crates/ui-egui/tests/it/ui_effect_controls.rs | 53 +++++++++++++++++++ crates/ui-egui/tests/it/ui_effect_names.rs | 33 +++++++++++- crates/ui-egui/tests/it/ui_group_rename.rs | 3 +- 4 files changed, 106 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 0857dfa3f..cf12711e3 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -5,12 +5,14 @@ use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, Pr // --------------------------------------------------------------------------------------------- -/// Open the rename field on effect `uid` (Enter with the effect selected), name selected. +/// Open the rename field on effect `uid` (Enter with the effect selected). It takes the keyboard, +/// its whole name selected, the pass it is first drawn. pub fn start_rename(ctx: &egui::Context, uid: u64, name: &str) { let id = egui::Id::unique(("ec-rename", uid)); - ctx.data_mut(|d| d.insert_temp(id, name.to_string())); - ctx.data_mut(|d| d.insert_temp(id.with("select"), true)); - ctx.memory_mut(|m| m.request_focus(id.with("field"))); + ctx.data_mut(|d| { + d.insert_temp(id, name.to_string()); + d.insert_temp(id.with("start"), true); + }); } /// Where an effect header's parts sit, for its rename field. @@ -29,16 +31,22 @@ fn rename_field(app: &mut EffectcraftApp, ui: &mut egui::Ui, h: &EffectHdr, mut let EffectHdr { g, shown, r, tw, reset, rename_id, layer_id } = *h; let ctx = ui.ctx().clone(); let name_rect = Rect::from_min_max(pos2(tw.max.x + 6.0, r.min.y + 2.0), pos2(reset.min.x - 6.0, r.max.y - 2.0)); - let response = ui.put(name_rect, egui::TextEdit::singleline(&mut value).id(rename_id.with("field")).font(Tokens::semibold(12.0))); - // Focus the field the frame it first exists: focusing an id not yet on screen leaves - // the accessibility tree with a focus it doesn't contain. - if ctx.data_mut(|d| d.remove_temp::(rename_id.with("start"))).is_some() { - crate::widgets::select_all(&ctx, response.id, &value); - response.request_focus(); + // The field opens with the name the header draws (in the interface language), however it + // was opened, so committing it untouched is not a rename. + let opening = ctx.data_mut(|d| d.remove_temp::(rename_id.with("start"))).is_some(); + if opening { + value = shown.to_string(); } + let response = ui.put(name_rect, egui::TextEdit::singleline(&mut value).id(rename_id.with("field")).font(Tokens::semibold(12.0))); app.auto.add(&format!("effectControls.effect.{}.nameEdit", g.uid), name_rect, "Effect name"); - if ctx.data_mut(|d| d.remove_temp::(rename_id.with("select"))).unwrap_or(false) { + // Focus the field the frame it first exists, its whole name selected so typing replaces it: + // focusing an id not yet on screen leaves the accessibility tree with a focus it doesn't + // contain. The Enter that opened it (it is drawn in that pass) doesn't also commit it. + if opening { widgets::select_all(&ctx, response.id, &value); + response.request_focus(); + ctx.data_mut(|d| d.insert_temp(rename_id, value)); + return; } // Escape cancels. egui drops the field's focus on Escape in the same frame, so the // key is checked on the focus being lost as well as held. diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index 1db059220..22f9a3d50 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -866,6 +866,59 @@ fn effect_control_name_can_be_renamed_from_its_header() { assert_eq!(current_name(&h), "Point Control"); } +/// #3198: the effect rename field opens with the whole name selected, so typing replaces it (no +/// Ctrl/Cmd+A first), whether Enter or a double-click on the header opens it, as in After +/// Effects. Enter used to leave the field open without the keyboard, the caret after the name. +#[test] +fn effect_rename_opens_with_the_name_selected() { + let (app, x) = app(); + let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).with_step_dt(1.0 / 60.0).build_eframe(|_| app); + settle(&mut h); + let edit = format!("effectControls.effect.{}.nameEdit", x.point_fx); + let current_name = + |h: &Harness<'_, EffectcraftApp>| layer(&h.state().session, x.small).effects().unwrap().groups().find(|g| g.uid == x.point_fx).unwrap().name.clone(); + let type_and_commit = |h: &mut Harness<'_, EffectcraftApp>, text: &str| { + assert!(ids(h).contains(&edit), "the rename field is open"); + assert!(h.ctx.egui_wants_keyboard_input(), "the rename field has the keyboard"); + h.event(egui::Event::Text(text.into())); + h.step(); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(!ids(h).contains(&edit), "Enter closes the rename field"); + }; + // Enter with the effect selected. + h.state_mut().session.execute("prop.select", json!({"layer": x.small, "prop": x.point_fx, "selectKeys": false})).unwrap(); + h.run_steps(3); + h.key_press(egui::Key::Enter); + h.run_steps(3); + type_and_commit(&mut h, "Speed"); + assert_eq!(current_name(&h), "Speed", "the typed text replaced the whole name"); + // A double-click on the effect's name. + let point = rect_of(&h, &format!("effectControls.effect.{}.name", x.point_fx)).center(); + double_click(&mut h, point); + h.run_steps(3); + type_and_commit(&mut h, "Drift"); + assert_eq!(current_name(&h), "Drift", "the typed text replaced the whole name"); + // The header's context menu ▸ Rename. + let header = rect_of(&h, &format!("effectControls.effect.{}", x.point_fx)).center(); + right_click(&mut h, header); + let rename = h.query_by_label("Rename").expect("the effect menu's Rename").rect().center(); + click(&mut h, rename); + h.run_steps(3); + type_and_commit(&mut h, "Wander"); + assert_eq!(current_name(&h), "Wander", "the typed text replaced the whole name"); + // Enter and Enter again without typing: no rename, no undo step. + let undo = h.state().session.history.undo.len(); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(ids(&h).contains(&edit) && h.ctx.egui_wants_keyboard_input(), "Enter opens the rename field with the keyboard"); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(!ids(&h).contains(&edit), "Enter closes the rename field"); + assert_eq!(current_name(&h), "Wander", "the name stays"); + assert_eq!(h.state().session.history.undo.len(), undo, "no undo step"); +} + // Regression test for #1497: double-clicking an effect parameter's name in Effect Controls did // nothing; it should reveal that property in the Timeline, as After Effects does. #[test] diff --git a/crates/ui-egui/tests/it/ui_effect_names.rs b/crates/ui-egui/tests/it/ui_effect_names.rs index d57f9c581..01b57178b 100644 --- a/crates/ui-egui/tests/it/ui_effect_names.rs +++ b/crates/ui-egui/tests/it/ui_effect_names.rs @@ -9,7 +9,7 @@ use effectcraft_ui_egui::dock::PanelKind; use effectcraft_ui_egui::native_menu::{NativeNode, build as native_menu}; use egui::vec2; use egui_kittest::Harness; -use egui_kittest::kittest::Queryable; +use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::json; /// The effect under test, and the names the registry and the catalog give it. @@ -122,6 +122,37 @@ fn the_effect_context_menu_follows_the_interface_language() { } } +/// #3198: Enter opens an effect's rename field on its translated name, as a double-click does, +/// so Enter again without typing renames nothing: the effect keeps its English group name (and +/// so its translation) and there is no undo step. +#[test] +fn renaming_a_translated_effect_without_typing_keeps_its_name() { + let (mut h, fx) = harness("zh-hans"); + let name = |h: &Harness<'_, EffectcraftApp>| { + let s = &h.state().session; + let layer = s.state.selected_layers.first().copied().expect("the selected layer"); + s.active_comp().and_then(|c| c.layer(layer)).and_then(|l| l.props.find_group(fx)).map(|g| g.name.clone()) + }; + let edit = format!("effectControls.effect.{fx}.nameEdit"); + let editing = |h: &Harness<'_, EffectcraftApp>| h.state().auto.elements.iter().any(|e| e.id == edit); + let layer = h.state().session.state.selected_layers.first().map(|l| l.0).expect("the selected layer"); + h.state_mut().session.execute("prop.select", json!({"layer": layer, "prop": fx, "selectKeys": false})).unwrap(); + h.run_steps(3); + let undo = h.state().session.history.undo.len(); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(editing(&h), "Enter opens the rename field"); + assert!(h.ctx.egui_wants_keyboard_input(), "the rename field has the keyboard"); + let field = h.query_by(|n| n.is_focused()).and_then(|n| n.accesskit_node().value()); + assert_eq!(field.as_deref(), Some(BLUR_ZH), "the field opens on the drawn (Chinese) name"); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(!editing(&h), "Enter closes the rename field"); + assert_eq!(name(&h).as_deref(), Some(BLUR_EN), "the effect keeps its name"); + assert_eq!(h.state().session.history.undo.len(), undo, "no undo step"); + assert!(drawn(&h, BLUR_ZH) > 0, "the header still draws the Chinese name"); +} + /// Everything an agent or a script addresses stays English: the automation label, the property /// tree's group name and `effect.list`. #[test] diff --git a/crates/ui-egui/tests/it/ui_group_rename.rs b/crates/ui-egui/tests/it/ui_group_rename.rs index cf56ac6f4..999ed1e3e 100644 --- a/crates/ui-egui/tests/it/ui_group_rename.rs +++ b/crates/ui-egui/tests/it/ui_group_rename.rs @@ -63,7 +63,8 @@ fn rename_with_enter(h: &mut Harness<'_, EffectcraftApp>, layer: u64, uid: u64, select(h, layer, uid); key(h, Key::Enter); assert!(editing(h, uid), "Enter opens a rename field on group {uid}"); - h.key_press_modifiers(Modifiers::COMMAND, Key::A); + // The field has the keyboard with the whole name selected: typing replaces it (#3198). + assert!(h.ctx.egui_wants_keyboard_input(), "the rename field on group {uid} has the keyboard"); h.event(Event::Text(new.into())); h.step(); key(h, Key::Enter); From 82cef516176f5f9c0aa38f4a78806be0b59b4948 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:24:04 +0100 Subject: [PATCH 024/350] Frame blending averages the source frames a comp frame skips over (#3180) Footage interpreted at a much higher frame rate than the comp (for example a 60 fps clip interpreted as 600 fps in a 25 fps comp) showed no frame blending. Frame Mix only cross-faded the two source frames around the comp frame's source time, and only when that time fell between frames. With a whole-number ratio every comp frame lands exactly on a source frame, so nothing blended, and at other ratios at most two of the many skipped frames were used. When a comp frame runs through at least four source frames (conformed rate, time stretch or time remap), Frame Mix and Pixel Motion now average the source frames that comp frame spans, each weighted by how much of it the span covers; spans over 16 frames are sampled at 16 evenly spread frames. Played backwards, the span ends with the comp frame's own source frame. The speed is measured locally (over 1/16 of a comp frame) and must agree with the difference to the next comp frame, so a time remap jump (a hold followed by a cut, a loop wrapping back) stays crisp instead of smearing across the jump. Reduced-resolution previews mix frames at the reduced size. Below four source frames per comp frame (slow-downs, equal rates, 60p in a 30, 25 or 24 fps comp, 50p in 25) nothing changes. The threshold and the 16-frame cap are named constants, pending a comparison with After Effects. The layer cache key, and the key of a layer that selects a frame-blended footage layer as an effect input, now include how far the source runs during the comp frame, which the mix depends on. Fixes #3180 --- crates/render/src/cache.rs | 10 ++ crates/render/src/cache/dependencies.rs | 5 +- crates/render/src/frames.rs | 78 +++++++++++ crates/render/src/lib.rs | 2 +- crates/render/src/pipeline.rs | 91 ++++++++++-- crates/render/src/tests/frame_blend.rs | 175 +++++++++++++++++++++++- 6 files changed, 341 insertions(+), 20 deletions(-) diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index cf1a84e82..b6c38f80d 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -477,6 +477,15 @@ fn group_has(g: &PropGroup, match_id: &str) -> bool { }) } +/// A frame-blended layer's pixels also depend on how far its source runs during the comp frame: +/// a fast-conformed clip mixes every source frame the comp frame spans (#3180), so a time +/// stretch or remap change can alter the pixels at an unchanged source time. +fn hash_blend_spans(h: &mut KeyHasher, ctx: &EvalCtx, layer: &Layer) { + if crate::frames::frame_blend_mode(ctx, layer) != effectcraft_project::FrameBlend::Off { + crate::frames::blend_spans(ctx, layer).hash(h); + } +} + /// Feed every property of `g` that can change over time (keyframes or expression) as its value /// at the context time. The static structure is hashed separately. /// Mix the collapsed-instance overrides that target `id` into a cache key. They replace values @@ -646,6 +655,7 @@ fn key_any(ctx: &EvalCtx, layer: &Layer, scale: f64, draft: bool, blur: bool, fo } // Frame blending mixes neighbouring source frames. ctx.comp.enable_frame_blending.hash(&mut h); + hash_blend_spans(&mut h, ctx, layer); } _ => return None, } diff --git a/crates/render/src/cache/dependencies.rs b/crates/render/src/cache/dependencies.rs index db1465c07..c8c483f99 100644 --- a/crates/render/src/cache/dependencies.rs +++ b/crates/render/src/cache/dependencies.rs @@ -165,7 +165,10 @@ impl Dependencies<'_> { hash_debug(self.h, &source); if let Some(source) = source { match &source.kind { - ItemKind::Footage(_) => ctx.source_time(layer).0.hash(self.h), + ItemKind::Footage(_) => { + ctx.source_time(layer).0.hash(self.h); + super::hash_blend_spans(self.h, ctx, layer); + } ItemKind::Comp(comp) => { let time = ctx.source_time(layer); time.0.hash(self.h); diff --git a/crates/render/src/frames.rs b/crates/render/src/frames.rs index 20c655694..7d072438c 100644 --- a/crates/render/src/frames.rs +++ b/crates/render/src/frames.rs @@ -89,6 +89,84 @@ pub fn frame_position(t: Tick, rate: FrameRate) -> (i64, f64) { (num.div_euclid(den) as i64, num.rem_euclid(den) as f64 / den as f64) } +/// The fewest source frames one comp frame must run through before frame blending averages all +/// of them ([`mix_window`]); below this the two neighbouring frames blend as they always have. +/// Conservative on purpose: common footage (60p in a 30, 25 or 24 fps comp, 50p in 25) keeps +/// its familiar look, while fast-conformed footage (600 fps in a 25 fps comp, #3180) gets the +/// mix. Pending a comparison with After Effects, whose exact threshold is not documented. +pub const MIN_MIX_FRAMES: f64 = 4.0; + +/// The most source frames one comp frame mixes; a wider span is sampled at this many evenly +/// spaced frames (After Effects is reported to blend about a dozen; this also bounds the +/// decoding per comp frame). Pending a comparison with After Effects. +pub const MAX_MIX_FRAMES: usize = 16; + +/// A span of more source frames than this in one comp frame is a jump (a time remap cut), not +/// motion to blur. +pub const MAX_MIX_SPAN: f64 = 1000.0; + +/// How finely [`blend_spans`] probes the source's speed: at 1/16 of a comp frame. +const SPEED_PROBE: i64 = 16; + +/// How far a frame-blended layer's source runs during one comp frame from the context time +/// (Interpret Footage's conformed rate, time stretch and time remap all show up in it), as +/// `(local, whole)`: the local speed just after the comp frame's time scaled to a whole frame, +/// and the actual difference to the next comp frame. They disagree across a time remap jump +/// (a hold keyframe followed by a cut, a `loopOut` wrapping back), which then does not mix. +/// Measured on the shown source time ([`EvalCtx::nested_time`]: a precomp proxy that preserves +/// its frame rate is floored to its frames). +pub(crate) fn blend_spans(ctx: &EvalCtx, layer: &Layer) -> (Tick, Tick) { + let frame = ctx.comp.frame_duration(); + let t0 = ctx.nested_time(layer); + let probe = Tick(frame.0 / SPEED_PROBE); + let local = ctx.at(ctx.time + probe).nested_time(layer) - t0; + let whole = ctx.at(ctx.time + frame).nested_time(layer) - t0; + (Tick(local.0.saturating_mul(SPEED_PROBE)), whole) +} + +/// The source frames a frame-blended comp frame averages when the layer runs through many +/// source frames per comp frame (footage interpreted at a much higher frame rate than the comp, +/// a sped-up time stretch or time remap), so a comp frame skips whole source frames: the frames +/// at `rate` that one comp frame's worth of source from `t` overlaps, each weighted by the share +/// of a frame it covers. `spans` is [`blend_spans`]' `(local, whole)`; playing forwards the span +/// is `[t, t + span)`, backwards it ends with the frame at `t` (the unblended frame). `None` — +/// the two neighbouring frames then blend ([`frame_position`], [`blend_frames`]) — when the span +/// is shorter than [`MIN_MIX_FRAMES`], longer than [`MAX_MIX_SPAN`], or the local speed and the +/// whole frame's difference disagree by more than a source frame (a time remap jump). +pub fn mix_window(t: Tick, spans: (Tick, Tick), rate: FrameRate) -> Option> { + if rate.num <= 0 || rate.den <= 0 { + return None; + } + let frames_of = |s: Tick| s.0 as f64 * rate.num as f64 / (TICKS_PER_SECOND as f64 * rate.den as f64); + let width = frames_of(spans.0); + let whole = frames_of(spans.1); + if !width.is_finite() || !whole.is_finite() || (width - whole).abs() > 1.0 + 1e-9 { + return None; + } + if width.abs() < MIN_MIX_FRAMES - 1e-9 || width.abs() > MAX_MIX_SPAN { + return None; + } + let (i, w) = frame_position(t, rate); + let p = i as f64 + w; + let lo = if width < 0.0 { p + 1.0 + width } else { p }; + let width = width.abs(); + let hi = lo + width; + let first = lo.floor(); + let n = hi.ceil() - first; + if n <= MAX_MIX_FRAMES as f64 { + let frames = (0..n as usize) + .filter_map(|k| { + let j = first + k as f64; + let cover = (j + 1.0).min(hi) - j.max(lo); + (cover > 1e-9).then_some((j as i64, cover)) + }) + .collect(); + return Some(frames); + } + let m = MAX_MIX_FRAMES as f64; + Some((0..MAX_MIX_FRAMES).map(|k| ((lo + width * (k as f64 + 0.5) / m).floor() as i64, 1.0)).collect()) +} + /// Blend two neighbouring frames `w` of the way from `a` to `b`: a cross-fade (Frame Mix) or a /// motion-compensated interpolation (Pixel Motion). pub fn blend_frames(mode: FrameBlend, a: &Image, b: &Image, w: f32) -> Image { diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index dbd948320..0a8476344 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -49,7 +49,7 @@ pub use accel::{Accelerator, Backend, FxStep, FxTarget}; pub use footage::{CancellableFootage, FloatCurve, FootageSource, NoFootage, is_vector_footage, missing_item_problem}; pub use frames::{ adaptive_samples, blend_frames, cineon_frame, content_bounds, extended_region, field_frame, frame_position, frame_size, interpret_pixels, kurbo_path, - render_frame, roi_inside, + mix_window, render_frame, roi_inside, }; #[cfg(test)] use fx_host::FxHost; diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index e84348fdf..6012a7404 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -50,6 +50,23 @@ pub struct Collapsed { pub(crate) media_overrides: Vec, } +/// Cache key of a frame-blended footage frame: `seed` (which frames, how they mix, the footage +/// file) and the Interpret Footage settings that change the decoded pixels. +fn blended_frame_key(f: &Footage, seed: impl std::hash::Hash) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + seed.hash(&mut h); + (f.alpha as u8).hash(&mut h); + if f.alpha == effectcraft_project::AlphaMode::Premultiplied { + f.premul_color.map(f32::to_bits).hash(&mut h); + } + if f.cineon_applies() { + let c = &f.cineon; + [c.converted_black, c.converted_white, c.black, c.white, c.gamma, c.rolloff].map(f32::to_bits).hash(&mut h); + } + h.finish() +} + /// The property and media overrides of a collapsed precomp layer. `None` when its Essential /// Properties group or the source comp's definitions are missing (the flattened path then /// preserves the old behaviour). @@ -848,23 +865,16 @@ impl<'a> Renderer<'a> { let mode = frame_blend_mode(ctx, layer); let float = self.pipe.float_curve(f); if mode != FrameBlend::Off && matches!(f.kind, FootageKind::Video | FootageKind::Sequence) { + // Many source frames per comp frame (footage interpreted much faster than the comp, + // #3180): the frames the comp frame spans are averaged. + if let Some(img) = self.mixed_span_frame(ctx, layer, item, f, t, size) { + return Some(img); + } let (i, w) = frame_position(t, f.frame_rate); if w > 1e-6 { // Blended frames are cached (Pixel Motion is expensive). - let key = self.cache.map(|_| { - use std::hash::{Hash, Hasher}; - let mut h = std::collections::hash_map::DefaultHasher::new(); - (0x00f8_a3e5_u64, item, i, (w as f32).to_bits(), mode as u8, f.frame_rate.num, f.frame_rate.den, &f.path, float).hash(&mut h); - (f.alpha as u8).hash(&mut h); - if f.alpha == effectcraft_project::AlphaMode::Premultiplied { - f.premul_color.map(f32::to_bits).hash(&mut h); - } - if f.cineon_applies() { - let c = &f.cineon; - [c.converted_black, c.converted_white, c.black, c.white, c.gamma, c.rolloff].map(f32::to_bits).hash(&mut h); - } - h.finish() - }); + let seed = (0x00f8_a3e5_u64, item, i, (w as f32).to_bits(), mode as u8, f.frame_rate.num, f.frame_rate.den, &f.path, float); + let key = self.cache.map(|_| blended_frame_key(f, seed)); if let (Some(c), Some(k)) = (self.cache, key) && let Some(b) = c.get(k) { @@ -885,6 +895,59 @@ impl<'a> Renderer<'a> { self.footage.frame_with(item, f, t, size, float).map(|img| cineon_frame(img, f)) } + /// A frame-blended footage frame at source time `t` when one comp frame runs through many + /// source frames: the average of the source frames the comp frame spans, each weighted by + /// the share of it the span covers ([`super::frames::mix_window`]). Both Frame Mix and + /// Pixel Motion average real frames here (there is no in-between frame to synthesise). + /// `size`: the frames may come resampled to it ([`FootageSource::frame_at_size`]). `None` + /// when the span is too short (or a jump) to mix, or no frame decodes. + fn mixed_span_frame(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option> { + use rayon::prelude::*; + let float = self.pipe.float_curve(f); + let spans = super::frames::blend_spans(ctx, layer); + let frames = super::frames::mix_window(t, spans, f.frame_rate)?; + let seed = (0x00f8_a3e6_u64, item, t.0, spans, size, f.frame_rate.num, f.frame_rate.den, &f.path, float); + let key = self.cache.map(|_| blended_frame_key(f, seed)); + if let (Some(c), Some(k)) = (self.cache, key) + && let Some(b) = c.get(k) + { + return Some(Arc::new(b.img.clone())); + } + // A weighted sum in one buffer, divided by the total weight at the end. A frame that does + // not decode, or has another size than the first (a sequence of mixed sizes), is left + // out and the mix renormalises over the rest. Log scans are converted before they mix. + let mut acc: Option = None; + let mut total = 0.0; + for (j, w) in frames { + let Some(img) = self.footage.frame_with(item, f, f.frame_rate.tick_of(j), size, float).map(|a| cineon_frame(a, f)) else { continue }; + let sum = acc.get_or_insert_with(|| Image::new(img.width, img.height)); + if (sum.width, sum.height) != (img.width, img.height) { + continue; + } + let wf = w as f32; + sum.data.par_iter_mut().zip(img.data.par_iter()).for_each(|(s, p)| { + for (a, b) in s.iter_mut().zip(p) { + *a += b * wf; + } + }); + total += w; + } + let mut img = acc?; + if total <= 1e-9 { + return None; + } + let norm = (1.0 / total) as f32; + img.data.par_iter_mut().for_each(|s| { + for c in s.iter_mut() { + *c *= norm; + } + }); + if let (Some(c), Some(k)) = (self.cache_to_keep(), key) { + c.insert(k, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 })); + } + Some(Arc::new(img)) + } + /// Source → masks, clamped/quantised to the project depth. fn masked_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option { let mut buf = match self.content_times(ctx, layer) { diff --git a/crates/render/src/tests/frame_blend.rs b/crates/render/src/tests/frame_blend.rs index ac3555af1..e80731d35 100644 --- a/crates/render/src/tests/frame_blend.rs +++ b/crates/render/src/tests/frame_blend.rs @@ -5,12 +5,13 @@ use std::sync::Arc; use rayon::prelude::*; use effectcraft_color::Label; -use effectcraft_keyframe::{Keyframe, Value}; +use effectcraft_keyframe::{Interp, Keyframe, Value}; use effectcraft_project::build; use effectcraft_project::{AlphaMode, BitDepth, Comp, Footage, FootageKind, FrameBlend, ItemId, ItemKind, LayerSource, Project, Solid}; use effectcraft_time::{FrameRate, Tick}; -use crate::{FootageSource, Image, RenderOpts, Renderer, frame_position}; +use crate::frames::MAX_MIX_FRAMES; +use crate::{FootageSource, Image, RenderOpts, Renderer, frame_position, mix_window}; #[test] fn frame_mix_weights_for_24_to_30_conform() { @@ -58,6 +59,11 @@ impl FootageSource for Frames { } fn setup(mode: FrameBlend, size: (u32, u32)) -> (Project, ItemId) { + setup_rated(mode, size, FrameRate::FPS_24, None) +} + +/// [`setup`] with footage at `rate` (its Interpret Footage frame rate) recorded at `native`. +fn setup_rated(mode: FrameBlend, size: (u32, u32), rate: FrameRate, native: Option) -> (Project, ItemId) { let mut p = Project::default(); p.settings.bit_depth = BitDepth::Bpc32; let comp = Comp::new(size.0, size.1, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); @@ -68,8 +74,8 @@ fn setup(mode: FrameBlend, size: (u32, u32)) -> (Project, ItemId) { width: size.0, height: size.1, pixel_aspect: 1.0, - frame_rate: FrameRate::FPS_24, - native_rate: None, + frame_rate: rate, + native_rate: native, duration: Tick::from_seconds_f64(2.0), has_video: true, has_audio: false, @@ -168,6 +174,167 @@ fn time_stretched_precomp_frame_mixes() { assert!(img.get(12, 10)[3] < 1e-3 && (img.get(17, 10)[3] - 1.0).abs() < 1e-3 && (img.get(22, 10)[3] - 1.0).abs() < 1e-3 && img.get(27, 10)[3] < 1e-3); } +#[test] +fn frame_mix_averages_every_frame_of_fast_conformed_footage() { + // #3180: 30 fps footage interpreted as 300 fps in a 30 fps comp. Every comp frame lands + // exactly on a source frame, but it spans ten of them: frame blending averages all ten + // (greys 0.0 … 0.9 → 0.45), not just the frame it starts on (0.0). + let fast = FrameRate::new(300, 1); + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 0.45).abs() < 1e-5, "{v}"); + // Pixel Motion has no in-between frame to make either: it averages the same frames. + let (p, cid) = setup_rated(FrameBlend::PixelMotion, (16, 8), fast, Some(FrameRate::FPS_30)); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 0.45).abs() < 1e-5, "{v}"); + // Off (layer switch or comp switch): the one frame at the comp frame's time. + let (p, cid) = setup_rated(FrameBlend::Off, (16, 8), fast, Some(FrameRate::FPS_30)); + assert!(frame(&p, cid, 0, false).get(4, 4)[0].abs() < 1e-6); + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + p.comp_mut(cid).unwrap().enable_frame_blending = false; + assert!(frame(&p, cid, 0, false).get(4, 4)[0].abs() < 1e-6); +} + +#[test] +fn frame_mix_of_common_footage_is_unchanged() { + // Footage at the comp's rate: each comp frame is exactly one source frame, blended or not. + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), FrameRate::FPS_30, None); + for k in 0..4 { + let v = frame(&p, cid, k, false).get(4, 4)[0]; + assert!((v - k as f32 / 10.0).abs() < 1e-6, "comp frame {k}: {v}"); + } + // A mild speed-up (36 fps footage in a 30 fps comp) keeps the two-frame cross-fade: comp + // frame 1 → source frame 1.2, 0.8·frame 1 + 0.2·frame 2 = 0.12. + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), FrameRate::new(36, 1), None); + let v = frame(&p, cid, 1, false).get(4, 4)[0]; + assert!((v - 0.12).abs() < 1e-6, "{v}"); + // 60 and 90 fps in a 30 fps comp (two and three source frames per comp frame) stay crisp: + // comp frame 1 is source frame 2 or 3, as before. + for (rate, want) in [(FrameRate::FPS_60, 0.2), (FrameRate::new(90, 1), 0.3)] { + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), rate, None); + let v = frame(&p, cid, 1, false).get(4, 4)[0]; + assert!((v - want).abs() < 1e-6, "{rate:?}: {v}"); + } +} + +/// Adds Time Remap with `keys` (layer time → source seconds) to the comp's footage layer. +fn remap(p: &mut Project, cid: ItemId, keys: Vec) { + let mut next = p.next_id; + let mut tr = build::Ids(&mut next).prop("timeRemap", "Time Remap", Value::Scalar(0.0)); + p.next_id = next; + tr.keys = keys; + p.comp_mut(cid).unwrap().layers[0].props.children.push(effectcraft_project::Node::Prop(tr)); +} + +#[test] +fn time_remap_jumps_stay_crisp() { + let fast = FrameRate::new(300, 1); + let s = |frames: f64| Value::Scalar(frames / 300.0); + let at = |k: i64| FrameRate::FPS_30.tick_of(k); + // Hold on source frame 0, then cut to source frame 10 at comp frame 1: comp frame 0 is a + // still frame, not a smear of the ten frames the cut skips. + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + remap(&mut p, cid, vec![Keyframe::new(at(0), s(0.0)).hold(), Keyframe::new(at(1), s(10.0)).hold()]); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!(v.abs() < 1e-6, "hold then cut: {v}"); + // A loop wrapping back (like `loopOut("cycle")`): from source frame 10 at normal speed, + // back to frame 0 by the next comp frame. Comp frame 0 shows frame 10 (grey 1.0), not + // frames 1 … 10 run backwards. + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + let mut top = Keyframe::new(FrameRate::FPS_60.tick_of(1), s(15.0)); + top.out_interp = Interp::Hold; + remap(&mut p, cid, vec![Keyframe::new(at(0), s(10.0)), top, Keyframe::new(at(1), s(0.0))]); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 1.0).abs() < 1e-6, "loop wrap: {v}"); + // A remap running at normal speed through fast-conformed footage still mixes its frames. + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + remap(&mut p, cid, vec![Keyframe::new(at(0), s(0.0)), Keyframe::new(at(30), s(300.0))]); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 0.45).abs() < 1e-5, "steady remap: {v}"); +} + +#[test] +fn a_frame_blended_dependency_keys_how_far_its_source_runs() { + // A footage layer selected by another layer's effect: at comp frames 0 and 1 its source time + // is the same (0), but at frame 0 it holds and at frame 1 it runs at speed, which mixes ten + // source frames. The owner's cache key must tell the two apart. + let fast = FrameRate::new(300, 1); + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + let at = |k: i64| FrameRate::FPS_30.tick_of(k); + let keys = vec![Keyframe::new(at(0), Value::Scalar(0.0)).hold(), Keyframe::new(at(1), Value::Scalar(0.0)), Keyframe::new(at(31), Value::Scalar(1.0))]; + remap(&mut p, cid, keys); + let comp = p.comp(cid).unwrap().clone(); + let footage = comp.layers[0].id; + let sid = p.add_item("Solid", Label::Red, None, ItemKind::Solid(Solid { color: [1.0; 3], width: 16, height: 8, pixel_aspect: 1.0 })); + let mut owner = build::layer(&mut p, &comp, "Solid", LayerSource::Solid { item: sid }, (16, 8), None); + super::add_effect(&mut p, &mut owner, "ec.channel.setmatte", [16.0, 8.0], &[("takeMatteFromLayer", Value::Layer(Some(footage.0)))]); + p.comp_mut(cid).unwrap().layers.push(owner); + let key = |k: i64| { + let c = p.comp(cid).unwrap(); + crate::cache::layer_key(&crate::EvalCtx::new(&p, cid, c, at(k)), &c.layers[1], 1.0, false, false).unwrap() + }; + assert_ne!(key(0), key(1)); +} + +#[test] +fn mix_window_covers_the_source_span_of_one_comp_frame() { + let comp_frame = |r: FrameRate| r.frame_duration(); + let steady = |r: FrameRate| (comp_frame(r), comp_frame(r)); + // Fewer than four source frames per comp frame (slow-down, same rate, 60p in 30, 24 or 25, + // 50p in 25, 90 in 30): the two-frame blend applies, as before. + for (comp, footage) in [ + (FrameRate::FPS_30, FrameRate::FPS_30), + (FrameRate::FPS_30, FrameRate::FPS_24), + (FrameRate::FPS_30, FrameRate::new(36, 1)), + (FrameRate::FPS_25, FrameRate::FPS_30), + (FrameRate::FPS_30, FrameRate::FPS_60), + (FrameRate::FPS_24, FrameRate::FPS_60), + (FrameRate::FPS_25, FrameRate::FPS_60), + (FrameRate::FPS_25, FrameRate::FPS_50), + (FrameRate::FPS_29_97, FrameRate::FPS_59_94), + (FrameRate::FPS_30, FrameRate::new(90, 1)), + ] { + assert_eq!(mix_window(Tick::ZERO, steady(comp), footage), None, "{footage:?} in {comp:?}"); + } + // Four source frames per comp frame: 120 fps in a 30 fps comp mixes all four. + assert_eq!(mix_window(Tick::ZERO, steady(FrameRate::FPS_30), FrameRate::FPS_120), Some(vec![(0, 1.0), (1, 1.0), (2, 1.0), (3, 1.0)])); + // The report's case: 600 fps in a 25 fps comp, 24 source frames per comp frame, sampled at + // 16 evenly spread frames. + let w = mix_window(FrameRate::FPS_25.tick_of(3), steady(FrameRate::FPS_25), FrameRate::new(600, 1)).unwrap(); + assert_eq!(w.len(), MAX_MIX_FRAMES); + assert!(w.iter().all(|x| (72..96).contains(&x.0) && x.1 == 1.0), "{w:?}"); + assert!(w.windows(2).all(|p| p[0].0 < p[1].0), "{w:?}"); + // 150 fps in a 30 fps comp: comp frame 1 spans source frames 5 … 10. + let w = mix_window(FrameRate::FPS_30.tick_of(1), steady(FrameRate::FPS_30), FrameRate::new(150, 1)); + assert_eq!(w, Some((5..10).map(|j| (j, 1.0)).collect())); + // 135 fps in a 30 fps comp: comp frame 1 spans source frames 4.5 … 9.0. + let w = mix_window(FrameRate::FPS_30.tick_of(1), steady(FrameRate::FPS_30), FrameRate::new(135, 1)).unwrap(); + assert_eq!(w.iter().map(|x| x.0).collect::>(), vec![4, 5, 6, 7, 8]); + for (got, want) in w.iter().map(|x| x.1).zip([0.5, 1.0, 1.0, 1.0, 1.0]) { + assert!((got - want).abs() < 1e-9, "{w:?}"); + } + // Playing backwards: the span ends with the comp frame's own source frame. + let back = -comp_frame(FrameRate::FPS_30); + let w = mix_window(FrameRate::FPS_30.tick_of(1), (back, back), FrameRate::new(300, 1)).unwrap(); + assert_eq!(w.iter().map(|x| x.0).collect::>(), (1..=10).collect::>()); + // A hold (no local speed) or a jump (local speed and the whole frame disagree) does not mix. + let f = comp_frame(FrameRate::FPS_30); + assert_eq!(mix_window(Tick::ZERO, (Tick::ZERO, f), FrameRate::new(300, 1)), None); + assert_eq!(mix_window(Tick::ZERO, (f, -f), FrameRate::new(300, 1)), None); + // A long span is sampled at a bounded number of frames; a span beyond any footage's speed is + // a jump. + let ten = Tick::from_seconds_f64(10.0); + let w = mix_window(Tick::ZERO, (ten, ten), FrameRate::FPS_60).unwrap(); + assert_eq!(w.len(), MAX_MIX_FRAMES); + assert!(w.iter().all(|x| (0..600).contains(&x.0))); + let far = Tick::from_seconds_f64(100.0); + assert_eq!(mix_window(Tick::ZERO, (far, far), FrameRate::FPS_60), None); + // Degenerate rates and huge spans don't panic. + assert_eq!(mix_window(Tick::ZERO, (Tick::MAX, Tick::MAX), FrameRate { num: 0, den: 1 }), None); + assert_eq!(mix_window(Tick::MIN, (Tick::MAX, Tick::MAX), FrameRate::FPS_60), None); + assert_eq!(mix_window(Tick::MAX, (Tick::MIN, Tick::MAX), FrameRate::FPS_60), None); +} + #[test] fn frame_position_survives_degenerate_rates() { for rate in [FrameRate { num: 24, den: 0 }, FrameRate { num: 0, den: 1 }, FrameRate { num: -24, den: 1 }] { From e30f4f64358553eddf2c56051ed7de281ca6d7d7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:56:59 +0100 Subject: [PATCH 025/350] Vector footage: faster, budget-safe clipping in the SVG/PDF rasteriser Continuously rasterised vector footage (an A4 PDF scaled 200-400 %) is a page of millions of pixels, and the rasteriser did page-sized work per element: - Every path, run of text and image computed and walked a coverage mask the size of the whole page. Coverage and paint now run over the pixels the shape (fill, or stroke outline) or image can reach. A generated A4 page of 720 text-run paths and 60 strokes rasterises at 300 % in 0.18 s instead of 4.97 s (200 %: 0.13 s instead of 2.44 s), with the same pixels. - Every clipping group copied the whole backdrop and drew on a second page-size surface, so deeply nested clips ran past the 512 MB surface budget and their artwork was dropped (at 200 % from the eighth level, at 400 % from the second). A plain clipping group (no soft mask, blend mode, group opacity or knockout, Normal content throughout) is now drawn straight onto the page when every clip is an axis-aligned rectangle covering each pixel the content can reach, and otherwise on a surface the size of its clip. Both give the same pixels; groups with a blend mode or knockout inside keep the backdrop path. Never crash: drawing stops below 256 levels of groups, and the PDF/EPS builder keeps groups at most 200 deep (a crafted page nesting `q ... W n` twenty thousand deep overflowed the stack when the document was drawn or dropped). The 596 x 842 px import size of an A4 page is unchanged: the page in points at 72 dpi, rounded up, as After Effects takes a point as a pixel. Refs #2895 --- crates/host/tests/it/vector_import.rs | 75 +++++++ crates/pdf/src/build.rs | 22 +- crates/pdf/src/tests.rs | 29 +++ crates/svg/src/raster.rs | 290 ++++++++++++++++++++++++-- crates/svg/src/tests.rs | 190 +++++++++++++++++ 5 files changed, 583 insertions(+), 23 deletions(-) diff --git a/crates/host/tests/it/vector_import.rs b/crates/host/tests/it/vector_import.rs index badd60b66..c54c61550 100644 --- a/crates/host/tests/it/vector_import.rs +++ b/crates/host/tests/it/vector_import.rs @@ -596,3 +596,78 @@ fn ai_composition_retains_layer_sizes_and_positions() { assert!(frame.get(39, 10)[0] > 0.99, "reload invalidates cached pixels"); } } + +/// An A4 page (595.28 × 841.89 pt) whose artwork sits inside a page clip and ten nested +/// clipping groups, as PDFs exported from layout and office applications nest them: a black bar +/// whose left edge is at x = 30 pt, from 41.89 to 191.89 pt down the page. +fn a4_clipped_pdf() -> Vec { + let mut content = String::from( + "q 0 0 595.28 841.89 re W n +", + ); + for i in 0..10 { + content.push_str(&format!( + "q {} {} {} {} re W n +", + 20 + i, + 600 + i, + 300 - 2 * i, + 230 - 2 * i + )); + } + content.push_str( + "0 0 0 rg 30 650 40 150 re f +", + ); + content.push_str( + &"Q +" + .repeat(11), + ); + effectcraft_pdf::write::pdf( + &[ + (1, "<< /Type /Catalog /Pages 2 0 R >>".into(), None), + (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".into(), None), + (3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595.28 841.89] /Resources << >> /Contents 4 0 R >>".into(), None), + (4, "<< >>".into(), Some(content.into_bytes())), + ], + 1, + ) +} + +/// #2895: an A4 PDF imports at its size in points, rounded up (596 × 842 px; After Effects +/// also takes a point as a pixel), and scaled 200 % with Continuously Rasterize on it is drawn +/// at that scale: the bar's left edge stays one pixel wide. It used to come out empty: at +/// 200 % (a 32 MB page) each nested clipping group copied the whole page and drew on a second +/// page-size surface, so the eighth level of clips ran past the rasteriser's 512 MB surface +/// budget. With the switch off the layer is the page's pixels enlarged, soft as before. +#[test] +fn a4_pdf_scaled_200_percent_continuously_rasterizes_sharp() { + let path = tmp("a4-clipped.pdf"); + std::fs::write(&path, a4_clipped_pdf()).unwrap(); + let mut s = effectcraft_host::session(); + let r = s.execute_checked("file.import", json!({"paths": [path]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let item = r["items"][0].as_u64().unwrap(); + let effectcraft_project::ItemKind::Footage(f) = &s.project.item(ItemId(item)).unwrap().kind else { panic!("footage") }; + assert_eq!((f.codec.as_str(), f.width, f.height), ("PDF", 596, 842), "A4 in points"); + s.execute("comp.new", json!({"name": "A4", "width": 400, "height": 400, "frameRate": 30, "duration": 1})).unwrap(); + let lid = s.execute_checked("layer.addItem", json!({"item": item})).unwrap()["layer"].as_u64().unwrap(); + for (k, v) in [("transform/anchor", json!([0, 0])), ("transform/position", json!([0, 0])), ("transform/scale", json!([200, 200]))] { + s.execute("prop.set", json!({"layer": lid, "path": k, "value": v})).unwrap(); + } + let cid = s.active_comp_id().unwrap(); + let t = effectcraft_time::Tick::ZERO; + // Row 240 (120 pt down the page) crosses the bar's left edge at x = 60 (30 pt × 2). + let soft_pixels = |img: &Image| (50..70).filter(|&x| (0.05..0.95).contains(&img.get(x, 240)[3])).count(); + let off = s.render(cid, t, RenderOpts::default()); + assert!(off.get(100, 240)[3] > 0.99, "the bar shows with the switch off"); + assert!(soft_pixels(&off) >= 2, "off: the page's pixels enlarged, a soft edge ({} partial pixels)", soft_pixels(&off)); + s.execute("layer.setSwitch", json!({"layers": [lid], "switch": "collapse", "value": true})).unwrap(); + let on = s.render(cid, t, RenderOpts::default()); + assert!(on.get(100, 240)[3] > 0.99, "the clipped bar is drawn at 200 %: {:?}", on.get(100, 240)); + assert!(on.get(61, 240)[3] > 0.99 && on.get(58, 240)[3] < 0.01, "a hard edge at x = 60: {:?} {:?}", on.get(58, 240), on.get(61, 240)); + assert!(soft_pixels(&on) <= 1, "on: the edge is at most one pixel wide ({} partial pixels)", soft_pixels(&on)); + // Outside the clips (x = 10 pt) nothing is drawn. + assert!(on.get(20, 240)[3] < 0.01); +} diff --git a/crates/pdf/src/build.rs b/crates/pdf/src/build.rs index 0b1586f89..497d567a6 100644 --- a/crates/pdf/src/build.rs +++ b/crates/pdf/src/build.rs @@ -7,6 +7,10 @@ use effectcraft_svg::{Affine, BezPath, FillRule, Geom, Group, Node, Paint, Shape /// PostScript program, or the PDF content stream. pub(crate) const MAX_PATH: usize = 1 << 20; +/// The deepest groups (clips, layers, transparency groups) nest in the document; below the +/// renderer's own limit (256), so everything kept is drawn. +pub(crate) const MAX_GROUP_DEPTH: usize = 200; + #[derive(Clone, Copy, PartialEq, Eq)] enum Kind { Plain, @@ -68,17 +72,29 @@ impl Builder { g.clip.push((path, rule)); // Clipping is not a transparency group: blend modes inside reach the backdrop. g.isolated = false; - self.stack.push((g, Kind::Clip)); + self.push_group(g, Kind::Clip); } /// Open an optional-content layer (or a named group). pub fn open(&mut self, name: &str, layer: bool) { - self.stack.push((group(name), if layer { Kind::Layer } else { Kind::Plain })); + self.push_group(group(name), if layer { Kind::Layer } else { Kind::Plain }); } /// Open a group (blend mode, soft mask) closed with the graphics state. pub fn open_group(&mut self, g: Group) { - self.stack.push((g, Kind::Plain)); + self.push_group(g, Kind::Plain); + } + + /// Open a group, unless groups are already nested [`MAX_GROUP_DEPTH`] deep: the document + /// is a tree that is drawn, measured, copied and dropped recursively, and a crafted file + /// can nest `q … W n` without end. Deeper groups are left out (their content is drawn + /// in the group around them) and the document says so. + fn push_group(&mut self, g: Group, kind: Kind) { + if self.stack.len() >= MAX_GROUP_DEPTH { + self.skip("groups nested deeper than 200 levels"); + return; + } + self.stack.push((g, kind)); } /// Add a finished node (an image, pattern tiles). diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index 2d09a7406..275e7021f 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -1139,3 +1139,32 @@ fn cmap_entries_at_the_top_of_the_code_range_do_not_overflow() { // Code 0 is CID u32::MAX; code 2 would be past it, so it falls back to CID 0 like an unmapped code. assert_eq!((f.width(0), f.width(2)), (0.5, 0.7)); } + +/// Never crash: a crafted page nests `q … W n` twenty thousand deep. The document is a tree +/// that is drawn, measured and dropped recursively, so groups past the nesting limit are left +/// out (said in the skip notes) and the page parses, draws and drops on a thread with a small +/// stack; the artwork inside them still draws, clipped by the groups that were kept. +#[test] +fn clip_groups_nested_without_end_are_capped() { + let run = std::thread::Builder::new().stack_size(2 << 20).spawn(|| { + let n = 20_000; + let mut content = "q 0 0 50 50 re W n\n".repeat(n); + content.push_str("0 0 1 rg 0 0 50 50 re f\n"); + content.push_str(&"Q\n".repeat(n)); + let bytes = pdf( + &[ + (1, "<< /Type /Catalog /Pages 2 0 R >>".to_string(), None), + (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(), None), + (3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Contents 4 0 R >>".to_string(), None), + (4, "<< >>".to_string(), Some(content.into_bytes())), + ], + 1, + ); + let doc = parse(&bytes).unwrap(); + (doc.skipped.clone(), px(&doc, 25, 75), px(&doc, 75, 25)) + }); + let (skipped, inside, outside) = run.unwrap().join().unwrap(); + assert!(skipped.iter().any(|s| s.contains("nested deeper")), "{skipped:?}"); + assert!(inside[2] > 0.99 && inside[3] > 0.99, "the artwork draws inside the clips: {inside:?}"); + assert!(outside[3] < 0.01, "and is clipped: {outside:?}"); +} diff --git a/crates/svg/src/raster.rs b/crates/svg/src/raster.rs index 568b10b25..59deae964 100644 --- a/crates/svg/src/raster.rs +++ b/crates/svg/src/raster.rs @@ -20,7 +20,13 @@ pub fn rasterize(doc: &Doc, w: u32, h: u32, scale: f64) -> Image { /// Rasterise `doc` with `m` mapping document pixels to image pixels. pub fn rasterize_with(doc: &Doc, w: u32, h: u32, m: Affine) -> Image { - let mut budget = SurfaceBudget { live_bytes: 0, limit_bytes: MAX_LIVE_SURFACE_BYTES }; + rasterize_with_budget(doc, w, h, m, MAX_LIVE_SURFACE_BYTES) +} + +/// [`rasterize_with`] with at most `limit_bytes` of image surfaces (the destination included) +/// live at once. +pub(crate) fn rasterize_with_budget(doc: &Doc, w: u32, h: u32, m: Affine, limit_bytes: usize) -> Image { + let mut budget = SurfaceBudget { live_bytes: 0, limit_bytes, depth: 0, plain: false }; let Some(mut img) = budget.image(w, h, None) else { return Image::default() }; draw_group(&doc.root, m, 1.0, &mut img, &mut budget); img @@ -29,9 +35,19 @@ pub fn rasterize_with(doc: &Doc, w: u32, h: u32, m: Affine) -> Image { /// Bound the simultaneously live offscreen image surfaces created by nested groups. const MAX_LIVE_SURFACE_BYTES: usize = 512 * 1024 * 1024; +/// The deepest group nesting drawn: drawing recurses, and a crafted document can nest groups +/// without end, so groups deeper than this are not drawn. +const MAX_GROUP_DEPTH: usize = 256; + +/// The state of one rasterisation: the surface budget and where the drawing is in the tree. struct SurfaceBudget { live_bytes: usize, limit_bytes: usize, + /// Groups entered on the way to the one being drawn. + depth: usize, + /// Every group being drawn below the current one composites Normal, without knockout + /// (known once for a whole subtree, so it isn't walked again at every level). + plain: bool, } impl SurfaceBudget { @@ -61,6 +77,15 @@ impl SurfaceBudget { } fn draw_group(g: &Group, m: Affine, opacity: f64, dst: &mut Image, budget: &mut SurfaceBudget) { + if budget.depth >= MAX_GROUP_DEPTH { + return; + } + budget.depth += 1; + draw_group_entered(g, m, opacity, dst, budget); + budget.depth -= 1; +} + +fn draw_group_entered(g: &Group, m: Affine, opacity: f64, dst: &mut Image, budget: &mut SurfaceBudget) { let m = m * g.transform; let o = opacity * g.opacity; if o <= 0.0 { @@ -77,9 +102,192 @@ fn draw_group(g: &Group, m: Affine, opacity: f64, dst: &mut Image, budget: &mut } return; } + let only_clip = !g.clip.is_empty() && g.mask.is_none() && g.blend == BlendMode::Normal && o >= 1.0 && !g.knockout; + let plain = only_clip && (budget.plain || !needs_backdrop(&g.children, MAX_GROUP_DEPTH.saturating_sub(budget.depth))); + if plain { + let outer = std::mem::replace(&mut budget.plain, true); + let left = MAX_GROUP_DEPTH.saturating_sub(budget.depth); + if clips_cover_content(g, m, dst.width, dst.height, left) { + for c in &g.children { + draw_node(c, m, 1.0, dst, budget); + } + } else { + draw_clip_local(g, m, dst, budget); + } + budget.plain = outer; + return; + } draw_offscreen_group(g, m, o, dst, budget); } +/// Whether every clip of `g` is a rectangle along the pixel grid's axes that fully covers each +/// pixel of a `w`×`h` image its children can draw on: the clips change nothing there, so the +/// children are drawn straight onto the image, without a surface (a page or artboard clip +/// around the artwork inside it, the outermost groups of most PDF pages). `levels`: how many +/// levels of groups below are drawn. +fn clips_cover_content(g: &Group, m: Affine, w: u32, h: u32, levels: usize) -> bool { + let Some(rects) = g.clip.iter().map(|(p, _)| axis_rect(p, m)).collect::>>() else { return false }; + // Children that draw nothing need no clip. + let Some(b) = draw_bounds(&g.children, m, levels) else { return true }; + let (fw, fh) = (f64::from(w), f64::from(h)); + // The pixels the children can touch (a pixel of margin for anti-aliasing), on the image. + let (bx0, by0) = ((b.x0.floor() - 1.0).max(0.0), (b.y0.floor() - 1.0).max(0.0)); + let (bx1, by1) = ((b.x1.ceil() + 1.0).min(fw), (b.y1.ceil() + 1.0).min(fh)); + if !(bx0 < bx1 && by0 < by1) { + // Nothing reaches the image (or the bounds aren't numbers): keep the clip. + return false; + } + rects.iter().all(|r| r.x0.ceil() <= bx0 && bx1 <= r.x1.floor() && r.y0.ceil() <= by0 && by1 <= r.y1.floor()) +} + +/// A device-space rectangle holding every pixel `nodes` drawn with `m` can cover, `None` when +/// they draw nothing. Cheap rather than tight: a stroke counts as its path's bounds grown by half +/// its width times its miter limit (at least √2, for square caps), and anything it can't +/// bound (more than `levels` levels deep, a transform that isn't finite) as the whole plane. +fn draw_bounds(nodes: &[Node], m: Affine, levels: usize) -> Option { + let everything = kurbo::Rect::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::INFINITY, f64::INFINITY); + if !m.as_coeffs().iter().all(|v| v.is_finite()) { + return Some(everything); + } + let mut out: Option = None; + for n in nodes { + let r = match n { + Node::Group(g) if levels == 0 => (!g.children.is_empty()).then_some(everything), + Node::Group(g) => { + let gm = m * g.transform; + let mut r = draw_bounds(&g.children, gm, levels - 1); + for (clip, _) in &g.clip { + r = r.map(|r| r.intersect(gm.transform_rect_bbox(clip.bounding_box()))); + } + r + } + Node::Shape(s) => { + let sm = m * s.transform; + let b = s.geom.to_path().bounding_box(); + let fill = s.fill.as_ref().map(|_| sm.transform_rect_bbox(b)); + let stroke = s.stroke.as_ref().map(|st| { + let pad = (st.width.abs() * 0.5 * st.miter.max(std::f64::consts::SQRT_2)).max(0.0); + sm.transform_rect_bbox(b.inflate(pad, pad)) + }); + match (fill, stroke) { + (Some(a), Some(b)) => Some(a.union(b)), + (a, b) => a.or(b), + } + } + Node::Image(im) => Some((m * im.transform).transform_rect_bbox(kurbo::Rect::new(0.0, 0.0, f64::from(im.width), f64::from(im.height)))), + }; + if let Some(r) = r { + let r = if [r.x0, r.y0, r.x1, r.y1].iter().any(|v| v.is_nan()) { everything } else { r }; + out = Some(out.map_or(r, |o| o.union(r))); + } + } + out +} + +/// The device-space rectangle a clip path is when it is one along the pixel grid's axes: a +/// move, three or four lines and an optional close, its sides alternately horizontal and +/// vertical. +pub(crate) fn axis_rect(path: &kurbo::BezPath, m: Affine) -> Option { + use kurbo::PathEl; + let els = path.elements(); + let mut pts: Vec = Vec::with_capacity(5); + for (i, el) in els.iter().enumerate() { + match el { + PathEl::MoveTo(p) if i == 0 => pts.push(m * *p), + PathEl::LineTo(p) if i > 0 && pts.len() < 5 => pts.push(m * *p), + PathEl::ClosePath if i > 0 && i + 1 == els.len() => {} + _ => return None, + } + } + const EPS: f64 = 1e-6; + let same = |a: Point, b: Point| (a.x - b.x).abs() <= EPS && (a.y - b.y).abs() <= EPS; + if pts.len() == 5 && pts.first().zip(pts.last()).is_some_and(|(a, b)| same(*a, *b)) { + pts.pop(); + } + let p: [Point; 4] = pts.try_into().ok()?; + let sides = [(p[0], p[1]), (p[1], p[2]), (p[2], p[3]), (p[3], p[0])]; + // Each side horizontal or vertical (and not a point), the next one the other way. + let horizontal = |(a, b): (Point, Point)| -> Option { + let (dx, dy) = ((b.x - a.x).abs(), (b.y - a.y).abs()); + match (dx > EPS, dy > EPS) { + (true, false) => Some(true), + (false, true) => Some(false), + _ => None, + } + }; + let kinds = sides.map(horizontal); + let [Some(k0), Some(k1), Some(k2), Some(k3)] = kinds else { return None }; + if k0 == k1 || k1 == k2 || k2 == k3 || k3 == k0 { + return None; + } + let r = kurbo::Rect::from_points(p[0], p[2]); + [r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()).then_some(r) +} + +/// Whether drawing `nodes` reads what is below them beyond Normal compositing: a blend mode or +/// a knockout group anywhere inside. Past `levels` levels of groups the answer is yes. +fn needs_backdrop(nodes: &[Node], levels: usize) -> bool { + nodes.iter().any(|n| match n { + Node::Group(g) => levels == 0 || g.blend != BlendMode::Normal || g.knockout || needs_backdrop(&g.children, levels - 1), + Node::Shape(_) | Node::Image(_) => false, + }) +} + +/// A plain clipping group (no soft mask, blend mode, group opacity or knockout) whose content +/// composites Normal throughout: drawn into a surface the size of its clip's bounds and +/// composited over `dst`. With Normal content that is the same as drawing it over a copy of +/// the backdrop and keeping the clip (`(C over B)·c + B·(1 − c) = (C·c) over B`), without two +/// full-size copies per nesting level: a page continuously rasterised at 400 % lost its +/// nested clipped artwork to the surface budget (#2895). +fn draw_clip_local(g: &Group, m: Affine, dst: &mut Image, budget: &mut SurfaceBudget) { + let mut bounds: Option = None; + for (path, _) in &g.clip { + let b = m.transform_rect_bbox(path.bounding_box()); + bounds = Some(bounds.map_or(b, |a| a.intersect(b))); + } + let Some((x0, y0, w, h)) = bounds.and_then(|b| pixel_rect(b, dst.width, dst.height)) else { return }; + let Some(mut tmp) = budget.image(w, h, None) else { return }; + let local = to_local((x0, y0), m); + for c in &g.children { + draw_node(c, local, 1.0, &mut tmp, budget); + } + let cov = clip_coverage(g, local, w, h); + tmp.data.par_iter_mut().zip(cov.par_iter()).for_each(|(p, c)| { + for v in p.iter_mut() { + *v *= c; + } + }); + over_at(dst, &tmp, (x0, y0), 1.0); + budget.release(&tmp); +} + +/// The pixels of a `w`×`h` image that the device-space rectangle `r` touches, with a pixel of +/// margin for anti-aliasing, as `(x, y, width, height)`; `None` when it misses the image. A +/// rectangle that isn't finite stands for the whole image. +fn pixel_rect(r: kurbo::Rect, w: u32, h: u32) -> Option<(u32, u32, u32, u32)> { + if w == 0 || h == 0 { + return None; + } + if ![r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()) { + return Some((0, 0, w, h)); + } + let (fw, fh) = (f64::from(w), f64::from(h)); + let x0 = (r.x0.min(r.x1) - 1.0).floor().clamp(0.0, fw); + let y0 = (r.y0.min(r.y1) - 1.0).floor().clamp(0.0, fh); + let x1 = (r.x0.max(r.x1) + 1.0).ceil().clamp(0.0, fw); + let y1 = (r.y0.max(r.y1) + 1.0).ceil().clamp(0.0, fh); + if x1 <= x0 || y1 <= y0 { + return None; + } + Some((x0 as u32, y0 as u32, (x1 - x0) as u32, (y1 - y0) as u32)) +} + +/// A translation by `-origin`: maps device space onto a surface whose top-left pixel sits at +/// `origin` of the device image. +fn to_local(origin: (u32, u32), m: Affine) -> Affine { + Affine::translate((-f64::from(origin.0), -f64::from(origin.1))) * m +} + /// Draws a group through an offscreen buffer, then composites it onto `dst`. fn draw_offscreen_group(g: &Group, m: Affine, o: f64, dst: &mut Image, budget: &mut SurfaceBudget) { let only_clip = g.mask.is_none() && g.blend == BlendMode::Normal && o >= 1.0 && !g.knockout; @@ -191,7 +399,10 @@ fn apply_soft_mask(sm: &SoftMask, m: Affine, tmp: &mut Image, budget: &mut Surfa let Some(mut mk) = budget.image(tmp.width, tmp.height, None) else { return false; }; + // The mask's content is a tree of its own: what is known of the masked group's isn't. + let plain = std::mem::replace(&mut budget.plain, false); draw_group(&sm.content, m, 1.0, &mut mk, budget); + budget.plain = plain; let bc = sm.backdrop.map(|v| v as f32); let lum = sm.luminosity; tmp.data.par_iter_mut().zip(mk.data.par_iter()).for_each(|(p, q)| { @@ -421,12 +632,16 @@ fn draw_image(im: &crate::Image, m: Affine, opacity: f64, dst: &mut Image) { if !inv.as_coeffs().iter().all(|v| v.is_finite()) { return; } - let quad = kurbo::Rect::new(0.0, 0.0, iw as f64, ih as f64).to_path(0.01); - let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&quad), &mat3(full), dst.width, dst.height, effectcraft_path::FillRule::NonZero); - let w = dst.width as usize; - if w == 0 { + // Coverage over the pixels the image's quadrilateral reaches only (see `draw_shape`). + let rect = kurbo::Rect::new(0.0, 0.0, iw as f64, ih as f64); + let Some((x0, y0, rw, rh)) = pixel_rect(full.transform_rect_bbox(rect), dst.width, dst.height) else { return }; + let quad = rect.to_path(0.01); + let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&quad), &mat3(to_local((x0, y0), full)), rw, rh, effectcraft_path::FillRule::NonZero); + let (w, cw) = (dst.width as usize, cov.width as usize); + if w == 0 || cw == 0 { return; } + let (ox, oy) = (x0 as usize, y0 as usize); let px = |x: usize, y: usize| -> [f32; 4] { let i = (y * iw + x) * 4; let a = im.rgba[i + 3] as f32 / 255.0; @@ -438,9 +653,10 @@ fn draw_image(im: &crate::Image, m: Affine, opacity: f64, dst: &mut Image) { let fy = (c[2] * c[2] + c[3] * c[3]).sqrt(); let taps = (fx.max(fy).ceil() as usize).clamp(1, 8); let k = opacity as f32; - dst.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| { - for (x, d) in row.iter_mut().enumerate() { - let cv = cov.data[y * w + x]; + dst.data.par_chunks_mut(w).enumerate().skip(oy).zip(cov.data.par_chunks(cw)).for_each(|((y, row), crow)| { + let Some(seg) = row.get_mut(ox..) else { return }; + for (lx, (d, &cv)) in seg.iter_mut().zip(crow).enumerate() { + let x = ox + lx; if cv <= 0.0 { continue; } @@ -486,6 +702,30 @@ fn over(dst: &mut Image, src: &Image, k: f32) { }); } +/// `dst = src·k over dst` for `src` placed with its top-left pixel at `origin` of `dst`. +fn over_at(dst: &mut Image, src: &Image, origin: (u32, u32), k: f32) { + let (w, sw) = (dst.width as usize, src.width as usize); + if w == 0 || sw == 0 { + return; + } + let (x0, y0) = (origin.0 as usize, origin.1 as usize); + dst.data.par_chunks_mut(w).skip(y0).zip(src.data.par_chunks(sw)).for_each(|(row, srow)| { + let Some(seg) = row.get_mut(x0..) else { return }; + for (d, s) in seg.iter_mut().zip(srow) { + let a = s[3] * k; + if a > 0.0 { + for c in 0..3 { + d[c] = s[c] * k + d[c] * (1.0 - a); + } + d[3] = a + d[3] * (1.0 - a); + } + } + }); +} + +/// Draw a shape. Its coverage is computed over the pixels its fill or stroke can reach only, +/// not the whole image: a page drawn at a large scale cost a full-size coverage pass per path +/// and per run of text (#2895). fn draw_shape(s: &Shape, m: Affine, opacity: f64, dst: &mut Image) { let m = m * s.transform; let path = s.geom.to_path(); @@ -496,22 +736,32 @@ fn draw_shape(s: &Shape, m: Affine, opacity: f64, dst: &mut Image) { FillRule::NonZero => effectcraft_path::FillRule::NonZero, FillRule::EvenOdd => effectcraft_path::FillRule::EvenOdd, }; - let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&path), &mat3(m), w, h, rule); - paint(dst, &cov, fill, (opacity * s.fill_opacity) as f32, m, bbox); + if let Some((x0, y0, rw, rh)) = pixel_rect(m.transform_rect_bbox(bbox), w, h) { + let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&path), &mat3(to_local((x0, y0), m)), rw, rh, rule); + paint(dst, &cov, (x0, y0), fill, (opacity * s.fill_opacity) as f32, m, bbox); + } } if let Some(st) = &s.stroke { + // The outline as `effectcraft_path::stroke_coverage` makes it (the resolution scale of + // the whole transform, which a translation doesn't change). let style = st.style(); - let cov = effectcraft_path::stroke_coverage(std::slice::from_ref(&path), &style, &mat3(m), w, h); - paint(dst, &cov, &st.paint, (opacity * st.opacity) as f32, m, bbox); + if let Some(outline) = effectcraft_path::stroke_outline(std::slice::from_ref(&path), &style, mat3(m).mean_scale()) + && let Some((x0, y0, rw, rh)) = pixel_rect(m.transform_rect_bbox(outline.bounding_box()), w, h) + { + let cov = + effectcraft_path::fill_coverage(std::slice::from_ref(&outline), &mat3(to_local((x0, y0), m)), rw, rh, effectcraft_path::FillRule::NonZero); + paint(dst, &cov, (x0, y0), &st.paint, (opacity * st.opacity) as f32, m, bbox); + } } } -/// Composite paint through coverage. -fn paint(dst: &mut Image, cov: &Mask, p: &Paint, opacity: f32, m: Affine, bbox: kurbo::Rect) { - let w = dst.width as usize; - if w == 0 { +/// Composite paint through coverage `cov`, whose top-left pixel sits at `origin` of `dst`. +fn paint(dst: &mut Image, cov: &Mask, origin: (u32, u32), p: &Paint, opacity: f32, m: Affine, bbox: kurbo::Rect) { + let (w, cw) = (dst.width as usize, cov.width as usize); + if w == 0 || cw == 0 { return; } + let (x0, y0) = (origin.0 as usize, origin.1 as usize); let shader = match p { Paint::Color(c) => Shader::Solid([c[0] as f32, c[1] as f32, c[2] as f32, 1.0]), Paint::Gradient(g) => { @@ -523,13 +773,13 @@ fn paint(dst: &mut Image, cov: &Mask, p: &Paint, opacity: f32, m: Affine, bbox: Shader::Gradient(g, to_grad) } }; - dst.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| { - for (x, d) in row.iter_mut().enumerate() { - let c = cov.data[y * w + x]; + dst.data.par_chunks_mut(w).enumerate().skip(y0).zip(cov.data.par_chunks(cw)).for_each(|((y, row), crow)| { + let Some(seg) = row.get_mut(x0..) else { return }; + for (lx, (d, &c)) in seg.iter_mut().zip(crow).enumerate() { if c <= 0.0 { continue; } - let col = shader.at(x as f64 + 0.5, y as f64 + 0.5); + let col = shader.at((x0 + lx) as f64 + 0.5, y as f64 + 0.5); let a = col[3] * c * opacity; if a <= 0.0 { continue; diff --git a/crates/svg/src/tests.rs b/crates/svg/src/tests.rs index 06dfbd333..0b4b02832 100644 --- a/crates/svg/src/tests.rs +++ b/crates/svg/src/tests.rs @@ -446,3 +446,193 @@ fn at_rule_blocks_are_skipped_by_brace_depth() { assert_eq!(sheet.rules.len(), 1); assert_eq!(sheet.rules[0].classes, vec!["e".to_string()]); } + +fn solid_rect(r: kurbo::Rect, c: [f64; 3]) -> Node { + Node::Shape(Shape { + name: "rect".into(), + geom: Geom::Rect { x: r.x0, y: r.y0, w: r.width(), h: r.height(), rx: 0.0, ry: 0.0 }, + transform: Affine::IDENTITY, + fill: Some(Paint::Color(c)), + fill_opacity: 1.0, + fill_rule: FillRule::NonZero, + stroke: None, + opacity: 1.0, + }) +} + +/// A PDF-style clipping group (not isolated: blend modes inside reach the backdrop). +fn clip_group(r: kurbo::Rect, children: Vec) -> Node { + let mut g = Group::new("Clip Group"); + g.clip.push((kurbo::Shape::to_path(&r, 0.1), FillRule::NonZero)); + g.isolated = false; + g.children = children; + Node::Group(g) +} + +fn doc_of(w: f64, h: f64, children: Vec) -> Doc { + let mut root = Group::new("page"); + root.children = children; + Doc { width: w, height: h, root, skipped: vec![] } +} + +/// Nested clipping groups over a backdrop: the content shows where every clip covers, half +/// where a clip edge halves a pixel, and the backdrop everywhere else (#2895: plain clipping +/// groups are drawn on surfaces the size of their clip). +#[test] +fn nested_clip_groups_keep_the_backdrop_outside_their_clips() { + let blue = [0.0, 0.0, 1.0]; + let red = [1.0, 0.0, 0.0]; + let inner = clip_group(kurbo::Rect::new(10.5, 0.0, 20.0, 40.0), vec![solid_rect(kurbo::Rect::new(0.0, 0.0, 40.0, 40.0), red)]); + let doc = + doc_of(40.0, 40.0, vec![solid_rect(kurbo::Rect::new(0.0, 0.0, 40.0, 40.0), blue), clip_group(kurbo::Rect::new(5.0, 5.0, 35.0, 35.0), vec![inner])]); + let img = rasterize(&doc, 40, 40, 1.0); + let (r, b) = ([1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]); + for (x, y, want) in [(15, 20, r), (11, 6, r), (19, 34, r), (25, 20, b), (15, 2, b), (15, 36, b), (9, 20, b), (21, 20, b)] { + assert!(near(px(&img, x, y), want, 1e-3), "({x}, {y}): {:?}", px(&img, x, y)); + } + assert!(near(px(&img, 10, 20), [0.5, 0.0, 0.5, 1.0], 0.02), "half-covered edge pixel {:?}", px(&img, 10, 20)); + // Scaled 4×: the clip edge at 10.5 lands on a pixel boundary (42), hard on both sides. + let big = rasterize(&doc, 160, 160, 4.0); + assert!(near(px(&big, 41, 80), b, 0.01) && near(px(&big, 42, 80), r, 0.01), "{:?} {:?}", px(&big, 41, 80), px(&big, 42, 80)); +} + +/// A clipping group whose content blends (Multiply) still composites with the real backdrop: +/// red multiplied over blue is black inside the clip, blue outside. +#[test] +fn a_blend_mode_inside_a_clip_group_reaches_the_backdrop() { + let mut multiply = Group::new("Multiply"); + multiply.blend = BlendMode::Multiply; + multiply.children = vec![solid_rect(kurbo::Rect::new(0.0, 0.0, 20.0, 20.0), [1.0, 0.0, 0.0])]; + let doc = doc_of( + 20.0, + 20.0, + vec![ + solid_rect(kurbo::Rect::new(0.0, 0.0, 20.0, 20.0), [0.0, 0.0, 1.0]), + clip_group(kurbo::Rect::new(5.0, 5.0, 15.0, 15.0), vec![Node::Group(multiply)]), + ], + ); + let img = rasterize(&doc, 20, 20, 1.0); + assert!(near(px(&img, 10, 10), [0.0, 0.0, 0.0, 1.0], 1e-3), "{:?}", px(&img, 10, 10)); + assert!(near(px(&img, 2, 10), [0.0, 0.0, 1.0, 1.0], 1e-3), "{:?}", px(&img, 2, 10)); +} + +/// A stroke reaches past its path's bounds by half its width: a horizontal line (a path +/// without height) stroked 4 px wide covers 4 rows, and its butt caps end at the path's ends. +#[test] +fn a_stroke_draws_its_full_width_past_the_path_bounds() { + let mut line = BezPath::new(); + line.move_to((5.0, 20.0)); + line.line_to((35.0, 20.0)); + let shape = Node::Shape(Shape { + name: "line".into(), + geom: Geom::Path(line), + transform: Affine::IDENTITY, + fill: None, + fill_opacity: 1.0, + fill_rule: FillRule::NonZero, + stroke: Some(Stroke { paint: Paint::Color([0.0, 0.0, 0.0]), opacity: 1.0, width: 4.0, cap: Cap::Butt, join: Join::Miter, miter: 4.0, dash: None }), + opacity: 1.0, + }); + let img = rasterize(&doc_of(40.0, 40.0, vec![shape]), 40, 40, 1.0); + for y in 18..22 { + assert!(px(&img, 20, y)[3] > 0.99, "row {y}: {:?}", px(&img, 20, y)); + } + for (x, y) in [(20, 17), (20, 22), (3, 20), (36, 20)] { + assert!(px(&img, x, y)[3] < 0.01, "({x}, {y}): {:?}", px(&img, x, y)); + } +} + +/// Bytes of `n` RGBA f32 surfaces of `w`×`h` pixels (a surface budget for the tests below). +fn surfaces(n: f64, w: u32, h: u32) -> usize { + (n * f64::from(w) * f64::from(h) * 16.0) as usize +} + +/// #2895: an A4 page continuously rasterised at 400 % is a 128 MB surface, and every nested +/// clipping group used to copy the backdrop and draw on a second surface of that size, so the +/// second level of clips ran past the 512 MB surface budget and its artwork was dropped. Clip +/// groups now draw on surfaces the size of their clip: with room for four canvas-size surfaces, +/// artwork inside four nested clips still draws (it used to need nine). +#[test] +fn deeply_clipped_artwork_draws_within_the_surface_budget() { + let red = [1.0, 0.0, 0.0]; + let mut node = solid_rect(kurbo::Rect::new(0.0, 0.0, 300.0, 300.0), red); + for i in 1..=4 { + let inset = 10.0 * f64::from(i); + node = clip_group(kurbo::Rect::new(inset, inset, 300.0 - inset, 300.0 - inset), vec![node]); + } + let img = crate::raster::rasterize_with_budget(&doc_of(300.0, 300.0, vec![node]), 300, 300, Affine::IDENTITY, surfaces(4.0, 300, 300)); + assert_eq!((img.width, img.height), (300, 300)); + assert!(near(px(&img, 150, 150), [1.0, 0.0, 0.0, 1.0], 1e-3), "centre {:?}", px(&img, 150, 150)); + assert!(near(px(&img, 45, 150), [1.0, 0.0, 0.0, 1.0], 1e-3), "just inside every clip {:?}", px(&img, 45, 150)); + assert!(px(&img, 35, 150)[3] < 0.01, "outside the outermost clip {:?}", px(&img, 35, 150)); +} + +/// #2895: clips that cover the whole canvas around the artwork inside them (a page clip, an +/// artboard clip, the outermost groups of most PDF pages) change no pixel, so they are drawn +/// without a surface: with room for two and a half canvas-size surfaces, the artwork inside two +/// of them still draws, and an inner clip still cuts it. +#[test] +fn canvas_covering_clips_draw_without_a_surface() { + let red = [1.0, 0.0, 0.0]; + let art = clip_group(kurbo::Rect::new(150.0, 0.0, 300.0, 350.0), vec![solid_rect(kurbo::Rect::new(100.0, 100.0, 250.0, 250.0), red)]); + let page = clip_group(kurbo::Rect::new(0.0, 0.0, 350.0, 350.0), vec![art]); + let outer = clip_group(kurbo::Rect::new(-1.0, -1.0, 351.0, 351.0), vec![page]); + let img = crate::raster::rasterize_with_budget(&doc_of(350.0, 350.0, vec![outer]), 350, 350, Affine::IDENTITY, surfaces(2.5, 350, 350)); + assert_eq!((img.width, img.height), (350, 350)); + for (x, y) in [(200, 200), (151, 101), (248, 248)] { + assert!(near(px(&img, x, y), [1.0, 0.0, 0.0, 1.0], 1e-3), "({x}, {y}): {:?}", px(&img, x, y)); + } + for (x, y) in [(120, 200), (148, 200), (200, 90), (260, 200)] { + assert!(px(&img, x, y)[3] < 0.01, "({x}, {y}): {:?}", px(&img, x, y)); + } +} + +/// Take a tree apart without recursing (a deep one would overflow the stack when dropped). +fn dismantle(mut nodes: Vec) { + while let Some(n) = nodes.pop() { + if let Node::Group(mut g) = n { + nodes.append(&mut g.children); + } + } +} + +/// Never crash: drawing recurses, and clipping groups nested twenty thousand deep (a crafted +/// file) are drawn without running out of stack, on a thread with a small one; groups past the +/// nesting limit are not drawn, shallower artwork is. +#[test] +fn clip_groups_nested_without_end_do_not_overflow_the_stack() { + let run = std::thread::Builder::new().stack_size(4 << 20).spawn(|| { + let mut node = solid_rect(kurbo::Rect::new(0.0, 0.0, 64.0, 64.0), [1.0, 0.0, 0.0]); + for _ in 0..20_000 { + node = clip_group(kurbo::Rect::new(0.0, 0.0, 64.0, 64.0), vec![node]); + } + let shallow = solid_rect(kurbo::Rect::new(0.0, 0.0, 8.0, 8.0), [0.0, 0.0, 1.0]); + let doc = doc_of(64.0, 64.0, vec![node, shallow]); + let img = rasterize(&doc, 64, 64, 1.0); + let out = (img.width, img.height, px(&img, 4, 4), px(&img, 40, 40)); + dismantle(vec![Node::Group(doc.root)]); + out + }); + let (w, h, shallow, deep) = run.unwrap().join().unwrap(); + assert_eq!((w, h), (64, 64)); + assert!(near(shallow, [0.0, 0.0, 1.0, 1.0], 1e-3), "{shallow:?}"); + assert!(deep[3] < 0.01, "artwork past the nesting limit is not drawn: {deep:?}"); +} + +/// Only a rectangle along the pixel grid's axes counts as a clip that can be skipped: a +/// rotated square or a path that folds back on itself does not. +#[test] +fn axis_rect_recognises_only_axis_aligned_rectangles() { + use crate::raster::axis_rect; + let rect = kurbo::Shape::to_path(&kurbo::Rect::new(1.0, 2.0, 5.0, 7.0), 0.1); + assert_eq!(axis_rect(&rect, Affine::IDENTITY), Some(kurbo::Rect::new(1.0, 2.0, 5.0, 7.0))); + assert_eq!(axis_rect(&rect, Affine::scale(2.0)), Some(kurbo::Rect::new(2.0, 4.0, 10.0, 14.0))); + assert_eq!(axis_rect(&rect, Affine::rotate(0.3)), None); + let mut folded = BezPath::new(); + folded.move_to((0.0, 0.0)); + folded.line_to((10.0, 0.0)); + folded.line_to((10.0, 10.0)); + folded.line_to((10.0, 0.0)); + folded.close_path(); + assert_eq!(axis_rect(&folded, Affine::IDENTITY), None); +} From b33f5f62a8fd19f3c52699d8189cd54b9b3d8e99 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 13:58:35 +0100 Subject: [PATCH 026/350] Expressions: a function declaration ends at its brace, so a next line starting with '-' is a new statement (#3212) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The array-math rewrite folded `function f(x){…}\n-50 + 100` into `__add(__sub(function f(x){…}, 50), 100)`: the declaration became a function expression operand, the result was NaN and the property fell back to its static value. In ECMAScript an expression statement cannot start with `function`, so at statement start it is a FunctionDeclaration that ends at its `}`. The rewriter now emits a function in statement position as a declaration on its own and never as an operand of what follows; function expressions (after `=`, `(`, `,`, `:`, `return` …) stay operands. The empty expressionError for evaluation errors is tracked in #3219. Fixes #3212 --- crates/expr/src/rewrite.rs | 115 +++++++++++++++++++++++++++++-------- crates/expr/src/tests.rs | 16 ++++++ 2 files changed, 106 insertions(+), 25 deletions(-) diff --git a/crates/expr/src/rewrite.rs b/crates/expr/src/rewrite.rs index 34b52439b..69762d21e 100644 --- a/crates/expr/src/rewrite.rs +++ b/crates/expr/src/rewrite.rs @@ -334,7 +334,7 @@ fn raw(item: &Item) -> Piece { Item::Tok(t) => Piece { ws: t.ws.clone(), body: t.text.clone() }, Item::Group { open, inner, close } => { let mut body = open.text.clone(); - body.push_str(&rewrite_seq(inner)); + body.push_str(&rewrite_seq(inner, open.text == "{")); if let Some(c) = close { body.push_str(&c.ws); body.push_str(&c.text); @@ -378,25 +378,7 @@ fn operand(items: &[Item], i: usize) -> Option<(Piece, usize)> { }; return Some((Piece { ws: t.ws.clone(), body }, j)); } - let mut p = raw(first); - let mut j = i + 1; - // `function name(args) { body }` is one atom. - if first.tok().is_some_and(|t| t.is_kw(&["function"])) { - if items.get(j).is_some_and(|x| x.is("*")) { - push(&mut p, &items[j]); - j += 1; - } - if items.get(j).and_then(Item::tok).is_some_and(|t| t.kind == Kind::Ident) { - push(&mut p, &items[j]); - j += 1; - } - for g in ["(", "{"] { - if items.get(j).and_then(Item::group) == Some(g) { - push(&mut p, &items[j]); - j += 1; - } - } - } + let (mut p, mut j) = if is_function(first) { function_atom(items, i) } else { (raw(first), i + 1) }; // Postfix: member access, calls, indexing, tagged templates, `x++`. while let Some(it) = items.get(j) { let member = it.is(".") || it.is("?."); @@ -428,6 +410,59 @@ fn operand(items: &[Item], i: usize) -> Option<(Piece, usize)> { Some((p, j)) } +fn is_function(item: &Item) -> bool { + item.tok().is_some_and(|t| t.is_kw(&["function"])) +} + +/// `function [*] [name] (args) { body }` from the `function` keyword at `i`, as one atom. +fn function_atom(items: &[Item], i: usize) -> (Piece, usize) { + let Some(first) = items.get(i) else { + // Unreachable for callers (they pass a `function` item); still advance so no loop stalls. + return (Piece { ws: String::new(), body: String::new() }, i.saturating_add(1)); + }; + let mut p = raw(first); + let mut j = i + 1; + if items.get(j).is_some_and(|x| x.is("*")) { + push(&mut p, &items[j]); + j += 1; + } + if items.get(j).and_then(Item::tok).is_some_and(|t| t.kind == Kind::Ident) { + push(&mut p, &items[j]); + j += 1; + } + for g in ["(", "{"] { + if items.get(j).and_then(Item::group) == Some(g) { + push(&mut p, &items[j]); + j += 1; + } + } + (p, j) +} + +/// Is the `function` keyword at `i` at the start of a statement, so a function declaration +/// (ECMAScript: an expression statement cannot start with `function`)? A declaration ends at its +/// `}`: whatever follows is a new statement, never an operand of it. `stmts` says whether +/// `items` is a statement list (the whole expression or a `{ … }` block) rather than the inside +/// of `( … )` or `[ … ]`. +fn at_statement_start(items: &[Item], i: usize, stmts: bool) -> bool { + // `async function`: the statement starts at `async` (no line break may come between them). + let is_async = |k: usize| items.get(k).and_then(Item::tok).is_some_and(|a| a.is_kw(&["async"])); + let at = match i.checked_sub(1) { + Some(k) if is_async(k) && !items.get(i).is_some_and(Item::nl) => k, + _ => i, + }; + let Some(prev) = at.checked_sub(1).and_then(|k| items.get(k)) else { + return stmts; + }; + match prev { + // A block, an `if (…)`/`while (…)` header or a value the line break ends. + Item::Group { .. } => true, + // Known limit: after a label, `case …:` or `default:` a declaration is still read as an + // expression (that `:` cannot be told from an object literal's or a conditional's here). + Item::Tok(t) => t.is(";") || t.is_kw(&["else", "do"]) || ends_value(t), + } +} + fn push(p: &mut Piece, item: &Item) { let r = raw(item); p.body.push_str(&r.ws); @@ -510,7 +545,9 @@ fn assignment_end(items: &[Item], i: usize) -> usize { j } -fn rewrite_seq(items: &[Item]) -> String { +/// Rewrite a sequence of items. `stmts`: the sequence is a statement list (the whole expression +/// or the inside of `{ … }`), not the inside of `( … )` or `[ … ]`. +fn rewrite_seq(items: &[Item], stmts: bool) -> String { let mut out = String::new(); let mut i = 0; while i < items.len() { @@ -521,12 +558,20 @@ fn rewrite_seq(items: &[Item]) -> String { i += 2; continue; } + // A function declaration ends at its `}` (#3212): `function f() {}\n-1` is two statements, + // so the next line must not become an operand (`__sub(function f() {}, 1)` is NaN). + if is_function(it) && at_statement_start(items, i, stmts) { + let (p, j) = function_atom(items, i); + push_piece(&mut out, p); + i = j; + continue; + } if let Some((p, j)) = chain(items, i) { // Compound assignment: `x += v` → `x = __add(x, (v))` (the target is evaluated twice, // which only matters for targets with side effects). if let Some(op) = items.get(j).and_then(Item::tok).filter(|t| t.kind == Kind::Punct && matches!(t.text.as_str(), "+=" | "-=" | "*=" | "/=")) { let end = assignment_end(items, j + 1); - let rhs = rewrite_seq(&items[j + 1..end]); + let rhs = rewrite_seq(&items[j + 1..end], false); let h = helper(&op.text[..1]).unwrap_or("__add"); let target = compound_assignment_target(items, i, j); out.push_str(&p.ws); @@ -555,7 +600,7 @@ fn raw_ref(item: &Item) -> String { out.push_str(&raw_ref(item)); } } else { - out.push_str(&rewrite_seq(inner)); + out.push_str(&rewrite_seq(inner, open.text == "{")); } if let Some(close) = close { out.push_str(&close.text); @@ -794,7 +839,7 @@ pub fn check_nesting(src: &str) -> Result<(), String> { if too_nested(&toks) { return Err(too_deep()); } - let mut out = rewrite_seq(&build(toks)); + let mut out = rewrite_seq(&build(toks), true); out.push_str(&tail); if over_rewrite_limit(&out) { return Err(too_large()); @@ -825,7 +870,7 @@ pub fn rewrite(src: &str) -> String { if too_nested(&toks) { return throw_too_deep(); } - let mut out = rewrite_seq(&build(toks)); + let mut out = rewrite_seq(&build(toks), true); out.push_str(&tail); if over_rewrite_limit(&out) { return throw_too_large(); @@ -897,6 +942,26 @@ mod tests { assert_eq!(n(&rewrite("2 ** 3 * 2")), n("__mul(2 ** 3, 2)")); } + #[test] + fn a_function_declaration_is_not_an_operand_of_the_next_statement() { + // #3212: a declaration ends at its `}` (an expression statement cannot start with + // `function`), so a following `-`, `+` or `[` starts a new statement. + assert_eq!(n(&rewrite("function f(x){ return x; }\n-50 + 100")), n("function f(x){ return x; }\n__add(-50, 100)")); + assert_eq!(n(&rewrite("function f(){}\n+a - b")), n("function f(){}\n__sub(+a, b)")); + assert_eq!(n(&rewrite("function f(){}\n[1, 2] - [1, 1]")), n("function f(){}\n__sub([1, 2], [1, 1])")); + assert_eq!(n(&rewrite("function f(){}\n(a) - b")), n("function f(){}\n__sub((a), b)")); + assert_eq!(n(&rewrite("function* g(){} -x")), n("function* g(){} __neg(x)")); + assert_eq!(n(&rewrite("async function f(){}\n-x")), n("async function f(){}\n__neg(x)")); + assert_eq!(n(&rewrite("var a = 1;function f(){}\n-x")), n("var a = 1;function f(){}\n__neg(x)")); + assert_eq!(n(&rewrite("if (a) { function f(){}\n-b }")), n("if (a) { function f(){}\n__neg(b) }")); + // Function expressions stay operands, whatever follows on the next line. + assert_eq!(n(&rewrite("x = function(a){ return a } - 1")), n("x = __sub(function(a){ return a }, 1)")); + assert_eq!(n(&rewrite("var g = function(){}\n- 1")), n("var g = __sub(function(){},\n 1)")); + assert_eq!(n(&rewrite("(function f(){} - 1)")), n("(__sub(function f(){}, 1))")); + assert_eq!(n(&rewrite("[function(){} - 1]")), n("[__sub(function(){}, 1)]")); + assert_eq!(n(&rewrite("o = {k: function(){} - 1}")), n("o = {k: __sub(function(){}, 1)}")); + } + #[test] fn source_expansion_is_bounded_before_rewriting() { let src = "x += 1;".repeat(super::MAX_REWRITTEN_BYTES / 6 + 1); diff --git a/crates/expr/src/tests.rs b/crates/expr/src/tests.rs index a7d2489c7..b3a18f6ee 100644 --- a/crates/expr/src/tests.rs +++ b/crates/expr/src/tests.rs @@ -193,6 +193,22 @@ fn functions_and_let_are_reusable_across_evaluations() { assert_close!(f.num(e, 2.0), 9.0); } +#[test] +fn a_function_declaration_ends_at_its_brace_so_a_leading_minus_starts_a_new_statement() { + // #3212: the array-math rewrite took `function … {…}\n-50 + 100` for one expression, + // `(function … {…}) - 50 + 100`, which is NaN (and left the function undeclared). + let f = fx(); + assert_close!(f.num("function same3212(x){ return x; }\n-50 + 100", 0.0), 50.0); + // The declaration really declared the function (globals do not outlive one evaluation). + assert_close!(f.num("function same3212(x){ return x; }\n-50 + 100;\nsame3212(7)", 0.0), 7.0); + assert_close!(f.num("function twice3212(x){ return x * 2; }\n-twice3212(10) + 100", 0.0), 80.0); + assert_close!(f.num("function seven3212(){ return 7; }\n+seven3212()", 0.0), 7.0); + assert_close!(f.num("function one3212(){ return 1; } -one3212() * 3", 0.0), -3.0); + assert_close!(f.num("if (time >= 0) { function neg3212(x){ return -x; }\n-neg3212(4) }", 0.0), 4.0); + // A function expression stays an operand: `function - 1` is NaN in JavaScript too. + assert_close!(f.num("var g3212 = function(){ return 1; }\n- 1;\ntypeof g3212 == 'number' ? 5 : 6", 0.0), 5.0); +} + #[test] fn an_expression_cannot_rebind_a_prelude_name_for_the_next_one() { let f = fx(); From e0d9e024bd077f610b8d3878165e8cf6cd8945b2 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 14:13:23 +0100 Subject: [PATCH 027/350] Graph Editor: follow the theme (plot, bar buttons, grid, keys, handles, curves) (#3202) The Graph Editor painted its own surfaces with fixed dark greys: the plot #1d1d1d and the unlit bar buttons #2c2c2c under the theme's text colour. In the Light theme the plot stayed dark and the button labels (#222 on #2c2c2c, 1.1:1) only showed while hovered; lit buttons put white on a pale blue. GraphPalette builds the editor's colours from the theme tokens once per frame: the plot takes the Timeline's time graph background, the buttons a fill just off the panel with the more readable label, the grid an opaque tint of the plot. Keys, Bezier handles, the transform box and the curves have a darker set for a pale plot, picked by the plot colour itself so it holds under the brightness slider. theme.rs gains WCAG relative_luminance/contrast_ratio, also used by the expression editor's contrast test. Fixes #3202 --- crates/ui-egui/src/panels/expr_editor.rs | 15 +- crates/ui-egui/src/panels/graph.rs | 218 ++++++++++++++++++---- crates/ui-egui/src/panels/graph_tools.rs | 5 +- crates/ui-egui/src/theme.rs | 15 ++ crates/ui-egui/tests/it/main.rs | 1 + crates/ui-egui/tests/it/ui_graph_theme.rs | 102 ++++++++++ 6 files changed, 307 insertions(+), 49 deletions(-) create mode 100644 crates/ui-egui/tests/it/ui_graph_theme.rs diff --git a/crates/ui-egui/src/panels/expr_editor.rs b/crates/ui-egui/src/panels/expr_editor.rs index 6d742dc4e..b7f05f023 100644 --- a/crates/ui-egui/src/panels/expr_editor.rs +++ b/crates/ui-egui/src/panels/expr_editor.rs @@ -419,20 +419,7 @@ pub fn editor(ui: &mut egui::Ui, id: egui::Id, buf: &mut String, rect: Rect, opt #[cfg(test)] mod contrast_tests { use super::*; - use crate::theme::ThemeKind; - - /// WCAG contrast ratio of two opaque sRGB colours. - fn contrast(a: Color32, b: Color32) -> f32 { - let lum = |c: Color32| { - let f = |v: u8| { - let v = v as f32 / 255.0; - if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } - }; - 0.2126 * f(c.r()) + 0.7152 * f(c.g()) + 0.0722 * f(c.b()) - }; - let (x, y) = (lum(a), lum(b)); - (x.max(y) + 0.05) / (x.min(y) + 0.05) - } + use crate::theme::{ThemeKind, contrast_ratio as contrast}; /// #644: on every theme, effect names stay readable on Effect Controls' headers, and the /// expression field's text (every syntax colour, enabled and disabled expressions, matched diff --git a/crates/ui-egui/src/panels/graph.rs b/crates/ui-egui/src/panels/graph.rs index be18999d4..3b883d191 100644 --- a/crates/ui-egui/src/panels/graph.rs +++ b/crates/ui-egui/src/panels/graph.rs @@ -14,7 +14,7 @@ use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2}; use serde_json::json; use super::timeline::TMap; -use crate::theme::Tokens; +use crate::theme::{Tokens, contrast_ratio}; use crate::{EffectcraftApp, widgets}; type Actions = Vec<(String, serde_json::Value)>; @@ -29,6 +29,84 @@ const PROP_COLORS: [Color32; 6] = [ Color32::from_rgb(0x9c, 0xd8, 0x50), Color32::from_rgb(0xf0, 0x70, 0x90), ]; +/// The same hues, darker, for the Light theme's pale graph (the bright ones vanish on it). +const DIM_COLORS_LIGHT: [Color32; 4] = + [Color32::from_rgb(0xb0, 0x20, 0x20), Color32::from_rgb(0x1a, 0x7a, 0x1a), Color32::from_rgb(0x1c, 0x50, 0xc0), Color32::from_rgb(0x8a, 0x6a, 0x00)]; +const PROP_COLORS_LIGHT: [Color32; 6] = [ + Color32::from_rgb(0x8a, 0x6a, 0x00), + Color32::from_rgb(0x90, 0x30, 0xa8), + Color32::from_rgb(0x00, 0x78, 0x78), + Color32::from_rgb(0xb0, 0x50, 0x00), + Color32::from_rgb(0x4a, 0x80, 0x10), + Color32::from_rgb(0xc0, 0x30, 0x60), +]; + +/// The Graph Editor's colours for the active theme (#3202): its surfaces come from the theme's +/// tokens (the plot is the Timeline's time graph, the buttons sit on the panel), and the curve, +/// key and handle colours have a darker set for a pale plot (Light), so all of it stays readable. +/// Built once per frame in [`show`] and passed down. +#[derive(Clone, Copy, Debug)] +pub(crate) struct GraphPalette { + /// The plot's background. + pub bg: Color32, + /// The horizontal value grid lines. + pub grid: Color32, + /// The value labels on the grid and the "select animated properties" hint. + pub label: Color32, + /// An unlit bar button's fill and label. + pub button: Color32, + pub button_text: Color32, + /// A lit bar button's fill and label. + pub button_on: Color32, + pub button_on_text: Color32, + /// Bezier handles (line and end). + pub handle: Color32, + /// A key's fill, unselected and muted-unselected. + pub key: Color32, + pub key_muted: Color32, + /// The transform box's outline and its handles' fill. + pub box_line: Color32, + pub box_fill: Color32, + /// Curve colours: one per dimension, or one per property. + pub dims: [Color32; 4], + pub props: [Color32; 6], +} + +/// The lowest contrast of any of `colors` against `bg`. +fn min_contrast(colors: &[Color32], bg: Color32) -> f32 { + colors.iter().map(|c| contrast_ratio(*c, bg)).fold(f32::INFINITY, f32::min) +} + +impl GraphPalette { + pub(crate) fn new(t: &Tokens) -> Self { + let bg = t.tl_bg; + // The darker marks for a pale plot: chosen by the plot itself, not the theme's name, as + // the brightness slider moves the plot (Dark lightened, Light darkened). Whichever set + // stands out more from the plot wins. + let bright: Vec = DIM_COLORS.iter().chain(PROP_COLORS.iter()).copied().collect(); + let dark: Vec = DIM_COLORS_LIGHT.iter().chain(PROP_COLORS_LIGHT.iter()).copied().collect(); + let light = min_contrast(&dark, bg) > min_contrast(&bright, bg); + let button_on = t.panel_bg.lerp_to_gamma(t.accent, 0.55); + // White on a deep blue; on a pale one (Light) the theme's dark text. + let button_on_text = if contrast_ratio(Color32::WHITE, button_on) >= contrast_ratio(t.text, button_on) { Color32::WHITE } else { t.text }; + GraphPalette { + bg, + grid: bg.lerp_to_gamma(t.text, 0.06), + label: t.text_dim, + button: t.panel_bg.lerp_to_gamma(t.text, 0.06), + button_text: t.text, + button_on, + button_on_text, + handle: if light { Color32::from_rgb(0x80, 0x70, 0x10) } else { Color32::from_rgb(0xd8, 0xd8, 0x60) }, + key: if light { t.keyframe } else { Color32::from_gray(0xe0) }, + key_muted: if light { Color32::from_gray(0xa0) } else { Color32::from_gray(0x70) }, + box_line: if light { Color32::from_gray(0x40) } else { Color32::from_gray(0xd8) }, + box_fill: if light { Color32::from_gray(0xf2) } else { Color32::from_gray(0x20) }, + dims: if light { DIM_COLORS_LIGHT } else { DIM_COLORS }, + props: if light { PROP_COLORS_LIGHT } else { PROP_COLORS }, + } + } +} struct Handle { out: bool, @@ -53,12 +131,12 @@ struct KeyPt { } /// A graph key's fill and edge colours: muted keys stay visible and selectable, dimmed. -fn key_colors(t: &Tokens, edge: Color32, k: &KeyPt) -> (Color32, Color32) { +fn key_colors(t: &Tokens, pal: &GraphPalette, edge: Color32, k: &KeyPt) -> (Color32, Color32) { let fill = match (k.muted, k.sel) { (true, true) => t.keyframe_selected.gamma_multiply(0.5), - (true, false) => Color32::from_gray(0x70), + (true, false) => pal.key_muted, (false, true) => t.keyframe_selected, - (false, false) => Color32::from_gray(0xe0), + (false, false) => pal.key, }; (fill, if k.muted { edge.gamma_multiply(0.4) } else { edge }) } @@ -131,7 +209,7 @@ fn speed_at(pr: &Property, spatial: bool, d: usize, lt: Tick) -> f64 { } /// Build the curves for the visible time span. -fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, plot: Rect, speed: bool) -> Vec> { +fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, plot: Rect, speed: bool, pal: &GraphPalette) -> Vec> { let sel = &app.session.state.selected_keys; let n = ((plot.width() / 2.0) as usize).max(2); let mut out = vec![]; @@ -141,7 +219,7 @@ fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, pl let spatial = pr.spatial && matches!(pr.value, Value::Vec2(_) | Value::Vec3(_)); let dims = if speed && spatial { 1 } else { shown_dims(l, pr) }; for d in 0..dims { - let color = if dims > 1 { DIM_COLORS[d % 4] } else { PROP_COLORS[pi % PROP_COLORS.len()] }; + let color = if dims > 1 { pal.dims[d % pal.dims.len()] } else { pal.props[pi % pal.props.len()] }; let mut pts = Vec::with_capacity(n); for i in 0..n { let x = plot.min.x + plot.width() * i as f32 / (n - 1) as f32; @@ -193,6 +271,9 @@ struct ButtonBar { hidden: Vec<(String, String, bool, String)>, /// The id picked from the "»" menu last frame, run by its button this frame. chosen: Option, + /// The bar's rectangle and the theme's Graph Editor colours. + bar: Rect, + pal: GraphPalette, } /// The "»" menu's popup and the id it picked (read by the next frame's buttons). @@ -204,22 +285,22 @@ impl ButtonBar { /// A bar starting 8 points into `bar`, stopping short of its right end by the "»" button's /// width. It takes the id picked from the "»" menu in the previous frame, so the button whose /// id that was reports a click on the frame after the pick. - fn new(ctx: &egui::Context, bar: Rect) -> Self { + fn new(ctx: &egui::Context, bar: Rect, pal: GraphPalette) -> Self { let chosen = ctx.data_mut(|d| d.remove_temp::(egui::Id::unique(MORE_POPUP).with("chosen"))); - ButtonBar { x: bar.min.x + 8.0, limit: bar.max.x - 8.0 - Self::MORE_W - 4.0, hidden: Vec::new(), chosen } + ButtonBar { x: bar.min.x + 8.0, limit: bar.max.x - 8.0 - Self::MORE_W - 4.0, hidden: Vec::new(), chosen, bar, pal } } /// The "»" button and its menu, when buttons did not fit. - fn more(&mut self, app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, bar: Rect) { + fn more(&mut self, app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter) { if self.hidden.is_empty() { return; } - let t = app.tokens; + let (t, pal, bar) = (app.tokens, self.pal, self.bar); let r = Rect::from_min_size(pos2(bar.max.x - 8.0 - Self::MORE_W, bar.center().y - 9.0), vec2(Self::MORE_W, 18.0)); let pid = egui::Id::unique(MORE_POPUP); let resp = ui.interact(r, pid.with("button"), Sense::click()).on_hover_text(crate::i18n::tr("More")); - p.rect_filled(r, 3.0, if resp.hovered() { t.hover } else { Color32::from_rgb(0x2c, 0x2c, 0x2c) }); - p.text(r.center(), Align2::CENTER_CENTER, "»", Tokens::ui(12.0), t.text); + p.rect_filled(r, 3.0, if resp.hovered() { t.hover } else { pal.button }); + p.text(r.center(), Align2::CENTER_CENTER, "»", Tokens::ui(12.0), pal.button_text); app.auto.add("timeline.graph.more", r, "More"); if resp.clicked() { let open = widgets::popup_is_open(ui, pid); @@ -273,18 +354,19 @@ fn text_button(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, b } let r = Rect::from_min_size(pos2(bar.x, y - 9.0), vec2(w, 18.0)); let resp = ui.interact(r, egui::Id::unique(("graph-btn", id)), Sense::click()).on_hover_text(shown_tip); + let pal = &bar.pal; p.rect_filled( r, 3.0, if on { - t.accent.gamma_multiply(0.55) + pal.button_on } else if resp.hovered() { t.hover } else { - Color32::from_rgb(0x2c, 0x2c, 0x2c) + pal.button }, ); - p.text(r.center(), Align2::CENTER_CENTER, shown, Tokens::ui(11.0), if on { Color32::WHITE } else { t.text }); + p.text(r.center(), Align2::CENTER_CENTER, shown, Tokens::ui(11.0), if on { pal.button_on_text } else { pal.button_text }); app.auto.add(&format!("timeline.graph.{id}"), r, tip); bar.x += w + 4.0; resp.clicked() @@ -299,9 +381,9 @@ pub(crate) struct GraphView<'a> { } /// Show Reference Graph: the other graph type, faint, scaled to the plot. -fn reference_graph(app: &EffectcraftApp, view: GraphView, plot: Rect, speed: bool, pc: &egui::Painter) { +fn reference_graph(app: &EffectcraftApp, view: GraphView, plot: Rect, speed: bool, pc: &egui::Painter, pal: &GraphPalette) { let GraphView { comp, ectx, tm } = view; - let other = curves(app, comp, ectx, tm, plot, !speed); + let other = curves(app, comp, ectx, tm, plot, !speed, pal); let (mut rlo, mut rhi) = (f64::INFINITY, f64::NEG_INFINITY); for c in &other { for (_, v) in &c.pts { @@ -407,16 +489,17 @@ fn fit_on_open(app: &mut EffectcraftApp, ctx: &egui::Context, cs: &[Curve], tm: pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, view: GraphView, area: Rect, actions: &mut Actions) { let GraphView { comp, ectx, tm } = view; let t = app.tokens; + let pal = GraphPalette::new(&t); let ctx = ui.ctx().clone(); let footer_h = 28.0; let plot = Rect::from_min_max(area.min + vec2(0.0, 6.0), pos2(area.max.x, area.max.y - footer_h)); let bar = Rect::from_min_max(pos2(area.min.x, area.max.y - footer_h), area.max); // Background first so keys and handles (added later) sit on top for hit-testing. let bg = ui.interact(plot, egui::Id::unique("graph-bg"), Sense::click_and_drag()); - p.rect_filled(area, 0.0, Color32::from_rgb(0x1d, 0x1d, 0x1d)); + p.rect_filled(area, 0.0, pal.bg); app.auto.add("timeline.graph", plot, &app.ui.timeline.graph_mode.clone()); let speed = speed_graph(app, comp); - let cs = curves(app, comp, ectx, tm, plot, speed); + let cs = curves(app, comp, ectx, tm, plot, speed, &pal); fit_on_open(app, &ctx, &cs, tm, plot, speed); // Vertical range: auto-zoom to the curves + handles, or the stored manual range. @@ -437,8 +520,8 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte let mut gv = (lo / step).ceil() * step; while gv <= hi { let y = ymap(gv); - pc.line_segment([pos2(plot.min.x, y), pos2(plot.max.x, y)], Stroke::new(1.0, Color32::from_white_alpha(12))); - pc.text(pos2(plot.min.x + 4.0, y - 1.0), Align2::LEFT_BOTTOM, fmt(gv, step), Tokens::ui(10.0), t.text_faint); + pc.line_segment([pos2(plot.min.x, y), pos2(plot.max.x, y)], Stroke::new(1.0, pal.grid)); + pc.text(pos2(plot.min.x + 4.0, y - 1.0), Align2::LEFT_BOTTOM, fmt(gv, step), Tokens::ui(10.0), pal.label); gv += step; } if cs.is_empty() { @@ -447,12 +530,12 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte Align2::CENTER_CENTER, crate::i18n::tr("Select animated properties to show them in the Graph Editor"), Tokens::ui(12.0), - t.text_faint, + pal.label, ); } if app.ui.timeline.graph_reference && !cs.is_empty() { - reference_graph(app, view, plot, speed, &pc); + reference_graph(app, view, plot, speed, &pc, &pal); } // Curves. for c in &cs { @@ -465,7 +548,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte .flat_map(|c| c.keys.iter().filter(|k| k.sel).map(move |k| super::graph_tools::SelKey { t: k.t, v: k.v, dim: c.dim, editable: c.editable })) .collect(); let all_key_times: Vec<(u64, usize, f64)> = cs.iter().flat_map(|c| c.keys.iter().map(move |k| (c.prop.uid, k.idx, k.t))).collect(); - super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, false, actions); + super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, false, &pal, actions); // Keys and handles. let mut key_screens: Vec<(serde_json::Value, Pos2)> = vec![]; for c in &cs { @@ -484,8 +567,8 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte // Handles (selected keys). for h in &k.handles { let hp = pos2(tm.x(h.t), ymap(h.v)); - pc.line_segment([kp, hp], Stroke::new(1.0, Color32::from_rgb(0xd8, 0xd8, 0x60))); - pc.circle_filled(hp, 3.5, Color32::from_rgb(0xd8, 0xd8, 0x60)); + pc.line_segment([kp, hp], Stroke::new(1.0, pal.handle)); + pc.circle_filled(hp, 3.5, pal.handle); let side = if h.out { "out" } else { "in" }; let hr = Rect::from_center_size(hp, vec2(10.0, 10.0)); let hresp = ui.interact(hr, egui::Id::unique(("ghandle", uid, d, k.idx, side)), Sense::drag()); @@ -539,7 +622,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte } } // The key itself. - let (col, edge) = key_colors(&t, c.color, k); + let (col, edge) = key_colors(&t, &pal, c.color, k); pc.rect_filled(Rect::from_center_size(kp, vec2(7.0, 7.0)), 0.0, col); pc.rect_stroke(Rect::from_center_size(kp, vec2(7.0, 7.0)), 0.0, Stroke::new(1.0, edge), StrokeKind::Outside); let kr = Rect::from_center_size(kp, vec2(11.0, 11.0)); @@ -597,25 +680,24 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte } } - super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, true, actions); + super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, true, &pal, actions); box_select(ui, &pc, &bg, &key_screens, t.accent, actions); wheel_zoom(app, ui, plot, (lo, hi)); - if button_bar(app, ui, p, comp, bar, speed, actions) { + if button_bar(app, ui, p, comp, ButtonBar::new(&ctx, bar, pal), speed, actions) { fit_selection(app, &cs, tm, plot, speed); } } /// The footer button bar: graph type, display toggles, fit, snap and keyframe interpolation. /// Returns whether Fit Selection was pressed. -fn button_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, comp: &Comp, bar: Rect, speed: bool, actions: &mut Actions) -> bool { +fn button_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, comp: &Comp, mut buttons: ButtonBar, speed: bool, actions: &mut Actions) -> bool { let t = app.tokens; - let ctx = ui.ctx().clone(); + let bar = buttons.bar; let mut fit_sel = false; p.rect_filled(bar, 0.0, t.panel_bg); p.line_segment([bar.left_top(), bar.right_top()], Stroke::new(1.0, t.separator)); let y = bar.center().y; - let mut buttons = ButtonBar::new(&ctx, bar); let tl = &app.ui.timeline; let (auto_type, show_sel, auto_on) = (tl.graph_mode == "auto", tl.graph_show_selected, tl.graph_auto_zoom); if text_button( @@ -706,7 +788,7 @@ fn button_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, co actions.push((cmd.into(), params)); } } - buttons.more(app, ui, p, bar); + buttons.more(app, ui, p); fit_sel } @@ -890,6 +972,76 @@ fn shift_axes(shift: bool, dx: f32, dy: f32) -> (bool, bool) { if shift { (dx.abs() >= dy.abs(), dy.abs() > dx.abs()) } else { (true, true) } } +#[cfg(test)] +mod theme_tests { + use super::{Color32, DIM_COLORS, DIM_COLORS_LIGHT, GraphPalette, PROP_COLORS, PROP_COLORS_LIGHT, min_contrast}; + use crate::theme::{ThemeKind, Tokens, contrast_ratio as contrast}; + + /// #3202: on every theme the Graph Editor's surfaces come from the theme, the bar buttons' + /// labels (lit and unlit) are readable on their fills, and the curves, keys, handles and the + /// transform box show on the plot. Before, the plot was a fixed #1d1d1d and the unlit buttons + /// a fixed #2c2c2c under the theme's text colour: Light's #222 text on them measured 1.1:1. + #[test] + fn the_graph_editor_follows_the_theme_and_stays_readable() { + let mut fills = vec![]; + for kind in [ThemeKind::Dark, ThemeKind::Darker, ThemeKind::Light] { + let t = Tokens::for_kind(kind); + let pal = GraphPalette::new(&t); + assert_eq!(pal.bg, t.tl_bg, "{kind:?}: the plot is the Timeline's time graph background"); + for (what, fg, bg, min) in [ + ("unlit button label", pal.button_text, pal.button, 4.5), + ("lit button label", pal.button_on_text, pal.button_on, 4.5), + ("hovered button label", pal.button_text, t.hover, 4.5), + ("grid value labels", pal.label, pal.bg, 4.5), + ("Bezier handles", pal.handle, pal.bg, 3.0), + ("unselected keys", pal.key, pal.bg, 3.0), + ("transform box", pal.box_line, pal.bg, 3.0), + ("transform box handles", pal.box_line, pal.box_fill, 3.0), + ("unlit button on the bar", pal.button, t.panel_bg, 1.05), + ("grid lines", pal.grid, pal.bg, 1.08), + ] { + let r = contrast(fg, bg); + assert!(r >= min, "{kind:?}: {what}: {r:.2}:1, needs {min}:1"); + } + for (i, c) in pal.dims.iter().chain(pal.props.iter()).enumerate() { + let r = contrast(*c, pal.bg); + assert!(r >= 3.0, "{kind:?}: curve colour {i} {c:?} on the plot: {r:.2}:1"); + } + fills.push((pal.bg, pal.button)); + } + assert_ne!(fills[0], fills[2], "Dark and Light draw the graph differently, not one fixed grey"); + // The dark curve colours would not do on the Light plot: that is why it has its own set. + let light = GraphPalette::new(&Tokens::for_kind(ThemeKind::Light)); + assert!(DIM_COLORS.iter().chain(PROP_COLORS.iter()).any(|c| contrast(*c, light.bg) < 3.0)); + } + + /// The brightness slider moves the plot (Dark lightened towards mid grey, Light darkened): + /// the plot still follows it, the curve set is whichever stands out more from that plot (not + /// the one the theme's name picks), and the lit button's label stays the more readable one. + #[test] + fn the_curve_set_follows_the_plot_under_the_brightness_slider() { + let bright: Vec = DIM_COLORS.iter().chain(PROP_COLORS.iter()).copied().collect(); + let dark: Vec = DIM_COLORS_LIGHT.iter().chain(PROP_COLORS_LIGHT.iter()).copied().collect(); + for kind in [ThemeKind::Dark, ThemeKind::Darker, ThemeKind::Light] { + for b in [-1.0, -0.5, 0.0, 0.5, 1.0] { + let t = Tokens::for_kind(kind).with_brightness(b); + let pal = GraphPalette::new(&t); + assert_eq!(pal.bg, t.tl_bg, "{kind:?} at {b}: the plot follows the slider"); + let shown: Vec = pal.dims.iter().chain(pal.props.iter()).copied().collect(); + let best = min_contrast(&bright, pal.bg).max(min_contrast(&dark, pal.bg)); + assert_eq!(min_contrast(&shown, pal.bg), best, "{kind:?} at {b}: the curves are the set that stands out more from {:?}", pal.bg); + let lit = contrast(pal.button_on_text, pal.button_on); + assert!(lit >= contrast(Color32::WHITE, pal.button_on).max(contrast(t.text, pal.button_on)), "{kind:?} at {b}: lit label {lit:.2}:1"); + } + } + // Light darkened all the way is a dark plot: it gets the bright curves, as Dark does. + let dimmed = GraphPalette::new(&Tokens::for_kind(ThemeKind::Light).with_brightness(-1.0)); + assert_eq!(dimmed.props, PROP_COLORS); + let lit = GraphPalette::new(&Tokens::for_kind(ThemeKind::Light)); + assert_eq!(lit.props, PROP_COLORS_LIGHT); + } +} + #[cfg(test)] mod tests { use super::shift_axes; diff --git a/crates/ui-egui/src/panels/graph_tools.rs b/crates/ui-egui/src/panels/graph_tools.rs index 7950d4a66..d611051a0 100644 --- a/crates/ui-egui/src/panels/graph_tools.rs +++ b/crates/ui-egui/src/panels/graph_tools.rs @@ -71,6 +71,7 @@ pub(crate) fn transform_box( vmap: &dyn Fn(f32) -> f64, speed: bool, handles: bool, + pal: &super::graph::GraphPalette, actions: &mut Actions, ) { if !app.ui.timeline.graph_transform_box { @@ -79,7 +80,7 @@ pub(crate) fn transform_box( let Some(b) = bounds(keys) else { return }; let r = Rect::from_min_max(pos2(tm.x(b[0]), ymap(b[3])), pos2(tm.x(b[2]), ymap(b[1]))).expand(6.0); let value_ok = !speed && keys.iter().all(|k| k.editable); - let col = Color32::from_rgb(0xd8, 0xd8, 0xd8); + let col = pal.box_line; let ctx = ui.ctx().clone(); let drag_id = egui::Id::unique("graph-dragging"); let hp = handle_points(r); @@ -116,7 +117,7 @@ pub(crate) fn transform_box( continue; } let hr = Rect::from_center_size(*h, vec2(7.0, 7.0)); - p.rect_filled(hr, 0.0, Color32::from_gray(0x20)); + p.rect_filled(hr, 0.0, pal.box_fill); p.rect_stroke(hr, 0.0, Stroke::new(1.0, col), StrokeKind::Middle); let resp = ui.interact(hr.expand(2.0), egui::Id::unique(("graph-box-handle", i)), Sense::drag()); app.auto.add(&format!("timeline.graph.transformBox.{i}"), hr, "Transform box handle"); diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index e67bd2d7d..e18d834ee 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -111,6 +111,21 @@ pub struct Tokens { pub gradients: bool, } +/// WCAG 2 relative luminance of an opaque sRGB colour (alpha is ignored). +pub fn relative_luminance(c: Color32) -> f32 { + let f = |v: u8| { + let v = f32::from(v) / 255.0; + if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } + }; + 0.2126 * f(c.r()) + 0.7152 * f(c.g()) + 0.0722 * f(c.b()) +} + +/// WCAG 2 contrast ratio of two opaque sRGB colours (1 to 21). +pub fn contrast_ratio(a: Color32, b: Color32) -> f32 { + let (x, y) = (relative_luminance(a), relative_luminance(b)); + (x.max(y) + 0.05) / (x.min(y) + 0.05) +} + /// A vertical gradient fill (`top` → `bottom`) over `rect`. pub fn gradient_rect(painter: &egui::Painter, rect: egui::Rect, top: Color32, bottom: Color32) { let mut m = egui::Mesh::default(); diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 422f0223e..d8cdc5cb3 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -46,6 +46,7 @@ mod ui_gradient_editor; mod ui_gradient_handles; mod ui_gradient_value; mod ui_graph_fit; +mod ui_graph_theme; mod ui_group_rename; mod ui_guide_layers; mod ui_info_tracker_a11y; diff --git a/crates/ui-egui/tests/it/ui_graph_theme.rs b/crates/ui-egui/tests/it/ui_graph_theme.rs new file mode 100644 index 000000000..49c6689a0 --- /dev/null +++ b/crates/ui-egui/tests/it/ui_graph_theme.rs @@ -0,0 +1,102 @@ +//! #3202: the Graph Editor follows the theme (egui_kittest, a real render). Its plot used to be a +//! fixed dark grey in every theme, and its bar buttons a fixed dark fill under the theme's text +//! colour, so in Light their labels (dark text) only showed while hovered. Now the plot takes the +//! Timeline's time graph background and every bar button's label is readable on its fill, lit or +//! not, in Dark and Light. `GRAPH_THEME_SNAPSHOT=/abs/dir` also saves the rendered window. + +use effectcraft_engine::Session; +use effectcraft_ui_egui::EffectcraftApp; +use effectcraft_ui_egui::theme::ThemeKind; +use egui::Color32; +use egui_kittest::Harness; +use serde_json::json; + +/// WCAG contrast ratio of two rendered pixels. +fn contrast(a: [u8; 4], b: [u8; 4]) -> f32 { + effectcraft_ui_egui::theme::contrast_ratio(Color32::from_rgb(a[0], a[1], a[2]), Color32::from_rgb(b[0], b[1], b[2])) +} + +fn close(got: [u8; 4], want: Color32) -> bool { + got[..3].iter().zip(want.to_array()).all(|(a, b)| a.abs_diff(b) <= 3) +} + +#[test] +fn the_graph_editor_follows_the_theme() { + let _one_at_a_time = crate::gpu_test::lock(); + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Theme", "width": 640, "height": 360, "duration": 4})).unwrap(); + s.execute("layer.newSolid", json!({"name": "Box", "color": "#204060", "width": 80, "height": 80})).unwrap(); + // An animated property on the selected layer, so the graph shows a curve and its keys. + let id = s.active_comp().unwrap().layers[0].id; + for (t, v) in [(0.5, 0.0), (2.0, 180.0), (3.5, 90.0)] { + s.execute("prop.addKey", json!({"layer": id.0, "path": "transform/rotation", "time": t, "value": v})).unwrap(); + } + s.execute("layer.select", json!({"layers": [id.0]})).unwrap(); + + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(move |_| EffectcraftApp::new(s)); + h.state_mut().ui.timeline.graph_editor = true; + h.run_steps(4); + let ctx = h.ctx.clone(); + + let mut plots = vec![]; + let mut failures = vec![]; + for (kind, name) in [(ThemeKind::Dark, "dark"), (ThemeKind::Light, "light")] { + h.state_mut().set_theme(&ctx, kind); + h.run_steps(4); + let tokens = h.state().tokens; + assert_eq!(tokens.kind, kind); + let plot = h.state().auto.find("timeline.graph").unwrap_or_else(|| panic!("the Graph Editor is shown ({name})")).rect; + let buttons: Vec<(&str, [f32; 4])> = ["valueGraph", "speedGraph"] + .iter() + .map(|id| (*id, h.state().auto.find(&format!("timeline.graph.{id}")).unwrap_or_else(|| panic!("{id} is on the bar ({name})")).rect)) + .collect(); + let image = h.render().expect("the window renders"); + if let Some(dir) = std::env::var_os("GRAPH_THEME_SNAPSHOT") { + image.save(std::path::Path::new(&dir).join(format!("graph-theme-{name}.png"))).unwrap(); + } + let scale = image.width() as f32 / 1600.0; + let at = |px: f32, py: f32| { + let (x, y) = (((px * scale) as u32).min(image.width() - 1), ((py * scale) as u32).min(image.height() - 1)); + image.get_pixel(x, y).0 + }; + + // The plot: the commonest colour down a column near its right edge (clear of the value + // labels on the left and of the scroll bar; grid lines and the curve cross it only at a + // sample or two). + let x = plot[0] + plot[2] - 20.0; + let mut column: Vec<[u8; 4]> = (0..24).map(|i| at(x, plot[1] + 8.0 + (plot[3] - 16.0) * i as f32 / 23.0)).collect(); + column.sort_unstable(); + let mut runs: Vec<([u8; 4], usize)> = vec![]; + for px in column { + match runs.last_mut() { + Some((c, n)) if *c == px => *n += 1, + _ => runs.push((px, 1)), + } + } + let bg = runs.iter().max_by_key(|(_, n)| *n).map(|(c, _)| *c).unwrap(); + if !close(bg, tokens.tl_bg) { + failures.push(format!("{name}: the plot is {bg:?}, the theme's time graph background is {:?}", tokens.tl_bg)); + } + plots.push(bg); + + // Each button (Value is lit, Speed is not): its fill left of the label, and the label's + // ink, the pixel across its middle rows that stands out most from the fill. Only the + // label's span is scanned (7 points of padding each side), not the button's feathered + // edges, which blend the fill with the bar behind it. + for (id, r) in buttons { + let mid = r[1] + r[3] / 2.0; + let fill = at(r[0] + 2.5, mid); + let ink = (-3..=3) + .flat_map(|dy| (3..(r[2] as i32 - 3)).map(move |dx| (r[0] + dx as f32, mid + dy as f32))) + .map(|(px, py)| at(px, py)) + .max_by(|a, b| contrast(*a, fill).total_cmp(&contrast(*b, fill))) + .unwrap(); + let c = contrast(ink, fill); + if c < 3.0 { + failures.push(format!("{name}: the {id} button's label {ink:?} on its fill {fill:?} is {c:.2}:1")); + } + } + } + assert!(failures.is_empty(), "{}", failures.join("\n")); + assert_ne!(plots[0], plots[1], "the plot looks different in Dark and in Light, so it is not a fixed grey"); +} From 795b019deb3dcafc329d29ce03250813840fc1f1 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 14:11:02 +0100 Subject: [PATCH 028/350] Timeline waveform: read keyframed Audio Levels at layer time The waveform scaled each column by Audio Levels evaluated at comp time, but keyframe times are layer time. On a layer with a start offset or a stretch the drawn level was shifted against what the mix plays. Read the levels at layer.layer_time(t), as the render evaluator does. Fixes #3224 --- crates/ui-egui/src/panels/waveform.rs | 79 ++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/panels/waveform.rs b/crates/ui-egui/src/panels/waveform.rs index 4e355f951..91c5a8458 100644 --- a/crates/ui-egui/src/panels/waveform.rs +++ b/crates/ui-egui/src/panels/waveform.rs @@ -76,11 +76,14 @@ pub fn summary(app: &EffectcraftApp, ctx: &egui::Context, item: ItemId) -> Optio None } -/// Left/right gain of the layer's Audio Levels (dB) at comp time `t`; unity without the property. -fn level_gains(levels: Option<&Property>, t: Tick) -> [f32; 2] { +/// Left/right gain of `layer`'s Audio Levels (`levels`, dB) at comp time `t`; unity without the +/// property. Keyframes are in layer time, so they are read at the layer time of `t` (start +/// offset and stretch), as the mix reads them. A reversed layer (negative stretch) is still +/// drawn, though the mix leaves it silent. +fn level_gains(layer: &Layer, levels: Option<&Property>, t: Tick) -> [f32; 2] { match levels { Some(p) => { - let [l, r] = p.value_at(t).as_vec2(); + let [l, r] = p.value_at(layer.layer_time(t)).as_vec2(); [db_to_gain(l), db_to_gain(r)] } None => [1.0, 1.0], @@ -114,7 +117,7 @@ pub(crate) fn draw(p: &Painter, rect: Rect, tm: &TMap, layer: &Layer, s: &Summar agg[2] = agg[2].min(b[2]); agg[3] = agg[3].max(b[3]); } - let gain = level_gains(levels, Tick::from_seconds_f64((ta + tb) * 0.5)); + let gain = level_gains(layer, levels, Tick::from_seconds_f64((ta + tb) * 0.5)); for c in 0..2 { let (lo, hi) = ((agg[c * 2] * gain[c]).clamp(-1.0, 1.0), (agg[c * 2 + 1] * gain[c]).clamp(-1.0, 1.0)); let (ya, yb) = (mid[c] - hi * half, mid[c] - lo * half); @@ -126,14 +129,76 @@ pub(crate) fn draw(p: &Painter, rect: Rect, tm: &TMap, layer: &Layer, s: &Summar #[cfg(test)] mod tests { use super::*; - use effectcraft_engine::project::Value; + use effectcraft_engine::color::Label; + use effectcraft_engine::project::{Comp, Footage, FootageKind, Keyframe, Value, build}; + use effectcraft_engine::render::{FootageSource, Image, audio::mix_comp}; + use effectcraft_engine::time::FrameRate; #[test] fn waveform_follows_audio_levels() { let p = Property::new(1, "levels", "Audio Levels", Value::Vec2([-6.0206, -96.0])); - let g = level_gains(Some(&p), Tick::ZERO); + let mut project = Project::default(); + let comp = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); + let l = build::layer(&mut project, &comp, "a", LayerSource::Null, (0, 0), None); + let g = level_gains(&l, Some(&p), Tick::ZERO); assert!((g[0] - 0.5).abs() < 1e-3, "{}", g[0]); assert_eq!(g[1], 0.0); - assert_eq!(level_gains(None, Tick::ZERO), [1.0, 1.0]); + assert_eq!(level_gains(&l, None, Tick::ZERO), [1.0, 1.0]); + } + + /// Constant 0.5 audio on both channels, at any source time. + struct Half; + impl FootageSource for Half { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, _: u32) -> Option> { + Some(vec![0.5; frames * 2]) + } + } + + /// #3224: keyframed Audio Levels scale the drawn waveform at the layer time the mix reads + /// them, so on a layer that starts later (or is stretched) a column's gain is the gain the + /// rendered audio has at that comp time. + #[test] + fn keyed_levels_scale_the_waveform_as_the_mix_does() { + let mut p = Project::default(); + let f = Footage { + path: "x.wav".into(), + kind: FootageKind::Audio, + has_audio: true, + frame_rate: FrameRate::new(25, 1), + duration: Tick::from_seconds_f64(10.0), + ..Default::default() + }; + let fid = p.add_item("x.wav", Label::SeaFoam, None, ItemKind::Footage(f)); + let base = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(10.0)); + let mut l = build::layer(&mut p, &base, "a", LayerSource::Footage { item: fid }, (0, 0), None); + // -6 dB at layer time 0 s, 0 dB at layer time 1 s. + let Some(levels) = l.props.sub_mut("audio").and_then(|g| g.get_mut("levels")) else { panic!("no Audio Levels") }; + levels.keys = vec![Keyframe::new(Tick::ZERO, Value::Vec2([-6.0206, -6.0206])), Keyframe::new(Tick::from_seconds_f64(1.0), Value::Vec2([0.0, 0.0]))]; + for stretch in [100.0, 200.0] { + let mut l = l.clone(); + l.start_time = Tick::from_seconds_f64(2.0); + l.stretch = stretch; + l.in_point = l.start_time; + l.out_point = Tick::from_seconds_f64(9.0); + let mut comp = base.clone(); + comp.layers = vec![l.clone()].into(); + let cid = p.add_item("C", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let levels = l.props.sub("audio").and_then(|g| g.get("levels")); + // The layer's start, a quarter and half of its ramp, its end, and after it. + for s in [2.0, 2.25, 2.5, 3.0, 4.0, 6.0] { + let t = Tick::from_seconds_f64(s); + let drawn = level_gains(&l, levels, t); + let mixed = mix_comp(&p, &Half, None, cid, t, 1, 1000); + for c in 0..2 { + let heard = mixed.get(c).copied().unwrap_or(f32::NAN) / 0.5; + assert!((drawn[c] - heard).abs() < 2e-3, "stretch {stretch}, {s} s, channel {c}: drawn {} heard {heard}", drawn[c]); + } + } + // At the layer's start (layer time 0) it is the -6 dB key, not the 0 dB one. + assert!((level_gains(&l, levels, Tick::from_seconds_f64(2.0))[0] - 0.5).abs() < 1e-3); + } } } From cdbb884f28e29221e910654ca641b43b18887f81 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 14:25:32 +0100 Subject: [PATCH 029/350] Scripting: expressionError reports evaluation errors, as After Effects Property.expressionError only compiled the expression, so one that compiles but throws or yields a non-finite number read as "" while expr.errors reported it. It now evaluates the property's expression at the comp's current time through the same per-property check expr.errors uses (expr_tools::property_error), without rendering anything. Without an evaluator, an enabled expression falls back to the syntax check. Fixes #3219 --- crates/engine/src/cmd/expr_tools.rs | 24 ++++++++++++-------- crates/script/src/model.rs | 23 +++++++++++++------ crates/script/src/prelude.js | 4 ++-- crates/script/src/tests.rs | 35 +++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/cmd/expr_tools.rs b/crates/engine/src/cmd/expr_tools.rs index cb39077af..e55dc7b20 100644 --- a/crates/engine/src/cmd/expr_tools.rs +++ b/crates/engine/src/cmd/expr_tools.rs @@ -65,15 +65,7 @@ pub fn errors_in(ctx: &EvalCtx, check: Option std::result::Result<() if !keep_going() { return None; } - let msg = if e.enabled { - match &ctx.expr { - Some(h) => h.eval(ctx, l, p, &p.value_at(l.layer_time(ctx.time))).err(), - None => None, - } - } else { - check.and_then(|check| check(&e.text).err()) - }; - if let Some(message) = msg { + if let Some(message) = property_error(ctx, check, l, p) { out.push(error_of(ctx.comp_id, i, l, p, message, !e.enabled)); } } @@ -81,6 +73,20 @@ pub fn errors_in(ctx: &EvalCtx, check: Option std::result::Result<() keep_going().then_some(out) } +/// Why property `p` of layer `l` fails at `ctx.time` (comp time), as `expr.errors` reports it and +/// scripts read it as `Property.expressionError`: the evaluation error (an exception or a +/// non-finite result) of an enabled expression, or the syntax error that disabled one. `None` +/// without an expression or when it evaluates. Evaluates this one expression; renders nothing. +/// Without an evaluator an enabled expression gets the syntax check, so a broken one (from a +/// project file) still reports why. +pub fn property_error(ctx: &EvalCtx, check: Option std::result::Result<(), String>>, l: &Layer, p: &Property) -> Option { + let e = p.expr.as_ref().filter(|e| !e.text.trim().is_empty())?; + match ctx.expr { + Some(h) if e.enabled => h.eval(ctx, l, p, &p.value_at(l.layer_time(ctx.time))).err(), + _ => check.and_then(|check| check(&e.text).err()), + } +} + fn error_of(cid: ItemId, i: usize, l: &Layer, p: &Property, message: String, disabled: bool) -> ExprError { ExprError { comp: cid.0, diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index e662c1cdc..1140820c6 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -51,13 +51,7 @@ pub fn query(s: &mut Session, kind: &str, a: &J) -> R { let i = u(a, "index").unwrap_or(0) as usize; Ok(effectcraft_engine::command_specs().get(i.wrapping_sub(1)).map(|c| json!(c.id)).unwrap_or(J::Null)) } - "exprError" => { - let code = a.get("code").and_then(J::as_str).unwrap_or(""); - Ok(match s.expr_check { - Some(check) => json!(check(code).err().unwrap_or_default()), - None => json!(""), - }) - } + "exprError" => expr_error(s, a), "labels" => Ok(json!(Label::ALL.iter().map(|l| l.name()).collect::>())), "effects" => Ok(json!( effectcraft_engine::effects::all() @@ -529,6 +523,21 @@ fn value(s: &Session, a: &J) -> R { Ok(js_value(c, &v)) } +/// `Property.expressionError`: why the property's expression fails at the comp's current time +/// (where After Effects evaluates it; the prelude sends no `time`, a direct query may give one +/// in comp seconds). The message is the one `expr.errors` gives: an evaluation error (an +/// exception or a non-finite result) or the syntax error that disabled it; `""` when it +/// evaluates (#3219). +fn expr_error(s: &Session, a: &J) -> R { + let (cid, c, l) = layer_of(s, a)?; + let uid = u(a, "uid").ok_or("missing uid")?; + let p = l.props.find(uid).ok_or("the property no longer exists (it was deleted)")?; + let t = f(a, "time").map(Tick::from_seconds_f64).unwrap_or_else(|| s.time_of(cid)); + // With the footage source, like `value`: `sampleImage` must not fail for want of it (#1000). + let ctx = EvalCtx { footage: Some(s.footage.as_ref()), expr: s.expr.as_deref(), ..EvalCtx::new(&s.project, cid, c, t) }; + Ok(json!(effectcraft_engine::commands::expr_tools::property_error(&ctx, s.expr_check, l, p).unwrap_or_default())) +} + fn interp(i: Interp) -> &'static str { match i { Interp::Linear => "LINEAR", diff --git a/crates/script/src/prelude.js b/crates/script/src/prelude.js index b9108b2a1..a25a37a4b 100644 --- a/crates/script/src/prelude.js +++ b/crates/script/src/prelude.js @@ -799,8 +799,8 @@ Property.prototype.property = function () { return null; }; __call("prop.setExpression", this.__ref({ enabled: !!v })); }); def("expressionError", function () { - var e = this.expression; - return e ? __get("exprError", { code: e }) : ""; + // Evaluated at the comp's current time, so a runtime error or a non-finite result shows too. + return this.expression ? __get("exprError", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid }) : ""; }); def("canSetExpression", function () { return !!this.__info().canSetExpression; }); def("canVaryOverTime", function () { return !!this.__info().canVaryOverTime; }); diff --git a/crates/script/src/tests.rs b/crates/script/src/tests.rs index 9e934b555..44b561d5d 100644 --- a/crates/script/src/tests.rs +++ b/crates/script/src/tests.rs @@ -1252,6 +1252,41 @@ fn sample_image_reads_footage_pixels_in_scripts() { assert_eq!(out.result, json!([0, 1, 0, 1, ""]), "green footage sampled, not transparent black"); } +/// `expressionError` reports what evaluating the expression at the comp's current time does, as +/// After Effects: a non-finite result and an exception, not only a syntax error (#3219). +#[test] +fn expression_error_reports_evaluation_errors() { + let mut s = session(); + let out = ok( + &mut s, + r#" + var c = app.project.items.addComp("E", 320, 240, 1, 5, 30); + var p = c.layers.addSolid([1, 1, 1], "S", 320, 240, 1).transform.opacity; + p.expression = "Math.sqrt(-1)"; + var nan = p.expressionError; + p.expression = 'throw new Error("boom")'; + var thrown = p.expressionError; + p.expression = "value"; + var fine = p.expressionError; + p.expression = "thisLayer.nope("; + var syntax = p.expressionError; + p.expression = "time > 1 ? Math.sqrt(-1) : value"; + c.time = 0; + var early = p.expressionError; + c.time = 2; + var late = p.expressionError; + [nan, thrown, fine, syntax.length > 0, early, late]; + "#, + ); + let v = out.result; + assert!(v[0].as_str().is_some_and(|e| e.contains("not a finite number")), "{v}"); + assert!(v[1].as_str().is_some_and(|e| e.contains("boom")), "{v}"); + assert_eq!(v[2], json!(""), "{v}"); + assert_eq!(v[3], json!(true), "a syntax error is still reported: {v}"); + assert_eq!(v[4], json!(""), "evaluated at the current time (0), where it is fine: {v}"); + assert!(v[5].as_str().is_some_and(|e| e.contains("not a finite number")), "evaluated at the current time (2): {v}"); +} + #[test] fn binary_file_io_preserves_all_byte_values() { let dir = std::env::temp_dir().join(format!("ec-script-binary-{}", std::process::id())); From d733a699fcfcf3d90811a96f1626f9e857746552 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 13:51:32 +0100 Subject: [PATCH 030/350] Viewer snapping: a point beats a line pair through it unless the pair is clearly nearer (#2681) Moving a circle so its edge point met another circle's point snapped at 200 % but missed at 100 %. A layer's point lies on its own edge line and centre row, so a vertical and a horizontal line through it tie with it, and the point only won that tie by 1e-9. Any other line a little nearer on one axis (the comp's centre row, a guide) then made a pair nearer than the point, taking x from the edge and y from that line and leaving the moved circle off the point. At 100 % the tolerance (8 screen points) covers more comp pixels and reaches those lines; zoomed in they were out of reach. Before #2855 a single nearer line was enough, which is how it was reported on 0.18.0. snap() now lets a line pair with a line through the point beat that point only when the pair is nearer by POINT_BIAS of the tolerance (2 of the 8 screen points at any zoom), in place of the exact tie. A pair whose lines both miss the point gets no margin, so a guide crossing right beside a layer's point stays reachable; two clearly nearer lines still win, as #2855 intended. Tests: two Ellipse-tool circles, parametric and converted to Bezier paths, at 1x, 2x and 4x on device scales 1, 1.25 and 1.5, dropped just off the point and nearer the comp's centre row than the circle's own row, with and without snapping to a guide between them: the moved point lands exactly on the other's, a single point hit (a path point for the Bezier circles). The margin: a pair through the point only a little nearer leaves the point, a clearly nearer one wins; a guide crossing 1 px beside a point is reached next to the crossing. Moving a layer snaps the feature nearest where it was grabbed, as before (the snap handle, as in After Effects): grab the circle near its edge point to snap that point; grabbed in the middle, its centre snaps. Fixes #2681 --- crates/engine/src/tests/viewer.rs | 142 +++++++++++++++++++++++++++++- crates/engine/src/viewer.rs | 27 +++++- 2 files changed, 164 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index 31197b2a8..d2c2180c1 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -8,7 +8,9 @@ use effectcraft_time::Tick; use serde_json::json; use crate::Session; -use crate::viewer::{Channel, DisplayPass, FrameEncoding, PRI_GRID, PRI_GUIDE, PRI_LAYER, PRI_VERTEX, PassCurve, SnapKind, SnapSource, SnapTarget, snap}; +use crate::viewer::{ + Channel, DisplayPass, FrameEncoding, PRI_COMP, PRI_GRID, PRI_GUIDE, PRI_LAYER, PRI_VERTEX, PassCurve, SnapKind, SnapSource, SnapTarget, snap, +}; fn comp() -> Session { let mut s = Session::default(); @@ -84,6 +86,46 @@ fn snap_point_beats_one_line_and_loses_only_to_two_nearer_lines() { assert_eq!(s.hits, [v, h]); } +/// #2681: a pair of lines, one of them through the point (the layer's own edge line), beats the +/// point only when it is clearly nearer (by `POINT_BIAS` of the tolerance, 2 of 8 here); a pair +/// only a little nearer leaves the feature on the point. +#[test] +fn a_line_pair_through_a_point_beats_it_only_by_a_clear_margin() { + use crate::viewer::POINT_BIAS; + assert_eq!(8.0 * POINT_BIAS, 2.0); + let src = SnapSource::Layer(LayerId(1)); + let edge = SnapTarget::vline(100.0, PRI_LAYER, src); + let row = SnapTarget::hline(51.5, PRI_COMP, SnapSource::Comp); + // The point is 5 away; the pair (3 and ½) about 3.04, under 2 nearer: the point. + let pt = SnapTarget::point([100.0, 47.0], PRI_LAYER, src); + let s = snap(&[[103.0, 51.0]], &[edge, row, pt], 8.0).unwrap(); + assert_eq!(s.hits, [pt]); + assert_eq!(s.at, [100.0, 47.0]); + // The point about 6.7 away, the same pair more than 2 nearer: the pair. + let far = SnapTarget::point([100.0, 45.0], PRI_LAYER, src); + let s = snap(&[[103.0, 51.0]], &[edge, row, far], 8.0).unwrap(); + assert_eq!(s.hits, [edge, row]); + assert_eq!(s.at, [100.0, 51.5]); +} + +/// #2681: two guides crossing 1 px beside a layer's point are reachable right next to it: a pair +/// of lines that both miss the point gets no margin, only the nearer one wins. +#[test] +fn a_guide_intersection_beside_a_point_stays_reachable() { + let src = SnapSource::Layer(LayerId(1)); + let pt = SnapTarget::point([100.0, 50.0], PRI_LAYER, src); + let own = [SnapTarget::vline(100.0, PRI_LAYER, src), SnapTarget::hline(50.0, PRI_LAYER, src)]; + let guides = [SnapTarget::vline(101.0, PRI_GUIDE, SnapSource::Guide), SnapTarget::hline(51.0, PRI_GUIDE, SnapSource::Guide)]; + let t = [own[0], own[1], guides[0], guides[1], pt]; + // Nearer the intersection (about 0.71) than the point (about 2.12): the intersection. + let s = snap(&[[101.5, 51.5]], &t, 8.0).unwrap(); + assert_eq!(s.at, [101.0, 51.0]); + assert_eq!(s.hits, guides); + // Nearer the point: the point. + let s = snap(&[[100.3, 50.3]], &t, 8.0).unwrap(); + assert_eq!(s.hits, [pt]); +} + /// #2855: an unrotated layer's centre lines follow its edges' span rule, reach the outer comp /// through a collapsed precomp layer, and a rotated layer keeps its points only. #[test] @@ -116,6 +158,104 @@ fn layer_centre_lines_follow_the_edges() { assert_eq!(lines(&at(&s, off), pre), (None, None)); } +/// #2681: two Ellipse-tool circles, one moved so its left point meets the other's right point. +/// The point must land exactly on the other circle's point at 100 % as at 200 % and 400 %, on +/// HiDPI displays too. The tolerance is the viewer's (8 screen points over the zoom in points per +/// comp pixel), so at 100 % it reaches the comp's centre row 6 px below the circles and a guide +/// 3 px below them. Neither may pull the circle off the point: the other circle's own edge line +/// and row pass through its point, so a pair of lines made with that row or guide is only a +/// little nearer than the point (the row or guide a few pixels nearer on y), and a point wins +/// unless a pair is clearly nearer (`POINT_BIAS`). Before #2855 one nearer line was enough, and +/// even after it any pair nearer by a hair; zoomed in the row and guide were out of reach, so +/// the snap looked right there and not at 100 %. +#[test] +fn circles_snap_point_to_point_at_every_zoom() { + circles_snap_point_to_point(false); +} + +/// #2681 with the circles converted to Bezier paths (Convert To Bezier Path): the dragged path +/// point lands on the other circle's path point (a vertex target) the same way. +#[test] +fn bezier_circles_snap_path_point_to_path_point_at_every_zoom() { + circles_snap_point_to_point(true); +} + +/// The #2681 drops: parametric circles (their edge midpoints snap) or, with `bezier`, the same +/// circles as Bezier paths (their path points snap). +fn circles_snap_point_to_point(bezier: bool) { + use crate::viewer::{SnapOptions, layer_features, layer_vertices, targets}; + let mut s = comp(); + // Circle A: 100 px, centred at (200, 174), its right point at (250, 174); the comp's centre + // row is y = 180, a horizontal guide at y = 177. + s.execute("view.addGuide", json!({"orientation": "horizontal", "position": 177})).unwrap(); + let a = s.execute("shape.newShape", json!({"kind": "ellipse", "size": [100, 100], "position": [200, 174]})).unwrap()["layer"].as_u64().unwrap(); + s.execute("edit.deselectAll", json!({})).unwrap(); + let b = s.execute("shape.newShape", json!({"kind": "ellipse", "size": [100, 100], "position": [400, 300]})).unwrap()["layer"].as_u64().unwrap(); + assert_ne!(a, b, "two shape layers"); + if bezier { + for l in [a, b] { + fn ellipse(g: &effectcraft_project::PropGroup) -> Option { + if g.match_id == "ellipse" { + return Some(g.uid); + } + g.groups().find_map(ellipse) + } + let uid = ellipse(layer(&s, l).props.sub("contents").unwrap()).unwrap(); + s.state.selected_props = vec![(LayerId(l), uid)]; + s.execute("path.convertToBezier", json!({})).unwrap(); + } + } + let cid = s.active_comp_id().unwrap(); + // The circle's right (`right`) or left point: an edge midpoint of its box (features: + // corners, edge midpoints top, right, bottom, left, centre, anchor point), or a path point. + let point = |s: &Session, id: u64, right: bool| { + let comp = s.project.comp(cid).unwrap().clone(); + let ctx = effectcraft_render::EvalCtx::new(&s.project, cid, &comp, Tick::ZERO); + let l = comp.layer(LayerId(id)).unwrap(); + if !bezier { + return layer_features(&ctx, l)[if right { 5 } else { 7 }]; + } + let v = layer_vertices(&ctx, l); + assert!(v.len() >= 4, "a Bezier circle: {v:?}"); + let x = |p: &&[f64; 2]| p[0]; + *(if right { v.iter().max_by(|p, q| x(p).total_cmp(&x(q))) } else { v.iter().min_by(|p, q| x(p).total_cmp(&x(q))) }).unwrap() + }; + let near = |p: [f64; 2], q: [f64; 2], e: f64| (p[0] - q[0]).abs() < e && (p[1] - q[1]).abs() < e; + let right_of_a = point(&s, a, true); + assert!(near(right_of_a, [250.0, 174.0], 1e-6), "{right_of_a:?}"); + let left_of_b = point(&s, b, false); + assert!(near(left_of_b, [350.0, 300.0], 1e-6), "{left_of_b:?}"); + let to_left = [left_of_b[0] - 400.0, left_of_b[1] - 300.0]; + let source = if bezier { SnapSource::Vertex(LayerId(a)) } else { SnapSource::Layer(LayerId(a)) }; + // B's left point dropped right of and below A's right point (screen points): just off it, + // and nearer the comp's centre row (at 100 %) than A's own row. With and without snapping to + // the guide. + for (dx, dy) in [(3.0, 1.0), (3.0, 3.5)] { + for guides in [false, true] { + for ppp in [1.0, 1.25, 1.5] { + for magnification in [1.0, 2.0, 4.0] { + let case = format!("bezier {bezier}, drop ({dx}, {dy}) pt, guides {guides}, {magnification}x, ppp {ppp}"); + // The viewer's zoom (points per comp pixel) and snap tolerance (`SNAP_PX / zoom`). + let zoom: f64 = magnification / ppp; + let tol = 8.0 / zoom; + let want = [right_of_a[0] + dx / zoom, right_of_a[1] + dy / zoom]; + s.execute("prop.set", json!({"layer": b, "path": "transform/position", "value": [want[0] - to_left[0], want[1] - to_left[1], 0]})).unwrap(); + let src = point(&s, b, false); + assert!(near(src, want, 1e-9), "{case}: {src:?} {want:?}"); + let comp = s.project.comp(cid).unwrap().clone(); + let ctx = effectcraft_render::EvalCtx::new(&s.project, cid, &comp, Tick::ZERO); + let t = targets(&ctx, &[LayerId(b)], SnapOptions { layers: true, guides, ..Default::default() }); + let hit = snap(&[src], &t, tol).unwrap_or_else(|| panic!("{case}: nothing snapped")); + assert!(near(hit.at, right_of_a, 1e-9), "{case}: B's left point lands at {:?}, not on A's right point {right_of_a:?}", hit.at); + assert_eq!(hit.hits.len(), 1, "{case}: one point, not two lines: {:?}", hit.hits); + assert_eq!(hit.hits[0].kind, SnapKind::Point, "{case}"); + assert_eq!(hit.hits[0].source, source, "{case}"); + } + } + } + } +} + #[test] fn snap_targets_cover_layers_comp_guides_and_grid() { let mut s = comp(); diff --git a/crates/engine/src/viewer.rs b/crates/engine/src/viewer.rs index f8e4c7740..5b4064a23 100644 --- a/crates/engine/src/viewer.rs +++ b/crates/engine/src/viewer.rs @@ -128,11 +128,23 @@ fn better(a: (f64, u8), b: (f64, u8)) -> bool { if (a.0 - b.0).abs() < 1e-6 { a.1 < b.1 } else { a.0 < b.0 } } +/// How much nearer (as a share of the tolerance: 2 of the viewer's 8 screen points, whatever the +/// zoom) a vertical and a horizontal line together must be than a point target to win over it +/// when one of the two lines runs through the point. A layer's point lies on that layer's own +/// edge and centre lines, so such a pair is only as near as the point itself plus whatever a +/// foreign line (the comp's centre row, a guide) gains on the other axis; without the margin that +/// pair took the foreign axis and left the feature just off the point, more often the wider the +/// tolerance is in comp pixels, i.e. at 100 % rather than zoomed in (#2681). A pair of lines that +/// both miss the point (a guide crossing beside a layer's point) gets no margin, so such an +/// intersection stays reachable right next to the point. +pub const POINT_BIAS: f64 = 0.25; + /// Snap feature points `sources` (comp pixels) to the nearest target within `tol` comp pixels. /// A point target snaps both axes and wins unless a vertical and a horizontal line both resolve -/// and together lie nearer than it (one line alone leaves the other axis free, so it never beats -/// a point: a layer's edge midpoint lies on its edge line); otherwise the nearest vertical and -/// horizontal lines snap x and y independently. Ties go to the lower priority value. +/// and together lie nearer than it, by more than [`POINT_BIAS`] of `tol` when one of them runs +/// through the point (one line alone leaves the other axis free, so it never beats a point: a +/// layer's edge midpoint lies on its edge line); otherwise the nearest vertical and horizontal +/// lines snap x and y independently. Ties go to the lower priority value. pub fn snap(sources: &[[f64; 2]], targets: &[SnapTarget], tol: f64) -> Option { let mut best_pt: Option<((f64, u8), [f64; 2], SnapTarget)> = None; let mut best_x: Option<((f64, u8), [f64; 2], SnapTarget)> = None; @@ -155,8 +167,15 @@ pub fn snap(sources: &[[f64; 2]], targets: &[SnapTarget], tol: f64) -> Option, at: usize| { + best_pt.zip(line).is_some_and(|((_, _, p), (_, _, l))| (l.pos[at] - p.pos[at]).abs() < 1e-6) + }; + // (At least a hair, so an exact tie goes to the point.) + let bias = if through(best_x, 0) || through(best_y, 1) { (tol * POINT_BIAS).max(1e-9) } else { 1e-9 }; if let Some((k, s, t)) = best_pt - && k.0 <= both_lines + 1e-9 + && k.0 <= both_lines + bias { return Some(Snap { delta: [t.pos[0] - s[0], t.pos[1] - s[1]], hits: vec![t], at: t.pos }); } From 0cb5bfefb5874247898751387f515c8ce74ffd69 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:06:13 +0100 Subject: [PATCH 031/350] Number fields: typed arithmetic (960/2, (3+4)*2) and a leading *, / or + applied to the value (#3195) Number fields read + - * /, brackets and signs through a small bounded parser (crates/ui-egui/src/num_math.rs): no code is run, length and nesting are capped, every step must stay finite and a division by zero keeps the old value with no undo step. Hot-text values (Timeline, Effect Controls, Properties and the other panels) also take a leading *, / or + applied to the value shown, as the reporter describes for After Effects (not confirmed in public docs). A leading - stays a negative number. Hot text used to read +50 as 50; it now adds 50. On an angle the step goes from the signed remainder (+10 on 0x-30 is 0x-20). With several layers selected each layer takes the step from its own value: + through the offset path, * and / through a new prop.set factor. Typed results in whole-number hot text round instead of truncating. Dialog fields (the shared drag_value wrapper, Essential Graphics, Gradient Editor, colour picker) re-read their text per keystroke, so they take absolute arithmetic only. Fixes #3195 --- crates/engine/src/cmd/prop.rs | 29 +- crates/engine/src/tests/timeline.rs | 23 + crates/ui-egui/src/color_picker.rs | 7 +- crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/num_math.rs | 394 ++++++++++++++++++ .../src/panels/effect_controls/rows.rs | 9 +- crates/ui-egui/src/panels/essential.rs | 8 +- crates/ui-egui/src/panels/fx_widgets.rs | 32 +- crates/ui-egui/src/panels/gradient_editor.rs | 18 +- crates/ui-egui/src/panels/properties.rs | 9 +- crates/ui-egui/src/panels/timeline.rs | 9 +- crates/ui-egui/src/widgets.rs | 62 ++- crates/ui-egui/tests/it/ui_angles.rs | 15 + crates/ui-egui/tests/it/ui_inline_edit.rs | 78 ++++ 14 files changed, 658 insertions(+), 36 deletions(-) create mode 100644 crates/ui-egui/src/num_math.rs diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index b4432dd2f..dc043d78f 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -371,20 +371,35 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { }; let follow: Vec = p.get("follow").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_u64).filter_map(|d| usize::try_from(d).ok()).collect(); + // A typed `*2` or `/2` (#3195): each layer's own value times it, not the edited layer's result. + let factor = p.get("factor").and_then(Value::as_f64).filter(|k| k.is_finite() && delta.is_none()); // Each layer's own value where `set` writes it (a `time` other than the CTI keys there). let typed = |s: &Session, q: &mut Value, olid: LayerId, ouid: Uid| { - if let (Some(dims), Some(n)) = (&dims, &new) - && let Some(mut c) = components_at(s, cid, olid, ouid, snapped_key_time(s, cid, olid, q, "prop.set").ok().flatten()) + let at = snapped_key_time(s, cid, olid, q, "prop.set").ok().flatten(); + if let (true, Some(k)) = (scalar, factor) + && let Some(own) = components_at(s, cid, olid, ouid, at).and_then(|c| c.first().copied()) + && (own * k).is_finite() { - let change = dims.first().and_then(|&d| Some(n.get(d)? - c.get(d)?)).unwrap_or(0.0); + q["value"] = json!(own * k); + } + if let (Some(dims), Some(n)) = (&dims, &new) + && let Some(mut c) = components_at(s, cid, olid, ouid, at) + { + // The typed components' new values on this layer. + let typed_value = |c: &[f64], d: usize| match factor { + Some(k) => c.get(d).map(|v| v * k).filter(|v| v.is_finite()), + None => n.get(d).copied(), + }; + let change = dims.first().and_then(|&d| Some(typed_value(&c, d)? - c.get(d)?)).unwrap_or(0.0); + let values: Vec> = dims.iter().map(|&d| typed_value(&c, d)).collect(); for &d in &follow { if let Some(slot) = c.get_mut(d) { *slot += change; } } - for &d in dims { - if let (Some(slot), Some(v)) = (c.get_mut(d), n.get(d)) { - *slot = *v; + for (&d, v) in dims.iter().zip(values) { + if let (Some(slot), Some(v)) = (c.get_mut(d), v) { + *slot = v; } } q["value"] = json!(c); @@ -1421,7 +1436,7 @@ pub fn specs() -> Vec { "Set Property Value", [], None, - "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes)}", + "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes), factor? (with selected, a typed `*` or `/`: the other layers take their own value times it, in the dims components of a vector)}", has_layers, set_selected ), diff --git a/crates/engine/src/tests/timeline.rs b/crates/engine/src/tests/timeline.rs index 55475eef3..6ea4d1336 100644 --- a/crates/engine/src/tests/timeline.rs +++ b/crates/engine/src/tests/timeline.rs @@ -1531,6 +1531,29 @@ fn a_typed_component_applies_only_that_component_to_the_selected_layers() { assert_eq!(pos(&s, c), vec![1.0, 2.0, 3.0]); } +/// #3195: a typed `*2` or `/2` with several layers selected scales each layer's own value (in +/// the typed components of a vector), not the edited layer's result. +#[test] +fn a_typed_factor_scales_each_selected_layers_own_value() { + let (mut s, a) = setup(); + let b = s.execute("layer.newSolid", json!({"color": "#ff0000", "width": 50, "height": 50})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.set", json!({"layer": a, "path": "transform/opacity", "value": 40.0})).unwrap(); + s.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 30.0})).unwrap(); + s.execute("prop.set", json!({"layer": a, "path": "transform/position", "value": [10.0, 20.0]})).unwrap(); + s.execute("prop.set", json!({"layer": b, "path": "transform/position", "value": [100.0, 50.0]})).unwrap(); + s.execute("layer.select", json!({"layers": [a, b]})).unwrap(); + let opacity = prop(&s, a, "transform/opacity").uid; + let undo = s.history.undo.len(); + s.execute_checked("prop.set", json!({"layer": a, "prop": opacity, "value": 80.0, "selected": true, "factor": 2.0})).unwrap(); + assert_eq!(prop(&s, a, "transform/opacity").value.as_f64(), 80.0); + assert_eq!(prop(&s, b, "transform/opacity").value.as_f64(), 60.0, "B doubles its own 30"); + assert_eq!(s.history.undo.len(), undo + 1, "one undo step"); + let position = prop(&s, a, "transform/position").uid; + s.execute_checked("prop.set", json!({"layer": a, "prop": position, "value": [5.0, 20.0], "selected": true, "dims": [0], "factor": 0.5})).unwrap(); + assert_eq!(prop(&s, a, "transform/position").value.components()[..2], [5.0, 20.0]); + assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [50.0, 50.0], "B halves its own X, keeps its Y"); +} + /// #2485: linked Audio Levels typed with several layers selected: each takes the typed Left, and /// its Right moves by its own change, so every layer keeps its own stereo balance. #[test] diff --git a/crates/ui-egui/src/color_picker.rs b/crates/ui-egui/src/color_picker.rs index d434538e6..2b9982cec 100644 --- a/crates/ui-egui/src/color_picker.rs +++ b/crates/ui-egui/src/color_picker.rs @@ -447,7 +447,12 @@ fn color_picker(ui: &mut Ui, id: egui::Id, c: &mut [f32; 3], original: [f32; 3]) if ch == Chan::H && v >= 360.0 { v = 0.0; } - let drag = egui::DragValue::new(&mut v).range(0.0..=scale).fixed_decimals(0).suffix(suffix).speed(scale / 255.0); + let drag = egui::DragValue::new(&mut v) + .range(0.0..=scale) + .fixed_decimals(0) + .suffix(suffix) + .speed(scale / 255.0) + .custom_parser(|text| crate::num_math::calc(text, None)); if ui.add_sized(vec2(56.0, 18.0), drag).labelled_by(radio.id).changed() { pick = pick.set(ch, v.round() / scale); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 795a01e1e..0dc02f7d1 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -36,6 +36,7 @@ pub mod icons; pub mod menu_keys; pub mod menus; pub mod native_menu; +pub mod num_math; pub mod panels; mod pointer_wrap; pub mod prefs_live; diff --git a/crates/ui-egui/src/num_math.rs b/crates/ui-egui/src/num_math.rs new file mode 100644 index 000000000..3e329c9e4 --- /dev/null +++ b/crates/ui-egui/src/num_math.rs @@ -0,0 +1,394 @@ +//! Basic arithmetic typed into a number field (#3195): `960/2`, `(3+4)*2`, `100*2`, and, in a +//! field that knows the value it showed, a leading operator that applies to it: `*2` doubles, +//! `/2` halves, `+50` adds 50, as the issue's reporter describes for After Effects (not confirmed +//! in public docs). A leading `-` is a negative number (`-50`), not a subtraction. (Hot text used +//! to read `+50` as 50; it now adds 50.) +//! +//! This is a small parser for numbers and `+ - * / ( )` only: no names, no functions, nothing is +//! run as code (expressions, `crates/expr`, are a different thing). The text is bounded in +//! length and nesting, every step must stay finite, and a division by zero is no value. Text it +//! can't read is `None`, so the field keeps its value. + +/// The longest text read as arithmetic (characters, after removing spaces). +const MAX_LEN: usize = 256; +/// How deeply brackets and signs (`-(-(…))`, `--5`) may nest. +const MAX_DEPTH: usize = 32; + +/// A typed leading operator: what it does to the value the field showed. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Step { + /// `+n` + Add(f64), + /// `*n` + Mul(f64), + /// `/n` (never zero) + Div(f64), +} + +impl Step { + /// `current` changed by the step; `None` when that isn't a finite number. + pub fn apply(self, current: f64) -> Option { + match self { + Step::Add(n) => finite(current + n), + Step::Mul(k) => finite(current * k), + Step::Div(n) => divide(current, n), + } + } + + /// The factor a `*` or `/` step multiplies by (`None` for `+`), for other layers that scale + /// their own value by it. + pub fn factor(self) -> Option { + match self { + Step::Add(_) => None, + Step::Mul(k) => finite(k), + Step::Div(n) => divide(1.0, n), + } + } +} + +/// What typed text says: a value, or (with a leading `+`, `*` or `/`) a step from the value shown. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Typed { + Value(f64), + Step(Step), +} + +/// Read typed `text`. With `steps`, a leading `+`, `*` or `/` is a [`Step`]; without (a field +/// that reads its text on every keystroke, where a step would compound) a leading `+` is a plain +/// sign and a leading `*` or `/` is no value. Spaces, including thousands groups, are ignored +/// (as egui's own parser did) and `−` (U+2212) is a minus. `,` is a decimal point: hot text +/// always read it so, and the dialog fields, whose egui parser rejected it, now do too (so +/// `1,5` is 1.5 and `1,000` is 1, not a thousand). `None` for anything else that isn't a finite +/// number. +pub fn read(text: &str, steps: bool) -> Option { + let s: String = text + .chars() + .filter(|c| !c.is_whitespace()) + .map(|c| match c { + '−' => '-', + ',' => '.', + c => c, + }) + .collect(); + if s.is_empty() || s.chars().count() > MAX_LEN { + return None; + } + let first = s.chars().next()?; + if steps && matches!(first, '+' | '*' | '/') { + let operand = parse(s.get(first.len_utf8()..)?)?; + return Some(Typed::Step(match first { + '+' => Step::Add(operand), + '*' => Step::Mul(operand), + _ => Step::Div((operand != 0.0).then_some(operand)?), + })); + } + parse(&s).map(Typed::Value) +} + +/// The value typed `text` stands for: [`read`], with a step applied to `current` (the value the +/// field showed; without it a leading `*` or `/` is no value and `+` only a sign). +pub fn calc(text: &str, current: Option) -> Option { + match read(text, current.is_some())? { + Typed::Value(v) => Some(v), + Typed::Step(step) => step.apply(current.filter(|c| c.is_finite())?), + } +} + +/// The whole of `s` as an expression. +fn parse(s: &str) -> Option { + let mut p = Parser { chars: s.chars().peekable(), depth: 0 }; + let v = p.expr()?; + if p.chars.next().is_some() { + return None; + } + finite(v) +} + +/// `a / b`; `None` for a division by zero. +fn divide(a: f64, b: f64) -> Option { + if b == 0.0 { + return None; + } + finite(a / b) +} + +/// A finite result, or `None`. +fn finite(v: f64) -> Option { + v.is_finite().then_some(v) +} + +/// A recursive-descent reader of `expr := term (('+'|'-') term)*`, +/// `term := factor (('*'|'/') factor)*`, `factor := ('+'|'-') factor | '(' expr ')' | number`. +struct Parser<'a> { + chars: std::iter::Peekable>, + /// The brackets and signs open around the current point. + depth: usize, +} + +impl Parser<'_> { + fn expr(&mut self) -> Option { + let mut v = self.term()?; + while let Some(&op) = self.chars.peek() { + match op { + '+' => { + self.chars.next(); + v = finite(v + self.term()?)?; + } + '-' => { + self.chars.next(); + v = finite(v - self.term()?)?; + } + _ => break, + } + } + Some(v) + } + + fn term(&mut self) -> Option { + let mut v = self.factor()?; + while let Some(&op) = self.chars.peek() { + match op { + '*' => { + self.chars.next(); + v = finite(v * self.factor()?)?; + } + '/' => { + self.chars.next(); + v = divide(v, self.factor()?)?; + } + _ => break, + } + } + Some(v) + } + + fn factor(&mut self) -> Option { + let c = *self.chars.peek()?; + match c { + '+' | '-' | '(' => { + self.depth = self.depth.checked_add(1)?; + if self.depth > MAX_DEPTH { + return None; + } + self.chars.next(); + let v = if c == '(' { + let v = self.expr()?; + if self.chars.next() != Some(')') { + return None; + } + v + } else { + let v = self.factor()?; + if c == '-' { -v } else { v } + }; + self.depth = self.depth.saturating_sub(1); + Some(v) + } + _ => self.number(), + } + } + + /// Digits with at most one decimal point and an optional exponent (`12`, `1.5`, `.5`, `2.`, + /// `2e2`, `1.5E-3`). + fn number(&mut self) -> Option { + let mut text = String::new(); + let mut point = false; + while let Some(&c) = self.chars.peek() { + if c.is_ascii_digit() { + text.push(c); + } else if c == '.' && !point { + point = true; + text.push(c); + } else { + break; + } + self.chars.next(); + } + if !text.chars().any(|c| c.is_ascii_digit()) { + return None; + } + if let Some(&e) = self.chars.peek() + && matches!(e, 'e' | 'E') + { + self.chars.next(); + text.push('e'); + if let Some(&sign) = self.chars.peek() + && matches!(sign, '+' | '-') + { + self.chars.next(); + text.push(sign); + } + let mut digits = false; + while let Some(&c) = self.chars.peek() { + if !c.is_ascii_digit() { + break; + } + digits = true; + text.push(c); + self.chars.next(); + } + if !digits { + return None; + } + } + finite(text.parse::().ok()?) + } +} + +#[cfg(test)] +mod tests { + use super::calc as eval; + + fn abs(text: &str) -> Option { + eval(text, None) + } + + #[test] + fn plain_numbers_read_as_before() { + assert_eq!(abs("100"), Some(100.0)); + assert_eq!(abs("-12.5"), Some(-12.5)); + assert_eq!(abs(" 12,5 "), Some(12.5), "a decimal comma"); + assert_eq!(abs("1 000"), Some(1000.0), "a thousands space"); + assert_eq!(abs("−3"), Some(-3.0), "the minus sign U+2212"); + assert_eq!(abs("1e3"), Some(1000.0)); + assert_eq!(abs(".5"), Some(0.5)); + } + + #[test] + fn arithmetic() { + assert_eq!(abs("960/2"), Some(480.0)); + assert_eq!(abs("100*2"), Some(200.0)); + assert_eq!(abs("(3+4)*2"), Some(14.0)); + assert_eq!(abs("3+4*2"), Some(11.0), "* before +"); + assert_eq!(abs("10-2-3"), Some(5.0), "left to right"); + assert_eq!(abs("8/4/2"), Some(1.0), "left to right"); + assert_eq!(abs("-(2+3)"), Some(-5.0)); + assert_eq!(abs("2*-3"), Some(-6.0)); + assert_eq!(abs("1.5 * 2"), Some(3.0)); + assert_eq!(abs("((((1))))"), Some(1.0)); + assert_eq!(abs("100+50"), Some(150.0)); + assert_eq!(abs("100-50"), Some(50.0)); + assert_eq!(abs("2e2*2"), Some(400.0), "an exponent inside arithmetic"); + assert_eq!(abs("1E-3*1000"), Some(1.0)); + assert_eq!(abs("1e+2/4"), Some(25.0)); + } + + #[test] + fn steps_are_read_as_steps() { + use super::{Step, Typed, read}; + assert_eq!(read("*2", true), Some(Typed::Step(Step::Mul(2.0)))); + assert_eq!(read("/4", true), Some(Typed::Step(Step::Div(4.0)))); + assert_eq!(read("+(1+2)", true), Some(Typed::Step(Step::Add(3.0)))); + assert_eq!(read("-5", true), Some(Typed::Value(-5.0))); + assert_eq!(read("+5", false), Some(Typed::Value(5.0))); + assert_eq!(read("/0", true), None); + assert_eq!(Step::Div(4.0).factor(), Some(0.25)); + assert_eq!(Step::Mul(3.0).factor(), Some(3.0)); + assert_eq!(Step::Add(3.0).factor(), None); + assert_eq!(Step::Div(1e-320).factor(), None, "a factor that overflows"); + assert_eq!(Step::Div(3.0).apply(3.0), Some(1.0), "a division, not a reciprocal factor"); + } + + #[test] + fn a_leading_operator_applies_to_the_current_value() { + assert_eq!(eval("*2", Some(100.0)), Some(200.0)); + assert_eq!(eval("/2", Some(100.0)), Some(50.0)); + assert_eq!(eval("+50", Some(100.0)), Some(150.0)); + assert_eq!(eval("*(1+1)", Some(3.0)), Some(6.0)); + assert_eq!(eval("+-50", Some(100.0)), Some(50.0)); + // A leading minus is a negative number, not a subtraction. + assert_eq!(eval("-50", Some(100.0)), Some(-50.0)); + assert_eq!(eval("-50*2", Some(100.0)), Some(-100.0)); + // Without the current value there is nothing to apply it to; a `+` is only a sign. + assert_eq!(eval("*2", None), None); + assert_eq!(eval("/2", None), None); + assert_eq!(eval("+50", None), Some(50.0)); + assert_eq!(eval("*2", Some(f64::NAN)), None); + assert_eq!(eval("+1", Some(f64::INFINITY)), None); + assert_eq!(eval("/0", Some(100.0)), None); + assert_eq!(eval("/(1-1)", Some(100.0)), None); + assert_eq!(eval("*", Some(100.0)), None); + assert_eq!(eval("*1e400", Some(1.0)), None); + // Overflow past the largest finite value. + assert_eq!(eval(&format!("*{}", "9".repeat(20)), Some(1e300)), None); + assert_eq!(eval("/0.0000000001", Some(1e300)), None); + } + + #[test] + fn invalid_or_hostile_text_is_no_value() { + for text in [ + "", " ", "*", "/", "1+", "+", "-", "2**3", "2//3", "1/0", "0/0", "-1/0", "1/(2-2)", "(1", "1)", "()", "(1+2", "abc", "NaN", "nan", "inf", "-inf", + "infinity", "1e999", "-1e999", "1..2", "1.2.3", ".", "1e", "2x", "x*2", "1+a", "5;rm", "1,2,3", "0x10", "1_000", "%", "50%", + ] { + assert_eq!(abs(text), None, "{text:?}"); + assert_eq!(eval(text, Some(10.0)), None, "{text:?} with a current value"); + } + // Far too long: rejected by length before any reading. + assert_eq!(abs(&format!("{}1{}", "(".repeat(1000), ")".repeat(1000))), None); + assert_eq!(abs(&format!("{}5", "-".repeat(10_000))), None); + assert_eq!(abs(&"(".repeat(10_000)), None); + assert_eq!(abs(&"1".repeat(10_000)), None); + assert_eq!(abs(&"1+".repeat(5_000)), None); + // Short enough, but nested past the depth bound. + assert_eq!(abs(&format!("{}1{}", "(".repeat(100), ")".repeat(100))), None); + assert_eq!(abs(&format!("{}5", "-".repeat(200))), None); + assert_eq!(abs(&format!("2*{}5", "-+".repeat(100))), None); + // Nesting within the bound reads. + assert_eq!(abs(&format!("{}1{}", "(".repeat(20), ")".repeat(20))), Some(1.0)); + assert_eq!(abs("--5"), Some(5.0)); + // Overflow on the way is no value. + assert_eq!(abs("1e308*10"), None); + assert_eq!(abs("1e308+1e308"), None); + assert_eq!(abs("1e200*1e200/1e200"), None, "an infinite step is rejected even if the result would be finite"); + // The largest numbers the length allows still multiply to a finite value. + let big = "9".repeat(120); + assert!(abs(&format!("{big}*{big}")).is_some_and(f64::is_finite)); + } + + /// The result is always finite (no NaN reaches a property). + #[test] + fn results_are_finite() { + for text in ["0*1e308", "1e-320/1e10", "0-0", "-0"] { + if let Some(v) = abs(text) { + assert!(v.is_finite(), "{text:?} → {v}"); + } + } + } + + /// A standard numeric editor (dialogs) reads typed arithmetic too. + /// (egui's own parser read `960/2` as no value, so the field stayed 10.) + #[test] + fn a_drag_value_reads_typed_arithmetic() { + use egui::{Context, Event, Key, Modifiers, PointerButton, Pos2, RawInput, Rect, pos2, vec2}; + let ctx = Context::default(); + let mut value = 10.0_f64; + let mut time = 0.0; + let field = Rect::from_min_size(pos2(100.0, 100.0), vec2(80.0, 20.0)); + let mut frame = |events: Vec, value: &mut f64| { + time += 0.02; + let raw = RawInput { + screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0))), + time: Some(time), + events, + focused: true, + ..Default::default() + }; + let mut out = ctx.run_ui(raw, |ui| { + ui.put(field, crate::widgets::drag_value(egui::DragValue::new(&mut *value).speed(1.0))); + }); + out.textures_delta.clear(); + }; + let p = field.center(); + frame(vec![], &mut value); + frame( + vec![Event::PointerMoved(p), Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }], + &mut value, + ); + frame(vec![Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }], &mut value); + frame(vec![], &mut value); + frame(vec![Event::Text("960/2".into())], &mut value); + frame(vec![Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }], &mut value); + frame(vec![], &mut value); + assert_eq!(value, 480.0); + } +} diff --git a/crates/ui-egui/src/panels/effect_controls/rows.rs b/crates/ui-egui/src/panels/effect_controls/rows.rs index c14630af4..e2634d6b8 100644 --- a/crates/ui-egui/src/panels/effect_controls/rows.rs +++ b/crates/ui-egui/src/panels/effect_controls/rows.rs @@ -265,11 +265,12 @@ fn value_angle(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: return; }; let PropCx { uid, cy, vx, prop, .. } = *cx; - let (rr, dr, nv) = fw::angle_field(ui, pos2(vx, cy - 9.0), egui::Id::unique(("ec-v", uid)), *v, 1, &cx.value_tokens()); + let (rr, dr, nv, step) = fw::angle_field_step(ui, pos2(vx, cy - 9.0), egui::Id::unique(("ec-v", uid)), *v, 1, &cx.value_tokens()); app.auto.add(&format!("effectControls.prop.{uid}.value"), rr.union(dr), &fw::format_angle(*v, 1)); app.auto.add(&format!("effectControls.prop.{uid}.revolutions"), rr, &prop.name); if let Some(nv) = nv { cx.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } cx.twirl_open(app, ui); } @@ -288,7 +289,7 @@ fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: _ => "", }; let speed = widgets::slider_scrub_speed(&prop.ui); - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(vx, cy - 9.0), egui::Id::unique(("ec-v", uid)), @@ -299,6 +300,7 @@ fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: app.auto.add(&format!("effectControls.prop.{uid}.value"), vr, &prop.name); if let Some(nv) = nv { cx.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } // AE hides a slider param's slider until its twirl is opened (see `slider_row`). if matches!(prop.ui, ParamUi::Slider { .. }) { @@ -326,7 +328,7 @@ fn value_vector(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: x += 22.0; } for d in 0..n.min(c.len()) { - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, cy - 9.0), egui::Id::unique(("ec-v", uid, d)), @@ -345,6 +347,7 @@ fn value_vector(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: if let Some((_, a)) = actions.last_mut() { a["dims"] = json!([d]); } + widgets::mark_step(actions.last_mut(), step); } x = vr.max.x + 8.0; } diff --git a/crates/ui-egui/src/panels/essential.rs b/crates/ui-egui/src/panels/essential.rs index 02e077508..5df6f6b8c 100644 --- a/crates/ui-egui/src/panels/essential.rs +++ b/crates/ui-egui/src/panels/essential.rs @@ -399,7 +399,13 @@ fn value_widget(app: &mut EffectcraftApp, ui: &mut egui::Ui, auto: &str, ty: Opt let mut rect = lr; for (d, &old) in comps.iter().enumerate() { let mut x = old; - let r = ui.add(egui::DragValue::new(&mut x).speed(0.5).suffix(if d + 1 == comps.len() { "%" } else { "" }).max_decimals(1)); + let r = ui.add( + egui::DragValue::new(&mut x) + .speed(0.5) + .suffix(if d + 1 == comps.len() { "%" } else { "" }) + .max_decimals(1) + .custom_parser(|text| crate::num_math::calc(text, None)), + ); app.auto.add(&format!("{auto}.{d}"), r.rect, &p.name); rect = rect.union(r.rect); if r.changed() { diff --git a/crates/ui-egui/src/panels/fx_widgets.rs b/crates/ui-egui/src/panels/fx_widgets.rs index b3140d394..c39009fd3 100644 --- a/crates/ui-egui/src/panels/fx_widgets.rs +++ b/crates/ui-egui/src/panels/fx_widgets.rs @@ -8,6 +8,7 @@ use effectcraft_engine::geom::{Mat3, vec2 as gv2}; use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2}; +use crate::num_math::Step; use crate::theme::Tokens; use crate::widgets; @@ -56,18 +57,35 @@ fn degrees_edit(v: f64, shown: f64, new: f64, dragging: bool) -> f64 { /// are hot numbers of their own (drag to scrub, click to type) and changing one keeps the other. /// Returns the revolutions' rect, the degrees' rect and the new angle when either changed. pub fn angle_field(ui: &mut egui::Ui, at: Pos2, id: egui::Id, v: f64, decimals: usize, t: &Tokens) -> (Rect, Rect, Option) { + let (rr, dr, out, _) = angle_field_step(ui, at, id, v, decimals, t); + (rr, dr, out) +} + +/// [`angle_field`], also returning a typed `+n` into the degrees (#3195): other selected layers +/// can turn their own angle by it. A typed step applies to the signed remainder, so `+10` on +/// `0x-30°` is `0x-20°`. +pub fn angle_field_step(ui: &mut egui::Ui, at: Pos2, id: egui::Id, v: f64, decimals: usize, t: &Tokens) -> (Rect, Rect, Option, Option) { let (rev, deg) = split_angle(v); let (rr, new_rev, _) = widgets::hot_int_at(ui, at, id.with("rev"), rev, 0.1, "x", t); let sign = ui.painter().text(pos2(rr.max.x - 2.0, rr.center().y), Align2::LEFT_CENTER, if deg < 0.0 { "-" } else { "+" }, Tokens::ui(12.0), t.hot_text); - let (dr, new_deg, _) = - widgets::hot_number_at(ui, pos2(sign.max.x - 2.0, at.y), id, deg.abs(), &widgets::HotSpec { speed: 0.5, range: (-1e9, 1e9), decimals, suffix: "°" }, t); + let (dr, new_deg, _, step) = widgets::hot_number_step_at( + ui, + pos2(sign.max.x - 2.0, at.y), + id, + deg.abs(), + &widgets::HotSpec { speed: 0.5, range: (-1e9, 1e9), decimals, suffix: "°" }, + t, + ); let dragging = new_deg.is_some() && ui.ctx().is_being_dragged(id); - let out = match (new_rev, new_deg) { - (Some(r), _) => Some(r as f64 * 360.0 + deg), - (None, Some(d)) => Some(degrees_edit(v, deg.abs(), d, dragging)), - (None, None) => None, + let out = match (new_rev, new_deg, step) { + (Some(r), _, _) => Some(r as f64 * 360.0 + deg), + // (The field shows the remainder's size; the step goes from the signed remainder.) + (None, Some(_), Some(s)) => s.apply(deg).map(|d| rev as f64 * 360.0 + d), + (None, Some(d), None) => Some(degrees_edit(v, deg.abs(), d, dragging)), + (None, None, _) => None, }; - (rr, dr, out) + let step = step.filter(|s| matches!(s, Step::Add(_)) && new_rev.is_none() && out.is_some()); + (rr, dr, out, step) } /// Dial angle (degrees, 0 = up, clockwise) of `p` around `c` in screen space. diff --git a/crates/ui-egui/src/panels/gradient_editor.rs b/crates/ui-egui/src/panels/gradient_editor.rs index 0f0bbe412..6f88519b7 100644 --- a/crates/ui-egui/src/panels/gradient_editor.rs +++ b/crates/ui-egui/src/panels/gradient_editor.rs @@ -440,7 +440,14 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { } .unwrap_or(0.0); let mut pct = pos * 100.0; - let r = ui.add(egui::DragValue::new(&mut pct).range(0.0..=100.0).speed(0.5).max_decimals(1).suffix("%")); + let r = ui.add( + egui::DragValue::new(&mut pct) + .range(0.0..=100.0) + .speed(0.5) + .max_decimals(1) + .suffix("%") + .custom_parser(|text| crate::num_math::calc(text, None)), + ); app.auto.add("dialog.gradient.location", r.rect, "Location"); if r.changed() { app.dialog_state.gradient.move_to(track, id, pct / 100.0); @@ -452,7 +459,14 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { Track::Opacity => { ui.label(tr("Opacity:")); let mut a = app.dialog_state.gradient.opacities.get(i).map_or(1.0, |s| s.value) as f64 * 100.0; - let r = ui.add(egui::DragValue::new(&mut a).range(0.0..=100.0).speed(0.5).max_decimals(1).suffix("%")); + let r = ui.add( + egui::DragValue::new(&mut a) + .range(0.0..=100.0) + .speed(0.5) + .max_decimals(1) + .suffix("%") + .custom_parser(|text| crate::num_math::calc(text, None)), + ); app.auto.add("dialog.gradient.opacity", r.rect, "Opacity"); if r.changed() { app.dialog_state.gradient.set_opacity(id, (a / 100.0) as f32); diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 25829be7d..e6d3be35b 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -291,16 +291,17 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, match &value { Value::Scalar(v) if pr.name == "Rotation" || pr.name.ends_with(" Rotation") => { let (_, deg) = super::fx_widgets::split_angle(*v); - let (rr, dr, nv) = super::fx_widgets::angle_field(ui, pos2(vx, cy - 9.0), egui::Id::unique(("props-v", uid)), *v, decimals(deg), &vt); + let (rr, dr, nv, step) = super::fx_widgets::angle_field_step(ui, pos2(vx, cy - 9.0), egui::Id::unique(("props-v", uid)), *v, decimals(deg), &vt); app.auto.add(&format!("{base}.value"), dr, &pr.name); app.auto.add(&format!("{base}.revolutions"), rr, &pr.name); if let Some(nv) = nv { set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } Value::Scalar(v) => { let range = if pr.name == "Opacity" { (0.0, 100.0) } else { (-1e9, 1e9) }; - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(vx, cy - 9.0), egui::Id::unique(("props-v", uid)), @@ -311,6 +312,7 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, app.auto.add(&format!("{base}.value"), vr, &pr.name); if let Some(nv) = nv { set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } Value::Vec2(_) | Value::Vec3(_) => { @@ -332,7 +334,7 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, } } for d in 0..n.min(c.len()) { - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, cy - 9.0), egui::Id::unique(("props-v", uid, d)), @@ -357,6 +359,7 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, if let Some((_, a)) = actions.last_mut() { a["dims"] = json!((from..from + shown).collect::>()); } + widgets::mark_step(actions.last_mut(), step); } x = vr.max.x + 10.0; } diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 038dcc9b9..c6da2ccbd 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -5831,17 +5831,18 @@ fn scalar_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> _ => (-1e9, 1e9, 1, 1.0), }; if matches!(prop.ui, ParamUi::Angle) { - let (rr, dr, nv) = super::fx_widgets::angle_field(ui, pos2(x, y), egui::Id::unique(("v", uid, 0)), *v, 1, &vt); + let (rr, dr, nv, step) = super::fx_widgets::angle_field_step(ui, pos2(x, y), egui::Id::unique(("v", uid, 0)), *v, 1, &vt); app.auto.add(&format!("timeline.prop.{uid}.value"), dr, &prop.name); app.auto.add(&format!("timeline.prop.{uid}.revolutions"), rr, &prop.name); if let Some(nv) = nv { ve.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } else { // A Range Selector's Start, End and Offset count characters with Units = Index (#1814). let pct = (matches!(prop.ui, ParamUi::Percent) || prop.match_id == "opacity") && !effectcraft_engine::render::text::in_index_units(layer, uid); let suffix = if pct { "%" } else { "" }; - let (r, nv, _) = widgets::hot_number_at( + let (r, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, y), egui::Id::unique(("v", uid, 0)), @@ -5852,6 +5853,7 @@ fn scalar_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> app.auto.add(&format!("timeline.prop.{uid}.value"), r, &prop.name); if let Some(nv) = nv { ve.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } } @@ -5890,7 +5892,7 @@ fn vector_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> } for d in 0..n.min(c.len()) { let suffix = if pct && d + 1 == n { "%" } else { "" }; - let (r, nv, _) = widgets::hot_number_at( + let (r, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, y), egui::Id::unique(("v", uid, d)), @@ -5924,6 +5926,7 @@ fn vector_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> a["dims"] = json!((from..from + shown).collect::>()); } } + widgets::mark_step(actions.last_mut(), step); } x = r.max.x + if d + 1 < n { 2.0 } else { 0.0 }; if d + 1 < n { diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 162a86c6a..c5b7223da 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -63,8 +63,23 @@ pub struct HotSpec<'a> { /// "Hot text": a blue number you drag horizontally to scrub, click to type. Tab / Shift+Tab /// while typing commits and types in the panel's next / previous one, as in After Effects. +/// Typed text may be arithmetic, `960/2` or `*2` for twice the value ([`crate::num_math`]). /// Returns (response, Some(new value) when changed, drag finished). pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64, spec: &HotSpec, t: &Tokens) -> (Rect, Option, bool) { + let (rect, out, done, _) = hot_number_step_at(ui, rect_min, id, value, spec, t); + (rect, out, done) +} + +/// [`hot_number_at`], also returning the typed step (`*2`, `+50`) the new value came from, so +/// that other selected layers can take the same step from their own value (#3195). +pub fn hot_number_step_at( + ui: &mut Ui, + rect_min: egui::Pos2, + id: egui::Id, + value: f64, + spec: &HotSpec, + t: &Tokens, +) -> (Rect, Option, bool, Option) { let HotSpec { speed, range, decimals, suffix } = *spec; let editing_id = id.with("editing"); let mut editing: Option = ui.data(|d| d.get_temp(editing_id)); @@ -87,14 +102,18 @@ pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64 .lock_focus(true), ); let tab = r.has_focus().then(|| ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Tab).then_some(i.modifiers.shift))).flatten(); - let mut out = None; + let (mut out, mut step) = (None, None); if r.lost_focus() || tab.is_some() { - let txt = buf.trim().trim_end_matches(suffix).trim().replace(',', "."); + // Typed arithmetic reads too (`960/2`), a leading `*`, `/` or `+` applied to the value + // shown (#3195). if !ui.input(|i| i.key_pressed(egui::Key::Escape)) - && let Ok(v) = txt.parse::() - && v.is_finite() + && let Some(typed) = crate::num_math::read(buf.trim().trim_end_matches(suffix), true) { - out = Some(v.clamp(range.0, range.1)); + let v = match typed { + crate::num_math::Typed::Value(v) => Some(v), + crate::num_math::Typed::Step(s) => s.apply(value).inspect(|_| step = Some(s)), + }; + out = v.map(|v| v.clamp(range.0, range.1)); } ui.data_mut(|d| d.remove::(editing_id)); if let Some(back) = tab { @@ -106,7 +125,7 @@ pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64 keep_focus(ui.ctx(), &r, &buf); ui.data_mut(|d| d.insert_temp(editing_id, buf)); } - return (er, out, out.is_some()); + return (er, out, out.is_some(), step); } let resp = ui.interact(rect, id, Sense::click_and_drag()); crate::pointer_wrap::track(&resp); @@ -140,7 +159,20 @@ pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64 if resp.clicked() { ui.data_mut(|d| d.insert_temp(editing_id, format!("{value:.decimals$}"))); } - (rect, out, resp.drag_stopped()) + (rect, out, resp.drag_stopped(), None) +} + +/// Mark a queued `prop.set` (with `selected`) as the typed `step` it came from, so every other +/// selected layer takes that step from its own value rather than the edited layer's result +/// (#3195): `+n` by the same change (`offset`), `*n` and `/n` by the same `factor`. +pub fn mark_step(action: Option<&mut (String, serde_json::Value)>, step: Option) { + let (Some((_, a)), Some(step)) = (action, step) else { return }; + match step.factor() { + Some(k) => a["factor"] = serde_json::json!(k), + None if matches!(step, crate::num_math::Step::Add(_)) => a["offset"] = serde_json::json!(true), + // (A factor too large to send: the other layers take the typed result.) + None => {} + } } /// Ctrl+Alt (Cmd+Opt on macOS) held while scrubbing hot text: After Effects' Proportional @@ -162,10 +194,19 @@ pub fn slider_scrub_speed(ui: &ParamUi) -> f64 { /// [`hot_number_at`] for a whole number (an angle's revolutions): a drag steps it once every /// `1 / speed` pixels however slowly the pointer moves (the fractions add up between frames), -/// and a typed value is truncated. Returns (rect, Some(new value) when changed, drag finished). +/// and a typed value is rounded (`5/2` is 3). Returns (rect, Some(new value) when changed, drag +/// finished). pub fn hot_int_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: i64, speed: f64, suffix: &str, t: &Tokens) -> (Rect, Option, bool) { let (rect, nv, done) = hot_number_at(ui, rect_min, id, value as f64, &HotSpec { speed, range: (-1e6, 1e6), decimals: 0, suffix }, t); let acc_id = id.with("acc"); + if let Some(n) = nv + && !ui.ctx().is_being_dragged(id) + { + // Typed (a drag changes the value only while it is dragged). (Within ±1e6: the cast is exact.) + ui.data_mut(|d| d.remove::(acc_id)); + let typed = n.round() as i64; + return (rect, (typed != value).then_some(typed), done); + } let acc = ui.data(|d| d.get_temp::(acc_id)).unwrap_or(0.0) + nv.map_or(0.0, |n| n - value as f64); // (Ten 0.1 steps add up to just under 1.) let step = (acc + acc.signum() * 1e-9).trunc(); @@ -177,8 +218,11 @@ pub fn hot_int_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: i64, s (rect, (step != 0.0).then(|| value.saturating_add(step as i64)), done) } -/// Standard numeric editors use the same continuous native scrubbing as hot text. +/// Standard numeric editors use the same continuous native scrubbing as hot text, and read typed +/// arithmetic (`960/2`) too. They read their text on every keystroke, so a leading `*` or `/` +/// (relative to the value) is left to hot text, where it would otherwise compound (#3195). pub fn drag_value(value: egui::DragValue<'_>) -> impl egui::Widget { + let value = value.custom_parser(|text| crate::num_math::calc(text, None)); move |ui: &mut Ui| { let response = ui.add(value); crate::pointer_wrap::track(&response); diff --git a/crates/ui-egui/tests/it/ui_angles.rs b/crates/ui-egui/tests/it/ui_angles.rs index 007e11286..37eb01c8d 100644 --- a/crates/ui-egui/tests/it/ui_angles.rs +++ b/crates/ui-egui/tests/it/ui_angles.rs @@ -172,6 +172,21 @@ fn typing_replaces_the_clicked_value() { } } +/// #3195: a typed `+10` turns the angle by 10°, from its signed remainder: `0x-30°` becomes +/// `0x-20°` (the degrees field shows the remainder's size, 30, which would have given -40°). +#[test] +fn a_typed_step_applies_to_the_signed_angle() { + for panel in [Panel::EffectControls, Panel::Timeline, Panel::Properties] { + for (text, expected) in [("+10", -20.0), ("*2", -60.0), ("/3", -10.0)] { + let (mut h, layer, uid) = harness(panel, -30.0); + let p = component(&h, panel, uid, false); + begin_edit(&mut h, p, text); + key(&mut h, Key::Enter, Modifiers::NONE); + assert_eq!(angle(&h, layer, uid), expected, "{panel:?}: {text}"); + } + } +} + fn commit_edits(click_away: bool) { for panel in [Panel::EffectControls, Panel::Timeline, Panel::Properties] { for (revolutions, text, expected) in [(true, "3", 1110.5), (false, "45.5", 405.5)] { diff --git a/crates/ui-egui/tests/it/ui_inline_edit.rs b/crates/ui-egui/tests/it/ui_inline_edit.rs index 6c29045bb..c845863e4 100644 --- a/crates/ui-egui/tests/it/ui_inline_edit.rs +++ b/crates/ui-egui/tests/it/ui_inline_edit.rs @@ -404,6 +404,84 @@ fn tab_commits_a_value_and_moves_to_the_next() { assert_eq!(value(&h, "effects/#2/slider"), [42.0]); } +/// #3195: a value field reads typed arithmetic, as in After Effects: `960/2` sets 480, a leading +/// `*` applies to the value shown, and text that is no number (a division by zero) keeps the +/// value with no undo step. In the Timeline and in Effect Controls. +#[test] +fn typed_arithmetic_sets_a_value() { + let (mut h, _) = harness(); + let (layer, transform) = { + let s = &mut h.state_mut().session; + let l = s.execute("layer.newSolid", json!({"name": "Plate", "color": "#406080"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.select", json!({"layers": [l]})).unwrap(); + s.execute("effect.apply", json!({"layers": [l], "effect": "Point Control"})).unwrap(); + s.execute("prop.set", json!({"layer": l, "path": "transform/position", "value": [160.0, 90.0]})).unwrap(); + let tr = s.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(l)).unwrap().props.sub("transform").unwrap().uid; + (l, tr) + }; + h.state_mut().ui.timeline.open_layers.insert(layer); + h.state_mut().ui.timeline.open_groups.insert(transform); + h.state_mut().show_panel(effectcraft_ui_egui::dock::PanelKind::EffectControls); + h.run_steps(3); + let props = + |h: &Harness<'_, EffectcraftApp>| h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(layer)).unwrap().props.clone(); + let value = |h: &Harness<'_, EffectcraftApp>, path: &str| props(h).prop(path).unwrap().value.components(); + let position = props(&h).prop("transform/position").unwrap().uid; + assert_eq!(value(&h, "transform/position")[..2], [160.0, 90.0]); + let type_into = |h: &mut Harness<'_, EffectcraftApp>, id: &str, text: &str| { + // (Settle first: a click right after the last one's would count as part of a double click.) + h.run_steps(5); + let p = center(h, id); + click(h, p, 1); + type_text(h, text); + key(h, Key::Enter); + }; + type_into(&mut h, &format!("timeline.prop.{position}.value.0"), "960/2"); + assert_eq!(value(&h, "transform/position")[..2], [480.0, 90.0], "960/2 is 480"); + type_into(&mut h, &format!("timeline.prop.{position}.value.1"), "*2"); + assert_eq!(value(&h, "transform/position")[..2], [480.0, 180.0], "*2 doubles the value shown"); + let undo = h.state().session.history.undo.len(); + type_into(&mut h, &format!("timeline.prop.{position}.value.0"), "1/0"); + assert_eq!(value(&h, "transform/position")[..2], [480.0, 180.0], "a division by zero keeps the value"); + assert_eq!(h.state().session.history.undo.len(), undo, "and adds no undo step"); + let point = props(&h).prop("effects/#1/point").unwrap().uid; + type_into(&mut h, &format!("effectControls.prop.{point}.value.1"), "(3+4)*2"); + assert_eq!(value(&h, "effects/#1/point")[1], 14.0); +} + +/// #3195: with several layers selected, a typed `+10` or `*2` takes each layer's own value one +/// step on; it doesn't give them all the edited layer's result. +#[test] +fn a_typed_step_applies_to_each_selected_layer() { + let (mut h, _) = harness(); + let (a, b, transform) = { + let s = &mut h.state_mut().session; + let mut ids = vec![]; + for (name, opacity) in [("A", 40.0), ("B", 30.0)] { + let l = s.execute("layer.newSolid", json!({"name": name, "color": "#406080"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.set", json!({"layer": l, "path": "transform/opacity", "value": opacity})).unwrap(); + ids.push(l); + } + s.execute("layer.select", json!({"layers": ids})).unwrap(); + let tr = s.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(ids[0])).unwrap().props.sub("transform").unwrap().uid; + (ids[0], ids[1], tr) + }; + h.state_mut().ui.timeline.open_layers.insert(a); + h.state_mut().ui.timeline.open_groups.insert(transform); + h.run_steps(3); + let opacity_of = |h: &Harness<'_, EffectcraftApp>, l: u64| { + h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(l)).unwrap().props.prop("transform/opacity").unwrap().value.as_f64() + }; + let opacity = h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(a)).unwrap().props.prop("transform/opacity").unwrap().uid; + for (text, expected) in [("+10", (50.0, 40.0)), ("*2", (100.0, 80.0)), ("/4", (25.0, 20.0)), ("60", (60.0, 60.0))] { + let p = center(&h, &format!("timeline.prop.{opacity}.value")); + click(&mut h, p, 1); + type_text(&mut h, text); + key(&mut h, Key::Enter); + assert_eq!((opacity_of(&h, a), opacity_of(&h, b)), expected, "{text}"); + } +} + /// A click at `p` with `modifiers` held. fn click_with(h: &mut Harness<'_, EffectcraftApp>, p: Pos2, modifiers: Modifiers) { h.input_mut().events.push(Event::PointerMoved(p)); From dd92908bc82eecc32896dd6b9db80068ae39ab16 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:06:49 +0100 Subject: [PATCH 032/350] Project panel: items dropped on New Folder go into a new folder, as After Effects project.newFolder takes optional `items` (ids or names) and moves them into the new folder in the same undo step, out of any folder they were in; a folder can't go into a new folder made inside itself. Dropping dragged Project items (the dragged item, or the selection it belongs to) on the panel's New Folder button runs it, with the drop outline New Composition shows; the new folder opens with its name ready to type, like one made with the button. Fixes #3196 --- crates/engine/src/cmd/file_more.rs | 36 +++++++++++- crates/engine/src/cmd/project_items.rs | 2 +- crates/engine/src/tests/project_items.rs | 46 +++++++++++++++ crates/ui-egui/src/menus/invoke.rs | 12 +++- crates/ui-egui/src/panels/project.rs | 20 +++++-- .../ui-egui/tests/it/ui_project_new_folder.rs | 58 +++++++++++++++++++ 6 files changed, 162 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/file_more.rs b/crates/engine/src/cmd/file_more.rs index 168529c47..104907f74 100644 --- a/crates/engine/src/cmd/file_more.rs +++ b/crates/engine/src/cmd/file_more.rs @@ -56,15 +56,39 @@ fn items_p(s: &Session, p: &Value) -> Vec { } } +/// `project.newFolder {name?, parent?, items?}`. With `items` (ids or names), they move into the +/// new folder in the same undo step: Project items dropped on the panel's New Folder button, as +/// in After Effects (#3196). A folder can't end up inside itself. fn new_folder(s: &mut Session, p: &Value) -> Result { + let c = "project.newFolder"; let name = str_p(p, "name").unwrap_or("Untitled Folder").to_string(); let parent = p.get("parent").and_then(Value::as_u64).map(ItemId).filter(|i| s.project.item(*i).is_some_and(|x| x.is_folder())); + let mut items: Vec = vec![]; + if let Some(v) = p.get("items") { + let Value::Array(a) = v else { return Err(bad(c, "`items` must be an array of item ids or names")) }; + for v in a { + let id = super::project_items::item_ref(s, v, c)?; + if !items.contains(&id) { + items.push(id); + } + } + if let Some(f) = parent + && let Some(inside) = items.iter().find(|i| super::project_items::is_within(&s.project, f, **i)) + { + return Err(bad(c, format!("can't move folder {} into a folder inside itself", inside.0))); + } + } let id = s.edit("New Folder", None, |proj, st| { let id = proj.add_item(&name, Label::Yellow, parent, ItemKind::Folder); + for i in &items { + if let Some(it) = proj.item_mut(*i) { + it.parent = Some(id); + } + } st.project_selection = vec![id]; Ok(id) })?; - Ok(json!({"item": id.0})) + if p.get("items").is_some() { Ok(json!({"item": id.0, "moved": items.len()})) } else { Ok(json!({"item": id.0})) } } fn close(s: &mut Session, p: &Value) -> Result { @@ -1202,7 +1226,15 @@ fn reveal_in_finder(s: &mut Session, p: &Value) -> Result { pub fn specs() -> Vec { vec![ - cmd!("project.newFolder", "New Folder", ["File", "New"], Some("Cmd+Alt+Shift+N"), "{name?, parent?}", always, new_folder), + cmd!( + "project.newFolder", + "New Folder", + ["File", "New"], + Some("Cmd+Alt+Shift+N"), + "{name?, parent?, items?: [id|name] (moved into it)}", + always, + new_folder + ), cmd!("file.close", "Close", ["File"], Some("Cmd+W"), "{comp?}", has_comp, close), cmd!("file.closeProject", "Close Project", ["File"], None, "{}", always, close_project), cmd!("file.saveCopy", "Save a Copy...", ["File", "Save As"], None, "{path}", always, save_copy), diff --git a/crates/engine/src/cmd/project_items.rs b/crates/engine/src/cmd/project_items.rs index 4d76861dc..e513a0511 100644 --- a/crates/engine/src/cmd/project_items.rs +++ b/crates/engine/src/cmd/project_items.rs @@ -8,7 +8,7 @@ use super::{CommandSpec, always, b_p, bad, str_p}; use crate::{EngineError, Result, Session, cmd, query}; /// `item` (id or name) → id. -fn item_ref(s: &Session, v: &Value, cmd: &str) -> Result { +pub(super) fn item_ref(s: &Session, v: &Value, cmd: &str) -> Result { match v { Value::Number(n) => n.as_u64().map(ItemId).filter(|i| s.project.item(*i).is_some()), Value::String(name) => s.project.find_by_name(name).map(|i| i.id), diff --git a/crates/engine/src/tests/project_items.rs b/crates/engine/src/tests/project_items.rs index 105174c35..27e8e7818 100644 --- a/crates/engine/src/tests/project_items.rs +++ b/crates/engine/src/tests/project_items.rs @@ -40,6 +40,52 @@ fn move_into_folder_rename_label_comment_with_undo() { assert_eq!(s.project.item(comp).unwrap().comment, ""); } +/// #3196: `project.newFolder` with `items` (items dropped on the Project panel's New Folder +/// button) makes the folder and moves them into it, out of the folder they were in, in one undo +/// step. A folder can't go into a new folder made inside itself. +#[test] +fn new_folder_with_items_moves_them_into_it_in_one_undo_step() { + let mut s = Session::default(); + let a = ItemId(s.execute("comp.new", json!({"name": "A", "width": 64, "height": 64, "duration": 1})).unwrap()["comp"].as_u64().unwrap()); + let b = ItemId(s.execute("comp.new", json!({"name": "B", "width": 64, "height": 64, "duration": 1})).unwrap()["comp"].as_u64().unwrap()); + let bin = ItemId(s.execute("project.newFolder", json!({"name": "Bin"})).unwrap()["item"].as_u64().unwrap()); + s.execute("project.move", json!({"items": [b.0], "folder": bin.0})).unwrap(); + let before = s.project.clone(); + let undo = s.history.undo.len(); + + let r = s.execute("project.newFolder", json!({"items": [a.0, "B", a.0]})).unwrap(); + let f = ItemId(r["item"].as_u64().unwrap()); + assert_eq!(r["moved"], 2, "duplicates count once"); + let folder = s.project.item(f).unwrap(); + assert!(folder.is_folder()); + assert_eq!((folder.name.as_str(), folder.parent), ("Untitled Folder", None), "the New Folder button's name and place"); + assert_eq!(s.project.item(a).unwrap().parent, Some(f)); + assert_eq!(s.project.item(b).unwrap().parent, Some(f), "out of the folder it was in"); + assert_eq!(s.state.project_selection, [f], "the new folder is selected"); + assert_eq!(s.history.undo.len(), undo + 1, "one undo step"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.project, before, "one undo takes back the folder and the move"); + + // A folder dropped on New Folder goes into the new folder; a new folder made inside the + // folder that is moving into it is refused, and nothing changes. + let r = s.execute("project.newFolder", json!({"items": [bin.0]})).unwrap(); + let f = ItemId(r["item"].as_u64().unwrap()); + assert_eq!(s.project.item(bin).unwrap().parent, Some(f)); + s.execute("edit.undo", json!({})).unwrap(); + // Inside another folder (`parent`), and an empty list moves nothing. + let r = s.execute("project.newFolder", json!({"parent": bin.0, "items": [a.0]})).unwrap(); + let f = ItemId(r["item"].as_u64().unwrap()); + assert_eq!((s.project.item(f).unwrap().parent, s.project.item(a).unwrap().parent), (Some(bin), Some(f))); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.execute("project.newFolder", json!({"items": []})).unwrap()["moved"], 0); + s.execute("edit.undo", json!({})).unwrap(); + let before = s.project.clone(); + assert!(s.execute("project.newFolder", json!({"parent": bin.0, "items": [bin.0]})).is_err()); + assert!(s.execute("project.newFolder", json!({"items": [999_999]})).is_err(), "an unknown item"); + assert!(s.execute("project.newFolder", json!({"items": a.0})).is_err(), "not an array"); + assert_eq!(s.project, before); +} + #[test] fn delete_and_duplicate_items() { let mut s = Session::default(); diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 0c7573c91..0f53a8372 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -195,10 +195,16 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V } // A new folder (File ▸ New, its shortcut, the Project panel's button and context menu) opens // with its name ready to type, as in After Effects (#1658). The command selects it. - // `engine.execute` keeps the headless default. - if id == "project.newFolder" && no_params(¶ms) { + // `engine.execute` keeps the headless default. Items dropped on the New Folder button (only + // `items`, #3196) open the name too. + if id == "project.newFolder" && (no_params(¶ms) || params.as_object().is_some_and(|m| m.len() == 1 && m.contains_key("items"))) { + let dropped = params.get("items").is_some(); let r = run_engine(app, ctx, id, params); - if r.is_ok() { + if let Ok(v) = &r { + // Open a folder that items were dropped into, so they stay in view. + if dropped && let Some(f) = v.get("item").and_then(Value::as_u64) { + app.ui.project_open_folders.insert(f); + } app.show_panel(PanelKind::Project); crate::panels::project::begin_rename(app, ctx); } diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index 8d67fd9ee..21163391c 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -3,7 +3,8 @@ //! Media Duration, Frame Rate, File Path, Comment — shown or hidden from the header's context //! menu), folders (drag items into and out of them), renaming (Enter, or double-click the name), //! the label colour picker, and the bottom bar (interpret, new folder, new comp: click, or drop -//! items on it for a comp from them, bit depth, delete), and the keyboard (the arrow keys select, +//! items on New Folder for a folder holding them, or on New Comp for a comp from them; bit depth, +//! delete), and the keyboard (the arrow keys select, //! extend the selection and open / close folders; rows and buttons carry AccessKit names and //! states for screen readers). Edits are engine commands, so they are undoable. @@ -905,17 +906,24 @@ fn footer(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, foot: } } // Items dropped on Create a new Composition: New Comp from Selection with them (After - // Effects; several items ask how in its dialog). - if id == "newComp" + // Effects; several items ask how in its dialog). Dropped on Create a new Folder: a new + // folder holding them (#3196). The dragged item, or the selection it belongs to. + if matches!(id, "newComp" | "newFolder") && let Some(DragPayload::Item(iid)) = egui::DragAndDrop::payload::(&ctx).as_deref() && ui.rect_contains_pointer(r) { p.rect_stroke(r, 4.0, Stroke::new(2.0, t.accent), egui::StrokeKind::Inside); if ctx.input(|i| i.pointer.any_released()) { - if !app.session.state.project_selection.contains(&ItemId(*iid)) { - app.session.state.project_selection = vec![ItemId(*iid)]; + let selected = app.session.state.project_selection.contains(&ItemId(*iid)); + if id == "newComp" { + if !selected { + app.session.state.project_selection = vec![ItemId(*iid)]; + } + actions.push(("file.newCompFromSelection".into(), json!({}))); + } else { + let items: Vec = if selected { app.session.state.project_selection.iter().map(|i| i.0).collect() } else { vec![*iid] }; + actions.push(("project.newFolder".into(), json!({"items": items}))); } - actions.push(("file.newCompFromSelection".into(), json!({}))); egui::DragAndDrop::clear_payload(&ctx); } } diff --git a/crates/ui-egui/tests/it/ui_project_new_folder.rs b/crates/ui-egui/tests/it/ui_project_new_folder.rs index 63a1c6ae3..b86bf663d 100644 --- a/crates/ui-egui/tests/it/ui_project_new_folder.rs +++ b/crates/ui-egui/tests/it/ui_project_new_folder.rs @@ -143,6 +143,64 @@ fn the_file_menu_opens_the_name_in_view() { assert_eq!(name(&h, folder).as_deref(), Some("Renders")); } +/// Press on `from`, move to `to` in steps, release there. +fn drag(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: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + for k in 1..=10 { + h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (k as f32 / 10.0))); + h.step(); + } + h.input_mut().events.push(Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +fn item_named(h: &Harness<'_, EffectcraftApp>, name: &str) -> ItemId { + h.state().session.project.items.values().find(|i| i.name == name).map(|i| i.id).unwrap_or_else(|| panic!("no item {name}")) +} + +fn parent(h: &Harness<'_, EffectcraftApp>, id: ItemId) -> Option { + h.state().session.project.item(id).and_then(|i| i.parent) +} + +/// #3196: two selected items (one at the root, one in a folder) dragged onto the New Folder +/// button go into a new folder, out of the folder they were in, with its name open as the +/// button's; one undo takes it all back. +#[test] +fn items_dropped_on_the_new_folder_button_go_into_a_new_folder() { + let (mut h, bin) = harness(2); + let (a, b) = (item_named(&h, "Comp 000"), item_named(&h, "Comp 001")); + h.state_mut().session.execute("project.move", json!({"items": [b.0], "folder": bin.0})).unwrap(); + h.state_mut().session.state.project_selection = vec![a, b]; + h.run_steps(3); + let before = folders(&h); + let project = h.state().session.project.clone(); + let from = rect(&h, &format!("project.item.{}.name", a.0)).center(); + let to = rect(&h, "project.newFolder").center(); + drag(&mut h, from, to); + let [folder] = new_folders(&h, &before)[..] else { panic!("one new folder") }; + assert_eq!((parent(&h, a), parent(&h, b)), (Some(folder), Some(folder)), "both dragged items are in it"); + assert_eq!(parent(&h, folder), None, "where the button makes folders"); + assert_eq!(h.state().session.state.project_selection, [folder]); + assert!(renaming(&h, folder), "its name is open, as from the button"); + assert!(h.state().ui.project_open_folders.contains(&folder.0), "open, so the moved items stay in view"); + key(&mut h, Key::Escape); + assert_eq!(name(&h, folder).as_deref(), Some("Untitled Folder")); + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + assert_eq!(h.state().session.project, project, "one undo takes back the folder and the move"); + + // An item that is not selected goes alone. + h.state_mut().session.state.project_selection = vec![b]; + h.run_steps(3); + let before = folders(&h); + let from = rect(&h, &format!("project.item.{}.name", a.0)).center(); + drag(&mut h, from, to); + let [folder] = new_folders(&h, &before)[..] else { panic!("one new folder") }; + assert_eq!((parent(&h, a), parent(&h, b)), (Some(folder), Some(bin)), "only the dragged item"); +} + /// Agents and scripts: `engine.execute` only creates the folder. #[test] fn engine_execute_only_creates_the_folder() { From f3a9afda1475d0b3df893278913226acf11cb8a7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:14:09 +0100 Subject: [PATCH 033/350] Effect Controls: lock the panel to the layer it shows (#3193) The Effect Controls tab gets the lock icon the Composition viewers and the Timeline have (panel.tab.EffectControls.lock). Locked, the panel and its tab name keep the layer they showed while that layer's comp is active, whatever is selected next, so a pick whip from another layer's property can reach its parameters. An effect dropped on the locked panel goes to its layer and leaves the selection alone; one applied to another layer says the panel is locked. Deleting the layer or its comp, or replacing the project, unlocks the panel, which follows the selection again. The lock is UI state (UiState::effect_controls_lock) and agents set it with ui.set {effectControlsLocked}. Fixes #3193 --- crates/ui-egui/src/app/frame.rs | 1 + crates/ui-egui/src/control.rs | 6 + crates/ui-egui/src/dock.rs | 4 +- crates/ui-egui/src/dock_ui.rs | 30 +++- crates/ui-egui/src/panels/effect_controls.rs | 11 +- .../src/panels/effect_controls/common.rs | 74 ++++++++- .../src/panels/effect_controls/panel.rs | 9 +- crates/ui-egui/src/state.rs | 21 +++ crates/ui-egui/tests/it/ui_effect_controls.rs | 155 ++++++++++++++++++ docs/control-protocol.md | 2 +- 10 files changed, 296 insertions(+), 17 deletions(-) diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index e77f07840..56794cfe4 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -421,6 +421,7 @@ impl EffectcraftApp { crate::color_picker::note_dialog(&ctx, self.dialog); // After the shortcuts, menus and header, which can replace the project (New, Open). self.ui.timeline.scope_links(self.session.script_ui.project_epoch); + panels::effect_controls::scope_lock(self); self.dock_area(ui, body); #[cfg(not(target_arch = "wasm32"))] prefs_live::dropped_files(self, ui, full); diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 86bd20c93..f6c255d69 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -281,6 +281,12 @@ fn set_ui(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome { { return err(e); } + // Effect Controls' tab lock: on locks it to the layer it shows. + if let Some(on) = p.get("effectControlsLocked").and_then(Value::as_bool) + && crate::panels::effect_controls::set_lock(app, on) != on + { + return err("effectControlsLocked: Effect Controls shows no layer to lock"); + } set_ui_layout(app, p) } diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index c8ada9bf8..5f03af14c 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -676,8 +676,8 @@ pub fn doc_tabs( out } -/// A viewer lock button (Composition viewers and the Timeline keep their comp while locked): a -/// closed padlock while locked, an open one while unlocked. +/// A panel lock button (Composition viewers and the Timeline keep their comp while locked, +/// Effect Controls its layer): a closed padlock while locked, an open one while unlocked. /// Returns true when clicked. Automation: `panel.tab..lock`. pub fn lock_button(ui: &mut egui::Ui, rect: Rect, panel: PanelKind, locked: bool, t: &Tokens, reg: &mut crate::automation::Registry) -> bool { let resp = ui.interact(rect, egui::Id::unique(("tab-lock", panel.id())), Sense::click()); diff --git a/crates/ui-egui/src/dock_ui.rs b/crates/ui-egui/src/dock_ui.rs index 6523ff757..1fbaa6f83 100644 --- a/crates/ui-egui/src/dock_ui.rs +++ b/crates/ui-egui/src/dock_ui.rs @@ -333,8 +333,11 @@ impl EffectcraftApp { } } let Some(comp) = self.session.active_comp() else { return out }; - if let Some(l) = self.session.state.selected_layers.first().and_then(|id| comp.layer(*id)) { + // Effect Controls names the layer it shows (the one it is locked to, if any). + if let Some(l) = panels::effect_controls::shown_layer_id(self).and_then(|id| comp.layer(id)) { out.push((PanelKind::EffectControls, format!("{} {}", crate::i18n::command_label("window.panel", "Effect Controls"), l.name))); + } + if let Some(l) = self.session.state.selected_layers.first().and_then(|id| comp.layer(*id)) { out.push((PanelKind::Properties, format!("{}: {}", crate::i18n::command_label("window.panel", "Properties"), l.name))); } out @@ -492,7 +495,13 @@ impl EffectcraftApp { } } for p in locks { - if !self.ui.locked_tabs.remove(&p.id()) { + if p == PanelKind::EffectControls { + // Locks on the layer the panel shows (nothing to lock without one). + let on = self.ui.effect_controls_lock.is_none(); + if panels::effect_controls::set_lock(self, on) != on { + self.ui.status = crate::i18n::tr("Select a layer to lock Effect Controls").to_string(); + } + } else if !self.ui.locked_tabs.remove(&p.id()) { self.ui.locked_tabs.insert(p.id().to_string()); } } @@ -638,10 +647,19 @@ fn dock_style(ui: &egui::Ui, t: &Tokens) -> DockStyle { style } -/// Panels with a viewer lock: the Composition viewers and the Timeline keep their comp while -/// locked. +/// Panels with a lock: the Composition viewers and the Timeline keep their comp while locked, +/// Effect Controls its layer. fn has_lock(panel: PanelKind) -> bool { - matches!(panel, PanelKind::Composition | PanelKind::Viewer(_) | PanelKind::Timeline) + matches!(panel, PanelKind::Composition | PanelKind::Viewer(_) | PanelKind::Timeline | PanelKind::EffectControls) +} + +/// Whether `panel`'s tab lock is on. Effect Controls keeps the layer it is locked to +/// (`UiState::effect_controls_lock`), the others are in `UiState::locked_tabs`. +fn is_locked(app: &EffectcraftApp, panel: PanelKind) -> bool { + match panel { + PanelKind::EffectControls => app.ui.effect_controls_lock.is_some(), + _ => app.ui.locked_tabs.contains(&panel.id()), + } } /// EffectCraft's side of the dock area for one frame. @@ -865,7 +883,7 @@ impl DockContent for EffectContent<'_> { let menu = Rect::from_min_max(pos2((header.max.x - MENU_W).max(header.min.x), header.min.y), header.max); if has_lock(*active) { let lock = Rect::from_center_size(pos2(menu.min.x - LOCK_W / 2.0, header.center().y), vec2(14.0, 14.0)); - let locked = self.app.ui.locked_tabs.contains(&active.id()); + let locked = is_locked(self.app, *active); if dock::lock_button(ui, lock, *active, locked, &t, &mut self.app.auto) { self.locks.push(*active); } diff --git a/crates/ui-egui/src/panels/effect_controls.rs b/crates/ui-egui/src/panels/effect_controls.rs index d9706f147..83f502988 100644 --- a/crates/ui-egui/src/panels/effect_controls.rs +++ b/crates/ui-egui/src/panels/effect_controls.rs @@ -1,6 +1,13 @@ //! Effect Controls: the selected layer's effects with all parameters, in After Effects' //! conventions: //! +//! - the tab's lock icon keeps the panel on the layer it shows whatever is selected next (a +//! pick whip from another layer's property can then reach its parameters); deleting that +//! layer or replacing the project unlocks it (see [`set_lock`]). The lock holds while the +//! locked layer's comp is the active comp: with another comp active the panel shows that +//! comp's selection (its edits address layers of the active comp), and the locked layer +//! again when its comp is back. The Effect menu of a right-click on empty space applies to +//! the selected layers, as in After Effects; //! - effect headers with the fx switch, twirl, Reset and About…; click selects the effect //! (Edit ▸ Copy / Paste / Duplicate and Delete then act on it), drag reorders the stack, the //! context menu copies, pastes, duplicates, resets and removes; @@ -34,8 +41,8 @@ mod panel; mod rows; mod viewer; -pub(crate) use common::gesture_key; -pub use common::{choose_files_after_apply, reveal_applied}; +pub use common::{choose_files_after_apply, reveal_applied, set_lock}; +pub(crate) use common::{gesture_key, scope_lock, shown_layer_id}; pub use panel::{show, start_rename}; pub(crate) use rows::toggle_pick; pub use viewer::viewer_hook; diff --git a/crates/ui-egui/src/panels/effect_controls/common.rs b/crates/ui-egui/src/panels/effect_controls/common.rs index ab46a07ac..49bb514dd 100644 --- a/crates/ui-egui/src/panels/effect_controls/common.rs +++ b/crates/ui-egui/src/panels/effect_controls/common.rs @@ -27,10 +27,66 @@ pub(super) fn effect_active(g: &PropGroup, layer: effectcraft_engine::project::L selected.iter().any(|(l, uid)| *l == layer && contains(g, *uid, 0)) } -pub(super) fn selected_layer(app: &EffectcraftApp) -> Option { +/// The layer Effect Controls shows: the layer it is locked to (its tab's lock) while that layer's +/// comp is the active comp, else the first selected layer of the active comp. +pub(crate) fn shown_layer_id(app: &EffectcraftApp) -> Option { let comp = app.session.active_comp()?; - let id = app.session.state.selected_layers.first()?; - comp.layer(*id).cloned() + if let Some(lock) = app.ui.effect_controls_lock + && app.session.active_comp_id().is_some_and(|c| c.0 == lock.comp) + && comp.layer(effectcraft_engine::project::LayerId(lock.layer)).is_some() + { + return Some(effectcraft_engine::project::LayerId(lock.layer)); + } + app.session.state.selected_layers.first().copied().filter(|id| comp.layer(*id).is_some()) +} + +pub(super) fn shown_layer(app: &EffectcraftApp) -> Option { + let id = shown_layer_id(app)?; + app.session.active_comp()?.layer(id).cloned() +} + +/// Lock Effect Controls on the layer it shows (`on`), or unlock it. Locking with no layer shown +/// does nothing; locking while already locked keeps the layer it is locked to. Returns whether +/// the panel is locked afterwards. The tab's lock icon and `ui.set {effectControlsLocked}`. +pub fn set_lock(app: &mut EffectcraftApp, on: bool) -> bool { + if !on { + app.ui.effect_controls_lock = None; + return false; + } + if app.ui.effect_controls_lock.is_some() { + return true; + } + let Some(comp) = app.session.active_comp_id() else { return false }; + let Some(layer) = shown_layer_id(app) else { return false }; + app.ui.effect_controls_lock = Some(crate::state::EffectControlsLock { comp: comp.0, layer: layer.0 }); + app.ui.effect_controls_lock_epoch = app.session.script_ui.project_epoch; + true +} + +/// Once a frame: a lock made in another project (New, Open: the ids start over) or on a layer or +/// comp since deleted is dropped, whether or not Effect Controls is on screen. +pub(crate) fn scope_lock(app: &mut EffectcraftApp) { + let epoch = app.session.script_ui.project_epoch; + if app.ui.effect_controls_lock_epoch != epoch { + app.ui.effect_controls_lock_epoch = epoch; + app.ui.effect_controls_lock = None; + } + prune_lock(app); +} + +/// A lock whose layer or comp no longer exists (deleted, or its comp deleted) is dropped, so the +/// panel follows the selection again and its tab shows it unlocked. Another comp being active +/// keeps the lock: the panel shows the locked layer again when its comp comes back. +pub(super) fn prune_lock(app: &mut EffectcraftApp) { + let Some(lock) = app.ui.effect_controls_lock else { return }; + let alive = app + .session + .project + .comp(effectcraft_engine::project::ItemId(lock.comp)) + .is_some_and(|c| c.layer(effectcraft_engine::project::LayerId(lock.layer)).is_some()); + if !alive { + app.ui.effect_controls_lock = None; + } } /// An effect was just applied (`effect.apply` / `effect.applyLast` with `params`): Effect @@ -39,10 +95,14 @@ pub(super) fn selected_layer(app: &EffectcraftApp) -> Option { pub fn reveal_applied(app: &mut EffectcraftApp, params: &serde_json::Value) { // `effect.apply` selects the new effect on (the last of) the layers it went to. let target = app.session.state.selected_props.last().map(|(l, _)| *l); + // Effect Controls locked to a layer of the active comp keeps showing that layer. + let locked = app.ui.effect_controls_lock.filter(|k| app.session.active_comp_id().is_some_and(|c| c.0 == k.comp)); if let Some(l) = target && !app.session.state.selected_layers.contains(&l) && params.get("comp").is_none() && app.session.active_comp().is_some_and(|c| c.layer(l).is_some()) + // Dropped on the locked panel: it shows that layer already, the selection stays. + && locked.is_none_or(|k| k.layer != l.0) { // Show that layer (dropped on an unselected one), keeping the new effect selected. let props = app.session.state.selected_props.clone(); @@ -51,6 +111,14 @@ pub fn reveal_applied(app: &mut EffectcraftApp, params: &serde_json::Value) { Err(e) => app.ui.status = e.to_string(), } } + // The effect went to another layer than the one the locked panel shows: say why it isn't + // there (After Effects opens another Effect Controls tab). + if let (Some(k), Some(l)) = (locked, target) + && k.layer != l.0 + && let Some(name) = app.session.active_comp().and_then(|c| c.layer(effectcraft_engine::project::LayerId(k.layer))).map(|x| x.name.clone()) + { + app.ui.status = crate::i18n::tr_args("Effect Controls is locked to {}", &[&name]); + } app.raise_panel(crate::dock::PanelKind::EffectControls); } diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 0857dfa3f..95f810d1a 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -1,4 +1,4 @@ -use super::common::{Actions, PARAM_NAME_X, ROW, effect_active, selected_layer}; +use super::common::{Actions, PARAM_NAME_X, ROW, effect_active, prune_lock, shown_layer}; use super::editors::{curves_editor, editor_height, histogram_only, levels_editor, object_matte_editor, roto_editor, warp_editor}; use super::rows::{FxScope, group_rows, toggle_closed}; use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, PropGroup, Rect, Sense, Stroke, Tokens, json, pos2, vec2, widgets}; @@ -158,7 +158,8 @@ fn about_button(app: &mut EffectcraftApp, ui: &mut egui::Ui, bp: &egui::Painter, } } -/// The Effect Controls panel for the selected layer: a header with the comp and layer names, then +/// The Effect Controls panel for the selected layer (or the layer the panel is locked to, see +/// `common::shown_layer_id`): a header with the comp and layer names, then /// one header per effect (Fx toggle, twirl, rename, Reset, About, label bar, reorder drag and its /// right-click menu) followed by that effect's custom editor and its property rows. Dragging /// effects or presets onto the panel applies them; a right-click on empty space opens the Effect @@ -168,7 +169,9 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let t = app.tokens; let p = ui.painter().with_clip_rect(rect); let ctx = ui.ctx().clone(); - let Some(layer) = selected_layer(app) else { + // A locked layer that was deleted unlocks the panel, which then follows the selection. + prune_lock(app); + let Some(layer) = shown_layer(app) else { p.text(rect.center(), Align2::CENTER_CENTER, crate::i18n::tr("Select a layer to see its effects"), Tokens::ui(12.0), t.text_faint); return; }; diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 60a30e97e..34336c297 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -594,6 +594,14 @@ impl Default for AnimToolsState { } } +/// The layer a locked Effect Controls panel keeps showing (the tab's lock icon, as After Effects' +/// panel lock): comp item id and layer id. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct EffectControlsLock { + pub comp: u64, + pub layer: u64, +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum ProjectStatus { @@ -651,6 +659,17 @@ pub struct UiState { /// bring a locked panel forward (the tab's lock icon, as After Effects' viewer lock). #[serde(default)] pub locked_tabs: BTreeSet, + /// Effect Controls locked to a layer (its tab's lock icon): the panel keeps showing that + /// layer whatever is selected while its comp is the active comp. Cleared when the layer or + /// its comp is deleted, and when the project is replaced (see + /// `panels::effect_controls::set_lock`). + #[serde(default)] + pub effect_controls_lock: Option, + /// The project the lock was made in (`ScriptUi::project_epoch`): a new or opened project + /// reuses comp and layer ids, so its lock is dropped (see + /// `panels::effect_controls::scope_lock`). + #[serde(skip)] + pub effect_controls_lock_epoch: u64, /// The comp each Composition viewer shows, by viewer id (0 = the Composition panel; see /// `panels::viewers`). #[serde(default)] @@ -773,6 +792,8 @@ impl Default for UiState { native_outer: Vec::new(), maximized: None, locked_tabs: BTreeSet::new(), + effect_controls_lock: None, + effect_controls_lock_epoch: 0, viewers: Default::default(), active_viewer: 0, focused: PanelKind::Composition, diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index 1db059220..d72febf01 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -912,3 +912,158 @@ fn double_click(h: &mut Harness<'_, EffectcraftApp>, pos: egui::Pos2) { } h.run_steps(2); } + +/// Two solids in one comp, `A` with a Point Control and `B` with an Angle Control, `A` selected +/// and Effect Controls open: the app, A, B and their effects. +fn two_layers() -> (EffectcraftApp, u64, u64, u64, u64) { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Main", "width": 320, "height": 180, "frameRate": 30, "duration": 5})).unwrap(); + let a = s.execute("layer.newSolid", json!({"name": "A", "color": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + let b = s.execute("layer.newSolid", json!({"name": "B", "color": "#0000ff"})).unwrap()["layer"].as_u64().unwrap(); + let fa = s.execute("effect.apply", json!({"layers": [a], "effect": "Point Control"})).unwrap()["effects"][0].as_u64().unwrap(); + let fb = s.execute("effect.apply", json!({"layers": [b], "effect": "Angle Control"})).unwrap()["effects"][0].as_u64().unwrap(); + s.state.selected_layers = vec![LayerId(a)]; + s.state.selected_props.clear(); + let mut app = EffectcraftApp::new(s); + app.show_panel(PanelKind::EffectControls); + (app, a, b, fa, fb) +} + +/// Whether Effect Controls drew effect `fx`'s header in the last frame (not the frame before: +/// a stale id must not pass). +fn shows_effect(h: &Harness<'_, EffectcraftApp>, fx: u64) -> bool { + let id = format!("effectControls.effect.{fx}"); + h.state().auto.elements.iter().any(|e| e.id == id) +} + +fn select_layer(h: &mut Harness<'_, EffectcraftApp>, layer: u64) { + h.state_mut().session.execute("layer.select", json!({"layers": [layer]})).unwrap(); + h.run_steps(3); +} + +/// #3193: the Effect Controls tab's lock keeps the panel (and its tab name) on the layer it showed +/// when locked whatever is selected next, through undo; deleting that layer unlocks it without a +/// crash and the panel follows the selection again. +#[test] +fn locked_effect_controls_keep_their_layer_until_it_is_deleted() { + let (app, a, b, fa, fb) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb), "A is shown before locking"); + let comp = h.state().session.active_comp_id().unwrap().0; + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock, Some(effectcraft_ui_egui::state::EffectControlsLock { comp, layer: a })); + // Selecting B keeps A in the panel and its tab. + select_layer(&mut h, b); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb), "the locked panel keeps A"); + let tab = h.state().auto.elements.iter().find(|e| e.id == "panel.tab.EffectControls").map(|e| e.label.clone()).unwrap_or_default(); + assert!(tab.ends_with(" A"), "the tab names the locked layer: {tab:?}"); + // An edit and its undo leave the lock alone. + h.state_mut().session.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 50.0})).unwrap(); + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + h.run_steps(3); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb), "undo keeps the lock"); + // The lock state is UI state: it survives a serde round trip. + let ui = serde_json::to_value(&h.state().ui).unwrap(); + let back: effectcraft_ui_egui::state::UiState = serde_json::from_value(ui).unwrap(); + assert_eq!(back.effect_controls_lock, h.state().ui.effect_controls_lock); + // Unlocking follows the selection again. + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock, None); + assert!(shows_effect(&h, fb) && !shows_effect(&h, fa), "unlocked, the panel shows the selection"); + // Lock on A again, select B, delete A: no crash, the lock is gone, B is shown. + select_layer(&mut h, a); + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + select_layer(&mut h, b); + h.state_mut().session.execute("edit.clear", json!({"layers": [a]})).unwrap(); + h.run_steps(3); + assert_eq!(h.state().ui.effect_controls_lock, None, "deleting the locked layer unlocks the panel"); + assert!(!shows_effect(&h, fa), "A is gone from the panel"); + // Deleting clears the selection; the panel follows the next one. + select_layer(&mut h, b); + assert!(shows_effect(&h, fb), "the panel follows the selection"); + // Undoing the delete brings A back without bringing back (or breaking) the lock. + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + h.run_steps(3); + assert_eq!(h.state().ui.effect_controls_lock, None); + let selected = h.state().session.state.selected_layers.first().map(|l| l.0); + let (on, off) = if selected == Some(a) { (fa, fb) } else { (fb, fa) }; + assert!(shows_effect(&h, on) && !shows_effect(&h, off), "the panel shows the selection ({selected:?})"); +} + +/// #3193: a new or opened project reuses comp and layer ids, so a lock does not carry into it, +/// even onto a layer with the locked one's ids. +#[test] +fn replacing_the_project_unlocks_effect_controls() { + let (app, a, b, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + select_layer(&mut h, b); + // The same project opened again: every id is alive. + let project = (*h.state().session.project).clone(); + h.state_mut().session.replace_project(project, None); + h.run_steps(3); + assert_eq!(h.state().ui.effect_controls_lock, None, "the lock belongs to the replaced project"); +} + +/// #3193: an effect dropped on the locked panel goes to the locked layer and leaves the selection +/// alone; one applied to another layer says why the panel doesn't show it. +#[test] +fn applying_an_effect_while_locked_keeps_the_selection() { + let (app, a, b, fa, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + select_layer(&mut h, b); + let ctx = h.ctx.clone(); + effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "effect.apply", json!({"effect": "Color Control", "layers": [a]})).unwrap(); + h.run_steps(3); + assert_eq!(h.state().session.state.selected_layers, vec![LayerId(b)], "the selection stays on B"); + assert!(shows_effect(&h, fa), "the panel still shows A"); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + // Applied to the selected B: the panel stays on A and says so. + h.state_mut().ui.status.clear(); + effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "effect.apply", json!({"effect": "Color Control", "layers": [b]})).unwrap(); + h.run_steps(3); + assert!(shows_effect(&h, fa)); + assert_eq!(h.state().ui.status, "Effect Controls is locked to A"); +} + +/// #3193: agents lock Effect Controls with `ui.set {effectControlsLocked}`; with no layer shown +/// there is nothing to lock. +#[test] +fn effect_controls_lock_over_the_control_channel() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>, params: serde_json::Value) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", params); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, b, fa, fb) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h, json!({"effectControlsLocked": true}))["ok"], true); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + select_layer(&mut h, b); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb)); + assert_eq!(send(&mut h, json!({"effectControlsLocked": false}))["ok"], true); + h.run_steps(3); + assert!(shows_effect(&h, fb) && !shows_effect(&h, fa)); + // Nothing selected: nothing to lock. + h.state_mut().session.state.selected_layers.clear(); + h.run_steps(2); + assert_eq!(send(&mut h, json!({"effectControlsLocked": true}))["ok"], false); + assert_eq!(h.state().ui.effect_controls_lock, None); +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 9f3d69ea5..8af641a3a 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -66,7 +66,7 @@ panel registers an element id; both are enforced by `crates/ui-egui/tests/it/ui_ |---|---|---| | `ui.inspect` | `{}` | Window size, pixels per point, fps, serialized `UiState`, playback, time, active comp, selection, open dialog, last render ms, `keyboardFocus`, `windowFocused`, `workspaceStorageError`. | | `ui.elements` | `{prefix?}` | `[{id, label, rect: [x, y, w, h]}]`: the widgets drawn in the last frame, in points. | -| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | +| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, effectControlsLocked?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `effectControlsLocked: true` locks Effect Controls on the layer it shows (an error when it shows none), `false` unlocks it (the tab's lock icon, `panel.tab.EffectControls.lock`; `ui.inspect` reports it as `effect_controls_lock: {comp, layer}`); `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | | `ui.panel.show` / `ui.panel.close` | `{panel}` | Panels: `Project`, `Composition`, `Timeline`, `EffectControls`, `EffectsPresets`, `Info`, `Preview`, `Character`, `Paragraph`, `Align` and others. | | `ui.playback` | `{action: play\|stop\|toggle\|status}` | `{playing, time, audio, levelsDb, peaksDb}` (audio: an audio preview is running; L/R meter levels and held peaks in dBFS) | | `ui.resize` | `{width, height}` | Resizes the window (points). | From 714ff82fd64a7e3d8550590aad209377e21b94a5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:35:14 +0100 Subject: [PATCH 034/350] Viewer: a Shift-constrained drag snaps only along its axis A layer Move, path-vertex drag or Pan Behind drag held to one axis with Shift snapped with the full 2D rule, so a point target (another layer's corner or midpoint, a path vertex, the comp centre) gave the snap both of its coordinates and pulled the dragged feature off the axis. The new engine function viewer::snap_along snaps only the free coordinate: a point acts as a line through it across the drag, reaching it within the tolerance on each axis; lines along the drag are left out. A point hit reports the point and the short line from it to where the feature landed, so the viewer draws the alignment. The viewer's SnapQuery carries the axis one_axis picks (drag_axis); Move, Vertices and Pan Behind pass it. snap() itself is unchanged. Fixes #3229 --- crates/engine/src/tests/viewer.rs | 33 ++++++++ crates/engine/src/viewer.rs | 44 ++++++++++ crates/ui-egui/src/panels/viewer.rs | 24 ++++-- crates/ui-egui/src/panels/viewer/geometry.rs | 16 ++-- crates/ui-egui/src/panels/viewer_tools.rs | 15 +++- crates/ui-egui/tests/it/ui_viewer.rs | 87 ++++++++++++++++++++ 6 files changed, 201 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index 31197b2a8..9d3087f20 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -920,3 +920,36 @@ fn polystar_undo_sanitizes_live_and_parked_property_selections_before_uid_reuse( assert_eq!(layer(&s, lid).props.prop("transform/positionZ").unwrap(), &position_z, "the reused UID must not become an implicit expression target"); } } + +/// #3229: a drag Shift keeps to one axis snaps only that axis. A point a few pixels off the axis +/// lines the feature up with it (along x: its x) without taking it off the axis, as a line +/// through the point would; lines along the drag are left out, and the point reaches only `tol` +/// off the axis. Unconstrained (`snap`) the same point takes both coordinates, the feature leaving +/// the axis. +#[test] +fn a_shift_drag_snaps_only_along_its_axis() { + use crate::viewer::snap_along; + let src = SnapSource::Layer(LayerId(1)); + let corner = SnapTarget::point([450.0, 150.0], PRI_LAYER, src); + let row = SnapTarget::hline(151.0, PRI_GUIDE, SnapSource::Guide); + let column = SnapTarget::vline(449.0, PRI_GUIDE, SnapSource::Guide); + let feature = [[448.0, 153.0]]; + assert_eq!(snap(&feature, &[corner, row], 8.0).unwrap().at, [450.0, 150.0]); + // Along x: x onto the corner's, y kept (the nearer row does not pull it either). + let s = snap_along(&feature, &[corner, row], 8.0, true).unwrap(); + assert_eq!(s.delta, [2.0, 0.0]); + assert_eq!(s.at, [450.0, 153.0]); + // The point, and the line from it to the feature (the feedback). + assert_eq!(s.hits, [corner, SnapTarget::vline(450.0, PRI_LAYER, src).within(Some([150.0, 153.0]))]); + // Along y: y onto the corner's, x kept (the column is left out). + let s = snap_along(&feature, &[corner, column], 8.0, false).unwrap(); + assert_eq!(s.delta, [0.0, -3.0]); + assert_eq!(s.at, [448.0, 150.0]); + assert_eq!(s.hits, [corner, SnapTarget::hline(150.0, PRI_LAYER, src).within(Some([450.0, 448.0]))]); + // A line across the drag still snaps; a nearer one wins over the point. + let s = snap_along(&feature, &[corner, column], 8.0, true).unwrap(); + assert_eq!(s.at, [449.0, 153.0]); + assert_eq!(s.hits, [column]); + // The point reaches only the tolerance off the axis. + assert_eq!(snap_along(&[[448.0, 170.0]], &[corner], 8.0, true), None); +} diff --git a/crates/engine/src/viewer.rs b/crates/engine/src/viewer.rs index f8e4c7740..fd9cb220a 100644 --- a/crates/engine/src/viewer.rs +++ b/crates/engine/src/viewer.rs @@ -189,6 +189,50 @@ pub fn layer_features(ctx: &EvalCtx, layer: &Layer) -> Vec<[f64; 2]> { out } +/// [`snap`] for a drag Shift keeps to one axis (`along_x`: it moves along x and y stays put, else +/// the other way round): only the free coordinate snaps, so the feature never leaves the axis +/// (#3229). A point target acts as a line through it across the drag (a vertical line at its x +/// for a drag along x) that reaches it only within `tol` on each axis (a square around the point), +/// so the feature lines up with a point near the axis, not with every point in the comp; lines +/// along the drag (horizontal ones for a drag along x) would only move the locked coordinate and +/// are left out. The nearest target wins, ties to the lower priority value. A hit on a point +/// reports the point (the feedback box) and the line from it to where the feature landed. +pub fn snap_along(sources: &[[f64; 2]], targets: &[SnapTarget], tol: f64, along_x: bool) -> Option { + // (Key, source, target, and the free coordinate it snaps to.) + let mut best: Option<((f64, u8), [f64; 2], SnapTarget, f64)> = None; + for s in sources { + let (free, along) = if along_x { (s[0], s[1]) } else { (s[1], s[0]) }; + for t in targets { + // The line the target snaps as: its coordinate and extent along it. + let (line, span) = match (t.kind, along_x) { + (SnapKind::Point, true) => (t.pos[0], Some([t.pos[1] - tol, t.pos[1] + tol])), + (SnapKind::Point, false) => (t.pos[1], Some([t.pos[0] - tol, t.pos[0] + tol])), + (SnapKind::VLine, true) => (t.pos[0], t.span), + (SnapKind::HLine, false) => (t.pos[1], t.span), + (SnapKind::VLine, false) | (SnapKind::HLine, true) => continue, + }; + let d = (line - free).abs(); + if d > tol || span.is_some_and(|[a, b]| along < a || along > b) { + continue; + } + let key = (d, t.priority); + if best.as_ref().is_none_or(|(k, _, _, _)| better(key, *k)) { + best = Some((key, *s, *t, line)); + } + } + } + let (_, s, t, line) = best?; + let (delta, at) = if along_x { ([line - s[0], 0.0], [line, s[1]]) } else { ([0.0, line - s[1]], [s[0], line]) }; + let mut hits = vec![t]; + if t.kind == SnapKind::Point { + // The alignment, drawn from the point to the feature. + let (from, to) = if along_x { (t.pos[1], at[1]) } else { (t.pos[0], at[0]) }; + let l = if along_x { SnapTarget::vline(line, t.priority, t.source) } else { SnapTarget::hline(line, t.priority, t.source) }; + hits.push(l.within(Some([from, to]))); + } + Some(Snap { delta, hits, at }) +} + /// A layer's content box in comp space: corners (top left, top right, bottom right, bottom /// left), edge midpoints (top, right, bottom, left) and centre. struct BoxPoints { diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index b1899e1f8..d84626b45 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -1448,10 +1448,13 @@ fn phase_drag(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: } } Gesture::Move { layers, start, snap_src } => { - let mut d = one_axis([cpt[0] - start[0], cpt[1] - start[1]], mods.shift); + let raw = [cpt[0] - start[0], cpt[1] - start[1]]; + let mut d = one_axis(raw, mods.shift); if let Some(src) = snap_src { let ids: Vec = layers.iter().map(|x| x.0).collect(); - let c = vt::snap(app, &ctx, ectx, &map, &ids, &[[src[0] + d[0], src[1] + d[1]]], mods); + // Shift: only the coordinate along the axis snaps, so the layer stays on it (#3229). + let q = vt::SnapQuery { exclude: &ids, sources: &[[src[0] + d[0], src[1] + d[1]]], mods, extra: &[], along_x: drag_axis(raw, mods.shift) }; + let c = vt::snap_with(app, &ctx, ectx, &map, &q); d = [d[0] + c[0], d[1] + c[1]]; if vt::snapping_on(app, mods) { vt::draw_handle(&painter, map.to_screen([src[0] + d[0], src[1] + d[1]])); @@ -1491,10 +1494,12 @@ fn phase_drag(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: Gesture::Anchor { layer, start_anchor, start_pos, start, inv, l2p, own, anchor_only } => { // Pan Behind snaps the anchor point (to its own layer's box, other layers' features, // guides and the grid); Shift keeps the move to one axis. - let d = one_axis([cpt[0] - start[0], cpt[1] - start[1]], mods.shift); + let raw = [cpt[0] - start[0], cpt[1] - start[1]]; + let d = one_axis(raw, mods.shift); let anchor_c = inv.inverse().map(|m| m.apply(gv2(start_anchor[0], start_anchor[1]))).unwrap_or(gv2(start[0], start[1])); let want = [anchor_c.x + d[0], anchor_c.y + d[1]]; - let sc = vt::snap_with(app, &ctx, ectx, &map, &vt::SnapQuery { exclude: &[layer], sources: &[want], mods, extra: &own }); + let q = vt::SnapQuery { exclude: &[layer], sources: &[want], mods, extra: &own, along_x: drag_axis(raw, mods.shift) }; + let sc = vt::snap_with(app, &ctx, ectx, &map, &q); let a = inv.apply(gv2(start[0], start[1])); let b = inv.apply(gv2(start[0] + d[0] + sc[0], start[1] + d[1] + sc[1])); let dl = gv2(b.x - a.x, b.y - a.y); @@ -1528,9 +1533,12 @@ fn phase_drag(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: } Gesture::Vertices { start, src, applied, inv, layer } => { // The grabbed vertex snaps (to other layers' vertices and features, guides, grid); - // Shift keeps it to one axis, as a layer move does (#1964). - let want = one_axis([cpt[0] - start[0], cpt[1] - start[1]], mods.shift); - let c = vt::snap(app, &ctx, ectx, &map, &[layer], &[[src[0] + want[0], src[1] + want[1]]], mods); + // Shift keeps it to one axis, as a layer move does (#1964), snapping too (#3229). + let raw = [cpt[0] - start[0], cpt[1] - start[1]]; + let want = one_axis(raw, mods.shift); + let at = [src[0] + want[0], src[1] + want[1]]; + let q = vt::SnapQuery { exclude: &[layer], sources: &[at], mods, extra: &[], along_x: drag_axis(raw, mods.shift) }; + let c = vt::snap_with(app, &ctx, ectx, &map, &q); let total = [want[0] + c[0], want[1] + c[1]]; let d = inv.apply_vec(gv2(total[0] - applied[0], total[1] - applied[1])); if d.x != 0.0 || d.y != 0.0 { @@ -1993,7 +2001,7 @@ mod view; use frame::{empty_state, rect_of, show_frame}; pub(crate) use frame::{extended_region, minify_factor, snap_to_pixels, styled_line, zoom_sampler, zoom_texture_options}; use geometry::{ - anchor_in_comp, cam_state, drag_ratio, handle_axes, handle_cursor, handle_point, one_axis, project_segment, quad_intersects_box, rotate_value, + anchor_in_comp, cam_state, drag_axis, drag_ratio, handle_axes, handle_cursor, handle_point, one_axis, project_segment, quad_intersects_box, rotate_value, rotation_sign, selected_others, snap_handle, turn_delta, }; #[cfg(test)] diff --git a/crates/ui-egui/src/panels/viewer/geometry.rs b/crates/ui-egui/src/panels/viewer/geometry.rs index 144047f32..e597b50b3 100644 --- a/crates/ui-egui/src/panels/viewer/geometry.rs +++ b/crates/ui-egui/src/panels/viewer/geometry.rs @@ -260,15 +260,19 @@ pub(super) fn rotate_value(start_rot: f64, turned: f64, shift: bool) -> f64 { /// A drag `d` (comp pixels), kept to the axis it moves along most when `shift` is held. pub(super) fn one_axis(d: [f64; 2], shift: bool) -> [f64; 2] { - if !shift { - d - } else if d[0].abs() > d[1].abs() { - [d[0], 0.0] - } else { - [0.0, d[1]] + match drag_axis(d, shift) { + None => d, + Some(true) => [d[0], 0.0], + Some(false) => [0.0, d[1]], } } +/// The axis [`one_axis`] keeps a drag `d` to: `Some(true)` along x, `Some(false)` along y, `None` +/// without `shift` (both axes free). Its snapping keeps to the same axis (#3229). +pub(super) fn drag_axis(d: [f64; 2], shift: bool) -> Option { + shift.then(|| d[0].abs() > d[1].abs()) +} + pub(super) fn point_in_quad(p: [f64; 2], q: &[[f64; 2]; 4]) -> bool { let mut sign = 0.0; for i in 0..4 { diff --git a/crates/ui-egui/src/panels/viewer_tools.rs b/crates/ui-egui/src/panels/viewer_tools.rs index 563657e91..ab415fd9a 100644 --- a/crates/ui-egui/src/panels/viewer_tools.rs +++ b/crates/ui-egui/src/panels/viewer_tools.rs @@ -48,21 +48,23 @@ pub(crate) fn snap( sources: &[[f64; 2]], mods: egui::Modifiers, ) -> [f64; 2] { - snap_with(app, ctx, ectx, map, &SnapQuery { exclude, sources, mods, extra: &[] }) + snap_with(app, ctx, ectx, map, &SnapQuery { exclude, sources, mods, extra: &[], along_x: None }) } /// What [`snap_with`] snaps: the dragged features and the layers to ignore, plus `extra` layer -/// targets (the Pan Behind layer's own box). +/// targets (the Pan Behind layer's own box). `along_x` is the axis a Shift drag keeps to +/// (`Some(true)`: x), whose coordinate alone snaps ([`vw::snap_along`], #3229). pub(crate) struct SnapQuery<'a> { pub exclude: &'a [LayerId], pub sources: &'a [[f64; 2]], pub mods: egui::Modifiers, pub extra: &'a [vw::SnapTarget], + pub along_x: Option, } /// [`snap`] with extra layer targets. pub(crate) fn snap_with(app: &EffectcraftApp, ctx: &egui::Context, ectx: &EvalCtx, map: &ViewerMap, q: &SnapQuery) -> [f64; 2] { - let SnapQuery { exclude, sources, mods, extra } = *q; + let SnapQuery { exclude, sources, mods, extra, along_x } = *q; let layers = snapping_on(app, mods); let v = &app.ui.viewer; let guides = v.snap_guides && v.guides; @@ -75,7 +77,12 @@ pub(crate) fn snap_with(app: &EffectcraftApp, ctx: &egui::Context, ectx: &EvalCt if layers { targets.extend_from_slice(extra); } - match vw::snap(sources, &targets, SNAP_PX / map.zoom.max(1e-3) as f64) { + let tol = SNAP_PX / map.zoom.max(1e-3) as f64; + let hit = match along_x { + Some(x) => vw::snap_along(sources, &targets, tol, x), + None => vw::snap(sources, &targets, tol), + }; + match hit { Some(s) => { let d = s.delta; ctx.data_mut(|dd| dd.insert_temp(snap_id(), s)); diff --git a/crates/ui-egui/tests/it/ui_viewer.rs b/crates/ui-egui/tests/it/ui_viewer.rs index 7b32fa2ec..7cc504bd6 100644 --- a/crates/ui-egui/tests/it/ui_viewer.rs +++ b/crates/ui-egui/tests/it/ui_viewer.rs @@ -456,6 +456,37 @@ fn layer_drag_snaps_to_other_layers_midpoints() { } } +/// #3229: a layer Shift-dragged along x past another layer's corner a few pixels off the axis +/// lines up with the corner's x and stays on the axis (it used to jump onto the corner), with +/// Snap Edges Extended on and off. +#[test] +fn shift_drag_snaps_a_layer_along_its_axis_only() { + let mut h = harness(); + let box_id = h.state().session.active_comp().unwrap().layers[0].id; + // A 100×100 target at x 450–550, y 50–150: its bottom left corner at (450, 150). + let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"] + .as_u64() + .unwrap(); + for extended in [true, false] { + // The 80×80 box at x 60–140, y 153–233: its top right corner (140, 153) 3 px below the + // target's corner. + let s = &mut h.state_mut().session; + s.execute("view.snappingOptions", json!({"edgesExtended": extended})).unwrap(); + s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 100, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 193, 0]})).unwrap(); + s.execute("edit.deselectAll", json!({})).unwrap(); + h.run_steps(2); + // Grab the box by that corner and Shift-drag it right (and a pixel down), the corner to + // 2 px short of the target corner's x. + let (from, to) = (screen(&h, [138.0, 155.0]), screen(&h, [446.0, 156.0])); + hold_drag(&mut h, from, to, egui::Modifiers::SHIFT); + let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone(); + let p = l.props.prop("transform/position").unwrap().value.as_vec3(); + assert!((p[0] - 410.0).abs() < 0.01, "the corner lines up with the target's, extended {extended}: {p:?}"); + assert!((p[1] - 193.0).abs() < 1e-6, "and the box stays on the axis, extended {extended}: {p:?}"); + } +} + /// With a shape layer selected the Rectangle tool draws a new group into its Contents (After /// Effects' behaviour); with nothing selected it draws a new shape layer (#227). #[test] @@ -927,6 +958,32 @@ fn shift_drag_keeps_a_mask_vertex_to_one_axis() { assert!(free[0] > shifted[0] && free[1] > shifted[1], "without Shift both axes move: {shifted:?} -> {free:?}"); } +/// #3229: a mask vertex Shift-dragged along x past another layer's corner a few pixels off the +/// axis lines up with the corner's x and stays on the axis (it used to jump onto the corner). +#[test] +fn shift_drag_snaps_a_mask_vertex_along_its_axis_only() { + let mut h = harness(); + let plate = layer_id(&h, "Plate"); + // The Box (80×80 at the comp centre) has its top left corner at (280, 140); the mask's first + // vertex starts 3 px below that row. + let corners = [[200.0, 143.0], [240.0, 100.0], [240.0, 60.0], [200.0, 60.0]]; + let mask = { + let s = &mut h.state_mut().session; + s.execute("layer.select", json!({"layers": [plate]})).unwrap(); + let m = s.execute("mask.new", json!({"layer": plate, "vertices": corners, "closed": true})).unwrap()["mask"].as_u64().unwrap(); + s.execute("mask.selectVertices", json!({"vertices": [{"layer": plate, "mask": m, "index": 0}]})).unwrap(); + m + }; + h.run_steps(2); + // Shift-drag it right (and a pixel down) to 2 px short of the corner's x. + let from = rect(&h, &format!("viewer.mask.{mask}.vertex.0")).center(); + let to = from + (screen(&h, [278.0, 144.0]) - screen(&h, [200.0, 143.0])); + drag_path(&mut h, &[from, to], egui::Modifiers::SHIFT); + let v = mask_path(&h, "Plate").vertices[0]; + assert!((v[0] - 280.0).abs() < 0.01, "the vertex lines up with the corner: {v:?}"); + assert!((v[1] - 143.0).abs() < 1e-6, "and stays on the axis: {v:?}"); +} + /// #999: a mask vertex dragged against the edge of the Composition panel pans the view, and the /// vertex keeps moving while the pointer is held there, as in After Effects. #[test] @@ -1392,6 +1449,36 @@ fn pan_behind_snaps_the_anchor_point() { assert!((a[0] - 260.0).abs() < 0.01 && (a[1] - 120.0).abs() < 0.01, "{a:?}"); } +/// #3229: Pan Behind Shift-dragging the anchor point along x past another layer's corner a few +/// pixels off the axis lines it up with the corner's x and keeps it on the axis (it used to jump +/// onto the corner). +#[test] +fn pan_behind_shift_drag_snaps_the_anchor_along_its_axis_only() { + let mut h = harness(); + let box_id = h.state().session.active_comp().unwrap().layers[0].id; + // A 100×100 target at x 450–550, y -3–97: its bottom left corner at (450, 97), 3 px above + // the anchor point's row. The anchor point (100, 100) sits 10 px above the box's centre, so + // none of the box's own lines runs along that row. + let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"] + .as_u64() + .unwrap(); + let s = &mut h.state_mut().session; + s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 47, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/anchor", "value": [40, 30, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap(); + s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap(); + h.state_mut().ui.tool = Tool::PanBehind; + h.run_steps(2); + // Shift-drag the anchor right (and a pixel down) to 2 px short of the corner's x. The drag + // starts in small steps (Pan Behind picks the layer where the drag starts, still inside the + // box) and holds the end point (the gesture applies the previous frame's pointer). + let path = [screen(&h, [100.0, 100.0]), screen(&h, [110.0, 100.0]), screen(&h, [448.0, 101.0]), screen(&h, [448.0, 101.0])]; + drag_path(&mut h, &path, egui::Modifiers::SHIFT); + let (p, a) = position_and_anchor(&h, box_id); + assert!((p[0] - 450.0).abs() < 0.01 && (a[0] - 390.0).abs() < 0.01, "the anchor point lines up with the corner: {p:?} {a:?}"); + assert!((p[1] - 100.0).abs() < 1e-6 && (a[1] - 30.0).abs() < 1e-6, "and stays on the axis: {p:?} {a:?}"); +} + /// Issue #147: Pan Behind snaps the anchor to its own layer's corners, edge midpoints and centre /// (only the anchor itself is left out: the box stays put while the anchor moves). #[test] From 61af9e5b725658ec1d8a394cfb6d2b14391ba0f7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:41:14 +0100 Subject: [PATCH 035/350] #3233: a text layer's name follows its text until the user renames it A text layer's name followed its Source Text whenever the name started with "Text", so a user's "Text Title" was replaced by the next text edit. The layer now records whether its name follows the text (`name_follows_text`, optional in the project file). A new text layer named after its text follows it; a name given at creation or by Rename (Enter, layer.rename, a script's layer.name, Layer Settings' name) is the user's and stays; renaming to an empty name shows the text again and follows it; committing the unchanged name changes nothing; copies keep the state. On-canvas typing and layer.setText share the rule. The name is the text's first paragraph, which ends at a line feed or a bare carriage return (After Effects' paragraph break). Projects saved without the state keep the old rule for the names that followed: a name that is empty, the text's own, or "Text"/"Text 2"... follows; any other name stays. AEP import records a stored layer name as the user's and no name as following, and names an empty text layer without a name "Text" instead of "Layer"; export writes no name for a following layer and keeps the name the user gave, even when it equals the text. Fixes #3233 --- crates/aep/src/export/plan.rs | 5 +- crates/aep/src/import/build.rs | 11 ++- crates/aep/tests/it/export/text.rs | 42 ++++++++++ crates/engine/src/cmd/layer.rs | 10 ++- crates/engine/src/cmd/layer/contents.rs | 12 +-- crates/engine/src/cmd/layer/create.rs | 4 +- crates/engine/src/cmd/layer/switches.rs | 26 ++++++- crates/engine/src/cmd/layer/transform.rs | 4 +- crates/engine/src/cmd/text_edit.rs | 15 ++-- crates/engine/src/tests/text_edit.rs | 99 ++++++++++++++++++++++++ crates/project/src/build.rs | 1 + crates/project/src/layer.rs | 26 +++++++ crates/project/src/lib.rs | 2 +- docs/agents.md | 2 + 14 files changed, 236 insertions(+), 23 deletions(-) diff --git a/crates/aep/src/export/plan.rs b/crates/aep/src/export/plan.rs index 9f45c50cd..f8aa2b3b8 100644 --- a/crates/aep/src/export/plan.rs +++ b/crates/aep/src/export/plan.rs @@ -1047,9 +1047,10 @@ fn plan_layer(cx: &PlanCx<'_>, at: CompAt<'_>, cp: &CompPlan, layer: &Layer, col let props = plan_transform(layer, &src, comp, source_size, cp, comp_name, col); let remap = remap_prop.filter(|_| remapped).map(|p| plan_remap(p, comp, cp, &layer.name, comp_name, col)); let mut name = cut_name(&layer.name, 255, &what("layer"), col); - // A text layer named after its text stores no name (110, 113: After Effects shows the text). + // A text layer named after its text stores no name (110, 113: After Effects shows the text): + // the layer's own rule decides (#3233). if let LayerSrc::Text(tp) = &src - && tp.text.split('\r').next().is_some_and(|first| first.chars().take(64).collect::() == name) + && layer.name_follows_text(&effectcraft_project::text_layer_name(&tp.text)) { name.clear(); } diff --git a/crates/aep/src/import/build.rs b/crates/aep/src/import/build.rs index 4ec346328..fa66ee411 100644 --- a/crates/aep/src/import/build.rs +++ b/crates/aep/src/import/build.rs @@ -479,7 +479,12 @@ fn build_layers(project: &mut Project, cx: &mut Ctx<'_>, defs: &EffectDefinition .map(|d| crate::import::text::first_paragraph(&d.raw_text)) .filter(|t| !t.trim().is_empty()) }; - let name = aep_name.clone().or_else(|| src.as_ref().map(|s| s.name.clone())).or_else(text_name).unwrap_or_else(|| "Layer".to_string()); + let name = aep_name + .clone() + .or_else(|| src.as_ref().map(|s| s.name.clone())) + .or_else(text_name) + // An empty text layer without a name is named as a new one is (#3233). + .unwrap_or_else(|| if h.layer_type == LayerType::Text { "Text" } else { "Layer" }.to_string()); let skip = |cx: &mut Ctx<'_>, skipped: &mut HashMap, category: LossCategory, what: &str| { cx.col.lose(category, format!("{comp_name}: {what} '{name}'")); skipped.insert(h.id, what.to_string()); @@ -537,6 +542,10 @@ fn build_layers(project: &mut Project, cx: &mut Ctx<'_>, defs: &EffectDefinition }; let mut l: Layer = build::layer(project, &comp, &name, source.clone(), size, None); l.name = name.clone(); + // A text layer without a stored name follows its text; a stored name is the user's (#3233). + if is_text { + l.name_follows_text = Some(aep_name.is_none()); + } l.label = Label::ALL.get(usize::from(h.label)).copied().unwrap_or(Label::None); apply_timing(&mut l, &h, &tm, &name, &mut cx.col); apply_switches(&mut l, &h); diff --git a/crates/aep/tests/it/export/text.rs b/crates/aep/tests/it/export/text.rs index 32c7572c5..32e55a269 100644 --- a/crates/aep/tests/it/export/text.rs +++ b/crates/aep/tests/it/export/text.rs @@ -196,6 +196,48 @@ fn documents_built_in_code_are_written_as_their_fields() { assert_eq!(layer.header().map(|h| h.layer_type), Some(effectcraft_aep::LayerType::Text)); } +/// #3233: whether a text layer's name follows its text survives export and import. A followed +/// name is written as no name (After Effects shows the text) and comes back following; a name the +/// user gave is written, even when it equals the text, and comes back as the user's. Without a +/// recorded state (older projects) a name equal to the text follows. +#[test] +fn a_text_layer_name_follows_its_text_through_export_and_import() { + for (name, follows, stored) in [ + ("Hello", Some(false), Some("Hello")), + ("Hello 2", Some(true), None), + ("Hello", None, None), + ("Text 2", None, None), + ("Text Title", None, Some("Text Title")), + ("Text Title", Some(false), Some("Text Title")), + ] { + let case = format!("{name:?} {follows:?}"); + let (mut p, c) = text_project(vec![TextDoc::plain("Hello")]); + with_layer(&mut p, c, 0, |l| { + l.name = name.into(); + l.name_follows_text = follows; + }); + let r = export_all(&p); + let file = parse(&r.bytes).expect("parse"); + let view = effectcraft_aep::Project::new(&file).expect("project"); + let layer = view.all_items().into_iter().find_map(|i| i.comp()).and_then(|c| c.layers().next()).expect("layer"); + assert_eq!(layer.name().as_deref(), stored, "stored name ({case})"); + let back = reimport(&r.bytes); + let l = back.comps().flat_map(|(_, c)| c.layers.iter()).find(|l| matches!(l.source, LayerSource::Text)).expect("text layer"); + assert_eq!(l.name, stored.unwrap_or("Hello"), "imported name ({case})"); + assert_eq!(l.name_follows_text, Some(stored.is_none()), "imported state ({case})"); + } + // An empty text layer that follows its text is written without a name, as After Effects + // writes it, and comes back named "Text", still following. + let (mut p, c) = text_project(vec![TextDoc::plain("")]); + with_layer(&mut p, c, 0, |l| { + l.name = "Text".into(); + l.name_follows_text = Some(true); + }); + let back = reimport(&export_all(&p).bytes); + let l = back.comps().flat_map(|(_, c)| c.layers.iter()).find(|l| matches!(l.source, LayerSource::Text)).expect("text layer"); + assert_eq!((l.name.as_str(), l.name_follows_text), ("Text", Some(true)), "an empty text layer"); +} + #[test] fn what_a_text_document_cannot_hold_is_reported() { let mut d = TextDoc::plain("x"); diff --git a/crates/engine/src/cmd/layer.rs b/crates/engine/src/cmd/layer.rs index f88ab32c8..b015b72b4 100644 --- a/crates/engine/src/cmd/layer.rs +++ b/crates/engine/src/cmd/layer.rs @@ -114,7 +114,15 @@ pub fn specs() -> Vec { has_layers, set_switch ), - cmd!("layer.rename", "Rename", [], Some("Enter"), "{layer?, name}", has_layers, rename), + cmd!( + "layer.rename", + "Rename", + [], + Some("Enter"), + "{layer?, name} — a text layer's name follows its text until renamed; an empty name follows it again", + has_layers, + rename + ), cmd!("layer.setComment", "Layer Comment", [], None, "{layers?, comment}", has_layers, set_comment), cmd!("layer.setBlendMode", "Blending Mode", [], None, "{layers?, mode?: Normal|Multiply|Screen|…, step?: ±1}", has_layers, blend), cmd!( diff --git a/crates/engine/src/cmd/layer/contents.rs b/crates/engine/src/cmd/layer/contents.rs index 5eb253beb..3015f4ecf 100644 --- a/crates/engine/src/cmd/layer/contents.rs +++ b/crates/engine/src/cmd/layer/contents.rs @@ -207,14 +207,14 @@ pub(super) fn set_text(s: &mut Session, p: &Value) -> Result { KV::Str(t) => TextDoc::plain(&t), _ => TextDoc::default(), }; + let before = effectcraft_project::text_layer_name(&doc.text); apply_text_params("layer.setText", p, &mut doc, range.clone())?; pr.set_value_at(lt, KV::Text(Box::new(doc.clone()))); - if let Some(first) = doc.text.lines().next() - && str_p(p, "text").is_some() - && !first.is_empty() - && (l.name.starts_with("Text") || l.name.is_empty()) - { - l.name = first.chars().take(40).collect(); + // Text layers are named after their text until renamed (#3233). + let after = effectcraft_project::text_layer_name(&doc.text); + if str_p(p, "text").is_some() && !after.is_empty() && l.name_follows_text(&before) { + l.name = after; + l.name_follows_text = Some(true); } Ok(()) })?; diff --git a/crates/engine/src/cmd/layer/create.rs b/crates/engine/src/cmd/layer/create.rs index 621ae866b..0196b5aee 100644 --- a/crates/engine/src/cmd/layer/create.rs +++ b/crates/engine/src/cmd/layer/create.rs @@ -187,8 +187,10 @@ pub(super) fn new_text(s: &mut Session, p: &Value) -> Result { } let edit = b_p(p, "edit").unwrap_or(false); let id = s.edit("New Text Layer", None, |proj, st| { - let name: String = layer_name.clone().unwrap_or_else(|| doc.text.lines().next().unwrap_or("").chars().take(40).collect()); + let name: String = layer_name.clone().unwrap_or_else(|| effectcraft_project::text_layer_name(&doc.text)); let mut l = build::layer(proj, &comp, if name.is_empty() { "Text" } else { &name }, LayerSource::Text, (comp.width, comp.height), None); + // Named after its text, the name follows the text until renamed; a given name does not. + l.name_follows_text = Some(layer_name.as_deref().is_none_or(str::is_empty)); if let Some(pr) = l.props.prop_mut("text/sourceText") { pr.value = KV::Text(Box::new(doc.clone())); } diff --git a/crates/engine/src/cmd/layer/switches.rs b/crates/engine/src/cmd/layer/switches.rs index 366c1eeb1..afee522c6 100644 --- a/crates/engine/src/cmd/layer/switches.rs +++ b/crates/engine/src/cmd/layer/switches.rs @@ -139,13 +139,37 @@ fn drop_3d_values(l: &mut Layer) { pub(super) fn rename(s: &mut Session, p: &Value) -> Result { let (cid, lid) = layer_p(s, p, "layer.rename")?; let name = str_p(p, "name").ok_or_else(|| bad("layer.rename", "missing `name`"))?.to_string(); + let t = s.time(); s.edit("Rename Layer", None, |proj, _| { - layer_mut(proj, cid, lid)?.name = name.clone(); + set_layer_name(layer_mut(proj, cid, lid)?, &name, t); Ok(()) })?; Ok(Value::Null) } +/// Give layer `l` the name `name` the user typed (comp time `t` picks the text). A text layer's +/// name follows its text until the user renames it; an empty name makes it follow the text again +/// (After Effects, #3233). Committing the unchanged name changes nothing. +pub(super) fn set_layer_name(l: &mut Layer, name: &str, t: effectcraft_time::Tick) { + let text_name = + l.props.prop("text/sourceText").filter(|_| matches!(l.source, effectcraft_project::LayerSource::Text)).map(|pr| match pr.value_at(l.layer_time(t)) { + KV::Text(d) => effectcraft_project::text_layer_name(&d.text), + KV::Str(txt) => effectcraft_project::text_layer_name(&txt), + _ => String::new(), + }); + match text_name { + Some(first) if name.trim().is_empty() => { + l.name = if first.is_empty() { "Text".to_string() } else { first }; + l.name_follows_text = Some(true); + } + Some(_) if name != l.name => { + l.name = name.to_string(); + l.name_follows_text = Some(false); + } + _ => l.name = name.to_string(), + } +} + /// The Timeline's Comment column. pub(super) fn set_comment(s: &mut Session, p: &Value) -> Result { let (cid, ids) = layers_p(s, p)?; diff --git a/crates/engine/src/cmd/layer/transform.rs b/crates/engine/src/cmd/layer/transform.rs index eb0f610f9..87403fb26 100644 --- a/crates/engine/src/cmd/layer/transform.rs +++ b/crates/engine/src/cmd/layer/transform.rs @@ -172,10 +172,12 @@ pub(super) fn layer_settings(s: &mut Session, p: &Value) -> Result { let pixel_aspect = pixel_aspect_p(p)?; let name = str_p(p, "name").map(str::trim).filter(|n| !n.is_empty()).map(str::to_string); let affect_all = p.get("affectAll").and_then(Value::as_bool).unwrap_or(true); + let t = s.time(); let solid = s.edit("Layer Settings", None, |proj, _| { let src = layer_mut(proj, cid, lid)?.source.clone(); if let Some(n) = &name { - layer_mut(proj, cid, lid)?.name = n.clone(); + // A user name: a text layer's name stops following its text (#3233). + super::switches::set_layer_name(layer_mut(proj, cid, lid)?, n, t); } let LayerSource::Solid { item } = src else { return Ok(None) }; let shared = proj.comps().flat_map(|(_, c)| &c.layers).filter(|l| l.source == src).count() > 1; diff --git a/crates/engine/src/cmd/text_edit.rs b/crates/engine/src/cmd/text_edit.rs index cdb650486..9240f0c1a 100644 --- a/crates/engine/src/cmd/text_edit.rs +++ b/crates/engine/src/cmd/text_edit.rs @@ -6,7 +6,7 @@ //! Positions are character indices. The selection is `anchor..caret` (either order). use effectcraft_keyframe::{CharStyle, TextDoc, Value as KV}; -use effectcraft_project::{ItemId, LayerId, LayerSource}; +use effectcraft_project::{ItemId, LayerId, LayerSource, text_layer_name}; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; @@ -288,10 +288,6 @@ fn session_on(s: &mut Session, lid: LayerId) -> TextEdit { } } -fn first_line_name(text: &str) -> String { - text.lines().next().unwrap_or("").chars().take(40).collect() -} - /// Edit a layer's Source Text at the CTI (one undo step, or merged by `merge`), then set the /// session's selection. pub(crate) fn edit_doc( @@ -319,12 +315,13 @@ pub(crate) fn edit_doc( // already has) must not turn a static Str into Text or add a keyframe at the CTI: that // would change the project and record an empty undo step. if doc != original { - let before = first_line_name(&original.text); - let after = first_line_name(&doc.text); + let before = text_layer_name(&original.text); + let after = text_layer_name(&doc.text); pr.set_value_at(lt, KV::Text(Box::new(doc))); - // Text layers are named after their text until renamed. - if before != after && !after.is_empty() && (l.name == before || l.name.starts_with("Text") || l.name.is_empty()) { + // Text layers are named after their text until renamed (#3233). + if before != after && !after.is_empty() && l.name_follows_text(&before) { l.name = after; + l.name_follows_text = Some(true); } } Ok(()) diff --git a/crates/engine/src/tests/text_edit.rs b/crates/engine/src/tests/text_edit.rs index eb1de6f89..bcb4f0559 100644 --- a/crates/engine/src/tests/text_edit.rs +++ b/crates/engine/src/tests/text_edit.rs @@ -1417,3 +1417,102 @@ fn duplicate_of_an_empty_type_tool_layer_ends_the_edit_and_copies_it() { assert_eq!(layer_count(&s), before + 1, "the empty layer is kept, as text.endEdit keeps it, and copied"); assert_eq!(s.state.selected_layers, vec![LayerId(copy)], "and the copy is selected"); } + +fn layer_name(s: &Session, t: u64) -> String { + s.active_comp().unwrap().layer(effectcraft_project::LayerId(t)).unwrap().name.clone() +} + +/// Replace the text with `text` in the on-canvas editor (everything selected, typed, committed). +fn retype(s: &mut Session, t: u64, text: &str) { + begin_edit(s, t); + s.execute("text.insert", json!({"text": text})).unwrap(); + s.execute("text.endEdit", json!({})).unwrap(); +} + +/// #3233: a text layer's name follows its Source Text, edited on canvas or by `layer.setText`, +/// until the user renames it (After Effects). A user name stays, even one that starts with +/// "Text", and so does a copy's; renaming to an empty name follows the text again. +#[test] +fn a_text_layer_name_follows_its_text_until_renamed() { + let (mut s, t) = session("Hello"); + assert_eq!(layer_name(&s, t), "Hello"); + retype(&mut s, t, "World"); + assert_eq!(layer_name(&s, t), "World", "a new text layer follows its text"); + s.execute("layer.setText", json!({"layer": t, "text": "Again"})).unwrap(); + assert_eq!(layer_name(&s, t), "Again"); + s.execute("layer.rename", json!({"layer": t, "name": "Text Title"})).unwrap(); + s.execute("layer.setText", json!({"layer": t, "text": "Changed"})).unwrap(); + assert_eq!(layer_name(&s, t), "Text Title", "layer.setText keeps the user's name"); + retype(&mut s, t, "Typed"); + assert_eq!(layer_name(&s, t), "Text Title", "typing keeps the user's name"); + s.execute("layer.select", json!({"layers": [t]})).unwrap(); + let copy = s.execute("edit.duplicate", json!({})).unwrap()[0].as_u64().unwrap(); + let copy_name = layer_name(&s, copy); + s.execute("layer.setText", json!({"layer": copy, "text": "Copied"})).unwrap(); + assert_eq!(layer_name(&s, copy), copy_name, "a copy keeps the user's name too"); + // Renaming to an empty name: the name is the text's again, and follows it. + s.execute("layer.rename", json!({"layer": t, "name": ""})).unwrap(); + assert_eq!(layer_name(&s, t), "Typed", "an empty name shows the text"); + retype(&mut s, t, "Followed"); + assert_eq!(layer_name(&s, t), "Followed", "and follows it again"); + // Committing the unchanged name (Enter, Enter) does not stop the following. + s.execute("layer.rename", json!({"layer": t, "name": "Followed"})).unwrap(); + s.execute("layer.setText", json!({"layer": t, "text": "Still"})).unwrap(); + assert_eq!(layer_name(&s, t), "Still"); + // A name given in Layer Settings is the user's too. The UI offers Layer Settings only for + // solids, nulls, cameras and lights (as After Effects does); this guards the command, which + // agents can call on any layer. + s.execute("layer.settings", json!({"layer": t, "name": "Settings Name"})).unwrap(); + s.execute("layer.setText", json!({"layer": t, "text": "Later"})).unwrap(); + assert_eq!(layer_name(&s, t), "Settings Name", "Layer Settings names it for good"); + // A layer given a name when it is made does not follow. + let named = s.execute("layer.newText", json!({"text": "Body", "name": "Caption"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.setText", json!({"layer": named, "text": "Other"})).unwrap(); + assert_eq!(layer_name(&s, named), "Caption"); +} + +/// #3233: After Effects breaks paragraphs with a bare carriage return; the name is the first +/// paragraph, without the break, and an older layer named after it counts as following. +#[test] +fn a_text_layer_name_ends_at_a_bare_carriage_return() { + let (mut s, t) = session("A\rB"); + assert_eq!(layer_name(&s, t), "A"); + s.execute("layer.setText", json!({"layer": t, "text": "C\rD"})).unwrap(); + assert_eq!(layer_name(&s, t), "C"); + retype(&mut s, t, "E\rF"); + assert_eq!(layer_name(&s, t), "E"); + let cid = s.active_comp_id().unwrap(); + let l = std::sync::Arc::make_mut(&mut s.project).comp_mut(cid).unwrap().layer_mut(effectcraft_project::LayerId(t)).unwrap(); + l.name = "Para1".into(); + l.name_follows_text = None; + l.props.prop_mut("text/sourceText").unwrap().value = effectcraft_keyframe::Value::Text(Box::new(TextDoc::plain("Para1\rPara2"))); + s.execute("layer.setText", json!({"layer": t, "text": "Next\rLine"})).unwrap(); + assert_eq!(layer_name(&s, t), "Next", "an older layer named after its first paragraph follows"); +} + +/// #3233: projects saved before the state was recorded load without it; there a name that is +/// the text's, empty, or the default "Text"/"Text 2"… follows the text, any other name stays. +#[test] +fn older_text_layers_without_the_name_state_follow_as_before() { + let (mut s, t) = session("Hello"); + let mut v = serde_json::to_value(s.active_comp().unwrap().layer(effectcraft_project::LayerId(t)).unwrap()).unwrap(); + v.as_object_mut().unwrap().remove("name_follows_text"); + let old: effectcraft_project::Layer = serde_json::from_value(v).unwrap(); + assert_eq!(old.name_follows_text, None, "an older project has no state"); + for (name, follows) in [("Hello", true), ("", true), ("Text", true), ("Text 3", true), ("Text Title", false), ("Text 3b", false), ("Title", false)] { + let l = effectcraft_project::Layer { name: name.into(), ..old.clone() }; + assert_eq!(l.name_follows_text("Hello"), follows, "{name:?}"); + } + let legacy = |s: &mut Session, name: &str| { + let cid = s.active_comp_id().unwrap(); + let l = std::sync::Arc::make_mut(&mut s.project).comp_mut(cid).unwrap().layer_mut(effectcraft_project::LayerId(t)).unwrap(); + l.name = name.into(); + l.name_follows_text = None; + }; + legacy(&mut s, "Hello"); + s.execute("layer.setText", json!({"layer": t, "text": "World"})).unwrap(); + assert_eq!(layer_name(&s, t), "World", "a name equal to the text follows it"); + legacy(&mut s, "Title"); + retype(&mut s, t, "Typed"); + assert_eq!(layer_name(&s, t), "Title", "another name stays"); +} diff --git a/crates/project/src/build.rs b/crates/project/src/build.rs index 1aa8d7c90..7a960dbfb 100644 --- a/crates/project/src/build.rs +++ b/crates/project/src/build.rs @@ -935,6 +935,7 @@ pub fn layer(project: &mut Project, comp: &Comp, name: &str, source: LayerSource Layer { id, name: comp.unique_layer_name(name), + name_follows_text: None, source, label, comment: String::new(), diff --git a/crates/project/src/layer.rs b/crates/project/src/layer.rs index 34eeb50c0..f7b3b5e51 100644 --- a/crates/project/src/layer.rs +++ b/crates/project/src/layer.rs @@ -299,6 +299,8 @@ impl Layer { let Layer { id, name, + // Whether the name follows the text changes no pixels until the name changes. + name_follows_text: _, source, label, comment, @@ -474,6 +476,11 @@ pub enum AutoOrient { pub struct Layer { pub id: LayerId, pub name: String, + /// A text layer's name follows the first line of its Source Text until the user renames it + /// (After Effects). `Some(true)`: it follows, `Some(false)`: the user named it. `None` (older + /// projects, layers made before this was recorded): see [`Layer::name_follows_text()`]. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name_follows_text: Option, pub source: LayerSource, pub label: Label, #[serde(default)] @@ -512,7 +519,26 @@ pub struct Layer { pub props: PropGroup, } +/// The name a text layer takes from its text: the first 40 characters of the first line. A line +/// ends at a line feed or at a bare carriage return (After Effects' paragraph break). +pub fn text_layer_name(text: &str) -> String { + text.split(['\r', '\n']).next().unwrap_or("").chars().take(40).collect() +} + +/// A text layer's default name when its text gives none: "Text", numbered "Text 2", "Text 3"… +fn is_default_text_name(name: &str) -> bool { + name == "Text" || name.strip_prefix("Text ").is_some_and(|n| !n.is_empty() && n.chars().all(|c| c.is_ascii_digit())) +} + impl Layer { + /// Whether this text layer's name follows its Source Text, whose first line gives the name + /// `current` (see [`text_layer_name`]). Without a recorded state (an older project), a name + /// that is empty, the text's own, or the default "Text"/"Text 2"… follows; any other name was + /// given by the user. + pub fn name_follows_text(&self, current: &str) -> bool { + self.name_follows_text.unwrap_or_else(|| self.name.is_empty() || self.name == current || is_default_text_name(&self.name)) + } + /// The layers this layer reads: its parent, track matte, layer parameters (Displacement Map, /// Audio Spectrum's Audio Layer…) and tracker targets. pub fn referenced_layers(&self) -> Vec { diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index 079b81765..fabcd607f 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -43,7 +43,7 @@ pub use item::NO_LAYER; pub use item::{Heard, Item, ItemId, ItemKind, LayerId}; pub use layer::{ AutoOrient, FrameBlend, Layer, LayerSource, LightKind, MatteKind, PrimitiveKind, Quality, Sampling, Switches, TrackMatte, is_audio_effect, - reads_other_frames, + reads_other_frames, text_layer_name, }; pub use layers::Layers; pub use settings::{ diff --git a/docs/agents.md b/docs/agents.md index ac77439b7..27664cb73 100644 --- a/docs/agents.md +++ b/docs/agents.md @@ -713,6 +713,8 @@ in the Plug-in Library: [publishing-plugins.md](publishing-plugins.md). step (separated X/Y/Z Position; Orientation and X/Y/Z Rotation on 3D layers). * Rename a layer with Enter (or double-click a name): the whole name is selected, Enter or a click elsewhere commits, Escape cancels; `layer.rename {layer, name}` does it directly. + A text layer's name follows its text until it is renamed; renaming it to an empty name makes + it follow the text again (After Effects). * Ctrl/Cmd-click on the current-time display (Timeline or Composition panel) toggles the Time Display Style between Timecode and Frames: `file.projectSettings {"timeDisplay": "frames"}`. * `prop.pickWhip {layer, prop, target: {layer, prop, dimension?}}` links a property as its From fc9eca99a0832ceac83fba2217c8bc59595741d5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:42:40 +0100 Subject: [PATCH 036/350] #3231: evaluate prop.get and expression errors with the footage source prop.get and Session::expression_errors (Reveal Expression Errors, the viewer's error banner) built their expression context without the footage source, so an expression that reads footage pixels (sampleImage) gave a different value or a spurious error there while it worked in the render and in expr.errors (#1000). expr_tools::eval_ctx builds the render's context (expression host and footage source) for a comp at a time; expr.errors, prop.get, Session::expression_errors and the script value query all use it. Fixes #3231 --- crates/engine/src/cmd/expr_tools.rs | 14 ++++++-- crates/engine/src/cmd/layer_menu.rs | 4 +-- crates/engine/src/cmd/prop.rs | 5 +-- crates/engine/src/tests/track.rs | 54 +++++++++++++++++++++++++++++ crates/script/src/model.rs | 5 +-- 5 files changed, 71 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/expr_tools.rs b/crates/engine/src/cmd/expr_tools.rs index cb39077af..bd62a8e46 100644 --- a/crates/engine/src/cmd/expr_tools.rs +++ b/crates/engine/src/cmd/expr_tools.rs @@ -2,7 +2,7 @@ //! Language menu (the ⓕ button next to an expression), written from the public Expression //! Language Reference. -use effectcraft_project::{Expression, ItemId, Layer, Property}; +use effectcraft_project::{Comp, Expression, ItemId, Layer, Property}; use effectcraft_render::EvalCtx; use effectcraft_time::Tick; use serde::Serialize; @@ -42,8 +42,16 @@ impl ExprError { /// expressions and the syntax errors that disabled others. pub fn errors(s: &Session, cid: ItemId, t: Tick) -> Vec { let Some(comp) = s.project.comp(cid) else { return vec![] }; - let ctx = EvalCtx { footage: Some(s.footage.as_ref()), expr: s.expr.as_deref(), ..EvalCtx::new(&s.project, cid, comp, t) }; - errors_in(&ctx, s.expr_check, || true).unwrap_or_default() + errors_in(&eval_ctx(s, cid, comp, t), s.expr_check, || true).unwrap_or_default() +} + +/// The context to evaluate expressions of comp `cid` at comp time `t` as the render does for the +/// comp at top level: with the session's expression host and its footage source, so +/// `sampleImage()` reads footage pixels instead of failing or sampling transparent (#1000, +/// #3231). `expr.errors`, `prop.get`, [`Session::expression_errors`] and the script value query +/// all evaluate through this one context. +pub fn eval_ctx<'a>(s: &'a Session, cid: ItemId, comp: &'a Comp, t: Tick) -> EvalCtx<'a> { + EvalCtx { footage: Some(s.footage.as_ref()), expr: s.expr.as_deref(), ..EvalCtx::new(&s.project, cid, comp, t) } } /// Evaluate diagnostics against an immutable snapshot. Background callers can cancel between diff --git a/crates/engine/src/cmd/layer_menu.rs b/crates/engine/src/cmd/layer_menu.rs index 11a5a3665..8047baa39 100644 --- a/crates/engine/src/cmd/layer_menu.rs +++ b/crates/engine/src/cmd/layer_menu.rs @@ -867,8 +867,8 @@ impl Session { }); for pr in has { let text = pr.expr.as_ref().map(|e| e.text.clone()).unwrap_or_default(); - let mut ctx = effectcraft_render::EvalCtx::new(&self.project, cid, comp, t); - ctx.expr = Some(host.as_ref()); + // With the footage source, so `sampleImage()` doesn't fail here only (#3231). + let ctx = super::expr_tools::eval_ctx(self, cid, comp, t); if let Err(e) = host.eval(&ctx, l, pr, &pr.value_at(l.layer_time(t))) { errors.push(json!({"layer": l.id.0, "prop": pr.uid, "name": pr.name, "expression": text, "error": e})); } diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index b4432dd2f..4b91649cf 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -558,8 +558,9 @@ fn get(s: &mut Session, p: &Value) -> Result { } else if pr.has_expression() && let (Some(h), Some(comp)) = (s.expr.as_deref(), s.project.comp(cid)) { - let mut ctx = crate::render::EvalCtx::new(&s.project, cid, comp, t); - ctx.expr = Some(h); + // The render's context, footage source included, so `sampleImage()` evaluates here as it + // renders and as `expr.errors` reports it (#3231). + let ctx = super::expr_tools::eval_ctx(s, cid, comp, t); match h.eval(&ctx, l, pr, &raw) { Ok(v) => out["evaluated"] = v.to_json(), Err(e) => { diff --git a/crates/engine/src/tests/track.rs b/crates/engine/src/tests/track.rs index 8c7037b69..bd1adaeca 100644 --- a/crates/engine/src/tests/track.rs +++ b/crates/engine/src/tests/track.rs @@ -433,3 +433,57 @@ fn analyze_1080p_single_point_speed() { assert!(dist(fc.value_at(frame_time(30)).as_vec2(), [990.0, 555.0]) < 0.25); assert!(fps > 30.0, "{fps} frames/s"); } + +/// A stand-in for `sampleImage()` on a footage layer (the engine has no expression language): the +/// pixel at (0, 0) of the layer's footage, read through the context's footage source. The real +/// one samples transparent without the source; this one fails, so either symptom shows. +struct SampleFootage; + +impl crate::render::ExprHost for SampleFootage { + fn eval( + &self, + ctx: &EvalCtx, + layer: &effectcraft_project::Layer, + _: &effectcraft_project::Property, + _: &effectcraft_keyframe::Value, + ) -> Result { + let item = layer.source.item().ok_or("not a footage layer")?; + let Some(ItemKind::Footage(footage)) = ctx.project.item(item).map(|i| &i.kind) else { return Err("not a footage layer".into()) }; + let src = ctx.footage.ok_or("sampleImage: no footage source")?; + let img = src.frame(item, footage, ctx.time).ok_or("no frame")?; + Ok(effectcraft_keyframe::Value::Scalar(f64::from(img.get(0, 0)[0]) * 100.0)) + } +} + +/// The clip of [`setup`] (uniform 25 % grey footage) with an opacity expression that samples its +/// footage: 25 with the footage source. Here for `setup`'s footage layer (#3231). +fn sampling_clip() -> (Session, ItemId, LayerId) { + let (mut s, clip, _) = setup(|_| Image::filled(W, H, [0.25, 0.25, 0.25, 1.0])); + let cid = s.active_comp_id().unwrap(); + s.expr = Some(Arc::new(SampleFootage)); + s.execute("prop.setExpression", json!({"layer": clip.0, "path": "transform/opacity", "expression": "sampleImage([0, 0])[0] * 100"})).unwrap(); + (s, cid, clip) +} + +/// #3231: `prop.get` evaluates an expression that samples footage pixels with the footage source, +/// as the render and `expr.errors` do: the same value and no spurious `expressionError`. +#[test] +fn prop_get_evaluates_footage_sampling_expressions_with_the_footage_source() { + let (mut s, cid, clip) = sampling_clip(); + let got = s.execute("prop.get", json!({"layer": clip.0, "path": "transform/opacity", "time": 0.0})).unwrap(); + assert!(got.get("expressionError").is_none(), "no spurious error: {got}"); + assert_eq!(got["evaluated"].as_f64(), Some(25.0), "the footage pixel, not the keyframed 100: {got}"); + // The render applies the same opacity: the clip alone (outside the solid) covers 25 %. + let img = s.render(cid, Tick::ZERO, crate::render::RenderOpts::default()); + let a = img.get(5, 5)[3]; + assert!((a - 0.25).abs() < 0.01, "rendered opacity {a}"); + assert!(crate::cmd::expr_tools::errors(&s, cid, Tick::ZERO).is_empty()); +} + +/// #3231: Reveal Expression Errors and the viewer's error banner evaluate with the footage source, +/// so an expression that samples footage pixels isn't reported as failing. +#[test] +fn expression_errors_evaluate_footage_sampling_expressions_with_the_footage_source() { + let (s, cid, _) = sampling_clip(); + assert_eq!(s.expression_errors(cid), Vec::::new()); +} diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index e662c1cdc..694d82d39 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -519,12 +519,9 @@ fn value(s: &Session, a: &J) -> R { let v = if pre || !p.has_expression() { p.value_at(l.layer_time(t)) } else { - let mut ctx = EvalCtx::new(&s.project, cid, c, t); - ctx.expr = s.expr.as_deref(); // Sample expressions (`sampleImage`) read footage through the session's source; without // it footage layers sample as transparent (#1000). - ctx.footage = Some(s.footage.as_ref()); - ctx.value(l, p) + effectcraft_engine::commands::expr_tools::eval_ctx(s, cid, c, t).value(l, p) }; Ok(js_value(c, &v)) } From 6ff41bcb10356f43ebb4d19659a4ed6a88424c7d Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:26:41 +0100 Subject: [PATCH 037/350] Timeline: Convert To Bezier Path in the context menu of a parametric shape path Right-clicking a Rectangle, Ellipse or Polystar Path row offers After Effects' Convert To Bezier Path, running path.convertToBezier with that layer and path. A shape group's row offers it too and converts the static parametric paths inside it; keyframed or expression-driven ones are left alone, since converting drops their animation. Fixes #3237 --- crates/ui-egui/src/panels/timeline.rs | 71 +++++++++++++++- .../tests/it/ui_blank_context_menus.rs | 84 ++++++++++++++++++- 2 files changed, 153 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 038dcc9b9..0770f5e1f 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -3716,12 +3716,14 @@ fn group_row_click(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: let RowKind::Group { uid, name, fx, .. } = &rc.row.kind else { return }; let gr_rect = Rect::from_min_max(pos2(indent + 8.0, r.min.y), pos2(cw.switches, r.max.y)); // Effects, masks, shape items and animators drag to a new place among their siblings. - let movable = !layer.switches.locked && layer.props.find_group(*uid).is_some_and(PropGroup::is_instance); + let group = layer.props.find_group(*uid); + let movable = !layer.switches.locked && group.is_some_and(PropGroup::is_instance); let gr = ui.interact(gr_rect, egui::Id::unique(("grow", uid)), if movable { Sense::click_and_drag() } else { Sense::click() }); if movable && gr.drag_started_by(egui::PointerButton::Primary) { ui.ctx().data_mut(|d| d.insert_temp(group_drag_id(), GroupDrag { comp: cid, owner: gr.id, layer: layer.id, uid: *uid })); } app.auto.add(&format!("timeline.group.{uid}.name"), gr_rect, name); + shape_path_menu(&mut app.auto, &gr, layer, group, &mut *actions); if gr.clicked() && uid & (WAVE_BIT | GEOM_BIT) == 0 { // Shift or Cmd/Ctrl adds the group to the selection, or takes a selected one out, as // property rows and Effect Controls headers do (#2552: several effects and styles for @@ -3745,6 +3747,73 @@ fn group_row_click(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: } } +/// The shape items Convert To Bezier Path turns into editable paths. +const PARAMETRIC_PATHS: [&str; 3] = ["rect", "ellipse", "star"]; + +/// The parametric paths (Rectangle, Ellipse, Polystar Path) inside shape group `g`, nested groups +/// included, that convert without losing anything: a keyframed or expression-driven one would +/// lose its animation, so the group leaves it alone. +fn static_parametric_paths(g: &PropGroup) -> Vec { + // Depth-first, without recursion; only groups and their contents hold paths. + let mut out = Vec::new(); + let mut stack = vec![g]; + while let Some(n) = stack.pop() { + for c in n.groups() { + match c.match_id.as_str() { + m if PARAMETRIC_PATHS.contains(&m) => { + if !c.any_animated() { + out.push(c.uid); + } + } + "group" | "contents" => stack.push(c), + _ => {} + } + } + } + out +} + +/// Right-click on a Rectangle, Ellipse or Polystar Path row, or on a shape group holding such +/// paths: After Effects' Convert To Bezier Path, acting on that row (#3237). A path's own row +/// converts it even when animated (After Effects makes a static path at the current time); the +/// group's entry, an addition to After Effects, converts the static paths inside it. +fn shape_path_menu( + auto: &mut crate::automation::Registry, + resp: &egui::Response, + layer: &Layer, + group: Option<&PropGroup>, + actions: &mut Vec<(String, serde_json::Value)>, +) { + let Some(g) = group.filter(|_| matches!(layer.source, LayerSource::Shape)) else { return }; + let uid = g.uid; + let items = if PARAMETRIC_PATHS.contains(&g.match_id.as_str()) { + vec![uid] + } else if g.match_id == "group" { + // Walked once, when the menu opens, not for every group row on every frame. + let key = egui::Id::unique(("tl-shape-path-menu", layer.id.0, uid)); + if resp.secondary_clicked() { + let found = static_parametric_paths(g); + resp.ctx.data_mut(|d| d.insert_temp(key, found)); + } + resp.ctx.data(|d| d.get_temp::>(key)).unwrap_or_default() + } else { + return; + }; + if items.is_empty() { + return; + } + let (cmd, label) = ("path.convertToBezier", "Convert To Bezier Path"); + let params = json!({"layer": layer.id.0, "items": items}); + crate::widgets::context_menu(resp, |ui| { + let r = ui.add_enabled(!layer.switches.locked, egui::Button::new(crate::i18n::context_item(cmd, ¶ms, label))); + auto.add(&format!("timeline.group.{uid}.menu.convertToBezier"), r.rect, label); + if r.clicked() { + actions.push((cmd.into(), params.clone())); + ui.close(); + } + }); +} + fn waveform_row(app: &mut EffectcraftApp, g: &Rw, rc: &RowCtx<'_>) { let t = g.tl.t; let graph_x0 = g.tl.graph_x0; diff --git a/crates/ui-egui/tests/it/ui_blank_context_menus.rs b/crates/ui-egui/tests/it/ui_blank_context_menus.rs index 84080be08..31312d5b2 100644 --- a/crates/ui-egui/tests/it/ui_blank_context_menus.rs +++ b/crates/ui-egui/tests/it/ui_blank_context_menus.rs @@ -1,4 +1,5 @@ -//! Right-click actions in the blank Project and Timeline areas, and on Timeline property names. +//! Right-click actions in the blank Project and Timeline areas, and on Timeline property and +//! shape path names. use crate::gpu_test::RenderSerial; use effectcraft_engine::Session; use effectcraft_engine::project::ItemKind; @@ -12,6 +13,8 @@ fn harness() -> Harness<'static, EffectcraftApp> { let mut session = Session::default(); session.execute("comp.new", json!({"name":"Empty", "width":640,"height":360,"duration":4})).unwrap(); let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(session)); + // English whatever the system language: the menu items found below by label are translated. + h.state_mut().session.prefs.general.language = "en".into(); h.run_steps(3); h } @@ -230,6 +233,85 @@ fn timeline_property_names_have_the_property_menu() { assert_eq!(h.state().dialog, Some(Dialog::Form)); } +/// #3237: right-clicking a Rectangle Path row in the Timeline offers After Effects' Convert To +/// Bezier Path, which turns that path into an editable one; the shape group holding it offers the +/// same and converts the parametric paths inside it. +#[test] +fn timeline_parametric_shape_path_menu_converts_to_bezier() { + use effectcraft_engine::project::{LayerId, PropGroup}; + let mut h = harness(); + let layer = h.state_mut().session.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let contents = + |h: &Harness<'_, EffectcraftApp>| h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.sub("contents").unwrap().clone(); + fn groups(g: &PropGroup, out: &mut Vec<(u64, String)>) { + out.push((g.uid, g.match_id.clone())); + for c in g.groups() { + groups(c, out); + } + } + let all = |h: &Harness<'_, EffectcraftApp>| { + let mut out = Vec::new(); + groups(&contents(h), &mut out); + out + }; + let find = |h: &Harness<'_, EffectcraftApp>, ids: &[&str]| all(h).into_iter().find(|(_, m)| ids.contains(&m.as_str())).map(|(u, _)| u); + let open = |h: &mut Harness<'_, EffectcraftApp>| { + h.state_mut().ui.timeline.open_layers.insert(layer); + for (uid, _) in all(&*h) { + h.state_mut().ui.timeline.open_groups.insert(uid); + } + h.run_steps(3); + }; + let choose = |h: &mut Harness<'_, EffectcraftApp>, row: u64| { + let p = rect(h, &format!("timeline.group.{row}.name")).center(); + right_click(h, p); + assert!(h.query_by_label("Convert To Bezier Path").is_some(), "the menu offers Convert To Bezier Path"); + let p = rect(h, &format!("timeline.group.{row}.menu.convertToBezier")).center(); + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerMoved(p)); + h.input_mut().events.push(Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.step(); + } + h.run_steps(3); + }; + let group = find(&h, &["group"]).expect("a shape group"); + let path = find(&h, &["rect"]).expect("a Rectangle Path"); + // The Rectangle Path's own row. + open(&mut h); + snapshot(&mut h, "shape-path-rows"); + choose(&mut h, path); + assert!(find(&h, &["rect"]).is_none(), "the rectangle is converted"); + assert!(find(&h, &["path"]).is_some(), "it is a bezier path now"); + assert_eq!(h.state().session.history.undo.last().map(|u| u.0.as_str()), Some("Convert To Bezier Path")); + // A bezier path has nothing to convert: its row has no such entry. + let bezier = find(&h, &["path"]).unwrap(); + open(&mut h); + let p = rect(&h, &format!("timeline.group.{bezier}.name")).center(); + right_click(&mut h, p); + assert!(h.query_by_label("Convert To Bezier Path").is_none(), "a bezier path has no Convert To Bezier Path"); + egui::Popup::close_all(&h.ctx); + // The shape group's row converts the rectangle inside it. + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + assert_eq!(find(&h, &["rect"]), Some(path), "undo restores the rectangle"); + open(&mut h); + choose(&mut h, group); + assert!(find(&h, &["rect"]).is_none(), "the group's rectangle is converted"); + assert!(find(&h, &["path"]).is_some()); + // A keyframed rectangle would lose its animation: the group leaves it out (and so has nothing + // to offer), while the path's own row still converts it, as After Effects does. + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + let size = contents(&h).find_group(path).unwrap().get("size").unwrap().uid; + h.state_mut().session.execute("prop.addKey", json!({"layer": layer, "path": format!("@{size}"), "time": 0.0, "value": [120, 80]})).unwrap(); + open(&mut h); + let p = rect(&h, &format!("timeline.group.{group}.name")).center(); + right_click(&mut h, p); + assert!(h.query_by_label("Convert To Bezier Path").is_none(), "nothing static to convert in the group"); + egui::Popup::close_all(&h.ctx); + h.run_steps(3); + choose(&mut h, path); + assert!(find(&h, &["rect"]).is_none(), "the path's own row converts an animated rectangle"); +} + /// The rows of a context menu touch: a gap between two entries highlighted neither and /// swallowed a click there. #[test] From f4af2c093bcf4f10edaef0ad88ca5278c499d550 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:40:33 +0100 Subject: [PATCH 038/350] Timeline: Add Expression from the stopwatch's right-click menu (#3192) Linux window managers (Cinnamon, KDE, GNOME) take Alt+click to move the window, so Alt-clicking a stopwatch never reached the app. Right-clicking the stopwatch now opens the property's menu (the one its name has), and Add Expression picked there or from the name's menu opens the new expression's editor with the keyboard, as the Alt-click does (#1072). Fixes #3192 --- crates/ui-egui/src/panels/timeline.rs | 55 +++++++++++++++++------ crates/ui-egui/tests/it/ui_inline_edit.rs | 44 ++++++++++++++++++ 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 038dcc9b9..81784a373 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -4053,6 +4053,7 @@ fn prop_key_navigator(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, r /// The stopwatch. fn prop_stopwatch(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: &RowCtx<'_>, prop: &Property) { let t = g.tl.t; + let cid = g.tl.cid; let ctx = g.tl.ctx.clone(); let actions = &mut g.tl.actions; let lp = g.lp.clone(); @@ -4062,8 +4063,9 @@ fn prop_stopwatch(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: & let indent = rc.indent; let uid = &prop.uid; - // Stopwatch: Alt+click adds an expression; a press toggles the animation, and - // dragging on over other stopwatches gives them the same state (#284). + // Stopwatch: Alt+click (or Add Expression in its right-click menu) adds an expression; a + // press toggles the animation, and dragging on over other stopwatches gives them the same + // state (#284). if !prop.static_only { let swr = Rect::from_center_size(pos2(indent, cy), vec2(15.0, 15.0)); let resp = ui.interact(swr, egui::Id::unique(("stopwatch", uid)), Sense::click_and_drag()); @@ -4076,18 +4078,13 @@ fn prop_stopwatch(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: & }; icons::paint(&lp, swr, Icon::Stopwatch, col); app.auto.add(&format!("timeline.prop.{uid}.stopwatch"), swr, &prop.name); + // Right-click: the property's menu, whose Add Expression needs no Alt, which Linux + // window managers take for moving the window (#3192). + prop_context_menu(app, &ctx, &resp, cid, layer, prop, &mut *actions); if ui.input(|i| i.modifiers.alt) { if resp.clicked() { actions.push(("prop.setExpression".into(), json!({"layer": layer.id.0, "prop": uid}))); - if prop.expr.is_none() { - // Adding one: its editor opens, comes into view and takes the - // keyboard with the new expression selected (#1072). - app.ui.timeline.expr_closed.remove(uid); - focus_expression(&ctx, *uid, [usize::MAX, 0]); - } else if ctx.data(|d| d.get_temp::(expr_focus_id())).is_some_and(|f| f.prop == *uid) { - // Removing it: nothing is left to take the keyboard. - ctx.data_mut(|d| d.remove::(expr_focus_id())); - } + expression_toggled(app, &ctx, prop); } } else if let Some((value, merge)) = switch_gesture(ui, &resp, r, "stopwatch", prop.is_animated()) { actions.push(("prop.toggleAnimation".into(), json!({"layer": layer.id.0, "prop": uid, "value": value, "merge": merge}))); @@ -4131,10 +4128,41 @@ fn prop_name(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: &RowCt if name_resp.clicked() { actions.push(("prop.select".into(), json!({"layer": layer.id.0, "prop": uid, "add": ui.input(|i| i.modifiers.shift)}))); } - crate::widgets::context_menu(&name_resp, |ui| prop_menu(ui, &mut app.auto, cid, layer, prop, &mut *actions)); + prop_context_menu(app, &ctx, &name_resp, cid, layer, prop, &mut *actions); name_x } +/// `prop`'s context menu ([`prop_menu`]) on `resp`, its name or its stopwatch. Its Add or +/// Remove Expression does what an Alt-click on the stopwatch does to the editor. +fn prop_context_menu( + app: &mut EffectcraftApp, + ctx: &egui::Context, + resp: &egui::Response, + cid: ItemId, + layer: &Layer, + prop: &Property, + actions: &mut Vec<(String, serde_json::Value)>, +) { + let before = actions.len(); + crate::widgets::context_menu(resp, |ui| prop_menu(ui, &mut app.auto, cid, layer, prop, &mut *actions)); + if actions.get(before..).is_some_and(|picked| picked.iter().any(|(id, _)| id == "prop.setExpression")) { + expression_toggled(app, ctx, prop); + } +} + +/// An expression was just added to `prop` (it had none) or removed: adding one opens its +/// editor, which comes into view and takes the keyboard with the new expression selected +/// (#1072); removing it leaves nothing to take the keyboard. +fn expression_toggled(app: &mut EffectcraftApp, ctx: &egui::Context, prop: &Property) { + let uid = prop.uid; + if prop.expr.is_none() { + app.ui.timeline.expr_closed.remove(&uid); + focus_expression(ctx, uid, [usize::MAX, 0]); + } else if ctx.data(|d| d.get_temp::(expr_focus_id())).is_some_and(|f| f.prop == uid) { + ctx.data_mut(|d| d.remove::(expr_focus_id())); + } +} + /// The value editors and the property pick whip. fn prop_value_cell(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: &RowCtx<'_>, ectx: &EvalCtx<'_>, prop: &Property, name_x: f32) { let vis = g.tl.vis; @@ -5496,7 +5524,8 @@ pub(crate) fn is_position(layer: &Layer, uid: u64) -> bool { layer.transform().is_some_and(|tr| ["position", "positionX", "positionY", "positionZ"].iter().any(|m| tr.get(m).is_some_and(|p| p.uid == uid))) } -/// A property's context menu (right-click its name in the Timeline), as in After Effects. +/// A property's context menu: right-click its name in the Timeline, as in After Effects, or its +/// stopwatch (an Add Expression that needs no Alt-click, #3192). fn prop_menu( ui: &mut egui::Ui, auto: &mut crate::automation::Registry, diff --git a/crates/ui-egui/tests/it/ui_inline_edit.rs b/crates/ui-egui/tests/it/ui_inline_edit.rs index 6c29045bb..5d338118b 100644 --- a/crates/ui-egui/tests/it/ui_inline_edit.rs +++ b/crates/ui-egui/tests/it/ui_inline_edit.rs @@ -634,6 +634,50 @@ fn an_expression_added_at_the_bottom_of_the_timeline_comes_into_view() { assert_eq!(expression(&h, layer, opacity).as_deref(), Some("25")); } +fn right_click(h: &mut Harness<'_, EffectcraftApp>, p: Pos2) { + h.input_mut().events.push(Event::PointerMoved(p)); + h.step(); + h.input_mut().events.push(Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + h.input_mut().events.push(Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +/// Right-click Opacity's `target` (its stopwatch or name) in the Timeline, pick Add Expression +/// and type `text` without another click: the new expression's editor has the keyboard. +fn add_expression_from_menu(target: &str, text: &str) { + let (mut h, _) = harness(); + let (layer, opacity, _) = open_transform(&mut h, "Plate"); + let at = center(&h, &format!("timeline.prop.{opacity}.{target}")); + right_click(&mut h, at); + let item = format!("timeline.prop.{opacity}.menu.addExpression"); + assert!(h.state().auto.find(&item).is_some(), "the {target}'s menu offers Add Expression"); + let animated = + h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(layer)).unwrap().props.find(opacity).unwrap().is_animated(); + assert!(!animated, "the right-click didn't toggle the stopwatch"); + let at = center(&h, &item); + click(&mut h, at, 1); + assert!(expression(&h, layer, opacity).is_some(), "an expression was added from the {target}'s menu"); + type_text(&mut h, text); + commit_expression(&mut h); + assert_eq!(expression(&h, layer, opacity).as_deref(), Some(text), "typing replaced the expression added from the {target}'s menu"); +} + +/// #3192: Linux window managers take Alt+click to move the window, so the stopwatch has a +/// right-click menu whose Add Expression does what the Alt-click does: the expression is added +/// and its editor takes the keyboard (the stopwatch itself isn't toggled). +#[test] +fn add_expression_from_the_stopwatch_menu_focuses_the_new_expression() { + add_expression_from_menu("stopwatch", "50"); +} + +/// #3192: Add Expression from the property name's menu opens the editor with the keyboard too, +/// as the Alt-click does (it took another click). +#[test] +fn add_expression_from_the_name_menu_focuses_the_new_expression() { + add_expression_from_menu("name", "90"); +} + /// A primary-button drag from `from` to `to` with `modifiers` held from the press to the release. fn drag_with(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2, modifiers: Modifiers) { h.input_mut().events.push(Event::PointerMoved(from)); From c54b542274076139f0ad52902f341bf8d485748c Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:02:19 +0100 Subject: [PATCH 039/350] Tests: Match System resolves to English in ui-egui test builds Fixes #3244. The ui-egui tests look up English labels, but Session::default() keeps the language pref "system", so on a German (or any non-English) system they read translated labels and fail. Match System now resolves to English under cfg(test) (lib tests) and under a new test-only `test-support` feature, which the crate's own dev-dependency on itself turns on for tests/it, examples and workspace test builds. Product builds are unchanged; tests about a translation still set their language explicitly. The Match System test now asserts the English pin explicitly. --- Cargo.lock | 1 + crates/ui-egui/Cargo.toml | 10 ++++++++++ crates/ui-egui/src/i18n.rs | 30 +++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b426c8f8..c7c81888f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1988,6 +1988,7 @@ dependencies = [ "effectcraft-segment", "effectcraft-text", "effectcraft-time", + "effectcraft-ui-egui", "eframe", "egui", "egui_extras", diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 1576907cb..3c5862594 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -45,7 +45,17 @@ craft-update = { workspace = true } pollster = { workspace = true } sys-locale = { workspace = true } +[features] +# Tests only, never enable in a product: Settings ▸ General ▸ Language ▸ Match System resolves to +# English, so English label lookups pass on any system language (#3244). The self dev-dependency +# below turns it on wherever dev-dependencies are built: this crate's integration tests, its +# examples (`snapshot` shots are deterministic English unless a step sets a language) and +# workspace test builds of the apps (`cargo test --workspace`). +test-support = [] + [dev-dependencies] +# This crate's own integration tests build it with `test-support` (see `[features]`). +effectcraft-ui-egui = { workspace = true, features = ["test-support"] } # Fonts for kittest: the workspace `egui` features are what test roots resolve, but stating them # here documents the requirement (kittest's own dev-dependencies do not propagate to consumers). egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] } diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 68e58f842..8ab99ead7 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -84,7 +84,14 @@ pub(crate) fn swedish(app: &EffectcraftApp) -> bool { /// Settings ▸ General ▸ Language ▸ Match System: the operating system's interface language /// where EffectCraft has it, else English (#229), as After Effects installs in the system's /// language. The browser build follows the locales its page passes in ([`set_system_locales`]). +/// +/// Test builds (this crate's unit tests, and its integration tests through the `test-support` +/// feature) resolve Match System to English, so tests that look up English labels pass on any +/// system language (#3244); tests about a translation choose their language explicitly. fn system_language() -> &'static str { + if cfg!(any(test, feature = "test-support")) { + return "en"; + } #[cfg(not(target_arch = "wasm32"))] { static LANGUAGE: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new(); @@ -9169,13 +9176,22 @@ mod tests { } let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); assert_eq!(app.session.prefs.general.language, "system", "the default"); - assert_eq!(japanese(&app), system_language() == "ja"); - assert_eq!(simplified(&app), system_language() == "zh-hans"); - assert_eq!(traditional(&app), system_language() == "zh-hant"); - assert_eq!(spanish(&app), system_language() == "es"); - assert_eq!(czech(&app), system_language() == "cs"); - assert_eq!(polish(&app), system_language() == "pl"); - assert_eq!(swedish(&app), system_language() == "sv"); + assert_eq!(system_language(), "en", "test builds pin Match System to English (#3244)"); + assert_eq!(language(&app), "en"); + for (flag, name) in [ + (japanese(&app), "ja"), + (simplified(&app), "zh-hans"), + (traditional(&app), "zh-hant"), + (ukrainian(&app), "uk"), + (spanish(&app), "es"), + (brazilian_portuguese(&app), "pt-br"), + (czech(&app), "cs"), + (polish(&app), "pl"), + (georgian(&app), "ka"), + (swedish(&app), "sv"), + ] { + assert!(!flag, "Match System shows {name} in a test build"); + } app.session.execute("prefs.set", json!({"key":"general.language", "value":"ja"})).unwrap(); assert!(japanese(&app)); app.session.execute("prefs.set", json!({"key":"general.language", "value":"zh-hans"})).unwrap(); From 7dabd6e6f16265218fc07248c5c0ef217bf5610e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:24:57 +0100 Subject: [PATCH 040/350] Effect Controls: drop a replaced project's lock before answering a lock request (#3193) A ui.set {effectControlsLocked: true} that ran after the project was replaced but before the next frame reported the old project's lock as success; the next frame then cleared it. set_lock now scopes the lock first. --- .../src/panels/effect_controls/common.rs | 3 +++ crates/ui-egui/tests/it/ui_effect_controls.rs | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/crates/ui-egui/src/panels/effect_controls/common.rs b/crates/ui-egui/src/panels/effect_controls/common.rs index 49bb514dd..1e7d7cba1 100644 --- a/crates/ui-egui/src/panels/effect_controls/common.rs +++ b/crates/ui-egui/src/panels/effect_controls/common.rs @@ -49,6 +49,9 @@ pub(super) fn shown_layer(app: &EffectcraftApp) -> Option { /// does nothing; locking while already locked keeps the layer it is locked to. Returns whether /// the panel is locked afterwards. The tab's lock icon and `ui.set {effectControlsLocked}`. pub fn set_lock(app: &mut EffectcraftApp, on: bool) -> bool { + // A lock left from a replaced project is gone before this one is answered (a control + // request can run between the replacement and the next frame). + scope_lock(app); if !on { app.ui.effect_controls_lock = None; return false; diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index d72febf01..ce51a2b2f 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -1013,6 +1013,32 @@ fn replacing_the_project_unlocks_effect_controls() { assert_eq!(h.state().ui.effect_controls_lock, None, "the lock belongs to the replaced project"); } +/// #3193: a lock request right after the project is replaced, before the next frame, is not +/// answered with the old project's lock: the reply matches the lock the panel keeps. +#[test] +fn a_lock_request_after_a_project_replacement_does_not_report_the_old_lock() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, _, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h)["ok"], true); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + let project = (*h.state().session.project).clone(); + h.state_mut().session.replace_project(project, None); + let reply = send(&mut h); + h.run_steps(3); + assert_eq!(reply["ok"] == true, h.state().ui.effect_controls_lock.is_some(), "{reply}"); +} + /// #3193: an effect dropped on the locked panel goes to the locked layer and leaves the selection /// alone; one applied to another layer says why the panel doesn't show it. #[test] From b2d6690fd202c6a4a053839f946fc2e0fdb939c0 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:27:36 +0100 Subject: [PATCH 041/350] Number fields: Mask Feather stays non-negative on the other selected layers (#3195) A typed *-1 (the edited layer clamped at 0 by the panel) or a change by the same amount could take another selected layer's Mask Feather below 0. Clamp it at 0 there, as the panels do for the edited layer. --- crates/engine/src/cmd/prop.rs | 23 +++++++++++++++++++++++ crates/engine/src/tests/timeline.rs | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index dc043d78f..ae37ea1b7 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -436,6 +436,9 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { let mut q = twin_params(p, olid, ouid); shifted(s, &mut q, olid, ouid, k); typed(s, &mut q, olid, ouid); + if is_mask_feather(s, cid, olid, ouid) { + non_negative(&mut q["value"]); + } // (a layer that can't take it, e.g. a clamped or static property, keeps its value) let _ = set(s, &q); } @@ -444,6 +447,26 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { if merge_p(p).is_some() { run(s) } else { super::app_more::grouped(s, "Change Property", run) } } +/// Whether property `uid` of `lid` is a mask's Mask Feather, which is never negative (After +/// Effects; the panels clamp a typed or scrubbed value at 0). +fn is_mask_feather(s: &Session, cid: ItemId, lid: LayerId, uid: Uid) -> bool { + let Some(l) = s.project.comp(cid).and_then(|c| c.layer(lid)) else { return false }; + l.props.find(uid).is_some_and(|q| q.match_id == "feather") && l.props.parent_of(uid).is_some_and(|g| matches!(g.kind, GroupKind::Mask { .. })) +} + +/// A JSON number, or each number of an array, raised to at least 0. +fn non_negative(v: &mut Value) { + match v { + Value::Number(n) => { + if let Some(x) = n.as_f64().filter(|x| *x < 0.0) { + *v = json!(x.max(0.0)); + } + } + Value::Array(a) => a.iter_mut().for_each(non_negative), + _ => {} + } +} + /// `prop.toggleAnimation`; with `selected`, the other selected layers' same property follows. fn toggle_anim_selected(s: &mut Session, p: &Value) -> Result { let twins = selected_twins(s, p, "prop.toggleAnimation")?; diff --git a/crates/engine/src/tests/timeline.rs b/crates/engine/src/tests/timeline.rs index 6ea4d1336..43eb922e2 100644 --- a/crates/engine/src/tests/timeline.rs +++ b/crates/engine/src/tests/timeline.rs @@ -1554,6 +1554,30 @@ fn a_typed_factor_scales_each_selected_layers_own_value() { assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [50.0, 50.0], "B halves its own X, keeps its Y"); } +/// #3195: Mask Feather is never negative on the other selected layers either, when a typed `*-1` +/// (the edited layer clamped at 0 by the panel) or a change by the same amount takes theirs +/// below 0. +#[test] +fn a_selected_mask_feather_stays_non_negative() { + let (mut s, a) = setup(); + let b = s.execute("layer.newSolid", json!({"color": "#ff0000", "width": 50, "height": 50})).unwrap()["layer"].as_u64().unwrap(); + for (l, feather) in [(a, [10.0, 20.0]), (b, [4.0, 30.0])] { + s.execute("mask.new", json!({"layer": l, "vertices": [[0, 0], [20, 0], [20, 20]]})).unwrap(); + s.execute("prop.set", json!({"layer": l, "path": "masks/#1/feather", "value": feather})).unwrap(); + } + s.execute("layer.select", json!({"layers": [a, b]})).unwrap(); + let feather = |s: &Session, l: u64| prop(s, l, "masks/#1/feather").value.components(); + let uid = prop(&s, a, "masks/#1/feather").uid; + // `*-1` into the linked pair: the panel clamps the edited layer at [0, 0]. + s.execute_checked("prop.set", json!({"layer": a, "prop": uid, "value": [0.0, 0.0], "selected": true, "dims": [0, 1], "factor": -1.0})).unwrap(); + assert_eq!(feather(&s, a), [0.0, 0.0]); + assert_eq!(feather(&s, b), [0.0, 0.0], "B is clamped at 0, not [-4, -30]"); + // A change by the same amount (a scrub, or a typed `+`): A by [-10, -15] from [10, 20]. + s.execute("edit.undo", json!({})).unwrap(); + s.execute_checked("prop.set", json!({"layer": a, "prop": uid, "value": [0.0, 5.0], "selected": true, "offset": true})).unwrap(); + assert_eq!(feather(&s, b), [0.0, 15.0], "B's X is clamped at 0, its Y moves by -15"); +} + /// #2485: linked Audio Levels typed with several layers selected: each takes the typed Left, and /// its Right moves by its own change, so every layer keeps its own stereo balance. #[test] From 998448f94be3474e164d4feb0fcc2cdd97a4a2d1 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:41:19 +0100 Subject: [PATCH 042/350] Audio: a time-reversed layer plays its audio backwards (#3239) A layer with a negative time stretch was skipped by the mix. It now plays its audio reversed at its stretched speed, through the same linear resampler as forward stretch: a reversed read starts at the layer time of the run's end and is played last to first, so comp sample k plays the source just before layer_time(k) (at -100 % with the start at the out point, the exact mirror Backwards gives a forward layer). Backwards on a reversed layer plays it forwards. Resampled runs start at the exact source position (from the layer time in ticks) instead of the grid sample a block starts on, so mixing in frame blocks and in export's one-second blocks gives the same samples, and they read one more source sample so a block's last sample blends instead of holding. Audio effects run on comp time since the layer's start, which joins up across blocks at any stretch and equals the layer time at 100 %. Reads are made in pieces of at most MAX_READ source frames, so memory stays bounded for fast layers and nested fast precomps (the work for nested 1 % precomps is not bounded; follow-up). A stretch faster than 1 % is silent as a last-resort guard, with a tolerance so exactly 1 % plays. A zero or non-finite stretch is skipped. Fixes #3239 --- crates/effects/src/audio_fx.rs | 3 +- crates/render/src/audio.rs | 131 ++++++++++++++--- crates/render/src/tests/audio_fx.rs | 218 +++++++++++++++++++++++++++- 3 files changed, 329 insertions(+), 23 deletions(-) diff --git a/crates/effects/src/audio_fx.rs b/crates/effects/src/audio_fx.rs index c4cb50de2..d8c0a98f3 100644 --- a/crates/effects/src/audio_fx.rs +++ b/crates/effects/src/audio_fx.rs @@ -244,7 +244,8 @@ pub fn preroll(id: &str, params: &Params) -> f64 { } /// Run audio effect `id` over interleaved stereo `buf` (L R L R …) at `rate` Hz, from a cleared -/// state. `start` is the layer time (seconds) of the first sample (oscillator phase). +/// state. `start` is the time (seconds) of the first sample since the layer's start, counted at +/// comp speed whatever the layer's time stretch (oscillator phase). pub fn process(id: &str, params: &Params, buf: &mut [f32], rate: u32, start: f64) { if rate == 0 || buf.len() < 2 { return; diff --git a/crates/render/src/audio.rs b/crates/render/src/audio.rs index e2861340c..b41307b4e 100644 --- a/crates/render/src/audio.rs +++ b/crates/render/src/audio.rs @@ -12,8 +12,11 @@ //! pre-roll of earlier audio and only the block's tail is kept — so preview playback and export //! produce identical samples. //! -//! Time-stretched layers are resampled nearest-sample; reversed (negative stretch) layers are -//! silent for now. +//! Time-stretched layers are resampled linearly, so pitch follows the speed. A reversed layer +//! (negative stretch, Layer > Time > Time-Reverse Layer) plays its audio backwards at the same +//! speed: comp sample `k` plays the source just before layer time `layer_time(k)`, so at -100 % +//! with the start at the out point (After Effects' layout) it is the exact mirror of the +//! layer's span. Backwards on a reversed layer plays it forwards again. use effectcraft_effects::Params; use effectcraft_effects::audio_fx; @@ -24,6 +27,18 @@ use crate::{EvalCtx, ExprHost, FootageSource}; const MAX_DEPTH: usize = 16; +/// Fastest a layer's audio plays (source samples per comp sample): 1 % stretch, the smallest +/// Time Stretch accepts. A faster layer (a stretch below 1 %, only from a damaged or foreign +/// file) is silent instead of reading unbounded source: a last-resort guard, imports clamp +/// such stretches. +const MAX_SPEED: f64 = 100.0; + +/// Most source sample frames one read asks a footage item or precomp for; a longer read is made +/// in pieces, so memory stays bounded however fast a layer plays and however deep stretched +/// precomps nest. The work is not bounded: each 1 % precomp level still reads 100 times its +/// span. +pub(crate) const MAX_READ: usize = 1 << 18; + /// `frames` stereo sample frames of `comp`'s mix from comp time `start` at `rate` Hz, /// interleaved (L R L R …). pub fn mix_comp(project: &Project, footage: &dyn FootageSource, expr: Option<&dyn ExprHost>, comp: ItemId, start: Tick, frames: usize, rate: u32) -> Vec { @@ -102,6 +117,13 @@ fn animated(g: &effectcraft_project::PropGroup) -> bool { }) } +/// The clock (seconds) a layer's audio effects run on at comp time `t` (oscillator phase): comp +/// time since the layer's start. It advances one sample per sample at any stretch, forwards or +/// reversed, so blocks join up; at 100 % it is the layer time. +fn fx_clock(l: &Layer, t: Tick) -> f64 { + Tick(t.0.saturating_sub(l.start_time.0)).seconds() +} + fn sample_tick(i: i64, rate: u32) -> Tick { // Keep the requested sample when the rate does not divide the tick clock. let t = Tick::from_units(i, i64::from(rate)); @@ -127,7 +149,7 @@ impl MixEnv<'_> { /// The layer's own (pre-effect) audio for comp sample indices `i0 .. i0 + n`, silent /// outside its [in, out) span. Comp sample index `k` maps to `in + out − 1 − k` when - /// `reversed`. + /// `reversed` (the Backwards effect). fn layer_raw(&self, l: &Layer, i0: i64, n: usize, reversed: bool) -> Vec { let rate = self.rate; let mut out = vec![0.0f32; n * 2]; @@ -154,40 +176,84 @@ impl MixEnv<'_> { if a >= b { return out; } - let m = b - a; let speed = 100.0 / l.stretch; - let src_n = ((m as f64 * speed).ceil() as usize).max(1); - let lt = l.layer_time(sample_tick(i0 + a as i64, rate)); + let aspeed = speed.abs(); + // A zero or non-finite stretch never gets here (see `mix_into`); NaN is checked anyway. + // The tolerance keeps exactly 1 % audible whatever the float noise in the stretch. + if aspeed.is_nan() || aspeed > MAX_SPEED * (1.0 + 1e-9) { + return out; + } + let backwards = speed < 0.0; + // Comp samples per piece, so that one piece reads at most MAX_READ source frames (its + // span, the fraction it starts past a source sample, and the sample after its end). + let piece = (((MAX_READ - 4) as f64 / aspeed) as usize).clamp(1, b - a); + let mut p0 = a; + while p0 < b { + let p1 = p0.saturating_add(piece).min(b); + // Forwards a piece reads the source from the layer time of its first sample. Backwards + // it reads from the layer time of its end and plays what it read last to first, so + // comp sample `k` plays the source just before `layer_time(k)` — at -100 % the exact + // mirror Backwards gives a forward layer. + let origin = sample_tick(i0.saturating_add((if backwards { p1 } else { p0 }) as i64), rate); + let Some(run) = self.source_run(l, l.layer_time(origin), p1 - p0, aspeed) else { return out }; + for (i, f) in run.as_chunks::<2>().0.iter().take(p1 - p0).enumerate() { + let k = if backwards { (p1 - 1).saturating_sub(i) } else { p0 + i }; + if let Some(o) = out.get_mut(k * 2..k * 2 + 2) { + o.copy_from_slice(f); + } + } + p0 = p1; + } + out + } + + /// `m` sample frames of the layer's source audio from layer time `lt` on, played at `speed` + /// (≥ 0) source samples per comp sample: blended linearly between source samples, or copied + /// as they are at speed 1. `None` when the layer has no source audio. + /// + /// Sources deliver samples on their sample grid (from the sample at or before the time asked + /// for), so a resampled run reads from that grid sample and starts `x` at `lt`'s exact + /// position past it: a run then continues where the previous block or piece left off. + fn source_run(&self, l: &Layer, lt: Tick, m: usize, speed: f64) -> Option> { + let rate = self.rate; + let same = (speed - 1.0).abs() < 1e-9; + let s0 = lt.to_units_floor(i64::from(rate)); + let from = sample_tick(s0, rate); + // `lt`'s position past sample s0, in samples (0 ≤ frac < 1); copies ignore it. + let tps = TICKS_PER_SECOND as i128; + let frac = if same { 0.0 } else { (i128::from(lt.0) * i128::from(rate)).rem_euclid(tps) as f64 / tps as f64 }; + // Resampled, one more source sample than the run covers: the last sample blends with it. + let src_n = ((frac + m as f64 * speed).ceil() as usize).saturating_add(usize::from(!same)).max(1); let buf = match &l.source { LayerSource::Footage { item } => match self.project.item(*item).map(|i| &i.kind) { - Some(ItemKind::Footage(f)) if f.has_audio => self.footage.audio(*item, f, lt, src_n, rate).unwrap_or_else(|| vec![0.0; src_n * 2]), - _ => return out, + Some(ItemKind::Footage(f)) if f.has_audio => self.footage.audio(*item, f, from, src_n, rate).unwrap_or_else(|| vec![0.0; src_n * 2]), + _ => return None, }, LayerSource::Comp { item } => { if self.project.comp_contains(*item, self.comp_id) { - return out; + return None; } let mut sub = vec![0.0f32; src_n * 2]; - mix_into(&AudioSources { project: self.project, footage: self.footage, expr: self.expr, rate }, *item, lt, &mut sub, self.depth + 1); + mix_into(&AudioSources { project: self.project, footage: self.footage, expr: self.expr, rate }, *item, from, &mut sub, self.depth + 1); sub } - _ => return out, + _ => return None, }; - let same = (speed - 1.0).abs() < 1e-9; + let mut out = vec![0.0f32; m * 2]; for i in 0..m { // The source sample at fractional index `x`, blended linearly with the next one unless the // layer plays at its own speed (then the samples are copied as they are). - let x = i as f64 * speed; + let x = frac + i as f64 * speed; let j = if same { i } else { (x as usize).min(src_n - 1) }; if j * 2 + 1 >= buf.len() { break; } let k = if same || (j + 1) * 2 + 1 >= buf.len() { j } else { j + 1 }; let f = if same { 0.0 } else { ((x - j as f64) as f32).clamp(0.0, 1.0) }; - out[(a + i) * 2] = buf[j * 2] + (buf[k * 2] - buf[j * 2]) * f; - out[(a + i) * 2 + 1] = buf[j * 2 + 1] + (buf[k * 2 + 1] - buf[j * 2 + 1]) * f; + out[i * 2] = buf[j * 2] + (buf[k * 2] - buf[j * 2]) * f; + out[i * 2 + 1] = buf[j * 2 + 1] + (buf[k * 2 + 1] - buf[j * 2 + 1]) * f; } - out + Some(out) } /// Effect parameters at comp time `t`. @@ -215,7 +281,7 @@ impl MixEnv<'_> { let pre = (pre_s * self.rate as f64).ceil() as usize; let b0 = i0 - pre as i64; let mut buf = self.layer_raw(l, b0, n + pre, reversed); - let start = l.layer_time(sample_tick(b0, self.rate)).seconds(); + let start = fx_clock(l, sample_tick(b0, self.rate)); for (id, g, p) in &fx { if audio_fx::per_sample(id) && animated(g) { self.process_stepped(l, id, g, &mut buf, b0); @@ -239,7 +305,7 @@ impl MixEnv<'_> { let end = usize::try_from(next.saturating_sub(b0)).unwrap_or(frames).clamp(k + 1, frames); let t = sample_tick(i, self.rate); if let Some(piece) = buf.get_mut(k * 2..end * 2) { - audio_fx::process(id, &self.params(l, g, t), piece, self.rate, l.layer_time(t).seconds()); + audio_fx::process(id, &self.params(l, g, t), piece, self.rate, fx_clock(l, t)); } k = end; } @@ -291,7 +357,8 @@ fn mix_into(sources: &AudioSources<'_>, comp_id: ItemId, start: Tick, out: &mut let any_solo = comp.layers.iter().any(|l| l.switches.solo && audible(project, l)); let s0 = start.to_units_floor(rate as i64); for l in &comp.layers { - if !audible(project, l) || (any_solo && !l.switches.solo) || l.stretch <= 0.0 { + // A zero or non-finite stretch has no layer time to play; a negative one plays backwards. + if !audible(project, l) || (any_solo && !l.switches.solo) || l.stretch == 0.0 || !l.stretch.is_finite() { continue; } // Sample range of the block that falls inside the layer's [in, out). @@ -482,6 +549,11 @@ mod tests { let base = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); let mut tone = build::layer(&mut p, &base, "tone", LayerSource::Footage { item: fid }, (0, 0), None); tone.stretch = stretch; + // A reversed layer as After Effects lays it out: over [0, 2) s, starting at its out point. + let end = if stretch < 0.0 { 2.0 } else { 0.0 }; + if stretch < 0.0 { + (tone.in_point, tone.out_point, tone.start_time) = (Tick::ZERO, Tick::from_seconds_f64(2.0), Tick::from_seconds_f64(2.0)); + } let mut comp = base.clone(); comp.layers = vec![tone].into(); let cid = p.add_item("C", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(comp.into())); @@ -491,8 +563,11 @@ mod tests { let speed = 100.0 / stretch; let (mut worst, mut sq) = (0.0f64, 0.0f64); for i in 0..n { - // The source plays `speed` times as fast as the comp: source time = speed * comp time. - let ideal = (std::f64::consts::TAU * 440.0 * speed * (t.seconds() + i as f64 / rate as f64)).sin() * 0.5; + // The source plays `speed` times as fast as the comp: source time = speed * (comp time − + // start). Reversed, a comp sample plays the source sample just before that time. + let back = if stretch < 0.0 { speed / rate as f64 } else { 0.0 }; + let src = speed * (t.seconds() + i as f64 / rate as f64 - end) + back; + let ideal = (std::f64::consts::TAU * 440.0 * src).sin() * 0.5; let e = (m[i * 2] as f64 - ideal).abs(); worst = worst.max(e); sq += e * e; @@ -511,4 +586,18 @@ mod tests { assert!(worst < 1e-2, "stretch {stretch}%: error {worst} at 8 kHz"); } } + + /// A reversed layer plays its tone backwards with the same resampling quality as forwards + /// (it was silent, #3239); at -100 % the samples are copied exactly. + #[test] + fn reversed_layers_resample_a_tone_with_small_error() { + for stretch in [-70.0, -33.0] { + let (worst, _) = stretch_error(stretch, 48_000); + assert!(worst < 1e-3, "stretch {stretch}%: error {worst} at 48 kHz"); + let (worst, _) = stretch_error(stretch, 8_000); + assert!(worst < 1e-2, "stretch {stretch}%: error {worst} at 8 kHz"); + } + let (worst, _) = stretch_error(-100.0, 48_000); + assert!(worst < 1e-5, "stretch -100%: error {worst}"); + } } diff --git a/crates/render/src/tests/audio_fx.rs b/crates/render/src/tests/audio_fx.rs index a6c85f2de..5ef2949e5 100644 --- a/crates/render/src/tests/audio_fx.rs +++ b/crates/render/src/tests/audio_fx.rs @@ -1,13 +1,14 @@ //! Audio effects through the mixdown (the path preview playback and export share). use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; use effectcraft_keyframe::{Keyframe, Value}; use effectcraft_project::build::{self, Ids}; use effectcraft_project::{Comp, Footage, FootageKind, ItemId, ItemKind, LayerSource, Project, Solid}; use effectcraft_time::{FrameRate, Tick}; -use crate::audio::{comp_has_audio, footage_peaks, mix_comp, peak_summary}; +use crate::audio::{MAX_READ, comp_has_audio, footage_peaks, mix_comp, peak_summary}; use crate::{FootageSource, Image}; const SR: u32 = 8000; @@ -109,6 +110,221 @@ fn backwards_reverses_the_layer_span() { assert!((m[4000 * 2] - 0.5).abs() < 1e-3); } +/// `comp_with`, its layer reversed at `stretch` (< 0) the way After Effects lays it out: in 0, +/// out `out` s, starting at the out point. +fn reversed(id: Option<&str>, vals: &[(&str, Value)], solid: bool, stretch: f64, out: f64) -> (Project, ItemId) { + let (mut p, cid) = comp_with(id, vals, solid); + let l = &mut p.comp_mut(cid).unwrap().layers[0]; + l.stretch = stretch; + (l.in_point, l.out_point, l.start_time) = (Tick::ZERO, Tick::from_seconds_f64(out), Tick::from_seconds_f64(out)); + (p, cid) +} + +/// A time-reversed layer plays its audio backwards instead of being silent (#3239): at -100 % +/// sample for sample what Backwards plays on the forward layer, in any block size. +#[test] +fn a_reversed_layer_plays_its_audio_backwards() { + let n = SR as usize; + let (p, cid) = reversed(None, &[], false, -100.0, 1.0); + let m = mix(&p, &Signal(ramp), cid, 0.0, n); + // Comp sample k plays source sample n − 1 − k. + for k in [0usize, 1, 2000, 4000, 7000] { + let want = (n - 1 - k) as f32 / SR as f32; + assert!((m[k * 2] - want).abs() < 1e-6 && (m[k * 2 + 1] + want).abs() < 1e-6, "k={k}: {} {}, want {want}", m[k * 2], m[k * 2 + 1]); + } + let (bp, bcid) = comp_with(Some("ec.audio.backwards"), &[], false); + assert_eq!(m, mix(&bp, &Signal(ramp), bcid, 0.0, n), "not the mirror Backwards plays"); + let mut blocks = vec![]; + for k in 0..n / 320 { + blocks.extend(mix(&p, &Signal(ramp), cid, (k * 320) as f64 / SR as f64, 320)); + } + let worst = m.iter().zip(&blocks).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(worst < 1e-6, "block size changes the result by {worst}"); +} + +/// The speed of a reversed layer follows its stretch as a forward one's does: -50 % plays the +/// source twice as fast, -200 % at half speed, both backwards. +#[test] +fn a_reversed_layer_plays_at_its_stretch() { + // -50 % over 0.5 s: comp sample k plays source sample 2 (n − 1 − k), n = 4000. + let (p, cid) = reversed(None, &[], false, -50.0, 0.5); + let m = mix(&p, &Signal(ramp), cid, 0.0, 4000); + for k in [0usize, 1000, 3000] { + let want = 2.0 * (3999 - k) as f32 / SR as f32; + assert!((m[k * 2] - want).abs() < 1e-6, "-50 %, k={k}: {}, want {want}", m[k * 2]); + } + // -200 % over 2 s: comp sample k plays source sample (n − 1 − k) / 2, n = 16000, blended. + let (p, cid) = reversed(None, &[], false, -200.0, 2.0); + let m = mix(&p, &Signal(ramp), cid, 0.0, 16_000); + for k in [0usize, 1, 5001, 12_000] { + let want = (15_999 - k) as f32 / 2.0 / SR as f32; + assert!((m[k * 2] - want).abs() < 1e-5, "-200 %, k={k}: {}, want {want}", m[k * 2]); + } +} + +/// Backwards on a reversed layer plays it forwards: the two reversals cancel. +#[test] +fn backwards_on_a_reversed_layer_plays_forwards() { + let (p, cid) = reversed(Some("ec.audio.backwards"), &[], false, -100.0, 1.0); + let (fp, fcid) = comp_with(None, &[], false); + for (from, frames) in [(0.0, SR as usize), (0.25, 2000)] { + let m = mix(&p, &Signal(ramp), cid, from, frames); + assert!(m.iter().any(|v| v.abs() > 0.1), "silent from {from} s"); + assert_eq!(m, mix(&fp, &Signal(ramp), fcid, from, frames), "from {from} s"); + } +} + +/// Effects on a reversed layer run on a clock that moves forwards with the samples at comp speed, +/// so a Tone joins up across blocks (preview's frame blocks and export's one-second blocks +/// agree), at -50 % too. On the layer-time clock, block k at -100 % would start 0.08 k s off: +/// 35.2 k cycles at 440 Hz, a phase jump. +#[test] +fn tone_on_a_reversed_layer_joins_up_across_blocks() { + let mut vals: Vec<(&str, Value)> = vec![("frequency1", Value::Scalar(440.0))]; + for k in ["frequency2", "frequency3", "frequency4", "frequency5"] { + vals.push((k, Value::Scalar(0.0))); + } + for stretch in [-100.0, -50.0] { + let (p, cid) = reversed(Some("ec.audio.tone"), &vals, true, stretch, 1.0); + let long = mix(&p, &Signal(ramp), cid, 0.2, 1600); + assert!(rms(&long) > 0.05, "{stretch} %: silent: {}", rms(&long)); + let mut blocks = vec![]; + for k in 0..5 { + blocks.extend(mix(&p, &Signal(ramp), cid, 0.2 + k as f64 * 0.04, 320)); + } + let worst = long.iter().zip(&blocks).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(worst < 1e-3, "{stretch} %: block size changes the tone by {worst}"); + } +} + +/// Footage audio on its sample grid, as the media pool decodes it: a read from any time starts +/// at the sample at or before it. Sample n is `f(n / rate)` over [0, 2) s. +struct Grid(fn(f64) -> (f32, f32)); +impl FootageSource for Grid { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, start: Tick, frames: usize, rate: u32) -> Option> { + let s0 = start.to_units_floor(i64::from(rate)); + Some( + (0..frames as i64) + .flat_map(|i| { + let t = (s0 + i) as f64 / rate as f64; + let (l, r) = if (0.0..2.0).contains(&t) { (self.0)(t) } else { (0.0, 0.0) }; + [l, r] + }) + .collect(), + ) + } +} + +/// A stretched layer, forwards or reversed, gives the same samples mixed in one block as in +/// blocks of any size (preview's video-frame blocks, export's seconds): each block resamples +/// from the exact source position, not from the source sample a block happens to start on. +#[test] +fn stretched_layers_join_up_across_blocks() { + // (stretch, out point, start): the reversed layers start at their out point, the -50 % one + // off the sample grid (a third of a sample late), so its source positions are fractional. + let third = Tick(Tick::from_seconds_f64(1.0 / SR as f64).0 / 3); + for (stretch, out, start) in [ + (200.0, 2.0, Tick::ZERO), + (-70.0, 0.7, Tick::from_seconds_f64(0.7)), + (-50.0, 0.5, Tick(Tick::from_seconds_f64(0.5).0 + third.0)), + (-200.0, 2.0, Tick::from_seconds_f64(2.0)), + ] { + let (mut p, cid) = comp_with(None, &[], false); + let l = &mut p.comp_mut(cid).unwrap().layers[0]; + l.stretch = stretch; + (l.in_point, l.out_point, l.start_time) = (Tick::ZERO, Tick::from_seconds_f64(out), start); + let n = 3210; + let long = mix(&p, &Grid(ramp), cid, 0.1, n); + assert!(long.iter().any(|v| v.abs() > 0.05), "{stretch} %: silent"); + for block in [321, 107] { + let mut blocks = vec![]; + for k in 0..n / block { + blocks.extend(mix(&p, &Grid(ramp), cid, 0.1 + (k * block) as f64 / SR as f64, block)); + } + let worst = long.iter().zip(&blocks).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(worst < 1e-6, "{stretch} %, blocks of {block}: off by {worst}"); + } + } +} + +/// At 200 % the last sample of a block blends with the source sample after it, as inside the +/// block, instead of holding the one before. +#[test] +fn a_stretched_block_ends_on_a_blended_sample() { + let (mut p, cid) = comp_with(None, &[], false); + let l = &mut p.comp_mut(cid).unwrap().layers[0]; + l.stretch = 200.0; + (l.in_point, l.out_point, l.start_time) = (Tick::ZERO, Tick::from_seconds_f64(2.0), Tick::ZERO); + let m = mix(&p, &Grid(ramp), cid, 0.0, 320); + // Comp sample 319 plays source position 159.5. + let want = 159.5 / SR as f32; + assert!((m[319 * 2] - want).abs() < 1e-6, "{}, want {want}", m[319 * 2]); +} + +/// Records the longest audio read and answers with a short run, so an unbounded request fails +/// the assert below instead of allocating. +struct Longest(AtomicUsize); +impl FootageSource for Longest { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, _: u32) -> Option> { + self.0.fetch_max(frames, Ordering::Relaxed); + Some(vec![0.25; frames.min(4096) * 2]) + } +} + +/// Tiny, huge, zero and non-finite stretches, and fast precomps nested in fast precomps, read +/// at most MAX_READ source frames at a time and mix to finite samples (a tiny positive stretch +/// asked for 10⁸ times the block, and its precomp buffer could abort the process). +#[test] +fn extreme_stretches_read_bounded_source() { + let stretches = [1e-6, -1e-6, 0.5, -0.5, 1.0, -1.0, 1e12, -1e12, f64::MIN_POSITIVE, 0.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY]; + for stretch in stretches { + let (mut p, cid) = comp_with(None, &[], false); + p.comp_mut(cid).unwrap().layers[0].stretch = stretch; + let src = Longest(AtomicUsize::new(0)); + let m = mix(&p, &src, cid, 0.0, 800); + let longest = src.0.load(Ordering::Relaxed); + assert!(longest <= MAX_READ, "stretch {stretch}: read {longest} frames at once"); + assert!(m.iter().all(|v| v.is_finite()), "stretch {stretch}"); + } + // Exactly 1 %, give or take float noise, stays audible. + let (mut p, cid) = comp_with(None, &[], false); + p.comp_mut(cid).unwrap().layers[0].stretch = 1.0 - 1e-12; + let m = mix(&p, &Longest(AtomicUsize::new(0)), cid, 0.0, 800); + assert!(m.iter().any(|v| *v != 0.0), "1 % is silent"); + // A 1 % footage layer in a 1 % precomp: 10⁴ source samples per comp sample. The precomp layer + // lasts 0.02 s, which plays the inner layer's 2 s once (backwards from its out point at -1 %). + for sign in [1.0, -1.0] { + let mut p = Project::default(); + let base = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); + let fid = p.add_item("x.wav", effectcraft_color::Label::SeaFoam, None, ItemKind::Footage(audio_footage())); + let mut inner_l = build::layer(&mut p, &base, "a", LayerSource::Footage { item: fid }, (64, 64), None); + inner_l.stretch = sign; + (inner_l.in_point, inner_l.out_point, inner_l.start_time) = + (Tick::ZERO, Tick::from_seconds_f64(2.0), if sign < 0.0 { Tick::from_seconds_f64(2.0) } else { Tick::ZERO }); + let mut inner = base.clone(); + inner.layers = vec![inner_l].into(); + let inner_id = p.add_item("Inner", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(inner.into())); + let mut pre = build::layer(&mut p, &base, "pre", LayerSource::Comp { item: inner_id }, (64, 64), None); + pre.stretch = sign; + let pre_out = Tick::from_seconds_f64(0.02); + (pre.in_point, pre.out_point, pre.start_time) = (Tick::ZERO, pre_out, if sign < 0.0 { pre_out } else { Tick::ZERO }); + let mut outer = base.clone(); + outer.layers = vec![pre].into(); + let outer_id = p.add_item("Outer", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(outer.into())); + let src = Longest(AtomicUsize::new(0)); + let m = mix(&p, &src, outer_id, 0.0, 800); + let longest = src.0.load(Ordering::Relaxed); + assert!(longest > 0 && longest <= MAX_READ, "nested {sign}%: read {longest} frames at once"); + assert!(m.iter().all(|v| v.is_finite())); + } +} + #[test] fn high_low_pass_attenuates() { fn hi(t: f64) -> (f32, f32) { From 2abd727338dea0a7b48131794689253ba01a8173 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:41:56 +0100 Subject: [PATCH 043/350] AEP import: Puppet (ADBE FreePin3) arrives as EffectCraft's Puppet, its meshes and pins are reported After Effects' Puppet was dropped with a one-line "has the effect" loss. It now arrives as EffectCraft's Puppet (ec.distort.puppet) in its place in the stack, with its name and fx switch, and the report lists per mesh Triangles, Expansion and per pin group (Deform, Overlap, Stiffness) the pins, keyed pins and atom match names; entries outside the known tree are named once, and long lists stop at ten with "and N more". No pins are created: a pin's rest place is a vertex of the hidden mesh data and the unit of its Position is unknown, and no corpus file holds a Puppet. The tree (docs/aep-format.md 3.7) comes from an external scripting listing, not the corpus; the sample that settles it is described there. The fx_inject test helpers can now inject any effect instance, which the new tests use. Refs #3093 --- crates/aep/src/import.rs | 1 + crates/aep/src/import/effects.rs | 7 +- crates/aep/src/import/puppet.rs | 234 ++++++++++++++++++++++++ crates/aep/tests/it/import.rs | 1 + crates/aep/tests/it/import/fx_inject.rs | 40 ++-- crates/aep/tests/it/import/puppet.rs | 171 +++++++++++++++++ docs/aep-format.md | 49 ++++- docs/aep-import.md | 4 +- 8 files changed, 489 insertions(+), 18 deletions(-) create mode 100644 crates/aep/src/import/puppet.rs create mode 100644 crates/aep/tests/it/import/puppet.rs diff --git a/crates/aep/src/import.rs b/crates/aep/src/import.rs index 956e3b1c5..938d0c46e 100644 --- a/crates/aep/src/import.rs +++ b/crates/aep/src/import.rs @@ -30,6 +30,7 @@ mod effects; mod markers; mod masks; mod props; +mod puppet; mod report; mod settings; mod shapes; diff --git a/crates/aep/src/import/effects.rs b/crates/aep/src/import/effects.rs index 8409a6f7e..f205245eb 100644 --- a/crates/aep/src/import/effects.rs +++ b/crates/aep/src/import/effects.rs @@ -76,6 +76,11 @@ pub(crate) fn import_effects<'f, 'a>( col: &mut Collector, ) { for fx in layer.effects() { + // Puppet keeps its meshes and pins in nested groups, not in `parT` (its own step). + if fx.match_name() == crate::import::puppet::MATCH_NAME { + crate::import::puppet::import_puppet(defs, &fx, cx, ids, target, col); + continue; + } let shown = fx.display_name().unwrap_or_else(|| fx.match_name().to_string()); let Some(map) = EFFECTS.iter().find(|m| m.match_name == fx.match_name()) else { col.lose(LossCategory::Effects, format!("{}: '{}' has the effect '{shown}' ({})", cx.comp, cx.layer, fx.match_name())); @@ -112,7 +117,7 @@ pub(crate) fn import_effects<'f, 'a>( } /// Parameters that never carry a value to import. -fn carries_nothing(p: &EffectParam<'_, '_>) -> bool { +pub(crate) fn carries_nothing(p: &EffectParam<'_, '_>) -> bool { matches!(p.kind, ParamKind::Button | ParamKind::NoData | ParamKind::GroupStart | ParamKind::GroupEnd) || (p.index == 0 && p.kind == ParamKind::Layer) } diff --git a/crates/aep/src/import/puppet.rs b/crates/aep/src/import/puppet.rs new file mode 100644 index 000000000..ce10ae7f5 --- /dev/null +++ b/crates/aep/src/import/puppet.rs @@ -0,0 +1,234 @@ +//! After Effects' Puppet effect (`ADBE FreePin3`) arrives as EffectCraft's Puppet +//! (`ec.distort.puppet`) in its place in the stack, with its name and fx switch, but **without +//! its meshes and pins**: how After Effects stores a pin's rest place (a vertex index and offset +//! into the hidden mesh data) and its Position (pixels or a fraction of the layer) is not decoded +//! yet, and a guessed rest place would deform the layer wrongly without a word. What the instance +//! holds is reported instead, mesh by mesh: Triangles, Expansion, and per pin container (Deform, +//! Stiffness, Overlap) the number of pins, how many are keyed or carry an expression, and the +//! match names of their atoms (Advanced and Bend pins show up there). Any entry this reader does +//! not know is named verbatim. +//! +//! The tree read here (docs/aep-format.md 3.7, Puppet) comes from an external scripting listing +//! of the effect's property tree, not from the corpus (no corpus file has a Puppet): **?** until a +//! byte sample exists. It is read with the same verified group reader as masks, shapes and layer +//! styles; a file whose tree differs is reported, never guessed at. + +use effectcraft_project::build::Ids; +use effectcraft_project::{Node, PropGroup}; + +use crate::import::effects::{LayerCtx, carries_nothing}; +use crate::import::report::{Collector, LossCategory}; +use crate::{Effect as AepEffect, EffectDefinitions, ParamValue, PropGroup as AepGroup, PropNode, Property as AepProperty}; + +/// After Effects' Puppet effect. +pub(crate) const MATCH_NAME: &str = "ADBE FreePin3"; +/// The hidden group that holds the meshes (`arap`). +const ARAP: &str = "ADBE FreePin3 ARAP Group"; +/// Auto-traced Shapes, beside the meshes. +const OUTLINES: &str = "ADBE FreePin3 Outlines"; +/// The hidden group of the meshes. +const MESH_GROUP: &str = "ADBE FreePin3 Mesh Group"; +/// One mesh (`Mesh 1`). +const MESH_ATOM: &str = "ADBE FreePin3 Mesh Atom"; +/// A mesh's hidden triangulation. +const MESH_DATA: &str = "ADBE FreePin3 Mesh"; +const TRIANGLES: &str = "ADBE FreePin3 Mesh Tri Count"; +const EXPANSION: &str = "ADBE FreePin3 Mesh Expansion"; +/// At most this many meshes, pin atom names or unrecognised names are listed in a report line; +/// the rest are counted (`and 3 more`). +const LIST_CAP: usize = 10; +/// The pin containers of a mesh: match name and the name After Effects shows. +const PIN_GROUPS: [(&str, &str); 3] = [("ADBE FreePin3 PosPins", "Deform"), ("ADBE FreePin3 HghtPins", "Overlap"), ("ADBE FreePin3 StarchPins", "Stiffness")]; + +/// Imports a Puppet instance (the caller checked the match name) into `target`. +pub(crate) fn import_puppet<'f, 'a>( + defs: &EffectDefinitions<'f, 'a>, + fx: &AepEffect<'f, 'a>, + cx: &LayerCtx<'_>, + ids: &mut Ids<'_>, + target: &mut PropGroup, + col: &mut Collector, +) { + let shown = fx.display_name().unwrap_or_else(|| fx.match_name().to_string()); + let here = format!("{}: '{}' effect '{shown}' ({MATCH_NAME})", cx.comp, cx.layer); + let Some(spec) = effectcraft_effects::find(effectcraft_effects::puppet::ID) else { + col.lose(LossCategory::Effects, format!("{}: '{}' has the effect '{shown}' ({MATCH_NAME})", cx.comp, cx.layer)); + return; + }; + let size = [f64::from(cx.size.0), f64::from(cx.size.1)]; + let mut group = effectcraft_effects::instantiate(spec, ids, &shown, size); + match fx.enabled() { + Some(on) => group.enabled = on, + None => col.lose(LossCategory::DamagedData, format!("{}: '{}': the fx switch of '{shown}' is unreadable, imported switched on", cx.comp, cx.layer)), + } + // The ordinary parameters (`parT`): none is mapped yet, so a stored one is reported. + let params = defs.params(fx); + for p in ¶ms { + if carries_nothing(p) { + continue; + } + if p.stored().is_some() && !matches!(p.stored_value(), Some(ParamValue::Layer(0))) { + let label = if p.name.is_empty() { p.match_name.clone() } else { format!("{} ({})", p.name, p.match_name) }; + col.lose(LossCategory::Effects, format!("{here}: parameter {label} not imported")); + } + } + report_meshes(fx, ¶ms.iter().map(|p| p.match_name.as_str()).collect::>(), &here, col); + // The effect itself arrives (an EffectCraft Puppet the user can pin again), so it counts as + // imported; what it held is in the losses above. + col.imported.effects = col.imported.effects.saturating_add(1); + target.children.push(Node::Group(group)); +} + +/// Reports the meshes and pins of `fx` and every entry of its value group that is neither a +/// `parT` parameter (`param_names`) nor the mesh tree. +fn report_meshes(fx: &AepEffect<'_, '_>, param_names: &[&str], here: &str, col: &mut Collector) { + let mut unknown: Vec = Vec::new(); + let values = fx.chunk().child_list(b"tdgp").and_then(AepGroup::from_chunk); + let mut arap = None; + for e in values.map(|g| g.entries()).unwrap_or_default() { + if e.match_name == ARAP { + if let PropNode::Group(g) = e.node { + if arap.is_none() { + arap = Some(g); + } + } else { + push_unique(&mut unknown, e.match_name); + } + } else if !param_names.contains(&e.match_name.as_str()) { + match e.node { + // A value outside `parT` (On Transparent, ...): reported with what it holds. + PropNode::Property(p) => col.lose(LossCategory::Effects, format!("{here}: property {} ({}) not imported", e.match_name, describe(&p))), + PropNode::Group(_) | PropNode::Other(_) => push_unique(&mut unknown, e.match_name), + } + } + } + let mut meshes: Vec = Vec::new(); + let mut found = false; + if let Some(arap) = arap { + for e in arap.entries() { + match (e.match_name.as_str(), e.node) { + (MESH_GROUP, PropNode::Group(g)) if !found => { + found = true; + for m in g.entries() { + match (m.match_name.as_str(), m.node) { + (MESH_ATOM, PropNode::Group(mesh)) => { + let n = meshes.len().saturating_add(1); + meshes.push(mesh_summary(&mesh, n, &mut unknown)); + } + _ => push_unique(&mut unknown, m.match_name.clone()), + } + } + } + // Auto-traced Shapes are made from the layer again (EffectCraft traces the alpha). + (OUTLINES, _) => {} + _ => push_unique(&mut unknown, e.match_name.clone()), + } + } + } + let extra = if unknown.is_empty() { String::new() } else { format!("; not recognised: {}", capped(&unknown, ", ")) }; + if !found { + col.lose( + LossCategory::Effects, + format!("{here}: imported without meshes or pins: its mesh group ({ARAP} / {MESH_GROUP}) was not found or is not a group{extra}"), + ); + } else if !meshes.is_empty() { + col.lose( + LossCategory::Effects, + format!( + "{here}: imported without its meshes and pins (After Effects' pin rest places and positions are not decoded yet): {}{extra}", + capped(&meshes, "; ") + ), + ); + } else if !unknown.is_empty() { + col.lose(LossCategory::Effects, format!("{here}: no mesh{extra}")); + } +} + +/// `Mesh n (Triangles t, Expansion e): Deform 3 pins (1 keyed) [atom names], Overlap no pins, ...` +fn mesh_summary(mesh: &AepGroup<'_, '_>, n: usize, unknown: &mut Vec) -> String { + let name = mesh.user_name().unwrap_or_else(|| format!("Mesh {n}")); + let mut settings: Vec = Vec::new(); + let mut containers: Vec = Vec::new(); + for e in mesh.entries() { + let label = match e.match_name.as_str() { + TRIANGLES => Some("Triangles"), + EXPANSION => Some("Expansion"), + _ => None, + }; + match (label, e.node) { + (Some(label), PropNode::Property(p)) => settings.push(format!("{label} {}", describe(&p))), + // The triangulation After Effects keeps; EffectCraft builds its own. + _ if e.match_name == MESH_DATA => {} + _ => match (PIN_GROUPS.iter().find(|(m, _)| *m == e.match_name), e.node) { + (Some((_, shown)), PropNode::Group(g)) => containers.push(format!("{shown} {}", pins_summary(&g, unknown))), + _ => push_unique(unknown, e.match_name), + }, + } + } + let settings = if settings.is_empty() { String::new() } else { format!(" ({})", settings.join(", ")) }; + let containers = if containers.is_empty() { "no pin groups".to_string() } else { containers.join(", ") }; + format!("{name}{settings}: {containers}") +} + +/// `3 pins (1 keyed, 1 with an expression) [ADBE FreePin3 PosPin Atom]`, or `no pins`. An entry +/// of the container that is not a group (a pin is one) goes to `unknown`. +fn pins_summary(container: &AepGroup<'_, '_>, unknown: &mut Vec) -> String { + let mut count = 0usize; + let mut keyed = 0usize; + let mut expressions = 0usize; + let mut atoms: Vec = Vec::new(); + for e in container.entries() { + let PropNode::Group(pin) = e.node else { + push_unique(unknown, e.match_name); + continue; + }; + count = count.saturating_add(1); + push_unique(&mut atoms, e.match_name); + let props: Vec> = + pin.entries().into_iter().filter_map(|p| if let PropNode::Property(p) = p.node { Some(p) } else { None }).collect(); + if props.iter().any(AepProperty::is_animated) { + keyed = keyed.saturating_add(1); + } + if props.iter().any(|p| p.expression().is_some()) { + expressions = expressions.saturating_add(1); + } + } + if count == 0 { + return "no pins".to_string(); + } + let mut what = vec![format!("{keyed} keyed")]; + if expressions > 0 { + what.push(format!("{expressions} with an expression")); + } + format!("{count} pin{} ({}) [{}]", if count == 1 { "" } else { "s" }, what.join(", "), capped(&atoms, ", ")) +} + +/// A stored property's value for the report: `keyed`, the static value, or `unreadable`. +fn describe(p: &AepProperty<'_, '_>) -> String { + if p.is_animated() { + return "keyed".to_string(); + } + match p.static_value() { + Some(v) if v.len() == 1 => v.first().map(|x| format!("{x}")).unwrap_or_default(), + Some(v) => format!("[{}]", v.iter().map(|x| format!("{x}")).collect::>().join(", ")), + None => "unreadable".to_string(), + } +} + +/// Adds `name` to `names` unless it is there already. +fn push_unique(names: &mut Vec, name: String) { + if !names.contains(&name) { + names.push(name); + } +} + +/// The first [`LIST_CAP`] of `items` joined by `sep`, and how many more there are. +fn capped(items: &[String], sep: &str) -> String { + let shown: Vec<&str> = items.iter().take(LIST_CAP).map(String::as_str).collect(); + let mut out = shown.join(sep); + let more = items.len().saturating_sub(LIST_CAP); + if more > 0 { + out.push_str(&format!("{sep}and {more} more")); + } + out +} diff --git a/crates/aep/tests/it/import.rs b/crates/aep/tests/it/import.rs index 102a215c8..61a04c401 100644 --- a/crates/aep/tests/it/import.rs +++ b/crates/aep/tests/it/import.rs @@ -19,6 +19,7 @@ mod fx_inject; mod markers; mod masks; mod mattes; +mod puppet; mod resaved_defaults; mod robustness; mod shapes; diff --git a/crates/aep/tests/it/import/fx_inject.rs b/crates/aep/tests/it/import/fx_inject.rs index 81e6a821a..ae61794d3 100644 --- a/crates/aep/tests/it/import/fx_inject.rs +++ b/crates/aep/tests/it/import/fx_inject.rs @@ -103,18 +103,18 @@ pub fn tdmn(name: &str) -> Chunk<'static> { } /// A UTF-8 string chunk (`Utf8`). -fn utf8(s: &str) -> Chunk<'static> { +pub fn utf8(s: &str) -> Chunk<'static> { Chunk::data(*b"Utf8", s.as_bytes().to_vec()) } /// A chunk whose body is chunks without being a `LIST` (`pdnm`, `fnam`: `NESTED_IDS`). -fn nested(id: [u8; 4], children: Vec>) -> Chunk<'static> { +pub fn nested(id: [u8; 4], children: Vec>) -> Chunk<'static> { Chunk { id, body: Body::Chunks(children), pad: None } } /// A static value record holding `v`, after the one After Effects wrote for Glow's A & B Midpoint /// (its header; without the expression). -fn record(template: &Chunk<'_>, v: f64) -> Chunk<'static> { +pub fn record(template: &Chunk<'_>, v: f64) -> Chunk<'static> { let mut kids = Vec::new(); for c in template.children() { match &c.body { @@ -131,7 +131,7 @@ fn record(template: &Chunk<'_>, v: f64) -> Chunk<'static> { } /// A keyframed value record: `record`'s header with a key list of linear one-component keys. -fn keyed_record(template: &Chunk<'_>, keys: &[(i32, f64)]) -> Chunk<'static> { +pub fn keyed_record(template: &Chunk<'_>, keys: &[(i32, f64)]) -> Chunk<'static> { let Body::List { children, .. } = record(template, keys.first().map_or(0.0, |k| k.1)).body else { unreachable!("record builds a LIST") }; let mut children = children; let mut lhd3 = vec![0u8; 52]; @@ -229,39 +229,51 @@ fn find_glow_template(chunks: &[Chunk<'_>]) -> Option> { None } -/// Replaces every Glow (`ADBE Glo2` with its `sspc`) below `chunks` with `fx`; the number replaced. -fn replace_glow(chunks: &mut [Chunk<'_>], fx: &Fx, template: &Chunk<'_>) -> usize { +/// Replaces every Glow (`ADBE Glo2` with its `sspc`) below `chunks` with the effect `match_name` +/// and its `sspc`; the number replaced. +fn replace_glow(chunks: &mut [Chunk<'_>], match_name: &str, sspc: &Chunk<'static>) -> usize { let mut n = 0; for ch in chunks { let kids = ch.children_mut(); if let Some(at) = kids.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.starts_with(b"ADBE Glo2\0"))) && kids.get(at + 1).is_some_and(|k| k.is_list(b"sspc")) { - kids[at] = tdmn(fx.match_name); - kids[at + 1] = sspc(fx, template); + kids[at] = tdmn(match_name); + kids[at + 1] = sspc.clone(); n += 1; } - n += replace_glow(kids, fx, template); + n += replace_glow(kids, match_name, sspc); } n } -/// `synthetic.aep` with `fx` in place of the Glow effect. -pub fn bytes_with(fx: &Fx) -> Vec { +/// `synthetic.aep` with the effect `match_name` in place of the Glow effect; `make` builds its +/// `sspc` from the Glow's value record (the template of every record). +pub fn bytes_with_effect(match_name: &str, make: impl Fn(&Chunk<'_>) -> Chunk<'static>) -> Vec { let file = parse(SYNTHETIC).unwrap(); let template = find_glow_template(&file.chunks).expect("Glow's A & B Midpoint record"); + let sspc = make(&template); let mut file = parse(SYNTHETIC).unwrap(); - assert_eq!(replace_glow(&mut file.chunks, fx, &template), 2, "the Glow effect and its shared definitions"); + assert_eq!(replace_glow(&mut file.chunks, match_name, &sspc), 2, "the Glow effect and its shared definitions"); write(&file).unwrap() } +/// `synthetic.aep` with `fx` in place of the Glow effect. +pub fn bytes_with(fx: &Fx) -> Vec { + bytes_with_effect(fx.match_name, |t| sspc(fx, t)) +} + /// Imports `synthetic.aep` with `fx` in place of its Glow: the EffectCraft effect `effect` (if /// imported) and the result. pub fn run(fx: &Fx, effect: &str) -> (Option, ImportResult) { - let bytes = bytes_with(fx); + import_bytes(&bytes_with(fx), effect) +} + +/// Imports `bytes`: the first EffectCraft effect `effect` (if imported) and the result. +pub fn import_bytes(bytes: &[u8], effect: &str) -> (Option, ImportResult) { let mut project = Project::default(); let none = |_: &str| false; - let r = import(&mut project, &bytes, &ImportOptions { allow_losses: true, ..ImportOptions::new(&none) }).unwrap(); + let r = import(&mut project, bytes, &ImportOptions { allow_losses: true, ..ImportOptions::new(&none) }).unwrap(); let g = project .comps() .flat_map(|(_, c)| c.layers.iter()) diff --git a/crates/aep/tests/it/import/puppet.rs b/crates/aep/tests/it/import/puppet.rs new file mode 100644 index 000000000..a3054286e --- /dev/null +++ b/crates/aep/tests/it/import/puppet.rs @@ -0,0 +1,171 @@ +//! After Effects' Puppet (`ADBE FreePin3`, #3093) arrives as EffectCraft's Puppet +//! (`ec.distort.puppet`) in its place in the stack, and what its meshes and pins hold is reported +//! mesh by mesh instead of the one line "has the effect 'Puppet'". The effect is built into the +//! committed `synthetic.aep` in place of its Glow (`fx_inject`, in memory), in the tree +//! docs/aep-format.md 3.7 gives for Puppet. That tree comes from an external scripting listing, +//! not a byte sample: these tests prove the reader's walk and its report, not the format. + +use effectcraft_aep::Chunk; +use effectcraft_aep::import::{ImportOptions, LossCategory, import}; +use effectcraft_project::Project; + +use crate::import::fx_inject::{bytes_with_effect, import_bytes, keyed_record, losses, nested, record, tdmn, utf8}; + +const SYNTHETIC: &[u8] = include_bytes!("../../fixtures/synthetic.aep"); +const PUPPET: &str = "ADBE FreePin3"; +const POS_ATOM: &str = "ADBE FreePin3 PosPin Atom"; + +fn tdsb(v: u32) -> Chunk<'static> { + Chunk::data(*b"tdsb", v.to_be_bytes().to_vec()) +} + +/// A property group of `entries` (match name, node). +fn group(entries: Vec<(&str, Chunk<'static>)>) -> Chunk<'static> { + let mut kids = vec![tdsb(1)]; + for (name, node) in entries { + kids.push(tdmn(name)); + kids.push(node); + } + Chunk::list(*b"tdgp", kids) +} + +/// A pin atom with its hidden vertex records and Position (keyed when `keys`). The values are +/// one-component records: the reader only asks whether a pin is keyed. +fn pin(t: &Chunk<'_>, atom: &'static str, keys: bool) -> (&'static str, Chunk<'static>) { + let position = if keys { keyed_record(t, &[(0, 1.0), (25_600, 2.0)]) } else { record(t, 1.0) }; + ( + atom, + group(vec![ + ("ADBE FreePin3 PosPin Vtx Offset", record(t, 0.0)), + ("ADBE FreePin3 PosPin Vtx Index", record(t, 7.0)), + ("ADBE FreePin3 PosPin Position", position), + ]), + ) +} + +/// `Mesh 1`: Triangles 500, Expansion 5, four Deform pins (one keyed, one with an atom name +/// nobody knows) and a property among them, no Overlap pin, one Stiffness pin, and a group +/// nobody knows. +fn full_mesh(t: &Chunk<'_>) -> Chunk<'static> { + let mesh_data = + Chunk::list(*b"tdbs", vec![tdsb(1), Chunk::data(*b"cdat", vec![0u8; 4]), Chunk::list(*b"aRbs", vec![Chunk::data(*b"aRbp", vec![1, 2, 3, 4])])]); + let deform = group(vec![ + pin(t, POS_ATOM, false), + pin(t, POS_ATOM, true), + pin(t, POS_ATOM, false), + pin(t, "ADBE FreePin3 Test Atom", false), + ("ADBE FreePin3 Test Prop", record(t, 3.0)), + ]); + let starch = group(vec![("ADBE FreePin3 StarchPin Atom", group(vec![("ADBE FreePin3 StarchPin Amount", record(t, 0.5))]))]); + group(vec![ + ("ADBE FreePin3 Mesh", mesh_data), + ("ADBE FreePin3 Mesh Tri Count", record(t, 500.0)), + ("ADBE FreePin3 Mesh Expansion", record(t, 5.0)), + ("ADBE FreePin3 PosPins", deform), + ("ADBE FreePin3 HghtPins", group(vec![])), + ("ADBE FreePin3 StarchPins", starch), + ("ADBE FreePin3 Test Group", group(vec![])), + ]) +} + +/// The Puppet instance: `fx_switch` is the value group's `tdsb`, `arap` its mesh tree (`None`: +/// none), `extra` more entries of its value group. +fn sspc(fx_switch: u32, arap: Option>, extra: Vec<(&str, Chunk<'static>)>) -> Chunk<'static> { + let pard = |kind: u8| { + let mut b = vec![0u8; 148]; + b[15] = kind; + Chunk::data(*b"pard", b) + }; + let part = vec![Chunk::data(*b"parn", 2u32.to_be_bytes().to_vec()), tdmn("ADBE FreePin3-0000"), pard(0), tdmn("ADBE Effect Built In Params"), pard(9)]; + let mut values = vec![tdsb(fx_switch)]; + if let Some(arap) = arap { + values.push(tdmn("ADBE FreePin3 ARAP Group")); + values.push(arap); + } + for (name, node) in extra { + values.push(tdmn(name)); + values.push(node); + } + Chunk::list(*b"sspc", vec![nested(*b"fnam", vec![utf8("Puppet")]), Chunk::list(*b"parT", part), Chunk::list(*b"tdgp", values)]) +} + +/// The layer that holds `effect`, the effect's place in that layer's stack and the stack's size. +fn stack_place(bytes: &[u8], effect: &str) -> Option<(String, usize, usize)> { + let mut project = Project::default(); + let none = |_: &str| false; + import(&mut project, bytes, &ImportOptions { allow_losses: true, ..ImportOptions::new(&none) }).unwrap(); + project.comps().flat_map(|(_, c)| c.layers.iter()).find_map(|l| { + let fx: Vec<_> = l.props.sub("effects")?.children.iter().filter_map(|n| n.as_group()).collect(); + let at = fx.iter().position(|g| g.match_id == effect)?; + Some((l.name.clone(), at, fx.len())) + }) +} + +#[test] +fn puppet_arrives_and_its_pins_are_reported_mesh_by_mesh() { + let bytes = bytes_with_effect(PUPPET, |t| { + let arap = group(vec![("ADBE FreePin3 Outlines", group(vec![])), ("ADBE FreePin3 Mesh Group", group(vec![("ADBE FreePin3 Mesh Atom", full_mesh(t))]))]); + sspc(1, Some(arap), vec![("ADBE FreePin3 On Transparent", record(t, 1.0))]) + }); + let (g, r) = import_bytes(&bytes, effectcraft_effects::puppet::ID); + let g = g.expect("an EffectCraft Puppet in the Glow's place"); + assert!(g.enabled); + assert_eq!(g.name, "Puppet"); + // No pin is made up: the rest places are not decoded. + assert_eq!(effectcraft_effects::puppet::pin_count(&g), 0); + // In the Glow's place in its layer's stack (the second of two effects there). + assert!(stack_place(SYNTHETIC, "ec.stylize.glow").is_some_and(|(_, at, n)| at > 0 && n > 1)); + assert_eq!(stack_place(&bytes, effectcraft_effects::puppet::ID), stack_place(SYNTHETIC, "ec.stylize.glow")); + let l = losses(&r, LossCategory::Effects, PUPPET); + assert_eq!(l.len(), 2, "{l:#?}"); + assert!(!l.iter().any(|d| d.contains("has the effect")), "not the old one-line report: {l:#?}"); + assert!(l.iter().any(|d| d.ends_with("property ADBE FreePin3 On Transparent (1) not imported")), "{l:#?}"); + let mesh = l.iter().find(|d| d.contains("imported without its meshes and pins")).expect("the mesh report"); + assert!( + mesh.ends_with( + ": Mesh 1 (Triangles 500, Expansion 5): Deform 4 pins (1 keyed) [ADBE FreePin3 PosPin Atom, ADBE FreePin3 Test Atom], \ + Overlap no pins, Stiffness 1 pin (0 keyed) [ADBE FreePin3 StarchPin Atom]; not recognised: ADBE FreePin3 Test Prop, ADBE FreePin3 Test Group" + ), + "{mesh}" + ); + assert_eq!(losses(&r, LossCategory::DamagedData, PUPPET), Vec::::new()); +} + +#[test] +fn many_meshes_are_listed_up_to_ten_and_names_once() { + let bytes = bytes_with_effect(PUPPET, |_| { + let mesh = || group(vec![("ADBE FreePin3 Test Group", group(vec![]))]); + let meshes = (0..12).map(|_| ("ADBE FreePin3 Mesh Atom", mesh())).collect(); + sspc(1, Some(group(vec![("ADBE FreePin3 Mesh Group", group(meshes))])), vec![]) + }); + let (_, r) = import_bytes(&bytes, effectcraft_effects::puppet::ID); + let l = losses(&r, LossCategory::Effects, PUPPET); + assert_eq!(l.len(), 1, "{l:#?}"); + assert!(l[0].ends_with("Mesh 9: no pin groups; Mesh 10: no pin groups; and 2 more; not recognised: ADBE FreePin3 Test Group"), "{}", l[0]); +} + +#[test] +fn a_puppet_without_a_mesh_loses_nothing_and_keeps_its_fx_switch() { + let bytes = bytes_with_effect(PUPPET, |_| sspc(0, Some(group(vec![("ADBE FreePin3 Mesh Group", group(vec![]))])), vec![])); + let (g, r) = import_bytes(&bytes, effectcraft_effects::puppet::ID); + let g = g.expect("an EffectCraft Puppet"); + assert!(!g.enabled, "switched off in the Effect Controls"); + assert_eq!(losses(&r, LossCategory::Effects, PUPPET), Vec::::new()); +} + +#[test] +fn a_puppet_whose_mesh_tree_is_not_found_is_reported() { + let bytes = bytes_with_effect(PUPPET, |t| sspc(1, None, vec![("ADBE FreePin3 Other Group", group(vec![("x", record(t, 1.0))]))])); + let (g, r) = import_bytes(&bytes, effectcraft_effects::puppet::ID); + assert!(g.is_some(), "the effect still arrives"); + let l = losses(&r, LossCategory::Effects, PUPPET); + assert_eq!(l.len(), 1, "{l:#?}"); + assert!( + l[0].ends_with( + "imported without meshes or pins: its mesh group (ADBE FreePin3 ARAP Group / ADBE FreePin3 Mesh Group) was not found or is not a group; \ + not recognised: ADBE FreePin3 Other Group" + ), + "{}", + l[0] + ); +} diff --git a/docs/aep-format.md b/docs/aep-format.md index d6ec7fb14..db0d79d1c 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -1,6 +1,6 @@ # After Effects project format (`.aep`) -> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-05 · **Change:** versions: `svap` is the version that wrote the file, `head` bytes 4 to 7 the version it is written for, and what a Save a Copy As 25.x / 24.x of After Effects 2026 changes (section 1, "Versions"); before that: the stretch numerator is signed (a negative one reverses the layer, 981-983); layer styles (3.12b, 950-980); before that: footage: `sspc@118` of an image sequence is its folder's modification time (860, read-back x1-x3); before that: text: paragraph `/24`, `/35` and style `/68` are neutral for 0 and 1 (732-734, edited in place); before that: keyframes: the ease of mask path keys (599, 700), the spatial speed unit of fractional values (597, 598, 730); before that: text: character and paragraph settings, manual kerning, vertical text, `/nil` is neutral (S17: 538-578, 595, 596); before that: cameras, lights and 3D rotations (S16: the auto Position of a two-node camera is where After Effects renders it, 537; camera and light defaults read back 255, 256; Orientation `otst/otda` 488), Timewarp (529-533), an expression on an effect parameter (534); before that: text: style slot /13 is superscript / subscript (526-528; every scripted text of the corpus is superscript), wrapping box text 517; before that: effects of batch 5 (409-450, 511-516: integer sliders, checkbox defaults in `pard`, arbitrary data of Hue/Saturation and Levels, the parameters of Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow; section 3.7); before that: shape items of batch 5 (451-484, 519: ellipse, fill and stroke opacity, fill rule, group transform, shape layer anchor, trim paths, rectangle roundness, polystar fields, gradient default, nested groups, the defaults of every item; section 3.9); before that: track mattes: mode at `ldta` byte 107 and the matte layer id at byte 160 (cases 402-408, section 3.1); before that: text: automatic leading is verified (400); Exposure (`ADBE Exposure2`, cases 389-397): group start / end parameter types 13 / 14, float slider valid minimum and slider range (section 3.7); before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: the XML form `.aepx` is read into the same chunk tree (section 1, "The XML form"); the `head` save counter is a `u32` at byte 16; text documents for the importer (stroke colour, automatic leading, the font of each run, the undecoded-slot check, the text-layer Anchor Point, the Source Text record); before that: third round of verified field layouts (explicit-default semantics, type-major bezier ease, exact time stretch and in / out points, Time Remapping state, comp pixel aspect / shutter phase / display start, Point default, Layer Control None, dropdown selection, one `mkif` per mask, polystar and radial gradient records, text styles, camera and light options, marker cue data, audio and image-sequence footage) added to the typed view of `effectcraft-aep` · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-07 · **Change:** Puppet (`ADBE FreePin3`, 3.7): the property tree from an external scripting listing, **?** until a byte sample exists (#3093); before that: versions: `svap` is the version that wrote the file, `head` bytes 4 to 7 the version it is written for, and what a Save a Copy As 25.x / 24.x of After Effects 2026 changes (section 1, "Versions"); before that: the stretch numerator is signed (a negative one reverses the layer, 981-983); layer styles (3.12b, 950-980); before that: footage: `sspc@118` of an image sequence is its folder's modification time (860, read-back x1-x3); before that: text: paragraph `/24`, `/35` and style `/68` are neutral for 0 and 1 (732-734, edited in place); before that: keyframes: the ease of mask path keys (599, 700), the spatial speed unit of fractional values (597, 598, 730); before that: text: character and paragraph settings, manual kerning, vertical text, `/nil` is neutral (S17: 538-578, 595, 596); before that: cameras, lights and 3D rotations (S16: the auto Position of a two-node camera is where After Effects renders it, 537; camera and light defaults read back 255, 256; Orientation `otst/otda` 488), Timewarp (529-533), an expression on an effect parameter (534); before that: text: style slot /13 is superscript / subscript (526-528; every scripted text of the corpus is superscript), wrapping box text 517; before that: effects of batch 5 (409-450, 511-516: integer sliders, checkbox defaults in `pard`, arbitrary data of Hue/Saturation and Levels, the parameters of Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow; section 3.7); before that: shape items of batch 5 (451-484, 519: ellipse, fill and stroke opacity, fill rule, group transform, shape layer anchor, trim paths, rectangle roundness, polystar fields, gradient default, nested groups, the defaults of every item; section 3.9); before that: track mattes: mode at `ldta` byte 107 and the matte layer id at byte 160 (cases 402-408, section 3.1); before that: text: automatic leading is verified (400); Exposure (`ADBE Exposure2`, cases 389-397): group start / end parameter types 13 / 14, float slider valid minimum and slider range (section 3.7); before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: the XML form `.aepx` is read into the same chunk tree (section 1, "The XML form"); the `head` save counter is a `u32` at byte 16; text documents for the importer (stroke colour, automatic leading, the font of each run, the undecoded-slot check, the text-layer Anchor Point, the Source Text record); before that: third round of verified field layouts (explicit-default semantics, type-major bezier ease, exact time stretch and in / out points, Time Remapping state, comp pixel aspect / shutter phase / display start, Point default, Layer Control None, dropdown selection, one `mkif` per mask, polystar and radial gradient records, text styles, camera and light options, marker cue data, audio and image-sequence footage) added to the typed view of `effectcraft-aep` · **Target:** Adobe After Effects 2026 What `crates/aep` ([`effectcraft-aep`](../crates/aep/README.md)) reads and writes, and the evidence for each statement. Everything here was observed on project files we generated ourselves; no @@ -610,6 +610,52 @@ presets, and After Effects draws exactly the picked objects as selected (`crypto `crypto-07`: box and disc, the two objects of `mat_paint`). API: `CryptomatteData` (refuses data without `cry1`, a length past the end or a non-zero byte at 112). +**Puppet (`ADBE FreePin3`; ?: external listing, not the corpus, #3093).** No file of the +corpus, the committed fixtures or the samples holds a Puppet. The tree below comes from outside +the corpus: a public scripting listing of the effect's properties (a forum post, an After Effects +release from before the Advanced and Bend pins and Density). It is not bytes we have seen; that +After Effects writes it by the general rule (a `tdmn` and the `LIST tdgp` or `tdbs` it names, +nested in the instance's value group `sspc/tdgp`, as masks, shapes and layer styles are) is +**H**: + +```text +Puppet ADBE FreePin3 + On Transparent ADBE FreePin3 On Transparent + arap ADBE FreePin3 ARAP Group (hidden) + Auto-traced Shapes ADBE FreePin3 Outlines + Mesh ADBE FreePin3 Mesh Group (hidden) + Mesh 1 ADBE FreePin3 Mesh Atom + Mesh ADBE FreePin3 Mesh (hidden: the triangulation) + Triangles ADBE FreePin3 Mesh Tri Count + Expansion ADBE FreePin3 Mesh Expansion + Deform ADBE FreePin3 PosPins + Puppet Pin 1 ADBE FreePin3 PosPin Atom + Vertex Offset ADBE FreePin3 PosPin Vtx Offset (hidden, 2 values) + Vertex Index ADBE FreePin3 PosPin Vtx Index (hidden; 4294967295 = none) + Position ADBE FreePin3 PosPin Position + Overlap ADBE FreePin3 HghtPins (hidden while empty) + Stiffness ADBE FreePin3 StarchPins (hidden while empty) +``` + +A pin's rest place is not a property: it is a vertex of the hidden mesh (Vertex Index) plus an +offset, so it cannot be read without decoding the mesh data. The unit of Position (layer pixels, +or a fraction of the layer as effect points are) is **?**. The match names of the Overlap and +Starch pin atoms and their properties, of the Advanced and Bend pins (Scale, Rotation) and of +Density, Mesh Rotation Refinement and the Puppet Engine are **?**, and so is which of them are +`parT` parameters; whether After Effects stores some `parT` parameters of a fresh Puppet even at +their defaults (the importer would then report them every time) is **?**; whether a Puppet +without pins has an (empty) Mesh Group, or no ARAP group at all, is **?**. The importer +(`import/puppet.rs`) therefore brings the effect in as EffectCraft's Puppet without meshes or +pins and reports, per mesh, Triangles, Expansion and per pin group the pins, how many are keyed +or carry an expression, and their atom match names; every entry outside this tree is named in the +report. The sample that settles it: a fresh Puppet with no pins in one composition and, in +another, a 400 x 800 solid in a larger composition (pixels against fractions) with a Puppet of +three Position pins (one keyed, one moved off its rest place without +keys), one Advanced, one Bend, one Starch and one Overlap pin, Triangles, Expansion and Density +off their defaults, a second mesh, and a scripting read-back of every property's match name and +value, the hidden ones included. Pins added by script are reported to come out disabled, so the +pins are placed by hand or by a preset. + ### 3.8 Masks and bezier paths (`tdgp[ADBE Mask Parade]`) | Field | Chunk path | Offset | Type | Meaning | Evidence | Conf. | @@ -894,6 +940,7 @@ another value). **Effects** - `pard` byte 48 (flags: **?**), 68 / 72 (the point default as a percent pair: **V** in the corpus for 074 and 368, not exposed), 120 (the float slider default again as f32: **V**, not exposed), 124 (display decimals: **H**), 132..147 (fixed slider slider-max, default copy and flags; the low bit of @144 is set on the two percent-displayed sliders and clear on Scatter: **H** "display as percent"), the colour default copy at @60, and the stale memory after the parameter name. The fixed slider valid minimum is not decoded (the float slider one is, 389). +- Puppet (`ADBE FreePin3`, 3.7): the hidden mesh data, a pin's Vertex Index and Vertex Offset (its rest place), the unit of a pin's Position, the Overlap, Starch, Advanced and Bend pins, Density, Mesh Rotation Refinement and the Puppet Engine: no sample (**?**). - Effects renamed, or stacked twice on ONE layer (`Gradient Ramp 2`: does the second keep an empty `parT`?); which instance keeps the full `parT` when a second instance is created on a layer below the first; expressions on effect parameters; keyed per-channel Exposure values and a keyed checkbox; Curves / Levels (arrays), Fractal Noise, and whether the `tdpi` of a Layer Control holds an id or an index after the referenced layer is reordered or deleted. **Masks, shapes** diff --git a/docs/aep-import.md b/docs/aep-import.md index ed3ab57f5..5d268e503 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -1,6 +1,6 @@ # After Effects project import -> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-05 · **Change:** the report names the After Effects version a file is written for and the one that wrote it (`aeVersion`); copies saved as 25.x and 24.x import like the 2026 save; before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-07 · **Change:** After Effects' Puppet arrives as EffectCraft's Puppet without its meshes and pins, which are reported mesh by mesh (#3093; the storage is not sampled yet, aep-format 3.7); before that: the report names the After Effects version a file is written for and the one that wrote it (`aeVersion`); copies saved as 25.x and 24.x import like the 2026 save; before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 File ▸ Import ▸ After Effects Project… (`file.importAep`) reads an `.aep` or its XML form `.aepx` and adds its items to the open project, in a folder named after the file (as After Effects does when it imports a project). @@ -58,7 +58,7 @@ outside the sampled ones keeps the current value and is reported as `project set | Text | the Source Text of a text layer: the text (paragraphs separated by CR; a hard line break is a CR too; U+0003 is laid out by After Effects exactly like a CR (386 against 387) and becomes a CR), per style run the font (PostScript name), size, fill and stroke colour, whether fill and stroke are drawn, stroke width, stroke over fill (one switch for the text), tracking, leading (automatic or explicit), faux bold, faux italic, all caps, small caps, superscript and subscript (526-528), horizontal and vertical scale, baseline shift, automatic kerning (metrics, optical, none), ligatures, tsume (538-578); per paragraph the justification (left, centre, right and the four justified variants), first-line / start / end indent, space before / after, direction, composer, hanging punctuation (in a right-to-left paragraph the stored justification is relative to the direction: After Effects lays 0 and 3 out flush right and 1 and 4 flush left, read-back test t7, so they arrive as right / left); point text or box text (box size and top-left in layer pixels); vertical right-to-left text (575) | | Text animators | every animator with its name and its range selectors (Start, End, Offset, or the Index range when Units is Index; Units, Based On, Mode, Amount, Shape, Smoothness, Ease High / Low, Randomize Order, Random Seed) and the properties Anchor Point, Position, Scale, Skew, Skew Axis, Rotation, Opacity, Fill Color, Stroke Color, Stroke Width, Tracking Amount, Line Spacing, Blur, Character Offset and Character Range, with keys and expressions (579-594, 114, 728). Values are stored as After Effects shows them; popups are 1-based in the file | | Masks | every mask of an imported layer, in stack order (on text and shape layers, which have no source, the stored mask box is in layer pixels: 705, 706): name, path (vertices and tangents in layer pixels, open or closed), mode (None, Add, Subtract, Intersect, Lighten, Darken, Difference), inverted, feather (x, y in pixels), opacity (percent), expansion (pixels); path keyframes (time, linear or hold sides, roving, label), feather / opacity / expansion keyframes with ease | -| Effects | **Curves** (961-964: the five curves' points from the arbitrary data as EffectCraft's `x,y` points in 0..1; render within 6 levels: EffectCraft interpolates monotone, After Effects stores a natural spline) and **Mesh Warp** (965, 985: Rows, Columns, Quality and the Distortion Mesh as per-vertex offsets in layer pixels; Bezier handles moved on their own are kept as eight extra numbers of the vertex for the export and reported, EffectCraft draws the mesh bilinear; the header is rows + 1, columns + 1; render 985: 1.4 % of pixels > 2 levels around the moved vertex), **Tint**, **Gradient Ramp**, **Exposure**, **Invert**, **Fast Box Blur**, **Gaussian Blur**, **Drop Shadow**, **Glow**, **Fill**, **Hue/Saturation**, **Levels**, **Timewarp** (Method, Adjust Time By, Speed with keys, Source Frame; 529-533), **Directional Blur**, **Radial Blur**, **CC Radial Blur**, **Venetian Blinds**, **Channel Blur**, **Black & White**, **Tritone**, **Luma Key**, **Simple Choker**, **Matte Choker**, **Transform** (all parameters; 711-723) and the expression controls **Slider**, **Angle**, **Checkbox**, **Color** and **Point Control** (table below) on any imported layer (also text and shape layers and layers with masks), in stack order, with the instance name, every parameter (static value or default) and its keyframes; also the second and later instances of a type, whose parameter definitions are only in the project's shared definition list. Other effects are reported (below) | +| Effects | **Curves** (961-964: the five curves' points from the arbitrary data as EffectCraft's `x,y` points in 0..1; render within 6 levels: EffectCraft interpolates monotone, After Effects stores a natural spline) and **Mesh Warp** (965, 985: Rows, Columns, Quality and the Distortion Mesh as per-vertex offsets in layer pixels; Bezier handles moved on their own are kept as eight extra numbers of the vertex for the export and reported, EffectCraft draws the mesh bilinear; the header is rows + 1, columns + 1; render 985: 1.4 % of pixels > 2 levels around the moved vertex), **Tint**, **Gradient Ramp**, **Exposure**, **Invert**, **Fast Box Blur**, **Gaussian Blur**, **Drop Shadow**, **Glow**, **Fill**, **Hue/Saturation**, **Levels**, **Timewarp** (Method, Adjust Time By, Speed with keys, Source Frame; 529-533), **Directional Blur**, **Radial Blur**, **CC Radial Blur**, **Venetian Blinds**, **Channel Blur**, **Black & White**, **Tritone**, **Luma Key**, **Simple Choker**, **Matte Choker**, **Transform** (all parameters; 711-723) and the expression controls **Slider**, **Angle**, **Checkbox**, **Color** and **Point Control** (table below) on any imported layer (also text and shape layers and layers with masks), in stack order, with the instance name, every parameter (static value or default) and its keyframes; also the second and later instances of a type, whose parameter definitions are only in the project's shared definition list. **Puppet** arrives in its place with its name and fx switch but without meshes and pins (a pin's rest place lies in the mesh data, not decoded yet); per mesh the report gives Triangles, Expansion and each pin group's pins, keyed pins and atom match names. Other effects are reported (below) | | Layer styles | Drop Shadow, Inner Shadow, Outer Glow, Inner Glow, Bevel and Emboss, Satin, Color Overlay, Gradient Overlay and Stroke when applied (switch group `tdsb` 1): every parameter, starting from After Effects' defaults (Use Global Light is off there), with keys on scalars and colours and expressions; Blending Options: Fill Opacity, the channel switches, Blend Interior Styles as Group, Use Blend Ranges, and Global Light Angle / Altitude, which the composition adopts (one setting per composition here). Units and popups through `effectcraft_aep::style_map` (aep-format 3.12b). Render check against After Effects (`proof\styles`): drop shadow, inner shadow, satin, colour overlay, stroke within a few levels; glow and bevel intensities and the Gradient Overlay direction differ in EffectCraft's renderer | | Shape layers | groups (nested, with anchor, position, scale, skew, skew axis, rotation, opacity), rectangle (size, position, roundness), ellipse (size, position), polystar (star or polygon, points, position, rotation, radii, roundness), bezier path (vertices and tangents in layer pixels, path keys), fill (colour, opacity, fill rule), stroke (colour, opacity, width, cap, join, miter limit), gradient fill and stroke (linear or radial, start and end point; the default white-to-black colours), trim paths (start, end, offset, trim multiple shapes), round corners (radius), merge paths (mode; 707, 708), offset paths (amount, line join, miter limit, copies, copy offset; 710; After Effects offsets copy *i* by Amount x (*i* + Copy Offset), default 1, EffectCraft by Amount x (*i* + 1 + copyOffset), so the copy offset arrives 1 less, also its keys; an expression on it is kept and reported; 826-828, 828 renders equal to After Effects on every grid point), repeater (copies, offset, composite, transform; 709), in file order, with keyframes and expressions, the operators' defaults as After Effects reads them back (731); the layer anchor in pixels; 238-242, 330-333, 451-484, 519, 705-710, 731 | | Markers | layer and composition markers: time (layer time for layer markers, exact frames), duration (1/600 s units, 509, 510), comment, chapter, URL, frame target, label, cue point (name, event or navigation, parameters); 122, 123, 249, 347-349, 509, 510 | From 5f73e5b5e935aae74587d18f20034f93a85e811e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:03:59 +0100 Subject: [PATCH 044/350] =?UTF-8?q?Time=20stretch:=20hold=20a=20loaded=20o?= =?UTF-8?q?r=20imported=20stretch=20to=20=C2=B11=20%=20to=20=C2=B19900=20%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A project file, an .aep, a timeline document or a Lottie file could give a layer a time stretch of 1e-300 %, 0 or 1e300 %. Layer and comp time divide by it and the audio mix read about usize::MAX source samples for one block, which overflowed or tried a huge allocation and aborted the app. - limits::stretch brings a stretch within After Effects' range (±1 % to ±9900 %, Layer.stretch in the scripting guide), sign kept; 0 and NaN are 100 %. Opening a project file corrects each layer and says so; a stretch written as null (NaN in an auto-save) now opens instead of failing. - .aep import and Lottie import clamp and report the stretch; timeline import clamps the clip speed and warns. - layer.timeStretch refuses a non-number and more than 9900 %; the Time Stretch dialog's ranges match. - Layer::layer_time / comp_time and the audio mix use the clamped stretch, so a value set in memory can't divide by zero or read without bound. Fixes #3247 --- crates/aep/src/import/build.rs | 9 +- crates/aep/tests/it/import/synthetic.rs | 41 ++++++++ crates/engine/src/cmd/layer_time.rs | 7 ++ crates/engine/src/tests/limits.rs | 18 ++++ crates/interchange/src/import.rs | 24 ++++- crates/interchange/src/tests.rs | 18 ++++ crates/lottie/src/import.rs | 7 +- crates/lottie/src/tests.rs | 15 +++ crates/project/src/layer.rs | 28 +++++- crates/project/src/limits.rs | 113 +++++++++++++++++++++- crates/render/src/audio.rs | 7 +- crates/render/src/tests.rs | 1 + crates/render/src/tests/stretch_limits.rs | 96 ++++++++++++++++++ crates/ui-egui/src/panels/key_dialogs.rs | 16 ++- 14 files changed, 389 insertions(+), 11 deletions(-) create mode 100644 crates/render/src/tests/stretch_limits.rs diff --git a/crates/aep/src/import/build.rs b/crates/aep/src/import/build.rs index 4ec346328..ded1e64ad 100644 --- a/crates/aep/src/import/build.rs +++ b/crates/aep/src/import/build.rs @@ -681,7 +681,14 @@ fn apply_timing(l: &mut Layer, h: &LayerHeader, tm: &TimeMap, name: &str, col: & col.lose(LossCategory::DamagedData, format!("layer '{name}': the time stretch is zero or unreadable, 100 % is used")); Rational { num: 1, den: 1 } }); - l.stretch = stretch.to_f64().map_or(100.0, |r| r * 100.0); + let stored = stretch.to_f64().map_or(100.0, |r| r * 100.0); + // A ratio such as 1/2^31 (5e-8 %) or 2^31/1 is outside what After Effects allows (±1 % to + // ±9900 %); taken as stored, the layer and audio time would divide by ~0 (#3247). The in and + // out points keep the comp times the file stores. + l.stretch = effectcraft_project::limits::stretch(stored); + if l.stretch.to_bits() != stored.to_bits() { + col.lose(LossCategory::DamagedData, format!("layer '{name}': the time stretch of {stored:e} % is outside ±1 % to ±9900 %, {} % is used", l.stretch)); + } // A stored time with a zero denominator (or a sum that overflows) has no value; it is read as 0 // and reported, so the layer's timing never changes silently. let unreadable: Vec<&str> = [("start", h.start), ("in point", h.in_relative), ("out point", h.out_relative)] diff --git a/crates/aep/tests/it/import/synthetic.rs b/crates/aep/tests/it/import/synthetic.rs index 18709f9e5..e2fb20da4 100644 --- a/crates/aep/tests/it/import/synthetic.rs +++ b/crates/aep/tests/it/import/synthetic.rs @@ -273,6 +273,47 @@ fn layer_times_with_a_zero_denominator_are_reported() { assert!(losses(&r.unwrap(), LossCategory::DamagedData).is_empty()); } +/// The time stretch of every imported layer. +fn stretches(p: &Project) -> Vec { + p.items.values().filter_map(|i| if let ItemKind::Comp(c) = &i.kind { Some(c) } else { None }).flat_map(|c| c.layers.iter().map(|l| l.stretch)).collect() +} + +#[test] +fn a_time_stretch_outside_one_to_9900_percent_is_clamped_and_reported() { + // ldta byte 8 is the stretch numerator (i32), byte 108 the denominator (u32). 1/(2^31 - 1) is + // about 5e-8 %: imported as stored, the layer and audio time divided by ~0 (#3247). + for (num, den, want) in [(1i32, i32::MAX as u32, 1.0), (-1, i32::MAX as u32, -1.0), (i32::MAX, 1, 9900.0), (i32::MIN, 1, -9900.0)] { + let bytes = mutate(&mut |c| { + if c.id == *b"ldta" { + set_data(c, 8, &num.to_be_bytes()); + set_data(c, 108, &den.to_be_bytes()); + } + }); + let (p, r) = run(&bytes, true); + let r = r.unwrap(); + let found = losses(&r, LossCategory::DamagedData); + assert!(found.iter().any(|d| d.contains("time stretch") && d.contains("outside")), "{num}/{den}: {found:?}"); + let s = stretches(&p); + assert!(!s.is_empty() && s.iter().all(|v| *v == want), "{num}/{den}: {s:?}"); + let (p, r) = run(&bytes, false); + assert!(r.is_err(), "{num}/{den}: strict import must refuse"); + assert!(p.items.is_empty()); + } + // A small stretch within the range (1/64: 1.5625 %) and -9900 % itself are no damage. + for (num, den, want) in [(1i32, 64u32, 1.5625), (-99, 1, -9900.0)] { + let bytes = mutate(&mut |c| { + if c.id == *b"ldta" { + set_data(c, 8, &num.to_be_bytes()); + set_data(c, 108, &den.to_be_bytes()); + } + }); + let (p, r) = run(&bytes, true); + let r = r.unwrap(); + assert!(!losses(&r, LossCategory::DamagedData).iter().any(|d| d.contains("time stretch")), "{num}/{den}: {:?}", r.report.losses); + assert!(stretches(&p).iter().all(|v| *v == want), "{num}/{den}: {:?}", stretches(&p)); + } +} + /// The project with `nhed` bytes from `at` replaced. fn header(at: usize, bytes: &[u8]) -> Vec { mutate(&mut |c| { diff --git a/crates/engine/src/cmd/layer_time.rs b/crates/engine/src/cmd/layer_time.rs index 4ecf725ec..a6fe3d990 100644 --- a/crates/engine/src/cmd/layer_time.rs +++ b/crates/engine/src/cmd/layer_time.rs @@ -59,9 +59,16 @@ fn time_stretch(s: &mut Session, p: &Value) -> Result { (None, Some(d)) if span > 0.0 => old * d / span, _ => old, }; + if !new.is_finite() { + return Err(bad("layer.timeStretch", "the stretch factor must be a number")); + } if new.abs() < 1.0 { return Err(bad("layer.timeStretch", "the stretch factor must be at least 1 %")); } + // After Effects' largest stretch; a project file holds no more either (#3247). + if new.abs() > effectcraft_project::limits::MAX_STRETCH { + return Err(bad("layer.timeStretch", "the stretch factor must be at most 9900 %")); + } // The hold point keeps showing the same layer time. let h = match hold { "current" => cti, diff --git a/crates/engine/src/tests/limits.rs b/crates/engine/src/tests/limits.rs index 524719bbf..9c156c4f7 100644 --- a/crates/engine/src/tests/limits.rs +++ b/crates/engine/src/tests/limits.rs @@ -103,6 +103,24 @@ fn commands_cap_sizes_at_30000_px() { assert_eq!((f.width, f.height, f.frame_rate), (limits::MAX_SIZE, 8, FrameRate::FPS_29_97)); } +/// Time Stretch takes After Effects' range, ±1 % to ±9900 %: before, 1e300 % was set, and a +/// project file holding it opened at 9900 % (#3247). +#[test] +fn time_stretch_refuses_more_than_9900_percent() { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Main", "width": 64, "height": 32, "frameRate": 30, "duration": 2})).unwrap(); + let layer = s.execute("layer.newSolid", json!({"color": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + let stretch = |s: &Session| s.active_comp().unwrap().layers.iter().find(|l| l.id.0 == layer).unwrap().stretch; + for pct in [1e300, -1e300, 9900.5] { + assert!(s.execute("layer.timeStretch", json!({"layers": [layer], "percent": pct})).is_err(), "{pct}"); + assert_eq!(stretch(&s), 100.0, "{pct}"); + } + for pct in [limits::MAX_STRETCH, -limits::MAX_STRETCH] { + s.execute("layer.timeStretch", json!({"layers": [layer], "percent": pct})).unwrap(); + assert_eq!(stretch(&s), pct); + } +} + #[test] fn a_project_with_a_precomp_cycle_opens_adds_and_renders_without_running_out_of_stack() { // Comps A and B nest each other (a damaged file); C is unrelated. Checking whether A diff --git a/crates/interchange/src/import.rs b/crates/interchange/src/import.rs index 9789ceb89..22e22b4f2 100644 --- a/crates/interchange/src/import.rs +++ b/crates/interchange/src/import.rs @@ -522,11 +522,31 @@ impl Importer<'_> { // ---- timing let start = tick_in(ti.start); let dur = tick_in(ti.duration); - let speed = if ti.speed.abs() < 1e-9 { 1.0 } else { ti.speed.abs() }; + // A zero, tiny (below 1e-9) or non-finite speed plays at 100 %. Otherwise the layer's + // stretch (100 / speed) is held to the range the app supports (±1 % to ±9900 %): a speed + // of 1e300 or 1e-300 from a damaged file made the layer and audio time divide by ~0 + // (#3247). The speed follows the stretch so the start time below matches it. + let raw = ti.speed.abs(); + let (speed, stretch) = if !raw.is_finite() || raw < 1e-9 { + // A speed of exactly 0 keeps its old silent 100 %; a tiny or non-finite one is named. + if !raw.is_finite() || raw > 0.0 { + self.warn(format!("clip \"{}\": a speed of {} is not supported (100 % used)", ti.name, ti.speed)); + } + (1.0, 100.0) + } else { + let stretch = effectcraft_project::limits::stretch(100.0 / raw); + if stretch.to_bits() == (100.0 / raw).to_bits() { + (raw, stretch) + } else { + let speed = 100.0 / stretch; + self.warn(format!("clip \"{}\": a speed of {:e} % is outside what a layer supports ({} % used)", ti.name, raw * 100.0, speed * 100.0)); + (speed, stretch) + } + }; let reverse = ti.reverse || ti.speed < 0.0; let src_in = tick_in(ti.source_in); let span = Tick((dur.0 as f64 * speed).round() as i64); - l.stretch = if reverse { -100.0 / speed } else { 100.0 / speed }; + l.stretch = if reverse { -stretch } else { stretch }; l.start_time = if reverse { start + Tick(((src_in + span).0 as f64 / speed).round() as i64) } else { start - Tick((src_in.0 as f64 / speed).round() as i64) }; l.in_point = start; diff --git a/crates/interchange/src/tests.rs b/crates/interchange/src/tests.rs index f4c9158f8..b451ecf81 100644 --- a/crates/interchange/src/tests.rs +++ b/crates/interchange/src/tests.rs @@ -314,6 +314,24 @@ fn imports_fcp7_xml_fcpxml_and_otio() { } } +/// A clip speed past what a layer's stretch supports (±1 % to ±9900 %) is held to it and named. +/// Before, a speed of 1e6 gave a stretch of 1e-4 % and the layer and audio time divided by ~0 +/// (#3247). +#[test] +fn an_extreme_clip_speed_is_held_to_the_stretch_range() { + for (speed, stretch) in [(1e6, 1.0), (1e-6, 9900.0)] { + let (mut doc, main) = edit(); + doc.sequence_mut(main).unwrap().video_tracks[1].items[0].speed = speed; + let (bytes, _) = fc::export(&doc, main, fc::Format::Otio, &Default::default()).unwrap(); + let (p, r) = import_doc(&bytes, "Main.otio", &mut probe); + let main_comp = r.comps.iter().copied().find(|c| p.item(*c).unwrap().name == "Main").expect("Main comp"); + let c = p.comp(main_comp).unwrap(); + let stretched: Vec = c.layers.iter().map(|l| l.stretch).filter(|s| (s - 100.0).abs() > 1e-9).collect(); + assert_eq!(stretched, [stretch], "speed {speed}"); + assert!(r.warnings.iter().any(|w| w.contains("speed") && w.contains("outside")), "speed {speed}: {:?}", r.warnings); + } +} + #[test] fn imports_aaf_and_omf() { let (doc, main) = edit(); diff --git a/crates/lottie/src/import.rs b/crates/lottie/src/import.rs index 53b1ba71c..57ef6427a 100644 --- a/crates/lottie/src/import.rs +++ b/crates/lottie/src/import.rs @@ -431,7 +431,12 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[ // Below 1 % (or 0) the layer time divides by ~0, and layer.timeStretch refuses it too. let sr = f(j, "sr").unwrap_or(1.0); l.stretch = if sr.is_finite() && sr.abs() >= 0.01 { - sr * 100.0 + // Past ±9900 % too (#3247): the largest stretch a layer supports, sign kept. + let pct = effectcraft_project::limits::stretch(sr * 100.0); + if pct.to_bits() != (sr * 100.0).to_bits() { + cx.im.warn(format!("{name}: a time stretch of {} % is not supported ({pct} % used)", sr * 100.0)); + } + pct } else { cx.im.warn(format!("{name}: a time stretch of {} % is not supported (100 % used)", sr * 100.0)); 100.0 diff --git a/crates/lottie/src/tests.rs b/crates/lottie/src/tests.rs index d26b3642d..414c875f0 100644 --- a/crates/lottie/src/tests.rs +++ b/crates/lottie/src/tests.rs @@ -520,6 +520,21 @@ fn zero_time_stretch_is_not_imported() { assert!(imp.warnings.iter().any(|w| w.contains("time stretch")), "{:?}", imp.warnings); } +#[test] +fn a_time_stretch_past_9900_percent_is_held_to_it() { + // `sr` 1e300 was imported as 1e302 % (#3247); the sign of a reversed layer is kept. + for (sr, want) in [(1e300, 9900.0), (-1e300, -9900.0), (99.0, 9900.0)] { + let doc = json!({"w": 20, "h": 20, "fr": 30, "ip": 0, "op": 30, "assets": [], + "layers": [{"ty": 1, "ind": 1, "sc": "#ff0000", "sw": 20, "sh": 20, "sr": sr, "ip": 0, "op": 30, "st": 0, "ks": {}}]}); + let mut q = Project::default(); + let imp = import(&mut q, doc.to_string().as_bytes(), "Imported", "", &mut |_| None).unwrap(); + let l = &q.comp(imp.comp).unwrap().layers[0]; + assert_eq!(l.stretch, want, "sr {sr}"); + let warned = imp.warnings.iter().any(|w| w.contains("time stretch")); + assert_eq!(warned, sr != 99.0, "sr {sr}: {:?}", imp.warnings); + } +} + #[test] fn embedded_glyphs_are_reported_when_dropped() { let glyph = |ch: &str, fam: &str| json!({"ch": ch, "size": 100, "style": "Regular", "w": 60, "fFamily": fam, "data": {"shapes": []}}); diff --git a/crates/project/src/layer.rs b/crates/project/src/layer.rs index 34eeb50c0..bc747bd88 100644 --- a/crates/project/src/layer.rs +++ b/crates/project/src/layer.rs @@ -470,6 +470,20 @@ pub enum AutoOrient { TowardsPointOfInterest, } +/// A stretch written as `null` (a NaN or infinite stretch: JSON holds neither, and auto-saves +/// write it so) reads as NaN, so the project still opens and loading brings it to 100 % with a +/// note ([`crate::limits::stretch`], #3247) instead of the whole file being refused. An infinite +/// stretch therefore also reopens at 100 %, not at ±9900 %. +fn stretch_or_nan<'de, D: serde::Deserializer<'de>>(d: D) -> Result { + Ok(Option::::deserialize(d)?.unwrap_or(f64::NAN)) +} + +/// `t × mul / div` in ticks, within [`Tick::MIN`]..=[`Tick::MAX`] (the cast saturates, NaN is 0), +/// so a layer time of an extreme stretch can't overflow later tick arithmetic. +fn scaled(t: Tick, mul: f64, div: f64) -> Tick { + Tick(((t.0 as f64 * mul / div) as i64).clamp(Tick::MIN.0, Tick::MAX.0)) +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub struct Layer { pub id: LayerId, @@ -484,6 +498,7 @@ pub struct Layer { pub in_point: Tick, pub out_point: Tick, /// Time stretch in percent (100 = normal, negative = reversed). + #[serde(deserialize_with = "stretch_or_nan")] pub stretch: f64, pub switches: Switches, pub blend_mode: BlendMode, @@ -555,15 +570,24 @@ impl Layer { t >= self.in_point && t < self.out_point } + /// The time stretch the layer plays at: [`Layer::stretch`] within + /// [`limits::stretch`](crate::limits::stretch) (a damaged value that got past loading, 0 or + /// NaN, never divides the layer's time by zero). + pub fn effective_stretch(&self) -> f64 { + crate::limits::stretch(self.stretch) + } + /// Layer time for a comp time (stretch-aware). pub fn layer_time(&self, comp_t: Tick) -> Tick { let d = comp_t - self.start_time; - if (self.stretch - 100.0).abs() < 1e-9 { d } else { Tick((d.0 as f64 * 100.0 / self.stretch) as i64) } + let s = self.effective_stretch(); + if (s - 100.0).abs() < 1e-9 { d } else { scaled(d, 100.0, s) } } /// Comp time for a layer time. pub fn comp_time(&self, layer_t: Tick) -> Tick { - if (self.stretch - 100.0).abs() < 1e-9 { self.start_time + layer_t } else { self.start_time + Tick((layer_t.0 as f64 * self.stretch / 100.0) as i64) } + let s = self.effective_stretch(); + if (s - 100.0).abs() < 1e-9 { self.start_time + layer_t } else { self.start_time + scaled(layer_t, s, 100.0) } } pub fn transform(&self) -> Option<&PropGroup> { diff --git a/crates/project/src/limits.rs b/crates/project/src/limits.rs index 998f40225..7a74c992b 100644 --- a/crates/project/src/limits.rs +++ b/crates/project/src/limits.rs @@ -27,6 +27,30 @@ pub const MAX_PIXEL_ASPECT: f64 = 10.0; pub const MOTION_BLUR_SAMPLES: (u32, u32) = (2, 64); /// Composition Settings ▸ Advanced ▸ Adaptive Sample Limit. pub const ADAPTIVE_SAMPLE_LIMIT: (u32, u32) = (16, 256); +/// The smallest time stretch, in percent, either way (After Effects sets a stretch between 0 and +/// 1 % to 1 %, between -1 % and 0 to -1 %; `layer.timeStretch` refuses less). +pub const MIN_STRETCH: f64 = 1.0; +/// The largest time stretch, in percent, either way (After Effects' `Layer.stretch` range is +/// -9900 to 9900). +pub const MAX_STRETCH: f64 = 9900.0; + +/// A layer's time stretch from [`MIN_STRETCH`] to [`MAX_STRETCH`] % either way, its sign (a +/// reversed layer) kept; 100 % for 0 or NaN. Layer and comp time divide by the stretch, and the +/// audio mix reads `100 / stretch` source samples per sample, so a stretch of 1e-300 % or NaN +/// from a damaged file must not reach them (#3247). +pub fn stretch(v: f64) -> f64 { + if v.is_nan() || v == 0.0 { + return 100.0; + } + // Infinity clamps to the largest stretch with its sign. + v.signum() * v.abs().clamp(MIN_STRETCH, MAX_STRETCH) +} + +/// A stretch for a note: a plain number in a readable range, scientific notation beyond it (a +/// plain 1e-300 would print 300 digits). +fn percent(v: f64) -> String { + if v == 0.0 || (1e-3..1e7).contains(&v.abs()) { format!("{v} %") } else { format!("{v:e} %") } +} /// A width or height from 1 to [`MAX_SIZE`]. pub fn size(v: u64) -> u32 { @@ -97,9 +121,11 @@ fn repair_comp(name: &str, c: &Comp, notes: &mut Vec) -> Option { let duration = c.duration.max(rate.frame_duration()); let samples = c.motion_blur_samples.clamp(MOTION_BLUR_SAMPLES.0, MOTION_BLUR_SAMPLES.1); let adaptive = c.motion_blur_adaptive_limit.clamp(ADAPTIVE_SAMPLE_LIMIT.0, ADAPTIVE_SAMPLE_LIMIT.1); + let stretch_ok = |l: &crate::Layer| stretch(l.stretch).to_bits() == l.stretch.to_bits(); let unchanged = (width, height, rate, duration, samples, adaptive) == (c.width, c.height, c.frame_rate, c.duration, c.motion_blur_samples, c.motion_blur_adaptive_limit) - && pa.to_bits() == c.pixel_aspect.to_bits(); + && pa.to_bits() == c.pixel_aspect.to_bits() + && c.layers.iter().all(stretch_ok); if unchanged { return None; } @@ -125,6 +151,12 @@ fn repair_comp(name: &str, c: &Comp, notes: &mut Vec) -> Option { } (n.width, n.height, n.pixel_aspect, n.frame_rate, n.duration) = (width, height, pa, rate, duration); (n.motion_blur_samples, n.motion_blur_adaptive_limit) = (samples, adaptive); + // Only the layers out of range are copied (the others stay shared). + for l in n.layers.iter_mut_where(|l| !stretch_ok(l)) { + let fixed = stretch(l.stretch); + note(notes, name, &format!("layer '{}' time stretch", l.name), percent(l.stretch), percent(fixed)); + l.stretch = fixed; + } Some(n) } @@ -217,6 +249,85 @@ mod tests { ); } + #[test] + fn a_stretch_is_held_to_one_to_9900_percent_with_its_sign() { + assert_eq!(stretch(f64::NAN), 100.0); + assert_eq!(stretch(0.0), 100.0); + assert_eq!(stretch(-0.0), 100.0); + assert_eq!(stretch(1e-300), MIN_STRETCH); + assert_eq!(stretch(-1e-300), -MIN_STRETCH); + assert_eq!(stretch(0.5), MIN_STRETCH); + assert_eq!(stretch(1e300), MAX_STRETCH); + assert_eq!(stretch(f64::INFINITY), MAX_STRETCH); + assert_eq!(stretch(f64::NEG_INFINITY), -MAX_STRETCH); + for v in [1.0, -1.0, 33.3, 100.0, -100.0, 200.0, 9900.0, -9900.0] { + assert_eq!(stretch(v).to_bits(), v.to_bits(), "{v} is in range"); + } + } + + /// A layer whose stretch got past loading (0, NaN, 1e-300 %, set in memory) still maps time + /// through a stretch in range: before, 0 and 1e-300 % saturated every layer time to the end of + /// time and NaN collapsed it to 0 (#3247). + #[test] + fn layer_and_comp_time_use_a_stretch_in_range() { + let mut p = crate::Project::default(); + let comp = Comp::new(64, 64, FrameRate::FPS_25, Tick::from_seconds_f64(10.0)); + let mut l = crate::build::layer(&mut p, &comp, "L", crate::LayerSource::Camera, (64, 64), None); + l.start_time = Tick::from_seconds_f64(1.0); + let at = |s: f64| Tick::from_seconds_f64(s); + for (bad, effective) in [(0.0, 100.0), (f64::NAN, 100.0), (1e-300, 1.0), (-1e-300, -1.0), (1e300, 9900.0), (f64::NEG_INFINITY, -9900.0)] { + l.stretch = bad; + assert_eq!(l.effective_stretch(), effective, "{bad}"); + // One comp second after the start is 100 / stretch layer seconds. + let want = Tick((at(1.0).0 as f64 * 100.0 / effective) as i64); + assert_eq!(l.layer_time(at(2.0)), want, "{bad}"); + // And back, within the rounding of the two conversions. + assert!((l.comp_time(l.layer_time(at(2.0))) - at(2.0)).abs() <= Tick(100), "{bad}"); + } + } + + #[test] + fn a_stretch_out_of_range_in_a_file_is_corrected_and_reported() { + let mut p = crate::Project::default(); + let comp = Comp::new(64, 64, FrameRate::FPS_25, Tick::from_seconds_f64(2.0)); + let mut c = comp.clone(); + for (name, s) in [("tiny", 1e-300), ("reversed tiny", -1e-300), ("zero", 0.0), ("huge", 1e300), ("reversed huge", -5e4), ("fine", -50.0)] { + let mut l = crate::build::layer(&mut p, &comp, name, crate::LayerSource::Camera, (64, 64), None); + l.stretch = s; + c.layers.push(l); + } + // Added through `add_item`, the comp is held to the limits too: write the values back in. + let id = p.add_item("Comp", effectcraft_color::Label::None, None, ItemKind::Comp(Arc::new(c.clone()))); + assert!(p.comp(id).unwrap().layers.iter().all(|l| l.stretch.abs() >= MIN_STRETCH && l.stretch.abs() <= MAX_STRETCH)); + let mut v: serde_json::Value = serde_json::from_str(&p.to_json()).unwrap(); + for (k, l) in c.layers.iter().enumerate() { + v["items"][id.0.to_string()]["kind"]["layers"][k]["stretch"] = serde_json::json!(l.stretch); + } + let (back, notes) = crate::Project::from_json_repaired(&v.to_string()).unwrap(); + let got: Vec = back.comp(id).unwrap().layers.iter().map(|l| l.stretch).collect(); + assert_eq!(got, [1.0, -1.0, 100.0, 9900.0, -9900.0, -50.0]); + assert_eq!( + notes, + [ + "Comp: layer 'tiny' time stretch 1e-300 % → 1 %", + "Comp: layer 'reversed tiny' time stretch -1e-300 % → -1 %", + "Comp: layer 'zero' time stretch 0 % → 100 %", + "Comp: layer 'huge' time stretch 1e300 % → 9900 %", + "Comp: layer 'reversed huge' time stretch -50000 % → -9900 %", + ] + ); + // A NaN stretch is written as null (an auto-save): the file opens, at 100 %, and says so. + // Before, the whole project was refused. + v["items"][id.0.to_string()]["kind"]["layers"][0]["stretch"] = serde_json::Value::Null; + let (back, notes) = crate::Project::from_json_repaired(&v.to_string()).unwrap(); + assert_eq!(back.comp(id).unwrap().layers.iter().next().unwrap().stretch, 100.0); + assert_eq!(notes.first().map(String::as_str), Some("Comp: layer 'tiny' time stretch NaN % → 100 %")); + let mut nan = p.clone(); + nan.comp_mut(id).unwrap().layers.iter_mut().for_each(|l| l.stretch = f64::NAN); + let (back, _) = crate::Project::from_json_repaired(&nan.to_json_compact()).unwrap(); + assert!(back.comp(id).unwrap().layers.iter().all(|l| l.stretch == 100.0)); + } + #[test] fn a_comp_in_range_is_not_copied() { let c = Comp::new(1920, 1080, FrameRate::FPS_24, Tick::from_seconds_f64(2.0)); diff --git a/crates/render/src/audio.rs b/crates/render/src/audio.rs index e2861340c..00342847d 100644 --- a/crates/render/src/audio.rs +++ b/crates/render/src/audio.rs @@ -155,7 +155,9 @@ impl MixEnv<'_> { return out; } let m = b - a; - let speed = 100.0 / l.stretch; + // Within ±1 % to ±9900 % (#3247): a stretch of 1e-300 % set in memory read about + // usize::MAX source samples for one block. + let speed = 100.0 / l.effective_stretch(); let src_n = ((m as f64 * speed).ceil() as usize).max(1); let lt = l.layer_time(sample_tick(i0 + a as i64, rate)); let buf = match &l.source { @@ -291,7 +293,8 @@ fn mix_into(sources: &AudioSources<'_>, comp_id: ItemId, start: Tick, out: &mut let any_solo = comp.layers.iter().any(|l| l.switches.solo && audible(project, l)); let s0 = start.to_units_floor(rate as i64); for l in &comp.layers { - if !audible(project, l) || (any_solo && !l.switches.solo) || l.stretch <= 0.0 { + // The stretch the layer's time uses (#3247): 0 and NaN play at 100 %, as `layer_time`. + if !audible(project, l) || (any_solo && !l.switches.solo) || l.effective_stretch() <= 0.0 { continue; } // Sample range of the block that falls inside the layer's [in, out). diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index c77fc2d94..930bd810b 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -22,6 +22,7 @@ mod nested; mod paint; mod remap; mod roi; +mod stretch_limits; mod styles; mod time; diff --git a/crates/render/src/tests/stretch_limits.rs b/crates/render/src/tests/stretch_limits.rs new file mode 100644 index 000000000..17bdff317 --- /dev/null +++ b/crates/render/src/tests/stretch_limits.rs @@ -0,0 +1,96 @@ +//! A project file whose layers carry a time stretch outside ±1 % to ±9900 % (1e-300 %, 0, +//! 1e300 %…) opens with the stretch brought into that range, and a frame of it draws and its audio +//! mixes with bounded source reads. Before, a stretch of 1e-300 % asked the footage for about +//! `usize::MAX` samples and the mix overflowed or tried a huge allocation, aborting the app (#3247). + +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; + +use effectcraft_color::Label; +use effectcraft_project::{Footage, FootageKind, ItemId, ItemKind, LayerSource, Project, build, limits}; +use effectcraft_time::Tick; + +use super::{setup, solid}; +use crate::{FootageSource, Image, audio::mix_comp, render_frame}; + +/// Silent audio footage that records the longest read asked of it and refuses a read past a +/// thousand seconds (so a runaway read can't take the machine's memory: the mix then fills the +/// read with silence itself). +#[derive(Default)] +struct Probe { + longest: AtomicUsize, +} + +impl FootageSource for Probe { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, rate: u32) -> Option> { + self.longest.fetch_max(frames, Ordering::Relaxed); + (frames <= rate as usize * 1000).then(|| vec![0.0; frames * 2]) + } +} + +/// The project file of a comp holding a red solid and a sound, both with their stretch set to +/// `stretch` in the file text. +fn file_with_stretch(stretch: f64) -> (String, ItemId) { + let (mut p, cid, comp) = setup(); + let red = solid(&mut p, &comp, [1.0, 0.0, 0.0], 200, 100); + let f = Footage { + path: "tone.wav".into(), + kind: FootageKind::Audio, + has_audio: true, + frame_rate: comp.frame_rate, + duration: Tick::from_seconds_f64(10.0), + ..Default::default() + }; + let fid = p.add_item("tone.wav", Label::SeaFoam, None, ItemKind::Footage(f)); + let sound = build::layer(&mut p, &comp, "sound", LayerSource::Footage { item: fid }, (0, 0), None); + let c = p.comp_mut(cid).unwrap(); + c.layers.push(red); + c.layers.push(sound); + // Both layers are at 100 %; `{:e}` writes a JSON number for any finite value, and NaN is + // written as null, as serializing the project writes it. + let text = p.to_json(); + assert_eq!(text.matches("\"stretch\": 100.0").count(), 2); + let value = if stretch.is_nan() { "null".to_string() } else { format!("{stretch:e}") }; + (text.replace("\"stretch\": 100.0", &format!("\"stretch\": {value}")), cid) +} + +#[test] +fn a_file_with_an_extreme_stretch_opens_in_range_draws_and_mixes_with_bounded_reads() { + for (stored, opened) in + [(1e-300, 1.0), (-1e-300, -1.0), (0.0, 100.0), (1e300, limits::MAX_STRETCH), (-1e300, -limits::MAX_STRETCH), (0.5, 1.0), (f64::NAN, 100.0)] + { + let (text, cid) = file_with_stretch(stored); + let (p, notes) = Project::from_json_repaired(&text).unwrap(); + assert!(notes.iter().filter(|n| n.contains("time stretch")).count() == 2, "{stored}: {notes:?}"); + let c = p.comp(cid).unwrap(); + assert!(c.layers.iter().all(|l| l.stretch == opened), "{stored}: {:?}", c.layers.iter().map(|l| l.stretch).collect::>()); + // A frame draws: the solid has no keys and keeps its in and out points, so it is red. + let img = render_frame(&p, cid, Tick::ZERO, 1.0); + assert_eq!(img.get(10, 10), [1.0, 0.0, 0.0, 1.0], "{stored}"); + // The audio mixes, reading at most 100 source samples per comp sample (a stretch of 1 %). + let (rate, n) = (8000, 4000); + let probe = Probe::default(); + let mixed = mix_comp(&p, &probe, None, cid, Tick::from_seconds_f64(0.5), n, rate); + assert_eq!(mixed.len(), n * 2, "{stored}"); + assert!(probe.longest.load(Ordering::Relaxed) <= 100 * n + 1, "{stored}: a read of {} samples", probe.longest.load(Ordering::Relaxed)); + } +} + +/// A stretch set in memory (not through a file, so not repaired) mixes as the clamped stretch the +/// layer's time uses: before, 1e-300 % read about `usize::MAX` source samples and overflowed. +#[test] +fn an_in_memory_extreme_stretch_mixes_with_bounded_reads() { + for stretch in [1e-300, 0.0, f64::NAN, 1e300] { + let (text, cid) = file_with_stretch(100.0); + let (mut p, _) = Project::from_json_repaired(&text).unwrap(); + p.comp_mut(cid).unwrap().layers.iter_mut().for_each(|l| l.stretch = stretch); + let (rate, n) = (8000, 4000); + let probe = Probe::default(); + let mixed = mix_comp(&p, &probe, None, cid, Tick::from_seconds_f64(0.5), n, rate); + assert_eq!(mixed.len(), n * 2, "{stretch}"); + assert!(probe.longest.load(Ordering::Relaxed) <= 100 * n + 1, "{stretch}: a read of {} samples", probe.longest.load(Ordering::Relaxed)); + } +} diff --git a/crates/ui-egui/src/panels/key_dialogs.rs b/crates/ui-egui/src/panels/key_dialogs.rs index 2a5579837..db3144ffd 100644 --- a/crates/ui-egui/src/panels/key_dialogs.rs +++ b/crates/ui-egui/src/panels/key_dialogs.rs @@ -262,7 +262,14 @@ pub fn time_stretch(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { ui.end_row(); ui.label(tr("Stretch Factor:")); let before = d.percent; - drag(ui, app, "dialog.timeStretch.percent", &mut d.percent, DragSpec { speed: 0.5, range: -10000.0..=10000.0, suffix: " %" }, true); + drag( + ui, + app, + "dialog.timeStretch.percent", + &mut d.percent, + DragSpec { speed: 0.5, range: -effectcraft_project::limits::MAX_STRETCH..=effectcraft_project::limits::MAX_STRETCH, suffix: " %" }, + true, + ); if (before - d.percent).abs() > 1e-9 && d.orig_percent.abs() > 1e-9 { d.duration = d.orig_duration * d.percent.abs() / d.orig_percent.abs(); } @@ -271,7 +278,12 @@ pub fn time_stretch(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { let before = d.duration; drag(ui, app, "dialog.timeStretch.duration", &mut d.duration, DragSpec { speed: 0.05, range: 0.01..=100000.0, suffix: " s" }, true); if (before - d.duration).abs() > 1e-9 && d.orig_duration > 1e-9 { - d.percent = d.orig_percent.signum() * d.orig_percent.abs() * d.duration / d.orig_duration; + let wanted = d.orig_percent.signum() * d.orig_percent.abs() * d.duration / d.orig_duration; + // Within what Time Stretch accepts (±1 % to ±9900 %, #3247); the duration follows. + d.percent = effectcraft_project::limits::stretch(wanted); + if d.percent.to_bits() != wanted.to_bits() && d.orig_percent.abs() > 1e-9 { + d.duration = d.orig_duration * d.percent.abs() / d.orig_percent.abs(); + } } ui.end_row(); }); From f715f30cccffa9ed29f4d081d0b032803f6f0c47 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:31:58 +0100 Subject: [PATCH 045/350] Cryptomatte: the Selection tool picks IDs while the effect is selected As in After Effects, with the Cryptomatte effect selected in Effect Controls and the Selection tool active, a click on its layer in the Composition viewer picks the ID under the cursor without arming the eyedropper: plain replaces, Shift adds, Alt or Ctrl removes, one undo step each, with the same hover name and highlight. A drag there moves nothing. The viewer keeps its clicks off the layer, on its own handles there (bounding box, gizmo, mask vertices, motion-path keys, guides, region of interest), while a region of interest or Free Transform box is drawn, with another effect or a property row selected, and with other tools. A press in the pass the pointer leaves the layer is passed on to the viewer as a click. Reuses the eyedropper's code path (effect.pickCryptomatte); automation id viewer.cryptoSelect. Fixes #2839 --- crates/ui-egui/src/panels/crypto_pick.rs | 155 ++++++++-- .../src/panels/effect_controls/viewer.rs | 22 +- crates/ui-egui/src/panels/viewer.rs | 79 ++++- crates/ui-egui/src/panels/viewer/shapes.rs | 6 + crates/ui-egui/tests/it/main.rs | 1 + crates/ui-egui/tests/it/ui_crypto_pick.rs | 3 + .../ui-egui/tests/it/ui_crypto_pick_undo.rs | 2 + crates/ui-egui/tests/it/ui_crypto_select.rs | 288 ++++++++++++++++++ docs/control-protocol.md | 4 +- docs/effects-parity.md | 2 +- 10 files changed, 531 insertions(+), 31 deletions(-) create mode 100644 crates/ui-egui/tests/it/ui_crypto_select.rs diff --git a/crates/ui-egui/src/panels/crypto_pick.rs b/crates/ui-egui/src/panels/crypto_pick.rs index 2310ba93b..e5e6dd88f 100644 --- a/crates/ui-egui/src/panels/crypto_pick.rs +++ b/crates/ui-egui/src/panels/crypto_pick.rs @@ -3,8 +3,17 @@ //! the viewer names the ID under the cursor, and a click picks it into the Selection: a plain //! click replaces it, Shift adds, Alt or Ctrl (Option or Cmd on a Mac) removes. It stays armed //! for the next click until Esc or the button again, and highlights the hovered ID's coverage. -//! Arming it whenever the effect is selected (After Effects' behaviour, #1811) would only need -//! to set the same pick. +//! +//! As in After Effects, the Selection tool picks the same way without arming anything while the +//! Cryptomatte effect is selected in Effect Controls (#2839, [`implicit_pick`]): over the +//! effect's layer (selected, so the viewer's press acts on it) a click picks and a drag moves nothing; +//! elsewhere the viewer keeps its clicks (selecting and moving layers). Esc has nothing to end. +//! "Over the layer" is its bounding quad, as the viewer's own hit test (not its alpha), less the +//! viewer's handles on it ([`super::viewer::grabs_at`]: bounding-box handles, mask vertices, +//! motion-path keys, guides), which keep scaling, reshaping and moving. A press in the very pass +//! the pointer leaves the layer is passed on to the viewer as a click. While the effect is +//! selected (or the eyedropper armed), a double-click on the layer picks (one step: the second +//! click changes nothing) instead of opening its precomp or editing its text. //! //! The hover needs the frame's Cryptomatte probe ([`effectcraft_engine::Session::crypto_probe_job`]): //! it is built off the UI thread, one at a time, not while playing or scrubbing; the chip says so @@ -21,7 +30,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use effectcraft_engine::commands::CryptoProbeJob; use effectcraft_engine::geom::Mat3; -use effectcraft_engine::project::Layer; +use effectcraft_engine::project::{GroupKind, Layer}; use effectcraft_engine::render::EvalCtx; use egui::{Color32, Rect, Sense, vec2}; use serde_json::{Value, json}; @@ -35,6 +44,9 @@ use crate::theme::Tokens; /// [`FxPick::kind`] of the Cryptomatte eyedropper. pub const KIND: &str = "crypto"; +/// The Cryptomatte effect's match name. +const EFFECT: &str = "ec.3d.cryptomatte"; + /// How long the time must stay put before a probe is built for it (scrubbing). const SETTLE: f64 = 0.1; @@ -111,6 +123,15 @@ struct State { seen: Option<((u64, u64), u64)>, /// The hovered ID's highlight: its hash and probe query, coverage texture, size and uv. mask: Option, + /// Picking with the Selection tool while the effect is selected ([`implicit_pick`]), not with + /// the armed eyedropper. + implicit: bool, + /// The last egui pass the overlay took clicks in (egui hands a press to what sensed it the + /// pass before). + sensed: Option, + /// The query of the last click queued: the second click of a double-click on the same pixel + /// with the same keys is not queued again. + last_click: Option, } /// Clears the busy flag and repaints when a build ends, a panic included. @@ -185,9 +206,20 @@ pub fn upkeep(app: &mut EffectcraftApp, ctx: &egui::Context) { } ctx.data_mut(|d| d.insert_temp(eid, epoch)); } - if app.ui.fx_pick.as_ref().is_none_or(|p| p.kind != KIND) && ctx.data(|d| d.get_temp::(sid)).is_some() { + // Picking with the Selection tool (the effect selected) keeps its state while the viewer runs + // it, every pass. + let old = ctx.data(|d| d.get_temp::(sid)); + let implicit = old.as_ref().is_some_and(|s| s.implicit && s.seen.is_some_and(|(_, n)| n == ctx.cumulative_pass_nr())); + if app.ui.fx_pick.as_ref().is_none_or(|p| p.kind != KIND) + && !implicit + && let Some(mut old) = old + { ctx.data_mut(|d| d.remove::(sid)); app.session.drop_crypto_probe(); + // The effect deselected (or the tool changed) while clicks waited: say they're not made. + if old.implicit && !old.pending.is_empty() { + drop_waiting(app, ctx, &mut old); + } } // The clicks waiting in the viewer, after everything else this pass did (its keys, edits and // frame steps included). @@ -273,10 +305,16 @@ fn viewer_pan_and_secondary(app: &mut EffectcraftApp, ctx: &egui::Context, resp: } } -/// Whether Space / Hand / Zoom (`tools`) or middle/right buttons (`nav`) own the pointer. -struct PointerNav { +/// Whether Space / Hand / Zoom (`tools`) or middle/right buttons (`nav`) own the pointer, and +/// whether a press at a point is the overlay's (`owned`: the Selection tool picking only over the +/// effect's layer, and only where the overlay took the press from the viewer). +struct PointerNav<'a> { tools: bool, nav: bool, + owned: &'a dyn Fn(egui::Pos2) -> bool, + /// The overlay took the pass's presses from the viewer (it sensed clicks the pass before): + /// one it does not own is passed on to the viewer ([`super::viewer::FORWARDED_PRIMARY`]). + forward: bool, } /// Queues Cryptomatte picks from primary presses and releases on the armed eyedropper overlay. @@ -290,7 +328,7 @@ fn viewer_click_to_pick( map: &super::viewer::ViewerMap, ectx: &EvalCtx, st: &mut State, - pointer: PointerNav, + pointer: PointerNav<'_>, at: &dyn Fn(egui::Pos2) -> Option, ) { let ctx = ui.ctx().clone(); @@ -320,7 +358,14 @@ fn viewer_click_to_pick( continue; }; if pressed { - let ours = map.area.contains(pos) && ctx.layer_id_at(pos) == Some(ui.layer_id()); + let in_viewer = map.area.contains(pos) && ctx.layer_id_at(pos) == Some(ui.layer_id()); + let ours = in_viewer && (pointer.owned)(pos); + // Taken from the viewer but not ours (the pointer left the layer in this very pass): + // the viewer clicks there next pass, so it isn't swallowed. + if pointer.forward && in_viewer && !ours && !pointer.nav { + let pass = ctx.cumulative_pass_nr(); + ctx.data_mut(|d| d.insert_temp(egui::Id::unique(super::viewer::FORWARDED_PRIMARY), (pos, keys, pass))); + } st.press = (!pointer.nav && ours).then(|| Press { pos, keys, @@ -348,6 +393,11 @@ fn viewer_click_to_pick( "replace" }); let same_frame = q["time"].as_f64() == Some(ectx.time.seconds()); + let double = ctx.input(|i| i.pointer.button_double_clicked(egui::PointerButton::Primary)); + if double && st.last_click.as_ref() == Some(&q) { + // The second click of a double-click, same pixel and keys: it would change nothing. + continue; + } if pos.distance(from) > SLOP { app.say(&ctx, tr("Drag cancelled the pick").to_string()); } else if app.playback.playing && !(same_frame && app.session.crypto_probe_ready(&q).unwrap_or(false)) { @@ -361,6 +411,7 @@ fn viewer_click_to_pick( st.pending_rev = rev; } st.after_typing |= typing || ctx.text_edit_focused(); + st.last_click = Some(q.clone()); st.pending.push(q); } } @@ -522,7 +573,8 @@ fn handle_escape(app: &mut EffectcraftApp, ui: &mut egui::Ui, st: &mut State) { } } -/// The armed eyedropper's part of the viewer. `m` maps the layer's effect space to the comp. +/// The armed eyedropper's part of the viewer, or (`implicit`) the Selection tool's while the +/// effect is selected ([`implicit_pick`]). `m` maps the layer's effect space to the comp. #[allow(clippy::too_many_arguments)] pub fn viewer_hook( app: &mut EffectcraftApp, @@ -533,6 +585,7 @@ pub fn viewer_hook( layer: &Layer, m: &Mat3, pick: &FxPick, + implicit: bool, ) { let ctx = ui.ctx().clone(); let sid = pick_id(); @@ -540,7 +593,9 @@ pub fn viewer_hook( reset_stale_state(app, &ctx, &mut st, pick); // The Cryptomatte effect that owns the Selection (gone: the pick ends). let Some(fx) = layer.effects().and_then(|f| f.groups().find(|g| g.find(pick.prop).is_some())).map(|g| g.uid) else { - app.ui.fx_pick = None; + if !implicit { + app.ui.fx_pick = None; + } return; }; let start = track_scrub_settle(app, &ctx, &mut st, ectx.time.seconds()); @@ -565,21 +620,83 @@ pub fn viewer_hook( // The Hand and Zoom tools (Space held too) and other buttons keep their own clicks and drags. let tools = super::space_hand(&ctx) || matches!(app.ui.tool, Tool::Hand | Tool::Zoom); let nav = tools || ctx.input(|i| i.pointer.middle_down() || i.pointer.secondary_down()); - // Drags too: a click that drifts must not move the layer under it. (egui hands a press to - // what sensed it the pass before, so middle and right presses land here: passed on below.) - let resp = ui.interact(map.area, egui::Id::unique("viewer-fx-pick"), if tools { Sense::hover() } else { Sense::click_and_drag() }); - viewer_pan_and_secondary(app, &ctx, &resp); - app.auto.add("viewer.fxPick", map.area, &pick.name); - let mut reading = hover_read(app, &ctx, map, &mut st, &resp, start, &at); - viewer_click_to_pick(app, ui, map, ectx, &mut st, PointerNav { tools, nav }, &at); + let oid = egui::Id::unique("viewer-fx-pick"); + // The Selection tool picks only over the effect's layer (#2839): the viewer's press there acts + // on it, a selected layer, even where layers in front cover it ([`super::viewer::pick`]). + // Elsewhere, and on the viewer's own handles there, the viewer keeps its clicks. The armed + // eyedropper picks anywhere. + let ui_layer = ui.layer_id(); + let on_layer = |p: egui::Pos2| { + !implicit + || (map.area.contains(p) + && ctx.layer_id_at(p) == Some(ui_layer) + && super::viewer::layer_hit(ectx, layer.id, map.to_comp(p)) + && !super::viewer::grabs_at(&ctx, p)) + }; + // A press the overlay holds (or its middle-button pan) stays its own wherever the pointer goes. + let panning = ctx.is_being_dragged(oid) && ctx.input(|i| i.pointer.middle_down()); + let over = st.press.is_some() || panning || ctx.pointer_hover_pos().is_some_and(on_layer); + let pass = ctx.cumulative_pass_nr(); + // A press is the overlay's only where it sensed clicks the pass before (egui handed it the + // press, not the viewer underneath, which would select or move the layer as well). + let owned_before = !implicit || st.sensed.is_some_and(|n| n.saturating_add(1) == pass); + st.implicit = implicit; + let mut reading = false; + if !implicit || over { + // Drags too: a click that drifts must not move the layer under it. (egui hands a press to + // what sensed it the pass before, so middle and right presses land here: passed on below.) + let resp = ui.interact(map.area, oid, if tools { Sense::hover() } else { Sense::click_and_drag() }); + if !tools { + st.sensed = Some(pass); + } + viewer_pan_and_secondary(app, &ctx, &resp); + app.auto.add(if implicit { "viewer.cryptoSelect" } else { "viewer.fxPick" }, map.area, &pick.name); + reading = hover_read(app, &ctx, map, &mut st, &resp, start, &at); + } else { + (st.asked, st.reply) = (None, None); + } + let owned = |p: egui::Pos2| owned_before && on_layer(p); + viewer_click_to_pick(app, ui, map, ectx, &mut st, PointerNav { tools, nav, owned: &owned, forward: implicit && owned_before }, &at); // Clicks waiting for their probe ([`make_waiting`], at the end of the pass). reading |= !st.pending.is_empty(); draw_coverage_highlight(app, &ctx, painter, map, m, &mut st); - draw_status_chip(app, &ctx, painter, map, &st, reading); - handle_escape(app, ui, &mut st); + if !implicit || over || reading { + draw_status_chip(app, &ctx, painter, map, &st, reading); + } + // (Selected, the effect has nothing for Esc to end.) + if !implicit { + handle_escape(app, ui, &mut st); + } ctx.data_mut(|d| d.insert_temp(sid, st)); } +/// The Cryptomatte effect selected in Effect Controls with the Selection tool active (After +/// Effects: clicks in the Composition panel pick its IDs then, no eyedropper armed; #2839): the +/// pick for its Selection. The last selected item on a selected layer counts, and only the +/// effect itself (its header in Effect Controls or the Timeline): a press on one of its rows (the +/// eyedropper's button, the Selection field) selects that property instead. An armed eyedropper +/// or crosshair (of any kind) comes first. +pub fn implicit_pick(app: &EffectcraftApp, ctx: &egui::Context) -> Option { + if app.ui.tool != Tool::Selection || app.ui.fx_pick.is_some() || app.ui.text_color_pick.is_some() || crate::color_picker::eyedropper_armed(ctx) { + return None; + } + // Drawing a region of interest or a Free Transform box: the viewer keeps every press. + if app.ui.viewer.roi_draw || super::viewer_overlays::free_transform_target(ctx).is_some() { + return None; + } + let comp = app.session.active_comp()?; + let time = app.session.time(); + let st = &app.session.state; + let (lid, uid) = st.selected_props.iter().rev().find(|(l, _)| st.selected_layers.contains(l))?; + let layer = comp.layer(*lid).filter(|l| l.is_active_at(time))?; + let fx = layer.effects()?.groups().find(|g| g.uid == *uid)?; + if !fx.enabled || !matches!(&fx.kind, GroupKind::Effect { effect } if effect == EFFECT) { + return None; + } + let prop = fx.get("selection")?; + Some(FxPick { kind: KIND.into(), layer: lid.0, prop: prop.uid, name: prop.name.clone() }) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ui-egui/src/panels/effect_controls/viewer.rs b/crates/ui-egui/src/panels/effect_controls/viewer.rs index 6d4eb7ffd..a2da5aa9b 100644 --- a/crates/ui-egui/src/panels/effect_controls/viewer.rs +++ b/crates/ui-egui/src/panels/effect_controls/viewer.rs @@ -221,12 +221,24 @@ pub fn viewer_hook( let ctx = ui.ctx().clone(); crate::color_picker::note_viewer(&ctx, map.area); let mut actions: Actions = vec![]; - let point_preview = if let Some(pick) = app.ui.fx_pick.clone().filter(|p| p.kind == crate::panels::crypto_pick::KIND) { + // The armed Cryptomatte eyedropper, or the Selection tool while the effect is selected (#2839). + let crypto = app.ui.fx_pick.clone().filter(|p| p.kind == crate::panels::crypto_pick::KIND).map(|p| (p, false)); + let crypto = crypto.or_else(|| crate::panels::crypto_pick::implicit_pick(app, &ctx).map(|p| (p, true))); + let point_preview = if let Some((pick, implicit)) = crypto { match ectx.comp.layer(effectcraft_engine::project::LayerId(pick.layer)) { - Some(l) => { - crate::panels::crypto_pick::viewer_hook(app, ui, painter, map, ectx, l, &crate::panels::viewer::from_effect_space(ectx, l, l2c(ectx, l)), &pick) - } - None => app.ui.fx_pick = None, + Some(l) => crate::panels::crypto_pick::viewer_hook( + app, + ui, + painter, + map, + ectx, + l, + &crate::panels::viewer::from_effect_space(ectx, l, l2c(ectx, l)), + &pick, + implicit, + ), + None if !implicit => app.ui.fx_pick = None, + None => {} } None } else { diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index b1899e1f8..d653dc662 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -67,6 +67,45 @@ fn map_id() -> egui::Id { /// the viewer opens its context menu there on the next pass. pub(crate) const FORWARDED_SECONDARY: &str = "viewer-forwarded-secondary"; +/// The egui data key (`(egui::Pos2, egui::Modifiers, u64)`: point, keys, egui pass) of a primary +/// press an overlay sensed but does not want (the Selection tool's Cryptomatte picking, #2839, when +/// the pointer left the layer in the pass of the press): the viewer clicks there on the next pass. +pub(crate) const FORWARDED_PRIMARY: &str = "viewer-forwarded-primary"; + +/// What the viewer's own handles reach this pass, for overlays drawn on top of it. +#[derive(Clone, Default)] +struct Grabs { + handles: Vec, + vertices: Vec, + keys: Vec, + axes: Vec<(LayerId, [Pos2; 2], [f64; 3], [f64; 3])>, + /// Region-of-interest handles. + roi: Vec, + /// Visible, unlocked guides on screen: (x or y, vertical). + guides: Vec<(f32, bool)>, +} + +fn grabs_id() -> egui::Id { + egui::Id::unique("viewer-grabs") +} + +/// Whether a Selection-tool press at `pos` grabs one of the viewer's own controls (a layer's +/// bounding-box handle or gizmo arrow, a mask vertex or tangent, a motion-path key, a guide, a +/// region-of-interest handle) instead of the layer under it, with the reach the viewer gives +/// them. An overlay that takes presses on a layer leaves these to the viewer. +pub(crate) fn grabs_at(ctx: &egui::Context, pos: Pos2) -> bool { + ctx.data(|d| { + let Some(g) = d.get_temp::(grabs_id()) else { return false }; + let reach = vertex_grab_radius(pos, g.handles.iter().copied()); + g.handles.iter().any(|h| h.distance(pos) < HANDLE + 2.0) + || g.vertices.iter().any(|v| v.distance(pos) < reach) + || g.keys.iter().any(|k| k.distance(pos) < 6.0) + || axis_at(&g.axes, pos).is_some() + || g.roi.iter().any(|h| h.distance(pos) < 6.0) + || g.guides.iter().any(|&(at, vertical)| (if vertical { pos.x } else { pos.y } - at).abs() < 4.0) + }) +} + pub fn comp_to_screen(ctx: &egui::Context, p: [f32; 2]) -> Option { let m: ViewerMap = ctx.data(|d| d.get_temp(map_id()))?; Some(m.to_screen([p[0] as f64, p[1] as f64])) @@ -1759,10 +1798,14 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response layers_at(ectx, cpt).any(|h| h.id == l.id) && super::puppet_tool::takes_pin(app, ectx, l, cpt) }) }; - if resp.clicked() - && !text_owns - && let Some(pos) = resp.interact_pointer_pos() - { + // A press an overlay passed on in the pass before (FORWARDED_PRIMARY) is a click here. + let pass = ctx.cumulative_pass_nr(); + let forwarded = ctx + .data_mut(|d| d.remove_temp::<(Pos2, egui::Modifiers, u64)>(egui::Id::unique(FORWARDED_PRIMARY))) + .filter(|f| f.2.saturating_add(1) == pass) + .map(|f| (f.0, f.1)); + let clicked = if resp.clicked() && !text_owns { resp.interact_pointer_pos().map(|p| (p, mods)) } else { None }; + if let Some((pos, mods)) = clicked.or(forwarded) { let cpt = map.to_comp(pos); match app.ui.tool { Tool::Zoom => { @@ -1937,6 +1980,32 @@ fn phase_tail(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, v: &m let ctx = v.ctx.clone(); let painter = v.painter.clone(); let comp = v.comp; + // The viewer's own controls, which overlays on top leave to it (#2839). + let roi = app.session.state.region_of_interest.map(|[rx, ry, rw, rh]| { + let r = Rect::from_min_size(v.comp_rect.min + vec2(rx as f32 * zoom, ry as f32 * zoom), vec2(rw as f32 * zoom, rh as f32 * zoom)); + roi_handles(r).to_vec() + }); + let guides = if app.ui.viewer.guides && !app.ui.viewer.lock_guides { + comp.guides + .iter() + .map(|g| { + let px = g.px(comp.width, comp.height); + let s = map.to_screen([px, px]); + (if g.vertical { s.x } else { s.y }, g.vertical) + }) + .collect() + } else { + vec![] + }; + let grabs = Grabs { + handles: v.handle_hits.iter().map(|h| h.2).collect(), + vertices: v.vertex_hits.iter().map(|h| h.pos).collect(), + keys: v.key_hits.iter().map(|h| h.pos).collect(), + axes: v.axis_hits.clone(), + roi: roi.unwrap_or_default(), + guides, + }; + ctx.data_mut(|d| d.insert_temp(grabs_id(), grabs)); // 3D Camera Tracker points, targets and their menu (on top of the viewer's gestures while // the effect is selected), and its analysis banner. super::camera_tracker_ui::viewer_hook(app, ui, &painter, &map, ectx, &|c, l| l2c(c, l).0); @@ -2001,7 +2070,7 @@ use geometry::{box_corners, project_clipped}; pub(crate) use geometry::{from_effect_space, fx2c, l2c, layer_outline, layer_quad}; use gesture::{Gesture, HANDLE, ROTO_HINT, pen_id, roi_drag, roi_handles, vertex_grab_radius}; use shapes::{create_shape, draw_shape_ghost, shape_drag}; -pub(crate) use shapes::{layer_at, layer_at_including_locked, layers_at, pick, selectable_layers}; +pub(crate) use shapes::{layer_at, layer_at_including_locked, layer_hit, layers_at, pick, selectable_layers}; #[cfg(test)] use tools::constrain_45; use tools::{add_motion_key, axis_at, axis_drag, drag_axes, draw_rigs, parent_world_inverse, pen_cursor, pen_cursor_icon, pen_press, select_motion_key}; diff --git a/crates/ui-egui/src/panels/viewer/shapes.rs b/crates/ui-egui/src/panels/viewer/shapes.rs index 0cd579883..f38b16e31 100644 --- a/crates/ui-egui/src/panels/viewer/shapes.rs +++ b/crates/ui-egui/src/panels/viewer/shapes.rs @@ -22,6 +22,12 @@ pub(crate) fn layers_at<'a, 'b>(ectx: &'b EvalCtx<'a>, cpt: [f64; 2]) -> impl It selectable_layers(ectx.comp, ectx.time).filter(move |l| layer_quad(ectx, l).is_some_and(|(_, q, _)| point_in_quad(cpt, &q))) } +/// Whether `layer` is among the selectable layers under a comp point ([`layers_at`] for that one +/// layer: its bounding quad, not its alpha). +pub(crate) fn layer_hit(ectx: &EvalCtx, layer: LayerId, cpt: [f64; 2]) -> bool { + selectable_layers(ectx.comp, ectx.time).find(|l| l.id == layer).is_some_and(|l| layer_quad(ectx, l).is_some_and(|(_, q, _)| point_in_quad(cpt, &q))) +} + /// Topmost selectable layer under a comp point. pub(crate) fn layer_at<'a>(ectx: &EvalCtx<'a>, cpt: [f64; 2]) -> Option<&'a Layer> { layers_at(ectx, cpt).next() diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 422f0223e..ace9f7222 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -26,6 +26,7 @@ mod ui_contrast; mod ui_control_modifiers; mod ui_crypto_pick; mod ui_crypto_pick_undo; +mod ui_crypto_select; mod ui_dialog_preview; mod ui_dock_resize; mod ui_drag_drop; diff --git a/crates/ui-egui/tests/it/ui_crypto_pick.rs b/crates/ui-egui/tests/it/ui_crypto_pick.rs index 0d1767c7e..893e5d562 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick.rs @@ -76,6 +76,9 @@ fn crypto_session(tag: &str, w: usize, h: usize, blur: bool) -> (Session, Ids) { s.execute_checked("prop.set", json!({"path": "effects/#1/blurriness", "value": 40.0})).unwrap(); } s.execute_checked("effect.apply", json!({"effect": "Cryptomatte"})).unwrap(); + // Not selected: these test the armed eyedropper alone (the Selection tool picks too while + // the effect is selected, #2839: `ui_crypto_select`). + s.state.selected_props.clear(); let fx = s.active_comp().unwrap().layers[0].effects().unwrap().groups().last().unwrap(); let sel = fx.get("selection").unwrap().uid; (s, Ids { layer, sel, footage, dir }) diff --git a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs index 22aaeb9a5..a35743ac0 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs @@ -538,6 +538,8 @@ fn run_step(h: &mut Harness<'_, EffectcraftApp>, f: &Fix, m: &mut Model, size: [ } Step::ApplyCrypto(layer) => { h.state_mut().session.execute_checked("effect.apply", json!({"comp": f.comp, "layers": [layer], "effect": "Cryptomatte"})).unwrap(); + // Not left selected: the model covers the armed eyedropper alone (#2839). + h.state_mut().session.state.selected_props.clear(); let uid = sel_uid(&h.state().session, f.comp, *layer).unwrap(); layer_mut(&mut o, *layer).effects.push((CRYPTO.into(), Some((uid, String::new())))); (format!("#{n} apply Cryptomatte to {layer}"), true) diff --git a/crates/ui-egui/tests/it/ui_crypto_select.rs b/crates/ui-egui/tests/it/ui_crypto_select.rs new file mode 100644 index 000000000..f4b693556 --- /dev/null +++ b/crates/ui-egui/tests/it/ui_crypto_select.rs @@ -0,0 +1,288 @@ +//! Picking Cryptomatte IDs with the Selection tool (#2839): as in After Effects, while the +//! Cryptomatte effect is selected in Effect Controls a click on its layer in the Composition +//! viewer picks the ID under the cursor (plain replaces, Shift adds, Alt or Ctrl removes), one +//! undo step each, without arming the eyedropper, and moves or selects nothing. With another +//! effect selected, off the layer, or with another tool, the viewer behaves as before. + +use crate::ui_crypto_pick::{chip, crypto_exr, settle, wait_probe}; +use effectcraft_engine::Session; +use effectcraft_engine::keyframe::Value as KV; +use effectcraft_engine::project::LayerId; +use effectcraft_ui_egui::EffectcraftApp; +use effectcraft_ui_egui::dock::{self, PanelKind, SplitSize}; +use effectcraft_ui_egui::panels::viewer::comp_to_screen; +use effectcraft_ui_egui::state::Tool; +use egui::{Event, Modifiers, PointerButton, Pos2, vec2}; +use egui_kittest::Harness; +use serde_json::json; + +struct Ids { + plate: u64, + side: u64, + blur: u64, + crypto: u64, + sel: u64, + dir: std::path::PathBuf, +} + +/// A 480 × 180 comp: the 320 × 180 Cryptomatte plate in the middle (comp x 80 to 400) with a +/// Gaussian Blur and a Cryptomatte effect, and a 64 × 64 solid beside it (comp x 408 to 472). +/// The plate is selected, and its Cryptomatte effect in Effect Controls. +fn session(tag: &str) -> (Session, Ids) { + let dir = std::env::temp_dir().join(format!("ec-ui-crysel-{tag}-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = crypto_exr(&dir, "plate.exr", 320, 180, 1); + let mut s = effectcraft_host::session(); + let r = s.execute("file.import", json!({"paths": [path]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let footage = r["items"][0].as_u64().unwrap(); + s.execute("comp.new", json!({"name": "Crypto", "width": 480, "height": 180, "frameRate": 24, "duration": 2})).unwrap(); + let plate = s.execute("layer.addItem", json!({"item": footage})).unwrap()["layer"].as_u64().unwrap(); + s.state.selected_layers = vec![LayerId(plate)]; + s.execute_checked("effect.apply", json!({"effect": "Gaussian Blur"})).unwrap(); + s.execute_checked("effect.apply", json!({"effect": "Cryptomatte"})).unwrap(); + let side = s.execute("layer.newSolid", json!({"name": "Side", "width": 64, "height": 64})).unwrap()["layer"].as_u64().unwrap(); + s.execute_checked("prop.set", json!({"layer": side, "path": "transform/position", "value": [440.0, 40.0]})).unwrap(); + let fx = s.active_comp().unwrap().layer(LayerId(plate)).unwrap().effects().unwrap(); + let (blur, crypto) = (fx.groups().next().unwrap(), fx.groups().nth(1).unwrap()); + assert_eq!(crypto.match_id, "ec.3d.cryptomatte"); + let (blur, crypto, sel) = (blur.uid, crypto.uid, crypto.get("selection").unwrap().uid); + s.state.selected_layers = vec![LayerId(plate)]; + s.state.selected_props = vec![(LayerId(plate), crypto)]; + s.history = Default::default(); + (s, Ids { plate, side, blur, crypto, sel, dir }) +} + +/// The Composition viewer and Effect Controls side by side, the Timeline below. +fn harness(s: Session) -> Harness<'static, EffectcraftApp> { + let mut app = EffectcraftApp::new(s); + app.ui.viewer.show_layer_controls = false; + app.ui.tool = Tool::Selection; + app.ui.dock = dock::docked(dock::vsplit( + SplitSize::FixedSecond(160.0), + dock::hsplit(SplitSize::FixedSecond(420.0), dock::tabs(&[PanelKind::Composition], 0), dock::tabs(&[PanelKind::EffectControls], 0)), + dock::tabs(&[PanelKind::Timeline], 0), + )); + let mut h = Harness::builder().with_size(vec2(1400.0, 800.0)).with_pixels_per_point(1.0).with_step_dt(0.25).build_eframe(|_| app); + settle(&mut h); + h +} + +/// The screen point of the plate's pixel (x, y). +fn on_plate(h: &Harness<'_, EffectcraftApp>, x: f64, y: f64) -> Pos2 { + comp_to_screen(&h.ctx, [(80.0 + x) as f32, y as f32]).unwrap() +} + +fn hover(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2) -> Option { + h.event(Event::PointerMoved(pos)); + // The overlay comes up over the layer in this pass and is hovered from the next one on. + h.run_steps(2); + wait_probe(h); + h.step(); + chip(h) +} + +/// Hover (a pass for the viewer to see the pointer), then press and release with `modifiers`. +fn click(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2, modifiers: Modifiers) { + h.event(Event::PointerMoved(pos)); + h.event(Event::ModifiersChanged(modifiers)); + h.step(); + for pressed in [true, false] { + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers }); + h.step(); + } + h.event(Event::ModifiersChanged(Modifiers::NONE)); + h.step(); + wait_probe(h); + h.run_steps(2); +} + +fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, by: egui::Vec2) { + h.event(Event::PointerMoved(from)); + h.step(); + h.event(Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + for k in 1..=6 { + h.event(Event::PointerMoved(from + by * k as f32 / 6.0)); + h.step(); + } + h.event(Event::PointerButton { pos: from + by, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +fn selection(h: &Harness<'_, EffectcraftApp>, ids: &Ids) -> String { + let s = &h.state().session; + match &s.active_comp().unwrap().layer(LayerId(ids.plate)).unwrap().props.find(ids.sel).unwrap().value { + KV::Str(t) => t.clone(), + v => panic!("{v:?}"), + } +} + +fn position(h: &Harness<'_, EffectcraftApp>, layer: u64) -> String { + let l = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().clone(); + format!("{:?}", l.transform().and_then(|t| t.get("position")).map(|p| p.value.clone())) +} + +fn undo_steps(h: &Harness<'_, EffectcraftApp>) -> usize { + h.state().session.history.undo.len() +} + +fn selected_layers(h: &Harness<'_, EffectcraftApp>) -> Vec { + h.state().session.state.selected_layers.iter().map(|l| l.0).collect() +} + +/// Without the change, the viewer took these clicks itself: the Selection stayed empty. +#[test] +fn the_selection_tool_picks_while_the_cryptomatte_effect_is_selected() { + let (s, ids) = session("picks"); + let mut h = harness(s); + let (steps, before) = (undo_steps(&h), position(&h, ids.plate)); + let (a, b) = (on_plate(&h, 50.0, 40.0), on_plate(&h, 120.0, 100.0)); + // The viewer names the ID under the cursor, as the armed eyedropper does. + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + assert!(h.state().auto.find("viewer.cryptoSelect").is_some(), "the Selection tool picks over the layer"); + assert_eq!(hover(&mut h, b).as_deref(), Some("c3_3")); + click(&mut h, a, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c1_1", "a click replaces"); + click(&mut h, b, Modifiers::SHIFT); + assert_eq!(selection(&h, &ids), "c1_1, c3_3", "Shift adds"); + click(&mut h, a, Modifiers::ALT); + assert_eq!(selection(&h, &ids), "c3_3", "Alt removes"); + click(&mut h, b, Modifiers::COMMAND); + assert_eq!(selection(&h, &ids), "", "Ctrl removes"); + click(&mut h, b, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c3_3"); + click(&mut h, b, Modifiers::CTRL); + assert_eq!(selection(&h, &ids), "", "Ctrl (not Cmd) removes too"); + click(&mut h, b, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c3_3"); + assert_eq!(undo_steps(&h), steps + 7, "one undo step per pick"); + assert!(h.state().ui.fx_pick.is_none(), "nothing armed"); + // Nothing else changed: the layer stays put and stays the only one selected. + assert_eq!(position(&h, ids.plate), before); + assert_eq!(selected_layers(&h), vec![ids.plate]); + // A drag on the layer moves nothing and picks nothing. + drag(&mut h, a, vec2(120.0, 40.0)); + assert_eq!((position(&h, ids.plate), undo_steps(&h), selection(&h, &ids)), (before, steps + 7, "c3_3".to_string())); + // Esc has nothing to end: the next click still picks. + h.key_press(egui::Key::Escape); + h.run_steps(2); + click(&mut h, a, Modifiers::SHIFT); + assert_eq!(selection(&h, &ids), "c3_3, c1_1", "added at the end"); + // Undo takes the picks back one at a time. + h.state_mut().session.execute_checked("edit.undo", json!({})).unwrap(); + assert_eq!(selection(&h, &ids), "c3_3"); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// With another effect selected, beside the layer, or with another tool, the viewer keeps its +/// clicks: the Selection tool moves and selects layers as before, and nothing is picked. +#[test] +fn the_viewer_keeps_its_clicks_with_another_effect_beside_the_layer_or_another_tool() { + let (s, ids) = session("keeps"); + let mut h = harness(s); + let a = on_plate(&h, 50.0, 40.0); + // Gaussian Blur selected instead: a drag moves the layer. + h.state_mut().session.state.selected_props = vec![(LayerId(ids.plate), ids.blur)]; + h.run_steps(2); + let before = position(&h, ids.plate); + assert_eq!(hover(&mut h, a), None, "no ID named"); + assert!(h.state().auto.find("viewer.cryptoSelect").is_none()); + drag(&mut h, a, vec2(60.0, 0.0)); + assert_ne!(position(&h, ids.plate), before, "the drag moved the layer"); + assert_eq!(selection(&h, &ids), ""); + // The Cryptomatte effect selected again: a click on the other layer beside it selects that one. + h.state_mut().session.execute_checked("edit.undo", json!({})).unwrap(); + h.state_mut().session.state.selected_layers = vec![LayerId(ids.plate)]; + h.state_mut().session.state.selected_props = vec![(LayerId(ids.plate), ids.crypto)]; + h.run_steps(2); + let side = comp_to_screen(&h.ctx, [440.0, 40.0]).unwrap(); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + click(&mut h, side, Modifiers::NONE); + assert_eq!(selected_layers(&h), vec![ids.side], "the click beside the layer selected the other layer"); + assert_eq!(selection(&h, &ids), ""); + // The Hand tool with the effect selected: no pick. + h.state_mut().session.state.selected_layers = vec![LayerId(ids.plate)]; + h.state_mut().session.state.selected_props = vec![(LayerId(ids.plate), ids.crypto)]; + h.state_mut().ui.tool = Tool::Hand; + h.run_steps(2); + let a = on_plate(&h, 50.0, 40.0); + click(&mut h, a, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "", "the Hand tool picks nothing"); + // Back to the Selection tool: it picks. + h.state_mut().ui.tool = Tool::Selection; + h.run_steps(2); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + click(&mut h, a, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c1_1"); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// A press in the very pass the pointer leaves the layer for another one (the overlay sensed it +/// the pass before, so egui hands it the press): passed on to the viewer, which selects that layer. +#[test] +fn a_press_as_the_pointer_leaves_the_layer_still_selects_the_other_layer() { + let (s, ids) = session("leave"); + let mut h = harness(s); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + let side = comp_to_screen(&h.ctx, [440.0, 40.0]).unwrap(); + let ev = &mut h.input_mut().events; + ev.push(Event::PointerMoved(side)); + ev.push(Event::PointerButton { pos: side, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + h.event(Event::PointerButton { pos: side, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); + assert_eq!(selected_layers(&h), vec![ids.side], "the press wasn't swallowed"); + assert_eq!(selection(&h, &ids), ""); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// The viewer's own handles on the layer keep working with the effect selected: a drag on a +/// corner of the bounding box scales the layer and picks nothing. +#[test] +fn a_corner_handle_still_scales_with_the_effect_selected() { + let (s, ids) = session("handle"); + let mut h = harness(s); + h.state_mut().ui.viewer.show_layer_controls = true; + h.run_steps(2); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1"), "inside the layer it picks"); + let scale = |h: &Harness<'_, EffectcraftApp>| { + let l = h.state().session.active_comp().unwrap().layer(LayerId(ids.plate)).unwrap().clone(); + format!("{:?}", l.transform().and_then(|t| t.get("scale")).map(|p| p.value.clone())) + }; + let before = scale(&h); + // The bottom-right corner of the plate (comp 400, 180). + let corner = comp_to_screen(&h.ctx, [400.0, 180.0]).unwrap(); + drag(&mut h, corner, vec2(-40.0, -20.0)); + assert_ne!(scale(&h), before, "the handle scaled the layer"); + assert_eq!(selection(&h, &ids), ""); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// From the effect selected to the eyedropper and back: its button selects the Selection +/// property (the effect itself no longer), so after Esc a click on the layer picks nothing and +/// no Cryptomatte IDs are kept read. +#[test] +fn after_the_eyedropper_and_esc_the_selection_tool_does_not_pick() { + let (s, ids) = session("handoff"); + let mut h = harness(s); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1"), "the effect selected: it picks"); + let eye = crate::ui_crypto_pick::at(&h, &format!("effectControls.prop.{}.eyedropper", ids.sel)); + click(&mut h, eye, Modifiers::NONE); + assert!(h.state().ui.fx_pick.as_ref().is_some_and(|p| p.kind == "crypto" && p.prop == ids.sel), "armed"); + assert_eq!(h.state().session.state.selected_props, vec![(LayerId(ids.plate), ids.sel)], "the row is selected"); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().ui.fx_pick.is_none(), "Esc ends the eyedropper"); + click(&mut h, a, Modifiers::NONE); + h.run_steps(2); + assert_eq!(selection(&h, &ids), "", "no pick"); + assert!(h.state().auto.find("viewer.cryptoSelect").is_none()); + assert!(!h.state().session.crypto_probe_stats().0, "no IDs kept read"); + let _ = std::fs::remove_dir_all(&ids.dir); +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 9f3d69ea5..c28d009ce 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -152,7 +152,9 @@ revolutions are `timeline.prop..revolutions`, `effectControls.prop..re `viewer.motionPath..[.in|.out]`, `viewer.puppetPin.[.rotate|.scale]`, `viewer.freeTransform.handle.`, `viewer.cryptoPick` (the Cryptomatte eyedropper's hover chip; its label is the chip's text), `viewer.cryptoPick.error` (the chip when it shows an error; label `whole`, or -`cut` when the message is cut off), `viewer.cryptoPick.highlight` (the hovered ID's highlighted area) and +`cut` when the message is cut off), `viewer.cryptoPick.highlight` (the hovered ID's highlighted area), +`viewer.cryptoSelect` (the Selection tool picking Cryptomatte IDs over the layer while its Cryptomatte +effect is selected in Effect Controls, no eyedropper armed; label: the Selection's name) and `viewer.regionOfInterest`; the Graph Editor adds `timeline.graph.transformBox[.]`, `timeline.graph.snap` and `timeline.graph.reference`. Use `ui.elements` to see what is on screen. diff --git a/docs/effects-parity.md b/docs/effects-parity.md index c7f850834..2f51c4cc8 100644 --- a/docs/effects-parity.md +++ b/docs/effects-parity.md @@ -26,7 +26,7 @@ corpus compares renders, each effect is assumed to differ somewhere, and users k | Category | Effects | On the GPU | Known differences or bugs | |---|---:|---:|---| -| 3D Channel | 8 | 8 | Cryptomatte's GPU path is slow (#473; the Selection is picked in the viewer with an eyedropper, quoted names with commas fixed); multi-layer EXR: one decode per frame for the picture and the channels, cache softly bounded at 1 GB (#474; holding channels as f16 is still to do) | +| 3D Channel | 8 | 8 | Cryptomatte's GPU path is slow (#473; the Selection is picked in the viewer with an eyedropper, or with the Selection tool while the effect is selected as in After Effects (#2839), quoted names with commas fixed); multi-layer EXR: one decode per frame for the picture and the channels, cache softly bounded at 1 GB (#474; holding channels as f16 is still to do) | | Audio | 13 | 0 | CPU by design (applied in the mixdown) | | Blur & Sharpen | 16 | 15 | Radial Blur keeps the layer edges as After Effects 2026 (713 now pixel-identical); Gaussian Blur interaction and output to be measured first ([fidelity/ae-2026-acceptance.md](fidelity/ae-2026-acceptance.md)) | | Channel | 13 | 13 | — | From 16dccbb8fa8bb0a9e37a99f17109e63d23c99da6 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:37:39 +0100 Subject: [PATCH 046/350] Effect Controls: a lock request with no layer shown is an error (#3193) With the lock kept for a layer of another comp and nothing selected in the active one, ui.set {effectControlsLocked: true} reported success. It now returns the documented error and leaves the kept lock alone. --- crates/ui-egui/src/control.rs | 14 +++++---- crates/ui-egui/tests/it/ui_effect_controls.rs | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index f6c255d69..b9ff46c4b 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -281,11 +281,15 @@ fn set_ui(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome { { return err(e); } - // Effect Controls' tab lock: on locks it to the layer it shows. - if let Some(on) = p.get("effectControlsLocked").and_then(Value::as_bool) - && crate::panels::effect_controls::set_lock(app, on) != on - { - return err("effectControlsLocked: Effect Controls shows no layer to lock"); + // Effect Controls' tab lock: on locks it to the layer it shows. With no layer shown (the + // lock kept for a layer of another comp, nothing selected here) a lock request is an error + // and the kept lock stays. + if let Some(on) = p.get("effectControlsLocked").and_then(Value::as_bool) { + crate::panels::effect_controls::scope_lock(app); + let shown = crate::panels::effect_controls::shown_layer_id(app).is_some(); + if (on && !shown) || crate::panels::effect_controls::set_lock(app, on) != on { + return err("effectControlsLocked: Effect Controls shows no layer to lock"); + } } set_ui_layout(app, p) } diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index ce51a2b2f..ffafe867d 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -1013,6 +1013,36 @@ fn replacing_the_project_unlocks_effect_controls() { assert_eq!(h.state().ui.effect_controls_lock, None, "the lock belongs to the replaced project"); } +/// #3193: with the lock kept for a layer of another comp and no layer selected in the active one, +/// Effect Controls shows no layer: a lock request is an error and the kept lock stays. +#[test] +fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, _, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h)["ok"], true); + let kept = h.state().ui.effect_controls_lock; + assert_eq!(kept.map(|l| l.layer), Some(a)); + // Another comp becomes active, nothing selected in it. + h.state_mut().session.execute("comp.new", json!({"name": "Other", "width": 320, "height": 180, "frameRate": 30, "duration": 5})).unwrap(); + h.state_mut().session.state.selected_layers.clear(); + h.run_steps(3); + assert_ne!(h.state().session.active_comp_id().map(|c| c.0), kept.map(|l| l.comp), "another comp is active"); + let reply = send(&mut h); + assert_eq!(reply["ok"], false, "{reply}"); + assert_eq!(h.state().ui.effect_controls_lock, kept, "the other comp's lock stays"); +} + /// #3193: a lock request right after the project is replaced, before the next frame, is not /// answered with the old project's lock: the reply matches the lock the panel keeps. #[test] From 78acc6e1b1f02a19c18c7ecb7fe9d9afc368fd4b Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:41:58 +0100 Subject: [PATCH 047/350] Number fields: a typed +n adds n to each selected layer, even where the edited one is clamped (#3195) A typed +10 on several selected layers went through the same-change offset path, so when the edited layer's range cut its change short (Opacity 95 + 10 stops at 100) the others moved by 5. Send the typed operand as prop.set addend: each other layer takes its own value plus it, within its own range; scrubs keep the offset path. --- crates/engine/src/cmd/prop.rs | 26 ++++++++++++++--------- crates/engine/src/tests/timeline.rs | 11 ++++++++++ crates/ui-egui/src/widgets.rs | 11 +++++----- crates/ui-egui/tests/it/ui_inline_edit.rs | 10 +++++++++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index ae37ea1b7..028fdb0dd 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -371,24 +371,30 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { }; let follow: Vec = p.get("follow").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_u64).filter_map(|d| usize::try_from(d).ok()).collect(); - // A typed `*2` or `/2` (#3195): each layer's own value times it, not the edited layer's result. + // A typed `*2`, `/2` (`factor`) or `+10` (`addend`) (#3195): each layer's own value times it + // or plus it, not the edited layer's result (which its range may have clamped). let factor = p.get("factor").and_then(Value::as_f64).filter(|k| k.is_finite() && delta.is_none()); + let addend = p.get("addend").and_then(Value::as_f64).filter(|n| n.is_finite() && delta.is_none()); + let own_step = |v: f64| -> Option { + let r = match (factor, addend) { + (Some(k), _) => v * k, + (None, Some(n)) => v + n, + (None, None) => return None, + }; + r.is_finite().then_some(r) + }; // Each layer's own value where `set` writes it (a `time` other than the CTI keys there). let typed = |s: &Session, q: &mut Value, olid: LayerId, ouid: Uid| { let at = snapped_key_time(s, cid, olid, q, "prop.set").ok().flatten(); - if let (true, Some(k)) = (scalar, factor) - && let Some(own) = components_at(s, cid, olid, ouid, at).and_then(|c| c.first().copied()) - && (own * k).is_finite() - { - q["value"] = json!(own * k); + if scalar && let Some(v) = components_at(s, cid, olid, ouid, at).and_then(|c| c.first().copied()).and_then(own_step) { + q["value"] = json!(v); } if let (Some(dims), Some(n)) = (&dims, &new) && let Some(mut c) = components_at(s, cid, olid, ouid, at) { // The typed components' new values on this layer. - let typed_value = |c: &[f64], d: usize| match factor { - Some(k) => c.get(d).map(|v| v * k).filter(|v| v.is_finite()), - None => n.get(d).copied(), + let typed_value = |c: &[f64], d: usize| { + if factor.is_some() || addend.is_some() { c.get(d).copied().and_then(own_step) } else { n.get(d).copied() } }; let change = dims.first().and_then(|&d| Some(typed_value(&c, d)? - c.get(d)?)).unwrap_or(0.0); let values: Vec> = dims.iter().map(|&d| typed_value(&c, d)).collect(); @@ -1459,7 +1465,7 @@ pub fn specs() -> Vec { "Set Property Value", [], None, - "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes), factor? (with selected, a typed `*` or `/`: the other layers take their own value times it, in the dims components of a vector)}", + "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes), factor? (with selected, a typed `*` or `/`: the other layers take their own value times it, in the dims components of a vector), addend? (the same for a typed `+`: their own value plus it)}", has_layers, set_selected ), diff --git a/crates/engine/src/tests/timeline.rs b/crates/engine/src/tests/timeline.rs index 43eb922e2..f291de627 100644 --- a/crates/engine/src/tests/timeline.rs +++ b/crates/engine/src/tests/timeline.rs @@ -1552,6 +1552,17 @@ fn a_typed_factor_scales_each_selected_layers_own_value() { s.execute_checked("prop.set", json!({"layer": a, "prop": position, "value": [5.0, 20.0], "selected": true, "dims": [0], "factor": 0.5})).unwrap(); assert_eq!(prop(&s, a, "transform/position").value.components()[..2], [5.0, 20.0]); assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [50.0, 50.0], "B halves its own X, keeps its Y"); + // A typed `+10` (`addend`): each layer's own value plus 10, within its own range, even where + // the edited layer's range cut its change short (95 + 10 stops at 100). + s.execute("prop.set", json!({"layer": a, "path": "transform/opacity", "value": 95.0})).unwrap(); + s.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 30.0})).unwrap(); + s.execute_checked("prop.set", json!({"layer": a, "prop": opacity, "value": 100.0, "selected": true, "addend": 10.0})).unwrap(); + assert_eq!(prop(&s, b, "transform/opacity").value.as_f64(), 40.0, "B adds 10, not the 5 A moved"); + s.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 95.0})).unwrap(); + s.execute_checked("prop.set", json!({"layer": a, "prop": opacity, "value": 100.0, "selected": true, "addend": 10.0})).unwrap(); + assert_eq!(prop(&s, b, "transform/opacity").value.as_f64(), 100.0, "B's own range still applies"); + s.execute_checked("prop.set", json!({"layer": a, "prop": position, "value": [15.0, 20.0], "selected": true, "dims": [0], "addend": 10.0})).unwrap(); + assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [60.0, 50.0], "B adds 10 to its own X"); } /// #3195: Mask Feather is never negative on the other selected layers either, when a typed `*-1` diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index c5b7223da..87a6d217e 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -164,14 +164,15 @@ pub fn hot_number_step_at( /// Mark a queued `prop.set` (with `selected`) as the typed `step` it came from, so every other /// selected layer takes that step from its own value rather than the edited layer's result -/// (#3195): `+n` by the same change (`offset`), `*n` and `/n` by the same `factor`. +/// (#3195): `+n` adds `n` to each (`addend`; not the edited layer's change, which its range may +/// have clamped), `*n` and `/n` multiply each by the same `factor`. pub fn mark_step(action: Option<&mut (String, serde_json::Value)>, step: Option) { let (Some((_, a)), Some(step)) = (action, step) else { return }; - match step.factor() { - Some(k) => a["factor"] = serde_json::json!(k), - None if matches!(step, crate::num_math::Step::Add(_)) => a["offset"] = serde_json::json!(true), + match (step, step.factor()) { + (crate::num_math::Step::Add(n), _) => a["addend"] = serde_json::json!(n), + (_, Some(k)) => a["factor"] = serde_json::json!(k), // (A factor too large to send: the other layers take the typed result.) - None => {} + (_, None) => {} } } diff --git a/crates/ui-egui/tests/it/ui_inline_edit.rs b/crates/ui-egui/tests/it/ui_inline_edit.rs index c845863e4..7d51f0015 100644 --- a/crates/ui-egui/tests/it/ui_inline_edit.rs +++ b/crates/ui-egui/tests/it/ui_inline_edit.rs @@ -480,6 +480,16 @@ fn a_typed_step_applies_to_each_selected_layer() { key(&mut h, Key::Enter); assert_eq!((opacity_of(&h, a), opacity_of(&h, b)), expected, "{text}"); } + // `+10` where the edited layer stops at 100: the others still add 10, not the 5 it moved. + for (l, v) in [(a, 95.0), (b, 30.0)] { + h.state_mut().session.execute("prop.set", json!({"layer": l, "path": "transform/opacity", "value": v})).unwrap(); + } + h.run_steps(5); + let p = center(&h, &format!("timeline.prop.{opacity}.value")); + click(&mut h, p, 1); + type_text(&mut h, "+10"); + key(&mut h, Key::Enter); + assert_eq!((opacity_of(&h, a), opacity_of(&h, b)), (100.0, 40.0)); } /// A click at `p` with `modifiers` held. From d1bdb701b74b5e7b6b764d66607b31af0f1ba109 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 18:22:03 +0100 Subject: [PATCH 048/350] Effect Controls: a lock request while locked in another comp is an error (#3193) Locked to a layer of another comp, the panel shows the active comp's selection and its tab lock reads locked (a click unlocks). ui.set {effectControlsLocked: true} reported success there without locking the shown layer; it now returns an error and keeps the other comp's lock. --- crates/ui-egui/src/control.rs | 5 +++ crates/ui-egui/tests/it/ui_effect_controls.rs | 31 +++++++++++++++++++ docs/control-protocol.md | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index b9ff46c4b..15a10380d 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -290,6 +290,11 @@ fn set_ui(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome { if (on && !shown) || crate::panels::effect_controls::set_lock(app, on) != on { return err("effectControlsLocked: Effect Controls shows no layer to lock"); } + // Locked to a layer of another comp, the panel shows this comp's selection: the tab's + // lock reads locked and a click unlocks, so a lock request does not lock what is shown. + if on && app.ui.effect_controls_lock.is_some_and(|k| app.session.active_comp_id().is_none_or(|c| c.0 != k.comp)) { + return err("effectControlsLocked: Effect Controls is locked to a layer of another composition; unlock it first"); + } } set_ui_layout(app, p) } diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index ffafe867d..ec7d5683d 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -1043,6 +1043,37 @@ fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { assert_eq!(h.state().ui.effect_controls_lock, kept, "the other comp's lock stays"); } +/// #3193: locked to a layer of another comp, the panel shows this comp's selection; a lock request +/// does not claim to lock that layer: it is an error and the other comp's lock stays. +#[test] +fn a_lock_request_while_locked_in_another_comp_fails() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, _, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h)["ok"], true); + let kept = h.state().ui.effect_controls_lock; + assert_eq!(kept.map(|l| l.layer), Some(a)); + // Another comp with a selected layer becomes active: the panel shows that layer. + h.state_mut().session.execute("comp.new", json!({"name": "Other", "width": 320, "height": 180, "frameRate": 30, "duration": 5})).unwrap(); + let c = h.state_mut().session.execute("layer.newSolid", json!({"name": "C", "color": "#00ff00"})).unwrap()["layer"].as_u64().unwrap(); + h.state_mut().session.state.selected_layers = vec![LayerId(c)]; + h.run_steps(3); + assert_ne!(h.state().session.active_comp_id().map(|x| x.0), kept.map(|l| l.comp), "another comp is active"); + let reply = send(&mut h); + assert_eq!(reply["ok"], false, "{reply}"); + assert_eq!(h.state().ui.effect_controls_lock, kept, "the other comp's lock stays"); +} + /// #3193: a lock request right after the project is replaced, before the next frame, is not /// answered with the old project's lock: the reply matches the lock the panel keeps. #[test] diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 8af641a3a..126b8fd02 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -66,7 +66,7 @@ panel registers an element id; both are enforced by `crates/ui-egui/tests/it/ui_ |---|---|---| | `ui.inspect` | `{}` | Window size, pixels per point, fps, serialized `UiState`, playback, time, active comp, selection, open dialog, last render ms, `keyboardFocus`, `windowFocused`, `workspaceStorageError`. | | `ui.elements` | `{prefix?}` | `[{id, label, rect: [x, y, w, h]}]`: the widgets drawn in the last frame, in points. | -| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, effectControlsLocked?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `effectControlsLocked: true` locks Effect Controls on the layer it shows (an error when it shows none), `false` unlocks it (the tab's lock icon, `panel.tab.EffectControls.lock`; `ui.inspect` reports it as `effect_controls_lock: {comp, layer}`); `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | +| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, effectControlsLocked?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `effectControlsLocked: true` locks Effect Controls on the layer it shows (an error when it shows none, or while it is locked to a layer of another composition), `false` unlocks it (the tab's lock icon, `panel.tab.EffectControls.lock`; `ui.inspect` reports it as `effect_controls_lock: {comp, layer}`); `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | | `ui.panel.show` / `ui.panel.close` | `{panel}` | Panels: `Project`, `Composition`, `Timeline`, `EffectControls`, `EffectsPresets`, `Info`, `Preview`, `Character`, `Paragraph`, `Align` and others. | | `ui.playback` | `{action: play\|stop\|toggle\|status}` | `{playing, time, audio, levelsDb, peaksDb}` (audio: an audio preview is running; L/R meter levels and held peaks in dBFS) | | `ui.resize` | `{width, height}` | Resizes the window (points). | From df01e1d75c369b5ec52bbe60873b9ed12a0c07f5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 18:26:04 +0100 Subject: [PATCH 049/350] Cryptomatte: a touch that lands on the layer and presses in one pass picks A touch, or a pen without hover, arrives on the layer and presses in the same pass. The overlay wasn't there the pass before, so egui handed the press to the viewer and nothing was picked. The overlay now takes such a press when it is on the layer and off the viewer's handles, and claims it from the viewer (CLAIMED_PRIMARY), which then neither clicks, double-clicks nor drags with it: Shift no longer toggles the layer off, and a drag moves nothing. The claim ends with the button up. --- crates/ui-egui/src/panels/crypto_pick.rs | 22 ++++++++--- crates/ui-egui/src/panels/viewer.rs | 24 +++++++++++- crates/ui-egui/tests/it/ui_crypto_select.rs | 41 +++++++++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/panels/crypto_pick.rs b/crates/ui-egui/src/panels/crypto_pick.rs index e5e6dd88f..543fa6bdf 100644 --- a/crates/ui-egui/src/panels/crypto_pick.rs +++ b/crates/ui-egui/src/panels/crypto_pick.rs @@ -11,7 +11,8 @@ //! "Over the layer" is its bounding quad, as the viewer's own hit test (not its alpha), less the //! viewer's handles on it ([`super::viewer::grabs_at`]: bounding-box handles, mask vertices, //! motion-path keys, guides), which keep scaling, reshaping and moving. A press in the very pass -//! the pointer leaves the layer is passed on to the viewer as a click. While the effect is +//! the pointer leaves the layer is passed on to the viewer as a click; one in the pass it arrives +//! on the layer (a touch, a pen without hover) is claimed from the viewer and picks. While the effect is //! selected (or the eyedropper armed), a double-click on the layer picks (one step: the second //! click changes nothing) instead of opening its precomp or editing its text. //! @@ -307,7 +308,7 @@ fn viewer_pan_and_secondary(app: &mut EffectcraftApp, ctx: &egui::Context, resp: /// Whether Space / Hand / Zoom (`tools`) or middle/right buttons (`nav`) own the pointer, and /// whether a press at a point is the overlay's (`owned`: the Selection tool picking only over the -/// effect's layer, and only where the overlay took the press from the viewer). +/// effect's layer, off the viewer's own handles there). struct PointerNav<'a> { tools: bool, nav: bool, @@ -315,6 +316,10 @@ struct PointerNav<'a> { /// The overlay took the pass's presses from the viewer (it sensed clicks the pass before): /// one it does not own is passed on to the viewer ([`super::viewer::FORWARDED_PRIMARY`]). forward: bool, + /// The overlay did not sense clicks the pass before, so egui hands the pass's presses to the + /// viewer: one it owns (a touch, or a pen without hover, arriving on the layer and pressing in + /// one pass) is claimed from the viewer ([`super::viewer::CLAIMED_PRIMARY`]). + claim: bool, } /// Queues Cryptomatte picks from primary presses and releases on the armed eyedropper overlay. @@ -366,6 +371,10 @@ fn viewer_click_to_pick( let pass = ctx.cumulative_pass_nr(); ctx.data_mut(|d| d.insert_temp(egui::Id::unique(super::viewer::FORWARDED_PRIMARY), (pos, keys, pass))); } + // Handed to the viewer but ours: the viewer leaves it alone, so it only picks. + if pointer.claim && ours && !pointer.nav { + ctx.data_mut(|d| d.insert_temp(egui::Id::unique(super::viewer::CLAIMED_PRIMARY), ())); + } st.press = (!pointer.nav && ours).then(|| Press { pos, keys, @@ -637,8 +646,9 @@ pub fn viewer_hook( let panning = ctx.is_being_dragged(oid) && ctx.input(|i| i.pointer.middle_down()); let over = st.press.is_some() || panning || ctx.pointer_hover_pos().is_some_and(on_layer); let pass = ctx.cumulative_pass_nr(); - // A press is the overlay's only where it sensed clicks the pass before (egui handed it the - // press, not the viewer underneath, which would select or move the layer as well). + // Whether the overlay sensed clicks the pass before: egui handed it the pass's presses, not the + // viewer underneath (which would select or move the layer as well). If it didn't, a press of + // ours is claimed from the viewer; if it did, one not ours is passed on to the viewer. let owned_before = !implicit || st.sensed.is_some_and(|n| n.saturating_add(1) == pass); st.implicit = implicit; let mut reading = false; @@ -655,8 +665,8 @@ pub fn viewer_hook( } else { (st.asked, st.reply) = (None, None); } - let owned = |p: egui::Pos2| owned_before && on_layer(p); - viewer_click_to_pick(app, ui, map, ectx, &mut st, PointerNav { tools, nav, owned: &owned, forward: implicit && owned_before }, &at); + let pointer = PointerNav { tools, nav, owned: &on_layer, forward: implicit && owned_before, claim: implicit && !owned_before }; + viewer_click_to_pick(app, ui, map, ectx, &mut st, pointer, &at); // Clicks waiting for their probe ([`make_waiting`], at the end of the pass). reading |= !st.pending.is_empty(); draw_coverage_highlight(app, &ctx, painter, map, m, &mut st); diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index d653dc662..5886f722e 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -72,6 +72,17 @@ pub(crate) const FORWARDED_SECONDARY: &str = "viewer-forwarded-secondary"; /// the pointer left the layer in the pass of the press): the viewer clicks there on the next pass. pub(crate) const FORWARDED_PRIMARY: &str = "viewer-forwarded-primary"; +/// The egui data key (`()`) of a primary press an overlay claimed although egui handed it to the +/// viewer (the Selection tool's Cryptomatte picking, #2839, when the pointer arrived on the layer +/// and pressed in one pass, as a touch or a pen without hover does): the viewer neither clicks +/// nor drags with it, and lets go of the claim once the button is up. +pub(crate) const CLAIMED_PRIMARY: &str = "viewer-claimed-primary"; + +/// Whether an overlay claimed the primary press in progress ([`CLAIMED_PRIMARY`]). +fn press_claimed(ctx: &egui::Context) -> bool { + ctx.data(|d| d.get_temp::<()>(egui::Id::unique(CLAIMED_PRIMARY))).is_some() +} + /// What the viewer's own handles reach this pass, for overlays drawn on top of it. #[derive(Clone, Default)] struct Grabs { @@ -1203,6 +1214,7 @@ fn phase_dragstart(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, }; if resp.drag_started() && !text_owns + && !press_claimed(ui.ctx()) && let Some(pos) = resp.interact_pointer_pos() { let cpt = map.to_comp(pos); @@ -1804,7 +1816,9 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response .data_mut(|d| d.remove_temp::<(Pos2, egui::Modifiers, u64)>(egui::Id::unique(FORWARDED_PRIMARY))) .filter(|f| f.2.saturating_add(1) == pass) .map(|f| (f.0, f.1)); - let clicked = if resp.clicked() && !text_owns { resp.interact_pointer_pos().map(|p| (p, mods)) } else { None }; + // A press an overlay claimed (CLAIMED_PRIMARY) is not a click here. + let claimed = press_claimed(&ctx); + let clicked = if resp.clicked() && !text_owns && !claimed { resp.interact_pointer_pos().map(|p| (p, mods)) } else { None }; if let Some((pos, mods)) = clicked.or(forwarded) { let cpt = map.to_comp(pos); match app.ui.tool { @@ -1914,8 +1928,14 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response fn phase_dblclick(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let map = v.map; - let text_owns = v.text_owns; let ctx = v.ctx.clone(); + // A press an overlay claimed (CLAIMED_PRIMARY) is no double-click here either; the claim + // ends with the button up (after this pass's click and double-click). + let claimed = press_claimed(&ctx); + if claimed && !ctx.input(|i| i.pointer.primary_down()) { + ctx.data_mut(|d| d.remove::<()>(egui::Id::unique(CLAIMED_PRIMARY))); + } + let text_owns = v.text_owns || claimed; let handle_hits = std::mem::take(&mut v.handle_hits); let vertex_hits = std::mem::take(&mut v.vertex_hits); let paths = std::mem::take(&mut v.paths); diff --git a/crates/ui-egui/tests/it/ui_crypto_select.rs b/crates/ui-egui/tests/it/ui_crypto_select.rs index f4b693556..b1b716e24 100644 --- a/crates/ui-egui/tests/it/ui_crypto_select.rs +++ b/crates/ui-egui/tests/it/ui_crypto_select.rs @@ -286,3 +286,44 @@ fn after_the_eyedropper_and_esc_the_selection_tool_does_not_pick() { assert!(!h.state().session.crypto_probe_stats().0, "no IDs kept read"); let _ = std::fs::remove_dir_all(&ids.dir); } + +/// A touch (or a pen without hover) arrives on the layer and presses in one pass: the overlay +/// wasn't there the pass before, so egui hands the press to the viewer. It is still a pick (and +/// Shift still adds), and the viewer neither selects, toggles nor moves the layer with it. +#[test] +fn a_touch_that_arrives_and_presses_in_one_pass_picks() { + let (s, ids) = session("touch"); + let mut h = harness(s); + let (steps, before) = (undo_steps(&h), position(&h, ids.plate)); + let (a, b) = (on_plate(&h, 50.0, 40.0), on_plate(&h, 120.0, 100.0)); + let touch = |h: &mut Harness<'_, EffectcraftApp>, pos: Pos2, modifiers: Modifiers, by: egui::Vec2| { + h.event(Event::PointerGone); + h.run_steps(2); + let ev = &mut h.input_mut().events; + ev.push(Event::ModifiersChanged(modifiers)); + ev.push(Event::PointerMoved(pos)); + ev.push(Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers }); + h.step(); + for k in 1..=4 { + h.event(Event::PointerMoved(pos + by * k as f32 / 4.0)); + h.step(); + } + h.event(Event::PointerButton { pos: pos + by, button: PointerButton::Primary, pressed: false, modifiers }); + h.step(); + h.event(Event::ModifiersChanged(Modifiers::NONE)); + h.event(Event::PointerGone); + h.step(); + wait_probe(h); + h.run_steps(2); + }; + touch(&mut h, a, Modifiers::NONE, vec2(0.0, 0.0)); + assert_eq!(selection(&h, &ids), "c1_1", "the touch picked"); + touch(&mut h, b, Modifiers::SHIFT, vec2(0.0, 0.0)); + assert_eq!(selection(&h, &ids), "c1_1, c3_3", "Shift added"); + assert_eq!(selected_layers(&h), vec![ids.plate], "the viewer didn't toggle the layer"); + // A touch that drags moves nothing (and picks nothing). + touch(&mut h, a, Modifiers::NONE, vec2(120.0, 40.0)); + assert_eq!((position(&h, ids.plate), undo_steps(&h)), (before, steps + 2), "the layer stays put"); + assert_eq!(selection(&h, &ids), "c1_1, c3_3"); + let _ = std::fs::remove_dir_all(&ids.dir); +} From 691befb201660bdacf672d9a8ee3dbfa1cf6b02b Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:45:59 +0100 Subject: [PATCH 050/350] Cryptomatte selection: doc comments for the functions this change touches --- crates/ui-egui/src/panels/viewer.rs | 9 +++++++++ crates/ui-egui/tests/it/ui_crypto_pick_undo.rs | 2 ++ crates/ui-egui/tests/it/ui_crypto_select.rs | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index 5886f722e..05e9b23d7 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -96,6 +96,7 @@ struct Grabs { guides: Vec<(f32, bool)>, } +/// The egui data key of the viewer's [`Grabs`] for the current pass. fn grabs_id() -> egui::Id { egui::Id::unique("viewer-grabs") } @@ -1180,6 +1181,8 @@ fn led_by_a_moved_ancestor(comp: &Comp, layers: &[MoveLayer]) -> Vec { layers.iter().map(|l| led(l.0)).collect() } +/// Starts the gesture a new primary drag begins (pan, move, scale, a handle, a vertex, a marquee), +/// unless an overlay claimed the press. fn phase_dragstart(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let comp_rect = v.comp_rect; let zoom = v.zoom; @@ -1780,6 +1783,8 @@ fn phase_stopped(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, re v.pin_hits = pin_hits; } +/// Acts on a click in the viewer for the active tool (select, zoom, add or delete vertices), +/// including a click an overlay passed on, and not one an overlay claimed. fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let area = v.area; let zoom = v.zoom; @@ -1926,6 +1931,8 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response v.puppet_layers = puppet_layers; } +/// Acts on a double-click: a path's points into a Free Transform box, or a precomp opened; lets +/// go of an overlay's claim on the press once the button is up. fn phase_dblclick(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let map = v.map; let ctx = v.ctx.clone(); @@ -1986,6 +1993,8 @@ fn phase_dblclick(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Respons v.paths = paths; } +/// Runs the overlays drawn on top of the viewer (effect, gradient and text handles), after +/// recording the viewer's own controls for them, and draws the status chips. fn phase_tail(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, v: &mut Vw<'_>) { let t = v.t; let cid = v.cid; diff --git a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs index a35743ac0..7780dac0c 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs @@ -452,6 +452,8 @@ enum Step { Blur(u64, f64), } +/// Performs one model step (a pick, an edit, undo or redo, applying or removing the effect) and +/// records the state the model expects after it. fn run_step(h: &mut Harness<'_, EffectcraftApp>, f: &Fix, m: &mut Model, size: [f64; 2], step: &Step, n: usize) { let mut o = m.state().clone(); let before = h.state().session.history.undo.len(); diff --git a/crates/ui-egui/tests/it/ui_crypto_select.rs b/crates/ui-egui/tests/it/ui_crypto_select.rs index b1b716e24..9331913d4 100644 --- a/crates/ui-egui/tests/it/ui_crypto_select.rs +++ b/crates/ui-egui/tests/it/ui_crypto_select.rs @@ -73,6 +73,7 @@ fn on_plate(h: &Harness<'_, EffectcraftApp>, x: f64, y: f64) -> Pos2 { comp_to_screen(&h.ctx, [(80.0 + x) as f32, y as f32]).unwrap() } +/// Moves the pointer to `pos`, waits until the Cryptomatte IDs are read, and returns the chip's text. fn hover(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2) -> Option { h.event(Event::PointerMoved(pos)); // The overlay comes up over the layer in this pass and is hovered from the next one on. @@ -97,6 +98,7 @@ fn click(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2, modifiers: Modifiers) { h.run_steps(2); } +/// Presses at `from`, drags by `by` over a few passes and releases. fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, by: egui::Vec2) { h.event(Event::PointerMoved(from)); h.step(); @@ -110,6 +112,7 @@ fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, by: egui::Vec2) { h.run_steps(3); } +/// The plate's Cryptomatte Selection text. fn selection(h: &Harness<'_, EffectcraftApp>, ids: &Ids) -> String { let s = &h.state().session; match &s.active_comp().unwrap().layer(LayerId(ids.plate)).unwrap().props.find(ids.sel).unwrap().value { @@ -118,15 +121,18 @@ fn selection(h: &Harness<'_, EffectcraftApp>, ids: &Ids) -> String { } } +/// The layer's Position value, formatted for comparison. fn position(h: &Harness<'_, EffectcraftApp>, layer: u64) -> String { let l = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().clone(); format!("{:?}", l.transform().and_then(|t| t.get("position")).map(|p| p.value.clone())) } +/// How many steps the undo history holds. fn undo_steps(h: &Harness<'_, EffectcraftApp>) -> usize { h.state().session.history.undo.len() } +/// The ids of the selected layers. fn selected_layers(h: &Harness<'_, EffectcraftApp>) -> Vec { h.state().session.state.selected_layers.iter().map(|l| l.0).collect() } From 12264b4e6380bb9104f03bd3a9105c03911f1316 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:47:55 +0100 Subject: [PATCH 051/350] Doc comments for the functions this PR touches --- crates/ui-egui/src/panels/effect_controls/common.rs | 1 + crates/ui-egui/tests/it/ui_effect_controls.rs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/crates/ui-egui/src/panels/effect_controls/common.rs b/crates/ui-egui/src/panels/effect_controls/common.rs index 1e7d7cba1..31e884b98 100644 --- a/crates/ui-egui/src/panels/effect_controls/common.rs +++ b/crates/ui-egui/src/panels/effect_controls/common.rs @@ -40,6 +40,7 @@ pub(crate) fn shown_layer_id(app: &EffectcraftApp) -> Option Option { let id = shown_layer_id(app)?; app.session.active_comp()?.layer(id).cloned() diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index ec7d5683d..b79aade0d 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -936,6 +936,7 @@ fn shows_effect(h: &Harness<'_, EffectcraftApp>, fx: u64) -> bool { h.state().auto.elements.iter().any(|e| e.id == id) } +/// Selects `layer` through the `layer.select` command and runs a few frames. fn select_layer(h: &mut Harness<'_, EffectcraftApp>, layer: u64) { h.state_mut().session.execute("layer.select", json!({"layers": [layer]})).unwrap(); h.run_steps(3); @@ -1018,6 +1019,7 @@ fn replacing_the_project_unlocks_effect_controls() { #[test] fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request that locks Effect Controls and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); @@ -1048,6 +1050,7 @@ fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { #[test] fn a_lock_request_while_locked_in_another_comp_fails() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request that locks Effect Controls and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); @@ -1079,6 +1082,7 @@ fn a_lock_request_while_locked_in_another_comp_fails() { #[test] fn a_lock_request_after_a_project_replacement_does_not_report_the_old_lock() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request that locks Effect Controls and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); @@ -1129,6 +1133,7 @@ fn applying_an_effect_while_locked_keeps_the_selection() { #[test] fn effect_controls_lock_over_the_control_channel() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request with `params` and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>, params: serde_json::Value) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", params); From 6739afd3d6456eb25c18681b6def37fba5290bbd Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:48:27 +0100 Subject: [PATCH 052/350] Doc comments for the functions this PR touches --- crates/engine/src/tests/track.rs | 1 + crates/script/src/model.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/engine/src/tests/track.rs b/crates/engine/src/tests/track.rs index bd1adaeca..52c8a2bc0 100644 --- a/crates/engine/src/tests/track.rs +++ b/crates/engine/src/tests/track.rs @@ -440,6 +440,7 @@ fn analyze_1080p_single_point_speed() { struct SampleFootage; impl crate::render::ExprHost for SampleFootage { + /// Reads the red channel of the layer's footage pixel at (0, 0) through the context's footage source, scaled to 0-100. fn eval( &self, ctx: &EvalCtx, diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index 694d82d39..a7476f370 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -510,6 +510,7 @@ fn text_json(d: &TextDoc) -> J { doc } +/// Returns the property's value at the requested time, evaluating its expression unless `pre` is set. fn value(s: &Session, a: &J) -> R { let (cid, c, l) = layer_of(s, a)?; let uid = u(a, "uid").ok_or("missing uid")?; From 3ab35d0ff656530cd873c5cf293514ddfaa8b4b3 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:49:24 +0100 Subject: [PATCH 053/350] Doc comments for the functions this PR touches --- crates/engine/src/cmd/prop.rs | 1 + crates/ui-egui/src/num_math.rs | 7 +++++++ crates/ui-egui/src/panels/effect_controls/rows.rs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index 028fdb0dd..dec791b88 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -1457,6 +1457,7 @@ fn convert_one_expr(s: &mut Session, cid: ItemId, lid: LayerId, uid: Uid) -> Res }) } +/// Returns the command specs for the property commands (get, set, keyframes, expressions and the rest). pub fn specs() -> Vec { vec![ query!("prop.get", "Get Property", "{layer?, path|prop, time? (comp s)}", get), diff --git a/crates/ui-egui/src/num_math.rs b/crates/ui-egui/src/num_math.rs index 3e329c9e4..68225511c 100644 --- a/crates/ui-egui/src/num_math.rs +++ b/crates/ui-egui/src/num_math.rs @@ -126,6 +126,8 @@ struct Parser<'a> { } impl Parser<'_> { + /// Reads a sum or difference of terms, left to right. + /// Returns `None` on a syntax error or a non-finite result. fn expr(&mut self) -> Option { let mut v = self.term()?; while let Some(&op) = self.chars.peek() { @@ -144,6 +146,8 @@ impl Parser<'_> { Some(v) } + /// Reads a product or quotient of factors, left to right. + /// Returns `None` on a syntax error, a division by zero or a non-finite result. fn term(&mut self) -> Option { let mut v = self.factor()?; while let Some(&op) = self.chars.peek() { @@ -162,6 +166,8 @@ impl Parser<'_> { Some(v) } + /// Reads a signed factor, a bracketed expression or a number. + /// Nesting deeper than `MAX_DEPTH` gives `None`. fn factor(&mut self) -> Option { let c = *self.chars.peek()?; match c { @@ -239,6 +245,7 @@ impl Parser<'_> { mod tests { use super::calc as eval; + /// Evaluates `text` with no current value, so a leading operator has nothing to apply to. fn abs(text: &str) -> Option { eval(text, None) } diff --git a/crates/ui-egui/src/panels/effect_controls/rows.rs b/crates/ui-egui/src/panels/effect_controls/rows.rs index e2634d6b8..b21021bb3 100644 --- a/crates/ui-egui/src/panels/effect_controls/rows.rs +++ b/crates/ui-egui/src/panels/effect_controls/rows.rs @@ -260,6 +260,7 @@ fn value_mask(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: & } } +/// Draws an angle property's value cell as revolutions plus degrees and queues a set action when it is edited. fn value_angle(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: &Value, actions: &mut Actions) { let Value::Scalar(v) = value else { return; @@ -275,6 +276,7 @@ fn value_angle(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: cx.twirl_open(app, ui); } +/// Draws a single-number property's value cell with its range, unit suffix and scrub speed, and queues a set action when it is edited. fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: &Value, actions: &mut Actions) { let Value::Scalar(v) = value else { return; @@ -308,6 +310,8 @@ fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: } } +/// Draws one numeric cell per component of a point or vector property, with the pick crosshair for points. +/// Returns the cell rectangles. fn value_vector(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: &Value, actions: &mut Actions) -> Vec { let mut dims = vec![]; let PropCx { t, uid, cy, vx, layer, prop, .. } = *cx; From 2a00e26be5f6ea39fcfc007ad420e0d7f1e61780 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:50:27 +0100 Subject: [PATCH 054/350] Doc comments for the functions this PR touches --- crates/render/src/cache/dependencies.rs | 2 ++ crates/render/src/pipeline.rs | 2 ++ crates/render/src/tests/frame_blend.rs | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/crates/render/src/cache/dependencies.rs b/crates/render/src/cache/dependencies.rs index c8c483f99..c1ff20565 100644 --- a/crates/render/src/cache/dependencies.rs +++ b/crates/render/src/cache/dependencies.rs @@ -122,6 +122,8 @@ impl Dependencies<'_> { Some(()) } + /// Folds what the layer contributes at this time into the dependency cache key, including its source, effects and frame blend state. + /// Returns `None` when the layer cannot be keyed: past the depth limit, in a reference cycle, or with expressions that read other times. fn layer(&mut self, ctx: &EvalCtx, layer: &Layer) -> Option<()> { let id = (ctx.comp_id, layer.id); if self.path.len() > crate::MAX_FX_DEPTH || self.path.contains(&id) { diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index 6012a7404..89ce84f7f 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -861,6 +861,8 @@ impl<'a> Renderer<'a> { if per_pixel { Some(Arc::new(interpret_pixels(&img, f, self.pipe.linear_footage_space(f)))) } else { Some(img) } } + /// The footage frame at source time `t` after frame blending: the average of the source frames one comp frame spans, else a blend of the two neighbouring frames, else the plain frame. + /// Log scans are converted first, and blended frames are cached. fn footage_frame_raw(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option> { let mode = frame_blend_mode(ctx, layer); let float = self.pipe.float_curve(f); diff --git a/crates/render/src/tests/frame_blend.rs b/crates/render/src/tests/frame_blend.rs index e80731d35..ecee73b6d 100644 --- a/crates/render/src/tests/frame_blend.rs +++ b/crates/render/src/tests/frame_blend.rs @@ -58,6 +58,7 @@ impl FootageSource for Frames { } } +/// [`setup_rated`] with 24 fps footage and no recorded native rate. fn setup(mode: FrameBlend, size: (u32, u32)) -> (Project, ItemId) { setup_rated(mode, size, FrameRate::FPS_24, None) } @@ -174,6 +175,7 @@ fn time_stretched_precomp_frame_mixes() { assert!(img.get(12, 10)[3] < 1e-3 && (img.get(17, 10)[3] - 1.0).abs() < 1e-3 && (img.get(22, 10)[3] - 1.0).abs() < 1e-3 && img.get(27, 10)[3] < 1e-3); } +/// Fast-conformed footage (30 fps interpreted as 300 fps) averages every source frame a comp frame spans, for Frame Mix and Pixel Motion. #[test] fn frame_mix_averages_every_frame_of_fast_conformed_footage() { // #3180: 30 fps footage interpreted as 300 fps in a 30 fps comp. Every comp frame lands @@ -195,6 +197,7 @@ fn frame_mix_averages_every_frame_of_fast_conformed_footage() { assert!(frame(&p, cid, 0, false).get(4, 4)[0].abs() < 1e-6); } +/// Footage at the comp's rate, or only mildly faster, renders as before. #[test] fn frame_mix_of_common_footage_is_unchanged() { // Footage at the comp's rate: each comp frame is exactly one source frame, blended or not. @@ -226,6 +229,7 @@ fn remap(p: &mut Project, cid: ItemId, keys: Vec) { p.comp_mut(cid).unwrap().layers[0].props.children.push(effectcraft_project::Node::Prop(tr)); } +/// Time remap holds and cuts do not smear: a still frame stays a single source frame. #[test] fn time_remap_jumps_stay_crisp() { let fast = FrameRate::new(300, 1); @@ -253,6 +257,7 @@ fn time_remap_jumps_stay_crisp() { assert!((v - 0.45).abs() < 1e-5, "steady remap: {v}"); } +/// A frame-blended dependency changes its owner's cache key when its source goes from holding to running at speed. #[test] fn a_frame_blended_dependency_keys_how_far_its_source_runs() { // A footage layer selected by another layer's effect: at comp frames 0 and 1 its source time @@ -276,6 +281,7 @@ fn a_frame_blended_dependency_keys_how_far_its_source_runs() { assert_ne!(key(0), key(1)); } +/// The mix window is the source span of one comp frame, and falls back to the two-frame blend below four source frames per comp frame. #[test] fn mix_window_covers_the_source_span_of_one_comp_frame() { let comp_frame = |r: FrameRate| r.frame_duration(); From 7dc345c4c2343f4e17b8cd0ea2a6b616e3c19a87 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:50:40 +0100 Subject: [PATCH 055/350] Doc comments for the functions this PR touches --- crates/script/src/model.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index 1140820c6..3cf25ed81 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -32,6 +32,7 @@ fn layer_of<'a>(s: &'a Session, a: &J) -> Result<(ItemId, &'a Comp, &'a Layer), Ok((cid, c, l)) } +/// Answers a script model query of the given `kind` against the session, dispatching to the matching reader, and errors on an unknown kind. pub fn query(s: &mut Session, kind: &str, a: &J) -> R { match kind { "project" => Ok(project(s)), From 25a6a76c47e39e588a3f2655d9ad434bcc56ea1c Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:50:42 +0100 Subject: [PATCH 056/350] Doc comments for the functions this PR touches --- crates/engine/src/tests/viewer.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index d2c2180c1..431b9e735 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -194,6 +194,7 @@ fn circles_snap_point_to_point(bezier: bool) { assert_ne!(a, b, "two shape layers"); if bezier { for l in [a, b] { + /// Returns the uid of the first ellipse group in the tree, searching depth first. fn ellipse(g: &effectcraft_project::PropGroup) -> Option { if g.match_id == "ellipse" { return Some(g.uid); From 99e24f5867e1460be9a820f1558cc1fce6134a11 Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Sun, 8 Nov 2026 01:33:26 +0100 Subject: [PATCH 057/350] .aep import: image sequences without a filename prefix or with Force alphabetical order (#3213) * AEP import: read image sequences without a filename prefix and with Force alphabetical order (#3174) After Effects leaves out an empty filename prefix, so only one Utf8 (the suffix, such as `.png`) follows the alias. The single part is now placed by the first file name stored in `opti`: a name starting with it has it as the prefix, one ending with it as the suffix; without that evidence a part starting with `.` is the suffix and anything else is not read. A sequence imported with Force alphabetical order stores its files in `LIST StVc` (`StVS` with the count as a little-endian u32, then one Utf8 per file name) instead of prefix and suffix; that list now replaces the numbering. A list with a name that is not a plain file name (a separator, a colon, `.` or `..`) is not read, since the names are joined to the folder. A folder alias whose naming still cannot be read is reported as an import loss instead of silently linking the folder. docs/aep-format.md describes the empty prefix and StVc/StVS. * AEP import: a listed sequence file name with a control character such as NUL is not read (#3174) A NUL or another control character in a Force alphabetical order file name now makes the whole list unreadable, like a separator or a dot directory, so the import reports the naming instead of linking files that cannot exist. * AEP import: a sequence file list without a readable count is not read (#3174) After Effects writes the StVS count before the names. A StVc list without StVS, or with one shorter than its u32, is now reported as unreadable naming instead of taking every listed name. * Doc comments for the functions this PR touches --- crates/aep/src/container/footage.rs | 89 ++++++++++++- crates/aep/src/import/build.rs | 36 ++++-- crates/aep/tests/it/import/synthetic.rs | 161 ++++++++++++++++++++++++ docs/aep-format.md | 5 +- 4 files changed, 274 insertions(+), 17 deletions(-) diff --git a/crates/aep/src/container/footage.rs b/crates/aep/src/container/footage.rs index 3c9148155..da0369d64 100644 --- a/crates/aep/src/container/footage.rs +++ b/crates/aep/src/container/footage.rs @@ -2,6 +2,7 @@ //! of a layer's source. use crate::container::bytes::{be_f32, be_f64, be_u32, cstr, le_u32, text}; +use crate::container::chunk::Chunk; use crate::container::header::Rational; use crate::container::typed::{Item, ItemKind, Layer, Project}; @@ -159,19 +160,25 @@ fn json_string_at(json: &[u8], mut i: usize) -> Option { } } -/// The numbering of an image sequence item (353, 603). +/// The numbering of an image sequence item (353, 603), or its file list when it was imported with +/// Force alphabetical order. #[derive(Debug, Clone, PartialEq, Eq)] pub struct SequenceInfo { /// The file name before the frame number (`seq_`, `shot_`): the first `Utf8` after the alias. + /// An empty prefix is not stored (`0001.png`): the one `Utf8` is then the suffix. pub prefix: String, /// The file name after the frame number (`.png`, `.exr`): the second `Utf8` after the alias. pub suffix: String, /// The first and last frame number (`sspc` `u32` at 172 and 176: 1 and 3 in 353, 101 and 103 - /// in 603). + /// in 603; both `0xFFFFFFFF` for a sequence with a file list). pub start: u32, pub end: u32, /// The digits of the frame number (`sspc` `u32` at 180: 3 for `seq_001`, 4 for `shot_0101`). pub digits: u32, + /// The file names in order for a sequence imported with Force alphabetical order (`LIST StVc` + /// after the alias: `StVS` with the count as a little-endian `u32`, then one `Utf8` per file); + /// empty for a numbered sequence. When set, it replaces the numbering. + pub files: Vec, } impl SequenceInfo { @@ -180,6 +187,62 @@ impl SequenceInfo { let width = usize::try_from(self.digits.min(16)).unwrap_or(0); format!("{}{n:0width$}{}", self.prefix, self.suffix) } + + /// The number of files: the listed files, or the frame numbers from start to end (0 when the + /// end lies before the start). + pub fn frame_count(&self) -> u32 { + if self.files.is_empty() { + self.end.checked_sub(self.start).map_or(0, |d| d.saturating_add(1)) + } else { + u32::try_from(self.files.len()).unwrap_or(u32::MAX) + } + } + + /// The name of the `i`-th file (from 0): from the file list, or numbered from `start`; `None` + /// past the last file. + pub fn nth_file_name(&self, i: u32) -> Option { + if self.files.is_empty() { + let n = self.start.checked_add(i).filter(|n| *n <= self.end)?; + Some(self.file_name(n)) + } else { + self.files.get(usize::try_from(i).ok()?).cloned() + } + } +} + +/// The file names of a `LIST StVc` (Force alphabetical order): the `Utf8` children in order, at most +/// as many as the count in `StVS` (little-endian `u32`) says. Empty names count toward that number +/// and are then skipped. Without a readable count (no `StVS`, or one shorter than 4 bytes) the list +/// is unreadable (empty). A name that is not a plain file name (holding `/`, `\`, `:` or a control +/// character such as NUL, or being `.` or `..`) makes the whole list unreadable (empty): the names +/// are joined to the folder. +fn sequence_file_list(list: &Chunk<'_>) -> Vec { + // After Effects writes the count first (the one sample); a list without a readable count is + // not read rather than guessed. + let Some(count) = list.child(b"StVS").and_then(|c| c.bytes()).and_then(|b| le_u32(b, 0)).and_then(|n| usize::try_from(n).ok()) else { + return Vec::new(); + }; + let names: Vec = + list.children().iter().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).take(count).map(text).filter(|n| !n.is_empty()).collect(); + if names.iter().any(|n| n.contains(|c: char| matches!(c, '/' | '\\' | ':') || c.is_control()) || n == "." || n == "..") { + return Vec::new(); + } + names +} + +/// The prefix and suffix of a sequence whose naming holds one `Utf8` (`part`): After Effects leaves +/// out an empty prefix (`0001.png` stores only `.png`). The stored first file name (`opti`) decides: +/// a name starting with `part` has it as the prefix, one ending with it as the suffix. Without that +/// evidence, a `part` starting with `.` is the suffix (an extension); anything else is not read. +fn single_name_part(part: &str, first_file: Option<&str>) -> Option<(String, String)> { + if let Some(first) = first_file.filter(|_| !part.is_empty()) { + match (first.starts_with(part), first.ends_with(part)) { + (true, false) => return Some((part.to_string(), String::new())), + (false, true) => return Some((String::new(), part.to_string())), + _ => {} + } + } + part.starts_with('.').then(|| (String::new(), part.to_string())) } impl<'f, 'a> Item<'f, 'a> { @@ -194,10 +257,24 @@ impl<'f, 'a> Item<'f, 'a> { let sspc = pin.child(b"sspc")?.bytes()?; let kids = pin.children(); let after = kids.iter().position(|c| c.is_list(b"Als2"))?; - let mut names = kids.iter().skip(after + 1).take_while(|c| !c.is(b"opti")).filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).map(text); - let prefix = names.next()?; - let suffix = names.next()?; - Some(SequenceInfo { prefix, suffix, start: be_u32(sspc, 172)?, end: be_u32(sspc, 176)?, digits: be_u32(sspc, 180)? }) + let naming = || kids.iter().skip(after.saturating_add(1)).take_while(|c| !c.is(b"opti")); + let (start, end, digits) = (be_u32(sspc, 172)?, be_u32(sspc, 176)?, be_u32(sspc, 180)?); + // Force alphabetical order: the file list replaces the numbering. + if let Some(list) = naming().find(|c| c.is_list(b"StVc")) { + let files = sequence_file_list(list); + if files.is_empty() { + return None; + } + return Some(SequenceInfo { prefix: String::new(), suffix: String::new(), start, end, digits, files }); + } + let names: Vec = naming().filter(|c| c.is(b"Utf8")).filter_map(|c| c.bytes()).map(text).take(2).collect(); + let (prefix, suffix) = match names.as_slice() { + [prefix, suffix] => (prefix.clone(), suffix.clone()), + // An empty prefix is not stored (`0001.png` has only `.png`). + [one] => single_name_part(one, f.name.as_deref())?, + _ => return None, + }; + Some(SequenceInfo { prefix, suffix, start, end, digits, files: Vec::new() }) } /// The rate a clip is conformed to (Interpret Footage ▸ Conform to frame rate; `sspc` `u32` at diff --git a/crates/aep/src/import/build.rs b/crates/aep/src/import/build.rs index 4ec346328..48ae1c1a5 100644 --- a/crates/aep/src/import/build.rs +++ b/crates/aep/src/import/build.rs @@ -206,6 +206,8 @@ fn new_comp(h: &CompHeader, renderer: Option, map: &TimeMap, name: comp } +/// Adds one footage item to the project under the given parent. +/// Unreadable footage is recorded as a loss, solids and null placeholders are handled separately, and file and sequence footage is linked to its files. fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_>, name: String, label: Label, parent: ItemId, null_only: &HashSet) { let Some(f) = item.footage() else { cx.col.lose(LossCategory::DamagedData, format!("footage '{name}': data unreadable, not imported")); @@ -240,31 +242,42 @@ fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_ } let mut path = f.path.clone().unwrap_or_default(); // An image sequence's alias is its folder; its files are named by the stored numbering - // (353, 603). The first file stands for the sequence, as in EffectCraft. + // (353, 603), or listed one by one when imported with Force alphabetical order. The first + // file stands for the sequence, as in EffectCraft. let mut sequence = Vec::new(); - if let Some(seq) = item.sequence_info().filter(|s| !path.is_empty() && s.end >= s.start) { - let frames = (seq.end - seq.start).saturating_add(1); + if let Some(seq) = item.sequence_info().filter(|s| !path.is_empty() && s.frame_count() > 0) { + let frames = seq.frame_count(); let sep = if path.contains('\\') { '\\' } else { '/' }; let folder = path.trim_end_matches(['\\', '/']).to_string(); - // One name is the folder, a separator and the file name; the number is as wide as the stored - // digits or as the largest number, whichever is longer (an upper bound for every frame). - let name_bytes = (folder.len() + 1 + seq.prefix.len() + seq.suffix.len() + usize::try_from(seq.digits.min(16)).unwrap_or(16).max(10)) as u64; - let bytes = name_bytes.saturating_mul(u64::from(frames)); + // One name is the folder, a separator and the file name. A numbered name is as wide as the + // stored digits or as the largest number, whichever is longer (an upper bound for every + // frame); listed names are counted as they are. + let len = |n: usize| u64::try_from(n).unwrap_or(u64::MAX); + let file_bytes = if seq.files.is_empty() { + len(seq.prefix.len().saturating_add(seq.suffix.len()).saturating_add(usize::try_from(seq.digits.min(16)).unwrap_or(16).max(10))) + .saturating_mul(u64::from(frames)) + } else { + seq.files.iter().fold(0u64, |sum, n| sum.saturating_add(len(n.len()))) + }; + let bytes = len(folder.len().saturating_add(1)).saturating_mul(u64::from(frames)).saturating_add(file_bytes); if frames > MAX_SEQUENCE_FRAMES || frames > cx.sequence_budget || bytes > cx.sequence_bytes { // Not expanded: the item keeps its first file (a still standing for the sequence). cx.col.lose( LossCategory::DamagedData, format!("footage '{name}': a sequence of {frames} frames is longer than the import expands; only its first file is linked"), ); - path = format!("{folder}{sep}{}", seq.file_name(seq.start)); + path = format!("{folder}{sep}{}", seq.nth_file_name(0).unwrap_or_default()); } else { cx.sequence_budget -= frames; cx.sequence_bytes -= bytes; - sequence = (seq.start..=seq.end).map(|n| format!("{folder}{sep}{}", seq.file_name(n))).collect(); + sequence = (0..frames).filter_map(|i| seq.nth_file_name(i)).map(|n| format!("{folder}{sep}{n}")).collect(); } if let Some(first) = sequence.first() { path = first.clone(); } + } else if f.is_sequence && !path.is_empty() { + // The alias is a folder, but the file names could not be read: the item links the folder. + cx.col.lose(LossCategory::DamagedData, format!("footage '{name}': image sequence naming not read, linked to its folder")); } let present = !path.is_empty() && (cx.options.exists)(&path); if !present { @@ -318,6 +331,11 @@ fn import_footage(project: &mut Project, cx: &mut Ctx<'_>, item: &AepItem<'_, '_ /// sequence the name with the frame range (`shot_[0101-0103].exr`). fn default_footage_name(item: &AepItem<'_, '_>, f: &crate::Footage) -> Option { if let Some(s) = item.sequence_info() { + // A listed sequence (Force alphabetical order) has no numbering: its first file names it. + // Assumption: what After Effects shows for such an item without a stored name was not sampled. + if let Some(first) = s.files.first() { + return Some(first.clone()); + } let w = usize::try_from(s.digits.min(16)).unwrap_or(0); return Some(format!("{}[{:0w$}-{:0w$}]{}", s.prefix, s.start, s.end, s.suffix)); } diff --git a/crates/aep/tests/it/import/synthetic.rs b/crates/aep/tests/it/import/synthetic.rs index 18709f9e5..5d4d89c56 100644 --- a/crates/aep/tests/it/import/synthetic.rs +++ b/crates/aep/tests/it/import/synthetic.rs @@ -77,6 +77,167 @@ fn a_sequence_with_long_names_is_not_expanded_past_the_byte_budget() { assert_eq!(longest, 0, "the sequence was not expanded"); } +/// The project with the sequence's naming (the chunks between `Als2` and `opti` in its `Pin `) +/// replaced by `naming`, and the `sspc` numbering (start, end, digits at 172, 176, 180) set to +/// `numbering` when given. +fn with_sequence_naming(naming: &[Chunk<'static>], numbering: Option<[u32; 3]>) -> Vec { + let mut file: AepFile<'_> = parse(SYNTHETIC).unwrap(); + let mut patched = 0; + visit(&mut file.chunks, &mut |c| { + if !c.is_list(b"Pin ") { + return; + } + let Body::List { children, .. } = &mut c.body else { return }; + let Some(als) = children.iter().position(|k| k.is_list(b"Als2")) else { return }; + let Some(opti) = children.iter().position(|k| k.is(b"opti")) else { return }; + assert!(als < opti); + let tail = children.split_off(opti); + children.truncate(als + 1); + children.extend(naming.iter().map(shorten)); + children.extend(tail); + if let Some(numbering) = numbering { + let sspc = children.iter_mut().find(|k| k.is(b"sspc")).unwrap(); + for (at, v) in [172, 176, 180].into_iter().zip(numbering) { + set_data(sspc, at, &v.to_be_bytes()); + } + } + patched += 1; + }); + assert_eq!(patched, 1, "the synthetic project has one sequence"); + write(&file).unwrap() +} + +/// `bytes` with the first file name the sequence stores in its png `opti` (byte 58, NUL-terminated) +/// set to `name`. +fn with_first_file(bytes: &[u8], name: &str) -> Vec { + let mut file: AepFile<'_> = parse(bytes).unwrap(); + let mut patched = 0; + visit(&mut file.chunks, &mut |c| { + if c.is(b"opti") && c.bytes().is_some_and(|d| d.get(..4) == Some(&b"png!"[..])) { + let mut z = name.as_bytes().to_vec(); + z.push(0); + set_data(c, 58, &z); + patched += 1; + } + }); + assert_eq!(patched, 1, "the synthetic project has one png item"); + write(&file).unwrap() +} + +/// The imported sequence item's footage. +fn the_footage(p: &Project) -> effectcraft_project::Footage { + p.items.values().find_map(|i| if let ItemKind::Footage(f) = &i.kind { Some(f.clone()) } else { None }).expect("the sequence") +} + +/// The file names (without the folder) of a footage's sequence. +fn file_names(f: &effectcraft_project::Footage) -> Vec { + f.sequence.iter().map(|s| s.rsplit(['\\', '/']).next().unwrap_or_default().to_string()).collect() +} + +/// A copy of `c` that borrows for any lifetime (a chunk is covariant in its lifetime). +fn shorten<'a>(c: &Chunk<'static>) -> Chunk<'a> { + c.clone() +} + +/// A `Utf8` string chunk holding `s`. +fn utf8(s: &str) -> Chunk<'static> { + Chunk::data(*b"Utf8", s.as_bytes().to_vec()) +} + +/// A sequence whose naming has only a suffix (no filename prefix) imports as a sequence of its files. +#[test] +fn a_sequence_without_a_filename_prefix_imports_its_files() { + // `0001.png` .. `0003.png`: After Effects omits the empty prefix, so only `.png` follows the + // alias. Before, `.png` was read as the prefix, the suffix was missing and the item became a + // video linked to its folder. + let (p, r) = run(&with_sequence_naming(&[utf8(".png")], None), true); + r.unwrap(); + let f = the_footage(&p); + assert_eq!(f.kind, FootageKind::Sequence); + assert_eq!(file_names(&f), ["0001.png", "0002.png", "0003.png"]); + assert!(f.path.ends_with("0001.png"), "{}", f.path); +} + +/// A single `Utf8` naming part is read as the suffix or the prefix depending on how the stored first file name matches it. +#[test] +fn a_single_naming_part_is_placed_by_the_stored_first_file_name() { + // `0001_v2.png` ..: the one `Utf8` is the suffix, as the first file name (`opti`) ends with it. + let bytes = with_first_file(&with_sequence_naming(&[utf8("_v2.png")], None), "0001_v2.png"); + let (p, r) = run(&bytes, true); + r.unwrap(); + let f = the_footage(&p); + assert_eq!(f.kind, FootageKind::Sequence); + assert_eq!(file_names(&f), ["0001_v2.png", "0002_v2.png", "0003_v2.png"]); + // `shot_0001` ..: a name without an extension; the one `Utf8` is the prefix. + let bytes = with_first_file(&with_sequence_naming(&[utf8("shot_")], None), "shot_0001"); + let (p, r) = run(&bytes, true); + r.unwrap(); + assert_eq!(file_names(&the_footage(&p)), ["shot_0001", "shot_0002", "shot_0003"]); + // Without that evidence (the stored first file is `shot_0001.png`), a part that is no extension + // is not guessed: the item is reported. + let bytes = with_sequence_naming(&[utf8("_v2.png")], None); + reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder"); +} + +/// A file list containing a name with a separator, drive colon, dot directory or control character is refused and reported. +#[test] +fn a_file_list_with_a_name_that_is_not_a_plain_file_name_is_reported() { + // The listed names are joined to the folder: a separator, a drive colon or a dot directory + // would point outside it, and a NUL or another control character is no file name, so such a + // list is not read. + for hostile in ["..\\..\\x.png", "../x.png", "C:x.png", "..", ".", "x\0.png", "x\n.png"] { + let list = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 2u32.to_le_bytes().to_vec()), utf8("a.png"), utf8(hostile)]); + let bytes = with_sequence_naming(&[list], Some([u32::MAX, u32::MAX, 0])); + reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder"); + } +} + +/// A file list with a missing or too short count chunk is refused and reported. +#[test] +fn a_file_list_without_a_readable_count_is_reported() { + // No `StVS`, or one too short for its `u32`: the list is not read rather than taken whole. + for count in [None, Some(vec![2u8, 0])] { + let mut kids: Vec> = count.map(|c| Chunk::data(*b"StVS", c)).into_iter().collect(); + kids.extend([utf8("a.png"), utf8("b.png")]); + let bytes = with_sequence_naming(&[Chunk::list(*b"StVc", kids)], Some([u32::MAX, u32::MAX, 0])); + reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder"); + } +} + +/// A force-alphabetical sequence imports its stored file list in the stored order. +#[test] +fn a_sequence_imported_with_force_alphabetical_order_imports_its_file_list() { + // Force alphabetical order: `LIST StVc` with `StVS` (the count, little-endian) and one `Utf8` + // per file instead of the prefix and suffix; the numbering in `sspc` is not set. Before, the + // item became a video linked to its folder. + let list = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 3u32.to_le_bytes().to_vec()), utf8("b_take.png"), utf8("a_take.png"), utf8("c_take.png")]); + let (p, r) = run(&with_sequence_naming(&[list], Some([u32::MAX, u32::MAX, 0])), true); + let r = r.unwrap(); + let f = the_footage(&p); + assert_eq!(f.kind, FootageKind::Sequence); + // The stored order is kept. + assert_eq!(file_names(&f), ["b_take.png", "a_take.png", "c_take.png"]); + assert!(f.path.ends_with("b_take.png"), "{}", f.path); + assert!(losses(&r, LossCategory::DamagedData).is_empty(), "{:?}", r.report.losses); + // The count bounds the list. + let list = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 2u32.to_le_bytes().to_vec()), utf8("x.png"), utf8("y.png"), utf8("z.png")]); + let (p, r) = run(&with_sequence_naming(&[list], Some([u32::MAX, u32::MAX, 0])), true); + r.unwrap(); + assert_eq!(file_names(&the_footage(&p)), ["x.png", "y.png"]); +} + +/// A sequence with no naming or an empty file list is reported and linked to its folder. +#[test] +fn a_sequence_whose_naming_cannot_be_read_is_reported() { + // No naming at all after the alias. Before, the item silently linked its folder. + let bytes = with_sequence_naming(&[], None); + reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder"); + // An empty file list is no naming either. + let empty = Chunk::list(*b"StVc", vec![Chunk::data(*b"StVS", 0u32.to_le_bytes().to_vec())]); + let bytes = with_sequence_naming(&[empty], Some([u32::MAX, u32::MAX, 0])); + reported_and_refused(&bytes, LossCategory::DamagedData, "image sequence naming not read, linked to its folder"); +} + #[test] fn a_solid_with_a_nan_channel_is_repaired_and_reported() { let nan = f32::NAN.to_be_bytes(); diff --git a/docs/aep-format.md b/docs/aep-format.md index d6ec7fb14..99a1199f0 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -1,6 +1,6 @@ # After Effects project format (`.aep`) -> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-05 · **Change:** versions: `svap` is the version that wrote the file, `head` bytes 4 to 7 the version it is written for, and what a Save a Copy As 25.x / 24.x of After Effects 2026 changes (section 1, "Versions"); before that: the stretch numerator is signed (a negative one reverses the layer, 981-983); layer styles (3.12b, 950-980); before that: footage: `sspc@118` of an image sequence is its folder's modification time (860, read-back x1-x3); before that: text: paragraph `/24`, `/35` and style `/68` are neutral for 0 and 1 (732-734, edited in place); before that: keyframes: the ease of mask path keys (599, 700), the spatial speed unit of fractional values (597, 598, 730); before that: text: character and paragraph settings, manual kerning, vertical text, `/nil` is neutral (S17: 538-578, 595, 596); before that: cameras, lights and 3D rotations (S16: the auto Position of a two-node camera is where After Effects renders it, 537; camera and light defaults read back 255, 256; Orientation `otst/otda` 488), Timewarp (529-533), an expression on an effect parameter (534); before that: text: style slot /13 is superscript / subscript (526-528; every scripted text of the corpus is superscript), wrapping box text 517; before that: effects of batch 5 (409-450, 511-516: integer sliders, checkbox defaults in `pard`, arbitrary data of Hue/Saturation and Levels, the parameters of Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow; section 3.7); before that: shape items of batch 5 (451-484, 519: ellipse, fill and stroke opacity, fill rule, group transform, shape layer anchor, trim paths, rectangle roundness, polystar fields, gradient default, nested groups, the defaults of every item; section 3.9); before that: track mattes: mode at `ldta` byte 107 and the matte layer id at byte 160 (cases 402-408, section 3.1); before that: text: automatic leading is verified (400); Exposure (`ADBE Exposure2`, cases 389-397): group start / end parameter types 13 / 14, float slider valid minimum and slider range (section 3.7); before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: the XML form `.aepx` is read into the same chunk tree (section 1, "The XML form"); the `head` save counter is a `u32` at byte 16; text documents for the importer (stroke colour, automatic leading, the font of each run, the undecoded-slot check, the text-layer Anchor Point, the Source Text record); before that: third round of verified field layouts (explicit-default semantics, type-major bezier ease, exact time stretch and in / out points, Time Remapping state, comp pixel aspect / shutter phase / display start, Point default, Layer Control None, dropdown selection, one `mkif` per mask, polystar and radial gradient records, text styles, camera and light options, marker cue data, audio and image-sequence footage) added to the typed view of `effectcraft-aep` · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-07 · **Change:** footage: an image sequence without a filename prefix stores only the suffix `Utf8`, and one imported with Force alphabetical order stores its file list in `LIST StVc` / `StVS` (section 3, footage); before that: versions: `svap` is the version that wrote the file, `head` bytes 4 to 7 the version it is written for, and what a Save a Copy As 25.x / 24.x of After Effects 2026 changes (section 1, "Versions"); before that: the stretch numerator is signed (a negative one reverses the layer, 981-983); layer styles (3.12b, 950-980); before that: footage: `sspc@118` of an image sequence is its folder's modification time (860, read-back x1-x3); before that: text: paragraph `/24`, `/35` and style `/68` are neutral for 0 and 1 (732-734, edited in place); before that: keyframes: the ease of mask path keys (599, 700), the spatial speed unit of fractional values (597, 598, 730); before that: text: character and paragraph settings, manual kerning, vertical text, `/nil` is neutral (S17: 538-578, 595, 596); before that: cameras, lights and 3D rotations (S16: the auto Position of a two-node camera is where After Effects renders it, 537; camera and light defaults read back 255, 256; Orientation `otst/otda` 488), Timewarp (529-533), an expression on an effect parameter (534); before that: text: style slot /13 is superscript / subscript (526-528; every scripted text of the corpus is superscript), wrapping box text 517; before that: effects of batch 5 (409-450, 511-516: integer sliders, checkbox defaults in `pard`, arbitrary data of Hue/Saturation and Levels, the parameters of Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow; section 3.7); before that: shape items of batch 5 (451-484, 519: ellipse, fill and stroke opacity, fill rule, group transform, shape layer anchor, trim paths, rectangle roundness, polystar fields, gradient default, nested groups, the defaults of every item; section 3.9); before that: track mattes: mode at `ldta` byte 107 and the matte layer id at byte 160 (cases 402-408, section 3.1); before that: text: automatic leading is verified (400); Exposure (`ADBE Exposure2`, cases 389-397): group start / end parameter types 13 / 14, float slider valid minimum and slider range (section 3.7); before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: the XML form `.aepx` is read into the same chunk tree (section 1, "The XML form"); the `head` save counter is a `u32` at byte 16; text documents for the importer (stroke colour, automatic leading, the font of each run, the undecoded-slot check, the text-layer Anchor Point, the Source Text record); before that: third round of verified field layouts (explicit-default semantics, type-major bezier ease, exact time stretch and in / out points, Time Remapping state, comp pixel aspect / shutter phase / display start, Point default, Layer Control None, dropdown selection, one `mkif` per mask, polystar and radial gradient records, text styles, camera and light options, marker cue data, audio and image-sequence footage) added to the typed view of `effectcraft-aep` · **Target:** Adobe After Effects 2026 What `crates/aep` ([`effectcraft-aep`](../crates/aep/README.md)) reads and writes, and the evidence for each statement. Everything here was observed on project files we generated ourselves; no @@ -279,7 +279,8 @@ Footage items beyond solids and stills (`Pin /sspc`, 222 bytes; audio and image | bits per pixel | 60 | u32 | RGBA still 32, RGB sequence 24, 0 for solids and audio | 126, 352 | V | | **sample rate** | 160 | f64 | 48 000.0 for the WAV; the same for an mp4 with sound (48 000.0 and 44 100.0 read back from AAC tracks of those rates), absent for an mp4 without (the importer takes a clip's audio from it) | 350, read-back vid-audio | V | | **file size** | 208 | u32 | bytes of the source file(s): png 149, WAV 96 044 (44 + 48 000 x 2), three png of 168 bytes 504 | 126, 350, 352 | V | -| **sequence numbering** | 172, 176, 180 | u32 x 3 | first and last frame number and the digits of a sequence: 1, 3, 3 for `seq_001.png`..`seq_003.png` (353), 101, 103, 4 for `shot_0101.exr`..`shot_0103.exr` (603); 0 for single files (604, 605, 607). The name around the number is the pair of `Utf8` chunks between `Als2` and `opti` in `Pin ` (`seq_` / `.png`, `shot_` / `.exr`), absent on single files. `Item::sequence_info`, `SequenceInfo::file_name` | 353, 603, 604, 605, 607 | V | +| **sequence numbering** | 172, 176, 180 | u32 x 3 | first and last frame number and the digits of a sequence: 1, 3, 3 for `seq_001.png`..`seq_003.png` (353), 101, 103, 4 for `shot_0101.exr`..`shot_0103.exr` (603); 0 for single files (604, 605, 607). The name around the number is the pair of `Utf8` chunks between `Als2` and `opti` in `Pin ` (`seq_` / `.png`, `shot_` / `.exr`), absent on single files. **An empty prefix is not written**: `0001.png`..`0005.png` leave one `Utf8` (`.png`) there, so a single `Utf8` is the prefix when the first file name in `opti` (byte 58: `0001.png`) starts with it and the suffix when it ends with it; without that evidence the reader takes one starting with `.` as the suffix and reads nothing else. `Item::sequence_info`, `SequenceInfo::file_name` | 353, 603, 604, 605, 607; the prefix-less sequence of issue 3174 (After Effects 25.2) | V | +| **sequence file list** (Force alphabetical order) | `Pin /LIST StVc` | `StVS` (4 bytes, `u32` LE), then `Utf8` x count | a sequence imported with Force alphabetical order has, between `Als2` and `opti`, a `LIST StVc` instead of the prefix and suffix `Utf8`s: `StVS` holds the file count as a **little-endian** `u32` (`05 00 00 00`), followed by one `Utf8` per file name in order (`shot_0001.png`..`shot_0005.png`). The numbering at @172 / @176 is then `0xFFFFFFFF` and the digits @180 0; duration and rate are stored as for a numbered sequence (5/30, 30). The list replaces the numbering; a list without a readable `StVS` count, or with a name that is not a plain file name (`/`, `\`, `:`, a control character such as NUL, `.`, `..`), is not read. Without a stored item name the importer names the item after its first listed file (an assumption: what After Effects shows was not sampled). `SequenceInfo::files`, `SequenceInfo::nth_file_name` | the alphabetical sequence of issue 3174 (After Effects 25.2) | V | | **sequence folder time** | 118 | u32 (unaligned) | seconds since 1904-01-01 UTC: the modification time of a sequence's folder when After Effects scanned it (860: 0xe6fb443a = 2026-10-19 03:51:22, the folder's and its files' time). With the folder's time After Effects keeps the stored numbering on open; with another (an older time, the first or the last file's time while the folder's differs: x1, x2 against x3) it scans the folder again and warns about a gap in a modal dialog | 860, `export-tests\sparse860\exp` x1-x3 | V | | still duration denominator | 42 | u32 | 1 for png (126, 607), 600 for jpeg (605), 100 for OpenEXR (604): a still's duration is 0 over a per-format denominator | 126, 604, 605, 607 | V | From 514e16c027cae577ee40bac5ef27790365775af6 Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Sun, 8 Nov 2026 01:57:47 +0100 Subject: [PATCH 058/350] .aep import: effects without definitions borrow them from another instance (#3223) Refs #3170 (the Color Control part). Some files have no project-level effect definition list (EfdG); it was seen in 17 projects, all from inside After Effects-made Motion Graphics templates. In those, one instance per effect type keeps the full parameter definitions (parT) and every other instance of the type has an empty parT. The others were reported "without readable parameter definitions" and dropped (129 Color Controls of one template). The definition index now falls back to the parT of the first instance of the same match name that has its own (DefinitionSource::OtherInstance). Values still come from each instance's own records; a value record that borrowed definitions do not name is reported instead of vanishing. The "Color ... holds an unreadable value" report came from 0.21.1, which read only written records (tdsb 1). Reading default-state records (tdsb 3, #3126) probably fixes it, but that is not confirmed for Color Control; a synthetic test now pins a default-state colour record. --- crates/aep/src/container/effects.rs | 65 +++++- crates/aep/src/import/effects.rs | 16 ++ crates/aep/tests/it/container/robustness.rs | 6 +- crates/aep/tests/it/import/robustness.rs | 12 +- crates/aep/tests/it/import/synthetic.rs | 237 ++++++++++++++++++++ docs/aep-format.md | 2 +- 6 files changed, 322 insertions(+), 16 deletions(-) diff --git a/crates/aep/src/container/effects.rs b/crates/aep/src/container/effects.rs index e336d84e5..2fa77d017 100644 --- a/crates/aep/src/container/effects.rs +++ b/crates/aep/src/container/effects.rs @@ -12,6 +12,11 @@ //! project-level `LIST EfdG` (one `EfDf` per effect type: a `tdmn` with the match name, then a copy //! of one instance's `sspc`), see [`Project::effect_params`]. That copy also carries the values of //! the instance it was copied from, so values are only ever read from the instance's own `tdgp`. +//! +//! Some files have no `EfdG` at all (seen in 17 projects, all from inside After Effects-made +//! Motion Graphics templates). In those, one instance per effect type kept its full `parT` (which +//! one is not known) and it is the only place the definitions are written, so the other instances +//! of that type borrow them from it (#3170). use std::collections::{HashMap, HashSet}; @@ -138,7 +143,11 @@ pub enum DefinitionSource { /// The project-level `EfdG/EfDf` copy for the effect's match name (the instance's own `parT` /// is empty, 383). Project, - /// Neither has any: the effect has no readable parameter definitions. + /// The own `parT` of another instance of the same match name in the file: the instance's + /// own `parT` is empty and the file has no project-level copy of the type (a project without + /// `EfdG`, #3170). + OtherInstance, + /// None of these has any: the effect has no readable parameter definitions. Missing, } @@ -155,7 +164,8 @@ pub struct EffectParam<'f, 'a> { pub name: String, /// The popup items (`pdnm`, split at `|`; for a button its label, e.g. `Swap Colors`). pub items: Option>, - /// True when the definition is the project-level copy (see [`DefinitionSource::Project`]). + /// True when the definition is borrowed: the project-level copy ([`DefinitionSource::Project`]) + /// or another instance's ([`DefinitionSource::OtherInstance`]). pub borrowed_definition: bool, pard: &'f [u8], stored: Option>, @@ -214,6 +224,22 @@ impl<'f, 'a> Effect<'f, 'a> { self.chunk.child_list(b"parT").is_some_and(has_pard) } + /// The match names of the value records in the instance's own `tdgp` (properties only, not + /// groups), in file order, each once. + pub fn value_record_names(&self) -> Vec { + let mut seen: HashSet = HashSet::new(); + self.chunk + .child_list(b"tdgp") + .and_then(PropGroup::from_chunk) + .map(|g| g.entries()) + .unwrap_or_default() + .into_iter() + .filter(|e| matches!(e.node, PropNode::Property(_))) + .map(|e| e.match_name) + .filter(|n| seen.insert(n.clone())) + .collect() + } + /// The parameters in the order of the instance's own `parT`. Empty for an instance whose /// `parT` is empty; [`Project::effect_params`] falls back to the project's definitions. pub fn params(&self) -> Vec> { @@ -299,8 +325,25 @@ impl<'f, 'a> Project<'f, 'a> { /// [`Self::effect_definitions`] indexed by match name, for looking up many effects: build it /// once per file, not once per effect (a file can hold many definitions and many instances). + /// For a type without a project-level copy it also keeps the own `parT` of the first instance + /// in the file (items, layers and effects in file order) that has one. pub fn effect_definition_index(&self) -> EffectDefinitions<'f, 'a> { - EffectDefinitions { by_name: self.effect_definitions().into_iter().collect() } + let by_name: HashMap> = self.effect_definitions().into_iter().collect(); + let mut instances: HashMap> = HashMap::new(); + for item in self.all_items() { + let Some(comp) = item.comp() else { continue }; + for layer in comp.layers() { + for fx in layer.effects() { + if by_name.contains_key(&fx.match_name) || instances.contains_key(&fx.match_name) { + continue; + } + if let Some(part) = fx.chunk.child_list(b"parT").filter(|p| has_pard(p)) { + instances.insert(fx.match_name, part); + } + } + } + } + EffectDefinitions { by_name, instances } } /// Where the parameter definitions of `effect` come from. Builds the index each call: use @@ -316,10 +359,13 @@ impl<'f, 'a> Project<'f, 'a> { } } -/// The project-level effect definitions by match name ([`Project::effect_definition_index`]). +/// The borrowable effect definitions by match name ([`Project::effect_definition_index`]). #[derive(Debug, Clone, Default)] pub struct EffectDefinitions<'f, 'a> { + /// The project-level copies (`EfdG`). by_name: HashMap>, + /// For a type without a project-level copy: the own `parT` of the first instance that has one. + instances: HashMap>, } impl<'f, 'a> EffectDefinitions<'f, 'a> { @@ -329,20 +375,23 @@ impl<'f, 'a> EffectDefinitions<'f, 'a> { DefinitionSource::Instance } else if self.by_name.contains_key(effect.match_name()) { DefinitionSource::Project + } else if self.instances.contains_key(effect.match_name()) { + DefinitionSource::OtherInstance } else { DefinitionSource::Missing } } /// The parameters of `effect`: from its own `parT`, or, when that is empty, from the - /// project-level definition of its match name ([`EffectParam::borrowed_definition`]). Values - /// always come from the instance's own records (absent = the `pard` default); the project - /// copy's values belong to another instance and are never read. + /// project-level definition of its match name, else from another instance of it that has + /// its own ([`EffectParam::borrowed_definition`]). Values always come from the instance's own + /// records (absent = the `pard` default); the values next to borrowed definitions belong to + /// another instance and are never read. pub fn params(&self, effect: &Effect<'f, 'a>) -> Vec> { if effect.has_own_definitions() { return effect.params(); } - match self.by_name.get(effect.match_name()) { + match self.by_name.get(effect.match_name()).or_else(|| self.instances.get(effect.match_name())) { Some(part) => effect.params_from(part, true), None => Vec::new(), } diff --git a/crates/aep/src/import/effects.rs b/crates/aep/src/import/effects.rs index 8409a6f7e..b097c3600 100644 --- a/crates/aep/src/import/effects.rs +++ b/crates/aep/src/import/effects.rs @@ -99,6 +99,22 @@ pub(crate) fn import_effects<'f, 'a>( None => col.lose(LossCategory::DamagedData, format!("{}: '{}': the fx switch of '{shown}' is unreadable, imported switched on", cx.comp, cx.layer)), } let params = defs.params(&fx); + // Borrowed definitions (another instance's or the project copy) may not name every value + // record this instance has: such a record has nothing to import into, so it is reported. + if params.first().is_some_and(|p| p.borrowed_definition) { + let defined: std::collections::HashSet<&str> = params.iter().map(|p| p.match_name.as_str()).collect(); + for name in fx.value_record_names().iter().filter(|n| !defined.contains(n.as_str())) { + col.lose( + LossCategory::Effects, + format!( + "{}: '{}' effect '{shown}' ({}): the value record {name} has no parameter definition, not imported", + cx.comp, + cx.layer, + fx.match_name() + ), + ); + } + } if map.extra == Extra::Cryptomatte { import_cryptomatte(&fx, ¶ms, cx, &shown, &mut group, col); } else { diff --git a/crates/aep/tests/it/container/robustness.rs b/crates/aep/tests/it/container/robustness.rs index 27622d887..5c3422a10 100644 --- a/crates/aep/tests/it/container/robustness.rs +++ b/crates/aep/tests/it/container/robustness.rs @@ -237,9 +237,11 @@ fn exercise_layer(project: &Project<'_, '_>, comp_size: (u32, u32), layer: &Laye if let Some(root) = layer.properties() { exercise_group(&root, 0); } + // One index for the layer's effects: building it walks every layer of the file. + let defs = project.effect_definition_index(); for e in layer.effects() { - let _ = (e.match_name().len(), e.name(), e.declared_param_count(), e.has_own_definitions(), project.definition_source(&e)); - for p in e.params().into_iter().chain(project.effect_params(&e)) { + let _ = (e.match_name().len(), e.name(), e.declared_param_count(), e.has_own_definitions(), defs.source(&e)); + for p in e.params().into_iter().chain(defs.params(&e)) { let _ = (p.index, p.kind, p.name.len(), p.items.as_ref().map(Vec::len), p.pard().len(), p.value(), p.stored_value(), p.pard_default()); let _ = (p.is_default(), p.popup_item_count(), p.popup_selected_index(), p.popup_selected_item(), p.valid_max(), p.is_animated()); let _ = (p.valid_min(), p.slider_range()); diff --git a/crates/aep/tests/it/import/robustness.rs b/crates/aep/tests/it/import/robustness.rs index 1a42665bc..902c9cc4b 100644 --- a/crates/aep/tests/it/import/robustness.rs +++ b/crates/aep/tests/it/import/robustness.rs @@ -626,9 +626,10 @@ fn bogus_effect_structure_is_reported_not_imported_blindly() { "{r:?}" ); - // No project-level definitions: a Ramp with an empty parT cannot be read and is reported; one - // with its own parT still arrives. (In the single-case file one of the two had its own; in - // the combined file an earlier case holds the definitions, so both take the project copy.) + // No project-level definitions: a Ramp with an empty parT borrows the definitions of a Ramp + // in the file that has its own parT (#3170). In the single-case file one of the two has its + // own, so both arrive; in the case cut from the combined file an earlier case (not kept) + // held them, so neither has readable definitions and both are reported. let own = { let bytes = skip!(crate::common::try_fixture("383-fx-ramp-on-100x100-start-10-20.aep")); let file = parse(&bytes).unwrap(); @@ -646,8 +647,9 @@ fn bogus_effect_structure_is_reported_not_imported_blindly() { } })); let r = report_of(&bytes); - assert_eq!(r.imported.effects, own, "{r:?}"); - assert_eq!(r.count(effectcraft_aep::import::LossCategory::Effects), 2 - own, "{r:?}"); + let arrive = if own > 0 { 2 } else { 0 }; + assert_eq!(r.imported.effects, arrive, "{r:?}"); + assert_eq!(r.count(effectcraft_aep::import::LossCategory::Effects), 2 - arrive, "{r:?}"); assert!(try_all(&bytes)); // 368, the first Gradient Ramp of its file, has its own parT: it still arrives. let bytes = skip!(mutate("368-fx-ramp-default.aep", &mut |c| { if let Body::List { list_type, .. } = &mut c.body diff --git a/crates/aep/tests/it/import/synthetic.rs b/crates/aep/tests/it/import/synthetic.rs index 5d4d89c56..a023bb162 100644 --- a/crates/aep/tests/it/import/synthetic.rs +++ b/crates/aep/tests/it/import/synthetic.rs @@ -553,6 +553,186 @@ fn default_state_effect_records_import_like_written_ones() { assert_eq!(clip, vec![("clipToOutputBlack".to_string(), effectcraft_keyframe::Value::Enum(1))]); } +/// Sets the `cdat` of the value record `param` in the value group `tdgp` (the first `f64`), and +/// its state to `tdsb` when given. True when the group has that record. +fn patch_record(tdgp: &mut Chunk<'_>, param: &str, tdsb: Option, value: f64) -> bool { + let kids = tdgp.children_mut(); + let Some(at) = kids.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(param.as_bytes()))) else { + return false; + }; + let Some(record) = kids.get_mut(at + 1).filter(|k| k.is_list(b"tdbs")) else { return false }; + for k in record.children_mut() { + if k.is(b"tdsb") + && let Some(state) = tdsb + { + set_data(k, 0, &state.to_be_bytes()); + } else if k.is(b"cdat") { + set_data(k, 0, &value.to_be_bytes()); + } + } + true +} + +/// The project without its project-level effect definitions (`EfdG`; some files have none, seen +/// in the projects inside After Effects-made Motion Graphics templates) and with a second Glow +/// just above the first on the layer: a copy of it whose `parT` is empty, as such files write +/// every instance of a type but one. `edit_copy` is applied to every chunk of the copy; with +/// `empty_original` the original's `parT` is emptied too. +fn second_glow_without_definitions(edit_copy: &mut dyn FnMut(&mut Chunk<'_>), empty_original: bool) -> Vec { + fn empty_part(fx: &mut Chunk<'_>) { + for k in fx.children_mut() { + if let Body::List { list_type, children, .. } = &mut k.body + && list_type == b"parT" + { + children.clear(); + } + } + } + let mut file: AepFile<'_> = parse(SYNTHETIC).unwrap(); + let mut added = 0; + visit(&mut file.chunks, &mut |c| { + if let Body::List { list_type, children, .. } = &mut c.body { + if list_type == b"EfdG" { + *list_type = *b"XfdG"; + return; + } + if list_type != b"tdgp" { + return; + } + let Some(at) = children.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(b"ADBE Glo2"))) else { + return; + }; + let (Some(name), Some(mut fx)) = (children.get(at).cloned(), children.get(at + 1).filter(|k| k.is_list(b"sspc")).cloned()) else { return }; + empty_part(&mut fx); + edit_copy(&mut fx); + visit(fx.children_mut(), edit_copy); + if empty_original && let Some(original) = children.get_mut(at + 1) { + empty_part(original); + } + children.insert(at, fx); + children.insert(at, name); + added += 1; + } + }); + assert_eq!(added, 1, "one Glow on the layer"); + write(&file).unwrap() +} + +/// The imported Glows in stack order, each as its property values by property id. +fn glow_values(p: &Project) -> Vec> { + let glow = effectcraft_project::GroupKind::Effect { effect: "ec.stylize.glow".to_string() }; + let mut glows = Vec::new(); + for item in p.items.values() { + if let ItemKind::Comp(c) = &item.kind { + for layer in &c.layers { + for node in layer.effects().map(|fx| fx.children.as_slice()).unwrap_or_default() { + if let effectcraft_project::Node::Group(g) = node + && g.kind == glow + { + let mut values = Vec::new(); + g.walk("", &mut |_, prop| values.push((prop.match_id.clone(), prop.value.clone()))); + glows.push(values); + } + } + } + } + } + glows +} + +/// The value of property `id` in `values`. +fn value_of<'v>(values: Option<&'v Vec<(String, effectcraft_keyframe::Value)>>, id: &str) -> Option<&'v effectcraft_keyframe::Value> { + values?.iter().find(|(i, _)| i == id).map(|(_, v)| v) +} + +/// Every instance of an effect type but one has an empty `parT`; without a project-level copy the +/// others borrow the definitions of that one (#3170: 129 Color Controls of a template were +/// reported "without readable parameter definitions" and dropped). +#[test] +fn an_instance_without_definitions_imports_with_those_of_another_instance() { + // Both Glows import; before, the copy was reported and not imported. + let (p, r) = run(&second_glow_without_definitions(&mut |_| {}, false), true); + let r = r.unwrap(); + let found = losses(&r, LossCategory::Effects); + assert!(!found.iter().any(|d| d.contains("without readable parameter definitions") || d.contains("no parameter definition")), "{found:?}"); + assert_eq!(r.report.imported.effects, 3, "Levels and both Glows: {:?}", r.report.losses); + let glows = glow_values(&p); + assert_eq!(glows.len(), 2); + assert_eq!(glows.first(), glows.get(1), "the copy has the same records, so the same values"); + + // The values come from each instance's own records: the copy's A & B Midpoint (`-0011`) set + // to 0.75 arrives as 75 % in the copy only; the Glow it borrows from keeps its own. + let mut patched = 0; + let bytes = second_glow_without_definitions( + &mut |c| { + if c.is_list(b"tdgp") && patch_record(c, "ADBE Glo2-0011", None, 0.75) { + patched += 1; + } + }, + false, + ); + assert_eq!(patched, 1, "the copy's record"); + let (p, r) = run(&bytes, true); + r.unwrap(); + let glows = glow_values(&p); + let seventy_five = effectcraft_keyframe::Value::Scalar(75.0); + assert_eq!(value_of(glows.first(), "abMidpoint"), Some(&seventy_five)); + let own = value_of(glows.get(1), "abMidpoint"); + assert!(own.is_some() && own != Some(&seventy_five), "{own:?}"); +} + +/// A value record that the borrowed definitions do not name is reported, not dropped silently. +#[test] +fn a_value_record_the_borrowed_definitions_do_not_name_is_reported() { + let bytes = second_glow_without_definitions( + &mut |c| { + if c.is(b"tdmn") && c.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(b"ADBE Glo2-0011")) { + set_data(c, 0, b"ADBE Glo2-0099\0"); + } + }, + false, + ); + let (_, r) = run(&bytes, true); + let r = r.unwrap(); + let found = losses(&r, LossCategory::Effects); + assert!(found.iter().any(|d| d.contains("ADBE Glo2-0099") && d.contains("no parameter definition")), "{found:?}"); + assert_eq!(r.report.imported.effects, 3, "{:?}", r.report.losses); +} + +/// When no instance of the type keeps its definitions, every instance is reported. +#[test] +fn instances_without_any_definitions_are_reported() { + let (p, r) = run(&second_glow_without_definitions(&mut |_| {}, true), true); + let r = r.unwrap(); + let found = losses(&r, LossCategory::Effects); + assert_eq!(found.iter().filter(|d| d.contains("ADBE Glo2") && d.contains("without readable parameter definitions, not imported")).count(), 2, "{found:?}"); + assert_eq!(r.report.imported.effects, 1, "Levels only: {:?}", r.report.losses); + assert!(glow_values(&p).is_empty()); +} + +/// The reader's side: the copy's definitions come from the other instance. +#[test] +fn an_instance_without_definitions_reads_those_of_another_instance() { + let bytes = second_glow_without_definitions(&mut |_| {}, false); + let file = parse(&bytes).unwrap(); + let project = effectcraft_aep::Project::new(&file).unwrap(); + let defs = project.effect_definition_index(); + let mut sources = Vec::new(); + for item in project.all_items() { + let Some(comp) = item.comp() else { continue }; + for layer in comp.layers() { + for fx in layer.effects().iter().filter(|fx| fx.match_name() == "ADBE Glo2") { + sources.push(defs.source(fx)); + let params = defs.params(fx); + assert_eq!(params.len(), 16, "Glow's parameters"); + assert_eq!(params.iter().all(|p| p.borrowed_definition), defs.source(fx) == effectcraft_aep::DefinitionSource::OtherInstance); + } + } + } + // The copy is above the instance it borrows from (earlier in the stack). + assert_eq!(sources, [effectcraft_aep::DefinitionSource::OtherInstance, effectcraft_aep::DefinitionSource::Instance]); +} + /// A record in a state other than 1 or 3 is not read and is reported. #[test] fn an_effect_record_in_another_state_is_reported_as_damaged() { @@ -564,3 +744,60 @@ fn an_effect_record_in_another_state_is_reported_as_damaged() { assert!(damaged.iter().any(|l| l.contains("unreadable value")), "tdsb {tdsb}: {:?}", r.report.losses); } } + +/// The project with a value record for Glow's Color A (`ADBE Glo2-0012`, which the project does +/// not store) in state `tdsb` holding the ARGB doubles `argb` (0..255): a copy of the A & B +/// Midpoint record (`-0011`) made a 4-component colour without its expression. +fn with_color_a(tdsb: u32, argb: [f64; 4]) -> Vec { + let mut file: AepFile<'_> = parse(SYNTHETIC).unwrap(); + let mut added = 0; + visit(&mut file.chunks, &mut |c| { + let Body::List { list_type, children, .. } = &mut c.body else { return }; + if list_type != b"tdgp" { + return; + } + let Some(at) = children.iter().position(|k| k.is(b"tdmn") && k.bytes().is_some_and(|b| b.split(|x| *x == 0).next() == Some(b"ADBE Glo2-0011"))) else { + return; + }; + let (Some(mut name), Some(mut record)) = (children.get(at).cloned(), children.get(at + 1).filter(|k| k.is_list(b"tdbs")).cloned()) else { return }; + set_data(&mut name, 0, b"ADBE Glo2-0012\0"); + if let Body::List { children: kids, .. } = &mut record.body { + kids.retain(|k| !k.is(b"Utf8")); + for k in kids.iter_mut() { + if k.is(b"tdsb") { + set_data(k, 0, &tdsb.to_be_bytes()); + } else if k.is(b"tdb4") { + // 4 components; no expression (bytes 76, 77 and 120). + set_data(k, 3, &[4]); + for byte in [76, 77, 120] { + set_data(k, byte, &[0]); + } + } else if k.is(b"cdat") { + *k = Chunk::data(*b"cdat", argb.iter().flat_map(|v| v.to_be_bytes()).collect::>()); + } + } + } + children.insert(at + 2, record); + children.insert(at + 2, name); + added += 1; + }); + assert_eq!(added, 2, "the instance and the EfdG copy"); + write(&file).unwrap() +} + +/// A colour recorded in the default state (`tdsb` 3) reads like a written one (#3170's "parameter +/// Color ... holds an unreadable value" on 0.21.1, which read only written records). +#[test] +fn a_default_state_colour_record_holds_the_value() { + let argb = [255.0, 51.0, 153.0, 255.0]; + let (written, r1) = run(&with_color_a(1, argb), true); + let (default_state, r3) = run(&with_color_a(3, argb), true); + let (r1, r3) = (r1.unwrap(), r3.unwrap()); + assert!(losses(&r3, LossCategory::DamagedData).is_empty(), "{:?}", r3.report.losses); + assert_eq!(r3.report.losses, r1.report.losses); + let colour = |p: &Project| value_of(glow_values(p).first(), "colorA").cloned(); + let (unchanged, _) = run(SYNTHETIC, true); + assert!(colour(&written).is_some()); + assert_eq!(colour(&default_state), colour(&written)); + assert_ne!(colour(&default_state), colour(&unchanged), "the recorded colour, not the default"); +} diff --git a/docs/aep-format.md b/docs/aep-format.md index 99a1199f0..d6eff25f8 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -461,7 +461,7 @@ The expression engine is project-wide (`LIST ExEn/Utf8`, `javascript-1.0`). API: | **default** | `pard` | 56 | by type | float slider: f64 (Slider 0, **Blurriness 25**); fixed slider and angle: 16.16 (drop shadow opacity 127.5, direction 135, distance 5, softness 0; Fill opacity 1.0 = 100 %; **Tint Amount to Tint 100, Ramp Scatter 0, Blend With Original 0**); **colour: ARGB bytes (drop shadow `ff000000` black, Fill `ffff0000` red; Ramp Start `ff000000`, End `ffffffff`; Tint Map Black To `00000000` and Map White To `00ffffff`: alpha 0, which is what After Effects reads back)**; popup: u32 index (1); **point: two 16.16 fractions of the layer size at 56 and 60 (Point Control 0.5, 0.5; Ramp Start 0.5, 0 and End 0.5, 1)** | 071, 077, 080, 214, 215, 356, 368, 383 | V | | **valid maximum** | `pard` | 128 / 108 | 16.16 / f32 | the largest valid value of a fixed slider (128: Tint Amount 100, Ramp Scatter 512, Blend With Original **1.0**) and of a float slider (108: Blurriness 30 000; **Exposure 100, Offset 2, Gamma Correction 10**). The stored scale differs per slider (below), so this is the reader's only hint at it | 356, 368, 078, 389 | V | | **float slider valid minimum, slider range** | `pard` | 104; 112 / 116 | f32; f32 x 2 | the smallest valid value (Exposure -100, Offset -2, Gamma Correction 0.1 as f32) and the range the slider covers (Exposure -4..4, Offset -0.5..0.5, Gamma 0.1..10); the slider range is repeated as `tdum` / `tduM` (f64) in each value record (391-394). @120 holds the default again as f32 (Gamma 1.0, Blurriness 25), u16 @124 the display decimals (**H**: Exposure 2, Offset 4) | 389, 391-394 | V | -| **definitions of a second instance** | top-level `LIST EfdG` | | | one `EfDf` per effect TYPE (`tdmn` = match name, then a copy of ONE instance's `sspc`, including that instance's value records); the `EfDC` count before them is a little-endian u32. When two layers carry the same effect type, only one instance keeps a full `parT`; **the other has an empty `LIST parT` (4 bytes)** but its own `tdgp` (input layer `-0000` with its own `tdpi`, the button record, and its value records). Its definitions come from the `EfdG` copy by match name, its values from its own records, never from the copy (383: the copy holds S100's Start [0.1, 0.2]; L's Ramp reads the 368 defaults). Which instance keeps the full `parT` is **?** (in 383 S100 is both first in the file and newest). The `EfDf` order follows the stack order (365 / 366); `Pefl/pjef` does not | 365, 366, 383 | V | +| **definitions of a second instance** | top-level `LIST EfdG` | | | one `EfDf` per effect TYPE (`tdmn` = match name, then a copy of ONE instance's `sspc`, including that instance's value records); the `EfDC` count before them is a little-endian u32. When two layers carry the same effect type, only one instance keeps a full `parT`; **the other has an empty `LIST parT` (4 bytes)** but its own `tdgp` (input layer `-0000` with its own `tdpi`, the button record, and its value records). Its definitions come from the `EfdG` copy by match name, its values from its own records, never from the copy (383: the copy holds S100's Start [0.1, 0.2]; L's Ramp reads the 368 defaults). Which instance keeps the full `parT` is **?** (in 383 S100 is both first in the file and newest). The `EfDf` order follows the stack order (365 / 366); `Pefl/pjef` does not. **Some files have no `EfdG` at all** (seen in 17 projects, all from inside After Effects-made Motion Graphics templates). In each of them every effect type (196 in all; 2 to 14 Color Controls per file) has exactly one instance with a full `parT` and an empty one in every other instance, and the reader takes the others' definitions from that instance (`DefinitionSource::OtherInstance`, #3170). Which instance keeps it is **?** here too | 365, 366, 383; templates | V | | popup items | `parT/pdnm/Utf8` | | text | items separated by `\|` | 077 (`Item 1\|Item 2\|Item 3`), 214 | V | | stored value | `sspc/tdgp/-NNNN` | | `tdbs` | present only for a parameter that differs from its default; `tdsb` 1, `cdat` as in 3.4. **Absent record = the `pard` default.** Some parameters are recorded in the default state (`tdsb` 3) and the record still holds the value After Effects uses: every one of the 25 such records in the corpus and the samples differs from its `pard` default and reads back as recorded (Lumetri Color's hidden controls High Dynamic Range, HDR Specular, the second Look, Legacy Tone And Saturation, Face Detection, Auto Color, Auto Intensity and SemanticAutoTone Progress, 1200; Cryptomatte's Selection Mode on, 1400). No effect record in the default state holds a value that does not count. The reader takes the value in either state (`EffectParam::recorded_value`, `EffectParam::value`); `EffectParam::stored_value` keeps to written records, and a record in any other state (no sample has one) is not read: the importer reports it as damaged data. That other effects record values so is H (no sample shows one) | see below; 1200, 1400 | V (1200, 1400), H (other effects) | From 84382487bbeaef3d45dde3e1c76a91597132ac57 Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Sun, 8 Nov 2026 02:30:59 +0100 Subject: [PATCH 059/350] Disk cache: entries keep the files they read, keys from memory (#3184) (#3191) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Disk cache: record the source files each frame reads Part of #3184 (step 1): renders now report which footage files they read, each with its identity (size and modification time) as read. Nothing uses the list yet: images, cache keys and cache behaviour are unchanged. - render: `reads` module with `FileStamp`, `ReadFile`, `ReadFiles` (sorted, deduplicated, bounded; marked incomplete when it can't vouch for every file) and the thread-safe `ReadLog`. `Renderer::reads` collects a frame's files; `FootageSource::recording` lets a source note what it reads. `file_stamp` uses the same identity. - Layer cache entries keep the files their buffer was made of and report them on every hit; derived entries (styles, blend space, flattened) keep their base's. Buffers from the disk cache or the browser store don't know their files yet and mark the list incomplete. - Media pool: every file read notes its identity, taken from the file it opens. Decoded frames, opened movies, GIFs, models and EXR channels keep it with the cache entry, so a frame served from memory reports its file without touching the disk. - Viewer frame worker collects each frame's list and carries it to where the disk entry is written (step 2 stores it). Co-Authored-By: Claude Opus 5.5 * Automation: confined footage passes on the files it reads Part of #3184: ConfinedFootage forwards FootageSource::recording to the source it wraps, so renders of an automation session list their files instead of coming back incomplete. The read tests also render through the viewer's CancellableFootage wrapper. Co-Authored-By: Claude Opus 5.5 * Disk cache: frame entries keep their source files, comp key from memory Part of #3184 (step 2). Fixes #3175: opening a comp no longer stamps every footage file of the project before its first frame can show. - The comp's content key (`disk_cache::comp_content_key`) is taken from the project in memory alone: no footage or proxy file is stamped, no `VirtualFileId` is asked. `comp_content_key_with` is gone; `frame_key` hashes a new entry version, so entries of the earlier format are never looked up again (LRU evicts them). - Frame entries (formats 2 and 3) store the files the frame read (`EntryFiles`: size and modification time as read; size and content hash through `VirtualFileId` for files from memory; missing files), then the pixels. Formats 0 and 1 read as misses and are deleted. - Check on hit: `get_frame`/`get_frame_f16` look only at the listed files (one metadata call each) on the frame worker. A changed, back or unreadable file, or a damaged entry, is a miss: the entry is deleted and the frame rendered and stored again. - A frame whose file list is incomplete is shown but not stored. The RAM preview keeps each frame's files, so `persist_missing` writes only frames with a complete list. - Remote frame worker: `FrameServer` records each frame's reads (`take_files`); the web worker stores them with the entry and the page checks them through the file table before using it. The browser's media epoch existed only for the old key and is removed. Export does not use frame entries. - The cache indicator still only asks whether an entry exists. Tests: entry format round trip and damage, the check per file, and in the viewer: a changed file re-renders exactly its frames (and a reopen reads the rest from disk); a frame checks only its own files in a project with a large unused sequence and the key looks at no file; missing, unreadable files and damaged entries never serve a stale frame. Co-Authored-By: Claude Opus 5.5 * Disk cache: layer entries keep their source files, no footage salt Part of #3184 (step 3 of 4). Layer buffers on disk (and in the browser's layer store) now carry the files they were made of, like frame entries since step 2, and are checked against exactly those files when used. The project-wide footage salt is gone: nothing stamps every footage file any more. - Layer entry format 4: the buffer's EntryFiles, then the pixels. A hit checks only the listed files (metadata, or the VirtualFileId hook for files from memory; Missing must still be missing); a change, a damaged entry or the old format 0 is a miss and the entry is deleted, so the re-rendered buffer replaces it. A hit reports its file list, so frames made of buffers from disk are complete and get stored again. - Buffers whose files can't all be named are not written. - Layer disk keys: layer_disk_key = hash of the layer entry version, the renderer version and the layer cache key (which names the footage items with their paths and settings, from memory). - PrefetchStore checks on use; an entry made of changed files is dropped and forgotten, so the new buffer is queued over it. FrameServer passes its virtual-file hook to its layer cache. - Removed: footage_salt(_with), file_stamp, footage_stamp, set_disk_salt, set_store_salt, Session::refresh_disk_salt, poll_disk_salt, footage_fingerprint, disk_salt, disk_salt_background, disk_salt_job, DiskSaltJob, the UI's per-frame poll, the salt in FrameServer and in the availability worker (with its cache_enabled plumbing). The disk cache is attached as soon as it is configured. - Docs: architecture.md, web.md. Tests: layer entries check their files (size, time, missing/back, folder, damaged, old format, incomplete); virtual files; prefetch store; media tests with a real pool across restarts (only the changed file is read, projects with the same item ids never share an entry, missing, unreadable and damaged never serve stale buffers); engine tests that the cache is attached at once and edits do no file work (#1926, #3106). Co-Authored-By: Claude Opus 5.5 * Disk cache: no-salt worker test, sequence paths in the frame-blend key Part of #3184 (step 3 of 4). - A frame-blended footage frame's layer-cache key now hashes the sequence's file list: with no project-wide salt, two projects whose items share an id and a first frame must not share its disk entry. - Test: a frame worker syncing, patching and rendering a project with an unused 5000-frame sequence asks its file hook only about the file its buffer reads (the salt asked about every file at every sync). Co-Authored-By: Claude Opus 5.5 * Disk cache: the comp key covers what its expressions can read With an expression anywhere in the project, every comp's disk key hashed every item of the project, so any edit (another comp, an unused footage item) threw away every comp's frames on disk. The key now covers what the comp can draw plus what the expressions among it can reach (Project::pixel_reach): - without expressions: the items the comp draws, without silent layers, plus the footage its precomp layers swap in (Media Replacement); - with expressions: every layer of the comps they reach, their markers, the comps and footage they name with string literals (comp("Name"), footage("Name")) together with which items have those names now, and the footage named like a reached source (its data is read by name); - the whole project when an expression computes a name, uses the object model's constructors or internals, dynamic code, computed keys or writes, or text the scanner can't read for sure (regex literals, template strings, escapes): expression_reach errs towards reaching more. Markers count only where an expression can read them (#1083). Memory only, no file is looked at. Tests: scanner and reach unit tests in the project crate; engine tests for the key (own-comp expressions keep the key through edits elsewhere, comp("Other") follows Other and not a third comp, a second comp taking the name counts, a computed name counts every item, markers read from another comp); a ui-egui test that the disk frames outside an expression layer are carried when the comp it names changes. Part of #3184. Part of #1492. Co-Authored-By: Claude Opus 5.5 * Disk cache: Reload Footage drops the entries of its files, docs File > Reload Footage now deletes the frame and layer entries whose file lists name the reloaded item's files (DiskCache::drop_files), so a file replaced by one with the same size and modification time, which the check on hit can't tell apart, is read again. The index knows keys, not files: the writer thread reads the files section at the start of each frame and layer entry (not the pixels) after the writes queued before it, and deletes the entries that list one of the files. Until it is done, an entry made of one of them is a miss and frames aren't linked to new keys. The entries are gone for good, so they don't come back after a restart. Automatic reloads don't drop anything: they follow a changed stamp, which the check on hit catches. The browser needs nothing (its file identities are content hashes). Removed: disk_cache::seal, which steps 2 and 3 left without callers (the tests use seal_format). Updated the module, seal_format and forget_media docs, and docs/architecture.md / docs/web.md describe the final design: what is cached, keys from memory, file lists checked on hit, nothing scans the project, incomplete lists stay in memory, Reload Footage drops entries, edits keep the frames they can't change. Tests: render unit tests (same size and time passes the check but not the drop; other files' entries stay; an entry queued before the drop is dropped; a layer entry too; written again afterwards it is used; the drop survives a restart; a pending drop is a miss), and an engine test through file.reloadFootage. Closes #3184. Part of #1492. Co-Authored-By: Claude Opus 5.5 * Disk cache: file stamps keep the whole modification time A file's modification time was nanoseconds since 1970 as u64: a time before 1970 read as "no time" (two such files of one size looked alike) and times after 2554 were truncated. It is now i128 nanoseconds, negative before the epoch; None only means the file system has no time. The content-key hash writes a presence tag and the full value. Co-Authored-By: Claude Opus 5.5 * Automation: a refused footage read leaves the render's file list incomplete Footage outside the read roots shows nothing and was noted as no file at all, so a picture made without it had a complete, empty list and could be kept on disk for a later session with other roots. A refused read during `recording` now marks the list incomplete. Co-Authored-By: Claude Opus 5.5 * Render: a buffer kept by another worker comes with its own files LayerCache::insert returns the files of the buffer it keeps, and Renderer::keep notes them in the render's log and returns them, so a render that uses another worker's buffer lists that buffer's files (a file changed between the two reads then has two identities and the list is incomplete). Co-Authored-By: Claude Opus 5.5 * Render: sampleImage and text fonts note the files they read EvalCtx carries the render's read log. A sampleImage render records its footage reads, keeps them with its per-thread cached buffer and notes them in the evaluating render's log on every use. Text notes the font files it is drawn with, with the identity their bytes were read with; a family that isn't installed, a fallback or missing glyph, or a font from memory leaves the list incomplete. Co-Authored-By: Claude Opus 5.5 * Disk cache: frame entries store the whole modification time A frame entry's file list stores a file's modification time as i128 nanoseconds since 1970 (negative before), as the file stamps now keep it; the frame entry format version goes to frame-entry-files-2 so entries in the old layout are not read. Co-Authored-By: Claude Opus 5.5 * Disk cache: layer entries store the whole modification time Layer entries share the frame entries' file list layout, which now stores the full i128 modification time: the layer entry format version goes to layer-entry-files-2. The module doc names the font files text is drawn with among an entry's source files, and what keeps a list incomplete. Co-Authored-By: Claude Opus 5.5 * Layer cache: buffers that read other layers are keyed by the switches passed down A layer whose effect reads another layer (Gradient Wipe, Set Matte, Blend…) gets that layer drawn by its own renderer. Through a precomp layer, Switches Affect Nested Comps, the switches inherited from the precomp layers above and motion blur on change those pixels, but the key held only the layers' own switches: a buffer made with the setting on was reused (from memory or the disk cache) with it off. Such layers now derive their key with what the renderer passes down. Co-Authored-By: Claude Opus 5.5 * Layer cache: masked buffers are keyed by the mask motion blur gate A mask with Mask Motion Blur On blurs even when the layer's own switch is off, as long as the render's gate is open (Render Settings motion blur, and with Switches Affect Nested Comps the precomp layers above). The key held the layer's own motion blur state only, so a buffer blurred with the gate open was reused with it closed (and the other way round), also from the disk cache. Masked layer, input and footprint keys now tell a closed gate apart. Co-Authored-By: Claude Opus 5.5 * Render: cached buffers list what their expressions and substituted characters read - Buffers made under `Renderer::recorded` (layer content, a Time effect's input, an adjustment layer's footprint) evaluate their masks, shapes and effects with the context that notes into the buffer's own read log, so a file an expression reads (`sampleImage`, also at shutter times mask motion blur adds and the cache key doesn't see) is in the buffer's files. - A styled layer served from the cache lists the files of the cached body and passes too, so the buffers made of it (flattened, blending space) don't get a complete list when one of them came with an incomplete one. - A character put in by Character Offset or Character Value without a relayout notes the font it is drawn with; a fallback face (or none) leaves the list incomplete. Co-Authored-By: Claude Opus 5.5 * Render: text lists the font file of a run that draws no glyph; tests clean their temp dirs An empty first paragraph draws no glyph, but in a box its face sets the first baseline that places every later line. The read list took faces only from glyphs, so it could be complete without that file. Note each style run's resolved face as well. The font-reads and confine tests use tempfile::tempdir(): a fresh directory per run, removed on panic too. Co-Authored-By: Claude Opus 5.5 * Disk cache: Reload Footage closes the gaps around its drop (#3184) - A reload while the disk cache is off is kept on the session (pending_disk_drops) and applied when the cache is switched on, before anything is read from it. - Each render takes the cache's drop generation before it reads (frame worker; layer cache at the miss). An entry listing a file dropped after that is refused under the queue's lock, so a render that was under way during the reload can't store pixels from before it. Such a frame is shown with an incomplete file list, so persist_missing skips it too. The per-file record is bounded (past it, everything older is refused). - An entry read while its drop was pending is a miss even when the writer ends the sweep between the read and the check (a stale hit found under load): the pending drops and the generation are taken before the entry is read. - Test: a reopened project serves its frame from disk with no source file opened or decoded. Co-Authored-By: Claude Opus 5.5 * Disk cache: RAM frames keep the drop generation their render started at Reload Footage empties the RAM preview, but an idle pass of persist_missing may already hold a frame from before the reload. It took the cache's drop generation when it ran, after the drop, so the old pixels could be written back and, with the file's size and time unchanged, served again after a restart. Each RAM frame now keeps the disk cache its render started with and that cache's drop generation then. persist_missing writes a frame only to that cache, with that generation, and skips it once one of its files was dropped since; a frame rendered without that cache is not written from there. Tests use tempfile directories (the layer drop test, the frame_files folders under target/). Co-Authored-By: Claude Opus 5.5 * Doc comments for the functions this PR touches * Disk cache: cap the free-space reserve at 10 GB, skip encodes the limit refuses The reserve the cache leaves free on its disk was max(2 GB, 5% of the disk) (#2551). On a 4 TB disk with 190 GB free that is 200 GB, so a cache set to 20 GB had a limit of 0: every write was refused, the cache never held anything, and only a log warning said so. The reserve is now clamp(5% of the disk, 2 GB, 10 GB). It is there to keep the cache from filling the disk, not to keep a share of a large disk free: 10 GB leaves the system room to work on any disk, a small disk still keeps 2 GB, and a 100 GB disk keeps 5 GB as before. After Effects has only a maximum cache size and no reserve; a clamp keeps the floor for small disks, where a fixed 10 GB would take most of the space. Writes the limit refuses cost no work: DiskCache::accepts_writes (one atomic read) is checked before a frame is converted (put_disk_frame) and before frames and layers are compressed (put_frame, put_frame_f16, put_layer). The frame workers spent about 8 ms per frame and 14 ms per layer on encodes that were then refused. persist_missing returns at once at limit 0 instead of converting the RAM frames on every idle pass. Settings > Disk shows the limit in effect in MB under Maximum Disk Cache Size, with a note when free disk space lowers it (settings.disk.cacheLimit). * Disk cache: refuse writes below 64 MB of room, show this disk's reserve Review follow-ups to the reserve fix: - accepts_writes also refuses while free disk space leaves less than 64 MB of room (a limit the setting asks for is always used): a few frames would fill it, and the rest would be compressed only to be refused. - While writes are refused the idle passes ask for no repaint, so the UI keeps a frame coming every 5 s: the free space is read again and the RAM frames are written once there is room. - Settings > Disk names the space kept free on this disk (the reserve the last reading applied; the rule from MIN/MAX_DISK_RESERVE when it is unknown), and the "no room" note follows accepts_writes. - The settings.disk.cacheLimit automation label is machine-readable and the same in every language: limitMb, maxMb, reserveMb, spaceLimited, writes. - cargo fmt. * Disk cache: follow 3D model sources in the comp's reach The reach walk followed comp, footage and solid sources but not LayerSource::Model. An expression reading a model layer's source (thisComp.layer("Model").source.name) left the model item out of the comp's content key, so renaming the model kept the key and the old frames on disk were served. Every source item is followed now (LayerSource::item), which also counts the model when no expression reads it (Advanced 3D draws it). The same omission is fixed in the RAM preview's content walk (frames.rs) and in the layer key's walk of referenced layers (cache/dependencies.rs). * Disk cache: a render's drop generation travels with its reads; no disk entries for Expression Selectors Drop generation per render: the layer cache recovered a buffer's drop generation from the latest miss of its key, so a second render of the key that started after File > Reload Footage lent its generation to a render still running from before, and that render's buffer (the file as it was) was written after the sweep. Now Renderer::recorded starts each buffer's read log at the store's drop generation before anything is read (ReadLog::starting_at); ReadFiles carries the earliest generation through merges, so derived buffers (styles, the blend-space copy, the flattened layer) and frames that use a buffer from memory carry it too. LayerCache::insert keeps a buffer made of a file dropped since its reads began neither on disk nor in memory; a list that doesn't say counts as made before every drop. DiskCache::dropped_since takes the list's own generation too. missed_at keeps only the miss time. Expression Selectors: the layer key evaluates a text Expression Selector's amount for the first character only, so a per-character dependency such as textIndex == 1 ? 0 : footage("ref.png").width was not in the key, and after the footage item's width changed a reopened session reused the old layer entry. A text layer with an Expression Selector now marks its reads unkeyed (ReadFiles::is_unkeyed), which merges like the generation; such buffers stay in memory and are never written to disk. Frame entries are unaffected: the comp's frame key hashes what the expressions name. The equality and hash of ReadFiles ignore both new fields. * Disk cache: never serve a RAM layer buffer made before a drop of its files A render that passed the drop check could sit in put_layer's encode while Reload Footage cleared the layer cache and dropped the file, then publish its old pixels to memory after the purge; get, and a fresh render's insert, then served them. The disk-hit promotion went through the same insert_mem. Every way the cache hands out a RAM buffer (get, the kept-first path of insert, an existing entry in insert_mem) now checks the entry's files against the store's drops from their own generation (ReadFiles::since): an entry made before a drop of one of its files is forgotten and the lookup misses, so a fresh render's buffer replaces it. insert_mem also re-checks a buffer just before publishing it. --------- Co-authored-by: Claude Opus 5.5 --- Cargo.lock | 3 + apps/effectcraft-web/src/app.rs | 2 +- apps/effectcraft-web/src/diskcache.rs | 24 +- apps/effectcraft-web/src/frames.rs | 7 +- apps/effectcraft-web/src/store.rs | 18 +- apps/effectcraft/src/main.rs | 3 - crates/automation/Cargo.toml | 1 + crates/automation/src/confine.rs | 105 +- crates/engine/src/analysis/tracking.rs | 2 +- crates/engine/src/background/remote.rs | 53 +- crates/engine/src/background/remote/tests.rs | 138 +- crates/engine/src/cmd/effect.rs | 5 + crates/engine/src/cmd/layer/parent.rs | 1 + crates/engine/src/cmd/layer_time.rs | 1 + crates/engine/src/cmd/path_nulls.rs | 4 + crates/engine/src/cmd/prop.rs | 3 + crates/engine/src/cmd/puppet.rs | 13 +- crates/engine/src/cmd/rig3d.rs | 2 + crates/engine/src/cmd/three_d.rs | 27 +- crates/engine/src/footage/availability.rs | 43 +- crates/engine/src/footage/footage_check.rs | 2 +- crates/engine/src/footage/footage_reload.rs | 45 +- crates/engine/src/lib.rs | 17 +- crates/engine/src/session.rs | 2 +- crates/engine/src/session/edit.rs | 117 -- .../engine/src/settings/session_settings.rs | 14 +- crates/engine/src/tests/crypto_pick.rs | 1 + crates/engine/src/tests/disk_cache.rs | 352 +++-- crates/engine/src/tests/rig3d.rs | 14 +- crates/engine/src/tests/sequence.rs | 21 +- crates/expr/src/host.rs | 2 + crates/expr/src/lib.rs | 1 + crates/expr/src/sample.rs | 24 +- crates/expr/src/tests.rs | 39 + crates/gpu/src/tests/adv3d.rs | 2 + crates/host/src/tests.rs | 34 + crates/media/Cargo.toml | 4 + crates/media/src/exr_channels.rs | 84 +- crates/media/src/lib.rs | 26 +- crates/media/src/pool.rs | 202 ++- crates/media/src/reads.rs | 120 ++ crates/media/src/reads/tests.rs | 390 +++++ crates/media/src/stream.rs | 17 +- crates/project/src/lib.rs | 2 + crates/project/src/reach.rs | 405 +++++ crates/project/src/reach/tests.rs | 198 +++ crates/render/Cargo.toml | 3 + crates/render/src/accel.rs | 23 +- crates/render/src/cache.rs | 597 +++++-- crates/render/src/cache/dependencies.rs | 2 +- crates/render/src/disk_cache.rs | 1390 +++++++++++++---- crates/render/src/disk_cache/entry_files.rs | 276 ++++ crates/render/src/eval.rs | 6 +- crates/render/src/footage.rs | 19 + crates/render/src/fx_host.rs | 3 +- crates/render/src/layers.rs | 87 +- crates/render/src/lib.rs | 5 + crates/render/src/pipeline.rs | 236 ++- crates/render/src/reads.rs | 348 +++++ crates/render/src/tests.rs | 174 +++ crates/render/src/tests/collapse.rs | 13 +- crates/render/src/tests/mask_blur.rs | 29 + crates/render/src/tests/styles.rs | 31 + crates/render/src/tests/time.rs | 1 + crates/render/src/text.rs | 72 +- crates/render/src/text/tests.rs | 87 ++ crates/render/src/three_d/adv/scene.rs | 11 +- crates/render/src/three_d/adv/tests.rs | 4 + crates/render/src/three_d/compose.rs | 6 +- crates/script/src/tests/ui_more.rs | 1 + crates/text/src/fonts.rs | 33 +- crates/ui-egui/src/app/frame.rs | 4 - crates/ui-egui/src/frames.rs | 339 ++-- crates/ui-egui/src/frames/tests.rs | 548 ++++++- crates/ui-egui/src/menus/invoke.rs | 1 + .../src/panels/effect_controls/panel.rs | 1 + crates/ui-egui/src/panels/layer_panel.rs | 1 + crates/ui-egui/src/panels/properties.rs | 1 + crates/ui-egui/src/panels/roto_tool.rs | 1 + crates/ui-egui/src/panels/settings.rs | 110 +- crates/ui-egui/src/panels/text_panels.rs | 1 + crates/ui-egui/src/panels/timeline.rs | 1 + crates/ui-egui/src/panels/viewer.rs | 2 + crates/ui-egui/src/panels/viewer/shapes.rs | 2 + crates/ui-egui/src/panels/viewer/view.rs | 1 + crates/ui-egui/src/prefs_live.rs | 6 + .../ui-egui/tests/it/ui_unreadable_footage.rs | 3 +- crates/ui-egui/tests/it/ui_web_gpu_storage.rs | 9 +- docs/architecture.md | 47 +- docs/preferences.md | 7 +- docs/web.md | 15 +- 91 files changed, 5945 insertions(+), 1202 deletions(-) create mode 100644 crates/media/src/reads.rs create mode 100644 crates/media/src/reads/tests.rs create mode 100644 crates/project/src/reach.rs create mode 100644 crates/project/src/reach/tests.rs create mode 100644 crates/render/src/disk_cache/entry_files.rs create mode 100644 crates/render/src/reads.rs diff --git a/Cargo.lock b/Cargo.lock index 5b426c8f8..f897737db 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1468,6 +1468,7 @@ dependencies = [ "effectcraft-script", "image", "serde_json", + "tempfile", "thiserror 2.0.21", ] @@ -1741,6 +1742,7 @@ version = "0.21.0" dependencies = [ "craft-docpath", "effectcraft-color", + "effectcraft-effects", "effectcraft-model", "effectcraft-pdf", "effectcraft-project", @@ -1877,6 +1879,7 @@ dependencies = [ "lz4_flex", "rayon", "serde", + "tempfile", "web-time", ] diff --git a/apps/effectcraft-web/src/app.rs b/apps/effectcraft-web/src/app.rs index 2882b16cf..99e71117f 100644 --- a/apps/effectcraft-web/src/app.rs +++ b/apps/effectcraft-web/src/app.rs @@ -329,7 +329,7 @@ pub async fn start(canvas_id: String) -> Result<(), JsValue> { if n > 0 { app.frames.set_remote(Some(Arc::new(frames::WebFrames::start(n, !query_flag("nogpuworkers"))))); } - app.frames.set_virtual_media(files::STORE.media_epoch(), Arc::new(|path| files::STORE.file_identity(path))); + app.frames.set_virtual_id(Arc::new(|path| files::STORE.file_identity(path))); let (tx, rx) = std::sync::mpsc::channel(); api::set_sender(tx); app = app.with_control(rx); diff --git a/apps/effectcraft-web/src/diskcache.rs b/apps/effectcraft-web/src/diskcache.rs index ef9d46f8c..72e9a9e11 100644 --- a/apps/effectcraft-web/src/diskcache.rs +++ b/apps/effectcraft-web/src/diskcache.rs @@ -4,23 +4,28 @@ //! //! - **Writes** happen in the frame workers: a render request carries the frame's disk-cache key //! (`effectcraft_engine::remote::FrameMsg::Render`), the worker posts the frame, then writes -//! the sealed entry (`disk_cache::frame_entry`: LZ4, checksum) with a synchronous access -//! handle into a temporary file moved into place (`js/host.js` `cacheWrite`), and reports -//! `FrameReply::Stored`. +//! the sealed entry (`disk_cache::frame_entry`: the source files the frame read, LZ4, +//! checksum) with a synchronous access handle into a temporary file moved into place +//! (`js/host.js` `cacheWrite`), and reports `FrameReply::Stored`. A frame whose files can't +//! all be named is not stored. //! - **The index** lives here, on the page ([`DiskIndex`], shared with the desktop's cache): //! rebuilt at startup from the folder listing (oldest files first), updated by the workers' //! `Stored` replies and by reads; eviction deletes the least recently used files. //! - **Reads** are asynchronous: a frame whose key is indexed is read and decoded here instead -//! of rendering it (`crate::frames`); a damaged or missing file is a miss and is forgotten. +//! of rendering it (`crate::frames`), if the files its entry lists still have the size and +//! content hash it was made of (the file table's identities); a damaged or missing entry, or +//! one whose files changed, is a miss and is deleted. //! //! - **Layer buffers** too (`layers/<32 hex>.ecc`): a layer lookup happens in the middle of a //! synchronous render, where the browser's file system can't be read, so the frame workers //! back their layer caches with a `PrefetchStore`. They report each frame's layer-cache //! misses; before the next request the page names the recent misses that are on disk //! ([`layer_prefetch`], `disk_cache::LayerPrefetch`), and the worker reads those entries -//! into memory before rendering, so the lookups find them. Slow buffers are written by the -//! worker after its frame and indexed here like frames (one least-recently-used order for -//! both kinds). +//! into memory before rendering, so the lookups find them; an entry is used only while the +//! files it lists (the file table's size and content hash) are unchanged, and a buffer whose +//! files can't all be named is not written (#3184). Slow buffers are written by the worker +//! after its frame and indexed here like frames (one least-recently-used order for both +//! kinds). use std::cell::RefCell; @@ -187,11 +192,12 @@ pub fn note_miss() { } /// Read and decode a cached frame; `None` (forgotten and deleted) when the file is missing or -/// damaged. +/// damaged, or the source files it was made of changed since. pub async fn read(key: u128) -> Option { let name = disk_cache::entry_name(key); let bytes = JsFuture::from(cache_read(&name)).await.ok().and_then(|b| b.dyn_into::().ok()).map(|b| b.to_vec()); - let frame = bytes.as_deref().and_then(disk_cache::read_frame_entry); + let identity = |path: &str| crate::files::STORE.file_identity(path); + let frame = bytes.as_deref().and_then(|b| disk_cache::read_frame_entry(b, Some(&identity))); ST.with(|s| { let mut s = s.borrow_mut(); if frame.is_some() { diff --git a/apps/effectcraft-web/src/frames.rs b/apps/effectcraft-web/src/frames.rs index 69089e308..0159fe23b 100644 --- a/apps/effectcraft-web/src/frames.rs +++ b/apps/effectcraft-web/src/frames.rs @@ -473,10 +473,13 @@ pub async fn worker_frame(json: String) { g.settled().await; replies = SERVER.with(|c| c.borrow_mut().as_mut().map(|s| s.resume()).unwrap_or_default()); } + // The source files the frame read, kept with its entry (checked by the page before use); a + // frame whose files can't all be named is not stored (#3184). + let (files, virtual_id) = SERVER.with(|c| c.borrow_mut().as_mut().map(|s| (s.take_files(), s.virtual_id().cloned())).unwrap_or_default()); for (r, px) in replies { post_reply(&r, px.as_deref()); - if let (FrameReply::Frame { width, height, .. }, Some(px), Some(key)) = (&r, &px, &disk) { - let entry = effectcraft_engine::render::disk_cache::frame_entry(*width, *height, px); + if let (FrameReply::Frame { width, height, .. }, Some(px), Some(key), Some(files)) = (&r, &px, &disk, &files) { + let Some(entry) = effectcraft_engine::render::disk_cache::frame_entry(*width, *height, px, files, virtual_id.as_deref()) else { continue }; let name = format!("{key}.ecc"); match JsFuture::from(crate::diskcache::cache_write(&name, js_sys::Uint8Array::from(&entry[..]))).await { Ok(n) => post_reply(&FrameReply::Stored { key: key.clone(), bytes: n.as_f64().unwrap_or(entry.len() as f64) as u64, layer: false }, None), diff --git a/apps/effectcraft-web/src/store.rs b/apps/effectcraft-web/src/store.rs index 0cc877948..a4947600b 100644 --- a/apps/effectcraft-web/src/store.rs +++ b/apps/effectcraft-web/src/store.rs @@ -20,7 +20,6 @@ use std::collections::BTreeMap; use std::path::{Path, PathBuf}; -use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex, OnceLock}; use effectcraft_engine::config::{ConfigStore, FileOps}; @@ -187,13 +186,12 @@ pub struct Store { pub on_change: OnceLock, /// Clock in milliseconds since the Unix epoch (tests pin it). pub clock: OnceLock f64>, - /// Bumped when a virtual file is written. Preview cache lookups key on it; they do not hash. - media_epoch: Arc, } impl Default for Store { + /// An empty store with no mirror contents, change callback or clock. fn default() -> Self { - Self { mirror: Mutex::default(), on_change: OnceLock::new(), clock: OnceLock::new(), media_epoch: Arc::new(AtomicU64::new(0)) } + Self { mirror: Mutex::default(), on_change: OnceLock::new(), clock: OnceLock::new() } } } @@ -218,22 +216,12 @@ impl Store { pub fn put_file(&self, path: &str, data: Arc<[u8]>, persist: bool) { let now = self.now(); let key = file_key(path); - let replaced = self.lock().get(&key).is_none_or(|e| !Arc::ptr_eq(&e.data, &data)); self.lock().put(&key, data, now, persist); - if replaced { - self.media_epoch.fetch_add(1, Ordering::Relaxed); - } if persist { self.changed(); } } - /// The generation preview cache lookups key on. It changes when [`Self::put_file`] writes - /// a different blob, and it is not a content hash. - pub fn media_epoch(&self) -> Arc { - self.media_epoch.clone() - } - /// Size and content hash of the file under `path`. The hash is stored on the entry so a /// later key does not walk the bytes again. pub fn file_identity(&self, path: &str) -> Option<(u64, u64)> { @@ -541,11 +529,9 @@ mod tests { assert_ne!(a, b); assert_eq!(&data[..], b"blue"); assert_eq!(s.file_versioned("/collision.png").map(|f| f.1), Some(b), "reading doesn't change it"); - let epoch = s.media_epoch().load(Ordering::Relaxed); let id = s.file_identity("/collision.png").expect("identity"); assert_eq!(id, (4, content_hash(b"blue"))); assert_eq!(s.file_identity("/collision.png"), Some(id), "the hash is kept on the entry"); - assert!(epoch >= 2, "each new blob bumps the generation the preview keys on"); } /// A write that failed is flushed again with the key's latest state (#215). diff --git a/apps/effectcraft/src/main.rs b/apps/effectcraft/src/main.rs index 07d327a21..5350addbe 100644 --- a/apps/effectcraft/src/main.rs +++ b/apps/effectcraft/src/main.rs @@ -276,9 +276,6 @@ fn main() -> eframe::Result { // written on background threads (M13.14). session.check_footage_on_open = true; session.autosave.background = true; - // The disk cache's footage stamps too: an imported or opened project with thousands of - // sequence frames on a network share took half a minute to stamp. - session.disk_salt_background = true; // Settings, shortcut presets and the crash-recovery sentinel live in the platform // config directory. Agent-driven runs (`--control`) skip crash recovery. if let Some(dir) = effectcraft_host::config_dir() { diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index 916cc3501..e0caf3218 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -19,3 +19,4 @@ effectcraft-model = { workspace = true } [dev-dependencies] effectcraft-script = { workspace = true } effectcraft-host = { workspace = true } +tempfile = { workspace = true } diff --git a/crates/automation/src/confine.rs b/crates/automation/src/confine.rs index 6d8ce2819..570726932 100644 --- a/crates/automation/src/confine.rs +++ b/crates/automation/src/confine.rs @@ -346,32 +346,60 @@ impl ConfinedFootage { fn inside(&self, f: &Footage) -> bool { std::iter::once(&f.path).chain(&f.sequence).filter(|p| !p.is_empty()).all(|p| self.confinement.check(p, Access::Read).is_ok()) } + + /// [`Self::inside`] for a read: a refused one is counted on this thread, so the render + /// [`recording`](FootageSource::recording) it can't list what it made without the footage. + fn readable(&self, f: &Footage) -> bool { + let inside = self.inside(f); + if !inside { + let _ = REFUSED_READS.try_with(|n| n.set(n.get().wrapping_add(1))); + } + inside + } +} + +thread_local! { + /// Footage reads refused on this thread (see [`ConfinedFootage::readable`]). + static REFUSED_READS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +/// The number of reads this thread has refused for lying outside the allowed roots. +fn refused_reads() -> u64 { + REFUSED_READS.try_with(std::cell::Cell::get).unwrap_or(0) } impl FootageSource for ConfinedFootage { + /// The frame from the inner source, or `None` when the footage lies outside the allowed roots. fn frame(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { - self.inside(footage).then(|| self.inner.frame(item, footage, t)).flatten() + self.readable(footage).then(|| self.inner.frame(item, footage, t)).flatten() } + /// The frame scaled to `width` by `height`, or `None` when the footage lies outside the allowed roots. fn frame_at_size(&self, item: ItemId, footage: &Footage, t: Tick, width: u32, height: u32) -> Option> { - self.inside(footage).then(|| self.inner.frame_at_size(item, footage, t, width, height)).flatten() + self.readable(footage).then(|| self.inner.frame_at_size(item, footage, t, width, height)).flatten() } + /// The frame with an explicit size and float curve, or `None` when the footage lies outside the allowed roots. fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option> { - self.inside(footage).then(|| self.inner.frame_with(item, footage, t, size, float)).flatten() + self.readable(footage).then(|| self.inner.frame_with(item, footage, t, size, float)).flatten() } + /// The footage audio from the inner source, or `None` when the footage lies outside the allowed roots. fn audio(&self, item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option> { - self.inside(footage).then(|| self.inner.audio(item, footage, t, frames, rate)).flatten() + self.readable(footage).then(|| self.inner.audio(item, footage, t, frames, rate)).flatten() } + /// The auxiliary channels of the footage frame, or `None` when the footage lies outside the allowed roots. fn aux(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { - self.inside(footage).then(|| self.inner.aux(item, footage, t)).flatten() + self.readable(footage).then(|| self.inner.aux(item, footage, t)).flatten() } + /// Forwards the auxiliary footage paths to the inner source. fn set_aux_footage(&self, paths: &[String]) { self.inner.set_aux_footage(paths) } + /// The 3D model for the footage, or `None` when the footage lies outside the allowed roots. fn model(&self, item: ItemId, footage: &Footage) -> Option> { - self.inside(footage).then(|| self.inner.model(item, footage)).flatten() + self.readable(footage).then(|| self.inner.model(item, footage)).flatten() } + /// The rasterised vector footage at `scale`, or `None` when the footage lies outside the allowed roots. fn vector_frame(&self, item: ItemId, footage: &Footage, scale: f64) -> Option> { - self.inside(footage).then(|| self.inner.vector_frame(item, footage, scale)).flatten() + self.readable(footage).then(|| self.inner.vector_frame(item, footage, scale)).flatten() } /// Says why footage outside the roots shows nothing, so the agent can act on it. fn problem(&self, footage: &Footage) -> Option { @@ -392,15 +420,29 @@ impl FootageSource for ConfinedFootage { fn set_conform_folder(&self, folder: Option) { self.inner.set_conform_folder(folder) } + /// The inner source's cache budget. fn cache_budget(&self) -> Option { self.inner.cache_budget() } + /// Runs `f` under the inner source's cancel flag. fn cancellable(&self, cancel: &Arc, f: &mut dyn FnMut()) { self.inner.cancellable(cancel, f) } + /// Whether the inner source has been called off. fn called_off(&self) -> bool { self.inner.called_off() } + /// The files read inside the roots, as the inner source notes them. Footage outside reads + /// nothing, and what is made without it can't be listed by its files: a refused read makes + /// the list incomplete, so it never outlives the session in a disk cache (another session + /// with other roots would read the footage). + fn recording(&self, reads: &effectcraft_engine::render::reads::ReadLog, f: &mut dyn FnMut()) { + let refused = refused_reads(); + self.inner.recording(reads, f); + if refused_reads() != refused { + reads.mark_incomplete(); + } + } } /// The session's file reads, only inside the read roots: what a project or an imported file @@ -593,6 +635,55 @@ mod tests { } } + /// Footage outside the roots shows nothing: a render that asked for it can't be listed by its + /// files (#3184), or a disk cache would keep the empty picture for a session with other + /// roots. Reads inside the roots record as the inner source tells. + #[test] + fn a_refused_footage_read_makes_the_render_unlisted() { + use effectcraft_engine::render::reads::{FileStamp, ReadFile, ReadLog}; + /// Reads every file, noting it. + struct Noting; + impl FootageSource for Noting { + /// Returns a 1x1 frame and notes the footage path it was asked for. + fn frame(&self, _: ItemId, f: &Footage, _: Tick) -> Option> { + NOTED.with(|n| n.borrow_mut().push(f.path.clone())); + Some(Arc::new(Image::new(1, 1))) + } + /// Runs `f`, then reports every path noted meanwhile to `reads` as a file with a missing stamp. + fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) { + f(); + for p in NOTED.with(|n| std::mem::take(&mut *n.borrow_mut())) { + reads.note(ReadFile::new(p, FileStamp::Missing)); + } + } + } + thread_local! { + static NOTED: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; + } + // (a fresh directory, removed when the test ends, panicking or not) + let tmp = tempfile::tempdir().unwrap(); + let base = tmp.path(); + let (inside, outside) = (base.join("in"), base.join("elsewhere")); + for d in [&inside, &outside] { + std::fs::create_dir_all(d).unwrap(); + } + let c = Confinement::new(std::slice::from_ref(&inside), &[]).unwrap(); + let src = ConfinedFootage::new(Arc::new(Noting), c); + let footage = |d: &std::path::Path| Footage { path: d.join("a.png").to_string_lossy().into_owned(), ..Default::default() }; + let read = |f: &Footage| { + let log = ReadLog::new(); + let mut got = None; + src.recording(&log, &mut || got = src.frame(ItemId(1), f, Tick::ZERO)); + (got.is_some(), log.into_files()) + }; + let (shown, files) = read(&footage(&inside)); + assert!(shown && files.is_complete() && files.len() == 1, "inside the roots: {files:?}"); + let (shown, files) = read(&footage(&outside)); + assert!(!shown && !files.is_complete(), "outside the roots: {files:?}"); + // The next render on the thread is not affected. + assert!(read(&footage(&inside)).1.is_complete()); + } + #[test] fn relative_and_outside_paths_of_newly_listed_commands_are_refused() { let base = std::env::temp_dir().join(format!("ec-confine-1243-{}", std::process::id())); diff --git a/crates/engine/src/analysis/tracking.rs b/crates/engine/src/analysis/tracking.rs index 221b28602..912a8adfe 100644 --- a/crates/engine/src/analysis/tracking.rs +++ b/crates/engine/src/analysis/tracking.rs @@ -208,7 +208,7 @@ pub(crate) fn source_frame( t: Tick, expr: Option<&dyn ExprHost>, ) -> Option<(Arc, [f64; 2])> { - let ctx = EvalCtx { project, comp_id: cid, comp, time: t, expr, footage: None, property_overrides: &[], media_overrides: &[] }; + let ctx = EvalCtx { project, comp_id: cid, comp, time: t, expr, footage: None, property_overrides: &[], media_overrides: &[], reads: None }; if let effectcraft_project::LayerSource::Footage { item } = &layer.source && let Some(effectcraft_project::ItemKind::Footage(f)) = project.item(*item).map(|i| &i.kind) && f.has_video diff --git a/crates/engine/src/background/remote.rs b/crates/engine/src/background/remote.rs index 27f9396e1..aea210ec5 100644 --- a/crates/engine/src/background/remote.rs +++ b/crates/engine/src/background/remote.rs @@ -20,13 +20,16 @@ //! calls [`FrameServer::resume`] for the next. Backend Auto picks CPU or GPU per comp from //! the frames' total times ([`effectcraft_render::AutoPick`]). //! - Disk cache: a render request may carry the frame's disk-cache key ([`FrameMsg::Render`] -//! `disk`); the transport stores the finished frame under it (the browser: in the Origin -//! Private File System) and reports [`FrameReply::Stored`]. +//! `disk`); the transport stores the finished frame under it with the source files it read +//! ([`FrameServer::take_files`], `disk_cache::frame_entry`; the browser: in the Origin Private +//! File System) and reports [`FrameReply::Stored`]. A frame whose files can't all be named is +//! not stored. The page checks an entry's files before it uses it (#3184). //! - Layer buffers on disk: with `layers`, the server's layer cache is backed by a //! [`PrefetchStore`] ([`FrameServer::set_layer_store`]): before the request the transport //! fetches the entries named in `prefetch` (recent misses that are on disk, chosen by the -//! page, `disk_cache::LayerPrefetch`), so the lookups in the middle of the render find them; -//! the reply lists the layer keys that missed and those served from the store +//! page, `disk_cache::LayerPrefetch`), so the lookups in the middle of the render find them +//! (each entry keeps the source files its buffer was made of and is used only while they are +//! unchanged, #3184); the reply lists the layer keys that missed and those served from the store //! ([`FrameReply::Frame`] `layer_misses` / `layer_hits`), and slow buffers queue up for the //! transport to write after the frame ([`PrefetchStore::take_writes`], reported as //! [`FrameReply::Stored`] with `layer`). @@ -37,6 +40,7 @@ use std::sync::Arc; use effectcraft_project::{ItemId, Project}; +use effectcraft_render::reads::{ReadFiles, ReadLog}; use effectcraft_render::{Accelerator, AutoKey, Backend, ExprHost, FootageSource, Image, LayerCache, LayerStore, PrefetchStore, RenderOpts, Renderer}; use effectcraft_time::Tick; use serde::{Deserialize, Serialize}; @@ -177,7 +181,7 @@ pub enum FrameReply { passes: u32, #[serde(default)] gpu: bool, - /// Layer-cache lookups that missed the store (salted keys, 32 hex digits). + /// Layer-cache lookups that missed the store (disk keys, 32 hex digits). #[serde(default, rename = "layerMisses", skip_serializing_if = "Vec::is_empty")] layer_misses: Vec, /// Layer buffers served from the store (fetched from disk). @@ -260,11 +264,12 @@ pub struct FrameServer { revision: Option, /// The frame being rendered in passes. pending: Option, - /// The disk cache's layer buffers (see the module docs) and the project's salt. + /// The disk cache's layer buffers (see the module docs). layer_store: Option>, - salt: u64, /// Size and content hash for footage paths with no native metadata (the browser file table). virtual_id: Option Option<(u64, u64)> + Send + Sync>>, + /// The source files the last finished frame read ([`FrameServer::take_files`]). + files: Option, } /// A frame whose readbacks are in flight. @@ -284,6 +289,7 @@ struct Pending { pub const MAX_PASSES: u32 = 24; impl FrameServer { + /// A frame server over `footage` and the optional expression host, with an empty layer cache. pub fn new(footage: Arc, expr: Option>) -> FrameServer { FrameServer { footage, @@ -295,17 +301,30 @@ impl FrameServer { revision: None, pending: None, layer_store: None, - salt: 0, virtual_id: None, + files: None, } } - /// Identity of virtual footage files, used when native metadata cannot describe the path. - /// Called from `FrameServer::load`, not from the UI thread's cache lookup. + /// Identity of virtual footage files, used when native metadata cannot describe the path: + /// what the disk entries of frames and layer buffers keep for them and are checked against. pub fn set_virtual_id(&mut self, id: Option Option<(u64, u64)> + Send + Sync>>) { + self.cache.set_virtual_id(id.clone()); self.virtual_id = id; } + /// The source files the frame of the last [`FrameReply::Frame`] read, with their identities + /// as read: what its disk entry keeps (`disk_cache::frame_entry`). Taken once. + pub fn take_files(&mut self) -> Option { + self.files.take() + } + + /// The identity of footage paths with no native metadata (the browser file table), to store + /// with a frame's disk entry. + pub fn virtual_id(&self) -> Option<&Arc Option<(u64, u64)> + Send + Sync>> { + self.virtual_id.as_ref() + } + /// The store behind the layer cache when a request asks for `layers` (the browser: the /// disk cache's layer buffers, fetched before the request). pub fn set_layer_store(&mut self, store: Option>) { @@ -316,11 +335,6 @@ impl FrameServer { self.layer_store.as_ref() } - /// The salt of the replica's layer keys on disk (keys are `(salt, key)`). - pub fn salt(&self) -> u64 { - self.salt - } - /// Render with `accel` when the request's backend asks for it (the worker's GPU). Its miss /// gate keeps placeholder buffers of unfinished passes out of the layer cache. pub fn set_accel(&mut self, accel: Option>) { @@ -347,8 +361,6 @@ impl FrameServer { fn load(&mut self, revision: u64) -> Result<(), String> { let p = Project::from_json(&self.doc.to_string()).map_err(|e| e.to_string())?; - self.salt = effectcraft_render::disk_cache::footage_salt_with(&p, self.virtual_id.as_deref()); - self.cache.set_store_salt(self.salt); self.project = Some(Arc::new(p)); self.revision = Some(revision); Ok(()) @@ -392,7 +404,7 @@ impl FrameServer { } FrameMsg::Render { id, revision, comp, time, opts, layers, .. } => { let store = self.layer_store.clone().filter(|_| layers); - self.cache.set_store(store.map(|s| (s as Arc, self.salt))); + self.cache.set_store(store.map(|s| s as Arc)); let fail = |error: String, stale: bool| vec![(FrameReply::Failed { id, error, stale }, None)]; if self.revision != Some(revision) { return fail(format!("the replica is at {:?}, not {revision}", self.revision), true); @@ -432,12 +444,16 @@ impl FrameServer { /// Run the next pass of the frame in progress; its reply once it is done. pub fn resume(&mut self) -> Vec<(FrameReply, Option>)> { let Some(mut pr) = self.pending.take() else { return vec![] }; + self.files = None; pr.passes += 1; let accel = self.accel.clone().filter(|_| pr.opts.backend != Backend::Cpu); let give_up = pr.passes > MAX_PASSES; + // (each pass renders the whole frame: the last one's files are the frame's) + let reads = ReadLog::new(); let mut r = Renderer::new(&pr.project, self.footage.as_ref(), pr.opts); r.expr = self.expr.as_deref(); r.cache = Some(&self.cache); + r.reads = Some(&reads); if !give_up { r.accel = accel.as_deref(); } @@ -451,6 +467,7 @@ impl FrameServer { } let ms = pr.t0.elapsed().as_secs_f64() * 1000.0; let gpu = r.accel.is_some() && r.active_accel().is_some(); + self.files = Some(reads.files()); let hex = |v: Vec| v.into_iter().map(|k| format!("{k:032x}")).collect::>(); let (layer_misses, layer_hits) = match &self.layer_store { Some(st) => (hex(st.take_misses()), hex(st.take_hits())), diff --git a/crates/engine/src/background/remote/tests.rs b/crates/engine/src/background/remote/tests.rs index e2db10b8e..e6d797f9d 100644 --- a/crates/engine/src/background/remote/tests.rs +++ b/crates/engine/src/background/remote/tests.rs @@ -69,7 +69,8 @@ fn layer_buffers_go_through_the_disk_store() { assert!(!layer_misses.is_empty() && layer_hits.is_empty()); let writes = store_a.take_writes(); assert!(!writes.is_empty()); - assert!(writes.iter().all(|(k, _)| (*k >> 64) as u64 == a.salt()), "keys are salted per project"); + // Sealed with the files each buffer was made of (none here: the demo reads no footage). + assert!(writes.iter().all(|(_, e)| effectcraft_render::disk_cache::read_layer_entry(e).is_some_and(|(_, f)| f.is_empty()))); let mut index = DiskIndex::default(); let mut disk = std::collections::HashMap::new(); for (k, e) in writes { @@ -375,3 +376,138 @@ fn every_frame_begin_has_a_frame_end() { assert!(matches!(w.handle(req(6, s.revision, comp, cpu))[0].0, FrameReply::Frame { id: 6, .. })); assert_eq!(load(), (3, 3)); } + +/// Footage that reads one file from memory (the browser's file table) for every frame. +struct OneVirtualFile { + /// Tell which files it reads (else every render's list is incomplete). + records: bool, +} + +impl FootageSource for OneVirtualFile { + fn frame(&self, _: ItemId, _: &effectcraft_project::Footage, _: Tick) -> Option> { + Some(Arc::new(Image::filled(4, 4, [1.0, 0.5, 0.0, 1.0]))) + } + fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) { + if self.records { + reads.note(effectcraft_render::reads::ReadFile::new("/clip.png", effectcraft_render::reads::FileStamp::Virtual)); + } else { + reads.mark_incomplete(); + } + f() + } +} + +/// The worker hands the transport the files each frame read, so its disk entry keeps them; the +/// page reads the entry back only while those files are unchanged. A frame whose files can't +/// all be named is not stored (#3184). +#[test] +fn the_worker_reports_the_files_of_each_frame_for_its_disk_entry() { + use effectcraft_project::{Comp, Footage, ItemKind, LayerSource, Project, build}; + use effectcraft_render::disk_cache::{frame_entry, read_frame_entry}; + use effectcraft_time::FrameRate; + let mut p = Project::default(); + let f = Footage { path: "/clip.png".into(), width: 4, height: 4, duration: Tick::from_seconds_f64(1.0), has_video: true, ..Default::default() }; + let fid = p.add_item("clip.png", effectcraft_color::Label::None, None, ItemKind::Footage(f)); + let mut comp = Comp::new(4, 4, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + comp.layers = vec![build::layer(&mut p, &comp, "clip", LayerSource::Footage { item: fid }, (4, 4), None)].into(); + let cid = p.add_item("C", effectcraft_color::Label::None, None, ItemKind::Comp(comp.into())); + let id = |mark: u64| move |path: &str| (path == "/clip.png").then_some((10, mark)); + let (one, two) = (id(1), id(2)); + for records in [true, false] { + let mut w = FrameServer::new(Arc::new(OneVirtualFile { records }), None); + w.set_virtual_id(Some(Arc::new(one))); + let mut m = Mirror::default(); + assert!(w.handle(round(&m.sync(1, &p).unwrap())).is_empty()); + assert!(w.take_files().is_none(), "no frame yet"); + let out = w.handle(round(&FrameMsg::Render { + id: 1, + revision: 1, + comp: cid, + time: Tick::ZERO, + opts: Box::default(), + disk: Some(format!("{:032x}", 5u128)), + layers: false, + prefetch: vec![], + })); + let (FrameReply::Frame { width, height, .. }, Some(px)) = &out[0] else { panic!("{:?}", out[0].0) }; + let files = w.take_files().expect("the frame's files"); + assert!(w.take_files().is_none(), "taken once"); + let vid = w.virtual_id().cloned(); + let entry = frame_entry(*width, *height, px, &files, vid.as_deref()); + if !records { + assert!(!files.is_complete() && entry.is_none(), "an incomplete list is not stored"); + continue; + } + assert_eq!(files.files().iter().map(|f| &*f.path).collect::>(), ["/clip.png"]); + let entry = entry.expect("stored"); + assert_eq!(read_frame_entry(&entry, Some(&one)).map(|f| f.rgba), Some(px.clone())); + assert!(read_frame_entry(&entry, Some(&two)).is_none(), "the file was replaced: a miss"); + } +} + +/// No salt work at all (#3184, replacing the background salt of #3106): syncing a project with a +/// large image sequence nobody shows, patching it and rendering with the layer store never asks +/// the file hook about the sequence (the salt asked about every file at every sync). Only the +/// file the rendered buffer reads is asked about: to store its entry and, in another worker, to +/// check it before the buffer from the store is used. +#[test] +fn the_worker_asks_only_about_the_files_its_buffers_read() { + use effectcraft_project::{Comp, Footage, FootageKind, ItemKind, LayerSource, Project, build}; + use effectcraft_time::FrameRate; + use std::sync::Mutex; + let mut p = Project::default(); + let f = Footage { path: "/clip.png".into(), width: 4, height: 4, has_video: true, ..Default::default() }; + let fid = p.add_item("clip.png", effectcraft_color::Label::None, None, ItemKind::Footage(f)); + let seq = Footage { + kind: FootageKind::Sequence, + path: "/seq/f_00000.png".into(), + sequence: (0..5000).map(|i| format!("/seq/f_{i:05}.png")).collect(), + width: 4, + height: 4, + has_video: true, + ..Default::default() + }; + p.add_item("seq", effectcraft_color::Label::None, None, ItemKind::Footage(seq)); + let mut comp = Comp::new(4, 4, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + comp.layers = vec![build::layer(&mut p, &comp, "clip", LayerSource::Footage { item: fid }, (4, 4), None)].into(); + let cid = p.add_item("C", effectcraft_color::Label::None, None, ItemKind::Comp(comp.into())); + let asked: Arc>> = Arc::default(); + let worker = || { + let mut w = FrameServer::new(Arc::new(OneVirtualFile { records: true }), None); + let seen = asked.clone(); + w.set_virtual_id(Some(Arc::new(move |path: &str| { + seen.lock().unwrap().push(path.to_string()); + (path == "/clip.png").then_some((10, 1)) + }))); + let store = Arc::new(PrefetchStore::new(1 << 20).with_min_ms(0)); + w.set_layer_store(Some(store.clone())); + (w, store) + }; + let render = |w: &mut FrameServer, revision: u64, prefetch: Vec| { + let out = w.handle(round(&FrameMsg::Render { id: 1, revision, comp: cid, time: Tick::ZERO, opts: Box::default(), disk: None, layers: true, prefetch })); + let FrameReply::Frame { layer_hits, .. } = &out[0].0 else { panic!("{:?}", out[0].0) }; + layer_hits.len() + }; + // Worker A: synced, patched, rendered; its buffer is queued with its file. + let (mut a, store_a) = worker(); + let mut m = Mirror::default(); + assert!(a.handle(round(&m.sync(1, &p).unwrap())).is_empty()); + let mut edited = p.clone(); + edited.item_mut(cid).unwrap().name = "Renamed".into(); + assert!(a.handle(round(&m.sync(2, &edited).unwrap())).is_empty()); + assert!(asked.lock().unwrap().is_empty(), "syncing looks at no file"); + assert_eq!(render(&mut a, 2, vec![]), 0); + let writes = store_a.take_writes(); + assert!(!writes.is_empty(), "the clip's buffer is stored"); + // Worker B: fetched before its frame, checked, used. + let (mut b, store_b) = worker(); + let mut m = Mirror::default(); + assert!(b.handle(round(&m.sync(2, &edited).unwrap())).is_empty()); + for (k, e) in &writes { + assert!(store_b.provide(*k, e)); + } + assert!(render(&mut b, 2, writes.iter().map(|(k, _)| format!("{k:032x}")).collect()) > 0, "served from the store"); + let asked = asked.lock().unwrap(); + assert!(!asked.is_empty(), "the hook names the buffer's file"); + assert!(asked.iter().all(|p| p == "/clip.png"), "only the file the buffer read: {:?}", &asked[..asked.len().min(5)]); +} diff --git a/crates/engine/src/cmd/effect.rs b/crates/engine/src/cmd/effect.rs index 69bdb5a4a..1d352d192 100644 --- a/crates/engine/src/cmd/effect.rs +++ b/crates/engine/src/cmd/effect.rs @@ -345,6 +345,7 @@ fn warning(s: &mut Session, p: &Value) -> Result { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; Ok(json!({"effect": uid, "warning": effectcraft_effects::warning(g, &mut |pr| ctx.value(layer, pr))})) } @@ -378,6 +379,7 @@ fn pick_color(s: &mut Session, p: &Value) -> Result { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let mut r = effectcraft_render::Renderer::new(&s.project, &*s.footage, effectcraft_render::RenderOpts::default()); r.expr = s.expr.as_deref(); @@ -515,6 +517,7 @@ impl CryptoProbe { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let params = effectcraft_effects::flatten_params(g, &mut |pr| ctx.value(layer, pr)); let aux = r.layer_aux(&ctx, layer).ok_or(NO_CRYPTO)?; @@ -586,6 +589,7 @@ fn probe_key<'a>(s: &'a Session, p: &Value, cmd: &str) -> Result<(ProbeKey, Tick footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let frame = probe_frame(&ctx, layer, pos); let o = s.render_opts(Default::default()); @@ -780,6 +784,7 @@ fn crypto_hit(s: &Session, p: &Value, cmd: &str) -> Result { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let selection = effectcraft_effects::flatten_params(g, &mut |pr| ctx.value(layer, pr)).s("selection").to_string(); let driven = prop.expr.as_ref().is_some_and(|e| e.enabled); diff --git a/crates/engine/src/cmd/layer/parent.rs b/crates/engine/src/cmd/layer/parent.rs index 3611ef047..cd923c950 100644 --- a/crates/engine/src/cmd/layer/parent.rs +++ b/crates/engine/src/cmd/layer/parent.rs @@ -249,6 +249,7 @@ pub(crate) fn parent_fixes(s: &Session, cid: ItemId, ids: &[LayerId], parent: Op footage: Some(s.footage.as_ref()), property_overrides: &[], media_overrides: &[], + reads: None, }; let space = |id: Option| -> effectcraft_geom::Mat3 { id.and_then(|i| comp.layer(i)).filter(|l| !l.is_3d()).map(|l| ectx.layer_to_comp(l).0).unwrap_or(effectcraft_geom::Mat3::IDENTITY) diff --git a/crates/engine/src/cmd/layer_time.rs b/crates/engine/src/cmd/layer_time.rs index 4ecf725ec..2f8661885 100644 --- a/crates/engine/src/cmd/layer_time.rs +++ b/crates/engine/src/cmd/layer_time.rs @@ -194,6 +194,7 @@ fn freeze(s: &mut Session, p: &Value, last: bool) -> Result { footage: Some(s.footage.as_ref()), property_overrides: &[], media_overrides: &[], + reads: None, }; let now: Vec<(effectcraft_project::LayerId, f64)> = comp .layers diff --git a/crates/engine/src/cmd/path_nulls.rs b/crates/engine/src/cmd/path_nulls.rs index 93d71cd67..6da43afde 100644 --- a/crates/engine/src/cmd/path_nulls.rs +++ b/crates/engine/src/cmd/path_nulls.rs @@ -73,6 +73,8 @@ struct PathInfo { span: (f64, f64), } +/// Reads a mask or shape path at the current time and returns its comp-space vertices, value, expression reference and layer name. +/// Errors when the property is missing, is not a path or has no vertices. fn info(s: &Session, cid: ItemId, lid: LayerId, uid: Uid, cmd: &str) -> Result { let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; let layer = comp.layer(lid).ok_or(EngineError::NoComp)?; @@ -87,6 +89,7 @@ fn info(s: &Session, cid: ItemId, lid: LayerId, uid: Uid, cmd: &str) -> Result

Result Result { convert_one_expr(s, cid, lid, uid) } +/// Replaces the expression on one property with keyframes sampled from its evaluated values. +/// Returns the number of properties converted. fn convert_one_expr(s: &mut Session, cid: ItemId, lid: LayerId, uid: Uid) -> Result { let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?.clone(); let layer = comp.layer(lid).ok_or(EngineError::NoComp)?.clone(); @@ -1397,6 +1399,7 @@ fn convert_one_expr(s: &mut Session, cid: ItemId, lid: LayerId, uid: Uid) -> Res footage: Some(s.footage.as_ref()), property_overrides: &[], media_overrides: &[], + reads: None, }; keys.push(Keyframe::new(layer.layer_time(t), ctx.value(&layer, &pr))); t += fd; diff --git a/crates/engine/src/cmd/puppet.rs b/crates/engine/src/cmd/puppet.rs index d894516c2..bd20bd885 100644 --- a/crates/engine/src/cmd/puppet.rs +++ b/crates/engine/src/cmd/puppet.rs @@ -39,8 +39,17 @@ pub fn puppet_eval(s: &Session, cid: ItemId, lid: LayerId, fx_uid: Uid, t: Tick) let fx = layer.effects()?; let index = fx.groups().position(|g| g.uid == fx_uid)?; let g = fx.groups().nth(index)?; - let ctx = - EvalCtx { project: &s.project, comp_id: cid, comp, time: t, expr: s.expr.as_deref(), footage: None, property_overrides: &[], media_overrides: &[] }; + let ctx = EvalCtx { + project: &s.project, + comp_id: cid, + comp, + time: t, + expr: s.expr.as_deref(), + footage: None, + property_overrides: &[], + media_overrides: &[], + reads: None, + }; let mut r = Renderer::new(&s.project, &*s.footage, RenderOpts::default()); r.expr = s.expr.as_deref(); r.cache = Some(&s.layer_cache); diff --git a/crates/engine/src/cmd/rig3d.rs b/crates/engine/src/cmd/rig3d.rs index dbcaa3c3c..58a4281a8 100644 --- a/crates/engine/src/cmd/rig3d.rs +++ b/crates/engine/src/cmd/rig3d.rs @@ -47,6 +47,7 @@ impl Env { fn of(s: &Session, cid: ItemId) -> Env { Env { time: s.time_of(cid), expr: s.expr.clone(), footage: s.footage.clone() } } + /// The evaluation context of `comp` in `proj` at the session's current time. fn ctx<'a>(&'a self, proj: &'a Project, cid: ItemId, comp: &'a Comp) -> EvalCtx<'a> { EvalCtx { project: proj, @@ -57,6 +58,7 @@ impl Env { footage: Some(self.footage.as_ref()), property_overrides: &[], media_overrides: &[], + reads: None, } } } diff --git a/crates/engine/src/cmd/three_d.rs b/crates/engine/src/cmd/three_d.rs index 009a33b2e..3355b68e3 100644 --- a/crates/engine/src/cmd/three_d.rs +++ b/crates/engine/src/cmd/three_d.rs @@ -418,14 +418,24 @@ fn set_view_camera(s: &mut Session, p: &Value) -> Result { Ok(json!({"view": v.id()})) } +/// Returns the active comp's 3D view state, with the comp size, for the 3D view commands. fn get_3d(s: &mut Session, p: &Value) -> Result { let cid = comp_id(s, p)?; let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?; let (w, h) = (comp.width as f64, comp.height as f64); let vs = s.state.views3d.get(&cid).cloned().unwrap_or_default(); let t = s.state.times.get(&cid).copied().unwrap_or(Tick::ZERO); - let ctx = - EvalCtx { project: &s.project, comp_id: cid, comp, time: t, expr: s.expr.as_deref(), footage: None, property_overrides: &[], media_overrides: &[] }; + let ctx = EvalCtx { + project: &s.project, + comp_id: cid, + comp, + time: t, + expr: s.expr.as_deref(), + footage: None, + property_overrides: &[], + media_overrides: &[], + reads: None, + }; let cam = vs.override_camera(w, h).unwrap_or_else(|| camera::active_camera(&ctx)); let f = cam.forward(); let lights: Vec = comp @@ -567,8 +577,17 @@ fn auto_orient(s: &mut Session, p: &Value) -> Result { /// through each layer's world matrix; cameras and lights contribute their position). fn layer_points(s: &Session, cid: ItemId, ids: &[LayerId], t: Tick) -> Vec { let Some(comp) = s.project.comp(cid) else { return vec![] }; - let ctx = - EvalCtx { project: &s.project, comp_id: cid, comp, time: t, expr: s.expr.as_deref(), footage: None, property_overrides: &[], media_overrides: &[] }; + let ctx = EvalCtx { + project: &s.project, + comp_id: cid, + comp, + time: t, + expr: s.expr.as_deref(), + footage: None, + property_overrides: &[], + media_overrides: &[], + reads: None, + }; let mut out = vec![]; for l in comp.layers.iter().filter(|l| ids.contains(&l.id)) { let m = ctx.world_matrix(l); diff --git a/crates/engine/src/footage/availability.rs b/crates/engine/src/footage/availability.rs index 7ad2bc03f..75e2a1e1c 100644 --- a/crates/engine/src/footage/availability.rs +++ b/crates/engine/src/footage/availability.rs @@ -215,20 +215,18 @@ impl Scanner { } } -/// A disk observation prepared entirely by the existing worker, including the potentially -/// expensive sequence stamps used for disk-cache keys. The UI only swaps a current snapshot. +/// A disk observation prepared entirely by the existing worker. The UI only swaps a current +/// snapshot (the disk cache needs nothing: its entries check their own files when read, #3184). pub(crate) struct Update { before: Arc, after: Arc, paths: BTreeSet, newly_missing: usize, - cache_enabled: bool, - salt: Option<(u64, u64)>, } -/// Prepare a replacement only when availability changed. Project cloning and optional disk -/// salt computation belong on the worker; apply later rejects an outdated project snapshot. -pub(crate) fn prepare(project: &Arc, sources: &[Source], missing: &[bool], cache_enabled: bool) -> Option { +/// Prepare a replacement only when availability changed. Project cloning belongs on the +/// worker; apply later rejects an outdated project snapshot. +pub(crate) fn prepare(project: &Arc, sources: &[Source], missing: &[bool]) -> Option { let mut after = project.clone(); let mut paths = BTreeSet::new(); let mut newly_missing = 0; @@ -245,22 +243,17 @@ pub(crate) fn prepare(project: &Arc, sources: &[Source], missing: &[boo if paths.is_empty() { return None; } - let salt = cache_enabled.then(|| (Session::footage_fingerprint(&after), crate::render::disk_cache::footage_salt(&after))); - Some(Update { before: project.clone(), after, paths, newly_missing, cache_enabled, salt }) + Some(Update { before: project.clone(), after, paths, newly_missing }) } impl Update { - /// False means a newer project or cache configuration needs a fresh observation. + /// False means a newer project needs a fresh observation. pub fn apply(self, s: &mut Session) -> bool { - if !Arc::ptr_eq(&s.project, &self.before) || s.disk_cache.is_some() != self.cache_enabled { + if !Arc::ptr_eq(&s.project, &self.before) { return false; } let dirty = s.is_dirty(); s.project = self.after; - if let Some((fingerprint, salt)) = self.salt { - s.disk_salt = Some((fingerprint, salt)); - s.layer_cache.set_disk_salt(salt); - } s.bump(); if !dirty { s.mark_saved(); @@ -371,7 +364,7 @@ mod tests { fn stale_observation_cannot_mark_a_replacement_missing() { let (mut s, id) = session(Arc::new(Disk::default())); let old = sources(&s); - let update = prepare(&s.project, &old, &[true, true], false).unwrap(); + let update = prepare(&s.project, &old, &[true, true]).unwrap(); if let ItemKind::Footage(f) = &mut Arc::make_mut(&mut s.project).item_mut(id).unwrap().kind { f.path = "/new.png".into(); } @@ -398,9 +391,12 @@ mod tests { assert_eq!(disk.looks.load(Ordering::Relaxed), 2, "one file per source, no scan of an unmounted folder"); } + /// An availability change does no disk-cache work (#3184): the cache stays attached and + /// no file is looked at for it, on the worker or when the UI applies the observation (it + /// used to stamp every footage file for a new layer salt). #[test] - fn applying_prepared_cache_salt_does_not_restat_files_on_the_ui_thread() { - let dir = std::env::temp_dir().join(format!("ec-availability-salt-{}", std::process::id())); + fn applying_an_availability_change_does_no_disk_cache_work() { + let dir = std::env::temp_dir().join(format!("ec-availability-cache-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let file = dir.join("plate.png"); std::fs::write(&file, b"plate").unwrap(); @@ -413,16 +409,13 @@ mod tests { ); let cache = crate::render::disk_cache::DiskCache::open(dir.join("cache"), 1 << 20).unwrap(); s.disk_cache = Some(cache.clone()); - s.layer_cache.set_disk(Some((cache, 0))); + s.layer_cache.set_disk(Some(cache.clone())); s.mark_saved(); - let update = prepare(&s.project, &sources(&s), &[false], true).unwrap(); - let expected = update.salt.unwrap().1; - // The filesystem can change again after the worker's observation. Applying it must - // retain that observation's salt, rather than scan the disk a second time on the UI. + let update = prepare(&s.project, &sources(&s), &[false]).unwrap(); std::fs::remove_file(file).unwrap(); - assert_ne!(crate::render::disk_cache::footage_salt(&update.after), expected); assert!(update.apply(&mut s)); - assert_eq!(s.disk_salt.map(|(_, salt)| salt), Some(expected)); + assert!(s.layer_cache.disk().is_some_and(|d| Arc::ptr_eq(&d, &cache)), "still attached"); + assert_eq!(cache.file_checks(), 0, "no file looked at for the disk cache"); assert!(!s.is_dirty()); drop(s); std::fs::remove_dir_all(dir).unwrap(); diff --git a/crates/engine/src/footage/footage_check.rs b/crates/engine/src/footage/footage_check.rs index 9e941a6ed..42401d195 100644 --- a/crates/engine/src/footage/footage_check.rs +++ b/crates/engine/src/footage/footage_check.rs @@ -272,7 +272,7 @@ mod tests { let id = source.id; let mut recovered = source.footage.clone(); recovered.path = "/new/project/plate.png".into(); - let observed = super::super::availability::prepare(&s.project, std::slice::from_ref(&source), &[true], false).unwrap(); + let observed = super::super::availability::prepare(&s.project, std::slice::from_ref(&source), &[true]).unwrap(); assert!(observed.apply(&mut s)); assert!(matches!(&s.project.item(id).unwrap().kind, ItemKind::Footage(f) if f.missing)); let result = apply(&mut s, vec![(source, Found::Moved(Box::new(recovered)))]); diff --git a/crates/engine/src/footage/footage_reload.rs b/crates/engine/src/footage/footage_reload.rs index 7065620af..e56e16a96 100644 --- a/crates/engine/src/footage/footage_reload.rs +++ b/crates/engine/src/footage/footage_reload.rs @@ -6,7 +6,8 @@ //! or PDF page an item shows and its interpretation (alpha, conformed frame rate, pixel aspect, //! fields, colour). A Photoshop layer is found again by name when layers were added or removed //! around it. The media layer then forgets the file and the layer cache is emptied, so the next -//! render reads the new pixels. +//! render reads the new pixels; File ▸ Reload Footage also deletes the disk cache entries made of +//! the file (#3184). //! //! The automatic part compares each footage file's size and modification time with what they //! were when the file was last seen. The desktop app keeps a [`Watcher`]: a worker thread that @@ -76,7 +77,6 @@ pub struct Watched { reload: BTreeSet, sources: Arc>, project: Option>, - cache_enabled: bool, reload_mode: String, stored_paths: crate::doc_path::StoredPaths, } @@ -85,8 +85,7 @@ impl PartialEq for Watched { /// Compare watched inputs, excluding unrelated project edits whose latest Arc is refreshed /// separately without waking the worker or restarting partial sequence scans. fn eq(&self, other: &Self) -> bool { - self.cache_enabled == other.cache_enabled - && self.stored_paths == other.stored_paths + self.stored_paths == other.stored_paths && self.reload_mode == other.reload_mode && self.reload == other.reload && (Arc::ptr_eq(&self.sources, &other.sources) || self.sources == other.sources) @@ -100,7 +99,6 @@ pub fn watched(s: &Session) -> Watched { reload: automatic_paths(s), sources: Arc::new(if s.check_footage_on_open { availability::sources(s) } else { Vec::new() }), project: Some(s.project.clone()), - cache_enabled: s.disk_cache.is_some(), reload_mode: s.prefs.import.auto_reload_footage.clone(), stored_paths: s.stored_paths.clone(), } @@ -139,10 +137,10 @@ pub struct Scan { } impl Scan { - /// Prepare availability and cache state before the session applies the observation. + /// Prepare the availability change before the session applies the observation. /// Automatic callers construct this result on the worker; explicit commands run inline. fn new(watched: Watched, observation: Observation, policy: ReloadPolicy) -> Self { - let update = watched.project.as_ref().and_then(|p| availability::prepare(p, &watched.sources, &observation.missing, watched.cache_enabled)); + let update = watched.project.as_ref().and_then(|p| availability::prepare(p, &watched.sources, &observation.missing)); Self { watched, observation, update, policy } } @@ -281,7 +279,17 @@ fn reload(s: &mut Session, ids: &[ItemId], undoable: bool) -> crate::Result<(usi } let n = updates.len(); apply_reloads(s, updates, undoable)?; - forget(s, &paths); + forget_media(s, &paths); + // An explicit reload also reads files again that kept their size and time (the check on hit + // can't tell those apart): their entries in the disk cache go (#3184), when the cache is + // switched on if it is off now. An automatic one only follows a changed stamp, which the + // check catches. + if undoable { + match &s.disk_cache { + Some(dc) => dc.drop_files(paths.iter().map(String::as_str)), + None => s.pending_disk_drops.extend(paths), + } + } Ok((n, failed)) } @@ -320,14 +328,9 @@ fn apply_reloads(s: &mut Session, updates: Vec<(ItemId, Footage)>, undoable: boo Ok(()) } -/// Make the media layer and the layer cache let go of `paths`, so renders read them again. -fn forget(s: &mut Session, paths: &BTreeSet) { - forget_media(s, paths); - s.refresh_disk_salt(true); -} - -/// Invalidate decoded and rendered buffers without probing the disk for a new salt. -/// Availability updates already carry their worker-computed salt; content reload refreshes it. +/// Make the media layer and the layer cache let go of `paths`, so renders read them again. Disk +/// cache entries check the files they were made of when read (#3184); File ▸ Reload Footage +/// also drops the entries of its files ([`reload`]). pub(crate) fn forget_media(s: &mut Session, paths: &BTreeSet) { for p in paths { s.footage.forget(p); @@ -374,7 +377,7 @@ pub fn apply_stamps(s: &mut Session, now: Vec<(String, Option)>) -> crate return Ok(json!({"files": 0, "reloaded": 0})); } // Changed frames of image sequences (their items keep their metadata). - forget(s, &changed); + forget_media(s, &changed); let files = changed.len(); s.toast(if reloaded > 0 { format!("Reloaded {reloaded} footage item(s) changed on disk") @@ -391,7 +394,6 @@ pub(crate) fn command(s: &mut Session, _: &Value) -> crate::Result { reload: reload_paths(s), sources: Arc::new(availability::sources(s)), project: Some(s.project.clone()), - cache_enabled: s.disk_cache.is_some(), reload_mode: s.prefs.import.auto_reload_footage.clone(), stored_paths: s.stored_paths.clone(), }; @@ -452,12 +454,7 @@ impl Watcher { pub fn update(&mut self, s: &Session) { let same_sources = availability::sources_match(s, &self.paths.sources); let same_data = self.paths.project.as_ref().is_some_and(|before| data_paths(before).eq(data_paths(&s.project))); - if same_sources - && same_data - && self.paths.stored_paths == s.stored_paths - && self.paths.reload_mode == s.prefs.import.auto_reload_footage - && self.paths.cache_enabled == s.disk_cache.is_some() - { + if same_sources && same_data && self.paths.stored_paths == s.stored_paths && self.paths.reload_mode == s.prefs.import.auto_reload_footage { let project = Some(s.project.clone()); self.paths.project = project.clone(); self.pending.lock().unwrap_or_else(PoisonError::into_inner).paths.project = project; diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index e50b15d04..599aba1b7 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -97,7 +97,7 @@ pub use error::{EngineError, Result}; pub use event::Event; pub use fonts::{FontRow, font_features, font_menu, font_preview, font_styles, text_families}; pub use services::{FsServices, Importer, Services}; -pub use session::{DiskSaltJob, upgrade_effects}; +pub use session::upgrade_effects; pub struct Session { pub project: Arc, @@ -171,6 +171,9 @@ pub struct Session { pub crypto_probe: Arc>, /// The persistent disk cache (Settings ▸ Media & Disk Cache), when enabled. pub disk_cache: Option>, + /// Files File ▸ Reload Footage reloaded while the disk cache was off: their entries are + /// dropped when it is switched on ([`Session::configure_disk_cache`]), before it is used. + pub pending_disk_drops: std::collections::BTreeSet, /// Settings (Preferences). pub prefs: prefs::Prefs, /// Bumped whenever settings change (frontends re-apply theme, labels…). @@ -254,14 +257,6 @@ 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, - /// The disk cache's footage salt and the footage it was taken for ([`Session::refresh_disk_salt`]). - pub disk_salt: Option<(u64, u64)>, - /// Take the disk cache's footage salt on a worker thread ([`Session::refresh_disk_salt`]): - /// frontends with an event loop (the desktop app) turn this on and call - /// [`Session::poll_disk_salt`] each frame; headless sessions take it at once. - pub disk_salt_background: bool, - /// The footage salt being taken in the background. - pub disk_salt_job: Option, /// 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>, @@ -356,6 +351,7 @@ impl Default for Session { layer_cache: Arc::new(LayerCache::default()), crypto_probe: Default::default(), disk_cache: None, + pending_disk_drops: Default::default(), prefs: prefs::Prefs::default(), prefs_revision: 0, config: None, @@ -391,9 +387,6 @@ impl Default for Session { check_footage_on_open: false, stored_paths: Default::default(), footage_stamps: Default::default(), - disk_salt: None, - disk_salt_background: false, - disk_salt_job: None, aux_footage: None, script_calls: ScriptCalls::default(), } diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs index 31ed40c1d..18b0ff4cf 100644 --- a/crates/engine/src/session.rs +++ b/crates/engine/src/session.rs @@ -10,4 +10,4 @@ mod dispatch; mod edit; mod render; -pub use edit::{DiskSaltJob, upgrade_effects}; +pub use edit::upgrade_effects; diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index 28813e2ec..67a8e4dcb 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -97,114 +97,10 @@ impl Session { if Arc::ptr_eq(&self.project, &self.saved_project) { self.saved_revision = self.revision; } - self.refresh_disk_salt(false); self.refresh_aux_footage(); self.events.push(Event::ProjectChanged { revision: self.revision }); } - /// Salt the disk cache's layer keys with the project's footage files - /// ([`effectcraft_render::disk_cache::footage_salt`]) and attach the disk cache to the layer - /// cache with it. Taking it reads the size and time of every file, every frame of an image - /// sequence included, so it is taken again only when the footage items changed, or with - /// `force` (a project opened, footage reloaded, the disk cache attached), not at every edit: - /// with a few large sequences that cost each keystroke in a text field over half a second - /// (#1926). - /// - /// With [`Session::disk_salt_background`] the files are read on a worker thread: an imported - /// or opened project with thousands of sequence frames on a network share took half a minute - /// to stamp, all of it on the UI thread. Until [`Session::poll_disk_salt`] picks the salt up, - /// renders use the memory cache only (keys under the old salt may name other files, or - /// another project's items) and this returns `None`. - pub fn refresh_disk_salt(&mut self, force: bool) -> Option { - let Some(dc) = self.disk_cache.clone() else { - self.disk_salt = None; - self.disk_salt_job = None; - return None; - }; - self.poll_disk_salt(); - let footage = Self::footage_fingerprint(&self.project); - if !force { - if let Some((seen, salt)) = self.disk_salt - && seen == footage - { - return Some(salt); - } - if self.disk_salt_job.as_ref().is_some_and(|j| j.footage == footage) { - return None; - } - } - if self.disk_salt_background && !cfg!(target_arch = "wasm32") { - let project = self.project.clone(); - let slot: Arc>> = Arc::default(); - let out = slot.clone(); - let spawned = std::thread::Builder::new().name("ec-disk-salt".into()).spawn(move || { - let salt = effectcraft_render::disk_cache::footage_salt(&project); - if let Ok(mut s) = out.lock() { - *s = Some(salt); - } - }); - // Without a thread the salt is taken here, as before. - if let Ok(thread) = spawned { - self.disk_salt = None; - self.disk_salt_job = Some(DiskSaltJob { footage, slot, thread }); - self.layer_cache.set_disk(None); - return None; - } - } - self.disk_salt_job = None; - let salt = effectcraft_render::disk_cache::footage_salt(&self.project); - self.disk_salt = Some((footage, salt)); - self.layer_cache.set_disk(Some((dc, salt))); - Some(salt) - } - - /// Pick up a footage salt taken in the background ([`Session::refresh_disk_salt`]) and attach - /// the disk cache with it. Returns whether one is still being taken (frontends call this each - /// frame while it is). - pub fn poll_disk_salt(&mut self) -> bool { - let Some(job) = &self.disk_salt_job else { return false }; - // (looked at first: a worker that has finished has left its salt in the slot) - let finished = job.thread.is_finished(); - let salt = job.slot.lock().ok().and_then(|mut s| s.take()); - let footage = job.footage; - match salt { - Some(salt) => { - self.disk_salt_job = None; - if let Some(dc) = self.disk_cache.clone() { - self.disk_salt = Some((footage, salt)); - self.layer_cache.set_disk(Some((dc, salt))); - } - false - } - None if finished => { - // The worker stopped without a salt: the disk cache stays off until the next - // edit takes it again. - log::warn!("disk cache: the footage files could not be stamped; the disk cache is off until the next edit"); - self.disk_salt_job = None; - false - } - None => true, - } - } - - /// In-memory identity of the footage settings represented by a disk salt. Workers use the - /// same fingerprint when preparing availability changes without disk access on the UI. - pub(crate) fn footage_fingerprint(project: &effectcraft_project::Project) -> u64 { - use std::hash::{Hash, Hasher}; - let mut h = std::collections::hash_map::DefaultHasher::new(); - for (id, item) in &project.items { - if let effectcraft_project::ItemKind::Footage(f) = &item.kind { - id.hash(&mut h); - format!("{f:?}").hash(&mut h); - } - if let Some(proxy) = &item.proxy { - id.hash(&mut h); - format!("{proxy:?}").hash(&mut h); - } - } - h.finish() - } - /// Set a comp's Resolution. As in After Effects it is saved with the project (which becomes /// modified) but is no undo step, and it changes no pixels: the comp's cached frames stay /// valid ([`effectcraft_project::Comp::same_pixels`]). @@ -322,11 +218,6 @@ impl Session { } self.layer_cache.clear(); self.project = Arc::new(p); - // Its files may have changed since they were last stamped. - self.disk_salt = None; - // A salt still being taken is for the old project's files: dropped, so that the next refresh - // stamps the new project anew (a matching fingerprint would otherwise keep the stale job). - self.disk_salt_job = None; // The set of footage effects read the channels of is sent again, even when it is empty. self.aux_footage = None; self.history = History::default(); @@ -389,11 +280,3 @@ pub fn upgrade_effects(p: &mut Project) { p.next_id = next; p.schema = effectcraft_project::SCHEMA_VERSION; } - -/// A footage salt being taken on a worker thread ([`Session::refresh_disk_salt`]). -pub struct DiskSaltJob { - /// The footage it is taken for ([`Session::footage_fingerprint`]). - footage: u64, - slot: Arc>>, - thread: std::thread::JoinHandle<()>, -} diff --git a/crates/engine/src/settings/session_settings.rs b/crates/engine/src/settings/session_settings.rs index 056616201..8ade9f8a3 100644 --- a/crates/engine/src/settings/session_settings.rs +++ b/crates/engine/src/settings/session_settings.rs @@ -137,10 +137,6 @@ impl Session { // over the store of a roto test running beside it. Serialise them with the roto tests. #[cfg(test)] crate::tests::roto::hold_roto_cache_test_lock(); - // Another setting changed (the disk cache stays as it is): its salt still holds, and - // stamping every footage file again would cost each settings change seconds with large - // sequences. - let kept = self.disk_cache.as_ref().is_some_and(|c| c.folder() == folder.as_path()); let dc = match &self.disk_cache { Some(c) if c.folder() == folder.as_path() => { c.set_max_bytes(max); @@ -162,11 +158,17 @@ impl Session { } }, }; + // Files reloaded while the cache was off: their entries go before anything is read from + // it (until the writer has deleted them they are misses, and no frame is linked). + if !self.pending_disk_drops.is_empty() { + dc.drop_files(std::mem::take(&mut self.pending_disk_drops)); + } self.disk_cache = Some(dc.clone()); // The reserve applies from the start (a new folder's watcher is idle). self.poll_disk_space(); - // Attaches the disk cache to the layer cache once the footage salt is known. - self.refresh_disk_salt(!kept); + // Used at once (no file is looked at first: each entry checks the source files it lists + // when it is read, #3184). + self.layer_cache.set_disk(Some(dc.clone())); // Roto Brush / Object Matte segmentations persist there too (held weakly: switching the // disk cache off drops it). let store: std::sync::Arc = dc.clone(); diff --git a/crates/engine/src/tests/crypto_pick.rs b/crates/engine/src/tests/crypto_pick.rs index e3a8cd50c..82fe21e71 100644 --- a/crates/engine/src/tests/crypto_pick.rs +++ b/crates/engine/src/tests/crypto_pick.rs @@ -214,6 +214,7 @@ fn rendered_matte_of_pick(s: &mut Session, x: f64, y: f64, scale: f64) -> (Strin footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let buf = s.renderer(effectcraft_render::RenderOpts { scale, ..Default::default() }).content_buf(&ctx, layer).unwrap(); let (px, py) = buf.to_px([x, y]); diff --git a/crates/engine/src/tests/disk_cache.rs b/crates/engine/src/tests/disk_cache.rs index 4805d68fe..4c610dceb 100644 --- a/crates/engine/src/tests/disk_cache.rs +++ b/crates/engine/src/tests/disk_cache.rs @@ -76,11 +76,13 @@ fn disk_cache_settings_persistence_and_invalidation() { assert!(s2.layer_cache.disk().is_none()); } +/// The comp key comes from the project in memory: a footage file that changes on disk doesn't +/// change it (the entries check their own files, #3184); edits that change pixels do. #[test] -fn footage_changes_change_the_salt() { - let dir = folder("salt"); +fn comp_content_keys_come_from_memory() { + let dir = folder("keys"); let mut s = session(&dir); - let foot = std::env::temp_dir().join(format!("effectcraft-salt-{}.png", std::process::id())); + let foot = std::env::temp_dir().join(format!("effectcraft-keys-{}.png", std::process::id())); std::fs::write(&foot, b"one").unwrap(); s.edit("add", None, |p, _| { let f = effectcraft_project::Footage { @@ -108,12 +110,10 @@ fn footage_changes_change_the_salt() { Ok(()) }) .unwrap(); - let a = effectcraft_render::disk_cache::footage_salt(&s.project); - std::fs::write(&foot, b"two, longer").unwrap(); - let b = effectcraft_render::disk_cache::footage_salt(&s.project); - assert_ne!(a, b); let cid = project(&mut s); let k1 = effectcraft_render::disk_cache::comp_content_key(&s.project, cid); + std::fs::write(&foot, b"two, longer").unwrap(); + assert_eq!(effectcraft_render::disk_cache::comp_content_key(&s.project, cid), k1, "no file is looked at"); s.execute("prop.set", json!({"layer": "#1", "path": "transform/opacity", "value": 50})).unwrap(); let k2 = effectcraft_render::disk_cache::comp_content_key(&s.project, cid); assert_ne!(k1, k2); @@ -137,66 +137,51 @@ fn footage_changes_change_the_salt() { assert_ne!(set_proxy(&mut s, false), k3); } -/// The desktop app stamps footage files for the disk cache's salt on a worker thread: importing an -/// After Effects project with thousands of sequence frames on a network share stamped them all -/// inside the import's edit, freezing the UI for half a minute. Until the salt lands the disk -/// cache sits out (keys under the old salt may name other files); a newer footage change -/// supersedes a salt still being taken; a settings change keeps the salt. +/// No salt and no scan (#3184, replacing the background footage salt of #3106): the disk cache +/// is attached to the layer cache as soon as it is switched on, and stays attached through +/// edits that add footage (a large image sequence included), settings changes and opening a +/// project, none of which looks at a single file for it. The first render may use it at once. #[test] -fn footage_salt_is_taken_off_the_ui_thread_and_the_disk_cache_waits_for_it() { - let dir = folder("bg-salt"); +fn the_disk_cache_is_attached_at_once_and_edits_do_no_file_work() { + let dir = folder("attach"); let mut s = session(&dir); - s.disk_salt_background = true; - let at = |n: &str| std::env::temp_dir().join(format!("effectcraft-bg-salt-{}-{n}.png", std::process::id())); - let add = |s: &mut Session, path: &std::path::Path| { - let f = effectcraft_project::Footage { - path: path.to_string_lossy().to_string(), - kind: effectcraft_project::FootageKind::Still, - width: 1, - height: 1, - has_video: true, - ..Default::default() - }; - s.edit("add", None, |p, _| { - p.add_item("f.png", effectcraft_color::Label::None, None, effectcraft_project::ItemKind::Footage(f)); - Ok(()) - }) - .unwrap(); + let dc = s.disk_cache.clone().unwrap(); + let attached = |s: &Session| s.layer_cache.disk().is_some_and(|d| std::sync::Arc::ptr_eq(&d, &dc)); + assert!(attached(&s), "attached at once"); + // A sequence of 5000 frames that don't even exist. + let seq_dir = std::env::temp_dir().join(format!("effectcraft-no-scan-{}", std::process::id())); + let f = effectcraft_project::Footage { + path: seq_dir.join("f_00000.exr").to_string_lossy().to_string(), + kind: effectcraft_project::FootageKind::Sequence, + sequence: (0..5000).map(|i| seq_dir.join(format!("f_{i:05}.exr")).to_string_lossy().to_string()).collect(), + width: 8, + height: 8, + has_video: true, + ..Default::default() }; - let wait = |s: &mut Session| { - let t0 = std::time::Instant::now(); - while s.poll_disk_salt() { - assert!(t0.elapsed().as_secs() < 20, "the salt never landed"); - std::thread::sleep(std::time::Duration::from_millis(5)); - } - }; - assert!(s.layer_cache.disk().is_some(), "attached with its salt"); - std::fs::write(at("a"), b"one").unwrap(); - add(&mut s, &at("a")); - // The edit returned before the files were stamped, and the disk cache waits for the salt. - assert!(s.disk_salt.is_none() && s.layer_cache.disk().is_none()); - // Edits that leave the footage alone don't start over (and may pick up the salt meanwhile). - s.execute("comp.new", json!({"name": "C", "width": 8, "height": 8, "frameRate": 30, "duration": 1})).unwrap(); - let r = s.refresh_disk_salt(false); - assert!(r.is_none() || r == Some(effectcraft_render::disk_cache::footage_salt(&s.project)), "{r:?}"); - // New footage while it is taken: the salt for the newest footage lands. - std::fs::write(at("b"), b"two").unwrap(); - add(&mut s, &at("b")); - wait(&mut s); - let salt = effectcraft_render::disk_cache::footage_salt(&s.project); - assert_eq!(s.disk_salt.map(|(_, salt)| salt), Some(salt)); - assert_eq!(s.refresh_disk_salt(false), Some(salt)); - assert!(s.layer_cache.disk().is_some(), "attached again"); - // Another setting keeps the salt and the disk cache attached. + s.edit("add", None, |p, _| { + p.add_item("seq", effectcraft_color::Label::None, None, effectcraft_project::ItemKind::Footage(f)); + Ok(()) + }) + .unwrap(); + assert!(attached(&s), "an import keeps it attached"); + let cid = project(&mut s); + s.execute("prop.set", json!({"layer": "#1", "path": "transform/opacity", "value": 50})).unwrap(); s.execute_checked("prefs.set", json!({"key": "disk.diskCacheMaxGb", "value": 30})).unwrap(); - assert!(s.layer_cache.disk().is_some() && s.disk_salt.map(|(_, salt)| salt) == Some(salt)); - // Turned off and on again: stamped anew, in the background. + assert!(attached(&s), "edits and other settings keep it attached"); + let same = (*s.project).clone(); + s.replace_project(same, None); + assert!(attached(&s), "opening a project keeps it attached"); + // The first render uses it: a solid reads no file, so nothing is checked either. + s.render(cid, s.time(), RenderOpts::default()); + dc.flush(); + assert!(dc.stats().entries > 0, "written by the first render"); + assert_eq!(dc.file_checks(), 0, "no file looked at for the disk cache"); + // Switched off and on: attached again at once. s.execute_checked("prefs.set", json!({"key": "disk.diskCacheEnabled", "value": false})).unwrap(); + assert!(s.layer_cache.disk().is_none()); s.execute_checked("prefs.set", json!({"key": "disk.diskCacheEnabled", "value": true})).unwrap(); - wait(&mut s); - assert_eq!(s.refresh_disk_salt(false), Some(salt)); - let _ = std::fs::remove_file(at("a")); - let _ = std::fs::remove_file(at("b")); + assert!(s.layer_cache.disk().is_some()); } /// Marker edits keep the disk key (the sound of a playing preview must not wait for frames to @@ -258,38 +243,227 @@ fn protected_markers_with_a_duration_change_the_content_key() { assert_ne!(key(&s), k1, "its duration"); } -/// A project opened while a footage salt is still being taken must not keep that salt when it -/// names the same footage: its files may have changed since the worker read them. +/// With expressions the comp key covers what they can read, not the whole project (#3184, +/// #1492): an expression reading only its own comp keeps the key through edits of another comp +/// and of unused footage; `comp("Other")` makes "Other" count (and which comps have that name), +/// not a third comp; a computed name counts every item. #[test] -fn opening_a_project_drops_the_footage_salt_being_taken() { - let dir = folder("bg-salt-open"); - let mut s = session(&dir); - s.disk_salt_background = true; - let path = std::env::temp_dir().join(format!("effectcraft-bg-salt-open-{}.png", std::process::id())); - std::fs::write(&path, b"one").unwrap(); - let f = effectcraft_project::Footage { - path: path.to_string_lossy().to_string(), - kind: effectcraft_project::FootageKind::Still, - width: 1, - height: 1, - has_video: true, - ..Default::default() +fn expressions_reach_only_what_they_name() { + let mut s = Session::default(); + let comp = |s: &mut Session, name: &str| { + let c = s.execute("comp.new", json!({"name": name, "width": 8, "height": 8, "frameRate": 30, "duration": 1})).unwrap()["comp"].as_u64().unwrap(); + let l = s.execute("layer.newSolid", json!({"comp": c, "color": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + (effectcraft_project::ItemId(c), (c, l)) }; - s.edit("add", None, |p, _| { - p.add_item("f.png", effectcraft_color::Label::None, None, effectcraft_project::ItemKind::Footage(f)); + let (main, ml) = comp(&mut s, "Main"); + let (_, ol) = comp(&mut s, "Other"); + let (third, tl) = comp(&mut s, "Third"); + let unused = s.edit("add", None, |p, _| { + let f = effectcraft_project::Footage { path: "/nowhere/a.png".into(), has_video: true, ..Default::default() }; + Ok(p.add_item("a.png", effectcraft_color::Label::None, None, effectcraft_project::ItemKind::Footage(f))) + }); + let unused = unused.unwrap(); + let key = |s: &Session| effectcraft_render::disk_cache::comp_content_key(&s.project, main); + let expr = |s: &mut Session, (comp, layer): (u64, u64), text: &str| { + s.execute("prop.setExpression", json!({"comp": comp, "layer": layer, "path": "transform/opacity", "expression": text})).unwrap(); + }; + let opacity = |s: &mut Session, (comp, layer): (u64, u64), v: f64| { + s.execute("prop.set", json!({"comp": comp, "layer": layer, "path": "transform/opacity", "value": v})).unwrap(); + }; + let repath = |s: &mut Session, path: &str| { + let path = path.to_string(); + s.edit("repath", None, move |p, _| { + if let Some(effectcraft_project::ItemKind::Footage(f)) = p.item_mut(unused).map(|i| &mut i.kind) { + f.path = path; + } + Ok(()) + }) + .unwrap(); + }; + // Its own comp only. + expr(&mut s, ml, "thisComp.layer(1).transform.rotation + wiggle(1, 1)[0]"); + expr(&mut s, ol, "wiggle(2, 10)"); + let k0 = key(&s); + opacity(&mut s, ol, 40.0); + expr(&mut s, tl, "time * 10"); + repath(&mut s, "/nowhere/b.png"); + s.execute("comp.addMarker", json!({"comp": third.0, "time": 0.5})).unwrap(); + assert_eq!(key(&s), k0, "edits elsewhere keep the key"); + // An edit in the comp itself changes it. + opacity(&mut s, ml, 30.0); + assert_ne!(key(&s), k0); + // `comp("Other")`: Other counts, Third doesn't. + expr(&mut s, ml, "comp(\"Other\").layer(1).transform.opacity"); + let k1 = key(&s); + expr(&mut s, ol, "wiggle(3, 10)"); + let k2 = key(&s); + assert_ne!(k2, k1, "the comp it names"); + opacity(&mut s, tl, 20.0); + repath(&mut s, "/nowhere/c.png"); + assert_eq!(key(&s), k2, "a comp it doesn't name"); + // Another comp taking the name changes what it may read. + s.edit("rename", None, |p, _| { + p.item_mut(third).unwrap().name = "Other".into(); Ok(()) }) .unwrap(); - // The file changes after the edit started the salt, before the same footage is opened again. - std::fs::write(&path, b"a longer content").unwrap(); - let same = (*s.project).clone(); - s.replace_project(same, None); - let t0 = std::time::Instant::now(); - while s.poll_disk_salt() { - assert!(t0.elapsed().as_secs() < 20, "the salt never landed"); - std::thread::sleep(std::time::Duration::from_millis(5)); + let k3 = key(&s); + assert_ne!(k3, k2, "a second comp named Other"); + // A computed name reaches every item. + expr(&mut s, ml, "comp(\"Oth\" + \"er\").layer(1).transform.opacity"); + let k4 = key(&s); + repath(&mut s, "/nowhere/d.png"); + assert_ne!(key(&s), k4, "unused footage counts with a computed name"); +} + +/// Markers count only for the comps whose expressions can read them (#1083): an expression in +/// another comp that reads markers leaves this comp's markers out of its key. +#[test] +fn markers_count_only_where_an_expression_can_read_them() { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "M", "width": 8, "height": 8, "frameRate": 30, "duration": 2})).unwrap(); + let cid = s.active_comp_id().unwrap(); + s.execute("layer.newSolid", json!({"color": "#ff0000"})).unwrap(); + let other = s.execute("comp.new", json!({"name": "N", "width": 8, "height": 8, "frameRate": 30, "duration": 2})).unwrap()["comp"].as_u64().unwrap(); + let ol = s.execute("layer.newSolid", json!({"comp": other, "color": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.setExpression", json!({"comp": other, "layer": ol, "path": "transform/opacity", "expression": "thisComp.marker.numKeys"})).unwrap(); + let key = |s: &Session| effectcraft_render::disk_cache::comp_content_key(&s.project, cid); + let k0 = key(&s); + s.execute("comp.addMarker", json!({"comp": cid.0, "time": 0.5, "comment": "beat"})).unwrap(); + assert_eq!(key(&s), k0, "no expression of M reads its markers"); + s.execute("prop.setExpression", json!({"comp": other, "layer": ol, "path": "transform/opacity", "expression": "comp(\"M\").marker.numKeys"})).unwrap(); + let other_key = |s: &Session| effectcraft_render::disk_cache::comp_content_key(&s.project, effectcraft_project::ItemId(other)); + let n0 = other_key(&s); + s.execute("comp.addMarker", json!({"comp": cid.0, "time": 1.0})).unwrap(); + assert_ne!(other_key(&s), n0, "N reads M's markers"); + assert_eq!(key(&s), k0, "M itself still doesn't"); +} + +/// File ▸ Reload Footage drops the disk entries made of the item's files (#3184): a file +/// replaced by one with the same size and modification time is read again (the check on hit +/// can't tell), and entries of other files stay. +#[test] +fn reload_footage_drops_the_entries_of_its_files() { + use crate::render::reads::{FileStamp, ReadFile, ReadFiles}; + struct Probe; + impl crate::Importer for Probe { + fn probe(&self, path: &str) -> std::result::Result { + Ok(effectcraft_project::Footage { + path: path.into(), + kind: effectcraft_project::FootageKind::Still, + width: 1, + height: 1, + has_video: true, + ..Default::default() + }) + } } - let now = effectcraft_render::disk_cache::footage_salt(&s.project); - assert_eq!(s.disk_salt.map(|(_, salt)| salt), Some(now), "the salt reflects the file as it is now"); - let _ = std::fs::remove_file(&path); + let dir = folder("reload"); + let mut s = Session { importer: Some(std::sync::Arc::new(Probe)), ..Default::default() }; + s.execute_checked("prefs.set", json!({"values": {"disk.diskCacheFolder": &dir, "disk.diskCacheEnabled": true}})).unwrap(); + let src = std::path::Path::new(&dir).join("src"); + std::fs::create_dir_all(&src).unwrap(); + let mut add = |name: &str| { + let p = src.join(name); + std::fs::write(&p, b"aa").unwrap(); + let path = p.to_string_lossy().to_string(); + let f = effectcraft_project::Footage { + path: path.clone(), + kind: effectcraft_project::FootageKind::Still, + width: 1, + height: 1, + has_video: true, + ..Default::default() + }; + let id = s.edit("add", None, |p, _| Ok(p.add_item(name, effectcraft_color::Label::None, None, effectcraft_project::ItemKind::Footage(f)))).unwrap(); + (id, path) + }; + let (a, a_path) = add("a.png"); + let (_, b_path) = add("b.png"); + let dc = s.disk_cache.clone().unwrap(); + let read = |p: &str| -> ReadFiles { [ReadFile::new(p, FileStamp::native(p).unwrap())].into_iter().collect() }; + dc.put_frame_now(1, 1, 1, &[1; 4], &read(&a_path)); + dc.put_frame_now(2, 1, 1, &[2; 4], &read(&b_path)); + dc.put_layer_now(3, &effectcraft_effects::Buf { img: effectcraft_raster::Image::new(1, 1), offset: [0.0; 2], scale: 1.0 }, &read(&a_path)); + // Other bytes, same size, the old time back. + let t = std::fs::metadata(&a_path).unwrap().modified().unwrap(); + std::fs::write(&a_path, b"AA").unwrap(); + std::fs::File::options().append(true).open(&a_path).unwrap().set_modified(t).unwrap(); + assert!(dc.contains(crate::render::disk_cache::Kind::Frame, 1)); + s.execute_checked("file.reloadFootage", json!({"item": a.0})).unwrap(); + dc.flush(); + assert!(dc.get_frame(1, None).is_none(), "the reloaded file's frame renders again"); + assert!(dc.get_layer(3, None).is_none(), "and its layer buffer"); + assert!(dc.get_frame(2, None).is_some(), "another file's frame stays"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Reload Footage while the disk cache is off (#3184): when it is switched on later in the +/// session, the entries made of the reloaded files are dropped before anything is read from it +/// (a file replaced by one with the same size and time would pass the check on hit); entries of +/// other files stay. +#[test] +fn reload_footage_while_the_disk_cache_is_off_drops_its_entries_when_it_is_on() { + use crate::render::disk_cache::{DiskCache, Kind}; + use crate::render::reads::{FileStamp, ReadFile, ReadFiles}; + struct Probe; + impl crate::Importer for Probe { + fn probe(&self, path: &str) -> std::result::Result { + Ok(effectcraft_project::Footage { + path: path.into(), + kind: effectcraft_project::FootageKind::Still, + width: 1, + height: 1, + has_video: true, + ..Default::default() + }) + } + } + let dir = folder("reload-off"); + let src = std::path::Path::new(&dir).join("src"); + std::fs::create_dir_all(&src).unwrap(); + let mut s = Session { importer: Some(std::sync::Arc::new(Probe)), ..Default::default() }; + let mut add = |name: &str| { + let p = src.join(name); + std::fs::write(&p, b"aa").unwrap(); + let path = p.to_string_lossy().to_string(); + let f = effectcraft_project::Footage { + path: path.clone(), + kind: effectcraft_project::FootageKind::Still, + width: 1, + height: 1, + has_video: true, + ..Default::default() + }; + let id = s.edit("add", None, |p, _| Ok(p.add_item(name, effectcraft_color::Label::None, None, effectcraft_project::ItemKind::Footage(f)))).unwrap(); + (id, path) + }; + let (a, a_path) = add("a.png"); + let (_, b_path) = add("b.png"); + // Entries from an earlier session in the cache folder. + let read = |p: &str| -> ReadFiles { [ReadFile::new(p, FileStamp::native(p).unwrap())].into_iter().collect() }; + { + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + dc.put_frame_now(1, 1, 1, &[1; 4], &read(&a_path)); + dc.put_frame_now(2, 1, 1, &[2; 4], &read(&b_path)); + dc.put_layer_now(3, &effectcraft_effects::Buf { img: effectcraft_raster::Image::new(1, 1), offset: [0.0; 2], scale: 1.0 }, &read(&a_path)); + } + // Other bytes, same size, the old time back; reloaded while the cache is off. + let t = std::fs::metadata(&a_path).unwrap().modified().unwrap(); + std::fs::write(&a_path, b"AA").unwrap(); + std::fs::File::options().append(true).open(&a_path).unwrap().set_modified(t).unwrap(); + assert!(s.disk_cache.is_none()); + s.execute_checked("file.reloadFootage", json!({"item": a.0})).unwrap(); + // Switched on: the reloaded file's entries are misses from the start, and then gone. + s.execute_checked("prefs.set", json!({"values": {"disk.diskCacheFolder": &dir, "disk.diskCacheEnabled": true}})).unwrap(); + let dc = s.disk_cache.clone().unwrap(); + assert!(dc.get_frame(1, None).is_none(), "the reloaded file's frame renders again"); + assert!(dc.get_layer(3, None).is_none(), "and its layer buffer"); + dc.flush(); + assert!(!dc.contains(Kind::Frame, 1) && !dc.contains(Kind::Layer, 3)); + assert!(dc.get_frame(2, None).is_some(), "another file's frame stays"); + assert!(s.pending_disk_drops.is_empty(), "applied once"); + drop(dc); + drop(s); + let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/engine/src/tests/rig3d.rs b/crates/engine/src/tests/rig3d.rs index db64a9066..634d84e02 100644 --- a/crates/engine/src/tests/rig3d.rs +++ b/crates/engine/src/tests/rig3d.rs @@ -40,7 +40,17 @@ fn close3(a: [f64; 3], b: [f64; 3], tol: f64) -> bool { fn frame(s: &Session, id: u64) -> (Vec3, Vec3) { let c = cid(s); let comp = s.project.comp(c).unwrap(); - let ctx = EvalCtx { project: &s.project, comp_id: c, comp, time: Tick::ZERO, expr: None, footage: None, property_overrides: &[], media_overrides: &[] }; + let ctx = EvalCtx { + project: &s.project, + comp_id: c, + comp, + time: Tick::ZERO, + expr: None, + footage: None, + property_overrides: &[], + media_overrides: &[], + reads: None, + }; let (e, f, _) = layer_frame(&ctx, comp.layer(LayerId(id)).unwrap()); (e, f) } @@ -244,7 +254,7 @@ fn parented_camera_keeps_its_frame_under_an_offset_null() { fn frame_at(s: &Session, id: u64, t: Tick) -> (Vec3, Vec3, Vec3) { let c = cid(s); let comp = s.project.comp(c).unwrap(); - let ctx = EvalCtx { project: &s.project, comp_id: c, comp, time: t, expr: None, footage: None, property_overrides: &[], media_overrides: &[] }; + let ctx = EvalCtx { project: &s.project, comp_id: c, comp, time: t, expr: None, footage: None, property_overrides: &[], media_overrides: &[], reads: None }; layer_frame(&ctx, comp.layer(LayerId(id)).unwrap()) } diff --git a/crates/engine/src/tests/sequence.rs b/crates/engine/src/tests/sequence.rs index c5faa232b..b084e6b36 100644 --- a/crates/engine/src/tests/sequence.rs +++ b/crates/engine/src/tests/sequence.rs @@ -269,30 +269,31 @@ fn dpx_and_cineon_runs_import_as_sequences() { assert_eq!(got, [("plate.[0001-0003].dpx".to_string(), 3), ("scan_[01-02].cin".to_string(), 2)], "{r}"); } -/// #1926: an edit that leaves the footage alone doesn't stamp every frame of its image sequences -/// again for the disk cache's salt (with large sequences each keystroke in a text field took over -/// half a second); Reload Footage does, and so does new footage. +/// #1926, #3184: an edit does no file work at all for the disk cache (it used to stamp every +/// frame of the project's image sequences for a salt; with large sequences each keystroke in a +/// text field took over half a second): the cache stays attached and no file is looked at, after +/// edits, after a frame of the sequence was rewritten, after Reload Footage and new footage. +/// Entries check the files they list when they are read instead. #[test] -fn edits_keep_the_disk_salt_and_reload_stamps_the_files_again() { +fn edits_do_no_disk_cache_file_work() { let dir = folder(&RUN); - let cache = std::env::temp_dir().join(format!("ec-seq-salt-{}", std::process::id())); + let cache = std::env::temp_dir().join(format!("ec-seq-nofiles-{}", std::process::id())); let _ = std::fs::remove_dir_all(&cache); let mut s = session(); s.execute_checked("prefs.set", json!({"values": {"disk.diskCacheFolder": cache.to_string_lossy(), "disk.diskCacheEnabled": true}})).unwrap(); + let dc = s.disk_cache.clone().expect("a disk cache"); + let attached = |s: &Session| s.layer_cache.disk().is_some_and(|d| std::sync::Arc::ptr_eq(&d, &dc)); s.execute("comp.new", json!({"name": "C", "width": 64, "height": 32, "frameRate": 30, "duration": 1})).unwrap(); let r = s.execute("file.import", json!({"paths": [at(&dir, "shot_0002.png")], "sequence": true, "addToComp": true})).unwrap(); let item = r["items"][0].clone(); - let salt = s.refresh_disk_salt(false).expect("a disk cache"); // A frame rewritten in place: same footage settings, another size on disk. std::fs::write(at(&dir, "shot_0004.png"), b"a new render of this frame").unwrap(); s.execute("layer.newText", json!({"text": "title"})).unwrap(); s.execute("layer.setText", json!({"text": "title 2"})).unwrap(); - assert_eq!(s.refresh_disk_salt(false), Some(salt), "edits don't stamp the files again"); s.execute("file.reloadFootage", json!({"items": [item]})).unwrap(); - let reloaded = s.refresh_disk_salt(false).unwrap(); - assert_ne!(reloaded, salt, "Reload Footage stamps them again"); std::fs::write(at(&dir, "extra.png"), b"").unwrap(); s.execute("file.import", json!({"paths": [at(&dir, "extra.png")]})).unwrap(); - assert_ne!(s.refresh_disk_salt(false), Some(reloaded), "new footage counts"); + assert!(attached(&s), "attached all along"); + assert_eq!(dc.file_checks(), 0, "no file looked at for the disk cache"); let _ = std::fs::remove_dir_all(&cache); } diff --git a/crates/expr/src/host.rs b/crates/expr/src/host.rs index fa2993f97..43400ea1d 100644 --- a/crates/expr/src/host.rs +++ b/crates/expr/src/host.rs @@ -297,6 +297,7 @@ impl<'a> Resolver<'a> { } self.comp(c)?.layer(LayerId(l)) } + /// The evaluation context for comp `c` at time `t` seconds, or `None` when the comp does not exist. fn ctx_at(&self, c: u64, t: f64) -> Option> { let comp = self.comp(c)?; let same_comp = c == self.ctx.comp_id.0; @@ -309,6 +310,7 @@ impl<'a> Resolver<'a> { footage: self.ctx.footage, property_overrides: if same_comp { self.ctx.property_overrides } else { &[] }, media_overrides: if same_comp { self.ctx.media_overrides } else { &[] }, + reads: self.ctx.reads, }) } fn prop(&self, c: u64, l: u64, path: &str) -> Option<(&'a Layer, &'a Property)> { diff --git a/crates/expr/src/lib.rs b/crates/expr/src/lib.rs index 9e78d250f..ae9e647f8 100644 --- a/crates/expr/src/lib.rs +++ b/crates/expr/src/lib.rs @@ -172,6 +172,7 @@ fn evaluate_out( footage: ctx.footage, property_overrides: ctx.property_overrides, media_overrides: ctx.media_overrides, + reads: ctx.reads, }; let comp = ctx.comp_id.0; let path = layer.props.path_of(prop.uid).unwrap_or_else(|| format!("@{}", prop.uid)); diff --git a/crates/expr/src/sample.rs b/crates/expr/src/sample.rs index 28f830bb2..38f617289 100644 --- a/crates/expr/src/sample.rs +++ b/crates/expr/src/sample.rs @@ -4,13 +4,16 @@ //! expression (its footage source; [`EvalCtx::footage`]): source → masks → effects for //! `postEffect = true`, the bare source otherwise, in layer space (before the transform). Buffers //! are cached per thread for the frame, so many expressions sampling the same layer at the same -//! time render it once. +//! time render it once. The source files a sampled render reads are kept with its buffer and +//! noted in the evaluating render's log ([`EvalCtx::reads`]) on every use, so a frame whose +//! expressions sample footage lists that footage too (#3184). use std::cell::RefCell; use std::collections::HashMap; use std::sync::Arc; use effectcraft_project::{ItemId, Layer}; +use effectcraft_render::reads::{ReadFiles, ReadLog}; use effectcraft_render::{EvalCtx, NoFootage, RenderOpts, Renderer}; use effectcraft_time::Tick; @@ -26,7 +29,8 @@ struct Sampled { type Key = (usize, u64, u64, u64, bool, u64); thread_local! { - static CACHE: RefCell>> = RefCell::new(HashMap::new()); + /// The sampled buffers and the source files each was made of. + static CACHE: RefCell, Arc)>> = RefCell::new(HashMap::new()); /// Nested sampleImage renders (a sampled layer whose expressions sample again). static DEPTH: RefCell = const { RefCell::new(0) }; } @@ -42,13 +46,21 @@ fn fingerprint(layer: &Layer) -> u64 { h.finish() } +/// Renders `layer` of `comp` at time `t` for an expression sample, before or after effects as `post` says. +/// Returns `None` once the nesting depth limit is reached. fn render(ctx: EvalCtx, comp: ItemId, layer: &Layer, post: bool, t: f64) -> Option { let depth = DEPTH.with(|d| *d.borrow()); if depth >= MAX_DEPTH { return None; } let key: Key = (ctx.project as *const _ as usize, comp.0, layer.id.0, t.to_bits(), post, fingerprint(layer)); - if let Some(hit) = CACHE.with(|c| c.borrow().get(&key).cloned()) { + let note = |files: &ReadFiles| { + if let Some(log) = ctx.reads { + log.note_all(files); + } + }; + if let Some((hit, files)) = CACHE.with(|c| c.borrow().get(&key).cloned()) { + note(&files); return hit; } DEPTH.with(|d| *d.borrow_mut() += 1); @@ -60,20 +72,24 @@ fn render(ctx: EvalCtx, comp: ItemId, layer: &Layer, post: bool, t: f64) -> Opti } let _pop = Pop; let footage: &dyn effectcraft_render::FootageSource = ctx.footage.unwrap_or(&NoFootage); + let log = ReadLog::new(); let mut r = Renderer::new(ctx.project, footage, RenderOpts { motion_blur: false, ..Default::default() }); r.expr = ctx.expr; + r.reads = Some(&log); let ectx = r.eval_ctx(comp, Tick::from_seconds_f64(t))?; let out = if post { r.content_buf(&ectx, layer).map(|b| Sampled { img: Arc::new(b.img.clone()), offset: b.offset, scale: b.scale }) } else { r.layer_source(&ectx, layer).map(|b| Sampled { img: Arc::new(b.img), offset: b.offset, scale: b.scale }) }; + let files = Arc::new(log.into_files()); + note(&files); CACHE.with(|c| { let mut c = c.borrow_mut(); if c.len() >= MAX_CACHE { c.clear(); } - c.insert(key, out.clone()); + c.insert(key, (out.clone(), files)); }); out } diff --git a/crates/expr/src/tests.rs b/crates/expr/src/tests.rs index a7d2489c7..050a5a550 100644 --- a/crates/expr/src/tests.rs +++ b/crates/expr/src/tests.rs @@ -1332,6 +1332,7 @@ fn source_dimensions_follow_media_replacement() { footage: None, property_overrides: &[], media_overrides: &[media], + reads: None, }; assert_eq!(ctx.value(l, l.props.prop(ROT).unwrap()), Value::Scalar(130.0)); } @@ -1554,6 +1555,7 @@ fn text_expression_selector_values() { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let doc = effectcraft_render::text::source_text(&ctx, l).unwrap(); let lay = effectcraft_text::layout_doc(&doc); @@ -1606,6 +1608,43 @@ fn sample_image_reads_rendered_pixels() { assert_close!(f.num(&code("thisComp.layer(\"B\").sampleImage([-50, -50])"), 0.0), 0.0); } +/// The footage `sampleImage` renders is a file the evaluating render read (#3184): it is noted in +/// the render's log, also when the sampled buffer comes from the per-thread cache. +#[test] +fn sample_image_notes_the_files_it_reads() { + use effectcraft_render::reads::{FileStamp, ReadFile, ReadLog}; + /// A grey picture of any footage, noting `/clip.png` for every read. + struct Clip; + impl effectcraft_render::FootageSource for Clip { + fn frame(&self, _: ItemId, f: &effectcraft_project::Footage, _: Tick) -> Option> { + let mut img = effectcraft_render::Image::new(f.width, f.height); + img.data.fill([0.5, 0.5, 0.5, 1.0]); + Some(std::sync::Arc::new(img)) + } + fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) { + f(); + reads.note(ReadFile::new("/clip.png", FileStamp::Native { len: 3, modified: Some(1) })); + } + } + let mut p = Project::default(); + let comp = Comp::new(20, 20, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + let footage = + effectcraft_project::Footage { path: "/clip.png".into(), width: 8, height: 8, has_video: true, frame_rate: FrameRate::FPS_30, ..Default::default() }; + let item = p.add_item("Clip", Label::Aqua, None, ItemKind::Footage(footage)); + let layer = build::layer(&mut p, &comp, "Clip", LayerSource::Footage { item }, (8, 8), None); + let cid = p.add_item("C", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let c = p.comp(cid).unwrap(); + for pass in ["rendered", "cached"] { + let log = ReadLog::new(); + let ctx = EvalCtx { footage: Some(&Clip), reads: Some(&log), ..EvalCtx::new(&p, cid, c, Tick::ZERO) }; + let px = crate::sample::sample_image(ctx, cid, &layer, [4.0, 4.0], [0.5, 0.5], false, 0.0).unwrap(); + assert!((px[0] - 0.5).abs() < 1e-3 && px[3] > 0.99, "{pass}: {px:?}"); + let files = log.into_files(); + assert_eq!(files.files().iter().map(|f| &*f.path).collect::>(), ["/clip.png"], "{pass}"); + assert!(files.is_complete(), "{pass}"); + } +} + /// A huge sample point saturates the pixel coordinates; it must not overflow the loop bounds. #[test] fn sample_image_huge_point_is_transparent() { diff --git a/crates/gpu/src/tests/adv3d.rs b/crates/gpu/src/tests/adv3d.rs index 00b1fee14..4914c4912 100644 --- a/crates/gpu/src/tests/adv3d.rs +++ b/crates/gpu/src/tests/adv3d.rs @@ -123,6 +123,7 @@ pub(crate) fn scene(p: &Project, cid: ItemId) -> Scene { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let run: Vec<&effectcraft_project::Layer> = ctx.comp.layers.iter().rev().filter(|l| l.is_3d() && l.has_video()).collect(); adv::scene_of(&r, &ctx, &run, (ctx.comp.width, ctx.comp.height)) @@ -315,6 +316,7 @@ pub(crate) fn run_of(p: &Project, cid: ItemId) -> (EvalCtx<'_>, Vec<&effectcraft footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let run: Vec<&effectcraft_project::Layer> = ctx.comp.layers.iter().rev().filter(|l| l.is_3d() && l.has_video()).collect(); (ctx, run) diff --git a/crates/host/src/tests.rs b/crates/host/src/tests.rs index f628ce4e4..55240698f 100644 --- a/crates/host/src/tests.rs +++ b/crates/host/src/tests.rs @@ -343,6 +343,7 @@ fn value(s: &effectcraft_engine::Session, l: u64, path: &str) -> effectcraft_eng footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; ctx.value(layer, layer.props.prop(path).unwrap()) } @@ -606,6 +607,7 @@ fn stereo_rig_expressions_follow_the_master_camera() { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; ctx.value(layer, layer.props.prop(path).unwrap()).as_vec3() }; @@ -624,3 +626,35 @@ fn stereo_rig_expressions_follow_the_master_camera() { assert!(close(v3(&s, "transform/position"), [300.0 - d, 180.0, -800.0]), "{:?}", v3(&s, "transform/position")); assert!(close(v3(&s, "transform/poi"), [300.0, 180.0, 0.0]), "{:?}", v3(&s, "transform/poi")); } + +/// A text layer showing a 3D model's name (`thisComp.layer("Model").source.name`): renaming the +/// model gives the comp a new disk key and the text shows the new name (expressions on). +#[test] +fn renaming_a_model_a_text_expression_reads_changes_the_key_and_the_text() { + use effectcraft_engine::Session; + let mut s = super::session(); + let cid = effectcraft_project::ItemId( + s.execute("comp.new", json!({"name": "M", "width": 160, "height": 60, "frameRate": 30, "duration": 1})).unwrap()["comp"].as_u64().unwrap(), + ); + let model = s + .edit("add model", None, |p, _| { + let f = effectcraft_project::Footage { kind: effectcraft_project::FootageKind::Model, ..Default::default() }; + let model = p.add_item("chair.glb", Default::default(), None, effectcraft_project::ItemKind::Footage(f)); + let comp = p.comp(cid).cloned().unwrap(); + let mut l = effectcraft_project::build::layer(p, &comp, "Model", effectcraft_project::LayerSource::Model { item: model }, (16, 16), None); + // (not drawn: no model file; an expression still reads its source) + l.switches.video = false; + p.comp_mut(cid).unwrap().layers.push(l); + Ok(model) + }) + .unwrap(); + let text = s.execute("layer.newText", json!({"comp": cid.0, "text": "x"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.setExpression", json!({"comp": cid.0, "layer": text, "path": "text/sourceText", "expression": "thisComp.layer(\"Model\").source.name"})) + .unwrap(); + let key = effectcraft_engine::render::disk_cache::comp_content_key(&s.project, cid); + let shown = |s: &mut Session| s.execute("prop.get", json!({"comp": cid.0, "layer": text, "path": "text/sourceText"})).unwrap()["evaluated"].to_string(); + assert!(shown(&mut s).contains("chair.glb"), "{}", shown(&mut s)); + s.execute("project.rename", json!({"item": model.0, "name": "table.glb"})).unwrap(); + assert_ne!(effectcraft_engine::render::disk_cache::comp_content_key(&s.project, cid), key, "the comp's frames on disk hold the old name"); + assert!(shown(&mut s).contains("table.glb"), "the text shows the new name: {}", shown(&mut s)); +} diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml index a4070297b..4947d928a 100644 --- a/crates/media/Cargo.toml +++ b/crates/media/Cargo.toml @@ -36,3 +36,7 @@ rayon = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } log = { workspace = true } + +[dev-dependencies] +# (tests render comps that read layers through effects; already a dependency of effectcraft-render) +effectcraft-effects = { workspace = true } diff --git a/crates/media/src/exr_channels.rs b/crates/media/src/exr_channels.rs index c3e7cfc3d..d0acf590f 100644 --- a/crates/media/src/exr_channels.rs +++ b/crates/media/src/exr_channels.rs @@ -17,6 +17,7 @@ use std::sync::{Arc, Mutex, OnceLock, PoisonError}; use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, split_channel}; use effectcraft_raster::{AuxChannels, Image}; +use effectcraft_render::reads::{ReadFile, ReadFiles}; use exr::block::UncompressedBlock; use exr::block::chunk::TileCoordinates; use exr::image::read::image::ReadImage; @@ -477,8 +478,8 @@ fn layered_picture_of(aux: &AuxChannels, op: AlphaOp) -> Option { /// decoded anyway), else of the picture's channels alone (also when the cache has no channels /// for the file: a read that failed, or a broken part the picture doesn't use). `Err(Some(why))` /// when the file is too large to decode, `Err(None)` when it isn't a readable multi-layer EXR. -pub(crate) fn layered_picture(path: &str, bytes: Arc<[u8]>, op: AlphaOp, shared: bool) -> Result> { - if shared && let Some(d) = cache().get(path, Use::Picture, || Some(bytes.clone())) { +pub(crate) fn layered_picture(path: &str, bytes: Arc<[u8]>, file: &ReadFile, op: AlphaOp, shared: bool) -> Result> { + if shared && let Some(d) = cache().get(path, Use::Picture, || read_as(file, bytes.clone())) { return layered_picture_of(&d.aux, op).ok_or(None); } let (d, rgb, a) = picture_only(&bytes).map_err(|r| match r { @@ -499,8 +500,8 @@ pub(crate) fn layered_picture(path: &str, bytes: Arc<[u8]>, op: AlphaOp, shared: /// decoder shows for a file it reads, [`layered_image`]'s rule for one it refuses. `None` when /// the planes can't stand in for the `image` crate's picture (decode with it), or the file is /// not a readable EXR. -pub(crate) fn cached_picture(path: &str, bytes: Arc<[u8]>, op: AlphaOp) -> Option { - let d = cache().get(path, Use::Picture, || Some(bytes))?; +pub(crate) fn cached_picture(path: &str, bytes: Arc<[u8]>, file: &ReadFile, op: AlphaOp) -> Option { + let d = cache().get(path, Use::Picture, || read_as(file, bytes))?; let aux = &d.aux; match d.rgb { Rgb::None => layered_picture_of(aux, op), @@ -536,8 +537,16 @@ const CACHE_FILES: usize = 64; /// rendered at once. The oldest stops being kept when another comes. const HELD_FILES: usize = 4; -/// A file's decode: done once, by the first caller; the others wait for it. -type Slot = Arc>>>; +/// A file's decode: done once, by the first caller; the others wait for it. With the files read +/// for it (see [`crate::reads`]), noted again on every hit. +type Slot = Arc>, ReadFiles)>>; + +/// Bytes the caller already read from `file`, given to the cache as read from it (so the entry +/// keeps the file's identity). +fn read_as(file: &ReadFile, bytes: Arc<[u8]>) -> Option> { + crate::reads::note(file.clone()); + Some(bytes) +} struct Entry { path: String, @@ -554,8 +563,8 @@ impl Entry { fn in_use(&self) -> bool { match self.slot.get() { None => true, - Some(None) => false, - Some(Some(d)) => Arc::strong_count(d) > 1 || Arc::strong_count(&d.aux) > 1, + Some((None, _)) => false, + Some((Some(d), _)) => Arc::strong_count(d) > 1 || Arc::strong_count(&d.aux) > 1, } } } @@ -648,30 +657,18 @@ impl ChannelCache { // is safe because the decode never waits on rayon's global pool (the exr crate // decompresses on a pool of its own and this thread only receives the blocks), so the // decoding thread can't be waiting for this one. - let v = slot - .get_or_init(|| { - decoded_here = true; - self.decodes.fetch_add(1, Ordering::Relaxed); - #[cfg(test)] - count_decode(true); - let _abandon = Abandon { cache: self, slot: &slot }; - let bytes = read()?; - // The planes it will allocate count against the budget while it decodes. - if let Ok(meta) = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(&bytes[..]), false) - && let Some(need) = planes_bytes(&meta, &|_| true) - { - self.account(&slot, need.min(MAX_DECODE_BYTES)); - } - match decode(&bytes, &|_| true) { - Ok(d) => Some(Arc::new(d)), - Err(Refused::TooLarge(why)) => { - log::warn!("media: {path}: {why}"); - None - } - Err(Refused::Unreadable) => None, - } - }) - .clone(); + let (v, files) = slot.get_or_init(|| { + decoded_here = true; + self.decodes.fetch_add(1, Ordering::Relaxed); + #[cfg(test)] + count_decode(true); + let _abandon = Abandon { cache: self, slot: &slot }; + let (bytes, files) = crate::reads::captured(read); + (bytes.and_then(|bytes| self.decode_file(path, &slot, &bytes)), files) + }); + // The file(s) the decode was made of, read now or for an earlier caller. + crate::reads::note_all(files); + let v = v.clone(); if decoded_here { self.account(&slot, v.as_ref().map_or(0, |d| d.bytes())); } @@ -692,6 +689,24 @@ impl ChannelCache { v } + /// Decode the file at `path` whose bytes are `bytes` for `slot` (see [`ChannelCache::get`]). + fn decode_file(&self, path: &str, slot: &Slot, bytes: &[u8]) -> Option> { + // The planes it will allocate count against the budget while it decodes. + if let Ok(meta) = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false) + && let Some(need) = planes_bytes(&meta, &|_| true) + { + self.account(slot, need.min(MAX_DECODE_BYTES)); + } + match decode(bytes, &|_| true) { + Ok(d) => Some(Arc::new(d)), + Err(Refused::TooLarge(why)) => { + log::warn!("media: {path}: {why}"); + None + } + Err(Refused::Unreadable) => None, + } + } + /// Drop least recently used files not in use until the rest fit the budget, keeping `newest`. fn evict(entries: &mut Vec, budget: usize, newest: &Slot) { while entries.iter().map(|e| e.bytes).sum::() > budget || entries.len() > CACHE_FILES { @@ -996,7 +1011,7 @@ mod tests { let path = "/474-shared-fallback.exr"; assert!(cache().get(path, Use::Channels, || None).is_none(), "a read that failed"); let op = AlphaOp::of(&Footage::default(), FloatCurve::Linear); - let img = layered_picture(path, bytes, op, true).unwrap(); + let img = layered_picture(path, bytes, &ReadFile::new(path, effectcraft_render::reads::FileStamp::Virtual), op, true).unwrap(); assert_eq!(img.get(1, 1), [0.5, 0.5, 0.5, 1.0]); } @@ -1174,7 +1189,8 @@ mod tests { for curve in [FloatCurve::Srgb, FloatCurve::Linear, FloatCurve::Gamma24] { let op = AlphaOp::of(&Footage { alpha, premul_color: [0.2, 0.4, 0.6], ..Default::default() }, curve); let want = crate::convert::dynamic_to_image(&img, op); - let got = cached_picture(&path, bytes.as_slice().into(), op).unwrap(); + let got = cached_picture(&path, bytes.as_slice().into(), &ReadFile::new(path.as_str(), effectcraft_render::reads::FileStamp::Virtual), op) + .unwrap(); assert_eq!((got.width, got.height), (want.width, want.height)); assert_eq!(bits(&got), bits(&want), "file {k} {alpha:?} {curve:?}"); } diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index 31478f111..0b509c45f 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -25,6 +25,7 @@ mod layered; pub use layered::vector_doc; mod pool; mod probe; +mod reads; mod stream; mod wav_markers; mod webp_anim; @@ -85,7 +86,15 @@ pub fn is_importable(path: &std::path::Path) -> bool { /// A whole file's bytes, refusing anything that isn't a regular file: a project can name /// `/dev/zero` or a FIFO as footage, and reading those never ends (#2134). pub(crate) fn read_regular_file(path: &std::path::Path) -> std::io::Result> { + read_regular_file_stamped(path).map(|(bytes, _)| bytes) +} + +/// [`read_regular_file`] with the file's identity, from the metadata of the open file (no +/// second look at the path). +pub(crate) fn read_regular_file_stamped(path: &std::path::Path) -> std::io::Result<(Vec, effectcraft_render::reads::FileStamp)> { use std::io::Read; + #[cfg(test)] + count_open(path); let mut file = std::fs::File::open(path)?; let meta = file.metadata()?; if !meta.is_file() { @@ -95,7 +104,22 @@ pub(crate) fn read_regular_file(path: &std::path::Path) -> std::io::Result> = std::sync::Mutex::new(Vec::new()); + +/// Count an open of a file (tests: which files a render touched on disk). +#[cfg(test)] +pub(crate) fn count_open(path: &std::path::Path) { + OPENS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(path.to_path_buf()); +} + +/// How many times files under `dir` were opened so far (tests). +#[cfg(test)] +pub(crate) fn opens_under(dir: &std::path::Path) -> usize { + OPENS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).iter().filter(|p| p.starts_with(dir)).count() } pub(crate) fn ext_of(path: &std::path::Path) -> String { diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 9e10b4eb3..a60048b66 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -6,6 +6,7 @@ use std::sync::{Arc, Condvar, Mutex, OnceLock}; use effectcraft_project::{AlphaMode, Footage, FootageKind, ItemId}; use effectcraft_raster::{Image, Px}; +use effectcraft_render::reads::{FileStamp, ReadFile, ReadFiles, ReadLog}; use effectcraft_render::{FloatCurve, FootageSource}; use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick}; use filmcraft_media::{FrameRequest, SharedSource}; @@ -14,6 +15,7 @@ use image::metadata::Orientation; use crate::convert::{AlphaOp, dynamic_to_image, frame_to_image_in, frame_to_image_scaled, turned}; use crate::downmix::Order; use crate::gif_anim::{self, Anim}; +use crate::reads; use crate::{MediaError, Result}; /// Default frame-cache budget: 1 GiB of decoded frames. @@ -53,6 +55,8 @@ struct Key { struct Entry { img: Arc, + /// The files the frame was decoded from (see [`crate::reads`]). + files: Arc, stamp: u64, bytes: usize, } @@ -106,18 +110,20 @@ struct Lru { } impl Lru { - fn get(&mut self, k: &Key) -> Option> { + /// The cached image and its read files for `k`, marking the entry as most recently used. + fn get(&mut self, k: &Key) -> Option<(Arc, Arc)> { let e = self.map.get_mut(k)?; self.order.remove(&e.stamp); self.clock += 1; e.stamp = self.clock; self.order.insert(e.stamp, k.clone()); - Some(e.img.clone()) + Some((e.img.clone(), e.files.clone())) } - fn insert(&mut self, k: Key, img: Arc, budget: usize) { + /// Stores an image with its read files under `k` and evicts the least recently used entries until the cache fits `budget` bytes. + fn insert(&mut self, k: Key, img: Arc, files: Arc, budget: usize) { let bytes = img.data.len() * std::mem::size_of::() + 64; self.clock += 1; - if let Some(old) = self.map.insert(k.clone(), Entry { img, stamp: self.clock, bytes }) { + if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes }) { self.order.remove(&old.stamp); self.bytes -= old.bytes; } @@ -180,8 +186,9 @@ struct CacheState { } /// A movie/audio source by path ([`Opened`]), opened by the first caller while the others wait -/// for it (`None`: failed to open; not retried until `forget`). -type SourceSlot = Arc>>; +/// for it (`None`: failed to open; not retried until `forget`), with the file's identity as +/// opened. +type SourceSlot = Arc, ReadFile)>>; /// An opened movie/audio source, the turn its picture is shown with and the order its decoded /// audio channels arrive in. @@ -211,7 +218,7 @@ fn channel_order(src: &SharedSource, header: Option<&[u8]>, file: Option<&dyn fi } /// An animated GIF by path, as [`SourceSlot`] (the error message when it failed to load). -type GifSlot = Arc, String>>>; +type GifSlot = Arc, String>, ReadFile)>>; struct Inner { /// Opened movie/audio sources by path. @@ -220,8 +227,9 @@ struct Inner { opened: AtomicU64, /// In-memory files registered with `add_bytes` (web builds, tests). files: Mutex>>, - /// Parsed 3D models by path (`None`: failed to load; not retried until `forget`). - models: Mutex>>>, + /// Parsed 3D models by path (`None`: failed to load; not retried until `forget`), with the + /// files read for them (the model's and the resources it names). + models: Mutex>, Arc)>>, /// Decoded animated GIFs and WebPs by path, loaded by the first caller while the others wait (an /// `Err` is not retried until `forget`). gifs: Mutex, GifSlot>>, @@ -439,11 +447,13 @@ impl MediaPool { } /// The conformed-audio file of `path` at `rate` (named by a hash of the path, size and - /// modification time, so an edited file conforms again, and of `CONFORM_VERSION`). + /// modification time, so an edited file conforms again, and of `CONFORM_VERSION`). Notes + /// `path` as read with that identity (see [`crate::reads`]): its samples come from it. pub fn conformed_path(&self, path: &str, rate: u32) -> Option { use std::hash::{Hash, Hasher}; let folder = lock(&self.inner.conform).clone()?; let meta = std::fs::metadata(path).ok()?; + reads::note(ReadFile::new(path, FileStamp::of_metadata(&meta))); let mut h = std::collections::hash_map::DefaultHasher::new(); (CONFORM_VERSION, path, meta.len(), meta.modified().ok()).hash(&mut h); Some(folder.join(format!("{:016x}_{rate}.ecaf", h.finish()))) @@ -612,16 +622,20 @@ impl Inner { /// A 3D model file and its sibling resources (buffers, textures, MTL files), parsed once. fn model(&self, path: &str) -> Option> { - if let Some(m) = lock(&self.models).get(path) { - return m.clone(); + let found = lock(&self.models).get(path).cloned(); + if let Some((m, files)) = found { + reads::note_all(&files); + return m; } - let loaded = self.read(path).map_err(|e| e.to_string()).and_then(|bytes| { - // Files the model names are read only when local or on the model's own network share. - let resolve = |uri: &str| -> Option> { - let p = craft_docpath::resolve_absolute(uri, Some(std::path::Path::new(path))).ok()?; - self.read(&p.to_string_lossy()).ok().map(|b| b.to_vec()) - }; - effectcraft_model::load(path, &bytes, &resolve).map_err(|e| e.to_string()) + let (loaded, files) = reads::captured(|| { + self.read(path).map_err(|e| e.to_string()).and_then(|bytes| { + // Files the model names are read only when local or on the model's own network share. + let resolve = |uri: &str| -> Option> { + let p = craft_docpath::resolve_absolute(uri, Some(std::path::Path::new(path))).ok()?; + self.read(&p.to_string_lossy()).ok().map(|b| b.to_vec()) + }; + effectcraft_model::load(path, &bytes, &resolve).map_err(|e| e.to_string()) + }) }); let m = match loaded { Ok(m) => Some(Arc::new(m)), @@ -630,14 +644,29 @@ impl Inner { None } }; - lock(&self.models).entry(path.to_string()).or_insert(m).clone() + let (m, files) = lock(&self.models).entry(path.to_string()).or_insert((m, Arc::new(files))).clone(); + reads::note_all(&files); + m } + /// The bytes of the file at `path`, noted as read (see [`crate::reads`]). fn read(&self, path: &str) -> Result> { - if let Some(b) = lock(&self.files).get(path) { - return Ok(b.clone()); + let (bytes, file) = self.read_file(path); + reads::note(file); + bytes + } + + /// The bytes of the file at `path` and its identity as read ([`FileStamp::Missing`] when it + /// can't be read), not noted. + fn read_file(&self, path: &str) -> (Result>, ReadFile) { + let in_memory = lock(&self.files).get(path).cloned(); + if let Some(b) = in_memory { + return (Ok(b), ReadFile::new(path, FileStamp::Virtual)); + } + match crate::read_regular_file_stamped(std::path::Path::new(path)) { + Ok((bytes, stamp)) => (Ok(bytes.into()), ReadFile::new(path, stamp)), + Err(e) => (Err(MediaError::Io(format!("{path}: {e}"))), ReadFile::new(path, FileStamp::Missing)), } - crate::read_regular_file(std::path::Path::new(path)).map(Into::into).map_err(|e| MediaError::Io(format!("{path}: {e}"))) } /// The opened movie/audio source for `path` ([`Opened`]). The first caller opens it, outside @@ -645,35 +674,42 @@ impl Inner { /// files are decoded whole); callers asking for the same file meanwhile wait for that instead /// of opening it again: a preview's frame workers, audio feeder, conform and waveform threads /// each decoded a long FLAC in full at once (#408). + /// Each call notes the file as read (see [`crate::reads`]), with its identity as opened. fn opened(&self, path: &Arc) -> Option { let slot = lock(&self.sources).entry(path.clone()).or_default().clone(); - slot.get_or_init(|| { - self.opened.fetch_add(1, Ordering::Relaxed); - // Files on disk are read in place (the index, then samples on demand) rather than - // whole; in-memory files (`add_bytes`) and wasm32 builds open from bytes. - let in_memory = lock(&self.files).contains_key(&**path); - let file = std::path::Path::new(&**path); - let in_place = if in_memory { None } else { crate::stream::open_source(file) }; - let in_place = in_place.map(|src| { - let (header, reader) = (crate::stream::movie_header(file), crate::stream::file_reader(file)); - src.map(|src| Opened::new(src, header.as_deref(), reader.as_deref())) + let (opened, file) = slot.get_or_init(|| self.open(path)); + reads::note(file.clone()); + opened.clone() + } + + /// Open the movie/audio file at `path` (see [`Inner::opened`]), with its identity as opened. + fn open(&self, path: &Arc) -> (Option, ReadFile) { + self.opened.fetch_add(1, Ordering::Relaxed); + // Files on disk are read in place (the index, then samples on demand) rather than + // whole; in-memory files (`add_bytes`) and wasm32 builds open from bytes. + let in_memory = lock(&self.files).contains_key(&**path); + let file = std::path::Path::new(&**path); + let in_place = if in_memory { None } else { crate::stream::open_source(file) }; + let in_place = in_place.map(|(src, stamp)| { + let (header, reader) = (crate::stream::movie_header(file), crate::stream::file_reader(file)); + (src.map(|src| Opened::new(src, header.as_deref(), reader.as_deref())), ReadFile::new(path.clone(), stamp)) + }); + let (opened, read) = in_place.unwrap_or_else(|| { + let (bytes, read) = self.read_file(path); + let opened = bytes.and_then(|bytes| { + let name = std::path::Path::new(&**path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); + let src = filmcraft_codecs::open_bytes(&name, bytes.clone()).map_err(MediaError::from)?; + Ok(Opened::new(src, Some(&bytes), Some(&filmcraft_media::reader::MemReader(bytes.clone())))) }); - let opened = in_place.unwrap_or_else(|| { - self.read(path).and_then(|bytes| { - let name = std::path::Path::new(&**path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); - let src = filmcraft_codecs::open_bytes(&name, bytes.clone()).map_err(MediaError::from)?; - Ok(Opened::new(src, Some(&bytes), Some(&filmcraft_media::reader::MemReader(bytes.clone())))) - }) - }); - match opened { - Ok(s) => Some(s), - Err(e) => { - self.note_problem(path, "", &e); - None - } + (opened, read) + }); + match opened { + Ok(s) => (Some(s), read), + Err(e) => { + self.note_problem(path, "", &e); + (None, read) } - }) - .clone() + } } /// Remember why the movie at `path` cannot be shown (`codec` is the footage's, when known) @@ -694,13 +730,15 @@ impl Inner { /// map's lock; callers asking for the same file meanwhile wait for that. fn gif(&self, path: &Arc) -> Result> { let slot = lock(&self.gifs).entry(path.clone()).or_default().clone(); - let loaded = slot.get_or_init(|| { - let loaded = self.read(path).and_then(|bytes| Anim::load(&bytes)); - loaded.map(Arc::new).map_err(|e| { + let (loaded, file) = slot.get_or_init(|| { + let (bytes, file) = self.read_file(path); + let loaded = bytes.and_then(|bytes| Anim::load(&bytes)).map(Arc::new).map_err(|e| { log::warn!("media: cannot decode {path}: {e}"); e.to_string() - }) + }); + (loaded, file) }); + reads::note(file.clone()); loaded.clone().map_err(|e| MediaError::Decode(format!("{path}: {e}"))) } @@ -783,19 +821,15 @@ impl Inner { let mut c = lock(&self.cache); // sequential playback of a movie: read the next frame ahead if playback && movie { - let prev = c.last.insert(loc.key.path.clone(), loc.key.frame); - if prev == Some(loc.key.frame - 1) && self.read_ahead.load(Ordering::Relaxed) { - let next_t = t + footage.frame_rate.frame_duration(); - let mut next = Self::locate(footage, next_t, float); - next.key.size = loc.key.size; - if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) { - self.prefetch(footage, next_t, size, float); - } - } + self.prefetch_next(&mut c, &loc, footage, t, size, float); } + // (Each exit notes the files the frame came from, see `crate::reads`; a cached frame + // notes those it was decoded from, without touching them.) loop { - if let Some(img) = c.lru.get(&loc.key) { + if let Some((img, files)) = c.lru.get(&loc.key) { self.hits.fetch_add(1, Ordering::Relaxed); + drop(c); + reads::note_all(&files); return Ok(img); } if !c.inflight.contains(&loc.key) { @@ -808,7 +842,9 @@ impl Inner { if rayon::current_thread_index().is_some() { self.misses.fetch_add(1, Ordering::Relaxed); drop(c); - return Ok(Arc::new(self.decode(&loc, footage)?)); + let (img, files) = reads::captured(|| self.decode(&loc, footage)); + reads::note_all(&files); + return Ok(Arc::new(img?)); } c = self.done.wait(c).unwrap_or_else(std::sync::PoisonError::into_inner); } @@ -816,11 +852,26 @@ impl Inner { c.inflight.insert(loc.key.clone()); drop(c); let _guard = Inflight { inner: self, key: loc.key.clone() }; - let img = Arc::new(self.decode(&loc, footage)?); - lock(&self.cache).lru.insert(loc.key, img.clone(), self.budget()); + let (img, files) = reads::captured(|| self.decode(&loc, footage)); + reads::note_all(&files); + let img = Arc::new(img?); + lock(&self.cache).lru.insert(loc.key, img.clone(), Arc::new(files), self.budget()); Ok(img) } + /// Sequential playback of a movie (`loc`, at `t`): read the frame after it ahead. + fn prefetch_next(self: &Arc, c: &mut CacheState, loc: &Loc, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) { + let prev = c.last.insert(loc.key.path.clone(), loc.key.frame); + if prev == Some(loc.key.frame - 1) && self.read_ahead.load(Ordering::Relaxed) { + let next_t = t + footage.frame_rate.frame_duration(); + let mut next = Self::locate(footage, next_t, float); + next.key.size = loc.key.size; + if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) { + self.prefetch(footage, next_t, size, float); + } + } + } + #[cfg(not(target_arch = "wasm32"))] fn prefetch(self: &Arc, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) { let (me, f) = (self.clone(), footage.clone()); @@ -856,13 +907,17 @@ impl Inner { } } + /// Decodes the still image at `loc` and applies the footage alpha and float handling. + /// A placeholder path decodes to colour bars. fn decode_still(&self, loc: &Loc, footage: &Footage) -> Result { let op = AlphaOp::of(footage, loc.float); let path = &loc.key.path; if &**path == PLACEHOLDER { return Ok(colour_bars(footage.width, footage.height)); } - let bytes = self.read(path)?; + let (bytes, file) = self.read_file(path); + reads::note(file.clone()); + let bytes = bytes?; if let Some(img) = crate::dpx::decode(path, &bytes)? { return Ok(dynamic_to_image(&img, op)); } @@ -880,7 +935,7 @@ impl Inner { let shared = lock(&self.aux_footage).contains(&footage.path); if shared && path.to_ascii_lowercase().ends_with(".exr") - && let Some(img) = crate::exr_channels::cached_picture(path, bytes.clone(), op) + && let Some(img) = crate::exr_channels::cached_picture(path, bytes.clone(), &file, op) { return Ok(img); } @@ -889,7 +944,7 @@ impl Inner { match crate::probe::load_oriented(&bytes, crate::still_format(path, &bytes)) { Ok(img) => Ok(dynamic_to_image(&img, op)), // A multi-layer OpenEXR file without an unnamed RGB layer: its colour layer. - Err(e) => crate::exr_channels::layered_picture(path, bytes, op, shared) + Err(e) => crate::exr_channels::layered_picture(path, bytes, &file, op, shared) .map_err(|why| MediaError::Decode(format!("{path}: {}", why.unwrap_or_else(|| e.to_string())))), } } @@ -957,6 +1012,13 @@ impl FootageSource for MediaPool { fn cancellable(&self, cancel: &Arc, f: &mut dyn FnMut()) { filmcraft_media::cancel::with_cancel(cancel, f) } + /// Every read of a file notes it (see [`crate::reads`]): a frame's image file or sequence + /// frame, a movie, a model and the files it names, an EXR's channels, a movie's audio. A + /// frame (or other entry) served from memory notes the files it was made of, as they were + /// when read, without reading them again. A missing sequence frame (colour bars) reads none. + fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) { + reads::recording(reads, f) + } fn frame(&self, item: ItemId, footage: &Footage, t: Tick) -> Option> { self.frame_sized(item, footage, t, None, FloatCurve::Srgb) } @@ -1207,13 +1269,13 @@ mod tests { let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0) }; let budget = 3 * (4096 + 64); for i in 0..10 { - c.insert(k(i), img(), budget); + c.insert(k(i), img(), Arc::default(), budget); } assert_eq!(c.map.len(), 3); assert!(c.get(&k(9)).is_some() && c.get(&k(6)).is_none()); // touching 7 makes 8 the oldest c.get(&k(7)); - c.insert(k(10), img(), budget); + c.insert(k(10), img(), Arc::default(), budget); assert!(c.get(&k(7)).is_some() && c.get(&k(8)).is_none()); assert_eq!(c.spare.len(), MAX_SPARE); assert_eq!(c.take_spare(256).len(), 256); diff --git a/crates/media/src/reads.rs b/crates/media/src/reads.rs new file mode 100644 index 000000000..43ea209b8 --- /dev/null +++ b/crates/media/src/reads.rs @@ -0,0 +1,120 @@ +//! Which files the [`MediaPool`](crate::MediaPool) reads for a render +//! ([`FootageSource::recording`](effectcraft_render::FootageSource::recording)). +//! +//! Every read of a file notes it, with the identity it had when read, in the innermost scope open +//! on this thread. The pool's caches (decoded frames, opened movies, GIFs, models, EXR channels) +//! keep what their entries were made of and note it again on each hit, so a frame served from +//! memory reports its file without touching the disk. +//! +//! The scopes are per thread: a read noted while a render's scope is open on the thread belongs +//! to that render. A thread waiting inside a decode can run another job meanwhile (rayon); that +//! job's reads go to its own scope when it opens one, and otherwise to the scope below it, which +//! then lists a file more than it read (never fewer). + +use std::cell::RefCell; + +use effectcraft_render::reads::{ReadFile, ReadFiles, ReadLog}; + +/// Scopes nested deeper than this are not opened (a thread that keeps taking jobs while it waits +/// cannot pile them up without bound): their reads go to the scope below, and they come back +/// incomplete. +const MAX_DEPTH: usize = 64; + +thread_local! { + /// The open scopes on this thread, innermost last, each with the files noted in it. + static SCOPES: RefCell> = const { RefCell::new(Vec::new()) }; +} + +/// Note `file` in the innermost open scope on this thread (nothing when none is open). +pub(crate) fn note(file: ReadFile) { + let _ = SCOPES.try_with(|s| { + if let Ok(mut s) = s.try_borrow_mut() + && let Some(top) = s.last_mut() + { + top.add(file); + } + }); +} + +/// Note every file of `files` in the innermost open scope on this thread. +pub(crate) fn note_all(files: &ReadFiles) { + if files.is_empty() && files.is_complete() { + return; + } + let _ = SCOPES.try_with(|s| { + if let Ok(mut s) = s.try_borrow_mut() + && let Some(top) = s.last_mut() + { + top.extend(files); + } + }); +} + +/// Closes the scope it opened, also when what runs inside panics: its files go to `out`. +struct Scope<'a> { + out: Out<'a>, + opened: bool, +} + +enum Out<'a> { + /// A render's log ([`recording`]). + Log(&'a ReadLog), + /// The caller's ([`captured`]). + Files(&'a mut ReadFiles), +} + +impl Scope<'_> { + /// Starts collecting the files read on this thread into `out`. + /// At the nesting limit the scope stays closed and reports an incomplete list. + fn open(out: Out<'_>) -> Scope<'_> { + let opened = SCOPES + .try_with(|s| match s.try_borrow_mut() { + Ok(mut s) if s.len() < MAX_DEPTH => { + s.push(ReadFiles::default()); + true + } + _ => false, + }) + .unwrap_or(false); + Scope { out, opened } + } +} + +impl Drop for Scope<'_> { + /// Ends the scope and hands the collected files to its output, marked incomplete when the scope could not be opened. + fn drop(&mut self) { + let files = if self.opened { SCOPES.try_with(|s| s.try_borrow_mut().ok().and_then(|mut s| s.pop())).ok().flatten() } else { None }; + // A scope that could not open saw nothing: what it made can't name its files. + let files = files.unwrap_or_else(|| { + let mut f = ReadFiles::default(); + f.mark_incomplete(); + f + }); + match &mut self.out { + Out::Log(log) => log.note_all(&files), + Out::Files(out) => out.extend(&files), + } + } +} + +/// Run `f` with the files it reads on this thread noted in `log` +/// ([`FootageSource::recording`](effectcraft_render::FootageSource::recording)). +pub(crate) fn recording(log: &ReadLog, f: &mut dyn FnMut()) { + let _scope = Scope::open(Out::Log(log)); + f(); +} + +/// Run `f` and return what it returns with the files it read on this thread (to keep with a +/// cache entry made of them). They are not noted in the enclosing scope: the caller notes them, +/// as it does on each later hit of the entry. +pub(crate) fn captured(f: impl FnOnce() -> T) -> (T, ReadFiles) { + let mut files = ReadFiles::default(); + let out = { + let _scope = Scope::open(Out::Files(&mut files)); + f() + }; + (out, files) +} + +#[cfg(test)] +mod tests; diff --git a/crates/media/src/reads/tests.rs b/crates/media/src/reads/tests.rs new file mode 100644 index 000000000..5dae8415a --- /dev/null +++ b/crates/media/src/reads/tests.rs @@ -0,0 +1,390 @@ +//! The files a render reads, as the media pool reports them (#3184): exactly the files the frame +//! was made of, also when its pixels come from memory. + +use std::path::{Path, PathBuf}; + +use effectcraft_color::Label; +use effectcraft_project::build::{self, Ids}; +use effectcraft_project::{Comp, Footage, FootageKind, ItemId, ItemKind, Layer, LayerSource, Project, Solid, Value}; +use effectcraft_render::reads::{FileStamp, ReadFiles, ReadLog}; +use effectcraft_render::{LayerCache, RenderOpts, Renderer}; +use effectcraft_time::FrameRate; + +use crate::MediaPool; + +const RATE: FrameRate = FrameRate::FPS_30; +const SIZE: (u32, u32) = (16, 8); + +/// A fresh folder for test `name` (on the build drive, not the system temp folder). +fn folder(name: &str) -> PathBuf { + let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join(format!("../../target/test-reads-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +/// A PNG at `path`, a flat grey of `level`. +fn png(path: &Path, level: u8) -> String { + image::RgbaImage::from_pixel(SIZE.0, SIZE.1, image::Rgba([level, level, level, 255])).save(path).unwrap(); + path.to_string_lossy().to_string() +} + +/// An image sequence of `n` PNG frames in `dir`. +fn sequence(dir: &Path, n: usize) -> Footage { + let files: Vec = (0..n).map(|i| png(&dir.join(format!("seq_{i:04}.png")), 20 + i as u8 * 10)).collect(); + let mut f = Footage { kind: FootageKind::Sequence, sequence: files, frame_rate: RATE, ..crate::probe(dir.join("seq_0000.png")).unwrap() }; + f.sync_sequence_duration(); + f +} + +/// A still footage item of `path`. +fn still(path: &Path, level: u8) -> Footage { + png(path, level); + crate::probe(path).unwrap() +} + +fn comp() -> Comp { + Comp::new(SIZE.0, SIZE.1, RATE, RATE.tick_of(10)) +} + +fn footage_layer(p: &mut Project, comp: &Comp, name: &str, f: Footage) -> Layer { + let item = p.add_item(name, Label::Aqua, None, ItemKind::Footage(f)); + build::layer(p, comp, name, LayerSource::Footage { item }, SIZE, Some(comp.duration)) +} + +fn add_comp(p: &mut Project, name: &str, mut comp: Comp, layers: Vec) -> ItemId { + comp.layers = layers.into(); + p.add_item(name, Label::Sandstone, None, ItemKind::Comp(comp.into())) +} + +/// Render frame `frame` of `comp` with `pool` and `cache`: the picture's top-left pixel and the +/// files the render reports. +fn render(p: &Project, pool: &MediaPool, cache: &LayerCache, comp: ItemId, frame: i64) -> ([f32; 4], ReadFiles) { + let log = ReadLog::new(); + let mut r = Renderer::new(p, pool, RenderOpts::default()); + r.cache = Some(cache); + r.reads = Some(&log); + let img = r.comp_frame(comp, RATE.tick_of(frame)); + (img.get(0, 0), log.into_files()) +} + +fn paths(files: &ReadFiles) -> Vec { + files.files().iter().map(|f| f.path.to_string()).collect() +} + +/// Every listed file has the identity it has on disk now. +fn assert_stamps_match_disk(files: &ReadFiles) { + for f in files.files() { + assert_eq!(Some(f.stamp), FileStamp::native(&f.path), "{}", f.path); + } +} + +/// (a) One frame of a comp with an image-sequence layer reads that frame's file and nothing else +/// of the sequence; a large sequence in the project that no layer uses adds nothing, not even a +/// look at its files. +#[test] +fn a_sequence_frame_reports_only_its_own_file() { + let dir = folder("sequence"); + let unused_dir = dir.join("unused"); + let mut p = Project::default(); + let c = comp(); + let seq = sequence(&dir, 5); + let (frame_2, frame_4) = (seq.sequence[2].clone(), seq.sequence[4].clone()); + let layer = footage_layer(&mut p, &c, "Seq", seq); + // 5000 frames nobody shows (they don't even exist). + let unused = Footage { + kind: FootageKind::Sequence, + sequence: (0..5000).map(|i| unused_dir.join(format!("big_{i:05}.exr")).to_string_lossy().to_string()).collect(), + frame_rate: RATE, + width: SIZE.0, + height: SIZE.1, + has_video: true, + ..Default::default() + }; + p.add_item("Unused", Label::Aqua, None, ItemKind::Footage(unused)); + let cid = add_comp(&mut p, "C", c, vec![layer]); + let (pool, cache) = (MediaPool::new(), LayerCache::default()); + let opened = crate::opens_under(&dir); + let (px, files) = render(&p, &pool, &cache, cid, 2); + assert!((px[0] - 40.0 / 255.0).abs() < 0.01, "frame 2 shows: {px:?}"); + assert_eq!(paths(&files), [frame_2]); + assert!(files.is_complete()); + assert_stamps_match_disk(&files); + assert_eq!(crate::opens_under(&dir) - opened, 1, "one file opened: the frame's"); + assert_eq!(crate::opens_under(&unused_dir), 0, "the unused sequence was not looked at"); + // Another frame reads its own file, also through the viewer's wrapper of the pool. + let viewer = effectcraft_render::CancellableFootage { inner: &pool, cancel: Default::default() }; + let log = ReadLog::new(); + let mut r = Renderer::new(&p, &viewer, RenderOpts::default()); + r.cache = Some(&cache); + r.reads = Some(&log); + r.comp_frame(cid, RATE.tick_of(4)); + let files = log.into_files(); + assert_eq!(paths(&files), [frame_4]); + assert!(files.is_complete()); + let _ = std::fs::remove_dir_all(&dir); +} + +/// (b) A precomp reports the files of what it draws, and an effect that reads another layer +/// reports that layer's files (the layer itself is hidden: its pixels come only through the +/// effect). +#[test] +fn precomps_and_layers_read_by_effects_report_their_files() { + let dir = folder("nested"); + let mut p = Project::default(); + let c = comp(); + let seq = sequence(&dir, 4); + let nested_frame = seq.sequence[3].clone(); + let inner_layer = footage_layer(&mut p, &c, "Seq", seq); + let inner = add_comp(&mut p, "Inner", c.clone(), vec![inner_layer]); + let pre = build::layer(&mut p, &c, "Inner", LayerSource::Comp { item: inner }, SIZE, Some(c.duration)); + let matte_path = dir.join("matte.png"); + let mut matte = footage_layer(&mut p, &c, "Matte", still(&matte_path, 255)); + matte.switches.video = false; + let sid = p.add_item("S", Label::Red, None, ItemKind::Solid(Solid { color: [1.0, 0.0, 0.0], width: SIZE.0, height: SIZE.1, pixel_aspect: 1.0 })); + let mut solid = build::layer(&mut p, &c, "S", LayerSource::Solid { item: sid }, SIZE, Some(c.duration)); + let spec = effectcraft_effects::find("ec.channel.setmatte").unwrap(); + let mut next = p.next_id; + let mut fx = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [SIZE.0 as f64, SIZE.1 as f64]); + p.next_id = next; + fx.prop_mut("takeMatteFromLayer").unwrap().value = Value::Layer(Some(matte.id.0)); + solid.props.sub_mut("effects").unwrap().children.push(fx.into()); + let cid = add_comp(&mut p, "Outer", c, vec![solid, matte, pre]); + let (pool, cache) = (MediaPool::new(), LayerCache::default()); + let (_, files) = render(&p, &pool, &cache, cid, 3); + let mut want = vec![matte_path.to_string_lossy().to_string(), nested_frame]; + want.sort(); + assert_eq!(paths(&files), want); + assert!(files.is_complete()); + assert_stamps_match_disk(&files); + let _ = std::fs::remove_dir_all(&dir); +} + +/// (c) A frame rendered again comes from memory (the sequence frame from the media pool, the +/// still layer from the layer cache) and reports the same files, with the identities they had +/// when read, without opening anything: here the files are even gone by then. +#[test] +fn a_frame_served_from_memory_reports_its_files_without_touching_them() { + let dir = folder("memory"); + let mut p = Project::default(); + let c = comp(); + // The sequence at half opacity over the still: both show. + let mut seq = footage_layer(&mut p, &c, "Seq", sequence(&dir, 3)); + seq.props.prop_mut("transform/opacity").unwrap().value = Value::Scalar(50.0); + let plate = footage_layer(&mut p, &c, "Plate", still(&dir.join("plate.png"), 128)); + let cid = add_comp(&mut p, "C", c, vec![seq, plate]); + let (pool, cache) = (MediaPool::new(), LayerCache::default()); + let (px, first) = render(&p, &pool, &cache, cid, 1); + assert_eq!(first.len(), 2, "{:?}", paths(&first)); + assert_stamps_match_disk(&first); + std::fs::remove_dir_all(&dir).unwrap(); + let opened = crate::opens_under(&dir); + let hits = cache.stats().hits; + let (again_px, again) = render(&p, &pool, &cache, cid, 1); + assert_eq!(again_px, px, "the same picture"); + assert_eq!(again, first, "the same files, as they were when read"); + assert!(cache.stats().hits > hits, "the still layer came from the layer cache"); + // The media pool alone (a new layer cache) reports them too. + let (_, from_pool) = render(&p, &pool, &LayerCache::default(), cid, 1); + assert_eq!(from_pool, first); + assert_eq!(crate::opens_under(&dir), opened, "nothing was opened on disk"); +} + +// ---------------------------------------------------------------- layer entries on disk (#3184) + +use std::sync::Arc; + +use effectcraft_render::disk_cache::{DiskCache, Kind}; + +/// A "session": a new media pool and layer cache over the disk cache in `cache` (opened anew, +/// as after a restart), writing every layer buffer however quick to render. +fn session(cache: &Path) -> (MediaPool, LayerCache, Arc) { + let dc = DiskCache::open(cache, 1 << 30).unwrap(); + dc.set_min_layer_ms(0); + let lc = LayerCache::default(); + lc.set_disk(Some(dc.clone())); + (MediaPool::new(), lc, dc) +} + +/// Give `path` a later modification time, so a rewrite within the clock's resolution counts. +fn touch_later(path: &Path) { + let t = std::fs::metadata(path).unwrap().modified().unwrap() + std::time::Duration::from_secs(7); + std::fs::File::options().append(true).open(path).unwrap().set_modified(t).unwrap(); +} + +/// Two still plates (the upper one at half opacity, so both show), and a 5000-frame sequence +/// in the project that no layer uses (its files don't even exist). +fn plates(dir: &Path) -> (Project, ItemId, PathBuf) { + let mut p = Project::default(); + let c = comp(); + let a = dir.join("a.png"); + let mut top = footage_layer(&mut p, &c, "A", still(&a, 60)); + top.props.prop_mut("transform/opacity").unwrap().value = Value::Scalar(50.0); + let bottom = footage_layer(&mut p, &c, "B", still(&dir.join("b.png"), 200)); + let unused = dir.join("unused"); + let seq = Footage { + kind: FootageKind::Sequence, + sequence: (0..5000).map(|i| unused.join(format!("big_{i:05}.exr")).to_string_lossy().to_string()).collect(), + frame_rate: RATE, + width: SIZE.0, + height: SIZE.1, + has_video: true, + ..Default::default() + }; + p.add_item("Unused", Label::Aqua, None, ItemKind::Footage(seq)); + let cid = add_comp(&mut p, "C", c, vec![top, bottom]); + (p, cid, a) +} + +/// Correct and no scan: after a restart, only the layer buffer whose source file changed is +/// rendered again (only that file is opened); the other comes from disk with its file list, so +/// the frame's list is complete again (its frame entry can be stored). The disk cache checks +/// exactly the files its entries list, never the unused sequence. The re-rendered buffer +/// replaces its entry: the next restart opens nothing. +#[test] +fn a_changed_file_renders_only_the_layer_buffers_that_read_it_again() { + let dir = folder("layer-disk"); + let (src, cache) = (dir.join("src"), dir.join("cache")); + std::fs::create_dir_all(&src).unwrap(); + let (p, cid, a) = plates(&src); + let (pool, lc, dc) = session(&cache); + let (px, files) = render(&p, &pool, &lc, cid, 0); + assert!(files.is_complete() && files.len() == 2, "{:?}", paths(&files)); + dc.flush(); + let entries = dc.keys(Kind::Layer).len(); + assert!(entries >= 2, "both plates' buffers are on disk: {entries}"); + drop((pool, lc, dc)); + + // Restart without a change: everything from disk, nothing opened, a complete list. + let (pool, lc, dc) = session(&cache); + let opened = crate::opens_under(&src); + let (again, files_again) = render(&p, &pool, &lc, cid, 0); + assert_eq!(again, px); + assert_eq!(files_again, files, "the files of the entries, as they were read"); + assert!(files_again.is_complete(), "a frame made of buffers from disk can be stored"); + assert_eq!(crate::opens_under(&src), opened, "nothing opened"); + let checks = dc.file_checks(); + assert!(checks >= 2 && checks as usize <= entries, "one check per entry used: {checks} of {entries}"); + drop((pool, lc, dc)); + + // a.png gets new pixels: only the top plate renders again. + png(&a, 120); + touch_later(&a); + let (pool, lc, dc) = session(&cache); + let opened = crate::opens_under(&src); + let (changed, files_changed) = render(&p, &pool, &lc, cid, 0); + assert_ne!(changed, px, "the new pixels show"); + assert_eq!(crate::opens_under(&src) - opened, 1, "only the changed file is read"); + assert!(files_changed.is_complete() && files_changed.len() == 2); + assert_stamps_match_disk(&files_changed); + assert!(dc.file_checks() as usize <= entries, "only listed files are checked"); + assert_eq!(crate::opens_under(&src.join("unused")), 0); + dc.flush(); + drop((pool, lc, dc)); + + // Its entry was replaced: the next restart opens nothing and shows the new pixels. + let (pool, lc, _dc) = session(&cache); + let opened = crate::opens_under(&src); + let (last, _) = render(&p, &pool, &lc, cid, 0); + assert_eq!(last, changed); + assert_eq!(crate::opens_under(&src), opened, "both from disk"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Two projects whose footage items have the same ids but name different files never share a +/// layer entry: the layer key names the item's path and settings. +#[test] +fn projects_with_the_same_item_ids_never_share_a_layer_entry() { + let dir = folder("layer-disk-projects"); + let (src, cache) = (dir.join("src"), dir.join("cache")); + std::fs::create_dir_all(&src).unwrap(); + let mut p = Project::default(); + let c = comp(); + let layer = footage_layer(&mut p, &c, "Plate", still(&src.join("one.png"), 40)); + let LayerSource::Footage { item } = layer.source else { panic!("a footage layer") }; + let cid = add_comp(&mut p, "C", c, vec![layer]); + let mut other = p.clone(); + other.item_mut(item).unwrap().kind = ItemKind::Footage(still(&src.join("two.png"), 220)); + let (pool, lc, dc) = session(&cache); + let (one, _) = render(&p, &pool, &lc, cid, 0); + dc.flush(); + drop((pool, lc, dc)); + let (pool, lc, dc) = session(&cache); + let hits = dc.stats().hits; + let (two, files) = render(&other, &pool, &lc, cid, 0); + assert!((two[0] - 220.0 / 255.0).abs() < 0.01, "the other project's own file shows: {two:?}"); + assert_ne!(two, one); + assert_eq!(dc.stats().hits, hits, "no entry of the first project is used"); + assert_eq!(paths(&files), [src.join("two.png").to_string_lossy().to_string()]); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Safe: a damaged layer entry, a source file that is missing, one replaced by something +/// unreadable (a folder), and one back with other pixels never serve a stale buffer after a +/// restart; the damaged entry is rendered again, replaced and then used. +#[test] +fn missing_unreadable_files_and_damaged_layer_entries_never_serve_stale_buffers() { + let dir = folder("layer-disk-safe"); + let (src, cache) = (dir.join("src"), dir.join("cache")); + std::fs::create_dir_all(&src).unwrap(); + let mut p = Project::default(); + let c = comp(); + let path = src.join("plate.png"); + let layer = footage_layer(&mut p, &c, "Plate", still(&path, 90)); + let cid = add_comp(&mut p, "C", c, vec![layer]); + let (pool, lc, dc) = session(&cache); + let (shown, _) = render(&p, &pool, &lc, cid, 0); + assert!(shown[3] > 0.99, "the plate shows: {shown:?}"); + dc.flush(); + drop((pool, lc, dc)); + + // A damaged entry: a miss, deleted, rendered again and stored anew. + let layers = cache.join("v1").join(Kind::Layer.dir()); + for shard in std::fs::read_dir(&layers).unwrap().flatten() { + for f in std::fs::read_dir(shard.path()).unwrap().flatten() { + let mut d = std::fs::read(f.path()).unwrap(); + let n = d.len() / 2; + d[n] ^= 0x5a; + std::fs::write(f.path(), d).unwrap(); + } + } + let (pool, lc, dc) = session(&cache); + let opened = crate::opens_under(&src); + let (again, files) = render(&p, &pool, &lc, cid, 0); + assert_eq!(again, shown); + assert_eq!(crate::opens_under(&src) - opened, 1, "read again, not served from the damaged entry"); + assert!(files.is_complete()); + dc.flush(); + drop((pool, lc, dc)); + let (pool, lc, dc) = session(&cache); + let hits = dc.stats().hits; + assert_eq!(render(&p, &pool, &lc, cid, 0).0, shown); + assert!(dc.stats().hits > hits, "the rewritten entry is used"); + drop((pool, lc, dc)); + + // The file is gone: not the old plate. + std::fs::remove_file(&path).unwrap(); + let (pool, lc, dc) = session(&cache); + let hits = dc.stats().hits; + let (gone, files) = render(&p, &pool, &lc, cid, 0); + assert!(gone[3] < 0.01, "a missing file shows nothing, never the cached plate: {gone:?}"); + assert_eq!(dc.stats().hits, hits); + assert!(files.files().iter().all(|f| f.stamp == FileStamp::Missing), "{files:?}"); + dc.flush(); + drop((pool, lc, dc)); + + // Something unreadable at its path (a folder): still not the old plate. + std::fs::create_dir(&path).unwrap(); + let (pool, lc, _dc) = session(&cache); + let (unreadable, _) = render(&p, &pool, &lc, cid, 0); + assert!(unreadable[3] < 0.01, "{unreadable:?}"); + std::fs::remove_dir(&path).unwrap(); + + // Back with other pixels: those show, not the entry made of the old file. + png(&path, 240); + touch_later(&path); + let (pool, lc, _dc) = session(&cache); + let (back, _) = render(&p, &pool, &lc, cid, 0); + assert!((back[0] - 240.0 / 255.0).abs() < 0.01, "{back:?}"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/media/src/stream.rs b/crates/media/src/stream.rs index 80db21fc4..0155b4acd 100644 --- a/crates/media/src/stream.rs +++ b/crates/media/src/stream.rs @@ -9,6 +9,7 @@ use std::path::Path; +use effectcraft_render::reads::FileStamp; use filmcraft_media::SharedSource; use crate::Result; @@ -19,17 +20,22 @@ use crate::Result; pub(crate) struct FileReader { file: std::fs::File, len: u64, + /// The file's identity when it was opened. + stamp: FileStamp, } #[cfg(any(unix, windows))] impl FileReader { + /// Opens `path` for reading and records its length and modification time. pub(crate) fn open(path: &Path) -> std::io::Result { + #[cfg(test)] + crate::count_open(path); let file = std::fs::File::open(path)?; let m = file.metadata()?; if !m.is_file() { return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display()))); } - Ok(Self { file, len: m.len() }) + Ok(Self { file, len: m.len(), stamp: FileStamp::of_metadata(&m) }) } } @@ -72,16 +78,19 @@ fn name_of(path: &Path) -> String { /// Open a movie/audio file for the media pool, reading only what its container needs; files the /// reader openers don't handle are read whole and opened from memory, exactly as before. `None` /// when the file can't be opened in place (the caller reads it whole, which reports the error). +/// With the file's identity as opened. #[cfg(any(unix, windows))] -pub(crate) fn open_source(path: &Path) -> Option> { +pub(crate) fn open_source(path: &Path) -> Option<(Result, FileStamp)> { let reader = FileReader::open(path).ok()?; + let stamp = reader.stamp; let reader: filmcraft_media::SharedReader = std::sync::Arc::new(reader); let opened = filmcraft_media::reader::open_reader(&name_of(path), reader, &filmcraft_codecs::reader_openers(), &filmcraft_codecs::openers()); - Some(opened.map_err(Into::into)) + Some((opened.map_err(Into::into), stamp)) } +/// Without a native file system there is no in-place source: always `None`. #[cfg(not(any(unix, windows)))] -pub(crate) fn open_source(_path: &Path) -> Option> { +pub(crate) fn open_source(_path: &Path) -> Option<(Result, FileStamp)> { None } diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index 079b81765..c9322ac37 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -35,6 +35,7 @@ mod footage; mod item; mod layer; mod layers; +mod reach; mod settings; pub use comp::{Comp, Guide, Renderer, Resolution}; @@ -46,6 +47,7 @@ pub use layer::{ reads_other_frames, }; pub use layers::Layers; +pub use reach::{ExprReach, PixelReach, expression_reach}; pub use settings::{ APP_VERSION, BitDepth, ColorEngine, CuePoint, HdrMode, Marker, ProjectError, ProjectSettings, SCHEMA_EFFECT_SPACE, SCHEMA_VERSION, SavedView, TimeDisplayStyle, is_newer_version, saved_by, saved_view, diff --git a/crates/project/src/reach.rs b/crates/project/src/reach.rs new file mode 100644 index 000000000..31a2ed567 --- /dev/null +++ b/crates/project/src/reach.rs @@ -0,0 +1,405 @@ +//! What a composition's pixels can depend on, from the project in memory: the items it draws +//! and, when expressions are among them, what those expressions can read (#3184). The disk +//! cache keys a comp's frames by exactly these items, so an edit elsewhere in the project keeps +//! them. +//! +//! An expression reaches its own comp's layers (`thisComp`, `thisLayer`), the sources of the +//! layers it can read (`layer.source`, a precomp's layers through it), and other items by name: +//! `comp("Name")` and `footage("Name")`. A name given as a plain string literal is followed; a +//! computed name (`comp("Shot " + index)`), the object model's constructors and internals +//! (`new Comp(7)`, `__h`), dynamic code (`eval`, `Function`) and anything the scanner can't read +//! for sure (a regular expression literal, a template string, an escape in a name) count as +//! reaching every item of the project. The scan errs on the side of reaching more: an item +//! wrongly left out would serve a stale frame, one wrongly let in only renders again. +//! +//! Known limit (`ponytail`): an expression that builds the names of the object model's private +//! fields at run time into a variable and writes them with a computed key it reads from that +//! variable could forge another comp's id. That is deliberate tampering with the internals, not +//! an expression one writes; the scan refuses every direct form of it (`__` names, the +//! constructors, `Object`, `JSON`, `call`/`apply`/`bind`, computed writes and computed keys). +//! Upgrade: have the expression host record which items an evaluation reached. + +use std::collections::{BTreeMap, BTreeSet}; + +use crate::{Comp, ItemId, ItemKind, LayerId, Node, Project, PropGroup, Value}; + +/// What an expression's text can reach outside its own comp and the layers it can read. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ExprReach { + /// Only items named by string literals: comps by `comp("…")`, footage by `footage("…")`. + Names { comps: Vec, footage: Vec }, + /// Any item of the project. + Anything, +} + +/// Names that hand an expression another item by id, run code built at run time, or give it the +/// tools to forge the object model's private fields. +const REACHES_ANYTHING: &[&str] = &[ + "Comp", + "Footage", + "Layer", + "Prop", + "Markers", + "eval", + "Function", + "globalThis", + "this", + "self", + "window", + "arguments", + "constructor", + "prototype", + "Object", + "Reflect", + "Proxy", + "JSON", + "call", + "apply", + "bind", + "class", + "with", + "import", + "require", + "fromCharCode", + "fromCodePoint", + "getOwnPropertyDescriptor", + "getOwnPropertyDescriptors", + "getPrototypeOf", + "setPrototypeOf", + "defineProperty", +]; + +/// Keywords after which a `/` starts a regular expression. +const BEFORE_REGEX: &[&str] = &["return", "typeof", "instanceof", "in", "of", "new", "delete", "void", "throw", "case", "do", "else", "yield", "await"]; + +/// Keywords whose `( … )` a regular expression may follow (`if (a) /x/.test(s)`). +const CONTROL: &[&str] = &["if", "while", "for", "with"]; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Tok<'a> { + Ident(&'a str), + /// A string literal's text between its quotes; `plain` when it has no escape. + Str { + text: &'a str, + plain: bool, + }, + Num, + Punct(u8), +} + +/// The tokens of `text`, or `None` when it can't be read for sure. +fn tokens(text: &str) -> Option>> { + let b = text.as_bytes(); + let mut out: Vec> = Vec::new(); + // Per open `(`: whether a control keyword is before it; whether the last `)` closed one + // (only looked at right after a `)`). + let mut parens: Vec = Vec::new(); + let mut control_close = false; + let mut i = 0; + while let Some(&c) = b.get(i) { + match c { + b' ' | b'\t' | b'\n' | b'\r' | 0x0b | 0x0c => i += 1, + b'/' if b.get(i + 1) == Some(&b'/') => { + while b.get(i).is_some_and(|c| *c != b'\n') { + i += 1; + } + } + b'/' if b.get(i + 1) == Some(&b'*') => { + let end = text.get(i + 2..)?.find("*/")?; + i += 2 + end + 2; + } + b'/' => { + if regex_may_start(out.last(), control_close) { + return None; + } + out.push(Tok::Punct(c)); + i += 1; + } + b'"' | b'\'' => { + let start = i + 1; + let mut j = start; + let mut plain = true; + loop { + match *b.get(j)? { + q if q == c => break, + b'\n' | b'\r' => return None, + b'\\' => { + // Only escapes that can't spell a name: `\n`, `\t`, a quote... + if !matches!(b.get(j + 1)?, b'n' | b'r' | b't' | b'b' | b'f' | b'v' | b'\'' | b'"' | b'\\') { + return None; + } + plain = false; + j += 2; + } + _ => j += 1, + } + } + let s = text.get(start..j)?; + if s.contains("__") { + return None; + } + out.push(Tok::Str { text: s, plain }); + i = j + 1; + } + b'0'..=b'9' => { + while b.get(i).is_some_and(|c| c.is_ascii_alphanumeric() || *c == b'_' || *c == b'.') { + i += 1; + } + out.push(Tok::Num); + } + b'.' if b.get(i + 1).is_some_and(u8::is_ascii_digit) => { + i += 1; + while b.get(i).is_some_and(|c| c.is_ascii_alphanumeric() || *c == b'_' || *c == b'.') { + i += 1; + } + out.push(Tok::Num); + } + c if c.is_ascii_alphabetic() || c == b'_' || c == b'$' => { + let start = i; + while b.get(i).is_some_and(|c| c.is_ascii_alphanumeric() || *c == b'_' || *c == b'$') { + i += 1; + } + out.push(Tok::Ident(text.get(start..i)?)); + } + // A template string (code inside `${}`), an escaped identifier, a name in another + // alphabet: not followed. + b'`' | b'\\' => return None, + c if !c.is_ascii() => return None, + c => { + match c { + b'(' => parens.push(matches!(out.last(), Some(Tok::Ident(k)) if CONTROL.contains(k))), + b')' => control_close = parens.pop().unwrap_or(false), + _ => {} + } + out.push(Tok::Punct(c)); + i += 1; + } + } + } + Some(out) +} + +/// Whether a `/` after `prev` may start a regular expression literal (which the scanner doesn't +/// read: its text could hide a quote). +fn regex_may_start(prev: Option<&Tok<'_>>, control_close: bool) -> bool { + match prev { + None => true, + Some(Tok::Ident(k)) => BEFORE_REGEX.contains(k), + Some(Tok::Num | Tok::Str { .. }) => false, + Some(Tok::Punct(b')')) => control_close, + Some(Tok::Punct(b']')) => false, + Some(Tok::Punct(_)) => true, + } +} + +/// What the expression `text` can reach by name, or [`ExprReach::Anything`]. +pub fn expression_reach(text: &str) -> ExprReach { + expression_names(text).map_or(ExprReach::Anything, |(comps, footage)| ExprReach::Names { comps, footage }) +} + +/// The comp names and footage names an expression references by literal name. +/// Returns `None` when it can reach anything by a computed name or cannot be tokenised. +fn expression_names(text: &str) -> Option<(Vec, Vec)> { + let toks = tokens(text)?; + let (mut comps, mut footage) = (Vec::new(), Vec::new()); + // Per open `[`: a computed key (`{[k]: v}`), a computed member (`o[k]`). + let mut brackets: Vec<(bool, bool)> = Vec::new(); + for (i, t) in toks.iter().enumerate() { + let prev = i.checked_sub(1).and_then(|p| toks.get(p)); + let next = |n: usize| toks.get(i + n); + match *t { + Tok::Ident(name) if name.starts_with("__") || REACHES_ANYTHING.contains(&name) => return None, + Tok::Ident(name @ ("comp" | "footage")) => { + // Only a call with a plain literal, not a property of something else. + let (Some(Tok::Punct(b'(')), Some(Tok::Str { text, plain: true }), Some(Tok::Punct(b')'))) = (next(1), next(2), next(3)) else { return None }; + if matches!(prev, Some(Tok::Punct(b'.'))) { + return None; + } + if name == "comp" { comps.push((*text).to_string()) } else { footage.push((*text).to_string()) } + } + Tok::Punct(b'[') => { + let key = matches!(prev, Some(Tok::Punct(b'{' | b',')) | Some(Tok::Ident("get" | "set" | "static" | "async")) | Some(Tok::Punct(b'*'))); + let member = matches!(prev, Some(Tok::Ident(_) | Tok::Str { .. } | Tok::Num | Tok::Punct(b')' | b']' | b'.'))); + brackets.push((key, member)); + } + Tok::Str { .. } if brackets.last().is_some_and(|(_, member)| *member) => return None, + Tok::Punct(b']') => { + let (key, _) = brackets.pop().unwrap_or((true, true)); + // A computed key (`{[k]: v}`, `{[k]() {}}`) or a write through brackets (`o[k] = v`, + // `o[k] ||= v`, `for (o[k] in …)`, `[o[k]] = v`) could name a private field. + if key && matches!(next(1), Some(Tok::Punct(b':' | b'('))) { + return None; + } + if assigns(&toks, i + 1) || matches!(next(1), Some(Tok::Ident("in" | "of"))) { + return None; + } + } + // A destructuring write (`({a: o[k]} = v)`). + Tok::Punct(b'}') if assigns(&toks, i + 1) => return None, + _ => {} + } + } + Some((comps, footage)) +} + +/// Whether the tokens from `at` are an assignment operator (`=`, `+=`, `>>>=`, `??=`...), not a +/// comparison (`==`, `<=`) or an arrow (`=>`). +fn assigns(toks: &[Tok<'_>], at: usize) -> bool { + let mut op = Vec::new(); + for t in toks.iter().skip(at).take(5) { + match t { + Tok::Punct(c) if b"+-*/%&|^<>?=".contains(c) => op.push(*c), + _ => break, + } + } + let Some(eq) = op.iter().position(|c| *c == b'=') else { return false }; + let (before, after) = op.split_at(eq); + if after.get(1).is_some_and(|c| *c == b'=' || *c == b'>') { + return false; + } + matches!(before, b"" | b"+" | b"-" | b"*" | b"/" | b"%" | b"&" | b"|" | b"^" | b"**" | b"<<" | b">>" | b">>>" | b"&&" | b"||" | b"??") +} + +/// The items a comp's pixels can depend on ([`Project::pixel_reach`]). +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct PixelReach { + /// The items whose state counts; `None`: every item of the project. + pub items: Option>, + /// Expressions are among them: they can read every layer of the comps they reach (hidden + /// and silent ones too) and the markers, which therefore count as well (#1083). + pub expressions: bool, + /// The names expressions look items up by (`true`: a comp's, `false`: footage's), each with + /// the ids of the items of that kind that have it now, in id order (the first one is what + /// the lookup finds; another item taking or giving up the name changes it). + pub names: Vec<(bool, String, Vec)>, +} + +/// Each property of a group and its subgroups (iterative: no depth limit to hit). +fn each_prop<'a>(g: &'a PropGroup, f: &mut dyn FnMut(&'a crate::Property) -> Option<()>) -> Option<()> { + let mut stack = vec![g]; + while let Some(g) = stack.pop() { + for n in &g.children { + match n { + Node::Prop(p) => f(p)?, + Node::Group(sub) => stack.push(sub), + } + } + } + Some(()) +} + +impl Project { + /// The items comp `comp` draws ([`Self::silent_layers`] aside, with `heard`) and the + /// footage its precomp layers swap in (Essential Graphics Media Replacement); with an + /// expression in any of its comps, everything those expressions can reach + /// ([`expression_reach`]). Memory only: no file is looked at. + pub fn pixel_reach(&self, comp: ItemId, heard: &crate::Heard) -> PixelReach { + let Some(drawn) = self.reach_walk(comp, &|id, c| self.silent_layers(id, c, heard), &mut |_| Some(vec![])) else { return PixelReach::every() }; + let has_expression = |g: &PropGroup| each_prop(g, &mut |p| p.expr.is_none().then_some(())).is_none(); + if !drawn.iter().filter_map(|id| self.comp(*id)).any(|c| c.layers.iter().any(|l| has_expression(&l.props))) { + return PixelReach { items: Some(drawn), expressions: false, names: vec![] }; + } + // Expressions: every layer of a comp they reach counts, and what they name. + let mut names: BTreeMap<(bool, String), Vec> = BTreeMap::new(); + let mut scan = |p: &crate::Property| match p.expr.as_ref().map(|e| expression_reach(&e.text)) { + None => Some(vec![]), + Some(ExprReach::Anything) => None, + Some(ExprReach::Names { comps, footage }) => { + let mut found = vec![]; + for (is_comp, name) in comps.into_iter().map(|n| (true, n)).chain(footage.into_iter().map(|n| (false, n))) { + let ids = self.named(is_comp, &name); + found.extend(ids.iter().copied()); + names.insert((is_comp, name), ids); + } + Some(found) + } + }; + let Some(mut items) = self.reach_walk(comp, &|_, _| BTreeSet::new(), &mut scan) else { return PixelReach::every() }; + // A source's data is read by its name (`layer.source.sourceData`). + let footage_names: BTreeSet<&str> = + items.iter().filter_map(|id| self.item(*id)).filter(|i| matches!(i.kind, ItemKind::Footage(_))).map(|i| i.name.as_str()).collect(); + let same_name: Vec = + self.items.values().filter(|i| matches!(i.kind, ItemKind::Footage(_)) && footage_names.contains(i.name.as_str())).map(|i| i.id).collect(); + items.extend(same_name); + PixelReach { items: Some(items), expressions: true, names: names.into_iter().map(|((c, n), ids)| (c, n, ids)).collect() } + } + + /// The items of one kind (`is_comp`: comps, else footage) named `name`, in id order. + fn named(&self, is_comp: bool, name: &str) -> Vec { + self.items + .values() + .filter(|i| i.name == name && if is_comp { matches!(i.kind, ItemKind::Comp(_)) } else { matches!(i.kind, ItemKind::Footage(_)) }) + .map(|i| i.id) + .collect() + } + + /// The items reached from `comp` through its layers (but those `quiet` names) and the items + /// `scan` returns for each of their properties; `None` when something reaches every item. + /// (A stack and a visited set: each item is visited once, cycles included.) + fn reach_walk( + &self, + comp: ItemId, + quiet: &dyn Fn(ItemId, &Comp) -> BTreeSet, + scan: &mut dyn FnMut(&crate::Property) -> Option>, + ) -> Option> { + let mut seen = BTreeSet::new(); + let mut stack = vec![comp]; + while let Some(id) = stack.pop() { + if !seen.insert(id) { + continue; + } + let Some(c) = self.comp(id) else { continue }; + let quiet = quiet(id, c); + for l in c.layers.iter().filter(|l| !quiet.contains(&l.id)) { + // Every source item: a 3D model's too (it is drawn, and `layer.source` reads its + // name and size). + if let Some(item) = l.source.item() { + stack.push(item); + } + stack.extend(self.media_replacements(l)?); + let mut reached = vec![]; + each_prop(&l.props, &mut |p| { + reached.extend(scan(p)?); + Some(()) + })?; + stack.extend(reached); + } + } + Some(seen) + } + + /// The footage a precomp layer swaps in through its Essential Properties (Media Replacement): + /// the items its overridden properties name. `None` when they can't be listed (an + /// expression or keyframes choose the item over time). + fn media_replacements(&self, layer: &crate::Layer) -> Option> { + let over = crate::essential::overridden(layer); + let Some(g) = crate::essential::group(layer).filter(|_| !over.is_empty()) else { return Some(vec![]) }; + let mut out = vec![]; + each_prop(g, &mut |p| { + if !over.contains(&p.uid) { + return Some(()); + } + if p.expr.is_some() || !p.keys.is_empty() { + return None; + } + if let Value::Scalar(id) = p.value { + let item = ItemId(id.max(0.0) as u64); + if matches!(self.item(item).map(|i| &i.kind), Some(ItemKind::Footage(_))) { + out.push(item); + } + } + Some(()) + })?; + Some(out) + } +} + +impl PixelReach { + /// Every item of the project, with expressions. + fn every() -> PixelReach { + PixelReach { items: None, expressions: true, names: vec![] } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/project/src/reach/tests.rs b/crates/project/src/reach/tests.rs new file mode 100644 index 000000000..53db8172a --- /dev/null +++ b/crates/project/src/reach/tests.rs @@ -0,0 +1,198 @@ +use super::*; +use crate::{Expression, LayerSource, Solid, build}; +use effectcraft_time::{FrameRate, Tick}; + +fn names(comps: &[&str], footage: &[&str]) -> ExprReach { + ExprReach::Names { comps: comps.iter().map(|s| s.to_string()).collect(), footage: footage.iter().map(|s| s.to_string()).collect() } +} + +/// Expressions that read their own comp, or other items by literal names, are followed; every +/// way to reach items by a computed name or an id, or text the scanner can't read for sure, +/// reaches everything. +#[test] +fn expression_reach_follows_literal_names_and_refuses_the_rest() { + let follows = [ + ("wiggle(2, 20)", names(&[], &[])), + ("thisComp.layer(\"Comp 1 Layer\").transform.opacity * 2 / 3", names(&[], &[])), + ("value + [10, 0]; value[0] / 2", names(&[], &[])), + ("// comp(name) in a comment\n/* footage(x) */ value", names(&[], &[])), + ("\"a string that says comp(x) and Layer\"", names(&[], &[])), + ("comp(\"Other\").layer(1).transform.position", names(&["Other"], &[])), + ("comp( 'A' ).width + comp(\"B\").height", names(&["A", "B"], &[])), + ("footage(\"data.json\").sourceData.x", names(&[], &["data.json"])), + ("x = a[0] <= b ? a[1] : 0; y = a[0] == 1", names(&[], &[])), + ("thisLayer.source.layer(1).name", names(&[], &[])), + ("if (a) x = 1; else x = 2; (a + b) / 2", names(&[], &[])), + ("\"Ünïcödé name\"", names(&[], &[])), + ("const f = (v) => v * 2; f(1)", names(&[], &[])), + ]; + for (text, want) in follows { + assert_eq!(expression_reach(text), want, "{text}"); + } + let anything = [ + "comp(\"Shot \" + index).layer(1)", + "comp(name)", + "var n = \"Other\"; comp(n)", + "comp(\"O\\\"ther\")", + "x.comp(\"Other\")", + "var comp = 1", + "footage(thisComp.name)", + "new Comp(7).layer(1)", + "Layer(7, 3).name", + "__h(__COMP, 7)", + "o.__c = 7", + "eval('1')", + "Function('return 1')()", + "this.x", + "({}).constructor", + "Object.getPrototypeOf(thisComp)", + "JSON.parse(s)", + "thisComp.layer.call(o, 1)", + "o['_' + '_c']", + "o[k] = 7", + "o[k] ||= 7", + "o[k] += 7", + "[o[k]] = [7]", + "({a: o[k]} = {a: 7})", + "for (o[k] in x) {}", + "({[k]: 7})", + "({a: 1, [k]: 7})", + "o = {'__c': 7}", + "'\\x5f\\x5fc'", + "'\\_\\_c'", + "`${x}`", + "c\\u006fmp('A')", + "comp\u{0430}('A')", + "/\"/.test(s); comp(n); \"", + "return /x/", + "if (a) /\"/.test(s)", + "x = '", + "/* comp(n)", + "class X {}", + "String.fromCharCode(95)", + ]; + for text in anything { + assert_eq!(expression_reach(text), ExprReach::Anything, "{text}"); + } +} + +/// `a / b` divides after a name, a number, a string and a closing bracket or parenthesis. +#[test] +fn division_is_not_a_regular_expression() { + for text in ["a / b", "2 / 3", "'a' / 2", "x[0] / 2", "(a + b) / 2", "f(x) / 2 / g(y)"] { + assert_eq!(expression_reach(text), names(&[], &[]), "{text}"); + } +} + +struct Fixture { + p: Project, + main: ItemId, + other: ItemId, + third: ItemId, + unused: ItemId, + solid: ItemId, +} + +/// Comps "Main" (a solid), "Other" (a solid), "Third" (empty), an unused footage item. +fn fixture() -> Fixture { + let mut p = Project::default(); + let comp = Comp::new(64, 64, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); + let solid = + p.add_item("Solid", effectcraft_color::Label::None, None, ItemKind::Solid(Solid { color: [1.0, 0.0, 0.0], width: 64, height: 64, pixel_aspect: 1.0 })); + let main = p.add_item("Main", effectcraft_color::Label::None, None, ItemKind::Comp(comp.clone().into())); + let other = p.add_item("Other", effectcraft_color::Label::None, None, ItemKind::Comp(comp.clone().into())); + let third = p.add_item("Third", effectcraft_color::Label::None, None, ItemKind::Comp(comp.clone().into())); + let unused = p.add_item("unused.png", effectcraft_color::Label::None, None, ItemKind::Footage(crate::Footage { has_video: true, ..Default::default() })); + for c in [main, other] { + let l = build::layer(&mut p, &comp, "S", LayerSource::Solid { item: solid }, (64, 64), None); + p.comp_mut(c).unwrap().layers.push(l); + } + Fixture { p, main, other, third, unused, solid } +} + +fn set_expr(p: &mut Project, comp: ItemId, text: &str) { + let l = p.comp_mut(comp).unwrap().layers.first_mut().unwrap(); + l.props.prop_mut("transform/opacity").unwrap().expr = Some(Expression { text: text.into(), enabled: true }); +} + +fn reach(p: &Project, comp: ItemId) -> PixelReach { + p.pixel_reach(comp, &p.heard_layers()) +} + +#[test] +fn pixel_reach_is_what_the_comp_draws_and_what_its_expressions_name() { + let mut f = fixture(); + let ids = |r: &PixelReach| r.items.clone().map(|s| s.into_iter().collect::>()); + assert_eq!(reach(&f.p, f.main), PixelReach { items: Some([f.solid, f.main].into()), expressions: false, names: vec![] }); + // An expression reading only its own comp: what it draws, with expressions. + set_expr(&mut f.p, f.main, "thisComp.layer(1).transform.opacity"); + let r = reach(&f.p, f.main); + assert!(r.expressions); + assert_eq!(ids(&r), Some(vec![f.solid, f.main])); + // An expression in another comp doesn't reach this one. + set_expr(&mut f.p, f.other, "wiggle(1, 1)"); + assert_eq!(ids(&reach(&f.p, f.main)), Some(vec![f.solid, f.main])); + // A literal name: that comp and what it draws, with the name and the comps that have it. + set_expr(&mut f.p, f.main, "comp(\"Other\").layer(1).transform.opacity"); + let r = reach(&f.p, f.main); + assert_eq!(ids(&r), Some(vec![f.solid, f.main, f.other])); + assert_eq!(r.names, vec![(true, "Other".to_string(), vec![f.other])]); + // Another comp taking the name is part of it, whichever the lookup finds. + f.p.item_mut(f.third).unwrap().name = "Other".into(); + let r = reach(&f.p, f.main); + assert_eq!(r.names, vec![(true, "Other".to_string(), vec![f.other, f.third])]); + assert!(r.items.unwrap().contains(&f.third)); + // A name no item has yet still counts (with no item). + set_expr(&mut f.p, f.main, "footage(\"later.json\").sourceData"); + assert_eq!(reach(&f.p, f.main).names, vec![(false, "later.json".to_string(), vec![])]); + // A computed name reaches everything. + set_expr(&mut f.p, f.main, "comp(\"Ot\" + \"her\").width"); + assert_eq!(reach(&f.p, f.main), PixelReach { items: None, expressions: true, names: vec![] }); + // Footage reached as a layer's source brings the footage of its name (its data is read by + // name). + let comp = f.p.comp(f.main).unwrap().clone(); + let l = build::layer(&mut f.p, &comp, "U", LayerSource::Footage { item: f.unused }, (64, 64), None); + f.p.comp_mut(f.main).unwrap().layers.push(l); + let twin = f.p.add_item("unused.png", effectcraft_color::Label::None, None, ItemKind::Footage(crate::Footage::default())); + set_expr(&mut f.p, f.main, "value"); + let r = reach(&f.p, f.main).items.unwrap(); + assert!(r.contains(&f.unused) && r.contains(&twin), "{r:?}"); +} + +/// A comp nested in the reach is scanned too: its expression's names count for the outer comp. +#[test] +fn nested_comps_bring_their_expressions_reach() { + let mut f = fixture(); + let comp = f.p.comp(f.main).unwrap().clone(); + let pre = build::layer(&mut f.p, &comp, "Pre", LayerSource::Comp { item: f.other }, (64, 64), None); + f.p.comp_mut(f.main).unwrap().layers.push(pre); + set_expr(&mut f.p, f.other, "comp(\"Third\").width"); + let r = reach(&f.p, f.main); + assert!(r.expressions && r.items.unwrap().contains(&f.third)); + // A cycle through names ends. + let l = build::layer(&mut f.p, &comp, "S", LayerSource::Solid { item: f.solid }, (64, 64), None); + f.p.comp_mut(f.third).unwrap().layers.push(l); + set_expr(&mut f.p, f.third, "comp(\"Main\").width"); + assert!(reach(&f.p, f.main).items.unwrap().contains(&f.main)); +} + +/// A 3D model layer's item is reached like any other source: it is drawn, and an expression +/// reads its name and size through `layer.source` (a renamed model left the comp's disk key and +/// its old frames in place). +#[test] +fn model_sources_are_reached() { + let mut f = fixture(); + let model = f.p.add_item( + "chair.glb", + effectcraft_color::Label::None, + None, + ItemKind::Footage(crate::Footage { kind: crate::FootageKind::Model, ..Default::default() }), + ); + let comp = f.p.comp(f.main).unwrap().clone(); + let l = build::layer(&mut f.p, &comp, "Model", LayerSource::Model { item: model }, (64, 64), None); + f.p.comp_mut(f.main).unwrap().layers.push(l); + assert!(reach(&f.p, f.main).items.unwrap().contains(&model), "drawn"); + set_expr(&mut f.p, f.main, "thisComp.layer(\"Model\").source.name.length"); + let r = reach(&f.p, f.main); + assert!(r.expressions && r.items.unwrap().contains(&model), "read by an expression"); +} diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml index efbd1fc19..cd67e846a 100644 --- a/crates/render/Cargo.toml +++ b/crates/render/Cargo.toml @@ -26,3 +26,6 @@ rayon = { workspace = true } web-time = { workspace = true } serde = { workspace = true } log = { workspace = true } + +[dev-dependencies] +tempfile = { workspace = true } diff --git a/crates/render/src/accel.rs b/crates/render/src/accel.rs index f545a62ee..0f271c59d 100644 --- a/crates/render/src/accel.rs +++ b/crates/render/src/accel.rs @@ -224,7 +224,7 @@ impl<'a> Renderer<'a> { LayerSource::Footage { item } => { let item = ctx.media_item(layer, *item); let ItemKind::Footage(f) = &self.project.item(item)?.kind else { return None }; - let a = self.footage.aux(item, f, ctx.source_time(layer))?; + let a = self.read(|| self.footage.aux(item, f, ctx.source_time(layer)))?; // Aux pixels per layer pixel (the layer is the footage at its nominal size). The // planes are only copied when that changes the scale. let scale = a.width as f64 / f.width as f64; @@ -349,20 +349,19 @@ impl<'a> Renderer<'a> { pub fn adjustment_footprint(&self, ctx: &EvalCtx, layer: &Layer) -> Option> { // Cached, so an accelerator sees the same buffer (and uploads it once) every frame. let key = self.cache.and_then(|_| cache::footprint_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer))); - if let (Some(c), Some(k)) = (self.cache, key) - && let Some(b) = c.get(k) - { + let key = self.mask_gate_key(ctx, layer, key); + if let Some((b, _)) = self.cached(key) { return Some(b); } - let mut foot = self.source(ctx, layer)?; - let _ = masks::apply(ctx, layer, &mut foot, self.mask_blur(ctx, layer)); - let foot = Arc::new(foot); + let (foot, files) = self.recorded(key, |r| { + // (what the masks' expressions read, at shutter times too, goes to the buffer's files) + let ctx = &r.with_reads(ctx); + let mut foot = r.source(ctx, layer)?; + let _ = masks::apply(ctx, layer, &mut foot, r.mask_blur(ctx, layer)); + Some(foot) + }); // The buffer the cache keeps (another worker's, if it kept one first). - let foot = match (self.cache_to_keep(), key) { - (Some(c), Some(k)) => c.insert(k, foot), - _ => foot, - }; - Some(foot) + Some(self.keep(key, Arc::new(foot?), &files).0) } /// Draw one 2D layer into `canvas` on the CPU (any kind: adjustment, wireframe, styled…). diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index cf1a84e82..8a3d7b8ae 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -22,27 +22,36 @@ use std::hash::{Hash, Hasher}; use std::sync::{Arc, Mutex}; use effectcraft_effects::Buf; +use effectcraft_keyframe::Value; use effectcraft_project::{FootageKind, ItemKind, Layer, LayerSource, Node, PropGroup}; +use crate::disk_cache::{EntryFiles, SharedVirtualFileId, VirtualFileId, layer_disk_key}; use crate::eval::EvalCtx; +use crate::reads::ReadFiles; mod dependencies; /// Thread-safe, memory-budgeted cache of processed layer buffers, optionally backed by the /// persistent [`DiskCache`](crate::disk_cache::DiskCache): a memory miss reads the disk, and -/// buffers that were slow to render are written there (keys salted per project, see -/// [`LayerCache::set_disk`]). +/// buffers that were slow to render are written there with the source files they were made of +/// (keys: [`crate::disk_cache::layer_disk_key`]; an entry is used only while its files are +/// unchanged, #3184). pub struct LayerCache { inner: Mutex, - disk: Mutex, u64)>>, + disk: Mutex>>, /// Another persistent store when there is no disk cache (the browser's, [`PrefetchStore`]). - store: Mutex, u64)>>, + store: Mutex>>, + /// Size and content hash of files served from memory (the browser file table), to store + /// and check the source files of persistent entries. + virtual_id: Mutex>, /// While set, inserts are dropped (see [`LayerCache::set_gate`]). gate: Mutex>>, } struct Entry { buf: Arc, + /// The source files the buffer was made of (see [`crate::reads`]). + files: Arc, bytes: usize, last_use: u64, } @@ -54,7 +63,9 @@ struct Inner { clock: u64, hits: u64, misses: u64, - /// When each recent miss happened (to time the render that follows it). + /// When each recent miss happened, to time the render that follows it (the drop generation + /// a render's reads started at travels with its files instead, [`ReadFiles::since`]: two + /// renders of a key may overlap). missed_at: HashMap, } @@ -80,6 +91,7 @@ impl LayerCache { inner: Mutex::new(Inner { map: HashMap::new(), bytes: 0, budget, clock: 0, hits: 0, misses: 0, missed_at: HashMap::new() }), disk: Mutex::new(None), store: Mutex::new(None), + virtual_id: Mutex::new(None), gate: Mutex::new(None), } } @@ -98,77 +110,75 @@ impl LayerCache { self.gate.lock().ok().and_then(|g| g.as_ref().map(|g| g.load(std::sync::atomic::Ordering::Relaxed))).unwrap_or(false) } - /// Attach (or detach) the disk cache. `salt` separates projects and footage versions (see - /// `footage_salt`); keys on disk are `(salt, key)`. - pub fn set_disk(&self, disk: Option<(Arc, u64)>) { + /// Attach (or detach) the disk cache. It is used at once: its entries check their own + /// source files when read, so nothing has to be looked at first. + pub fn set_disk(&self, disk: Option>) { if let Ok(mut d) = self.disk.lock() { *d = disk; } } - /// Change the disk salt (after footage changed) keeping the attached disk cache. - pub fn set_disk_salt(&self, salt: u64) { - if let Ok(mut d) = self.disk.lock() - && let Some((_, s)) = d.as_mut() - { - *s = salt; - } - } - + /// The attached disk cache, if any. pub fn disk(&self) -> Option> { - self.disk.lock().ok()?.as_ref().map(|d| d.0.clone()) + self.disk.lock().ok()?.clone() } /// Attach (or detach) a [`LayerStore`] used when no disk cache is attached (the browser's - /// Origin Private File System through a [`PrefetchStore`]); `salt` as in - /// [`LayerCache::set_disk`]. - pub fn set_store(&self, store: Option<(Arc, u64)>) { + /// Origin Private File System through a [`PrefetchStore`]). + pub fn set_store(&self, store: Option>) { if let Ok(mut s) = self.store.lock() { *s = store; } } - /// Change the store's salt (a new project or footage), keeping the store. - pub fn set_store_salt(&self, salt: u64) { - if let Ok(mut d) = self.store.lock() - && let Some((_, s)) = d.as_mut() - { - *s = salt; + /// Size and content hash of files served from memory (the browser file table): what a + /// persistent entry keeps for them and checks them against. + pub fn set_virtual_id(&self, id: Option) { + if let Ok(mut v) = self.virtual_id.lock() { + *v = id; } } - fn disk_key(&self, key: u64) -> Option<(Arc, u128)> { - let salted = |salt: u64| ((salt as u128) << 64) | key as u128; - if let Some((dc, salt)) = self.disk.lock().ok()?.as_ref() { - return Some((dc.clone() as Arc, salted(*salt))); - } - let s = self.store.lock().ok()?; - let (st, salt) = s.as_ref()?; - Some((st.clone(), salted(*salt))) + /// The attached virtual-file identity lookup, if any. + fn virtual_id(&self) -> Option { + self.virtual_id.lock().ok()?.clone() } - pub fn get(&self, key: u64) -> Option> { - let mut g = self.inner.lock().ok()?; - g.clock += 1; - let now = g.clock; - match g.map.get_mut(&key) { - Some(e) => { - e.last_use = now; - let b = e.buf.clone(); - g.hits += 1; - Some(b) - } + /// The persistent store behind the memory: the disk cache, else the other store. + fn persistent(&self) -> Option> { + if let Some(dc) = self.disk.lock().ok()?.as_ref() { + return Some(dc.clone() as Arc); + } + self.store.lock().ok()?.clone() + } + + /// The buffer kept under `key` and the source files it was made of (see [`crate::reads`]). + /// A buffer from the disk cache or another store comes with the files its entry lists, after + /// they were found unchanged. A buffer made of a file dropped since its reads began (File ▸ + /// Reload Footage) is never served: it is forgotten, and the lookup is a miss. + pub fn get(&self, key: u64) -> Option<(Arc, Arc)> { + match self.kept(key, true) { + Some(found) => Some(found), None => { + let mut g = self.inner.lock().ok()?; g.misses += 1; - if g.missed_at.len() > 4096 { - g.missed_at.clear(); - } - g.missed_at.insert(key, web_time::Instant::now()); drop(g); - // Disk cache: a hit comes back into memory. - let (dc, dk) = self.disk_key(key)?; - let buf = dc.get_layer(dk)?; - Some(self.insert_mem(key, buf)) + let store = self.persistent(); + // (before the entry is read: what it is made of may be dropped meanwhile) + let since = store.as_ref().map_or(0, |s| s.drop_generation()); + if let Ok(mut g) = self.inner.lock() { + if g.missed_at.len() > 4096 { + g.missed_at.clear(); + } + g.missed_at.insert(key, web_time::Instant::now()); + } + // Disk cache: a hit comes back into memory, read at generation `since`. + let store = store?; + let id = self.virtual_id(); + let (buf, mut files) = store.get_layer(layer_disk_key(key), id.as_deref())?; + files.note_since(since); + let files = Arc::new(files); + Some(self.insert_mem(key, buf, files)) } } } @@ -178,39 +188,113 @@ impl LayerCache { /// together each rendered every layer; sharing the first buffer, the GPU compositor uploads /// and packs it once instead of once per worker, which ran it out of memory). Renders don't /// wait for each other: on the frame workers' shared thread pool a wait can end up under - /// the very work it waits for. - pub fn insert(&self, key: u64, buf: Arc) -> Arc { + /// the very work it waits for. `files`: the source files `buf` was made of (see + /// [`crate::reads`]); a buffer whose files can't all be named stays in memory. The files + /// returned are the ones the returned buffer was made of. + /// + /// The drop generation `buf`'s reads started at comes with `files` ([`ReadFiles::since`], + /// from [`LayerCache::drop_generation`] when the render began): a buffer made of a file + /// dropped since (File ▸ Reload Footage while it rendered) is kept neither on disk nor in + /// memory. A list that doesn't say counts as made before every drop. A buffer made of values + /// its key doesn't capture ([`ReadFiles::is_unkeyed`]) stays in memory. + pub fn insert(&self, key: u64, buf: Arc, files: Arc) -> (Arc, Arc) { if self.gated() { - return buf; + return (buf, files); } - if let Some(kept) = self.inner.lock().ok().and_then(|g| g.map.get(&key).map(|e| e.buf.clone())) { + if let Some(kept) = self.kept(key, false) { return kept; } + let store = self.persistent(); + let since = files.since().unwrap_or(0); + // (before the miss's time is taken: it may time another render of the key) + if store.as_ref().is_some_and(|s| s.dropped_since(&files, since)) { + return (buf, files); + } let started = self.inner.lock().ok().and_then(|mut g| g.missed_at.remove(&key)); - if let Some((dc, dk)) = self.disk_key(key) { - let slow = started.is_some_and(|t| t.elapsed().as_secs_f64() * 1e3 >= dc.min_layer_ms() as f64); - if slow { - dc.put_layer(dk, &buf); + if let Some(store) = store { + let id = self.virtual_id(); + // (an incomplete list, or a file from memory without its identity: not stored) + if let Some(t) = started + && !files.is_unkeyed() + && t.elapsed().as_secs_f64() * 1e3 >= store.min_layer_ms() as f64 + && let Some(entry) = EntryFiles::of(&files, id.as_deref()) + { + store.put_layer(layer_disk_key(key), &buf, &entry, since); } } - self.insert_mem(key, buf) + self.insert_mem(key, buf, files) } - /// See [`LayerCache::insert`]: the buffer kept under `key` (`buf`, unless one was kept first - /// or `buf` is too large to keep). - fn insert_mem(&self, key: u64, buf: Arc) -> Arc { + /// The buffer kept in memory under `key` with its files, if it is not made of a file dropped + /// since its reads began (one that is, is forgotten). `hit`: count it as a hit and as just + /// used. (A render that started before a drop may publish its buffer after the drop's + /// purge, Reload Footage clearing the cache while the buffer was being written: the check + /// here, not the order of the two, keeps it from being served.) + fn kept(&self, key: u64, hit: bool) -> Option<(Arc, Arc)> { + let found = { + let mut g = self.inner.lock().ok()?; + g.clock += 1; + let now = g.clock; + let e = g.map.get_mut(&key)?; + if hit { + e.last_use = now; + } + (e.buf.clone(), e.files.clone()) + }; + if self.made_before_a_drop(&found.1) { + if let Ok(mut g) = self.inner.lock() + && g.map.get(&key).is_some_and(|e| Arc::ptr_eq(&e.files, &found.1)) + && let Some(e) = g.map.remove(&key) + { + g.bytes = g.bytes.saturating_sub(e.bytes); + } + return None; + } + if hit && let Ok(mut g) = self.inner.lock() { + g.hits += 1; + } + Some(found) + } + + /// Whether a buffer made of `files` holds a file as it was before a drop that came after its + /// reads began ([`ReadFiles::since`]; a list that doesn't say counts as made before every + /// drop). + fn made_before_a_drop(&self, files: &ReadFiles) -> bool { + self.persistent().is_some_and(|s| s.dropped_since(files, files.since().unwrap_or(0))) + } + + /// The persistent store's drop generation now ([`LayerStore::drop_generation`]), taken by a + /// render before it starts reading (`None` without a store). + pub fn drop_generation(&self) -> Option { + self.persistent().map(|s| s.drop_generation()) + } + + /// See [`LayerCache::insert`]: the buffer kept under `key` and its files (`buf` and `files`, + /// unless one was kept first or `buf` is too large to keep). + fn insert_mem(&self, key: u64, buf: Arc, files: Arc) -> (Arc, Arc) { let bytes = buf.img.data.len() * std::mem::size_of::(); - let Ok(mut g) = self.inner.lock() else { return buf }; + let Ok(mut g) = self.inner.lock() else { return (buf, files) }; if bytes > g.budget / 4 { - return buf; + return (buf, files); } g.clock += 1; let now = g.clock; if let Some(e) = g.map.get_mut(&key) { - e.last_use = now; - return e.buf.clone(); + // (one made before a drop gives way: see [`LayerCache::kept`]) + if !self.made_before_a_drop(&e.files) { + e.last_use = now; + return (e.buf.clone(), e.files.clone()); + } + if let Some(old) = g.map.remove(&key) { + g.bytes = g.bytes.saturating_sub(old.bytes); + } } - g.map.insert(key, Entry { buf: buf.clone(), bytes, last_use: now }); + // (checked again just before it is published: a drop may have come since `insert` + // checked; one that comes later is caught when the buffer is looked up) + if self.made_before_a_drop(&files) { + return (buf, files); + } + g.map.insert(key, Entry { buf: buf.clone(), files: files.clone(), bytes, last_use: now }); g.bytes += bytes; if g.bytes > g.budget { // Evict least-recently used entries down to 3/4 of the budget. @@ -226,7 +310,7 @@ impl LayerCache { } } } - buf + (buf, files) } /// The memory budget in bytes. @@ -264,12 +348,28 @@ impl LayerCache { } } -/// Persistent storage of layer buffers behind a [`LayerCache`] (keys salted per project). +/// Persistent storage of layer buffers behind a [`LayerCache`] (keys: +/// [`crate::disk_cache::layer_disk_key`]). Every entry keeps the source files its buffer was made +/// of and is used only while they are unchanged (#3184). pub trait LayerStore: Send + Sync { - /// A stored buffer (synchronous: mid-render). - fn get_layer(&self, key: u128) -> Option>; - /// Keep a buffer that was slow to render. - fn put_layer(&self, key: u128, buf: &Buf); + /// A stored buffer whose source files are unchanged, with those files (synchronous: + /// mid-render). An entry whose files changed, or damaged, is a miss and is dropped, so the + /// buffer rendered again replaces it. `virtual_id` names files served from memory. + fn get_layer(&self, key: u128, virtual_id: Option>) -> Option<(Arc, ReadFiles)>; + /// Keep a buffer that was slow to render, with the source files it was made of. `since`: the + /// [`LayerStore::drop_generation`] when its render started (a buffer made of a file dropped + /// after that is refused). + fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64); + /// How many times files were dropped from the store (File ▸ Reload Footage, + /// [`crate::disk_cache::DiskCache::drop_files`]); `0` for a store that never drops. + fn drop_generation(&self) -> u64 { + 0 + } + /// Whether one of `files` was dropped after generation `since`: what was made of them is + /// from before the drop. `false` for a store that never drops. + fn dropped_since(&self, _files: &ReadFiles, _since: u64) -> bool { + false + } /// Buffers faster to render than this (ms) are not stored. fn min_layer_ms(&self) -> u64 { 0 @@ -281,9 +381,10 @@ pub const PREFETCH_MIN_LAYER_MS: u64 = 20; /// A [`LayerStore`] over storage that can't be read in the middle of a render (the browser's /// Origin Private File System, read asynchronously). Lookups are served from buffers fetched -/// before the frame ([`PrefetchStore::provide`]); the keys a render looked up and didn't find -/// are recorded ([`PrefetchStore::take_misses`]) so the transport can fetch them before the next -/// frame; buffers to store queue up as sealed entries ([`PrefetchStore::take_writes`]) for the +/// before the frame ([`PrefetchStore::provide`]), once the files their entries list are found +/// unchanged; the keys a render looked up and didn't find are recorded +/// ([`PrefetchStore::take_misses`]) so the transport can fetch them before the next frame; +/// buffers to store queue up as sealed entries ([`PrefetchStore::take_writes`]) for the /// transport to write after the frame. Fetched buffers are kept least recently used first, up /// to a byte budget. pub struct PrefetchStore { @@ -292,9 +393,21 @@ pub struct PrefetchStore { min_ms: u64, } +/// A fetched entry: the buffer, the source files it was made of, its last use. +struct Fetched { + buf: Arc, + files: Arc, + used: u64, +} + +/// The memory a buffer's pixels take, in bytes. +fn buf_bytes(b: &Buf) -> usize { + b.img.data.len().saturating_mul(std::mem::size_of::()) +} + #[derive(Default)] struct Prefetched { - ready: HashMap, u64)>, + ready: HashMap, bytes: usize, clock: u64, misses: Vec, @@ -304,6 +417,22 @@ struct Prefetched { known: std::collections::HashSet, } +impl Prefetched { + /// Records a missed key for prefetching, up to a fixed bound. + fn note_miss(&mut self, key: u128) { + if !self.misses.contains(&key) && self.misses.len() < 1024 { + self.misses.push(key); + } + } + + /// Drops the prefetched entry for `key` and frees its bytes. + fn remove(&mut self, key: u128) { + if let Some(f) = self.ready.remove(&key) { + self.bytes = self.bytes.saturating_sub(buf_bytes(&f.buf)); + } + } +} + impl Default for PrefetchStore { fn default() -> Self { PrefetchStore::new(256 << 20) @@ -327,23 +456,21 @@ impl PrefetchStore { } /// A fetched entry (sealed bytes, [`crate::disk_cache::layer_entry`]) for `key`; `false` - /// when damaged. + /// when damaged or of another format. Its files are checked when the buffer is used. pub fn provide(&self, key: u128, entry: &[u8]) -> bool { - let Some(buf) = crate::disk_cache::read_layer_entry(entry) else { return false }; - let bytes = buf.img.data.len() * std::mem::size_of::(); + let Some((buf, files)) = crate::disk_cache::read_layer_entry(entry) else { return false }; + let buf = Arc::new(buf); + let bytes = buf_bytes(&buf); let Ok(mut s) = self.st.lock() else { return false }; s.clock += 1; - let now = s.clock; - if let Some((old, _)) = s.ready.insert(key, (Arc::new(buf), now)) { - s.bytes -= old.img.data.len() * std::mem::size_of::(); - } - s.bytes += bytes; + let used = s.clock; + s.remove(key); + s.ready.insert(key, Fetched { buf, files: Arc::new(files), used }); + s.bytes = s.bytes.saturating_add(bytes); s.known.insert(key); while s.bytes > self.budget { - let Some((&k, _)) = s.ready.iter().min_by_key(|(_, (_, t))| *t) else { break }; - if let Some((b, _)) = s.ready.remove(&k) { - s.bytes -= b.img.data.len() * std::mem::size_of::(); - } + let Some(oldest) = s.ready.iter().min_by_key(|(_, f)| f.used).map(|(k, _)| *k) else { break }; + s.remove(oldest); } true } @@ -372,32 +499,46 @@ impl PrefetchStore { } impl LayerStore for PrefetchStore { - fn get_layer(&self, key: u128) -> Option> { + /// The prefetched layer for `key` with its read files, or `None` when it is not ready. + fn get_layer(&self, key: u128, virtual_id: Option>) -> Option<(Arc, ReadFiles)> { + let found = { + let mut s = self.st.lock().ok()?; + s.clock += 1; + let now = s.clock; + match s.ready.get_mut(&key) { + Some(f) => { + f.used = now; + Some((f.buf.clone(), f.files.clone())) + } + None => { + s.note_miss(key); + None + } + } + }; + let (buf, files) = found?; + // (looked at without the lock: one call of the hook, or one `metadata`, per file) + let unchanged = files.unchanged(virtual_id); let mut s = self.st.lock().ok()?; - s.clock += 1; - let now = s.clock; - match s.ready.get_mut(&key) { - Some((b, t)) => { - *t = now; - let b = b.clone(); - if !s.hits.contains(&key) { - s.hits.push(key); - } - Some(b) - } - None => { - if !s.misses.contains(&key) && s.misses.len() < 1024 { - s.misses.push(key); - } - None - } + if !unchanged { + // Made of files that changed since: forgotten, so the buffer rendered again is + // written over its entry. + s.remove(key); + s.known.remove(&key); + s.note_miss(key); + return None; } + if !s.hits.contains(&key) { + s.hits.push(key); + } + Some((buf, files.to_read_files())) } - fn put_layer(&self, key: u128, buf: &Buf) { + /// Queues the layer for writing to the persistent store the first time `key` is seen. + fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, _since: u64) { let Ok(mut s) = self.st.lock() else { return }; if s.known.insert(key) { - s.writes.push((key, crate::disk_cache::layer_entry(buf))); + s.writes.push((key, crate::disk_cache::layer_entry(buf, files))); } } @@ -470,6 +611,12 @@ fn time_dependent(layer: &Layer, effects: bool) -> bool { false } +/// A text layer with an Expression Selector: its amount is evaluated per character, and the +/// layer's key holds only the first character's (see [`crate::reads::ReadFiles::is_unkeyed`]). +pub(crate) fn per_character_expressions(layer: &Layer) -> bool { + matches!(layer.source, LayerSource::Text) && layer.props.sub("text").is_some_and(|t| group_has(t, "expressionSelector")) +} + fn group_has(g: &PropGroup, match_id: &str) -> bool { g.children.iter().any(|c| match c { Node::Group(sub) => sub.match_id == match_id || group_has(sub, match_id), @@ -741,6 +888,21 @@ fn key_any(ctx: &EvalCtx, layer: &Layer, scale: f64, draft: bool, blur: bool, fo Some(h.finish()) } +/// Does the layer read another layer through an enabled effect's layer parameter (Gradient Wipe, +/// Set Matte…)? Those layers are drawn by the reading layer's renderer, so the switches it +/// passes down belong in the key (`Renderer::nested_look`). +pub(crate) fn reads_layers(ctx: &EvalCtx, layer: &Layer) -> bool { + /// Whether any enabled layer-valued property of `layer` points at another layer. + fn reads(ctx: &EvalCtx, layer: &Layer, g: &PropGroup, depth: usize) -> bool { + depth < 32 + && g.children.iter().any(|n| match n { + Node::Prop(p) => matches!(p.value, Value::Layer(_)) && ctx.value(layer, p).as_layer().is_some_and(|id| id != layer.id.0), + Node::Group(sub) => sub.enabled && reads(ctx, layer, sub, depth + 1), + }) + } + layer.switches.effects && layer.effects().is_some_and(|fx| fx.groups().any(|g| g.enabled && reads(ctx, layer, g, 0))) +} + /// Does the layer run an effect that reads the comp camera or lights (Card Dance, CC Particle /// World…)? fn reads_comp_scene(layer: &Layer) -> bool { @@ -786,8 +948,211 @@ mod shared_insert_tests { let cache = LayerCache::default(); let buf = || Arc::new(Buf { img: Image::new(4, 4), offset: [0.0; 2], scale: 1.0 }); let (a, b) = (buf(), buf()); - assert!(Arc::ptr_eq(&cache.insert(3, a.clone()), &a)); - assert!(Arc::ptr_eq(&cache.insert(3, b), &a), "the later render gets the first buffer"); - assert!(Arc::ptr_eq(&cache.get(3).unwrap(), &a)); + assert!(Arc::ptr_eq(&cache.insert(3, a.clone(), Default::default()).0, &a)); + assert!(Arc::ptr_eq(&cache.insert(3, b, Default::default()).0, &a), "the later render gets the first buffer"); + assert!(Arc::ptr_eq(&cache.get(3).unwrap().0, &a)); + } + + /// The browser's store (#3184): a fetched entry is used only while the files it lists are + /// unchanged (here from memory, through the hook), and comes with them; one made of files + /// that changed is dropped and counts as a miss, so the buffer rendered again is queued over + /// it. A buffer whose files can't all be named is not queued at all. + #[test] + fn the_prefetch_store_checks_the_files_of_its_entries() { + use super::{LayerStore, PrefetchStore}; + use crate::disk_cache::{EntryFiles, layer_disk_key, layer_entry}; + use crate::reads::{FileStamp, ReadFile, ReadFiles}; + let buf = || Buf { img: Image::new(2, 2), offset: [0.0; 2], scale: 1.0 }; + let files: ReadFiles = [ReadFile::new("/v.png", FileStamp::Virtual)].into_iter().collect(); + let (one, two) = (|_: &str| Some((3, 1)), |_: &str| Some((3, 2))); + let entry = layer_entry(&buf(), &EntryFiles::of(&files, Some(&one)).unwrap()); + let store = PrefetchStore::new(1 << 20).with_min_ms(0); + assert!(store.provide(5, &entry)); + assert!(!store.provide(6, b"damaged")); + let (_, got) = store.get_layer(5, Some(&one)).unwrap(); + assert_eq!(got, files); + assert_eq!(store.take_hits(), [5]); + // The file changed: a miss, forgotten, and the new buffer is queued over it. + assert!(store.get_layer(5, Some(&two)).is_none()); + assert!(!store.has(5)); + assert_eq!(store.take_misses(), [5]); + store.put_layer(5, &buf(), &EntryFiles::of(&files, Some(&two)).unwrap(), 0); + assert_eq!(store.take_writes().len(), 1, "written over the changed entry"); + // Through the layer cache: a hit reports its files; incomplete lists are not queued. + let cache = LayerCache::default(); + let store = Arc::new(PrefetchStore::new(1 << 20).with_min_ms(0)); + cache.set_store(Some(store.clone())); + cache.set_virtual_id(Some(Arc::new(one))); + assert!(store.provide(layer_disk_key(9), &entry)); + let (_, from_store) = cache.get(9).unwrap(); + assert!(*from_store == files && from_store.is_complete()); + let mut incomplete = files.clone(); + incomplete.mark_incomplete(); + assert!(cache.get(10).is_none()); + cache.insert(10, Arc::new(buf()), Arc::new(incomplete)); + assert!(cache.get(11).is_none()); + cache.insert(11, Arc::new(buf()), Arc::new(files.clone())); + let written: Vec = store.take_writes().into_iter().map(|w| w.0).collect(); + assert_eq!(written, [layer_disk_key(11)], "only the buffer whose files are all named"); + } + + /// A layer buffer whose render started before Reload Footage dropped its file (#3184) is + /// not written to the disk cache when it is kept afterwards; one whose render started after + /// the drop is. + #[test] + fn a_buffer_rendered_across_a_drop_is_not_written() { + use crate::disk_cache::{DiskCache, Kind, layer_disk_key}; + use crate::reads::{FileStamp, ReadFile, ReadLog}; + let dir = tempfile::tempdir().unwrap(); + let dc = DiskCache::open(dir.path(), 1 << 30).unwrap(); + dc.set_min_layer_ms(0); + let cache = LayerCache::default(); + cache.set_disk(Some(dc.clone())); + let buf = || Arc::new(Buf { img: Image::new(2, 2), offset: [0.0; 2], scale: 1.0 }); + // A render's files, read after it took the drop generation (`Renderer::recorded`). + let render = || { + let log = ReadLog::starting_at(cache.drop_generation().unwrap()); + move || { + log.note(ReadFile::new("/a.png", FileStamp::Native { len: 3, modified: Some(1) })); + Arc::new(log.into_files()) + } + }; + // The miss starts the render; the file is reloaded before the buffer is kept. + assert!(cache.get(1).is_none()); + let a = render(); + dc.drop_files(["/a.png"]); + dc.flush(); + // Another render of the same key starts after the drop, while the first one still runs + // (its miss must not lend the first render its generation). + assert!(cache.get(1).is_none()); + let b = render(); + let (stale, _) = cache.insert(1, buf(), a()); + dc.flush(); + assert!(!dc.contains(Kind::Layer, layer_disk_key(1)), "made of the file as it was before the drop"); + // Not kept in memory either: the second render keeps its own buffer, and writes it. + let fresh = buf(); + let (kept, _) = cache.insert(1, fresh.clone(), b()); + assert!(Arc::ptr_eq(&kept, &fresh) && !Arc::ptr_eq(&kept, &stale)); + // A render of another key after the drop. + assert!(cache.get(2).is_none()); + cache.insert(2, buf(), render()()); + dc.flush(); + assert!(dc.contains(Kind::Layer, layer_disk_key(1)), "the render after the drop"); + assert!(dc.contains(Kind::Layer, layer_disk_key(2))); + // A list that doesn't say when its reads began counts as made before the drop. + assert!(cache.get(3).is_none()); + cache.insert(3, buf(), Arc::new([ReadFile::new("/a.png", FileStamp::Native { len: 3, modified: Some(1) })].into_iter().collect())); + dc.flush(); + assert!(!dc.contains(Kind::Layer, layer_disk_key(3))); + drop((cache, dc)); + } + + /// A disk cache whose layer writes stop at a barrier until the test lets them go on. + struct PausedWrites { + dc: Arc, + arrived: std::sync::Barrier, + go_on: std::sync::Barrier, + } + + impl super::LayerStore for PausedWrites { + fn get_layer(&self, key: u128, virtual_id: Option>) -> Option<(Arc, crate::reads::ReadFiles)> { + super::LayerStore::get_layer(&*self.dc, key, virtual_id) + } + fn put_layer(&self, key: u128, buf: &Buf, files: &crate::disk_cache::EntryFiles, since: u64) { + self.arrived.wait(); + self.go_on.wait(); + super::LayerStore::put_layer(&*self.dc, key, buf, files, since); + } + fn drop_generation(&self) -> u64 { + self.dc.drop_generation() + } + fn dropped_since(&self, files: &crate::reads::ReadFiles, since: u64) -> bool { + self.dc.dropped_since(files, since) + } + } + + /// Reload Footage while a render from before it is writing its buffer: the cache is cleared + /// and the file dropped after the buffer passed its check, before it is kept. The buffer + /// then reaches memory after the purge, but it is never served, and a render after the drop + /// keeps its own buffer (#3191 review). + #[test] + fn a_buffer_kept_after_the_purge_of_a_reload_is_not_served() { + use crate::reads::{FileStamp, ReadFile, ReadLog}; + let dir = tempfile::tempdir().unwrap(); + let dc = crate::disk_cache::DiskCache::open(dir.path(), 1 << 30).unwrap(); + let store = Arc::new(PausedWrites { dc: dc.clone(), arrived: std::sync::Barrier::new(2), go_on: std::sync::Barrier::new(2) }); + let cache = Arc::new(LayerCache::default()); + cache.set_store(Some(store.clone())); + let buf = || Arc::new(Buf { img: Image::new(2, 2), offset: [0.0; 2], scale: 1.0 }); + let files = |cache: &LayerCache| { + let log = ReadLog::starting_at(cache.drop_generation().unwrap()); + log.note(ReadFile::new("/a.png", FileStamp::Native { len: 3, modified: Some(1) })); + Arc::new(log.into_files()) + }; + // The old render: a miss, its reads, then its insert, which stops in the disk write. + assert!(cache.get(1).is_none()); + let old_files = files(&cache); + let old = buf(); + let writer = { + let (cache, old) = (cache.clone(), old.clone()); + std::thread::spawn(move || cache.insert(1, old, old_files)) + }; + store.arrived.wait(); + // Reload Footage meanwhile: the layer cache is purged and the file dropped. + cache.clear(); + dc.drop_files(["/a.png"]); + dc.flush(); + store.go_on.wait(); + let _ = writer.join().unwrap(); + dc.flush(); + assert!(!dc.contains(crate::disk_cache::Kind::Layer, crate::disk_cache::layer_disk_key(1)), "not on disk"); + assert!(cache.get(1).is_none(), "the old pixels are not served from memory"); + // A render after the drop keeps its own buffer. + let fresh = buf(); + let new_files = files(&cache); + let mut kept = None; + std::thread::scope(|s| { + s.spawn(|| { + store.arrived.wait(); + store.go_on.wait(); + }); + kept = Some(cache.insert(1, fresh.clone(), new_files).0); + }); + let kept = kept.unwrap(); + assert!(Arc::ptr_eq(&kept, &fresh) && !Arc::ptr_eq(&kept, &old)); + assert!(Arc::ptr_eq(&cache.get(1).unwrap().0, &fresh)); + } + + /// A render that gets another render's buffer gets that buffer's files too (#3184): when a + /// file changed between the two reads, its frame lists the file with both identities, so the + /// list is incomplete and the frame is not kept on disk with the new identity and the old + /// pixels. + #[test] + fn the_buffer_kept_first_brings_its_files() { + use crate::reads::{FileStamp, ReadFile, ReadFiles, ReadLog}; + use crate::{NoFootage, RenderOpts, Renderer}; + let cache = LayerCache::default(); + let buf = || Arc::new(Buf { img: Image::new(4, 4), offset: [0.0; 2], scale: 1.0 }); + let files = |len| Arc::new([ReadFile::new("/a.png", FileStamp::Native { len, modified: Some(1) })].into_iter().collect::()); + let (first, second) = (files(1), files(2)); + let a = buf(); + cache.insert(3, a.clone(), first.clone()); + let project = effectcraft_project::Project::default(); + let log = ReadLog::new(); + let mut r = Renderer::new(&project, &NoFootage, RenderOpts::default()); + r.cache = Some(&cache); + r.reads = Some(&log); + // (what `Renderer::recorded` noted for the second render) + log.note_all(&second); + let (kept, kept_files) = r.keep(Some(3), buf(), &second); + assert!(Arc::ptr_eq(&kept, &a)); + assert_eq!(kept_files, first); + assert!(!log.files().is_complete()); + // The same files: nothing changes. + let same = ReadLog::new(); + r.reads = Some(&same); + same.note_all(&first); + let _ = r.keep(Some(3), buf(), &files(1)); + assert!(same.files().is_complete()); } } diff --git a/crates/render/src/cache/dependencies.rs b/crates/render/src/cache/dependencies.rs index db1465c07..dc4216098 100644 --- a/crates/render/src/cache/dependencies.rs +++ b/crates/render/src/cache/dependencies.rs @@ -156,7 +156,7 @@ impl Dependencies<'_> { layer.layer_time(ctx.time).0.hash(self.h); } match layer.source { - LayerSource::Solid { item } | LayerSource::Footage { item } | LayerSource::Comp { item } => { + LayerSource::Solid { item } | LayerSource::Footage { item } | LayerSource::Comp { item } | LayerSource::Model { item } => { let item = match layer.source { LayerSource::Footage { .. } => ctx.media_item(layer, item), _ => item, diff --git a/crates/render/src/disk_cache.rs b/crates/render/src/disk_cache.rs index 427198ad2..f509a5896 100644 --- a/crates/render/src/disk_cache.rs +++ b/crates/render/src/disk_cache.rs @@ -2,15 +2,25 @@ //! and layer cache entries kept on disk between sessions, like After Effects' disk cache. //! //! * Entries are files keyed by a 128-bit content hash (`/v1//<2 hex>/<32 hex>.ecc`): -//! frames by the hash of the composition's content (the comp and everything it uses, footage -//! files' size and modification time included) plus frame, scale and view; layer buffers by -//! the layer cache's content key salted with the project's footage fingerprint. A changed -//! project therefore never reads stale pixels: its keys simply change. +//! frames by the hash of the composition's content as it is in memory (the comp and what it can +//! draw, footage items with their settings and paths, and what its expressions can read; no +//! file is looked at, [`comp_content_key`]) plus frame, scale and view; layer buffers by the +//! layer cache's content key (which names the footage items a buffer uses with their settings +//! and paths) and the renderer version ([`layer_disk_key`]). A changed project therefore never +//! reads stale pixels: its keys simply change, and an edit elsewhere keeps them. +//! * Frame and layer entries keep the source files they were made of (footage, and the font +//! files text is drawn with), each with the size and modification time it was read with +//! ([`EntryFiles`], #3184). An entry is used only while those files are unchanged: a handful +//! of `metadata` calls when it is read, never a scan of the project's files. A frame or +//! buffer whose files can't all be named is not stored (text in a family that isn't installed +//! or with fallback glyphs, footage refused by automation read roots, …). File ▸ Reload +//! Footage deletes the entries made of the reloaded files ([`DiskCache::drop_files`]): a file +//! replaced by one of the same size and time is read again. //! * Payloads are LZ4-compressed (`lz4_flex`, MIT): frames as 8-bit premultiplied RGBA (or, for //! 32 bpc viewer frames, half-float RGBA with the bytes shuffled into planes; the entry -//! header's format byte tells them apart, 8-bit entries keep format 0), layer -//! buffers as `f32` RGBA with the bytes shuffled into planes (exact, so cached and fresh -//! renders are bit-identical), Roto Brush / Object Matte segmentations as +//! header's format byte tells them apart: 2 and 3, both with their source files first), layer +//! buffers (format 4, their source files first) as `f32` RGBA with the bytes shuffled into +//! planes (exact, so cached and fresh renders are bit-identical), Roto Brush / Object Matte segmentations as //! `FrameSeg::to_bytes` (keyed by their content-keyed chain key and scale, so they are //! reused whenever the same frames, settings and prompt come back, also after a restart). Every file ends in a checksum; damaged files read as misses and //! are deleted. @@ -32,6 +42,11 @@ use std::sync::{Arc, Condvar, Mutex}; use effectcraft_effects::Buf; use effectcraft_raster::Image; +use crate::reads::ReadFiles; + +mod entry_files; +pub use entry_files::{EntryFile, EntryFiles, EntryStamp}; + /// File format version (part of the folder layout). const VERSION: &str = "v1"; const MAGIC: &[u8; 4] = b"ECDC"; @@ -87,9 +102,16 @@ pub struct FrameF16 { pub rgba: Vec, } -/// The entry header's format byte of frame entries. +/// The entry header's format byte: segmentations (and 8-bit frames and layer buffers before +/// #3184). const FORMAT_RGBA8: u8 = 0; -const FORMAT_F16: u8 = 1; +/// Frame entries: their source files ([`EntryFiles`]), then 8-bit or half-float pixels. The +/// formats before them (0: 8-bit, 1: half float, without files) read as misses and are deleted. +const FORMAT_RGBA8_FILES: u8 = 2; +const FORMAT_F16_FILES: u8 = 3; +/// Layer entries: their source files ([`EntryFiles`]), then the buffer. The format before it +/// (0, without files) reads as a miss and is deleted. +const FORMAT_LAYER_FILES: u8 = 4; /// Counters for tests, the Info panel and `cache.diskStats`. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -220,7 +242,7 @@ pub struct LayerPrefetch { const PREFETCH_RECENT: usize = 512; impl LayerPrefetch { - /// A frame's layer-cache misses (salted keys). + /// A frame's layer-cache misses (disk keys, [`layer_disk_key`]). pub fn note_misses(&mut self, keys: impl IntoIterator) { for k in keys { self.recent.retain(|x| *x != k); @@ -243,6 +265,8 @@ impl LayerPrefetch { enum Job { /// Kind, key, header format byte, payload. Write(Kind, u128, u8, Vec), + /// Delete the frame and layer entries made of these files ([`DiskCache::drop_files`]). + Drop(Vec>), } struct Queue { @@ -255,14 +279,55 @@ struct Queue { busy: bool, /// Keys of frames queued or being written. pending_frames: std::collections::HashSet, + /// The drops so far (under the queue's lock: a write is queued either before a drop, which + /// then sweeps it, or after it, when this refuses it if it was made from before). + drops: Drops, +} + +/// Files dropped from the cache files beyond this are not remembered one by one: everything made +/// before the drop that forgot them is refused instead. +const MAX_DROPPED_PATHS: usize = 1 << 16; + +/// The [`DiskCache::drop_files`] calls so far: their number (the drop generation) and the last one +/// of each file. An entry made of a file read before that file's last drop (its render started at +/// an earlier generation) holds the pixels from before and is refused. +#[derive(Default)] +struct Drops { + generation: u64, + last: HashMap, u64>, + /// Everything made before this generation is refused (the per-file record was forgotten). + floor: u64, +} + +impl Drops { + /// Notes that the given paths were dropped, bumping the generation and tracking them for later checks. + fn record(&mut self, paths: &[Arc]) { + self.generation = self.generation.saturating_add(1); + if self.last.len().saturating_add(paths.len()) > MAX_DROPPED_PATHS { + self.last.clear(); + self.floor = self.generation; + return; + } + for p in paths { + self.last.insert(p.clone(), self.generation); + } + } + + /// Whether something made of `paths`, whose reads started at generation `since`, read one of + /// them before it was dropped. + fn stale<'a>(&self, mut paths: impl Iterator>, since: u64) -> bool { + since < self.floor || paths.any(|p| self.last.get(p).is_some_and(|g| *g > since)) + } } impl Queue { + /// Takes the next queued job, frames first, and releases the bytes it held. #[cfg(not(target_arch = "wasm32"))] fn pop(&mut self) -> Option { if let Some(j) = self.frames.pop_front() { - let Job::Write(_, _, _, p) = &j; - self.frame_bytes = self.frame_bytes.saturating_sub(p.len()); + if let Job::Write(_, _, _, p) = &j { + self.frame_bytes = self.frame_bytes.saturating_sub(p.len()); + } return Some(j); } self.jobs.pop_front() @@ -280,16 +345,25 @@ pub struct DiskCache { disk_cap: AtomicU64, /// Free space is limiting the cache (warned once per episode). space_limited: AtomicBool, + /// The free space the cache leaves on its disk ([`disk_reserve`]); 0 = unknown. + reserve: AtomicU64, hits: AtomicU64, misses: AtomicU64, writes: AtomicU64, evictions: AtomicU64, dropped: AtomicU64, failed: AtomicU64, + /// Source files looked at to check frame and layer entries ([`DiskCache::file_checks`]). + file_checks: AtomicU64, + /// Frame and layer entries compressed for a queued write ([`DiskCache::encodes`]). + encodes: AtomicU64, tmp_counter: AtomicU64, queue: Arc<(Mutex, Condvar)>, /// Layer buffers that took less than this to render are not written (ms). min_layer_ms: AtomicU64, + /// Files whose entries are being deleted ([`DiskCache::drop_files`]), each with the number + /// of drops of it pending: until then an entry made of one is a miss. + dropping: Mutex, u32>>, } /// FNV-1a 128-bit (content keys). @@ -326,6 +400,23 @@ fn checksum(data: &[u8]) -> u64 { h } +/// The least free space the disk cache leaves on its disk. +pub const MIN_DISK_RESERVE: u64 = 2 << 30; +/// The most free space the disk cache leaves on its disk: 5% of a large disk is far more than +/// the system needs to keep working (on a 4 TB disk it held a 20 GB cache at 0 with 190 GB free). +pub const MAX_DISK_RESERVE: u64 = 10 << 30; +/// Below this much room left by free disk space, frame and layer writes are refused before they +/// are encoded ([`DiskCache::accepts_writes`]): a few frames would fill it, and the rest would be +/// compressed only to be refused. +const MIN_WRITE_ROOM: u64 = 64 << 20; + +/// The free space the disk cache leaves on a disk of `total` bytes: 5% of it, at least 2 GB and +/// at most 10 GB. It keeps the cache from filling the disk; the cache's size is the user's +/// Maximum Disk Cache Size (After Effects has that maximum and no reserve). +pub fn disk_reserve(total: u64) -> u64 { + (total / 20).clamp(MIN_DISK_RESERVE, MAX_DISK_RESERVE) +} + /// The platform's default disk cache folder. pub fn default_folder() -> PathBuf { let home = std::env::var_os("HOME").map(PathBuf::from); @@ -356,12 +447,15 @@ impl DiskCache { max_bytes: AtomicU64::new(max_bytes), disk_cap: AtomicU64::new(u64::MAX), space_limited: AtomicBool::new(false), + reserve: AtomicU64::new(0), hits: AtomicU64::new(0), misses: AtomicU64::new(0), writes: AtomicU64::new(0), evictions: AtomicU64::new(0), dropped: AtomicU64::new(0), failed: AtomicU64::new(0), + file_checks: AtomicU64::new(0), + encodes: AtomicU64::new(0), tmp_counter: AtomicU64::new(0), queue: Arc::new(( Mutex::new(Queue { @@ -370,10 +464,12 @@ impl DiskCache { jobs: Default::default(), busy: cfg!(not(target_arch = "wasm32")), pending_frames: Default::default(), + drops: Drops::default(), }), Condvar::new(), )), min_layer_ms: AtomicU64::new(30), + dropping: Mutex::new(HashMap::new()), }); dc.rescan(); // Shrinking a large cache deletes many files: the writer thread does it. @@ -446,19 +542,22 @@ impl DiskCache { self.max_bytes().min(self.disk_cap.load(Ordering::Relaxed)) } - /// The cache's disk: `(total, available)` bytes, `None` = unknown. The cache leaves at least - /// 2 GB or 5% of the disk free, whichever is more (evicting when it must). + /// The cache's disk: `(total, available)` bytes, `None` = unknown. The cache leaves + /// [`disk_reserve`] free (5% of the disk, at least 2 GB and at most 10 GB), evicting when it + /// must. pub fn set_disk_space(&self, space: Option<(u64, u64)>) { + let mut reserve = 0; let cap = match space { // (a disk reported as 0 bytes, such as an automount map, is unknown) Some((total, available)) if total > 0 => { - let reserve = (2u64 << 30).max(total / 20); + reserve = disk_reserve(total); let used = self.index.lock().map(|g| g.total()).unwrap_or_default(); // The cache's own files count as free: below the reserve it gives space back. used.saturating_add(available).saturating_sub(reserve) } _ => u64::MAX, }; + self.reserve.store(reserve, Ordering::Relaxed); self.disk_cap.store(cap, Ordering::Relaxed); let limited = cap < self.max_bytes(); if self.space_limited.swap(limited, Ordering::Relaxed) != limited && limited { @@ -467,6 +566,33 @@ impl DiskCache { self.evict(); } + /// Whether frame and layer writes are worth encoding now: `false` while free disk space + /// holds the limit at 0 or under 64 MB (a limit the setting asks for is always used). Two + /// atomic reads, so writes check it before they spend time converting and compressing + /// pixels that [`DiskCache::write_format`] would refuse anyway. (Whether a given entry fits + /// is known only once it is compressed: `write_format` decides.) + pub fn accepts_writes(&self) -> bool { + let (max, limit) = (self.max_bytes(), self.limit_bytes()); + limit > 0 && (limit >= MIN_WRITE_ROOM || limit == max) + } + + /// The free space the cache leaves on its disk ([`disk_reserve`]), once the disk's size is + /// known ([`DiskCache::set_disk_space`]). + pub fn reserve_bytes(&self) -> Option { + Some(self.reserve.load(Ordering::Relaxed)).filter(|r| *r > 0) + } + + /// Frame and layer entries compressed for writing so far (tests: a write refused up front + /// costs no encode). + pub fn encodes(&self) -> u64 { + self.encodes.load(Ordering::Relaxed) + } + + /// Count one entry compressed for a queued write. + fn count_encode(&self) { + self.encodes.fetch_add(1, Ordering::Relaxed); + } + /// Layer buffers faster to render than this are not worth a disk write. pub fn set_min_layer_ms(&self, ms: u64) { self.min_layer_ms.store(ms, Ordering::Relaxed); @@ -541,36 +667,94 @@ impl DiskCache { /// An entry's payload and its header's format byte. fn read_entry(&self, kind: Kind, key: u128) -> Option<(u8, Vec)> { + let entry = self.load(kind, key); + match &entry { + Some(_) => self.hit(kind, key), + None => _ = self.misses.fetch_add(1, Ordering::Relaxed), + } + entry + } + + /// An entry's payload and format byte, without counting it as a hit or miss. A damaged file + /// is deleted; a missing one forgotten. + fn load(&self, kind: Kind, key: u128) -> Option<(u8, Vec)> { if !self.contains(kind, key) { - self.misses.fetch_add(1, Ordering::Relaxed); return None; } let path = self.path(kind, key); let data = std::fs::read(&path).ok(); - let ok = data.as_deref().and_then(|d| verify(d, kind).map(|p| (d[5], p))); - match ok { - Some((format, payload)) => { - let payload = (format, payload.to_vec()); - self.hits.fetch_add(1, Ordering::Relaxed); - if let Ok(mut g) = self.index.lock() { - g.touch(kind, key); - } - // Persist the recency for the next session (best effort). - if let Ok(f) = std::fs::File::options().append(true).open(&path) { - let _ = f.set_modified(std::time::SystemTime::now()); - } - Some(payload) - } - None => { - self.misses.fetch_add(1, Ordering::Relaxed); - // Gone (another process evicted it) or damaged: forget it. - if data.is_some() { - let _ = std::fs::remove_file(&path); - } - self.forget(kind, key); - None + let ok = data.as_deref().and_then(|d| Some((*d.get(5)?, verify(d, kind)?.to_vec()))); + if ok.is_none() { + // Gone (another process evicted it) or damaged: forget it. + if data.is_some() { + let _ = std::fs::remove_file(&path); } + self.forget(kind, key); } + ok + } + + /// Count a hit: the entry becomes the most recently used, also for the next session. + fn hit(&self, kind: Kind, key: u128) { + self.hits.fetch_add(1, Ordering::Relaxed); + if let Ok(mut g) = self.index.lock() { + g.touch(kind, key); + } + // Persist the recency for the next session (best effort). + if let Ok(f) = std::fs::File::options().append(true).open(self.path(kind, key)) { + let _ = f.set_modified(std::time::SystemTime::now()); + } + } + + /// An entry of `kind` and `format` whose source files are unchanged, decoded by `decode`, + /// with those files. An entry whose files changed, of the format before #3184, or damaged, + /// is a miss and is deleted (it renders again and is stored anew). A frame entry of the + /// other current frame format (8-bit and half-float frames have keys of their own) is a miss + /// and stays. + fn get_checked( + &self, + kind: Kind, + key: u128, + format: u8, + virtual_id: Option>, + decode: impl FnOnce(&[u8]) -> Option, + ) -> Option<(T, ReadFiles)> { + let miss = || { + self.misses.fetch_add(1, Ordering::Relaxed); + None + }; + // (before the file is read: the writer may finish a drop's sweep between the read and the + // check below, and a drop may start meanwhile) + let drops = self.drops_now(); + let Some((found, payload)) = self.load(kind, key) else { return miss() }; + if found != format { + let other_frame_format = kind == Kind::Frame && (found == FORMAT_RGBA8_FILES || found == FORMAT_F16_FILES); + if !other_frame_format { + self.discard(kind, key); + } + return miss(); + } + let Some((files, body)) = EntryFiles::decode(&payload) else { + self.discard(kind, key); + return miss(); + }; + self.file_checks.fetch_add(files.len() as u64, Ordering::Relaxed); + if self.made_of_dropped(&files, &drops) || !files.unchanged(virtual_id) { + self.discard(kind, key); + return miss(); + } + let Some(value) = decode(body) else { + self.discard(kind, key); + return miss(); + }; + self.hit(kind, key); + Some((value, files.to_read_files())) + } + + /// Source files looked at so far to check frame and layer entries before using them (one per + /// file a read entry lists; tests). + pub fn file_checks(&self) -> u64 { + self.file_checks.load(Ordering::Relaxed) } /// A valid file whose payload does not decode: delete it, as a damaged one. @@ -587,13 +771,19 @@ impl DiskCache { /// Queue a write; `false` when it was dropped (a full queue). fn enqueue(&self, kind: Kind, key: u128, payload: Vec) -> bool { - self.enqueue_format(kind, key, FORMAT_RGBA8, payload) + self.enqueue_format(kind, key, FORMAT_RGBA8, payload, None) } - /// [`DiskCache::enqueue`] with the entry's header format byte. - fn enqueue_format(&self, kind: Kind, key: u128, format: u8, payload: Vec) -> bool { + /// [`DiskCache::enqueue`] with the entry's header format byte. `made`: the source files of a + /// frame or layer entry and the drop generation its reads started at + /// ([`DiskCache::drop_generation`]); refused (`false`) when one of them was dropped since. + fn enqueue_format(&self, kind: Kind, key: u128, format: u8, payload: Vec, made: Option<(&EntryFiles, u64)>) -> bool { + let stale = |q: &Queue| made.is_some_and(|(files, since)| q.drops.stale(files.files().iter().map(|f| &f.path), since)); #[cfg(target_arch = "wasm32")] { + if self.queue.0.lock().is_ok_and(|q| stale(&q)) { + return false; + } self.write_format(kind, key, format, &payload); true } @@ -601,6 +791,9 @@ impl DiskCache { { let (m, cv) = &*self.queue; let Ok(mut q) = m.lock() else { return false }; + if stale(&q) { + return false; + } let full = match kind { Kind::Frame => q.frame_bytes.saturating_add(payload.len()) > FRAME_QUEUE_BYTES, _ => q.jobs.len() >= QUEUE_LIMIT, @@ -622,10 +815,6 @@ impl DiskCache { } /// Write an entry now (atomic rename), then evict down to the limit. - fn write_now(&self, kind: Kind, key: u128, payload: &[u8]) { - self.write_format(kind, key, FORMAT_RGBA8, payload); - } - fn write_format(&self, kind: Kind, key: u128, format: u8, payload: &[u8]) { if self.contains(kind, key) { return; @@ -680,70 +869,229 @@ impl DiskCache { } } + /// Delete the frame and layer entries made of any of `paths` (File ▸ Reload Footage: a file + /// replaced by one with the same size and modification time must be read again, which the + /// check on hit can't tell). The index knows keys, not files, so the writer thread reads the + /// files section at the start of each frame and layer entry (no pixels) and deletes the + /// entries that list one of `paths`, after the writes queued before (those are swept too); + /// until it is done, an entry made of one of them is a miss. Entries are deleted for good, + /// so they don't come back after a restart. Nothing else in the cache is touched. + /// + /// A render that was under way meanwhile may still offer an entry made of one of `paths` as + /// it was before: it is refused, see [`DiskCache::drop_generation`]. + pub fn drop_files(&self, paths: impl IntoIterator>>) { + let paths: Vec> = paths.into_iter().map(Into::into).collect(); + if paths.is_empty() { + return; + } + { + let mut d = self.dropping.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + for p in &paths { + let n = d.entry(p.clone()).or_default(); + *n = n.saturating_add(1); + } + } + #[cfg_attr(target_arch = "wasm32", allow(unused_variables))] + let (m, cv) = &*self.queue; + match m.lock() { + Ok(mut q) => { + q.drops.record(&paths); + #[cfg(not(target_arch = "wasm32"))] + { + // (never refused for a full queue) + q.jobs.push_back(Job::Drop(paths)); + cv.notify_all(); + return; + } + } + // (no writer thread to take the job any more) + Err(poisoned) => poisoned.into_inner().drops.record(&paths), + } + self.sweep(&paths); + } + + /// The number of [`DiskCache::drop_files`] calls so far. A render takes it before it starts + /// reading and hands it to `put_frame` / `put_layer` with what it made: an entry listing a + /// file dropped after that is refused (the render may have read the file as it was before). + pub fn drop_generation(&self) -> u64 { + self.queue.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner).drops.generation + } + + /// Whether one of `files` was dropped after generation `since` ([`DiskCache::drop_generation`]), + /// or after the earlier generation the list's own reads started at ([`ReadFiles::since`]: a + /// buffer made by an earlier render, from memory): what was made of them then is from before + /// the drop (shown, never stored). + pub fn dropped_since(&self, files: &ReadFiles, since: u64) -> bool { + let since = files.since().map_or(since, |s| s.min(since)); + self.queue.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner).drops.stale(files.files().iter().map(|f| &f.path), since) + } + + /// Whether a [`DiskCache::drop_files`] is pending. + pub fn is_dropping(&self) -> bool { + !self.dropping.lock().unwrap_or_else(std::sync::PoisonError::into_inner).is_empty() + } + + /// The drop generation and the files being dropped now, taken before an entry is read + /// (see [`DiskCache::made_of_dropped`]). + fn drops_now(&self) -> (u64, Vec>) { + let generation = self.drop_generation(); + let d = self.dropping.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + (generation, d.keys().cloned().collect()) + } + + /// An entry read after [`DiskCache::drops_now`] gave `drops` that is made of a file whose + /// drop was pending then (its sweep may have ended since, after the entry was read) or that + /// was dropped since. + fn made_of_dropped(&self, files: &EntryFiles, drops: &(u64, Vec>)) -> bool { + let (since, pending) = drops; + let paths = || files.files().iter().map(|f| &f.path); + paths().any(|p| pending.contains(p)) || self.queue.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner).drops.stale(paths(), *since) + } + + /// The writer's part of [`DiskCache::drop_files`]: delete the entries made of `paths`, then + /// end their drop. + fn sweep(&self, paths: &[Arc]) { + let set: std::collections::HashSet<&str> = paths.iter().map(|p| &**p).collect(); + for kind in [Kind::Frame, Kind::Layer] { + for key in self.keys(kind) { + if self.entry_files(kind, key).is_some_and(|f| f.files().iter().any(|f| set.contains(&*f.path))) { + self.discard(kind, key); + } + } + } + let mut d = self.dropping.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + for p in paths { + if let Some(n) = d.get_mut(p) { + *n = n.saturating_sub(1); + if *n == 0 { + d.remove(p); + } + } + } + } + + /// The source files a frame or layer entry lists, read from the start of its file (the + /// whole file only when the list is longer than that). `None` when it has none to read (an + /// earlier format, gone, damaged): such an entry is never used anyway. + fn entry_files(&self, kind: Kind, key: u128) -> Option { + use std::io::Read; + const HEAD: u64 = 64 << 10; + let parse = |d: &[u8]| { + let format = *d.get(5)?; + let ours = d.get(..4)? == MAGIC && *d.get(4)? == kind.tag(); + if !ours || !matches!(format, FORMAT_RGBA8_FILES | FORMAT_F16_FILES | FORMAT_LAYER_FILES) { + return None; + } + EntryFiles::decode(d.get(8..)?).map(|(files, _)| files) + }; + let path = self.path(kind, key); + let mut head = Vec::new(); + std::fs::File::open(&path).ok()?.take(HEAD).read_to_end(&mut head).ok()?; + parse(&head).or_else(|| if head.len() as u64 == HEAD { parse(&std::fs::read(&path).ok()?) } else { None }) + } + // ------------------------------------------------------------ typed entries - /// An 8-bit frame (`None` for a half-float entry of that key, which [`DiskCache::get_frame_f16`] - /// reads). - pub fn get_frame(&self, key: u128) -> Option { - let (format, p) = self.read_entry(Kind::Frame, key)?; - if format != FORMAT_RGBA8 { - return None; - } - decode_frame(&p) + /// An 8-bit frame whose source files are unchanged (see [`DiskCache::get_checked`]), with + /// those files; `None` for a half-float entry of that key, which + /// [`DiskCache::get_frame_f16`] reads. `virtual_id` names files served from memory. + pub fn get_frame(&self, key: u128, virtual_id: Option>) -> Option<(Frame8, ReadFiles)> { + self.get_checked(Kind::Frame, key, FORMAT_RGBA8_FILES, virtual_id, decode_frame) } - /// A half-float frame (32 bpc viewer frames). A damaged one is deleted. - pub fn get_frame_f16(&self, key: u128) -> Option { - let (format, p) = self.read_entry(Kind::Frame, key)?; - if format != FORMAT_F16 { - return None; - } - let f = decode_frame_f16(&p); - if f.is_none() { - self.discard(Kind::Frame, key); - } - f + /// A half-float frame (32 bpc viewer frames), as [`DiskCache::get_frame`]. + pub fn get_frame_f16(&self, key: u128, virtual_id: Option>) -> Option<(FrameF16, ReadFiles)> { + self.get_checked(Kind::Frame, key, FORMAT_F16_FILES, virtual_id, decode_frame_f16) } - /// Queue a half-float frame for writing (`width * height * 4` binary16 values). `true` when - /// it is on disk or queued; `false` when refused (wrong size, a full queue). - pub fn put_frame_f16(&self, key: u128, width: u32, height: u32, rgba: &[u16]) -> bool { + /// Queue a half-float frame for writing (`width * height * 4` binary16 values) with the + /// source files it was made of. `true` when it is on disk or queued; `false` when refused + /// (wrong size, a full queue, no room on the disk, files that can't be checked later: see + /// [`EntryFiles::of`]; a file dropped after `since`, the [`DiskCache::drop_generation`] the + /// render started at). + #[allow(clippy::too_many_arguments)] + pub fn put_frame_f16( + &self, + key: u128, + width: u32, + height: u32, + rgba: &[u16], + files: &ReadFiles, + virtual_id: Option>, + since: u64, + ) -> bool { if rgba.len() as u64 != width as u64 * height as u64 * 4 { return false; } - self.contains(Kind::Frame, key) || self.enqueue_format(Kind::Frame, key, FORMAT_F16, encode_frame_f16(width, height, rgba)) - } - - /// Write a half-float frame synchronously (tests). - pub fn put_frame_f16_now(&self, key: u128, width: u32, height: u32, rgba: &[u16]) { - self.write_format(Kind::Frame, key, FORMAT_F16, &encode_frame_f16(width, height, rgba)); - } - - /// Queue a frame for writing (8-bit premultiplied RGBA, `width * height * 4` bytes). `true` - /// when it is on disk or queued; `false` when refused (wrong size, a full queue). - pub fn put_frame(&self, key: u128, width: u32, height: u32, rgba: &[u8]) -> bool { - if rgba.len() != width as usize * height as usize * 4 { + let Some(files) = EntryFiles::of(files, virtual_id) else { return false }; + if self.contains(Kind::Frame, key) { + return true; + } + // (no room on the disk: refused before the pixels are compressed) + if !self.accepts_writes() { return false; } - self.contains(Kind::Frame, key) || self.enqueue(Kind::Frame, key, encode_frame(width, height, rgba)) + self.count_encode(); + let payload = with_files(&files, encode_frame_f16(width, height, rgba)); + self.enqueue_format(Kind::Frame, key, FORMAT_F16_FILES, payload, Some((&files, since))) } - pub fn get_layer(&self, key: u128) -> Option { - let p = self.read(Kind::Layer, key)?; - decode_layer(&p) + /// Write a half-float frame synchronously (tests); a list that can't be stored is not. + pub fn put_frame_f16_now(&self, key: u128, width: u32, height: u32, rgba: &[u16], files: &ReadFiles) { + if let Some(files) = EntryFiles::of(files, None) { + self.write_format(Kind::Frame, key, FORMAT_F16_FILES, &with_files(&files, encode_frame_f16(width, height, rgba))); + } } - /// Queue a layer buffer for writing. - pub fn put_layer(&self, key: u128, buf: &Buf) { - if self.contains(Kind::Layer, key) { + /// Queue a frame for writing (8-bit premultiplied RGBA, `width * height * 4` bytes) with the + /// source files it was made of. `true` when it is on disk or queued; `false` when refused + /// (wrong size, a full queue, no room on the disk, files that can't be checked later: see + /// [`EntryFiles::of`]; a file dropped after `since`, the [`DiskCache::drop_generation`] the + /// render started at). + #[allow(clippy::too_many_arguments)] + pub fn put_frame(&self, key: u128, width: u32, height: u32, rgba: &[u8], files: &ReadFiles, virtual_id: Option>, since: u64) -> bool { + if rgba.len() as u64 != width as u64 * height as u64 * 4 { + return false; + } + let Some(files) = EntryFiles::of(files, virtual_id) else { return false }; + if self.contains(Kind::Frame, key) { + return true; + } + // (no room on the disk: refused before the pixels are compressed) + if !self.accepts_writes() { + return false; + } + self.count_encode(); + let payload = with_files(&files, encode_frame(width, height, rgba)); + self.enqueue_format(Kind::Frame, key, FORMAT_RGBA8_FILES, payload, Some((&files, since))) + } + + /// A layer buffer (key: [`layer_disk_key`]) whose source files are unchanged, with those + /// files, as [`DiskCache::get_checked`]: an entry whose files changed, damaged, or of the + /// format before #3184 is a miss and is deleted, so the buffer renders again and replaces it. + /// `virtual_id` names files served from memory. + pub fn get_layer(&self, key: u128, virtual_id: Option>) -> Option<(Buf, ReadFiles)> { + self.get_checked(Kind::Layer, key, FORMAT_LAYER_FILES, virtual_id, decode_layer) + } + + /// Queue a layer buffer for writing with the source files it was made of (see + /// [`EntryFiles::of`]: a buffer whose files can't all be named must not be stored). Refused + /// when one of them was dropped after `since`, the [`DiskCache::drop_generation`] its render + /// started at. + pub fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64) { + // (no room on the disk: refused before the buffer is compressed) + if self.contains(Kind::Layer, key) || !self.accepts_writes() { return; } - let _ = self.enqueue(Kind::Layer, key, encode_layer(buf)); + self.count_encode(); + let _ = self.enqueue_format(Kind::Layer, key, FORMAT_LAYER_FILES, with_files(files, encode_layer(buf)), Some((files, since))); } - /// Write a layer buffer synchronously (tests). - pub fn put_layer_now(&self, key: u128, buf: &Buf) { - self.write_now(Kind::Layer, key, &encode_layer(buf)); + /// Write a layer buffer synchronously (tests); a list that can't be stored is not. + pub fn put_layer_now(&self, key: u128, buf: &Buf, files: &ReadFiles) { + if let Some(files) = EntryFiles::of(files, None) { + self.write_format(Kind::Layer, key, FORMAT_LAYER_FILES, &with_files(&files, encode_layer(buf))); + } } /// A segmentation (chain key, scale) written by [`DiskCache::put_seg`]. @@ -767,7 +1115,8 @@ impl DiskCache { /// has no links. `false` when `from` isn't cached, `to` already is, or the file system /// refused. (Entries don't contain their key, so the same file serves both.) pub fn link_frame(&self, from: u128, to: u128) -> bool { - if !self.contains(Kind::Frame, from) || self.contains(Kind::Frame, to) { + // (while files are being dropped, `from` may be made of one: it renders again instead) + if !self.contains(Kind::Frame, from) || self.contains(Kind::Frame, to) || self.is_dropping() { return false; } let (src, dst) = (self.path(Kind::Frame, from), self.path(Kind::Frame, to)); @@ -794,12 +1143,23 @@ impl DiskCache { true } - /// Write a frame synchronously (tests). - pub fn put_frame_now(&self, key: u128, width: u32, height: u32, rgba: &[u8]) { - self.write_now(Kind::Frame, key, &encode_frame(width, height, rgba)); + /// Write a frame synchronously (tests); a list that can't be stored is not. + pub fn put_frame_now(&self, key: u128, width: u32, height: u32, rgba: &[u8], files: &ReadFiles) { + if let Some(files) = EntryFiles::of(files, None) { + self.write_format(Kind::Frame, key, FORMAT_RGBA8_FILES, &with_files(&files, encode_frame(width, height, rgba))); + } } } +/// A frame or layer entry's payload: its source files, then its pixels. +fn with_files(files: &EntryFiles, pixels: Vec) -> Vec { + let mut out = Vec::with_capacity(pixels.len() + 64); + files.encode(&mut out); + out.extend_from_slice(&pixels); + out +} + +/// The writer thread: evicts to the limit at open, then drains the job queue until the cache is dropped. #[cfg(not(target_arch = "wasm32"))] fn writer(dc: std::sync::Weak, q: Arc<(Mutex, Condvar)>) { let (m, cv) = &*q; @@ -830,12 +1190,20 @@ fn writer(dc: std::sync::Weak, q: Arc<(Mutex, Condvar)>) { } }; let Some(cache) = dc.upgrade() else { return }; - let Job::Write(kind, key, format, payload) = job; - cache.write_format(kind, key, format, &payload); + let written = match job { + Job::Write(kind, key, format, payload) => { + cache.write_format(kind, key, format, &payload); + (kind == Kind::Frame).then_some(key) + } + Job::Drop(paths) => { + cache.sweep(&paths); + None + } + }; drop(cache); if let Ok(mut g) = m.lock() { g.busy = false; - if kind == Kind::Frame { + if let Some(key) = written { g.pending_frames.remove(&key); } } @@ -843,12 +1211,9 @@ fn writer(dc: std::sync::Weak, q: Arc<(Mutex, Condvar)>) { } } -/// An entry file's bytes: header (magic, kind, format 0), payload, checksum. -pub fn seal(kind: Kind, payload: &[u8]) -> Vec { - seal_format(kind, FORMAT_RGBA8, payload) -} - -/// [`seal`] with the header's format byte (frames: 0 = 8-bit, 1 = half float). +/// An entry file's bytes: header (magic, kind, the format byte: [`FORMAT_RGBA8`] for +/// segmentations, [`FORMAT_RGBA8_FILES`] / [`FORMAT_F16_FILES`] for frames, +/// [`FORMAT_LAYER_FILES`] for layer buffers), payload, checksum. fn seal_format(kind: Kind, format: u8, payload: &[u8]) -> Vec { let mut data = Vec::with_capacity(payload.len() + 16); data.extend_from_slice(MAGIC); @@ -860,26 +1225,47 @@ fn seal_format(kind: Kind, format: u8, payload: &[u8]) -> Vec { data } -/// A sealed frame entry ([`seal`] of the encoded frame): what the browser's frame workers -/// write to the Origin Private File System. -pub fn frame_entry(width: u32, height: u32, rgba: &[u8]) -> Vec { - seal(Kind::Frame, &encode_frame(width, height, rgba)) +/// A sealed 8-bit frame entry with the source files it was made of: what the browser's frame +/// workers write to the Origin Private File System. `None` when the frame must not be stored +/// (its files can't be checked later, see [`EntryFiles::of`]; or a wrong size). +pub fn frame_entry(width: u32, height: u32, rgba: &[u8], files: &ReadFiles, virtual_id: Option>) -> Option> { + if rgba.len() as u64 != width as u64 * height as u64 * 4 { + return None; + } + let files = EntryFiles::of(files, virtual_id)?; + Some(seal_format(Kind::Frame, FORMAT_RGBA8_FILES, &with_files(&files, encode_frame(width, height, rgba)))) } -/// A frame from a sealed entry's bytes (`None` when damaged). -pub fn read_frame_entry(data: &[u8]) -> Option { - decode_frame(verify(data, Kind::Frame)?) +/// The frame of a sealed entry's bytes, if its source files are unchanged. `None` when damaged, +/// of another format, or made of files that changed since: a miss, to be rendered again. +pub fn read_frame_entry(data: &[u8], virtual_id: Option>) -> Option { + let payload = verify(data, Kind::Frame)?; + if data.get(5) != Some(&FORMAT_RGBA8_FILES) { + return None; + } + let (files, pixels) = EntryFiles::decode(payload)?; + if !files.unchanged(virtual_id) { + return None; + } + decode_frame(pixels) } -/// A sealed layer-buffer entry: what the browser's frame workers write to the Origin Private -/// File System (`layers/<32 hex>.ecc`). -pub fn layer_entry(buf: &Buf) -> Vec { - seal(Kind::Layer, &encode_layer(buf)) +/// A sealed layer-buffer entry with the source files it was made of: what the browser's frame +/// workers write to the Origin Private File System (`layers/<32 hex>.ecc`). +pub fn layer_entry(buf: &Buf, files: &EntryFiles) -> Vec { + seal_format(Kind::Layer, FORMAT_LAYER_FILES, &with_files(files, encode_layer(buf))) } -/// A layer buffer from a sealed entry's bytes (`None` when damaged). -pub fn read_layer_entry(data: &[u8]) -> Option { - decode_layer(verify(data, Kind::Layer)?) +/// A layer buffer and its source files from a sealed entry's bytes (`None` when damaged or of +/// another format). The files are not looked at here: the store checks them when the buffer is +/// used ([`EntryFiles::unchanged`]). +pub fn read_layer_entry(data: &[u8]) -> Option<(Buf, EntryFiles)> { + let payload = verify(data, Kind::Layer)?; + if data.get(5) != Some(&FORMAT_LAYER_FILES) { + return None; + } + let (files, body) = EntryFiles::decode(payload)?; + Some((decode_layer(body)?, files)) } /// The disk key of a segmentation: its chain key and scale. @@ -901,11 +1287,20 @@ impl effectcraft_effects::roto::SegStore for DiskCache { } impl crate::cache::LayerStore for DiskCache { - fn get_layer(&self, key: u128) -> Option> { - DiskCache::get_layer(self, key).map(Arc::new) + /// The layer for `key` from the disk cache, or `None` when it is missing or stale. + fn get_layer(&self, key: u128, virtual_id: Option>) -> Option<(Arc, ReadFiles)> { + DiskCache::get_layer(self, key, virtual_id).map(|(b, f)| (Arc::new(b), f)) } - fn put_layer(&self, key: u128, buf: &Buf) { - DiskCache::put_layer(self, key, buf); + /// Queues a layer write on the disk cache. + fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64) { + DiskCache::put_layer(self, key, buf, files, since); + } + /// The current drop generation of the disk cache. + fn drop_generation(&self) -> u64 { + DiskCache::drop_generation(self) + } + fn dropped_since(&self, files: &ReadFiles, since: u64) -> bool { + DiskCache::dropped_since(self, files, since) } fn min_layer_ms(&self) -> u64 { DiskCache::min_layer_ms(self) @@ -1015,6 +1410,7 @@ fn encode_layer(b: &Buf) -> Vec { out } +/// Decodes a stored layer payload into a buffer, or `None` when it is too short or damaged. fn decode_layer(p: &[u8]) -> Option { if p.len() < 32 { return None; @@ -1025,8 +1421,8 @@ fn decode_layer(p: &[u8]) -> Option { let offset = [f(8)?, f(16)?]; let scale = f(24)?; let planes = lz4_flex::decompress_size_prepended(&p[32..]).ok()?; - let n = w as usize * h as usize; - if planes.len() != n * 16 { + let n = (w as usize).checked_mul(h as usize)?; + if planes.len() != n.checked_mul(16)? { return None; } let mut img = Image::new(w, h); @@ -1047,6 +1443,7 @@ fn decode_layer(p: &[u8]) -> Option { /// Bump when rendering changes in a way that makes old disk entries wrong. const RENDER_VERSION: &str = "effectcraft-render-6"; +/// Feeds the `Debug` form of `v` into the hash, followed by a separator byte. fn hash_debug(h: &mut Hash128, v: &impl std::fmt::Debug) { h.write(format!("{v:?}").as_bytes()); h.write(&[0xff]); @@ -1055,126 +1452,63 @@ fn hash_debug(h: &mut Hash128, v: &impl std::fmt::Debug) { /// Size and content hash of a path that has no native metadata (the browser file table). pub type VirtualFileId<'a> = &'a (dyn Fn(&str) -> Option<(u64, u64)> + Send + Sync); -/// A file's size and modification time (so edited footage gets new keys). When the path has no -/// native metadata, `virtual_id` supplies a durable size and content hash instead. -fn file_stamp(h: &mut Hash128, path: &str, virtual_id: Option>) { - if path.is_empty() { - return; - } - if let Ok(md) = std::fs::metadata(path) { - h.write_u64(md.len()); - if let Ok(t) = md.modified() - && let Ok(d) = t.duration_since(std::time::UNIX_EPOCH) - { - h.write_u64(d.as_nanos() as u64); - } - return; - } - if let Some((len, hash)) = virtual_id.and_then(|id| id(path)) { - h.write_u64(len); - h.write_u64(hash); - } -} +/// A [`VirtualFileId`] a cache can keep (the browser file table's identities). +pub type SharedVirtualFileId = Arc Option<(u64, u64)> + Send + Sync>; -fn footage_stamp(h: &mut Hash128, f: &effectcraft_project::Footage, virtual_id: Option>) { - file_stamp(h, &f.path, virtual_id); - for p in &f.sequence { - file_stamp(h, p, virtual_id); - } -} +/// Layer entries with their source files (#3184), part of every layer disk key: entries of the +/// earlier format (keyed by a project-wide footage salt instead) are never looked up again; +/// least recently used, they are evicted. +const LAYER_ENTRY_VERSION: &[u8] = b"layer-entry-files-2"; -/// Salt for layer cache keys on disk: the renderer version and every footage file's identity -/// (layer keys name footage items, not file contents). -pub fn footage_salt(project: &effectcraft_project::Project) -> u64 { - footage_salt_with(project, None) -} - -/// [`footage_salt`] with [`VirtualFileId`] for paths `std::fs::metadata` cannot describe. -pub fn footage_salt_with(project: &effectcraft_project::Project, virtual_id: Option>) -> u64 { +/// Disk key of a layer buffer: the layer cache's content key ([`crate::cache::layer_key`] and +/// the keys derived from it), the layer entry format and the renderer version. The content key +/// comes from memory alone: it names the footage items the buffer uses with their settings and +/// paths, so two projects only share an entry for the same pixels of the same files; whether +/// those files still hold what the buffer was made of is checked when the entry is read +/// ([`EntryFiles`]). No file is looked at here. +pub fn layer_disk_key(key: u64) -> u128 { let mut h = Hash128::default(); + h.write(LAYER_ENTRY_VERSION); h.write(RENDER_VERSION.as_bytes()); - // Footage another layer reads (Audio Spectrum's Audio Layer); a read precomp can hold any. - let mut read = std::collections::BTreeSet::new(); - let mut read_comp = false; - for (_, c) in project.comps() { - for l in c.layers.iter() { - for r in l.referenced_layers().into_iter().filter(|r| *r != l.id) { - match c.layer(r).map(|x| &x.source) { - Some(effectcraft_project::LayerSource::Footage { item }) => _ = read.insert(*item), - Some(effectcraft_project::LayerSource::Comp { .. }) => read_comp = true, - _ => {} - } - } - } - } - for (id, it) in &project.items { - // (files without video make layer buffers only through a layer that reads them: - // importing music keeps the keys) - if let effectcraft_project::ItemKind::Footage(f) = &it.kind - && (f.has_video || read_comp || read.contains(id)) - { - h.write_u64(id.0); - hash_debug(&mut h, f); - footage_stamp(&mut h, f, virtual_id); - } - if let Some(proxy) = it.proxy.as_ref().filter(|p| p.enabled) { - h.write_u64(id.0); - hash_debug(&mut h, proxy); - footage_stamp(&mut h, &proxy.footage, virtual_id); - } - } - let v = h.finish(); - (v as u64) ^ ((v >> 64) as u64) + h.write_u64(key); + h.finish() } -/// Content key of a composition's pixels: project render settings, the comp and everything it -/// uses (nested comps, footage, solids, their proxies and Use Proxy switches, footage and proxy -/// files' size and time). With expressions in play (which can read any comp) the whole project -/// counts. Switches that don't change pixels (Audio, Lock, Shy, Hide Shy Layers) don't count, and -/// neither do markers unless an expression mentions them. +/// Content key of a composition's pixels, from the project in memory alone (no file is looked +/// at: the frame entries check their own source files when read, see [`EntryFiles`]): project +/// render settings, the comp and the items it can draw (nested comps, footage items with their +/// settings and paths, solids, Media Replacement footage, their proxies and Use Proxy switches; +/// [`effectcraft_project::Project::pixel_reach`]). With expressions among them, everything those +/// expressions can read counts too: every layer of their comps, markers, the comps and footage +/// they name with literal names (and which items have those names), or the whole project when +/// they compute a name or reach items another way. An edit to an item the comp can't reach keeps +/// its key and its frames on disk (#3184, #1492). Switches that don't change pixels (Audio, Lock, +/// Shy, Hide Shy Layers) don't count, and neither do markers unless an expression can read them. pub fn comp_content_key(project: &effectcraft_project::Project, comp: effectcraft_project::ItemId) -> u128 { - comp_content_key_with(project, comp, None) + comp_content_key_for_renderer(project, comp, RENDER_VERSION) } -/// [`comp_content_key`] with [`VirtualFileId`] for paths `std::fs::metadata` cannot describe. -pub fn comp_content_key_with(project: &effectcraft_project::Project, comp: effectcraft_project::ItemId, virtual_id: Option>) -> u128 { - comp_content_key_for_renderer(project, comp, virtual_id, RENDER_VERSION) -} - -fn comp_content_key_for_renderer( - project: &effectcraft_project::Project, - comp: effectcraft_project::ItemId, - virtual_id: Option>, - renderer: &str, -) -> u128 { - use effectcraft_project::{ItemKind, LayerSource}; +/// The content key of `comp` for one renderer: its settings, the comps and footage it uses, and the renderer name. +fn comp_content_key_for_renderer(project: &effectcraft_project::Project, comp: effectcraft_project::ItemId, renderer: &str) -> u128 { + use effectcraft_project::ItemKind; let mut h = Hash128::default(); h.write(renderer.as_bytes()); hash_debug(&mut h, &project.settings); - // Expressions can read anything (every item counts); without them, layers that can't - // change the picture (music, hidden layers nothing reads) and their files don't count. - let expressions = project.reads_markers(); let heard = project.heard_layers(); + let reach = project.pixel_reach(comp, &heard); + // Expressions can read any layer of the comps they reach, hidden or not, and their markers + // (#1083); without them, layers that can't change the picture (music, hidden layers nothing + // reads) don't count. + let expressions = reach.expressions; let silent = |id, c: &effectcraft_project::Comp| if expressions { Default::default() } else { project.silent_layers(id, c, &heard) }; - let mut seen = std::collections::BTreeSet::new(); - let mut stack = vec![comp]; - while let Some(id) = stack.pop() { - if !seen.insert(id) { - continue; - } - let Some(it) = project.item(id) else { continue }; - if let ItemKind::Comp(c) = &it.kind { - let quiet = silent(id, c); - for l in c.layers.iter().filter(|l| !quiet.contains(&l.id)) { - if let LayerSource::Comp { item } | LayerSource::Footage { item } | LayerSource::Solid { item } = l.source { - stack.push(item); - } - } - } + // (the names expressions look items up by: which items have them now) + if !reach.names.is_empty() { + hash_debug(&mut h, &reach.names); } - // Markers are pixels only to the expressions that read them (#1083). - let markers = expressions; - let ids: Vec = if expressions { project.items.keys().copied().collect() } else { seen.into_iter().collect() }; + let ids: Vec = match reach.items { + Some(items) => items.into_iter().collect(), + None => project.items.keys().copied().collect(), + }; for id in ids { let Some(it) = project.item(id) else { continue }; h.write_u64(id.0); @@ -1184,30 +1518,30 @@ fn comp_content_key_for_renderer( let quiet = silent(id, c); let heard = |l| heard.contains(id, l); if quiet.is_empty() { - hash_debug(&mut h, &c.pixel_form_with(markers, &heard)); + hash_debug(&mut h, &c.pixel_form_with(expressions, &heard)); } else { let mut pic = (**c).clone(); pic.layers.retain(|l| !quiet.contains(&l.id)); - hash_debug(&mut h, &pic.pixel_form_with(markers, &heard)); + hash_debug(&mut h, &pic.pixel_form_with(expressions, &heard)); } } k => hash_debug(&mut h, k), } - if let ItemKind::Footage(f) = &it.kind { - footage_stamp(&mut h, f, virtual_id); - } hash_debug(&mut h, &it.proxy); - if let Some(px) = &it.proxy { - footage_stamp(&mut h, &px.footage, virtual_id); - } } h.write_u64(comp.0); h.finish() } +/// Frame entries with their source files (#3184), part of every frame key: entries of the +/// earlier format (keyed by footage stamps instead) are never looked up again; least recently +/// used, they are evicted. +const FRAME_ENTRY_VERSION: &[u8] = b"frame-entry-files-2"; + /// Disk key of one viewer frame. pub fn frame_key(content: u128, frame: i64, scale_milli: u32, view: u64) -> u128 { let mut h = Hash128::default(); + h.write(FRAME_ENTRY_VERSION); h.write(&content.to_le_bytes()); h.write_u64(frame as u64); h.write_u64(scale_milli as u64); @@ -1231,18 +1565,18 @@ mod tests { fn link_frame_serves_a_frame_under_a_new_key() { let dc = DiskCache::open(tmpdir("link"), 1 << 30).unwrap(); let rgba: Vec = (0..16).collect(); - dc.put_frame_now(1, 2, 2, &rgba); + dc.put_frame_now(1, 2, 2, &rgba, &ReadFiles::default()); assert!(dc.link_frame(1, 2)); - assert_eq!(dc.get_frame(2).map(|f| f.rgba), Some(rgba.clone())); + assert_eq!(dc.get_frame(2, None).map(|f| f.0).map(|f| f.rgba), Some(rgba.clone())); assert!(!dc.link_frame(1, 2), "already there"); assert!(!dc.link_frame(3, 4), "nothing to link"); assert!(!dc.contains(Kind::Frame, 4)); - dc.put_frame_now(5, 2, 2, &[7; 16]); + dc.put_frame_now(5, 2, 2, &[7; 16], &ReadFiles::default()); assert!(!dc.link_frame(5, 2), "an existing entry stays"); - assert_eq!(dc.get_frame(2).map(|f| f.rgba), Some(rgba.clone())); + assert_eq!(dc.get_frame(2, None).map(|f| f.0).map(|f| f.rgba), Some(rgba.clone())); let _ = std::fs::remove_file(dc.path(Kind::Frame, 1)); dc.forget(Kind::Frame, 1); - assert_eq!(dc.get_frame(2).map(|f| f.rgba), Some(rgba), "the link outlives the original"); + assert_eq!(dc.get_frame(2, None).map(|f| f.0).map(|f| f.rgba), Some(rgba), "the link outlives the original"); } fn buf(seed: u32, w: u32, h: u32) -> Buf { @@ -1277,11 +1611,21 @@ mod tests { #[test] fn frame_entries_round_trip_and_names_parse() { let rgba: Vec = (0..4 * 6 * 3).map(|i| (i * 7 % 256) as u8).collect(); - let e = frame_entry(6, 3, &rgba); - assert_eq!(read_frame_entry(&e), Some(Frame8 { width: 6, height: 3, rgba: rgba.clone() })); + let files: ReadFiles = [crate::reads::ReadFile::new("/v.png", crate::reads::FileStamp::Virtual)].into_iter().collect(); + let (one, two) = (|_: &str| Some((4, 1)), |_: &str| Some((4, 2))); + assert!(frame_entry(6, 3, &rgba, &files, None).is_none(), "a file from memory needs its identity"); + assert!(frame_entry(6, 3, &rgba[1..], &files, Some(&one)).is_none(), "a wrong size"); + let e = frame_entry(6, 3, &rgba, &files, Some(&one)).unwrap(); + assert_eq!(read_frame_entry(&e, Some(&one)), Some(Frame8 { width: 6, height: 3, rgba: rgba.clone() })); + assert!(read_frame_entry(&e, Some(&two)).is_none(), "the file changed"); + let mut incomplete = files.clone(); + incomplete.mark_incomplete(); + assert!(frame_entry(6, 3, &rgba, &incomplete, Some(&one)).is_none(), "an incomplete list is not stored"); let mut bad = e.clone(); bad[20] ^= 1; - assert!(read_frame_entry(&bad).is_none()); + assert!(read_frame_entry(&bad, Some(&one)).is_none()); + // An entry of the format before #3184 (pixels without files) is never read. + assert!(read_frame_entry(&seal_format(Kind::Frame, FORMAT_RGBA8, &encode_frame(6, 3, &rgba)), Some(&one)).is_none()); let key = 0x0123_4567_89ab_cdef_0011_2233_4455_6677u128; assert_eq!(parse_entry_name(&entry_name(key)), Some(key)); assert_eq!(parse_entry_name("abc.ecc"), None); @@ -1298,27 +1642,27 @@ mod tests { let (w, h) = (7u32, 5u32); // 4.0, 16.0, -0.25, 65504 and a spread of bit patterns. let rgba: Vec = (0..w * h * 4).map(|i| [0x4400, 0x4c00, 0xb400, 0x7bff][i as usize % 4] ^ ((i * 37) as u16 & 0x03ff)).collect(); - dc.put_frame_f16_now(1, w, h, &rgba); - assert_eq!(dc.get_frame_f16(1), Some(FrameF16 { width: w, height: h, rgba: rgba.clone() })); - assert!(dc.get_frame(1).is_none(), "not an 8-bit frame"); - dc.put_frame_now(2, 2, 2, &[9; 16]); - assert!(dc.get_frame_f16(2).is_none() && dc.contains(Kind::Frame, 2), "an 8-bit entry stays valid"); - assert_eq!(dc.get_frame(2).unwrap().rgba, vec![9; 16]); + dc.put_frame_f16_now(1, w, h, &rgba, &ReadFiles::default()); + assert_eq!(dc.get_frame_f16(1, None).map(|f| f.0), Some(FrameF16 { width: w, height: h, rgba: rgba.clone() })); + assert!(dc.get_frame(1, None).map(|f| f.0).is_none(), "not an 8-bit frame"); + dc.put_frame_now(2, 2, 2, &[9; 16], &ReadFiles::default()); + assert!(dc.get_frame_f16(2, None).map(|f| f.0).is_none() && dc.contains(Kind::Frame, 2), "an 8-bit entry stays valid"); + assert_eq!(dc.get_frame(2, None).map(|f| f.0).unwrap().rgba, vec![9; 16]); assert!(dc.stats().bytes > 0); // Reopened, still there (format byte persisted). drop(dc); let dc = DiskCache::open(&dir, 1 << 30).unwrap(); - assert_eq!(dc.get_frame_f16(1).unwrap().rgba, rgba); + assert_eq!(dc.get_frame_f16(1, None).map(|f| f.0).unwrap().rgba, rgba); // A sealed entry with a payload of the wrong size: deleted on read. let p = dc.path(Kind::Frame, 3); std::fs::create_dir_all(p.parent().unwrap()).unwrap(); - std::fs::write(&p, seal_format(Kind::Frame, FORMAT_F16, &encode_frame_f16(w, h + 1, &rgba[..])[..])).unwrap(); + std::fs::write(&p, seal_format(Kind::Frame, FORMAT_F16_FILES, &with_files(&EntryFiles::default(), encode_frame_f16(w, h + 1, &rgba[..])))).unwrap(); dc.rescan(); assert!(dc.contains(Kind::Frame, 3)); - assert!(dc.get_frame_f16(3).is_none()); + assert!(dc.get_frame_f16(3, None).map(|f| f.0).is_none()); assert!(!dc.contains(Kind::Frame, 3) && !p.exists()); // Wrong lengths are not written. - dc.put_frame_f16(4, w, h, &rgba[1..]); + dc.put_frame_f16(4, w, h, &rgba[1..], &ReadFiles::default(), None, 0); dc.flush(); assert!(!dc.contains(Kind::Frame, 4)); let _ = std::fs::remove_dir_all(&dir); @@ -1360,26 +1704,27 @@ mod tests { fn hit_miss_and_exact_round_trip() { let dir = tmpdir("hit"); let dc = DiskCache::open(&dir, 1 << 30).unwrap(); - assert!(dc.get_layer(42).is_none()); + assert!(dc.get_layer(42, None).is_none()); let b = buf(1, 33, 17); - dc.put_layer(42, &b); + dc.put_layer(42, &b, &EntryFiles::default(), 0); dc.flush(); - let back = dc.get_layer(42).unwrap(); + let (back, files) = dc.get_layer(42, None).unwrap(); + assert!(files.is_empty() && files.is_complete()); assert_eq!(back.img, b.img); assert_eq!((back.offset, back.scale), (b.offset, b.scale)); let rgba: Vec = (0..20 * 10 * 4).map(|i| (i % 251) as u8).collect(); - assert!(dc.put_frame(7, 20, 10, &rgba)); - assert!(!dc.put_frame(8, 2, 2, &[0; 3]), "a wrong-size frame is refused"); + assert!(dc.put_frame(7, 20, 10, &rgba, &ReadFiles::default(), None, 0)); + assert!(!dc.put_frame(8, 2, 2, &[0; 3], &ReadFiles::default(), None, 0), "a wrong-size frame is refused"); dc.flush(); - assert_eq!(dc.get_frame(7).unwrap().rgba, rgba); + assert_eq!(dc.get_frame(7, None).map(|f| f.0).unwrap().rgba, rgba); let st = dc.stats(); assert_eq!((st.entries, st.hits, st.misses, st.writes), (2, 2, 1, 2)); // Wrong kind is a miss. - assert!(dc.get_frame(42).is_none()); + assert!(dc.get_frame(42, None).map(|f| f.0).is_none()); } /// Music doesn't change a comp's disk keys (adding it to the timeline kept no blue frame); - /// hiding a clip does. Importing music keeps the layer-cache salt. + /// hiding a clip does. Importing music keeps them too. #[test] fn music_keeps_the_disk_keys() { use effectcraft_project::{Comp, Footage, FootageKind, ItemKind, LayerSource, Project, Solid, build}; @@ -1391,14 +1736,14 @@ mod tests { let clip = build::layer(&mut p, &comp, "Clip", LayerSource::Solid { item: sid }, (64, 64), None); let clip_id = clip.id; p.comp_mut(cid).unwrap().layers.push(clip); - let (key, salt) = (comp_content_key(&p, cid), footage_salt(&p)); + let key = comp_content_key(&p, cid); let song = p.add_item( "song.wav", Default::default(), None, ItemKind::Footage(Footage { kind: FootageKind::Audio, has_video: false, has_audio: true, ..Default::default() }), ); - assert_eq!(footage_salt(&p), salt, "importing music"); + assert_eq!(comp_content_key(&p, cid), key, "importing music"); let music = build::layer(&mut p, &comp, "Music", LayerSource::Footage { item: song }, (64, 64), None); p.comp_mut(cid).unwrap().layers.insert(0, music); assert_eq!(comp_content_key(&p, cid), key, "music on the timeline"); @@ -1406,6 +1751,28 @@ mod tests { assert_ne!(comp_content_key(&p, cid), key, "a hidden clip"); } + /// An expression that reads a 3D model layer's source (`thisComp.layer("Model").source.name`) + /// gives a new key when the model item is renamed: the frames on disk hold the old name. + #[test] + fn renaming_a_model_an_expression_reads_changes_the_disk_key() { + use effectcraft_project::{Comp, Expression, Footage, FootageKind, ItemKind, LayerSource, Project, Solid, build}; + use effectcraft_time::{FrameRate, Tick}; + let mut p = Project::default(); + let comp = Comp::new(64, 64, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); + let cid = p.add_item("Main", Default::default(), None, ItemKind::Comp(comp.clone().into())); + let model = p.add_item("chair.glb", Default::default(), None, ItemKind::Footage(Footage { kind: FootageKind::Model, ..Default::default() })); + let sid = p.add_item("Solid", Default::default(), None, ItemKind::Solid(Solid { color: [1.0, 0.0, 0.0], width: 64, height: 64, pixel_aspect: 1.0 })); + let mut m = build::layer(&mut p, &comp, "Model", LayerSource::Model { item: model }, (64, 64), None); + m.switches.video = false; + let mut s = build::layer(&mut p, &comp, "Reader", LayerSource::Solid { item: sid }, (64, 64), None); + s.props.prop_mut("transform/opacity").unwrap().expr = + Some(Expression { text: "thisComp.layer(\"Model\").source.name.length * 10".into(), enabled: true }); + p.comp_mut(cid).unwrap().layers.extend([s, m]); + let key = comp_content_key(&p, cid); + p.item_mut(model).unwrap().name = "table.glb".into(); + assert_ne!(comp_content_key(&p, cid), key, "the expression reads the model's name"); + } + /// A burst of layer writes never crowds out frames: every frame of a fully cached comp is /// on disk afterwards (the blue bar came back with gaps). #[test] @@ -1413,11 +1780,11 @@ mod tests { let dir = tmpdir("burst"); let dc = DiskCache::open(&dir, 1 << 30).unwrap(); for k in 0..300 { - dc.put_layer(10_000 + k, &buf(1, 64, 64)); + dc.put_layer(10_000 + k, &buf(1, 64, 64), &EntryFiles::default(), 0); } let rgba = vec![7u8; 32 * 32 * 4]; for k in 0..300 { - dc.put_frame(k, 32, 32, &rgba); + dc.put_frame(k, 32, 32, &rgba, &ReadFiles::default(), None, 0); } dc.flush(); assert!((0..300).all(|k| dc.contains(Kind::Frame, k)), "every frame written"); @@ -1432,17 +1799,17 @@ mod tests { let comp = Comp::new(2, 2, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); let id = project.add_item("Cached composition", effectcraft_color::Label::None, None, ItemKind::Comp(comp.into())); let key = frame_key(comp_content_key(&project, id), 0, 1000, 0); - let previous_key = frame_key(comp_content_key_for_renderer(&project, id, None, "effectcraft-render-5"), 0, 1000, 0); + let previous_key = frame_key(comp_content_key_for_renderer(&project, id, "effectcraft-render-5"), 0, 1000, 0); let dir = tmpdir("previous-renderer"); { let cache = DiskCache::open(&dir, 1 << 30).unwrap(); - cache.put_frame_now(previous_key, 2, 2, &[9; 16]); + cache.put_frame_now(previous_key, 2, 2, &[9; 16], &ReadFiles::default()); } let cache = DiskCache::open(&dir, 1 << 30).unwrap(); - assert_eq!(cache.get_frame(previous_key).unwrap().rgba, vec![9; 16]); - assert!(cache.get_frame(key).is_none()); - cache.put_frame_now(key, 2, 2, &[17; 16]); - assert_eq!(cache.get_frame(key).unwrap().rgba, vec![17; 16]); + assert_eq!(cache.get_frame(previous_key, None).map(|f| f.0).unwrap().rgba, vec![9; 16]); + assert!(cache.get_frame(key, None).map(|f| f.0).is_none()); + cache.put_frame_now(key, 2, 2, &[17; 16], &ReadFiles::default()); + assert_eq!(cache.get_frame(key, None).map(|f| f.0).unwrap().rgba, vec![17; 16]); } #[test] @@ -1450,16 +1817,16 @@ mod tests { let dir = tmpdir("persist"); { let dc = DiskCache::open(&dir, 1 << 30).unwrap(); - dc.put_layer_now(1, &buf(1, 8, 8)); - dc.put_frame_now(2, 2, 2, &[9; 16]); + dc.put_layer_now(1, &buf(1, 8, 8), &ReadFiles::default()); + dc.put_frame_now(2, 2, 2, &[9; 16], &ReadFiles::default()); } let dc = DiskCache::open(&dir, 1 << 30).unwrap(); assert_eq!(dc.stats().entries, 2); assert!(dc.contains(Kind::Layer, 1) && dc.contains(Kind::Frame, 2)); - assert_eq!(dc.get_frame(2).unwrap().rgba, vec![9; 16]); + assert_eq!(dc.get_frame(2, None).map(|f| f.0).unwrap().rgba, vec![9; 16]); dc.clear(); assert_eq!(dc.stats().entries, 0); - assert!(dc.get_frame(2).is_none()); + assert!(dc.get_frame(2, None).map(|f| f.0).is_none()); let again = DiskCache::open(&dir, 1 << 30).unwrap(); assert_eq!(again.stats().entries, 0); } @@ -1469,11 +1836,11 @@ mod tests { let dir = tmpdir("lru"); let dc = DiskCache::open(&dir, u64::MAX).unwrap(); for k in 0..6u128 { - dc.put_layer_now(k, &buf(k as u32, 64, 64)); + dc.put_layer_now(k, &buf(k as u32, 64, 64), &ReadFiles::default()); } let per = dc.stats().bytes / 6; // Touch 0 so it is the most recently used. - assert!(dc.get_layer(0).is_some()); + assert!(dc.get_layer(0, None).is_some()); dc.set_max_bytes(per * 3 + per * 3 / 4); let st = dc.stats(); assert!(st.bytes <= per * 3 + per * 3 / 4, "{st:?}"); @@ -1485,7 +1852,7 @@ mod tests { let files = walk(&dir); assert_eq!(files, 3); // New writes keep evicting. - dc.put_layer_now(99, &buf(9, 64, 64)); + dc.put_layer_now(99, &buf(9, 64, 64), &ReadFiles::default()); assert!(dc.stats().bytes <= per * 3 + per * 3 / 4); } @@ -1510,7 +1877,7 @@ mod tests { let dir = tmpdir("space"); let dc = DiskCache::open(&dir, 1 << 30).unwrap(); for k in 0..4u128 { - dc.put_layer_now(k, &buf(k as u32, 64, 64)); + dc.put_layer_now(k, &buf(k as u32, 64, 64), &ReadFiles::default()); } let used = dc.stats().bytes; // Unknown space: the setting alone. @@ -1519,17 +1886,18 @@ mod tests { // A disk reported as 0 bytes is unknown. dc.set_disk_space(Some((0, 0))); assert_eq!(dc.stats().limit_bytes, 1 << 30); - // 100 GB disk, 5 GB reserved, exactly 5 GB free: the cache may not grow. + // 100 GB disk, 5 GB reserved (5%, between the 2 GB floor and the 10 GB cap), exactly + // 5 GB free: the cache may not grow. dc.set_disk_space(Some((100 << 30, 5 << 30))); let st = dc.stats(); assert_eq!((st.max_bytes, st.limit_bytes), (1 << 30, used)); - dc.put_layer_now(9, &buf(9, 64, 64)); + dc.put_layer_now(9, &buf(9, 64, 64), &ReadFiles::default()); let st = dc.stats(); assert!(st.bytes <= st.limit_bytes && st.bytes <= used - used / 10 && st.evictions > 0, "{st:?}"); assert!(dc.contains(Kind::Layer, 9), "the new entry is the most recently used"); // Below the reserve the cache gives its space back, and skips writes that can't fit. dc.set_disk_space(Some((100 << 30, 4 << 30))); - dc.put_layer_now(10, &buf(10, 64, 64)); + dc.put_layer_now(10, &buf(10, 64, 64), &ReadFiles::default()); let st = dc.stats(); assert_eq!((st.limit_bytes, st.entries, st.bytes), (0, 0, 0), "{st:?}"); // Plenty of space again: back to the setting. @@ -1538,14 +1906,86 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// The reserve is 5% of the disk, at least 2 GB and at most 10 GB: a large disk with plenty + /// free keeps the user's size. (A 4 TB disk with 190 GB free reserved 200 GB, held a 20 GB + /// cache at 0, and every write was refused.) + #[test] + fn a_large_disk_reserves_at_most_ten_gb() { + const GB: u64 = 1 << 30; + assert_eq!(disk_reserve(4096 * GB), 10 * GB); + assert_eq!(disk_reserve(100 * GB), 5 * GB); + assert_eq!(disk_reserve(32 * GB), 2 * GB); + assert_eq!(disk_reserve(1), 2 * GB); + let dir = tmpdir("space-large"); + let dc = DiskCache::open(&dir, 20 * GB).unwrap(); + dc.set_disk_space(Some((4096 * GB, 190 * GB))); + let st = dc.stats(); + assert_eq!((st.max_bytes, st.limit_bytes), (20 * GB, 20 * GB), "{st:?}"); + assert!(dc.accepts_writes()); + assert_eq!(dc.reserve_bytes(), Some(10 * GB)); + // Just above the 10 GB reserve: the cache may use what is left above it. + dc.set_disk_space(Some((4096 * GB, 15 * GB))); + assert_eq!(dc.stats().limit_bytes, 5 * GB); + // A small disk still keeps 2 GB free: 3 GB free leaves 1 GB for the cache. + dc.set_disk_space(Some((32 * GB, 3 * GB))); + assert_eq!(dc.stats().limit_bytes, GB); + // Below the floor: nothing. + dc.set_disk_space(Some((32 * GB, GB))); + assert_eq!(dc.stats().limit_bytes, 0); + assert!(!dc.accepts_writes()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A write the free disk space refuses is refused before its pixels are compressed: the + /// frame workers spent about 8 ms per frame and 14 ms per layer encoding entries that were + /// then refused (`limit_bytes=0`). + #[test] + fn no_encode_while_the_disk_has_no_room() { + const GB: u64 = 1 << 30; + let dir = tmpdir("space-no-encode"); + let dc = DiskCache::open(&dir, 20 * GB).unwrap(); + // 4 TB disk, 1 GB free: under the 10 GB reserve, the limit is 0. + dc.set_disk_space(Some((4096 * GB, GB))); + assert_eq!(dc.stats().limit_bytes, 0); + let rgba = vec![7u8; 16 * 16 * 4]; + assert!(!dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0), "refused"); + assert!(!dc.put_frame_f16(2, 16, 16, &[0x3c00u16; 16 * 16 * 4], &ReadFiles::default(), None, 0), "refused"); + dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0); + dc.flush(); + assert_eq!(dc.encodes(), 0, "nothing compressed"); + assert_eq!(dc.stats().entries, 0); + // 32 MB of room above the reserve: a few frames would fill it, the rest would be + // compressed only to be refused. Not worth encoding either. + dc.set_disk_space(Some((4096 * GB, 10 * GB + (32 << 20)))); + assert_eq!(dc.stats().limit_bytes, 32 << 20); + assert!(!dc.accepts_writes()); + assert!(!dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0), "refused"); + dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0); + assert_eq!(dc.encodes(), 0, "nothing compressed"); + // A small limit the setting asks for (not free space) is used. + dc.set_disk_space(None); + dc.set_max_bytes(32 << 20); + assert!(dc.accepts_writes()); + dc.set_max_bytes(20 * GB); + // Room again: the same writes are compressed and stored. + dc.set_disk_space(Some((4096 * GB, 500 * GB))); + assert!(dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0)); + assert!(dc.put_frame_f16(2, 16, 16, &[0x3c00u16; 16 * 16 * 4], &ReadFiles::default(), None, 0)); + dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0); + dc.flush(); + assert_eq!(dc.encodes(), 3); + assert_eq!(dc.stats().entries, 3); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn failed_writes_are_counted() { let dir = tmpdir("failed"); let dc = DiskCache::open(&dir, 1 << 30).unwrap(); // A file where the layers folder should be: every layer write fails. std::fs::write(dir.join(VERSION).join(Kind::Layer.dir()), b"x").unwrap(); - dc.put_layer_now(1, &buf(1, 8, 8)); - dc.put_layer_now(2, &buf(2, 8, 8)); + dc.put_layer_now(1, &buf(1, 8, 8), &ReadFiles::default()); + dc.put_layer_now(2, &buf(2, 8, 8), &ReadFiles::default()); let st = dc.stats(); assert_eq!((st.failed, st.writes, st.entries), (2, 0, 0)); let _ = std::fs::remove_dir_all(&dir); @@ -1570,13 +2010,13 @@ mod tests { fn damaged_files_are_misses() { let dir = tmpdir("damaged"); let dc = DiskCache::open(&dir, 1 << 30).unwrap(); - dc.put_frame_now(5, 4, 4, &[1; 64]); + dc.put_frame_now(5, 4, 4, &[1; 64], &ReadFiles::default()); let p = dc.path(Kind::Frame, 5); let mut d = std::fs::read(&p).unwrap(); let n = d.len(); d[n / 2] ^= 0xff; std::fs::write(&p, d).unwrap(); - assert!(dc.get_frame(5).is_none()); + assert!(dc.get_frame(5, None).map(|f| f.0).is_none()); assert!(!dc.contains(Kind::Frame, 5)); assert!(!p.exists()); } @@ -1594,8 +2034,8 @@ mod tests { for i in 0..20u128 { let k = i % 5; let w = if t % 2 == 0 { &a } else { &b }; - w.put_layer_now(k, &buf(k as u32, 16, 16)); - if let Some(x) = w.get_layer(k) { + w.put_layer_now(k, &buf(k as u32, 16, 16), &ReadFiles::default()); + if let Some((x, _)) = w.get_layer(k, None) { assert_eq!(x.img, buf(k as u32, 16, 16).img); } } @@ -1604,39 +2044,16 @@ mod tests { }); b.rescan(); for k in 0..5u128 { - assert_eq!(b.get_layer(k).unwrap().img, buf(k as u32, 16, 16).img); + assert_eq!(b.get_layer(k, None).unwrap().0.img, buf(k as u32, 16, 16).img); } } - /// An audio file nothing reads stays out of the layer salt; one a layer reads (Audio - /// Spectrum) is in it, so overwriting it doesn't reuse that layer's buffers. + /// Replaced bytes of a path with no native metadata (the browser file table) change neither + /// the comp key nor a layer's disk key (both from memory): the frame and layer entries made + /// of them check the hook's size and content hash when read and are misses, deleted (#1253, + /// #3184). #[test] - fn read_audio_counts_in_the_footage_salt() { - use effectcraft_project::build; - use effectcraft_project::{Comp, Footage, FootageKind, ItemKind, LayerSource, MatteKind, Project, TrackMatte}; - use effectcraft_time::{FrameRate, Tick}; - - let mut p = Project::default(); - let f = Footage { path: "/song.wav".into(), kind: FootageKind::Audio, has_video: false, has_audio: true, ..Default::default() }; - let song = p.add_item("song.wav", effectcraft_color::Label::None, None, ItemKind::Footage(f)); - let mut comp = Comp::new(16, 9, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); - let music = build::layer(&mut p, &comp, "music", LayerSource::Footage { item: song }, (16, 9), None); - let mut reader = build::layer(&mut p, &comp, "reader", LayerSource::Null, (16, 9), None); - let music_id = music.id; - comp.layers = vec![reader.clone(), music].into(); - let cid = p.add_item("C", effectcraft_color::Label::None, None, ItemKind::Comp(comp.into())); - - let id = |mark: u64| move |path: &str| (path == "/song.wav").then_some((8, mark)); - let (one, two) = (id(1), id(2)); - assert_eq!(footage_salt_with(&p, Some(&one)), footage_salt_with(&p, Some(&two)), "unread music stays out"); - reader.track_matte = Some(TrackMatte { layer: music_id, kind: MatteKind::Alpha }); - *p.comp_mut(cid).unwrap().layers.first_mut().unwrap() = reader; - assert_ne!(footage_salt_with(&p, Some(&one)), footage_salt_with(&p, Some(&two)), "read audio counts"); - } - - /// A path with no native metadata takes its key from the caller's size and content hash (#1253). - #[test] - fn virtual_file_bytes_change_content_keys() { + fn virtual_file_bytes_are_checked_by_the_entries_not_the_keys() { use effectcraft_project::build; use effectcraft_project::{Comp, Footage, ItemKind, LayerSource, Project}; use effectcraft_time::{FrameRate, Tick}; @@ -1650,12 +2067,335 @@ mod tests { let id = |mark: u64| move |path: &str| (path == "/virtual.mov").then_some((8, mark)); let (one, two) = (id(1), id(2)); - let a = footage_salt_with(&p, Some(&one)); - let b = footage_salt_with(&p, Some(&two)); - let ka = comp_content_key_with(&p, cid, Some(&one)); - let kb = comp_content_key_with(&p, cid, Some(&two)); - assert_ne!(a, b, "replaced virtual media must not reuse the salt"); - assert_ne!(ka, kb, "replaced virtual media must not reuse the frame key"); - assert_eq!(comp_content_key(&p, cid), comp_content_key_with(&p, cid, None)); + let dc = DiskCache::open(tmpdir("virtual"), 1 << 30).unwrap(); + let k = frame_key(comp_content_key(&p, cid), 0, 1000, 0); + let files: ReadFiles = [crate::reads::ReadFile::new("/virtual.mov", crate::reads::FileStamp::Virtual)].into_iter().collect(); + assert!(dc.put_frame(k, 1, 1, &[1, 2, 3, 4], &files, Some(&one), 0)); + dc.flush(); + assert!(dc.get_frame(k, Some(&one)).is_some_and(|(_, f)| f == files)); + assert!(dc.get_frame(k, Some(&two)).is_none() && !dc.contains(Kind::Frame, k), "replaced: a miss, deleted"); + // A layer buffer made of it. + let lk = layer_disk_key(7); + dc.put_layer(lk, &buf(1, 4, 4), &EntryFiles::of(&files, Some(&one)).unwrap(), 0); + dc.flush(); + assert!(dc.get_layer(lk, Some(&one)).is_some_and(|(b, f)| b.img == buf(1, 4, 4).img && f == files && f.is_complete())); + assert!(dc.get_layer(lk, None).is_none() && !dc.contains(Kind::Layer, lk), "no identity: a miss, deleted"); + dc.put_layer(lk, &buf(1, 4, 4), &EntryFiles::of(&files, Some(&one)).unwrap(), 0); + dc.flush(); + assert!(dc.get_layer(lk, Some(&two)).is_none() && !dc.contains(Kind::Layer, lk), "replaced: a miss, deleted"); + // The browser's sealed layer entries carry the same list. + let e = layer_entry(&buf(2, 3, 2), &EntryFiles::of(&files, Some(&one)).unwrap()); + let (b, list) = read_layer_entry(&e).unwrap(); + assert!(b.img == buf(2, 3, 2).img && list.unchanged(Some(&one)) && !list.unchanged(Some(&two))); + assert!(read_layer_entry(&seal_format(Kind::Layer, FORMAT_RGBA8, &encode_layer(&buf(2, 3, 2)))).is_none(), "the format before #3184"); + let mut bad = e.clone(); + bad[12] ^= 1; + assert!(read_layer_entry(&bad).is_none(), "damaged"); + } + + /// Layer disk keys come from the layer cache's key alone (no file, no project-wide salt) and + /// differ per key. + #[test] + fn layer_disk_keys_need_no_files() { + assert_eq!(layer_disk_key(1), layer_disk_key(1)); + assert_ne!(layer_disk_key(1), layer_disk_key(2)); + // Not the layout of the salted keys before #3184 (`salt << 64 | key`). + assert_ne!(layer_disk_key(1) as u64, 1); + } + + /// A layer entry is used only while the files it lists are what the buffer was made of: a + /// new size, a new time, a missing file that is back, a file replaced by a folder, a damaged + /// files section and the format before #3184 are misses, and the entry is deleted so the + /// buffer is rendered and stored again. A buffer whose files can't all be named is not + /// stored. Only the listed files are looked at. + #[test] + fn layer_entries_check_their_files() { + use crate::reads::{FileStamp, ReadFile}; + let dir = tmpdir("layer-check"); + let src = dir.join("src"); + std::fs::create_dir_all(&src).unwrap(); + let file = |name: &str, bytes: &[u8]| { + let p = src.join(name); + std::fs::write(&p, bytes).unwrap(); + p.to_string_lossy().to_string() + }; + let (a, b) = (file("a.png", b"aa"), file("b.png", b"bb")); + let gone = src.join("gone.png").to_string_lossy().to_string(); + let read = |paths: &[&str]| -> ReadFiles { paths.iter().map(|p| ReadFile::new(*p, FileStamp::native(p).unwrap_or(FileStamp::Missing))).collect() }; + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + dc.put_layer_now(1, &buf(1, 4, 4), &read(&[&a])); + dc.put_layer_now(2, &buf(2, 4, 4), &read(&[&b, &gone])); + dc.put_layer_now(3, &buf(3, 4, 4), &read(&[&b])); + let (one, files) = dc.get_layer(1, None).unwrap(); + assert!(one.img == buf(1, 4, 4).img && files == read(&[&a]) && files.is_complete()); + assert_eq!(dc.file_checks(), 1, "only the file of that buffer"); + // a.png gets a new size: entry 1 is a miss and deleted, the others stay. + std::fs::write(&a, b"aaa").unwrap(); + assert!(dc.get_layer(1, None).is_none() && !dc.contains(Kind::Layer, 1)); + assert!(dc.get_layer(2, None).is_some() && dc.get_layer(3, None).is_some()); + // b.png gets a new time only. + let t = std::fs::metadata(&b).unwrap().modified().unwrap() + std::time::Duration::from_secs(7); + std::fs::File::options().append(true).open(&b).unwrap().set_modified(t).unwrap(); + assert!(dc.get_layer(3, None).is_none() && !dc.contains(Kind::Layer, 3)); + assert!(dc.get_layer(2, None).is_none(), "b.png is listed there too"); + // A missing file that is back. + dc.put_layer_now(4, &buf(4, 4, 4), &read(&[&gone])); + assert!(dc.get_layer(4, None).is_some(), "missing, still missing"); + file("gone.png", b"g"); + assert!(dc.get_layer(4, None).is_none() && !dc.contains(Kind::Layer, 4), "back"); + // A file replaced by a folder (unreadable). + let c = file("c.png", b"cc"); + dc.put_layer_now(5, &buf(5, 4, 4), &read(&[&c])); + std::fs::remove_file(&c).unwrap(); + std::fs::create_dir(&c).unwrap(); + assert!(dc.get_layer(5, None).is_none() && !dc.contains(Kind::Layer, 5)); + // Written again after the miss: a hit with the new identity. + dc.put_layer_now(1, &buf(9, 4, 4), &read(&[&a])); + assert_eq!(dc.get_layer(1, None).map(|l| l.0.img), Some(buf(9, 4, 4).img)); + // The format before #3184 (no files): a miss, deleted. + dc.write_format(Kind::Layer, 6, FORMAT_RGBA8, &encode_layer(&buf(6, 4, 4))); + assert!(dc.contains(Kind::Layer, 6)); + assert!(dc.get_layer(6, None).is_none() && !dc.contains(Kind::Layer, 6)); + // A valid checksum over a damaged files section, or over a cut buffer: misses, deleted. + let mut payload = vec![]; + payload.extend_from_slice(&3u32.to_le_bytes()); + payload.extend_from_slice(&encode_layer(&buf(7, 4, 4))); + dc.write_format(Kind::Layer, 7, FORMAT_LAYER_FILES, &payload); + assert!(dc.get_layer(7, None).is_none() && !dc.contains(Kind::Layer, 7)); + let mut cut = vec![]; + EntryFiles::default().encode(&mut cut); + cut.extend_from_slice(&encode_layer(&buf(8, 4, 4))[..40]); + dc.write_format(Kind::Layer, 8, FORMAT_LAYER_FILES, &cut); + assert!(dc.get_layer(8, None).is_none() && !dc.contains(Kind::Layer, 8)); + // A damaged file on disk. + dc.put_layer_now(9, &buf(9, 4, 4), &ReadFiles::default()); + let p = dc.path(Kind::Layer, 9); + let mut d = std::fs::read(&p).unwrap(); + let n = d.len(); + d[n / 2] ^= 0xff; + std::fs::write(&p, d).unwrap(); + assert!(dc.get_layer(9, None).is_none() && !dc.contains(Kind::Layer, 9) && !p.exists()); + // Buffers whose files can't all be named are not stored. + let mut incomplete = read(&[&a]); + incomplete.mark_incomplete(); + dc.put_layer_now(10, &buf(10, 4, 4), &incomplete); + assert!(!dc.contains(Kind::Layer, 10)); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A frame entry is used only while the files it lists are what it was made of: a new size, + /// a new time, a missing file that is back, a file replaced by a folder are misses, and the + /// entry is deleted so the frame is rendered and stored again. Only the listed files are + /// looked at. Entries of the format before #3184 are misses and deleted. + #[test] + fn frame_entries_check_their_files() { + use crate::reads::{FileStamp, ReadFile}; + let dir = tmpdir("check"); + let src = dir.join("src"); + std::fs::create_dir_all(&src).unwrap(); + let file = |name: &str, bytes: &[u8]| { + let p = src.join(name); + std::fs::write(&p, bytes).unwrap(); + p.to_string_lossy().to_string() + }; + let (a, b) = (file("a.png", b"aa"), file("b.png", b"bb")); + let gone = src.join("gone.png").to_string_lossy().to_string(); + let read = |paths: &[&str]| -> ReadFiles { paths.iter().map(|p| ReadFile::new(*p, FileStamp::native(p).unwrap_or(FileStamp::Missing))).collect() }; + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + dc.put_frame_now(1, 1, 1, &[1; 4], &read(&[&a])); + dc.put_frame_now(2, 1, 1, &[2; 4], &read(&[&b, &gone])); + dc.put_frame_now(3, 1, 1, &[3; 4], &read(&[&b])); + assert_eq!(dc.get_frame(1, None).map(|f| f.1), Some(read(&[&a]))); + assert_eq!(dc.file_checks(), 1, "only the file of that frame"); + // a.png gets a new size: frame 1 is a miss and deleted, the others stay. + std::fs::write(&a, b"aaa").unwrap(); + assert!(dc.get_frame(1, None).is_none() && !dc.contains(Kind::Frame, 1)); + assert!(dc.get_frame(2, None).is_some() && dc.get_frame(3, None).is_some()); + // b.png gets a new time only. + let t = std::fs::metadata(&b).unwrap().modified().unwrap() + std::time::Duration::from_secs(7); + std::fs::File::options().append(true).open(&b).unwrap().set_modified(t).unwrap(); + assert!(dc.get_frame(3, None).is_none() && !dc.contains(Kind::Frame, 3)); + assert!(dc.get_frame(2, None).is_none(), "b.png is listed there too"); + // A missing file that is back. + dc.put_frame_now(4, 1, 1, &[4; 4], &read(&[&gone])); + assert!(dc.get_frame(4, None).is_some(), "missing, still missing"); + file("gone.png", b"g"); + assert!(dc.get_frame(4, None).is_none() && !dc.contains(Kind::Frame, 4), "back"); + // A file replaced by a folder (unreadable). + let c = file("c.png", b"cc"); + dc.put_frame_now(5, 1, 1, &[5; 4], &read(&[&c])); + std::fs::remove_file(&c).unwrap(); + std::fs::create_dir(&c).unwrap(); + assert!(dc.get_frame(5, None).is_none() && !dc.contains(Kind::Frame, 5)); + // Written again after the miss: a hit with the new identity. + let fresh = file("d.png", b"dd"); + dc.put_frame_now(1, 1, 1, &[9; 4], &read(&[&fresh])); + assert_eq!(dc.get_frame(1, None).map(|f| f.0.rgba), Some(vec![9; 4])); + // The format before #3184 (no files): a miss, deleted. + dc.write_format(Kind::Frame, 6, FORMAT_RGBA8, &encode_frame(1, 1, &[6; 4])); + assert!(dc.contains(Kind::Frame, 6)); + assert!(dc.get_frame(6, None).is_none() && !dc.contains(Kind::Frame, 6)); + // A valid checksum over a damaged files section: a miss, deleted. + let mut payload = vec![]; + payload.extend_from_slice(&3u32.to_le_bytes()); + payload.extend_from_slice(&encode_frame(1, 1, &[7; 4])); + dc.write_format(Kind::Frame, 7, FORMAT_RGBA8_FILES, &payload); + assert!(dc.get_frame(7, None).is_none() && !dc.contains(Kind::Frame, 7)); + // Frames whose files can't all be named are not stored. + let mut incomplete = read(&[&fresh]); + incomplete.mark_incomplete(); + assert!(!dc.put_frame(8, 1, 1, &[8; 4], &incomplete, None, 0)); + dc.flush(); + assert!(!dc.contains(Kind::Frame, 8)); + let _ = std::fs::remove_dir_all(&dir); + } + + /// Reload Footage drops the entries made of the reloaded files (#3184): a file replaced by + /// one with the same size and modification time passes the check on hit, so only the drop + /// makes the next use a miss. Entries of other files stay; an entry written after the drop + /// is used; a layer entry and one written while the drop waits in the queue are dropped too; + /// the drop survives a restart (the files are gone). + #[test] + fn dropped_files_take_their_entries_with_them() { + use crate::reads::{FileStamp, ReadFile}; + let dir = tmpdir("drop"); + let src = dir.join("src"); + std::fs::create_dir_all(&src).unwrap(); + let file = |name: &str, bytes: &[u8]| { + let p = src.join(name); + std::fs::write(&p, bytes).unwrap(); + p.to_string_lossy().to_string() + }; + let (a, b) = (file("a.png", b"aa"), file("b.png", b"bb")); + let read = |paths: &[&str]| -> ReadFiles { paths.iter().map(|p| ReadFile::new(*p, FileStamp::native(p).unwrap())).collect() }; + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + dc.put_frame_now(1, 1, 1, &[1; 4], &read(&[&a])); + dc.put_frame_now(2, 1, 1, &[2; 4], &read(&[&b])); + dc.put_frame_now(3, 1, 1, &[3; 4], &read(&[&a, &b])); + dc.put_layer_now(4, &buf(4, 2, 2), &read(&[&a])); + // a.png gets other bytes of the same size and its old time back: the check can't tell. + let t = std::fs::metadata(&a).unwrap().modified().unwrap(); + std::fs::write(&a, b"AA").unwrap(); + std::fs::File::options().append(true).open(&a).unwrap().set_modified(t).unwrap(); + assert!(dc.get_frame(1, None).is_some(), "same size and time: still a hit"); + // A frame queued (not written yet) when the drop comes is dropped too. + assert!(dc.put_frame(5, 1, 1, &[5; 4], &read(&[&a]), None, 0)); + dc.drop_files([a.clone()]); + dc.flush(); + assert!(!dc.is_dropping()); + for (kind, key) in [(Kind::Frame, 1), (Kind::Frame, 3), (Kind::Layer, 4), (Kind::Frame, 5)] { + assert!(!dc.contains(kind, key), "{kind:?} {key} made of a.png is gone"); + } + assert!(dc.get_frame(1, None).is_none() && dc.get_layer(4, None).is_none()); + assert_eq!(dc.get_frame(2, None).map(|f| f.0.rgba), Some(vec![2; 4]), "b.png's frame stays"); + // Written again from the reloaded file: used. + dc.put_frame_now(1, 1, 1, &[9; 4], &read(&[&a])); + assert_eq!(dc.get_frame(1, None).map(|f| f.0.rgba), Some(vec![9; 4])); + // The drop is for good: a new session on the folder finds what is left. + drop(dc); + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + assert_eq!(dc.keys(Kind::Frame).into_iter().collect::>(), [1, 2].into()); + assert!(dc.keys(Kind::Layer).is_empty()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A render under way when Reload Footage drops its files offers its entries after the drop + /// (#3184), when the writer has already deleted the old ones: made of the files as they were, + /// they are refused, frames and layer buffers alike. What the same render made of other files + /// is stored, and so is what a render that started after the drop makes. + #[test] + fn entries_made_before_a_drop_are_refused_after_it() { + use crate::reads::{FileStamp, ReadFile}; + let dir = tmpdir("drop-in-flight"); + std::fs::create_dir_all(&dir).unwrap(); + let file = |name: &str| { + let p = dir.join(name); + std::fs::write(&p, name.as_bytes()).unwrap(); + p.to_string_lossy().to_string() + }; + let (a, b) = (file("a.png"), file("b.png")); + let read = |paths: &[&str]| -> ReadFiles { paths.iter().map(|p| ReadFile::new(*p, FileStamp::native(p).unwrap())).collect() }; + let dc = DiskCache::open(dir.join("cache"), 1 << 30).unwrap(); + // The render starts (and reads a.png and b.png), then a.png is reloaded. + let before = dc.drop_generation(); + dc.drop_files([a.clone()]); + dc.flush(); + assert!(!dc.is_dropping()); + assert!(dc.dropped_since(&read(&[&a]), before) && !dc.dropped_since(&read(&[&b]), before)); + assert!(!dc.put_frame(1, 1, 1, &[1; 4], &read(&[&a, &b]), None, before), "a frame from before the drop"); + assert!(!dc.put_frame_f16(2, 1, 1, &[1; 4], &read(&[&a]), None, before)); + dc.put_layer(3, &buf(3, 2, 2), &EntryFiles::of(&read(&[&a]), None).unwrap(), before); + assert!(dc.put_frame(4, 1, 1, &[4; 4], &read(&[&b]), None, before), "b.png wasn't dropped"); + dc.flush(); + assert!(!dc.contains(Kind::Frame, 1) && !dc.contains(Kind::Frame, 2) && !dc.contains(Kind::Layer, 3)); + assert!(dc.get_frame(1, None).is_none() && dc.get_layer(3, None).is_none()); + assert_eq!(dc.get_frame(4, None).map(|f| f.0.rgba), Some(vec![4; 4])); + // A render that starts after the drop reads the reloaded file: stored and served. + let after = dc.drop_generation(); + assert!(after > before && !dc.dropped_since(&read(&[&a]), after)); + assert!(dc.put_frame(5, 1, 1, &[5; 4], &read(&[&a]), None, after)); + dc.put_layer(6, &buf(6, 2, 2), &EntryFiles::of(&read(&[&a]), None).unwrap(), after); + dc.flush(); + assert_eq!(dc.get_frame(5, None).map(|f| f.0.rgba), Some(vec![5; 4])); + assert!(dc.get_layer(6, None).is_some()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The record of dropped files is bounded: past it, everything made before the drop that + /// forgot the record is refused, and what is made after it is stored. + #[test] + fn the_record_of_dropped_files_is_bounded() { + let mut d = Drops::default(); + let a: Arc = "/a.png".into(); + d.record(std::slice::from_ref(&a)); + assert!(d.stale([&a].into_iter(), 0) && !d.stale([&a].into_iter(), 1)); + let many: Vec> = (0..=MAX_DROPPED_PATHS).map(|i| format!("/f{i}").into()).collect(); + d.record(&many); + assert!(d.last.is_empty(), "forgotten, not grown"); + let other: Arc = "/other.png".into(); + assert!(d.stale([&other].into_iter(), 1), "made before the drop that forgot the record"); + assert!(!d.stale([&a].into_iter(), 2), "made after it"); + } + + /// While a drop waits, an entry made of a dropped file is a miss (and deleted), and frames + /// aren't linked to new keys. + #[test] + fn entries_of_a_pending_drop_are_misses() { + use crate::reads::{FileStamp, ReadFile}; + let dir = tmpdir("drop-pending"); + std::fs::create_dir_all(&dir).unwrap(); + let a = dir.join("a.png"); + std::fs::write(&a, b"aa").unwrap(); + let a = a.to_string_lossy().to_string(); + let files: ReadFiles = [ReadFile::new(a.as_str(), FileStamp::native(&a).unwrap())].into_iter().collect(); + let dc = DiskCache::open(dir.join("cache"), 1 << 30).unwrap(); + dc.put_frame_now(1, 1, 1, &[1; 4], &files); + dc.dropping.lock().unwrap().insert(a.as_str().into(), 1); + assert!(!dc.link_frame(1, 2)); + assert!(dc.get_frame(1, None).is_none() && !dc.contains(Kind::Frame, 1)); + let _ = std::fs::remove_dir_all(&dir); + } + + /// An entry read while its file's drop was pending is a miss even when the writer ends the + /// sweep between the read and the check (it deleted the file, but its bytes were read), and + /// so is one read before a drop that starts meanwhile. Found under load: a stale hit. + #[test] + fn a_sweep_ending_during_a_read_never_makes_a_hit() { + use crate::reads::{FileStamp, ReadFile}; + let dir = tmpdir("drop-race"); + let dc = DiskCache::open(dir.join("cache"), 1 << 30).unwrap(); + let files = EntryFiles::of(&[ReadFile::new("/a.png", FileStamp::Missing)].into_iter().collect(), None).unwrap(); + // Read while the drop is pending; the sweep ends before the check. + dc.dropping.lock().unwrap().insert("/a.png".into(), 1); + let drops = dc.drops_now(); + dc.dropping.lock().unwrap().clear(); + assert!(dc.made_of_dropped(&files, &drops)); + // Read before a drop that starts (and ends) before the check. + let drops = dc.drops_now(); + dc.drop_files(["/a.png"]); + dc.flush(); + assert!(dc.made_of_dropped(&files, &drops)); + assert!(!dc.made_of_dropped(&files, &dc.drops_now()), "read after the drop: used"); + let _ = std::fs::remove_dir_all(&dir); } } diff --git a/crates/render/src/disk_cache/entry_files.rs b/crates/render/src/disk_cache/entry_files.rs new file mode 100644 index 000000000..a36ca1033 --- /dev/null +++ b/crates/render/src/disk_cache/entry_files.rs @@ -0,0 +1,276 @@ +//! The source files a frame entry on disk was made of (#3184): stored with the pixels, checked +//! when the entry is used. Only the files that frame read are looked at, never every file of the +//! project. +//! +//! Layout (little endian), before the frame's pixel payload: +//! `u32 count`, then per file `u32 path length`, the path (UTF-8), a tag byte and its fields: +//! `0` a file on disk (`u64 size`, `u8 has time`, `i128 time`: nanoseconds since the Unix epoch, +//! negative before it), `1` bytes from the browser's file table (`u64 size`, `u64 content +//! hash`), `2` a file that could not be read. + +use std::sync::Arc; + +use super::VirtualFileId; +use crate::reads::{FileStamp, MAX_READ_FILES, ReadFile, ReadFiles}; + +/// Longest path an entry stores (bytes): a longer one is not a path this app opened. +const MAX_PATH_BYTES: usize = 1 << 16; + +/// A file's identity as a frame entry keeps it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum EntryStamp { + /// Size and modification time (nanoseconds since the Unix epoch, negative before it) on + /// disk. + Native { len: u64, modified: Option }, + /// Size and content hash from the [`VirtualFileId`] hook (the browser's file table). + Virtual { len: u64, hash: u64 }, + /// It could not be read when the frame was made: it still must not be readable. + Missing, +} + +/// One file of a frame entry. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct EntryFile { + pub path: Arc, + pub stamp: EntryStamp, +} + +/// Every file a frame entry was made of. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct EntryFiles(Vec); + +impl EntryFiles { + /// The list to store for a frame that read `files`: `None` when the frame can't be checked + /// against its files later (the list is incomplete, or a file from memory has no identity), + /// so the frame must not be stored. Native stamps are the ones the files were read with, + /// never a fresh look at the disk (a file changed during the render keeps the old one). + pub fn of(files: &ReadFiles, virtual_id: Option>) -> Option { + if !files.is_complete() { + return None; + } + let mut out = Vec::with_capacity(files.len()); + for f in files.files() { + let stamp = match f.stamp { + FileStamp::Native { len, modified } => EntryStamp::Native { len, modified }, + FileStamp::Virtual => { + let (len, hash) = virtual_id.and_then(|id| id(&f.path))?; + EntryStamp::Virtual { len, hash } + } + FileStamp::Missing => EntryStamp::Missing, + }; + out.push(EntryFile { path: f.path.clone(), stamp }); + } + Some(EntryFiles(out)) + } + + /// The listed files. + pub fn files(&self) -> &[EntryFile] { + &self.0 + } + + /// The number of listed files. + pub fn len(&self) -> usize { + self.0.len() + } + + /// Whether no files are listed. + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + /// The files as a render reports them (to keep with the frame in memory). + pub fn to_read_files(&self) -> ReadFiles { + self.0 + .iter() + .map(|f| { + let stamp = match f.stamp { + EntryStamp::Native { len, modified } => FileStamp::Native { len, modified }, + EntryStamp::Virtual { .. } => FileStamp::Virtual, + EntryStamp::Missing => FileStamp::Missing, + }; + ReadFile::new(f.path.clone(), stamp) + }) + .collect() + } + + /// Whether every file still is what the frame was made of: one `metadata` call per file + /// (and the hook for files from memory). + pub fn unchanged(&self, virtual_id: Option>) -> bool { + self.0.iter().all(|f| file_unchanged(f, virtual_id)) + } + + /// Appends the list to `out` in little-endian binary form. + pub(super) fn encode(&self, out: &mut Vec) { + out.extend_from_slice(&(self.0.len() as u32).to_le_bytes()); + for f in &self.0 { + out.extend_from_slice(&(f.path.len() as u32).to_le_bytes()); + out.extend_from_slice(f.path.as_bytes()); + match f.stamp { + EntryStamp::Native { len, modified } => { + out.push(0); + out.extend_from_slice(&len.to_le_bytes()); + out.push(u8::from(modified.is_some())); + out.extend_from_slice(&modified.unwrap_or(0).to_le_bytes()); + } + EntryStamp::Virtual { len, hash } => { + out.push(1); + out.extend_from_slice(&len.to_le_bytes()); + out.extend_from_slice(&hash.to_le_bytes()); + } + EntryStamp::Missing => out.push(2), + } + } + } + + /// The list at the start of `p` and what follows it; `None` when it is damaged. + pub(super) fn decode(p: &[u8]) -> Option<(EntryFiles, &[u8])> { + let mut r = Reader(p); + let n = r.u32()? as usize; + if n > MAX_READ_FILES { + return None; + } + let mut out = Vec::with_capacity(n); + for _ in 0..n { + let len = r.u32()? as usize; + if len > MAX_PATH_BYTES { + return None; + } + let path: Arc = std::str::from_utf8(r.take(len)?).ok()?.into(); + let stamp = match r.u8()? { + 0 => { + let len = r.u64()?; + let has = r.u8()?; + let t = r.i128()?; + match has { + 0 => EntryStamp::Native { len, modified: None }, + 1 => EntryStamp::Native { len, modified: Some(t) }, + _ => return None, + } + } + 1 => EntryStamp::Virtual { len: r.u64()?, hash: r.u64()? }, + 2 => EntryStamp::Missing, + _ => return None, + }; + out.push(EntryFile { path, stamp }); + } + Some((EntryFiles(out), r.0)) + } +} + +/// Whether the file still matches its stored stamp, by its native stamp or by its in-memory identity. +fn file_unchanged(f: &EntryFile, virtual_id: Option>) -> bool { + let from_memory = || virtual_id.and_then(|id| id(&f.path)); + match f.stamp { + EntryStamp::Native { len, modified } => FileStamp::native(&f.path) == Some(FileStamp::Native { len, modified }), + EntryStamp::Virtual { len, hash } => from_memory() == Some((len, hash)), + // (a folder at the path is still not a file to read) + EntryStamp::Missing => !std::fs::metadata(&*f.path).is_ok_and(|m| m.is_file()) && from_memory().is_none(), + } +} + +/// Reads little-endian fields off the front of a byte slice. +struct Reader<'a>(&'a [u8]); + +impl<'a> Reader<'a> { + /// Takes the next `n` bytes, or `None` when fewer remain. + fn take(&mut self, n: usize) -> Option<&'a [u8]> { + let head = self.0.get(..n)?; + self.0 = self.0.get(n..)?; + Some(head) + } + /// Reads one byte. + fn u8(&mut self) -> Option { + self.take(1)?.first().copied() + } + /// Reads a little-endian `u32`. + fn u32(&mut self) -> Option { + Some(u32::from_le_bytes(self.take(4)?.try_into().ok()?)) + } + /// Reads a little-endian `u64`. + fn u64(&mut self) -> Option { + Some(u64::from_le_bytes(self.take(8)?.try_into().ok()?)) + } + /// Reads a little-endian `i128`. + fn i128(&mut self) -> Option { + Some(i128::from_le_bytes(self.take(16)?.try_into().ok()?)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A sample list of native, missing and virtual files for the tests. + fn files() -> ReadFiles { + [ + ReadFile::new("/a.png", FileStamp::Native { len: 3, modified: Some(9) }), + ReadFile::new("/b.png", FileStamp::Native { len: 4, modified: None }), + // Before 1970, and past where `u64` nanoseconds end (2554). + ReadFile::new("/d.png", FileStamp::Native { len: 5, modified: Some(-86_400_000_000_000) }), + ReadFile::new("/e.png", FileStamp::Native { len: 6, modified: Some(i128::from(u64::MAX) + 1) }), + ReadFile::new("/c.png", FileStamp::Missing), + ReadFile::new("/v.mov", FileStamp::Virtual), + ] + .into_iter() + .collect() + } + + #[test] + fn lists_round_trip_and_damage_is_caught() { + let id = |p: &str| (p == "/v.mov").then_some((8, 77)); + let list = EntryFiles::of(&files(), Some(&id)).unwrap(); + assert_eq!(list.files()[5].stamp, EntryStamp::Virtual { len: 8, hash: 77 }); + let mut bytes = vec![]; + list.encode(&mut bytes); + bytes.extend_from_slice(b"rest"); + let (back, rest) = EntryFiles::decode(&bytes).unwrap(); + assert_eq!((back, rest), (list.clone(), &b"rest"[..])); + assert_eq!(list.to_read_files(), files()); + // Every truncation is caught (never a panic, never a shorter list). + let mut plain = vec![]; + list.encode(&mut plain); + for n in 0..plain.len() { + assert!(EntryFiles::decode(&plain[..n]).is_none(), "cut at {n}"); + } + // Bad tags and counts. + let mut bad = plain.clone(); + bad[0..4].copy_from_slice(&u32::MAX.to_le_bytes()); + assert!(EntryFiles::decode(&bad).is_none()); + } + + #[test] + fn frames_that_cant_be_checked_are_not_stored() { + let mut incomplete = files(); + incomplete.mark_incomplete(); + let id = |_: &str| Some((1, 1)); + assert!(EntryFiles::of(&incomplete, Some(&id)).is_none(), "an incomplete list"); + assert!(EntryFiles::of(&files(), None).is_none(), "a file from memory without the hook"); + assert!(EntryFiles::of(&ReadFiles::default(), None).is_some_and(|l| l.is_empty()), "a frame that reads no file"); + } + + #[test] + fn the_check_looks_at_the_listed_files() { + let dir = std::env::temp_dir().join(format!("effectcraft-entry-files-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let p = dir.join("f.bin"); + std::fs::write(&p, b"abc").unwrap(); + let path = p.to_string_lossy().to_string(); + let gone = dir.join("gone.bin").to_string_lossy().to_string(); + let stamp = FileStamp::native(&path).unwrap(); + let read: ReadFiles = [ReadFile::new(path.as_str(), stamp), ReadFile::new(gone.as_str(), FileStamp::Missing)].into_iter().collect(); + let list = EntryFiles::of(&read, None).unwrap(); + assert!(list.unchanged(None)); + std::fs::write(&p, b"abcd").unwrap(); + assert!(!list.unchanged(None), "a new size"); + std::fs::write(&gone, b"x").unwrap(); + let list = EntryFiles::of(&[ReadFile::new(gone.as_str(), FileStamp::Missing)].into_iter().collect(), None).unwrap(); + assert!(!list.unchanged(None), "a missing file that is back"); + let v: ReadFiles = [ReadFile::new("/v", FileStamp::Virtual)].into_iter().collect(); + let one = |_: &str| Some((1, 1)); + let two = |_: &str| Some((1, 2)); + let list = EntryFiles::of(&v, Some(&one)).unwrap(); + assert!(list.unchanged(Some(&one)) && !list.unchanged(Some(&two)) && !list.unchanged(None)); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/crates/render/src/eval.rs b/crates/render/src/eval.rs index 0a10afa45..41e6132d3 100644 --- a/crates/render/src/eval.rs +++ b/crates/render/src/eval.rs @@ -44,11 +44,15 @@ pub struct EvalCtx<'a> { /// Media Replacement values overridden by the Essential Properties of a collapsed precomp /// instance. pub media_overrides: &'a [crate::MediaOverride], + /// Where the source files of what expressions render (`sampleImage`) are noted: the + /// renderer's [`Renderer::reads`](crate::Renderer::reads) while rendering. + pub reads: Option<&'a crate::reads::ReadLog>, } impl<'a> EvalCtx<'a> { + /// A context for `comp` at `time` with no expressions, footage, overrides or read log. pub fn new(project: &'a Project, comp_id: ItemId, comp: &'a Comp, time: Tick) -> EvalCtx<'a> { - EvalCtx { project, comp_id, comp, time, expr: None, footage: None, property_overrides: &[], media_overrides: &[] } + EvalCtx { project, comp_id, comp, time, expr: None, footage: None, property_overrides: &[], media_overrides: &[], reads: None } } pub fn at(&self, time: Tick) -> EvalCtx<'a> { EvalCtx { time, ..*self } diff --git a/crates/render/src/footage.rs b/crates/render/src/footage.rs index 4c90967d3..0e17f152d 100644 --- a/crates/render/src/footage.rs +++ b/crates/render/src/footage.rs @@ -1,4 +1,5 @@ use super::{Arc, AtomicBool, Footage, Image, ItemId, ItemKind, Ordering, Tick}; +use crate::reads::ReadLog; /// How a [`FootageSource`] hands over float footage (OpenEXR, float TIFF), whose values are /// linear light: encoded with a curve, as the compositor takes footage, or as stored. Other @@ -92,6 +93,15 @@ pub trait FootageSource: Send + Sync { fn called_off(&self) -> bool { false } + /// Run `f`, which reads from this source on this thread, noting in `reads` every source file + /// those reads use, with its identity as read (see [`crate::reads`]): the file a frame was + /// decoded from, also when it comes from the source's own memory cache (without reading the + /// file again). Sources that can't tell which files they read keep the default, which marks + /// `reads` incomplete. + fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) { + reads.mark_incomplete(); + f() + } /// Why `footage` shows no picture although its file is there: a read of it failed because /// no decoder handles its codec, profile, chroma format or bit depth, or the stream is damaged /// (e.g. "H.264 (High 4:2:2) isn't supported by the built-in decoder: ..."). `None` while it @@ -194,6 +204,10 @@ impl FootageSource for CancellableFootage<'_> { fn called_off(&self) -> bool { self.cancel.load(Ordering::Relaxed) || self.inner.called_off() } + /// Runs `f` with the inner source, collecting the files it reads into `reads`. + fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) { + self.inner.recording(reads, f) + } fn problem(&self, footage: &Footage) -> Option { self.inner.problem(footage) } @@ -211,7 +225,12 @@ pub fn is_vector_footage(f: &Footage) -> bool { pub struct NoFootage; impl FootageSource for NoFootage { + /// Always `None`: there is no footage. fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { None } + /// Reads no files. + fn recording(&self, _: &ReadLog, f: &mut dyn FnMut()) { + f() + } } diff --git a/crates/render/src/fx_host.rs b/crates/render/src/fx_host.rs index 65bafe002..3a6f1a34c 100644 --- a/crates/render/src/fx_host.rs +++ b/crates/render/src/fx_host.rs @@ -95,6 +95,7 @@ impl EffectHost for FxHost<'_, '_, '_> { Some(self.pixels(other, buf, false)) } + /// The audio of footage layer `id` from `start`, or `None` when the layer is not footage. fn audio(&self, id: u64, start: f64, frames: usize, rate: u32) -> Option> { let other = self.ctx.layer(effectcraft_project::LayerId(id))?; let LayerSource::Footage { item } = &other.source else { return None }; @@ -104,7 +105,7 @@ impl EffectHost for FxHost<'_, '_, '_> { return None; } let st = other.layer_time(Tick::from_seconds_f64(start)); - self.r.footage.audio(item, f, st, frames, rate) + self.r.read(|| self.r.footage.audio(item, f, st, frames, rate)) } fn self_at(&self, layer_time: f64, effects: usize) -> Option { diff --git a/crates/render/src/layers.rs b/crates/render/src/layers.rs index d105c6f13..df976a857 100644 --- a/crates/render/src/layers.rs +++ b/crates/render/src/layers.rs @@ -34,21 +34,43 @@ impl Shutter { } } +/// `files` (a content's) with the files of the buffers made of it that the layer cache kept: +/// `files` itself when they are all its own. +fn with_kept_files(files: Arc, kept: &[Arc]) -> Arc { + if kept.iter().all(|f| Arc::ptr_eq(f, &files)) { + return files; + } + let mut all = (*files).clone(); + kept.iter().for_each(|f| all.extend(f)); + Arc::new(all) +} + impl<'a> Renderer<'a> { /// The layer with its styles: exterior passes and body (cached per pass). pub(crate) fn styled(&self, ctx: &EvalCtx, layer: &Layer, mut timing: Option<&mut LayerTiming>) -> Option { - let (content, ckey) = self.content_buf_timed(ctx, layer, timing.as_deref_mut())?; + let (content, ckey, files) = self.content_buf_timed(ctx, layer, timing.as_deref_mut())?; if !styles::active(ctx, layer) { - return Some(StyledLayer { passes: vec![], body: content.clone(), content, key: ckey, ckey, plain: true }); + return Some(StyledLayer { passes: vec![], body: content.clone(), content, key: ckey, ckey, plain: true, files }); } let modes = styles::pass_modes(ctx, layer); let key = ckey.map(|k| cache::styles_key(ctx, layer, k)); - if let (Some(c), Some(k)) = (self.cache, key) - && let Some(body) = c.get(k) - { - let passes: Option> = modes.iter().enumerate().map(|(i, m)| c.get(cache::derive(k, i as u64 + 1)).map(|b| (b, *m))).collect(); + // (a cached body or pass may have been made of other files than the content: another + // worker's, or one from disk; the styled layer lists the files of every buffer it holds) + if let Some((body, body_files)) = self.cached(key) { + let mut kept_files = vec![body_files]; + let passes: Option> = modes + .iter() + .enumerate() + .map(|(i, m)| { + self.cached(key.map(|k| cache::derive(k, i as u64 + 1))).map(|(b, f)| { + kept_files.push(f); + (b, *m) + }) + }) + .collect(); if let Some(passes) = passes { - return Some(StyledLayer { passes, body, content, key, ckey, plain: false }); + let files = with_kept_files(files, &kept_files); + return Some(StyledLayer { passes, body, content, key, ckey, plain: false, files }); } } let t0 = web_time::Instant::now(); @@ -59,16 +81,23 @@ impl<'a> Renderer<'a> { t.cached = false; t.effects.push(("layerStyles".into(), t0.elapsed().as_secs_f64() * 1e3)); } - let mut body = Arc::new(st.body); - let mut passes: Vec<_> = st.passes.into_iter().map(|(b, m)| (Arc::new(b), m)).collect(); - // The buffers the cache keeps (another worker's, if it kept them first). - if let (Some(c), Some(k)) = (self.cache_to_keep(), key) { - body = c.insert(k, body); - for (i, (b, _)) in passes.iter_mut().enumerate() { - *b = c.insert(cache::derive(k, i as u64 + 1), b.clone()); - } - } - Some(StyledLayer { passes, body, content, key, ckey, plain: false }) + // The buffers the cache keeps (another worker's, if it kept them first), made of the + // content's files; the styled layer then lists the files of every buffer it holds. + let mut kept_files = vec![]; + let (body, f) = self.keep(key, Arc::new(st.body), &files); + kept_files.push(f); + let passes: Vec<_> = st + .passes + .into_iter() + .enumerate() + .map(|(i, (b, m))| { + let (b, f) = self.keep(key.map(|k| cache::derive(k, i as u64 + 1)), Arc::new(b), &files); + kept_files.push(f); + (b, m) + }) + .collect(); + let files = with_kept_files(files, &kept_files); + Some(StyledLayer { passes, body, content, key, ckey, plain: false, files }) } /// The styled layer's buffers in the blending space. @@ -76,9 +105,10 @@ impl<'a> Renderer<'a> { if self.pipe.to_blend().is_none() { return st; } - let body = self.to_blend(st.body, st.key); - let content = if st.plain { body.clone() } else { self.to_blend(st.content, st.ckey) }; - let passes = st.passes.into_iter().enumerate().map(|(i, (b, m))| (self.to_blend(b, st.key.map(|k| cache::derive(k, i as u64 + 1))), m)).collect(); + let body = self.to_blend(st.body, st.key, &st.files); + let content = if st.plain { body.clone() } else { self.to_blend(st.content, st.ckey, &st.files) }; + let passes = + st.passes.into_iter().enumerate().map(|(i, (b, m))| (self.to_blend(b, st.key.map(|k| cache::derive(k, i as u64 + 1)), &st.files), m)).collect(); StyledLayer { passes, body, content, ..st } } @@ -236,7 +266,11 @@ impl<'a> Renderer<'a> { /// A processed-layer cache key that also covers content motion blur (the averaged source /// depends on the content at every sub-sample). pub(crate) fn content_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option) -> Option { - let k = key?; + let k = self.mask_gate_key(ctx, layer, key)?; + // A layer that reads other layers through its effects' layer parameters gets them drawn + // by this renderer: the switches it passes down (Switches Affect Nested Comps, the + // precomp layers above, motion blur on) change those pixels, not just the layers' own. + let k = if cache::reads_layers(ctx, layer) { cache::derive(k, self.nested_look()) } else { k }; let Some(times) = self.content_times(ctx, layer) else { return Some(k) }; times.iter().try_fold(cache::derive(k, 0x6d62_6c72), |d, t| Some(cache::derive(d, cache::layer_key(&ctx.at(*t), layer, 1.0, false, false)?))) } @@ -257,6 +291,15 @@ impl<'a> Renderer<'a> { } } + /// A cache key of a masked layer's buffer, told apart when mask motion blur can't run at all + /// in this render ([`Self::mask_blur`]'s gate: the render options, the precomp layers above). + /// The key has the layer's own motion blur state, but a mask set to On blurs without it. + pub(crate) fn mask_gate_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option) -> Option { + let k = key?; + let masked = layer.masks().is_some_and(|m| !m.children.is_empty()); + Some(if masked && !self.mask_blur(ctx, layer).allowed { cache::derive(k, 0x6d62_6f66) } else { k }) + } + /// Motion-blur sub-samples for a layer buffer (1 = no motion blur). As in After Effects, a 2D /// layer gets about one sample per output pixel its corners travel during the shutter, /// at least Samples Per Frame and at most the comp's Adaptive Sample Limit; a layer that @@ -582,6 +625,8 @@ pub struct StyledLayer { ckey: Option, /// No layer styles: composite `body` the usual way. pub plain: bool, + /// The source files the layer's buffers were made of (see [`crate::reads`]). + pub(crate) files: Arc, } /// Where a layer buffer lands (see [`Renderer::placement`]). diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index dbd948320..e09794de2 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -17,6 +17,7 @@ use color::Region; pub mod eval; pub mod masks; pub mod passes; +pub mod reads; pub mod shapes; pub mod styles; pub mod text; @@ -69,6 +70,10 @@ pub struct Renderer<'a> { pub opts: RenderOpts, /// Processed-layer cache shared across frames (see [`cache`]). `None` renders everything. pub cache: Option<&'a LayerCache>, + /// Where the source files this render reads are noted (see [`reads`]): the frame's own, + /// from the footage source and from the buffers the layer cache serves. `None` records + /// nothing for the frame (buffers kept in the layer cache still record theirs). + pub reads: Option<&'a reads::ReadLog>, /// Optional per-layer timing sink (`frame --bench` / perf readouts). pub profile: Option<&'a std::sync::Mutex>>, /// GPU backend used when [`RenderOpts::backend`] asks for it. diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index e84348fdf..6c79ba7bb 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -3,6 +3,7 @@ use super::footage::{FootageSource, is_vector_footage}; use super::frames::{blend_frames, cineon_frame, content_bounds, field_frame, frame_blend_mode, frame_position, frame_size, interpret_pixels, roi_inside}; use super::fx_host::{FxHost, essential_overrides}; use super::opts::{Collapse3d, LayerTiming, MAX_THROUGH, RenderOpts, Through}; +use super::reads::{ReadFiles, ReadLog}; use super::{ Arc, AutoKey, AutoPick, BlendMode, Buf, Comp, EffectCtx, EffectEnv, EvalCtx, Footage, FootageKind, FrameBlend, FrameRate, GroupKind, Image, ItemId, ItemKind, Layer, LayerCache, LayerSource, Params, Project, Quality, Region, Renderer, Tick, cache, color, masks, shapes, styles, text, three_d, vec2, @@ -101,6 +102,7 @@ fn collapsed_instance(ctx: &EvalCtx, layer: &Layer, item: ItemId) -> Option<(Vec } impl<'a> Renderer<'a> { + /// A renderer over `project` and `footage` with the given options. pub fn new(project: &'a Project, footage: &'a dyn FootageSource, opts: RenderOpts) -> Renderer<'a> { Renderer { project, @@ -108,6 +110,7 @@ impl<'a> Renderer<'a> { expr: None, opts, cache: None, + reads: None, profile: None, accel: None, depth: 0, @@ -129,6 +132,67 @@ impl<'a> Renderer<'a> { self.cache.filter(|_| !self.footage.called_off()) } + /// `read`, a read of this render's footage source, with the files it uses noted in + /// [`Renderer::reads`] (see [`FootageSource::recording`]). + pub(crate) fn read(&self, read: impl FnOnce() -> Option) -> Option { + let Some(log) = self.reads else { return read() }; + let (mut read, mut out) = (Some(read), None); + self.footage.recording(log, &mut || out = read.take().and_then(|f| f())); + out + } + + /// `ctx` noting what it reads (fonts, `sampleImage`) in this render's [`Renderer::reads`]: + /// a context made by an outer render carries that render's log, not the one of the buffer + /// [`Renderer::recorded`] makes. + pub(crate) fn with_reads<'c>(&'c self, ctx: &EvalCtx<'c>) -> EvalCtx<'c> { + EvalCtx { reads: self.reads, ..*ctx } + } + + /// The buffer the layer cache keeps under `key`, with the files it was made of noted in + /// [`Renderer::reads`]. + pub(crate) fn cached(&self, key: Option) -> Option<(Arc, Arc)> { + let (buf, files) = self.cache?.get(key?)?; + if let Some(log) = self.reads { + log.note_all(&files); + } + Some((buf, files)) + } + + /// Keep `buf`, made of `files`, under `key` (see [`Renderer::recorded`]): the buffer the cache + /// keeps (another worker's, if it kept one first), or `buf` when nothing is kept, with the + /// files that buffer was made of. Another worker's files are noted in [`Renderer::reads`] + /// too: its pixels are the ones this render uses (a file that changed between the two reads + /// then has two identities, and the list is incomplete). + pub(crate) fn keep(&self, key: Option, buf: Arc, files: &Arc) -> (Arc, Arc) { + let (Some(c), Some(k)) = (self.cache_to_keep(), key) else { return (buf, files.clone()) }; + let (kept, kept_files) = c.insert(k, buf, files.clone()); + if !Arc::ptr_eq(&kept_files, files) + && let Some(log) = self.reads + { + log.note_all(&kept_files); + } + (kept, kept_files) + } + + /// Run `make`, which makes a buffer to keep under `key` in the layer cache, on this renderer + /// with its reads noted apart: what `make` returns and the files it read (from the footage + /// source, or the files of cached buffers it used), which go to [`Renderer::reads`] too. A + /// cached buffer then reports the files it was made of whichever render uses it. Without a + /// cache or a key, `make` runs on this renderer as it is (no files: nothing is kept). + pub(crate) fn recorded(&self, key: Option, make: impl FnOnce(&Renderer<'_>) -> T) -> (T, Arc) { + if key.is_none() || self.cache.is_none() { + return (make(self), Arc::default()); + } + // (the generation before any read: a file dropped while `make` runs makes it stale) + let log = self.cache.and_then(LayerCache::drop_generation).map_or_else(ReadLog::new, ReadLog::starting_at); + let out = make(&Renderer { reads: Some(&log), ..*self }); + let files = log.into_files(); + if let Some(own) = self.reads { + own.note_all(&files); + } + (out, Arc::new(files)) + } + /// A layer's Quality switch, limited by the precomp layers above it when /// [`RenderOpts::nested_switches`] is on. pub fn quality(&self, layer: &Layer) -> Quality { @@ -143,6 +207,24 @@ impl<'a> Renderer<'a> { } } + /// What this renderer passes down to the layers it draws beyond their own switches + /// ([`RenderOpts::nested_switches`], the switches inherited from the precomp layers above, + /// motion blur on): part of the cache key of a buffer that reads other layers. + pub(crate) fn nested_look(&self) -> u64 { + let inherited = match self.inherited { + None => 0, + Some((q, mb)) => { + let q = match q { + Quality::Best => 0, + Quality::Draft => 1, + Quality::Wireframe => 2, + }; + 1 + q * 2 + u64::from(mb) + } + }; + 0x6e65_7374_0000_0000 | inherited << 2 | u64::from(self.opts.nested_switches) << 1 | u64::from(self.opts.motion_blur) + } + /// A layer's Motion Blur switch, limited by the precomp layers above it. pub(crate) fn layer_motion_blur(&self, layer: &Layer) -> bool { layer.switches.motion_blur && self.inherited.is_none_or(|(_, mb)| mb) @@ -366,8 +448,19 @@ impl<'a> Renderer<'a> { } } + /// The evaluation context of `comp` at `t`, carrying this renderer's expressions, footage and read log. pub(crate) fn ctx(&self, comp_id: ItemId, comp: &'a Comp, t: Tick) -> EvalCtx<'a> { - EvalCtx { project: self.project, comp_id, comp, time: t, expr: self.expr, footage: Some(self.footage), property_overrides: &[], media_overrides: &[] } + EvalCtx { + project: self.project, + comp_id, + comp, + time: t, + expr: self.expr, + footage: Some(self.footage), + property_overrides: &[], + media_overrides: &[], + reads: self.reads, + } } /// This renderer with another accelerator (or none): the passes of @@ -760,7 +853,7 @@ impl<'a> Renderer<'a> { // Continuously rasterised vector footage: drawn at the on-screen scale. if layer.switches.collapse && is_vector_footage(f) { let k = self.raster_scale(ctx, layer); - if let Some(img) = self.footage.vector_frame(item, f, k) { + if let Some(img) = self.read(|| self.footage.vector_frame(item, f, k)) { let mut buf = Buf { img: (*img).clone(), offset: [0.0; 2], scale: k }; if let Some(c) = self.pipe.media_in(f) { color::convert(&mut buf.img, &c); @@ -776,7 +869,20 @@ impl<'a> Renderer<'a> { let img = self.footage_frame(ctx, layer, item, pf, lt, size)?; Some(self.footage_buf(&img, pf, f.width, f.height)) } - LayerSource::Text => Some(self.authored(text::render(ctx, layer, self.raster_scale(ctx, layer)))), + // (the font files go to this render's log, which `ctx` may not carry: see + // [`Renderer::with_reads`]) + LayerSource::Text => { + // An Expression Selector evaluates per character: its results at other characters + // than the first are not in the layer's cache key (an amount reading a footage + // item's size by name, `textIndex == 1 ? 0 : footage("ref.png").width`), so the + // buffer is kept in memory only, never on disk (the frame's key covers them). + if cache::per_character_expressions(layer) + && let Some(log) = self.reads + { + log.mark_unkeyed(); + } + Some(self.authored(text::render(&self.with_reads(ctx), layer, self.raster_scale(ctx, layer)))) + } LayerSource::Shape => layer.props.sub("contents").map(|c| self.authored(shapes::render(ctx, layer, c, self.raster_scale(ctx, layer)))), _ => None, } @@ -831,7 +937,7 @@ impl<'a> Renderer<'a> { if f.fields != effectcraft_project::FieldOrder::Off && matches!(f.kind, FootageKind::Video | FootageKind::Sequence) { let field_rate = FrameRate::new(f.frame_rate.num * 2, f.frame_rate.den); let i = field_rate.frame_at(t).max(0); - let img = cineon_frame(self.footage.frame_with(item, f, f.frame_rate.tick_of(i / 2), None, self.pipe.float_curve(f))?, f); + let img = cineon_frame(self.read(|| self.footage.frame_with(item, f, f.frame_rate.tick_of(i / 2), None, self.pipe.float_curve(f)))?, f); let dominant_upper = f.fields == effectcraft_project::FieldOrder::UpperFirst; // Upper field = even lines (0, 2, …). let parity = if (i % 2 == 0) == dominant_upper { 0 } else { 1 }; @@ -844,6 +950,8 @@ impl<'a> Renderer<'a> { if per_pixel { Some(Arc::new(interpret_pixels(&img, f, self.pipe.linear_footage_space(f)))) } else { Some(img) } } + /// The footage frame at source time `t` after frame blending: the average of the source frames one comp frame spans, else a blend of the two neighbouring frames, else the plain frame. + /// Log scans are converted first, and blended frames are cached. fn footage_frame_raw(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option> { let mode = frame_blend_mode(ctx, layer); let float = self.pipe.float_curve(f); @@ -855,6 +963,9 @@ impl<'a> Renderer<'a> { use std::hash::{Hash, Hasher}; let mut h = std::collections::hash_map::DefaultHasher::new(); (0x00f8_a3e5_u64, item, i, (w as f32).to_bits(), mode as u8, f.frame_rate.num, f.frame_rate.den, &f.path, float).hash(&mut h); + // Every file of a sequence: the key must tell two projects apart whose items + // share an id and a first frame (on disk, no project-wide salt does, #3184). + f.sequence.hash(&mut h); (f.alpha as u8).hash(&mut h); if f.alpha == effectcraft_project::AlphaMode::Premultiplied { f.premul_color.map(f32::to_bits).hash(&mut h); @@ -865,24 +976,22 @@ impl<'a> Renderer<'a> { } h.finish() }); - if let (Some(c), Some(k)) = (self.cache, key) - && let Some(b) = c.get(k) - { + if let Some((b, _)) = self.cached(key) { return Some(Arc::new(b.img.clone())); } // Log scans are converted before they blend: the conversion is not linear. - let a = self.footage.frame_with(item, f, f.frame_rate.tick_of(i), None, float).map(|a| cineon_frame(a, f)); - let b = self.footage.frame_with(item, f, f.frame_rate.tick_of(i + 1), None, float).map(|b| cineon_frame(b, f)); + let ((a, b), files) = self.recorded(key, |r| { + let frame = |i| r.read(|| r.footage.frame_with(item, f, f.frame_rate.tick_of(i), None, float)).map(|a| cineon_frame(a, f)); + (frame(i), frame(i + 1)) + }); if let (Some(a), Some(b)) = (a, b) { let img = blend_frames(mode, &a, &b, w as f32); - if let (Some(c), Some(k)) = (self.cache_to_keep(), key) { - c.insert(k, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 })); - } + let _kept = self.keep(key, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 }), &files); return Some(Arc::new(img)); } } } - self.footage.frame_with(item, f, t, size, float).map(|img| cineon_frame(img, f)) + self.read(|| self.footage.frame_with(item, f, t, size, float)).map(|img| cineon_frame(img, f)) } /// Source → masks, clamped/quantised to the project depth. @@ -902,52 +1011,41 @@ impl<'a> Renderer<'a> { /// Fully processed layer buffer (source → masks → effects → layer styles, flattened). /// Served from the layer cache when the layer's content key matches. pub fn layer_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option> { - self.layer_buf_keyed(ctx, layer).map(|(b, _)| b) + self.layer_buf_keyed(ctx, layer).map(|(b, _, _)| b) } /// [`Self::layer_buf`] in the blending space (linear with Blend Colors Using 1.0 Gamma). pub fn blend_layer_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option> { - let (b, k) = self.layer_buf_keyed(ctx, layer)?; - Some(self.to_blend(b, k)) + let (b, k, files) = self.layer_buf_keyed(ctx, layer)?; + Some(self.to_blend(b, k, &files)) } - /// A buffer converted to the blending space (cached under a key derived from `key`). - pub(crate) fn to_blend(&self, b: Arc, key: Option) -> Arc { + /// A buffer converted to the blending space (cached under a key derived from `key`, with + /// `files`, the source files `b` was made of). + pub(crate) fn to_blend(&self, b: Arc, key: Option, files: &Arc) -> Arc { let Some(c) = self.pipe.to_blend() else { return b }; let key = key.map(|k| cache::derive(k, 0x1ea7)); - if let (Some(cache), Some(k)) = (self.cache, key) - && let Some(x) = cache.get(k) - { + if let Some((x, _)) = self.cached(key) { return x; } let mut nb = (*b).clone(); color::convert(&mut nb.img, &c); - let nb = Arc::new(nb); - // The buffer the cache keeps (another worker's, if it kept one first). - match (self.cache_to_keep(), key) { - (Some(cache), Some(k)) => cache.insert(k, nb), - _ => nb, - } + self.keep(key, Arc::new(nb), files).0 } - fn layer_buf_keyed(&self, ctx: &EvalCtx, layer: &Layer) -> Option<(Arc, Option)> { + /// The flattened layer buffer, its cache key and the source files it was made of. + fn layer_buf_keyed(&self, ctx: &EvalCtx, layer: &Layer) -> Option<(Arc, Option, Arc)> { let st = self.styled(ctx, layer, None)?; if st.passes.is_empty() { - return Some((st.body, st.key)); + return Some((st.body, st.key, st.files)); } let key = st.key.map(|k| cache::derive(k, 0xf1a7)); - if let (Some(c), Some(k)) = (self.cache, key) - && let Some(b) = c.get(k) - { - return Some((b, key)); + if let Some((b, files)) = self.cached(key) { + return Some((b, key, files)); } let flat = Arc::new(styles::Styled { passes: st.passes.iter().map(|(b, m)| ((**b).clone(), *m)).collect(), body: (*st.body).clone() }.flatten()); - // The buffer the cache keeps (another worker's, if it kept one first). - let flat = match (self.cache_to_keep(), key) { - (Some(c), Some(k)) => c.insert(k, flat), - _ => flat, - }; - Some((flat, key)) + let (flat, files) = self.keep(key, flat, &st.files); + Some((flat, key, files)) } /// A layer's source pixels at the context time, before masks, effects and the transform @@ -983,28 +1081,32 @@ impl<'a> Renderer<'a> { /// Layer pixels after source → masks → effects (no layer styles). pub fn content_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option> { - self.content_buf_timed(ctx, layer, None).map(|(b, _)| b) + self.content_buf_timed(ctx, layer, None).map(|(b, _, _)| b) } - pub(crate) fn content_buf_timed(&self, ctx: &EvalCtx, layer: &Layer, mut timing: Option<&mut LayerTiming>) -> Option<(Arc, Option)> { + /// [`Self::content_buf`], its cache key and the source files it was made of. + pub(crate) fn content_buf_timed( + &self, + ctx: &EvalCtx, + layer: &Layer, + mut timing: Option<&mut LayerTiming>, + ) -> Option<(Arc, Option, Arc)> { let key = self.cache.and_then(|_| cache::layer_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer))); let key = self.content_key(ctx, layer, key); - if let (Some(c), Some(k)) = (self.cache, key) - && let Some(b) = c.get(k) - { + if let Some((b, files)) = self.cached(key) { if let Some(t) = timing.as_deref_mut() { t.cached = true; } - return Some((b, key)); + return Some((b, key, files)); } - let buf = self.masked_source(ctx, layer)?; - let buf = Arc::new(self.apply_effects_timed(ctx, layer, buf, false, usize::MAX, timing.map(|t| &mut t.effects))); - // The buffer the cache keeps (another worker's, if it kept one first). - let buf = match (self.cache_to_keep(), key) { - (Some(c), Some(k)) => c.insert(k, buf), - _ => buf, - }; - Some((buf, key)) + let (buf, files) = self.recorded(key, |r| { + // (what expressions read while it is made, at shutter times too, goes to its files) + let ctx = &r.with_reads(ctx); + let buf = r.masked_source(ctx, layer)?; + Some(r.apply_effects_timed(ctx, layer, buf, false, usize::MAX, timing.map(|t| &mut t.effects))) + }); + let (buf, files) = self.keep(key, Arc::new(buf?), &files); + Some((buf, key, files)) } /// A layer's input at the context time: source → masks → its first `effects` effects @@ -1014,23 +1116,19 @@ impl<'a> Renderer<'a> { pub fn layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option> { let key = self.cache.and_then(|_| cache::input_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer), effects)); let key = self.content_key(ctx, layer, key); - if let (Some(c), Some(k)) = (self.cache, key) - && let Some(b) = c.get(k) - { + if let Some((b, _)) = self.cached(key) { return Some(b); } - let mut buf = self.masked_source(ctx, layer)?; - let origin = ctx.effect_bounds(layer).1; - buf.rebase(origin); - if effects > 0 { - buf = self.effect_space_stack(ctx, layer, buf, EffectStack { adjustment: false, limit: effects, origin }, None); - } - let buf = Arc::new(buf); - // The buffer the cache keeps (another worker's, if it kept one first). - let buf = match (self.cache_to_keep(), key) { - (Some(c), Some(k)) => c.insert(k, buf), - _ => buf, - }; - Some(buf) + let (buf, files) = self.recorded(key, |r| { + let ctx = &r.with_reads(ctx); + let mut buf = r.masked_source(ctx, layer)?; + let origin = ctx.effect_bounds(layer).1; + buf.rebase(origin); + if effects > 0 { + buf = r.effect_space_stack(ctx, layer, buf, EffectStack { adjustment: false, limit: effects, origin }, None); + } + Some(buf) + }); + Some(self.keep(key, Arc::new(buf?), &files).0) } } diff --git a/crates/render/src/reads.rs b/crates/render/src/reads.rs new file mode 100644 index 000000000..ed97e50fd --- /dev/null +++ b/crates/render/src/reads.rs @@ -0,0 +1,348 @@ +//! The source files a render read: which footage files its pixels came from, each with its +//! identity (size and modification time) as it was when it was read. +//! +//! A [`FootageSource`](crate::FootageSource) that knows which files it opens notes them while +//! [`FootageSource::recording`](crate::FootageSource::recording) runs; the renderer collects them +//! in a [`ReadLog`] ([`Renderer::reads`](crate::Renderer::reads)). Buffers in the layer cache keep +//! the files they were made of, so a frame served from memory reports the same files as the +//! render that made it, without touching the disk. +//! +//! The list is what a cache entry on disk has to check before it is used: a handful of files per +//! frame, never every file of every footage item. + +use std::sync::{Arc, Mutex, PoisonError}; + +/// The identity of a file as a render read it (frame and layer entries on disk store it, see +/// [`EntryFiles`](crate::disk_cache::EntryFiles)). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum FileStamp { + /// A file on disk: its size in bytes and its modification time (nanoseconds since the Unix + /// epoch, negative before it; `None` only where the file system has none). + Native { len: u64, modified: Option }, + /// Bytes given from memory instead of the file system (the browser's file table): their + /// identity is the size and content hash a + /// [`VirtualFileId`](crate::disk_cache::VirtualFileId) gives for the path. + Virtual, + /// The file could not be read (missing, not a regular file, no permission). What was made + /// without it changes once it can be read again. + Missing, +} + +impl FileStamp { + /// The identity of a file from metadata the caller already has (of an open file). + pub fn of_metadata(md: &std::fs::Metadata) -> FileStamp { + FileStamp::Native { len: md.len(), modified: md.modified().ok().map(epoch_nanos) } + } + + /// The identity of the file at `path` now: one `metadata` call. `None` when the file system + /// can't describe it (missing, or a path served from memory). + pub fn native(path: &str) -> Option { + std::fs::metadata(path).ok().map(|md| FileStamp::of_metadata(&md)) + } +} + +/// `t` in nanoseconds since the Unix epoch, negative before it: every time a file system can +/// store fits (`i128` nanoseconds span far more than any `SystemTime`), saturating otherwise. +pub fn epoch_nanos(t: std::time::SystemTime) -> i128 { + let nanos = |d: std::time::Duration| i128::try_from(d.as_nanos()).unwrap_or(i128::MAX); + match t.duration_since(std::time::UNIX_EPOCH) { + Ok(after) => nanos(after), + Err(before) => nanos(before.duration()).saturating_neg(), + } +} + +/// One file a render read. +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct ReadFile { + pub path: Arc, + pub stamp: FileStamp, +} + +impl ReadFile { + /// A file read with the given path and stamp. + pub fn new(path: impl Into>, stamp: FileStamp) -> ReadFile { + ReadFile { path: path.into(), stamp } + } +} + +/// At most this many files are listed per render or cached buffer; more mark the list +/// incomplete (a comp reading more files than this for one frame is not checked file by file). +pub const MAX_READ_FILES: usize = 4096; + +/// The files a render (or one cached buffer) read, each once, sorted by path. +/// +/// Incomplete when it can't say for sure: a footage source that doesn't tell what it reads, a +/// buffer from a store that doesn't keep its files, more than [`MAX_READ_FILES`] files, or one +/// file read with two identities (it changed during the render). A cache entry whose list is +/// incomplete can't be checked against the files, so it must not outlive the session. +/// +/// It also carries where its reads come from, which equality and hashing ignore: the drop +/// generation the earliest of them started at ([`ReadFiles::since`]), and whether a layer cache +/// key can't capture what it was made of ([`ReadFiles::is_unkeyed`]). +#[derive(Clone, Debug, Default)] +pub struct ReadFiles { + files: Vec, + incomplete: bool, + /// The disk cache's drop generation ([`DiskCache::drop_generation`]) when the earliest read + /// behind the list started; `None` when nothing says (a list built by hand). + /// + /// [`DiskCache::drop_generation`]: crate::disk_cache::DiskCache::drop_generation + since: Option, + /// Made of values a layer's cache key doesn't capture (a text Expression Selector evaluates + /// per character): such a buffer is not stored on disk. + unkeyed: bool, +} + +impl PartialEq for ReadFiles { + /// The same files with the same identities (where the reads come from doesn't count). + fn eq(&self, other: &Self) -> bool { + self.files == other.files && self.incomplete == other.incomplete + } +} + +impl Eq for ReadFiles {} + +impl std::hash::Hash for ReadFiles { + /// Hashes what [`PartialEq`] compares. + fn hash(&self, state: &mut H) { + self.files.hash(state); + self.incomplete.hash(state); + } +} + +impl ReadFiles { + /// Note a file read (once per path). + pub fn add(&mut self, file: ReadFile) { + match self.files.binary_search_by(|f| f.path.cmp(&file.path)) { + Ok(i) => { + if self.files.get(i).is_some_and(|f| f.stamp != file.stamp) { + self.incomplete = true; + } + } + Err(i) => { + if self.files.len() >= MAX_READ_FILES { + self.incomplete = true; + } else { + self.files.insert(i, file); + } + } + } + } + + /// Note every file of `other` (whether it is incomplete or unkeyed, and its generation). + pub fn extend(&mut self, other: &ReadFiles) { + self.incomplete |= other.incomplete; + self.unkeyed |= other.unkeyed; + if let Some(g) = other.since { + self.note_since(g); + } + for f in &other.files { + self.add(f.clone()); + } + } + + /// Some files read may be missing from the list (see [`ReadFiles`]). + pub fn mark_incomplete(&mut self) { + self.incomplete = true; + } + + /// Whether the list names every file read. + pub fn is_complete(&self) -> bool { + !self.incomplete + } + + /// The drop generation the earliest read behind the list started at (`None`: unknown). A + /// buffer made of a file dropped after it holds that file as it was before the drop. + pub fn since(&self) -> Option { + self.since + } + + /// A read behind the list started at drop generation `generation` (the earliest one counts). + pub fn note_since(&mut self, generation: u64) { + self.since = Some(self.since.map_or(generation, |s| s.min(generation))); + } + + /// Made of values a layer cache key doesn't capture (see [`ReadFiles::is_unkeyed`]). + pub fn mark_unkeyed(&mut self) { + self.unkeyed = true; + } + + /// Made of values a layer cache key doesn't capture: kept in memory, never stored on disk as + /// a layer buffer. (A frame's key captures them: frame entries are not affected.) + pub fn is_unkeyed(&self) -> bool { + self.unkeyed + } + + /// The files, sorted by path. + pub fn files(&self) -> &[ReadFile] { + &self.files + } + + /// The identity `path` was read with. + pub fn get(&self, path: &str) -> Option { + let i = self.files.binary_search_by(|f| (*f.path).cmp(path)).ok()?; + self.files.get(i).map(|f| f.stamp) + } + + /// The number of files in the list. + pub fn len(&self) -> usize { + self.files.len() + } + + /// Whether the list holds no files. + pub fn is_empty(&self) -> bool { + self.files.is_empty() + } +} + +impl FromIterator for ReadFiles { + /// Collects files into a list, adding each in turn. + fn from_iter>(iter: I) -> Self { + let mut out = ReadFiles::default(); + for f in iter { + out.add(f); + } + out + } +} + +/// Collects the files a render reads, from any thread (the layers of a frame render in +/// parallel). +#[derive(Debug, Default)] +pub struct ReadLog { + files: Mutex, +} + +impl ReadLog { + /// An empty read log. + pub fn new() -> ReadLog { + ReadLog::default() + } + + /// An empty read log for reads that start at drop generation `generation` (see + /// [`ReadFiles::since`]). + pub fn starting_at(generation: u64) -> ReadLog { + let mut files = ReadFiles::default(); + files.note_since(generation); + ReadLog { files: Mutex::new(files) } + } + + /// Locks the file list, recovering from a poisoned lock. + fn lock(&self) -> std::sync::MutexGuard<'_, ReadFiles> { + self.files.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// Note a file read. + pub fn note(&self, file: ReadFile) { + self.lock().add(file); + } + + /// Note every file of `files` (and whether it is incomplete). + pub fn note_all(&self, files: &ReadFiles) { + if files.is_empty() && files.is_complete() && files.since.is_none() && !files.unkeyed { + return; + } + self.lock().extend(files); + } + + /// Some files read can't be listed (see [`ReadFiles`]). + pub fn mark_incomplete(&self) { + self.lock().mark_incomplete(); + } + + /// What is read is made of values a layer cache key doesn't capture + /// ([`ReadFiles::is_unkeyed`]). + pub fn mark_unkeyed(&self) { + self.lock().mark_unkeyed(); + } + + /// The files noted so far. + pub fn files(&self) -> ReadFiles { + self.lock().clone() + } + + /// The files noted. + pub fn into_files(self) -> ReadFiles { + self.files.into_inner().unwrap_or_else(PoisonError::into_inner) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A native file with the given length for the tests. + fn file(path: &str, len: u64) -> ReadFile { + ReadFile::new(path, FileStamp::Native { len, modified: Some(7) }) + } + + #[test] + fn files_are_listed_once_sorted_by_path() { + let files: ReadFiles = [file("/b", 1), file("/a", 2), file("/b", 1)].into_iter().collect(); + assert_eq!(files.files().iter().map(|f| &*f.path).collect::>(), ["/a", "/b"]); + assert!(files.is_complete()); + assert_eq!(files.get("/a"), Some(FileStamp::Native { len: 2, modified: Some(7) })); + assert_eq!(files.get("/c"), None); + } + + /// A file read twice with different identities changed during the render: the list can't + /// vouch for what was made of it. + #[test] + fn a_file_read_with_two_identities_makes_the_list_incomplete() { + let files: ReadFiles = [file("/a", 1), file("/a", 2)].into_iter().collect(); + assert_eq!(files.len(), 1); + assert!(!files.is_complete()); + } + + /// Modification times keep their full value: before 1970 (not "no time") and past 2554 + /// (where `u64` nanoseconds end), so two such files never look alike. + #[test] + fn modification_times_are_kept_whole() { + use std::time::{Duration, UNIX_EPOCH}; + let day = Duration::from_secs(86_400); + assert_eq!(epoch_nanos(UNIX_EPOCH), 0); + assert_eq!(epoch_nanos(UNIX_EPOCH - day), -86_400_000_000_000); + let far = 600 * 365 * day; + if let Some(t) = UNIX_EPOCH.checked_add(far) { + assert_eq!(epoch_nanos(t), i128::try_from(far.as_nanos()).unwrap()); + } + let dir = std::env::temp_dir().join(format!("effectcraft-stamp-1969-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let (a, b) = (dir.join("a.bin"), dir.join("b.bin")); + let mut stamps = vec![]; + for (p, back) in [(&a, day), (&b, 2 * day)] { + std::fs::write(p, b"abc").unwrap(); + let f = std::fs::File::options().write(true).open(p).unwrap(); + if f.set_modified(UNIX_EPOCH - back).is_err() { + // (a file system that can't store times before 1970) + let _ = std::fs::remove_dir_all(&dir); + return; + } + drop(f); + stamps.push(FileStamp::native(&p.to_string_lossy()).unwrap()); + } + assert!(matches!(stamps[0], FileStamp::Native { modified: Some(t), .. } if t < 0), "{:?}", stamps[0]); + assert_ne!(stamps[0], stamps[1], "two files from before 1970 with the same size"); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn the_list_is_bounded() { + let files: ReadFiles = (0..MAX_READ_FILES + 3).map(|i| file(&format!("/f{i:06}"), 1)).collect(); + assert_eq!(files.len(), MAX_READ_FILES); + assert!(!files.is_complete(), "files past the bound make the list incomplete, never silently short"); + } + + #[test] + fn incompleteness_carries_over() { + let mut part = ReadFiles::default(); + part.mark_incomplete(); + let log = ReadLog::new(); + log.note(file("/a", 1)); + log.note_all(&part); + let all = log.into_files(); + assert_eq!(all.len(), 1); + assert!(!all.is_complete()); + } +} diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index c77fc2d94..80b1aa12d 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -482,6 +482,145 @@ impl crate::eval::ExprHost for ControllerRate { } } +/// An expression that reads a file (as `sampleImage` does, into [`EvalCtx::reads`]) and keeps +/// the value. +/// +/// [`EvalCtx::reads`]: crate::eval::EvalCtx::reads +struct ReadsAFile; + +impl crate::eval::ExprHost for ReadsAFile { + fn eval(&self, ctx: &crate::eval::EvalCtx, _: &effectcraft_project::Layer, _: &effectcraft_project::Property, value: &Value) -> Result { + if let Some(log) = ctx.reads { + log.note(crate::reads::ReadFile::new("/sampled.png", crate::reads::FileStamp::Native { len: 3, modified: Some(1) })); + } + Ok(value.clone()) + } +} + +/// What expressions read while a cached buffer is made (a mask expression, which mask motion +/// blur also runs at shutter times the cache key doesn't see) goes to that buffer's files, not +/// only to the frame's (#3184): a later render served the buffer lists the file too. Content, +/// a Time effect's input and an adjustment layer's footprint. +#[test] +fn what_expressions_read_goes_to_the_files_of_the_buffer_they_make() { + use crate::reads::ReadLog; + use crate::{NoFootage, RenderOpts, Renderer}; + let (mut p, cid, comp) = setup(); + for adjustment in [false, true] { + let mut l = solid(&mut p, &comp, [1.0, 1.0, 1.0], 200, 100); + l.switches.adjustment = adjustment; + let mut next = p.next_id; + let m = build::mask(&mut Ids(&mut next), "Mask 1", ShapePath::rect([50.0, 50.0], 40.0, 40.0), MaskMode::Add, [255, 255, 0]); + p.next_id = next; + l.props.sub_mut("masks").unwrap().children.push(m.into()); + l.props.prop_mut("masks/#1/path").unwrap().expr = Some(effectcraft_project::Expression { text: "sampled".into(), enabled: true }); + p.comp_mut(cid).unwrap().layers.push(l); + } + let cache = crate::LayerCache::default(); + let c = p.comp(cid).unwrap(); + let (content, adjustment) = (&c.layers[0], &c.layers[1]); + // `make`, rendered once with the expressions (the files go to `log`) and then served from + // the cache to a render without them: the files that render lists. + let files_served = |what: &str, make: &dyn Fn(&Renderer, &crate::eval::EvalCtx) -> bool| { + let log = ReadLog::new(); + let mut r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + r.expr = Some(&ReadsAFile); + r.cache = Some(&cache); + r.reads = Some(&log); + assert!(make(&r, &r.ctx(cid, c, Tick::ZERO)), "{what}"); + assert!(log.files().get("/sampled.png").is_some(), "{what}: the frame lists the file"); + let hits = cache.stats().hits; + let served = ReadLog::new(); + r.expr = None; + r.reads = Some(&served); + assert!(make(&r, &r.ctx(cid, c, Tick::ZERO)), "{what}"); + assert!(cache.stats().hits > hits, "{what}: served from the cache"); + served.into_files() + }; + let files = files_served("content", &|r, ctx| r.content_buf(ctx, content).is_some()); + assert!(files.get("/sampled.png").is_some(), "content: {files:?}"); + let files = files_served("input", &|r, ctx| r.layer_input(ctx, content, 0).is_some()); + assert!(files.get("/sampled.png").is_some(), "input: {files:?}"); + let files = files_served("footprint", &|r, ctx| r.adjustment_footprint(ctx, adjustment).is_some()); + assert!(files.get("/sampled.png").is_some(), "footprint: {files:?}"); +} + +/// A text Expression Selector whose Amount is 0 for the first character and the width of the +/// footage item named `ref.png` for the others (`textIndex == 1 ? 0 : footage("ref.png").width`). +struct WidthOfRef; + +impl crate::eval::ExprHost for WidthOfRef { + fn eval(&self, _: &crate::eval::EvalCtx, _: &effectcraft_project::Layer, _: &effectcraft_project::Property, value: &Value) -> Result { + Ok(value.clone()) + } + fn eval_text_selector( + &self, + ctx: &crate::eval::EvalCtx, + _: &effectcraft_project::Layer, + _: &effectcraft_project::Property, + index: usize, + _: usize, + _: [f64; 3], + ) -> Result<[f64; 3], String> { + let width = ctx.project.items.values().find(|i| i.name == "ref.png").and_then(|i| i.dimensions()).map_or(0.0, |d| d.0 as f64); + Ok([if index == 1 { 0.0 } else { width }; 3]) + } +} + +/// A text layer whose Expression Selector reads a footage item's size for every character but +/// the first: the layer's key holds the first character's amount only, so its buffer is not +/// stored on disk. Reopened over the same disk cache after the footage's width changed (16 to +/// 64), the frame matches one rendered without a cache. +#[test] +fn a_text_expression_selector_is_not_served_from_the_disk_cache() { + use crate::disk_cache::{DiskCache, Kind}; + use crate::{LayerCache, NoFootage, RenderOpts, Renderer}; + let project = |width: u32| { + let (mut p, cid, comp) = setup(); + let f = effectcraft_project::Footage { has_video: true, width, height: 16, ..Default::default() }; + p.add_item("ref.png", Label::None, None, ItemKind::Footage(f)); + let mut l = build::layer(&mut p, &comp, "Text", LayerSource::Text, (200, 100), None); + l.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "HH".into(), size: 60.0, ..Default::default() })); + let mut next = p.next_id; + let mut ids = Ids(&mut next); + let mut props = build::text_anim_props(&mut ids, "opacity", false); + for pr in &mut props { + pr.value = Value::Scalar(0.0); + } + let mut animator = build::text_animator(&mut ids, "Animator 1", props); + let sel = build::expression_selector(&mut ids, "Expression Selector 1"); + p.next_id = next; + animator.sub_mut("selectors").unwrap().children.push(sel.into()); + l.props.group_mut("text/animators").unwrap().children.push(animator.into()); + p.comp_mut(cid).unwrap().layers.push(l); + (p, cid) + }; + let render = |p: &Project, cid: ItemId, cache: Option<&LayerCache>| { + let mut r = Renderer::new(p, &NoFootage, RenderOpts::default()); + r.expr = Some(&WidthOfRef); + r.cache = cache; + r.comp_frame(cid, Tick::ZERO) + }; + let dir = tempfile::tempdir().unwrap(); + let dc = DiskCache::open(dir.path(), 1 << 30).unwrap(); + dc.set_min_layer_ms(0); + // First session: the 16 px wide footage. + let (p16, cid) = project(16); + let cache = LayerCache::default(); + cache.set_disk(Some(dc.clone())); + let first = render(&p16, cid, Some(&cache)); + dc.flush(); + assert!(first.data.iter().any(|px| px[3] > 0.5), "the text renders"); + // Reopened with a fresh memory cache, the footage now 64 px wide. + let (p64, cid) = project(64); + let uncached = render(&p64, cid, None); + assert_ne!(uncached.data, first.data, "the second character's opacity follows the width"); + let reopened = LayerCache::default(); + reopened.set_disk(Some(dc.clone())); + assert_eq!(render(&p64, cid, Some(&reopened)).data, uncached.data, "not the buffer from before the change"); + assert!(dc.keys(Kind::Layer).is_empty(), "the text layer's buffer is not on disk"); +} + /// A Birth Rate expression reading another layer: changing that layer changes the history but /// not the current rate (0) nor this layer's properties, which is all the layer cache key sees. /// CC Particle World layers with expressions therefore aren't layer cached. @@ -820,6 +959,41 @@ fn effects_read_layer_params() { assert!(px[2] > 0.99 && px[0] < 0.01, "{px:?}"); } +/// A cached layer whose effect reads a precomp layer: how the precomp's layers render depends on +/// Switches Affect Nested Comps (the precomp layer's Draft quality passes down) and on the +/// switches the reading layer's own comp inherits, so the key must tell them apart (a buffer +/// made under the other setting, from memory or disk, is stale). +#[test] +fn a_cached_layer_reading_a_precomp_is_keyed_by_the_nested_switches() { + let (mut p, cid, comp) = setup(); + let mut inner = Comp::new(200, 100, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); + // A rotated square off the pixel grid: Draft (nearest) and Best (bilinear) sampling differ. + let mut dot = solid(&mut p, &inner, [1.0, 1.0, 1.0], 40, 40); + dot.props.prop_mut("transform/position").unwrap().value = Value::Vec3([100.3, 50.6, 0.0]); + dot.props.prop_mut("transform/rotation").unwrap().value = Value::Scalar(30.0); + inner.layers.push(dot); + let iid = p.add_item("Inner", Label::Sandstone, None, ItemKind::Comp(inner.into())); + let mut pre = build::layer(&mut p, &comp, "Inner", LayerSource::Comp { item: iid }, (200, 100), None); + pre.switches.video = false; + pre.switches.quality = effectcraft_project::Quality::Draft; + let mut top = solid(&mut p, &comp, [1.0, 0.0, 0.0], 200, 100); + add_effect_200(&mut p, &mut top, "ec.channel.blend", &[("blendWithLayer", Value::Layer(Some(pre.id.0)))]); + let c = p.comp_mut(cid).unwrap(); + c.layers.push(top); + c.layers.push(pre); + let frame = |nested_switches: bool, cache: Option<&crate::LayerCache>| { + let mut r = crate::Renderer::new(&p, &crate::NoFootage, crate::RenderOpts { nested_switches, ..Default::default() }); + r.cache = cache; + r.comp_frame(cid, Tick::ZERO).data + }; + let (on, off) = (frame(true, None), frame(false, None)); + assert!(on != off, "the precomp layer's Draft quality passes down"); + let cache = crate::LayerCache::default(); + assert!(frame(true, Some(&cache)) == on); + assert!(frame(false, Some(&cache)) == off, "not the buffer made with switches affecting nested comps"); + assert!(frame(true, Some(&cache)) == on, "and back"); +} + /// Records whether the GPU compositor was asked to draw a frame. struct ProbeAccel(std::sync::atomic::AtomicBool); impl crate::Accelerator for ProbeAccel { diff --git a/crates/render/src/tests/collapse.rs b/crates/render/src/tests/collapse.rs index 30d2163c1..4104e0b38 100644 --- a/crates/render/src/tests/collapse.rs +++ b/crates/render/src/tests/collapse.rs @@ -161,8 +161,17 @@ fn referenced_layer_overrides_change_the_owner_cache_key() { let opacity = comp.layer(aid).unwrap().props.prop("transform/opacity").unwrap().uid; let key = |value: f64| { let overrides = [crate::PropertyOverride { layer: aid, prop: opacity, value: Value::Scalar(value), font: false }]; - let ctx = - EvalCtx { project: &p, comp_id: cid, comp, time: Tick::ZERO, expr: None, footage: None, property_overrides: &overrides, media_overrides: &[] }; + let ctx = EvalCtx { + project: &p, + comp_id: cid, + comp, + time: Tick::ZERO, + expr: None, + footage: None, + property_overrides: &overrides, + media_overrides: &[], + reads: None, + }; crate::cache::layer_key(&ctx, b, 1.0, false, false) }; let (opaque, half) = (key(100.0), key(50.0)); diff --git a/crates/render/src/tests/mask_blur.rs b/crates/render/src/tests/mask_blur.rs index 07bc1e95f..5f3ff4e2a 100644 --- a/crates/render/src/tests/mask_blur.rs +++ b/crates/render/src/tests/mask_blur.rs @@ -75,3 +75,32 @@ fn mask_motion_blur_follows_the_render_and_the_comp() { let (p, cid) = project(false); assert!(alpha_at_25(&p, cid, true) < 0.01, "the comp's switch off"); } + +/// Whether mask motion blur may run at all (the render options, Switches Affect Nested Comps with +/// a precomp layer's switch off) is part of the layer cache key, also for a layer whose own +/// switch is off: a buffer made with the gate open must not serve a render with it closed. +#[test] +fn the_mask_motion_blur_gate_keys_the_layer_cache() { + let (mut p, inner) = project(true); + let mut outer = Comp::new(100, 100, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + outer.enable_motion_blur = true; + let mut pre = build::layer(&mut p, &outer, "Inner", LayerSource::Comp { item: inner }, (100, 100), None); + pre.switches.motion_blur = false; + outer.layers.push(pre); + let oid = p.add_item("Outer", Label::Sandstone, None, ItemKind::Comp(outer.into())); + let half = Tick::from_seconds_f64(0.5 / 30.0); + let cache = crate::LayerCache::default(); + let render = |cid: ItemId, opts: RenderOpts, cached: bool| { + let mut r = Renderer::new(&p, &NoFootage, opts); + r.cache = cached.then_some(&cache); + r.comp_frame(cid, half).data + }; + let on = RenderOpts { motion_blur: true, nested_switches: true, ..Default::default() }; + let off = RenderOpts { nested_switches: false, ..on }; + assert!(render(oid, on, false) != render(oid, off, false), "the precomp layer's switch closes the gate"); + assert!(render(oid, off, true) == render(oid, off, false)); + assert!(render(oid, on, true) == render(oid, on, false), "not the buffer blurred with the gate open"); + let still = RenderOpts { motion_blur: false, ..on }; + assert!(render(inner, on, true) == render(inner, on, false)); + assert!(render(inner, still, true) == render(inner, still, false), "Render Settings ▸ Motion Blur off"); +} diff --git a/crates/render/src/tests/styles.rs b/crates/render/src/tests/styles.rs index 2450fcf64..a9de6fb52 100644 --- a/crates/render/src/tests/styles.rs +++ b/crates/render/src/tests/styles.rs @@ -507,3 +507,34 @@ fn workers_styling_a_layer_at_once_share_its_buffers() { assert!(st.passes.iter().zip(&first.passes).all(|(a, b)| std::sync::Arc::ptr_eq(&a.0, &b.0)), "one set of passes"); } } + +/// A styled layer served from the cache lists the files of the body and passes it was served, +/// not only its content's (#3184): one made of other files (another worker's, or from disk, +/// whose list can't vouch for them) passes that on to the buffers made of it (flattened, in the +/// blending space), which are then not kept on disk as complete. +#[test] +fn a_cached_styled_layer_lists_the_files_of_its_cached_buffers() { + let (p, cid) = scene([1.0; 3], None, |p, c, l| add(p, c, l, "dropShadow")); + let layer = &p.comp(cid).unwrap().layers[0]; + let styled = |cache: &crate::LayerCache| { + let mut r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + r.cache = Some(cache); + let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap(); + r.styled_layer(&ctx, layer).unwrap() + }; + let first = styled(&crate::LayerCache::default()); + assert!(first.files.is_complete() && !first.passes.is_empty()); + let key = first.key.unwrap(); + // Body and passes kept with a list that can't vouch for them; the content renders again. + let cache = crate::LayerCache::default(); + let mut unsure = crate::reads::ReadFiles::default(); + unsure.mark_incomplete(); + let unsure = std::sync::Arc::new(unsure); + cache.insert(key, first.body.clone(), unsure.clone()); + for (i, (b, _)) in first.passes.iter().enumerate() { + cache.insert(crate::cache::derive(key, i as u64 + 1), b.clone(), unsure.clone()); + } + let hit = styled(&cache); + assert!(std::sync::Arc::ptr_eq(&hit.body, &first.body), "the cached body"); + assert!(!hit.files.is_complete(), "{:?}", hit.files); +} diff --git a/crates/render/src/tests/time.rs b/crates/render/src/tests/time.rs index e9599f70b..715c37ef0 100644 --- a/crates/render/src/tests/time.rs +++ b/crates/render/src/tests/time.rs @@ -143,6 +143,7 @@ fn input_keys_ignore_later_effects() { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let l = &comp.layers[0]; (crate::cache::input_key(&ctx, l, 1.0, false, false, 1), crate::cache::input_key(&ctx, l, 1.0, false, false, 2)) diff --git a/crates/render/src/text.rs b/crates/render/src/text.rs index 683da82ee..ed369afa5 100644 --- a/crates/render/src/text.rs +++ b/crates/render/src/text.rs @@ -243,6 +243,13 @@ pub fn animator_selection(ctx: &EvalCtx, layer: &Layer, anim: &PropGroup, lay: & } "expressionSelector" => { let Some(prop) = sel.get("amount") else { continue }; + // Evaluated per character (`textIndex`): the layer's cache key holds the amount + // for the first character only, so the buffer must not outlive the session. + if prop.has_expression() + && let Some(log) = ctx.reads + { + log.mark_unkeyed(); + } let stat = ctx.v3(layer, sel, "amount", [100.0; 3]); Box::new(move |i, n, prev| { let pv = prev.map(|x| x * 100.0); @@ -638,11 +645,73 @@ fn align_index(j: Justify) -> u8 { } } +/// Note the font files `lay` is drawn with in [`EvalCtx::reads`] (#3184), each with its identity +/// as its bytes were read, so a cache entry of the text is checked against them. What the +/// files can't vouch for makes the list incomplete: a family that is not installed (installing +/// it changes the text), a character drawn with a fallback face or with none (another installed +/// font could take it), a font from memory. +fn note_fonts(ctx: &EvalCtx, lay: &TextLayout) { + note_layout_fonts(ctx, &lay.styles, &lay.layout); +} + +/// A character put in by Character Offset or Character Value (`ch`, in style `st`), outside a +/// relayout: its outline and advance, with the font it is drawn with noted as [`note_fonts`] does +/// (the run's face may not have it: a fallback face then draws it). +fn substituted_glyph(ctx: &EvalCtx, st: &effectcraft_keyframe::CharStyle, ch: char) -> (BezPath, f64) { + if ctx.reads.is_some() { + // (the layout `char_glyph_style` makes, served from the layout cache there) + let lay = effectcraft_text::layout_text(&ch.to_string(), &effectcraft_text::text_style(st), &effectcraft_text::ParagraphStyle::default()); + note_layout_fonts(ctx, std::slice::from_ref(st), &lay); + } + char_glyph_style(st, ch) +} + +/// [`note_fonts`] for `layout`, whose glyphs' runs index `styles`. +fn note_layout_fonts(ctx: &EvalCtx, styles: &[effectcraft_keyframe::CharStyle], layout: &effectcraft_text::Layout) { + use crate::reads::{FileStamp, ReadFile}; + use effectcraft_text::fonts; + let Some(log) = ctx.reads else { return }; + let resolved: Vec = styles.iter().map(|s| fonts::resolve(&s.font, &s.style)).collect(); + if resolved.iter().any(|r| r.missing) { + log.mark_incomplete(); + } + // Each run's face, glyphs or not: an empty paragraph's face sets its line's metrics (in a + // box, the first baseline that places every later line). + let mut faces: Vec = Vec::new(); + for r in &resolved { + if !faces.contains(&r.face) { + faces.push(r.face); + } + } + for g in &layout.glyphs { + if g.id == 0 || resolved.get(g.run).is_none_or(|r| r.face != g.face) { + log.mark_incomplete(); + } + if !faces.contains(&g.face) { + faces.push(g.face); + } + } + for id in faces { + let face = fonts::face(id); + match &face.info.data { + fonts::FaceData::Static(_) => {} + fonts::FaceData::Shared(_) => log.mark_incomplete(), + fonts::FaceData::File(_) => { + if let Some((path, meta)) = face.file_read() { + let stamp = meta.map_or(FileStamp::Missing, FileStamp::of_metadata); + log.note(ReadFile::new(path.to_string_lossy().as_ref(), stamp)); + } + } + } + } +} + /// Lay out and animate a text layer. pub fn text_geom(ctx: &EvalCtx, layer: &Layer) -> Option { let doc = source_text(ctx, layer)?; let text = layer.props.sub("text")?; let lay = layout_doc(&doc); + note_fonts(ctx, &lay); let xfs = char_transforms(ctx, layer, text, &lay); let three = per_char_3d(ctx, layer); let more = text.sub("moreOptions"); @@ -665,6 +734,7 @@ pub fn text_geom(ctx: &EvalCtx, layer: &Layer) -> Option { // Same character count: the style runs still line up. let d2 = TextDoc { text: chars.into_iter().collect(), ..doc.clone() }; let l2 = layout_doc(&d2); + note_fonts(ctx, &l2); let by_char: std::collections::HashMap = lay.glyphs.iter().zip(&xfs).map(|(g, x)| (g.char_index, *x)).collect(); let x2: Vec = l2.glyphs.iter().map(|g| by_char.get(&g.char_index).copied().unwrap_or_default()).collect(); subs = vec![None; l2.glyphs.len()]; @@ -789,7 +859,7 @@ pub fn text_geom(ctx: &EvalCtx, layer: &Layer) -> Option { // Outline (substituted characters aligned in the original slot). let (outline, adv) = match subs[gi] { Some(c) => { - let (p, a) = char_glyph_style(st, c); + let (p, a) = substituted_glyph(ctx, st, c); let dx = match x.char_align { 0 => 0.0, 2 => g.advance - a, diff --git a/crates/render/src/text/tests.rs b/crates/render/src/text/tests.rs index 0280115a5..9b52d3098 100644 --- a/crates/render/src/text/tests.rs +++ b/crates/render/src/text/tests.rs @@ -715,3 +715,90 @@ fn range_bar_drag_values() { assert_eq!(range_value_of(3.6 / 7.0, 0.0, true, 7), 4.0); assert_eq!(range_value_of(3.6 / 7.0, 2.0 / 7.0, true, 7), 2.0, "less a 2-character Offset"); } + +/// Text names the font files it is drawn with (#3184), with their identity as they were read, so +/// a cache entry of it is checked against them; what the files can't vouch for (a family that +/// isn't installed) leaves the list incomplete. +#[test] +fn text_notes_the_font_files_it_draws_with() { + use crate::reads::{FileStamp, ReadLog}; + use effectcraft_text::fonts::{self, FaceData, FaceInfo, FontSource}; + struct OneFile(std::path::PathBuf); + impl FontSource for OneFile { + fn faces(&self) -> Vec { + vec![FaceInfo { + family: "Reads Test Sans".into(), + style: "Regular".into(), + weight: 400, + italic: false, + index: 0, + data: FaceData::File(self.0.clone()), + origin: "system", + native_family: None, + full_name: "Reads Test Sans Regular".into(), + postscript: None, + variable: false, + instances: vec![], + }] + } + } + // (a fresh directory, removed when the test ends, panicking or not) + let dir = tempfile::tempdir().unwrap(); + let font = dir.path().join("reads-test-sans.ttf"); + std::fs::write(&font, fonts::INTER_REGULAR).unwrap(); + fonts::add_source(&OneFile(font.clone())); + let stamp = FileStamp::native(&font.to_string_lossy()).unwrap(); + // (`value`: a Character Value animator puts that character in for every one) + let files_with = |family: &str, value: Option| { + let (mut p, cid, comp) = setup(200, 80); + let mut l = + text_layer(&mut p, &comp, TextDoc { text: "Hi there\rtwo".into(), font: family.into(), style: "Regular".into(), size: 30.0, ..Default::default() }); + if let Some(c) = value { + animator(&mut p, &mut l, &["characterValue"], &["range"]); + set(&mut l, "text/animators/#1/properties/characterValue", Value::Scalar(c as u32 as f64)); + } + p.comp_mut(cid).unwrap().layers.push(l); + let log = ReadLog::new(); + let c = EvalCtx { reads: Some(&log), ..ctx(&p, cid, 0.0) }; + assert!(text_geom(&c, &p.comp(cid).unwrap().layers[0]).is_some()); + log.into_files() + }; + let files_of = |family: &str| files_with(family, None); + let files = files_of("Reads Test Sans"); + assert!(files.is_complete(), "{files:?}"); + assert_eq!(files.files().len(), 1, "{files:?}"); + assert_eq!(files.get(&font.to_string_lossy()), Some(stamp)); + // The bundled family reads no file. + let files = files_of(effectcraft_text::fonts::DEFAULT_FAMILY); + assert!(files.is_complete() && files.is_empty(), "{files:?}"); + // A family that isn't installed falls back: installing it would change the text. + assert!(!files_of("No Such Family 3184").is_complete()); + // A substituted character the face has is drawn with it: the same one file. + let files = files_with("Reads Test Sans", Some('B')); + assert!(files.is_complete(), "{files:?}"); + assert_eq!(files.get(&font.to_string_lossy()), Some(stamp)); + // One it lacks (Inter has no CJK) is drawn with a fallback face, or none: another installed + // font could take it, so the list can't vouch for the text. + assert!(!files_with("Reads Test Sans", Some('\u{4e2d}')).is_complete()); + // An empty first paragraph in the face draws no glyph, but in a box its face sets the first + // baseline that places the lines after it: the file is read all the same. + let (mut p, cid, comp) = setup(200, 80); + let base = TextDoc { + text: "\rHi".into(), + font: fonts::DEFAULT_FAMILY.into(), + style: "Regular".into(), + size: 30.0, + box_size: Some([180.0, 60.0]), + ..Default::default() + }; + let first = effectcraft_keyframe::CharStyle { font: "Reads Test Sans".into(), ..base.base_style() }; + let runs = vec![effectcraft_keyframe::StyleRun { len: 1, style: first }, effectcraft_keyframe::StyleRun { len: 2, style: base.base_style() }]; + let l = text_layer(&mut p, &comp, TextDoc { runs, ..base }); + p.comp_mut(cid).unwrap().layers.push(l); + let log = ReadLog::new(); + let c = EvalCtx { reads: Some(&log), ..ctx(&p, cid, 0.0) }; + assert!(text_geom(&c, &p.comp(cid).unwrap().layers[0]).is_some()); + let files = log.into_files(); + assert!(files.is_complete(), "{files:?}"); + assert_eq!(files.get(&font.to_string_lossy()), Some(stamp), "{files:?}"); +} diff --git a/crates/render/src/three_d/adv/scene.rs b/crates/render/src/three_d/adv/scene.rs index 22507069e..6d2b071d5 100644 --- a/crates/render/src/three_d/adv/scene.rs +++ b/crates/render/src/three_d/adv/scene.rs @@ -477,6 +477,7 @@ fn clip_start(ctx: &EvalCtx, layer: &Layer, prop: &Property, lt: Tick) -> Tick { start } +/// Adds a model layer to the Advanced 3D scene with its material options, opacity and world transform. fn model_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, b: &mut Builder) { let lin = b.s.linear_io; let (opts, prim_mat) = model_options(ctx, layer, lin); @@ -487,7 +488,7 @@ fn model_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, b: &mut Builder) { LayerSource::Model { item } => { let Some(it) = r.project.item(*item) else { return }; let ItemKind::Footage(f) = &it.kind else { return }; - let Some(m) = r.footage.model(*item, f) else { return }; + let Some(m) = r.read(|| r.footage.model(*item, f)) else { return }; let unit = geo.map_or(1.0, |g| ctx.f(layer, g, "unitScale", 1.0)); let clip = geo.map_or(0, |g| ctx.e(layer, g, "animation")) as usize; let clip = clip.checked_sub(1).filter(|c| *c < m.animations.len()); @@ -651,12 +652,13 @@ pub fn extruded_meshes(ctx: &EvalCtx, layer: &Layer, p: &model::extrude::Extrude out } +/// Adds an extruded layer to the Advanced 3D scene with its card material, opacity and world transform. fn extruded_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, p: &model::extrude::ExtrudeParams, b: &mut Builder) { let base = card_material(ctx, layer); let opacity = (ctx.opacity(layer) as f32 * r.opacity_mul()).clamp(0.0, 1.0); let world = b.outer * ctx.world_matrix(layer); let lin = b.s.linear_io; - for (mesh, c, local) in extruded_meshes(ctx, layer, p) { + for (mesh, c, local) in extruded_meshes(&r.with_reads(ctx), layer, p) { let l = |v: f32| if lin { v } else { srgb_to_linear(v) }; let m = b.material(Material { base: [l(c[0]), l(c[1]), l(c[2]), c[3]], @@ -694,13 +696,14 @@ fn card(b: &mut Builder, buf: &Buf, world: Mat4, mut mat: Material) { b.emit(&p, m, &|_| world); } +/// Adds a flat card layer to the Advanced 3D scene with its material, opacity and world transform. fn card_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, b: &mut Builder) { let mut mat = card_material(ctx, layer); mat.opacity = (ctx.opacity(layer) as f32 * r.opacity_mul()).clamp(0.0, 1.0); let world = b.outer * ctx.world_matrix(layer); if crate::text::per_char_3d(ctx, layer) { - for (buf, m) in crate::text::per_char_planes(ctx, layer, r.opts.scale) { - let buf = r.to_blend(Arc::new(buf), None); + for (buf, m) in crate::text::per_char_planes(&r.with_reads(ctx), layer, r.opts.scale) { + let buf = r.to_blend(Arc::new(buf), None, &Arc::default()); card(b, &buf, world * m, mat); } return; diff --git a/crates/render/src/three_d/adv/tests.rs b/crates/render/src/three_d/adv/tests.rs index b2612de98..88e805882 100644 --- a/crates/render/src/three_d/adv/tests.rs +++ b/crates/render/src/three_d/adv/tests.rs @@ -822,6 +822,7 @@ fn extruded_text_renders_as_a_mesh() { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let l = &ctx.comp.layers[0]; let params = scene::extrusion_params(&ctx, l).unwrap(); @@ -870,6 +871,7 @@ fn shape_surface_items_colour_the_extruded_surfaces() { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let l = &ctx.comp.layers[0]; let params = scene::extrusion_params(&ctx, l).unwrap(); @@ -915,6 +917,7 @@ fn text_animators_colour_the_extruded_surfaces() { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let l = &ctx.comp.layers[0]; let params = scene::extrusion_params(&ctx, l).unwrap(); @@ -1122,6 +1125,7 @@ fn extruded_strokes_render_as_bevelled_meshes() { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let l = &ctx.comp.layers[0]; let params = scene::extrusion_params(&ctx, l).unwrap(); diff --git a/crates/render/src/three_d/compose.rs b/crates/render/src/three_d/compose.rs index a5a7c170e..ff08eb05f 100644 --- a/crates/render/src/three_d/compose.rs +++ b/crates/render/src/three_d/compose.rs @@ -203,9 +203,11 @@ pub(crate) fn prepare<'a>(r: &Renderer, ctx: &EvalCtx<'_>, layer: &'a Layer, ord /// Prepare a layer as planes: one per character for per-character 3D text, else one. pub(crate) fn prepare_all<'a>(r: &Renderer, ctx: &EvalCtx<'_>, layer: &'a Layer, order: usize, out: (u32, u32), in_run: bool) -> Vec> { if crate::text::per_char_3d(ctx, layer) { - return crate::text::per_char_planes(ctx, layer, r.opts.scale) + return crate::text::per_char_planes(&r.with_reads(ctx), layer, r.opts.scale) .into_iter() - .filter_map(|(buf, m)| prepare_with(r, ctx, layer, order, out, in_run, PlaneInput { buf: r.to_blend(Arc::new(buf), None), local: m })) + .filter_map(|(buf, m)| { + prepare_with(r, ctx, layer, order, out, in_run, PlaneInput { buf: r.to_blend(Arc::new(buf), None, &Arc::default()), local: m }) + }) .collect(); } prepare(r, ctx, layer, order, out, in_run).into_iter().collect() diff --git a/crates/script/src/tests/ui_more.rs b/crates/script/src/tests/ui_more.rs index 483d686ef..c15aee0ae 100644 --- a/crates/script/src/tests/ui_more.rs +++ b/crates/script/src/tests/ui_more.rs @@ -231,6 +231,7 @@ fn position_now(s: &Session, layer: u64) -> [f64; 2] { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let v = ctx.value(l, l.props.prop("transform/position").unwrap()).components(); [v[0], v[1]] diff --git a/crates/text/src/fonts.rs b/crates/text/src/fonts.rs index 5388629cf..d199bf0ff 100644 --- a/crates/text/src/fonts.rs +++ b/crates/text/src/fonts.rs @@ -158,11 +158,24 @@ fn info_from(n: FaceNames, data: FaceData, origin: &'static str) -> FaceInfo { } } +/// A font file's bytes and its metadata from the same open file (so the metadata describes the +/// bytes, not a later version of the file). +fn read_with_metadata(path: &std::path::Path) -> std::io::Result<(Vec, std::fs::Metadata)> { + use std::io::Read; + let mut f = std::fs::File::open(path)?; + let meta = f.metadata()?; + let mut bytes = Vec::new(); + f.read_to_end(&mut bytes)?; + Ok((bytes, meta)) +} + /// A loaded (or loadable) face. pub struct Face { pub id: FaceId, pub info: FaceInfo, bytes: OnceLock>, + /// The file's metadata as its bytes were read (a [`FaceData::File`] face, once loaded). + file_meta: OnceLock, shaper: OnceLock, } @@ -195,16 +208,29 @@ pub struct VMetrics { } impl Face { + /// The font file bytes, loaded once on first use, or `None` when they cannot be read. fn bytes(&self) -> Option<&[u8]> { self.bytes .get_or_init(|| match &self.info.data { FaceData::Static(b) => Some(FaceBytes::Static(b)), FaceData::Shared(b) => Some(FaceBytes::Shared(b.clone())), - FaceData::File(p) => std::fs::read(p).ok().map(|v| FaceBytes::Shared(Arc::new(v))), + FaceData::File(p) => { + let (bytes, meta) = read_with_metadata(p).ok()?; + let _ = self.file_meta.set(meta); + Some(FaceBytes::Shared(Arc::new(bytes))) + } }) .as_ref() .map(FaceBytes::get) } + /// The file a [`FaceData::File`] face is drawn from, with its metadata as its bytes were read + /// (`None` when it could not be read): what text drawn with the face was made of. Loads the + /// face. `None` for bundled faces and faces from memory. + pub fn file_read(&self) -> Option<(&std::path::Path, Option<&std::fs::Metadata>)> { + let FaceData::File(p) = &self.info.data else { return None }; + let _ = self.bytes(); + Some((p, self.file_meta.get())) + } /// The parsed font (None if the file is missing or unreadable). pub fn font(&self) -> Option> { FontRef::from_index(self.bytes()?, self.info.index).ok() @@ -314,7 +340,7 @@ fn bundled_default_face() -> Arc { variable: false, instances: Vec::new(), }); - Arc::new(Face { id: 0, info, bytes: OnceLock::new(), shaper: OnceLock::new() }) + Arc::new(Face { id: 0, info, bytes: OnceLock::new(), file_meta: OnceLock::new(), shaper: OnceLock::new() }) } fn db() -> &'static RwLock { @@ -331,9 +357,10 @@ fn db() -> &'static RwLock { }) } +/// Adds a face to the database and returns its id. fn push(db: &mut Db, info: FaceInfo) -> FaceId { let id = db.faces.len(); - db.faces.push(Arc::new(Face { id, info, bytes: OnceLock::new(), shaper: OnceLock::new() })); + db.faces.push(Arc::new(Face { id, info, bytes: OnceLock::new(), file_meta: OnceLock::new(), shaper: OnceLock::new() })); id } diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index e77f07840..8ebea08fc 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -170,10 +170,6 @@ impl EffectcraftApp { self.session.poll_jobs(); ctx.request_repaint_after(std::time::Duration::from_millis(100)); } - // The disk cache's footage salt, taken off the UI thread after footage changed. - if self.session.poll_disk_salt() { - ctx.request_repaint_after(std::time::Duration::from_millis(100)); - } // Analyses running in a worker (the web app). if !self.session.offloaded.is_empty() { self.session.poll_offload(); diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index 001790116..968b2d791 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -15,9 +15,12 @@ //! egui frames, within a time budget. //! //! With the disk cache on (Settings ▸ Media & Disk Cache), every CPU-rendered frame is also -//! written to disk under a content key (the comp's content hash, frame, scale, view and render -//! options), and a job first looks there: frames survive restarts and project reopenings. The -//! timeline shows disk-only frames as a blue cache bar. +//! written to disk under a content key (the comp's content hash from the project in memory, +//! frame, scale, view and render options) with the source files it read, and a job first looks +//! there: frames survive restarts and project reopenings. An entry is used only while the files +//! it lists are unchanged, checked by the frame worker when it reads the entry (a frame whose +//! files can't all be named is shown but not stored). The timeline shows disk-only frames as a +//! blue cache bar (present entries; their files are checked only when a frame is used). //! //! With Mercury GPU Acceleration (a [`Gpu`] on egui-wgpu's device and the project's renderer set //! to the GPU) frames stay on the GPU: the compositor writes an RGBA8 texture that the viewer @@ -29,11 +32,12 @@ //! cache keeps them as half-float entries. use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; -use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; -use effectcraft_engine::project::{Comp, Heard, ItemId, ItemKind, LayerSource, Node, Project, PropGroup}; +use effectcraft_engine::project::{Comp, Heard, ItemId, ItemKind, Node, Project, PropGroup}; use effectcraft_engine::render::disk_cache::{self, DiskCache}; +use effectcraft_engine::render::reads::{ReadFiles, ReadLog}; use effectcraft_engine::render::{CancellableFootage, ExprHost, FootageSource, LayerCache, RenderOpts, Renderer}; use effectcraft_engine::time::Tick; use effectcraft_gpu::{DisplayFrame, FrameError, Gpu}; @@ -156,8 +160,41 @@ impl Playhead { } } +/// A frame in the RAM preview and the source files it was made of (`None`: unknown, a frame +/// rendered elsewhere or kept on the GPU), so it can be written to the disk cache later +/// ([`Frames::persist_missing`]). +struct Cached { + img: FrameImage, + /// (the browser's frames are stored by its frame workers, not from here) + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] + files: Option>, + /// The disk cache its render started with and that cache's drop generation then: the frame + /// is written to that cache only, and only while none of its files was dropped since + /// (`None`: rendered without a disk cache, or elsewhere; never written from here). + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] + since: Option, +} + +/// A disk cache and its drop generation ([`DiskCache::drop_generation`]) when a render started. +#[derive(Clone)] +struct DiskSince(std::sync::Weak, u64); + +impl DiskSince { + /// A marker for the drop generation of `dc` at the time of a render. + fn of(dc: &Arc, generation: u64) -> DiskSince { + DiskSince(Arc::downgrade(dc), generation) + } + + /// The generation, when `dc` is the cache it was taken from (the weak reference keeps that + /// cache's address from being reused by another). + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] + fn for_cache(&self, dc: &Arc) -> Option { + std::ptr::eq(self.0.as_ptr(), Arc::as_ptr(dc)).then_some(self.1) + } +} + struct Cache { - map: HashMap, + map: HashMap, order: VecDeque, bytes: usize, /// Bytes of GPU frames (also counted in `bytes`). @@ -174,10 +211,17 @@ struct Cache { } impl Cache { + /// A frame without its source files (tests). + #[cfg(test)] fn insert(&mut self, k: FrameKey, img: FrameImage) { + self.insert_with(k, img, None, None); + } + /// [`Cache::insert`] with the source files the frame was made of and the disk cache's drop + /// generation when its render started. + fn insert_with(&mut self, k: FrameKey, img: FrameImage, files: Option>, since: Option) { let (sz, gpu) = (img.bytes(), img.is_gpu()); - if let Some(old) = self.map.insert(k, img) { - self.forget(&old); + if let Some(old) = self.map.insert(k, Cached { img, files, since }) { + self.forget(&old.img); } else { self.order.push_back(k); } @@ -215,7 +259,7 @@ impl Cache { /// than the one on screen first (a zoom or resolution change). fn evict_gpu(&mut self) { while self.gpu_bytes > self.gpu_budget { - let is_gpu = |k: &FrameKey| self.map.get(k).is_some_and(FrameImage::is_gpu); + let is_gpu = |k: &FrameKey| self.map.get(k).is_some_and(|c| c.img.is_gpu()); let other_scale = self.order.iter().position(|k| Some(k.scale) != self.scale && is_gpu(k)); let Some(i) = other_scale.or_else(|| self.order.iter().position(is_gpu)) else { break }; if let Some(old) = self.order.remove(i) { @@ -251,9 +295,10 @@ impl Cache { self.gpu_bytes = self.gpu_bytes.saturating_sub(img.bytes()); } } + /// Removes the entry for `k` and releases its image. fn remove(&mut self, k: &FrameKey) { if let Some(i) = self.map.remove(k) { - self.forget(&i); + self.forget(&i.img); } } /// Shown again: last in line for eviction. @@ -316,10 +361,10 @@ pub struct RenderSource { /// ([`Frames::identity`]). type Identity = (u64, Option>); -/// Content keys of (project revision, comp, virtual-file generation). The generation is one -/// counter bumped when a browser file is replaced, so a lookup stays a map read. Hashing the -/// new bytes happens only when that generation misses. -type ContentKeys = Arc>>; +/// Disk-cache content keys ([`disk_cache::comp_content_key`]) of (project revision, comp). They +/// come from the project in memory alone: the files a frame read are checked by its entry +/// when it is read (#3184). +type ContentKeys = Arc>>; #[cfg(not(target_arch = "wasm32"))] #[derive(Default)] @@ -328,16 +373,17 @@ struct ContentKeyJobs { pending: VecDeque<(Arc, u64, u64)>, } -fn content_key(keys: &ContentKeys, project: &Project, revision: u64, comp: u64, epoch: u64, virtual_id: Option>) -> u128 { - if let Some(k) = keys.lock().ok().and_then(|m| m.get(&(revision, comp, epoch)).copied()) { +/// The content key of `comp` at `revision`, memoised per revision. +fn content_key(keys: &ContentKeys, project: &Project, revision: u64, comp: u64) -> u128 { + if let Some(k) = keys.lock().ok().and_then(|m| m.get(&(revision, comp)).copied()) { return k; } - let k = disk_cache::comp_content_key_with(project, ItemId(comp), virtual_id); + let k = disk_cache::comp_content_key(project, ItemId(comp)); if let Ok(mut m) = keys.lock() { if m.len() > 256 { m.clear(); } - m.insert((revision, comp, epoch), k); + m.insert((revision, comp), k); } k } @@ -388,7 +434,8 @@ fn comp_content_with<'a>( let Some(ItemKind::Comp(c)) = item(id).map(|i| &i.kind) else { continue }; let c = version(id, c); for l in &c.layers { - if let LayerSource::Comp { item } | LayerSource::Footage { item } | LayerSource::Solid { item } = l.source { + // (a 3D model's item too) + if let Some(item) = l.source.item() { stack.push(item); } expressions = expressions || (everything && has_expression(&l.props)); @@ -423,18 +470,30 @@ pub fn opts_hash(o: &RenderOpts) -> u64 { } /// Disk key of a viewer frame (render options included: region of interest, draft…). -fn frame_disk_key( - keys: &ContentKeys, - project: &Project, - key: &FrameKey, - opts_hash: u64, - epoch: u64, - virtual_id: Option>, -) -> u128 { - let content = content_key(keys, project, key.revision, key.comp, epoch, virtual_id); +fn frame_disk_key(keys: &ContentKeys, project: &Project, key: &FrameKey, opts_hash: u64) -> u128 { + let content = content_key(keys, project, key.revision, key.comp); disk_cache::frame_key(content, key.frame, key.scale, key.view ^ opts_hash) } +/// Queue `img` for the disk cache under `dk` with the source files it was made of. `false` when +/// refused: a GPU frame (no CPU pixels), files that can't be checked when the entry is read +/// (an incomplete list, #3184: the frame is shown, not stored), a full queue, a file dropped +/// (File ▸ Reload Footage) after `since`, the drop generation its render started at; no room on +/// the disk (checked first: the conversion and compression would be wasted). +fn put_disk_frame(dc: &DiskCache, dk: u128, img: &FrameImage, files: &ReadFiles, virtual_id: Option>, since: u64) -> bool { + if !files.is_complete() || !dc.accepts_writes() { + return false; + } + match img { + FrameImage::Cpu(img) => { + let rgba: Vec = img.pixels.iter().flat_map(|c| c.to_array()).collect(); + dc.put_frame(dk, img.size[0] as u32, img.size[1] as u32, &rgba, files, virtual_id, since) + } + FrameImage::CpuF16(f) => dc.put_frame_f16(dk, f.width, f.height, f.bits(), files, virtual_id, since), + FrameImage::Gpu(_) => false, + } +} + /// A frame for a [`RemoteFrames`] renderer. pub struct RemoteJob { pub project: Arc, @@ -512,9 +571,8 @@ pub struct Frames { /// Render time of the last viewer (urgent) frame in ms, for the Info panel / perf readout. pub last_ms: Arc>, content_keys: ContentKeys, - /// Bumped when a virtual file is replaced. Lookups key on it instead of statting footage. - virtual_epoch: Arc, - /// Size and content hash for paths with no native metadata. Called only on a cache miss. + /// Size and content hash for paths with no native metadata (the browser's file table): to + /// store and check the files of disk entries. virtual_id: Option Option<(u64, u64)> + Send + Sync>>, #[cfg(not(target_arch = "wasm32"))] content_key_jobs: Arc>, @@ -602,7 +660,6 @@ impl Default for Frames { ctx: None, last_ms: Arc::new(Mutex::new(0.0)), content_keys: Arc::default(), - virtual_epoch: Arc::new(AtomicU64::new(0)), virtual_id: None, #[cfg(not(target_arch = "wasm32"))] content_key_jobs: Arc::default(), @@ -754,7 +811,7 @@ impl Frames { pub fn retire_gpu(&self) { self.gpu_retired.store(true, Ordering::Release); if let Ok(mut cache) = self.cache.lock() { - let keys: Vec<_> = cache.map.iter().filter_map(|(key, image)| image.is_gpu().then_some(*key)).collect(); + let keys: Vec<_> = cache.map.iter().filter_map(|(key, c)| c.img.is_gpu().then_some(*key)).collect(); for key in keys { cache.remove(&key); } @@ -789,7 +846,7 @@ impl Frames { /// The cached frame, which becomes the most recently used. pub fn get(&self, k: &FrameKey) -> Option { let mut c = self.cache.lock().ok()?; - let img = c.map.get(k).cloned()?; + let img = c.map.get(k).map(|c| c.img.clone())?; c.touch(k); Some(img) } @@ -825,7 +882,7 @@ impl Frames { }) .max_by_key(|k| k.revision) .copied()?; - let img = c.map.get(&newest).cloned()?; + let img = c.map.get(&newest).map(|c| c.img.clone())?; c.touch(&newest); Some((newest, img)) } @@ -968,7 +1025,6 @@ impl Frames { if src.disk.is_none() && remote.is_none() { return vec![]; } - let epoch = self.virtual_epoch.load(Ordering::Relaxed); #[cfg(not(target_arch = "wasm32"))] { self.note_disk_series(base.comp, base.scale, base.view ^ opts_hash(opts)); @@ -976,7 +1032,7 @@ impl Frames { } #[cfg(not(target_arch = "wasm32"))] let content = { - let known = self.content_keys.lock().unwrap_or_else(std::sync::PoisonError::into_inner).get(&(base.revision, base.comp, epoch)).copied(); + let known = self.content_keys.lock().unwrap_or_else(std::sync::PoisonError::into_inner).get(&(base.revision, base.comp)).copied(); let Some(content) = known else { self.request_content_key(src.project.clone(), base.revision, base.comp); return vec![]; @@ -984,7 +1040,7 @@ impl Frames { content }; #[cfg(target_arch = "wasm32")] - let content = content_key(&self.content_keys, &src.project, base.revision, base.comp, epoch, self.virtual_id.as_deref()); + let content = content_key(&self.content_keys, &src.project, base.revision, base.comp); let ram: HashSet = self.cached_frames(base).into_iter().collect(); let oh = opts_hash(opts); (start..end).filter(|f| !ram.contains(f)).filter(|f| contains(disk_cache::frame_key(content, *f, base.scale, base.view ^ oh))).collect() @@ -995,20 +1051,27 @@ impl Frames { /// are converted on the frame pool, each queued once. `true` while there may be more to /// write (a content key being computed, frames being converted): call again soon, since a /// refused write is only retried by a later call. Disk keys hash `opts` the way the worker - /// writes them (`base.opts` also carries the Roto Brush model stamp). + /// writes them (`base.opts` also carries the Roto Brush model stamp). Only frames that know + /// every source file they were made of are written (an entry is checked against them). #[cfg(not(target_arch = "wasm32"))] pub fn persist_missing(&self, src: &RenderSource, base: &FrameKey, opts: &RenderOpts) -> bool { let Some(dc) = src.disk.clone() else { return false }; let oh = opts_hash(opts); self.note_disk_series(base.comp, base.scale, base.view ^ oh); self.carry_disk(src); - let epoch = self.virtual_epoch.load(Ordering::Relaxed); - let known = self.content_keys.lock().unwrap_or_else(std::sync::PoisonError::into_inner).get(&(base.revision, base.comp, epoch)).copied(); + // Free disk space leaves the cache (next to) no room: nothing would be stored, so nothing + // is converted (a refusal doesn't count as a try, so every idle pass would convert them + // all again). A later idle pass writes them once there is room (`prefs_live::memory_watch` + // keeps frames coming meanwhile). + if !dc.accepts_writes() { + return false; + } + let known = self.content_keys.lock().unwrap_or_else(std::sync::PoisonError::into_inner).get(&(base.revision, base.comp)).copied(); let Some(content) = known else { self.request_content_key(src.project.clone(), base.revision, base.comp); return true; }; - let (missing, converting): (Vec<(u128, FrameImage)>, bool) = { + let (missing, converting): (Vec<(u128, FrameImage, Arc, u64)>, bool) = { let (Ok(c), Ok(mut queued), Ok(mut written)) = (self.cache.lock(), self.persisted.lock(), self.written.lock()) else { return false }; if queued.len() > 100_000 { queued.clear(); @@ -1021,14 +1084,17 @@ impl Frames { .map .iter() .filter(|(k, _)| k.same_series(base)) - .filter_map(|(k, img)| { - // (GPU frames have no CPU pixels to write) - if img.is_gpu() { - return None; - } + .filter_map(|(k, c)| { + // (GPU frames have no CPU pixels to write; a frame that can't name its files + // can't be checked when read) + let files = c.files.as_ref().filter(|f| f.is_complete() && !c.img.is_gpu())?; + // (only with the generation its render started at, for this cache: a frame + // made of a file Reload Footage dropped since holds the pixels from before) + let since = c.since.as_ref().and_then(|s| s.for_cache(&dc)).filter(|s| !dc.dropped_since(files, *s))?; let dk = disk_cache::frame_key(content, k.frame, k.scale, k.view ^ oh); let tries = written.1.get(&dk).copied().unwrap_or(0); - (tries < 2 && !dc.contains(disk_cache::Kind::Frame, dk) && !dc.frame_pending(dk) && queued.insert(dk)).then(|| (dk, img.clone())) + (tries < 2 && !dc.contains(disk_cache::Kind::Frame, dk) && !dc.frame_pending(dk) && queued.insert(dk)) + .then(|| (dk, c.img.clone(), files.clone(), since)) }) .collect(); (missing, !queued.is_empty()) @@ -1036,19 +1102,12 @@ impl Frames { if missing.is_empty() { return converting; } - let (persisted, written) = (self.persisted.clone(), self.written.clone()); + let (persisted, written, virtual_id) = (self.persisted.clone(), self.written.clone(), self.virtual_id.clone()); let work = move || { - for (dk, img) in missing { + for (dk, img, files, since) in missing { // (accepted: the disk cache tracks it until written, and it counts as a try; // refused by a full queue: retried, without counting) - let accepted = match &img { - FrameImage::Cpu(img) => { - let rgba: Vec = img.pixels.iter().flat_map(|c| c.to_array()).collect(); - dc.put_frame(dk, img.size[0] as u32, img.size[1] as u32, &rgba) - } - FrameImage::CpuF16(f) => dc.put_frame_f16(dk, f.width, f.height, f.bits()), - FrameImage::Gpu(_) => false, - }; + let accepted = put_disk_frame(&dc, dk, &img, &files, virtual_id.as_deref(), since); if accepted && let Ok(mut w) = written.lock() { let n = w.1.entry(dk).or_default(); *n = n.saturating_add(1); @@ -1091,7 +1150,6 @@ impl Frames { return; } let (carries, scheduled, series, ctx) = (self.disk_carries.clone(), self.carry_scheduled.clone(), self.disk_series.clone(), self.ctx.clone()); - let virtual_id = self.virtual_id.clone(); let work = move || { let mut linked = false; loop { @@ -1111,7 +1169,7 @@ impl Frames { let Some(series) = series.lock().ok().and_then(|m| m.get(&c.comp).cloned()) else { return false }; let comp = ItemId(c.comp); let n = c.to.comp(comp).map_or(0, |k| k.frame_rate.frame_at(k.duration)).clamp(0, 100_000); - let key = |p: &Project| disk_cache::comp_content_key_with(p, comp, virtual_id.as_deref()); + let key = |p: &Project| disk_cache::comp_content_key(p, comp); let (a, b) = (key(&c.from), key(&c.to)); if a == b { return false; @@ -1137,6 +1195,7 @@ impl Frames { } } + /// Queues a background hash of `comp` at `revision`, coalescing rapid edits so only a bounded number of snapshots are kept. #[cfg(not(target_arch = "wasm32"))] fn request_content_key(&self, project: Arc, revision: u64, comp: u64) { let mut jobs = self.content_key_jobs.lock().unwrap_or_else(std::sync::PoisonError::into_inner); @@ -1153,7 +1212,6 @@ impl Frames { jobs.running = true; drop(jobs); let (jobs, keys, ctx) = (self.content_key_jobs.clone(), self.content_keys.clone(), self.ctx.clone()); - let (epoch, id) = (self.virtual_epoch.clone(), self.virtual_id.clone()); let work = move || loop { let next = { let mut jobs = jobs.lock().unwrap_or_else(std::sync::PoisonError::into_inner); @@ -1164,9 +1222,7 @@ impl Frames { next }; let Some((project, revision, comp)) = next else { return }; - let epoch = epoch.load(Ordering::Relaxed); - let id = id.clone(); - if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| content_key(&keys, &project, revision, comp, epoch, id.as_deref()))).is_err() { + if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| content_key(&keys, &project, revision, comp))).is_err() { log::error!("could not compute a preview disk-cache key; the cache indicator will retry"); } if let Some(ctx) = &ctx { @@ -1192,6 +1248,7 @@ impl Frames { self.cache.lock().map(|c| c.budget).unwrap_or(0) } + /// Drops every cached frame and purges the remote server, if there is one. pub fn clear(&self) { if let Some(r) = &self.remote { r.purge(); @@ -1205,9 +1262,8 @@ impl Frames { c.gpu_pressure = None; c.gpu_adjusted = None; } - // Disk keys hash the footage files' size and time, but are remembered per revision; a - // file reloaded from disk without a new revision (a re-rendered image sequence) must be - // keyed again, or the disk cache serves the frames from before. + // (the disk keys of a revision don't depend on files: a file changed on disk is caught + // when its frames' entries are read; they are forgotten with the frames all the same) if let Ok(mut k) = self.content_keys.lock() { k.clear(); } @@ -1386,6 +1442,7 @@ impl Frames { } } + /// A render worker sharing this cache's queue, cache and in-flight set. fn worker(&self) -> Worker { Worker { queue: self.queue.clone(), @@ -1394,20 +1451,15 @@ impl Frames { ctx: self.ctx.clone(), last: self.last_ms.clone(), content_keys: self.content_keys.clone(), - virtual_epoch: self.virtual_epoch.clone(), virtual_id: self.virtual_id.clone(), gpu_retired: self.gpu_retired.clone(), } } - /// Browser file table: `epoch` bumps on each replacement, and `id` is the size and content - /// hash used when a content key is rebuilt. Neither runs on a cache hit. - pub fn set_virtual_media(&mut self, epoch: Arc, id: Arc Option<(u64, u64)> + Send + Sync>) { - self.virtual_epoch = epoch; + /// Browser file table: `id` gives the size and content hash of a path served from memory, + /// stored with the disk entries of the frames that read it and checked when they are read. + pub fn set_virtual_id(&mut self, id: Arc Option<(u64, u64)> + Send + Sync>) { self.virtual_id = Some(id); - if let Ok(mut keys) = self.content_keys.lock() { - keys.clear(); - } } /// Render frames with `remote` from now on (`None`: on this thread again). @@ -1465,8 +1517,10 @@ impl Frames { let gpu_here = job.src.gpu.is_some() && job.src.gpu_display && !(remote.gpu() && comp_has_effects(&job.src.project, job.comp)); if gpu_here { let t0 = web_time::Instant::now(); - if let Some(img) = w.render_gpu(&job) { - w.finish(&job, img, t0); + // (A frame kept on the GPU is not written to the disk cache: its files are not + // needed.) + if let Some((img, _files)) = w.render_gpu(&job) { + w.finish(&job, img, None, None, t0); continue; } } @@ -1478,8 +1532,7 @@ impl Frames { let fw = self.worker(); let busy = self.remote_busy.clone(); let t0 = web_time::Instant::now(); - let epoch = w.virtual_epoch.load(Ordering::Relaxed); - let disk_key = remote.disk().then(|| frame_disk_key(&w.content_keys, &job.src.project, &key, opts_hash(&job.opts), epoch, w.virtual_id.as_deref())); + let disk_key = remote.disk().then(|| frame_disk_key(&w.content_keys, &job.src.project, &key, opts_hash(&job.opts))); let rj = RemoteJob { project: job.src.project.clone(), revision: key.revision, comp: job.comp, t: job.t, opts: job.opts, disk_key }; remote.start( rj, @@ -1490,7 +1543,7 @@ impl Frames { match r { Ok(f) => { let img = egui::ColorImage::from_rgba_premultiplied([f.width as usize, f.height as usize], &f.rgba); - fw.finish_key(key, urgent, FrameImage::Cpu(Arc::new(img)), t0); + fw.finish_key(key, urgent, FrameImage::Cpu(Arc::new(img)), None, None, t0); } Err(e) => { // Not rendered (a stale revision, a lost worker): the viewer asks again. @@ -1536,7 +1589,6 @@ struct Worker { ctx: Option, last: Arc>, content_keys: ContentKeys, - virtual_epoch: Arc, virtual_id: Option Option<(u64, u64)> + Send + Sync>>, gpu_retired: Arc, } @@ -1599,28 +1651,17 @@ impl Worker { job }; let t0 = web_time::Instant::now(); - let epoch = self.virtual_epoch.load(Ordering::Relaxed); - let disk = job - .src - .disk - .as_ref() - .map(|dc| (dc.clone(), frame_disk_key(content_keys, &job.src.project, &job.key, opts_hash(&job.opts), epoch, self.virtual_id.as_deref()))); - // Half-float frames (32 bpc) have entries of their own (their key has the options). - let from_disk = disk.as_ref().and_then(|(dc, k)| { - if job.opts.skip_hdr_step { - let f = dc.get_frame_f16(*k)?; - let encoding = effectcraft_engine::viewer::FrameEncoding::of(&job.src.project.settings, true); - Some(FrameImage::CpuF16(Arc::new(F16Frame::from_bits(f.width, f.height, f.rgba, encoding)))) - } else { - let f = dc.get_frame(*k)?; - Some(FrameImage::Cpu(Arc::new(egui::ColorImage::from_rgba_premultiplied([f.width as usize, f.height as usize], &f.rgba)))) - } - }); + // (the drop generation before the frame reads anything: File ▸ Reload Footage meanwhile + // makes what it read from a reloaded file stale) + let disk = + job.src.disk.as_ref().map(|dc| (dc.clone(), frame_disk_key(content_keys, &job.src.project, &job.key, opts_hash(&job.opts)), dc.drop_generation())); + let from_disk = disk.as_ref().and_then(|(dc, k, _)| self.disk_frame(dc, *k, &job)); let disk_hit = from_disk.is_some(); - let ci = match from_disk { - Some(f) => f, + // `files`: the source files the frame was made of (see `render::reads`). + let (ci, files) = match from_disk { + Some(hit) => hit, None => match self.render_guarded(&job).filter(|_| !job.cancel.load(Ordering::Relaxed)) { - Some(ci) => ci, + Some(rendered) => rendered, // Called off (frames it read may be missing) or failed: not cached, and not // "in progress" forever: the viewer asks for it again. None => { @@ -1629,42 +1670,61 @@ impl Worker { } }, }; + let mut files = files; + if let Some((dc, _, since)) = &disk + && dc.dropped_since(&files, *since) + { + // Made of a file as it was before File ▸ Reload Footage dropped it: shown, but its + // list can't vouch for it, so neither this worker nor `persist_missing` stores it. + files.mark_incomplete(); + } + let files = Arc::new(files); // Publish the shared frame and wake the viewer before optional RGBA conversion // and compression. Persistence still runs on this worker, using the same snapshot. - self.finish(&job, ci.clone(), t0); - if !disk_hit && let Some((dc, k)) = &disk { - let written = match &ci { - FrameImage::Cpu(img) => { - let rgba: Vec = img.pixels.iter().flat_map(|c| c.to_array()).collect(); - dc.put_frame(*k, img.size[0] as u32, img.size[1] as u32, &rgba); - true - } - FrameImage::CpuF16(f) => { - dc.put_frame_f16(*k, f.width, f.height, f.bits()); - true - } - FrameImage::Gpu(_) => false, - }; - if written && let Some(ctx) = &self.ctx { - ctx.request_repaint(); - } + let since = disk.as_ref().map(|(dc, _, since)| DiskSince::of(dc, *since)); + self.finish(&job, ci.clone(), Some(files.clone()), since, t0); + if !disk_hit + && let Some((dc, k, since)) = &disk + && put_disk_frame(dc, *k, &ci, &files, self.virtual_id.as_deref(), *since) + && let Some(ctx) = &self.ctx + { + ctx.request_repaint(); } true } - /// Store a finished frame and release its job. - fn finish(&self, job: &Job, img: FrameImage, t0: web_time::Instant) { - self.finish_key(job.key, job.urgent, img, t0); + /// The frame of `job` from the disk cache (key `k`), if its entry is there and the source + /// files it lists are unchanged (checked here, on the frame worker: a few `metadata` calls + /// for that frame's files). An entry whose files changed is deleted: the frame renders again + /// and its new entry replaces it. + fn disk_frame(&self, dc: &DiskCache, k: u128, job: &Job) -> Option<(FrameImage, ReadFiles)> { + let virtual_id = self.virtual_id.as_deref(); + // Half-float frames (32 bpc) have entries of their own (their key has the options). + if job.opts.skip_hdr_step { + let (f, files) = dc.get_frame_f16(k, virtual_id)?; + let encoding = effectcraft_engine::viewer::FrameEncoding::of(&job.src.project.settings, true); + Some((FrameImage::CpuF16(Arc::new(F16Frame::from_bits(f.width, f.height, f.rgba, encoding))), files)) + } else { + let (f, files) = dc.get_frame(k, virtual_id)?; + Some((FrameImage::Cpu(Arc::new(egui::ColorImage::from_rgba_premultiplied([f.width as usize, f.height as usize], &f.rgba))), files)) + } } - fn finish_key(&self, key: FrameKey, urgent: bool, img: FrameImage, t0: web_time::Instant) { + /// Store a finished frame (with the source files it was made of, when known) and release its + /// job. + fn finish(&self, job: &Job, img: FrameImage, files: Option>, since: Option, t0: web_time::Instant) { + self.finish_key(job.key, job.urgent, img, files, since, t0); + } + + /// Stores a finished frame in the cache with its read files, records the render time for urgent jobs, then releases the job. + fn finish_key(&self, key: FrameKey, urgent: bool, img: FrameImage, files: Option>, since: Option, t0: web_time::Instant) { if urgent && let Ok(mut l) = self.last.lock() { *l = t0.elapsed().as_secs_f64() * 1000.0; } if let Ok(mut c) = self.cache.lock() && (!img.is_gpu() || !self.gpu_retired.load(Ordering::Acquire)) { - c.insert(key, img); + c.insert_with(key, img, files, since); } self.release(key, urgent); } @@ -1689,9 +1749,13 @@ impl Worker { } /// [`Worker::render_job`], again on the CPU if it panicked (the panic hook has logged it); - /// `None` if that panicked too. - fn render_guarded(&self, job: &Job) -> Option { - let attempt = |gpu: bool| std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.render_job(job, gpu))).ok(); + /// `None` if that panicked too. With the source files the frame was made of. + fn render_guarded(&self, job: &Job) -> Option<(FrameImage, ReadFiles)> { + let attempt = |gpu: bool| { + let files = ReadLog::new(); + let img = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.render_job(job, gpu, &files))).ok()?; + Some((img, files.into_files())) + }; let img = attempt(true).or_else(|| { log::error!("a frame render panicked (frame {})", job.key.frame); job.src.gpu.as_ref().and_then(|_| attempt(false)) @@ -1721,26 +1785,31 @@ impl Worker { } } + /// Tells the cache that a GPU render succeeded. fn gpu_succeeded(&self) { if let Ok(mut c) = self.cache.lock() { c.gpu_succeeded(); } } + /// Whether GPU rendering is available and has not been retired. fn gpu_ready(&self) -> bool { !self.gpu_retired.load(Ordering::Acquire) && self.cache.lock().is_ok_and(|c| c.gpu_ready()) } - /// The frame on the GPU, kept there for the viewer, if the compositor can do it alone. - fn render_gpu(&self, job: &Job) -> Option { + /// The frame on the GPU, kept there for the viewer, if the compositor can do it alone, with + /// the source files it was made of. + fn render_gpu(&self, job: &Job) -> Option<(FrameImage, ReadFiles)> { if !self.gpu_ready() { return None; } let g = job.src.gpu.as_ref()?; let footage = job.footage(); + let files = ReadLog::new(); let mut r = Renderer::new(&job.src.project, &footage, job.opts); r.expr = job.src.expr.as_deref(); r.cache = Some(&job.src.layer_cache); + r.reads = Some(&files); r.accel = Some(g as &dyn effectcraft_engine::render::Accelerator); r.active_accel()?; let f = g.within_memory(|| g.render_display(&r, job.comp, job.t)); @@ -1750,7 +1819,7 @@ impl Worker { if f.is_some() { self.gpu_succeeded(); } - f.map(|f| FrameImage::Gpu(Arc::new(f))) + f.map(|f| (FrameImage::Gpu(Arc::new(f)), files.into_files())) } Err(e) => { self.gpu_attempt_failed(g, &e); @@ -1759,11 +1828,13 @@ impl Worker { } } - /// Render a frame; `gpu: false` keeps it off the GPU compositor. - fn render_job(&self, job: &Job, gpu: bool) -> FrameImage { + /// Render a frame; `gpu: false` keeps it off the GPU compositor. The source files it reads + /// are noted in `files` (a GPU attempt that falls back to the CPU notes both renders'). + fn render_job(&self, job: &Job, gpu: bool, files: &ReadLog) -> FrameImage { let footage = job.footage(); let mut r = Renderer::new(&job.src.project, &footage, job.opts); r.expr = job.src.expr.as_deref(); + r.reads = Some(files); let gpu_allowed = gpu && self.gpu_ready(); // A failed GPU attempt may have populated intermediate entries before its scope // reported failure. The CPU retry must compute its own pixels from the snapshot. @@ -1789,7 +1860,7 @@ impl Worker { // Time the GPU's work, not just its recording. g.wait(); if g.context().check_health().is_err() { - return self.render_job(job, false); + return self.render_job(job, false, files); } p.record(key, true, t0.elapsed().as_secs_f64() * 1000.0); } @@ -1802,7 +1873,7 @@ impl Worker { } Err(e) => { self.gpu_attempt_failed(g, &e); - return self.render_job(job, false); + return self.render_job(job, false, files); } } } @@ -1822,7 +1893,7 @@ impl Worker { } Err(e) => { self.gpu_attempt_failed(g, &e); - self.render_job(job, false) + self.render_job(job, false, files) } }; } diff --git a/crates/ui-egui/src/frames/tests.rs b/crates/ui-egui/src/frames/tests.rs index df7b3900c..360ddf1b6 100644 --- a/crates/ui-egui/src/frames/tests.rs +++ b/crates/ui-egui/src/frames/tests.rs @@ -1,4 +1,5 @@ use super::*; +use effectcraft_engine::project::LayerSource; #[test] fn render_scales_are_the_factors_the_thousandths_stand_for() { @@ -60,7 +61,7 @@ fn disk_fixture(stored_frames: &[i64]) -> (Frames, RenderSource, FrameKey, Rende disk.keys.lock().unwrap().extend(stored_frames.iter().map(|frame| disk_cache::frame_key(content, *frame, base.scale, base.view ^ opts_hash(&opts)))); let mut frames = Frames::default(); frames.set_remote(Some(disk.clone())); - frames.content_keys.lock().unwrap().insert((base.revision, base.comp, 0), content); + frames.content_keys.lock().unwrap().insert((base.revision, base.comp), content); let source = RenderSource { project: session.project.clone(), footage: session.footage.clone(), @@ -126,7 +127,7 @@ fn disk_indicator_does_not_hash_on_ui_thread_and_coalesces_edits() { assert_eq!(frames.content_key_jobs.lock().unwrap().pending.len(), 1); release.send(()).unwrap(); let until = Instant::now() + Duration::from_secs(3); - while !frames.content_keys.lock().unwrap().contains_key(&(base.revision, base.comp, 0)) { + while !frames.content_keys.lock().unwrap().contains_key(&(base.revision, base.comp)) { assert!(Instant::now() < until); std::thread::sleep(Duration::from_millis(1)); } @@ -153,7 +154,7 @@ fn disk_indicator_range_tracks_live_membership() { let mut revised = base; revised.revision += 1; - frames.content_keys.lock().unwrap().insert((revised.revision, revised.comp, 0), 5678); + frames.content_keys.lock().unwrap().insert((revised.revision, revised.comp), 5678); assert!(frames.disk_frames(&src, &revised, 10..13, &opts).is_empty()); let changed = FrameKey { scale: base.scale + 1, ..base }; @@ -211,7 +212,7 @@ fn retirement_reaches_existing_workers_and_keeps_cpu_frames_and_completion_live( let next = key(1, 1, 0); frames.inflight.lock().unwrap().insert(next); frames.queue.lock().unwrap().running.insert(next); - worker.finish_key(next, false, img(), web_time::Instant::now()); + worker.finish_key(next, false, img(), None, None, web_time::Instant::now()); assert!(frames.is_cached(&next), "CPU retry can still publish"); assert_eq!(frames.inflight(), 0); assert!(frames.queue.lock().unwrap().running.is_empty()); @@ -996,7 +997,7 @@ fn the_frames_an_edit_keeps_match_a_fresh_render() { // The work area is a preview and render range: setting it (B, N) keeps every frame, on // disk too. let n = s.project.comp(comp).map(|c| c.frame_rate.frame_at(c.duration)).unwrap(); - let disk = |s: &Session| effectcraft_engine::render::disk_cache::comp_content_key_with(&s.project, comp, None); + let disk = |s: &Session| effectcraft_engine::render::disk_cache::comp_content_key(&s.project, comp); let d0 = disk(&s); let work_area = |start: f64, end: f64| json!({"cmd": "comp.workArea", "params": {"comp": comp.0, "start": start, "end": end}}); assert_eq!(kept(&mut s, comp, work_area(1.0, 4.0)), (0..n).collect::>()); @@ -1593,6 +1594,71 @@ fn half_float_ram_frames_missing_on_disk_are_written() { ram_frames_missing_on_disk_are_written_as(true); } +/// A RAM frame whose render started before Reload Footage dropped one of its files (#3184) +/// holds the pixels from before: writing it later to the disk cache (an idle pass after the +/// drop) would bring them back, also after a restart, since the file kept its size and time. It +/// is written only with the drop generation its render started at, for the cache it started +/// with: so it is not written, nor is a frame rendered without that cache. Frames of other files, +/// and frames rendered after the drop, are written. +#[test] +fn ram_frames_from_before_a_drop_are_not_written() { + use effectcraft_engine::Session; + use effectcraft_engine::render::reads::{FileStamp, ReadFile}; + use serde_json::json; + let dir = tempfile::tempdir().unwrap(); + let disk = DiskCache::open(dir.path().join("cache"), 1 << 30).unwrap(); + let other = DiskCache::open(dir.path().join("other"), 1 << 30).unwrap(); + let mut s = Session::default(); + let cid = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 1})).unwrap()["comp"].as_u64().unwrap()); + s.execute("layer.newSolid", json!({})).unwrap(); + let src = RenderSource { + project: s.project.clone(), + footage: Arc::new(effectcraft_engine::render::NoFootage), + expr: None, + layer_cache: Arc::default(), + gpu: None, + gpu_display: false, + disk: Some(disk.clone()), + }; + let f = Frames::default(); + let series = FrameKey { revision: 1, content: f.content_of(&src.project, 1, cid), comp: cid.0, opts: opts_hash(&RenderOpts::default()), ..key(0, 0, 0) }; + let read = |p: &str| Some(Arc::new([ReadFile::new(p, FileStamp::Native { len: 3, modified: Some(1) })].into_iter().collect::())); + let before = DiskSince::of(&disk, disk.drop_generation()); + { + let mut c = f.cache.lock().unwrap(); + c.insert_with(FrameKey { frame: 0, ..series }, img(), read("/a.png"), Some(before.clone())); + c.insert_with(FrameKey { frame: 1, ..series }, img(), read("/b.png"), Some(before)); + c.insert_with(FrameKey { frame: 2, ..series }, img(), read("/a.png"), None); + c.insert_with(FrameKey { frame: 3, ..series }, img(), read("/a.png"), Some(DiskSince::of(&other, other.drop_generation()))); + } + // Reload Footage of a.png. + disk.drop_files(["/a.png"]); + disk.flush(); + f.cache.lock().unwrap().insert_with(FrameKey { frame: 4, ..series }, img(), read("/a.png"), Some(DiskSince::of(&disk, disk.drop_generation()))); + let on_disk = |fr: i64| { + disk.contains( + disk_cache::Kind::Frame, + frame_disk_key(&f.content_keys, &src.project, &FrameKey { frame: fr, ..series }, opts_hash(&RenderOpts::default())), + ) + }; + // Idle passes until there is nothing left to write. + let mut done = false; + for _ in 0..1000 { + let more = f.persist_missing(&src, &series, &RenderOpts::default()); + disk.flush(); + if !more && f.persisted.lock().unwrap().is_empty() { + done = true; + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert!(done, "the idle passes end"); + assert!(on_disk(1) && on_disk(4), "another file's frame, and a frame rendered after the drop"); + assert!(!on_disk(0), "made of a.png as it was before the drop"); + assert!(!on_disk(2) && !on_disk(3), "rendered without this disk cache"); + assert_eq!(disk.stats().entries, 2); +} + fn ram_frames_missing_on_disk_are_written_as(half: bool) { use effectcraft_engine::Session; use serde_json::json; @@ -1616,19 +1682,12 @@ fn ram_frames_missing_on_disk_are_written_as(half: bool) { FrameKey { revision: 1, content: f.content_of(&src.project, 1, cid), comp: cid.0, opts: opts_hash(&RenderOpts::default()) ^ 1, ..key(0, 0, 0) }; for fr in 0..10 { let frame = if half { FrameImage::CpuF16(Arc::new(F16Frame::from_image(&effectcraft_engine::render::Image::new(8, 8), SRGB))) } else { img() }; - f.cache.lock().unwrap().insert(FrameKey { frame: fr, ..series }, frame); + f.cache.lock().unwrap().insert_with(FrameKey { frame: fr, ..series }, frame, Some(Arc::default()), Some(DiskSince::of(&disk, disk.drop_generation()))); } let on_disk = |fr: i64| { disk.contains( disk_cache::Kind::Frame, - frame_disk_key( - &f.content_keys, - &src.project, - &FrameKey { frame: fr, ..series }, - opts_hash(&RenderOpts::default()), - f.virtual_epoch.load(Ordering::Relaxed), - None, - ), + frame_disk_key(&f.content_keys, &src.project, &FrameKey { frame: fr, ..series }, opts_hash(&RenderOpts::default())), ) }; let persist = || { @@ -1674,6 +1733,71 @@ fn ram_frames_missing_on_disk_are_written_as(half: bool) { let _ = std::fs::remove_dir_all(&dir); } +/// While free disk space holds the disk cache at 0, frames are not converted or compressed for +/// it (the frame workers spent about 8 ms per frame on writes that were then refused), and idle +/// passes don't keep converting the RAM frames; with room again they are written. +#[test] +fn no_frame_is_encoded_while_the_disk_has_no_room() { + use effectcraft_engine::Session; + use serde_json::json; + const GB: u64 = 1 << 30; + let dir = std::env::temp_dir().join(format!("ec-persist-full-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let disk = DiskCache::open(&dir, 20 * GB).unwrap(); + // 4 TB disk, 1 GB free: under the 10 GB reserve. + disk.set_disk_space(Some((4096 * GB, GB))); + assert!(!disk.accepts_writes()); + // The frame worker's write: refused up front. + assert!(!put_disk_frame(&disk, 1, &img(), &ReadFiles::default(), None, disk.drop_generation())); + let mut s = Session::default(); + let cid = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 1})).unwrap()["comp"].as_u64().unwrap()); + s.execute("layer.newSolid", json!({})).unwrap(); + let src = RenderSource { + project: s.project.clone(), + footage: Arc::new(effectcraft_engine::render::NoFootage), + expr: None, + layer_cache: Arc::default(), + gpu: None, + gpu_display: false, + disk: Some(disk.clone()), + }; + let f = Frames::default(); + let series = + FrameKey { revision: 1, content: f.content_of(&src.project, 1, cid), comp: cid.0, opts: opts_hash(&RenderOpts::default()) ^ 1, ..key(0, 0, 0) }; + for fr in 0..10 { + f.cache.lock().unwrap().insert_with(FrameKey { frame: fr, ..series }, img(), Some(Arc::default()), Some(DiskSince::of(&disk, disk.drop_generation()))); + } + // Idle passes: nothing to do, nothing converted. + for _ in 0..20 { + assert!(!f.persist_missing(&src, &series, &RenderOpts::default()), "no idle passes while there is no room"); + std::thread::sleep(std::time::Duration::from_millis(2)); + } + disk.flush(); + assert!(f.persisted.lock().unwrap().is_empty()); + assert_eq!((disk.encodes(), disk.stats().entries), (0, 0)); + // Room again: the RAM frames are written. + disk.set_disk_space(Some((4096 * GB, 500 * GB))); + let on_disk = |fr: i64| { + disk.contains( + disk_cache::Kind::Frame, + frame_disk_key(&f.content_keys, &src.project, &FrameKey { frame: fr, ..series }, opts_hash(&RenderOpts::default())), + ) + }; + let mut written = false; + for _ in 0..1000 { + f.persist_missing(&src, &series, &RenderOpts::default()); + disk.flush(); + if (0..10).all(on_disk) && f.persisted.lock().unwrap().is_empty() { + written = true; + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert!(written, "every RAM frame written once there is room"); + assert_eq!(disk.encodes(), 10); + let _ = std::fs::remove_dir_all(&dir); +} + /// Audio changes pixels only where something draws it: a clip's Audio Levels, audio effects and /// Audio switch keep every frame, in RAM and on disk, until a waveform picks the clip (on another /// layer, on itself, or through a precomp of it); then the frames of the reader render again. @@ -1681,7 +1805,7 @@ fn ram_frames_missing_on_disk_are_written_as(half: bool) { fn audio_edits_keep_the_frames_until_something_draws_the_audio() { use effectcraft_engine::Session; use effectcraft_engine::project::{Footage, GroupKind, LayerId, Value, build}; - use effectcraft_engine::render::disk_cache::comp_content_key_with; + use effectcraft_engine::render::disk_cache::comp_content_key; use serde_json::json; let mut s = Session::default(); let cid = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 4, "frameRate": 30})).unwrap()["comp"].as_u64().unwrap()); @@ -1736,7 +1860,7 @@ fn audio_edits_keep_the_frames_until_something_draws_the_audio() { let mut p1 = p0.clone(); edit(Arc::make_mut(&mut p1)); let id1 = f.content_of(&p1, 2, view); - (f.cached_frames(&k(id1, 0)), comp_content_key_with(&p0, view, None) == comp_content_key_with(&p1, view, None)) + (f.cached_frames(&k(id1, 0)), comp_content_key(&p0, view) == comp_content_key(&p1, view)) }; let all: Vec = (0..120).collect(); let edits: [(&str, &dyn Fn(&mut Project)); 3] = [("levels", &levels), ("audio effect", &backwards), ("audio switch", &mute)]; @@ -1933,9 +2057,9 @@ fn disk_frames_outlive_an_edit_that_cant_change_them() { disk: Some(disk.clone()), }; // Every frame of `p0` on disk, none in RAM. - let content0 = disk_cache::comp_content_key_with(&p0, cid, None); + let content0 = disk_cache::comp_content_key(&p0, cid); for f in 0..30 { - disk.put_frame_now(disk_cache::frame_key(content0, f, 1000, oh), 1, 1, &[1, 2, 3, 255]); + disk.put_frame_now(disk_cache::frame_key(content0, f, 1000, oh), 1, 1, &[1, 2, 3, 255], &ReadFiles::default()); } let rename: fn(&mut Project, ItemId, LayerId) = |p, c, l| p.comp_mut(c).unwrap().layer_mut(l).unwrap().name = "Renamed".into(); let trim: fn(&mut Project, ItemId, LayerId) = |p, c, l| { @@ -1982,6 +2106,80 @@ fn disk_frames_outlive_an_edit_that_cant_change_them() { let _ = std::fs::remove_dir_all(&dir); } +/// With expressions the disk key covers what they can read (#3184, #1492): an edit to a comp no +/// expression names keeps every frame on disk under the same key; an edit to the comp one names +/// gives the comp a new key, and the frames outside the layer with that expression are carried to +/// it (the dirty spans of #2795). +#[test] +fn disk_frames_outlive_edits_their_expressions_cant_read() { + use effectcraft_engine::Session; + use effectcraft_engine::project::{Expression, LayerId, Value}; + use serde_json::json; + let dir = std::env::temp_dir().join(format!("ec-disk-carry-expr-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let disk = DiskCache::open(&dir, 1 << 30).unwrap(); + let mut s = Session::default(); + let comp = |s: &mut Session, name: &str| { + ItemId(s.execute("comp.new", json!({"name": name, "width": 8, "height": 8, "duration": 1, "frameRate": 30})).unwrap()["comp"].as_u64().unwrap()) + }; + let solid = |s: &mut Session, c: ItemId| LayerId(s.execute("layer.newSolid", json!({"comp": c.0, "color": "#ff0000"})).unwrap()["layer"].as_u64().unwrap()); + let cid = comp(&mut s, "A"); + let (a, b) = (solid(&mut s, cid), solid(&mut s, cid)); + let other = comp(&mut s, "Other"); + let o = solid(&mut s, other); + let third = comp(&mut s, "Third"); + let t = solid(&mut s, third); + let fr = s.project.comp(cid).unwrap().frame_rate; + let mut p0 = (*s.project).clone(); + for (l, i, out) in [(a, 0, 15), (b, 15, 30)] { + let l = p0.comp_mut(cid).unwrap().layer_mut(l).unwrap(); + (l.start_time, l.in_point, l.out_point) = (fr.tick_of(i), fr.tick_of(i), fr.tick_of(out)); + } + p0.comp_mut(cid).unwrap().layer_mut(a).unwrap().props.prop_mut("transform/opacity").unwrap().expr = + Some(Expression { text: "comp(\"Other\").layer(1).transform.opacity".into(), enabled: true }); + let p0 = Arc::new(p0); + let opts = RenderOpts::default(); + let oh = opts_hash(&opts); + let src = |p: &Arc| RenderSource { + project: p.clone(), + footage: Arc::new(effectcraft_engine::render::NoFootage), + expr: None, + layer_cache: Arc::default(), + gpu: None, + gpu_display: false, + disk: Some(disk.clone()), + }; + let content0 = disk_cache::comp_content_key(&p0, cid); + for f in 0..30 { + disk.put_frame_now(disk_cache::frame_key(content0, f, 1000, oh), 1, 1, &[1, 2, 3, 255], &ReadFiles::default()); + } + let opacity = |p: &mut Project, c: ItemId, l: LayerId| { + p.comp_mut(c).unwrap().layer_mut(l).unwrap().props.prop_mut("transform/opacity").unwrap().value = Value::Scalar(40.0) + }; + for (name, edited, layer, same_key, kept) in [("Third", third, t, true, (0..30).collect::>()), ("Other", other, o, false, (17..30).collect())] { + let f = Frames::default(); + let series = |p: &Arc, rev: u64| FrameKey { revision: rev, content: f.content_of(p, rev, cid), comp: cid.0, opts: oh, ..key(0, 0, 0) }; + let blue = |p: &Arc, rev: u64, want: &[i64]| { + let base = series(p, rev); + let mut got = vec![]; + for _ in 0..1000 { + got = f.disk_frames(&src(p), &base, 0..30, &opts); + if got == want { + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + got + }; + assert_eq!(blue(&p0, 1, &(0..30).collect::>()), (0..30).collect::>(), "{name}: before"); + let mut p1 = p0.clone(); + opacity(Arc::make_mut(&mut p1), edited, layer); + assert_eq!(disk_cache::comp_content_key(&p1, cid) == content0, same_key, "{name}: the key"); + assert_eq!(blue(&p1, 2, &kept), kept, "{name}: after"); + } + let _ = std::fs::remove_dir_all(&dir); +} + /// A keyframe edit re-renders only around the keys it changed: two keys either side (auto-Bezier /// and continuous tangents reach the neighbouring segments), and from the layer's in point when /// the first key changes. A static value or an expression still reaches the whole layer. Every @@ -2041,7 +2239,7 @@ fn a_keyframe_edit_reaches_only_the_keys_around_it() { fn music_volume_keeps_the_frames_with_expressions_that_dont_read_audio() { use effectcraft_engine::Session; use effectcraft_engine::project::{Expression, Footage, FootageKind, LayerId, Value, build}; - use effectcraft_engine::render::disk_cache::comp_content_key_with; + use effectcraft_engine::render::disk_cache::comp_content_key; use serde_json::json; let mut s = Session::default(); let cid = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 4, "frameRate": 30})).unwrap()["comp"].as_u64().unwrap()); @@ -2069,7 +2267,7 @@ fn music_volume_keeps_the_frames_with_expressions_that_dont_read_audio() { let mut p1 = p0.clone(); Arc::make_mut(&mut p1).comp_mut(cid).unwrap().layer_mut(music_id).unwrap().props.prop_mut("audio/levels").unwrap().value = Value::Vec2([-6.0, -6.0]); let id1 = f.content_of(&p1, 2, cid); - (f.cached_frames(&k(id1, 0)).len(), comp_content_key_with(&p0, cid, None) == comp_content_key_with(&p1, cid, None)) + (f.cached_frames(&k(id1, 0)).len(), comp_content_key(&p0, cid) == comp_content_key(&p1, cid)) }; assert_eq!(kept("wiggle(2, 20)"), (120, true), "a wiggle reads no audio"); let (frames, disk) = kept("value + thisComp.layer(\"Music\")(\"Audio\")(\"Audio Levels\")[0]"); @@ -2449,3 +2647,313 @@ fn a_keyframe_edit_beside_muted_keys_reaches_the_keys_that_play() { assert!(same(i), "kept frame {i} renders differently after the edit"); } } + +/// Disk-cache frame entries that keep the source files they were made of (#3184): a sequence +/// comp in a folder of its own, with a large unused sequence in the project. +mod frame_files { + use super::*; + use effectcraft_engine::project::{Footage, FootageKind, build}; + use effectcraft_engine::time::FrameRate; + use std::path::{Path, PathBuf}; + use std::sync::atomic::AtomicUsize; + + const RATE: FrameRate = FrameRate::FPS_30; + const FRAMES: i64 = 5; + + /// A fresh folder for test `name` (on the build drive, not the system temp folder), deleted + /// when the guard is dropped. + fn folder(name: &str) -> tempfile::TempDir { + let target = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target"); + std::fs::create_dir_all(&target).unwrap(); + tempfile::Builder::new().prefix(&format!("test-frame-files-{name}-")).tempdir_in(target).unwrap() + } + + /// A flat grey PNG of `level` at `path`. + fn png(path: &Path, level: u8) -> String { + image::RgbaImage::from_pixel(16, 8, image::Rgba([level, level, level, 255])).save(path).unwrap(); + path.to_string_lossy().to_string() + } + + /// The grey level of sequence frame `i` as first written. + fn level(i: i64) -> u8 { + 20 + i as u8 * 30 + } + + /// A project with a comp showing a sequence of [`FRAMES`] PNGs (in `dir/seq`) and, unused, a + /// sequence of 5000 frames that don't even exist. The files of the shown sequence. + fn project(dir: &Path) -> (Arc, ItemId, Vec) { + let seq_dir = dir.join("seq"); + std::fs::create_dir_all(&seq_dir).unwrap(); + let files: Vec = (0..FRAMES).map(|i| png(&seq_dir.join(format!("f_{i:04}.png")), level(i))).collect(); + let mut seq = Footage { kind: FootageKind::Sequence, sequence: files.clone(), frame_rate: RATE, ..effectcraft_media::probe(&files[0]).unwrap() }; + seq.sync_sequence_duration(); + let unused = Footage { + kind: FootageKind::Sequence, + sequence: (0..5000).map(|i| dir.join("unused").join(format!("big_{i:05}.exr")).to_string_lossy().to_string()).collect(), + frame_rate: RATE, + width: 16, + height: 8, + has_video: true, + ..Default::default() + }; + let mut p = Project::default(); + p.add_item("Unused", effectcraft_engine::color::Label::Aqua, None, ItemKind::Footage(unused)); + let item = p.add_item("Seq", effectcraft_engine::color::Label::Aqua, None, ItemKind::Footage(seq)); + let mut comp = Comp::new(16, 8, RATE, RATE.tick_of(FRAMES)); + comp.layers = vec![build::layer(&mut p, &comp, "Seq", LayerSource::Footage { item }, (16, 8), Some(comp.duration))].into(); + let cid = p.add_item("C", effectcraft_engine::color::Label::None, None, ItemKind::Comp(comp.into())); + (Arc::new(p), cid, files) + } + + /// A session over the disk cache in `dir`: a new preview cache, media pool, layer cache and + /// disk cache instance (as after reopening the project), counting the calls of the hook for + /// files served from memory. + struct Session { + frames: Frames, + src: RenderSource, + disk: Arc, + hook_calls: Arc, + /// The session's media pool (`src.footage`): what it decoded and opened. + pool: Arc, + } + + fn session(p: &Arc, dir: &Path) -> Session { + let disk = DiskCache::open(dir.join("cache"), 1 << 30).unwrap(); + let hook_calls = Arc::new(AtomicUsize::new(0)); + let mut frames = Frames::default(); + let calls = hook_calls.clone(); + frames.set_virtual_id(Arc::new(move |_| { + calls.fetch_add(1, Ordering::Relaxed); + None + })); + let pool = Arc::new(effectcraft_media::MediaPool::new()); + let src = RenderSource { + project: p.clone(), + footage: pool.clone(), + expr: None, + layer_cache: Arc::default(), + gpu: None, + gpu_display: false, + disk: Some(disk.clone()), + }; + Session { frames, src, disk, hook_calls, pool } + } + + impl Session { + fn key(&self, cid: ItemId, frame: i64) -> FrameKey { + FrameKey { + revision: 1, + content: self.frames.content_of(&self.src.project, 1, cid), + comp: cid.0, + frame, + scale: 1000, + view: 0, + opts: opts_hash(&RenderOpts::default()), + } + } + + /// Show `frames` of `cid` (rendered or read from disk); the grey level of each, once + /// their disk writes are done. + fn show(&self, cid: ItemId, frames: &[i64]) -> Vec { + for &f in frames { + self.frames.request(&self.src, self.key(cid, f), cid, RATE.tick_of(f), RenderOpts::default()); + } + wait_for_renders(&self.frames); + // (the worker queues the write after it has released the frame) + for _ in 0..400 { + self.disk.flush(); + if frames.iter().all(|f| self.disk.contains(disk_cache::Kind::Frame, self.disk_key(cid, *f))) { + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + frames + .iter() + .map(|f| match self.frames.get(&self.key(cid, *f)) { + Some(FrameImage::Cpu(c)) => c.pixels[0].r(), + _ => panic!("frame {f}: an 8-bit CPU frame"), + }) + .collect() + } + + fn disk_key(&self, cid: ItemId, frame: i64) -> u128 { + frame_disk_key(&self.frames.content_keys, &self.src.project, &self.key(cid, frame), opts_hash(&RenderOpts::default())) + } + + /// (disk hits, disk misses) so far. + fn hits(&self) -> (u64, u64) { + let s = self.disk.stats(); + (s.hits, s.misses) + } + } + + fn close(a: u8, b: u8) -> bool { + a.abs_diff(b) <= 2 + } + + /// Give `path` a modification time `secs` later, contents and size unchanged. + fn touch(path: &str, secs: u64) { + let t = std::fs::metadata(path).unwrap().modified().unwrap() + std::time::Duration::from_secs(secs); + std::fs::File::options().append(true).open(path).unwrap().set_modified(t).unwrap(); + } + + /// Correct: a source file of the sequence changes (new contents and size; or only a new + /// time): exactly the frames that read it render again and their entries are replaced; the + /// others come from disk, also after reopening. + #[test] + fn a_changed_source_file_re_renders_exactly_its_frames() { + let tmp = folder("correct"); + let dir = tmp.path(); + let (p, cid, files) = project(dir); + let all: Vec = (0..FRAMES).collect(); + let first = session(&p, dir); + let shown = first.show(cid, &all); + assert!(all.iter().all(|f| close(shown[*f as usize], level(*f))), "{shown:?}"); + assert_eq!(first.hits(), (0, FRAMES as u64), "rendered, nothing on disk yet"); + assert_eq!(first.disk.stats().entries, FRAMES as usize, "every frame stored"); + drop(first); + // Frame 2's file gets new pixels (and size), frame 3's only a new time. + png(Path::new(&files[2]), 250); + touch(&files[3], 30); + let second = session(&p, dir); + let shown = second.show(cid, &all); + assert!(close(shown[2], 250), "frame 2 shows its new file: {shown:?}"); + assert!([0, 1, 3, 4].iter().all(|f| close(shown[*f as usize], level(*f))), "{shown:?}"); + assert_eq!(second.hits(), (3, 2), "frames 0, 1 and 4 from disk; 2 and 3 rendered again"); + assert_eq!(second.disk.file_checks(), FRAMES as u64, "one file checked per frame read"); + assert_eq!(second.disk.stats().entries, FRAMES as usize, "the entries of 2 and 3 were replaced"); + drop(second); + // Reopened: every frame from disk, the replaced ones with their new pixels. + let third = session(&p, dir); + let shown = third.show(cid, &all); + assert_eq!(third.hits(), (FRAMES as u64, 0)); + assert!(close(shown[2], 250) && close(shown[3], level(3)), "{shown:?}"); + } + + /// Reopened (a new preview cache, media pool, layer cache and disk cache on the same folder): + /// the frame comes from disk, its one source file is checked (a `metadata` call), and no + /// source file is opened or decoded for it. + #[test] + fn a_reopened_project_serves_its_frame_from_disk_without_opening_a_source_file() { + let tmp = folder("reopen"); + let dir = tmp.path(); + let (p, cid, _) = project(dir); + let first = session(&p, dir); + let shown = first.show(cid, &[1]); + assert!(close(shown[0], level(1)), "{shown:?}"); + assert!(first.pool.stats().misses > 0, "the first session decodes the frame's file: {:?}", first.pool.stats()); + assert!(first.disk.contains(disk_cache::Kind::Frame, first.disk_key(cid, 1))); + drop(first); + let again = session(&p, dir); + let shown = again.show(cid, &[1]); + assert!(close(shown[0], level(1)), "{shown:?}"); + assert_eq!(again.hits(), (1, 0), "served from disk"); + assert_eq!(again.disk.file_checks(), 1, "its one file checked"); + assert_eq!(again.pool.stats(), effectcraft_media::PoolStats::default(), "no source file opened or decoded"); + } + + /// No scan: opening a comp of a project that also holds a large sequence, and showing a + /// frame, looks only at the files that frame reads; the comp's key looks at no file (it + /// stays the same when its files change, and never asks for the identity of a path). + #[test] + fn showing_a_frame_checks_only_the_files_it_reads() { + let tmp = folder("no-scan"); + let dir = tmp.path(); + let (p, cid, files) = project(dir); + let key = disk_cache::comp_content_key(&p, cid); + png(Path::new(&files[1]), 99); + touch(&files[2], 5); + assert_eq!(disk_cache::comp_content_key(&p, cid), key, "the key looks at no file"); + let first = session(&p, dir); + first.show(cid, &[2]); + drop(first); + let again = session(&p, dir); + // (the indicator's key is computed off the UI thread first) + let mut blue = vec![]; + for _ in 0..400 { + blue = again.frames.disk_frames(&again.src, &again.key(cid, 0), 0..FRAMES, &RenderOpts::default()); + if !blue.is_empty() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + assert_eq!(blue, vec![2], "the blue bar: present entries, no file looked at"); + assert_eq!(again.disk.file_checks(), 0, "the indicator checks no file"); + let shown = again.show(cid, &[2]); + assert!(close(shown[0], level(2)), "{shown:?}"); + assert_eq!(again.hits(), (1, 0)); + assert_eq!(again.disk.file_checks(), 1, "only the file frame 2 read"); + assert_eq!(again.hook_calls.load(Ordering::Relaxed), 0, "no path of the unused sequence was asked about"); + } + + /// The frame entry files under the cache folder. + fn entries(dir: &Path) -> Vec { + let mut out = vec![]; + let mut stack = vec![dir.join("cache")]; + while let Some(d) = stack.pop() { + for e in std::fs::read_dir(&d).into_iter().flatten().flatten() { + let p = e.path(); + if p.is_dir() { + stack.push(p); + } else if p.extension().is_some_and(|x| x == "ecc") && p.to_string_lossy().contains("frames") { + out.push(p); + } + } + } + out + } + + /// Safe: a missing source file, an unreadable one (a folder in its place) and a damaged + /// cache entry never serve the frame from before; each is rendered again and stored anew. + #[test] + fn missing_or_unreadable_files_and_damaged_entries_never_serve_a_stale_frame() { + let tmp = folder("safe"); + let dir = tmp.path(); + let (p, cid, files) = project(dir); + let first = session(&p, dir); + first.show(cid, &[0]); + assert_eq!(first.disk.stats().entries, 1); + drop(first); + // Missing: not served; the frame is rendered without it. + std::fs::remove_file(&files[0]).unwrap(); + let missing = session(&p, dir); + let shown = missing.show(cid, &[0]); + assert!(!close(shown[0], level(0)), "the frame from before was served: {shown:?}"); + assert_eq!(missing.hits(), (0, 1)); + drop(missing); + // Back, with other pixels: the entry made while it was missing is not served either. + png(Path::new(&files[0]), 180); + let back = session(&p, dir); + let shown = back.show(cid, &[0]); + assert!(close(shown[0], 180), "{shown:?}"); + assert_eq!(back.hits(), (0, 1)); + drop(back); + // Unreadable (a folder in its place). + std::fs::remove_file(&files[0]).unwrap(); + std::fs::create_dir(&files[0]).unwrap(); + let unreadable = session(&p, dir); + let shown = unreadable.show(cid, &[0]); + assert!(!close(shown[0], 180), "the frame from before was served: {shown:?}"); + assert_eq!(unreadable.hits(), (0, 1)); + drop(unreadable); + std::fs::remove_dir(&files[0]).unwrap(); + png(Path::new(&files[0]), 120); + let fresh = session(&p, dir); + assert!(close(fresh.show(cid, &[0])[0], 120)); + drop(fresh); + // A damaged entry: a miss, rendered, stored again, then served. + let found = entries(dir); + assert_eq!(found.len(), 1, "{found:?}"); + let mut bytes = std::fs::read(&found[0]).unwrap(); + let n = bytes.len() / 2; + bytes[n] ^= 0x5a; + std::fs::write(&found[0], bytes).unwrap(); + let damaged = session(&p, dir); + assert!(close(damaged.show(cid, &[0])[0], 120)); + assert_eq!(damaged.hits(), (0, 1)); + drop(damaged); + let healed = session(&p, dir); + assert!(close(healed.show(cid, &[0])[0], 120)); + assert_eq!(healed.hits(), (1, 0), "the rewritten entry is used"); + } +} diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 0c7573c91..76a70426e 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -163,6 +163,7 @@ fn vertex_delta(app: &EffectcraftApp, layer: effectcraft_engine::project::LayerI footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let (l2c, _) = crate::panels::viewer::l2c(&ectx, l); l2c.inverse().map_or(d, |inv| { diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 0857dfa3f..39fd4847a 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -186,6 +186,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let hdr = Rect::from_min_size(rect.min, vec2(rect.width(), 24.0)); p.text(pos2(hdr.min.x + 10.0, hdr.center().y), Align2::LEFT_CENTER, format!("{} • {}", comp_name, layer.name), Tokens::ui(11.5), t.text_dim); diff --git a/crates/ui-egui/src/panels/layer_panel.rs b/crates/ui-egui/src/panels/layer_panel.rs index 8e4f3502f..e23e11cfb 100644 --- a/crates/ui-egui/src/panels/layer_panel.rs +++ b/crates/ui-egui/src/panels/layer_panel.rs @@ -113,6 +113,7 @@ fn render_tex( footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let mut r = Renderer::new(&app.session.project, &*app.session.footage, RenderOpts { scale, ..Default::default() }); r.expr = app.session.expr.as_deref(); diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 25829be7d..4edcbb31a 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -79,6 +79,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let scroll = widgets::PanelScroll::begin(ui, egui::Id::unique("props-scroll"), rect); let rect = scroll.content; diff --git a/crates/ui-egui/src/panels/roto_tool.rs b/crates/ui-egui/src/panels/roto_tool.rs index 194bba26a..c01a8f07e 100644 --- a/crates/ui-egui/src/panels/roto_tool.rs +++ b/crates/ui-egui/src/panels/roto_tool.rs @@ -114,6 +114,7 @@ pub fn draw_overlay( footage: None, property_overrides: &[], media_overrides: &[], + reads: None, }; let mut r = Renderer::new(&app.session.project, &*app.session.footage, RenderOpts { scale, ..Default::default() }); r.expr = app.session.expr.as_deref(); diff --git a/crates/ui-egui/src/panels/settings.rs b/crates/ui-egui/src/panels/settings.rs index 72564f112..cdeb85116 100644 --- a/crates/ui-egui/src/panels/settings.rs +++ b/crates/ui-egui/src/panels/settings.rs @@ -5,7 +5,8 @@ //! //! Automation ids: `settings.page.`, `settings.` (e.g. `settings.general.undoLevels`), //! `settings.labels..name` / `.color`, `settings.button.