Skip to content

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

Rust

fix(gpu): remake caches in time-bounded parts; saves and copies wait without holding the canvas

Joshc5f9aba150f0
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
index 98563c2..817d3e0 100644
--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt
@@ -2089,11 +2089,15 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
}
fun copyCanvas() {
- val copied = session?.copyCanvas()
- clipboard = copied ?: clipboard
- canPaste = clipboard != null
+ val s = session ?: return
closePanels()
- if (copied != null) showNotice("Canvas copied")
+ // off the main thread: the picture waits for the canvas to finish any remaking first
+ viewModelScope.launch {
+ val copied = withContext(Dispatchers.IO) { runCatching { s.copyCanvas() }.getOrNull() }
+ clipboard = copied ?: clipboard
+ canPaste = clipboard != null
+ if (copied != null) showNotice("Canvas copied")
+ }
}
fun cutLayer() {
diff --git a/apps/procraft-android/src/clipboard.rs b/apps/procraft-android/src/clipboard.rs
index 7a1a484..e9c2f9e 100644
--- a/apps/procraft-android/src/clipboard.rs
+++ b/apps/procraft-android/src/clipboard.rs
@@ -89,8 +89,7 @@ impl CanvasSession {
/// The whole canvas as it shows, to paste later.
pub fn copy_canvas(&self) -> Option<ThumbnailImage> {
- self.shared.finish_caches();
- let mut guard = self.shared.lock();
+ let mut guard = self.shared.lock_finished();
let st = &mut *guard;
st.finish_stroke();
st.settle();
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 43794d8..a05068f 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -655,17 +655,35 @@ impl Shared {
/// Lets the canvas finish remaking its caches a render at a time, the lock let go between,
/// so a save or export after reads the picture as it is without holding everything up.
- pub(crate) fn finish_caches(&self) {
+ fn finish_caches(&self) {
// each render remakes at least one item, so this ends well before the bound
for _ in 0..100_000 {
- let mut guard = self.lock();
- let st = &mut *guard;
- let (Some(canvas), Some(gpu)) = (st.canvas.as_mut(), st.gpu.as_ref()) else { return };
- canvas.render(gpu, &mut st.doc, None);
- if !canvas.busy() {
- return;
+ {
+ let mut guard = self.lock();
+ let st = &mut *guard;
+ let (Some(canvas), Some(gpu)) = (st.canvas.as_mut(), st.gpu.as_ref()) else { return };
+ canvas.render(gpu, &mut st.doc, None);
+ if !canvas.busy() {
+ return;
+ }
+ }
+ // a moment for whoever waits on the lock (a touch, the render thread)
+ std::thread::sleep(std::time::Duration::from_millis(2));
+ }
+ }
+
+ /// The lock, taken once the canvas has finished remaking its caches (made a render at a
+ /// time, the lock let go between): what is read under it is the picture as it is, and
+ /// nothing waits on a whole remake.
+ pub(crate) fn lock_finished(&self) -> MutexGuard<'_, State> {
+ for _ in 0..1000 {
+ self.finish_caches();
+ let guard = self.lock();
+ if guard.canvas.as_ref().is_none_or(|c| !c.busy()) {
+ return guard;
}
}
+ self.lock()
}
fn frame(&self, st: &mut State) {
diff --git a/apps/procraft-android/src/project.rs b/apps/procraft-android/src/project.rs
index 827be8d..14953b6 100644
--- a/apps/procraft-android/src/project.rs
+++ b/apps/procraft-android/src/project.rs
@@ -411,9 +411,8 @@ impl CanvasSession {
/// Saves started together run one after the other.
pub fn save(&self, dir: String) -> Result<(), MobileError> {
let mut saved = self.shared.saving.lock().unwrap_or_else(|e| e.into_inner());
- self.shared.finish_caches();
let snapshot = {
- let mut guard = self.shared.lock();
+ let mut guard = self.shared.lock_finished();
let st = &mut *guard;
st.finish_stroke();
st.settle();
@@ -428,9 +427,8 @@ impl CanvasSession {
/// would end (a stroke, a transform, an adjustment, text being edited...); false if it didn't.
pub fn save_if_idle(&self, dir: String) -> Result<bool, MobileError> {
let mut saved = self.shared.saving.lock().unwrap_or_else(|e| e.into_inner());
- self.shared.finish_caches();
let snapshot = {
- let mut guard = self.shared.lock();
+ let mut guard = self.shared.lock_finished();
let st = &mut *guard;
if !st.idle() {
return Ok(false);
@@ -468,9 +466,8 @@ impl CanvasSession {
/// Writes the flattened picture, full size and with straight alpha, to `path` as a PNG.
pub fn export_png(&self, path: String) -> Result<(), MobileError> {
- self.shared.finish_caches();
let (w, h, rgba, icc) = {
- let mut guard = self.shared.lock();
+ let mut guard = self.shared.lock_finished();
let st = &mut *guard;
st.finish_stroke();
st.settle();
@@ -534,9 +531,8 @@ impl CanvasSession {
}
None => None,
};
- self.shared.finish_caches();
let (doc, pixels, composite, name, info) = {
- let mut guard = self.shared.lock();
+ let mut guard = self.shared.lock_finished();
let st = &mut *guard;
st.finish_stroke();
st.settle();
@@ -725,8 +721,7 @@ impl CanvasSession {
}
fn pictures(&self, with_layers: bool) -> Result<Pictures, MobileError> {
- self.shared.finish_caches();
- let mut guard = self.shared.lock();
+ let mut guard = self.shared.lock_finished();
let st = &mut *guard;
st.finish_stroke();
st.settle();
diff --git a/apps/procraft-android/src/render.rs b/apps/procraft-android/src/render.rs
index 8745e40..2c59bf1 100644
--- a/apps/procraft-android/src/render.rs
+++ b/apps/procraft-android/src/render.rs
@@ -98,6 +98,7 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {
{
let mut guard = shared.lock();
let st = &mut *guard;
+ let held = Instant::now();
// the window changed size while the image was awaited (the device turned): the view is
// already for the new size, so this image is let go and the surface configured again
if st.window_size != size {
@@ -180,6 +181,10 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {
st.frame_interval_ms = now.duration_since(prev).as_secs_f32() * 1000.0;
}
last_present = Some(now);
+ // everything else waits on the lock meanwhile
+ if held.elapsed() > SLOW_FRAME {
+ log::warn!("a frame held the canvas {} ms", held.elapsed().as_millis());
+ }
}
queue.present(frame);
}
@@ -187,6 +192,9 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {
drop(window);
}
+/// A frame that holds the canvas longer than this is logged: touches wait on it.
+const SLOW_FRAME: Duration = Duration::from_millis(200);
+
/// How often the second display looks for a new frame of the main window.
const PROJECTION_POLL: Duration = Duration::from_millis(8);
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rs
index a21e171..d6ce531 100644
--- a/crates/gpu/src/canvas.rs
+++ b/crates/gpu/src/canvas.rs
@@ -4084,15 +4084,18 @@ impl Canvas {
let (textures, views) =
if below { (&r.caches.below, &r.caches.below_views) } else { (&r.caches.above, &r.caches.above_views) };
let stack = Stack { textures, views };
- // as many whole items as fit (at least one), each with what it is clipped to; an item
- // painted nowhere is passed over, a normal one composites only where it is painted
+ // as many whole items as fit and the slice's time allows (at least one), each with what
+ // it is clipped to; an item painted nowhere is passed over, a normal one composites only
+ // where it is painted
let room = self.most_resident().saturating_sub(self.layers.len()).max(1);
let mut part = Vec::new();
let mut layers = Vec::new();
+ let mut staged = 0;
while let Some(&item) = items.get(at) {
let mut more = item_layers(doc, item);
more.extend(doc.clip_base(item));
- if !part.is_empty() && layers.len() + more.len() > room {
+ let late = std::time::Instant::now() >= deadline;
+ if !part.is_empty() && (late || layers.len() + more.len() > room) {
break;
}
at += 1;
@@ -4104,10 +4107,17 @@ impl Canvas {
// has to be whole
let normal = doc.layer(item).is_some_and(|l| l.blend == BlendMode::Normal);
let rect = if normal { painted } else { full };
+ // brought back now (the slow part, so the time taken counts), the staged pixels sent
+ // on now and then so they don't pile up
+ staged += more.len();
+ self.restore_all(gpu, more.iter().copied(), false);
+ if staged >= 8 {
+ gpu.queue.submit(std::iter::empty());
+ staged = 0;
+ }
layers.extend(more);
part.push((item, rect));
}
- self.restore_all(gpu, layers, false);
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("cache part") });
let mut uniforms = UniformData::default();
@@ -4905,8 +4915,10 @@ impl Canvas {
self.sample_layer(gpu, id, full, max_side)
}
+ /// The picture as it shows (while the caches are remade, as it was), scaled to fit
+ /// `max_side` pixels: quick enough to ask for while it is drawn on.
pub fn composite_thumbnail(&mut self, gpu: &Gpu, doc: &mut Document, max_side: u32) -> Result<Thumbnail, GpuError> {
- self.render_finished(gpu, doc);
+ self.render(gpu, doc, None);
self.sample_view(gpu, &self.composite_views[0], self.full(), max_side)
}