diff --git a/crates/ui-egui/src/canvas/move_ui.rs b/crates/ui-egui/src/canvas/move_ui.rs index 83b74a9c8d7..172616cb148 100644 --- a/crates/ui-egui/src/canvas/move_ui.rs +++ b/crates/ui-egui/src/canvas/move_ui.rs @@ -58,19 +58,30 @@ pub(crate) fn float_selected(app: &mut PhotocraftApp, copy: bool, dx: f64, dy: f /// Show bounding box when dragging layer, and the release moves the layers once. const LIVE_LIMIT: u64 = 512 << 20; -/// Whether the pixel memory a live drag frame writes for the layers `ids` of `doc` (their content -/// and linked masks, worst case four tiles per stored tile) exceeds `limit`. -fn exceeds(doc: &Document, ids: &[LayerId], limit: u64) -> bool { - // A shift by a pixel offset that isn't a whole tile writes up to four new tiles per stored one. - let bytes = |s: &photocraft_raster::Surface| { - (s.tile_count() as u64) - .saturating_mul((photocraft_geom::TILE_SIZE as u64).pow(2)) - .saturating_mul(s.format().bytes_per_pixel() as u64) - .saturating_mul(4) - }; +/// Pixel memory a shift of `s` by a pixel offset that isn't a whole tile writes: one target tile per +/// tile the content can reach (dense layers write about as many tiles as they store), and at most +/// four target tiles per stored one (isolated tiles each straddle four). +fn shift_bytes(s: &photocraft_raster::Surface) -> u64 { + let b = photocraft_compose::bounds::content_bounds(s); + if b.is_empty() { + return 0; + } + let t = photocraft_geom::TILE_SIZE as u64; + let reach = (u64::from(b.width()).div_ceil(t) + 1).saturating_mul(u64::from(b.height()).div_ceil(t) + 1); + let tiles = (s.tile_count() as u64).saturating_mul(4).min(reach); + tiles.saturating_mul(t * t).saturating_mul(s.format().bytes_per_pixel() as u64) +} + +/// Whether the pixel memory a live drag frame writes for the layers `ids` of `doc` exceeds `limit`: +/// what `moved_target` shifts, that is a layer's content and linked mask, or only its mask when +/// `mask_target` drags an unlinked mask alone. +fn exceeds(doc: &Document, ids: &[LayerId], limit: u64, mask_target: bool) -> bool { let mut total = 0u64; for l in ids.iter().filter_map(|id| doc.layer(*id)) { - total = total.saturating_add(l.surface().map_or(0, bytes)).saturating_add(l.mask.as_ref().map_or(0, |m| bytes(&m.surface))); + let mask_alone = mask_target && photocraft_engine::layer_multi_cmds::moves_mask_alone(l); + let content = if mask_alone { 0 } else { l.surface().map_or(0, shift_bytes) }; + let mask = l.mask.as_ref().filter(|m| mask_alone || m.linked).map_or(0, |m| shift_bytes(&m.surface)); + total = total.saturating_add(content).saturating_add(mask); if total > limit { return true; } @@ -169,7 +180,7 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc 0.5), "the release moved the layer"); } +/// What a live drag frame writes is the tiles the content can reach, not four per stored tile for +/// every layer: a dense photo stays live, isolated tiles (each straddles four) don't (#6114). +#[test] +fn the_estimate_counts_reachable_target_tiles_and_what_moved_target_copies() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent"})).unwrap(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + let step = photocraft_geom::TILE_SIZE; + let tile_bytes = u64::from(step.unsigned_abs()).pow(2) * 4; + let tiles_over_limit = (super::LIVE_LIMIT / tile_bytes + 1) as i32; + // A dense block of the same number of stored tiles as the sparse one below. + let side = (f64::from(tiles_over_limit / 4)).sqrt() as i32; + app.session + .edit("paint", |doc, a| { + let surface = doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap(); + let tile = surface.solid_tile(&[1.0, 0.0, 0.0, 1.0]); + surface.put_tiles((0..side * side).map(|i| (photocraft_geom::TileCoord::new(i % side, i / side), tile.clone()))); + Ok(()) + }) + .unwrap(); + let st = app.session.active().unwrap(); + let id = st.active_layer.unwrap(); + assert!(!super::exceeds(&st.doc, &[id], super::LIVE_LIMIT, false), "a dense layer writes about as many tiles as it stores"); + // The same count of isolated tiles: each straddles four target tiles. + let sparse = tiles_over_limit / 4 + 1; + app.session + .edit("paint", |doc, a| { + let surface = doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap(); + let tile = surface.solid_tile(&[1.0, 0.0, 0.0, 1.0]); + surface.put_tiles((0..sparse).map(|i| (photocraft_geom::TileCoord::new(i * 10, i * 10 + 1000), tile.clone()))); + Ok(()) + }) + .unwrap(); + let st = app.session.active().unwrap(); + assert!(super::exceeds(&st.doc, &[id], super::LIVE_LIMIT / 4, false), "isolated tiles each write up to four"); + // Only the mask is copied when an unlinked mask is dragged alone: the huge content isn't charged. + app.session.execute("layer.layerMask.revealAll", json!({})).unwrap(); + app.session.execute("layer.layerMask.linked", json!({"linked": false})).unwrap(); + let st = app.session.active().unwrap(); + assert!(super::exceeds(&st.doc, &[id], 1, false), "dragging the layer copies its content"); + assert!(!super::exceeds(&st.doc, &[id], super::LIVE_LIMIT / 4, true), "dragging the unlinked mask alone copies the mask only"); +} + #[test] fn bounding_box_preference_turns_the_live_drag_off() { assert!(live(false), "by default the layer follows the pointer");