Move: estimate the tiles a live drag frame writes by what the content can reach, honour mask-only drags; cargo fmt

This commit is contained in:
SiFuMax09 committed 2026-10-29 01:53:13 +01:00
1 parent fbee069f0e
commit c8ddb8289a
2 files changed
+72 -18

No files matched your search

+23 -12
View File
@@ -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<Do
let bounds = ids.iter().try_fold(Rect::EMPTY, |acc, id| Some(acc.union(&photocraft_compose::change_bounds(doc.layer(*id)?, all)?)));
let canvas = doc.bounds();
let moving = with_descendants(&doc, &ids);
let too_big = exceeds(&doc, &moving, LIVE_LIMIT);
let too_big = exceeds(&doc, &moving, LIVE_LIMIT, mask_target);
let bounds = bounds.map(|b| moving.iter().filter_map(|id| shape_reach(doc.layer(*id)?, canvas)).fold(b, |acc, r| acc.union(&r)));
app.move_preview = Some(MovePreview { doc: doc_id, revision, ids, moving, bounds, canvas, offsets: Vec::new(), shown: None, floating: false, too_big });
}
+49 -6
View File
@@ -172,11 +172,11 @@ fn layers_over_the_limit_are_not_followed_live() {
let st = app.session.active().unwrap();
let id = st.active_layer.unwrap();
let one_tile = u64::from(photocraft_geom::TILE_SIZE.unsigned_abs()).pow(2) * 4;
assert!(super::exceeds(&st.doc, &[id], one_tile - 1), "a tile of pixels is over a limit below its size");
assert!(super::exceeds(&st.doc, &[id], one_tile * 4 - 1), "a shift can write four tiles for each stored one");
assert!(!super::exceeds(&st.doc, &[id], u64::MAX), "nothing is over an unlimited drag");
assert!(!super::exceeds(&st.doc, &[], 0), "no layers hold no pixels");
assert!(!super::exceeds(&st.doc, &[id], super::LIVE_LIMIT), "an ordinary layer follows the pointer");
assert!(super::exceeds(&st.doc, &[id], one_tile - 1, false), "a tile of pixels is over a limit below its size");
assert!(super::exceeds(&st.doc, &[id], one_tile * 4 - 1, false), "a shift can write four tiles for each stored one");
assert!(!super::exceeds(&st.doc, &[id], u64::MAX, false), "nothing is over an unlimited drag");
assert!(!super::exceeds(&st.doc, &[], 0, false), "no layers hold no pixels");
assert!(!super::exceeds(&st.doc, &[id], super::LIVE_LIMIT, false), "an ordinary layer follows the pointer");
}
/// A drag whose layers are past the limit shows no live copy (the outline and arrow stay), and
@@ -197,7 +197,7 @@ fn a_drag_over_the_limit_shows_no_live_copy_and_still_commits() {
.edit("paint", |doc, a| {
let surface = doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap();
let tile_bytes = u64::from(step.unsigned_abs()).pow(2) * surface.format().bytes_per_pixel() as u64;
let count = (super::LIVE_LIMIT / (tile_bytes * 4) + 1) as i32;
let count = (super::LIVE_LIMIT / (tile_bytes * 2) + 2) as i32; // a row writes about (n + 1) x 2 tiles
let tile = surface.solid_tile(&[1.0, 0.0, 0.0, 1.0]);
surface.put_tiles((0..count).map(|x| (photocraft_geom::TileCoord::new(x, 0), tile.clone())));
Ok(())
@@ -217,6 +217,49 @@ fn a_drag_over_the_limit_shows_no_live_copy_and_still_commits() {
assert!(d.layer(id).and_then(|l| l.surface()).is_some_and(|s| s.pixel(to as i32, 16)[3] > 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");