From 1fdc8a0fc0f99c4074bb0426d11f2071085d3288 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Sun, 15 Nov 2026 20:21:13 -0800 Subject: [PATCH] Propagate compositor allocation errors through effect shapes --- crates/compose/src/composite.rs | 6 +- crates/compose/src/content.rs | 15 +---- crates/compose/src/ctx.rs | 37 +++++++++--- crates/compose/src/extents.rs | 22 ++++++-- crates/compose/src/fx_cache.rs | 63 ++++++++++++++++----- crates/compose/src/gradient_fill.rs | 7 ++- crates/compose/src/pattern.rs | 6 +- crates/compose/src/render.rs | 88 ++++++++++++++++++++++++++--- 8 files changed, 186 insertions(+), 58 deletions(-) diff --git a/crates/compose/src/composite.rs b/crates/compose/src/composite.rs index 7e42a6455a..73e54d8911 100644 --- a/crates/compose/src/composite.rs +++ b/crates/compose/src/composite.rs @@ -1,7 +1,7 @@ use super::content::{channel_weights, empty_in, render_content_with, restore_channels, surface_to_buffer, surface_to_buffer_with}; use super::ctx::Ctx; use super::extents::{effects_frame, mask_k, mask_vals, recycle_mask, shapeless_stroke_bounds}; -use super::fx_cache::effect_maps; +use super::fx_cache::effect_maps_with; use super::{ BlendMode, Buffer, BufferAlloc, Fill, InfallibleBufferAlloc, Layer, LayerContent, Rect, Surface, advanced, blend, effects, psblend, shape_split, @@ -582,7 +582,7 @@ fn composite_layer_effects_with(layer: &Layer, clipped: &[Layer] }; advanced::composite_clipped_with(clipped, &mut content, cx, alloc)?; let vstroke = parts.as_ref().map(|(s, m)| effects::VectorStroke { stroke: s, mask: m.as_deref() }); - let maps = effect_maps(layer, rect, cx); + let maps = effect_maps_with(layer, rect, cx, alloc)?; effects::composite_with_effects_prepared_with(layer, &content, backdrop, &maps, effects_frame(layer, cx.canvas), cx.patterns, vstroke, alloc)?; if let Some((_, mask)) = parts { recycle_mask(mask); @@ -713,7 +713,7 @@ fn composite_atop_any_with(layer: &Layer, base: &mut Buffer, cx: let vstroke = parts.as_ref().map(|(s, m)| effects::VectorStroke { stroke: s, mask: m.as_deref() }); let mut opaque = alloc.clone(base)?; for p in &mut opaque.px { p[3] = 1.0; } - let maps = effect_maps(layer, rect, cx); + let maps = effect_maps_with(layer, rect, cx, alloc)?; effects::composite_with_effects_prepared_with(layer, &content, &mut opaque, &maps, effects_frame(layer, cx.canvas), cx.patterns, vstroke, alloc)?; if let Some((_, mask)) = parts { recycle_mask(mask); diff --git a/crates/compose/src/content.rs b/crates/compose/src/content.rs index 972fe4f943..a77551b182 100644 --- a/crates/compose/src/content.rs +++ b/crates/compose/src/content.rs @@ -192,18 +192,9 @@ pub fn render_fill_content(layer: &Layer, f: &Fill, canvas: Rect, patterns: &[Pa /// `quantum`: the document depth's levels, which a dithered gradient is rounded to /// ([`gradient_fill::render_quantized`]); `None` keeps it in float. pub(crate) fn render_fill(f: &Fill, rect: Rect, canvas: Rect, patterns: &pattern::PreparedPatterns<'_>, quantum: Option) -> Buffer { - match f { - Fill::Solid(c) => { - let rgb = c.to_rgb(); - Buffer::filled(rect, [rgb[0], rgb[1], rgb[2], c.alpha]) - } - // Gradient geometry relative to the layer's frame, independent of the render rect. - Fill::Gradient { .. } => Buffer { rect, px: gradient_fill::render_quantized(f, rect, canvas, quantum) }, - // Laid out from the layer's frame when linked; transparent if the pattern is missing. - Fill::Pattern { name, scale, id, angle, link, phase } => match patterns.get(id, name) { - Some(tile) => Buffer { rect, px: pattern::render(&tile, &pattern::Placement::new(canvas, *link, *phase, *scale, *angle), rect) }, - None => Buffer::transparent(rect), - }, + match render_fill_with(f, rect, canvas, patterns, quantum, &InfallibleBufferAlloc) { + Ok(buffer) => buffer, + Err(never) => match never {}, } } diff --git a/crates/compose/src/ctx.rs b/crates/compose/src/ctx.rs index f54c79865b..db3b4fcd93 100644 --- a/crates/compose/src/ctx.rs +++ b/crates/compose/src/ctx.rs @@ -1,6 +1,6 @@ use super::content::empty_in; -use super::fx_cache::{effect_maps, fx_over_budget, layer_identity}; -use super::{Buffer, Document, Layer, LayerContent, Rect, adjust, advanced, effects, pattern}; +use super::fx_cache::{effect_maps_with, fx_over_budget, layer_identity}; +use super::{Buffer, BufferAlloc, Document, InfallibleBufferAlloc, Layer, LayerContent, Rect, adjust, advanced, effects, pattern}; type VectorMaskSlot = std::sync::Arc>; @@ -183,22 +183,41 @@ pub(crate) const PREPARE_DEPTH: u32 = 64; /// `rect` before tiles render in parallel, layers concurrently on native targets, each build /// inside `setting` (the tiles' thread-local colour setting). Tiles then only read finished maps: /// none builds one while others need it (#276), and none duplicates another's build. Builds never -/// wait on each other (`cached_effect_maps`), so building them in parallel can't deadlock. +/// wait on each other (`cached_effect_maps_with`), so building them in parallel can't deadlock. pub(crate) fn prepare_effects(layers: &[Layer], rect: Rect, cx: &Ctx, setting: impl Fn(&mut dyn FnMut()) + Sync) { + let _ = prepare_effects_with(layers, rect, cx, &InfallibleBufferAlloc, |build| { + let mut infallible = || match build() { + Ok(()) => (), + Err(never) => match never {}, + }; + setting(&mut infallible); + Ok(()) + }); +} + +/// Fallible counterpart of [`prepare_effects`], used by the compositor before it starts tiles. +pub(crate) fn prepare_effects_with( + layers: &[Layer], + rect: Rect, + cx: &Ctx, + alloc: &A, + setting: F, +) -> Result<(), A::Error> +where + A: BufferAlloc + Sync, + F: Fn(&mut dyn FnMut() -> Result<(), A::Error>) -> Result<(), A::Error> + Sync, +{ let mut todo = Vec::new(); effect_layers(layers, rect, 0, &mut todo); let build = |l: &std::borrow::Cow| { - setting(&mut || { - let _ = effect_maps(l, rect, cx); - }); + setting(&mut || effect_maps_with(l, rect, cx, alloc).map(|_| ())) }; #[cfg(not(target_arch = "wasm32"))] if todo.len() > 1 { use rayon::prelude::*; - todo.par_iter().for_each(build); - return; + return todo.par_iter().try_for_each(build); } - todo.iter().for_each(build); + todo.iter().try_for_each(build) } /// The visible layers of `layers` (groups included) with effects that can reach `rect`. diff --git a/crates/compose/src/extents.rs b/crates/compose/src/extents.rs index d111ab8b57..c9cf0d7fad 100644 --- a/crates/compose/src/extents.rs +++ b/crates/compose/src/extents.rs @@ -1,6 +1,6 @@ -use super::content::{render_content, surface_to_buffer}; +use super::content::{render_content_with, surface_to_buffer_with}; use super::ctx::Ctx; -use super::{BlendMode, Document, Layer, LayerContent, Rect, adjustment_quantum, advanced, bounds, effects, masks, shape_split}; +use super::{BlendMode, BufferAlloc, Document, InfallibleBufferAlloc, Layer, LayerContent, Rect, adjustment_quantum, advanced, bounds, effects, masks, shape_split}; /// Bounds of a layer's own pixels (union over group children; the canvas /// for fill layers; an artboard's board). @@ -114,17 +114,27 @@ pub fn effect_outline(layer: &Layer) -> Option<&photocraft_doc::vector::Path> { /// fill is opaque (it can differ from ours by a fraction of a pixel: psd-tools shape-fx2), the /// path where the fill fades out (psd-tools stroke-effects). pub(crate) fn effect_shape(layer: &Layer, rect: Rect, cx: &Ctx) -> Vec { + match effect_shape_with(layer, rect, cx, &InfallibleBufferAlloc) { + Ok(shape) => shape, + Err(never) => match never {}, + } +} + +pub(crate) fn effect_shape_with(layer: &Layer, rect: Rect, cx: &Ctx, alloc: &A) -> Result, A::Error> { let n = rect.width() as usize * rect.height() as usize; // Transparency Shapes Layer off: the effects take the whole layer as their shape (its masks // still apply). if !layer.advanced.transparency_shapes { - return mask_vals(layer, rect, cx).unwrap_or_else(|| vec![1.0; n]); + return Ok(mask_vals(layer, rect, cx).unwrap_or_else(|| vec![1.0; n])); } let Some(path) = effect_outline(layer).filter(|_| n > 0) else { - return render_content(layer, rect, cx).map(|b| b.px.iter().map(|p| p[3]).collect()).unwrap_or_else(|| vec![0.0; n]); + return Ok(render_content_with(layer, rect, cx, alloc)?.map(|b| b.px.iter().map(|p| p[3]).collect()).unwrap_or_else(|| vec![0.0; n])); }; // Unmasked: the masks scale the shape, they aren't the fill's transparency. - let a: Vec = layer.surface().map(|s| surface_to_buffer(s, rect).px.iter().map(|p| p[3]).collect()).unwrap_or_else(|| vec![0.0; n]); + let a: Vec = match layer.surface() { + Some(s) => surface_to_buffer_with(s, rect, alloc)?.px.iter().map(|p| p[3]).collect(), + None => vec![0.0; n], + }; let cov = photocraft_vector::path_coverage(path, rect); let mv = mask_vals(layer, rect, cx); let (w, h) = (rect.width() as usize, rect.height() as usize); @@ -148,7 +158,7 @@ pub(crate) fn effect_shape(layer: &Layer, rect: Rect, cx: &Ctx) -> Vec { } } recycle_mask(mv); - out + Ok(out) } /// The frame layer-effect gradients and linked patterns are laid out in, when it isn't the diff --git a/crates/compose/src/fx_cache.rs b/crates/compose/src/fx_cache.rs index 02dc8b21cc..9d6eb33c07 100644 --- a/crates/compose/src/fx_cache.rs +++ b/crates/compose/src/fx_cache.rs @@ -1,7 +1,7 @@ use super::composite::texture_ctx; use super::ctx::{Ctx, PREPARE_DEPTH}; -use super::extents::{effect_shape, effect_source_bounds}; -use super::{Layer, LayerContent, Rect, Surface, advanced, effects}; +use super::extents::{effect_shape_with, effect_source_bounds}; +use super::{BufferAlloc, InfallibleBufferAlloc, Layer, LayerContent, Rect, Surface, advanced, effects}; // --------------------------------------------------------------------------------------------- // Layer-effect map cache @@ -207,6 +207,18 @@ const MAX_FX_BUILD_BYTES: u64 = if cfg!(target_pointer_width = "64") { 4 << 30 } /// canvas grown likewise; see [`effect_region`]), built once per layer state and shared by every /// tile. `rect` is what the caller composites the layer over (see [`effect_region`]). pub(crate) fn effect_maps(layer: &Layer, rect: Rect, cx: &Ctx) -> std::sync::Arc { + match effect_maps_with(layer, rect, cx, &InfallibleBufferAlloc) { + Ok(maps) => maps, + Err(never) => match never {}, + } +} + +pub(crate) fn effect_maps_with( + layer: &Layer, + rect: Rect, + cx: &Ctx, + alloc: &A, +) -> Result, A::Error> { use std::hash::{Hash, Hasher}; let (mut region, clipped) = effect_region(layer, rect, cx); // Non-local effects keep their whole source region, which a few distant pixels stretch over @@ -217,8 +229,8 @@ pub(crate) fn effect_maps(layer: &Layer, rect: Rect, cx: &Ctx) -> std::sync::Arc } static NO_CACHE: std::sync::OnceLock = std::sync::OnceLock::new(); if *NO_CACHE.get_or_init(|| std::env::var_os("PHOTOCRAFT_FX_NOCACHE").is_some()) { - let shape = if region.is_empty() { Vec::new() } else { effect_shape(layer, region, cx) }; - return std::sync::Arc::new(effects::build_maps_prepared(layer, shape, region, &cx.light, &texture_ctx(layer, region, cx), cx.patterns)); + let shape = if region.is_empty() { Vec::new() } else { effect_shape_with(layer, region, cx, alloc)? }; + return Ok(std::sync::Arc::new(effects::build_maps_prepared(layer, shape, region, &cx.light, &texture_ctx(layer, region, cx), cx.patterns))); } let mut h = std::collections::hash_map::DefaultHasher::new(); cx.layer_keys.identity(layer, cx.doc).hash(&mut h); @@ -227,15 +239,22 @@ pub(crate) fn effect_maps(layer: &Layer, rect: Rect, cx: &Ctx) -> std::sync::Arc let key = h.finish(); // Prepared for this render (see `prepare_effects`): no global lookup, no build. if let Some((m, _)) = cx.fx_maps.lock().unwrap_or_else(|e| e.into_inner()).get(&key) { - return m.clone(); + return Ok(m.clone()); } - let maps = cached_effect_maps(layer, region, key, cx); - cx.fx_maps.lock().unwrap_or_else(|e| e.into_inner()).entry(key).or_insert((maps, clipped)).0.clone() + let maps = cached_effect_maps_with(layer, region, key, cx, alloc)?; + Ok(cx.fx_maps.lock().unwrap_or_else(|e| e.into_inner()).entry(key).or_insert((maps, clipped)).0.clone()) } -fn build_fx_slot_maps(layer: &Layer, region: Rect, key: u64, cx: &Ctx, slot: &FxSlot) -> std::sync::Arc { +fn build_fx_slot_maps_with( + layer: &Layer, + region: Rect, + key: u64, + cx: &Ctx, + slot: &FxSlot, + alloc: &A, +) -> Result, A::Error> { let generation = photocraft_raster::spill::read_error_generation(); - let shape: Vec = if region.is_empty() { Vec::new() } else { effect_shape(layer, region, cx) }; + let shape: Vec = if region.is_empty() { Vec::new() } else { effect_shape_with(layer, region, cx, alloc)? }; let maps = effects::build_maps_prepared(layer, shape, region, &cx.light, &texture_ctx(layer, region, cx), cx.patterns); let mut pin = Vec::new(); let mut readable = true; @@ -248,7 +267,7 @@ fn build_fx_slot_maps(layer: &Layer, region: Rect, key: u64, cx: &Ctx, slot: &Fx let maps = std::sync::Arc::new(maps); if !readable || photocraft_raster::spill::check_since(generation).is_err() { // Built from a scratch placeholder: use it for this frame, never cache it. - return maps; + return Ok(maps); } if slot.set(FxEntry { maps: maps.clone(), _pin: pin, bytes }).is_ok() { // Counted exactly once, by the build that filled the slot, and only while the @@ -259,7 +278,7 @@ fn build_fx_slot_maps(layer: &Layer, region: Rect, key: u64, cx: &Ctx, slot: &Fx } } // A concurrent build that finished first wins, so every tile shares one map. - slot.get().map_or(maps, |e| e.maps.clone()) + Ok(slot.get().map_or(maps, |e| e.maps.clone())) } fn evict_fx_cache_over_budget(key: u64) { @@ -291,7 +310,7 @@ fn evict_fx_cache_over_budget(key: u64) { /// the same layer; had it blocked on a once-init held further up its own stack, it would deadlock. /// Instead a miss builds the maps here and the first finished build is kept. Renders build every /// layer's maps before their parallel tiles (`prepare_effects`), so tiles rarely miss. -fn cached_effect_maps(layer: &Layer, region: Rect, key: u64, cx: &Ctx) -> std::sync::Arc { +fn cached_effect_maps_with(layer: &Layer, region: Rect, key: u64, cx: &Ctx, alloc: &A) -> Result, A::Error> { let slot = { let mut c = fx_cache().lock().unwrap_or_else(|e| e.into_inner()); if let Some(s) = c.map.get(&key) { @@ -305,9 +324,25 @@ fn cached_effect_maps(layer: &Layer, region: Rect, key: u64, cx: &Ctx) -> std::s }; let maps = match slot.get() { Some(e) => e.maps.clone(), - None => build_fx_slot_maps(layer, region, key, cx, &slot), + None => match build_fx_slot_maps_with(layer, region, key, cx, &slot, alloc) { + Ok(maps) => maps, + Err(error) => { + // Allocation failures never publish a partial cache entry. If another renderer + // finished the same slot while this one failed, use that complete result. + let mut c = fx_cache().lock().unwrap_or_else(|e| e.into_inner()); + if let Some(entry) = slot.get() { + entry.maps.clone() + } else { + if c.map.get(&key).is_some_and(|current| std::sync::Arc::ptr_eq(current, &slot)) { + c.map.remove(&key); + c.order.retain(|candidate| *candidate != key); + } + return Err(error); + } + } + }, }; // Evict the oldest entries over budget (never the one just used). evict_fx_cache_over_budget(key); - maps + Ok(maps) } diff --git a/crates/compose/src/gradient_fill.rs b/crates/compose/src/gradient_fill.rs index 995d420b6e..82274747b5 100644 --- a/crates/compose/src/gradient_fill.rs +++ b/crates/compose/src/gradient_fill.rs @@ -137,7 +137,6 @@ pub fn render_quantized(f: &Fill, rect: Rect, frame: Rect, quantum: Option) /// Render into caller-owned pixels so compositor paths can use fallible pooled storage. pub(crate) fn render_quantized_into(f: &Fill, rect: Rect, frame: Rect, quantum: Option, px: &mut [[f32; 4]]) { - px.fill([0.0; 4]); render_ramp_into(f, rect, frame, Ramp::new(f), quantum, px); } @@ -168,8 +167,10 @@ fn render_ramp(f: &Fill, rect: Rect, frame: Rect, ramp: Option(f: &Fill, rect: Rect, frame: Rect, ramp: Option>, quantum: Option, px: &mut [[f32; N]]) { - px.fill([0.0; N]); - let (Some(ramp), Fill::Gradient { angle, scale, style, reverse, offset, dither: dith, .. }) = (ramp, f) else { return }; + let (Some(ramp), Fill::Gradient { angle, scale, style, reverse, offset, dither: dith, .. }) = (ramp, f) else { + px.fill([0.0; N]); + return; + }; let w = rect.width() as usize; if w == 0 { return; diff --git a/crates/compose/src/pattern.rs b/crates/compose/src/pattern.rs index c30db7f718..8a26fddcb4 100644 --- a/crates/compose/src/pattern.rs +++ b/crates/compose/src/pattern.rs @@ -456,8 +456,10 @@ pub fn render(tile: &Tile, place: &Placement, rect: Rect) -> Vec<[f32; 4]> { /// Render into caller-owned storage for fallible compositor allocations. pub(crate) fn render_into(tile: &Tile, place: &Placement, rect: Rect, out: &mut Vec<[f32; 4]>) { let len = rect.width() as usize * rect.height() as usize; - out.clear(); - out.resize(len, [0.0; 4]); + if out.len() != len { + out.clear(); + out.resize(len, [0.0; 4]); + } let mut i = 0; for y in rect.y0..rect.y1 { for x in rect.x0..rect.x1 { diff --git a/crates/compose/src/render.rs b/crates/compose/src/render.rs index 9d277c7bee..b0f658c596 100644 --- a/crates/compose/src/render.rs +++ b/crates/compose/src/render.rs @@ -1,6 +1,6 @@ use super::composite::{composite_stack_at, composite_stack_at_with}; use super::content::RENDER_TILE; -use super::ctx::{Ctx, RenderVectorMasks, prepare_effects, prepare_tones}; +use super::ctx::{Ctx, RenderVectorMasks, prepare_effects_with, prepare_tones}; use super::{ Buffer, BufferAlloc, Document, FallibleBufferAlloc, InfallibleBufferAlloc, Layer, Rect, Surface, adjust, advanced, cmyk, multichannel, native, native_lab, pattern, psblend, @@ -80,9 +80,9 @@ fn render_tiled_with_alloc(doc: &Document, rect: Rect, ti }); } // Effect maps are built once, here, before any tile needs them (#276). - prepare_effects(&doc.layers, rect, cx, |f| { - photocraft_color::convert::with_cmyk_space(cmyk, || in_blend_space(lab, hdr, f)); - }); + prepare_effects_with(&doc.layers, rect, cx, alloc, |f| { + photocraft_color::convert::with_cmyk_space(cmyk, || in_blend_space(lab, hdr, f)) + })?; let run = |t: Rect| { photocraft_color::convert::with_cmyk_space(cmyk, || -> Result<_, A::Error> { let mut b = multichannel::backdrop_with(doc, t, alloc)?; @@ -174,8 +174,9 @@ pub fn cmyk_space(doc: &Document) -> Option { /// The compositor could not reserve or size one of its RGBA buffers. Earlier complete bands @@ -224,6 +225,21 @@ mod fallible_bands_tests { doc } + fn outlined_effect_document() -> Document { + let mut doc = Document::new("fallible outlined effect", Size::new(512, 256), ColorMode::Rgb, SampleType::U8); + let path = photocraft_doc::vector::Path { + subpaths: vec![photocraft_doc::vector::Subpath::polygon(&[(12.0, 12.0), (60.0, 12.0), (60.0, 60.0), (12.0, 60.0)])], + ..Default::default() + }; + let mut cache = photocraft_doc::Surface::new(photocraft_color::PixelFormat::RGBA8); + cache.fill_rect(Rect::new(12, 12, 60, 60), &[0.0, 0.0, 0.0, 1.0]); + let shape = photocraft_doc::vector::ShapeLayer { path, fill: Some(Fill::Solid(Color::BLACK)), cache: Some(cache), ..Default::default() }; + let mut layer = Layer::new("outlined shape", LayerContent::Shape(shape)); + layer.effects.items.push(photocraft_doc::Effect::default_drop_shadow()); + doc.layers.push(layer); + doc + } + #[test] fn fail_nth_buffer_constructor_propagates_through_nested_rendering() { let doc = nested_document(); @@ -252,13 +268,67 @@ mod fallible_bands_tests { } #[test] - fn tiled_renderer_returns_buffer_allocation_error_before_delivering_a_band() { + fn render_bands_with_alloc_reports_outer_and_tile_allocations_before_the_sink() { let doc = Document::new("fallible tile", Size::new(512, 256), ColorMode::Rgb, SampleType::U8); let patterns = pattern::PreparedPatterns::new(&doc.patterns, pattern::PREPARED_PATTERN_BYTES).in_mode(doc.pixel_format().mode); let ctx = Ctx::for_doc(&doc, &patterns); + for fail_at in [1, 2] { + let alloc = FailBufferAt { fail_at, attempts: AtomicUsize::new(0) }; + let mut delivered = 0; + let result = render_bands_with_alloc(&doc, doc.bounds(), RENDER_TILE, &alloc, &mut |_| { + delivered += 1; + Ok::<(), ()>(()) + }); + assert!(matches!(result, Err(RenderBandsCoreError::Allocation(error)) if error == fail_at)); + assert_eq!(delivered, 0, "failed output or tile allocation must not deliver a partial band"); + if fail_at == 1 { + assert_eq!(alloc.attempts.load(Ordering::Relaxed), 1, "the output allocation is sequential"); + } else { + assert!(alloc.attempts.load(Ordering::Relaxed) >= 2, "parallel tile allocation may have other in-flight attempts"); + } + } + } + + #[test] + fn effect_shape_buffer_failure_is_not_cached_and_retry_uses_the_successful_cache() { + let mut doc = Document::new("fallible effect shape", Size::new(512, 256), ColorMode::Rgb, SampleType::U8); + let fill = Layer::new("group fill", LayerContent::Fill(Fill::Solid(Color::BLACK))); + let mut layer = Layer::group("styled group", vec![fill]); + layer.effects.items.push(photocraft_doc::Effect::default_drop_shadow()); + doc.layers.push(layer); + let patterns = pattern::PreparedPatterns::new(&doc.patterns, pattern::PREPARED_PATTERN_BYTES).in_mode(doc.pixel_format().mode); + let cx = Ctx::for_doc(&doc, &patterns); + + for _ in 0..2 { + let alloc = FailBufferAt { fail_at: 1, attempts: AtomicUsize::new(0) }; + let mut delivered = 0; + let result = render_bands_with_alloc(&doc, doc.bounds(), RENDER_TILE, &alloc, &mut |_| { + delivered += 1; + Ok::<(), ()>(()) + }); + assert!(matches!(result, Err(RenderBandsCoreError::Allocation(1)))); + assert_eq!(delivered, 0, "failed effect-shape allocation must happen before the sink"); + assert_eq!(alloc.attempts.load(Ordering::Relaxed), 1, "the cold effect shape requests an RGBA buffer before output"); + } + + let alloc = FallibleBufferAlloc; + render_tiled_with_alloc(&doc, doc.bounds(), RENDER_TILE, &cx, &alloc).unwrap(); + let cached_cx = Ctx::for_doc(&doc, &patterns); + let cache_hit_alloc = FailBufferAt { fail_at: 1, attempts: AtomicUsize::new(0) }; + crate::fx_cache::effect_maps_with(&doc.layers[0], doc.bounds(), &cached_cx, &cache_hit_alloc).unwrap(); + assert_eq!(cache_hit_alloc.attempts.load(Ordering::Relaxed), 0, "the successful cache avoids rebuilding its RGBA shape"); + + let outlined = outlined_effect_document(); + let patterns = pattern::PreparedPatterns::new(&outlined.patterns, pattern::PREPARED_PATTERN_BYTES).in_mode(outlined.pixel_format().mode); + let cx = Ctx::for_doc(&outlined, &patterns); let alloc = FailBufferAt { fail_at: 1, attempts: AtomicUsize::new(0) }; - assert!(render_tiled_with_alloc(&doc, doc.bounds(), RENDER_TILE, &ctx, &alloc).is_err()); - assert_eq!(alloc.attempts.load(Ordering::Relaxed), 1); + let mut delivered = 0; + let result = render_bands_with_alloc(&outlined, outlined.bounds(), RENDER_TILE, &alloc, &mut |_| { + delivered += 1; + Ok::<(), ()>(()) + }); + assert!(matches!(result, Err(RenderBandsCoreError::Allocation(1)))); + assert_eq!(delivered, 0, "outline alpha extraction must use the fallible surface buffer path"); } #[test]