From 1ce1bced950a862bc7e635edc170a36aae09ccc9 Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Fri, 6 Nov 2026 16:17:33 +0530 Subject: [PATCH 1/8] PSD admission: a little free memory no longer refuses every file With 1 GiB or less of free RAM, or 512 MiB or less of commit headroom, the hard limit was exactly 0, so even a small file was refused with a message that didn't say memory was low. The reserve now shrinks with the measurement: up to a quarter of free RAM opens automatically, the rest goes to approval, and an approved import always leaves at least 256 MiB free. Limits are unchanged for automatic opening above 4/3 GiB free and for approval from 4 GiB free. A refusal set by measured memory now gives the free amount and says to close other apps and try again, and the approval prompt warns when little memory would be left. An unreadable cgroup still measures Some(0) and stays refused. --- crates/automation/src/headless.rs | 2 +- crates/automation/src/rpc.rs | 8 +- crates/automation/src/rpc/tests.rs | 21 +- crates/automation/src/server.rs | 35 ++- crates/engine/src/cmd/file_cmds/open.rs | 3 +- crates/engine/src/cmd/file_cmds/tests.rs | 4 +- crates/engine/src/cmd/video_cmds/tests.rs | 8 +- crates/engine/src/error.rs | 44 ++- crates/engine/src/jobs/tests.rs | 4 +- crates/io/src/flat.rs | 6 +- crates/io/src/lib.rs | 10 +- crates/io/src/memory.rs | 352 +++++++++++++++++++--- crates/io/src/psd_budget.rs | 2 +- crates/io/src/psd_budget/tests.rs | 31 +- crates/io/src/psd_gate.rs | 6 +- crates/io/src/tiff_layers.rs | 10 +- crates/ui-egui/src/dialogs/open_prompt.rs | 4 + crates/ui-egui/src/psd_open_tests.rs | 21 ++ docs/development.md | 46 ++- 19 files changed, 523 insertions(+), 94 deletions(-) diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs index f6e3fd4a12..db662ada64 100644 --- a/crates/automation/src/headless.rs +++ b/crates/automation/src/headless.rs @@ -661,7 +661,7 @@ mod waited_error_tests { fn waited_job_keeps_typed_failure() { let mut headless = Headless::trusted_local(); let id = JobId(123); - let error = photocraft_engine::EngineError::PsdHardBudget { name: "large.psd".into(), required: 9000, limit: 4000 }; + let error = photocraft_engine::EngineError::PsdHardBudget { name: "large.psd".into(), required: 9000, limit: 4000, free_memory: None }; headless.waited.insert( id, Some(JobEvent { diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs index 80b5a32165..0f7540d139 100644 --- a/crates/automation/src/rpc.rs +++ b/crates/automation/src/rpc.rs @@ -79,12 +79,16 @@ fn attach_memory_error_details(obj: &mut Value, err: &AutomationError) { } } } - AutomationError::Import(photocraft_io::IoError::PsdImportBudget { required, limit }) - | AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { required, limit, .. }) => { + AutomationError::Import(photocraft_io::IoError::PsdImportBudget { required, limit, free_memory }) + | AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { required, limit, free_memory, .. }) => { if let Some(m) = obj.as_object_mut() { m.insert("required".into(), json!(required)); m.insert("hard".into(), json!(limit)); m.insert("hardLimit".into(), json!(limit)); + if let Some(free) = free_memory { + m.insert("available".into(), json!(free)); + m.insert("availableRam".into(), json!(free)); + } } } _ => {} diff --git a/crates/automation/src/rpc/tests.rs b/crates/automation/src/rpc/tests.rs index 8bd57a871a..cecc69f376 100644 --- a/crates/automation/src/rpc/tests.rs +++ b/crates/automation/src/rpc/tests.rs @@ -1092,7 +1092,12 @@ fn approve_high_memory_rejects_invalid_types_and_formats_structured_error() { // fields, and only the fields an approval request has: no soft limit and no capacity, since // no approval can raise the ceiling. let mut hard_obj = json!({"id": 2, "ok": false, "error": "test"}); - let hard = crate::AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { name: "big.psd".into(), required: 9000, limit: 8000 }); + let hard = crate::AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { + name: "big.psd".into(), + required: 9000, + limit: 8000, + free_memory: None, + }); super::attach_memory_error_details(&mut hard_obj, &hard); assert_eq!(hard_obj["required"], 9000); assert_eq!(hard_obj["hard"], 8000); @@ -1100,6 +1105,20 @@ fn approve_high_memory_rejects_invalid_types_and_formats_structured_error() { for absent in ["soft", "softLimit", "available", "availableRam"] { assert!(hard_obj.get(absent).is_none(), "a hard refusal has no {absent}: {hard_obj}"); } + + // When measured free memory set the limit, the refusal also reports it. + let mut low_obj = json!({"id": 3, "ok": false, "error": "test"}); + let low = crate::AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { + name: "big.psd".into(), + required: 9000, + limit: 8000, + free_memory: Some(10_000), + }); + super::attach_memory_error_details(&mut low_obj, &low); + assert_eq!(low_obj["hardLimit"], 8000); + assert_eq!(low_obj["available"], 10_000); + assert_eq!(low_obj["availableRam"], 10_000); + assert!(low_obj.get("soft").is_none(), "a hard refusal has no soft limit: {low_obj}"); } /// #10897: `doc.place` forwards the strict one-shot high-memory approval on both routes: the diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rs index 4f210b07f4..46468e0352 100644 --- a/crates/automation/src/server.rs +++ b/crates/automation/src/server.rs @@ -514,8 +514,9 @@ fn png_result(png: &[u8], note: String) -> CallToolResult { } /// Adds the typed byte counts of a memory refusal to an error object: an approval request keeps -/// its soft limit, hard limit and (when known) capacity; a hard refusal keeps only its required -/// and hard amounts, because no approval can raise that ceiling. +/// its soft limit, hard limit and (when known) capacity; a hard refusal keeps its required and +/// hard amounts, because no approval can raise that ceiling, and adds the free memory when +/// measured memory set the limit. fn attach_memory_error_details(obj: &mut Value, err: &AutomationError) { let Some(obj) = obj.as_object_mut() else { return }; match err { @@ -531,11 +532,15 @@ fn attach_memory_error_details(obj: &mut Value, err: &AutomationError) { obj.insert("availableRam".into(), json!(avail)); } } - AutomationError::Import(photocraft_io::IoError::PsdImportBudget { required, limit }) - | AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { required, limit, .. }) => { + AutomationError::Import(photocraft_io::IoError::PsdImportBudget { required, limit, free_memory }) + | AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { required, limit, free_memory, .. }) => { obj.insert("required".into(), json!(required)); obj.insert("hard".into(), json!(limit)); obj.insert("hardLimit".into(), json!(limit)); + if let Some(free) = free_memory { + obj.insert("available".into(), json!(free)); + obj.insert("availableRam".into(), json!(free)); + } } _ => {} } @@ -1614,7 +1619,12 @@ mod tests { assert!(serde_json::from_value::(json!({"path": "file.psd", "approve_high_memory": invalid})).is_err(), "{invalid}"); } - let err = AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { name: "huge.psd".into(), required: 9_000, limit: 8_000 }); + let err = AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { + name: "huge.psd".into(), + required: 9_000, + limit: 8_000, + free_memory: None, + }); let res = to_result(Err(err)).unwrap(); assert_eq!(res.is_error, Some(true)); let text = &res.content.first().and_then(|c| c.as_text()).unwrap().text; @@ -1625,6 +1635,21 @@ mod tests { for absent in ["soft", "softLimit", "available", "availableRam"] { assert!(v.get(absent).is_none(), "a hard refusal has no {absent}: {v}"); } + + // With measured free memory behind the limit, the refusal reports it too. + let err = AutomationError::Engine(photocraft_engine::EngineError::PsdHardBudget { + name: "huge.psd".into(), + required: 9_000, + limit: 8_000, + free_memory: Some(10_000), + }); + let res = to_result(Err(err)).unwrap(); + let text = &res.content.first().and_then(|c| c.as_text()).unwrap().text; + let v: Value = serde_json::from_str(text).unwrap(); + assert_eq!(v["hardLimit"], 8_000); + assert_eq!(v["available"], 10_000); + assert_eq!(v["availableRam"], 10_000); + assert!(v.get("soft").is_none(), "a hard refusal has no soft limit: {v}"); } /// The MCP server applies finished jobs before every tool call too (#503). diff --git a/crates/engine/src/cmd/file_cmds/open.rs b/crates/engine/src/cmd/file_cmds/open.rs index cb05f39483..ab5db491ed 100644 --- a/crates/engine/src/cmd/file_cmds/open.rs +++ b/crates/engine/src/cmd/file_cmds/open.rs @@ -287,7 +287,8 @@ pub(super) fn approve_memory(s: &mut Session, p: &Value) -> Result { // does not keep the tile share), and the import re-checks the exact split before decoding. let budget = photocraft_io::memory::resolve_budget_with_scratch(photocraft_io::memory::MemorySnapshot::current(), None, u64::MAX); if preflight.required > budget.hard_limit { - return Err(EngineError::PsdHardBudget { name: preflight.name, required: preflight.required, limit: budget.hard_limit }); + let free_memory = budget.refusal_free_memory(preflight.required); + return Err(EngineError::PsdHardBudget { name: preflight.name, required: preflight.required, limit: budget.hard_limit, free_memory }); } let started = s.start_open_approved_with_background( &preflight.name, diff --git a/crates/engine/src/cmd/file_cmds/tests.rs b/crates/engine/src/cmd/file_cmds/tests.rs index 6f97374126..c787ec4bee 100644 --- a/crates/engine/src/cmd/file_cmds/tests.rs +++ b/crates/engine/src/cmd/file_cmds/tests.rs @@ -4075,7 +4075,7 @@ fn approve_memory_releases_the_preflight_when_the_hard_limit_refuses_it() { photocraft_io::memory::with_mock_memory(snap_low, || { let err = s.execute("file.approveMemory", json!({"id": id, "approve": true, "background": false})).unwrap_err(); match &err { - EngineError::PsdHardBudget { name, required, limit } => { + EngineError::PsdHardBudget { name, required, limit, .. } => { assert_eq!(name, "ram_drop.psd"); assert_eq!((*required, *limit), (8 * gb, expected_limit)); } @@ -4104,7 +4104,7 @@ fn open_as_and_place_keep_the_typed_hard_budget_fields() { let mut s = Session::new(); let err = s.execute("file.openAs", json!({"path": path.clone(), "approveHighMemory": true})).unwrap_err(); match &err { - EngineError::PsdHardBudget { name, required, limit } => { + EngineError::PsdHardBudget { name, required, limit, .. } => { assert_eq!(name, &path); assert!(*required > hard && *limit == hard); } diff --git a/crates/engine/src/cmd/video_cmds/tests.rs b/crates/engine/src/cmd/video_cmds/tests.rs index f2acb0bd0f..e11f271d65 100644 --- a/crates/engine/src/cmd/video_cmds/tests.rs +++ b/crates/engine/src/cmd/video_cmds/tests.rs @@ -202,9 +202,9 @@ fn tiff_page_errors_keep_memory_refusals_typed_and_other_context() { other => panic!("page 2 approval refusal lost its fields: {other:?}"), } - let hard = photocraft_io::IoError::PsdImportBudget { required: 9_000, limit: 8_000 }; + let hard = photocraft_io::IoError::PsdImportBudget { required: 9_000, limit: 8_000, free_memory: None }; match tiff_page_error(display, name, 2, hard) { - EngineError::PsdHardBudget { name: named, required, limit } => { + EngineError::PsdHardBudget { name: named, required, limit, .. } => { assert_eq!(named, name); assert_eq!((required, limit), (9_000, 8_000)); } @@ -233,7 +233,7 @@ fn video_frames_to_layers_keeps_a_layered_tiff_page_refusal_typed() { let _limit = super::limits::Scoped::psd_max_pixel_bytes(1); let err = s.execute("file.import.videoFramesToLayers", json!({"path": path})).unwrap_err(); match err { - EngineError::PsdHardBudget { name, required, limit } => { + EngineError::PsdHardBudget { name, required, limit, .. } => { assert_eq!(name, path); assert_eq!(limit, 1); assert!(required > limit, "the page estimate must be past the caller's limit: {required} > {limit}"); @@ -284,7 +284,7 @@ fn video_frames_to_layers_uses_the_footage_limit_for_a_single_page_layered_tiff( let _limit = super::limits::Scoped::psd_max_pixel_bytes(1); let err = s.execute("file.import.videoFramesToLayers", json!({"path": path})).unwrap_err(); match err { - EngineError::PsdHardBudget { name, required, limit } => { + EngineError::PsdHardBudget { name, required, limit, .. } => { assert_eq!(name, path); assert_eq!(limit, 1); assert!(required > limit, "the page estimate must be past the caller's limit: {required} > {limit}"); diff --git a/crates/engine/src/error.rs b/crates/engine/src/error.rs index 0abdb6302a..a103edb999 100644 --- a/crates/engine/src/error.rs +++ b/crates/engine/src/error.rs @@ -23,8 +23,13 @@ pub enum EngineError { PsdApprovalRequired { name: String, required: u64, soft_limit: u64, hard_limit: u64, available_ram: Option }, /// The file's estimated memory cannot be admitted within the hard ceiling, which no approval /// can raise. The requirement may equal the ceiling when the estimate saturated. - #[error("cannot open “{name}”: its estimated memory requirement of {required} bytes cannot be admitted within the {limit} byte hard limit on this machine")] - PsdHardBudget { name: String, required: u64, limit: u64 }, + /// + /// `free_memory` is the free memory measured when it set the limit, if it did. + #[error( + "cannot open “{name}”: its estimated memory requirement of {required} bytes cannot be admitted within the {limit} byte hard limit on this machine{advice}", + advice = photocraft_io::memory::low_memory_advice(.free_memory) + )] + PsdHardBudget { name: String, required: u64, limit: u64, free_memory: Option }, /// The file behind an opened handle is not the one whose memory estimate was approved. /// The document is discarded and is not opened. #[error("“{name}” changed since it was checked; open it again to approve the new file")] @@ -59,7 +64,7 @@ pub(crate) fn io_open_error(name: &str, error: photocraft_io::IoError) -> Engine IoError::PsdApprovalRequired { required, soft_limit, hard_limit, available_ram } => { EngineError::PsdApprovalRequired { name: name.to_string(), required, soft_limit, hard_limit, available_ram } } - IoError::PsdImportBudget { required, limit } => EngineError::PsdHardBudget { name: name.to_string(), required, limit }, + IoError::PsdImportBudget { required, limit, free_memory } => EngineError::PsdHardBudget { name: name.to_string(), required, limit, free_memory }, IoError::SourceChanged => EngineError::SourceChanged { name: name.to_string() }, error => EngineError::Other(format!("{name}: {error}")), } @@ -70,3 +75,36 @@ impl From for EngineError { Self::Other(format!("not enough memory for this operation ({error}); the document was not changed")) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn hard(free_memory: Option) -> EngineError { + EngineError::PsdHardBudget { name: "big.psd".into(), required: 9000, limit: 4000, free_memory } + } + + #[test] + fn hard_budget_message_tells_the_user_to_free_memory_when_it_was_measured() { + let message = hard(Some(900 << 20)).to_string(); + assert!(message.ends_with("Close other apps and try again"), "{message}"); + assert!(message.contains("900 MiB"), "{message}"); + + let message = hard(Some(0)).to_string(); + assert!(message.contains("measured no free memory"), "{message}"); + + let message = hard(None).to_string(); + assert!(message.ends_with("on this machine"), "{message}"); + } + + #[test] + fn io_open_error_carries_the_free_memory_through() { + for free_memory in [None, Some(0), Some(3 << 30)] { + let error = photocraft_io::IoError::PsdImportBudget { required: 9000, limit: 4000, free_memory }; + match io_open_error("big.psd", error) { + EngineError::PsdHardBudget { required: 9000, limit: 4000, free_memory: carried, .. } => assert_eq!(carried, free_memory), + other => panic!("expected the typed hard refusal, got {other:?}"), + } + } + } +} diff --git a/crates/engine/src/jobs/tests.rs b/crates/engine/src/jobs/tests.rs index 5810ff5d21..5e43ca6b24 100644 --- a/crates/engine/src/jobs/tests.rs +++ b/crates/engine/src/jobs/tests.rs @@ -1350,14 +1350,14 @@ fn a_failed_job_keeps_its_typed_error_for_wait_and_events() { json!({}), "Test hard budget", false, - |_ctx| -> Result<()> { Err(EngineError::PsdHardBudget { name: "too-big.psd".into(), required: 9000, limit: 4000 }) }, + |_ctx| -> Result<()> { Err(EngineError::PsdHardBudget { name: "too-big.psd".into(), required: 9000, limit: 4000, free_memory: None }) }, |_, _| Ok(Value::Null), ) .unwrap(); let id = job(started); let err = s.wait_job(id).expect_err("the job fails"); assert!( - matches!(&err, EngineError::PsdHardBudget { name, required, limit } if name == "too-big.psd" && *required == 9000 && *limit == 4000), + matches!(&err, EngineError::PsdHardBudget { name, required, limit, .. } if name == "too-big.psd" && *required == 9000 && *limit == 4000), "wait_job must not flatten the typed refusal: {err:?}" ); let event = s.poll_jobs().into_iter().find(|e| e.id == id).expect("the job event"); diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index f56052d9a9..6af29f9287 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -2498,14 +2498,14 @@ mod streamed_png_tests { let lowered = crate::ImportOptions { psd_max_pixel_bytes: 99, ..Default::default() }; match tiff_admission(100, 0, &lowered, &ctl) { - Err(IoError::PsdImportBudget { required, limit }) => assert_eq!((required, limit), (100, 99)), + Err(IoError::PsdImportBudget { required, limit, .. }) => assert_eq!((required, limit), (100, 99)), other => panic!("expected the caller's limit to refuse, got {:?}", other.map(|_| ())), } assert_eq!(crate::psd_gate::active_attempt_remaining(), Some(ample), "a refused nested import leaves the parent's allowance untouched"); let stale = crate::ImportOptions { psd_high_memory_approved: true, psd_approved_estimate: Some(99), ..Default::default() }; match tiff_admission(100, 0, &stale, &ctl) { - Err(IoError::PsdImportBudget { required, limit }) => assert_eq!((required, limit), (100, 99)), + Err(IoError::PsdImportBudget { required, limit, .. }) => assert_eq!((required, limit), (100, 99)), other => panic!("expected the approved estimate bound to refuse, got {:?}", other.map(|_| ())), } assert_eq!(crate::psd_gate::active_attempt_remaining(), Some(ample), "a refused nested import leaves the parent's allowance untouched"); @@ -2532,7 +2532,7 @@ mod streamed_png_tests { crate::ImportOptions { psd_high_memory_approved: true, psd_approved_estimate: Some(u64::MAX), ..Default::default() }, ] { match tiff_admission(u64::MAX, 0, &opts, &Interrupt::NONE) { - Err(IoError::PsdImportBudget { required, limit }) => assert_eq!((required, limit), (u64::MAX, u64::MAX)), + Err(IoError::PsdImportBudget { required, limit, .. }) => assert_eq!((required, limit), (u64::MAX, u64::MAX)), other => panic!("expected a hard refusal for {opts:?}, got {:?}", other.map(|_| ())), } } diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index fe85d88ce1..dbae0d70a7 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -124,8 +124,12 @@ pub enum IoError { #[error("PSD: {0}")] Psd(#[from] PsdError), /// The conservative PSD pixel-storage and workspace estimate exceeded its allowance. - #[error("PSD import needs an estimated {required} bytes of pixel memory; limit is {limit} bytes")] - PsdImportBudget { required: u64, limit: u64 }, + /// + /// `free_memory` is the free memory measured when it set the limit (see + /// [`memory::MemoryBudget::measured_free`]); `None` when a caller or attempt allowance, the + /// platform cap or an unknown capacity set it. + #[error("PSD import needs an estimated {required} bytes of pixel memory; limit is {limit} bytes{advice}", advice = crate::memory::low_memory_advice(.free_memory))] + PsdImportBudget { required: u64, limit: u64, free_memory: Option }, /// PSD pixel-storage estimate exceeds the automatic soft limit, but is within the hard limit. /// Explicit approval is required to proceed. #[error( @@ -1406,7 +1410,7 @@ mod tests { for (opts, what) in cases { super::psd_gate::set_active_attempt_budget(ample); match super::import_with_options_and_interrupt("nested.psd", &bytes, &opts, &ctl) { - Err(super::IoError::PsdImportBudget { required: r, limit }) => { + Err(super::IoError::PsdImportBudget { required: r, limit, .. }) => { assert_eq!((r, limit), (required, required - 1), "{what} must refuse with its exact fields") } other => panic!("expected {what} to refuse, got {:?}", other.map(|_| ())), diff --git a/crates/io/src/memory.rs b/crates/io/src/memory.rs index c9b8c54378..24e7b873ac 100644 --- a/crates/io/src/memory.rs +++ b/crates/io/src/memory.rs @@ -31,9 +31,23 @@ pub const MAX_SOFT_BUDGET: u64 = 8 << 30; /// platform's actual allocation limits, but the process may run out of memory. pub const UNKNOWN_MEMORY_WARNING_THRESHOLD: u64 = 8 << 30; -/// Reserve subtracted from commit headroom when known. +/// Largest reserve subtracted from commit headroom when known. The reserve is a quarter of the +/// headroom, at least [`LOW_MEMORY_RESERVE`] and at most this, so a machine with little commit +/// headroom still has room for a small file. pub const COMMIT_RESERVE: u64 = 512 << 20; +/// The smallest amount an approved import leaves free: for the app, its display and the system. +/// +/// Free RAM or commit headroom at or below it leaves no room at all, so every file is refused. +pub const LOW_MEMORY_RESERVE: u64 = 256 << 20; + +/// What automatic opening leaves free once that is less than the quarter-of-free-RAM rule. +const AUTOMATIC_RAM_RESERVE: u64 = 1 << 30; + +/// When opening a file would leave less than this free, the approval prompt warns that +/// PhotoCraft may run out of memory. +pub const LOW_MEMORY_WARNING_MARGIN: u64 = 1 << 30; + /// Bytes Rust's layout rules allow for one contiguous allocation on this target /// (`isize::MAX`), not a bound on an import's aggregate peak. /// @@ -171,6 +185,11 @@ pub struct MemoryBudget { /// Set when available memory is unknown: estimates above this threshold still need explicit /// approval but also carry the uncertain-capacity warning. `None` when capacity is known. pub warning_threshold: Option, + /// The free memory measured when measured memory is what set the hard limit: the smaller of + /// the known available RAM and commit headroom. `None` when neither is known, when the + /// platform cap or the caller's limit is what bound the hard limit, and on the web. A refusal + /// reports it so the user can be told to free memory (see [`low_memory_advice`]). + pub measured_free: Option, } /// Admission decision for a given required memory estimate. @@ -213,6 +232,33 @@ impl MemoryBudget { pub fn requires_uncertainty_warning(&self, required: u64) -> bool { self.warning_threshold.is_some_and(|threshold| required > threshold) } + + /// The free memory to report when `required` is refused, or `None` when closing other apps + /// cannot help: the estimate overflowed `u64` (`u64::MAX`), or the limit that refused it was + /// not set by measured memory. + pub fn refusal_free_memory(&self, required: u64) -> Option { + if required == u64::MAX { None } else { self.measured_free } + } +} + +/// The sentence appended to a hard-memory refusal when measured free memory set the limit; empty +/// for `None`. `Some(0)` also covers the fail-closed reading of an unreadable memory limit. +pub fn low_memory_advice(free_memory: &Option) -> String { + match free_memory { + None => String::new(), + Some(0) => ". PhotoCraft measured no free memory, or could not read this system's memory limit. Close other apps and try again".to_string(), + Some(n) => format!(". About {} of memory is free. Close other apps and try again", size_text(*n)), + } +} + +fn size_text(bytes: u64) -> String { + if bytes >= 1 << 30 { + format!("{:.1} GiB", bytes as f64 / (1u64 << 30) as f64) + } else if bytes >= 1 << 20 { + format!("{} MiB", bytes / (1 << 20)) + } else { + format!("{bytes} B") + } } /// Whether an approval-required estimate carries the unknown-capacity warning, given the @@ -252,16 +298,22 @@ pub fn native_platform_cap() -> u64 { /// The limits are for an import's aggregate peak estimate, not for one allocation: a peak may sum /// many buffers, each of which is separately bounded where the decoder allocates it. /// -/// Policy: -/// - Known RAM: H = min(floor(3*A/4), A - 1 GiB, C - reserve if known, [`native_platform_cap`]); -/// S = min(A/2, 8 GiB, H). -/// - Unknown native RAM: S = min(2 GiB, H) where H is [`native_platform_cap`], reduced by known -/// commit headroom; approval above S is possible up to H. 8 GiB is a warning threshold, not H. -/// - Web (wasm32): S = 1 GiB; H = min([`WASM_MAX_PEAK_BUDGET`], caller limit). +/// Policy (A = available RAM, C = commit headroom, each when known): +/// - Known RAM: H = min(floor(3*A/4), A - [`LOW_MEMORY_RESERVE`], C - reserve if known, +/// [`native_platform_cap`]), where the commit reserve is a quarter of C, at least +/// [`LOW_MEMORY_RESERVE`] and at most [`COMMIT_RESERVE`]. S = min(A/2, 8 GiB, max(A - 1 GiB, +/// A/4), max(C - [`COMMIT_RESERVE`], C/4) if known, H). Above 4/3 GiB free this is the usual +/// "leave 1 GiB free"; below it up to a quarter of free RAM opens automatically and the rest +/// goes to approval. At or below [`LOW_MEMORY_RESERVE`] of either there is no room. +/// - Unknown native RAM: S = min(2 GiB, commit soft room if known, H) where H is +/// [`native_platform_cap`], reduced by known commit headroom as above; approval above S is +/// possible up to H. 8 GiB is a warning threshold, not H. /// - <= S: automatic; S < E <= H: explicit approval; > H: refuse, no unlimited override. /// - The saturated `u64::MAX` overflow sentinel is always refused, even when H is `u64::MAX` /// (see [`MemoryBudget::decide`]). -/// - Known zero available RAM stays zero (it never becomes unknown/free). +/// - Known zero available RAM stays zero (it never becomes unknown/free). The fail-closed reading +/// of an unreadable memory limit is `Some(0)`, so it stays refused; only a credited tile share +/// (see [`resolve_budget_with_scratch`]) can pass, as before. /// - swap is never part of the adaptive budget. /// - Explicit caller limits may lower admission, never bypass the hard boundary. pub fn resolve_budget(snapshot: MemorySnapshot, caller_limit: Option) -> MemoryBudget { @@ -294,47 +346,71 @@ fn resolve_budget_credited(platform: Platform, snapshot: MemorySnapshot, caller_ if let Some(caller) = caller_limit.filter(|&c| c > 0) { hard = hard.min(caller); } - return MemoryBudget { soft_limit: WASM_BUDGET.min(hard), hard_limit: hard, available_ram: None, warning_threshold: None }; + return MemoryBudget { soft_limit: WASM_BUDGET.min(hard), hard_limit: hard, available_ram: None, warning_threshold: None, measured_free: None }; } let platform_cap = native_platform_cap(); - let (mut hard, mut soft, warning_threshold) = match snapshot.available_ram { - Some(a) => { - let h_pct = ((a as u128 * 3) / 4) as u64; - let h_reserve = a.saturating_sub(1 << 30); - let mut h = h_pct.min(h_reserve); + let ram = snapshot.available_ram; + let commit = snapshot.commit_headroom; - if let Some(c) = snapshot.commit_headroom { - let c_room = c.saturating_sub(COMMIT_RESERVE); - h = h.min(c_room); - } - - h = h.min(platform_cap); - - let s_pct = a / 2; - let s = s_pct.min(MAX_SOFT_BUDGET).min(h); - (h, s, None) - } - None => { - let mut h = platform_cap; - if let Some(c) = snapshot.commit_headroom { - let c_room = c.saturating_sub(COMMIT_RESERVE); - h = h.min(c_room); - } - let s = NATIVE_FALLBACK_BUDGET.min(h); - (h, s, Some(UNKNOWN_MEMORY_WARNING_THRESHOLD)) - } + // The room measured memory leaves; with neither measurement known only the platform cap bounds. + let measured_room = match (ram.map(ram_hard_room), commit.map(commit_hard_room)) { + (Some(r), Some(c)) => Some(r.min(c)), + (r, c) => r.or(c), + }; + let mut hard = measured_room.map_or(platform_cap, |room| room.min(platform_cap)); + let commit_soft = commit.map(commit_soft_room); + let (mut soft, warning_threshold) = match ram { + Some(a) => (ram_soft_room(a).min(commit_soft.unwrap_or(u64::MAX)).min(hard), None), + None => (NATIVE_FALLBACK_BUDGET.min(commit_soft.unwrap_or(u64::MAX)).min(hard), Some(UNKNOWN_MEMORY_WARNING_THRESHOLD)), }; // Tile pixels the scratch disk takes need no RAM (see [`resolve_budget_with_scratch`]). hard = hard.saturating_add(credit); soft = soft.saturating_add(credit); + let machine_limit = hard; if let Some(caller) = caller_limit.filter(|&c| c > 0) { hard = hard.min(caller); soft = soft.min(hard); } - MemoryBudget { soft_limit: soft, hard_limit: hard, available_ram: snapshot.available_ram, warning_threshold } + // Report the measured free memory only when it is what set the hard limit. + let measured_free = match (ram, commit) { + (None, None) => None, + (Some(a), None) => Some(a), + (None, Some(c)) => Some(c), + (Some(a), Some(c)) => Some(a.min(c)), + } + .filter(|_| measured_room.is_some_and(|room| room <= platform_cap) && hard >= machine_limit); + + MemoryBudget { soft_limit: soft, hard_limit: hard, available_ram: ram, warning_threshold, measured_free } +} + +/// Hard room (what an approved import may use) when `a` bytes of RAM are free: three quarters of +/// it, leaving at least [`LOW_MEMORY_RESERVE`]. +fn ram_hard_room(a: u64) -> u64 { + let quarter_rule = ((a as u128 * 3) / 4) as u64; + quarter_rule.min(a.saturating_sub(LOW_MEMORY_RESERVE)) +} + +/// Automatic room when `a` bytes of RAM are free: half of it, up to [`MAX_SOFT_BUDGET`], leaving +/// 1 GiB free or, when that is less, a quarter of it. +fn ram_soft_room(a: u64) -> u64 { + (a / 2).min(MAX_SOFT_BUDGET).min(a.saturating_sub(AUTOMATIC_RAM_RESERVE).max(a / 4)) +} + +/// Hard room when `c` bytes of commit headroom remain: all but a reserve of a quarter, at least +/// [`LOW_MEMORY_RESERVE`] and at most [`COMMIT_RESERVE`]. +fn commit_hard_room(c: u64) -> u64 { + // `clamp` panics only if its bounds are out of order, and they are constants. + const { assert!(LOW_MEMORY_RESERVE <= COMMIT_RESERVE) }; + let reserve = (c / 4).clamp(LOW_MEMORY_RESERVE, COMMIT_RESERVE); + c.saturating_sub(reserve) +} + +/// Automatic room when `c` bytes of commit headroom remain. +fn commit_soft_room(c: u64) -> u64 { + c.saturating_sub(COMMIT_RESERVE).max(c / 4) } /// Result of evaluating an import admission request. @@ -411,7 +487,9 @@ fn evaluate_psd_admission_for( } cap } - BudgetDecision::Refused => return Err(IoError::PsdImportBudget { required, limit: budget.hard_limit }), + BudgetDecision::Refused => { + return Err(IoError::PsdImportBudget { required, limit: budget.hard_limit, free_memory: budget.refusal_free_memory(required) }); + } }; if required > authorized_total { @@ -448,7 +526,7 @@ pub(crate) fn nested_import_local_bound(required: u64, caller_limit: u64, approv (caller, approved) => caller.or(approved), }; match limit { - Some(limit) if required > limit => Err(IoError::PsdImportBudget { required, limit }), + Some(limit) if required > limit => Err(IoError::PsdImportBudget { required, limit, free_memory: None }), _ => Ok(()), } } @@ -767,7 +845,7 @@ mod tests { for (approved, estimate) in [(false, None), (true, None), (true, Some(u64::MAX))] { match evaluate_psd_admission_for(Platform::Native, u64::MAX, 0, 0, approved, estimate, MemorySnapshot::UNKNOWN) { - Err(IoError::PsdImportBudget { required, limit }) => { + Err(IoError::PsdImportBudget { required, limit, .. }) => { assert_eq!((required, limit), (u64::MAX, u64::MAX), "the typed refusal keeps the estimate and limit unchanged") } other => panic!("approved={approved}, estimate={estimate:?}: expected a hard refusal, got {other:?}"), @@ -777,7 +855,7 @@ mod tests { // A caller limit equal to the sentinel, or an approved estimate of it, is no exception. assert!(matches!( evaluate_psd_admission_for(Platform::Native, u64::MAX, 0, u64::MAX, true, Some(u64::MAX), MemorySnapshot::UNKNOWN), - Err(IoError::PsdImportBudget { required: u64::MAX, limit: u64::MAX }) + Err(IoError::PsdImportBudget { required: u64::MAX, limit: u64::MAX, .. }) )); } @@ -816,8 +894,9 @@ mod tests { let snap_tight = MemorySnapshot { available_ram: None, swap_free: None, commit_headroom: Some(gb) }; let b_tight = resolve_budget(snap_tight, None); - assert_eq!(b_tight.hard_limit, gb - COMMIT_RESERVE); - assert_eq!(b_tight.soft_limit, gb - COMMIT_RESERVE); + // 1 GiB of headroom keeps a quarter free (256 MiB), not the full 512 MiB. + assert_eq!(b_tight.hard_limit, 768 << 20); + assert_eq!(b_tight.soft_limit, 512 << 20); let snap_room = MemorySnapshot { available_ram: None, swap_free: None, commit_headroom: Some(4 * gb) }; let b_room = resolve_budget(snap_room, None); @@ -995,7 +1074,7 @@ mod tests { assert!(evaluate_psd_admission(26 * gb, 18 * gb, 0, true, Some(26 * gb - 1), snap).is_err()); // 10 GiB non-spillable is over H: refused even when approved. let err = evaluate_psd_admission(28 * gb, 18 * gb, 0, true, Some(28 * gb), snap).unwrap_err(); - assert!(matches!(err, IoError::PsdImportBudget { required, limit } if required == 28 * gb && limit == 27 * gb)); + assert!(matches!(err, IoError::PsdImportBudget { required, limit, .. } if required == 28 * gb && limit == 27 * gb)); // A spillable share claimed beyond the total is clamped to the total. assert!(evaluate_psd_admission(5 * gb, u64::MAX, 0, false, None, snap).is_ok()); // The caller's explicit limit still bounds the whole import. @@ -1081,4 +1160,193 @@ mod tests { let without_swap = MemorySnapshot { available_ram: Some(4 * gb), swap_free: None, commit_headroom: None }; assert_eq!(resolve_budget_for(Platform::Native, with_swap, None), resolve_budget_for(Platform::Native, without_swap, None)); } + + const MIB: u64 = 1 << 20; + const GIB: u64 = 1 << 30; + + fn native_budget(ram: Option, commit: Option) -> MemoryBudget { + resolve_budget_for(Platform::Native, MemorySnapshot { available_ram: ram, swap_free: None, commit_headroom: commit }, None) + } + + fn hard_refusal(result: Result) -> (u64, u64, Option) { + match result { + Err(IoError::PsdImportBudget { required, limit, free_memory }) => (required, limit, free_memory), + other => panic!("expected a hard refusal, got {other:?}"), + } + } + + #[test] + fn low_ram_boundaries_keep_room_for_small_files() { + if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { + return; + } + // (free RAM, soft limit, hard limit) + let table = [ + (0, 0, 0), + (128 * MIB, 0, 0), + (256 * MIB, 0, 0), + (256 * MIB + 1, 64 * MIB, 1), + (512 * MIB, 128 * MIB, 256 * MIB), + (GIB, 256 * MIB, 768 * MIB), + (GIB + 1, 256 * MIB, 768 * MIB), + (1366 * MIB, 342 * MIB, 2049 * MIB / 2), + (3 * GIB / 2, 512 * MIB, 1152 * MIB), + (2 * GIB, GIB, 3 * GIB / 2), + (3 * GIB, 3 * GIB / 2, 9 * GIB / 4), + (4 * GIB, 2 * GIB, 3 * GIB), + (8 * GIB, 4 * GIB, 6 * GIB), + ]; + for (ram, soft, hard) in table { + let b = native_budget(Some(ram), None); + // The soft limit is also bounded by the hard limit: 256 MiB + 1 leaves 1 byte. + let soft = soft.min(hard); + assert_eq!((b.soft_limit, b.hard_limit), (soft, hard), "free RAM {ram}"); + assert_eq!(b.warning_threshold, None); + } + } + + #[test] + fn low_commit_boundaries_keep_room_for_small_files() { + if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { + return; + } + // (commit headroom, soft limit, hard limit) with plenty of RAM, then with RAM unknown. + let table = [ + (0, 0, 0), + (128 * MIB, 0, 0), + (256 * MIB, 0, 0), + (512 * MIB, 128 * MIB, 256 * MIB), + (512 * MIB + 1, 128 * MIB, 256 * MIB + 1), + (GIB, 512 * MIB, 768 * MIB), + (3 * GIB / 2, GIB, 1152 * MIB), + (2 * GIB, 3 * GIB / 2, 3 * GIB / 2), + (5 * GIB, 9 * GIB / 2, 9 * GIB / 2), + ]; + for (commit, soft, hard) in table { + let b = native_budget(Some(64 * GIB), Some(commit)); + assert_eq!((b.soft_limit, b.hard_limit), (soft, hard), "commit {commit}, RAM known"); + let unknown = native_budget(None, Some(commit)); + assert_eq!(unknown.hard_limit, hard, "commit {commit}, RAM unknown"); + assert_eq!(unknown.soft_limit, soft.min(2 * GIB), "commit {commit}, RAM unknown"); + assert_eq!(unknown.warning_threshold, Some(UNKNOWN_MEMORY_WARNING_THRESHOLD)); + } + } + + #[test] + fn budgets_grow_with_free_memory_and_leave_the_reserve() { + if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { + return; + } + for by_commit in [false, true] { + let (mut last_soft, mut last_hard) = (0, 0); + let mut measured = 0; + while measured <= 9 * GIB { + let b = if by_commit { native_budget(Some(64 * GIB), Some(measured)) } else { native_budget(Some(measured), None) }; + assert!(b.soft_limit <= b.hard_limit, "commit={by_commit} {measured}"); + assert!(b.hard_limit <= measured, "commit={by_commit} {measured}"); + assert!(b.soft_limit >= last_soft && b.hard_limit >= last_hard, "commit={by_commit} {measured}: not non-decreasing"); + if b.hard_limit > 0 { + assert!(measured - b.hard_limit >= LOW_MEMORY_RESERVE, "commit={by_commit} {measured}: reserve not kept"); + } + (last_soft, last_hard) = (b.soft_limit, b.hard_limit); + measured += 16 * MIB; + } + } + } + + #[test] + fn a_small_file_opens_on_a_machine_with_little_free_memory() { + if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { + return; + } + let b = native_budget(Some(900 * MIB), None); + assert_eq!(b.decide(100 * MIB), BudgetDecision::Automatic); + assert_eq!(b.decide(300 * MIB), BudgetDecision::RequiresApproval); + assert_eq!(b.decide(700 * MIB), BudgetDecision::Refused); + + // The reported case: about 190 MB beyond 1 GiB free. + let snap = MemorySnapshot { available_ram: Some(190_693_376 + GIB), swap_free: None, commit_headroom: None }; + let (required, limit, free) = hard_refusal(evaluate_psd_admission_for(Platform::Native, 1_136_504_592, 0, 0, false, None, snap)); + assert_eq!((required, limit), (1_136_504_592, (190_693_376 + GIB) * 3 / 4)); + assert_eq!(free, Some(190_693_376 + GIB)); + assert!(matches!(evaluate_psd_admission_for(Platform::Native, 500_000_000, 0, 0, false, None, snap), Err(IoError::PsdApprovalRequired { .. }))); + } + + #[test] + fn refusals_report_free_memory_only_when_measured_memory_set_the_limit() { + if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { + return; + } + let snap = |ram, commit| MemorySnapshot { available_ram: ram, swap_free: None, commit_headroom: commit }; + let refuse = |required, caller, snapshot| hard_refusal(evaluate_psd_admission_for(Platform::Native, required, 0, caller, false, None, snapshot)); + + // RAM binds. + assert_eq!(refuse(2 * GIB, 0, snap(Some(GIB), None)), (2 * GIB, 768 * MIB, Some(GIB))); + // Commit headroom below RAM binds. + assert_eq!(refuse(2 * GIB, 0, snap(Some(64 * GIB), Some(GIB))), (2 * GIB, 768 * MIB, Some(GIB))); + // Commit headroom alone (RAM unknown). + assert_eq!(refuse(2 * GIB, 0, snap(None, Some(GIB))), (2 * GIB, 768 * MIB, Some(GIB))); + // Zero measured RAM (also the fail-closed unreadable limit) refuses everything. + assert_eq!(refuse(1, 0, snap(Some(0), None)), (1, 0, Some(0))); + // A binding caller limit is not the machine's. + assert_eq!(refuse(2 * GIB, GIB, snap(Some(8 * GIB), None)), (2 * GIB, GIB, None)); + assert_eq!(refuse(2 * GIB, GIB, MemorySnapshot::UNKNOWN), (2 * GIB, GIB, None)); + // The overflow sentinel is not helped by closing apps. + assert_eq!(refuse(u64::MAX, 0, snap(Some(8 * GIB), None)), (u64::MAX, 6 * GIB, None)); + assert_eq!(refuse(u64::MAX, 0, MemorySnapshot::UNKNOWN), (u64::MAX, u64::MAX, None)); + + // On the budget itself. + assert_eq!(native_budget(Some(8 * GIB), None).measured_free, Some(8 * GIB)); + assert_eq!(native_budget(Some(8 * GIB), None).refusal_free_memory(u64::MAX), None); + assert_eq!(native_budget(None, None).measured_free, None); + assert_eq!(resolve_budget_for(Platform::Web, snap(Some(GIB), None), None).measured_free, None); + // A caller limit equal to the machine's limit does not hide the measurement. + assert_eq!(resolve_budget_for(Platform::Native, snap(Some(8 * GIB), None), Some(6 * GIB)).measured_free, Some(8 * GIB)); + assert_eq!(resolve_budget_for(Platform::Native, snap(Some(8 * GIB), None), Some(6 * GIB - 1)).measured_free, None); + } + + #[test] + fn scratch_credit_adds_to_the_low_memory_limits() { + if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { + return; + } + let snap = |ram, commit| MemorySnapshot { available_ram: ram, swap_free: None, commit_headroom: commit }; + with_mock_scratch(Some(40 * GIB), || { + let one_gib = snap(Some(GIB), None); + let out = evaluate_psd_admission_for(Platform::Native, 2 * GIB + 100 * MIB, 2 * GIB, 0, false, None, one_gib).unwrap(); + assert_eq!(out.authorized_total, 256 * MIB + 2 * GIB); + + let err = evaluate_psd_admission_for(Platform::Native, 2 * GIB + 500 * MIB, 2 * GIB, 0, false, None, one_gib).unwrap_err(); + assert!(matches!(err, IoError::PsdApprovalRequired { soft_limit, hard_limit, .. } + if soft_limit == 2 * GIB + 256 * MIB && hard_limit == 2 * GIB + 768 * MIB)); + + let refused = hard_refusal(evaluate_psd_admission_for(Platform::Native, 2 * GIB + 800 * MIB, 2 * GIB, 0, true, None, one_gib)); + assert_eq!(refused, (2 * GIB + 800 * MIB, 2 * GIB + 768 * MIB, Some(GIB))); + + // Fail closed: nothing measured, so only the credited tile share passes. + let zero = snap(Some(0), None); + assert!(evaluate_psd_admission_for(Platform::Native, 2 * GIB, 2 * GIB, 0, false, None, zero).is_ok()); + let refused = hard_refusal(evaluate_psd_admission_for(Platform::Native, 2 * GIB + 1, 2 * GIB, 0, true, None, zero)); + assert_eq!(refused, (2 * GIB + 1, 2 * GIB, Some(0))); + + // Low commit headroom with plenty of RAM: H = 256 MiB + credit. + let low_commit = snap(Some(64 * GIB), Some(512 * MIB)); + let refused = hard_refusal(evaluate_psd_admission_for(Platform::Native, 2 * GIB + 300 * MIB, 2 * GIB, 0, true, None, low_commit)); + assert_eq!(refused, (2 * GIB + 300 * MIB, 2 * GIB + 256 * MIB, Some(512 * MIB))); + let b = resolve_budget_credited(Platform::Native, low_commit, None, 2 * GIB); + assert_eq!((b.soft_limit, b.hard_limit), (2 * GIB + 128 * MIB, 2 * GIB + 256 * MIB)); + }); + } + + #[test] + fn low_memory_advice_names_the_free_memory() { + assert_eq!(low_memory_advice(&None), ""); + assert_eq!( + low_memory_advice(&Some(0)), + ". PhotoCraft measured no free memory, or could not read this system's memory limit. Close other apps and try again" + ); + assert_eq!(low_memory_advice(&Some(900 << 20)), ". About 900 MiB of memory is free. Close other apps and try again"); + assert_eq!(low_memory_advice(&Some(3 << 30)), ". About 3.0 GiB of memory is free. Close other apps and try again"); + assert_eq!(low_memory_advice(&Some(512)), ". About 512 B of memory is free. Close other apps and try again"); + } } diff --git a/crates/io/src/psd_budget.rs b/crates/io/src/psd_budget.rs index a80230322d..9a91469add 100644 --- a/crates/io/src/psd_budget.rs +++ b/crates/io/src/psd_budget.rs @@ -249,7 +249,7 @@ pub fn estimate(file: &PsdFile) -> Result { pub fn check(file: &PsdFile, limit: u64) -> Result { let est = estimate(file)?; if est.total_bytes > limit { - return Err(IoError::PsdImportBudget { required: est.total_bytes, limit }); + return Err(IoError::PsdImportBudget { required: est.total_bytes, limit, free_memory: None }); } Ok(est) } diff --git a/crates/io/src/psd_budget/tests.rs b/crates/io/src/psd_budget/tests.rs index 9e24676716..edf83b7612 100644 --- a/crates/io/src/psd_budget/tests.rs +++ b/crates/io/src/psd_budget/tests.rs @@ -284,6 +284,29 @@ fn zero_available_ram_refuses_admission() { }); } +/// A small file opens with 900 MiB free (it used to be refused outright below 1 GiB); with no +/// free memory at all it is refused and the refusal says why. +#[test] +fn small_file_opens_with_little_free_memory_and_a_refusal_names_it() { + if cfg!(target_arch = "wasm32") { + return; // the web policy ignores a host snapshot; it has its own tests + } + let file = testgen::small(Version::Psd, Compression::Zip); + let bytes = file.to_bytes().unwrap(); + + let low = crate::memory::MemorySnapshot { available_ram: Some(900 << 20), swap_free: None, commit_headroom: None }; + crate::memory::with_mock_memory(low, || { + let res = crate::import_with_options("small.psd", &bytes, &crate::ImportOptions::default()); + assert!(res.is_ok(), "{:?}", res.map(|_| ())); + }); + + let none = crate::memory::MemorySnapshot { available_ram: Some(0), swap_free: None, commit_headroom: None }; + crate::memory::with_mock_memory(none, || { + let res = crate::import_with_options("small.psd", &bytes, &crate::ImportOptions::default()); + assert!(matches!(res, Err(IoError::PsdImportBudget { limit: 0, free_memory: Some(0), .. }))); + }); +} + #[test] fn preflight_rejection_and_approval_files_release_gate() { if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { @@ -337,7 +360,7 @@ fn retry_estimate_cap_rejects_altered_or_enlarged_file() { // 3. Exceeds hard limit: refused completely regardless of approval let res = crate::memory::evaluate_psd_admission(10 * gb, 0, 0, true, Some(10 * gb), snap); - assert!(matches!(res, Err(IoError::PsdImportBudget { required, limit }) if required == 10 * gb && limit == 9 * gb)); + assert!(matches!(res, Err(IoError::PsdImportBudget { required, limit, .. }) if required == 10 * gb && limit == 9 * gb)); } #[test] @@ -413,7 +436,7 @@ fn nested_imports_consume_from_shared_allowance_and_do_not_reset_parent() { // Second nested consume exceeds remaining 1 GB let err = crate::psd_gate::consume_attempt_allowance(2 * gb).unwrap_err(); - assert!(matches!(err, IoError::PsdImportBudget { required, limit } if required == 2 * gb && limit == gb)); + assert!(matches!(err, IoError::PsdImportBudget { required, limit, .. } if required == 2 * gb && limit == gb)); // Allowance is still 1 GB (failed attempt does not reset or corrupt parent) assert!(crate::psd_gate::consume_attempt_allowance(gb).is_ok()); @@ -472,7 +495,7 @@ fn real_import_propagates_high_memory_approval_and_its_estimate_bound() { let lowered = crate::ImportOptions { psd_high_memory_approved: true, psd_max_pixel_bytes: required - 1, ..Default::default() }; assert!(matches!( crate::import_with_options("approval.psd", &bytes, &lowered), - Err(IoError::PsdImportBudget { required: r, limit: l }) if r == required && l == required - 1 + Err(IoError::PsdImportBudget { required: r, limit: l, .. }) if r == required && l == required - 1 )); }); }); @@ -527,7 +550,7 @@ fn memory_drop_while_waiting_for_the_gate_refuses_before_decoding() { let result = rx.recv_timeout(Duration::from_secs(10)).expect("the post-gate recheck must refuse instead of decoding"); match result { - Err(IoError::PsdImportBudget { required, limit: 0 }) => assert!(required > 0), + Err(IoError::PsdImportBudget { required, limit: 0, .. }) => assert!(required > 0), other => panic!("expected the post-gate recheck to refuse with a zero budget, got {other:?}"), } handle.join().expect("worker thread"); diff --git a/crates/io/src/psd_gate.rs b/crates/io/src/psd_gate.rs index d661166018..ce6c36dab2 100644 --- a/crates/io/src/psd_gate.rs +++ b/crates/io/src/psd_gate.rs @@ -79,14 +79,14 @@ pub(crate) fn consume_attempt_allowance(bytes: u64) -> Result<(), IoError> { ATTEMPT_ALLOWANCE.with(|a| match a.get() { Some(remaining) => { if bytes == u64::MAX || bytes > remaining { - return Err(IoError::PsdImportBudget { required: bytes, limit: remaining }); + return Err(IoError::PsdImportBudget { required: bytes, limit: remaining, free_memory: None }); } a.set(Some(remaining - bytes)); Ok(()) } None => { // Unknown allowance during reentrant gate must fail closed. - Err(IoError::PsdImportBudget { required: bytes, limit: 0 }) + Err(IoError::PsdImportBudget { required: bytes, limit: 0, free_memory: None }) } }) } @@ -193,7 +193,7 @@ mod tests { set_active_attempt_budget(u64::MAX); let err = consume_attempt_allowance(u64::MAX).expect_err("the overflow sentinel is never a charge"); - assert!(matches!(err, IoError::PsdImportBudget { required, limit } if required == u64::MAX && limit == u64::MAX)); + assert!(matches!(err, IoError::PsdImportBudget { required, limit, .. } if required == u64::MAX && limit == u64::MAX)); assert_eq!(active_attempt_remaining(), Some(u64::MAX), "a refused sentinel charge leaves the parent's allowance untouched"); // One byte below the sentinel is an ordinary, enormous charge. diff --git a/crates/io/src/tiff_layers.rs b/crates/io/src/tiff_layers.rs index 5c44776a01..413fee396e 100644 --- a/crates/io/src/tiff_layers.rs +++ b/crates/io/src/tiff_layers.rs @@ -1319,7 +1319,7 @@ mod tests { let lowered = ImportOptions { psd_high_memory_approved: true, psd_max_pixel_bytes: required - 1, ..Default::default() }; assert!(matches!( crate::import_with_options("l.tif", &bytes, &lowered), - Err(IoError::PsdImportBudget { required: r, limit: l }) if r == required && l == required - 1 + Err(IoError::PsdImportBudget { required: r, limit: l, .. }) if r == required && l == required - 1 )); }); }); @@ -1351,7 +1351,7 @@ mod tests { // remaining allowance is untouched. crate::psd_gate::set_active_attempt_budget(charged - 1); match crate::import_with_options_and_interrupt("charge.tif", &bytes, &ImportOptions::default(), &ctl) { - Err(IoError::PsdImportBudget { required, limit }) => assert_eq!((required, limit), (charged, charged - 1)), + Err(IoError::PsdImportBudget { required, limit, .. }) => assert_eq!((required, limit), (charged, charged - 1)), other => panic!("expected an import-budget refusal, got {other:?}"), } assert_eq!(crate::psd_gate::active_attempt_remaining(), Some(charged - 1), "a failed nested import leaves the allowance as it was"); @@ -1462,7 +1462,7 @@ mod tests { crate::memory::with_mock_memory(zero, || { crate::flat::with_before_composite_decode(hook(&decoded), || { match crate::import_with_options("bogus.tif", &bytes, &ImportOptions::default()) { - Err(IoError::PsdImportBudget { limit: 0, required }) => assert!(required > 0), + Err(IoError::PsdImportBudget { limit: 0, required, .. }) => assert!(required > 0), other => panic!("expected an import-budget refusal, got {other:?}"), } assert_eq!(decoded.get(), 0, "a refused file must not decode its composite"); @@ -1504,7 +1504,7 @@ mod tests { crate::memory::with_mock_memory(zero, || { crate::flat::with_before_composite_decode(hook(&decoded), || { match crate::import_with_options("zero.tif", &bytes, &ImportOptions::default()) { - Err(IoError::PsdImportBudget { limit: 0, required }) => assert!(required > 0), + Err(IoError::PsdImportBudget { limit: 0, required, .. }) => assert!(required > 0), other => panic!("expected an import-budget refusal, got {other:?}"), } assert_eq!(decoded.get(), 0, "a refused file must not decode its composite"); @@ -1589,7 +1589,7 @@ mod tests { let result = rx.recv_timeout(Duration::from_secs(10)).expect("the post-gate recheck must refuse instead of decoding"); match result { - Err(IoError::PsdImportBudget { limit: 0, required }) => assert!(required > 0), + Err(IoError::PsdImportBudget { limit: 0, required, .. }) => assert!(required > 0), other => panic!("expected the post-gate recheck to refuse with a zero budget, got {other:?}"), } assert!(!decoded.load(Ordering::SeqCst), "the composite must not be decoded after the post-gate refusal"); diff --git a/crates/ui-egui/src/dialogs/open_prompt.rs b/crates/ui-egui/src/dialogs/open_prompt.rs index 2da15f9fb4..bcd9f0b7b9 100644 --- a/crates/ui-egui/src/dialogs/open_prompt.rs +++ b/crates/ui-egui/src/dialogs/open_prompt.rs @@ -407,6 +407,10 @@ fn show_memory(app: &mut PhotocraftApp, prompt: &MemoryPrompt) { }; message.push_str("\n\n"); message.push_str(tl!("Opening this document uses more memory and may slow down your computer or other running applications.")); + if available.is_some_and(|a| a.saturating_sub(prompt.estimated) < photocraft_io::memory::LOW_MEMORY_WARNING_MARGIN) { + message.push_str("\n\n"); + message.push_str(tl!("Little memory would be left after opening this file. Close other apps first, or PhotoCraft may run out of memory.")); + } if prompt.uncertain_capacity_warning { message.push_str("\n\n"); message.push_str(tl!( diff --git a/crates/ui-egui/src/psd_open_tests.rs b/crates/ui-egui/src/psd_open_tests.rs index 719c760f35..02c18313f5 100644 --- a/crates/ui-egui/src/psd_open_tests.rs +++ b/crates/ui-egui/src/psd_open_tests.rs @@ -758,6 +758,27 @@ fn memory_prompt_states_unknown_capacity_and_the_eight_gib_warning() { assert!(app.ui.dialogs.is_empty()); } +/// The prompt warns when opening the file would leave less than 1 GiB of the measured free +/// memory, and stays quiet when there is room to spare. +#[test] +fn memory_prompt_warns_when_little_memory_would_be_left() { + let gb = 1u64 << 30; + let mut app = app(); + let available = 3 * gb + gb / 2; + let id = store_preflight(&mut app, "tight.psd", 3 * gb, Some(available)); + let tab = crate::jobs_ui::OpenTab { job: photocraft_engine::jobs::JobId(3), name: "tight.psd".into(), path: None, slot: None, order: None }; + crate::jobs_ui::finish_open_tab( + &mut app, + &tab, + &json!({"pendingPreflight": id, "estimated": 3 * gb, "availableRam": available, "capacityUnknown": false, "uncertainCapacityWarning": false}), + ) + .unwrap(); + let message = app.ui.dialogs[0].fields["message"].as_str().unwrap(); + assert!(message.contains("Little memory would be left") && message.contains("may run out of memory"), "{message}"); + let dialog = app.ui.dialogs[0].id; + assert_eq!(call(&mut app, "ui.dialog.cancel", json!({"dialog": dialog}))["ok"], true); +} + /// #10897: reopening an ordinary file whose high-memory prompt is already waiting shows that /// prompt instead of queueing a second one and a second open; a template still opens again as /// another Untitled, like one that is still loading. diff --git a/docs/development.md b/docs/development.md index 18a7610ee0..80b21bef67 100644 --- a/docs/development.md +++ b/docs/development.md @@ -744,8 +744,20 @@ clipped. Native desktop platforms use an adaptive memory policy evaluated against currently available physical RAM (excluding swap) and, where measurable, system commit headroom: -- **Hard budget (H)** for known RAM: $\min(75\% \text{ RAM}, \text{RAM} - 1 \text{ GiB}, \text{commit headroom} - 512 \text{ MiB}, \text{aggregate address-space cap})$. -- **Soft budget (S)**: $\min(50\% \text{ RAM}, 8 \text{ GiB}, H)$. +- **Hard budget (H)** for known memory, with A the available RAM and C the commit headroom: + $\min(\lfloor 3A/4 \rfloor, A - 256 \text{ MiB}, C - r(C), \text{aggregate address-space cap})$, where the + commit reserve $r(C) = \min(\max(C/4, 256 \text{ MiB}), 512 \text{ MiB})$. An approved import therefore + always leaves at least `LOW_MEMORY_RESERVE` (256 MiB) free. +- **Soft budget (S)**: $\min(A/2, 8 \text{ GiB}, \max(A - 1 \text{ GiB}, A/4), \max(C - 512 \text{ MiB}, C/4), H)$ + (the commit term only where C is known). +- The low-memory band: with more than 4/3 GiB free, automatic opening is unchanged (it leaves + 1 GiB free). Below that, up to a quarter of the free RAM opens automatically and the rest goes + to approval, up to $\min(3A/4, A - 256 \text{ MiB})$. The commit reserve shrinks from a flat + 512 MiB to a quarter of the headroom with a 256 MiB floor. At or below 256 MiB of either + measurement there is no room and every file is refused. For example 1 GiB free gives S = 256 MiB + and H = 768 MiB; 512 MiB free gives S = 128 MiB and H = 256 MiB. +- Scratch-disk credit (see `docs/scratch-disks.md`) is added on top of these limits before a + caller's limit applies; non-spillable memory stays against RAM alone. - Files requiring $\le S$ are admitted automatically. - Files requiring $S < E \le H$ prompt for explicit one-shot user confirmation ("Open anyway") in the UI. Headless callers default to no interactive dialog and require explicit per-invocation opt-in: - Engine commands: `file.openAs` and `file.placeEmbedded` take `approveHighMemory: bool` (default `false`); `file.approveMemory` takes `{"id": u64, "approve": bool = true}`. @@ -756,8 +768,8 @@ physical RAM (excluding swap) and, where measurable, system commit headroom: - Files requiring $> H$ are refused and cannot be overridden. - When available RAM is unknown (an unsupported platform or a failed provider), native automatic admission is 2 GiB, not 8 GiB: - $S = \min(2 \text{ GiB}, H)$, where $H$ is the aggregate address-space cap - (`native_platform_cap`), reduced by known commit headroom. Estimates above $S$ need the same + $S = \min(2 \text{ GiB}, \max(C - 512 \text{ MiB}, C/4), H)$, where $H$ is the aggregate + address-space cap (`native_platform_cap`), reduced by known commit headroom as above. Estimates above $S$ need the same explicit, per-invocation approval as known-memory estimates above $S$, and approval is possible up to $H$. Estimates above 8 GiB remain approvable; `MemoryBudget::warning_threshold` and `MemoryBudget::requires_uncertainty_warning` let callers show the uncertain-capacity warning. @@ -804,7 +816,9 @@ physical RAM (excluding swap) and, where measurable, system commit headroom: is `memsw limit - memory limit - swap already used`, and those allowances are combined separately from RAM headroom. A memory cgroup whose mount, path, membership, limit or usage cannot be read, or whose membership file is malformed or empty, fails closed (available memory - is treated as zero) rather than falling back to unrestricted host RAM. + is treated as zero, `Some(0)`) rather than falling back to unrestricted host RAM. That reading + gives H = 0 from the formula, so it stays refused (only a credited tile share can pass), and + the refusal says PhotoCraft measured no free memory or could not read the system's memory limit. - Applications can lower the admission threshold with `ImportOptions::psd_max_pixel_bytes`; explicit caller limits may lower admission, never bypass the hard boundary. - Concurrent PSD decodes are serialized using a process-wide, cancellation-aware admission gate, with nested decodes sharing the enclosing allowance. - A pending high-memory approval is bound to one source and one estimate. The engine keeps at @@ -821,8 +835,8 @@ physical RAM (excluding swap) and, where measurable, system commit headroom: for approval. Windows has no stable file-index API in `std`, so its identity is the length and modification time (a different file with both preserved is not distinguished there). The `file.approveMemory` permission is one-shot and is not journaled or recorded in an action. A - refusal above the hard ceiling is the typed `PsdHardBudget` error (name, required, limit), not - a string, and it releases the queued source. + refusal above the hard ceiling is the typed `PsdHardBudget` error (name, required, limit, + `free_memory`), not a string, and it releases the queued source. - A layered TIFF (Photoshop layer data in tags 37724/34377) is admitted by the same policy before its composite is decoded. The page's tags are read into the PSD model first, without pixels; then that model's layer/workspace estimate, the decoded composite and its @@ -847,15 +861,23 @@ physical RAM (excluding swap) and, where measurable, system commit headroom: opened without Photoshop layers. Headless RPC and MCP report approval-required memory errors with byte counts (`required`, -`softLimit`, `hardLimit`, and `availableRam` when known). A hard-budget refusal reports only -`required` and `hardLimit`: no approval can raise that ceiling, so there is no soft limit or -capacity to act on. This holds for `doc.open` and `doc.place` and for the MCP `doc_open` and +`softLimit`, `hardLimit`, and `availableRam` when known). A hard-budget refusal reports +`required` and `hardLimit`, plus `available` and `availableRam` when measured free memory set the +limit (the typed `free_memory` field); there is never a soft limit, because no approval can raise +that ceiling. A limit set by the caller, the platform cap, or an unknown capacity carries no +free-memory figure. This holds for `doc.open` and `doc.place` and for the MCP `doc_open` and `doc_place` tools, including a refusal from `file.openAs`/`file.placeEmbedded` reached through a command. GUI automation imports currently cross a service boundary that returns string errors. +When measured free memory set the limit, the refusal message gives the free amount and ends with +"Close other apps and try again"; a reading of zero says PhotoCraft measured no free memory or +could not read the system's memory limit. The approval prompt warns that PhotoCraft may run out of +memory when opening the file would leave less than 1 GiB of the measured free RAM +(`LOW_MEMORY_WARNING_MARGIN`). + An import exceeding the hard budget returns `IoError::PsdImportBudget` with the estimated -and allowed byte counts; imports requiring confirmation return `IoError::PsdApprovalRequired` -when unapproved. Damaged-file recovery never bypasses these memory guards. The legacy +and allowed byte counts (and `free_memory` when measured memory set the limit); imports requiring +confirmation return `IoError::PsdApprovalRequired` when unapproved. Damaged-file recovery never bypasses these memory guards. The legacy `psd_to_document` conversion APIs cannot return an error, so they return an empty document and an explicit warning on preflight refusal. Layer and mask pixel allocation failures also fail ordinary imports instead of opening partial content. From 20c0d65342ba7731e74949705fb9efaaa356df9c Mon Sep 17 00:00:00 2001 From: Khawar Zamman Wani Date: Fri, 6 Nov 2026 18:01:16 +0530 Subject: [PATCH 2/8] PSD admission: no free-memory advice when the platform cap also binds Where the 32-bit cap equals the measured room, more free memory can't raise the limit, so the refusal shouldn't tell the user to close other apps. --- crates/io/src/memory.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/io/src/memory.rs b/crates/io/src/memory.rs index 24e7b873ac..c4a304defa 100644 --- a/crates/io/src/memory.rs +++ b/crates/io/src/memory.rs @@ -251,6 +251,7 @@ pub fn low_memory_advice(free_memory: &Option) -> String { } } +/// A size for people: "3.0 GiB", "900 MiB", "512 B". fn size_text(bytes: u64) -> String { if bytes >= 1 << 30 { format!("{:.1} GiB", bytes as f64 / (1u64 << 30) as f64) @@ -374,14 +375,15 @@ fn resolve_budget_credited(platform: Platform, snapshot: MemorySnapshot, caller_ soft = soft.min(hard); } - // Report the measured free memory only when it is what set the hard limit. + // Report the measured free memory only when it alone set the hard limit: where the platform + // cap equals the measured room, more free memory could not raise the limit. let measured_free = match (ram, commit) { (None, None) => None, (Some(a), None) => Some(a), (None, Some(c)) => Some(c), (Some(a), Some(c)) => Some(a.min(c)), } - .filter(|_| measured_room.is_some_and(|room| room <= platform_cap) && hard >= machine_limit); + .filter(|_| measured_room.is_some_and(|room| room < platform_cap) && hard >= machine_limit); MemoryBudget { soft_limit: soft, hard_limit: hard, available_ram: ram, warning_threshold, measured_free } } @@ -1305,6 +1307,18 @@ mod tests { assert_eq!(resolve_budget_for(Platform::Native, snap(Some(8 * GIB), None), Some(6 * GIB - 1)).measured_free, None); } + /// Only 32-bit native has a finite cap: when it equals the measured room, freeing memory + /// cannot raise the limit, so no free-memory figure is reported. + #[test] + fn a_platform_cap_equal_to_the_measured_room_reports_no_free_memory() { + let cap = native_platform_cap(); + if cap == u64::MAX { + return; + } + assert_eq!(native_budget(None, Some(cap + COMMIT_RESERVE)).measured_free, None); + assert_eq!(native_budget(None, Some(cap - 1 + COMMIT_RESERVE)).measured_free, Some(cap - 1 + COMMIT_RESERVE)); + } + #[test] fn scratch_credit_adds_to_the_low_memory_limits() { if cfg!(any(target_arch = "wasm32", target_pointer_width = "32")) { From 1eba8f6c722e8512a8d8d0e9fb72ab5a4e7e75e6 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Fri, 6 Nov 2026 17:50:56 +0100 Subject: [PATCH 3/8] Bound scratch capacity test query waits --- crates/raster/tests/scratch_capacity.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/raster/tests/scratch_capacity.rs b/crates/raster/tests/scratch_capacity.rs index 399456d650..f6f0733ad5 100644 --- a/crates/raster/tests/scratch_capacity.rs +++ b/crates/raster/tests/scratch_capacity.rs @@ -9,9 +9,11 @@ use photocraft_raster::spill; /// until the room and the status describe the same answer. fn answered_capacity() -> Option { let mut room = None; + let started = std::time::Instant::now(); for _ in 0..10 { room = spill::scratch_capacity(); while spill::query_threads_for_tests() != 0 { + assert!(started.elapsed() < std::time::Duration::from_secs(30), "scratch capacity query did not finish within 30 seconds: {:?}", spill::stats()); std::thread::sleep(std::time::Duration::from_millis(10)); } if room.is_none() == spill::free_space_unmeasured() { From d41e29a24f2001728dd5e6be04d941ef246dfc99 Mon Sep 17 00:00:00 2001 From: FredApps Date: Fri, 6 Nov 2026 17:50:34 +0100 Subject: [PATCH 4/8] fix(ui): identify the active scratch directory in warnings --- crates/ui-egui/src/prefs/prefs_ui/fields.rs | 10 ++++++++-- docs/development.md | 7 +++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/prefs/prefs_ui/fields.rs b/crates/ui-egui/src/prefs/prefs_ui/fields.rs index 173421736e..6b02ddf785 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/fields.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/fields.rs @@ -522,8 +522,14 @@ fn field_rows(ui: &mut egui::Ui, section: &str, obj: &mut Map, ke if ui.small_button(tl!("Add disk")).clicked() { items.push(json!({"enabled": true, "path": ""})); } - if photocraft_raster::spill::free_space_unmeasured() { - ui.add(egui::Label::new(RichText::new(tl!("This scratch disk doesn't report its free space, so PhotoCraft can't stop before it fills up, and large files get no extra room from it without a Scratch Size Limit. Set a limit or use a local disk.")).color(t.warning)).wrap()); + let scratch = photocraft_raster::spill::stats(); + if scratch.free_space_unmeasured { + let dir = scratch.dir.as_deref().map(|p| p.display().to_string()).unwrap_or_else(|| tl!("Unknown").into()); + let warning = crate::i18n::fmt( + tl!("The current scratch directory ({path}) doesn't report its free space, so PhotoCraft can't stop before it fills up, and large files get no extra room from it without a Scratch Size Limit. This warning applies to the current configuration; changes above take effect on Apply or OK. Set a limit or use a local disk."), + &[("path", &dir)], + ); + ui.add(egui::Label::new(RichText::new(warning).color(t.warning)).wrap()); } }); obj.insert(k, Value::Array(items)); diff --git a/docs/development.md b/docs/development.md index 9db419fb95..0a8069cc44 100644 --- a/docs/development.md +++ b/docs/development.md @@ -866,9 +866,12 @@ physical RAM (excluding swap) and, where measurable, system commit headroom: opened without Photoshop layers. Headless RPC and MCP report approval-required memory errors with byte counts (`required`, -`softLimit`, `hardLimit`, and `availableRam` when known). A hard-budget refusal reports +`softLimit`, `hardLimit`, and `availableRam` when known); here `availableRam` means available +physical RAM. A hard-budget refusal reports `required` and `hardLimit`, plus `available` and `availableRam` when measured free memory set the -limit (the typed `free_memory` field); there is never a soft limit, because no approval can raise +limit (the typed `free_memory` field). Both fields then contain the smaller of available RAM and +commit headroom (or the one measurement available), so `availableRam` can represent commit +headroom rather than physical RAM. There is never a soft limit, because no approval can raise that ceiling. A limit set by the caller, the platform cap, or an unknown capacity carries no free-memory figure. This holds for `doc.open` and `doc.place` and for the MCP `doc_open` and `doc_place` tools, including a refusal from `file.openAs`/`file.placeEmbedded` reached through a From c49c1fd6fc6552c76730be64dc0b0366d5a5abf3 Mon Sep 17 00:00:00 2001 From: FredApps Date: Fri, 6 Nov 2026 17:51:28 +0100 Subject: [PATCH 5/8] Describe scratch settings without promising memory admission Present the live scratch state as current configuration rather than the cause of a file's refusal. Explain that configured scratch capacity cannot override caller/build limits or replace non-spillable memory. Keep the typed error and admission policy unchanged, and add wording regressions. Formatting and whitespace checks passed. No local build or test execution; validation awaits hosted CI. --- crates/engine/src/error.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/error.rs b/crates/engine/src/error.rs index 9d78082944..4f464b79d7 100644 --- a/crates/engine/src/error.rs +++ b/crates/engine/src/error.rs @@ -82,16 +82,17 @@ fn build_limit_scope(limit: BuildMemoryLimit) -> String { } } -/// Exactly when spilling is on but gives an import no room: see `spill::scratch_capacity`. +/// The current scratch configuration, not the admission-time cause of a refusal. fn scratch_room_unmeasured() -> bool { let stats = photocraft_raster::spill::stats(); stats.free_space_unmeasured && stats.size_cap_bytes.is_none() } -/// The hint that follows the scope when the scratch disk's free space can't be measured. +/// Configuration guidance only: the refusal can instead be bound by a caller or build limit, +/// or by memory that cannot spill. Current scratch stats cannot identify that cause. fn unmeasured_scratch_hint(unmeasured: bool) -> &'static str { if unmeasured { - ". The scratch disk's free space can't be measured, so it adds no room for this file: set a Scratch Size Limit in Preferences > Scratch Disks, or use a local disk" + ". Current scratch configuration: free space can't be measured and no Scratch Size Limit is set. To configure scratch capacity, set a Scratch Size Limit in Preferences > Scratch Disks, or use a disk whose free space can be measured. Scratch capacity cannot override caller or build limits, or replace memory that cannot spill to disk" } else { "" } @@ -198,7 +199,18 @@ mod tests { fn unmeasured_scratch_hint_only_when_unmeasured() { assert_eq!(unmeasured_scratch_hint(false), ""); let hint = unmeasured_scratch_hint(true); - assert!(hint.starts_with(". ") && hint.contains("Scratch Size Limit") && hint.contains("local disk"), "{hint}"); + assert!(hint.starts_with(". Current scratch configuration:"), "{hint}"); + assert!(hint.contains("Scratch Size Limit in Preferences > Scratch Disks") && hint.contains("disk whose free space can be measured"), "{hint}"); + } + + #[test] + fn scratch_configuration_hint_does_not_promise_to_raise_a_binding_limit() { + let hint = unmeasured_scratch_hint(true); + // A caller-bound refusal can carry the same live scratch state as a capacity refusal. + // The note must describe that state without attributing this file's refusal to it. + assert!(hint.contains("cannot override caller or build limits"), "{hint}"); + assert!(hint.contains("memory that cannot spill to disk"), "{hint}"); + assert!(!hint.contains("for this file") && !hint.contains("so it adds no room"), "{hint}"); } #[test] From f23d1739331fa2fe4e9118015480a08c1544f6a3 Mon Sep 17 00:00:00 2001 From: Triwarr <170976175+Triwarr@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:39:40 -0300 Subject: [PATCH 6/8] Add an opt-in WinTab backend for pen pressure on Windows (#11331) * Add an opt-in WinTab backend for pen pressure on Windows Windows Ink stays the default. Preferences > Tools > Pen Tablet has a Tablet input dropdown (Windows Ink (default) / WinTab). WinTab opens a WinTab 1.4 system context on PhotoCraft's window and polls pressure and the eraser end (tilt on devices that report orientation axes; rotation only with a pen and driver that report twist) into the stylus feed. The row says to turn Windows Ink off in the tablet's software utility; the choice applies at next launch. - crates/tablet: Windows-only wintab/wintab-sys backend. Devices with orientation axes use the orientation packet; pressure-only devices open a packet without it and report neutral tilt. Leaving is the driver's hardware proximity message (WT_PROXIMITY) and the packet's out-of-context status, with a long fallback for drivers that never report either. Overlap priority follows window focus, and the message hook requests a frame for each WinTab message so pressure-only strokes update without cursor movement. - apps/photocraft: install the backend when the preference is on; poll it at the start of each frame; the installed wake repaints the UI. - ui-egui: TabletPollFn service; the Windows-only Pen Tablet row with the Tablet input dropdown and the note. - engine: tools.tabletInput (windowsInk/winTab), default windowsInk. - docs: hardware-parity WinTab gap entry, qualifying tilt to orientation axes and rotation to twist-reporting pens. Tested locally on a Wacom tablet with Windows Ink off: pressure, tilt and the eraser end reach the brush (rotation where the pen reports twist); the default path is unchanged. * WinTab: a message that finds the state borrowed is skipped, not a panic in the hook Co-Authored-By: Claude Opus 5.5 * WinTab: rustfmt Co-Authored-By: Claude Opus 5.5 * Scorecard: count the Tablet input setting Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 --- Cargo.lock | 22 ++ apps/photocraft/src/main.rs | 8 + apps/photocraft/src/tablet.rs | 39 +- crates/engine/src/prefs.rs | 9 + crates/engine/src/prefs/tests.rs | 14 + crates/engine/src/prefs/validate.rs | 5 +- crates/tablet/Cargo.toml | 11 +- crates/tablet/src/lib.rs | 5 + crates/tablet/src/windows.rs | 3 + crates/tablet/src/wintab.rs | 334 ++++++++++++++++++ crates/ui-egui/src/app.rs | 4 + crates/ui-egui/src/lib.rs | 2 +- crates/ui-egui/src/prefs/prefs_ui/fields.rs | 64 +++- crates/ui-egui/src/prefs/prefs_ui/graphics.rs | 2 + crates/ui-egui/src/services.rs | 6 + docs/hardware-parity.md | 21 +- docs/scorecard.md | 4 +- 17 files changed, 536 insertions(+), 17 deletions(-) create mode 100644 crates/tablet/src/wintab.rs diff --git a/Cargo.lock b/Cargo.lock index 05f7de9cf2..201ca61db0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5531,10 +5531,13 @@ name = "photocraft-tablet" version = "0.20.1" dependencies = [ "block2 0.6.2", + "log", "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-core-graphics", "objc2-foundation 0.3.2", + "windows", + "wintab", "x11rb", ] @@ -9208,6 +9211,25 @@ version = "0.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ef0ffc427f045c0cc9ebffd6f4f91153dcd2a1547b5c3c29ee3f541e16e95c6" +[[package]] +name = "wintab" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e05efdae68aa69ef1f4bc0148a5c6cf7f2bc234963a723252fd75f2f9e70d86" +dependencies = [ + "windows", + "wintab-sys", +] + +[[package]] +name = "wintab-sys" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d3d0743ba07fe30ca1ccc1471ee50adf32bc70932b86a00c8f1051177950de2" +dependencies = [ + "windows", +] + [[package]] name = "winx" version = "0.36.4" diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rs index c599e16ab6..7d9f4f4e03 100644 --- a/apps/photocraft/src/main.rs +++ b/apps/photocraft/src/main.rs @@ -670,6 +670,14 @@ fn main() -> eframe::Result { if let Some(monitor) = tablet::install_windows() { let _ = tablet_monitor_win.set(monitor); } + // With Windows Ink disabled for the tablet, WinTab supplies the pen pressure, tilt, + // rotation and eraser end; the vendor driver still moves the cursor. + #[cfg(target_os = "windows")] + if let Some(poll) = + tablet::install_wintab(cc, &app.stylus.feed, app.session.prefs().tools.tablet_input == photocraft_engine::prefs::TabletInput::WinTab) + { + app.services.tablet_poll = Some(poll); + } #[cfg(target_os = "linux")] tablet::spawn_x11(&app.stylus.feed, display); app.services.right_button_down = tablet::right_button_service(); diff --git a/apps/photocraft/src/tablet.rs b/apps/photocraft/src/tablet.rs index 19ba28996c..224b682075 100644 --- a/apps/photocraft/src/tablet.rs +++ b/apps/photocraft/src/tablet.rs @@ -6,7 +6,9 @@ //! each sample into the UI's [`StylusFeed`], where the canvas reads it exactly like the web runner's //! Pointer Events and automation's simulated pen. On Windows, winit forwards `WM_POINTER` pressure //! as touch force, but drops `PEN_FLAG_BARREL` (`0x0001`); `photocraft-tablet` recovers the barrel -//! button through a thread-local message hook beside winit. +//! button through a thread-local message hook beside winit. With Windows Ink disabled for the +//! tablet, the optional WinTab mode reads pressure, tilt, rotation and the eraser end from a +//! WinTab 1.4 system context instead ([`install_wintab`]). //! //! Wayland compositors give a pen only to clients that bind the tablet protocol //! (`zwp_tablet_v2`), which winit 0.30 doesn't (and binding it on winit's connection needs @@ -39,6 +41,41 @@ pub fn install_windows() -> Option { photocraft_tablet::windows::Monitor::install().map_err(|e| log::warn!("{e}")).ok() } +/// Installs the WinTab backend on the real PhotoCraft window and returns its per-frame poll. +/// +/// `enabled` is Preferences > Tools > Pen Tablet > Tablet input set to WinTab. With Windows Ink disabled there, the vendor +/// driver still moves the cursor; this backend only supplies pressure/tilt/rotation/eraser +/// through the normal stylus feed. On failure it logs why and leaves Windows Ink untouched. +#[cfg(target_os = "windows")] +pub fn install_wintab(cc: &eframe::CreationContext<'_>, feed: &StylusFeed, enabled: bool) -> Option { + if !enabled { + return None; + } + use eframe::wgpu::rwh::{HasWindowHandle, RawWindowHandle}; + let hwnd = match cc.window_handle().ok()?.as_raw() { + RawWindowHandle::Win32(handle) => handle.hwnd.get(), + _ => return None, + }; + let ctx = cc.egui_ctx.clone(); + if let Err(error) = photocraft_tablet::wintab::install(hwnd, Box::new(move || ctx.request_repaint())) { + log::warn!("WinTab backend: {error}"); + return None; + } + let sink = sink(feed.clone()); + log::info!("WinTab backend active (system context)"); + let logged = std::cell::Cell::new(false); + Some(Box::new(move |focused| { + photocraft_tablet::wintab::poll(focused, &|sample| { + if let Some(sample) = sample + && !logged.replace(true) + { + log::info!("WinTab sample: pressure={:.3} tilt=({:.1},{:.1}) eraser={}", sample.pressure, sample.tilt_x, sample.tilt_y, sample.eraser); + } + sink(sample); + }); + })) +} + /// Stylus barrel button down check (Windows). The right mouse button is left to egui, which /// reports the logical secondary button (swapped buttons included) for the app's own window only. #[cfg(target_os = "windows")] diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 6a4413fe9a..ee61af791c 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -100,6 +100,11 @@ choice!( /// pointer (Photoshop), or erase with the current brush while dragging (Krita, Paint). RightClickPaint { BrushPicker = "brushPicker", Erase = "erase" } default BrushPicker ); +choice!( + /// Windows: which pen input supplies pressure. Windows Ink is the default; WinTab needs + /// Windows Ink turned off in the tablet driver's utility and a restart. + TabletInput { WindowsInk = "windowsInk", WinTab = "winTab" } default WindowsInk +); choice!(OtherCursor { Standard = "standard", Precise = "precise" } default Standard); choice!(CheckerSize { None = "none", Small = "small", Medium = "medium", Large = "large" } default Medium); choice!(CheckerColors { Light = "light", Medium = "medium", Dark = "dark", Red = "red", Orange = "orange", Green = "green", Blue = "blue", Purple = "purple", Custom = "custom" } default Light); @@ -392,6 +397,9 @@ pub struct Tools { pub right_click_with_painting_tools: RightClickPaint, /// Pen tablets: pressure, tilt and rotation reach the brush (off: a pen paints like a mouse). pub use_tablet_pressure: bool, + /// Windows: which pen input supplies pressure. Windows Ink is the default; WinTab needs + /// Windows Ink turned off in the tablet driver's utility and a restart. + pub tablet_input: TabletInput, /// Pen pressure response: `[input, output]` control points in 0..1 that the pen's pressure /// passes through before it reaches any brush ([`PressureCurve`]). Linear by default. pub pressure_curve: Vec<[f32; 2]>, @@ -419,6 +427,7 @@ impl Default for Tools { double_click_layer_mask_launches_select_and_mask: true, right_click_with_painting_tools: RightClickPaint::BrushPicker, use_tablet_pressure: true, + tablet_input: TabletInput::WindowsInk, pressure_curve: vec![[0.0, 0.0], [1.0, 1.0]], bundle_nudges: true, nudge_bundle_pause_ms: 1000, diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs index 026b6befc3..351ce2883c 100644 --- a/crates/engine/src/prefs/tests.rs +++ b/crates/engine/src/prefs/tests.rs @@ -512,6 +512,20 @@ fn svg_group_rasterization_threshold_is_bounded_by_document_depth() { assert_eq!(s.prefs().file_handling.rasterize_svg_groups_deeper_than, 0, "out-of-range preferences leave the valid value unchanged"); } +#[test] +fn tablet_input_pref_defaults_to_windows_ink() { + let mut s = session(); + assert_eq!(s.prefs().tools.tablet_input, TabletInput::WindowsInk); + assert_eq!(s.execute("prefs.get", json!({"path": "tools.tabletInput"})).unwrap(), json!("windowsInk")); + assert_eq!(choices("tools.tabletInput"), Some(TabletInput::NAMES)); + s.execute("prefs.set", json!({"path": "tools.tabletInput", "value": "winTab"})).unwrap(); + assert_eq!(s.prefs().tools.tablet_input, TabletInput::WinTab); + for bad in [json!("ink"), json!(1), json!(true), json!(null)] { + assert!(s.execute("prefs.set", json!({"path": "tools.tabletInput", "value": bad})).is_err()); + } + assert_eq!(s.prefs().tools.tablet_input, TabletInput::WinTab, "rejected values change nothing"); +} + #[test] fn right_click_with_painting_tools_pref() { let mut s = session(); diff --git a/crates/engine/src/prefs/validate.rs b/crates/engine/src/prefs/validate.rs index 175d33f26a..f9c0b9f805 100644 --- a/crates/engine/src/prefs/validate.rs +++ b/crates/engine/src/prefs/validate.rs @@ -2,8 +2,8 @@ use super::shortcuts::normalize_shortcut; use super::{ AppearanceMode, Ask, CanvasBorder, CanvasColor, CheckerColors, CheckerSize, ColorPicker, DarkTheme, ExportLocation, ExportMetadata, FontPreview, GpuBackend, Interpolation, LightTheme, LineStyle, LinuxDisplayServer, LogDestination, LogDetail, OtherCursor, PaintingCursor, PlaybackPerformance, - PointSize, PressureCurve, QuickExportFormat, RawColorSpace, RawDemosaic, RawDepth, RawSharpen, RenderingMode, RightClickPaint, TextEngine, Theme, TypeUnit, - UiFontSize, UiScale, Unit, Value, + PointSize, PressureCurve, QuickExportFormat, RawColorSpace, RawDemosaic, RawDepth, RawSharpen, RenderingMode, RightClickPaint, TabletInput, TextEngine, + Theme, TypeUnit, UiFontSize, UiScale, Unit, Value, }; /// Preferences dialog sections in Photoshop's order: (id, title). @@ -88,6 +88,7 @@ pub fn choices(path: &str) -> Option<&'static [&'static str]> { "cursors.painting" => PaintingCursor::NAMES, "cursors.other" => OtherCursor::NAMES, "tools.rightClickWithPaintingTools" => RightClickPaint::NAMES, + "tools.tabletInput" => TabletInput::NAMES, "transparencyAndGamut.gridSize" => CheckerSize::NAMES, "transparencyAndGamut.gridColors" => CheckerColors::NAMES, "unitsAndRulers.rulers" | "guidesGridAndSlices.gridUnit" => Unit::NAMES, diff --git a/crates/tablet/Cargo.toml b/crates/tablet/Cargo.toml index e14e4a5ae0..cc68771f61 100644 --- a/crates/tablet/Cargo.toml +++ b/crates/tablet/Cargo.toml @@ -3,7 +3,7 @@ name = "photocraft-tablet" version.workspace = true edition.workspace = true license.workspace = true -description = "Pen tablet pressure, tilt, rotation and eraser end on macOS (AppKit) and Linux X11 (XInput2), and barrel button on Windows" +description = "Pen tablet pressure, tilt, rotation and eraser end on macOS (AppKit), Linux X11 (XInput2) and Windows (WinTab), and barrel button on Windows" # The one isolated `unsafe` helper crate (see AGENTS.md, Never crash): the AppKit event # monitor on macOS and the WH_GETMESSAGE pointer hook on Windows need platform interop. Only @@ -31,6 +31,15 @@ objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std [target.'cfg(target_os = "linux")'.dependencies] x11rb = { version = "=0.13.2", default-features = false, features = ["xinput"] } +# WinTab 1.4: a system context on PhotoCraft's real window supplies pressure, tilt, rotation and +# the eraser end while Windows Ink is disabled. `wintab` safely wraps the vendor's wintab32.dll, +# loaded at run time; only the Windows target builds it. `windows` is pinned to the version +# `wintab` uses. +[target.'cfg(target_os = "windows")'.dependencies] +log = { workspace = true } +wintab = "=0.1.1" +windows = { version = "=0.62.2", features = ["Win32_Foundation"] } + # Tests build real tablet NSEvents (from CGEvents) and read them through the monitor's path. [target.'cfg(target_os = "macos")'.dev-dependencies] objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std", "block2", "NSEvent", "NSGraphicsContext", "objc2-core-graphics", "NSApplication", "NSResponder"] } diff --git a/crates/tablet/src/lib.rs b/crates/tablet/src/lib.rs index 41b48f9966..ca158a9137 100644 --- a/crates/tablet/src/lib.rs +++ b/crates/tablet/src/lib.rs @@ -8,6 +8,9 @@ //! - **Windows** ([`windows::Monitor`]): a thread-local message hook inspects `WM_POINTER*` //! messages on the UI thread's message queue to read `PEN_FLAG_BARREL` (`GetPointerPenInfo`), //! which winit 0.30 drops by mapping pen inputs to touch events. +//! - **Windows WinTab** ([`wintab`]): with Windows Ink disabled for the tablet, a WinTab 1.4 +//! system context on PhotoCraft's window supplies pressure, tilt, rotation and the eraser end. +//! The vendor driver still moves the cursor; [`wintab::poll`] is called once per frame. //! AppKit's monitor API and Windows' thread hook need OS interop, so `macos.rs` and `windows.rs` //! allow `unsafe` (an isolated, tested helper crate with `SAFETY:` //! comments on every block). @@ -37,6 +40,8 @@ pub mod xi; pub mod macos; #[cfg(target_os = "windows")] pub mod windows; +#[cfg(target_os = "windows")] +pub mod wintab; #[cfg(target_os = "linux")] pub mod x11; diff --git a/crates/tablet/src/windows.rs b/crates/tablet/src/windows.rs index eb5b2b28c2..c8426fefa5 100644 --- a/crates/tablet/src/windows.rs +++ b/crates/tablet/src/windows.rs @@ -102,6 +102,9 @@ unsafe extern "system" fn get_msg_proc(code: i32, w_param: usize, l_param: isize // SAFETY: Win32 guarantees that for code >= 0, l_param points to a valid MSG struct // dispatched to this thread's message queue. let msg = unsafe { &*(l_param as *const MSG) }; + // The WinTab backend shares this thread hook: it decodes the driver's proximity + // messages (and ignores everything else) to clear a pen that left the tablet. + crate::wintab::on_message(msg.message, msg.w_param, msg.l_param); match msg.message { WM_POINTERDOWN | WM_POINTERUPDATE => { let pointer_id = (msg.w_param & 0xFFFF) as u32; diff --git a/crates/tablet/src/wintab.rs b/crates/tablet/src/wintab.rs new file mode 100644 index 0000000000..3193ae24f1 --- /dev/null +++ b/crates/tablet/src/wintab.rs @@ -0,0 +1,334 @@ +//! WinTab 1.4 backend for Windows. +//! +//! The app opens a WinTab context on its real top-level window and polls queued packets once per +//! frame. This module only translates Wintab packets into [`Sample`]s; it does not synthesise +//! pointer events. With Windows Ink disabled for the tablet, the vendor driver provides the +//! mouse cursor while this module provides pressure, tilt, rotation and the eraser flag. +//! +//! Devices without orientation axes (pressure-only tablets) are opened with a packet that asks +//! only for the fields the driver reports; their tilt is neutral rather than a made-up value. +//! +//! The context is thread-local because Wintab contexts belong to the thread and window that +//! opened them. [`install`] and [`poll`] must therefore run on the UI thread. + +use std::cell::RefCell; +use std::ffi::c_void; +use std::time::{Duration, Instant}; + +use windows::Win32::Foundation::{HWND, LPARAM, WPARAM}; + +use crate::{Error, Sample}; + +wintab::packet! { + struct PenPacket { status, buttons, x, y, normal_pressure, orientation } +} + +wintab::packet! { + struct PressurePacket { status, buttons, x, y, normal_pressure } +} + +/// The open context. A device that reports orientation gets tilt and rotation; a pressure-only +/// device gets a packet without the orientation field, so a zeroed field never becomes a tilt. +enum Backend { + Oriented { context: wintab::Context, axes: [wintab::Axis; 3] }, + PressureOnly { context: wintab::Context }, +} + +struct State { + backend: Backend, + pressure_axis: wintab::Axis, + last_packet: Instant, + emitted: bool, + focused: bool, + /// The pen is inside the tablet's sensing range (`WT_PROXIMITY`, hardware transitions). + proximity_in: bool, + /// Requests a frame when a WinTab message arrives, so pressure-only strokes update without + /// cursor movement. + wake: Box, +} + +thread_local! { + static STATE: RefCell> = const { RefCell::new(None) }; +} + +/// Opens a WinTab system context on `hwnd`. +/// +/// Call on the UI thread. A previous context on this thread is replaced, so the caller keeps the +/// app's normal single-window lifetime. The context is polled with [`poll`]. `wake` is called from +/// the thread's message hook for every WinTab message: a frame is then requested even when the +/// pen only changes pressure (no cursor movement, no other input event). +pub fn install(hwnd: isize, wake: Box) -> Result<(), Error> { + if hwnd == 0 { + return Err(Error::Unsupported("no window handle".into())); + } + let wintab = wintab::Wintab::load().map_err(|e| Error::Platform(e.to_string()))?; + let device = wintab.device(0).ok_or_else(|| Error::Unsupported("no Wintab digitizer".into()))?; + let pressure_axis = device.pressure.ok_or_else(|| Error::Unsupported("Wintab device has no pressure axis".into()))?; + let hwnd = HWND(hwnd as *mut c_void); + let options = wintab::ContextOptions::new().system(true).name("PhotoCraft WinTab"); + // Ask only for the fields the device reports: a pressure-only tablet never fills + // orientation, and reading a zeroed orientation would invent a maximum tilt. + let backend = match device.orientation { + Some(axes) => { + let context = wintab.open::(hwnd, &options).map_err(|e| Error::Platform(e.to_string()))?; + if let Err(error) = context.overlap(true) { + log::warn!("WinTab: could not take overlap priority: {error}"); + } + Backend::Oriented { context, axes } + } + None => { + let context = wintab.open::(hwnd, &options).map_err(|e| Error::Platform(e.to_string()))?; + if let Err(error) = context.overlap(true) { + log::warn!("WinTab: could not take overlap priority: {error}"); + } + Backend::PressureOnly { context } + } + }; + STATE.with_borrow_mut(|slot| { + *slot = Some(State { backend, pressure_axis, last_packet: Instant::now(), emitted: false, focused: false, proximity_in: true, wake }); + }); + Ok(()) +} + +/// Drains queued packets and reports the newest sample. +/// +/// Reports `Some` while a pen is in use, `None` once the pen has left the tablet. Leaving is the +/// packet's proximity status, not a quiet period: a pen held still may send no packets for a +/// while, and treating that as left painted a held airbrush build-up at mouse pressure. A long +/// timeout remains only as a fallback for drivers that never report the leave. +/// +/// Coordinates in the packet are not used: with Windows Ink off, the vendor driver moves the +/// normal cursor, and PhotoCraft's existing pointer path supplies the position. +/// +/// `focused` is whether the app's window has focus. Another tablet application can take the top +/// WinTab overlap priority; when PhotoCraft is activated again it has to ask for it back. The +/// window procedure belongs to winit, so focus from the app's input is the activation signal. +pub fn poll(focused: bool, callback: &dyn Fn(Option)) { + STATE.with_borrow_mut(|slot| { + let Some(state) = slot.as_mut() else { return }; + if focused && !state.focused { + let result = match &state.backend { + Backend::Oriented { context, .. } => context.overlap(true), + Backend::PressureOnly { context } => context.overlap(true), + }; + if let Err(error) = result { + log::warn!("WinTab: could not take overlap priority: {error}"); + } + } else if !focused && state.focused { + let result = match &state.backend { + Backend::Oriented { context, .. } => context.overlap(false), + Backend::PressureOnly { context } => context.overlap(false), + }; + if let Err(error) = result { + log::warn!("WinTab: could not release overlap priority: {error}"); + } + } + state.focused = focused; + let State { backend, pressure_axis, last_packet, emitted, proximity_in, .. } = state; + match backend { + Backend::Oriented { context, axes } => { + let mut packets = Vec::new(); + context.drain(&mut packets); + for packet in packets { + if packet.status.out_of_context() { + if *emitted { + *emitted = false; + callback(None); + } + continue; + } + let sample = sample(&packet, pressure_axis, axes); + *last_packet = Instant::now(); + *emitted = true; + callback(Some(sample)); + } + } + Backend::PressureOnly { context } => { + let mut packets = Vec::new(); + context.drain(&mut packets); + for packet in packets { + if packet.status.out_of_context() { + if *emitted { + *emitted = false; + callback(None); + } + continue; + } + let sample = sample_pressure(&packet, pressure_axis); + *last_packet = Instant::now(); + *emitted = true; + callback(Some(sample)); + } + } + } + if !*proximity_in && *emitted { + *emitted = false; + callback(None); + } + if *emitted && last_packet.elapsed() >= Duration::from_secs(60) { + *emitted = false; + callback(None); + } + }); +} + +/// Handles a WinTab window message from the thread's message hook (`windows.rs`). +/// +/// A hardware proximity leave is a `WT_PROXIMITY` message, not a packet status: the context's +/// `TPS_PROXIMITY` status only says the cursor left the context. Clearing the feed here covers +/// drivers that send the hardware leave without a following out-of-context packet. +pub(crate) fn on_message(message: u32, wparam: usize, lparam: isize) { + // This runs inside the Win32 hook, where a panic aborts the app: a driver call under `poll` + // that pumps this thread's messages would find `STATE` borrowed. Such a message is skipped. + let _ = STATE.try_with(|cell| { + let Ok(mut slot) = cell.try_borrow_mut() else { return }; + let Some(state) = slot.as_mut() else { return }; + let decoded = match &state.backend { + Backend::Oriented { context, .. } => context.message(message, WPARAM(wparam), LPARAM(lparam)), + Backend::PressureOnly { context } => context.message(message, WPARAM(wparam), LPARAM(lparam)), + }; + let win_message = decoded.is_some(); + if let Some(wintab::Message::Proximity { entering, hardware }) = decoded + && hardware + { + state.proximity_in = entering; + } + if win_message { + // Packets can arrive with no cursor movement; the frame then polls them. + (state.wake)(); + } + }); +} + +/// Closes the context on this thread. +pub fn uninstall() { + STATE.with_borrow_mut(|slot| *slot = None); +} + +/// One packet from a device with orientation axes as the UI's pen sample. +/// +/// Angles come from the device's own axes (`TU_CIRCLE`), falling back to Wintab's usual tenths +/// of a degree when an axis does not measure angles. +fn sample(packet: &PenPacket, pressure: &wintab::Axis, orientation: &[wintab::Axis; 3]) -> Sample { + let raw = packet.orientation; + let degrees = |index: usize, value: i32| orientation[index].degrees(value).map_or(value as f32 / 10.0, |degrees| degrees as f32); + let azimuth = degrees(0, raw.azimuth).to_radians(); + // Wintab altitude is the angle from the tablet plane (0 = flat, 90 = upright); W3C tilt is + // the angle from the perpendicular, per axis: atan2 of the in-plane projection over height. + // Wintab azimuth 0 points at the tablet's top and grows clockwise, while W3C +x is right and + // +y towards the user, so the in-plane components are (sin(azimuth), -cos(azimuth)). + let elevation = degrees(1, raw.altitude).abs().to_radians(); + let in_plane_x = elevation.cos() * azimuth.sin(); + let in_plane_y = -(elevation.cos() * azimuth.cos()); + let height = elevation.sin(); + Sample { + pressure: pressure.normalize(packet.normal_pressure as i32), + tilt_x: in_plane_x.atan2(height).to_degrees(), + tilt_y: in_plane_y.atan2(height).to_degrees(), + rotation: degrees(2, raw.twist), + eraser: packet.status.inverted(), + } + .sanitized() +} + +/// One packet from a device without orientation axes: pressure and the eraser end, neutral tilt. +fn sample_pressure(packet: &PressurePacket, pressure: &wintab::Axis) -> Sample { + Sample { pressure: pressure.normalize(packet.normal_pressure as i32), tilt_x: 0.0, tilt_y: 0.0, rotation: 0.0, eraser: packet.status.inverted() } + .sanitized() +} + +#[cfg(test)] +mod tests { + use super::*; + use wintab::Units; + + fn pressure_axis() -> wintab::Axis { + wintab::Axis { min: 0, max: 32767, units: Units::None, resolution: 0.0 } + } + + fn angle_axis() -> wintab::Axis { + wintab::Axis { min: 0, max: 3599, units: Units::Circle, resolution: 3600.0 } + } + + fn axes() -> [wintab::Axis; 3] { + [angle_axis(), angle_axis(), angle_axis()] + } + + fn packet(normal_pressure: u32) -> PenPacket { + PenPacket { status: wintab::Status(0), buttons: wintab::Buttons(0), x: 0, y: 0, normal_pressure, orientation: wintab::Orientation::default() } + } + + fn pressure_packet(normal_pressure: u32) -> PressurePacket { + PressurePacket { status: wintab::Status(0), buttons: wintab::Buttons(0), x: 0, y: 0, normal_pressure } + } + + /// Pressure is normalized through the device's own axis. + #[test] + fn pressure_maps_through_the_device_axis() { + let axes = axes(); + let s = sample(&packet(16384), &pressure_axis(), &axes); + assert!((s.pressure - 0.5).abs() < 0.01, "{}", s.pressure); + let s = sample(&packet(0), &pressure_axis(), &axes); + assert_eq!(s.pressure, 0.0); + } + + /// The inverted status bit is the eraser end. + #[test] + fn eraser_comes_from_the_inverted_status() { + let axes = axes(); + let inverted = PenPacket { status: wintab::Status(wintab::sys::TPS_INVERT), ..packet(0) }; + assert!(sample(&inverted, &pressure_axis(), &axes).eraser); + assert!(!sample(&packet(0), &pressure_axis(), &axes).eraser); + } + + /// Orientation axes are converted to degrees for the W3C tilt projection. + #[test] + fn orientation_axes_report_degrees() { + let axes = axes(); + let tilted = PenPacket { orientation: wintab::Orientation { azimuth: 450, altitude: 300, twist: 900 }, ..packet(0) }; + let sample = sample(&tilted, &pressure_axis(), &axes); + assert!((sample.tilt_x - 50.8).abs() < 0.1, "{}", sample.tilt_x); + assert!((sample.tilt_y + 50.8).abs() < 0.1, "{}", sample.tilt_y); + assert!((sample.rotation - 90.0).abs() < 0.1, "{}", sample.rotation); + } + + /// A pressure-only device keeps pressure and the eraser end, with neutral tilt. + #[test] + fn pressure_only_devices_get_pressure_and_neutral_tilt() { + let s = sample_pressure(&pressure_packet(16384), &pressure_axis()); + assert!((s.pressure - 0.5).abs() < 0.01, "{}", s.pressure); + assert_eq!((s.tilt_x, s.tilt_y, s.rotation), (0.0, 0.0, 0.0)); + let inverted = PressurePacket { status: wintab::Status(wintab::sys::TPS_INVERT), ..pressure_packet(0) }; + assert!(sample_pressure(&inverted, &pressure_axis()).eraser); + } + + /// Wintab azimuth 0 is the tablet top; W3C axes are right and toward the user. + #[test] + fn cardinal_azimuths_match_w3c_tilt() { + let axes = axes(); + let tilt = |azimuth: i32| { + let p = PenPacket { orientation: wintab::Orientation { azimuth, altitude: 450, twist: 0 }, ..packet(0) }; + sample(&p, &pressure_axis(), &axes) + }; + let top = tilt(0); + assert!(top.tilt_x.abs() < 0.1 && (top.tilt_y + 45.0).abs() < 0.1, "{top:?}"); + let right = tilt(900); + assert!((right.tilt_x - 45.0).abs() < 0.1 && right.tilt_y.abs() < 0.1, "{right:?}"); + let bottom = tilt(1800); + assert!(bottom.tilt_x.abs() < 0.1 && (bottom.tilt_y - 45.0).abs() < 0.1, "{bottom:?}"); + let left = tilt(2700); + assert!((left.tilt_x + 45.0).abs() < 0.1 && left.tilt_y.abs() < 0.1, "{left:?}"); + } + + /// An upright pen has no tilt; a flat one has the most. + #[test] + fn an_upright_pen_has_no_tilt_and_a_flat_one_has_the_most() { + let axes = axes(); + let upright = PenPacket { orientation: wintab::Orientation { azimuth: 0, altitude: 900, twist: 0 }, ..packet(0) }; + let s = sample(&upright, &pressure_axis(), &axes); + assert!(s.tilt_x.abs() < 0.1 && s.tilt_y.abs() < 0.1, "{s:?}"); + let flat = PenPacket { orientation: wintab::Orientation { azimuth: 0, altitude: 0, twist: 0 }, ..packet(0) }; + let s = sample(&flat, &pressure_axis(), &axes); + assert!(s.tilt_x.abs() < 0.1 && (s.tilt_y + 90.0).abs() < 0.1, "{s:?}"); + } +} diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 34c2ef16e1..0a77bbdf3b 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -245,8 +245,12 @@ impl eframe::App for PhotocraftApp { self.finish_type_reset_logic(ctx); } + /// Drain the platform's optional WinTab queue before the frame's input is processed. fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) { let frame_start = gpu_canvas::now_ms(); + if let Some(tablet_poll) = self.services.tablet_poll.as_mut() { + tablet_poll(raw_input.focused); + } // An undecorated window gets its caption back when the toolkit changes its flags // (maximize, restore, …): take it off again before the frame is laid out (#2246). if let Some(window_frame) = self.services.window_frame.as_mut() { diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 25d77a32e2..794f1c7e26 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -261,7 +261,7 @@ pub use services::{ CaptionStyleFn, ClipboardGetFn, ClipboardGetSvgFn, ClipboardSetFn, ClipboardUnreadableFn, CursorPosFn, DiscardAutosaveFn, EncodePngFn, ExportFn, ExportSettings, ExternalSvgEditor, ExternalSvgSession, ExternalVectorSave, ExternalVectorSource, FileStamp, FileStampFn, FormatChoices, ImportFn, Inbox, LoadTextFn, OpenExternalSvgFn, OpenUrlFn, OsEventsFn, PhotoshopShortcutsFn, PlaceInbox, QuitFn, RecoverFn, Recoverable, RightButtonDownFn, SameFileFn, - SaveFileFn, SaveTextFn, Services, SystemThemeFn, WindowFrameFn, WriteFn, + SaveFileFn, SaveTextFn, Services, SystemThemeFn, TabletPollFn, WindowFrameFn, WriteFn, }; pub use text::bidi_text; pub use text::cjk_fonts; diff --git a/crates/ui-egui/src/prefs/prefs_ui/fields.rs b/crates/ui-egui/src/prefs/prefs_ui/fields.rs index 6b02ddf785..65c25dbc14 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/fields.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/fields.rs @@ -101,8 +101,11 @@ pub(super) fn humanize(key: &str) -> String { .replace("Lut ", "LUT ") } +/// The visible label of a stored choice value (`windowsInk` -> "Windows Ink (default)"). pub(super) fn choice_label(v: &str) -> String { match v { + "windowsInk" => "Windows Ink (default)".into(), + "winTab" => "WinTab".into(), "exr" => "EXR".into(), "cm" => "Centimeters".into(), "mm" => "Millimeters".into(), @@ -148,7 +151,11 @@ pub(super) fn color_of(s: &str) -> Color32 { /// Search the same visible, editable preference labels used in the dialog. pub(super) fn preference_matches(section: &str, key: &str, query: &str) -> bool { let label = humanize(key); - format!("{section}.{key}").to_lowercase().contains(query) || label.to_lowercase().contains(query) || tl!(&label).to_lowercase().contains(query) + format!("{section}.{key}").to_lowercase().contains(query) + || label.to_lowercase().contains(query) + || tl!(&label).to_lowercase().contains(query) + // The Tablet input row and its Pen Tablet note are the visible terms, not just the key. + || (cfg!(target_os = "windows") && section == "tools" && key == "tabletInput" && "windows ink pen tablet wintab tablet input".contains(query)) } pub(super) fn preference_sections(values: &Value, search: &str) -> Vec<(&'static str, &'static str)> { @@ -298,8 +305,13 @@ pub(super) fn export_field_visible(obj: &Map, key: &str) -> bool } /// The pause that bundles arrow-key nudges means nothing with Bundle Arrow-Key Nudges off (#2259). +/// WinTab is a Windows-only opt-in. pub(super) fn tools_field_visible(obj: &Map, key: &str) -> bool { - key != "nudgeBundlePauseMs" || obj.get("bundleNudges").and_then(Value::as_bool) != Some(false) + match key { + "tabletInput" => cfg!(target_os = "windows"), + "nudgeBundlePauseMs" => obj.get("bundleNudges").and_then(Value::as_bool) != Some(false), + _ => true, + } } /// A titled, outlined group of settings (Photoshop's Interface page has Appearance, @@ -374,7 +386,7 @@ pub(super) fn field_visible(section: &str, obj: &Map, k: &str, qu // The browser presents frames itself: Low-Latency Drawing does nothing there. || (cfg!(target_arch = "wasm32") && path == "performance.lowLatencyDrawing") || (section == "export" && !export_field_visible(obj, k)) - || (section == "tools" && !tools_field_visible(obj, k)); + || (section == "tools" && (!tools_field_visible(obj, k) || k == "tabletInput")); (query.is_empty() || preference_matches(section, k, query)) && !drawn_elsewhere && !prefs::is_hidden(&path) @@ -405,6 +417,29 @@ pub(super) fn section_fields( if section == "performance" && (query.is_empty() || preference_matches(section, "memoryUsageMb", &query)) { memory_usage_row(ui, obj); } + // WinTab is an explicit Windows choice; Windows Ink stays the default everywhere else. + if section == "tools" + && cfg!(target_os = "windows") + && (query.is_empty() || preference_matches(section, "tabletInput", &query)) + && obj.contains_key("tabletInput") + { + let t = Tokens::get(ui.ctx()); + pref_group(ui, "Pen Tablet", |ui| { + egui::Grid::new("prefs-grid-pen-tablet").num_columns(2).spacing([14.0, 7.0]).show(ui, |ui| { + field_rows(ui, section, obj, vec!["tabletInput".to_string()], lang); + }); + ui.add( + egui::Label::new( + RichText::new(tl!( + "WinTab reads pressure, tilt, rotation and the eraser end from the tablet driver. Turn off Windows Ink in your tablet's software utility (for example Wacom Center) before using it." + )) + .small() + .color(t.text_dim), + ) + .wrap(), + ); + }); + } keys.retain(|k| field_visible(section, obj, k, &query)); egui::Grid::new(("prefs-grid", section)).num_columns(2).spacing([14.0, 7.0]).show(ui, |ui| field_rows(ui, section, obj, keys, lang)); } @@ -457,7 +492,12 @@ fn field_rows(ui: &mut egui::Ui, section: &str, obj: &mut Map, ke let labels: Vec = opts.iter().map(|o| choice_label(o)).collect(); let pairs: Vec<(String, &str)> = opts.iter().map(|o| o.to_string()).zip(labels.iter().map(String::as_str)).collect(); let mut cur = s.clone(); - crate::widgets::dropdown(ui, &format!("pref-{path}"), tl!(&label), &mut cur, &pairs, 220.0); + ui.vertical(|ui| { + crate::widgets::dropdown(ui, &format!("pref-{path}"), tl!(&label), &mut cur, &pairs, 220.0); + if read_at_launch(&path) { + ui.label(RichText::new(tl!("Applies at next launch.")).color(t.text_dim)); + } + }); obj.insert(k, json!(cur)); } Value::String(s) if prefs::is_color(&path) => { @@ -546,3 +586,19 @@ fn field_rows(ui: &mut egui::Ui, section: &str, obj: &mut Map, ke ui.end_row(); } } + +#[cfg(test)] +mod tests { + use super::preference_matches; + + /// The Tablet input row matches its visible search terms. + #[test] + fn wintab_row_matches_its_visible_terms() { + if cfg!(target_os = "windows") { + for query in ["windows ink", "pen tablet", "wintab", "tablet input"] { + assert!(preference_matches("tools", "tabletInput", query), "{query}"); + } + } + assert!(preference_matches("tools", "useTabletPressure", "tablet pressure")); + } +} diff --git a/crates/ui-egui/src/prefs/prefs_ui/graphics.rs b/crates/ui-egui/src/prefs/prefs_ui/graphics.rs index 166f5ed299..45b2e9ccdb 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/graphics.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/graphics.rs @@ -136,6 +136,8 @@ pub(super) fn read_at_launch(path: &str) -> bool { "interface.systemTitleBar" => cfg!(all(not(target_arch = "wasm32"), not(target_os = "macos"))), // The desktop app decides on the control server before its window opens. "integrations.allowAgentControl" | "integrations.controlPort" => cfg!(not(target_arch = "wasm32")), + // The WinTab context is opened once, when the window starts. + "tools.tabletInput" => cfg!(target_os = "windows"), _ => false, } } diff --git a/crates/ui-egui/src/services.rs b/crates/ui-egui/src/services.rs index 8224b28698..ab5efac975 100644 --- a/crates/ui-egui/src/services.rs +++ b/crates/ui-egui/src/services.rs @@ -400,6 +400,10 @@ pub type CaptionStyleFn = Box CaptionStyle>; /// Physical right mouse button or stylus barrel button down (Windows; read for brush resize). pub type RightButtonDownFn = Box bool + Send + Sync>; +/// Polls the platform's optional WinTab packet queue into the app's stylus feed (Windows), with +/// whether the app window is focused (to re-assert WinTab overlap priority on activation). +pub type TabletPollFn = Box; + /// Platform services injected by the app binary (file dialogs, codecs), keeping this crate free of /// I/O dependencies. #[derive(Default)] @@ -522,6 +526,8 @@ pub struct Services { pub caption_style: Option, /// Physical right mouse button or pen barrel button currently down. pub right_button_down: Option, + /// WinTab packet poll (Windows with Windows Ink disabled; see `photocraft_tablet::wintab`). + pub tablet_poll: Option, /// The persistent brush preset store, loading in the background (desktop; see /// `photocraft_engine::preset_store`). Attached to the session once it arrives; without /// one, brush presets are session-only unless the shell attached a store before startup. diff --git a/docs/hardware-parity.md b/docs/hardware-parity.md index 0fe3e98549..7ed3d1e71e 100644 --- a/docs/hardware-parity.md +++ b/docs/hardware-parity.md @@ -1,6 +1,6 @@ # Hardware parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-28 · **Change:** minor (GPU compositor startup builds in the background, measurements pending; Camera Raw's preview develops its per-pixel stages on the GPU) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-05 · **Change:** minor (optional WinTab backend on Windows: pressure and the eraser end; tilt on devices with orientation axes; rotation only with a pen and driver that report twist, with Windows Ink off) · **Target:** Adobe Photoshop 2026 Each hardware feature Photoshop 2026 (27.11.0) uses, per platform, against PhotoCraft. Photoshop side: its bundle (Metal and OpenCL paths, "Precise Color Management for HDR Display" and EDR @@ -9,7 +9,9 @@ strings, Touch Bar resources, pen pressure/tilt strings) and Adobe's GPU FAQ. Ph `engine/src/display_color.rs`, `apps/photocraft/src/monitor_profile.rs`. **Summary (estimated):** ~50% ready. GPU compositing and colour-managed display are solid; pen -input is uneven by platform (Windows lacks tilt/rotation, Wayland is broken) and there is no HDR +input is uneven by platform (the default Windows Ink path lacks tilt and rotation, Wayland is +broken; the opt-in WinTab path on Windows supplies tilt only on devices that report orientation +axes, and rotation only when the pen and driver report twist) and there is no HDR output. Remaining: 80–150 h (gap G12). Human: tablets (Wacom, XP-Pen, Huion) on each OS and an HDR display to verify. @@ -31,12 +33,18 @@ HDR display to verify. | Feature | Photoshop | macOS | Windows | Linux X11 | Linux Wayland | Web | |---|---|---|---|---|---|---| | Pressure | yes | yes (AppKit monitor) | yes (WM_POINTER), jumpy (#2046) | yes (XInput2) | via Xwayland only; GNOME 50 pen doesn't click (#2622), KDE (#2297) | yes | -| Tilt | yes | yes | **no** | yes | no | yes | -| Barrel rotation | yes | yes (not applied to tip angle, #1301) | no | partial | no | yes (twist) | -| Eraser end | yes | yes | no (#2285) | partial | no | partial | -| WinTab driver mode | yes | – | no | – | – | – | +| Tilt | yes | yes | **no** (WinTab: with orientation axes, opt-in) | yes | no | yes | +| Barrel rotation | yes | yes (not applied to tip angle, #1301) | no (WinTab: with a twist-reporting pen and driver, opt-in) | partial | no | yes (twist) | +| Eraser end | yes | yes | no (#2285); WinTab: yes (opt-in) | partial | no | partial | +| WinTab driver mode | yes | – | yes (opt-in; Windows Ink off) | – | – | – | | Pressure curve | Windows Ink / driver | global curve in Preferences (#2195) | same | same | same | same | +Windows: the opt-in WinTab backend (`tools.tabletInput = "winTab"`, with Windows Ink turned off +in the tablet utility) supplies pressure and the eraser end, tilt only on devices that report +orientation axes, and rotation only when the pen and driver report twist (pressure-only devices +get neutral tilt and rotation). The default Windows Ink path is unchanged and still lacks tilt, +rotation and the eraser. + ## Displays and colour | Feature | Photoshop | PhotoCraft | Status | @@ -63,6 +71,7 @@ HDR display to verify. | Date | Change | Summary | |---|---|---| +| 2026-11-05 | minor | Optional WinTab driver mode on Windows: pressure and eraser; tilt with orientation axes; rotation with a twist-reporting pen, with Windows Ink off | | 2026-10-28 | minor | Build GPU compositor pipelines in the background; benchmark baseline pending (#9157) | | 2026-10-10 | major | First version | | 2026-10-17 | minor | GPU filters: Camera Raw's preview, per-pixel stages (#6916 step 1) | diff --git a/docs/scorecard.md b/docs/scorecard.md index de78fae0de..a3ecd984cb 100644 --- a/docs/scorecard.md +++ b/docs/scorecard.md @@ -9,7 +9,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js | [Performance](#performance) | 3 | 22 | 51 | 85 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 47 not run, 4 not measurable yet (baseline 2026-10-05) | | [File compatibility](#file-compatibility) | 11 | 14 | 2 | io corpus: oracle ≥ 148/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 249/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 190/258, round trip ≥ 258/258 | | [Tools](#tools) | 36 | 9 | 0 | | -| [Workspace and UI](#workspace-and-ui) | 15 | 6 | 2 | settings that do nothing: 10 of 191 (target 0) | +| [Workspace and UI](#workspace-and-ui) | 15 | 6 | 2 | settings that do nothing: 10 of 192 (target 0) | | [Type](#type) | 5 | 5 | 0 | | | [Automation](#automation) | 5 | 3 | 3 | 660 of 660 menu items live (100.0%) | | [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 30 of 30 crates; panic_hunt present | @@ -209,7 +209,7 @@ Corpus floors (`crates/io/tests/it/corpus.rs`): the enforced minimums; the corpu ## Workspace and UI -**Settings that do nothing: 10 of 191** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't. +**Settings that do nothing: 10 of 192** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't. Unread: `general.color_picker`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `performance.legacy_compositing`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `type_.text_engine`, `enhanced_controls.scrubby_slider_acceleration`. From 2ff8f419cbef9c231a5647eebda43040f6dfa3fb Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:44:13 +0100 Subject: [PATCH 7/8] ci: run the GPU tests on lavapipe on Linux and fail if they skip (#7975) The Linux runner has no GPU, so the GPU tests ran on Mesa llvmpipe over GL, which can't render Rgba32Float: 57 GPU checks (50 GPU parity, 3 canvas, 2 adjustment preview parity, 2 effect-map recovery) returned early and passed without comparing anything. A new step after Test installs mesa-vulkan-drivers and runs the nextest `gpu` group on lavapipe (Vulkan), reusing Test's build, and fails unless the parity tests report the Vulkan backend and no test printed a skip. No code change: photocraft's tests take whatever adapter wgpu returns, software adapters included. Follows the CI half of lightcraft#1148. Co-authored-by: FredApps Co-authored-by: Claude Opus 5.5 --- .config/nextest.toml | 2 +- .github/workflows/ci.yml | 4 +- docs/development.md | 2 + scripts/ci-gpu-lavapipe.py | 67 +++++++++++++++++++++++++++++++++ scripts/test-ci-gpu-lavapipe.py | 44 ++++++++++++++++++++++ 5 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 scripts/ci-gpu-lavapipe.py create mode 100644 scripts/test-ci-gpu-lavapipe.py diff --git a/.config/nextest.toml b/.config/nextest.toml index 2ef6d6f215..a1e0a2ee0a 100644 --- a/.config/nextest.toml +++ b/.config/nextest.toml @@ -11,7 +11,7 @@ nextest-version = "0.9.150" # # GPU tests: each test is its own process here, so the in-process `gpu_lock()` mutexes no longer # serialize them. Too many concurrent wgpu devices time out (`PollError: ... Wait timed out`), so -# this group caps them at 4. +# this group caps them at 4. scripts/ci-gpu-lavapipe.py reads this group's filters directly. [test-groups] gpu = { max-threads = 4 } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c15bc9542b..2c3597f5ee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,7 +151,7 @@ jobs: # egui/winit/rfd. packages: >- libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev - libgtk-3-dev + libgtk-3-dev mesa-vulkan-drivers libvulkan1 # The tests build with the optional craft-fonts input (crates/text/build.rs), so the # Japanese font tests that skip without it run. Same commit as release.yml. craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 @@ -164,7 +164,7 @@ jobs: timeout-minutes: 100 # fmt, clippy, layers, scorecard, tests, panic_hunt, doctests and the wasm32 check, with # `--locked --features heif`, then the missing-translations report (xtask/src/main.rs). - command: cargo xtask ci + command: python3 scripts/test-ci-gpu-lavapipe.py && cargo xtask ci && python3 scripts/ci-gpu-lavapipe.py # Windows tests run as two legs in parallel: the unit tests and the integration tests. As one job # they took ~28 min on the Windows runner; the legs take ~13 and ~15. Splitting the integration diff --git a/docs/development.md b/docs/development.md index 9faa93a18b..1fa912575f 100644 --- a/docs/development.md +++ b/docs/development.md @@ -113,6 +113,8 @@ the opted-in test; an unset variable leaves the ordinary behavior tests availabl After review, remove only the two generated PNGs and their owned temporary directory. The same Rust test and variable work on Windows with the native shell's environment-variable syntax. +**GPU tests without a GPU.** Linux CI uses the shared `craft-common-actions` gate with Mesa's Vulkan driver installed. After the regular gate, `python3 scripts/ci-gpu-lavapipe.py` reruns the configured `gpu` group with `WGPU_BACKEND=vulkan` and the lavapipe ICD explicitly selected. It requires Vulkan parity evidence and rejects authored skips, except the documented software-adapter timing exceptions. It reads the filter from `.config/nextest.toml`, so no second filter needs maintaining. To run locally on Linux, install `mesa-vulkan-drivers libvulkan1` and run the same script. Windows uses WARP; this check does not certify Windows skip coverage. + ## Fonts (craft-fonts) Font assets shared by the Crafting Apps live in [storytold/craft-fonts](https://github.com/storytold/craft-fonts), never in this repo: don't commit font files here (Inter and JetBrains Mono in `assets/fonts/` are the only exceptions; new fonts go to craft-fonts). diff --git a/scripts/ci-gpu-lavapipe.py b/scripts/ci-gpu-lavapipe.py new file mode 100644 index 0000000000..4d1fe78661 --- /dev/null +++ b/scripts/ci-gpu-lavapipe.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""Run the configured GPU group on lavapipe without silently losing comparisons.""" +import glob +import os +from pathlib import Path +import re +import subprocess +import sys +import tempfile +import tomllib + + +def gpu_filter(config): + filters = [row['filter'] for row in config['profile']['default']['overrides'] + if row.get('test-group') == 'gpu'] + if not filters: + raise ValueError('nextest GPU group has no filters') + return ' | '.join(f'({value})' for value in filters) + + +def validate_log(lines): + vulkan = False + rejected = [] + for line in lines: + vulkan |= 'GPU parity backend: Vulkan' in line + # Authored skips, not nextest's summary of ignored/filtered-out tests. + if re.search(r'\bskipping\b|\bno (?:GPU )?adapter\b', line): + rejected.append(line.rstrip()) + if 'SKIP ' in line and not ( + ('SKIP gpu timing:' in line and 'is a software adapter' in line) + or 'SKIP the comparison: this GPU finishes' in line + or 'SKIP the sum: this refresh still fit one command buffer' in line + ): + rejected.append(line.rstrip()) + if not vulkan: + rejected.append('GPU parity tests did not report the Vulkan backend') + return rejected + + +def main(): + root = Path(__file__).resolve().parents[1] + with (root / '.config/nextest.toml').open('rb') as source: + selected = gpu_filter(tomllib.load(source)) + icds = sorted(glob.glob('/usr/share/vulkan/icd.d/lvp_icd*.json')) + if not icds: + raise RuntimeError('Install mesa-vulkan-drivers and libvulkan1 before this check') + env = dict(os.environ, WGPU_BACKEND='vulkan', VK_ICD_FILENAMES=os.pathsep.join(icds)) + command = ['cargo', 'nextest', 'run', '--profile', 'ci', '--workspace', '--locked', + '--features', 'heif', '--lib', '--bins', '--tests', '-E', selected, + '--success-output', 'immediate', '--color', 'never'] + # Stream output and keep it on disk, rather than buffering every successful test in memory. + with tempfile.TemporaryFile(mode='w+t') as log: + with subprocess.Popen(command, cwd=root, env=env, stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, text=True) as process: + for line in process.stdout: + sys.stdout.write(line) + log.write(line) + result = process.wait() + log.seek(0) + errors = validate_log(log) + for error in errors: + print(f'::error::{error}', file=sys.stderr) + return result if result else int(bool(errors)) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/scripts/test-ci-gpu-lavapipe.py b/scripts/test-ci-gpu-lavapipe.py new file mode 100644 index 0000000000..e2a21a96ad --- /dev/null +++ b/scripts/test-ci-gpu-lavapipe.py @@ -0,0 +1,44 @@ +import importlib.util +from pathlib import Path +import unittest + +spec = importlib.util.spec_from_file_location('gpu_check', Path(__file__).with_name('ci-gpu-lavapipe.py')) +check = importlib.util.module_from_spec(spec) +spec.loader.exec_module(check) + + +class LogChecks(unittest.TestCase): + def test_requires_vulkan_evidence(self): + self.assertTrue(check.validate_log(['GPU parity backend: Gl'])) + self.assertTrue(check.validate_log([])) + + def test_success_and_nextest_summary(self): + self.assertFalse(check.validate_log(['GPU parity backend: Vulkan (llvmpipe, Cpu)', + 'Summary 100 tests run: 100 passed, 3 skipped'])) + + def test_rejects_missing_comparisons(self): + for message in ['skipping GPU parity tests: no device', 'skipping: unsupported format', + 'no GPU adapter', 'SKIP gpu timing: no adapter', + 'SKIP gpu timing: no device with timestamp queries', 'SKIP unknown test']: + with self.subTest(message=message): + self.assertTrue(check.validate_log(['GPU parity backend: Vulkan', message])) + + def test_allows_only_known_timing_exceptions(self): + self.assertFalse(check.validate_log(['GPU parity backend: Vulkan', + 'SKIP gpu timing: llvmpipe is a software adapter, which cannot show a GPU/CPU split', + 'SKIP the comparison: this GPU finishes a 6 MP composite in 1 ms against 2 ms of encoding', + 'SKIP the sum: this refresh still fit one command buffer'])) + + def test_selects_all_gpu_filters_from_config(self): + config = {'profile': {'default': {'overrides': [ + {'filter': 'package(a)', 'test-group': 'gpu'}, + {'filter': 'package(b)', 'test-group': 'cpu'}, + {'filter': 'test(c)', 'test-group': 'gpu'}]}}} + self.assertEqual(check.gpu_filter(config), '(package(a)) | (test(c))') + config['profile']['default']['overrides'] = [] + with self.assertRaises(ValueError): + check.gpu_filter(config) + + +if __name__ == '__main__': + unittest.main() From b49c6c97c5742f6747658ebf9656fc5d03b21f8c Mon Sep 17 00:00:00 2001 From: tasosf <221943975+fokas-hoa@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:59:25 +0200 Subject: [PATCH 8/8] fix(export): preserve high-quality JPEG colour detail in Export As (#7985) * fix(export): preserve full JPEG chroma in Export As * style(ui): simplify blend preview condition for Clippy * test(export): verify full JPEG chroma at every quality Exercise real JPEG export and decode at five qualities across U8, U16 and F32. Read all sampling factors from the main frame while skipping metadata segments and embedded thumbnails. Keep the colour-error limit at quality 100 and verify that the layered source is unchanged. * Keep JPEG regression compatible with current Clippy Restore the upstream blend-preview and IME predicates, removing unrelated lint-only changes. Use array chunks in the JPEG colour-error assertion for current stable Clippy. * Use quality-dependent JPEG sampling in Export As Keep 4:2:0 through quality 50 and full 4:4:4 above it, using the existing percentage-scale cutoff from Save for Web. Photoshop 27.11 modern Export As measurements show 4:2:0 at levels 1-4 and 4:4:4 at 5-7; no exact numerical mapping of the two scales is claimed. Exercise both sampling regimes through 15 real JPEG exports across U8/U16/F32, check all percentage qualities in settings, and retain the Save As default. Boundary mutations fail at 50 and 51 as expected. --- contributors/people.toml | 3 + crates/ui-egui/src/dialogs/export_dialog.rs | 3 + .../src/dialogs/export_dialog/tests.rs | 103 ++++++++++++++++++ crates/ui-egui/src/services.rs | 5 + 4 files changed, 114 insertions(+) diff --git a/contributors/people.toml b/contributors/people.toml index 59b4b9b7f0..a2ead57598 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -201,6 +201,9 @@ sync_github_name = true real_name = "Guillem Hermida" sync_github_name = true +[people.fokas-hoa] +display_name = "Tasos Fokas" + [people.fjamesprice] sync_github_name = true diff --git a/crates/ui-egui/src/dialogs/export_dialog.rs b/crates/ui-egui/src/dialogs/export_dialog.rs index acfaa16db1..dcd6ba003d 100644 --- a/crates/ui-egui/src/dialogs/export_dialog.rs +++ b/crates/ui-egui/src/dialogs/export_dialog.rs @@ -462,6 +462,9 @@ fn settings(f: &Map) -> ExportSettings { let metadata = s(f, "metadata"); ExportSettings { jpeg_quality: (fmt == "jpg").then_some(quality), + // Photoshop's 1-7 Export As presets measured 4:2:0 at 1-4 and 4:4:4 at 5-7. + // An exact mapping to 1-100 is unestablished; reuse PhotoCraft's Save for Web cutoff. + jpeg_chroma_subsampling: fmt == "jpg" && quality <= 50, webp_lossless: fmt != "webp" || lossless(f), webp_quality: (fmt == "webp" && !lossless(f)).then_some(quality), xmp_all: metadata == "all", diff --git a/crates/ui-egui/src/dialogs/export_dialog/tests.rs b/crates/ui-egui/src/dialogs/export_dialog/tests.rs index c5a7bdfa2f..90624f8778 100644 --- a/crates/ui-egui/src/dialogs/export_dialog/tests.rs +++ b/crates/ui-egui/src/dialogs/export_dialog/tests.rs @@ -526,6 +526,109 @@ fn export_document_scales_and_flattens() { assert_eq!(settings(&f).jpeg_quality, Some(85)); } +/// Read the main JPEG frame, skipping complete segments rather than searching marker bytes: +/// APP metadata can contain a thumbnail JPEG with its own frame and different sampling. +fn jpeg_sampling_factors(bytes: &[u8]) -> [(u8, u8); 3] { + assert_eq!(bytes.get(..2), Some([0xff, 0xd8].as_slice()), "JPEG SOI"); + let mut offset = 2; + loop { + assert_eq!(bytes.get(offset), Some(&0xff), "JPEG marker prefix"); + while bytes.get(offset) == Some(&0xff) { + offset += 1; + } + let marker = *bytes.get(offset).expect("JPEG marker"); + offset += 1; + match marker { + 0x01 | 0xd0..=0xd8 => continue, // Standalone markers have no length. + 0x00 | 0xd9 | 0xda => panic!("JPEG has no frame before scan/end"), + _ => {} + } + let length_bytes = bytes.get(offset..offset + 2).expect("JPEG segment length"); + let length = usize::from(u16::from_be_bytes([length_bytes[0], length_bytes[1]])); + assert!(length >= 2, "JPEG segment length includes its two length bytes"); + let end = offset.checked_add(length).expect("JPEG segment end"); + let segment = bytes.get(offset + 2..end).expect("complete JPEG segment"); + if matches!(marker, 0xc0..=0xc3 | 0xc5..=0xc7 | 0xc9..=0xcb | 0xcd..=0xcf) { + assert_eq!(segment.get(5), Some(&3), "JPEG frame must have three components"); + assert_eq!(segment.len(), 6 + 3 * 3, "JPEG frame component descriptors"); + return std::array::from_fn(|i| { + let sampling = segment[6 + 3 * i + 1]; + (sampling >> 4, sampling & 0x0f) + }); + } + offset = end; + } +} + +#[test] +fn jpeg_export_sampling_skips_embedded_thumbnail() { + // An APP segment contains an entire 4:2:0 thumbnail; the main frame is 4:4:4. + let frame_420 = [0xff, 0xc0, 0, 17, 8, 0, 1, 0, 1, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1]; + let mut thumbnail = vec![0xff, 0xd8]; + thumbnail.extend_from_slice(&frame_420); + thumbnail.extend_from_slice(&[0xff, 0xd9]); + assert_eq!(jpeg_sampling_factors(&thumbnail), [(2, 2), (1, 1), (1, 1)]); + let mut jpeg = vec![0xff, 0xd8, 0xff, 0xe1]; + jpeg.extend_from_slice(&u16::try_from(thumbnail.len() + 2).unwrap().to_be_bytes()); + jpeg.extend_from_slice(&thumbnail); + jpeg.push(0xff); // Marker fill byte before the main frame. + let mut frame_444 = frame_420; + frame_444[11] = 0x11; + jpeg.extend_from_slice(&frame_444); + assert_eq!(jpeg_sampling_factors(&jpeg), [(1, 1); 3]); +} + +/// High-quality JPEG must keep the colour detail of a transparent graphic over its background; +/// lower qualities use chroma subsampling, including at the quality-50 boundary. +#[test] +fn jpeg_export_preserves_fine_colour_edges() { + use photocraft_doc::{Color, ColorMode, Layer, SampleType, Size}; + use photocraft_geom::Rect; + + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut doc = Document::with_background("colour detail", Size::new(32, 16), ColorMode::Rgb, depth, Color::WHITE); + let mut graphic = Layer::raster("transparent graphic", doc.pixel_format()); + for x in (0..32).step_by(2) { + graphic.surface_mut().unwrap().fill_rect(Rect::from_xywh(x, 0, 1, 16), &[1.0, 0.0, 0.0, 1.0]); + } + doc.layers.push(graphic); + let before = doc.clone(); + for quality in [1, 50, 51, 85, 100] { + let fields = json!({"format": "jpg", "quality": quality, "scale": 100}).as_object().unwrap().clone(); + let out = default_export_document(&doc, &fields, None).unwrap(); + let encoded = photocraft_io::export(&out, "detail.jpg", &settings(&fields).io_options()).unwrap(); + let expected_sampling = if quality <= 50 { [(2, 2), (1, 1), (1, 1)] } else { [(1, 1); 3] }; + assert_eq!(jpeg_sampling_factors(&encoded.bytes), expected_sampling, "{depth:?}, quality {quality}: JPEG sampling"); + let decoded = photocraft_codecs::decode(&encoded.bytes).unwrap().convert(photocraft_codecs::ChannelLayout::Rgb, photocraft_codecs::SampleType::U8); + assert_eq!(decoded.dimensions(), (32, 16), "{depth:?}, quality {quality}"); + // Lower qualities also lose detail to quantization, independently of chroma sampling. + if quality == 100 { + let mut max_error = 0; + for (i, pixel) in decoded.data().as_chunks::<3>().0.iter().enumerate() { + let expected: [u8; 3] = if i % 32 % 2 == 0 { [255, 0, 0] } else { [255, 255, 255] }; + for (actual, expected) in pixel.iter().zip(expected) { + max_error = max_error.max(actual.abs_diff(expected)); + } + } + assert!(max_error <= 8, "{depth:?}: JPEG at quality 100 loses colour detail by {max_error} levels"); + } + assert_eq!(doc, before, "{depth:?}, quality {quality}: export leaves the layered source unchanged"); + } + } +} + +#[test] +fn jpeg_export_settings_follow_quality_sampling_cutoff() { + for quality in 1..=100 { + let fields = json!({"format": "jpg", "quality": quality}).as_object().unwrap().clone(); + let options = settings(&fields).io_options(); + assert_eq!(options.encode.jpeg_quality, quality); + assert_eq!(options.encode.jpeg_chroma_subsampling, quality <= 50, "quality {quality}"); + } + // Save As retains the codec's existing sampling default. + assert!(ExportSettings::default().io_options().encode.jpeg_chroma_subsampling); +} + /// #1353: a scale that changes only the height (the width rounds back to the original) /// used to be skipped, so the exported file kept the original size while the dialog /// labelled it as scaled. diff --git a/crates/ui-egui/src/services.rs b/crates/ui-egui/src/services.rs index ab5efac975..a9d511fb48 100644 --- a/crates/ui-egui/src/services.rs +++ b/crates/ui-egui/src/services.rs @@ -79,6 +79,9 @@ pub type ImportFn = Box Re pub struct ExportSettings { /// JPEG quality 1–100 (None = codec default). pub jpeg_quality: Option, + /// JPEG chroma sampling: `true` is 4:2:0, `false` keeps full 4:4:4 colour detail. + /// Export As keeps full chroma above quality 50 so saturated text and graphics stay sharp. + pub jpeg_chroma_subsampling: bool, /// JPEG Options › Format Options: baseline, baseline optimized or progressive. pub jpeg_format: photocraft_codecs::JpegFormat, /// WebP: lossless (VP8L) rather than lossy (VP8 at [`Self::webp_quality`]). @@ -133,6 +136,7 @@ impl Default for ExportSettings { fn default() -> Self { ExportSettings { jpeg_quality: None, + jpeg_chroma_subsampling: true, jpeg_format: photocraft_codecs::JpegFormat::Baseline, webp_lossless: true, webp_quality: None, @@ -163,6 +167,7 @@ impl ExportSettings { if let Some(q) = self.jpeg_quality { opts.encode.jpeg_quality = q; } + opts.encode.jpeg_chroma_subsampling = self.jpeg_chroma_subsampling; opts.encode.jpeg_format = self.jpeg_format; opts.encode.webp_lossless = self.webp_lossless; if let Some(q) = self.webp_quality {