PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Integration: merge main (#6966)
David Mann1e3323f64cd5
diff --cc crates/ui-egui/src/canvas.rsindex 54cfaa374,7f9db071c..8c1c9c82f--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@@ -142,11 -202,42 +202,45 @@@ pub struct CanvasCache/// `revision`, so the Navigator never shows a stale CPU image after GPU-path edits.pub tex_revision: u64,pub tex_preview_key: u64,+ /// All uploaded texels are opaque. Screen coverage is checked separately before omitting+ /// the checker. Updated with uploads, never by scanning during an unchanged frame.+ pub texture_opaque: bool,/// The CPU path's pixel grid for the part of the document in view (pixel_grid.rs).pub grid: Option<crate::pixel_grid::GridLines>,+ /// The CPU path's sharper textures of the part in view, over a reduced `texture` (#7568),+ /// by `InView::key` (two windows of a document can show different parts).+ pub detail: Vec<(u64, DetailTexture)>,+ }++ /// [`CanvasCache::detail`]: the part of the document `DetailPlan` chose, uploaded at its factor.+ pub struct DetailTexture {+ texture: egui::TextureHandle,+ plan: DetailPlan,+ /// The texels it holds, in the plan's reduced pixels (`render_reduced_damage`'s output rect).+ out: DRect,+ /// Revision and preview key (display key folded in) it was rendered at.+ revision: u64,+ preview_key: u64,+ /// Pixels were composited for it this call (for the perf span).+ rendered: bool,+ }++ /// The CPU canvas textures to draw: the whole document, and over it the sharper part in view.+ pub(crate) struct CpuTextures {+ pub texture: egui::TextureId,+ pub scale: f32,+ /// The detail texture and the document area it covers (x0, y0, x1, y1 in document pixels).+ pub detail: Option<(egui::TextureId, [f32; 4])>,+ }++ /// The part of a CPU canvas view in sight: its document area and zoom (physical pixels per+ /// document pixel), for [`ensure_texture_in_view`]'s detail texture.+ #[derive(Clone, Copy, Debug)]+ pub(crate) struct InView {+ pub visible: DRect,+ pub zoom: f32,+ /// Which view this is, so each keeps its own detail texture.+ pub key: u64,}/// An in-progress pointer gesture on the canvas.@@@ -1426,8 -1656,8 +1659,9 @@@ pub(crate) fn ensure_texture_in_viewon_gpu: false,tex_revision: 0,tex_preview_key: 0,+ texture_opaque: false,grid: None,+ detail: Vec::new(),});if cache.tex_revision != revision || cache.texture.is_none() || cache.tex_preview_key != preview_key {let t0 = crate::gpu_canvas::now_ms();@@@ -1621,8 -1973,8 +1982,9 @@@ fn ensure_gpu(app: &mut PhotocraftApp,on_gpu: false,tex_revision: 0,tex_preview_key: 0,+ texture_opaque: false,grid: None,+ detail: Vec::new(),});let present = cache.on_gpu && gpu.has(id.0, size);let current = cache.revision == revision && cache.preview_key == preview_key;@@@ -3098,13 -3580,18 +3590,21 @@@ pub fn canvas_view(app: &mut Photocraft}let rotated = view.rotation.abs() > 0.05;let checkerboard = app.session.prefs().transparency_and_gamut.square().map(|square| (checker(app, &ctx), square));- let tex = ensure_texture(app, &ctx, idx, output).map(|(tex, _scale)| tex);+ // A document too large for one full-resolution texture also gets the part in view at+ // the screen's resolution, drawn over the reduced whole (#7568).+ let in_view =+ InView { visible: visible_doc_rect(&xf), zoom: view.zoom, key: egui::Id::new(("pc-canvas-detail", ctx.viewport_id(), idx, primary)).value() };+ let textures = ensure_texture_in_view(app, &ctx, idx, output, Some(in_view));+ let tex = textures.as_ref().map(|t| t.texture);- paint_cpu_document(&painter, &xf, doc.bounds(), rotated, checkerboard, tex);+ // Repeated-quad coverage is not proven identical on all Pattern Preview adapters.+ // Retain its legacy checker submissions, including the central document's.+ let opaque = !app.ui.view.pattern_preview && app.canvases.get(&cache_key(doc.id, output)).is_some_and(|c| c.texture_opaque) && tex.is_some();+ paint_cpu_document(&painter, &xf, doc.bounds(), rotated, checkerboard, tex, opaque);+ if let Some((detail, area)) = textures.and_then(|t| t.detail) {+ paint_cpu_detail(&painter, &xf, doc.bounds(), rotated, checkerboard, detail, area);+ }for &c in &pattern {- paint_cpu_document(&painter, &pattern_xf(&xf, c, size), doc.bounds(), rotated, checkerboard, tex);+ paint_cpu_document(&painter, &pattern_xf(&xf, c, size), doc.bounds(), rotated, checkerboard, tex, opaque);}}// Channels panel: alpha / Quick Mask overlays and single-channel views (channel_view.rs).@@@ -8723,6 -10050,74 +10067,78 @@@ mod zoom_animation_tests#[path = "canvas_display_memo_tests.rs"]mod display_memo_tests;+#[cfg(test)]+#[path = "canvas_cpu_background_tests.rs"]+mod cpu_background_tests;+++ #[cfg(test)]+ #[path = "canvas/blend_viewport_tests.rs"]+ mod blend_viewport_regression;++ #[cfg(test)]+ mod lasso_preview_tests {+ use super::*;++ fn xf() -> ViewXform {+ ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }+ }++ /// What `draw` paints: (black dashes, black solid paths).+ fn painted(app: &mut PhotocraftApp, draw: impl Fn(&mut PhotocraftApp, &egui::Painter)) -> (usize, usize) {+ let ctx = egui::Context::default();+ let mut output = ctx.run_ui(egui::RawInput::default(), |ui| draw(app, ui.painter()));+ output.textures_delta.clear();+ let mut flat = Vec::new();+ for clipped in output.shapes {+ let mut stack = vec![clipped.shape];+ while let Some(shape) = stack.pop() {+ match shape {+ egui::Shape::Vec(v) => stack.extend(v),+ s => flat.push(s),+ }+ }+ }+ let dashes = flat.iter().filter(|s| matches!(s, egui::Shape::LineSegment { stroke, .. } if stroke.color == Color32::BLACK)).count();+ let solid = flat.iter().filter(|s| matches!(s, egui::Shape::Path(p) if p.stroke.color == egui::epaint::ColorMode::Solid(Color32::BLACK))).count();+ (dashes, solid)+ }++ fn document() -> PhotocraftApp {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap();+ app.sync_views();+ app+ }++ /// #7495: as in Photoshop, the Lasso's, Polygonal Lasso's and Magnetic Lasso's outline is a+ /// solid line until the selection is made; a Patch area dragged out keeps its marching ants.+ #[test]+ fn lasso_outlines_are_solid_until_the_selection_is_made() {+ let points = vec![[20.0, 20.0, 1.0], [120.0, 30.0, 1.0], [150.0, 140.0, 1.0], [40.0, 160.0, 1.0]];+ for tool in [Tool::Lasso, Tool::Patch] {+ let mut app = document();+ app.ui.tool = tool;+ app.drag = Some(Drag::new(tool, [20.0, 20.0], points.clone(), egui::Modifiers::NONE, false));+ let (dashes, solid) = painted(&mut app, |app, p| draw_drag_preview(app, p, &xf()));+ if tool == Tool::Lasso {+ assert_eq!((dashes, solid), (0, 1), "{tool:?}: one solid outline, no ants");+ } else {+ assert!(dashes > 10 && solid == 0, "{tool:?}: still marching ants ({dashes} dashes)");+ }+ }++ let mut app = document();+ app.ui.tool = Tool::PolygonLasso;+ app.ui.polygon = vec![[20.0, 20.0], [120.0, 30.0], [150.0, 140.0]];+ let (dashes, solid) = painted(&mut app, |app, p| draw_tool_state(app, p, &xf(), Some(pos2(60.0, 150.0))));+ assert_eq!((dashes, solid), (0, 1), "Polygonal Lasso: its sides and rubber band are one solid line");++ let mut app = document();+ app.ui.tool = Tool::MagneticLasso;+ app.ui.magnetic.path = vec![[20.0, 20.0], [80.0, 25.0], [120.0, 60.0]];+ app.ui.magnetic.anchors = vec![0, 2];+ app.ui.magnetic.live = vec![[120.0, 60.0], [130.0, 100.0]];+ let (dashes, solid) = painted(&mut app, |app, p| draw_tool_state(app, p, &xf(), None));+ assert_eq!((dashes, solid), (0, 1), "Magnetic Lasso: the traced border is solid");+ }+ }