Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Count video footage in history memory budgets (#3894)

jonathanwxh-cell8072636e0f5a
diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rs
index 0f29cf8e9..70cbebe0f 100644
--- a/crates/ops/src/lib.rs
+++ b/crates/ops/src/lib.rs
@@ -419,6 +419,89 @@ mod tests {
assert_eq!(counter.bytes_with(&empty), retained as u128);
}
+ fn video_document(depth: SampleType, frames: usize) -> Document {
+ let mut doc = Document::new("video", Size::new(64, 64), ColorMode::Rgb, depth);
+ let mut layer = Layer::raster("Video", doc.pixel_format());
+ let footage: Vec<_> = (0..frames)
+ .map(|i| {
+ let mut surface = photocraft_doc::Surface::new(doc.pixel_format());
+ surface.write_pixel(1, 1, &[i as f32 / frames as f32, 0.0, 0.0, 1.0]);
+ surface
+ })
+ .collect();
+ layer.content = photocraft_doc::LayerContent::Raster(footage[0].clone());
+ layer.video = Some(photocraft_doc::VideoData::new(footage, 24.0));
+ doc.layers.push(layer);
+ doc
+ }
+
+ #[test]
+ fn video_memory_counts_originals_and_deduplicates_displayed_frames() {
+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
+ let mut doc = video_document(depth, 3);
+ let tile = doc.layers[0].surface().unwrap().tiles().next().unwrap().1.bytes().len();
+ let video = doc.layers[0].video.as_mut().unwrap();
+ video.original_frames = Some(video.frames.clone());
+ // Original footage and the displayed frame alias the stack, except this painted frame.
+ video.frames[2].write_pixel(1, 1, &[0.0, 1.0, 0.0, 1.0]);
+ let mut cur = Arc::new(doc);
+ let mut h = History { max_bytes: usize::MAX, ..History::default() };
+ assert_eq!(h.pixel_bytes(&cur), 4 * tile);
+ for name in ["A", "B"] {
+ edit(&mut h, &mut cur, "Rename", |doc| doc.name = name.into());
+ h.trim(&cur);
+ }
+ assert_counter(&h, &cur);
+ assert_eq!(h.pixel_bytes(&cur), 4 * tile);
+ assert_eq!(h.unique_bytes(&cur), 0);
+ // Rasterizing keeps the displayed frame; the rest of the clip survives only in undo.
+ edit(&mut h, &mut cur, "Rasterize", |doc| doc.layers[0].video = None);
+ assert_eq!(h.unique_bytes(&cur), 3 * tile);
+ assert_eq!(h.pixel_bytes(&cur), 4 * tile);
+ assert_counter(&h, &cur);
+ cur = h.undo(cur).unwrap().0;
+ assert_eq!(h.unique_bytes(&cur), 0);
+ assert_counter(&h, &cur);
+ cur = h.redo(cur).unwrap().0;
+ assert_eq!(h.unique_bytes(&cur), 3 * tile);
+ assert_counter(&h, &cur);
+ h.clear();
+ assert_eq!(h.pixel_bytes(&cur), tile);
+ }
+ }
+
+ #[test]
+ fn video_memory_budget_evicts_deleted_off_playhead_frames() {
+ for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
+ let mut cur = Arc::new(video_document(depth, 6));
+ let tile = cur.layers[0].surface().unwrap().tiles().next().unwrap().1.bytes().len();
+ let mut h = History::default();
+ for _ in 0..4 {
+ edit(&mut h, &mut cur, "Delete Frame", |doc| {
+ doc.layers[0].video.as_mut().unwrap().frames.remove(1);
+ });
+ }
+ assert_eq!(h.pixel_bytes(&cur), 6 * tile);
+ assert_eq!(h.unique_bytes(&cur), 4 * tile);
+ h.max_bytes = 3 * tile;
+ assert_eq!(h.trim(&cur), 3);
+ assert_eq!(h.past_len(), 1);
+ assert_eq!(h.pixel_bytes(&cur), 3 * tile);
+ assert_eq!(h.unique_bytes(&cur), tile);
+ assert_counter(&h, &cur);
+ cur = h.undo(cur).unwrap().0;
+ assert_eq!(cur.layers[0].video.as_ref().unwrap().frames.len(), 3);
+ assert_counter(&h, &cur);
+ cur = h.redo(cur).unwrap().0;
+ assert_eq!(cur.layers[0].video.as_ref().unwrap().frames.len(), 2);
+ assert_counter(&h, &cur);
+ // A budget below the remaining clip still keeps the latest step undoable.
+ h.max_bytes = 1;
+ assert_eq!(h.trim(&cur), 0);
+ assert!(h.can_undo());
+ }
+ }
+
#[test]
fn counter_tracks_eviction_undo_redo_branching_purge_and_clones() {
for depth in [SampleType::U8, SampleType::U16, SampleType::F32] {
diff --git a/crates/ops/src/tiles.rs b/crates/ops/src/tiles.rs
index 59d73cb50..c4178b5b3 100644
--- a/crates/ops/src/tiles.rs
+++ b/crates/ops/src/tiles.rs
@@ -19,6 +19,13 @@ pub(crate) fn surfaces(doc: &Document, mut visit: impl FnMut(&Surface)) {
if let Some(mask) = &layer.mask {
visit(&mask.surface);
}
+ if let Some(video) = &layer.video {
+ // Undo retains the whole clip, not only the displayed frame. Visit aliases too:
+ // Tiles tracks every owner and counts their shared allocations only once.
+ for frame in video.frames.iter().chain(video.original_frames.iter().flatten()) {
+ visit(frame);
+ }
+ }
}
for channel in doc.channels.iter().chain(&doc.quick_mask) {
visit(&channel.surface);