mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 01:18:43 +00:00
* perf: reuse large buffers so a pinned glibc trim threshold costs no speed (#10479) With MALLOC_TRIM_THRESHOLD_ set (#8743), glibc maps every allocation over 128 KiB fresh, so tiles, compositor bands and filter scratch were page-faulted in on every call: viewport compose 3.6x slower, Gaussian Blur up to 1.8x, .pcraft save 2.4x. - photocraft_raster::scratch: a process-wide, capped pool of large Vec buffers (u8/u16/f32/[f32; 4]). A miss drops the oldest buffers worth its size down to a 256 MiB floor, so peak memory stays near glibc's defaults. The engine releases the pool after any command that closes a document, Headless::close does too, and an idle thread releases it after 2 s unused. - Tiles, compose Buffer/masks/bands, algo Image and the blur paths, engine adjustment/filter scratch, GPU tile upload and io import buffers go through the pool. - .pcraft save reuses one zstd FrameCompressor per worker (byte-identical). - interactive_bench reports minor page faults per run. 36 MP, trim threshold set vs glibc defaults: viewport 5.6 vs 6.1 ms (was 22.7), Blur r4 445 vs 437 ms (was 852), Curves CPU proxy 35.0 vs 35.7 ms (was 50), flatten 146 vs 147 ms (was 237), save .pcraft 251 vs 230 ms (was 545). RSS after closing a 5400x7200 document: 21 MB with trim set. * fix: corpus tests build again; return remaining pooled buffers (#10479 review) - Corpus-gated io tests and the Photoshop-oracle test moved `.px` out of a `Buffer`, which now implements `Drop` (E0509 with `--features corpus`): use `into_px()`. - io: masks, the merged image, extra channels, smart-filter masks, smart-object pixels and patterns give their `interleave` buffers back to the scratch pool after writing them. - compose: recycle the mask values of a pass-through group, `effect_shape` and both `split_parts` callers. - Styled Free Transform preview copies each band out instead of moving it, so the band returns to the pool. - interactive_bench: the fault count covers each whole run, untimed setup included; say so. * fix: recycle the masks of adjustment-atop and shape layers (#10479 review) * fix: recycle effect hiding masks (#10479 review) * fix: recycle knockout, CMYK and GPU shape masks; release the pool when Split/Merge Channels close a document (#10479 review) * fix: release the pool when a closed document's cancelled workers exit; idle-release switch for tests; don't pool PSD planes (#10479 review) * fix: release the pool for workers cancelled before their document closes; take the pool lock for budget and idle-release changes (#10479 review) * fix: idle release rechecks activity under the lock; try_take_exact releases the pool and retries before failing (#10479 review) * test: cover a successful try_take_exact miss; name the failed-reservation test for what it checks (#10479 review) * fix: pool the final GPU effect shape buffer and return it after use (#10479 review) * docs: describe scratch pool lifecycle regression invariants * test: verify polling releases cancelled document scratch buffers * perf: recycle remaining transform selection and PSD scratch buffers * fix: free PSD gather bands before allocating channel planes * test: assert exact scratch take reuses pooled storage
148 lines
6.5 KiB
Rust
148 lines
6.5 KiB
Rust
//! Re-render oracle over the Photoshop corpus (`corpus/photoshop`): our own Photoshop-authored
|
|
//! PSDs from https://github.com/storytold/photocraft-corpus, fetched at a pinned commit by
|
|
//! `cargo xtask corpus --all`. Feature `corpus` (run with `cargo xtask test-corpus`); a missing
|
|
//! corpus fails.
|
|
//!
|
|
//! The io corpus test (`crates/io/tests/it/corpus.rs`) flattens imported documents, which composite
|
|
//! Photoshop's own cached pixels for smart objects and type layers. This test throws those caches
|
|
//! away: every smart object is re-rendered from its embedded source through our smart-filter
|
|
//! stack, and every type layer is re-laid-out and re-rasterised by our text engine; the flatten is
|
|
//! then compared with Photoshop's merged composite. It is the failure map for smart filters and
|
|
//! the text engine. Results are printed per file and per feature group; the pass count must not
|
|
//! fall below the floor (raise it as features land, never lower it). Type re-renders only match
|
|
//! with the corpus fonts installed (see the corpus README), so the floor counts on none of them.
|
|
#![cfg(feature = "corpus")]
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use photocraft_doc::{Document, LayerContent};
|
|
use photocraft_psd::PsdFile;
|
|
|
|
/// Within two 8-bit steps of Photoshop (same tolerance as the io corpus).
|
|
const PASS_TOL: f32 = 2.0 / 255.0;
|
|
/// Re-rendered files that match Photoshop's merged composite (of 86 with smart objects or type).
|
|
const PASS_FLOOR: usize = 5;
|
|
|
|
fn collect(dir: &Path, out: &mut Vec<PathBuf>) {
|
|
let Ok(rd) = std::fs::read_dir(dir) else { return };
|
|
for e in rd.flatten() {
|
|
let p = e.path();
|
|
if p.is_dir() {
|
|
collect(&p, out);
|
|
} else if p.extension().and_then(|e| e.to_str()).is_some_and(|e| e.eq_ignore_ascii_case("psd")) {
|
|
out.push(p);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Premultiplied max difference over pixels visible in either image (1.0 if sizes differ).
|
|
fn max_diff(a: &[[f32; 4]], b: &[[f32; 4]]) -> (f32, f32) {
|
|
if a.len() != b.len() {
|
|
return (1.0, 100.0);
|
|
}
|
|
let (mut m, mut bad) = (0.0f32, 0usize);
|
|
for (p, q) in a.iter().zip(b) {
|
|
let mut pm = 0.0f32;
|
|
for c in 0..4 {
|
|
let (x, y) = if c < 3 { (p[c] * p[3], q[c] * q[3]) } else { (p[c], q[c]) };
|
|
pm = pm.max((x - y).abs());
|
|
}
|
|
if pm > PASS_TOL {
|
|
bad += 1;
|
|
}
|
|
m = m.max(pm);
|
|
}
|
|
(m, 100.0 * bad as f32 / a.len().max(1) as f32)
|
|
}
|
|
|
|
/// Re-renders every smart object and type layer of `doc`; returns how many were re-rendered.
|
|
fn rerender(doc: &mut Document) -> Result<usize, String> {
|
|
let snapshot = doc.clone();
|
|
let ids: Vec<_> = snapshot.walk().into_iter().map(|(_, _, l)| l.id).collect();
|
|
let mut n = 0;
|
|
for id in ids {
|
|
let Some(l) = doc.layer_mut(id) else { continue };
|
|
match &mut l.content {
|
|
LayerContent::Smart(_) => {
|
|
if photocraft_engine::smart_cmds::refresh_layer(&snapshot, l).map_err(|e| e.to_string())? {
|
|
n += 1;
|
|
} else {
|
|
return Err("smart object source unavailable".into());
|
|
}
|
|
}
|
|
LayerContent::Text(t) => {
|
|
photocraft_engine::type_cmds::refresh(&snapshot, t);
|
|
n += 1;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
Ok(n)
|
|
}
|
|
|
|
/// Re-rendering Photoshop's Screen/Motion Blur oracle matches its merged composite, so the result retains the input's coverage.
|
|
#[test]
|
|
fn screen_motion_blur_retains_input_coverage() {
|
|
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop/smart-filters/sf-blend-screen-100.psd");
|
|
let bytes = std::fs::read(&path).expect("Screen/Motion Blur oracle missing: run cargo xtask corpus --all");
|
|
let file = PsdFile::from_bytes(&bytes).expect("parse Screen/Motion Blur oracle");
|
|
let mut doc = photocraft_io::import("sf-blend-screen-100.psd", &bytes).expect("import Screen/Motion Blur oracle").document;
|
|
assert!(rerender(&mut doc).expect("re-render Screen/Motion Blur oracle") > 0);
|
|
let merged = photocraft_io::merged_composite(&file).expect("Photoshop merged composite");
|
|
let (error, bad) = max_diff(&photocraft_compose::flatten(&doc).px, &merged);
|
|
assert!(error <= PASS_TOL, "Screen/Motion Blur error {error}, {bad}% pixels exceed 2/255");
|
|
}
|
|
|
|
/// Re-render every Photoshop oracle in the corpus with PhotoCraft's filters and compare it with Photoshop's own merged composite.
|
|
#[test]
|
|
fn photoshop_oracles_rerendered() {
|
|
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop");
|
|
assert!(root.is_dir(), "{} is missing: run `cargo xtask corpus --all`", root.display());
|
|
let mut files = Vec::new();
|
|
collect(&root, &mut files);
|
|
files.sort();
|
|
let mut groups: std::collections::BTreeMap<String, (usize, usize)> = Default::default();
|
|
let (mut pass, mut total, mut errors) = (0, 0, 0);
|
|
eprintln!("{:<60} {:>7} {:>9} {:>8} status", "file (re-rendered)", "layers", "max_err", "bad_px%");
|
|
for p in &files {
|
|
let name = p.strip_prefix(&root).unwrap_or(p).display().to_string();
|
|
let bytes = std::fs::read(p).unwrap_or_default();
|
|
let Ok(file) = PsdFile::from_bytes(&bytes) else { continue };
|
|
let Ok(imp) = photocraft_io::import(&name, &bytes) else {
|
|
eprintln!("{name:<60} IMPORT-ERROR");
|
|
errors += 1;
|
|
continue;
|
|
};
|
|
let mut doc = imp.document;
|
|
let n = match rerender(&mut doc) {
|
|
Ok(0) => continue,
|
|
Ok(n) => n,
|
|
Err(e) => {
|
|
eprintln!("{name:<60} RERENDER-ERROR {e}");
|
|
errors += 1;
|
|
continue;
|
|
}
|
|
};
|
|
let Ok(merged) = photocraft_io::merged_composite(&file) else { continue };
|
|
let ours = photocraft_compose::flatten(&doc).into_px();
|
|
let (m, bad) = max_diff(&ours, &merged);
|
|
let group = name.split(['/', '\\']).next().unwrap_or(".").to_string();
|
|
let g = groups.entry(group).or_default();
|
|
g.1 += 1;
|
|
total += 1;
|
|
let status = if m <= PASS_TOL {
|
|
pass += 1;
|
|
g.0 += 1;
|
|
"PASS"
|
|
} else {
|
|
"DIFF"
|
|
};
|
|
eprintln!("{name:<60} {n:>7} {m:>9.4} {bad:>7.2}% {status}");
|
|
}
|
|
eprintln!("photoshop re-render: {total} files with smart objects or type: {pass} pass (<= 2/255), {errors} errors");
|
|
for (g, (p, n)) in &groups {
|
|
eprintln!("photoshop re-render: {g:<16} {p:>4} / {n:<4} pass");
|
|
}
|
|
assert!(pass >= PASS_FLOOR, "re-render oracle pass count {pass} fell below the floor {PASS_FLOOR}");
|
|
}
|