Files
photocraft/crates/compose/src/shape_split.rs
T
Dylan M. Taylor 6ca5bf359d security(compose): bound dense working sets and nested Blend If evaluation (#8949)
* security(compose): bound dense working sets and repeated group evaluation

Smart-filter mixing and stroke fitting work tile by tile instead of over the
union of sparse surfaces; render_layer admits its rect like render; hidden
layers no longer disable the proxy and uniform-area shortcuts; Blend If
groups nested deeper than two are judged by their result, so nesting costs
polynomial instead of exponential work; effect builds past an absolute byte
cap are skipped.

* CodeRabbit #8949: exact tile skips in filter_mask, allocated-tile scan in fit_stroke, contrasting backdrop in the nested-group test

- filter_mask mix/unmix: bare tiles (no tiles on either side) skip for
  non-alpha formats too when the two surfaces' default pixels are equal;
  differing defaults still get processed, so the clone's default pixel is
  never left wrong under a non-unit mask
- shape_split fit_stroke: scan the union of the three surfaces' allocated
  tiles (exact skip when all three defaults are transparent) instead of
  every tile of a 300k-canvas document; non-transparent defaults fall
  back to the full scan
- tests: the nested-groups assertion now uses a black backdrop so the
  asserted white can only come from rendering through the group chain

fixes CodeRabbit review comments (major/minor) on filter_mask.rs:27,29,
shape_split.rs:126, tests.rs:2354

* compose: skip a BTreeSet in fit_stroke — a sorted Vec compiles under the wasm toolchain (E0599 on ::drain there)
2026-10-23 15:33:01 -04:00

218 lines
9.3 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.
//! A stroked shape layer split into its fill and its vector stroke (rasterised over the canvas),
//! for shapes with clipped layers: Photoshop draws the stroke above the clipped layers. Cached
//! per shape state, so tiles (and the GPU's textures) reuse one rasterisation.
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::{Mutex, OnceLock};
use photocraft_color::{PixelFormat, SampleType};
use photocraft_doc::vector::ShapeLayer;
use photocraft_geom::{Rect, TileCoord};
use photocraft_raster::Surface;
/// (fill, stroke), last use, and the shape's pixels the stroke was fitted to (kept alive so
/// their tile addresses, part of the key, can't be reused).
type Entry = ((Surface, Surface), u64, Option<Surface>);
fn cache() -> &'static Mutex<(HashMap<u64, Entry>, u64)> {
static C: OnceLock<Mutex<(HashMap<u64, Entry>, u64)>> = OnceLock::new();
C.get_or_init(|| Mutex::new((HashMap::new(), 0)))
}
const CAPACITY: usize = 32;
/// Most pixel bytes the split parts may hold (#4530): each entry is two canvas-sized surfaces,
/// so 32 of them at 24 MP could hold several GB outside every memory budget.
#[cfg(not(test))]
const BUDGET: usize = 256 << 20;
/// Small under test, so a few 512² shapes exercise the eviction.
#[cfg(test)]
const BUDGET: usize = 8 << 20;
/// Pixel bytes of one entry's (fill, stroke) parts (the pinned shape pixels are the document's).
fn entry_bytes(parts: &(Surface, Surface)) -> usize {
[&parts.0, &parts.1].iter().map(|s| s.tiles().map(|(_, t)| t.bytes().len()).sum::<usize>()).sum()
}
/// Drop every cached split (Edit › Purge › All), returning the parts' pixel bytes. Callers may
/// still hold returned surfaces, so this is not a measure of freed process memory.
pub fn purge_cache() -> usize {
let mut c = cache().lock().unwrap_or_else(|e| e.into_inner());
let bytes = c.0.values().map(|e| entry_bytes(&e.0)).sum();
c.0.clear();
bytes
}
/// (fill only, stroke only) of a stroked shape, rendered over `canvas` in RGBA at `depth`.
/// `None` without a stroke. Where the shape's pixels
/// (Photoshop's rendering, `sh.cache`) reach past our fill, the part the fill doesn't explain is
/// stroke: the stroke follows Photoshop's coverage (psd-tools double-stroke-effects).
pub fn split(sh: &ShapeLayer, canvas: Rect, depth: SampleType) -> Option<(Surface, Surface)> {
sh.stroke.as_ref()?;
let mut h = std::collections::hash_map::DefaultHasher::new();
(canvas.x0, canvas.y0, canvas.x1, canvas.y1).hash(&mut h);
depth.hash(&mut h);
format!("{:?}{:?}{:?}{:?}", sh.path, sh.fill, sh.stroke, sh.live).hash(&mut h);
(sh.density.to_bits(), sh.feather.to_bits()).hash(&mut h);
if let Some(c) = &sh.cache {
format!("{:?}{:?}", c.format(), c.default_pixel()).hash(&mut h);
for (k, t) in c.tiles() {
(k.tx, k.ty, std::sync::Arc::as_ptr(t) as usize).hash(&mut h);
}
}
let key = h.finish();
{
let mut c = cache().lock().unwrap_or_else(|e| e.into_inner());
c.1 += 1;
let tick = c.1;
if let Some(e) = c.0.get_mut(&key) {
e.1 = tick;
return Some(e.0.clone());
}
}
let fmt = PixelFormat::RGBA8.with_sample(depth);
let fill_only = ShapeLayer { stroke: None, cache: None, ..sh.clone() };
let stroke_only = ShapeLayer { fill: None, cache: None, ..sh.clone() };
let mut parts = (photocraft_vector::render_shape(&fill_only, fmt, canvas), photocraft_vector::render_shape(&stroke_only, fmt, canvas));
if let Some(cache) = &sh.cache {
fit_stroke(&parts.0, &mut parts.1, cache, canvas);
}
let mut c = cache().lock().unwrap_or_else(|e| e.into_inner());
// Least recently used first, until the new entry fits both the count and the byte budget.
let new_bytes = entry_bytes(&parts);
loop {
let held: usize = c.0.values().map(|e| entry_bytes(&e.0)).sum();
if c.0.len() < CAPACITY && held.saturating_add(new_bytes) <= BUDGET {
break;
}
let Some(old) = c.0.iter().min_by_key(|e| (e.1).1).map(|e| *e.0) else { break };
c.0.remove(&old);
}
if new_bytes > BUDGET {
// Larger than the whole budget on its own: use it, don't keep it.
return Some(parts);
}
let tick = c.1;
c.0.insert(key, (parts.clone(), tick, sh.cache.clone()));
Some(parts)
}
/// Fits the stroke part to the shape's own pixels: where they reach past the fill, the stroke
/// covers what the fill doesn't (`(pixels - fill) / (1 - fill)`), in the pixels' colour where we
/// drew no stroke, and elsewhere in the colour that, drawn over the fill, gives back the pixels'
/// colour. Our stroke's own colour would be wrong where the file's fill covers more of the pixel
/// than ours (shapes snapped to whole pixels with a stroke thinner than a pixel): the fitted
/// stroke is then mostly the file's fill, and an edge drawn in the stroke's full colour is far
/// too dark.
fn fit_stroke(fill: &Surface, stroke: &mut Surface, cache: &Surface, canvas: Rect) {
// Where either has pixels (the stroke may reach past the shape's pixels, and vice versa).
let (cb, sb) = (crate::bounds::content_bounds(cache), crate::bounds::content_bounds(stroke));
let r = if cb.is_empty() {
sb
} else if sb.is_empty() {
cb
} else {
cb.union(&sb)
}
.intersect(&canvas);
if r.is_empty() {
return;
}
let float = stroke.format().sample == SampleType::F32;
// Tiles none of the three reaches read as the defaults; with transparent defaults the fit
// leaves the stroke's default pixel there, so those tiles are exact skips and we can scan
// just the union of the three surfaces' allocated tiles instead of every canvas tile (two
// sparse regions on a 300k×300k document would otherwise walk ~1.4M empty tiles). A
// non-transparent default is everywhere, so the scan falls back to the full rectangle.
let whole = r;
let bare_transparent = [fill, &*stroke, cache].iter().all(|s| s.default_pixel().iter().all(|v| *v == 0.0));
if bare_transparent {
let mut coords: Vec<TileCoord> = fill.tiles().map(|(c, _)| *c).chain(stroke.tiles().map(|(c, _)| *c)).chain(cache.tiles().map(|(c, _)| *c)).collect();
coords.sort_unstable();
coords.dedup();
for c in coords {
let r = c.rect().intersect(&whole);
if !r.is_empty() {
fit_stroke_rect(fill, stroke, cache, r, float);
}
}
} else {
for t in whole.tiles() {
let r = t.rect().intersect(&whole);
if !r.is_empty() {
fit_stroke_rect(fill, stroke, cache, r, float);
}
}
}
}
/// [`fit_stroke`] over one rectangle.
fn fit_stroke_rect(fill: &Surface, stroke: &mut Surface, cache: &Surface, r: Rect, float: bool) {
let n = r.width() as usize * r.height() as usize;
let (mut f, mut s, mut c) = (vec![[0.0f32; 4]; n], vec![[0.0f32; 4]; n], vec![[0.0f32; 4]; n]);
fill.read_rgba_into(r, &mut f);
stroke.read_rgba_into(r, &mut s);
cache.read_rgba_into(r, &mut c);
let mut out = Vec::with_capacity(n * 4);
for ((fp, sp), cp) in f.iter().zip(&s).zip(&c) {
let mut p = *sp;
if fp[3] < 1.0 - 1e-3 {
let a = ((cp[3] - fp[3]) / (1.0 - fp[3])).clamp(0.0, 1.0);
p = if sp[3] <= 0.0 {
[cp[0], cp[1], cp[2], a]
} else if a > 1e-3 {
// Normal over the fill: a·p + f·(1 - a)·fill = pixels (premultiplied).
let k = fp[3] * (1.0 - a);
let un = |i: usize| {
let v = (cp[3] * cp[i] - k * fp[i]) / a;
// Float RGB retains HDR values; integer samples use the normalized range.
if float { v } else { v.clamp(0.0, 1.0) }
};
[un(0), un(1), un(2), a]
} else {
[sp[0], sp[1], sp[2], a]
};
}
out.extend_from_slice(&p);
}
stroke.write_region(r, &out);
}
#[cfg(test)]
mod tests {
use super::*;
use photocraft_color::Color;
use photocraft_doc::Fill;
use photocraft_doc::vector::{Path, ShapeStroke, Subpath};
fn shape(x: f64) -> ShapeLayer {
ShapeLayer {
path: Path::new(vec![Subpath::polygon(&[(x, 10.0), (x + 400.0, 10.0), (x + 400.0, 500.0), (x, 500.0)])]),
fill: Some(Fill::Solid(Color::rgb(0.2, 0.4, 0.6))),
stroke: Some(ShapeStroke { width: 6.0, ..ShapeStroke::default() }),
live: None,
cache: None,
psd_raw: None,
..ShapeLayer::default()
}
}
fn held() -> usize {
cache().lock().unwrap_or_else(|e| e.into_inner()).0.values().map(|e| entry_bytes(&e.0)).sum()
}
/// The split cache stays within its byte budget, evicting the least recently used, and
/// Edit › Purge › All empties it (#4530).
#[test]
fn split_cache_keeps_to_its_budget_and_purges() {
let canvas = Rect::new(0, 0, 1024, 512);
for i in 0..6 {
let parts = split(&shape(f64::from(i) * 10.0), canvas, SampleType::U8).expect("stroked");
assert!(entry_bytes(&parts) > 0);
assert!(held() <= BUDGET, "shape {i}: {} bytes held", held());
}
// Other tests share the cache, so only what this test can rely on: a purge frees bytes.
assert!(purge_cache() > 0, "something to purge");
}
}