Skip to content

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

Rust

fix(open): an artwork of many layers opens without holding up the app or doubling its memory

Josh275054f4b798
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 48fe331..4734d2b 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -648,6 +648,48 @@ impl Shared {
st.needs_frame = true;
self.wake.notify_all();
}
+
+ /// 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
+ /// 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) {
+ let (gpu, doc, ids) = {
+ let st = self.lock();
+ let Some(gpu) = st.gpu.as_ref().filter(|_| st.canvas.is_none()) else { return };
+ (gpu.clone(), st.doc.clone(), st.pending_pixels.iter().map(|(id, _)| *id).collect::<Vec<_>>())
+ };
+ 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())
+ else {
+ break;
+ };
+ if let Err(e) = c.upload_layer(&gpu, *id, &pixels) {
+ log::error!("opening a layer: {e}");
+ }
+ }
+ c
+ });
+ let mut st = self.lock();
+ if st.canvas.is_some() {
+ return;
+ }
+ let same = st.doc == doc && st.pending_pixels.iter().map(|(id, _)| *id).eq(ids.iter().copied());
+ match made {
+ Ok(c) if same => {
+ st.pending_pixels.clear();
+ st.canvas = Some(c);
+ }
+ Ok(_) => st.ensure_canvas(),
+ Err(e) => {
+ log::error!("canvas: {e}");
+ st.error = Some(e.to_string());
+ }
+ }
+ }
}
/// A canvas being painted: the document, the brush, the input and the renderer.
diff --git a/apps/procraft-android/src/render.rs b/apps/procraft-android/src/render.rs
index e17b535..ae3b8bd 100644
--- a/apps/procraft-android/src/render.rs
+++ b/apps/procraft-android/src/render.rs
@@ -42,9 +42,9 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {
// it holds the session's layers from now on
shared.lock().gpu.get_or_insert(gpu);
}
+ shared.make_canvas();
let queue = {
let mut st = shared.lock();
- st.ensure_canvas();
st.reconfigure = true;
st.needs_frame = true;
st.gpu.as_ref().map(|g| g.queue.clone())
diff --git a/apps/procraft-android/src/tests.rs b/apps/procraft-android/src/tests.rs
index ca746f8..5f61135 100644
--- a/apps/procraft-android/src/tests.rs
+++ b/apps/procraft-android/src/tests.rs
@@ -340,8 +340,10 @@ fn artworks_save_and_open() {
st.window_size = (100, 100);
st.view = Some(View::fit((100, 100), (100, 100)));
st.gpu = Some(gpu);
- st.ensure_canvas();
}
+ // as the render thread makes it: the layers go up with the state let go between them
+ opened.shared.make_canvas();
+ assert!(opened.shared.lock().pending_pixels.is_empty());
assert_eq!(opened.name(), "Test Piece");
assert_eq!(opened.artwork_info().author, "A. Painter");
assert_eq!(opened.artwork_info().created_ms, info.created_ms);
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rs
index 3fbc963..25639af 100644
--- a/crates/gpu/src/canvas.rs
+++ b/crates/gpu/src/canvas.rs
@@ -4247,6 +4247,9 @@ impl Canvas {
},
extent(self.width, self.height),
);
+ // sent now, so many layers in a row (an artwork opening, a moving picture pasted) don't
+ // each keep a staged copy until the next frame
+ gpu.flush();
self.invalidate();
Ok(())
}
diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs
index 1fbed8f..77465ce 100644
--- a/crates/gpu/src/lib.rs
+++ b/crates/gpu/src/lib.rs
@@ -55,6 +55,15 @@ impl Gpu {
wgpu::Instance::new(desc)
}
+ /// Sends the writes queued so far to the device and waits for them, so the copies they were
+ /// staged in are let go.
+ pub fn flush(&self) {
+ self.queue.submit(std::iter::empty());
+ if let Err(e) = self.device.poll(wgpu::PollType::wait_indefinitely()) {
+ log::error!("flushing uploads: {e}");
+ }
+ }
+
/// A device for offscreen work (tests, exports).
pub fn headless() -> Result<Gpu, GpuError> {
Gpu::create(Gpu::instance(), None)