Skip to content

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

Rust

perf(open): an opened artwork's layers stay compressed until each goes to the GPU

Josh7a59e1ed7360
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 4734d2b..9917e7b 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -320,7 +320,7 @@ pub(crate) struct State {
/// What the artwork keeps about itself: when it was made, strokes, time spent, its author.
pub(crate) info: project::ArtworkInfo,
/// Saved layer pixels waiting for the canvas (an opened artwork before it is shown).
- pub(crate) pending_pixels: Vec<(procraft_engine::document::LayerId, Vec<u8>)>,
+ pub(crate) pending_pixels: Vec<(procraft_engine::document::LayerId, project::Opened)>,
/// A model open to paint on, in place of the flat canvas (the document is its current
/// texture set's).
pub(crate) scene: Option<scene3d::Scene>,
@@ -555,7 +555,8 @@ impl State {
let Some(gpu) = self.gpu.as_ref() else { return };
match Canvas::new(gpu, &self.doc) {
Ok(mut c) => {
- for (id, pixels) in std::mem::take(&mut self.pending_pixels) {
+ let (w, h) = (self.doc.width(), self.doc.height());
+ for (id, pixels) in project::opened_pixels(std::mem::take(&mut self.pending_pixels), w, h) {
if let Err(e) = c.upload_layer(gpu, id, &pixels) {
log::error!("opening a layer: {e}");
}
@@ -652,7 +653,8 @@ impl Shared {
/// Creates the canvas once there is a GPU, as [`State::ensure_canvas`] does, but with the
/// opened layers sent to the GPU between holds of the state: an artwork of many layers takes
/// seconds, and the app's own thread would wait that long on any call. Each layer is copied
- /// out as it goes, so what was opened stays in the state (a save meanwhile still has it); if
+ /// out (and decoded) as it goes, so what was opened stays in the state (a save meanwhile still
+ /// has it); if
/// the document or what was opened changed meanwhile, the canvas is made again under the hold.
#[cfg_attr(not(any(target_os = "android", test)), allow(dead_code))]
pub(crate) fn make_canvas(&self) {
@@ -663,12 +665,17 @@ impl Shared {
};
let made = Canvas::new(&gpu, &doc).map(|mut c| {
for (i, id) in ids.iter().enumerate() {
- let Some(pixels) = self.lock().pending_pixels.get(i).filter(|(at, _)| at == id).map(|(_, p)| p.clone())
+ let Some(layer) = self.lock().pending_pixels.get(i).filter(|(at, _)| at == id).map(|(_, p)| p.clone())
else {
break;
};
- if let Err(e) = c.upload_layer(&gpu, *id, &pixels) {
- log::error!("opening a layer: {e}");
+ match layer.pixels(doc.width(), doc.height()) {
+ Ok(pixels) => {
+ if let Err(e) = c.upload_layer(&gpu, *id, &pixels) {
+ log::error!("opening a layer: {e}");
+ }
+ }
+ Err(e) => log::error!("layer {id:?}: {e}"),
}
}
c
@@ -811,7 +818,7 @@ impl CanvasSession {
assist_from: None,
guide_before: None,
name,
- pending_pixels: pixels,
+ pending_pixels: project::opened(pixels),
fit_insets: [0.0; 4],
backdrops: Vec::new(),
saved_view: None,
diff --git a/apps/procraft-android/src/project.rs b/apps/procraft-android/src/project.rs
index 336dc2d..45aad14 100644
--- a/apps/procraft-android/src/project.rs
+++ b/apps/procraft-android/src/project.rs
@@ -187,7 +187,7 @@ impl crate::State {
);
} else {
// not shown yet: what was opened is still what there is
- layers = self.pending_pixels.clone();
+ layers = opened_pixels(self.pending_pixels.clone(), self.doc.width(), self.doc.height());
}
Ok(SaveSnapshot { file, layers, composite, model })
}
@@ -307,22 +307,24 @@ impl CanvasSession {
});
}
let doc = file.document.repaired();
- let mut pixels = Vec::new();
+ // each layer's file as it is, decoded as it goes to the GPU
+ let mut pngs = Vec::new();
for layer in doc.layers() {
let path = layer_path(&dir, layer.id);
// a layer with no file is empty
if !path.exists() {
continue;
}
- match read_png(&path, doc.width(), doc.height()) {
- Ok(rgba) => pixels.push((layer.id, rgba)),
+ match fs::read(&path) {
+ Ok(bytes) => pngs.push((layer.id, Opened::Png(bytes))),
Err(e) => log::error!("layer {:?}: {e}", layer.id),
}
}
let scene = file.model.as_ref().map(|m| crate::scene3d::open_saved_model(&dir, m)).transpose()?;
- let session = CanvasSession::with_document(doc, file.name, pixels);
+ let session = CanvasSession::with_document(doc, file.name, Vec::new());
{
let mut st = session.shared.lock();
+ st.pending_pixels = pngs;
st.info = file.info;
st.saved_view = file.view;
st.scene = scene;
@@ -786,10 +788,52 @@ fn png_bytes(width: u32, height: u32, rgba: &[u8], icc: &[u8]) -> Result<Vec<u8>
Ok(out)
}
+/// A layer's pixels waiting for the GPU: as they are, or (an opened artwork's) still the PNG they
+/// were read from, decoded only as each goes up, so a large artwork isn't all in memory at once.
+#[derive(Clone, Debug, PartialEq)]
+pub(crate) enum Opened {
+ Pixels(Vec<u8>),
+ Png(Vec<u8>),
+}
+
+impl Opened {
+ /// The pixels, which must be `width` × `height`.
+ pub(crate) fn pixels(self, width: u32, height: u32) -> Result<Vec<u8>, MobileError> {
+ match self {
+ Opened::Pixels(rgba) => Ok(rgba),
+ Opened::Png(bytes) => decode_png(std::io::Cursor::new(bytes), width, height),
+ }
+ }
+}
+
+/// Layers' pixels as they are, waiting for the GPU.
+pub(crate) fn opened(layers: Vec<(LayerId, Vec<u8>)>) -> Vec<(LayerId, Opened)> {
+ layers.into_iter().map(|(id, rgba)| (id, Opened::Pixels(rgba))).collect()
+}
+
+/// Waiting layers' pixels, `width` × `height`; one that can't be read is left out.
+pub(crate) fn opened_pixels(layers: Vec<(LayerId, Opened)>, width: u32, height: u32) -> Vec<(LayerId, Vec<u8>)> {
+ layers
+ .into_iter()
+ .filter_map(|(id, layer)| match layer.pixels(width, height) {
+ Ok(rgba) => Some((id, rgba)),
+ Err(e) => {
+ log::error!("layer {id:?}: {e}");
+ None
+ }
+ })
+ .collect()
+}
+
/// A PNG's RGBA8 pixels, which must be `width` × `height`.
pub(crate) fn read_png(path: &Path, width: u32, height: u32) -> Result<Vec<u8>, MobileError> {
let file = fs::File::open(path).map_err(|e| file_error("opening a layer", e))?;
- let decoder = png::Decoder::new(BufReader::new(file));
+ decode_png(BufReader::new(file), width, height)
+}
+
+/// The RGBA8 pixels of the PNG `from` reads, which must be `width` × `height`.
+fn decode_png(from: impl std::io::BufRead + std::io::Seek, width: u32, height: u32) -> Result<Vec<u8>, MobileError> {
+ let decoder = png::Decoder::new(from);
let mut reader = decoder.read_info().map_err(|e| file_error("opening a layer", e))?;
let size = reader.output_buffer_size().ok_or_else(|| file_error("opening a layer", "too large"))?;
let mut buf = vec![0; size];
diff --git a/apps/procraft-android/src/scene3d.rs b/apps/procraft-android/src/scene3d.rs
index 77326f6..27495d8 100644
--- a/apps/procraft-android/src/scene3d.rs
+++ b/apps/procraft-android/src/scene3d.rs
@@ -521,7 +521,7 @@ impl CanvasSession {
if set == scene.current && channel == scene.channel {
match st.canvas.as_mut() {
Some(canvas) => canvas.put_layer(base, rgba),
- None => st.pending_pixels.push((base, rgba)),
+ None => st.pending_pixels.push((base, crate::project::Opened::Pixels(rgba))),
}
} else {
let Some(slot) = scene.slot(set, channel) else { return false };
@@ -1525,7 +1525,7 @@ impl State {
let old = TextureSet {
doc: std::mem::replace(&mut self.doc, Document::new(1, 1)),
canvas: self.canvas.take(),
- pending: std::mem::take(&mut self.pending_pixels),
+ pending: crate::project::opened_pixels(std::mem::take(&mut self.pending_pixels), side, side),
};
let structure = old.doc.clone();
if let Some(slot) = scene.slot(part, from) {
@@ -1535,7 +1535,7 @@ impl State {
follow_layers(&mut new, &structure);
self.doc = new.doc;
self.canvas = new.canvas;
- self.pending_pixels = new.pending;
+ self.pending_pixels = crate::project::opened(new.pending);
scene.channel = channel;
self.ensure_canvas();
}
@@ -1566,10 +1566,12 @@ impl State {
self.settle();
let Some(scene) = self.scene.as_mut() else { return };
let Some(set) = scene.sets.get_mut(index).and_then(Option::take) else { return };
+ let (w, h) = (self.doc.width(), self.doc.height());
+ let pending = std::mem::replace(&mut self.pending_pixels, crate::project::opened(set.pending));
let old = TextureSet {
doc: std::mem::replace(&mut self.doc, set.doc),
canvas: std::mem::replace(&mut self.canvas, set.canvas),
- pending: std::mem::replace(&mut self.pending_pixels, set.pending),
+ pending: crate::project::opened_pixels(pending, w, h),
};
if let Some(slot) = scene.sets.get_mut(scene.current) {
*slot = Some(old);
diff --git a/apps/procraft-android/src/tests.rs b/apps/procraft-android/src/tests.rs
index 5f61135..794dec4 100644
--- a/apps/procraft-android/src/tests.rs
+++ b/apps/procraft-android/src/tests.rs
@@ -334,6 +334,19 @@ fn artworks_save_and_open() {
assert!(dir.join("artwork.json").exists() && dir.join("thumbnail.png").exists());
let opened = CanvasSession::open(dir.to_string_lossy().into_owned()).unwrap();
+ // saved before it is shown, its layers (still their files) are written back as they were
+ let again = dir.with_extension("again");
+ opened.save(again.to_string_lossy().into_owned()).unwrap();
+ let mut compared = 0;
+ for layer in opened.layers() {
+ let file = |d: &std::path::Path| std::fs::read(d.join("layers").join(format!("{}.png", layer.id))).ok();
+ let (before, after) = (file(&dir), file(&again));
+ compared += usize::from(before.is_some());
+ let rgba = |png: Option<Vec<u8>>| png.map(|b| crate::project::Opened::Png(b).pixels(100, 100).unwrap());
+ assert_eq!(rgba(after), rgba(before));
+ }
+ assert_eq!(compared, 2);
+ let _ = std::fs::remove_dir_all(&again);
{
let gpu = Gpu::headless().unwrap();
let mut st = opened.shared.lock();
@@ -1437,8 +1450,8 @@ fn an_artwork_from_a_picture() {
assert_eq!((s.info().width, s.info().height), (6, 4));
assert_eq!(s.name(), "Photo");
let st = s.shared.lock();
- let (_, pixels) = st.pending_pixels.first().unwrap();
- assert_eq!(&pixels[..4], &[128, 0, 0, 128]);
+ let (_, layer) = st.pending_pixels.first().unwrap();
+ assert_eq!(&layer.clone().pixels(6, 4).unwrap()[..4], &[128, 0, 0, 128]);
}
#[test]
@@ -1752,7 +1765,12 @@ fn saves_at_the_same_time_keep_the_layers_whole() {
});
}
let back = CanvasSession::open(path).unwrap();
- let pending = back.shared.lock().pending_pixels.clone();
+ let (pending, w, h) = {
+ let st = back.shared.lock();
+ (st.pending_pixels.clone(), st.doc.width(), st.doc.height())
+ };
+ let pending = crate::project::opened_pixels(pending, w, h);
+ assert!(!pending.is_empty());
let painted = pending.iter().map(|(_, px)| px.chunks(4).filter(|p| p[3] > 0).count()).sum::<usize>();
assert!(painted > 5000, "{painted}");
// nothing left over from the writes