Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

perf(app): layers that go up at open keep a compressed copy, so letting them go reads nothing back

Joshc785be777bca
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index d3eb86f..851a166 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -709,15 +709,22 @@ impl Shared {
let started = std::time::Instant::now();
/// A layer read, for the GPU or (past what fits) to keep compressed.
enum Read {
- Upload(Vec<u8>),
+ /// With its compressed copy, when layers are paged.
+ Upload(Vec<u8>, Option<procraft_gpu::ReadyToPark>),
Park(procraft_gpu::ReadyToPark),
}
let made = Canvas::new(&gpu, &doc).map(|mut c| {
- c.set_resident_budget(crate::layers::resident_budget());
+ let budget = crate::layers::resident_budget();
+ c.set_resident_budget(budget);
let (w, h) = (doc.width(), doc.height());
- // read on a few threads (each file decoded and, below the top layers that fit, its
- // compressed copy made), put on the canvas here in order
+ // read on a few threads (each file decoded and its compressed copy made: those below
+ // the top layers that fit are parked, those that fit keep it, so letting them go
+ // later needs no reading back), put on the canvas here in order
let first_resident = ids.len().saturating_sub(c.most_resident());
+ // the copies of layers that go up take an eighth of the textures' budget at most, and
+ // only those that pack to a quarter or less are kept
+ let copy_room = budget / 8;
+ let mut copies = 0u64;
let workers = std::thread::available_parallelism().map_or(2, |n| n.get()).saturating_sub(1).clamp(1, 6);
let next = std::sync::atomic::AtomicUsize::new(0);
let (send, receive) = std::sync::mpsc::sync_channel(workers);
@@ -735,7 +742,8 @@ impl Shared {
l.pixels(w, h).map(|px| {
let hash = project::pixels_hash(&px);
if i >= first_resident {
- (Read::Upload(px), hash)
+ let copy = (budget > 0).then(|| procraft_gpu::ReadyToPark::of(&px, w, h));
+ (Read::Upload(px, copy), hash)
} else {
(Read::Park(procraft_gpu::ReadyToPark::of(&px, w, h)), hash)
}
@@ -762,7 +770,14 @@ impl Shared {
saved.read(*id, hash);
}
let opened = match layer {
- Read::Upload(px) => c.upload_layer(&gpu, *id, &px),
+ Read::Upload(px, Some(copy))
+ if copy.bytes() * 4 <= px.len()
+ && copies + copy.bytes() as u64 <= copy_room =>
+ {
+ copies += copy.bytes() as u64;
+ c.upload_layer_kept(&gpu, *id, &px, copy)
+ }
+ Read::Upload(px, _) => c.upload_layer(&gpu, *id, &px),
Read::Park(ready) => {
c.park_ready(*id, ready);
Ok(())
@@ -780,10 +795,11 @@ impl Shared {
// as read: not changed
c.take_changed();
log::info!(
- "canvas made in {} ms: {} layers, {} kept compressed",
+ "canvas made in {} ms: {} layers, {} kept compressed, {} KB of copies of the rest",
started.elapsed().as_millis(),
ids.len(),
- c.parked_layers()
+ c.parked_layers(),
+ copies / 1024
);
c
});
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rs
index abceb6f..0494962 100644
--- a/crates/gpu/src/canvas.rs
+++ b/crates/gpu/src/canvas.rs
@@ -4259,6 +4259,22 @@ impl Canvas {
Ok(())
}
+ /// As [`Canvas::upload_layer`], keeping `ready` (made from the same pixels by
+ /// [`ReadyToPark::of`]) as the layer's compressed copy: letting its texture go later, while
+ /// it is unchanged, costs nothing.
+ pub fn upload_layer_kept(
+ &mut self,
+ gpu: &Gpu,
+ id: LayerId,
+ rgba: &[u8],
+ ready: ReadyToPark,
+ ) -> Result<(), GpuError> {
+ self.upload_layer(gpu, id, rgba)?;
+ self.parked.insert(id, ready.parked);
+ self.pictures.insert(id, ready.pictures);
+ Ok(())
+ }
+
/// Parks layer `id` with pixels already made ready (by [`ReadyToPark::of`], with this
/// canvas's size).
pub fn park_ready(&mut self, id: LayerId, ready: ReadyToPark) {
@@ -5852,6 +5868,11 @@ impl ReadyToPark {
let pictures = Pictures::of(rgba, width, height);
ReadyToPark { parked: Parked::of(rgba, width, pictures.bounds), pictures }
}
+
+ /// How many bytes the compressed pixels take.
+ pub fn bytes(&self) -> usize {
+ self.parked.lz4.len()
+ }
}
/// A parked layer's pixels: the rectangle painted (none if nothing), its rows compressed, so a
diff --git a/crates/gpu/tests/layers.rs b/crates/gpu/tests/layers.rs
index 1c064c2..c451caf 100644
--- a/crates/gpu/tests/layers.rs
+++ b/crates/gpu/tests/layers.rs
@@ -850,3 +850,42 @@ fn a_remake_part_composites_a_few_items_at_most() {
let stats = t.canvases[0].last_remake().unwrap();
assert!((1..=8).contains(&stats.largest_part), "{stats:?}");
}
+
+#[test]
+fn layers_uploaded_with_their_copies_are_let_go_without_reading_them_back() {
+ let Some((gpu, _, _)) = setup() else { return };
+ let mut doc = Document::new(W, W);
+ for _ in 0..11 {
+ doc.add_layer();
+ }
+ let ids: Vec<LayerId> = doc.layers().iter().map(|l| l.id).collect();
+ let pixels: Vec<Vec<u8>> = (0..ids.len())
+ .map(|i| {
+ let mut px = vec![0u8; (W * W * 4) as usize];
+ let (y, x0) = (i / 4 * 4 + 2, i % 4 * 4);
+ for x in x0..x0 + 3 {
+ let o = (y * W as usize + x) * 4;
+ px[o..o + 4].copy_from_slice(&[(i * 20) as u8, 200 - (i * 15) as u8, 90, 255]);
+ }
+ px
+ })
+ .collect();
+ let docs = [doc.clone(), doc];
+ let canvases = [Canvas::new(&gpu, &docs[0]).unwrap(), Canvas::new(&gpu, &docs[1]).unwrap()];
+ let mut t = Twins { docs, canvases };
+ // room for 3 of 12, every one uploaded with its compressed copy (as an opening artwork's)
+ t.canvases[0].set_resident_budget(3 * u64::from(W * W * 4));
+ for (id, px) in ids.iter().zip(&pixels) {
+ let ready = procraft_gpu::ReadyToPark::of(px, W, W);
+ t.canvases[0].upload_layer_kept(&gpu, *id, px, ready).unwrap();
+ t.canvases[1].upload_layer(&gpu, *id, px).unwrap();
+ }
+ // one render lets all but what fits go: nothing to read back first
+ let (doc, canvas) = (&mut t.docs[0], &mut t.canvases[0]);
+ canvas.render(&gpu, doc, None);
+ assert!(canvas.parked_layers() >= 9, "parked in one render: {}", canvas.parked_layers());
+ t.same(&gpu, 2, "opened");
+ for (id, px) in ids.iter().zip(&pixels) {
+ assert_eq!(&t.canvases[0].read_layer(&gpu, *id).unwrap(), px, "{id:?} as uploaded");
+ }
+}