Skip to content

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

Background redraw: a kept composite holds its state weakly

Higgs325844ea30f3673f2
diff --git a/crates/ui-egui/src/background_redraw.rs b/crates/ui-egui/src/background_redraw.rs
index 9b3837d53..5b8b5d7d8 100644
--- a/crates/ui-egui/src/background_redraw.rs
+++ b/crates/ui-egui/src/background_redraw.rs
@@ -16,9 +16,9 @@
//! uploads those blocks instead of compositing every layer again.
use std::collections::VecDeque;
-use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::{Receiver, SyncSender, TryRecvError};
+use std::sync::{Arc, Weak};
use photocraft_doc::{DocId, Document};
use photocraft_engine::display_color::CanvasDisplay;
@@ -86,7 +86,10 @@ impl Drop for Redraw {
/// A finished redraw: the canvas texels of every block of `doc`.
pub(crate) struct Kept {
- doc: Arc<Document>,
+ /// Weak, so a kept composite never keeps a state alive (and its tiles allocated, outside Memory
+ /// Usage) once History has dropped it; such an entry is dead and goes at the next trim.
+ doc: Weak<Document>,
+ id: DocId,
display: u64,
format: eframe::wgpu::TextureFormat,
blocks: Vec<Rect>,
@@ -100,7 +103,10 @@ impl Kept {
/// else it holds), so a stale composite can't come back; Arc-shared tiles compare without
/// reading their pixels.
fn matches(&self, doc: &Arc<Document>, display: u64, format: eframe::wgpu::TextureFormat, blocks: &[Rect]) -> bool {
- self.display == display && self.format == format && self.blocks == blocks && (Arc::ptr_eq(&self.doc, doc) || *self.doc == **doc)
+ self.display == display
+ && self.format == format
+ && self.blocks == blocks
+ && self.doc.upgrade().is_some_and(|kept| Arc::ptr_eq(&kept, doc) || *kept == **doc)
}
}
@@ -119,15 +125,18 @@ impl KeptComposites {
self.entries.front()
}
- /// Keep `k` (in place of an entry of the same state), within `cap` bytes in all.
- fn store(&mut self, k: Kept, cap: u64) {
- self.entries.retain(|e| !e.matches(&k.doc, k.display, k.format, &k.blocks));
+ /// Keep `k`, the redraw of `doc` (in place of an entry of the same state), within `cap` bytes
+ /// in all.
+ fn store(&mut self, k: Kept, doc: &Arc<Document>, cap: u64) {
+ self.entries.retain(|e| !e.matches(doc, k.display, k.format, &k.blocks));
self.entries.push_front(k);
self.trim(cap);
}
- /// Drop the oldest entries until at most [`KEEP`], holding at most `cap` bytes, are left.
+ /// Drop dead entries (their state is gone), then the oldest until at most [`KEEP`], holding at
+ /// most `cap` bytes, are left.
fn trim(&mut self, cap: u64) {
+ self.entries.retain(|k| k.doc.strong_count() > 0);
while self.entries.len() > KEEP || self.bytes() > cap {
self.entries.pop_back();
}
@@ -138,7 +147,7 @@ impl KeptComposites {
}
fn retain(&mut self, live: impl Fn(DocId) -> bool) {
- self.entries.retain(|k| live(k.doc.id));
+ self.entries.retain(|k| live(k.id));
}
}
@@ -275,8 +284,16 @@ pub(crate) fn poll(app: &mut PhotocraftApp, gpu: &GpuCanvas, doc: DocId) {
}
if r.collected.len() == r.blocks.len() && r.collected.iter().all(Option::is_some) {
let texels = std::mem::take(&mut r.collected).into_iter().flatten().collect();
- let k = Kept { doc: r.doc.clone(), display: r.display, format: r.format, blocks: r.blocks.clone(), texels, bytes: r.collected_bytes };
- app.kept.store(k, r.cap);
+ let k = Kept {
+ doc: Arc::downgrade(&r.doc),
+ id: r.doc.id,
+ display: r.display,
+ format: r.format,
+ blocks: r.blocks.clone(),
+ texels,
+ bytes: r.collected_bytes,
+ };
+ app.kept.store(k, &r.doc, r.cap);
}
let ms = crate::gpu_canvas::now_ms() - r.started_ms;
app.perf.record("background", r.drawn, ms, 0.0);
diff --git a/crates/ui-egui/src/background_redraw_tests.rs b/crates/ui-egui/src/background_redraw_tests.rs
index 1676491a6..72dcf94ad 100644
--- a/crates/ui-egui/src/background_redraw_tests.rs
+++ b/crates/ui-egui/src/background_redraw_tests.rs
@@ -35,7 +35,15 @@ fn visible_blocks_come_first_nearest_the_view_centre() {
fn kept(doc: &Arc<Document>, bytes: usize) -> Kept {
let all = blocks(doc.bounds());
let texels: Vec<Vec<u8>> = all.iter().map(|_| vec![1u8; bytes]).collect();
- Kept { doc: doc.clone(), display: 7, format: eframe::wgpu::TextureFormat::Rgba8Unorm, bytes: (bytes * all.len()) as u64, blocks: all, texels }
+ Kept {
+ doc: Arc::downgrade(doc),
+ id: doc.id,
+ display: 7,
+ format: eframe::wgpu::TextureFormat::Rgba8Unorm,
+ bytes: (bytes * all.len()) as u64,
+ blocks: all,
+ texels,
+ }
}
#[test]
@@ -55,7 +63,7 @@ fn kept_composites_come_back_only_for_the_exact_state_and_stay_bounded() {
Arc::new(d)
};
let mut cache = KeptComposites::default();
- cache.store(kept(&a, 4), u64::MAX);
+ cache.store(kept(&a, 4), &a, u64::MAX);
// The same state hits, also as an equal document that isn't the same Arc (a restore after a solo).
assert!(cache.find(&a, 7, Rgba8Unorm, &all).is_some());
assert!(cache.find(&Arc::new((*a).clone()), 7, Rgba8Unorm, &all).is_some());
@@ -75,14 +83,14 @@ fn kept_composites_come_back_only_for_the_exact_state_and_stay_bounded() {
assert!(cache.find(&a, 7, Rgba16Float, &all).is_none(), "another texture format");
assert!(cache.find(&a, 7, Rgba8Unorm, &all[1..]).is_none(), "other blocks");
// At most KEEP entries, the least recently used going first.
- cache.store(kept(&hidden, 4), u64::MAX);
+ cache.store(kept(&hidden, 4), &hidden, u64::MAX);
assert!(cache.find(&a, 7, Rgba8Unorm, &all).is_some());
- cache.store(kept(&faded, 4), u64::MAX);
+ cache.store(kept(&faded, 4), &faded, u64::MAX);
assert_eq!(cache.entries.len(), KEEP);
assert!(cache.find(&hidden, 7, Rgba8Unorm, &all).is_none(), "the least recently used went");
assert!(cache.find(&a, 7, Rgba8Unorm, &all).is_some() && cache.find(&faded, 7, Rgba8Unorm, &all).is_some());
// Storing a state again replaces its entry.
- cache.store(kept(&faded, 4), u64::MAX);
+ cache.store(kept(&faded, 4), &faded, u64::MAX);
assert_eq!(cache.entries.len(), 2);
// The byte cap: a smaller one drops the oldest, and a composite over it isn't kept.
let one = kept(&a, 4).bytes;
@@ -90,12 +98,20 @@ fn kept_composites_come_back_only_for_the_exact_state_and_stay_bounded() {
assert_eq!(cache.entries.len(), 1);
assert!(cache.bytes() <= one);
cache.trim(0);
- cache.store(kept(&a, 4), one - 1);
+ cache.store(kept(&a, 4), &a, one - 1);
assert!(cache.entries.is_empty(), "over the cap");
// A closed document's composites go.
- cache.store(kept(&a, 4), u64::MAX);
+ cache.store(kept(&a, 4), &a, u64::MAX);
cache.retain(|id| id != a.id);
assert!(cache.entries.is_empty());
+ // A state History no longer holds isn't kept alive: its entry is dead and goes.
+ let gone = changed(&|d| d.layers[0].opacity = 0.25);
+ cache.store(kept(&gone, 4), &gone, u64::MAX);
+ let equal = Arc::new((*gone).clone());
+ drop(gone);
+ assert!(cache.find(&equal, 7, Rgba8Unorm, &all).is_none(), "no strong reference to the drawn state");
+ cache.trim(u64::MAX);
+ assert!(cache.entries.is_empty());
}
struct Gpu {