Merge pull request #16561

This commit is contained in:
autocraft-bot[bot] committed 2026-12-01 15:26:27 +00:00
commit 67c72967df
8 files changed
+131 -85

No files matched your search

+2
View File
@@ -93,6 +93,8 @@ impl PhotocraftApp {
#[cfg(not(target_arch = "wasm32"))]
control: None,
live_adjust: None,
#[cfg(test)]
preview_test_area_multiplier: 1,
frame: 0,
notices_live: notices::Live::default(),
low_memory: crate::memory_warning::Watch::default(),
+4 -4
View File
@@ -30,8 +30,8 @@ mod tests;
/// the very stall this avoids.
pub(crate) fn filter_preview_factor(app: &PhotocraftApp, doc: &Document, cmd: &str, params: &serde_json::Value) -> u32 {
// A smart filter edit re-renders the whole stack: every stage constrains the factor.
let k = crate::filter_dialog::smart_preview_factor(doc, cmd, params, crate::proxy::preview_factor(doc, crate::proxy::reduced_previews(app)));
if cfg!(target_arch = "wasm32") { k } else { crate::proxy::view_factor(k, app.current_zoom(), doc.size.area()) }
let k = crate::filter_dialog::smart_preview_factor(doc, cmd, params, crate::proxy::app_factor(app, doc));
if cfg!(target_arch = "wasm32") { k } else { crate::proxy::view_factor(k, app.current_zoom(), crate::proxy::planning_area(app, doc)) }
}
/// The open adjustment dialog (a plain one or a Smart Filter edit) while its preview shows: its dialog id and what OK
@@ -364,11 +364,11 @@ pub(super) fn ensure_proxy_preview(app: &mut PhotocraftApp, idx: usize, zoom: f3
(st.doc.id, st.revision, st.doc.clone())
};
// Low-resolution previews off: k is 1 and the canvas shows `display_doc` (full size).
let mut k = crate::proxy::preview_factor(&doc, crate::proxy::reduced_previews(app));
let mut k = crate::proxy::app_factor(app, &doc);
// Zoomed in, a proxy coarser than the screen showed the drag in k-pixel blocks (#5764): when
// the wgpu compositor draws the full-size document, use a proxy only as coarse as the screen
// (none at 50 % and closer). Otherwise the full size would be a CPU composite per change.
let fine = crate::proxy::view_factor(k, zoom, doc.size.area());
let fine = crate::proxy::view_factor(k, zoom, crate::proxy::planning_area(app, &doc));
if fine < k && app.gpu.as_ref().is_some_and(|g| g.supports(&doc)) {
k = fine;
}
+1 -1
View File
@@ -97,7 +97,7 @@ pub(crate) fn canvas_picture(
if proxy == ProxyPreview::Document
&& crate::adjust_preview::shown_key(app) == Some(preview_key)
&& let Some(st) = app.session.documents().get(idx)
&& crate::proxy::preview_factor(&st.doc, crate::proxy::reduced_previews(app)) > 1
&& crate::proxy::app_factor(app, &st.doc) > 1
{
(doc, preview_key) = (st.doc.clone(), 0);
}
+6 -5
View File
@@ -254,7 +254,7 @@ pub(crate) fn targets_channel(app: &PhotocraftApp, idx: usize, kind: &str, param
fn cheap(app: &PhotocraftApp, doc: &Document) -> bool {
match &app.gpu {
Some(g) => g.supports(doc),
None => crate::proxy::preview_factor(doc, crate::proxy::reduced_previews(app)) <= 1,
None => crate::proxy::app_factor(app, doc) <= 1,
}
}
@@ -426,15 +426,16 @@ pub fn gpu_proxy(app: &mut PhotocraftApp, idx: usize, zoom: f32) -> Option<Proxy
}
let gpu = app.gpu.clone()?;
update(app, idx)?;
let p = app.adjust_preview.as_mut()?;
p.shown.as_ref()?;
let p = app.adjust_preview.as_ref()?;
let (_, key) = p.shown.as_ref()?;
let key = *key;
let base = p.base.clone()?;
let key = p.shown.as_ref()?.1;
// `ProxyPlan::factor` is 1 for a document too small for a proxy, which `k < 2` refuses with the near views.
let k = ProxyPlan::factor(zoom, base.size.area(), true);
let k = ProxyPlan::factor(zoom, crate::proxy::planning_area(app, &base), true);
if k < 2 || !photocraft_compose::proxy::proxy_faithful(&base) {
return None;
}
let p = app.adjust_preview.as_mut()?;
if p.proxy.as_ref().is_none_or(|g| g.k != k) {
let d = proxy_base(&base, k);
if !gpu.supports(&d) {
@@ -1,29 +1,32 @@
//! Preferences › Performance › Low Resolution Previews: on (the default) live previews of a large
//! document render on a reduced copy, off they render at full resolution.
//!
//! A 2502 × 2000 document of 1 px black and white columns: the reduced copy (every 2nd pixel at
//! this size) is all black, so its preview shows a flat colour where the full-size one shows the
//! columns at 100 % (and their mipmapped grey zoomed out). Drives the real app offscreen on wgpu;
//! A document of 1 px black and white columns: the reduced copy (every 2nd pixel) is all black,
//! so its preview shows a flat colour where the full-size one shows the columns at 100 %
//! and their mipmapped grey zoomed out. Drives the real app offscreen on wgpu;
//! skips when no GPU adapter exists (like `drag_preview_canvas.rs`).
use crate::PhotocraftApp;
use crate::control::{ControlRequest, handle};
use craft::behavior::kurbo::Point;
use photocraft_color::{Color, ColorMode, SampleType};
use photocraft_doc::{Document, Size};
use photocraft_geom::Rect;
use photocraft_ui_egui::PhotocraftApp;
use photocraft_ui_egui::control::{ControlRequest, handle};
use serde_json::{Value, json};
type Harness = egui_kittest::Harness<'static, Box<PhotocraftApp>>;
// Just above the 5 MP proxy threshold; keep the same factor-two reduction.
const W: i32 = 2502;
const H: i32 = 2000;
// Leave room for a visible patch of columns beside the dialogs at 50 % zoom.
const W: i32 = 800;
const H: i32 = 500;
const PREVIEW_AREA_MULTIPLIER: u64 = 13;
fn harness() -> Option<Harness> {
let built = std::panic::catch_unwind(|| {
egui_kittest::Harness::builder().with_size(egui::vec2(1100.0, 760.0)).with_pixels_per_point(1.0).with_max_steps(64).wgpu().build_eframe(|cc| {
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), Default::default());
app.preview_test_area_multiplier = PREVIEW_AREA_MULTIPLIER;
if let Some(rs) = cc.wgpu_render_state.as_ref() {
app.set_wgpu(rs.clone());
}
@@ -46,32 +49,35 @@ fn control(h: &mut Harness, method: &str, params: Value) {
h.run_steps(3);
}
fn columns_document() -> Document {
let mut doc = Document::with_background("columns", Size::new(W as u32, H as u32), ColorMode::Rgb, SampleType::U8, Color::WHITE);
assert_eq!(photocraft_ui_egui::proxy::factor(&doc), 2, "the fixture must exercise a reduced preview");
let s = doc.layers[0].surface_mut().expect("background pixels");
let row: Vec<f32> = (0..W)
fn columns_document(width: i32, height: i32) -> Result<Document, &'static str> {
let mut doc = Document::with_background("columns", Size::new(width as u32, height as u32), ColorMode::Rgb, SampleType::U8, Color::WHITE);
let s = doc.layers.first_mut().and_then(|layer| layer.surface_mut()).ok_or("missing background pixels")?;
let row: Vec<f32> = (0..width)
.flat_map(|x| {
let v = if x % 2 == 0 { 0.0 } else { 1.0 };
[v, v, v, 1.0]
})
.collect();
for y in (0..H).step_by(64) {
let end = (y + 64).min(H);
s.write_region(Rect::new(0, y, W, end), &row.repeat((end - y) as usize));
for y in (0..height).step_by(64) {
let end = (y + 64).min(height);
s.write_region(Rect::new(0, y, width, end), &row.repeat((end - y) as usize));
}
doc
Ok(doc)
}
fn setup(h: &mut Harness, zoom: f32) {
fn setup(h: &mut Harness, zoom: f32) -> Result<(), &'static str> {
{
let app = h.state_mut();
app.session.add_document(columns_document(), None);
let doc = columns_document(W, H)?;
assert_eq!(crate::proxy::factor(&doc), 1, "the fixture is below the production threshold");
assert_eq!(crate::proxy::app_factor(app, &doc), 2, "the test must exercise a twofold reduction");
app.session.add_document(doc, None);
app.sync_views();
app.ui.extras.rulers = false;
}
control(h, "ui.set", json!({"zoom": zoom, "center": [W / 2, H / 2]}));
h.run_steps(4);
Ok(())
}
/// Whether the canvas draws on the GPU (the previews under test are the GPU canvas's).
@@ -84,27 +90,29 @@ fn on_gpu(h: &mut Harness) -> bool {
gpu
}
fn low_res(h: &mut Harness, on: bool) {
h.state_mut().run("prefs.set", json!({"path": "performance.lowResolutionPreviews", "value": on})).expect("prefs.set");
fn low_res(h: &mut Harness, on: bool) -> Result<(), String> {
h.state_mut().run("prefs.set", json!({"path": "performance.lowResolutionPreviews", "value": on}))?;
h.run_steps(4);
Ok(())
}
/// Luma along a 300 px row near the canvas's top-left corner (clear of centred dialogs).
fn row(h: &mut Harness) -> Vec<i32> {
/// Luma along a row inside the visible document, to the left of centred dialogs.
fn row(h: &mut Harness) -> Result<Vec<i32>, &'static str> {
h.run_steps(4);
// The canvas as its last pass showed it: the rectangle the rendered pixels are sampled in.
let canvas = photocraft_ui_egui::canvas::active_view(h.state()).expect("the canvas has been shown").rect;
// egui_kittest waits 10 s for the GPU. Windows CI renders on WARP, a software rasterizer,
// where the first full-resolution composite of this 5 MP document can take longer; the
// submitted work still finishes, so wait for it again instead of failing.
let img = (0..3).find_map(|_| h.render().ok()).expect("render");
let (x0, y) = (canvas.min.x as u32 + 20, canvas.min.y as u32 + 40);
(x0..x0 + 300)
let view = crate::canvas::active_view(h.state()).ok_or("the canvas has not been shown")?;
let image = egui::Rect::from_two_pos(view.to_screen(Point::ORIGIN), view.to_screen(Point::new(f64::from(W), f64::from(H))));
let visible = view.rect.intersect(image);
// Windows CI uses a software rasterizer. A render timeout need not drop the submitted work.
let img = (0..3).find_map(|_| h.render().ok()).ok_or("could not render the canvas")?;
let (x0, y) = (visible.min.x.ceil() as u32 + 12, visible.min.y.ceil() as u32 + 12);
let x1 = x0 + 48;
assert!(visible.contains(egui::pos2(x1 as f32, y as f32)), "sample row is inside the visible image");
Ok((x0..x1)
.map(|x| {
let p = img.get_pixel(x, y).0;
(i32::from(p[0]) * 3 + i32::from(p[1]) * 6 + i32::from(p[2])) / 10
})
.collect()
.collect())
}
/// Fraction of neighbouring screen pixels that differ like black and white.
@@ -121,14 +129,14 @@ fn mean(r: &[i32]) -> f32 {
/// previews on; zoomed out to 50 % the default drag runs on the reduced copy (only the black columns, inverted), and with
/// them off it matches the committed view (the columns' mipmapped grey).
#[test]
fn adjustment_layer_drag_previews_at_full_resolution_when_low_res_is_off() {
fn adjustment_layer_drag_previews_at_full_resolution_when_low_res_is_off() -> Result<(), String> {
let _gpu = photocraft_testkit::gpu_lock();
let Some(mut h) = harness() else { return };
setup(&mut h, 1.0);
let Some(mut h) = harness() else { return Ok(()) };
setup(&mut h, 1.0)?;
if !on_gpu(&mut h) {
return;
return Ok(());
}
assert!(columns(&row(&mut h)) > 0.9, "the canvas shows the 1 px columns at 100 %");
assert!(columns(&row(&mut h)?) > 0.9, "the canvas shows the 1 px columns at 100 %");
let layer = {
let app = h.state_mut();
app.run("layer.newAdjustmentLayer.invert", json!({})).expect("adjustment layer");
@@ -137,66 +145,70 @@ fn adjustment_layer_drag_previews_at_full_resolution_when_low_res_is_off() {
app.live_adjust = Some((id, json!({})));
id
};
let at_100 = row(&mut h);
let at_100 = row(&mut h)?;
assert!(columns(&at_100) > 0.9, "default at 100 %: the drag is never coarser than the screen ({})", columns(&at_100));
control(&mut h, "ui.set", json!({"zoom": 0.5, "center": [W / 2, H / 2]}));
h.state_mut().live_adjust = None;
let committed = mean(&row(&mut h));
assert!(committed > 60.0, "zoomed out the inverted columns average to grey ({committed})");
let committed = mean(&row(&mut h)?);
assert!((60.0..220.0).contains(&committed), "zoomed out the inverted columns average to grey ({committed})");
h.state_mut().live_adjust = Some((layer, json!({})));
let reduced = mean(&row(&mut h));
let reduced = mean(&row(&mut h)?);
assert!(reduced > 225.0, "default at 50 %: the reduced copy keeps only the (inverted) black columns ({reduced})");
low_res(&mut h, false);
low_res(&mut h, false)?;
h.state_mut().live_adjust = Some((layer, json!({})));
let full = mean(&row(&mut h));
let full = mean(&row(&mut h)?);
assert!((full - committed).abs() < 8.0, "low-res previews off: the drag matches the committed view ({full} vs {committed})");
Ok(())
}
/// A filter dialog's live preview is never coarser than the screen either (#6326): at 100 % every
/// column shows with low-resolution previews on; zoomed out to 50 % the default preview runs on
/// the reduced copy (only the black columns), and with them off it matches the committed view.
#[test]
fn filter_dialog_previews_at_full_resolution_when_low_res_is_off() {
fn filter_dialog_previews_at_full_resolution_when_low_res_is_off() -> Result<(), String> {
let _gpu = photocraft_testkit::gpu_lock();
let Some(mut h) = harness() else { return };
setup(&mut h, 0.5);
let Some(mut h) = harness() else { return Ok(()) };
setup(&mut h, 0.5)?;
if !on_gpu(&mut h) {
return;
return Ok(());
}
let committed = mean(&row(&mut h));
assert!(committed > 60.0, "zoomed out the columns average to grey ({committed})");
let committed = mean(&row(&mut h)?);
assert!((60.0..220.0).contains(&committed), "zoomed out the columns average to grey ({committed})");
control(&mut h, "ui.set", json!({"zoom": 1.0, "center": [W / 2, H / 2]}));
// Offset 0, 0 leaves the pixels as they are: any change is the preview's resolution.
let _dialog = photocraft_ui_egui::filter_dialog::open(h.state_mut(), "filter.other.offset").expect("Offset dialog");
let at_100 = row(&mut h);
let _dialog = crate::filter_dialog::open(h.state_mut(), "filter.other.offset").expect("Offset dialog");
let at_100 = row(&mut h)?;
assert!(columns(&at_100) > 0.9, "default at 100 %: the preview is never coarser than the screen ({})", columns(&at_100));
control(&mut h, "ui.set", json!({"zoom": 0.5, "center": [W / 2, H / 2]}));
let reduced = mean(&row(&mut h));
let reduced = mean(&row(&mut h)?);
assert!(reduced < 30.0, "default at 50 %: the reduced copy keeps only the black columns ({reduced})");
low_res(&mut h, false);
let full = mean(&row(&mut h));
low_res(&mut h, false)?;
let full = mean(&row(&mut h)?);
assert!((full - committed).abs() < 8.0, "low-res previews off: the preview matches the committed view ({full} vs {committed})");
Ok(())
}
#[test]
fn adjustment_dialog_zoomed_out_skips_the_reduced_copy_when_low_res_is_off() {
fn adjustment_dialog_zoomed_out_skips_the_reduced_copy_when_low_res_is_off() -> Result<(), String> {
let _gpu = photocraft_testkit::gpu_lock();
let Some(mut h) = harness() else { return };
setup(&mut h, 0.5);
let Some(mut h) = harness() else { return Ok(()) };
setup(&mut h, 0.5)?;
if !on_gpu(&mut h) {
return;
return Ok(());
}
let committed = mean(&row(&mut h));
assert!(committed > 60.0, "zoomed out the columns average to grey ({committed})");
let _dialog = photocraft_ui_egui::adjust_dialog::open(h.state_mut(), "image.adjustments.levels").expect("Levels dialog");
let committed = mean(&row(&mut h)?);
assert!((60.0..220.0).contains(&committed), "zoomed out the columns average to grey ({committed})");
let _dialog = crate::adjust_dialog::open(h.state_mut(), "image.adjustments.levels").expect("Levels dialog");
h.run_steps(4);
assert!(photocraft_ui_egui::adjust_preview::gpu_proxy(h.state_mut(), 0, 0.5).is_some(), "default: zoomed out on the reduced copy");
let reduced = mean(&row(&mut h));
let proxy = crate::adjust_preview::gpu_proxy(h.state_mut(), 0, 0.5).expect("default: zoomed out on the reduced copy");
assert_eq!(proxy.k, 2);
let reduced = mean(&row(&mut h)?);
assert!(reduced < 30.0, "the reduced copy keeps only the black columns ({reduced})");
low_res(&mut h, false);
assert!(photocraft_ui_egui::adjust_preview::gpu_proxy(h.state_mut(), 0, 0.5).is_none(), "off: no reduced copy");
let full = mean(&row(&mut h));
low_res(&mut h, false)?;
assert!(crate::adjust_preview::gpu_proxy(h.state_mut(), 0, 0.5).is_none(), "off: no reduced copy");
let full = mean(&row(&mut h)?);
assert!((full - committed).abs() < 8.0, "off: the preview matches the committed view ({full} vs {committed})");
Ok(())
}
/// Check the encoded texture directly, before canvas painting or adjustment previews.
@@ -211,8 +223,11 @@ fn compositor_chunks_keep_their_columns() {
return;
};
eprintln!("canvas encode adapter {:?}", rs.adapter.get_info());
let gpu = photocraft_ui_egui::gpu_canvas::GpuCanvas::new(&rs);
let doc = columns_document();
let gpu = crate::gpu_canvas::GpuCanvas::new(&rs);
// Keep three chunks across and two down, with a partial chunk on both axes.
let width = 2 * photocraft_gpu::CHUNK + 24;
let height = photocraft_gpu::CHUNK + 24;
let doc = columns_document(width as i32, height as i32).expect("columns document");
let refresh = gpu.refresh(doc.id.0, &doc, None, None);
assert!(gpu.fault().is_none(), "GPU fault after canvas refresh: {:?}", gpu.fault());
if refresh.kind == "full" {
@@ -222,11 +237,11 @@ fn compositor_chunks_keep_their_columns() {
assert_eq!(refresh.kind, "gpu-full", "{:?}", refresh.fallback);
let (format, size, texels) = gpu.read_texels(doc.id.0).expect("read back the encoded canvas");
assert_eq!(format, eframe::wgpu::TextureFormat::Rgba8Unorm);
assert_eq!(size, [W as u32, H as u32]);
assert_eq!(texels.len(), (W * H) as usize);
assert_eq!(size, [width, height]);
assert_eq!(texels.len(), (width * height) as usize);
for (i, pixel) in texels.iter().enumerate() {
let x = i % W as usize;
let x = i % width as usize;
let v = (x % 2) as f32;
assert_eq!(*pixel, [v, v, v, 1.0], "canvas pixel ({x}, {})", i / W as usize);
assert_eq!(*pixel, [v, v, v, 1.0], "canvas pixel ({x}, {})", i / width as usize);
}
}
+7
View File
@@ -38,6 +38,10 @@ pub mod file_open;
pub mod frame_stages;
pub mod gpu_memo;
pub mod gpu_status;
#[cfg(all(test, not(target_arch = "wasm32")))]
mod gpu_tests {
mod full_res_preview;
}
mod history_keys;
pub mod home_recent_ui;
pub mod i18n;
@@ -413,6 +417,9 @@ pub struct PhotocraftApp {
control: Option<craft::automation::ControlChannel>,
/// Live (uncommitted) adjustment edit shown on canvas while a slider is dragged.
pub live_adjust: Option<(photocraft_doc::LayerId, Value)>,
/// Lets GPU tests use small fixtures while exercising large-document preview decisions.
#[cfg(test)]
preview_test_area_multiplier: u64,
/// Frames rendered (for tests and the status bar).
pub frame: u64,
/// What the notice stack keeps between frames: the pointer rest that gives a fresh auto-hide
+24 -2
View File
@@ -5,12 +5,17 @@
use craft::imaging::ui::live_preview::ProxyPlan;
use photocraft_doc::Document;
use crate::PhotocraftApp;
/// Target pixel count for previews (fast enough to composite every frame on CPU).
pub const PREVIEW_PIXELS: u64 = 2_500_000;
/// Downscale factor for a document, or 1 when it's already small.
pub fn factor(doc: &Document) -> u32 {
let px = doc.size.area();
factor_for_area(doc.size.area())
}
fn factor_for_area(px: u64) -> u32 {
if px <= PREVIEW_PIXELS * 2 {
return 1;
}
@@ -19,7 +24,7 @@ pub fn factor(doc: &Document) -> u32 {
/// Whether live previews may run on a reduced copy (Preferences › Performance › Low Resolution
/// Previews, on by default). Off, every live preview renders at full resolution.
pub fn reduced_previews(app: &crate::PhotocraftApp) -> bool {
pub fn reduced_previews(app: &PhotocraftApp) -> bool {
app.session.prefs().performance.low_resolution_previews
}
@@ -28,6 +33,19 @@ pub fn preview_factor(doc: &Document, reduced: bool) -> u32 {
if reduced { factor(doc) } else { 1 }
}
/// Pixel count for choosing preview resolution. Tests can scale the decision's input
/// while the fixture's real dimensions still govern rendering and allocation.
pub(crate) fn planning_area(_app: &PhotocraftApp, doc: &Document) -> u64 {
let area = doc.size.area();
#[cfg(test)]
let area = area.saturating_mul(_app.preview_test_area_multiplier);
area
}
pub(crate) fn app_factor(app: &PhotocraftApp, doc: &Document) -> u32 {
if reduced_previews(app) { factor_for_area(planning_area(app, doc)) } else { 1 }
}
/// A live preview's factor `k` capped for a view at `zoom` (physical screen pixels per document
/// pixel) of a document of `document_pixels`: never coarser than the screen
/// ([`ProxyPlan::factor`]), so a view at 100 % shows full resolution instead of k-pixel blocks while
@@ -48,6 +66,10 @@ mod tests {
fn factor_scales_with_size() {
let small = Document::new("s", Size::new(1000, 1000), ColorMode::Rgb, SampleType::U8);
assert_eq!(factor(&small), 1);
let boundary = Document::new("threshold", Size::new(2500, 2000), ColorMode::Rgb, SampleType::U8);
let above = Document::new("above threshold", Size::new(2501, 2000), ColorMode::Rgb, SampleType::U8);
assert_eq!(factor(&boundary), 1);
assert_eq!(factor(&above), 2);
let big = Document::new("b", Size::new(6016, 6016), ColorMode::Rgb, SampleType::U8);
let k = factor(&big);
assert!((3..=5).contains(&k), "{k}");
-1
View File
@@ -60,7 +60,6 @@ mod gpu_tests {
mod color_managed_canvas;
mod display_shadows;
mod drag_preview_canvas;
mod full_res_preview;
mod live_stroke_canvas;
mod pattern_preview_canvas;
mod ui_scale_canvas;