PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Compose: the effect cache estimate counts the maps the builder makes (strokes' distance fields, none for overlays)
Ian Swopeb0ed288292e8
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rsindex cbdf7f35a..1b077a29b 100644--- a/crates/compose/src/effects.rs+++ b/crates/compose/src/effects.rs@@ -1812,6 +1812,29 @@ pub fn build_maps(layer: &Layer, shape: Vec<f32>, rect: Rect, light: &GlobalLighbuild_maps_prepared(layer, shape, rect, light, tex, &prepared)}+/// How many region-sized arrays [`build_maps_prepared`] builds for `layer` (what+/// [`FxMaps::bytes`] counts): the shape, each effect's maps, and a stroke's distance fields. Kept+/// next to the builder so the effect cache's estimate (#8330) follows it.+pub(crate) fn map_count(layer: &Layer) -> usize {+ let items: Vec<&Effect> = layer.effects.items.iter().filter(|e| e.enabled()).collect();+ let per: usize = items+ .iter()+ .map(|e| match e {+ Effect::DropShadow(_) | Effect::InnerShadow(_) | Effect::OuterGlow(_) | Effect::InnerGlow(_) | Effect::Satin(_) => 1,+ Effect::BevelEmboss(_) => 2,+ _ => 0,+ })+ .sum();+ let out_widths: Vec<f32> =+ items.iter().filter_map(|e| if let Effect::Stroke(st) = e { Some(stroke_widths(st).1) } else { None }).filter(|w| *w > 0.0).collect();+ let has_stroke = items.iter().any(|e| matches!(e, Effect::Stroke(_)));+ let outside = usize::from(out_widths.iter().any(|&w| !stroke_outside_exact(w))) + usize::from(out_widths.iter().any(|&w| stroke_outside_exact(w)));+ let stroke = if has_stroke { 1 + outside } else { 0 };+ let vector_outline =+ if has_stroke && matches!(layer.content, photocraft_doc::LayerContent::Shape(_)) && crate::effect_outline(layer).is_none() { outside } else { 0 };+ 1 + per + stroke + vector_outline+}+pub(crate) fn build_maps_prepared(layer: &Layer,shape: Vec<f32>,diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rsindex 3bf93c4c9..4e5f5cc48 100644--- a/crates/compose/src/lib.rs+++ b/crates/compose/src/lib.rs@@ -2000,12 +2000,12 @@ fn effect_region(layer: &Layer, rect: Rect, cx: &Ctx) -> (Rect, bool) {/// whole region, would take more than the effect cache budget together. A render needs all of/// them, so when they don't fit, building one evicts the next one needed and every render builds/// them all again, seconds even for a small rect (#8330). Counted as [`effects::FxMaps::bytes`]-/// counts them, with only the shape and one map per enabled effect: a lower bound.+/// counts the built maps ([`effects::map_count`] arrays over each region).fn fx_over_budget(layers: &[Layer], canvas: Rect) -> bool {fx_region_bytes(layers, canvas) > effect_cache_budget() as u64}-/// The lower bound [`fx_over_budget`] compares with the budget.+/// The bytes [`fx_over_budget`] compares with the budget.fn fx_region_bytes(layers: &[Layer], canvas: Rect) -> u64 {fn bytes(layers: &[Layer], canvas: Rect, depth: u32) -> u64 {let mut total = 0u64;@@ -2013,7 +2013,7 @@ fn fx_region_bytes(layers: &[Layer], canvas: Rect) -> u64 {if effects::has_effects(l) {let m = effects::margin(l);let region = effect_source_bounds(l, canvas).inflate(m).intersect(&canvas.inflate(m));- let maps = 1 + l.effects.items.iter().filter(|e| e.enabled()).count() as u64;+ let maps = effects::map_count(l) as u64;let px = u64::from(region.width()) * u64::from(region.height());total = total.saturating_add(maps.saturating_mul(px).saturating_mul(4));}diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex 707ea98c8..b0531d374 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -1741,27 +1741,48 @@ fn renders_with_maps_clipped_over_the_budget_match_the_whole_maps() {}#[test]-fn the_effect_map_estimate_is_a_lower_bound() {- // fx_over_budget compares a lower bound of the maps' bytes with the budget: it must not- // exceed what building them takes, or a document that fits would lose its shared maps.- let d = ordinary_fx_doc(SampleType::U8);+fn the_effect_map_estimate_counts_the_maps_building_takes() {+ // fx_over_budget compares the maps' bytes with the budget: too few and a document over it+ // keeps thrashing, too many and one that fits loses its shared maps. Strokes build distance+ // fields and overlays nothing beyond the shape (#8372 review), so count what the builder does.+ let mut d = ordinary_fx_doc(SampleType::U8);+ let overlay = Effect::ColorOverlay { common: FxCommon::new(photocraft_color::BlendMode::Normal, 1.0), color: Color::rgb(0.0, 1.0, 0.0) };+ for fx in [vec![overlay], vec![stroke(2.0, StrokePosition::Outside), stroke(60.0, StrokePosition::Outside)], vec![stroke(4.0, StrokePosition::Inside)]] {+ let mut l = Layer::raster("extra", d.pixel_format());+ l.surface_mut().unwrap().fill_rect(Rect::new(14, 100, 34, 130), &photocraft_raster::from_rgba(&d.pixel_format(), [0.3, 0.6, 0.9, 1.0]));+ l.effects.items = fx;+ d.layers.push(l);+ }let patterns = pattern::PreparedPatterns::new(&d.patterns, pattern::PREPARED_PATTERN_BYTES);let cx = Ctx::for_doc(&d, &patterns);let mut todo = Vec::new();effect_layers(&d.layers, d.bounds(), 0, &mut todo);- let built: usize = todo.iter().map(|l| effect_maps(l, d.bounds(), &cx).bytes()).sum();- let estimate = fx_region_bytes(&d.layers, d.bounds());- assert!(estimate > 0 && estimate <= built as u64, "estimate {estimate} B, built {built} B");- // A 9000 x 6000 document with three full layers, each with a drop shadow (#8330's case):- // about 1.3 GB, over the default 768 MB. One such layer fits.- let mut big = Document::with_background("big", Size::new(9000, 6000), ColorMode::Rgb, SampleType::U8, Color::WHITE);- let mut l = Layer::raster("fill", big.pixel_format());- l.surface_mut().unwrap().fill_rect(big.bounds(), &photocraft_raster::from_rgba(&big.pixel_format(), [0.8, 0.5, 0.3, 1.0]));- l.effects.items = vec![Effect::DropShadow(Shadow { size: 40.0, ..shadow(20.0, 120.0) })];- big.layers.push(l.clone());- assert!(fx_region_bytes(&big.layers, big.bounds()) < DEFAULT_FX_CACHE_BUDGET as u64);- big.layers.extend([l.clone(), l]);- assert!(fx_region_bytes(&big.layers, big.bounds()) > DEFAULT_FX_CACHE_BUDGET as u64);+ assert!(todo.len() > 12);+ for l in &todo {+ let built = effect_maps(l, d.bounds(), &cx);+ let region = effect_region(l, d.bounds(), &cx).0;+ let counted = effects::map_count(l) * region.width() as usize * region.height() as usize * 4;+ assert_eq!(counted, built.bytes(), "layer {}", l.name);+ }+ // 9000 x 6000 documents, every layer full size.+ let big = |fx: Vec<Effect>, n: usize| {+ let mut big = Document::with_background("big", Size::new(9000, 6000), ColorMode::Rgb, SampleType::U8, Color::WHITE);+ let mut l = Layer::raster("fill", big.pixel_format());+ l.surface_mut().unwrap().fill_rect(big.bounds(), &photocraft_raster::from_rgba(&big.pixel_format(), [0.8, 0.5, 0.3, 1.0]));+ l.effects.items = fx;+ big.layers.extend(std::iter::repeat_n(l, n));+ fx_region_bytes(&big.layers, big.bounds())+ };+ let budget = DEFAULT_FX_CACHE_BUDGET as u64;+ // #8330's case: three drop shadows (about 1.3 GB) are over the default 768 MB; one fits.+ let shadow = Effect::DropShadow(Shadow { size: 40.0, ..shadow(20.0, 120.0) });+ assert!(big(vec![shadow.clone()], 1) < budget);+ assert!(big(vec![shadow], 3) > budget);+ // A narrow and a wide stroke: the shape, the inside distance and both outside fields.+ assert!(big(vec![stroke(2.0, StrokePosition::Outside), stroke(60.0, StrokePosition::Outside)], 1) > budget);+ // Colour overlays need the shape only: three fit.+ let overlay = Effect::ColorOverlay { common: FxCommon::new(photocraft_color::BlendMode::Normal, 1.0), color: Color::BLACK };+ assert!(big(vec![overlay], 3) < budget);}// ---------- PSD-fidelity effect semantics (fitted on Photoshop composites) ----------