Skip to content

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

vangel769fc04e8fe380
diff --cc crates/project/src/comp.rs
index 000000000,2ff97cd37..b9eb587e4
mode 000000,100644..100644
--- a/crates/project/src/comp.rs
+++ b/crates/project/src/comp.rs
@@@ -1,0 -1,427 +1,446 @@@
+ use super::item::LayerId;
+ use super::layer::{Layer, LayerSource};
+ use super::settings::{Marker, yes};
+ use super::*;
+
+ /// 3D renderer of a composition.
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum Renderer {
+ #[default]
+ Classic3D,
+ Advanced3D,
+ }
+
+ /// A composition's Resolution (the Composition panel's Resolution/Down Sample Factor popup, View ▸
+ /// Resolution): how many of its pixels the viewer renders. Auto follows the magnification.
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum Resolution {
+ #[default]
+ Auto,
+ Full,
+ Half,
+ Third,
+ Quarter,
+ /// View ▸ Resolution ▸ Custom: render every n-th pixel (2..=40).
+ Custom(u8),
+ }
+
+ impl Resolution {
+ pub const ALL: [Resolution; 5] = [Resolution::Auto, Resolution::Full, Resolution::Half, Resolution::Third, Resolution::Quarter];
+ pub fn label(self) -> &'static str {
+ match self {
+ Resolution::Auto => "Auto",
+ Resolution::Full => "Full",
+ Resolution::Half => "Half",
+ Resolution::Third => "Third",
+ Resolution::Quarter => "Quarter",
+ Resolution::Custom(_) => "Custom",
+ }
+ }
+ pub fn is_auto(&self) -> bool {
+ *self == Resolution::Auto
+ }
+ /// The down-sample factor (render every n-th pixel); `None` for Auto.
+ pub fn factor(self) -> Option<u8> {
+ match self {
+ Resolution::Auto => None,
+ Resolution::Full => Some(1),
+ Resolution::Half => Some(2),
+ Resolution::Third => Some(3),
+ Resolution::Quarter => Some(4),
+ Resolution::Custom(n) => Some(n),
+ }
+ }
+ /// The resolution that renders every `n`-th pixel (1 = Full … 4 = Quarter; up to 40).
+ pub fn from_factor(n: u64) -> Resolution {
+ match n.clamp(1, 40) {
+ 1 => Resolution::Full,
+ 2 => Resolution::Half,
+ 3 => Resolution::Third,
+ 4 => Resolution::Quarter,
+ n => Resolution::Custom(n as u8),
+ }
+ }
+ /// Render scale for a viewer magnification (and display pixel density).
+ pub fn scale(self, zoom: f32, ppp: f32) -> f64 {
+ match self.factor() {
+ Some(n) => 1.0 / f64::from(n.clamp(1, 40)),
+ // As in After Effects, Auto renders the pixels the magnification needs: the coarsest
+ // whole factor that still gives every screen pixel a rendered one (Full above 50 %,
+ // Half down to 33.3 %, Third down to 25 %, Quarter below), never fewer (#417).
+ None => {
+ let n = (1.0 / (zoom * ppp) as f64 + 1e-3).floor();
+ if n >= 1.0 { 1.0 / n.min(4.0) } else { 1.0 }
+ }
+ }
+ }
+ }
+
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Comp {
+ pub width: u32,
+ pub height: u32,
+ pub pixel_aspect: f64,
+ pub frame_rate: FrameRate,
+ pub duration: Tick,
+ /// Start timecode/frame shown for time 0.
+ pub display_start: Tick,
+ pub background: [f32; 3],
+ pub work_area: (Tick, Tick),
+ /// Layer #1 first (top of the stack).
+ pub layers: Vec<Layer>,
+ #[serde(default)]
+ pub markers: Vec<Marker>,
+ pub shutter_angle: f64,
+ pub shutter_phase: f64,
+ /// Samples Per Frame: the minimum motion-blur samples (and the count for 3D layers).
+ pub motion_blur_samples: u32,
+ /// Adaptive Sample Limit (Composition Settings ▸ Advanced): the most samples a 2D layer gets
+ /// when it moves far within the shutter.
+ #[serde(default = "adaptive_limit_default")]
+ pub motion_blur_adaptive_limit: u32,
+ #[serde(default)]
+ pub renderer: Renderer,
+ /// Comp switches (timeline toolbar).
+ #[serde(default)]
+ pub hide_shy: bool,
+ #[serde(default = "yes")]
+ pub enable_motion_blur: bool,
+ #[serde(default = "yes")]
+ pub enable_frame_blending: bool,
+ #[serde(default)]
+ pub draft_3d: bool,
+ /// Composition Settings ▸ Advanced ▸ Preserve frame rate when nested or in render queue:
+ /// nested in another comp, or rendered at another rate, it shows only its own frames.
+ #[serde(default)]
+ pub preserve_frame_rate: bool,
+ /// Composition Settings ▸ Advanced ▸ Preserve resolution when nested: nested in a comp
+ /// previewed at a lower resolution, it still renders at full size.
+ #[serde(default)]
+ pub preserve_resolution: bool,
+ #[serde(default)]
+ pub poster_time: Tick,
+ /// Global Light for layer styles (Layer ▸ Layer Styles ▸ Blending Options).
+ #[serde(default)]
+ pub global_light: styles::GlobalLight,
+ /// Viewer guides (View ▸ Add Guide…), in comp pixels.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub guides: Vec<Guide>,
+ /// Essential Graphics: the controls this comp exposes to instances and templates.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub essential: Option<essential::EssentialGraphics>,
+ /// The Composition panel's Resolution, saved with the project as in After Effects. It changes
+ /// what the viewer renders, not the comp's pixels ([`Comp::same_pixels`]), and stays out of
+ /// the undo history.
+ #[serde(default, skip_serializing_if = "Resolution::is_auto")]
+ pub resolution: Resolution,
+ }
+
+ /// A viewer guide line: vertical guides sit at an x position, horizontal ones at a y position.
+ #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Guide {
+ pub vertical: bool,
+ /// Comp pixels, or percent of the comp's width (vertical) / height (horizontal) when
+ /// [`Guide::percent`] is set.
+ pub position: f64,
+ /// A percentage guide (After Effects 26.5): `position` is a percent of the comp's size, so the
+ /// guide keeps its relative place when the comp is resized. Absent in older projects.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub percent: bool,
+ }
+
+ impl Guide {
+ /// The comp's extent along this guide's axis, never zero.
+ fn extent(&self, width: u32, height: u32) -> f64 {
+ f64::from(if self.vertical { width } else { height }).max(1.0)
+ }
+
+ /// The guide's position in comp pixels for a comp of this size.
+ pub fn px(&self, width: u32, height: u32) -> f64 {
+ if self.percent { self.position / 100.0 * self.extent(width, height) } else { self.position }
+ }
+
+ /// Place the guide at `px` comp pixels, keeping its unit.
+ pub fn set_px(&mut self, px: f64, width: u32, height: u32) {
+ self.position = if self.percent { px / self.extent(width, height) * 100.0 } else { px };
+ }
+
+ /// Switch between pixels and percent without moving the guide on the comp.
+ pub fn set_percent(&mut self, percent: bool, width: u32, height: u32) {
+ let px = self.px(width, height);
+ self.percent = percent;
+ self.set_px(px, width, height);
+ }
+ }
+
+ fn adaptive_limit_default() -> u32 {
+ 128
+ }
+
+ impl Comp {
+ pub fn new(width: u32, height: u32, frame_rate: FrameRate, duration: Tick) -> Comp {
+ Comp {
+ width,
+ height,
+ pixel_aspect: 1.0,
+ frame_rate,
+ duration,
+ display_start: Tick::ZERO,
+ background: [0.0, 0.0, 0.0],
+ work_area: (Tick::ZERO, duration),
+ layers: vec![],
+ markers: vec![],
+ shutter_angle: 180.0,
+ shutter_phase: -90.0,
+ motion_blur_samples: 16,
+ motion_blur_adaptive_limit: 128,
+ renderer: Renderer::Classic3D,
+ hide_shy: false,
+ enable_motion_blur: true,
+ enable_frame_blending: true,
+ draft_3d: false,
+ preserve_frame_rate: false,
+ preserve_resolution: false,
+ poster_time: Tick::ZERO,
+ guides: vec![],
+ global_light: styles::GlobalLight::default(),
+ essential: None,
+ resolution: Resolution::Auto,
+ }
+ }
+
+ /// Whether the composition has a frame at its time `t`: nested, it shows nothing before it
+ /// starts or after it ends (a precomp layer trimmed or remapped past it).
+ pub fn covers(&self, t: Tick) -> bool {
+ t >= Tick::ZERO && t < self.duration
+ }
+
+ /// A new, empty composition with this one's settings (frame rate, pixel aspect, background,
+ /// motion blur, frame blending and the 3D renderer), as Pre-compose makes.
+ pub fn nested_like(&self, width: u32, height: u32, duration: Tick) -> Comp {
+ Comp {
+ pixel_aspect: self.pixel_aspect,
+ background: self.background,
+ shutter_angle: self.shutter_angle,
+ shutter_phase: self.shutter_phase,
+ motion_blur_samples: self.motion_blur_samples,
+ motion_blur_adaptive_limit: self.motion_blur_adaptive_limit,
+ renderer: self.renderer,
+ enable_motion_blur: self.enable_motion_blur,
+ enable_frame_blending: self.enable_frame_blending,
+ preserve_frame_rate: self.preserve_frame_rate,
+ preserve_resolution: self.preserve_resolution,
+ ..Comp::new(width, height, self.frame_rate, duration)
+ }
+ }
+
+ pub fn layer(&self, id: LayerId) -> Option<&Layer> {
+ self.layers.iter().find(|l| l.id == id)
+ }
+
+ pub fn layer_mut(&mut self, id: LayerId) -> Option<&mut Layer> {
+ self.layers.iter_mut().find(|l| l.id == id)
+ }
+
+ /// 1-based layer number (#) of a layer.
+ pub fn index_of(&self, id: LayerId) -> Option<usize> {
+ self.layers.iter().position(|l| l.id == id).map(|i| i + 1)
+ }
+
+ pub fn layer_by_name(&self, name: &str) -> Option<&Layer> {
+ self.layers.iter().find(|l| l.name == name)
+ }
+
+ pub fn frame_duration(&self) -> Tick {
+ self.frame_rate.frame_duration()
+ }
+
+ /// Any 3D layer present (camera/lights or a 3D switch).
+ pub fn has_3d(&self) -> bool {
+ self.layers.iter().any(|l| l.switches.three_d || matches!(l.source, LayerSource::Camera | LayerSource::Light { .. }))
+ }
+
+ /// The active camera at `t`: the topmost enabled camera layer active at that time.
+ pub fn active_camera(&self, t: Tick) -> Option<&Layer> {
+ self.layers.iter().find(|l| matches!(l.source, LayerSource::Camera) && l.switches.video && l.is_active_at(t))
+ }
+
+ /// Unique layer name with a numeric suffix (`Shape Layer 2`).
+ pub fn unique_layer_name(&self, base: &str) -> String {
+ if !self.layers.iter().any(|l| l.name == base) {
+ return base.to_string();
+ }
+ let stem = base.trim_end_matches(|c: char| c.is_ascii_digit()).trim_end();
+ (2..).map(|i| format!("{stem} {i}")).find(|n| !self.layers.iter().any(|l| &l.name == n)).unwrap_or_default()
+ }
+ }
+
++/// A protected time marker with a duration sets the comp's timing (Responsive Design), so it is
++/// part of the pixels even where plain markers are not.
++fn is_timing_marker(m: &Marker) -> bool {
++ m.protected && m.duration > Tick::ZERO
++}
++
++fn timing_markers(m: &[Marker]) -> impl Iterator<Item = (Tick, Tick)> + '_ {
++ m.iter().filter(|m| is_timing_marker(m)).map(|m| (m.time, m.duration))
++}
++
+ 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 {
++ 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,
+ pixel_aspect,
+ frame_rate,
+ duration,
+ display_start,
+ background,
+ work_area,
+ layers: _,
+ markers,
+ shutter_angle,
+ shutter_phase,
+ motion_blur_samples,
+ motion_blur_adaptive_limit,
+ renderer,
+ hide_shy: _,
+ enable_motion_blur,
+ enable_frame_blending,
+ draft_3d,
+ preserve_frame_rate,
+ preserve_resolution,
+ poster_time,
+ global_light,
+ guides,
+ essential,
+ resolution: _,
+ } = self;
+ *width == o.width
+ && *height == o.height
+ && *pixel_aspect == o.pixel_aspect
+ && *frame_rate == o.frame_rate
+ && *duration == o.duration
+ && *display_start == o.display_start
+ && *background == o.background
+ && *work_area == o.work_area
- && *markers == o.markers
++ && (if with_markers { *markers == o.markers } else { timing_markers(markers).eq(timing_markers(&o.markers)) })
+ && *shutter_angle == o.shutter_angle
+ && *shutter_phase == o.shutter_phase
+ && *motion_blur_samples == o.motion_blur_samples
+ && *motion_blur_adaptive_limit == o.motion_blur_adaptive_limit
+ && *renderer == o.renderer
+ && *enable_motion_blur == o.enable_motion_blur
+ && *enable_frame_blending == o.enable_frame_blending
+ && *draft_3d == o.draft_3d
+ && *preserve_frame_rate == o.preserve_frame_rate
+ && *preserve_resolution == o.preserve_resolution
+ && *poster_time == o.poster_time
+ && *global_light == o.global_light
+ && *guides == o.guides
+ && *essential == o.essential
+ }
+
+ /// 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> {
- if !self.hide_shy && self.resolution.is_auto() && self.layers.iter().all(|l| l.switches == l.switches.pixels()) {
++ self.pixel_form_with(true)
++ }
++
++ /// [`Self::pixel_form`], also without the comp's plain markers and its layers' markers when not
++ /// `with_markers` (see [`Self::same_pixels_with`] and [`Project::reads_markers`]).
++ pub fn pixel_form_with(&self, with_markers: bool) -> std::borrow::Cow<'_, Comp> {
++ if !self.hide_shy
++ && self.resolution.is_auto()
++ && self.layers.iter().all(|l| l.switches == l.switches.pixels())
++ && (with_markers || (self.markers.iter().all(is_timing_marker) && self.layers.iter().all(|l| l.markers.is_empty())))
++ {
+ return std::borrow::Cow::Borrowed(self);
+ }
+ let mut c = self.clone();
+ c.hide_shy = false;
+ c.resolution = Resolution::Auto;
++ if !with_markers {
++ c.markers.retain(is_timing_marker);
++ }
+ for l in &mut c.layers {
+ l.switches = l.switches.pixels();
++ if !with_markers {
++ l.markers.clear();
++ }
+ }
+ std::borrow::Cow::Owned(c)
+ }
+ }
diff --cc crates/project/src/item.rs
index 000000000,d9d5184c9..a9b69bb3d
mode 000000,100644..100644
--- a/crates/project/src/item.rs
+++ b/crates/project/src/item.rs
@@@ -1,0 -1,308 +1,323 @@@
+ use super::comp::Comp;
+ use super::footage::{Footage, FootageKind, Proxy, Solid};
+ use super::layer::LayerSource;
+ use super::settings::{APP_VERSION, ProjectError, ProjectSettings, SCHEMA_VERSION, SavedView};
+ use super::*;
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
+ pub struct ItemId(pub u64);
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
+ pub struct LayerId(pub u64);
+
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ #[serde(tag = "type")]
+ #[allow(clippy::large_enum_variant)]
+ pub enum ItemKind {
+ Folder,
+ Comp(Arc<Comp>),
+ Footage(Footage),
+ Solid(Solid),
+ }
+
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Item {
+ pub id: ItemId,
+ pub name: String,
+ pub label: Label,
+ #[serde(default)]
+ pub comment: String,
+ /// Containing folder (None = project root).
+ #[serde(default)]
+ pub parent: Option<ItemId>,
+ pub kind: ItemKind,
+ /// The item's proxy (footage and compositions).
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub proxy: Option<Box<Proxy>>,
+ }
+
+ impl Item {
+ pub fn type_name(&self) -> &'static str {
+ match &self.kind {
+ ItemKind::Folder => "Folder",
+ ItemKind::Comp(_) => "Composition",
+ ItemKind::Footage(f) => match f.kind {
+ FootageKind::Video => "Video",
+ FootageKind::Still => "Image",
+ FootageKind::Sequence => "Image Sequence",
+ FootageKind::Audio => "Audio",
+ FootageKind::Model => "3D Model",
+ FootageKind::Data => "Data",
+ },
+ ItemKind::Solid(_) => "Solid",
+ }
+ }
+ pub fn as_comp(&self) -> Option<&Comp> {
+ if let ItemKind::Comp(c) = &self.kind { Some(c) } else { None }
+ }
+ pub fn is_folder(&self) -> bool {
+ matches!(self.kind, ItemKind::Folder)
+ }
+ /// (width, height) for visual items.
+ pub fn dimensions(&self) -> Option<(u32, u32)> {
+ match &self.kind {
+ ItemKind::Comp(c) => Some((c.width, c.height)),
+ ItemKind::Footage(f) if f.has_video => Some((f.width, f.height)),
+ ItemKind::Solid(s) => Some((s.width, s.height)),
+ _ => None,
+ }
+ }
+ pub fn duration(&self) -> Option<Tick> {
+ match &self.kind {
+ ItemKind::Comp(c) => Some(c.duration),
+ ItemKind::Footage(f) if f.kind != FootageKind::Still => Some(f.looped_duration()),
+ _ => None,
+ }
+ }
+ pub fn frame_rate(&self) -> Option<FrameRate> {
+ match &self.kind {
+ ItemKind::Comp(c) => Some(c.frame_rate),
+ ItemKind::Footage(f) if f.kind == FootageKind::Video || f.kind == FootageKind::Sequence => Some(f.frame_rate),
+ _ => None,
+ }
+ }
+ }
+
+ impl Default for Project {
+ fn default() -> Self {
+ Project {
+ schema: SCHEMA_VERSION,
+ settings: ProjectSettings::default(),
+ items: BTreeMap::new(),
+ next_id: 1,
+ render_queue: Vec::new(),
+ render_templates: Default::default(),
+ render_prefs: Default::default(),
+ }
+ }
+ }
+
+ impl Project {
++ /// Some layer has an expression, which can read markers (`marker.key(1)`, `numKeys`,
++ /// `nearestKey`...). Only then do the markers of comps and layers affect what is rendered, so
++ /// frame caches ignore them otherwise.
++ pub fn reads_markers(&self) -> bool {
++ let mut found = false;
++ for (_, c) in self.comps() {
++ for l in &c.layers {
++ if !found {
++ l.props.walk("", &mut |_, p| found |= p.expr.is_some());
++ }
++ }
++ }
++ found
++ }
++
+ /// Allocate an id (items, layers and property uids share one counter).
+ pub fn alloc(&mut self) -> u64 {
+ let id = self.next_id;
+ self.next_id += 1;
+ id
+ }
+ pub fn item(&self, id: ItemId) -> Option<&Item> {
+ self.items.get(&id)
+ }
+ pub fn item_mut(&mut self, id: ItemId) -> Option<&mut Item> {
+ self.items.get_mut(&id)
+ }
+ pub fn comp(&self, id: ItemId) -> Option<&Comp> {
+ self.items.get(&id)?.as_comp()
+ }
+ /// A shared handle on a comp: a snapshot that costs a reference count, not a deep copy of
+ /// its layers (frontends hold one across a frame instead of cloning the comp).
+ pub fn comp_arc(&self, id: ItemId) -> Option<Arc<Comp>> {
+ match &self.items.get(&id)?.kind {
+ ItemKind::Comp(c) => Some(c.clone()),
+ _ => None,
+ }
+ }
+ /// Mutable comp (copy-on-write).
+ pub fn comp_mut(&mut self, id: ItemId) -> Option<&mut Comp> {
+ match &mut self.items.get_mut(&id)?.kind {
+ ItemKind::Comp(c) => Some(Arc::make_mut(c)),
+ _ => None,
+ }
+ }
+ /// Adds an item; its size, frame rate and the like are brought within [`limits`] first.
+ pub fn add_item(&mut self, name: &str, label: Label, parent: Option<ItemId>, mut kind: ItemKind) -> ItemId {
+ limits::repair_kind(name, &mut kind, &mut vec![]);
+ let id = ItemId(self.alloc());
+ self.items.insert(id, Item { id, name: name.into(), label, comment: String::new(), parent, kind, proxy: None });
+ id
+ }
+ /// Children of a folder (None = root), folders first then by name.
+ pub fn children(&self, folder: Option<ItemId>) -> Vec<&Item> {
+ let mut v: Vec<&Item> = self.items.values().filter(|i| i.parent == folder).collect();
+ v.sort_by(|a, b| b.is_folder().cmp(&a.is_folder()).then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase())));
+ v
+ }
+ pub fn comps(&self) -> impl Iterator<Item = (&ItemId, &Comp)> {
+ self.items.iter().filter_map(|(id, i)| i.as_comp().map(|c| (id, c)))
+ }
+ pub fn find_by_name(&self, name: &str) -> Option<&Item> {
+ self.items.values().find(|i| i.name == name)
+ }
+ /// A numbered name for a copy of `base` that no project item has (Edit ▸ Duplicate): its
+ /// trailing number is dropped and the lowest free one from 2 is added (`Comp 1` → `Comp 2`,
+ /// `Name` → `Name 2`, then `Name 3`). Items for which `ignore` holds don't count.
+ pub fn numbered_item_name(&self, base: &str, ignore: impl Fn(&Item) -> bool) -> String {
+ let stem = base.trim_end_matches(|c: char| c.is_ascii_digit()).trim_end();
+ let taken = |n: &str| self.items.values().any(|i| i.name == n && !ignore(i));
+ (2u64..).map(|k| format!("{stem} {k}")).find(|n| !taken(n)).unwrap_or_default()
+ }
+ /// `base` when no project item has that name, else the next free numbered name as
+ /// [`Project::numbered_item_name`] gives it. Items for which `ignore` holds don't count.
+ pub fn unique_item_name(&self, base: &str, ignore: impl Fn(&Item) -> bool) -> String {
+ if self.items.values().any(|i| i.name == base && !ignore(i)) { self.numbered_item_name(base, ignore) } else { base.to_string() }
+ }
+ pub fn folder_named(&self, name: &str) -> Option<ItemId> {
+ self.items.values().find(|i| i.is_folder() && i.name == name).map(|i| i.id)
+ }
+ /// Find the comp that contains `layer`.
+ pub fn comp_of_layer(&self, layer: LayerId) -> Option<ItemId> {
+ self.comps().find(|(_, c)| c.layer(layer).is_some()).map(|(id, _)| *id)
+ }
+ /// The compositions whose layers use `item` as their source (footage, solid, 3D model or
+ /// precomp), each with how many of its layers do, ordered by comp name then id. Only direct
+ /// layers count, as in After Effects' Project panel ("used 3 times"): a solid inside a precomp
+ /// is used by the precomp, not by the comps that nest it. The total is the sum of the counts.
+ pub fn item_users(&self, item: ItemId) -> Vec<(ItemId, usize)> {
+ let mut users: Vec<(ItemId, String, usize)> = self
+ .items
+ .iter()
+ .filter_map(|(id, it)| {
+ let comp = it.as_comp()?;
+ let n = comp.layers.iter().filter(|l| l.source.item() == Some(item)).count();
+ (n > 0).then(|| (*id, it.name.to_lowercase(), n))
+ })
+ .collect();
+ users.sort_by(|a, b| a.1.cmp(&b.1).then(a.0.cmp(&b.0)));
+ users.into_iter().map(|(id, _, n)| (id, n)).collect()
+ }
+ /// Whether comp `inner` is (transitively) nested inside comp `outer` (to prevent cycles).
+ pub fn comp_contains(&self, outer: ItemId, inner: ItemId) -> bool {
+ // A walk with a stack and a visited set rather than recursion (#1237): a damaged project
+ // may hold a precomp cycle that doesn't pass `inner` (recursion never ended), a long
+ // chain must not use up the call stack, and comps shared by many paths are visited once
+ // (recursion re-walked them for every path).
+ if outer == inner {
+ return true;
+ }
+ let mut seen = std::collections::HashSet::new();
+ let mut stack = vec![outer];
+ while let Some(id) = stack.pop() {
+ if !seen.insert(id) {
+ continue;
+ }
+ let Some(c) = self.comp(id) else { continue };
+ for l in &c.layers {
+ if let LayerSource::Comp { item } = l.source {
+ if item == inner {
+ return true;
+ }
+ if !seen.contains(&item) {
+ stack.push(item);
+ }
+ }
+ }
+ }
+ false
+ }
+ /// Make sure `next_id` exceeds every id in the project (after loading or merging).
+ pub fn fix_next_id(&mut self) {
+ let mut m = self.next_id;
+ for (id, it) in &self.items {
+ // Saturating: a damaged file's ids must not overflow (the import then refuses it).
+ m = m.max(id.0.saturating_add(1));
+ if let ItemKind::Comp(c) = &it.kind {
+ for l in &c.layers {
+ m = m.max(l.id.0.saturating_add(1)).max(l.props.max_uid().saturating_add(1));
+ }
+ }
+ }
+ self.next_id = m;
+ }
+ /// Whether `other` holds the same content. An allocated id counts (an edit may hand it out
+ /// beyond the project, so it must stay taken). Shared compositions compare by pointer first,
+ /// so after a copy-on-write edit only the comps it touched are compared field by field.
+ pub fn same_content(&self, other: &Project) -> bool {
+ let item_eq = |a: &Item, b: &Item| {
+ let kind = match (&a.kind, &b.kind) {
+ (ItemKind::Comp(x), ItemKind::Comp(y)) => Arc::ptr_eq(x, y) || x == y,
+ (x, y) => x == y,
+ };
+ kind && a.id == b.id && a.name == b.name && a.label == b.label && a.comment == b.comment && a.parent == b.parent && a.proxy == b.proxy
+ };
+ self.schema == other.schema
+ && self.next_id == other.next_id
+ && self.settings == other.settings
+ && self.items.len() == other.items.len()
+ && self.items.iter().zip(&other.items).all(|((ka, a), (kb, b))| ka == kb && item_eq(a, b))
+ && self.render_queue == other.render_queue
+ && self.render_templates == other.render_templates
+ && self.render_prefs == other.render_prefs
+ }
+ /// The project file's text: the project with the version that wrote it (`savedBy`), checked
+ /// to read back, so a save never writes a file that can't be opened. Values JSON can't hold
+ /// (NaN or infinity, written as `null`, and nesting too deep to parse) are refused on every
+ /// save; debug builds also read the text back.
+ pub fn to_file_json(&self) -> Result<String, ProjectError> {
+ self.to_file_json_with_view(&SavedView::default())
+ }
+ /// [`Project::to_file_json`] that also records the open compositions ([`SavedView`]).
+ pub fn to_file_json_with_view(&self, view: &SavedView) -> Result<String, ProjectError> {
+ if self.schema > SCHEMA_VERSION {
+ return Err(ProjectError::Invalid(format!(
+ "the project can't be saved: it holds a value a project file can't store (project schema {} is newer than this version supports ({SCHEMA_VERSION}))",
+ self.schema
+ )));
+ }
+ #[derive(Serialize)]
+ struct File<'a> {
+ #[serde(rename = "savedBy")]
+ saved_by: &'a str,
+ #[serde(flatten)]
+ view: &'a SavedView,
+ #[serde(flatten)]
+ project: &'a Project,
+ }
+ let file = File { saved_by: APP_VERSION, view, project: self };
+ file_check::check(&file)
+ .map_err(|e| ProjectError::Invalid(format!("the project can't be saved: it holds a value a project file can't store ({e})")))?;
+ let text = serde_json::to_string_pretty(&file).map_err(|e| ProjectError::Invalid(format!("the project can't be saved: {e}")))?;
+ #[cfg(debug_assertions)]
+ Project::from_json(&text)
+ .map_err(|e| ProjectError::Invalid(format!("the project can't be saved: it holds a value a project file can't store ({e})")))?;
+ Ok(text)
+ }
+ pub fn to_json(&self) -> String {
+ serde_json::to_string_pretty(self).unwrap_or_default()
+ }
+ /// [`Project::to_json`] without indentation: smaller and faster (auto-saves).
+ pub fn to_json_compact(&self) -> String {
+ serde_json::to_string(self).unwrap_or_default()
+ }
+ pub fn from_json(s: &str) -> Result<Project, ProjectError> {
+ Project::from_json_repaired(s).map(|(p, _)| p)
+ }
+ /// [`Project::from_json`], also returning one line per value that was out of [`limits`] and
+ /// was corrected (a composition of a billion pixels, a zero frame rate…), so a corrupt or
+ /// hostile file still opens, within bounds, and the user can be told what changed.
+ pub fn from_json_repaired(s: &str) -> Result<(Project, Vec<String>), ProjectError> {
+ let mut p: Project = serde_json::from_str(s).map_err(|e| ProjectError::Invalid(e.to_string()))?;
+ if p.schema > SCHEMA_VERSION {
+ return Err(ProjectError::Invalid(format!("project schema {} is newer than this version supports ({SCHEMA_VERSION})", p.schema)));
+ }
+ p.fix_next_id();
+ let mut notes = vec![];
+ for item in p.items.values_mut() {
+ limits::repair_item(item, &mut notes);
+ }
+ Ok((p, notes))
+ }
+ }
diff --cc crates/project/src/layer.rs
index 000000000,b7286a6d5..18f6c1571
mode 000000,100644..100644
--- a/crates/project/src/layer.rs
+++ b/crates/project/src/layer.rs
@@@ -1,0 -1,370 +1,376 @@@
+ use super::item::{ItemId, LayerId};
+ use super::settings::Marker;
+ use super::*;
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum LightKind {
+ Parallel,
+ Spot,
+ #[default]
+ Point,
+ Ambient,
+ Environment,
+ }
+
+ impl LightKind {
+ pub const ALL: [LightKind; 5] = [LightKind::Parallel, LightKind::Spot, LightKind::Point, LightKind::Ambient, LightKind::Environment];
+ pub fn label(self) -> &'static str {
+ match self {
+ LightKind::Parallel => "Parallel",
+ LightKind::Spot => "Spot",
+ LightKind::Point => "Point",
+ LightKind::Ambient => "Ambient",
+ LightKind::Environment => "Environment",
+ }
+ }
+ }
+
+ /// A parametric 3D primitive (Layer ▸ New ▸ Cube/Sphere/Plane/Torus/Cone/Cylinder).
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
+ pub enum PrimitiveKind {
+ #[default]
+ Cube,
+ Sphere,
+ Plane,
+ Torus,
+ Cone,
+ Cylinder,
+ }
+
+ impl PrimitiveKind {
+ pub const ALL: [PrimitiveKind; 6] =
+ [PrimitiveKind::Cube, PrimitiveKind::Sphere, PrimitiveKind::Plane, PrimitiveKind::Torus, PrimitiveKind::Cone, PrimitiveKind::Cylinder];
+ pub fn label(self) -> &'static str {
+ match self {
+ PrimitiveKind::Cube => "Cube",
+ PrimitiveKind::Sphere => "Sphere",
+ PrimitiveKind::Plane => "Plane",
+ PrimitiveKind::Torus => "Torus",
+ PrimitiveKind::Cone => "Cone",
+ PrimitiveKind::Cylinder => "Cylinder",
+ }
+ }
+ pub fn parse(s: &str) -> Option<PrimitiveKind> {
+ PrimitiveKind::ALL.into_iter().find(|k| k.label().eq_ignore_ascii_case(s.trim()))
+ }
+ }
+
+ /// What a layer shows.
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ #[serde(tag = "type")]
+ pub enum LayerSource {
+ /// Footage item (video, still, sequence, audio).
+ Footage {
+ item: ItemId,
+ },
+ /// Nested composition (precomp).
+ Comp {
+ item: ItemId,
+ },
+ /// Solid item (solids also back adjustment layers).
+ Solid {
+ item: ItemId,
+ },
+ Text,
+ Shape,
+ Null,
+ Camera,
+ Light {
+ kind: LightKind,
+ },
+ /// An imported 3D model (a footage item of kind [`FootageKind::Model`]; Advanced 3D).
+ Model {
+ item: ItemId,
+ },
+ /// A parametric 3D primitive (Advanced 3D).
+ Primitive {
+ kind: PrimitiveKind,
+ },
+ }
+
+ impl LayerSource {
+ pub fn item(&self) -> Option<ItemId> {
+ match self {
+ LayerSource::Footage { item } | LayerSource::Comp { item } | LayerSource::Solid { item } | LayerSource::Model { item } => Some(*item),
+ _ => None,
+ }
+ }
+ pub fn type_name(&self) -> &'static str {
+ match self {
+ LayerSource::Footage { .. } => "Footage",
+ LayerSource::Comp { .. } => "Composition",
+ LayerSource::Solid { .. } => "Solid",
+ LayerSource::Text => "Text",
+ LayerSource::Shape => "Shape",
+ LayerSource::Null => "Null",
+ LayerSource::Camera => "Camera",
+ LayerSource::Light { .. } => "Light",
+ LayerSource::Model { .. } => "3D Model",
+ LayerSource::Primitive { .. } => "3D Primitive",
+ }
+ }
+ /// Layers with pixels (cameras, lights and nulls have none).
+ pub fn is_av(&self) -> bool {
+ !matches!(self, LayerSource::Camera | LayerSource::Light { .. } | LayerSource::Null)
+ }
+ /// A 3D model or primitive layer (meshes; drawn by the Advanced 3D renderer).
+ pub fn is_model(&self) -> bool {
+ matches!(self, LayerSource::Model { .. } | LayerSource::Primitive { .. })
+ }
+ }
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum Quality {
+ #[default]
+ Best,
+ Draft,
+ Wireframe,
+ }
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum Sampling {
+ #[default]
+ Bilinear,
+ Bicubic,
+ }
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum FrameBlend {
+ #[default]
+ Off,
+ FrameMix,
+ PixelMotion,
+ }
+
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Switches {
+ pub video: bool,
+ pub audio: bool,
+ pub solo: bool,
+ pub locked: bool,
+ pub shy: bool,
+ /// Collapse transformations (precomps) / continuously rasterize (vector layers).
+ pub collapse: bool,
+ pub quality: Quality,
+ pub sampling: Sampling,
+ pub effects: bool,
+ pub frame_blend: FrameBlend,
+ pub motion_blur: bool,
+ pub adjustment: bool,
+ pub three_d: bool,
+ pub guide: bool,
+ }
+
+ impl Default for Switches {
+ fn default() -> Self {
+ Switches {
+ video: true,
+ audio: true,
+ solo: false,
+ locked: false,
+ shy: false,
+ collapse: false,
+ quality: Quality::Best,
+ sampling: Sampling::Bilinear,
+ effects: true,
+ frame_blend: FrameBlend::Off,
+ motion_blur: false,
+ adjustment: false,
+ three_d: false,
+ guide: false,
+ }
+ }
+ }
+
+ // What a frame's pixels depend on. Caches key on it: the Audio, Lock and Shy switches and Hide
+ // Shy Layers don't change pixels, so toggling them keeps cached frames (#103).
+
+ impl Switches {
+ /// These switches with Audio, Lock and Shy at their defaults.
+ pub fn pixels(&self) -> Switches {
+ Switches { audio: true, locked: false, shy: false, ..self.clone() }
+ }
+ }
+
+ impl Layer {
+ /// Draws the same pixels as `o`: equal but for the Audio, Lock and Shy switches.
+ pub fn same_pixels(&self, o: &Layer) -> bool {
++ self.same_pixels_with(o, true)
++ }
++
++ /// [`Self::same_pixels`], with the markers counting only when `with_markers` (they are read by
++ /// expressions alone, so a project without any can edit them freely).
++ pub fn same_pixels_with(&self, o: &Layer, with_markers: bool) -> bool {
+ // Every field (no `..`: a new field must be considered here); the property tree last.
+ let Layer {
+ id,
+ name,
+ source,
+ label,
+ comment,
+ start_time,
+ in_point,
+ out_point,
+ stretch,
+ switches,
+ blend_mode,
+ preserve_transparency,
+ track_matte,
+ parent,
+ markers,
+ markers_locked,
+ auto_orient,
+ environment,
+ environment_background,
+ props,
+ } = self;
+ *id == o.id
+ && *name == o.name
+ && *source == o.source
+ && *label == o.label
+ && *comment == o.comment
+ && *start_time == o.start_time
+ && *in_point == o.in_point
+ && *out_point == o.out_point
+ && *stretch == o.stretch
+ && switches.pixels() == o.switches.pixels()
+ && *blend_mode == o.blend_mode
+ && *preserve_transparency == o.preserve_transparency
+ && *track_matte == o.track_matte
+ && *parent == o.parent
- && *markers == o.markers
++ && (!with_markers || *markers == o.markers)
+ && *markers_locked == o.markers_locked
+ && *auto_orient == o.auto_orient
+ && *environment == o.environment
+ && *environment_background == o.environment_background
+ && *props == o.props
+ }
+ }
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum MatteKind {
+ #[default]
+ Alpha,
+ AlphaInverted,
+ Luma,
+ LumaInverted,
+ }
+
+ impl MatteKind {
+ pub const ALL: [MatteKind; 4] = [MatteKind::Alpha, MatteKind::AlphaInverted, MatteKind::Luma, MatteKind::LumaInverted];
+ pub fn label(self) -> &'static str {
+ match self {
+ MatteKind::Alpha => "Alpha Matte",
+ MatteKind::AlphaInverted => "Alpha Inverted Matte",
+ MatteKind::Luma => "Luma Matte",
+ MatteKind::LumaInverted => "Luma Inverted Matte",
+ }
+ }
+ }
+
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
+ pub struct TrackMatte {
+ pub layer: LayerId,
+ pub kind: MatteKind,
+ }
+
+ #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
+ pub enum AutoOrient {
+ #[default]
+ Off,
+ AlongPath,
+ TowardsCamera,
+ TowardsPointOfInterest,
+ }
+
+ #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
+ pub struct Layer {
+ pub id: LayerId,
+ pub name: String,
+ pub source: LayerSource,
+ pub label: Label,
+ #[serde(default)]
+ pub comment: String,
+ /// Comp time where the layer's time 0 sits.
+ pub start_time: Tick,
+ /// Visible span in comp time.
+ pub in_point: Tick,
+ pub out_point: Tick,
+ /// Time stretch in percent (100 = normal, negative = reversed).
+ pub stretch: f64,
+ pub switches: Switches,
+ pub blend_mode: BlendMode,
+ #[serde(default)]
+ pub preserve_transparency: bool,
+ #[serde(default)]
+ pub track_matte: Option<TrackMatte>,
+ #[serde(default)]
+ pub parent: Option<LayerId>,
+ #[serde(default)]
+ pub markers: Vec<Marker>,
+ /// Layer ▸ Markers ▸ Lock Markers.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub markers_locked: bool,
+ #[serde(default)]
+ pub auto_orient: AutoOrient,
+ /// Layer ▸ Environment Layer: the layer is the environment (image-based light and
+ /// reflections) of Advanced 3D comps instead of being drawn.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub environment: bool,
+ /// Layer ▸ Light ▸ Create Environment Light Background Layer: the layer's (equirectangular)
+ /// image is drawn as the 3D scene's backdrop, seen through the camera, instead of as a card.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ pub environment_background: bool,
+ /// The property tree (Masks, Effects, Transform, Text, Contents, Camera/Light options…).
+ pub props: PropGroup,
+ }
+
+ impl Layer {
+ pub fn is_active_at(&self, t: Tick) -> bool {
+ t >= self.in_point && t < self.out_point
+ }
+
+ /// 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) }
+ }
+
+ /// 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) }
+ }
+
+ pub fn transform(&self) -> Option<&PropGroup> {
+ self.props.sub("transform")
+ }
+
+ pub fn transform_mut(&mut self) -> Option<&mut PropGroup> {
+ self.props.sub_mut("transform")
+ }
+
+ pub fn effects(&self) -> Option<&PropGroup> {
+ self.props.sub("effects")
+ }
+
+ pub fn masks(&self) -> Option<&PropGroup> {
+ self.props.sub("masks")
+ }
+
+ pub fn is_camera(&self) -> bool {
+ matches!(self.source, LayerSource::Camera)
+ }
+
+ pub fn is_light(&self) -> bool {
+ matches!(self.source, LayerSource::Light { .. })
+ }
+
+ /// The layer participates in 3D (3D switch, or cameras/lights which are always 3D).
+ pub fn is_3d(&self) -> bool {
+ self.switches.three_d || self.is_camera() || self.is_light()
+ }
+
+ pub fn has_video(&self) -> bool {
+ self.source.is_av() && self.switches.video
+ }
+ }
diff --cc crates/ui-egui/src/frames/tests.rs
index 000000000,6da1dc269..a2603dfec
mode 000000,100644..100644
--- a/crates/ui-egui/src/frames/tests.rs
+++ b/crates/ui-egui/src/frames/tests.rs
@@@ -1,0 -1,927 +1,953 @@@
+ use super::*;
+
+ struct ProbeDisk {
+ keys: Mutex<HashSet<u128>>,
+ probes: std::sync::atomic::AtomicUsize,
+ }
+
+ impl RemoteFrames for ProbeDisk {
+ fn slots(&self) -> usize {
+ 0
+ }
+
+ fn disk(&self) -> bool {
+ true
+ }
+
+ fn disk_contains(&self, key: u128) -> bool {
+ self.probes.fetch_add(1, Ordering::Relaxed);
+ self.keys.lock().unwrap().contains(&key)
+ }
+
+ fn start(&self, _: RemoteJob, _: RemoteDone) {}
+ }
+
+ fn disk_fixture(stored_frames: &[i64]) -> (Frames, RenderSource, FrameKey, RenderOpts, Arc<ProbeDisk>, u128) {
+ let mut session = effectcraft_engine::Session::default();
+ session.execute("comp.new", serde_json::json!({"width": 32, "height": 18, "duration": 1})).unwrap();
+ let comp = session.active_comp_id().unwrap();
+ let opts = RenderOpts::default();
+ let content = 1234;
+ let mut base = key(1, 0, opts_hash(&opts));
+ base.revision = 7;
+ base.comp = comp.0;
+ let disk = Arc::new(ProbeDisk { keys: Mutex::new(HashSet::new()), probes: 0.into() });
+ 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);
+ let source = RenderSource {
+ project: session.project.clone(),
+ footage: session.footage.clone(),
+ expr: None,
+ layer_cache: Arc::default(),
+ gpu: None,
+ gpu_display: false,
+ disk: None,
+ };
+ (frames, source, base, opts, disk, content)
+ }
+
+ #[cfg(not(target_arch = "wasm32"))]
+ #[test]
+ fn disk_indicator_does_not_hash_on_ui_thread_and_coalesces_edits() {
+ use std::sync::mpsc;
+ use std::time::{Duration, Instant};
+ struct Disk(u128);
+ impl RemoteFrames for Disk {
+ fn slots(&self) -> usize {
+ 1
+ }
+ fn disk(&self) -> bool {
+ true
+ }
+ fn disk_contains(&self, key: u128) -> bool {
+ self.0 == key
+ }
+ fn start(&self, _: RemoteJob, done: RemoteDone) {
+ done(Err("test has no renderer".into()));
+ }
+ }
+ let mut s = effectcraft_engine::Session::default();
+ s.execute("comp.new", serde_json::json!({"width":32,"height":18,"duration":1})).unwrap();
+ let cid = s.active_comp_id().unwrap();
+ let opts = RenderOpts::default();
+ let stored = disk_cache::frame_key(disk_cache::comp_content_key(&s.project, cid), 2, 1000, opts_hash(&opts));
+ let mut frames = Frames { pool: Some(rayon::ThreadPoolBuilder::new().num_threads(1).build().unwrap()), ..Default::default() };
+ frames.set_remote(Some(Arc::new(Disk(stored))));
+ let (entered, started) = mpsc::channel();
+ let (release, wait) = mpsc::channel();
+ frames.pool.as_ref().unwrap().spawn(move || {
+ entered.send(()).unwrap();
+ wait.recv_timeout(Duration::from_secs(5)).unwrap();
+ });
+ started.recv_timeout(Duration::from_secs(3)).unwrap();
+ let src = RenderSource {
+ project: s.project.clone(),
+ footage: s.footage.clone(),
+ expr: None,
+ layer_cache: Arc::default(),
+ gpu: None,
+ gpu_display: false,
+ disk: None,
+ };
+ let mut base = key(1, 0, 0);
+ base.comp = cid.0;
+ for revision in 1..50 {
+ base.revision = revision;
+ assert!(frames.disk_frames(&src, &base, 0..30, &opts).is_empty());
+ }
+ assert!(frames.content_keys.lock().unwrap().is_empty(), "painting computed a content hash while the worker was blocked");
+ 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)) {
+ assert!(Instant::now() < until);
+ std::thread::sleep(Duration::from_millis(1));
+ }
+ assert_eq!(frames.disk_frames(&src, &base, 0..30, &opts), vec![2], "the blue indicator must return once its key is ready");
+ assert_eq!(frames.content_keys.lock().unwrap().len(), 1, "superseded revisions were hashed");
+ }
+
+ #[test]
+ fn disk_indicator_range_tracks_live_membership() {
+ let (frames, src, base, opts, disk, _) = disk_fixture(&[10, 12]);
+ assert_eq!(frames.disk_frames(&src, &base, 10..13, &opts), vec![10, 12]);
+ assert_eq!(disk.probes.load(Ordering::Relaxed), 3);
+
+ disk.keys.lock().unwrap().insert(disk_cache::frame_key(1234, 11, base.scale, base.view ^ opts_hash(&opts)));
+ assert_eq!(frames.disk_frames(&src, &base, 10..13, &opts), vec![10, 11, 12]);
+
+ insert(&frames, FrameKey { frame: 11, ..base });
+ disk.probes.store(0, Ordering::Relaxed);
+ assert_eq!(frames.disk_frames(&src, &base, 10..13, &opts), vec![10, 12]);
+ assert_eq!(disk.probes.load(Ordering::Relaxed), 2);
+
+ disk.keys.lock().unwrap().remove(&disk_cache::frame_key(1234, 12, base.scale, base.view ^ opts_hash(&opts)));
+ assert_eq!(frames.disk_frames(&src, &base, 10..13, &opts), vec![10]);
+
+ let mut revised = base;
+ revised.revision += 1;
+ frames.content_keys.lock().unwrap().insert((revised.revision, revised.comp, 0), 5678);
+ assert!(frames.disk_frames(&src, &revised, 10..13, &opts).is_empty());
+
+ let changed = FrameKey { scale: base.scale + 1, ..base };
+ assert!(frames.disk_frames(&src, &changed, 10..13, &opts).is_empty());
+ let changed = FrameKey { view: base.view + 1, ..base };
+ assert!(frames.disk_frames(&src, &changed, 10..13, &opts).is_empty());
+ assert!(frames.disk_frames(&src, &base, 10..13, &RenderOpts { draft: true, ..opts }).is_empty());
+ }
+
+ #[test]
+ fn disk_indicator_range_is_bounded() {
+ let (frames, src, base, opts, disk, _) = disk_fixture(&[0, 1, 99_999]);
+ assert_eq!(frames.disk_frames(&src, &base, -5..2, &opts), vec![0, 1]);
+ assert_eq!(disk.probes.load(Ordering::Relaxed), 2);
+ disk.probes.store(0, Ordering::Relaxed);
+ assert_eq!(frames.disk_frames(&src, &base, 99_999..i64::MAX, &opts), vec![99_999]);
+ assert_eq!(disk.probes.load(Ordering::Relaxed), 1);
+ disk.probes.store(0, Ordering::Relaxed);
+ assert!(frames.disk_frames(&src, &base, 2..2, &opts).is_empty());
+ // Written as a struct literal: `3..2` trips clippy::reversed_empty_ranges.
+ let reversed = std::ops::Range { start: 3, end: 2 };
+ assert!(frames.disk_frames(&src, &base, reversed, &opts).is_empty());
+ assert_eq!(disk.probes.load(Ordering::Relaxed), 0);
+ }
+
+ /// Frames larger than the texture limit are averaged down by a whole factor (#201: an
+ /// 11000×2200 comp at Full resolution against an 8192 limit).
+ #[test]
+ fn frames_over_the_texture_limit_are_averaged_down() {
+ assert_eq!(fitted_size([11000, 2200], 8192), [5500, 1100]);
+ assert_eq!(fitted_size([8192, 100], 8192), [8192, 100]);
+ assert_eq!(fitted_size([30000, 30000], 16384), [15000, 15000]);
+ assert_eq!(fitted_size([0, 0], 0), [0, 0]);
+ // 5×2 (three columns of white, then black) into 2 px: 3×2 blocks (the last one 2×2).
+ let w = egui::Color32::WHITE;
+ let b = egui::Color32::from_rgba_premultiplied(0, 0, 0, 255);
+ let img = egui::ColorImage::new([5, 2], vec![w, w, w, b, b, w, w, w, b, b]);
+ let out = fit_texture(img.clone(), 2);
+ assert_eq!(out.size, [2, 1]);
+ assert_eq!(out.pixels, vec![w, b]);
+ assert_eq!(fit_texture(img.clone(), 5).pixels, img.pixels);
+ let mixed = fit_texture(egui::ColorImage::new([2, 1], vec![w, b]), 1);
+ assert_eq!(mixed.pixels, vec![egui::Color32::from_rgba_premultiplied(128, 128, 128, 255)]);
+ }
+
+ #[test]
+ fn retirement_reaches_existing_workers_and_keeps_cpu_frames_and_completion_live() {
+ let frames = Frames::default();
+ let worker = frames.worker();
+ let first = key(1, 0, 0);
+ insert(&frames, first);
+ frames.retire_gpu();
+ assert!(worker.gpu_retired.load(Ordering::Acquire), "already queued workers share retirement");
+ assert!(frames.is_cached(&first), "healthy materialized CPU frames survive");
+ 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());
+ assert!(frames.is_cached(&next), "CPU retry can still publish");
+ assert_eq!(frames.inflight(), 0);
+ assert!(frames.queue.lock().unwrap().running.is_empty());
+ }
+
+ fn key(content: u64, frame: i64, opts: u64) -> FrameKey {
+ FrameKey { revision: content, content, comp: 1, frame, scale: 1000, view: 0, opts }
+ }
+
+ fn img() -> FrameImage {
+ FrameImage::Cpu(Arc::new(egui::ColorImage::new([2, 2], vec![egui::Color32::BLACK; 4])))
+ }
+
+ fn insert(f: &Frames, k: FrameKey) {
+ f.cache.lock().unwrap().insert(k, img());
+ }
+
+ /// A frame whose render panics (a GPU out of memory used to, on every frame thread) is
+ /// released, not left "in progress" forever, so the viewer can ask for it again (#106).
+ #[test]
+ fn a_frame_whose_render_panics_is_released() {
+ use effectcraft_engine::Session;
+ use effectcraft_engine::project::{Footage, FootageKind};
+ use effectcraft_engine::render::Image;
+ use serde_json::json;
+ use std::sync::atomic::{AtomicUsize, Ordering};
+ #[derive(Default)]
+ struct Explodes(AtomicUsize);
+ impl FootageSource for Explodes {
+ fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
+ self.0.fetch_add(1, Ordering::SeqCst);
+ panic!("a decoder bug");
+ }
+ }
+ 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());
+ let clip = Footage { kind: FootageKind::Still, width: 8, height: 8, has_video: true, ..Default::default() };
+ let item = Arc::make_mut(&mut s.project).add_item("clip.png", Default::default(), None, ItemKind::Footage(clip));
+ s.execute("layer.addItem", json!({"item": item.0})).unwrap();
+ let footage = Arc::new(Explodes::default());
+ let src = RenderSource {
+ project: s.project.clone(),
+ footage: footage.clone(),
+ expr: None,
+ layer_cache: Arc::default(),
+ gpu: None,
+ gpu_display: false,
+ disk: None,
+ };
+ let f = Frames::default();
+ let key = FrameKey { revision: s.revision, content: f.content_of(&s.project, s.revision, cid), ..key(0, 0, 0) };
+ let key = FrameKey { comp: cid.0, ..key };
+ f.request(&src, key, cid, Tick::ZERO, RenderOpts::default());
+ for _ in 0..1000 {
+ if f.inflight() == 0 {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ assert!(footage.0.load(Ordering::SeqCst) > 0, "the render reached the footage");
+ assert_eq!(f.inflight(), 0, "released");
+ assert!(!f.is_cached(&key));
+ }
+
+ /// #647: a pool thread waiting inside a render (on a parallel loop) never starts another
+ /// frame render on top of it. Dragging a layer queues a frame every UI frame; renders used to
+ /// nest on every thread, each holding its buffers, until memory ran out.
+ #[test]
+ fn a_waiting_render_never_starts_another_on_its_thread() {
+ use effectcraft_engine::Session;
+ use effectcraft_engine::project::{Footage, FootageKind};
+ use effectcraft_engine::render::Image;
+ use effectcraft_engine::time::FrameRate;
+ use serde_json::json;
+ use std::cell::Cell;
+ use std::sync::atomic::AtomicUsize;
+ thread_local! {
+ static DEPTH: Cell<usize> = const { Cell::new(0) };
+ }
+ /// A decoder that works in parallel, as the renderer does: its thread joins the loop and
+ /// may wait for the parts other threads took.
+ #[derive(Default)]
+ struct Parallel {
+ calls: AtomicUsize,
+ deepest: AtomicUsize,
+ }
+ impl FootageSource for Parallel {
+ fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
+ let d = DEPTH.get() + 1;
+ DEPTH.set(d);
+ self.deepest.fetch_max(d, Ordering::SeqCst);
+ self.calls.fetch_add(1, Ordering::SeqCst);
+ (0..64).into_par_iter().for_each(|_| std::thread::sleep(std::time::Duration::from_millis(1)));
+ DEPTH.set(d - 1);
+ Some(Arc::new(Image::new(4, 4)))
+ }
+ }
+ let mut s = Session::default();
+ let cid = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 10})).unwrap()["comp"].as_u64().unwrap());
+ let clip = Footage {
+ kind: FootageKind::Video,
+ width: 4,
+ height: 4,
+ has_video: true,
+ frame_rate: FrameRate::FPS_30,
+ duration: Tick::from_seconds_f64(10.0),
+ ..Default::default()
+ };
+ let item = Arc::make_mut(&mut s.project).add_item("clip.mov", Default::default(), None, ItemKind::Footage(clip));
+ s.execute("layer.addItem", json!({"item": item.0})).unwrap();
+ let footage = Arc::new(Parallel::default());
+ let src = RenderSource {
+ project: s.project.clone(),
+ footage: footage.clone(),
+ expr: None,
+ layer_cache: Arc::default(),
+ gpu: None,
+ gpu_display: false,
+ disk: None,
+ };
+ let f = Frames::default();
+ let content = f.content_of(&s.project, s.revision, cid);
+ let frames = 60;
+ for frame in 0..frames {
+ let key = FrameKey { revision: s.revision, content, comp: cid.0, frame, scale: 1000, view: 0, opts: 0 };
+ f.request(&src, key, cid, FrameRate::FPS_30.tick_of(frame), RenderOpts::default());
+ }
+ for _ in 0..4000 {
+ if f.inflight() == 0 {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ assert_eq!(f.inflight(), 0, "every queued frame rendered");
+ assert!(footage.calls.load(Ordering::SeqCst) >= frames as usize);
+ assert_eq!(footage.deepest.load(Ordering::SeqCst), 1, "renders nested on a waiting thread");
+ }
+
+ /// #329: scrubbing a long movie, the viewer frame of every step the pointer passed started
+ /// rendering and seeked the movie's decoder in turn; the frame under the pointer waited for
+ /// all of them. A newer viewer frame of another time calls off the one rendering, which is
+ /// not cached (its footage may be missing).
+ #[test]
+ fn a_newer_viewer_frame_calls_off_the_one_rendering() {
+ use effectcraft_engine::Session;
+ use effectcraft_engine::project::{Footage, FootageKind};
+ use effectcraft_engine::render::Image;
+ use effectcraft_engine::time::FrameRate;
+ use serde_json::json;
+ use std::cell::RefCell;
+ thread_local! {
+ static CANCEL: RefCell<Option<Arc<AtomicBool>>> = const { RefCell::new(None) };
+ }
+ /// A movie whose first frames take a long seek, which a called-off read gives up.
+ #[derive(Default)]
+ struct Seeking {
+ seeking: AtomicBool,
+ called_off: AtomicBool,
+ }
+ impl FootageSource for Seeking {
+ fn cancellable(&self, cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
+ let prev = CANCEL.replace(Some(cancel.clone()));
+ f();
+ CANCEL.set(prev);
+ }
+ fn frame(&self, _: ItemId, _: &Footage, t: Tick) -> Option<Arc<Image>> {
+ if t < Tick::from_seconds_f64(0.25) {
+ self.seeking.store(true, Ordering::SeqCst);
+ for _ in 0..1000 {
+ if CANCEL.with_borrow(|c| c.as_ref().is_some_and(|c| c.load(Ordering::SeqCst))) {
+ self.called_off.store(true, Ordering::SeqCst);
+ return None;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ }
+ Some(Arc::new(Image::new(4, 4)))
+ }
+ }
+ let mut s = Session::default();
+ let cid = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 10})).unwrap()["comp"].as_u64().unwrap());
+ let clip = Footage {
+ kind: FootageKind::Video,
+ width: 4,
+ height: 4,
+ has_video: true,
+ frame_rate: FrameRate::FPS_30,
+ duration: Tick::from_seconds_f64(10.0),
+ ..Default::default()
+ };
+ let item = Arc::make_mut(&mut s.project).add_item("clip.mov", Default::default(), None, ItemKind::Footage(clip));
+ s.execute("layer.addItem", json!({"item": item.0})).unwrap();
+ let footage = Arc::new(Seeking::default());
+ let src = RenderSource {
+ project: s.project.clone(),
+ footage: footage.clone(),
+ expr: None,
+ layer_cache: Arc::default(),
+ gpu: None,
+ gpu_display: false,
+ disk: None,
+ };
+ let f = Frames::default();
+ let content = f.content_of(&s.project, s.revision, cid);
+ let key = |frame| FrameKey { revision: s.revision, content, comp: cid.0, frame, scale: 1000, view: 0, opts: 0 };
+ f.request_urgent(&src, key(0), cid, Tick::ZERO, RenderOpts::default());
+ for _ in 0..1000 {
+ if footage.seeking.load(Ordering::SeqCst) {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(2));
+ }
+ assert!(footage.seeking.load(Ordering::SeqCst), "the first frame's render reached the footage");
+ f.request_urgent(&src, key(150), cid, FrameRate::FPS_30.tick_of(150), RenderOpts::default());
+ for _ in 0..1000 {
+ if f.inflight() == 0 {
+ break;
+ }
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ assert!(footage.called_off.load(Ordering::SeqCst), "the superseded frame's seek ran on");
+ assert!(f.is_cached(&key(150)), "the newest frame rendered");
+ assert!(!f.is_cached(&key(0)), "a called-off frame is not cached");
+ assert_eq!(f.inflight(), 0, "released");
+ }
+
+ /// #284: scrubbing asks for a viewer frame at every step; a newer one drops the comp's viewer
+ /// frames still waiting (they would all render first), but not prefetch frames or another
+ /// comp's viewer frame.
+ #[test]
+ fn a_newer_viewer_frame_drops_the_ones_still_waiting() {
+ /// Starts nothing, so every request stays queued.
+ struct Idle;
+ impl RemoteFrames for Idle {
+ fn slots(&self) -> usize {
+ 0
+ }
+ fn start(&self, _: RemoteJob, _: RemoteDone) {}
+ }
+ let mut f = Frames::default();
+ f.set_remote(Some(Arc::new(Idle)));
+ let src = RenderSource {
+ project: Arc::default(),
+ footage: Arc::new(effectcraft_engine::render::NoFootage),
+ expr: None,
+ layer_cache: Arc::default(),
+ gpu: None,
+ gpu_display: false,
+ disk: None,
+ };
+ let request = |k: FrameKey, urgent: bool| f.request_with(&src, k, ItemId(k.comp), Tick::ZERO, RenderOpts::default(), urgent);
+ let other = FrameKey { comp: 2, ..key(1, 0, 0) };
+ request(other, true);
+ request(key(1, 20, 0), false);
+ for frame in 0..10 {
+ request(key(1, frame, 0), true);
+ }
+ let mut queued: Vec<(u64, i64, bool)> = f.queue.lock().unwrap().jobs.iter().map(|j| (j.key.comp, j.key.frame, j.urgent)).collect();
+ queued.sort();
+ assert_eq!(queued, [(1, 9, true), (1, 20, false), (2, 0, false)]);
+ assert_eq!(f.inflight(), 3, "dropped frames aren't in flight: the viewer can ask again");
+ }
+
+ /// Each time the GPU runs out of memory, half as many GPU frames are kept, down to a floor.
+ #[test]
+ fn running_out_of_video_memory_halves_the_gpu_frame_budget() {
+ let f = Frames::default();
+ let mut c = f.cache.lock().unwrap();
+ assert_eq!(c.gpu_budget, GPU_BUDGET);
+ c.gpu_out_of_memory();
+ assert_eq!(c.gpu_budget, GPU_BUDGET / 2);
+ for _ in 0..20 {
+ c.gpu_out_of_memory();
+ }
+ assert_eq!(c.gpu_budget, MIN_GPU_BUDGET);
+ }
+
+ #[test]
+ fn least_recently_shown_frames_go_first() {
+ let f = Frames::default();
+ f.set_budget(3 * 16);
+ for i in 0..3 {
+ insert(&f, key(1, i, 0));
+ }
+ // Frame 0 is shown again, so frame 1 is now the oldest.
+ assert!(f.get(&key(1, 0, 0)).is_some());
+ insert(&f, key(1, 3, 0));
+ assert!(f.is_cached(&key(1, 0, 0)) && !f.is_cached(&key(1, 1, 0)));
+ assert!(f.is_cached(&key(1, 2, 0)) && f.is_cached(&key(1, 3, 0)));
+ }
+
+ fn playhead(cur: i64, looping: bool) -> Playhead {
+ Playhead { series: key(1, 0, 0), cur, start: 0, end: 99, step: 1, looping }
+ }
+
+ /// #595: a playing preview's cache is full. The frames just ahead of the playhead were
+ /// rendered before the ones just shown, so plain LRU evicted them first and the preview
+ /// played into a gap. Frames the preview no longer reaches go first, then the ones it
+ /// reaches last.
+ #[test]
+ fn a_full_cache_keeps_the_frames_just_ahead_of_the_playhead() {
+ let f = Frames::default();
+ f.set_budget(4 * 16);
+ // Prefetch rendered 11..=13, then 10 was shown (the most recently used).
+ for i in [11, 12, 13, 10] {
+ insert(&f, key(1, i, 0));
+ }
+ let cached = |f: &Frames| f.cached_frames(&key(1, 0, 0));
+ f.set_playhead(Some(playhead(10, false)));
+ // The next frame rendered is the one the preview reaches last: the frames just ahead stay.
+ insert(&f, key(1, 14, 0));
+ assert_eq!(cached(&f), [10, 11, 12, 13]);
+ // Without looping, a frame behind the playhead goes before any frame ahead of it.
+ f.set_playhead(Some(playhead(12, false)));
+ insert(&f, key(1, 14, 0));
+ assert_eq!(cached(&f), [10, 12, 13, 14]);
+ // Looping, the frame just behind is the one reached last.
+ f.set_playhead(Some(playhead(13, true)));
+ insert(&f, key(1, 0, 0));
+ assert_eq!(cached(&f), [0, 10, 13, 14]);
+ // Stopped: least recently shown first again.
+ f.set_playhead(None);
+ insert(&f, key(1, 50, 0));
+ assert!(!f.is_cached(&key(1, 13, 0)) && f.is_cached(&key(1, 50, 0)));
+ }
+
+ /// A frame source that queues jobs and never renders them: the tests check the queue.
+ struct Idle;
+ impl RemoteFrames for Idle {
+ fn slots(&self) -> usize {
+ 0
+ }
+ fn start(&self, _: RemoteJob, _: RemoteDone) {}
+ }
+
+ fn idle_source() -> RenderSource {
+ RenderSource {
+ project: Arc::default(),
+ footage: Arc::new(effectcraft_engine::render::NoFootage),
+ expr: None,
+ layer_cache: Arc::default(),
+ gpu: None,
+ gpu_display: false,
+ disk: None,
+ }
+ }
+
+ /// #595: prefetch renders the frames the preview reaches first, whatever order they were
+ /// asked for in (a frame evicted and asked for again waited behind every frame further
+ /// ahead); queued frames the preview no longer reaches are dropped and not left in flight.
+ #[test]
+ fn prefetch_renders_the_frames_the_playhead_reaches_first() {
+ let mut f = Frames::default();
+ f.set_remote(Some(Arc::new(Idle)));
+ let src = idle_source();
+ for frame in [40, 30, 20, 5, 12, 11] {
+ f.request(&src, key(1, frame, 0), ItemId(1), Tick::ZERO, RenderOpts::default());
+ }
+ f.request(&src, FrameKey { comp: 2, ..key(1, 1, 0) }, ItemId(2), Tick::ZERO, RenderOpts::default());
+ f.set_playhead(Some(playhead(10, false)));
+ let mut order = vec![];
+ loop {
+ let mut q = f.queue.lock().unwrap();
+ let Some(i) = q.best() else { break };
+ let j = q.jobs.swap_remove(i);
+ order.push((j.key.comp, j.key.frame));
+ }
+ assert_eq!(order, [(1, 11), (1, 12), (1, 20), (1, 30), (1, 40), (2, 1)]);
+ assert_eq!(f.inflight(), 6, "frame 5, behind the playhead, is no longer in flight");
+ }
+
+ /// The cache is full. A zoom (or preview-resolution) change left the frames of the old
+ /// size to the plain least-recently-shown order, so the frames of the zoom the viewer is back
+ /// at went before the ones of the abandoned zoom. The other size's frames go first, then the
+ /// order is as before.
+ #[test]
+ fn a_size_change_evicts_frames_of_other_sizes_first() {
+ let mut f = Frames::default();
+ f.set_remote(Some(Arc::new(Idle)));
+ f.set_budget(4 * 16);
+ let at = |frame, scale| FrameKey { scale, ..key(1, frame, 0) };
+ // 100% (frames 0..=1) is cached, then 50% (2..=3): the 100% frames are the oldest.
+ for (frame, scale) in [(0, 1000), (1, 1000), (2, 500), (3, 500)] {
+ insert(&f, at(frame, scale));
+ }
+ // A passive viewer at 50% asks for a frame: that doesn't change which size is kept.
+ f.request(&idle_source(), at(9, 500), ItemId(1), Tick::ZERO, RenderOpts::default());
+ // Back at 100%: the viewer asks for frame 4.
+ f.request_urgent(&idle_source(), at(4, 1000), ItemId(1), Tick::ZERO, RenderOpts::default());
+ f.request(&idle_source(), at(10, 500), ItemId(1), Tick::ZERO, RenderOpts::default());
+ insert(&f, at(4, 1000));
+ assert!(
+ f.is_cached(&at(0, 1000)) && f.is_cached(&at(1, 1000)) && !f.is_cached(&at(2, 500)) && f.is_cached(&at(3, 500)),
+ "the first frame of the old size goes, not the cached ones of the current size"
+ );
+ insert(&f, at(5, 1000));
+ assert!(!f.is_cached(&at(3, 500)), "then the last frame of the old size");
+ assert_eq!(f.cached_frames(&at(0, 1000)), [0, 1, 4, 5]);
+ // No other size left: least recently shown first again.
+ insert(&f, at(6, 1000));
+ assert_eq!(f.cached_frames(&at(0, 1000)), [1, 4, 5, 6]);
+ }
+
+ #[test]
+ fn frames_are_keyed_by_content_and_series_are_kept_apart() {
+ let f = Frames::default();
+ insert(&f, key(1, 0, 0));
+ insert(&f, key(2, 0, 0));
+ insert(&f, key(2, 1, 7));
+ // Other content or render options (Draft, shadows…) are another series.
+ assert_eq!(f.cached_frames(&key(2, 0, 0)), vec![0]);
+ assert_eq!(f.cached_frames(&key(2, 0, 7)), vec![1]);
+ // A later revision with the same content finds the frame (an edit elsewhere, an undo).
+ assert!(f.is_cached(&FrameKey { revision: 99, ..key(1, 0, 0) }));
+ assert!(!f.is_cached(&key(3, 0, 0)));
+ }
+
+ #[test]
+ fn live_edits_present_the_newest_completed_revision_without_going_backwards() {
+ let f = Frames::default();
+ let shown = key(1, 0, 0);
+ let newest = key(3, 0, 0);
+ let wanted = key(5, 0, 0);
+ insert(&f, shown);
+ insert(&f, newest);
+ insert(&f, key(2, 0, 0)); // Older worker finishes after the newer one.
+ let displayed = f.get_for_viewer(&wanted, Some(&shown)).unwrap().0;
+ assert_eq!(displayed, newest);
+ assert!(f.get_for_viewer(&wanted, Some(&displayed)).is_none(), "never replay an older completion");
+ insert(&f, wanted);
+ assert_eq!(f.get_for_viewer(&wanted, Some(&displayed)).unwrap().0, wanted, "settle on the exact final edit");
+ // Undo reuses the content of an earlier revision immediately.
+ let undo = FrameKey { revision: 6, ..shown };
+ let reused = f.get_for_viewer(&undo, Some(&wanted)).unwrap().0;
+ assert_eq!(reused.content, shown.content);
+ assert_eq!(reused.revision, undo.revision);
+ }
+
+ #[test]
+ fn live_edits_do_not_substitute_other_times_views_or_render_settings() {
+ let f = Frames::default();
+ let shown = key(1, 0, 0);
+ let wanted = key(3, 0, 0);
+ let done = key(2, 0, 0);
+ for mismatch in [
+ FrameKey { comp: 9, ..done },
+ FrameKey { frame: 1, ..done },
+ FrameKey { scale: 500, ..done },
+ FrameKey { view: 1, ..done },
+ FrameKey { opts: 1, ..done },
+ FrameKey { revision: 4, ..done },
+ ] {
+ f.clear();
+ insert(&f, mismatch);
+ assert!(f.get_for_viewer(&wanted, Some(&shown)).is_none(), "wrong candidate: {mismatch:?}");
+ }
+ f.clear();
+ insert(&f, done);
+ assert!(f.get_for_viewer(&wanted, None).is_none(), "no stale content on opening a viewer");
+ assert!(f.get_for_viewer(&wanted, Some(&FrameKey { comp: 9, ..shown })).is_none());
+ assert!(f.get_for_viewer(&wanted, Some(&FrameKey { frame: 1, ..shown })).is_none());
+ assert!(f.get_for_viewer(&wanted, Some(&FrameKey { view: 1, ..shown })).is_none());
+ }
+
+ #[test]
+ fn an_edit_keeps_the_frames_of_comps_it_does_not_touch() {
+ use effectcraft_engine::Session;
+ use serde_json::json;
+ let mut s = Session::default();
+ let a = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 1})).unwrap()["comp"].as_u64().unwrap());
+ let b = ItemId(s.execute("comp.new", json!({"name": "B", "width": 8, "height": 8, "duration": 1})).unwrap()["comp"].as_u64().unwrap());
+ let f = Frames::default();
+ let at = |s: &Session, c: ItemId| f.content_of(&s.project, s.revision, c);
+ let (a0, b0) = (at(&s, a), at(&s, b));
+ // Edit B: A's content (and frames) stay, B's change.
+ s.execute("layer.newSolid", json!({"comp": b.0, "color": "#ff0000"})).unwrap();
+ assert_eq!(at(&s, a), a0);
+ assert_ne!(at(&s, b), b0);
+ // Undo: B is back to the content its old frames were rendered for.
+ s.execute("edit.undo", json!({})).unwrap();
+ assert_eq!(at(&s, b), b0);
+ }
+
+ #[test]
+ fn a_long_drag_keeps_a_bounded_number_of_states() {
+ use effectcraft_engine::Session;
+ use serde_json::json;
+ let mut s = Session::default();
+ let a = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 1})).unwrap()["comp"].as_u64().unwrap());
+ let f = Frames::default();
+ let mut last = None;
+ for i in 0..200 {
+ s.execute("comp.settings", json!({"width": 8 + i, "merge": "drag"})).unwrap();
+ let k = FrameKey { content: f.content_of(&s.project, s.revision, a), ..key(0, 0, 0) };
+ insert(&f, k);
+ last = Some(k);
+ }
+ // The snapshots and the frames kept stay bounded (a dropped identity's frames go with it,
+ // so a later state that reuses its addresses can't be shown an old frame).
+ assert!(f.keepers.lock().unwrap().1.len() <= MAX_KEEPERS);
+ assert!(f.cache.lock().unwrap().map.len() <= MAX_KEEPERS);
+ assert!(f.is_cached(&last.unwrap()), "the current state's frame stays");
+ }
+
+ #[test]
+ fn an_identity_keeps_its_comps_so_an_edit_in_place_still_changes_it() {
+ use effectcraft_engine::Session;
+ use serde_json::json;
+ let mut s = Session::default();
+ let a = ItemId(s.execute("comp.new", json!({"name": "A", "width": 8, "height": 8, "duration": 1})).unwrap()["comp"].as_u64().unwrap());
+ // A project nothing else holds (no undo history): make_mut would edit the comp in place,
+ // at the same address, if the identity hadn't kept a snapshot holding it.
+ let mut p = Arc::new((*s.project).clone());
+ drop(s);
+ let f = Frames::default();
+ let before = f.content_of(&p, 1, a);
+ let comp = Arc::make_mut(&mut p).comp_mut(a).unwrap();
+ comp.width = 16;
+ assert_ne!(f.content_of(&p, 2, a), before);
+ }
+
+ /// Audio, Lock and Shy (and Hide Shy Layers) don't change pixels: toggling them, in the comp
+ /// or a comp it nests, keeps its frames, also when toggled back (#103).
+ #[test]
+ fn audio_lock_and_shy_switches_keep_the_frames() {
+ 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 inner = ItemId(id(s.execute("comp.new", json!({"name": "Inner", "width": 8, "height": 8, "duration": 1})).unwrap(), "comp"));
+ let solid = id(s.execute("layer.newSolid", json!({"comp": inner.0, "color": "#ff0000"})).unwrap(), "layer");
+ let outer = ItemId(id(s.execute("comp.new", json!({"name": "Outer", "width": 8, "height": 8, "duration": 1})).unwrap(), "comp"));
+ let nested = id(s.execute("layer.addItem", json!({"comp": outer.0, "item": inner.0})).unwrap(), "layer");
+ let f = Frames::default();
+ let at = |s: &Session| f.content_of(&s.project, s.revision, outer);
+ let o0 = at(&s);
+ let switch = |s: &mut Session, comp: ItemId, layer: u64, sw: &str, v: bool| {
+ s.execute("layer.setSwitch", json!({"comp": comp.0, "layers": [layer], "switch": sw, "value": v})).unwrap();
+ };
+ for (comp, layer, sw) in [(outer, nested, "audio"), (outer, nested, "lock"), (outer, nested, "shy"), (inner, solid, "audio"), (inner, solid, "shy")] {
+ // Audio off; Lock and Shy on.
+ switch(&mut s, comp, layer, sw, sw != "audio");
+ assert_eq!(at(&s), o0, "{sw} on in {comp:?}");
+ }
+ s.execute("comp.setSwitch", json!({"comp": outer.0, "switch": "hideShy", "value": true})).unwrap();
+ assert_eq!(at(&s), o0, "Hide Shy Layers");
+ switch(&mut s, outer, nested, "audio", true);
+ switch(&mut s, outer, nested, "lock", false);
+ assert_eq!(at(&s), o0, "toggled back");
+ // Switches that change pixels still do.
+ switch(&mut s, inner, solid, "video", false);
+ let o1 = at(&s);
+ assert_ne!(o1, o0, "Video");
+ switch(&mut s, outer, nested, "audio", false);
+ assert_eq!(at(&s), o1, "a mute after a real edit keeps that edit's frames");
+ switch(&mut s, inner, solid, "video", true);
+ assert_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();
+ let id = |v: serde_json::Value, k: &str| v[k].as_u64().unwrap();
- let outer = ItemId(id(s.execute("comp.new", json!({"name": "Outer", "width": 8, "height": 8, "duration": 1})).unwrap(), "comp"));
- let solid = id(s.execute("layer.newSolid", json!({"comp": outer.0, "color": "#ff0000"})).unwrap(), "layer");
++ let inner = ItemId(id(s.execute("comp.new", json!({"name": "Inner", "width": 8, "height": 8, "duration": 2})).unwrap(), "comp"));
++ let solid = id(s.execute("layer.newSolid", json!({"comp": inner.0, "color": "#ff0000"})).unwrap(), "layer");
++ let outer = ItemId(id(s.execute("comp.new", json!({"name": "Outer", "width": 8, "height": 8, "duration": 2})).unwrap(), "comp"));
++ id(s.execute("layer.addItem", json!({"comp": outer.0, "item": inner.0})).unwrap(), "layer");
+ let f = Frames::default();
- let at = |s: &Session| f.content_of(&s.project, s.revision, outer);
- let o0 = at(&s);
- s.execute("layer.addMarker", json!({"comp": outer.0, "layers": [solid], "time": 0.5, "comment": "beat"})).unwrap();
- assert_eq!(at(&s), o0, "a marker changes no pixels, so it keeps the frames");
-}
-
++ let at = |s: &Session, c: ItemId| f.content_of(&s.project, s.revision, c);
++ let (o0, i0) = (at(&s, outer), at(&s, inner));
++ s.execute("comp.addMarker", json!({"comp": inner.0, "time": 0.5, "comment": "beat"})).unwrap();
++ s.execute("comp.addMarker", json!({"comp": outer.0, "time": 1.0})).unwrap();
++ s.execute("comp.addMarker", json!({"comp": outer.0, "time": 1.5})).unwrap();
++ assert_eq!((at(&s, outer), at(&s, inner)), (o0, i0), "comp markers");
++ s.execute("markers.set", json!({"comp": inner.0, "layer": solid, "new": true, "time": 0.25})).unwrap();
++ assert_eq!((at(&s, outer), at(&s, inner)), (o0, i0), "layer marker");
++ // An expression reading markers makes them count again.
++ s.execute("prop.setExpression", json!({"comp": inner.0, "layer": solid, "path": "transform/opacity", "expression": "thisComp.marker.numKeys"})).unwrap();
++ let (o1, i1) = (at(&s, outer), at(&s, inner));
++ assert_ne!(o1, o0);
++ s.execute("comp.addMarker", json!({"comp": inner.0, "time": 1.0})).unwrap();
++ assert_ne!(at(&s, inner), i1, "marker read by an expression");
++ assert_ne!(at(&s, outer), o1, "marker read by an expression in a nested comp");
++}
++
++/// A protected time marker with a duration sets the comp's timing (Responsive Design), so it
++/// changes the frames even though plain markers do not; and any expression, which can read
++/// markers, makes every marker count.
+ #[test]
-fn markers_count_when_an_expression_can_read_them() {
++fn timing_markers_and_expressions_still_change_the_frames() {
+ 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 outer = ItemId(id(s.execute("comp.new", json!({"name": "Outer", "width": 8, "height": 8, "duration": 1})).unwrap(), "comp"));
+ let solid = id(s.execute("layer.newSolid", json!({"comp": outer.0, "color": "#ff0000"})).unwrap(), "layer");
- s.execute("prop.setExpression", json!({"comp": outer.0, "layer": solid, "path": "transform/opacity", "expression": "1"})).unwrap();
+ let f = Frames::default();
+ let at = |s: &Session| f.content_of(&s.project, s.revision, outer);
+ let o0 = at(&s);
+ s.execute("layer.addMarker", json!({"comp": outer.0, "layers": [solid], "time": 0.5, "comment": "beat"})).unwrap();
- assert_ne!(at(&s), o0, "an expression can read the marker, so the frames change");
++ assert_eq!(at(&s), o0, "a plain marker changes no pixels, so it keeps the frames");
++ s.execute("comp.addMarker", json!({"comp": outer.0, "time": 0.25})).unwrap();
++ s.execute("markers.set", json!({"comp": outer.0, "index": 0, "duration": 0.25, "protected": true})).unwrap();
++ let o1 = at(&s);
++ assert_ne!(o1, o0, "a protected time marker with a duration sets the timing");
++ s.execute("prop.setExpression", json!({"comp": outer.0, "layer": solid, "path": "transform/opacity", "expression": "1"})).unwrap();
++ let o2 = at(&s);
++ 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");
+ }
diff --cc crates/ui-egui/tests/it/ui_audio_scrub.rs
index 7c9ec439f,c748afe8e..6c352625a
--- a/crates/ui-egui/tests/it/ui_audio_scrub.rs
+++ b/crates/ui-egui/tests/it/ui_audio_scrub.rs
@@@ -187,36 -291,9 +291,37 @@@ fn preview_with_audio_sounds_once_rende
}
let f = frame(&h);
assert!(f == last || f == last + 1, "{last} → {f}: a frame was skipped");
+ shown += usize::from(f != last);
last = f;
- std::thread::sleep(std::time::Duration::from_millis(20));
}
assert!(h.state().audio.is_some() && fake.0.lock().unwrap().0.is_some(), "the sound started within 2 s, at frame {last} of 900");
+ assert!(shown >= 3, "the sound starts only after the silent preview kept up for a moment");
}
+
+/// Adding markers while a preview sounds (Num*, one per beat) keeps the sound going (#1083): a
+/// marker changes no pixels, so no cached frame is dropped and no frame has to be rendered again
+/// (which would stop the sound until the frames ahead are cached).
+#[test]
+fn adding_markers_does_not_stop_the_sound() {
+ let fake = Fake::default();
+ let (app, cid) = setup(true, &fake);
+ // Everything is cached before the preview starts, so the sound starts at once.
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 31.0).build_eframe(|_| app);
+ h.run_steps(2);
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
+ let now = h.ctx.input(|i| i.time);
+ h.state_mut().play(now);
+ while h.state().audio.is_none() && std::time::Instant::now() < deadline {
+ h.step();
+ std::thread::sleep(std::time::Duration::from_millis(1));
+ }
+ assert!(h.state().audio.is_some(), "the sound started");
+ for beat in 0..6 {
+ let t = 0.1 + 0.05 * beat as f64;
+ h.state_mut().session.execute("comp.addMarker", json!({"comp": cid.0, "time": t, "comment": "beat"})).unwrap();
+ h.step();
+ let app = h.state();
+ assert!(app.playback.playing && app.audio.is_some() && !app.playback.audio_held, "marker {beat}: still playing with sound");
+ }
+ assert_eq!(h.state().session.project.comp(cid).unwrap().markers.len(), 6);
+}