Suspend automatic preview caching during exports (#4248)

Co-authored-by: Fredrik Hallin <fredrik.hallin@guidelinegeo.com>
This commit is contained in:
FredAppsandFredrik Hallin authored and GitHub committed 2026-11-28 20:57:56 +01:00
1 parent 3956c800aa
commit 4bf3e6d4e1
4 files changed
+99 -1

No files matched your search

+2
View File
@@ -206,6 +206,8 @@ impl EffectcraftApp {
/// `Context::request_discard` replays.
pub fn tick(&mut self, ctx: &egui::Context, frame: &eframe::Frame) {
self.now = self.clock.now();
// Preview workers share the export's GPU, but are outside its in-flight limit.
self.frames.suspend_background(self.session.is_rendering());
// The compositor exists before the first paint and while hidden (device-loss handling
// must run there too).
self.init_gpu(ctx, frame);
+21 -1
View File
@@ -1629,6 +1629,21 @@ impl Frames {
self.queue.lock().map(|q| q.urgent_running > 0 || q.jobs.iter().any(|j| j.urgent)).unwrap_or(false)
}
/// Give exports the workers used by automatic preview caching. Already running frames
/// finish normally; cached frames and explicit viewer requests remain available.
pub fn suspend_background(&self, suspended: bool) {
let Ok(mut q) = self.queue.lock() else { return };
q.background_suspended = suspended;
if suspended && let Ok(mut inf) = self.inflight.lock() {
q.jobs.retain(|j| {
if !j.urgent {
inf.remove(&j.key);
}
j.urgent
});
}
}
fn request_with(&self, src: &RenderSource, key: FrameKey, comp: ItemId, t: Tick, opts: RenderOpts, want: Want) {
let urgent = want == Want::Viewer;
// The frame on screen sets the scale evictions keep: a prefetch or a passive viewer at
@@ -1647,6 +1662,9 @@ impl Frames {
return;
}
let Ok(mut q) = self.queue.lock() else { return };
if q.background_suspended && !urgent {
return;
}
let Ok(mut inf) = self.inflight.lock() else { return };
// Jobs for an older project revision can never be shown: drop them.
let before = q.jobs.len();
@@ -1661,8 +1679,9 @@ impl Frames {
// into the cache. During edits the viewer advances through compatible completed
// revisions; scrubbing holds its last frame until the requested time is in (#284).
// Other comps' viewer frames become prefetch.
let suspended = q.background_suspended;
q.jobs.retain(|j| {
let superseded = j.urgent && j.key != key && j.key.comp == key.comp;
let superseded = j.key != key && ((j.urgent && j.key.comp == key.comp) || suspended);
if superseded {
inf.remove(&j.key);
}
@@ -2292,6 +2311,7 @@ impl Job {
#[derive(Default)]
struct Queue {
jobs: Vec<Job>,
background_suspended: bool,
running: HashSet<FrameKey>,
urgent_running: usize,
/// The running viewer (urgent) frames and how to call them off.
+23
View File
@@ -74,6 +74,29 @@ fn disk_fixture(stored_frames: &[i64]) -> (Frames, RenderSource, FrameKey, Rende
(frames, source, base, opts, disk, content)
}
#[test]
fn export_suspension_drops_prefetch_and_resumes_without_losing_cached_frames() {
let (frames, src, base, opts, _, _) = disk_fixture(&[]);
let cid = ItemId(base.comp);
let idle = FrameKey { frame: 1, ..base };
let prefetch = FrameKey { frame: 2, ..base };
frames.cache.lock().unwrap().insert(base, img());
frames.request_background(&src, idle, cid, Tick::ZERO, opts);
frames.request(&src, prefetch, cid, Tick::ZERO, opts);
assert_eq!(frames.inflight(), 2);
frames.suspend_background(true);
assert_eq!(frames.inflight(), 0, "queued automatic frames must release their admission keys");
assert!(frames.is_cached(&base), "an export must not purge the preview");
frames.request_background(&src, idle, cid, Tick::ZERO, opts);
frames.request(&src, prefetch, cid, Tick::ZERO, opts);
assert_eq!(frames.inflight(), 0, "new automatic work waits for the export");
frames.request_urgent(&src, idle, cid, Tick::ZERO, opts);
assert_eq!(frames.inflight(), 1, "explicit viewer requests still work");
frames.suspend_background(false);
frames.request(&src, prefetch, cid, Tick::ZERO, opts);
assert_eq!(frames.inflight(), 2, "prefetch resumes after the export");
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn disk_indicator_does_not_hash_on_ui_thread_and_coalesces_edits() {
@@ -261,3 +261,56 @@ fn replacing_project_before_render_poll_returns_an_explicit_error() {
assert_eq!(reply["ok"], false, "{reply}");
assert!(reply["error"].as_str().unwrap().contains("replaced before completion"));
}
#[test]
fn active_exports_suspend_automatic_preview_requests_and_completion_resumes_them() {
use effectcraft_engine::render::RenderOpts;
use effectcraft_time::Tick;
use effectcraft_ui_egui::frames::{FrameKey, RemoteDone, RemoteFrames, RemoteJob, RenderSource, opts_hash};
struct PausedWorker;
impl RemoteFrames for PausedWorker {
fn slots(&self) -> usize {
0
}
fn start(&self, _: RemoteJob, _: RemoteDone) {
panic!("no worker slots")
}
}
for scripted in [false, true] {
let (mut h, gate, tx) = harness();
h.state_mut().frames.set_remote(Some(Arc::new(PausedWorker)));
let params = if scripted {
json!({"command": "script.run", "params": {"code": "app.project.renderQueue.render();"}})
} else {
json!({"command": "renderQueue.render"})
};
let reply = send(&tx, "engine.execute", params);
h.run_steps(3);
assert!(h.state().session.is_rendering());
let app = h.state();
let s = &app.session;
let cid = s.active_comp_id().unwrap();
let opts = RenderOpts::default();
let key = FrameKey { revision: s.revision, content: 987654, comp: cid.0, frame: 100, scale: 1000, view: 0, opts: opts_hash(&opts) };
let src = RenderSource {
project: s.project.clone(),
footage: s.footage.clone(),
expr: None,
layer_cache: Arc::default(),
gpu: None,
gpu_display: false,
disk: None,
};
let before = app.frames.inflight();
app.frames.request_background(&src, key, cid, Tick::ZERO, opts);
assert_eq!(app.frames.inflight(), before, "automatic previews must not compete with an export (scripted={scripted})");
gate.0.store(true, Ordering::SeqCst);
assert_eq!(finish(&mut h, &reply)["ok"], true);
h.run_steps(2);
let before = h.state().frames.inflight();
h.state().frames.request_background(&src, key, cid, Tick::ZERO, opts);
assert_eq!(h.state().frames.inflight(), before + 1, "preview caching resumes when the export finishes");
}
}