Files
photocraft/crates/io/tests/it/banded.rs
T
Khawar Zamman Wani 86ddfb72f7 Reuse large buffers so returning freed memory to the OS doesn't slow filters (#10479) (#10514)
* 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
2026-11-02 09:29:59 +01:00

107 lines
5.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Exports that render the composite in bands (issue #49): results must equal the one-shot
//! composite, across band boundaries, depths and alpha.
use photocraft_color::{ColorMode, PixelFormat, SampleType};
use photocraft_doc::{Document, Layer, LayerContent};
use photocraft_geom::{Rect, Size};
use photocraft_io::*;
use photocraft_raster::Surface;
/// Two layers over more than one export band (a band is ~8 MP), with a gradient, so every row
/// differs; `alpha` < 1 leaves the top-right translucent.
fn layered(w: u32, h: u32, depth: SampleType, alpha: f32) -> Document {
let mut d = Document::new("b", Size::new(w, h), ColorMode::Rgb, depth);
let fmt = d.pixel_format();
let mut bg = Surface::new(fmt);
let mut row = Vec::with_capacity(w as usize * 4);
for y in 0..h as i32 {
row.clear();
for x in 0..w as i32 {
let a = if x > w as i32 / 2 && y < h as i32 / 2 { alpha } else { 1.0 };
row.extend([x as f32 / w as f32, y as f32 / h as f32, 0.5, a]);
}
bg.write_region(Rect::new(0, y, w as i32, y + 1), &row);
}
d.layers.push(Layer::new("bg", LayerContent::Raster(bg)));
let mut top = Layer::raster("top", fmt);
top.surface_mut().unwrap().fill_rect(Rect::new(10, 10, w as i32 - 10, 40), &[1.0, 0.0, 0.0, 1.0]);
top.opacity = 0.5;
d.layers.push(top);
d
}
fn composite_q(doc: &Document, scale: f32) -> Vec<f32> {
photocraft_compose::flatten(doc).px.iter().flat_map(|p| [p[0], p[1], p[2], p[3]]).map(|v| (v.clamp(0.0, 1.0) * scale).round()).collect()
}
#[test]
fn layered_png_export_spans_bands() {
// 3000 × 3000 = 9 MP: two bands.
for (depth, scale) in [(SampleType::U8, 255.0), (SampleType::U16, 65535.0)] {
for alpha in [1.0, 0.5] {
let d = layered(3000, 3000, depth, alpha);
let r = export(&d, "x.png", &ExportOptions::default()).unwrap();
let img = photocraft_codecs::decode(&r.bytes).unwrap();
assert_eq!(img.layout().has_alpha(), alpha < 1.0, "{depth:?} alpha {alpha}");
let got: Vec<f32> = img.to_rgba_f32().iter().map(|v| (v * scale).round()).collect();
assert!(got == composite_q(&d, scale), "{depth:?} alpha {alpha}: pixels differ");
}
}
}
#[test]
fn single_layer_native_export_strips_opaque_alpha() {
for alpha in [1.0, 0.25] {
let mut d = layered(700, 300, SampleType::U16, alpha);
d.layers.truncate(1);
let r = export(&d, "x.tiff", &ExportOptions::default()).unwrap();
let back = import("x.tiff", &r.bytes).unwrap().document;
let (a, b) = (d.layers[0].surface().unwrap(), back.layers[0].surface().unwrap());
assert_eq!(a.read_region(d.bounds()), b.read_region(d.bounds()), "alpha {alpha}");
let img = photocraft_codecs::decode(&r.bytes).unwrap();
assert_eq!(img.layout().has_alpha(), alpha < 1.0);
}
let mut d = layered(8, 4, SampleType::U16, 1.0);
d.layers.truncate(1);
// A fractional edge exercises coverage rather than merely cropping raw samples.
d.layers[0].vector_mask = Some(photocraft_doc::VectorMask::new(photocraft_vector::shapes::rect(0.0, 0.0, 4.5, 4.0)));
let r = export(&d, "x.png", &ExportOptions::default()).unwrap();
let img = photocraft_codecs::decode(&r.bytes).unwrap();
assert_eq!(img.sample_type(), photocraft_codecs::SampleType::U16);
assert!(img.layout().has_alpha());
let got: Vec<f32> = img.to_rgba_f32().iter().map(|v| (v * 65535.0).round()).collect();
assert_eq!(got, composite_q(&d, 65535.0), "active vector mask: pixels differ");
}
#[test]
fn psd_merged_image_matches_composite_and_drops_near_opaque_alpha() {
// Alpha 0.999 rounds to 255: no alpha channel, so the merged image must not be matted.
for alpha in [1.0, 0.999, 0.5] {
let d = layered(400, 300, SampleType::U8, alpha);
let file = document_to_psd(&d);
assert_eq!(file.merged_has_alpha(), alpha < 0.99, "alpha {alpha}");
let merged = merged_composite(&file).unwrap();
let want = photocraft_compose::flatten(&d).into_px();
let m = merged.iter().zip(&want).flat_map(|(a, b)| (0..4).map(move |i| (a[i] - b[i]).abs())).fold(0.0f32, f32::max);
// Un-matting divides the 8-bit rounding error by alpha.
assert!(m <= 1.0 / (255.0 * alpha) + 1e-4, "alpha {alpha}: max diff {m}");
}
}
#[test]
fn flat_import_converts_rgb_in_bands() {
// An opaque RGB file becomes an RGBA document (converted in ~32 MB bands: 36 MB here).
let (w, h) = (4000u32, 3000u32);
let px: Vec<u8> = (0..w * h * 3).map(|i| (i % 251) as u8).collect();
let img = photocraft_codecs::Image::from_u8(w, h, photocraft_codecs::ChannelLayout::Rgb, px.clone()).unwrap();
let bytes = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap();
let doc = import("x.png", &bytes).unwrap().document;
let s = doc.layers[0].surface().unwrap();
assert_eq!(s.format(), PixelFormat::RGBA8);
let back = s.to_interleaved(doc.bounds());
let rgb: Vec<u8> = back.as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[1], p[2]]).collect();
assert_eq!(rgb, px);
assert!(back.as_chunks::<4>().0.iter().all(|p| p[3] == 255));
}