Merge upstream/main into the plane store branch (#3301)

Conflicts: Cargo manifests (craft-libs pins from main, plus memory-stats, exr and
half for effectcraft-media), exr_channels.rs (the plane store's version; main's
craft-exr channel decode is replaced by the plane store until craft-exr reads
from a seekable source), pool.rs (frame_px takes the rows of #3271 and the
forget generation of #2042), session open/forget (main's forget_files plus the
reload counter), Project panel thumbnails (main's Thumb types, keyed by content).
This commit is contained in:
vangel76 committed 2026-11-16 20:57:45 +01:00
commit 9ff031c5f1
129 files changed
+11874 -18157

No files matched your search

+1 -1
View File
@@ -17,7 +17,7 @@ fn export_lottie(s: &mut Session, p: &Value) -> Result<Value> {
let dot = path.to_ascii_lowercase().ends_with(".lottie");
let bytes = if dot {
let stem = std::path::Path::new(&path).file_stem().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
effectcraft_lottie::to_dotlottie(&res, &stem)
effectcraft_lottie::try_to_dotlottie(&res, &stem).map_err(|e| EngineError::Other(e.to_string()))?
} else {
res.to_string_compact().into_bytes()
};
+8
View File
@@ -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)]
@@ -360,6 +367,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(),
+12 -6
View File
@@ -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();
let lost = 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.
@@ -244,6 +247,9 @@ impl Session {
log::warn!("channel selection is static: keyframes or expressions dropped on {}", lost.join("; "));
self.toast(format!("Channel selection can't be animated: kept the first value of {}", lost.join(", ")));
}
// 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);
+48 -8
View File
@@ -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))
@@ -177,9 +201,7 @@ impl Session {
/// 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) {
self.footage_reloads.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
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();
}
@@ -266,3 +288,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))
})
}
+240 -15
View File
@@ -255,6 +255,80 @@ fn release_problem(r: &Release) -> Option<String> {
None
}
/// An owner or repository name on GitHub: letters, digits, `.`, `-`, `_`, and not `.` or `..`.
fn valid_repo_part(p: &str) -> bool {
!p.is_empty() && p != "." && p != ".." && p.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
}
/// The rest of `s` after `prefix`, ignoring ASCII case.
fn strip_prefix_ignore_case<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
let head = s.get(..prefix.len())?;
head.eq_ignore_ascii_case(prefix).then(|| s.get(prefix.len()..)).flatten()
}
/// A `.` or `..` path segment, also percent-encoded.
fn dot_segment(p: &str) -> bool {
let p = p.to_ascii_lowercase().replace("%2e", ".");
p == "." || p == ".."
}
/// A character a release or repository URL may not contain: `?`, `#`, `\`, Unicode white space,
/// or U+FEFF (the catalog schema's `\s` matches it too).
fn bad_url_char(c: char) -> bool {
matches!(c, '?' | '#' | '\\' | '\u{feff}') || c.is_whitespace()
}
/// `(owner, name)` in lower case of a `https://github.com/<owner>/<name>` repository URL (an
/// optional trailing `/` or `.git`) or of a folder in one, `https://github.com/<owner>/<name>/tree/<ref>/<path>`;
/// `None` for anything else.
fn github_repo(repo: &str) -> Option<(String, String)> {
let rest = strip_prefix_ignore_case(repo.trim(), "https://github.com/")?;
if rest.contains(bad_url_char) {
return None;
}
let rest = rest.strip_suffix('/').unwrap_or(rest);
let parts: Vec<&str> = rest.split('/').collect();
let (owner, name) = match parts.as_slice() {
[owner, name] => (*owner, name.strip_suffix(".git").unwrap_or(name)),
[owner, name, "tree", folder @ ..] if !folder.is_empty() && folder.iter().all(|p| !p.is_empty() && !dot_segment(p)) => (*owner, *name),
_ => return None,
};
(valid_repo_part(owner) && valid_repo_part(name)).then(|| (owner.to_ascii_lowercase(), name.to_ascii_lowercase()))
}
/// Why an `https://` release URL can't be used, if it can't: it must be a release asset,
/// `https://github.com/<owner>/<name>/releases/download/<tag>/<file>`, and when the package
/// names a `repo`, that must be a GitHub repository URL (see [`github_repo`]) of `<owner>/<name>`.
fn release_url_problem(repo: &str, url: &str) -> Option<String> {
let not_asset = || Some(format!("`{url}` isn't a release asset (https://github.com/<owner>/<repo>/releases/download/<tag>/<file>)"));
let Some(rest) = strip_prefix_ignore_case(url, "https://github.com/") else { return not_asset() };
if rest.contains(bad_url_char) {
return not_asset();
}
let parts: Vec<&str> = rest.split('/').collect();
// At least six segments, not exactly six: a tag may contain `/` (`release/v1`), and its assets are
// then at `releases/download/release/v1/<file>`. `.` and `..` (also percent-encoded) would move
// the download to another path.
if parts.len() < 6 || parts.iter().any(|p| p.is_empty() || dot_segment(p)) {
return not_asset();
}
let (Some(owner), Some(name), Some(releases), Some(download)) = (parts.first(), parts.get(1), parts.get(2), parts.get(3)) else { return not_asset() };
if !valid_repo_part(owner) || !valid_repo_part(name) || *releases != "releases" || *download != "download" {
return not_asset();
}
let repo = repo.trim();
if repo.is_empty() {
return None;
}
match github_repo(repo) {
Some((o, n)) if o == owner.to_ascii_lowercase() && n == name.to_ascii_lowercase() => None,
Some(_) => Some(format!("`{url}` isn't a release asset of the package's repository {repo}")),
None => Some(format!(
"the package's `repo` {repo} isn't a GitHub repository URL (https://github.com/<owner>/<repo>), so `{url}` can't be checked against it"
)),
}
}
/// Read a catalog: packages and releases that are malformed are left out (and reported), so one
/// bad entry doesn't hide the others.
pub fn parse_catalog(bytes: &[u8]) -> std::result::Result<(Catalog, Vec<String>), String> {
@@ -325,27 +399,44 @@ enum Source {
File(PathBuf),
}
/// `file:///C:/x` or `file:///home/x` as a path (percent-decoded; no remote hosts).
fn file_url_path(url: &str) -> Option<PathBuf> {
let rest = url.strip_prefix("file://")?;
/// `file:///C:/x` or `file:///home/x` as a path (percent-decoded; no remote hosts). A path that
/// starts with two separators or holds a `\` (raw or percent-encoded), or a percent-encoded `/`,
/// is refused: on Windows such a path can name a network share, and reading it contacts that host.
fn file_url_path(url: &str) -> std::result::Result<PathBuf, String> {
let bad = |why: &str| format!("bad file URL `{url}`: {why}");
let rest = url.strip_prefix("file://").ok_or_else(|| bad("it doesn't start with file://"))?;
let rest = rest.strip_prefix("localhost").unwrap_or(rest);
if !rest.starts_with('/') {
return None;
return Err(bad("only local files can be read (file:///path)"));
}
let b = rest.as_bytes();
// `/C:/…` on Windows.
let rest = if b.len() > 2 && b.get(2) == Some(&b':') { rest.get(1..)? } else { rest };
let rest =
if b.get(1).is_some_and(u8::is_ascii_alphabetic) && b.get(2) == Some(&b':') { rest.get(1..).ok_or_else(|| bad("bad drive letter"))? } else { rest };
let mut out = Vec::with_capacity(rest.len());
let mut bytes = rest.bytes();
while let Some(c) = bytes.next() {
if c == b'%' {
let hex = [bytes.next()?, bytes.next()?];
out.push(u8::from_str_radix(std::str::from_utf8(&hex).ok()?, 16).ok()?);
let hex = [bytes.next(), bytes.next()];
let [Some(hi), Some(lo)] = hex else { return Err(bad("a % isn't followed by two hex digits")) };
let byte =
std::str::from_utf8(&[hi, lo]).ok().and_then(|h| u8::from_str_radix(h, 16).ok()).ok_or_else(|| bad("a % isn't followed by two hex digits"))?;
if matches!(byte, b'/' | b'\\') {
return Err(bad("a percent-encoded / or \\ isn't allowed"));
}
out.push(byte);
} else {
out.push(c);
}
}
String::from_utf8(out).ok().map(PathBuf::from)
let path = String::from_utf8(out).map_err(|_| bad("the path isn't UTF-8"))?;
if path.contains('\\') {
return Err(bad("use / between folders, not \\"));
}
if path.starts_with("//") {
return Err(bad("network paths can't be read, only local files"));
}
Ok(PathBuf::from(path))
}
/// Check `url` against the rules: `https://` on GitHub's hosts; `file://` when the catalog
@@ -360,7 +451,7 @@ fn source(url: &str, catalog: Option<&str>) -> std::result::Result<Source, Strin
}
let local_catalog = catalog.is_none_or(|c| c.starts_with("file://"));
if url.starts_with("file://") && local_catalog {
return file_url_path(url).map(Source::File).ok_or_else(|| format!("bad file URL `{url}`"));
return file_url_path(url).map(Source::File);
}
Err(format!("`{url}` isn't an https:// URL"))
}
@@ -801,7 +892,11 @@ fn pick(s: &Session, c: &str, p: &Value) -> Result<(String, Package, Release)> {
),
));
}
source(&rel.url, Some(&url)).map_err(|e| bad(c, e))?;
if let Source::Https(u) = source(&rel.url, Some(&url)).map_err(|e| bad(c, e))?
&& let Some(why) = release_url_problem(&pkg.repo, &u)
{
return Err(bad(c, format!("{}: {why}", pkg.name)));
}
Ok((url, pkg, rel))
}
@@ -1033,7 +1128,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Refresh Plug-in Catalog",
[],
None,
"{catalog?: https:// or file:// URL of an index.json (default: the community catalog), wait?}",
"{catalog?: https:// or file:// URL of an index.json (default: the community catalog; file:// takes a local path, not a network path such as //server/share), wait?}",
always,
refresh
),
@@ -1051,7 +1146,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Install Plug-in",
[],
None,
"{id, version?, wait?} — downloads the release, checks its SHA-256 against the catalog, loads it",
"{id, version?, wait?} — downloads the release (a GitHub release asset, in the package's repo when it names one), checks its SHA-256 against the catalog, loads it",
always,
install
),
@@ -1135,6 +1230,99 @@ mod tests {
assert!(latest_compatible(&Package::default()).is_none());
}
#[test]
fn release_urls_are_assets_of_the_declared_repository() {
let asset = "https://github.com/Me/Glow/releases/download/v1.0.0/glow.wasm";
for repo in ["https://github.com/me/glow", "https://github.com/Me/Glow/", "https://GitHub.com/me/glow.git", ""] {
assert_eq!(release_url_problem(repo, asset), None, "{repo}");
}
assert_eq!(release_url_problem("", "https://github.com/a/b/releases/download/tags/v1/x.wasm"), None, "a tag with a slash");
assert!(
release_url_problem("https://github.com/me/glow", "https://github.com/me/other/releases/download/v1/x.wasm")
.unwrap()
.contains("package's repository")
);
assert!(release_url_problem("https://github.com/me/glow", "https://github.com/you/glow/releases/download/v1/x.wasm").is_some());
for no in [
"https://raw.githubusercontent.com/me/glow/main/x.wasm",
"https://objects.githubusercontent.com/x.wasm",
"https://github.com/me/glow/blob/main/x.wasm",
"https://github.com/me/glow/releases/download/v1",
"https://github.com/me/glow/releases/download//x.wasm",
"https://github.com/me/glow/releases/download/v1/x.wasm?x=1",
"https://github.com/me/glow/releases/download/v1/x.wasm#x",
"https://github.com/me/glow/releases/download/../../../you/evil/releases/download/v1/x.wasm",
"https://github.com/me/glow/releases/download/%2e%2e/%2E./x/y/x.wasm",
"https://github.com/me/glow/releases/download/./v1/x.wasm",
"https://github.com/me/glow/releases/download/v1\\x.wasm",
"https://github.com/m e/glow/releases/download/v1/x.wasm",
"https://github.com/me/glow/releases/download/v 1/x.wasm",
"https://github.com/me/glow/releases/download/v1/x\t.wasm",
"https://github.com/me/glow/releases/download/v1/x.wasm\n",
"https://github.com/me/glow/releases/download/v1\u{a0}/x.wasm",
"https://github.com/me/glow/releases/download/v1/\u{85}x.wasm",
"https://github.com/me/glow/releases/download/v1/x\u{feff}.wasm",
"https://github.com/../glow/releases/download/v1/x.wasm",
"https://github.com.evil.com/me/glow/releases/download/v1/x.wasm",
] {
assert!(release_url_problem("https://github.com/me/glow", no).is_some(), "{no}");
assert!(release_url_problem("", no).is_some(), "{no}");
}
assert_eq!(github_repo("https://github.com/Me/Glow.git"), Some(("me".into(), "glow".into())));
assert_eq!(github_repo("https://github.com/Me/Glow/tree/main/plugins/glow"), Some(("me".into(), "glow".into())));
for no in ["https://github.com/me", "https://github.com/me/glow/tree", "http://github.com/me/glow", "me/glow"] {
assert_eq!(github_repo(no), None, "{no}");
}
}
#[test]
fn a_repo_that_isnt_a_github_repository_url_refuses_every_release() {
let asset = "https://github.com/me/glow/releases/download/v1/glow.wasm";
for repo in [
"https://github.com/me/glow",
"https://github.com/me/glow/",
"https://github.com/me/glow.git",
"https://github.com/me/glow.git/",
" https://github.com/ME/glow ",
"https://github.com/me/glow/tree/main",
"https://github.com/me/glow/tree/main/plugins/glow/",
"",
" ",
] {
assert_eq!(release_url_problem(repo, asset), None, "{repo:?}");
}
let elsewhere = release_url_problem("https://github.com/me/glow/tree/main", "https://github.com/you/evil/releases/download/v1/glow.wasm");
assert!(elsewhere.as_deref().is_some_and(|w| w.contains("package's repository")), "{elsewhere:?}");
for repo in [
"https://github.com/me/glow/tree",
"https://github.com/me/glow/tree/",
"https://github.com/me/glow/tree//x",
"https://github.com/me/glow/tree/main/../../you/evil",
"https://github.com/me/glow/tree/main/%2e%2e",
"https://github.com/me/glow/blob/main/README.md",
"https://github.com/me/glow.git/x",
"https://github.com/me/glow/releases",
"https://github.com/me/glow//",
"https://github.com/me",
"https://github.com/me/",
"http://github.com/me/glow",
"github.com/me/glow",
"me/glow",
"https://www.github.com/me/glow",
"https://gitlab.com/me/glow",
"https://github.com/me/glow?tab=readme",
"https://github.com/me/glow#readme",
"https://github.com/me/gl ow",
"https://github.com/me/glow/tree/main/my plug-in",
"https://github.com/me/glow/tree/ma\tin",
"https://github.com/me/glow/tree/main/x\u{feff}",
"https://github.com/me/..",
] {
let why = release_url_problem(repo, asset);
assert!(why.as_deref().is_some_and(|w| w.contains("isn't a GitHub repository URL")), "{repo:?}: {why:?}");
}
}
#[test]
fn catalogs_leave_out_bad_entries() {
let good = "a".repeat(64);
@@ -1180,10 +1368,47 @@ mod tests {
assert_eq!(source("file:///C:/p/x.wasm", None), Ok(Source::File(PathBuf::from("C:/p/x.wasm"))));
assert!(source("file://", None).is_err());
// Percent-decoded; no remote hosts.
assert_eq!(file_url_path("file:///C:/My%20Plug-ins/Jos%C3%A9%23.wasm"), Some(PathBuf::from("C:/My Plug-ins/José#.wasm")));
assert_eq!(file_url_path("file://localhost/home/x.wasm"), Some(PathBuf::from("/home/x.wasm")));
assert_eq!(file_url_path("file:///C:/My%20Plug-ins/Jos%C3%A9%23.wasm"), Ok(PathBuf::from("C:/My Plug-ins/José#.wasm")));
assert_eq!(file_url_path("file://localhost/home/x.wasm"), Ok(PathBuf::from("/home/x.wasm")));
for no in ["file://server/share/x.wasm", "file:///bad%2", "file:///bad%zz"] {
assert_eq!(file_url_path(no), None, "{no}");
assert!(file_url_path(no).is_err(), "{no}");
}
}
#[test]
fn file_urls_cannot_name_a_network_path() {
for ok in [
("file:///C:/p/x.wasm", "C:/p/x.wasm"),
("file:///c:/p/x.wasm", "c:/p/x.wasm"),
("file:///home/me/x.wasm", "/home/me/x.wasm"),
("file://localhost/C:/p/x.wasm", "C:/p/x.wasm"),
("file:///home/me/a%20b/x.wasm", "/home/me/a b/x.wasm"),
("file:///C:/p//x.wasm", "C:/p//x.wasm"),
] {
assert_eq!(file_url_path(ok.0), Ok(PathBuf::from(ok.1)), "{}", ok.0);
}
for no in [
// Two separators at the start: `//server/share/…`.
"file:////server/share/x.wasm",
"file://localhost//server/share/x.wasm",
"file://///server/share/x.wasm",
"file:////:x.wasm",
// Backslashes, raw or percent-encoded.
"file:///\\\\server\\share\\x.wasm",
"file:///\\server/share/x.wasm",
"file:///C:\\p\\x.wasm",
"file:///%5C%5Cserver/share/x.wasm",
"file:///%5c%5cserver/share/x.wasm",
"file:///C:/p%5Cx.wasm",
// Percent-encoded slashes.
"file:///%2F%2Fserver/share/x.wasm",
"file:///%2fserver/share/x.wasm",
"file:///C:/p%2Fx.wasm",
] {
let e = file_url_path(no).expect_err(no);
assert!(e.starts_with("bad file URL"), "{no}: {e}");
assert!(source(no, None).is_err(), "{no}");
assert!(source(no, Some("file:///tmp/index.json")).is_err(), "{no}");
}
}
+1
View File
@@ -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;
+161
View File
@@ -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"));
}