From f1433f4ab2968931905e5c4cda7e13a143572829 Mon Sep 17 00:00:00 2001 From: "Claude Opus 5.5" Date: Thu, 8 Oct 2026 18:27:48 -0400 Subject: [PATCH] Compose: restore the Lab-mix flag instead of clearing it after a tile (#1112) Rendering set the thread-local LAB_MIX to the document's value around each tile, then set it to false. A nested rayon job (a gradient fill, say) can let the same thread render another tile in the middle of an outer one; clearing the flag there made the rest of the outer Lab tile mix in sRGB. The issue's reproduction (a 4096 px Lab document with a gradient fill and two 50 % fills, 60 renders, RAYON_NUM_THREADS=4) showed whole 256 px tiles differing between identical renders. psblend::with_lab_mix sets the flag, runs the closure, and restores the previous value with a drop guard, as with_cmyk_space does for the CMYK space. The three set/clear pairs in render_tiled_with use it. The new test sets the flag, renders Lab and RGB documents on the single- and multi-tile paths, and checks the flag is what it was. It failed before the fix. The issue's reproduction, run twice each way on macOS with four threads: before, 6 and 2 of 60 renders had one whole tile wrong (65536 px); after, 0 of 60 both times. --- crates/compose/src/lib.rs | 14 +++----------- crates/compose/src/psblend.rs | 14 ++++++++++++++ crates/compose/src/tests.rs | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index d1d44e9755..a073325fa1 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -104,26 +104,18 @@ fn render_tiled_with(doc: &Document, rect: Rect, tile: i32, cx: &Ctx) -> Buffer if rect.width() as i32 <= tile && rect.height() as i32 <= tile { return photocraft_color::convert::with_cmyk_space(cmyk, || { let mut buf = multichannel::backdrop(doc, rect); - psblend::LAB_MIX.with(|l| l.set(lab)); - composite_stack(&doc.layers, &mut buf, cx); - psblend::LAB_MIX.with(|l| l.set(false)); + psblend::with_lab_mix(lab, || composite_stack(&doc.layers, &mut buf, cx)); buf }); } // 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, || { - psblend::LAB_MIX.with(|l| l.set(lab)); - f(); - psblend::LAB_MIX.with(|l| l.set(false)); - }); + photocraft_color::convert::with_cmyk_space(cmyk, || psblend::with_lab_mix(lab, f)); }); let run = |t: Rect| { photocraft_color::convert::with_cmyk_space(cmyk, || { let mut b = multichannel::backdrop(doc, t); - psblend::LAB_MIX.with(|l| l.set(lab)); - composite_stack(&doc.layers, &mut b, cx); - psblend::LAB_MIX.with(|l| l.set(false)); + psblend::with_lab_mix(lab, || composite_stack(&doc.layers, &mut b, cx)); b }) }; diff --git a/crates/compose/src/psblend.rs b/crates/compose/src/psblend.rs index c3698bb4be..c983352980 100644 --- a/crates/compose/src/psblend.rs +++ b/crates/compose/src/psblend.rs @@ -78,6 +78,20 @@ thread_local! { pub static LAB_MIX: std::cell::Cell = const { std::cell::Cell::new(false) }; } +/// Runs `f` with [`LAB_MIX`] set to `lab`, then restores the value it had (also on unwind). A +/// nested rayon job can run another tile on this thread mid-composite; clearing the flag there +/// made the rest of the outer tile mix in sRGB (#1112). +pub fn with_lab_mix(lab: bool, f: impl FnOnce() -> R) -> R { + struct Restore(bool); + impl Drop for Restore { + fn drop(&mut self) { + LAB_MIX.with(|l| l.set(self.0)); + } + } + let _restore = Restore(LAB_MIX.with(|l| l.replace(lab))); + f() +} + /// `B(Cb, Cs)` with Photoshop's variants. pub fn blend_rgb(mode: BlendMode, cb: [f32; 3], cs: [f32; 3]) -> [f32; 3] { match mode { diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs index f6e143c1e5..a7b8c8b7cf 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -1565,3 +1565,23 @@ fn photo_filter_matches_photoshop() { assert!(got.iter().zip(ps).all(|(g, p)| (g - p).abs() <= 2.5), "F32 {v:?}: got {got:?} want {ps:?}"); } } + +#[test] +fn rendering_restores_the_lab_mix_flag_it_found() { + // #1112: a render set LAB_MIX for its document, then cleared it. A nested rayon job (a + // gradient fill, say) can let a thread render another tile in the middle of an outer Lab + // tile, and clearing the flag made the rest of that outer tile mix in sRGB. + for mode in [ColorMode::Lab, ColorMode::Rgb] { + let doc = Document::with_background("t", Size::new(8, 8), mode, SampleType::U8, Color::WHITE); + // Single-tile and multi-tile paths (the latter also prepares effect maps under the flag). + for tile in [RENDER_TILE, 4] { + for outer in [true, false] { + psblend::LAB_MIX.with(|l| l.set(outer)); + let _ = render_tiled(&doc, doc.bounds(), tile); + let after = psblend::LAB_MIX.with(|l| l.get()); + psblend::LAB_MIX.with(|l| l.set(false)); + assert_eq!(after, outer, "{mode:?} with tile {tile} inside a tile that had LAB_MIX = {outer}"); + } + } + } +}