From 74099e32bcc422beddcf55ac3d375846288166cb Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 10:31:38 +0100 Subject: [PATCH] Footage cache (#3271): a region of interest asks for rows only where the source reads rows With a viewer region of interest the renderer asked a PNG still for some rows, which the media pool reads whole anyway, and the layer was left out of the layer cache: its masks and effects ran again on every render. FootageSource::reads_rows tells whether a source reads only the rows asked for (the media pool: an OpenEXR frame whose channels no effect reads); otherwise the renderer asks for the whole frame and the layer stays cached. A test renders a still twice under a region; it fails on the old code (two row reads instead of one whole read). --- crates/automation/src/confine.rs | 4 +++ crates/media/src/pool.rs | 14 +++++++- crates/render/src/footage.rs | 10 ++++++ crates/render/src/pipeline.rs | 3 +- crates/render/src/tests/footage_size.rs | 47 +++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/crates/automation/src/confine.rs b/crates/automation/src/confine.rs index 46e656d39..578ea8e05 100644 --- a/crates/automation/src/confine.rs +++ b/crates/automation/src/confine.rs @@ -385,6 +385,10 @@ impl FootageSource for ConfinedFootage { fn frame_rows(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, rows: (i64, i64)) -> Option> { self.readable(footage).then(|| self.inner.frame_rows(item, footage, t, size, float, rows)).flatten() } + /// Whether the inner source reads only the rows asked for (footage outside the roots: no). + fn reads_rows(&self, footage: &Footage, t: Tick) -> bool { + self.readable(footage) && self.inner.reads_rows(footage, t) + } /// The footage audio from the inner source, or `None` when the footage lies outside the allowed roots. fn audio(&self, item: ItemId, footage: &Footage, t: Tick, frames: usize, rate: u32) -> Option> { self.readable(footage).then(|| self.inner.audio(item, footage, t, frames, rate)).flatten() diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 9be4a3fd7..f10127dcd 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -1103,6 +1103,13 @@ impl Inner { self.frame_at_rows(footage, t, playback, size, float, None) } + /// Whether a request for some rows of `footage` at `t` reads only those: an OpenEXR frame + /// (not a movie) whose channels no effect reads. + fn reads_rows(&self, footage: &Footage, t: Tick) -> bool { + let loc = Self::locate(footage, t, FloatCurve::Srgb); + loc.media_t.is_none() && crate::exr_planes::is_exr(&loc.key.path) && lock(&self.aux_footage).plan(&footage.path).is_none() + } + /// [`Inner::frame_at`] of only the rows `rows` of an OpenEXR frame whose channels no effect /// reads (#3271; others are read whole). fn frame_at_rows( @@ -1119,7 +1126,7 @@ impl Inner { } let mut loc = Self::locate(footage, t, float); let movie = loc.media_t.is_some(); - loc.key.rows = rows.filter(|_| !movie && crate::exr_planes::is_exr(&loc.key.path) && lock(&self.aux_footage).plan(&footage.path).is_none()); + loc.key.rows = rows.filter(|_| self.reads_rows(footage, t)); // A still asked for at its own size is the unsized frame (one decode, one entry). if let Some(size) = size.filter(|s| movie || *s != (footage.width, footage.height)) { loc.key.size = size; @@ -1573,6 +1580,11 @@ impl FootageSource for MediaPool { } Inner::frame_at_rows(&self.inner, footage, t, true, size, float, Some(rows)).ok() } + /// Only an OpenEXR frame whose channels no effect reads is read by rows (others whole, see + /// [`Inner::frame_at_rows`]). + fn reads_rows(&self, footage: &Footage, t: Tick) -> bool { + !footage.missing && self.inner.reads_rows(footage, t) + } fn problem(&self, footage: &Footage) -> Option { if footage.missing || footage.kind != FootageKind::Video { diff --git a/crates/render/src/footage.rs b/crates/render/src/footage.rs index 3407d062c..9eff834f3 100644 --- a/crates/render/src/footage.rs +++ b/crates/render/src/footage.rs @@ -45,6 +45,13 @@ pub trait FootageSource: Send + Sync { let _ = rows; self.frame_with(item, footage, t, size, float) } + /// Whether [`FootageSource::frame_rows`] reads only the rows asked for of `footage` at `t` + /// (#3271). A source that reads the whole frame anyway leaves the default: the renderer then + /// asks for the whole frame, which its layer cache keeps. + fn reads_rows(&self, footage: &Footage, t: Tick) -> bool { + let _ = (footage, t); + false + } /// `frames` interleaved stereo sample frames of `item`'s audio from source time `t` at /// `rate` Hz (`None` when unavailable). The first sample is `t.to_units_floor(rate)`; /// a sample boundary uses the earliest representable tick that selects that sample. @@ -284,6 +291,9 @@ impl FootageSource for CancellableFootage<'_> { fn frame_rows(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, rows: (i64, i64)) -> Option> { self.read(|| self.inner.frame_rows(item, footage, t, size, float, rows)) } + fn reads_rows(&self, footage: &Footage, t: Tick) -> bool { + self.inner.reads_rows(footage, t) + } } /// Footage that can be rasterised at any scale (SVG). diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index d000ec2d0..1673eb1ae 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -962,7 +962,8 @@ impl<'a> Renderer<'a> { return None; } let fx_margin = effects_margin(ctx, layer)?; - if comp_reads_pixels(ctx) { + // (a source that reads whole frames anyway: the whole frame, which the layer cache keeps) + if comp_reads_pixels(ctx) || !self.footage.reads_rows(f, ctx.source_time(layer)) { return None; } let inv = ctx.world_matrix(layer).inverse()?; diff --git a/crates/render/src/tests/footage_size.rs b/crates/render/src/tests/footage_size.rs index 273184ec8..3ddccdb44 100644 --- a/crates/render/src/tests/footage_size.rs +++ b/crates/render/src/tests/footage_size.rs @@ -140,3 +140,50 @@ fn exr_sequence_layers_are_cached_per_frame() { assert_eq!(src.sizes.unwrap().into_inner().unwrap().len(), fetches, "{ext}"); } } + +/// #3271 review: with a viewer region of interest, a still from a source that reads whole frames +/// anyway (not by rows) is asked for whole and stays in the layer cache: a second render reads +/// nothing. A source that reads rows is asked for only the rows the region shows. +#[test] +fn a_source_that_reads_whole_frames_keeps_the_layer_cached_under_a_region() { + struct Counting { + reads: Mutex>>, + by_rows: bool, + } + impl FootageSource for Counting { + fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option> { + self.reads.lock().unwrap().push(None); + Clip { sizes: None }.frame(item, f, t) + } + fn frame_rows(&self, item: ItemId, f: &Footage, t: Tick, _: Option<(u32, u32)>, _: crate::FloatCurve, rows: (i64, i64)) -> Option> { + self.reads.lock().unwrap().push(Some(rows)); + Clip { sizes: None }.frame(item, f, t) + } + fn reads_rows(&self, _: &Footage, _: Tick) -> bool { + self.by_rows + } + } + let (mut p, cid) = project(BitDepth::Bpc8, false); + let Some(LayerSource::Footage { item }) = p.comp(cid).map(|c| c.layers[0].source.clone()) else { panic!("footage layer") }; + if let Some(ItemKind::Footage(f)) = p.item_mut(item).map(|it| &mut it.kind) { + f.kind = FootageKind::Still; + f.path = "/still.png".into(); + } + let opts = RenderOpts { roi: Some([0.0, 0.0, 96.0, 10.0]), ..Default::default() }; + for by_rows in [false, true] { + let src = Counting { reads: Mutex::default(), by_rows }; + let cache = crate::LayerCache::default(); + for _ in 0..2 { + let mut r = Renderer::new(&p, &src, opts); + r.cache = Some(&cache); + r.comp_frame(cid, Tick::ZERO); + } + let reads = src.reads.into_inner().unwrap(); + if by_rows { + assert_eq!(reads.len(), 2, "by rows, each render: {reads:?}"); + assert!(reads.iter().all(|r| r.is_some_and(|(y0, y1)| y0 == 0 && y1 < 54)), "{reads:?}"); + } else { + assert_eq!(reads, [None], "whole, once: the layer cache keeps it"); + } + } +}