PhotoCraft: An open-source, photo editor written by scratch in pure Rust
ops: count selection masks in history pixel budget (#3614)
Streetblockf4fa53140cb0
diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rsindex 70cbebe0f..2a82edb41 100644--- a/crates/ops/src/lib.rs+++ b/crates/ops/src/lib.rs@@ -5,8 +5,8 @@//! stores whole-document snapshots per transaction, which is simple, obviously correct, and the//! same approach Photoshop's History panel exposes to users (one state per step).//!-//! Memory is bounded by `max_states` plus an approximate byte budget. Only tiles *not shared*-//! with the current document count against it.+//! Memory is bounded by `max_states` plus an approximate byte budget. The current document's+//! tiles and tiles only history holds count against it, including selection masks.#![forbid(unsafe_code)]#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]@@ -248,7 +248,7 @@ impl History {}}-/// Bytes of the pixel tiles of `doc` (layers, masks, alpha channels) not already in `seen`.+/// Bytes of the pixel tiles of `doc` (layers, masks, alpha channels, selection) not already in `seen`.fn tile_bytes(doc: &Document, seen: &mut HashSet<usize>) -> usize {let mut n = 0usize;tiles::surfaces(doc, |surface| {@@ -408,6 +408,85 @@ mod tests {assert_eq!(h.unique_bytes(&cur), 256 * 256 * 4);}+ #[test]+ fn selection_tiles_count_current_and_history_without_double_counting() {+ use photocraft_doc::{PixelFormat, Surface};+ for depth in SampleType::ALL {+ let mut doc = Document::new("selection", Size::new(300, 64), ColorMode::Rgb, depth);+ let mut selection = Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false));+ selection.write_pixel(1, 1, &[1.0]);+ selection.write_pixel(257, 1, &[1.0]);+ doc.selection = Some(selection);+ let mut cur = Arc::new(doc);+ let tile = match depth {+ SampleType::U8 => 65_536,+ SampleType::U16 => 131_072,+ SampleType::F32 => 262_144,+ };+ let mut h = History { max_bytes: usize::MAX, ..History::default() };+ assert_eq!(h.pixel_bytes(&cur), 2 * tile, "current selection counts without any history");+ assert_eq!(h.unique_bytes(&cur), 0);+ edit(&mut h, &mut cur, "Rename", |doc| doc.name = "renamed".into());+ assert_eq!(h.pixel_bytes(&cur), 2 * tile, "the shared selection tiles count once");+ edit(&mut h, &mut cur, "Selection", |doc| {+ doc.selection.as_mut().unwrap().write_pixel(1, 1, &[0.5]);+ });+ assert_eq!(h.pixel_bytes(&cur), 3 * tile, "only the changed selection tile is copied");+ assert_eq!(h.unique_bytes(&cur), tile);+ h.trim(&cur);+ assert_counter(&h, &cur);+ let selected_value = cur.selection.as_ref().unwrap().sample_channel(1, 1, 0);+ edit(&mut h, &mut cur, "Deselect", |doc| doc.selection = None);+ assert_eq!(h.pixel_bytes(&cur), 3 * tile, "deselecting leaves the previous masks in history");+ assert_eq!(h.unique_bytes(&cur), 3 * tile);+ assert_counter(&h, &cur);+ cur = h.undo(cur).unwrap().0;+ assert_eq!(cur.selection.as_ref().unwrap().sample_channel(1, 1, 0), selected_value);+ assert_eq!(h.pixel_bytes(&cur), 3 * tile);+ assert_eq!(h.unique_bytes(&cur), tile);+ assert_counter(&h, &cur);+ // Coalescing can change the current mask without recording another state.+ Arc::make_mut(&mut cur).selection.as_mut().unwrap().write_pixel(257, 1, &[0.25]);+ assert_eq!(h.pixel_bytes(&cur), 4 * tile);+ assert_eq!(h.trim(&cur), 0);+ assert_counter(&h, &cur);+ cur = h.redo(cur).unwrap().0;+ assert!(cur.selection.is_none());+ assert_eq!(h.pixel_bytes(&cur), 4 * tile);+ assert_counter(&h, &cur);+ }+ }++ #[test]+ fn selection_byte_budget_drops_old_masks_and_keeps_the_last_undo() {+ use photocraft_doc::{PixelFormat, Surface};+ for depth in SampleType::ALL {+ let mut doc = Document::new("selection", Size::new(64, 64), ColorMode::Rgb, depth);+ doc.selection = Some(Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false)));+ let mut cur = Arc::new(doc);+ let tile = 256 * 256 * depth.bytes();+ let mut h = History::default();+ for i in 1..=6 {+ edit(&mut h, &mut cur, "Selection", |doc| {+ doc.selection.as_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0]);+ });+ }+ assert_eq!(h.pixel_bytes(&cur), 6 * tile);+ h.max_bytes = 3 * tile;+ assert_eq!(h.trim(&cur), 4);+ assert_eq!(h.past_len(), 2);+ assert_eq!(h.pixel_bytes(&cur), 3 * tile);+ assert_eq!(h.unique_bytes(&cur), 2 * tile);+ assert_counter(&h, &cur);+ h.max_bytes = 1;+ assert_eq!(h.trim(&cur), 1);+ assert_eq!(h.past_len(), 1, "the most recent undo survives even below one mask's size");+ assert_eq!(h.pixel_bytes(&cur), 2 * tile);+ cur = h.undo(cur).unwrap().0;+ assert_counter(&h, &cur);+ }+ }+fn assert_counter(h: &History, current: &Document) {let counter = h.tiles.as_ref().expect("budget initializes the counter");let total = tile_bytes(current, &mut HashSet::new()).saturating_add(h.unique_bytes(current));@@ -506,11 +585,13 @@ mod tests {fn counter_tracks_eviction_undo_redo_branching_purge_and_clones() {for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {let mut cur = Arc::new(Document::with_background("h", Size::new(300, 64), ColorMode::Rgb, depth, Color::WHITE));+ Arc::make_mut(&mut cur).selection = Some(photocraft_doc::Surface::new(photocraft_doc::PixelFormat::GRAY8));let mut h = History::new(4);h.max_bytes = usize::MAX;for i in 0..8 {edit(&mut h, &mut cur, "Paint", |doc| {doc.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0, 0.0, 0.0, 1.0]);+ doc.selection.as_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0]);});h.trim(&cur);if i >= 1 {@@ -547,6 +628,7 @@ mod tests {gray.write_pixel(1, 1, &[1.0]);doc.layers[0].mask = Some(LayerMask { surface: gray.clone(), ..LayerMask::reveal_all() });doc.channels.push(AlphaChannel::new("alpha", gray.clone()));+ doc.selection = Some(gray.clone());doc.quick_mask = Some(AlphaChannel::new("quick", gray));doc.layers.push(doc.layers[0].clone());let mut cur = Arc::new(doc);diff --git a/crates/ops/src/tiles.rs b/crates/ops/src/tiles.rsindex c4178b5b3..feb639c87 100644--- a/crates/ops/src/tiles.rs+++ b/crates/ops/src/tiles.rs@@ -30,6 +30,9 @@ pub(crate) fn surfaces(doc: &Document, mut visit: impl FnMut(&Surface)) {for channel in doc.channels.iter().chain(&doc.quick_mask) {visit(&channel.surface);}+ if let Some(selection) = &doc.selection {+ visit(selection);+ }}impl Tiles {