Disk cache: record the source files each frame reads

Part of #3184 (step 1): renders now report which footage files they read,
each with its identity (size and modification time) as read. Nothing uses
the list yet: images, cache keys and cache behaviour are unchanged.

- render: `reads` module with `FileStamp`, `ReadFile`, `ReadFiles` (sorted,
  deduplicated, bounded; marked incomplete when it can't vouch for every
  file) and the thread-safe `ReadLog`. `Renderer::reads` collects a frame's
  files; `FootageSource::recording` lets a source note what it reads.
  `file_stamp` uses the same identity.
- Layer cache entries keep the files their buffer was made of and report
  them on every hit; derived entries (styles, blend space, flattened) keep
  their base's. Buffers from the disk cache or the browser store don't know
  their files yet and mark the list incomplete.
- Media pool: every file read notes its identity, taken from the file it
  opens. Decoded frames, opened movies, GIFs, models and EXR channels keep
  it with the cache entry, so a frame served from memory reports its file
  without touching the disk.
- Viewer frame worker collects each frame's list and carries it to where
  the disk entry is written (step 2 stores it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Opus 5.5 committed 2026-11-06 22:20:40 +01:00
1 parent 69114a8294
commit 91443ef25e
20 files changed
+968 -247

No files matched your search

Generated
+1
View File
@@ -1741,6 +1741,7 @@ version = "0.21.0"
dependencies = [
"craft-docpath",
"effectcraft-color",
"effectcraft-effects",
"effectcraft-model",
"effectcraft-pdf",
"effectcraft-project",
+4
View File
@@ -36,3 +36,7 @@ rayon = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
log = { workspace = true }
[dev-dependencies]
# (tests render comps that read layers through effects; already a dependency of effectcraft-render)
effectcraft-effects = { workspace = true }
+50 -34
View File
@@ -17,6 +17,7 @@ use std::sync::{Arc, Mutex, OnceLock, PoisonError};
use effectcraft_raster::channels3d::{BACKGROUND_DEPTH, split_channel};
use effectcraft_raster::{AuxChannels, Image};
use effectcraft_render::reads::{ReadFile, ReadFiles};
use exr::block::UncompressedBlock;
use exr::block::chunk::TileCoordinates;
use exr::image::read::image::ReadImage;
@@ -477,8 +478,8 @@ fn layered_picture_of(aux: &AuxChannels, op: AlphaOp) -> Option<Image> {
/// decoded anyway), else of the picture's channels alone (also when the cache has no channels
/// for the file: a read that failed, or a broken part the picture doesn't use). `Err(Some(why))`
/// when the file is too large to decode, `Err(None)` when it isn't a readable multi-layer EXR.
pub(crate) fn layered_picture(path: &str, bytes: Arc<[u8]>, op: AlphaOp, shared: bool) -> Result<Image, Option<String>> {
if shared && let Some(d) = cache().get(path, Use::Picture, || Some(bytes.clone())) {
pub(crate) fn layered_picture(path: &str, bytes: Arc<[u8]>, file: &ReadFile, op: AlphaOp, shared: bool) -> Result<Image, Option<String>> {
if shared && let Some(d) = cache().get(path, Use::Picture, || read_as(file, bytes.clone())) {
return layered_picture_of(&d.aux, op).ok_or(None);
}
let (d, rgb, a) = picture_only(&bytes).map_err(|r| match r {
@@ -499,8 +500,8 @@ pub(crate) fn layered_picture(path: &str, bytes: Arc<[u8]>, op: AlphaOp, shared:
/// decoder shows for a file it reads, [`layered_image`]'s rule for one it refuses. `None` when
/// the planes can't stand in for the `image` crate's picture (decode with it), or the file is
/// not a readable EXR.
pub(crate) fn cached_picture(path: &str, bytes: Arc<[u8]>, op: AlphaOp) -> Option<Image> {
let d = cache().get(path, Use::Picture, || Some(bytes))?;
pub(crate) fn cached_picture(path: &str, bytes: Arc<[u8]>, file: &ReadFile, op: AlphaOp) -> Option<Image> {
let d = cache().get(path, Use::Picture, || read_as(file, bytes))?;
let aux = &d.aux;
match d.rgb {
Rgb::None => layered_picture_of(aux, op),
@@ -536,8 +537,16 @@ const CACHE_FILES: usize = 64;
/// rendered at once. The oldest stops being kept when another comes.
const HELD_FILES: usize = 4;
/// A file's decode: done once, by the first caller; the others wait for it.
type Slot = Arc<OnceLock<Option<Arc<Decoded>>>>;
/// A file's decode: done once, by the first caller; the others wait for it. With the files read
/// for it (see [`crate::reads`]), noted again on every hit.
type Slot = Arc<OnceLock<(Option<Arc<Decoded>>, ReadFiles)>>;
/// Bytes the caller already read from `file`, given to the cache as read from it (so the entry
/// keeps the file's identity).
fn read_as(file: &ReadFile, bytes: Arc<[u8]>) -> Option<Arc<[u8]>> {
crate::reads::note(file.clone());
Some(bytes)
}
struct Entry {
path: String,
@@ -554,8 +563,8 @@ impl Entry {
fn in_use(&self) -> bool {
match self.slot.get() {
None => true,
Some(None) => false,
Some(Some(d)) => Arc::strong_count(d) > 1 || Arc::strong_count(&d.aux) > 1,
Some((None, _)) => false,
Some((Some(d), _)) => Arc::strong_count(d) > 1 || Arc::strong_count(&d.aux) > 1,
}
}
}
@@ -648,30 +657,18 @@ impl ChannelCache {
// is safe because the decode never waits on rayon's global pool (the exr crate
// decompresses on a pool of its own and this thread only receives the blocks), so the
// decoding thread can't be waiting for this one.
let v = slot
.get_or_init(|| {
decoded_here = true;
self.decodes.fetch_add(1, Ordering::Relaxed);
#[cfg(test)]
count_decode(true);
let _abandon = Abandon { cache: self, slot: &slot };
let bytes = read()?;
// The planes it will allocate count against the budget while it decodes.
if let Ok(meta) = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(&bytes[..]), false)
&& let Some(need) = planes_bytes(&meta, &|_| true)
{
self.account(&slot, need.min(MAX_DECODE_BYTES));
}
match decode(&bytes, &|_| true) {
Ok(d) => Some(Arc::new(d)),
Err(Refused::TooLarge(why)) => {
log::warn!("media: {path}: {why}");
None
}
Err(Refused::Unreadable) => None,
}
})
.clone();
let (v, files) = slot.get_or_init(|| {
decoded_here = true;
self.decodes.fetch_add(1, Ordering::Relaxed);
#[cfg(test)]
count_decode(true);
let _abandon = Abandon { cache: self, slot: &slot };
let (bytes, files) = crate::reads::captured(read);
(bytes.and_then(|bytes| self.decode_file(path, &slot, &bytes)), files)
});
// The file(s) the decode was made of, read now or for an earlier caller.
crate::reads::note_all(files);
let v = v.clone();
if decoded_here {
self.account(&slot, v.as_ref().map_or(0, |d| d.bytes()));
}
@@ -692,6 +689,24 @@ impl ChannelCache {
v
}
/// Decode the file at `path` whose bytes are `bytes` for `slot` (see [`ChannelCache::get`]).
fn decode_file(&self, path: &str, slot: &Slot, bytes: &[u8]) -> Option<Arc<Decoded>> {
// The planes it will allocate count against the budget while it decodes.
if let Ok(meta) = exr::meta::MetaData::read_from_buffered(std::io::Cursor::new(bytes), false)
&& let Some(need) = planes_bytes(&meta, &|_| true)
{
self.account(slot, need.min(MAX_DECODE_BYTES));
}
match decode(bytes, &|_| true) {
Ok(d) => Some(Arc::new(d)),
Err(Refused::TooLarge(why)) => {
log::warn!("media: {path}: {why}");
None
}
Err(Refused::Unreadable) => None,
}
}
/// Drop least recently used files not in use until the rest fit the budget, keeping `newest`.
fn evict(entries: &mut Vec<Entry>, budget: usize, newest: &Slot) {
while entries.iter().map(|e| e.bytes).sum::<usize>() > budget || entries.len() > CACHE_FILES {
@@ -996,7 +1011,7 @@ mod tests {
let path = "/474-shared-fallback.exr";
assert!(cache().get(path, Use::Channels, || None).is_none(), "a read that failed");
let op = AlphaOp::of(&Footage::default(), FloatCurve::Linear);
let img = layered_picture(path, bytes, op, true).unwrap();
let img = layered_picture(path, bytes, &ReadFile::new(path, effectcraft_render::reads::FileStamp::Virtual), op, true).unwrap();
assert_eq!(img.get(1, 1), [0.5, 0.5, 0.5, 1.0]);
}
@@ -1174,7 +1189,8 @@ mod tests {
for curve in [FloatCurve::Srgb, FloatCurve::Linear, FloatCurve::Gamma24] {
let op = AlphaOp::of(&Footage { alpha, premul_color: [0.2, 0.4, 0.6], ..Default::default() }, curve);
let want = crate::convert::dynamic_to_image(&img, op);
let got = cached_picture(&path, bytes.as_slice().into(), op).unwrap();
let got = cached_picture(&path, bytes.as_slice().into(), &ReadFile::new(path.as_str(), effectcraft_render::reads::FileStamp::Virtual), op)
.unwrap();
assert_eq!((got.width, got.height), (want.width, want.height));
assert_eq!(bits(&got), bits(&want), "file {k} {alpha:?} {curve:?}");
}
+25 -1
View File
@@ -25,6 +25,7 @@ mod layered;
pub use layered::vector_doc;
mod pool;
mod probe;
mod reads;
mod stream;
mod wav_markers;
mod webp_anim;
@@ -85,7 +86,15 @@ pub fn is_importable(path: &std::path::Path) -> bool {
/// A whole file's bytes, refusing anything that isn't a regular file: a project can name
/// `/dev/zero` or a FIFO as footage, and reading those never ends (#2134).
pub(crate) fn read_regular_file(path: &std::path::Path) -> std::io::Result<Vec<u8>> {
read_regular_file_stamped(path).map(|(bytes, _)| bytes)
}
/// [`read_regular_file`] with the file's identity, from the metadata of the open file (no
/// second look at the path).
pub(crate) fn read_regular_file_stamped(path: &std::path::Path) -> std::io::Result<(Vec<u8>, effectcraft_render::reads::FileStamp)> {
use std::io::Read;
#[cfg(test)]
count_open(path);
let mut file = std::fs::File::open(path)?;
let meta = file.metadata()?;
if !meta.is_file() {
@@ -95,7 +104,22 @@ pub(crate) fn read_regular_file(path: &std::path::Path) -> std::io::Result<Vec<u
// A sparse file can claim far more than memory: an error, not an abort.
bytes.try_reserve_exact(usize::try_from(meta.len()).unwrap_or(usize::MAX)).map_err(|e| std::io::Error::new(std::io::ErrorKind::OutOfMemory, e))?;
file.read_to_end(&mut bytes)?;
Ok(bytes)
Ok((bytes, effectcraft_render::reads::FileStamp::of_metadata(&meta)))
}
#[cfg(test)]
static OPENS: std::sync::Mutex<Vec<std::path::PathBuf>> = std::sync::Mutex::new(Vec::new());
/// Count an open of a file (tests: which files a render touched on disk).
#[cfg(test)]
pub(crate) fn count_open(path: &std::path::Path) {
OPENS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(path.to_path_buf());
}
/// How many times files under `dir` were opened so far (tests).
#[cfg(test)]
pub(crate) fn opens_under(dir: &std::path::Path) -> usize {
OPENS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).iter().filter(|p| p.starts_with(dir)).count()
}
pub(crate) fn ext_of(path: &std::path::Path) -> String {
+128 -70
View File
@@ -6,6 +6,7 @@ use std::sync::{Arc, Condvar, Mutex, OnceLock};
use effectcraft_project::{AlphaMode, Footage, FootageKind, ItemId};
use effectcraft_raster::{Image, Px};
use effectcraft_render::reads::{FileStamp, ReadFile, ReadFiles, ReadLog};
use effectcraft_render::{FloatCurve, FootageSource};
use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick};
use filmcraft_media::{FrameRequest, SharedSource};
@@ -14,6 +15,7 @@ use image::metadata::Orientation;
use crate::convert::{AlphaOp, dynamic_to_image, frame_to_image_in, frame_to_image_scaled, turned};
use crate::downmix::Order;
use crate::gif_anim::{self, Anim};
use crate::reads;
use crate::{MediaError, Result};
/// Default frame-cache budget: 1 GiB of decoded frames.
@@ -53,6 +55,8 @@ struct Key {
struct Entry {
img: Arc<Image>,
/// The files the frame was decoded from (see [`crate::reads`]).
files: Arc<ReadFiles>,
stamp: u64,
bytes: usize,
}
@@ -106,18 +110,18 @@ struct Lru {
}
impl Lru {
fn get(&mut self, k: &Key) -> Option<Arc<Image>> {
fn get(&mut self, k: &Key) -> Option<(Arc<Image>, Arc<ReadFiles>)> {
let e = self.map.get_mut(k)?;
self.order.remove(&e.stamp);
self.clock += 1;
e.stamp = self.clock;
self.order.insert(e.stamp, k.clone());
Some(e.img.clone())
Some((e.img.clone(), e.files.clone()))
}
fn insert(&mut self, k: Key, img: Arc<Image>, budget: usize) {
fn insert(&mut self, k: Key, img: Arc<Image>, files: Arc<ReadFiles>, budget: usize) {
let bytes = img.data.len() * std::mem::size_of::<Px>() + 64;
self.clock += 1;
if let Some(old) = self.map.insert(k.clone(), Entry { img, stamp: self.clock, bytes }) {
if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes }) {
self.order.remove(&old.stamp);
self.bytes -= old.bytes;
}
@@ -180,8 +184,9 @@ struct CacheState {
}
/// A movie/audio source by path ([`Opened`]), opened by the first caller while the others wait
/// for it (`None`: failed to open; not retried until `forget`).
type SourceSlot = Arc<OnceLock<Option<Opened>>>;
/// for it (`None`: failed to open; not retried until `forget`), with the file's identity as
/// opened.
type SourceSlot = Arc<OnceLock<(Option<Opened>, ReadFile)>>;
/// An opened movie/audio source, the turn its picture is shown with and the order its decoded
/// audio channels arrive in.
@@ -211,7 +216,7 @@ fn channel_order(src: &SharedSource, header: Option<&[u8]>, file: Option<&dyn fi
}
/// An animated GIF by path, as [`SourceSlot`] (the error message when it failed to load).
type GifSlot = Arc<OnceLock<std::result::Result<Arc<Anim>, String>>>;
type GifSlot = Arc<OnceLock<(std::result::Result<Arc<Anim>, String>, ReadFile)>>;
struct Inner {
/// Opened movie/audio sources by path.
@@ -220,8 +225,9 @@ struct Inner {
opened: AtomicU64,
/// In-memory files registered with `add_bytes` (web builds, tests).
files: Mutex<HashMap<String, Arc<[u8]>>>,
/// Parsed 3D models by path (`None`: failed to load; not retried until `forget`).
models: Mutex<HashMap<String, Option<Arc<effectcraft_model::Model>>>>,
/// Parsed 3D models by path (`None`: failed to load; not retried until `forget`), with the
/// files read for them (the model's and the resources it names).
models: Mutex<HashMap<String, (Option<Arc<effectcraft_model::Model>>, Arc<ReadFiles>)>>,
/// Decoded animated GIFs and WebPs by path, loaded by the first caller while the others wait (an
/// `Err` is not retried until `forget`).
gifs: Mutex<HashMap<Arc<str>, GifSlot>>,
@@ -439,11 +445,13 @@ impl MediaPool {
}
/// The conformed-audio file of `path` at `rate` (named by a hash of the path, size and
/// modification time, so an edited file conforms again, and of `CONFORM_VERSION`).
/// modification time, so an edited file conforms again, and of `CONFORM_VERSION`). Notes
/// `path` as read with that identity (see [`crate::reads`]): its samples come from it.
pub fn conformed_path(&self, path: &str, rate: u32) -> Option<std::path::PathBuf> {
use std::hash::{Hash, Hasher};
let folder = lock(&self.inner.conform).clone()?;
let meta = std::fs::metadata(path).ok()?;
reads::note(ReadFile::new(path, FileStamp::of_metadata(&meta)));
let mut h = std::collections::hash_map::DefaultHasher::new();
(CONFORM_VERSION, path, meta.len(), meta.modified().ok()).hash(&mut h);
Some(folder.join(format!("{:016x}_{rate}.ecaf", h.finish())))
@@ -612,16 +620,20 @@ impl Inner {
/// A 3D model file and its sibling resources (buffers, textures, MTL files), parsed once.
fn model(&self, path: &str) -> Option<Arc<effectcraft_model::Model>> {
if let Some(m) = lock(&self.models).get(path) {
return m.clone();
let found = lock(&self.models).get(path).cloned();
if let Some((m, files)) = found {
reads::note_all(&files);
return m;
}
let loaded = self.read(path).map_err(|e| e.to_string()).and_then(|bytes| {
// Files the model names are read only when local or on the model's own network share.
let resolve = |uri: &str| -> Option<Vec<u8>> {
let p = craft_docpath::resolve_absolute(uri, Some(std::path::Path::new(path))).ok()?;
self.read(&p.to_string_lossy()).ok().map(|b| b.to_vec())
};
effectcraft_model::load(path, &bytes, &resolve).map_err(|e| e.to_string())
let (loaded, files) = reads::captured(|| {
self.read(path).map_err(|e| e.to_string()).and_then(|bytes| {
// Files the model names are read only when local or on the model's own network share.
let resolve = |uri: &str| -> Option<Vec<u8>> {
let p = craft_docpath::resolve_absolute(uri, Some(std::path::Path::new(path))).ok()?;
self.read(&p.to_string_lossy()).ok().map(|b| b.to_vec())
};
effectcraft_model::load(path, &bytes, &resolve).map_err(|e| e.to_string())
})
});
let m = match loaded {
Ok(m) => Some(Arc::new(m)),
@@ -630,14 +642,29 @@ impl Inner {
None
}
};
lock(&self.models).entry(path.to_string()).or_insert(m).clone()
let (m, files) = lock(&self.models).entry(path.to_string()).or_insert((m, Arc::new(files))).clone();
reads::note_all(&files);
m
}
/// The bytes of the file at `path`, noted as read (see [`crate::reads`]).
fn read(&self, path: &str) -> Result<Arc<[u8]>> {
if let Some(b) = lock(&self.files).get(path) {
return Ok(b.clone());
let (bytes, file) = self.read_file(path);
reads::note(file);
bytes
}
/// The bytes of the file at `path` and its identity as read ([`FileStamp::Missing`] when it
/// can't be read), not noted.
fn read_file(&self, path: &str) -> (Result<Arc<[u8]>>, ReadFile) {
let in_memory = lock(&self.files).get(path).cloned();
if let Some(b) = in_memory {
return (Ok(b), ReadFile::new(path, FileStamp::Virtual));
}
match crate::read_regular_file_stamped(std::path::Path::new(path)) {
Ok((bytes, stamp)) => (Ok(bytes.into()), ReadFile::new(path, stamp)),
Err(e) => (Err(MediaError::Io(format!("{path}: {e}"))), ReadFile::new(path, FileStamp::Missing)),
}
crate::read_regular_file(std::path::Path::new(path)).map(Into::into).map_err(|e| MediaError::Io(format!("{path}: {e}")))
}
/// The opened movie/audio source for `path` ([`Opened`]). The first caller opens it, outside
@@ -645,35 +672,42 @@ impl Inner {
/// files are decoded whole); callers asking for the same file meanwhile wait for that instead
/// of opening it again: a preview's frame workers, audio feeder, conform and waveform threads
/// each decoded a long FLAC in full at once (#408).
/// Each call notes the file as read (see [`crate::reads`]), with its identity as opened.
fn opened(&self, path: &Arc<str>) -> Option<Opened> {
let slot = lock(&self.sources).entry(path.clone()).or_default().clone();
slot.get_or_init(|| {
self.opened.fetch_add(1, Ordering::Relaxed);
// Files on disk are read in place (the index, then samples on demand) rather than
// whole; in-memory files (`add_bytes`) and wasm32 builds open from bytes.
let in_memory = lock(&self.files).contains_key(&**path);
let file = std::path::Path::new(&**path);
let in_place = if in_memory { None } else { crate::stream::open_source(file) };
let in_place = in_place.map(|src| {
let (header, reader) = (crate::stream::movie_header(file), crate::stream::file_reader(file));
src.map(|src| Opened::new(src, header.as_deref(), reader.as_deref()))
let (opened, file) = slot.get_or_init(|| self.open(path));
reads::note(file.clone());
opened.clone()
}
/// Open the movie/audio file at `path` (see [`Inner::opened`]), with its identity as opened.
fn open(&self, path: &Arc<str>) -> (Option<Opened>, ReadFile) {
self.opened.fetch_add(1, Ordering::Relaxed);
// Files on disk are read in place (the index, then samples on demand) rather than
// whole; in-memory files (`add_bytes`) and wasm32 builds open from bytes.
let in_memory = lock(&self.files).contains_key(&**path);
let file = std::path::Path::new(&**path);
let in_place = if in_memory { None } else { crate::stream::open_source(file) };
let in_place = in_place.map(|(src, stamp)| {
let (header, reader) = (crate::stream::movie_header(file), crate::stream::file_reader(file));
(src.map(|src| Opened::new(src, header.as_deref(), reader.as_deref())), ReadFile::new(path.clone(), stamp))
});
let (opened, read) = in_place.unwrap_or_else(|| {
let (bytes, read) = self.read_file(path);
let opened = bytes.and_then(|bytes| {
let name = std::path::Path::new(&**path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
let src = filmcraft_codecs::open_bytes(&name, bytes.clone()).map_err(MediaError::from)?;
Ok(Opened::new(src, Some(&bytes), Some(&filmcraft_media::reader::MemReader(bytes.clone()))))
});
let opened = in_place.unwrap_or_else(|| {
self.read(path).and_then(|bytes| {
let name = std::path::Path::new(&**path).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
let src = filmcraft_codecs::open_bytes(&name, bytes.clone()).map_err(MediaError::from)?;
Ok(Opened::new(src, Some(&bytes), Some(&filmcraft_media::reader::MemReader(bytes.clone()))))
})
});
match opened {
Ok(s) => Some(s),
Err(e) => {
self.note_problem(path, "", &e);
None
}
(opened, read)
});
match opened {
Ok(s) => (Some(s), read),
Err(e) => {
self.note_problem(path, "", &e);
(None, read)
}
})
.clone()
}
}
/// Remember why the movie at `path` cannot be shown (`codec` is the footage's, when known)
@@ -694,13 +728,15 @@ impl Inner {
/// map's lock; callers asking for the same file meanwhile wait for that.
fn gif(&self, path: &Arc<str>) -> Result<Arc<Anim>> {
let slot = lock(&self.gifs).entry(path.clone()).or_default().clone();
let loaded = slot.get_or_init(|| {
let loaded = self.read(path).and_then(|bytes| Anim::load(&bytes));
loaded.map(Arc::new).map_err(|e| {
let (loaded, file) = slot.get_or_init(|| {
let (bytes, file) = self.read_file(path);
let loaded = bytes.and_then(|bytes| Anim::load(&bytes)).map(Arc::new).map_err(|e| {
log::warn!("media: cannot decode {path}: {e}");
e.to_string()
})
});
(loaded, file)
});
reads::note(file.clone());
loaded.clone().map_err(|e| MediaError::Decode(format!("{path}: {e}")))
}
@@ -783,19 +819,15 @@ impl Inner {
let mut c = lock(&self.cache);
// sequential playback of a movie: read the next frame ahead
if playback && movie {
let prev = c.last.insert(loc.key.path.clone(), loc.key.frame);
if prev == Some(loc.key.frame - 1) && self.read_ahead.load(Ordering::Relaxed) {
let next_t = t + footage.frame_rate.frame_duration();
let mut next = Self::locate(footage, next_t, float);
next.key.size = loc.key.size;
if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) {
self.prefetch(footage, next_t, size, float);
}
}
self.prefetch_next(&mut c, &loc, footage, t, size, float);
}
// (Each exit notes the files the frame came from, see `crate::reads`; a cached frame
// notes those it was decoded from, without touching them.)
loop {
if let Some(img) = c.lru.get(&loc.key) {
if let Some((img, files)) = c.lru.get(&loc.key) {
self.hits.fetch_add(1, Ordering::Relaxed);
drop(c);
reads::note_all(&files);
return Ok(img);
}
if !c.inflight.contains(&loc.key) {
@@ -808,7 +840,9 @@ impl Inner {
if rayon::current_thread_index().is_some() {
self.misses.fetch_add(1, Ordering::Relaxed);
drop(c);
return Ok(Arc::new(self.decode(&loc, footage)?));
let (img, files) = reads::captured(|| self.decode(&loc, footage));
reads::note_all(&files);
return Ok(Arc::new(img?));
}
c = self.done.wait(c).unwrap_or_else(std::sync::PoisonError::into_inner);
}
@@ -816,11 +850,26 @@ impl Inner {
c.inflight.insert(loc.key.clone());
drop(c);
let _guard = Inflight { inner: self, key: loc.key.clone() };
let img = Arc::new(self.decode(&loc, footage)?);
lock(&self.cache).lru.insert(loc.key, img.clone(), self.budget());
let (img, files) = reads::captured(|| self.decode(&loc, footage));
reads::note_all(&files);
let img = Arc::new(img?);
lock(&self.cache).lru.insert(loc.key, img.clone(), Arc::new(files), self.budget());
Ok(img)
}
/// Sequential playback of a movie (`loc`, at `t`): read the frame after it ahead.
fn prefetch_next(self: &Arc<Self>, c: &mut CacheState, loc: &Loc, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) {
let prev = c.last.insert(loc.key.path.clone(), loc.key.frame);
if prev == Some(loc.key.frame - 1) && self.read_ahead.load(Ordering::Relaxed) {
let next_t = t + footage.frame_rate.frame_duration();
let mut next = Self::locate(footage, next_t, float);
next.key.size = loc.key.size;
if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) {
self.prefetch(footage, next_t, size, float);
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn prefetch(self: &Arc<Self>, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) {
let (me, f) = (self.clone(), footage.clone());
@@ -862,7 +911,9 @@ impl Inner {
if &**path == PLACEHOLDER {
return Ok(colour_bars(footage.width, footage.height));
}
let bytes = self.read(path)?;
let (bytes, file) = self.read_file(path);
reads::note(file.clone());
let bytes = bytes?;
if let Some(img) = crate::dpx::decode(path, &bytes)? {
return Ok(dynamic_to_image(&img, op));
}
@@ -880,7 +931,7 @@ impl Inner {
let shared = lock(&self.aux_footage).contains(&footage.path);
if shared
&& path.to_ascii_lowercase().ends_with(".exr")
&& let Some(img) = crate::exr_channels::cached_picture(path, bytes.clone(), op)
&& let Some(img) = crate::exr_channels::cached_picture(path, bytes.clone(), &file, op)
{
return Ok(img);
}
@@ -889,7 +940,7 @@ impl Inner {
match crate::probe::load_oriented(&bytes, crate::still_format(path, &bytes)) {
Ok(img) => Ok(dynamic_to_image(&img, op)),
// A multi-layer OpenEXR file without an unnamed RGB layer: its colour layer.
Err(e) => crate::exr_channels::layered_picture(path, bytes, op, shared)
Err(e) => crate::exr_channels::layered_picture(path, bytes, &file, op, shared)
.map_err(|why| MediaError::Decode(format!("{path}: {}", why.unwrap_or_else(|| e.to_string())))),
}
}
@@ -957,6 +1008,13 @@ impl FootageSource for MediaPool {
fn cancellable(&self, cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
filmcraft_media::cancel::with_cancel(cancel, f)
}
/// Every read of a file notes it (see [`crate::reads`]): a frame's image file or sequence
/// frame, a movie, a model and the files it names, an EXR's channels, a movie's audio. A
/// frame (or other entry) served from memory notes the files it was made of, as they were
/// when read, without reading them again. A missing sequence frame (colour bars) reads none.
fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) {
reads::recording(reads, f)
}
fn frame(&self, item: ItemId, footage: &Footage, t: Tick) -> Option<Arc<Image>> {
self.frame_sized(item, footage, t, None, FloatCurve::Srgb)
}
@@ -1207,13 +1265,13 @@ mod tests {
let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0) };
let budget = 3 * (4096 + 64);
for i in 0..10 {
c.insert(k(i), img(), budget);
c.insert(k(i), img(), Arc::default(), budget);
}
assert_eq!(c.map.len(), 3);
assert!(c.get(&k(9)).is_some() && c.get(&k(6)).is_none());
// touching 7 makes 8 the oldest
c.get(&k(7));
c.insert(k(10), img(), budget);
c.insert(k(10), img(), Arc::default(), budget);
assert!(c.get(&k(7)).is_some() && c.get(&k(8)).is_none());
assert_eq!(c.spare.len(), MAX_SPARE);
assert_eq!(c.take_spare(256).len(), 256);
+117
View File
@@ -0,0 +1,117 @@
//! Which files the [`MediaPool`](crate::MediaPool) reads for a render
//! ([`FootageSource::recording`](effectcraft_render::FootageSource::recording)).
//!
//! Every read of a file notes it, with the identity it had when read, in the innermost scope open
//! on this thread. The pool's caches (decoded frames, opened movies, GIFs, models, EXR channels)
//! keep what their entries were made of and note it again on each hit, so a frame served from
//! memory reports its file without touching the disk.
//!
//! The scopes are per thread: a read noted while a render's scope is open on the thread belongs
//! to that render. A thread waiting inside a decode can run another job meanwhile (rayon); that
//! job's reads go to its own scope when it opens one, and otherwise to the scope below it, which
//! then lists a file more than it read (never fewer).
use std::cell::RefCell;
use effectcraft_render::reads::{ReadFile, ReadFiles, ReadLog};
/// Scopes nested deeper than this are not opened (a thread that keeps taking jobs while it waits
/// cannot pile them up without bound): their reads go to the scope below, and they come back
/// incomplete.
const MAX_DEPTH: usize = 64;
thread_local! {
/// The open scopes on this thread, innermost last, each with the files noted in it.
static SCOPES: RefCell<Vec<ReadFiles>> = const { RefCell::new(Vec::new()) };
}
/// Note `file` in the innermost open scope on this thread (nothing when none is open).
pub(crate) fn note(file: ReadFile) {
let _ = SCOPES.try_with(|s| {
if let Ok(mut s) = s.try_borrow_mut()
&& let Some(top) = s.last_mut()
{
top.add(file);
}
});
}
/// Note every file of `files` in the innermost open scope on this thread.
pub(crate) fn note_all(files: &ReadFiles) {
if files.is_empty() && files.is_complete() {
return;
}
let _ = SCOPES.try_with(|s| {
if let Ok(mut s) = s.try_borrow_mut()
&& let Some(top) = s.last_mut()
{
top.extend(files);
}
});
}
/// Closes the scope it opened, also when what runs inside panics: its files go to `out`.
struct Scope<'a> {
out: Out<'a>,
opened: bool,
}
enum Out<'a> {
/// A render's log ([`recording`]).
Log(&'a ReadLog),
/// The caller's ([`captured`]).
Files(&'a mut ReadFiles),
}
impl Scope<'_> {
fn open(out: Out<'_>) -> Scope<'_> {
let opened = SCOPES
.try_with(|s| match s.try_borrow_mut() {
Ok(mut s) if s.len() < MAX_DEPTH => {
s.push(ReadFiles::default());
true
}
_ => false,
})
.unwrap_or(false);
Scope { out, opened }
}
}
impl Drop for Scope<'_> {
fn drop(&mut self) {
let files = if self.opened { SCOPES.try_with(|s| s.try_borrow_mut().ok().and_then(|mut s| s.pop())).ok().flatten() } else { None };
// A scope that could not open saw nothing: what it made can't name its files.
let files = files.unwrap_or_else(|| {
let mut f = ReadFiles::default();
f.mark_incomplete();
f
});
match &mut self.out {
Out::Log(log) => log.note_all(&files),
Out::Files(out) => out.extend(&files),
}
}
}
/// Run `f` with the files it reads on this thread noted in `log`
/// ([`FootageSource::recording`](effectcraft_render::FootageSource::recording)).
pub(crate) fn recording(log: &ReadLog, f: &mut dyn FnMut()) {
let _scope = Scope::open(Out::Log(log));
f();
}
/// Run `f` and return what it returns with the files it read on this thread (to keep with a
/// cache entry made of them). They are not noted in the enclosing scope: the caller notes them,
/// as it does on each later hit of the entry.
pub(crate) fn captured<T>(f: impl FnOnce() -> T) -> (T, ReadFiles) {
let mut files = ReadFiles::default();
let out = {
let _scope = Scope::open(Out::Files(&mut files));
f()
};
(out, files)
}
#[cfg(test)]
mod tests;
+184
View File
@@ -0,0 +1,184 @@
//! The files a render reads, as the media pool reports them (#3184): exactly the files the frame
//! was made of, also when its pixels come from memory.
use std::path::{Path, PathBuf};
use effectcraft_color::Label;
use effectcraft_project::build::{self, Ids};
use effectcraft_project::{Comp, Footage, FootageKind, ItemId, ItemKind, Layer, LayerSource, Project, Solid, Value};
use effectcraft_render::reads::{FileStamp, ReadFiles, ReadLog};
use effectcraft_render::{LayerCache, RenderOpts, Renderer};
use effectcraft_time::FrameRate;
use crate::MediaPool;
const RATE: FrameRate = FrameRate::FPS_30;
const SIZE: (u32, u32) = (16, 8);
/// A fresh folder for test `name` (on the build drive, not the system temp folder).
fn folder(name: &str) -> PathBuf {
let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join(format!("../../target/test-reads-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
/// A PNG at `path`, a flat grey of `level`.
fn png(path: &Path, level: u8) -> String {
image::RgbaImage::from_pixel(SIZE.0, SIZE.1, image::Rgba([level, level, level, 255])).save(path).unwrap();
path.to_string_lossy().to_string()
}
/// An image sequence of `n` PNG frames in `dir`.
fn sequence(dir: &Path, n: usize) -> Footage {
let files: Vec<String> = (0..n).map(|i| png(&dir.join(format!("seq_{i:04}.png")), 20 + i as u8 * 10)).collect();
let mut f = Footage { kind: FootageKind::Sequence, sequence: files, frame_rate: RATE, ..crate::probe(dir.join("seq_0000.png")).unwrap() };
f.sync_sequence_duration();
f
}
/// A still footage item of `path`.
fn still(path: &Path, level: u8) -> Footage {
png(path, level);
crate::probe(path).unwrap()
}
fn comp() -> Comp {
Comp::new(SIZE.0, SIZE.1, RATE, RATE.tick_of(10))
}
fn footage_layer(p: &mut Project, comp: &Comp, name: &str, f: Footage) -> Layer {
let item = p.add_item(name, Label::Aqua, None, ItemKind::Footage(f));
build::layer(p, comp, name, LayerSource::Footage { item }, SIZE, Some(comp.duration))
}
fn add_comp(p: &mut Project, name: &str, mut comp: Comp, layers: Vec<Layer>) -> ItemId {
comp.layers = layers.into();
p.add_item(name, Label::Sandstone, None, ItemKind::Comp(comp.into()))
}
/// Render frame `frame` of `comp` with `pool` and `cache`: the picture's top-left pixel and the
/// files the render reports.
fn render(p: &Project, pool: &MediaPool, cache: &LayerCache, comp: ItemId, frame: i64) -> ([f32; 4], ReadFiles) {
let log = ReadLog::new();
let mut r = Renderer::new(p, pool, RenderOpts::default());
r.cache = Some(cache);
r.reads = Some(&log);
let img = r.comp_frame(comp, RATE.tick_of(frame));
(img.get(0, 0), log.into_files())
}
fn paths(files: &ReadFiles) -> Vec<String> {
files.files().iter().map(|f| f.path.to_string()).collect()
}
/// Every listed file has the identity it has on disk now.
fn assert_stamps_match_disk(files: &ReadFiles) {
for f in files.files() {
assert_eq!(Some(f.stamp), FileStamp::native(&f.path), "{}", f.path);
}
}
/// (a) One frame of a comp with an image-sequence layer reads that frame's file and nothing else
/// of the sequence; a large sequence in the project that no layer uses adds nothing, not even a
/// look at its files.
#[test]
fn a_sequence_frame_reports_only_its_own_file() {
let dir = folder("sequence");
let unused_dir = dir.join("unused");
let mut p = Project::default();
let c = comp();
let seq = sequence(&dir, 5);
let (frame_2, frame_4) = (seq.sequence[2].clone(), seq.sequence[4].clone());
let layer = footage_layer(&mut p, &c, "Seq", seq);
// 5000 frames nobody shows (they don't even exist).
let unused = Footage {
kind: FootageKind::Sequence,
sequence: (0..5000).map(|i| unused_dir.join(format!("big_{i:05}.exr")).to_string_lossy().to_string()).collect(),
frame_rate: RATE,
width: SIZE.0,
height: SIZE.1,
has_video: true,
..Default::default()
};
p.add_item("Unused", Label::Aqua, None, ItemKind::Footage(unused));
let cid = add_comp(&mut p, "C", c, vec![layer]);
let (pool, cache) = (MediaPool::new(), LayerCache::default());
let opened = crate::opens_under(&dir);
let (px, files) = render(&p, &pool, &cache, cid, 2);
assert!((px[0] - 40.0 / 255.0).abs() < 0.01, "frame 2 shows: {px:?}");
assert_eq!(paths(&files), [frame_2]);
assert!(files.is_complete());
assert_stamps_match_disk(&files);
assert_eq!(crate::opens_under(&dir) - opened, 1, "one file opened: the frame's");
assert_eq!(crate::opens_under(&unused_dir), 0, "the unused sequence was not looked at");
// Another frame reads its own file.
let (_, files) = render(&p, &pool, &cache, cid, 4);
assert_eq!(paths(&files), [frame_4]);
let _ = std::fs::remove_dir_all(&dir);
}
/// (b) A precomp reports the files of what it draws, and an effect that reads another layer
/// reports that layer's files (the layer itself is hidden: its pixels come only through the
/// effect).
#[test]
fn precomps_and_layers_read_by_effects_report_their_files() {
let dir = folder("nested");
let mut p = Project::default();
let c = comp();
let seq = sequence(&dir, 4);
let nested_frame = seq.sequence[3].clone();
let inner_layer = footage_layer(&mut p, &c, "Seq", seq);
let inner = add_comp(&mut p, "Inner", c.clone(), vec![inner_layer]);
let pre = build::layer(&mut p, &c, "Inner", LayerSource::Comp { item: inner }, SIZE, Some(c.duration));
let matte_path = dir.join("matte.png");
let mut matte = footage_layer(&mut p, &c, "Matte", still(&matte_path, 255));
matte.switches.video = false;
let sid = p.add_item("S", Label::Red, None, ItemKind::Solid(Solid { color: [1.0, 0.0, 0.0], width: SIZE.0, height: SIZE.1, pixel_aspect: 1.0 }));
let mut solid = build::layer(&mut p, &c, "S", LayerSource::Solid { item: sid }, SIZE, Some(c.duration));
let spec = effectcraft_effects::find("ec.channel.setmatte").unwrap();
let mut next = p.next_id;
let mut fx = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [SIZE.0 as f64, SIZE.1 as f64]);
p.next_id = next;
fx.prop_mut("takeMatteFromLayer").unwrap().value = Value::Layer(Some(matte.id.0));
solid.props.sub_mut("effects").unwrap().children.push(fx.into());
let cid = add_comp(&mut p, "Outer", c, vec![solid, matte, pre]);
let (pool, cache) = (MediaPool::new(), LayerCache::default());
let (_, files) = render(&p, &pool, &cache, cid, 3);
let mut want = vec![matte_path.to_string_lossy().to_string(), nested_frame];
want.sort();
assert_eq!(paths(&files), want);
assert!(files.is_complete());
assert_stamps_match_disk(&files);
let _ = std::fs::remove_dir_all(&dir);
}
/// (c) A frame rendered again comes from memory (the sequence frame from the media pool, the
/// still layer from the layer cache) and reports the same files, with the identities they had
/// when read, without opening anything: here the files are even gone by then.
#[test]
fn a_frame_served_from_memory_reports_its_files_without_touching_them() {
let dir = folder("memory");
let mut p = Project::default();
let c = comp();
// The sequence at half opacity over the still: both show.
let mut seq = footage_layer(&mut p, &c, "Seq", sequence(&dir, 3));
seq.props.prop_mut("transform/opacity").unwrap().value = Value::Scalar(50.0);
let plate = footage_layer(&mut p, &c, "Plate", still(&dir.join("plate.png"), 128));
let cid = add_comp(&mut p, "C", c, vec![seq, plate]);
let (pool, cache) = (MediaPool::new(), LayerCache::default());
let (px, first) = render(&p, &pool, &cache, cid, 1);
assert_eq!(first.len(), 2, "{:?}", paths(&first));
assert_stamps_match_disk(&first);
std::fs::remove_dir_all(&dir).unwrap();
let opened = crate::opens_under(&dir);
let hits = cache.stats().hits;
let (again_px, again) = render(&p, &pool, &cache, cid, 1);
assert_eq!(again_px, px, "the same picture");
assert_eq!(again, first, "the same files, as they were when read");
assert!(cache.stats().hits > hits, "the still layer came from the layer cache");
// The media pool alone (a new layer cache) reports them too.
let (_, from_pool) = render(&p, &pool, &LayerCache::default(), cid, 1);
assert_eq!(from_pool, first);
assert_eq!(crate::opens_under(&dir), opened, "nothing was opened on disk");
}
+11 -4
View File
@@ -9,6 +9,7 @@
use std::path::Path;
use effectcraft_render::reads::FileStamp;
use filmcraft_media::SharedSource;
use crate::Result;
@@ -19,17 +20,21 @@ use crate::Result;
pub(crate) struct FileReader {
file: std::fs::File,
len: u64,
/// The file's identity when it was opened.
stamp: FileStamp,
}
#[cfg(any(unix, windows))]
impl FileReader {
pub(crate) fn open(path: &Path) -> std::io::Result<Self> {
#[cfg(test)]
crate::count_open(path);
let file = std::fs::File::open(path)?;
let m = file.metadata()?;
if !m.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display())));
}
Ok(Self { file, len: m.len() })
Ok(Self { file, len: m.len(), stamp: FileStamp::of_metadata(&m) })
}
}
@@ -72,16 +77,18 @@ fn name_of(path: &Path) -> String {
/// Open a movie/audio file for the media pool, reading only what its container needs; files the
/// reader openers don't handle are read whole and opened from memory, exactly as before. `None`
/// when the file can't be opened in place (the caller reads it whole, which reports the error).
/// With the file's identity as opened.
#[cfg(any(unix, windows))]
pub(crate) fn open_source(path: &Path) -> Option<Result<SharedSource>> {
pub(crate) fn open_source(path: &Path) -> Option<(Result<SharedSource>, FileStamp)> {
let reader = FileReader::open(path).ok()?;
let stamp = reader.stamp;
let reader: filmcraft_media::SharedReader = std::sync::Arc::new(reader);
let opened = filmcraft_media::reader::open_reader(&name_of(path), reader, &filmcraft_codecs::reader_openers(), &filmcraft_codecs::openers());
Some(opened.map_err(Into::into))
Some((opened.map_err(Into::into), stamp))
}
#[cfg(not(any(unix, windows)))]
pub(crate) fn open_source(_path: &Path) -> Option<Result<SharedSource>> {
pub(crate) fn open_source(_path: &Path) -> Option<(Result<SharedSource>, FileStamp)> {
None
}
+8 -12
View File
@@ -224,7 +224,7 @@ impl<'a> Renderer<'a> {
LayerSource::Footage { item } => {
let item = ctx.media_item(layer, *item);
let ItemKind::Footage(f) = &self.project.item(item)?.kind else { return None };
let a = self.footage.aux(item, f, ctx.source_time(layer))?;
let a = self.read(|| self.footage.aux(item, f, ctx.source_time(layer)))?;
// Aux pixels per layer pixel (the layer is the footage at its nominal size). The
// planes are only copied when that changes the scale.
let scale = a.width as f64 / f.width as f64;
@@ -349,20 +349,16 @@ impl<'a> Renderer<'a> {
pub fn adjustment_footprint(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
// Cached, so an accelerator sees the same buffer (and uploads it once) every frame.
let key = self.cache.and_then(|_| cache::footprint_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)));
if let (Some(c), Some(k)) = (self.cache, key)
&& let Some(b) = c.get(k)
{
if let Some((b, _)) = self.cached(key) {
return Some(b);
}
let mut foot = self.source(ctx, layer)?;
let _ = masks::apply(ctx, layer, &mut foot, self.mask_blur(ctx, layer));
let foot = Arc::new(foot);
let (foot, files) = self.recorded(key, |r| {
let mut foot = r.source(ctx, layer)?;
let _ = masks::apply(ctx, layer, &mut foot, r.mask_blur(ctx, layer));
Some(foot)
});
// The buffer the cache keeps (another worker's, if it kept one first).
let foot = match (self.cache_to_keep(), key) {
(Some(c), Some(k)) => c.insert(k, foot),
_ => foot,
};
Some(foot)
Some(self.keep(key, Arc::new(foot?), &files))
}
/// Draw one 2D layer into `canvas` on the CPU (any kind: adjustment, wireframe, styled…).
+22 -12
View File
@@ -25,6 +25,7 @@ use effectcraft_effects::Buf;
use effectcraft_project::{FootageKind, ItemKind, Layer, LayerSource, Node, PropGroup};
use crate::eval::EvalCtx;
use crate::reads::ReadFiles;
mod dependencies;
@@ -43,6 +44,8 @@ pub struct LayerCache {
struct Entry {
buf: Arc<Buf>,
/// The source files the buffer was made of (see [`crate::reads`]).
files: Arc<ReadFiles>,
bytes: usize,
last_use: u64,
}
@@ -147,16 +150,19 @@ impl LayerCache {
Some((st.clone(), salted(*salt)))
}
pub fn get(&self, key: u64) -> Option<Arc<Buf>> {
/// The buffer kept under `key` and the source files it was made of (see [`crate::reads`]).
/// A buffer from the disk cache or another store doesn't know its files: its list is
/// incomplete.
pub fn get(&self, key: u64) -> Option<(Arc<Buf>, Arc<ReadFiles>)> {
let mut g = self.inner.lock().ok()?;
g.clock += 1;
let now = g.clock;
match g.map.get_mut(&key) {
Some(e) => {
e.last_use = now;
let b = e.buf.clone();
let found = (e.buf.clone(), e.files.clone());
g.hits += 1;
Some(b)
Some(found)
}
None => {
g.misses += 1;
@@ -168,7 +174,10 @@ impl LayerCache {
// Disk cache: a hit comes back into memory.
let (dc, dk) = self.disk_key(key)?;
let buf = dc.get_layer(dk)?;
Some(self.insert_mem(key, buf))
let mut files = ReadFiles::default();
files.mark_incomplete();
let files = Arc::new(files);
Some((self.insert_mem(key, buf, files.clone()), files))
}
}
}
@@ -178,8 +187,9 @@ impl LayerCache {
/// together each rendered every layer; sharing the first buffer, the GPU compositor uploads
/// and packs it once instead of once per worker, which ran it out of memory). Renders don't
/// wait for each other: on the frame workers' shared thread pool a wait can end up under
/// the very work it waits for.
pub fn insert(&self, key: u64, buf: Arc<Buf>) -> Arc<Buf> {
/// the very work it waits for. `files`: the source files `buf` was made of (see
/// [`crate::reads`]).
pub fn insert(&self, key: u64, buf: Arc<Buf>, files: Arc<ReadFiles>) -> Arc<Buf> {
if self.gated() {
return buf;
}
@@ -193,12 +203,12 @@ impl LayerCache {
dc.put_layer(dk, &buf);
}
}
self.insert_mem(key, buf)
self.insert_mem(key, buf, files)
}
/// See [`LayerCache::insert`]: the buffer kept under `key` (`buf`, unless one was kept first
/// or `buf` is too large to keep).
fn insert_mem(&self, key: u64, buf: Arc<Buf>) -> Arc<Buf> {
fn insert_mem(&self, key: u64, buf: Arc<Buf>, files: Arc<ReadFiles>) -> Arc<Buf> {
let bytes = buf.img.data.len() * std::mem::size_of::<effectcraft_raster::Px>();
let Ok(mut g) = self.inner.lock() else { return buf };
if bytes > g.budget / 4 {
@@ -210,7 +220,7 @@ impl LayerCache {
e.last_use = now;
return e.buf.clone();
}
g.map.insert(key, Entry { buf: buf.clone(), bytes, last_use: now });
g.map.insert(key, Entry { buf: buf.clone(), files, bytes, last_use: now });
g.bytes += bytes;
if g.bytes > g.budget {
// Evict least-recently used entries down to 3/4 of the budget.
@@ -786,8 +796,8 @@ mod shared_insert_tests {
let cache = LayerCache::default();
let buf = || Arc::new(Buf { img: Image::new(4, 4), offset: [0.0; 2], scale: 1.0 });
let (a, b) = (buf(), buf());
assert!(Arc::ptr_eq(&cache.insert(3, a.clone()), &a));
assert!(Arc::ptr_eq(&cache.insert(3, b), &a), "the later render gets the first buffer");
assert!(Arc::ptr_eq(&cache.get(3).unwrap(), &a));
assert!(Arc::ptr_eq(&cache.insert(3, a.clone(), Default::default()), &a));
assert!(Arc::ptr_eq(&cache.insert(3, b, Default::default()), &a), "the later render gets the first buffer");
assert!(Arc::ptr_eq(&cache.get(3).unwrap().0, &a));
}
}
+7 -8
View File
@@ -1055,18 +1055,17 @@ fn hash_debug(h: &mut Hash128, v: &impl std::fmt::Debug) {
/// Size and content hash of a path that has no native metadata (the browser file table).
pub type VirtualFileId<'a> = &'a (dyn Fn(&str) -> Option<(u64, u64)> + Send + Sync);
/// A file's size and modification time (so edited footage gets new keys). When the path has no
/// native metadata, `virtual_id` supplies a durable size and content hash instead.
/// A file's size and modification time (so edited footage gets new keys): the identity a render
/// records for the files it reads ([`crate::reads::FileStamp`]). When the path has no native
/// metadata, `virtual_id` supplies a durable size and content hash instead.
fn file_stamp(h: &mut Hash128, path: &str, virtual_id: Option<VirtualFileId<'_>>) {
if path.is_empty() {
return;
}
if let Ok(md) = std::fs::metadata(path) {
h.write_u64(md.len());
if let Ok(t) = md.modified()
&& let Ok(d) = t.duration_since(std::time::UNIX_EPOCH)
{
h.write_u64(d.as_nanos() as u64);
if let Some(crate::reads::FileStamp::Native { len, modified }) = crate::reads::FileStamp::native(path) {
h.write_u64(len);
if let Some(t) = modified {
h.write_u64(t);
}
return;
}
+17
View File
@@ -1,4 +1,5 @@
use super::{Arc, AtomicBool, Footage, Image, ItemId, ItemKind, Ordering, Tick};
use crate::reads::ReadLog;
/// How a [`FootageSource`] hands over float footage (OpenEXR, float TIFF), whose values are
/// linear light: encoded with a curve, as the compositor takes footage, or as stored. Other
@@ -92,6 +93,15 @@ pub trait FootageSource: Send + Sync {
fn called_off(&self) -> bool {
false
}
/// Run `f`, which reads from this source on this thread, noting in `reads` every source file
/// those reads use, with its identity as read (see [`crate::reads`]): the file a frame was
/// decoded from, also when it comes from the source's own memory cache (without reading the
/// file again). Sources that can't tell which files they read keep the default, which marks
/// `reads` incomplete.
fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) {
reads.mark_incomplete();
f()
}
/// Why `footage` shows no picture although its file is there: a read of it failed because
/// no decoder handles its codec, profile, chroma format or bit depth, or the stream is damaged
/// (e.g. "H.264 (High 4:2:2) isn't supported by the built-in decoder: ..."). `None` while it
@@ -194,6 +204,9 @@ impl FootageSource for CancellableFootage<'_> {
fn called_off(&self) -> bool {
self.cancel.load(Ordering::Relaxed) || self.inner.called_off()
}
fn recording(&self, reads: &ReadLog, f: &mut dyn FnMut()) {
self.inner.recording(reads, f)
}
fn problem(&self, footage: &Footage) -> Option<String> {
self.inner.problem(footage)
}
@@ -214,4 +227,8 @@ impl FootageSource for NoFootage {
fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
None
}
/// Reads no files.
fn recording(&self, _: &ReadLog, f: &mut dyn FnMut()) {
f()
}
}
+1 -1
View File
@@ -104,7 +104,7 @@ impl EffectHost for FxHost<'_, '_, '_> {
return None;
}
let st = other.layer_time(Tick::from_seconds_f64(start));
self.r.footage.audio(item, f, st, frames, rate)
self.r.read(|| self.r.footage.audio(item, f, st, frames, rate))
}
fn self_at(&self, layer_time: f64, effects: usize) -> Option<Buf> {
+18 -20
View File
@@ -37,18 +37,17 @@ impl Shutter {
impl<'a> Renderer<'a> {
/// The layer with its styles: exterior passes and body (cached per pass).
pub(crate) fn styled(&self, ctx: &EvalCtx, layer: &Layer, mut timing: Option<&mut LayerTiming>) -> Option<StyledLayer> {
let (content, ckey) = self.content_buf_timed(ctx, layer, timing.as_deref_mut())?;
let (content, ckey, files) = self.content_buf_timed(ctx, layer, timing.as_deref_mut())?;
if !styles::active(ctx, layer) {
return Some(StyledLayer { passes: vec![], body: content.clone(), content, key: ckey, ckey, plain: true });
return Some(StyledLayer { passes: vec![], body: content.clone(), content, key: ckey, ckey, plain: true, files });
}
let modes = styles::pass_modes(ctx, layer);
let key = ckey.map(|k| cache::styles_key(ctx, layer, k));
if let (Some(c), Some(k)) = (self.cache, key)
&& let Some(body) = c.get(k)
{
let passes: Option<Vec<_>> = modes.iter().enumerate().map(|(i, m)| c.get(cache::derive(k, i as u64 + 1)).map(|b| (b, *m))).collect();
if let Some((body, _)) = self.cached(key) {
let passes: Option<Vec<_>> =
modes.iter().enumerate().map(|(i, m)| self.cached(key.map(|k| cache::derive(k, i as u64 + 1))).map(|(b, _)| (b, *m))).collect();
if let Some(passes) = passes {
return Some(StyledLayer { passes, body, content, key, ckey, plain: false });
return Some(StyledLayer { passes, body, content, key, ckey, plain: false, files });
}
}
let t0 = web_time::Instant::now();
@@ -59,16 +58,12 @@ impl<'a> Renderer<'a> {
t.cached = false;
t.effects.push(("layerStyles".into(), t0.elapsed().as_secs_f64() * 1e3));
}
let mut body = Arc::new(st.body);
let mut passes: Vec<_> = st.passes.into_iter().map(|(b, m)| (Arc::new(b), m)).collect();
// The buffers the cache keeps (another worker's, if it kept them first).
if let (Some(c), Some(k)) = (self.cache_to_keep(), key) {
body = c.insert(k, body);
for (i, (b, _)) in passes.iter_mut().enumerate() {
*b = c.insert(cache::derive(k, i as u64 + 1), b.clone());
}
}
Some(StyledLayer { passes, body, content, key, ckey, plain: false })
// The buffers the cache keeps (another worker's, if it kept them first), made of the
// content's files.
let body = self.keep(key, Arc::new(st.body), &files);
let passes: Vec<_> =
st.passes.into_iter().enumerate().map(|(i, (b, m))| (self.keep(key.map(|k| cache::derive(k, i as u64 + 1)), Arc::new(b), &files), m)).collect();
Some(StyledLayer { passes, body, content, key, ckey, plain: false, files })
}
/// The styled layer's buffers in the blending space.
@@ -76,9 +71,10 @@ impl<'a> Renderer<'a> {
if self.pipe.to_blend().is_none() {
return st;
}
let body = self.to_blend(st.body, st.key);
let content = if st.plain { body.clone() } else { self.to_blend(st.content, st.ckey) };
let passes = st.passes.into_iter().enumerate().map(|(i, (b, m))| (self.to_blend(b, st.key.map(|k| cache::derive(k, i as u64 + 1))), m)).collect();
let body = self.to_blend(st.body, st.key, &st.files);
let content = if st.plain { body.clone() } else { self.to_blend(st.content, st.ckey, &st.files) };
let passes =
st.passes.into_iter().enumerate().map(|(i, (b, m))| (self.to_blend(b, st.key.map(|k| cache::derive(k, i as u64 + 1)), &st.files), m)).collect();
StyledLayer { passes, body, content, ..st }
}
@@ -582,6 +578,8 @@ pub struct StyledLayer {
ckey: Option<u64>,
/// No layer styles: composite `body` the usual way.
pub plain: bool,
/// The source files the layer's buffers were made of (see [`crate::reads`]).
pub(crate) files: Arc<crate::reads::ReadFiles>,
}
/// Where a layer buffer lands (see [`Renderer::placement`]).
+5
View File
@@ -17,6 +17,7 @@ use color::Region;
pub mod eval;
pub mod masks;
pub mod passes;
pub mod reads;
pub mod shapes;
pub mod styles;
pub mod text;
@@ -69,6 +70,10 @@ pub struct Renderer<'a> {
pub opts: RenderOpts,
/// Processed-layer cache shared across frames (see [`cache`]). `None` renders everything.
pub cache: Option<&'a LayerCache>,
/// Where the source files this render reads are noted (see [`reads`]): the frame's own,
/// from the footage source and from the buffers the layer cache serves. `None` records
/// nothing for the frame (buffers kept in the layer cache still record theirs).
pub reads: Option<&'a reads::ReadLog>,
/// Optional per-layer timing sink (`frame --bench` / perf readouts).
pub profile: Option<&'a std::sync::Mutex<Vec<LayerTiming>>>,
/// GPU backend used when [`RenderOpts::backend`] asks for it.
+97 -67
View File
@@ -3,6 +3,7 @@ use super::footage::{FootageSource, is_vector_footage};
use super::frames::{blend_frames, cineon_frame, content_bounds, field_frame, frame_blend_mode, frame_position, frame_size, interpret_pixels, roi_inside};
use super::fx_host::{FxHost, essential_overrides};
use super::opts::{Collapse3d, LayerTiming, MAX_THROUGH, RenderOpts, Through};
use super::reads::{ReadFiles, ReadLog};
use super::{
Arc, AutoKey, AutoPick, BlendMode, Buf, Comp, EffectCtx, EffectEnv, EvalCtx, Footage, FootageKind, FrameBlend, FrameRate, GroupKind, Image, ItemId,
ItemKind, Layer, LayerCache, LayerSource, Params, Project, Quality, Region, Renderer, Tick, cache, color, masks, shapes, styles, text, three_d, vec2,
@@ -108,6 +109,7 @@ impl<'a> Renderer<'a> {
expr: None,
opts,
cache: None,
reads: None,
profile: None,
accel: None,
depth: 0,
@@ -129,6 +131,52 @@ impl<'a> Renderer<'a> {
self.cache.filter(|_| !self.footage.called_off())
}
/// `read`, a read of this render's footage source, with the files it uses noted in
/// [`Renderer::reads`] (see [`FootageSource::recording`]).
pub(crate) fn read<R>(&self, read: impl FnOnce() -> Option<R>) -> Option<R> {
let Some(log) = self.reads else { return read() };
let (mut read, mut out) = (Some(read), None);
self.footage.recording(log, &mut || out = read.take().and_then(|f| f()));
out
}
/// The buffer the layer cache keeps under `key`, with the files it was made of noted in
/// [`Renderer::reads`].
pub(crate) fn cached(&self, key: Option<u64>) -> Option<(Arc<Buf>, Arc<ReadFiles>)> {
let (buf, files) = self.cache?.get(key?)?;
if let Some(log) = self.reads {
log.note_all(&files);
}
Some((buf, files))
}
/// Keep `buf`, made of `files`, under `key` (see [`Renderer::recorded`]): the buffer the cache
/// keeps (another worker's, if it kept one first), or `buf` when nothing is kept.
pub(crate) fn keep(&self, key: Option<u64>, buf: Arc<Buf>, files: &Arc<ReadFiles>) -> Arc<Buf> {
match (self.cache_to_keep(), key) {
(Some(c), Some(k)) => c.insert(k, buf, files.clone()),
_ => buf,
}
}
/// Run `make`, which makes a buffer to keep under `key` in the layer cache, on this renderer
/// with its reads noted apart: what `make` returns and the files it read (from the footage
/// source, or the files of cached buffers it used), which go to [`Renderer::reads`] too. A
/// cached buffer then reports the files it was made of whichever render uses it. Without a
/// cache or a key, `make` runs on this renderer as it is (no files: nothing is kept).
pub(crate) fn recorded<T>(&self, key: Option<u64>, make: impl FnOnce(&Renderer<'_>) -> T) -> (T, Arc<ReadFiles>) {
if key.is_none() || self.cache.is_none() {
return (make(self), Arc::default());
}
let log = ReadLog::new();
let out = make(&Renderer { reads: Some(&log), ..*self });
let files = log.into_files();
if let Some(own) = self.reads {
own.note_all(&files);
}
(out, Arc::new(files))
}
/// A layer's Quality switch, limited by the precomp layers above it when
/// [`RenderOpts::nested_switches`] is on.
pub fn quality(&self, layer: &Layer) -> Quality {
@@ -760,7 +808,7 @@ impl<'a> Renderer<'a> {
// Continuously rasterised vector footage: drawn at the on-screen scale.
if layer.switches.collapse && is_vector_footage(f) {
let k = self.raster_scale(ctx, layer);
if let Some(img) = self.footage.vector_frame(item, f, k) {
if let Some(img) = self.read(|| self.footage.vector_frame(item, f, k)) {
let mut buf = Buf { img: (*img).clone(), offset: [0.0; 2], scale: k };
if let Some(c) = self.pipe.media_in(f) {
color::convert(&mut buf.img, &c);
@@ -831,7 +879,7 @@ impl<'a> Renderer<'a> {
if f.fields != effectcraft_project::FieldOrder::Off && matches!(f.kind, FootageKind::Video | FootageKind::Sequence) {
let field_rate = FrameRate::new(f.frame_rate.num * 2, f.frame_rate.den);
let i = field_rate.frame_at(t).max(0);
let img = cineon_frame(self.footage.frame_with(item, f, f.frame_rate.tick_of(i / 2), None, self.pipe.float_curve(f))?, f);
let img = cineon_frame(self.read(|| self.footage.frame_with(item, f, f.frame_rate.tick_of(i / 2), None, self.pipe.float_curve(f)))?, f);
let dominant_upper = f.fields == effectcraft_project::FieldOrder::UpperFirst;
// Upper field = even lines (0, 2, …).
let parity = if (i % 2 == 0) == dominant_upper { 0 } else { 1 };
@@ -865,24 +913,22 @@ impl<'a> Renderer<'a> {
}
h.finish()
});
if let (Some(c), Some(k)) = (self.cache, key)
&& let Some(b) = c.get(k)
{
if let Some((b, _)) = self.cached(key) {
return Some(Arc::new(b.img.clone()));
}
// Log scans are converted before they blend: the conversion is not linear.
let a = self.footage.frame_with(item, f, f.frame_rate.tick_of(i), None, float).map(|a| cineon_frame(a, f));
let b = self.footage.frame_with(item, f, f.frame_rate.tick_of(i + 1), None, float).map(|b| cineon_frame(b, f));
let ((a, b), files) = self.recorded(key, |r| {
let frame = |i| r.read(|| r.footage.frame_with(item, f, f.frame_rate.tick_of(i), None, float)).map(|a| cineon_frame(a, f));
(frame(i), frame(i + 1))
});
if let (Some(a), Some(b)) = (a, b) {
let img = blend_frames(mode, &a, &b, w as f32);
if let (Some(c), Some(k)) = (self.cache_to_keep(), key) {
c.insert(k, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 }));
}
self.keep(key, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 }), &files);
return Some(Arc::new(img));
}
}
}
self.footage.frame_with(item, f, t, size, float).map(|img| cineon_frame(img, f))
self.read(|| self.footage.frame_with(item, f, t, size, float)).map(|img| cineon_frame(img, f))
}
/// Source → masks, clamped/quantised to the project depth.
@@ -902,52 +948,40 @@ impl<'a> Renderer<'a> {
/// Fully processed layer buffer (source → masks → effects → layer styles, flattened).
/// Served from the layer cache when the layer's content key matches.
pub fn layer_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
self.layer_buf_keyed(ctx, layer).map(|(b, _)| b)
self.layer_buf_keyed(ctx, layer).map(|(b, _, _)| b)
}
/// [`Self::layer_buf`] in the blending space (linear with Blend Colors Using 1.0 Gamma).
pub fn blend_layer_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
let (b, k) = self.layer_buf_keyed(ctx, layer)?;
Some(self.to_blend(b, k))
let (b, k, files) = self.layer_buf_keyed(ctx, layer)?;
Some(self.to_blend(b, k, &files))
}
/// A buffer converted to the blending space (cached under a key derived from `key`).
pub(crate) fn to_blend(&self, b: Arc<Buf>, key: Option<u64>) -> Arc<Buf> {
/// A buffer converted to the blending space (cached under a key derived from `key`, with
/// `files`, the source files `b` was made of).
pub(crate) fn to_blend(&self, b: Arc<Buf>, key: Option<u64>, files: &Arc<ReadFiles>) -> Arc<Buf> {
let Some(c) = self.pipe.to_blend() else { return b };
let key = key.map(|k| cache::derive(k, 0x1ea7));
if let (Some(cache), Some(k)) = (self.cache, key)
&& let Some(x) = cache.get(k)
{
if let Some((x, _)) = self.cached(key) {
return x;
}
let mut nb = (*b).clone();
color::convert(&mut nb.img, &c);
let nb = Arc::new(nb);
// The buffer the cache keeps (another worker's, if it kept one first).
match (self.cache_to_keep(), key) {
(Some(cache), Some(k)) => cache.insert(k, nb),
_ => nb,
}
self.keep(key, Arc::new(nb), files)
}
fn layer_buf_keyed(&self, ctx: &EvalCtx, layer: &Layer) -> Option<(Arc<Buf>, Option<u64>)> {
/// The flattened layer buffer, its cache key and the source files it was made of.
fn layer_buf_keyed(&self, ctx: &EvalCtx, layer: &Layer) -> Option<(Arc<Buf>, Option<u64>, Arc<ReadFiles>)> {
let st = self.styled(ctx, layer, None)?;
if st.passes.is_empty() {
return Some((st.body, st.key));
return Some((st.body, st.key, st.files));
}
let key = st.key.map(|k| cache::derive(k, 0xf1a7));
if let (Some(c), Some(k)) = (self.cache, key)
&& let Some(b) = c.get(k)
{
return Some((b, key));
if let Some((b, files)) = self.cached(key) {
return Some((b, key, files));
}
let flat = Arc::new(styles::Styled { passes: st.passes.iter().map(|(b, m)| ((**b).clone(), *m)).collect(), body: (*st.body).clone() }.flatten());
// The buffer the cache keeps (another worker's, if it kept one first).
let flat = match (self.cache_to_keep(), key) {
(Some(c), Some(k)) => c.insert(k, flat),
_ => flat,
};
Some((flat, key))
Some((self.keep(key, flat, &st.files), key, st.files))
}
/// A layer's source pixels at the context time, before masks, effects and the transform
@@ -983,28 +1017,29 @@ impl<'a> Renderer<'a> {
/// Layer pixels after source → masks → effects (no layer styles).
pub fn content_buf(&self, ctx: &EvalCtx, layer: &Layer) -> Option<Arc<Buf>> {
self.content_buf_timed(ctx, layer, None).map(|(b, _)| b)
self.content_buf_timed(ctx, layer, None).map(|(b, _, _)| b)
}
pub(crate) fn content_buf_timed(&self, ctx: &EvalCtx, layer: &Layer, mut timing: Option<&mut LayerTiming>) -> Option<(Arc<Buf>, Option<u64>)> {
/// [`Self::content_buf`], its cache key and the source files it was made of.
pub(crate) fn content_buf_timed(
&self,
ctx: &EvalCtx,
layer: &Layer,
mut timing: Option<&mut LayerTiming>,
) -> Option<(Arc<Buf>, Option<u64>, Arc<ReadFiles>)> {
let key = self.cache.and_then(|_| cache::layer_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)));
let key = self.content_key(ctx, layer, key);
if let (Some(c), Some(k)) = (self.cache, key)
&& let Some(b) = c.get(k)
{
if let Some((b, files)) = self.cached(key) {
if let Some(t) = timing.as_deref_mut() {
t.cached = true;
}
return Some((b, key));
return Some((b, key, files));
}
let buf = self.masked_source(ctx, layer)?;
let buf = Arc::new(self.apply_effects_timed(ctx, layer, buf, false, usize::MAX, timing.map(|t| &mut t.effects)));
// The buffer the cache keeps (another worker's, if it kept one first).
let buf = match (self.cache_to_keep(), key) {
(Some(c), Some(k)) => c.insert(k, buf),
_ => buf,
};
Some((buf, key))
let (buf, files) = self.recorded(key, |r| {
let buf = r.masked_source(ctx, layer)?;
Some(r.apply_effects_timed(ctx, layer, buf, false, usize::MAX, timing.map(|t| &mut t.effects)))
});
Some((self.keep(key, Arc::new(buf?), &files), key, files))
}
/// A layer's input at the context time: source → masks → its first `effects` effects
@@ -1014,23 +1049,18 @@ impl<'a> Renderer<'a> {
pub fn layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option<Arc<Buf>> {
let key = self.cache.and_then(|_| cache::input_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer), effects));
let key = self.content_key(ctx, layer, key);
if let (Some(c), Some(k)) = (self.cache, key)
&& let Some(b) = c.get(k)
{
if let Some((b, _)) = self.cached(key) {
return Some(b);
}
let mut buf = self.masked_source(ctx, layer)?;
let origin = ctx.effect_bounds(layer).1;
buf.rebase(origin);
if effects > 0 {
buf = self.effect_space_stack(ctx, layer, buf, EffectStack { adjustment: false, limit: effects, origin }, None);
}
let buf = Arc::new(buf);
// The buffer the cache keeps (another worker's, if it kept one first).
let buf = match (self.cache_to_keep(), key) {
(Some(c), Some(k)) => c.insert(k, buf),
_ => buf,
};
Some(buf)
let (buf, files) = self.recorded(key, |r| {
let mut buf = r.masked_source(ctx, layer)?;
let origin = ctx.effect_bounds(layer).1;
buf.rebase(origin);
if effects > 0 {
buf = r.effect_space_stack(ctx, layer, buf, EffectStack { adjustment: false, limit: effects, origin }, None);
}
Some(buf)
});
Some(self.keep(key, Arc::new(buf?), &files))
}
}
+230
View File
@@ -0,0 +1,230 @@
//! The source files a render read: which footage files its pixels came from, each with its
//! identity (size and modification time) as it was when it was read.
//!
//! A [`FootageSource`](crate::FootageSource) that knows which files it opens notes them while
//! [`FootageSource::recording`](crate::FootageSource::recording) runs; the renderer collects them
//! in a [`ReadLog`] ([`Renderer::reads`](crate::Renderer::reads)). Buffers in the layer cache keep
//! the files they were made of, so a frame served from memory reports the same files as the
//! render that made it, without touching the disk.
//!
//! The list is what a cache entry on disk has to check before it is used: a handful of files per
//! frame, never every file of every footage item.
use std::sync::{Arc, Mutex, PoisonError};
/// The identity of a file as a render read it (see [`file_stamp`](crate::disk_cache) for the
/// same identity in content keys).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum FileStamp {
/// A file on disk: its size in bytes and its modification time (nanoseconds since the Unix
/// epoch; `None` where the file system has none).
Native { len: u64, modified: Option<u64> },
/// Bytes given from memory instead of the file system (the browser's file table): their
/// identity is the size and content hash a
/// [`VirtualFileId`](crate::disk_cache::VirtualFileId) gives for the path.
Virtual,
/// The file could not be read (missing, not a regular file, no permission). What was made
/// without it changes once it can be read again.
Missing,
}
impl FileStamp {
/// The identity of a file from metadata the caller already has (of an open file).
pub fn of_metadata(md: &std::fs::Metadata) -> FileStamp {
let modified = md.modified().ok().and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()).map(|d| d.as_nanos() as u64);
FileStamp::Native { len: md.len(), modified }
}
/// The identity of the file at `path` now: one `metadata` call. `None` when the file system
/// can't describe it (missing, or a path served from memory).
pub fn native(path: &str) -> Option<FileStamp> {
std::fs::metadata(path).ok().map(|md| FileStamp::of_metadata(&md))
}
}
/// One file a render read.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ReadFile {
pub path: Arc<str>,
pub stamp: FileStamp,
}
impl ReadFile {
pub fn new(path: impl Into<Arc<str>>, stamp: FileStamp) -> ReadFile {
ReadFile { path: path.into(), stamp }
}
}
/// At most this many files are listed per render or cached buffer; more mark the list
/// incomplete (a comp reading more files than this for one frame is not checked file by file).
pub const MAX_READ_FILES: usize = 4096;
/// The files a render (or one cached buffer) read, each once, sorted by path.
///
/// Incomplete when it can't say for sure: a footage source that doesn't tell what it reads, a
/// buffer from a store that doesn't keep its files, more than [`MAX_READ_FILES`] files, or one
/// file read with two identities (it changed during the render). A cache entry whose list is
/// incomplete can't be checked against the files, so it must not outlive the session.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct ReadFiles {
files: Vec<ReadFile>,
incomplete: bool,
}
impl ReadFiles {
/// Note a file read (once per path).
pub fn add(&mut self, file: ReadFile) {
match self.files.binary_search_by(|f| f.path.cmp(&file.path)) {
Ok(i) => {
if self.files.get(i).is_some_and(|f| f.stamp != file.stamp) {
self.incomplete = true;
}
}
Err(i) => {
if self.files.len() >= MAX_READ_FILES {
self.incomplete = true;
} else {
self.files.insert(i, file);
}
}
}
}
/// Note every file of `other` (and whether it is incomplete).
pub fn extend(&mut self, other: &ReadFiles) {
self.incomplete |= other.incomplete;
for f in &other.files {
self.add(f.clone());
}
}
/// Some files read may be missing from the list (see [`ReadFiles`]).
pub fn mark_incomplete(&mut self) {
self.incomplete = true;
}
/// Whether the list names every file read.
pub fn is_complete(&self) -> bool {
!self.incomplete
}
/// The files, sorted by path.
pub fn files(&self) -> &[ReadFile] {
&self.files
}
/// The identity `path` was read with.
pub fn get(&self, path: &str) -> Option<FileStamp> {
let i = self.files.binary_search_by(|f| (*f.path).cmp(path)).ok()?;
self.files.get(i).map(|f| f.stamp)
}
pub fn len(&self) -> usize {
self.files.len()
}
pub fn is_empty(&self) -> bool {
self.files.is_empty()
}
}
impl FromIterator<ReadFile> for ReadFiles {
fn from_iter<I: IntoIterator<Item = ReadFile>>(iter: I) -> Self {
let mut out = ReadFiles::default();
for f in iter {
out.add(f);
}
out
}
}
/// Collects the files a render reads, from any thread (the layers of a frame render in
/// parallel).
#[derive(Debug, Default)]
pub struct ReadLog {
files: Mutex<ReadFiles>,
}
impl ReadLog {
pub fn new() -> ReadLog {
ReadLog::default()
}
fn lock(&self) -> std::sync::MutexGuard<'_, ReadFiles> {
self.files.lock().unwrap_or_else(PoisonError::into_inner)
}
/// Note a file read.
pub fn note(&self, file: ReadFile) {
self.lock().add(file);
}
/// Note every file of `files` (and whether it is incomplete).
pub fn note_all(&self, files: &ReadFiles) {
if files.is_empty() && files.is_complete() {
return;
}
self.lock().extend(files);
}
/// Some files read can't be listed (see [`ReadFiles`]).
pub fn mark_incomplete(&self) {
self.lock().mark_incomplete();
}
/// The files noted so far.
pub fn files(&self) -> ReadFiles {
self.lock().clone()
}
/// The files noted.
pub fn into_files(self) -> ReadFiles {
self.files.into_inner().unwrap_or_else(PoisonError::into_inner)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn file(path: &str, len: u64) -> ReadFile {
ReadFile::new(path, FileStamp::Native { len, modified: Some(7) })
}
#[test]
fn files_are_listed_once_sorted_by_path() {
let files: ReadFiles = [file("/b", 1), file("/a", 2), file("/b", 1)].into_iter().collect();
assert_eq!(files.files().iter().map(|f| &*f.path).collect::<Vec<_>>(), ["/a", "/b"]);
assert!(files.is_complete());
assert_eq!(files.get("/a"), Some(FileStamp::Native { len: 2, modified: Some(7) }));
assert_eq!(files.get("/c"), None);
}
/// A file read twice with different identities changed during the render: the list can't
/// vouch for what was made of it.
#[test]
fn a_file_read_with_two_identities_makes_the_list_incomplete() {
let files: ReadFiles = [file("/a", 1), file("/a", 2)].into_iter().collect();
assert_eq!(files.len(), 1);
assert!(!files.is_complete());
}
#[test]
fn the_list_is_bounded() {
let files: ReadFiles = (0..MAX_READ_FILES + 3).map(|i| file(&format!("/f{i:06}"), 1)).collect();
assert_eq!(files.len(), MAX_READ_FILES);
assert!(!files.is_complete(), "files past the bound make the list incomplete, never silently short");
}
#[test]
fn incompleteness_carries_over() {
let mut part = ReadFiles::default();
part.mark_incomplete();
let log = ReadLog::new();
log.note(file("/a", 1));
log.note_all(&part);
let all = log.into_files();
assert_eq!(all.len(), 1);
assert!(!all.is_complete());
}
}
+2 -2
View File
@@ -487,7 +487,7 @@ fn model_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, b: &mut Builder) {
LayerSource::Model { item } => {
let Some(it) = r.project.item(*item) else { return };
let ItemKind::Footage(f) = &it.kind else { return };
let Some(m) = r.footage.model(*item, f) else { return };
let Some(m) = r.read(|| r.footage.model(*item, f)) else { return };
let unit = geo.map_or(1.0, |g| ctx.f(layer, g, "unitScale", 1.0));
let clip = geo.map_or(0, |g| ctx.e(layer, g, "animation")) as usize;
let clip = clip.checked_sub(1).filter(|c| *c < m.animations.len());
@@ -700,7 +700,7 @@ fn card_layer(r: &Renderer, ctx: &EvalCtx, layer: &Layer, b: &mut Builder) {
let world = b.outer * ctx.world_matrix(layer);
if crate::text::per_char_3d(ctx, layer) {
for (buf, m) in crate::text::per_char_planes(ctx, layer, r.opts.scale) {
let buf = r.to_blend(Arc::new(buf), None);
let buf = r.to_blend(Arc::new(buf), None, &Arc::default());
card(b, &buf, world * m, mat);
}
return;
+3 -1
View File
@@ -205,7 +205,9 @@ pub(crate) fn prepare_all<'a>(r: &Renderer, ctx: &EvalCtx<'_>, layer: &'a Layer,
if crate::text::per_char_3d(ctx, layer) {
return crate::text::per_char_planes(ctx, layer, r.opts.scale)
.into_iter()
.filter_map(|(buf, m)| prepare_with(r, ctx, layer, order, out, in_run, PlaneInput { buf: r.to_blend(Arc::new(buf), None), local: m }))
.filter_map(|(buf, m)| {
prepare_with(r, ctx, layer, order, out, in_run, PlaneInput { buf: r.to_blend(Arc::new(buf), None, &Arc::default()), local: m })
})
.collect();
}
prepare(r, ctx, layer, order, out, in_run).into_iter().collect()
+38 -15
View File
@@ -34,6 +34,7 @@ use std::sync::{Arc, Mutex};
use effectcraft_engine::project::{Comp, Heard, ItemId, ItemKind, LayerSource, Node, Project, PropGroup};
use effectcraft_engine::render::disk_cache::{self, DiskCache};
use effectcraft_engine::render::reads::{ReadFiles, ReadLog};
use effectcraft_engine::render::{CancellableFootage, ExprHost, FootageSource, LayerCache, RenderOpts, Renderer};
use effectcraft_engine::time::Tick;
use effectcraft_gpu::{DisplayFrame, FrameError, Gpu};
@@ -1465,7 +1466,9 @@ impl Frames {
let gpu_here = job.src.gpu.is_some() && job.src.gpu_display && !(remote.gpu() && comp_has_effects(&job.src.project, job.comp));
if gpu_here {
let t0 = web_time::Instant::now();
if let Some(img) = w.render_gpu(&job) {
// (A frame kept on the GPU is not written to the disk cache: its files are not
// needed.)
if let Some((img, _files)) = w.render_gpu(&job) {
w.finish(&job, img, t0);
continue;
}
@@ -1617,10 +1620,11 @@ impl Worker {
}
});
let disk_hit = from_disk.is_some();
let ci = match from_disk {
Some(f) => f,
// `files`: the source files the rendered frame was made of (see `render::reads`).
let (ci, files) = match from_disk {
Some(f) => (f, None),
None => match self.render_guarded(&job).filter(|_| !job.cancel.load(Ordering::Relaxed)) {
Some(ci) => ci,
Some((ci, files)) => (ci, Some(files)),
// Called off (frames it read may be missing) or failed: not cached, and not
// "in progress" forever: the viewer asks for it again.
None => {
@@ -1633,6 +1637,16 @@ impl Worker {
// and compression. Persistence still runs on this worker, using the same snapshot.
self.finish(&job, ci.clone(), t0);
if !disk_hit && let Some((dc, k)) = &disk {
// (#3184: the entry is to keep `files`, checked before it is used again.)
if let Some(files) = &files {
log::trace!(
"frame {} of comp {}: {} source files{}",
job.key.frame,
job.key.comp,
files.len(),
if files.is_complete() { "" } else { " (incomplete)" }
);
}
let written = match &ci {
FrameImage::Cpu(img) => {
let rgba: Vec<u8> = img.pixels.iter().flat_map(|c| c.to_array()).collect();
@@ -1689,9 +1703,13 @@ impl Worker {
}
/// [`Worker::render_job`], again on the CPU if it panicked (the panic hook has logged it);
/// `None` if that panicked too.
fn render_guarded(&self, job: &Job) -> Option<FrameImage> {
let attempt = |gpu: bool| std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.render_job(job, gpu))).ok();
/// `None` if that panicked too. With the source files the frame was made of.
fn render_guarded(&self, job: &Job) -> Option<(FrameImage, ReadFiles)> {
let attempt = |gpu: bool| {
let files = ReadLog::new();
let img = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.render_job(job, gpu, &files))).ok()?;
Some((img, files.into_files()))
};
let img = attempt(true).or_else(|| {
log::error!("a frame render panicked (frame {})", job.key.frame);
job.src.gpu.as_ref().and_then(|_| attempt(false))
@@ -1731,16 +1749,19 @@ impl Worker {
!self.gpu_retired.load(Ordering::Acquire) && self.cache.lock().is_ok_and(|c| c.gpu_ready())
}
/// The frame on the GPU, kept there for the viewer, if the compositor can do it alone.
fn render_gpu(&self, job: &Job) -> Option<FrameImage> {
/// The frame on the GPU, kept there for the viewer, if the compositor can do it alone, with
/// the source files it was made of.
fn render_gpu(&self, job: &Job) -> Option<(FrameImage, ReadFiles)> {
if !self.gpu_ready() {
return None;
}
let g = job.src.gpu.as_ref()?;
let footage = job.footage();
let files = ReadLog::new();
let mut r = Renderer::new(&job.src.project, &footage, job.opts);
r.expr = job.src.expr.as_deref();
r.cache = Some(&job.src.layer_cache);
r.reads = Some(&files);
r.accel = Some(g as &dyn effectcraft_engine::render::Accelerator);
r.active_accel()?;
let f = g.within_memory(|| g.render_display(&r, job.comp, job.t));
@@ -1750,7 +1771,7 @@ impl Worker {
if f.is_some() {
self.gpu_succeeded();
}
f.map(|f| FrameImage::Gpu(Arc::new(f)))
f.map(|f| (FrameImage::Gpu(Arc::new(f)), files.into_files()))
}
Err(e) => {
self.gpu_attempt_failed(g, &e);
@@ -1759,11 +1780,13 @@ impl Worker {
}
}
/// Render a frame; `gpu: false` keeps it off the GPU compositor.
fn render_job(&self, job: &Job, gpu: bool) -> FrameImage {
/// Render a frame; `gpu: false` keeps it off the GPU compositor. The source files it reads
/// are noted in `files` (a GPU attempt that falls back to the CPU notes both renders').
fn render_job(&self, job: &Job, gpu: bool, files: &ReadLog) -> FrameImage {
let footage = job.footage();
let mut r = Renderer::new(&job.src.project, &footage, job.opts);
r.expr = job.src.expr.as_deref();
r.reads = Some(files);
let gpu_allowed = gpu && self.gpu_ready();
// A failed GPU attempt may have populated intermediate entries before its scope
// reported failure. The CPU retry must compute its own pixels from the snapshot.
@@ -1789,7 +1812,7 @@ impl Worker {
// Time the GPU's work, not just its recording.
g.wait();
if g.context().check_health().is_err() {
return self.render_job(job, false);
return self.render_job(job, false, files);
}
p.record(key, true, t0.elapsed().as_secs_f64() * 1000.0);
}
@@ -1802,7 +1825,7 @@ impl Worker {
}
Err(e) => {
self.gpu_attempt_failed(g, &e);
return self.render_job(job, false);
return self.render_job(job, false, files);
}
}
}
@@ -1822,7 +1845,7 @@ impl Worker {
}
Err(e) => {
self.gpu_attempt_failed(g, &e);
self.render_job(job, false)
self.render_job(job, false, files)
}
};
}