EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Merge upstream/main into fix-1083-beat-marker-audio-pause
vangel764c8b89a6bed4
diff --cc crates/project/src/lib.rsindex acab736a2,8a4bbeb3c..3823f2cb2--- a/crates/project/src/lib.rs+++ b/crates/project/src/lib.rs@@@ -868,15 -852,12 +868,20 @@@ impl Comp/// Draws the same pixels as `o`: equal but for its layers' Audio, Lock and Shy switches, Hide/// Shy Layers and the viewer's Resolution.pub fn same_pixels(&self, o: &Comp) -> bool {- self.same_settings(o) && self.layers.len() == o.layers.len() && self.layers.iter().zip(&o.layers).all(|(a, b)| a.same_pixels(b))+ self.same_pixels_with(o, true)}- /// [`Self::same_pixels`] but for the layers.- fn same_settings(&self, o: &Comp) -> bool {+ /// [`Self::same_pixels`], with the comp's and its layers' markers counting only when+ /// `with_markers`: only expressions read them, so while none does, adding or moving a marker+ /// (even during playback) leaves every rendered frame valid. Protected time markers with a+ /// duration set the timing (Responsive Design), so those always count.+ pub fn same_pixels_with(&self, o: &Comp, with_markers: bool) -> bool {- // Every field (no `..`: a new field must be considered here); the layers last.++ self.same_settings(o, with_markers) && self.layers.len() == o.layers.len() && self.layers.iter().zip(&o.layers).all(|(a, b)| a.same_pixels_with(b, with_markers))++ }++++ /// [`Self::same_pixels_with`] but for the layers.++ fn same_settings(&self, o: &Comp, with_markers: bool) -> bool {+ // Every field (no `..`: a new field must be considered here).let Comp {width,height,@@@ -928,10 -909,80 +933,63 @@@&& *global_light == o.global_light&& *guides == o.guides&& *essential == o.essential- && layers.len() == o.layers.len()- && layers.iter().zip(&o.layers).all(|(a, b)| a.same_pixels_with(b, with_markers))+ }++ /// The comp-time span (`in_point..out_point` of every layer changed, added or removed,+ /// widened by the shutter) outside which `self` draws the same pixels as `o`, so cached+ /// frames there stay valid (#1641). `None` when the edit can reach any frame: a comp setting,+ /// a layer reorder, Solo turning on or off, an expression (it reads anything), or a changed+ /// layer another layer parents to, mattes with or picks in an effect.+ pub fn changed_span(&self, o: &Comp) -> Option<(Tick, Tick)> {+ fn reads(g: &PropGroup, changed: &[LayerId]) -> bool {+ g.children.iter().any(|c| match c {+ Node::Prop(p) => {+ p.expr.is_some()+ || std::iter::once(&p.value)+ .chain(p.keys.iter().map(|k| &k.value))+ .any(|v| matches!(v, Value::Layer(Some(id)) if changed.contains(&LayerId(*id))))+ }+ Node::Group(g) => reads(g, changed),+ })+ }+ // Solo hides every other layer at every frame.- if !self.same_settings(o) || self.any_solo() != o.any_solo() {++ if !self.same_settings(o, true) || self.any_solo() != o.any_solo() {+ return None;+ }+ let common = |a: &Comp, b: &Comp| a.layers.iter().map(|l| l.id).filter(|id| b.layer(*id).is_some()).collect::<Vec<_>>();+ if common(self, o) != common(o, self) {+ return None;+ }+ let mut changed = Vec::new();+ let mut span: Option<(Tick, Tick)> = None;+ // Each changed layer comes up from both sides: its old and its new span.+ for (l, other) in self.layers.iter().map(|l| (l, o.layer(l.id))).chain(o.layers.iter().map(|l| (l, self.layer(l.id)))) {+ if other.is_some_and(|b| l.same_pixels(b)) {+ continue;+ }+ changed.push(l.id);+ let (s, e) = (l.in_point.min(l.out_point), l.in_point.max(l.out_point));+ span = Some(span.map_or((s, e), |(a, b)| (a.min(s), b.max(e))));+ }+ let reaches = |l: &Layer| {+ l.parent.is_some_and(|p| changed.contains(&p)) || l.track_matte.as_ref().is_some_and(|m| changed.contains(&m.layer)) || reads(&l.props, &changed)+ };+ if self.layers.iter().chain(&o.layers).any(reaches) {+ return None;+ }+ // A frame at `t` samples cameras, lights and transforms over its shutter,+ // `t + (phase + angle * 0..=1) / 360` frames, and draws the layers active at `t` itself.+ let (a, p) = (self.shutter_angle / 360.0, self.shutter_phase / 360.0);+ let fd = self.frame_duration().seconds();+ let (before, after) = (Tick::from_seconds_f64(p.max(p + a).max(0.0) * fd), Tick::from_seconds_f64(-p.min(p + a).min(0.0) * fd));+ span.map(|(s, e)| (s - before, e + after))+ }++ /// Some layer is soloed: only the soloed layers draw.+ pub fn any_solo(&self) -> bool {+ self.layers.iter().any(|l| l.switches.solo && l.source.is_av())}- /// Draws the same pixels as `o` once plain markers are ignored: they change no pixels. Protected- /// time markers with a duration set the timing (Responsive Design), so those still count.- pub fn same_pixels_ignoring_markers(&self, o: &Comp) -> bool {- let protected =- |m: &[Marker]| -> Vec<(Tick, Tick)> { m.iter().filter(|m| m.protected && m.duration > Tick::ZERO).map(|m| (m.time, m.duration)).collect() };- if protected(&self.markers) != protected(&o.markers) {- return false;- }- let mut c = self.clone();- c.markers = o.markers.clone();- for (a, b) in c.layers.iter_mut().zip(&o.layers) {- a.markers = b.markers.clone();- a.markers_locked = b.markers_locked;- }- c.same_pixels(o)- }-/// The comp with those switches and the Resolution at their defaults (borrowed when they/// already are), for keys that hash the whole comp.pub fn pixel_form(&self) -> std::borrow::Cow<'_, Comp> {diff --cc crates/ui-egui/src/frames.rsindex 345072936,ed00690ac..6beccf6cf--- a/crates/ui-egui/src/frames.rs+++ b/crates/ui-egui/src/frames.rs@@@ -301,11 -314,29 +314,12 @@@ fn content_key(keys: &ContentKeys, proj/// hands out), and by value the project settings and the footage, solids and proxies it uses./// With expressions in its comps (which can read anything) every item counts.pub fn comp_content(project: &Project, comp: ItemId) -> u64 {- comp_content_with(project, comp, |_, c| Arc::as_ptr(c))+ comp_content_with(project, comp, project, |_, c| c)}- /// [`comp_content`] with each comp's address given by `addr`.- fn comp_content_with(project: &Project, comp: ItemId, addr: impl Fn(ItemId, &Arc<Comp>) -> *const Comp) -> u64 {-/// Whether two versions of a comp draw the same pixels. Markers only count when an expression-/// could read them (`markers_count`).-fn same_pixels(a: &Comp, b: &Comp, markers_count: bool) -> bool {- if markers_count { a.same_pixels(b) } else { a.same_pixels_ignoring_markers(b) }-}--/// Whether a layer anywhere in the project has an expression, which can read markers.-fn project_has_expressions(project: &Project) -> bool {- fn has_expression(g: &PropGroup) -> bool {- g.children.iter().any(|c| match c {- Node::Prop(p) => p.expr.is_some(),- Node::Group(sub) => has_expression(sub),- })- }- project.items.values().any(|i| matches!(&i.kind, ItemKind::Comp(c) if c.layers.iter().any(|l| has_expression(&l.props))))-}-+ /// [`comp_content`] with each comp's version selected for both its sources and its address.+ /// Sources removed by undo are read from `base`.+ fn comp_content_with<'a>(project: &'a Project, comp: ItemId, base: &'a Project, version: impl Fn(ItemId, &'a Arc<Comp>) -> &'a Arc<Comp>) -> u64 {use std::hash::{Hash, Hasher};fn has_expression(g: &PropGroup) -> bool {g.children.iter().any(|c| match c {@@@ -729,17 -762,32 +745,32 @@@ impl Framesfn identity(&self, project: &Arc<Project>, comp: ItemId) -> Identity {let plain = comp_content(project, comp);let Ok(mut bases) = self.bases.lock() else { return (plain, None) };- // Markers can only change pixels through expressions, which read them.- let markers_count = project_has_expressions(project);+ // Markers are pixels only to the expressions that read them (#1083).+ let markers = project.reads_markers();if let Some((base_id, base)) = bases.get(&comp.0)&& *base_id != plain- && comp_content_with(project, comp, |id, c| match base.item(id).map(|i| &i.kind) {- Some(ItemKind::Comp(b)) if Arc::ptr_eq(b, c) || b.same_pixels_with(c, markers) => Arc::as_ptr(b),- _ => Arc::as_ptr(c),+ && comp_content_with(project, comp, project, |id, c| match base.item(id).map(|i| &i.kind) {- Some(ItemKind::Comp(b)) if Arc::ptr_eq(b, c) || same_pixels(b, c, markers_count) => b,++ Some(ItemKind::Comp(b)) if Arc::ptr_eq(b, c) || b.same_pixels_with(c, markers) => b,+ _ => c,}) == *base_id{return (*base_id, Some(base.clone()));}+ // Only layers of this comp changed: its frames outside their span (and a frame either+ // side for rounding) keep drawing the same pixels, so a cut or trim recaches only the+ // layers it touched (#1641).+ if let Some((base_id, base)) = bases.get(&comp.0)+ && let (Some(old), Some(new)) = (base.comp(comp), project.comp(comp))+ && comp_content_with(project, comp, base, |id, c| match base.item(id).map(|i| &i.kind) {- Some(ItemKind::Comp(b)) if id == comp || Arc::ptr_eq(b, c) || same_pixels(b, c, markers_count) => b,++ Some(ItemKind::Comp(b)) if id == comp || Arc::ptr_eq(b, c) || b.same_pixels_with(c, markers) => b,+ _ => c,+ }) == *base_id+ && let Some((s, e)) = new.changed_span(old)+ && let Ok(mut cache) = self.cache.lock()+ {+ let (first, last) = (new.frame_rate.frame_at(s).saturating_sub(1), new.frame_rate.frame_at(e).saturating_add(1));+ cache.carry(*base_id, plain, |f| f < first || f > last);+ }if bases.len() > MAX_KEEPERS {bases.clear();}@@@ -2168,11 -2216,158 +2199,161 @@@ mod testsassert_ne!(at(&s), o1);}+ /// A cut or trim recaches only the frames of the layers it touched (#1641), and none are+ /// kept when the change reaches other layers (here a child of the trimmed layer).+ #[test]+ fn an_edit_keeps_the_frames_outside_the_layers_it_changed() {+ use effectcraft_engine::Session;+ use serde_json::json;+ let mut s = Session::default();+ let id = |v: serde_json::Value, k: &str| v[k].as_u64().unwrap();+ let comp = ItemId(id(s.execute("comp.new", json!({"name": "Edit", "width": 8, "height": 8, "duration": 10})).unwrap(), "comp"));+ let a = id(s.execute("layer.newSolid", json!({"comp": comp.0, "color": "#ff0000"})).unwrap(), "layer");+ let b = id(s.execute("layer.newSolid", json!({"comp": comp.0, "color": "#00ff00"})).unwrap(), "layer");+ s.execute("layer.timing", json!({"layers": [a], "in": 0, "out": 4})).unwrap();+ s.execute("layer.timing", json!({"layers": [b], "in": 4, "out": 10})).unwrap();+ let (fr, out) = s.project.comp(comp).map(|c| (c.frame_rate, c.layers.iter().find(|l| l.id.0 == a).unwrap().out_point)).unwrap();+ let frames: Vec<i64> = (0..fr.frame_at(Tick::from_seconds_f64(10.0))).collect();+ let f = Frames::default();+ let cache_all = |s: &Session| {+ let c = f.content_of(&s.project, s.revision, comp);+ frames.iter().for_each(|&n| insert(&f, key(c, n, 0)));+ c+ };+ let cached = |c: u64| frames.iter().filter(|&&n| f.is_cached(&key(c, n, 0))).copied().collect::<Vec<_>>();+ let before = cache_all(&s);+ s.execute("time.set", json!({"time": 2.0})).unwrap();+ s.execute("edit.splitLayer", json!({"layers": [a]})).unwrap();+ let after = f.content_of(&s.project, s.revision, comp);+ assert_ne!(after, before);+ let kept: Vec<i64> = (fr.frame_at(out) + 2..frames.len() as i64).collect();+ assert_eq!(cached(after), kept, "the cut layer's frames (and one either side) render again; the rest stay");++ s.execute("layer.setParent", json!({"layers": [b], "parent": a})).unwrap();+ let parented = cache_all(&s);+ s.execute("layer.timing", json!({"layers": [a], "out": 1})).unwrap();+ assert_eq!(cached(f.content_of(&s.project, s.revision, comp)), Vec::<i64>::new(), "the trimmed layer's child moves everywhere");+ assert_eq!(cached(parented).len(), frames.len(), "the earlier frames stay for undo");+ }++ /// Every frame an edit keeps renders as a fresh render of the edited project does: Solo+ /// hides the other layers at every frame, and a camera reaches the frames whose shutter+ /// samples it (#1641 review).+ #[test]+ fn the_frames_an_edit_keeps_match_a_fresh_render() {+ use effectcraft_engine::Session;+ use effectcraft_engine::render::NoFootage;+ use serde_json::json;+ // Cache every frame of `comp`, run `edit`, check the frames kept against fresh renders.+ fn kept(s: &mut Session, comp: ItemId, edit: serde_json::Value) -> Vec<i64> {+ let (fr, n) = s.project.comp(comp).map(|c| (c.frame_rate, c.frame_rate.frame_at(c.duration))).unwrap();+ let f = Frames::default();+ let before = f.content_of(&s.project, s.revision, comp);+ (0..n).for_each(|i| insert(&f, key(before, i, 0)));+ let old = s.project.clone();+ s.execute(edit["cmd"].as_str().unwrap(), edit["params"].clone()).unwrap();+ let after = f.content_of(&s.project, s.revision, comp);+ let kept: Vec<i64> = (0..n).filter(|&i| f.is_cached(&key(after, i, 0))).collect();+ let render = |p, i| Renderer::new(p, &NoFootage, RenderOpts::default()).comp_frame(comp, fr.tick_of(i));+ for &i in &kept {+ assert_eq!(render(&old, i), render(&s.project, i), "kept frame {i} draws other pixels now");+ }+ kept+ }+ let id = |v: serde_json::Value, k: &str| v[k].as_u64().unwrap();+ let mut s = Session::default();++ // Solo on a 2–3 s layer hides the 10 s underlay everywhere; turning it off shows it again.+ let comp = ItemId(id(s.execute("comp.new", json!({"name": "Solo", "width": 8, "height": 8, "duration": 10})).unwrap(), "comp"));+ s.execute("layer.newSolid", json!({"comp": comp.0, "color": "#ff0000"})).unwrap();+ let short = id(s.execute("layer.newSolid", json!({"comp": comp.0, "color": "#00ff00"})).unwrap(), "layer");+ s.execute("layer.timing", json!({"layers": [short], "in": 2, "out": 3})).unwrap();+ let solo = |on: bool| json!({"cmd": "layer.setSwitch", "params": {"layers": [short], "switch": "solo", "value": on}});+ assert_eq!(kept(&mut s, comp, solo(true)), Vec::<i64>::new());+ assert_eq!(kept(&mut s, comp, solo(false)), Vec::<i64>::new());++ // A 2–3 s camera over a 4 s motion-blurred 3D layer, shutter 720° at phase 360°: a frame+ // samples 1 to 3 frames ahead, so moving the camera reaches the 3 frames before it.+ let comp = ItemId(id(+ s.execute("comp.new", json!({"name": "Cam", "width": 16, "height": 16, "duration": 4, "frameRate": 30, "shutterAngle": 720, "shutterPhase": 360}))+ .unwrap(),+ "comp",+ ));+ let plane = id(s.execute("layer.newSolid", json!({"comp": comp.0, "color": "#ff0000"})).unwrap(), "layer");+ for sw in ["threeD", "motionBlur"] {+ s.execute("layer.setSwitch", json!({"layers": [plane], "switch": sw, "value": true})).unwrap();+ }+ let cam = id(s.execute("layer.newCamera", json!({"comp": comp.0})).unwrap(), "layer");+ s.execute("layer.timing", json!({"layers": [cam], "in": 2, "out": 3})).unwrap();+ let moved =+ kept(&mut s, comp, json!({"cmd": "prop.set", "params": {"comp": comp.0, "layer": cam, "path": "transform/position", "value": [40, 8, -30]}}));+ assert!(moved.contains(&0) && moved.contains(&100), "frames far from the camera stay: {moved:?}");+ assert!(!(56..=91).any(|f| moved.contains(&f)), "frames whose shutter samples the camera render again: {moved:?}");+ }++ #[test]+ fn short_solid_additions_and_removals_keep_unaffected_frames() {+ use effectcraft_engine::Session;+ use serde_json::json;+ let mut s = Session::default();+ let comp = ItemId(s.execute("comp.new", json!({"width": 8, "height": 8, "frameRate": 30, "duration": 20})).unwrap()["comp"].as_u64().unwrap());+ s.execute("layer.newSolid", json!({"color": "#ff0000"})).unwrap();+ let f = Frames::default();+ let cache_all = |s: &Session| {+ let content = f.content_of(&s.project, s.revision, comp);+ (0..600).for_each(|n| insert(&f, key(content, n, 0)));+ };+ let cached = |s: &Session| f.cached_frames(&key(f.content_of(&s.project, s.revision, comp), 0, 0));+ // 2–3 s, a quarter frame of the default shutter and a frame of rounding either side.+ let kept: Vec<i64> = (0..=57).chain(92..600).collect();+ cache_all(&s);+ let solid = s.execute("layer.newSolid", json!({"color": "#00ff00"})).unwrap()["layer"].as_u64().unwrap();+ s.execute("layer.timing", json!({"layers": [solid], "in": 2, "out": 3})).unwrap();+ assert_eq!(cached(&s), kept, "adding a source invalidates only frames 58..=91");+ cache_all(&s);+ s.execute("edit.undo", json!({})).unwrap();+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(cached(&s), (0..600).collect::<Vec<_>>(), "undoing the addition restores every frame");+ s.execute("edit.redo", json!({})).unwrap();+ s.execute("edit.redo", json!({})).unwrap();+ assert_eq!(cached(&s), (0..600).collect::<Vec<_>>(), "redoing the addition restores every frame");+ s.execute("edit.clear", json!({"layers": [solid]})).unwrap();+ assert_eq!(cached(&s), kept, "removing its layer invalidates the same span");+ }++ #[test]+ fn adding_a_source_does_not_hide_changes_to_existing_dependencies() {+ use effectcraft_engine::Session;+ use serde_json::json;+ for (command, params) in [+ ("layer.settings", json!({"comp": "Outer", "layer": "Underlay", "color": "#0000ff"})),+ ("prop.set", json!({"comp": "Inner", "layer": "Inner solid", "path": "transform/opacity", "value": 40})),+ ] {+ let mut s = Session::default();+ let comp = ItemId(+ s.execute("comp.new", json!({"name": "Outer", "width": 8, "height": 8, "frameRate": 30, "duration": 20})).unwrap()["comp"].as_u64().unwrap(),+ );+ s.execute("layer.newSolid", json!({"name": "Underlay", "color": "#ff0000"})).unwrap();+ s.execute("comp.new", json!({"name": "Inner", "width": 8, "height": 8, "duration": 20})).unwrap();+ s.execute("layer.newSolid", json!({"name": "Inner solid", "color": "#00ff00"})).unwrap();+ s.execute("comp.open", json!({"comp": "Outer"})).unwrap();+ s.execute("layer.addItem", json!({"item": "Inner"})).unwrap();+ let f = Frames::default();+ let before = f.content_of(&s.project, s.revision, comp);+ (0..600).for_each(|n| insert(&f, key(before, n, 0)));+ s.execute(command, params).unwrap();+ let solid = s.execute("layer.newSolid", json!({"color": "#ffffff"})).unwrap()["layer"].as_u64().unwrap();+ s.execute("layer.timing", json!({"layers": [solid], "in": 2, "out": 3})).unwrap();+ let after = f.content_of(&s.project, s.revision, comp);+ assert!(f.cached_frames(&key(after, 0, 0)).is_empty(), "{command} changes pixels outside the added layer");+ }+ }++ /// Adding, moving or deleting a marker (Num* while a preview plays) keeps the cached frames:+ /// the sound would otherwise stop at the next frame that has to be rendered again (#1083).+ /// Only expressions that read markers make them part of the pixels.#[test]- fn markers_keep_the_frames() {+ fn markers_keep_the_frames_unless_an_expression_reads_them() {use effectcraft_engine::Session;use serde_json::json;let mut s = Session::default();