Integration: a painted fill reserves its buffers before it builds them (#3779)

CodeRabbit on #9775: paint.stroke with fill:true builds the coverage, its feathered copy, the selection read and the mask over the region, up to the whole canvas (file.new allows 300000 x 300000), with infallible allocations that abort the process when memory runs out. The peak is now reserved through the fallible allocator first, and the selection is read through it; fill_survives_allocation_failure fails without this (the error came from a later blend).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-27 18:18:48 +01:00
1 parent 88c19a59d4
commit 425b5efeed
2 files changed
+24 -2

No files matched your search

+7 -2
View File
@@ -484,14 +484,19 @@ fn fill_region(s: &mut Session, p: &Value, label: &str, brush: BrushSettings, pt
return Ok(Rect::new(0, 0, 0, 0));
}
let (w, h) = (bounds.width() as usize, bounds.height() as usize);
// The coverage, its feathered copy (and the blur's scratch), the selection read and
// the mask are each a buffer over the whole region, which can be the whole canvas:
// reserve that peak first, so a region too big for memory is an error, not an abort.
const FILL: &str = "filling a painted region";
let peak = w.checked_mul(h).and_then(|n| n.checked_mul(5)).ok_or_else(|| crate::allocation::error(FILL, "requested size overflowed"))?;
drop(crate::allocation::capacity::<f32>(peak, FILL)?);
let xy: Vec<(f32, f32)> = pts.iter().map(|sp| (sp.x as f32, sp.y as f32)).collect();
let mut cov = photocraft_algo::selection::polygon(&xy, bounds, true);
if feather_px > 0.01 {
cov = photocraft_algo::selection::feather(&cov, w, h, feather_px);
}
if let Some(sel) = sel.as_ref() {
let mut sv = Vec::new();
sel.read_region_into(bounds, &mut sv);
let sv = crate::allocation::read_region(sel, bounds, FILL)?;
for (c, s) in cov.iter_mut().zip(&sv) {
*c *= s.clamp(0.0, 1.0);
}
+17
View File
@@ -580,6 +580,23 @@ fn define_brush_from_selection() {
assert!(rgba(&s, 30, 32)[3] > 0.9);
}
#[test]
fn fill_survives_allocation_failure() {
// CodeRabbit on #9775: a fill's buffers span the region, up to the whole canvas. Their peak is
// reserved through the fallible allocator before any of them is built, so running out of
// memory is the fill's own error, leaving the pixels and history alone, never an abort.
let mut s = session(60, 60);
let pixels_before = surface(&s);
let history_before = s.active().unwrap().history.past_len();
crate::allocation::fail_next_for_test();
let result = s.execute("paint.stroke", json!({"points": [[10, 10], [50, 10], [50, 50], [10, 50]], "fill": true, "color": "#ff0000"}));
let _ = crate::allocation::capacity::<u8>(0, "reset failpoint");
let err = result.expect_err("an allocation failure must be reported, not panic").to_string();
assert!(err.contains("filling a painted region"), "the fill reserves its buffers first: {err}");
assert_eq!(surface(&s), pixels_before, "no pixels changed");
assert_eq!(s.active().unwrap().history.past_len(), history_before, "no history step was recorded");
}
#[test]
fn define_brush_from_selection_survives_allocation_failure() {
// The sampled tip is one f32 buffer over the whole selection, and the selection mask is a