mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 01:18:43 +00:00
* Add headless GPU backend foundation for composition and flat export (issue #8062 PR 1) * Fix headless GPU readback bounds, API compliance, and error preservation (#8062) * Fix GPU readback bounds, context thread spawn error, and test parity (#8062) * Fix BandRenderer Send bound, PNG reborrows, and clippy lints (#8062) * Fix effect shadow test construction and remove dead code wrappers (#8062) * Use short reborrows for Headless renderer without lifetime escaping (#8062) * Fix clippy collapsible_if and ensure multi-band geometry in flat export tests (#8062) * Fix automation Rect import, add renderer diagnostics, and harden flat export retries (#8062) * Fix GPU feature test APIs and native target gates * Verify unsupported GPU effects before testing band seams * Use compositor buffer ownership API after upstream memory accounting change * Update export renderer test helper for owned compositor buffers * Fix headless GPU backend review findings (#10842) - release a closed document's GPU caches through a BandRenderer hook - bound the monitored readback wait and report a stalled GPU as device loss - reject 32-bit float documents before band readback so exports fall back to CPU - keep previews at 8-bit RGBA and report the palette/Duotone renderer bypass - retain renderer diagnostics when encoding fails - strengthen renderer, save-path and GPU diagnostics tests * Add GPU close-hook and 8-bit preview regression tests (#10842) * fix(io): correct renderer binding mutability * Fix remaining headless GPU review findings (#10842) - release a document's GPU caches when file.close, file.closeAll or file.closeOthers removes it through a command - honor cancellation while band readback waits, polling in 100 ms slices - fault the device after a readback timeout so later exports skip it - report single-layer, CMYK ink-mixing, ORA and PDF CPU paths to a requested renderer - keep fallback warnings in their export instead of leaking them into the next one - requeue renderer diagnostics when a renderer-aware save's final write fails - write MCP/serve diagnostics without panicking on a broken stderr - document the actual colour-space contract and drop F32 from the GPU depth list * Use question mark for cancellable readback errors * Keep GPU export diagnostics with their attempt and stop bands on cancel Encode native .pcraft bytes before draining renderer diagnostics, and requeue export warnings when a workspace write fails, so callers still report the adapter or fallback reason with the error. Move a failed GPU attempt's queued diagnostics into its CPU retry result instead of leaving them for a later successful export to report. Stop GPU band readback at the next band after cancellation, and check the interrupt before each CpuRenderer sink delivery. Band readback uploads half-precision textures, so reject U16 alongside F32: their samples cannot survive the upload. Tighten the 8-bit CPU/GPU parity checks to 1/255. * Keep headless export warnings when a workspace write fails (#10842) A capability-scoped save encodes the export, then writes it; when the write fails, the export warnings (folded together with renderer diagnostics) were pushed back through `BandRenderer::push_diagnostic`, whose default is a no-op. A custom renderer that overrides only `drain_diagnostics` therefore lost the adapter or fallback reason that belonged with the error. Keep them in `Headless` instead and return them from the next `drain_diagnostics`, alongside the renderer's own queue. The regression uses a renderer without a `push_diagnostic` override and checks the warnings survive the failed write and drain exactly once. Also name the actual source of the depth limit in the headless GPU matrix: layer content is uploaded as half-precision textures, not band readback. * Stop CPU bands and CPU retries on cancellation (#10912) The CPU retry after a retryable GPU error composes through `FlatSource::bands`, whose shared `emit` sink did not poll the export interrupt: a cancel was only observed after the whole composite finished. Check it once per band, as `CpuRenderer` does, for both RGB and CMYK ink bands. The flat and streamed-PNG retry branches also check before starting the full CPU composite again, so a cancelled export returns `IoError::Cancelled` instead of re-rendering the document. Tests: band-sink cancellation for RGB and CMYK inks, a renderer call counter proving a second export reaches the renderer, and a pending `wait_slices` poll observing cancellation set mid-wait. * Merge origin/main into GPU foundation (#10842) * Keep GPU-path diagnostics on every failure path, honour cancel in native copy - save: export warnings of a failed local write stay in Headless pending diagnostics - render: preview fallback warnings are returned, drained, and added to the doc.render reply - io: renderer-neutral fallback wording; native single-layer copy checks cancellation per band - gpu: gate wait_slices tests off wasm32 (#10842) --------- Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
284 lines
12 KiB
Rust
284 lines
12 KiB
Rust
//! A failed renderer-aware save must never replace the previous file, and closing a document
|
|
//! must release its renderer caches.
|
|
|
|
use std::fs;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use photocraft_automation::workspace::AuthorizedWorkspace;
|
|
use photocraft_automation::{Headless, files};
|
|
use photocraft_color::{Color, ColorMode, SampleType};
|
|
use photocraft_doc::{DocId, Document, Rect, Size};
|
|
use photocraft_io::{BandRenderer, CpuRenderer, ExportOptions, IoError, RenderBand, RenderError};
|
|
|
|
struct FailingRenderer;
|
|
|
|
impl BandRenderer for FailingRenderer {
|
|
fn render_bands(
|
|
&mut self,
|
|
_doc: &Document,
|
|
_rect: Rect,
|
|
_band_rows: i32,
|
|
_ctl: &photocraft_raster::Interrupt<'_>,
|
|
_sink: &mut dyn FnMut(RenderBand) -> Result<(), IoError>,
|
|
) -> Result<(), RenderError> {
|
|
Err(RenderError::Execution("forced renderer failure".into()))
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn failed_renderer_save_preserves_prior_file() {
|
|
let dir = std::env::temp_dir().join(format!("photocraft-renderer-save-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).unwrap();
|
|
let target = dir.join("existing.png");
|
|
let original = b"prior file contents that must survive a failed save";
|
|
fs::write(&target, original).unwrap();
|
|
|
|
let mut doc = Document::with_background("test", Size::new(32, 24), ColorMode::Rgb, SampleType::U8, Color::WHITE);
|
|
let mut top = doc.layers[0].clone();
|
|
top.name = "top".into();
|
|
doc.layers.push(top);
|
|
|
|
let mut renderer = FailingRenderer;
|
|
let res = files::save_with_renderer(&doc, &target, Some("png"), &ExportOptions::default(), None, Some(&mut renderer));
|
|
assert!(res.is_err(), "the renderer failure must fail the save: {res:?}");
|
|
assert_eq!(fs::read(&target).unwrap(), original, "a failed save must leave the previous file intact");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
struct ForgetRecordingRenderer {
|
|
forgotten: Arc<Mutex<Vec<DocId>>>,
|
|
}
|
|
|
|
impl BandRenderer for ForgetRecordingRenderer {
|
|
fn render_bands(
|
|
&mut self,
|
|
_doc: &Document,
|
|
_rect: Rect,
|
|
_band_rows: i32,
|
|
_ctl: &photocraft_raster::Interrupt<'_>,
|
|
_sink: &mut dyn FnMut(RenderBand) -> Result<(), IoError>,
|
|
) -> Result<(), RenderError> {
|
|
Ok(())
|
|
}
|
|
|
|
fn forget_document(&mut self, doc: DocId) {
|
|
self.forgotten.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(doc);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn closing_a_document_forgets_its_renderer_caches() {
|
|
let forgotten = Arc::new(Mutex::new(Vec::new()));
|
|
let mut h = Headless::trusted_local();
|
|
h.session.open_document(Document::with_background("close", Size::new(16, 16), ColorMode::Rgb, SampleType::U8, Color::WHITE), None);
|
|
let id = h.session.documents()[0].doc.id;
|
|
h.set_renderer(Some(Box::new(ForgetRecordingRenderer { forgotten: forgotten.clone() })));
|
|
|
|
h.close(Some(0)).expect("close document");
|
|
|
|
let forgotten = forgotten.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clone();
|
|
assert_eq!(forgotten, vec![id], "closing a document must release its renderer caches");
|
|
}
|
|
|
|
#[test]
|
|
fn closing_a_document_through_a_command_forgets_its_renderer_caches() {
|
|
let forgotten = Arc::new(Mutex::new(Vec::new()));
|
|
let mut h = Headless::trusted_local();
|
|
for name in ["first", "second"] {
|
|
h.session.open_document(Document::with_background(name, Size::new(16, 16), ColorMode::Rgb, SampleType::U8, Color::WHITE), None);
|
|
}
|
|
let closed = h.session.documents()[0].doc.id;
|
|
h.set_renderer(Some(Box::new(ForgetRecordingRenderer { forgotten: forgotten.clone() })));
|
|
|
|
// `file.close` removes the document through the session, not `Headless::close`.
|
|
h.command_run("file.close", serde_json::json!({"document": 0})).expect("close command");
|
|
|
|
let forgotten = forgotten.lock().unwrap_or_else(std::sync::PoisonError::into_inner).clone();
|
|
assert_eq!(forgotten, vec![closed], "a command that closes a document must release its renderer caches");
|
|
}
|
|
|
|
struct DiagnosingRenderer {
|
|
diagnostics: Vec<String>,
|
|
}
|
|
|
|
impl BandRenderer for DiagnosingRenderer {
|
|
fn render_bands(
|
|
&mut self,
|
|
doc: &Document,
|
|
rect: Rect,
|
|
band_rows: i32,
|
|
ctl: &photocraft_raster::Interrupt<'_>,
|
|
sink: &mut dyn FnMut(RenderBand) -> Result<(), IoError>,
|
|
) -> Result<(), RenderError> {
|
|
self.push_diagnostic("renderer diagnostic survives a failed write".into());
|
|
let mut cpu = CpuRenderer;
|
|
cpu.render_bands(doc, rect, band_rows, ctl, sink)
|
|
}
|
|
|
|
fn drain_diagnostics(&mut self) -> Vec<String> {
|
|
std::mem::take(&mut self.diagnostics)
|
|
}
|
|
|
|
fn push_diagnostic(&mut self, diagnostic: String) {
|
|
if !self.diagnostics.contains(&diagnostic) {
|
|
self.diagnostics.push(diagnostic);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn failed_atomic_write_keeps_renderer_diagnostics_for_the_caller() {
|
|
let dir = std::env::temp_dir().join(format!("photocraft-renderer-write-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).unwrap();
|
|
// A directory at the destination makes the final atomic rename fail after encoding.
|
|
let target = dir.join("target.png");
|
|
fs::create_dir(&target).unwrap();
|
|
|
|
let mut doc = Document::with_background("test", Size::new(32, 24), ColorMode::Rgb, SampleType::U8, Color::WHITE);
|
|
let mut top = doc.layers[0].clone();
|
|
top.name = "top".into();
|
|
doc.layers.push(top);
|
|
|
|
let mut renderer = DiagnosingRenderer { diagnostics: Vec::new() };
|
|
let res = files::save_with_renderer(&doc, &target, Some("png"), &ExportOptions::default(), None, Some(&mut renderer));
|
|
assert!(res.is_err(), "a write over a directory must fail: {res:?}");
|
|
let diagnostics = renderer.drain_diagnostics();
|
|
assert!(diagnostics.iter().any(|d| d.contains("failed write")), "diagnostics drained before the failed write must be queued again: {diagnostics:?}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn failed_workspace_write_keeps_export_diagnostics_for_the_caller() {
|
|
let dir = std::env::temp_dir().join(format!("photocraft-workspace-save-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
let root = dir.join("root");
|
|
fs::create_dir_all(&root).unwrap();
|
|
|
|
let workspace = AuthorizedWorkspace::new(Some(&root), Some(&root)).expect("authorized workspace");
|
|
let mut h = Headless::with_workspace(workspace);
|
|
let mut doc = Document::with_background("test", Size::new(32, 24), ColorMode::Rgb, SampleType::U8, Color::WHITE);
|
|
let mut top = doc.layers[0].clone();
|
|
top.name = "top".into();
|
|
doc.layers.push(top);
|
|
h.session.open_document(doc, None);
|
|
h.set_renderer(Some(Box::new(DiagnosingRenderer { diagnostics: Vec::new() })));
|
|
|
|
// Outside the authorized root: the export succeeds, then its write is refused.
|
|
let outside = dir.join("outside.png");
|
|
let res = h.save(None, Some(&outside), Some("png"), &Default::default());
|
|
assert!(res.is_err(), "a write outside the workspace must fail: {res:?}");
|
|
let diagnostics = h.drain_diagnostics();
|
|
assert!(
|
|
diagnostics.iter().any(|d| d.contains("failed write")),
|
|
"export diagnostics drained before the failed workspace write must be queued again: {diagnostics:?}"
|
|
);
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
/// Overrides only `drain_diagnostics`: [`BandRenderer::push_diagnostic`] keeps its default no-op,
|
|
/// as most custom renderers leave it.
|
|
struct DrainOnlyRenderer;
|
|
|
|
impl BandRenderer for DrainOnlyRenderer {
|
|
fn render_bands(
|
|
&mut self,
|
|
doc: &Document,
|
|
rect: Rect,
|
|
band_rows: i32,
|
|
ctl: &photocraft_raster::Interrupt<'_>,
|
|
sink: &mut dyn FnMut(RenderBand) -> Result<(), IoError>,
|
|
) -> Result<(), RenderError> {
|
|
let mut cpu = CpuRenderer;
|
|
cpu.render_bands(doc, rect, band_rows, ctl, sink)
|
|
}
|
|
|
|
fn drain_diagnostics(&mut self) -> Vec<String> {
|
|
Vec::new()
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn failed_workspace_write_keeps_export_warnings_with_a_drain_only_renderer() {
|
|
let dir = std::env::temp_dir().join(format!("photocraft-workspace-warnings-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
let root = dir.join("root");
|
|
fs::create_dir_all(&root).unwrap();
|
|
|
|
let workspace = AuthorizedWorkspace::new(Some(&root), Some(&root)).expect("authorized workspace");
|
|
let mut h = Headless::with_workspace(workspace);
|
|
let mut doc = Document::with_background("test", Size::new(32, 24), ColorMode::Rgb, SampleType::U8, Color::WHITE);
|
|
let mut top = doc.layers[0].clone();
|
|
top.name = "top".into();
|
|
doc.layers.push(top);
|
|
h.session.open_document(doc, None);
|
|
h.set_renderer(Some(Box::new(DrainOnlyRenderer)));
|
|
|
|
// Outside the authorized root: the export succeeds, then its write is refused.
|
|
let outside = dir.join("outside.png");
|
|
let res = h.save(None, Some(&outside), Some("png"), &Default::default());
|
|
assert!(res.is_err(), "a write outside the workspace must fail: {res:?}");
|
|
let diagnostics = h.drain_diagnostics();
|
|
assert!(
|
|
diagnostics.iter().any(|d| d.contains("flattened")),
|
|
"the export warnings must survive a failed workspace write when the renderer ignores pushed diagnostics: {diagnostics:?}"
|
|
);
|
|
let again = h.drain_diagnostics();
|
|
assert!(again.is_empty(), "the export warnings must drain exactly once: {again:?}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn failed_local_write_keeps_export_warnings_with_a_drain_only_renderer() {
|
|
let dir = std::env::temp_dir().join(format!("photocraft-local-warnings-{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).unwrap();
|
|
// A directory at the destination makes the final atomic rename fail after encoding.
|
|
let target = dir.join("target.png");
|
|
fs::create_dir(&target).unwrap();
|
|
|
|
let mut h = Headless::trusted_local();
|
|
let mut doc = Document::with_background("test", Size::new(32, 24), ColorMode::Rgb, SampleType::U8, Color::WHITE);
|
|
let mut top = doc.layers[0].clone();
|
|
top.name = "top".into();
|
|
doc.layers.push(top);
|
|
h.session.open_document(doc, None);
|
|
h.set_renderer(Some(Box::new(DrainOnlyRenderer)));
|
|
|
|
let res = h.save(None, Some(&target), Some("png"), &Default::default());
|
|
assert!(res.is_err(), "a write over a directory must fail: {res:?}");
|
|
let diagnostics = h.drain_diagnostics();
|
|
assert!(
|
|
diagnostics.iter().any(|d| d.contains("flattened")),
|
|
"the export warnings must survive a failed local write when the renderer ignores pushed diagnostics: {diagnostics:?}"
|
|
);
|
|
assert!(h.drain_diagnostics().is_empty(), "the export warnings must drain exactly once");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn scaled_preview_fallback_warning_is_drained_and_the_rpc_reply_carries_it() {
|
|
let mut h = Headless::trusted_local();
|
|
h.session.open_document(Document::with_background("preview", Size::new(64, 48), ColorMode::Rgb, SampleType::U8, Color::WHITE), None);
|
|
h.set_renderer(Some(Box::new(DrainOnlyRenderer)));
|
|
|
|
// A scaled preview composes on the CPU; the warning must not depend on `push_diagnostic`.
|
|
let png = h.render_png(None, 16).expect("scaled preview");
|
|
assert!(png.starts_with(b"\x89PNG"));
|
|
let diagnostics = h.drain_diagnostics();
|
|
assert!(diagnostics.iter().any(|d| d.contains("scaled previews")), "preview fallback warning must be drained: {diagnostics:?}");
|
|
|
|
let reply = h.handle("doc.render", serde_json::json!({"maxSide": 16})).expect("doc.render");
|
|
assert!(reply.get("base64").is_some(), "existing reply fields stay: {reply}");
|
|
let warnings = reply.get("warnings").and_then(|w| w.as_array()).expect("warnings array");
|
|
assert!(warnings.iter().any(|w| w.as_str().is_some_and(|w| w.contains("scaled previews"))), "{reply}");
|
|
assert!(h.drain_diagnostics().is_empty(), "the reply consumed the warnings");
|
|
|
|
// No renderer, no fallback, no `warnings` key.
|
|
let mut plain = Headless::trusted_local();
|
|
plain.session.open_document(Document::with_background("plain", Size::new(64, 48), ColorMode::Rgb, SampleType::U8, Color::WHITE), None);
|
|
let reply = plain.handle("doc.render", serde_json::json!({"maxSide": 16})).expect("doc.render");
|
|
assert!(reply.get("warnings").is_none(), "{reply}");
|
|
}
|