Skip to content

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

Rust

perf(app): read an opening artwork's layers on several threads

Josh654436e00d7c
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 3996ce9..3a280bd 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -705,28 +705,76 @@ impl Shared {
(gpu.clone(), st.doc.clone(), st.pending_pixels.iter().map(|(id, _)| *id).collect::<Vec<_>>())
};
let started = std::time::Instant::now();
+ /// A layer read, for the GPU or (past what fits) to keep compressed.
+ enum Read {
+ Upload(Vec<u8>),
+ Park(procraft_gpu::ReadyToPark),
+ }
let made = Canvas::new(&gpu, &doc).map(|mut c| {
c.set_resident_budget(crate::layers::resident_budget());
- for (i, id) in ids.iter().enumerate() {
- let Some(layer) = self.lock().pending_pixels.get(i).filter(|(at, _)| at == id).map(|(_, p)| p.clone())
- else {
- break;
- };
- match layer.pixels(doc.width(), doc.height()) {
- Ok(pixels) => {
- if let Ok(mut saved) = self.saving.lock() {
- saved.read(*id, &pixels);
+ 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
+ let first_resident = ids.len().saturating_sub(c.most_resident());
+ 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);
+ std::thread::scope(|scope| {
+ for _ in 0..workers {
+ let (send, next, ids) = (send.clone(), &next, &ids);
+ scope.spawn(move || {
+ loop {
+ let i = next.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ let Some(id) = ids.get(i) else { break };
+ // none if the artwork changed meanwhile: what is made is let go
+ let layer =
+ self.lock().pending_pixels.get(i).filter(|(at, _)| at == id).map(|(_, p)| p.clone());
+ let read = layer.map(|l| {
+ l.pixels(w, h).map(|px| {
+ let hash = project::pixels_hash(&px);
+ if i >= first_resident {
+ (Read::Upload(px), hash)
+ } else {
+ (Read::Park(procraft_gpu::ReadyToPark::of(&px, w, h)), hash)
+ }
+ })
+ });
+ if send.send((i, read)).is_err() {
+ break;
+ }
}
- // past what fits, kept compressed until needed
- let opened =
- if c.has_room() { c.upload_layer(&gpu, *id, &pixels) } else { c.park_pixels(*id, &pixels) };
- if let Err(e) = opened {
- log::error!("opening a layer: {e}");
+ });
+ }
+ drop(send);
+ let mut waiting = std::collections::BTreeMap::new();
+ let mut at = 0;
+ for (i, read) in receive {
+ waiting.insert(i, read);
+ while let Some(read) = waiting.remove(&at) {
+ let Some(id) = ids.get(at) else { break };
+ at += 1;
+ match read {
+ None => {}
+ Some(Ok((layer, hash))) => {
+ if let Ok(mut saved) = self.saving.lock() {
+ saved.read(*id, hash);
+ }
+ let opened = match layer {
+ Read::Upload(px) => c.upload_layer(&gpu, *id, &px),
+ Read::Park(ready) => {
+ c.park_ready(*id, ready);
+ Ok(())
+ }
+ };
+ if let Err(e) = opened {
+ log::error!("opening a layer: {e}");
+ }
+ }
+ Some(Err(e)) => log::error!("layer {id:?}: {e}"),
}
}
- Err(e) => log::error!("layer {id:?}: {e}"),
}
- }
+ });
// as read: not changed
c.take_changed();
log::info!(
diff --git a/apps/procraft-android/src/project.rs b/apps/procraft-android/src/project.rs
index 14953b6..ea79be8 100644
--- a/apps/procraft-android/src/project.rs
+++ b/apps/procraft-android/src/project.rs
@@ -139,9 +139,10 @@ impl Saved {
self.layers.clear();
}
- /// Layer `id` read from its file with these pixels: a save that finds it unchanged leaves it.
- pub(crate) fn read(&mut self, id: LayerId, rgba: &[u8]) {
- self.layers.insert(id, pixels_hash(rgba));
+ /// Layer `id` read from its file, its pixels' [`pixels_hash`]: a save that finds it unchanged
+ /// leaves it.
+ pub(crate) fn read(&mut self, id: LayerId, hash: u64) {
+ self.layers.insert(id, hash);
}
}
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rs
index 271e5b8..6e8ff4b 100644
--- a/crates/gpu/src/canvas.rs
+++ b/crates/gpu/src/canvas.rs
@@ -4037,7 +4037,7 @@ impl Canvas {
}
/// How many layers' textures fit the budget (any number without one).
- fn most_resident(&self) -> usize {
+ pub fn most_resident(&self) -> usize {
let per = u64::from(self.width) * u64::from(self.height) * 4;
if self.resident_budget == 0 {
usize::MAX
@@ -4193,13 +4193,18 @@ impl Canvas {
if rgba.len() != expected {
return Err(GpuError::Readback(format!("layer {id:?}: {} bytes, expected {expected}", rgba.len())));
}
+ self.park_ready(id, ReadyToPark::of(rgba, self.width, self.height));
+ 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) {
self.layers.remove(&id);
- let pictures = Pictures::of(rgba, self.width, self.height);
- self.parked.insert(id, Parked::of(rgba, self.width, pictures.bounds));
- self.pictures.insert(id, pictures);
+ self.parked.insert(id, ready.parked);
+ self.pictures.insert(id, ready.pictures);
self.caches = None;
self.invalidate();
- Ok(())
}
/// Whether work keeps the caches: painting, or undoing or redoing it, on a layer worked on.
@@ -5743,6 +5748,21 @@ fn painted_bounds(t: &Thumbnail, painted: impl Fn([u8; 4]) -> bool) -> Option<[u
/// The longest side of a parked layer's small pictures, pixels: as big as any thumbnail asked for.
const PARKED_PICTURE_SIDE: u32 = 192;
+/// A layer's pixels made ready to park: compressed, with its small pictures. Made on any thread
+/// (the slow part of [`Canvas::park_pixels`]), then given to [`Canvas::park_ready`].
+pub struct ReadyToPark {
+ parked: Parked,
+ pictures: Pictures,
+}
+
+impl ReadyToPark {
+ /// Of a layer's pixels (premultiplied RGBA, `width` x `height`).
+ pub fn of(rgba: &[u8], width: u32, height: u32) -> ReadyToPark {
+ let pictures = Pictures::of(rgba, width, height);
+ ReadyToPark { parked: Parked::of(rgba, width, pictures.bounds), pictures }
+ }
+}
+
/// A parked layer's pixels: the rectangle painted (none if nothing), its rows compressed, so a
/// layer drawn on in one place costs little to keep and to bring back.
struct Parked {
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 77465ce..c17d0ef 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -13,8 +13,8 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
pub use canvas::{
- Backdrop, Canvas, DisplayLut, Filter, LiquifyMode, LiquifyStamp, Resample, SelectionLook, SmallScene, Stripes,
- StrokeStyle, StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,
+ Backdrop, Canvas, DisplayLut, Filter, LiquifyMode, LiquifyStamp, ReadyToPark, Resample, SelectionLook, SmallScene,
+ Stripes, StrokeStyle, StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,
};
pub use lighting::Lighting;
pub use scene::{Light, MOST_LIGHTS, MeshView, Orbit, SurfaceMap};