diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs index 4ab50d378a..5e4c74cf8e 100644 --- a/crates/compose/src/lib.rs +++ b/crates/compose/src/lib.rs @@ -181,14 +181,10 @@ fn render_tiled_with(doc: &Document, rect: Rect, tile: i32, cx: &Ctx) -> Buffer } /// Runs `f` with this thread's document blend flags set ([`psblend::LAB_MIX`], [`psblend::HDR`]), -/// clearing them afterwards. +/// then restores the values they had (also on unwind): a nested rayon job can run another tile +/// on this thread mid-composite, and clearing the flags there broke the outer tile (#1112). fn in_blend_space(lab: bool, hdr: bool, f: impl FnOnce() -> R) -> R { - psblend::LAB_MIX.with(|l| l.set(lab)); - psblend::HDR.with(|h| h.set(hdr)); - let r = f(); - psblend::LAB_MIX.with(|l| l.set(false)); - psblend::HDR.with(|h| h.set(false)); - r + psblend::with_lab_mix(lab, || psblend::with_hdr(hdr, f)) } /// The document's own CMYK profile for reading its CMYK pixels (`None`: not a CMYK document, diff --git a/crates/compose/src/psblend.rs b/crates/compose/src/psblend.rs index 5d83068c91..063e32ef5a 100644 --- a/crates/compose/src/psblend.rs +++ b/crates/compose/src/psblend.rs @@ -81,6 +81,33 @@ thread_local! { pub static HDR: 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() +} + +/// Runs `f` with [`HDR`] set to `hdr`, then restores the value it had (also on unwind), like +/// [`with_lab_mix`]. +pub fn with_hdr(hdr: bool, f: impl FnOnce() -> R) -> R { + struct Restore(bool); + impl Drop for Restore { + fn drop(&mut self) { + HDR.with(|h| h.set(self.0)); + } + } + let _restore = Restore(HDR.with(|h| h.replace(hdr))); + 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 902202b6c2..3474ef205b 100644 --- a/crates/compose/src/tests.rs +++ b/crates/compose/src/tests.rs @@ -1654,6 +1654,26 @@ fn photo_filter_matches_photoshop() { } } +#[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}"); + } + } + } +} + /// Vibrance of an 8-bit sRGB colour (0–255 in and out). fn vibrance_255(c: [f32; 3], vibrance: f32, saturation: f32) -> [f32; 3] { let mut buf = Buffer::filled(Rect::new(0, 0, 1, 1), [c[0] / 255.0, c[1] / 255.0, c[2] / 255.0, 1.0]);