mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 15:22:43 +00:00
Merge pull request #2042 from arnav-hiwarkar/perf/close-project-frees-caches
Close, New and Open Project free the closed project's memory
This commit is contained in:
10 files changed
+335
-36
No files matched your search
@@ -167,6 +167,13 @@ pub struct Session {
|
||||
pub journal: Vec<(String, Value)>,
|
||||
/// Processed-layer pixels reused across frames and edits (content-keyed, never stale).
|
||||
pub layer_cache: Arc<LayerCache>,
|
||||
/// Set when this project closes ([`Session::replace_project`]), then replaced by a fresh flag
|
||||
/// for the next one: work started for the project off the UI thread (Project panel
|
||||
/// thumbnails) stops reading its footage and lets go of what it read, so it can't fill the
|
||||
/// shared caches with a closed project's frames. Public only so that other crates can build a
|
||||
/// `Session { .. }`.
|
||||
#[doc(hidden)]
|
||||
pub project_closed: Arc<std::sync::atomic::AtomicBool>,
|
||||
/// The last Cryptomatte pick probe (see [`Session::crypto_probe_job`]). Public only so that
|
||||
/// other crates can build a `Session { .. }`; its type is crate-private.
|
||||
#[doc(hidden)]
|
||||
@@ -351,6 +358,7 @@ impl Default for Session {
|
||||
events: vec![],
|
||||
journal: vec![],
|
||||
layer_cache: Arc::new(LayerCache::default()),
|
||||
project_closed: Default::default(),
|
||||
crypto_probe: Default::default(),
|
||||
disk_cache: None,
|
||||
pending_disk_drops: Default::default(),
|
||||
|
||||
@@ -196,6 +196,9 @@ impl Session {
|
||||
|
||||
/// Replace the whole project (open/new), resetting history and state.
|
||||
pub fn replace_project(&mut self, mut p: Project, path: Option<String>) {
|
||||
// Work still running for the closed project stops reading it (see `project_closed`).
|
||||
self.project_closed.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
self.project_closed = Arc::default();
|
||||
if self.stop_render()
|
||||
&& let Some(mut job) = self.render_job.take()
|
||||
{
|
||||
@@ -221,13 +224,13 @@ impl Session {
|
||||
self.script_ui.project_epoch = self.script_ui.project_epoch.wrapping_add(1);
|
||||
p.fix_next_id();
|
||||
upgrade_effects(&mut p);
|
||||
// The media layer keys decoded frames by path: files of the new project may have changed
|
||||
// since an earlier project decoded them (#1070).
|
||||
for it in p.items.values() {
|
||||
if let effectcraft_project::ItemKind::Footage(f) = &it.kind {
|
||||
self.forget_decoded(f);
|
||||
}
|
||||
// Let go of what the closed project's footage and proxies decoded (frames, decoders,
|
||||
// models), and of what the new project's files decoded under an earlier project: the
|
||||
// media layer keys them by path, and the files may have changed since (#1070).
|
||||
for f in super::render::project_footage(&self.project).chain(super::render::project_footage(&p)) {
|
||||
super::render::forget_files(self.footage.as_ref(), f);
|
||||
}
|
||||
self.drop_crypto_probe();
|
||||
self.layer_cache.clear();
|
||||
self.project = Arc::new(p);
|
||||
// The set of footage effects read the channels of is sent again, even when it is empty.
|
||||
@@ -237,6 +240,9 @@ impl Session {
|
||||
self.path = path;
|
||||
self.bump();
|
||||
self.mark_saved();
|
||||
// The viewers' RAM preview shows the closed project's frames, whose comp ids can collide
|
||||
// with the new project's.
|
||||
self.events.push(Event::PurgeCaches);
|
||||
let first = self.project.comps().next().map(|(id, _)| *id);
|
||||
if let Some(c) = first {
|
||||
self.open_comp(c);
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use effectcraft_render::{CancellableFootage, FootageSource};
|
||||
|
||||
use super::{EngineError, Image, ItemId, RenderOpts, Renderer, Result, Session, ThumbnailJob, Tick, Value, commands};
|
||||
|
||||
impl Session {
|
||||
@@ -77,9 +81,12 @@ impl Session {
|
||||
/// A Project panel thumbnail of an item (a comp at `t`, footage at its first frame), longest
|
||||
/// side at most `max_side`, as a job that owns everything it reads: frontends run it off the
|
||||
/// UI thread (a big comp takes far longer than a frame). `None` for items without pixels.
|
||||
/// Once the project closes ([`Session::replace_project`]) the job reads no more and answers
|
||||
/// `None`, after letting go of what it read meanwhile: a thread that started late or decoded
|
||||
/// through Close can't fill the shared caches with the closed project's frames.
|
||||
pub fn thumbnail_job(&self, item: ItemId, t: Tick, max_side: u32) -> Option<ThumbnailJob> {
|
||||
let it = self.project.item(item)?;
|
||||
let footage = self.footage.clone();
|
||||
let (footage, closed) = (self.footage.clone(), self.project_closed.clone());
|
||||
match &it.kind {
|
||||
effectcraft_project::ItemKind::Comp(c) => {
|
||||
let (project, expr, cache) = (self.project.clone(), self.expr.clone(), self.layer_cache.clone());
|
||||
@@ -93,21 +100,38 @@ impl Session {
|
||||
};
|
||||
let bg = c.background;
|
||||
Some(Box::new(move || {
|
||||
let mut r = Renderer::new(&project, footage.as_ref(), opts);
|
||||
if closed.load(Ordering::SeqCst) {
|
||||
return None;
|
||||
}
|
||||
let src = CancellableFootage { inner: footage.as_ref(), cancel: closed.clone() };
|
||||
let mut r = Renderer::new(&project, &src, opts);
|
||||
r.expr = expr.as_deref();
|
||||
r.cache = Some(&cache);
|
||||
let img = r.comp_frame(item, t);
|
||||
if closed.load(Ordering::SeqCst) {
|
||||
project_footage(&project).for_each(|f| forget_files(footage.as_ref(), f));
|
||||
cache.clear();
|
||||
return None;
|
||||
}
|
||||
Some((img.width, img.height, img.to_rgba8_over(bg)))
|
||||
}))
|
||||
}
|
||||
effectcraft_project::ItemKind::Footage(f) if f.has_video => {
|
||||
let f = f.clone();
|
||||
Some(Box::new(move || {
|
||||
if closed.load(Ordering::SeqCst) {
|
||||
return None;
|
||||
}
|
||||
// Vector footage is drawn at the thumbnail's size: its declared size can be a
|
||||
// 16384² canvas, gigabytes of pixels to point-sample down to a few dozen.
|
||||
let scale = (max_side.max(1) as f64 / f.width.max(f.height).max(1) as f64).min(1.0);
|
||||
let src = footage.vector_frame(item, &f, scale).or_else(|| footage.frame(item, &f, Tick(0)))?;
|
||||
let img = effectcraft_render::cineon_frame(src, &f);
|
||||
let read = CancellableFootage { inner: footage.as_ref(), cancel: closed.clone() };
|
||||
let src = read.vector_frame(item, &f, scale).or_else(|| read.frame(item, &f, Tick(0)));
|
||||
if closed.load(Ordering::SeqCst) {
|
||||
forget_files(footage.as_ref(), &f);
|
||||
return None;
|
||||
}
|
||||
let img = effectcraft_render::cineon_frame(src?, &f);
|
||||
// Point-sample down to at most `max_side` on the long side (a drawn size one
|
||||
// pixel over it, from rounding, is shrunk by that pixel, not halved).
|
||||
let (long, max) = (u64::from(img.width.max(img.height)), u64::from(max_side.max(1)));
|
||||
@@ -119,7 +143,7 @@ impl Session {
|
||||
for x in 0..w {
|
||||
let (sx, sy) = (u64::from(x) * u64::from(img.width) / u64::from(w), u64::from(y) * u64::from(img.height) / u64::from(h));
|
||||
let i = ((sy * u64::from(img.width) + sx) * 4) as usize;
|
||||
px.extend_from_slice(&full[i..i + 4]);
|
||||
px.extend_from_slice(full.get(i..i + 4)?);
|
||||
}
|
||||
}
|
||||
Some((w, h, px))
|
||||
@@ -145,9 +169,7 @@ impl Session {
|
||||
/// Make the media layer and the layer cache let go of what they decoded from `footage`'s
|
||||
/// files (it keys frames by path, so an overwritten file would keep showing its old pixels).
|
||||
pub(crate) fn forget_decoded(&self, footage: &effectcraft_project::Footage) {
|
||||
for p in std::iter::once(&footage.path).chain(&footage.sequence) {
|
||||
self.footage.forget(p);
|
||||
}
|
||||
forget_files(self.footage.as_ref(), footage);
|
||||
self.layer_cache.clear();
|
||||
self.drop_crypto_probe();
|
||||
}
|
||||
@@ -191,3 +213,21 @@ pub(crate) fn aux_footage(project: &effectcraft_project::Project) -> Vec<String>
|
||||
paths.dedup();
|
||||
paths
|
||||
}
|
||||
|
||||
/// Make `src` let go of what it decoded from `footage`'s files (its file, or a sequence's).
|
||||
pub(crate) fn forget_files(src: &dyn FootageSource, footage: &effectcraft_project::Footage) {
|
||||
for p in std::iter::once(&footage.path).chain(&footage.sequence) {
|
||||
src.forget(p);
|
||||
}
|
||||
}
|
||||
|
||||
/// Every footage `project` shows: its footage items and the proxies of its items.
|
||||
pub(crate) fn project_footage(project: &effectcraft_project::Project) -> impl Iterator<Item = &effectcraft_project::Footage> {
|
||||
project.items.values().flat_map(|it| {
|
||||
let own = match &it.kind {
|
||||
effectcraft_project::ItemKind::Footage(f) => Some(f),
|
||||
_ => None,
|
||||
};
|
||||
own.into_iter().chain(it.proxy.as_deref().map(|px| &px.footage))
|
||||
})
|
||||
}
|
||||
@@ -46,6 +46,7 @@ mod motion_blur_depth;
|
||||
mod paint;
|
||||
mod panels;
|
||||
mod project_items;
|
||||
mod project_release;
|
||||
mod proxy;
|
||||
mod quick_offset;
|
||||
mod render_overwrite;
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
//! Close, New and Open Project let go of what the closed project decoded (#2042).
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use effectcraft_project::{Footage, ItemId, ItemKind, Project};
|
||||
use effectcraft_time::Tick;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::render::{FootageSource, Image};
|
||||
use crate::{Event, Session};
|
||||
|
||||
/// A footage source that records the paths it is told to forget.
|
||||
#[derive(Default)]
|
||||
struct Recording(Mutex<Vec<String>>);
|
||||
|
||||
impl FootageSource for Recording {
|
||||
fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option<Arc<Image>> {
|
||||
None
|
||||
}
|
||||
fn forget(&self, path: &str) {
|
||||
self.0.lock().unwrap().push(path.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
fn with_footage(base: &Project, name: &str, path: &str) -> Project {
|
||||
let mut p = base.clone();
|
||||
p.add_item(name, Default::default(), None, ItemKind::Footage(Footage { path: path.into(), ..Default::default() }));
|
||||
p
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replacing_project_releases_old_footage() {
|
||||
let mut s = Session::default();
|
||||
s.execute("comp.new", json!({"name": "Main", "width": 640, "height": 360, "frameRate": 30, "duration": 10})).unwrap();
|
||||
let src = Arc::new(Recording::default());
|
||||
s.footage = src.clone();
|
||||
let recorded = || src.0.lock().unwrap().clone();
|
||||
let open_old = |s: &mut Session| {
|
||||
let old = with_footage(&s.project, "old", "/old.mp4");
|
||||
s.replace_project(old, None);
|
||||
s.drain_events();
|
||||
src.0.lock().unwrap().clear();
|
||||
};
|
||||
|
||||
// Close Project lets go of the closed project's footage and clears the RAM preview.
|
||||
open_old(&mut s);
|
||||
s.execute("file.closeProject", json!({})).unwrap();
|
||||
assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded());
|
||||
assert!(s.drain_events().contains(&Event::PurgeCaches));
|
||||
|
||||
// Opening a project forgets the old project's files and the new project's.
|
||||
open_old(&mut s);
|
||||
s.replace_project(with_footage(&Project::default(), "new", "/new.mp4"), None);
|
||||
let paths = recorded();
|
||||
assert!(paths.contains(&"/old.mp4".to_string()) && paths.contains(&"/new.mp4".to_string()), "{paths:?}");
|
||||
assert!(s.drain_events().contains(&Event::PurgeCaches));
|
||||
|
||||
// New Project does the same.
|
||||
open_old(&mut s);
|
||||
s.execute("file.newProject", json!({})).unwrap();
|
||||
assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded());
|
||||
assert!(s.drain_events().contains(&Event::PurgeCaches));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_a_project_releases_its_proxies() {
|
||||
let mut s = Session::default();
|
||||
let src = Arc::new(Recording::default());
|
||||
s.footage = src.clone();
|
||||
let mut p = with_footage(&Project::default(), "old", "/old.mp4");
|
||||
let item = p.items.values_mut().next().unwrap();
|
||||
let footage = Footage { path: "/old-proxy.mov".into(), ..Default::default() };
|
||||
item.proxy = Some(Box::new(effectcraft_project::Proxy { footage, enabled: true }));
|
||||
s.replace_project(p, None);
|
||||
src.0.lock().unwrap().clear();
|
||||
s.execute("file.closeProject", json!({})).unwrap();
|
||||
assert!(src.0.lock().unwrap().contains(&"/old-proxy.mov".to_string()), "{:?}", src.0.lock().unwrap());
|
||||
}
|
||||
|
||||
/// A footage source with a memory cache, like the media pool: a frame read keeps its file's
|
||||
/// entry until the file is forgotten. With a gate, the first read waits for the test.
|
||||
#[derive(Default)]
|
||||
struct Caching {
|
||||
cached: Mutex<std::collections::HashSet<String>>,
|
||||
gate: Mutex<Option<(std::sync::mpsc::Sender<()>, std::sync::mpsc::Receiver<()>)>>,
|
||||
}
|
||||
|
||||
impl FootageSource for Caching {
|
||||
fn frame(&self, _: ItemId, footage: &Footage, _: Tick) -> Option<Arc<Image>> {
|
||||
let gate = self.gate.lock().unwrap().take();
|
||||
if let Some((entered, go)) = gate {
|
||||
entered.send(()).unwrap();
|
||||
go.recv().unwrap();
|
||||
}
|
||||
self.cached.lock().unwrap().insert(footage.path.clone());
|
||||
Some(Arc::new(Image::new(footage.width, footage.height)))
|
||||
}
|
||||
fn forget(&self, path: &str) {
|
||||
self.cached.lock().unwrap().remove(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// A session showing `/old.png` (a footage item, and a comp with a layer of it) through a
|
||||
/// [`Caching`] source: (session, source, footage item, comp).
|
||||
fn old_project() -> (Session, Arc<Caching>, ItemId, ItemId) {
|
||||
let mut s = Session::default();
|
||||
let src = Arc::new(Caching::default());
|
||||
s.footage = src.clone();
|
||||
let still = Footage { path: "/old.png".into(), has_video: true, width: 8, height: 8, ..Default::default() };
|
||||
let mut p = Project::default();
|
||||
let item = p.add_item("old", Default::default(), None, ItemKind::Footage(still));
|
||||
s.replace_project(p, None);
|
||||
s.execute("comp.new", json!({"name": "Main", "width": 8, "height": 8, "frameRate": 30, "duration": 1})).unwrap();
|
||||
s.execute("layer.addItem", json!({"item": item.0})).unwrap();
|
||||
let comp = s.active_comp_id().unwrap();
|
||||
(s, src, item, comp)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn held_thumbnail_jobs_cannot_refill_the_closed_projects_caches() {
|
||||
for close in ["file.closeProject", "file.newProject"] {
|
||||
let (mut s, src, item, comp) = old_project();
|
||||
// Project panel thumbnails, made while the project is open and run after it closed (a
|
||||
// thread that started late).
|
||||
let jobs = [s.thumbnail_job(item, Tick(0), 16).unwrap(), s.thumbnail_job(comp, Tick(0), 16).unwrap()];
|
||||
s.execute(close, json!({})).unwrap();
|
||||
let shown: Vec<_> = jobs.into_iter().map(|job| job()).collect();
|
||||
assert!(src.cached.lock().unwrap().is_empty(), "{close}: the closed project's frames came back");
|
||||
assert_eq!(s.layer_cache.stats().entries, 0, "{close}: the closed project's layers came back");
|
||||
assert!(shown.iter().all(Option::is_none), "{close}: a closed project's thumbnail isn't shown");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thumbnails_reading_through_close_let_go_of_what_they_read() {
|
||||
for which in ["footage", "comp"] {
|
||||
let (mut s, src, item, comp) = old_project();
|
||||
let (entered, entered_rx) = std::sync::mpsc::channel();
|
||||
let (go, go_rx) = std::sync::mpsc::channel();
|
||||
*src.gate.lock().unwrap() = Some((entered, go_rx));
|
||||
let job = s.thumbnail_job(if which == "comp" { comp } else { item }, Tick(0), 16).unwrap();
|
||||
let running = std::thread::spawn(job);
|
||||
// The job is decoding the closed project's still when the project closes.
|
||||
entered_rx.recv_timeout(std::time::Duration::from_secs(30)).unwrap();
|
||||
s.execute("file.closeProject", json!({})).unwrap();
|
||||
go.send(()).unwrap();
|
||||
let shown = running.join().unwrap();
|
||||
assert!(src.cached.lock().unwrap().is_empty(), "{which}: the frame decoded through Close was kept");
|
||||
assert_eq!(s.layer_cache.stats().entries, 0, "{which}: the closed project's layers were kept");
|
||||
assert!(shown.is_none(), "{which}: a closed project's thumbnail isn't shown");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_open_projects_thumbnails_still_render() {
|
||||
let (s, src, item, comp) = old_project();
|
||||
let (w, h, px) = s.thumbnail_job(item, Tick(0), 16).unwrap()().unwrap();
|
||||
assert_eq!((w, h, px.len()), (8, 8, 8 * 8 * 4));
|
||||
assert!(s.thumbnail_job(comp, Tick(0), 16).unwrap()().is_some());
|
||||
assert!(src.cached.lock().unwrap().contains("/old.png"));
|
||||
}
|
||||
@@ -208,6 +208,10 @@ struct CacheState {
|
||||
last: HashMap<Arc<str>, i64>,
|
||||
/// Movies whose frames can't be kept as 8-bit levels (more bits, or shown turned).
|
||||
wide: HashSet<Arc<str>>,
|
||||
/// Counts [`MediaPool::forget`]s and frame clears: a decode keeps its frame only when none
|
||||
/// came while it ran, so one in flight when a file is forgotten (a closed project's, or one
|
||||
/// changed on disk) can't bring the frame back.
|
||||
forgets: u64,
|
||||
}
|
||||
|
||||
/// A movie/audio source by path ([`Opened`]), opened by the first caller while the others wait
|
||||
@@ -406,6 +410,7 @@ impl MediaPool {
|
||||
let mut c = lock(&self.inner.cache);
|
||||
c.lru = Lru::default();
|
||||
c.last.clear();
|
||||
c.forgets = c.forgets.wrapping_add(1);
|
||||
drop(c);
|
||||
// Decoded GIFs sit outside the frame budget; Purge frees them too.
|
||||
lock(&self.inner.gifs).clear();
|
||||
@@ -418,7 +423,9 @@ impl MediaPool {
|
||||
lock(&self.inner.problems).clear();
|
||||
}
|
||||
|
||||
/// Forget a file (after it changed on disk, or to retry a file that failed to open).
|
||||
/// Forget a file (after it changed on disk or its project closed, or to retry a file that
|
||||
/// failed to open). A decode of any file in flight meanwhile keeps nothing (see
|
||||
/// [`CacheState::forgets`]).
|
||||
pub fn forget(&self, path: &str) {
|
||||
lock(&self.inner.sources).remove(path);
|
||||
lock(&self.inner.models).remove(path);
|
||||
@@ -431,6 +438,7 @@ impl MediaPool {
|
||||
}
|
||||
c.last.remove(path);
|
||||
c.wide.remove(path);
|
||||
c.forgets = c.forgets.wrapping_add(1);
|
||||
drop(c);
|
||||
crate::exr_channels::forget(path);
|
||||
}
|
||||
@@ -455,7 +463,7 @@ impl MediaPool {
|
||||
if lock(&self.inner.cache).wide.contains(footage.path.as_str()) {
|
||||
return None;
|
||||
}
|
||||
match Inner::frame_px(&self.inner, footage, t, true, None, float, true) {
|
||||
match Inner::frame_px(&self.inner, footage, t, true, None, float, true, None) {
|
||||
Ok(Pixels::Q8(f)) => Some(f),
|
||||
_ => None,
|
||||
}
|
||||
@@ -850,7 +858,7 @@ impl Inner {
|
||||
/// The frame of `footage` at `t`, resampled to `size` (see [`FootageSource::frame_at_size`]):
|
||||
/// a movie's made at that size directly, a still's resampled once and kept at that size.
|
||||
fn frame_at(self: &Arc<Self>, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve) -> Result<Arc<Image>> {
|
||||
match Self::frame_px(self, footage, t, playback, size, float, false)? {
|
||||
match Self::frame_px(self, footage, t, playback, size, float, false, None)? {
|
||||
Pixels::F32(img) => Ok(img),
|
||||
// (a float request's key never holds 8-bit levels)
|
||||
Pixels::Q8(_) => Err(MediaError::Decode(format!("{}: 8-bit frame for a float request", footage.path))),
|
||||
@@ -859,8 +867,20 @@ impl Inner {
|
||||
|
||||
/// [`Self::frame_at`], kept as 8-bit levels when `q8` is set and the frame has 8 bits (a
|
||||
/// float frame under the float key otherwise, and the movie is remembered as `wide`).
|
||||
/// `since`: the [`CacheState::forgets`] count the read was asked at (a read-ahead's, when it
|
||||
/// was queued); a read that missed is not decoded once a forget came after it, and a frame
|
||||
/// decoded while one came is not kept.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn frame_px(self: &Arc<Self>, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) -> Result<Pixels> {
|
||||
fn frame_px(
|
||||
self: &Arc<Self>,
|
||||
footage: &Footage,
|
||||
t: Tick,
|
||||
playback: bool,
|
||||
size: Option<(u32, u32)>,
|
||||
float: FloatCurve,
|
||||
q8: bool,
|
||||
since: Option<u64>,
|
||||
) -> Result<Pixels> {
|
||||
if !footage.has_video {
|
||||
return Err(MediaError::Unsupported(format!("{}: no video", footage.path)));
|
||||
}
|
||||
@@ -909,6 +929,10 @@ impl Inner {
|
||||
}
|
||||
c = self.done.wait(c).unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
}
|
||||
let since = since.unwrap_or(c.forgets);
|
||||
if c.forgets != since {
|
||||
return Err(MediaError::Cancelled);
|
||||
}
|
||||
self.misses.fetch_add(1, Ordering::Relaxed);
|
||||
c.inflight.insert(loc.key.clone());
|
||||
drop(c);
|
||||
@@ -917,6 +941,9 @@ impl Inner {
|
||||
reads::note_all(&files);
|
||||
let img = img?;
|
||||
let mut c = lock(&self.cache);
|
||||
if c.forgets != since {
|
||||
return Ok(img);
|
||||
}
|
||||
if q8 && let Pixels::F32(_) = img {
|
||||
// not 8-bit: kept as the float frame it is, which the renderer asks for next
|
||||
c.wide.insert(loc.key.path.clone());
|
||||
@@ -935,23 +962,23 @@ impl Inner {
|
||||
next.key.size = loc.key.size;
|
||||
next.key.q8 = loc.key.q8;
|
||||
if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) {
|
||||
self.prefetch(footage, next_t, size, float, loc.key.q8);
|
||||
self.prefetch(footage, next_t, size, float, loc.key.q8, c.forgets);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn prefetch(self: &Arc<Self>, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) {
|
||||
fn prefetch(self: &Arc<Self>, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool, since: u64) {
|
||||
let (me, f) = (self.clone(), footage.clone());
|
||||
self.prefetched.fetch_add(1, Ordering::Relaxed);
|
||||
// A plain thread, not a rayon job: the decode below parallelises with rayon itself.
|
||||
std::thread::spawn(move || {
|
||||
let _ = me.frame_px(&f, t, false, size, float, q8);
|
||||
let _ = me.frame_px(&f, t, false, size, float, q8, Some(since));
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn prefetch(self: &Arc<Self>, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool) {}
|
||||
fn prefetch(self: &Arc<Self>, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool, _since: u64) {}
|
||||
|
||||
/// A still (image file or sequence frame) at `loc.key.size` when one is set: resampled as the
|
||||
/// renderer would ([`FootageSource::frame_at_size`]), so the pool keeps the reduced frame and a
|
||||
@@ -1215,6 +1242,25 @@ fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// #2042: a read queued before its file was forgotten (as a read-ahead of a closed project's
|
||||
/// movie is, by the read of the frame before) decodes nothing and keeps nothing; a read asked
|
||||
/// after the forget decodes and keeps its frame. (A still stands in for the movie.)
|
||||
#[test]
|
||||
fn reads_queued_before_a_forget_keep_nothing() {
|
||||
let mut png = std::io::Cursor::new(Vec::new());
|
||||
image::RgbaImage::new(2, 2).write_to(&mut png, image::ImageFormat::Png).unwrap();
|
||||
let pool = MediaPool::new();
|
||||
pool.add_bytes("/a.png", png.into_inner().into());
|
||||
let f = Footage { path: "/a.png".into(), kind: FootageKind::Still, width: 2, height: 2, has_video: true, ..Default::default() };
|
||||
let queued = lock(&pool.inner.cache).forgets;
|
||||
pool.forget("/a.png");
|
||||
let read = Inner::frame_px(&pool.inner, &f, Tick::ZERO, false, None, FloatCurve::Srgb, false, Some(queued));
|
||||
assert!(matches!(read, Err(MediaError::Cancelled)), "{:?}", read.err());
|
||||
assert_eq!(pool.stats().frames, 0, "the forgotten file's frame came back");
|
||||
pool.frame_at(&f, Tick::ZERO).unwrap();
|
||||
assert_eq!(pool.stats().frames, 1);
|
||||
}
|
||||
|
||||
/// Conforming a long file's audio reads every chunk at its own time: the sample index times
|
||||
/// ticks per second overflowed `i64` past 12:36 at 48 kHz, and later chunks came out silent
|
||||
/// (#172).
|
||||
|
||||
@@ -73,9 +73,9 @@ pub trait FootageSource: Send + Sync {
|
||||
/// Drop the decoded frames it keeps (Edit ▸ Purge ▸ All Memory / Image Cache Memory): the
|
||||
/// next reads decode again. Sources without a cache ignore it.
|
||||
fn purge(&self) {}
|
||||
/// The file at `path` changed on disk (Reload Footage): drop what it keeps of it (decoded
|
||||
/// frames, open decoders, parsed models), so the next reads see the new contents. Sources
|
||||
/// without a cache ignore it.
|
||||
/// The file at `path` changed on disk (Reload Footage), or the project showing it closed:
|
||||
/// drop what it keeps of it (decoded frames, open decoders, parsed models), so the next reads
|
||||
/// see the new contents. Sources without a cache ignore it.
|
||||
fn forget(&self, _path: &str) {}
|
||||
/// Settings ▸ Disk ▸ Conformed Audio Folder: where decoded audio is kept between reads
|
||||
/// (`None` = off); sources that don't decode audio ignore it.
|
||||
@@ -205,7 +205,7 @@ impl FootageSource for CancellableFootage<'_> {
|
||||
self.inner.cache_budget()
|
||||
}
|
||||
fn vector_frame(&self, item: ItemId, footage: &Footage, scale: f64) -> Option<Arc<Image>> {
|
||||
self.inner.vector_frame(item, footage, scale)
|
||||
self.read(|| self.inner.vector_frame(item, footage, scale))
|
||||
}
|
||||
fn cancellable(&self, cancel: &Arc<AtomicBool>, f: &mut dyn FnMut()) {
|
||||
self.inner.cancellable(cancel, f)
|
||||
|
||||
@@ -159,6 +159,7 @@ impl EffectcraftApp {
|
||||
}
|
||||
effectcraft_engine::Event::PurgeCaches => {
|
||||
self.frames.clear();
|
||||
crate::panels::project::forget_thumbnails(ctx);
|
||||
// (frames made after it get keys of their own: see `Self::purge_stamp`)
|
||||
self.purges = self.purges.wrapping_add(1);
|
||||
}
|
||||
|
||||
@@ -120,8 +120,27 @@ pub fn drop_folder(project: &effectcraft_engine::project::Project, row: Option<I
|
||||
if it.is_folder() { Some(it.id) } else { it.parent }
|
||||
}
|
||||
|
||||
/// A Project panel thumbnail: (the revision it shows, its texture).
|
||||
#[derive(Clone)]
|
||||
struct Thumb(u64, egui::TextureHandle);
|
||||
|
||||
/// A thumbnail being rendered off the UI thread: (revision, result slot).
|
||||
type PendingThumb = (u64, Arc<Mutex<Option<Option<(u32, u32, Vec<u8>)>>>>);
|
||||
#[derive(Clone)]
|
||||
struct PendingThumb(u64, Arc<Mutex<Option<Option<(u32, u32, Vec<u8>)>>>>);
|
||||
|
||||
/// The Project panel's thumbnail texture of `item`, once its render has landed.
|
||||
pub fn cached_thumbnail(ctx: &egui::Context, item: ItemId) -> Option<egui::TextureHandle> {
|
||||
ctx.data(|d| d.get_temp::<Thumb>(egui::Id::unique(("proj-thumb", item.0)))).map(|Thumb(_, t)| t)
|
||||
}
|
||||
|
||||
/// Let go of every Project panel thumbnail and the renders in flight for them (a cache purge, or
|
||||
/// a project closed: the next project's item ids can be the closed one's).
|
||||
pub fn forget_thumbnails(ctx: &egui::Context) {
|
||||
ctx.data_mut(|d| {
|
||||
d.remove_by_type::<Thumb>();
|
||||
d.remove_by_type::<PendingThumb>();
|
||||
});
|
||||
}
|
||||
|
||||
/// Thumbnail of a comp (its current time) or footage (first frame), cached per item/revision.
|
||||
/// Rendered on a background thread (inline on wasm32): until the new one lands the previous
|
||||
@@ -130,19 +149,19 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option<egu
|
||||
let key = egui::Id::unique(("proj-thumb", it.id.0));
|
||||
let pkey = egui::Id::unique(("proj-thumb-pending", it.id.0));
|
||||
let rev = app.session.revision;
|
||||
let cached: Option<(u64, egui::TextureHandle)> = ctx.data(|d| d.get_temp(key));
|
||||
let mut tex = cached.as_ref().map(|(_, t)| t.clone());
|
||||
let have = cached.as_ref().map(|(r, _)| *r);
|
||||
let cached: Option<Thumb> = ctx.data(|d| d.get_temp(key));
|
||||
let mut tex = cached.as_ref().map(|Thumb(_, t)| t.clone());
|
||||
let have = cached.as_ref().map(|Thumb(r, _)| *r);
|
||||
// A finished render: upload it.
|
||||
let pending: Option<PendingThumb> = ctx.data(|d| d.get_temp(pkey));
|
||||
if let Some((prev, slot)) = &pending {
|
||||
if let Some(PendingThumb(prev, slot)) = &pending {
|
||||
let done = slot.lock().ok().and_then(|mut s| s.take());
|
||||
match done {
|
||||
Some(Some((w, h, px))) => {
|
||||
let ci = egui::ColorImage::from_rgba_unmultiplied([w as usize, h as usize], &px);
|
||||
let t = ctx.load_texture(format!("proj-thumb-{}", it.id.0), ci, egui::TextureOptions::LINEAR);
|
||||
ctx.data_mut(|d| {
|
||||
d.insert_temp(key, (*prev, t.clone()));
|
||||
d.insert_temp(key, Thumb(*prev, t.clone()));
|
||||
d.remove::<PendingThumb>(pkey);
|
||||
});
|
||||
tex = Some(t);
|
||||
@@ -180,7 +199,7 @@ fn thumbnail(app: &EffectcraftApp, ctx: &egui::Context, it: &Item) -> Option<egu
|
||||
return tex;
|
||||
}
|
||||
}
|
||||
ctx.data_mut(|d| d.insert_temp::<PendingThumb>(pkey, (rev, slot)));
|
||||
ctx.data_mut(|d| d.insert_temp(pkey, PendingThumb(rev, slot)));
|
||||
ctx.request_repaint();
|
||||
tex
|
||||
}
|
||||
@@ -1512,6 +1531,25 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// #2042: closing the project drops the Project panel's thumbnails (and renders in flight),
|
||||
/// so their textures go with it and an item of the next project with a closed one's id never
|
||||
/// shows the closed project's picture.
|
||||
#[test]
|
||||
fn closing_the_project_forgets_its_thumbnails() {
|
||||
let mut app = EffectcraftApp::new(effectcraft_engine::Session::default());
|
||||
let ctx = egui::Context::default();
|
||||
let (key, pkey) = (egui::Id::unique(("proj-thumb", 1u64)), egui::Id::unique(("proj-thumb-pending", 1u64)));
|
||||
let tex = ctx.load_texture("t", egui::ColorImage::from_rgba_unmultiplied([1, 1], &[255; 4]), egui::TextureOptions::LINEAR);
|
||||
ctx.data_mut(|d| {
|
||||
d.insert_temp(key, Thumb(app.session.revision, tex));
|
||||
d.insert_temp(pkey, PendingThumb(app.session.revision, Arc::default()));
|
||||
});
|
||||
app.session.execute("file.closeProject", serde_json::json!({})).unwrap();
|
||||
app.handle_events(&ctx);
|
||||
assert!(ctx.data(|d| d.get_temp::<Thumb>(key)).is_none(), "the closed project's thumbnail is kept");
|
||||
assert!(ctx.data(|d| d.get_temp::<PendingThumb>(pkey)).is_none(), "a closed project's render is awaited");
|
||||
}
|
||||
|
||||
/// Long names show in full: the Name column fits them (it stopped at 260 points with "…")
|
||||
/// up to the panel's width less the Label column and, while optional columns show, room for
|
||||
/// them; a width the user dragged wins.
|
||||
|
||||
@@ -64,9 +64,7 @@ fn comp_thumbnail_renders_off_the_ui_thread() {
|
||||
let mut landed = false;
|
||||
for _ in 0..400 {
|
||||
h.step();
|
||||
if h.state().auto.find("project.thumbnail").is_some()
|
||||
&& h.ctx.data(|d| d.get_temp::<(u64, egui::TextureHandle)>(egui::Id::unique(("proj-thumb", main.0)))).is_some()
|
||||
{
|
||||
if h.state().auto.find("project.thumbnail").is_some() && effectcraft_ui_egui::panels::project::cached_thumbnail(&h.ctx, main).is_some() {
|
||||
landed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user