Skip to content

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

Rust

fix(app): hand the canvas lock on fairly and stop reloading every thumbnail on select

Josh71e9ca3af6f4
diff --git a/Cargo.lock b/Cargo.lock
index b53304e..23ea97a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -909,6 +909,7 @@ dependencies = [
"jni-sys 0.3.1",
"log",
"ndk",
+ "parking_lot",
"png",
"procraft-engine",
"procraft-gpu",
diff --git a/Cargo.toml b/Cargo.toml
index 88ef275..71d4385 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -37,6 +37,7 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
png = "0.18"
miniz_oxide = "0.8"
+parking_lot = "0.12"
[profile.dev]
opt-level = 1
diff --git a/apps/procraft-android/Cargo.toml b/apps/procraft-android/Cargo.toml
index bd75a4f..96e2a83 100644
--- a/apps/procraft-android/Cargo.toml
+++ b/apps/procraft-android/Cargo.toml
@@ -22,6 +22,7 @@ thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
png = { workspace = true }
+parking_lot = { workspace = true }
# The exact version: the Kotlin bindings are generated by the same release (bindgen/, sharing this
# lock file) and refuse to load against another one.
uniffi = { version = "=0.32.2", default-features = false }
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 817d3e0..b6dd36a 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
@@ -5575,7 +5575,8 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
fun addLayer() = layerChange { if (it.addLayer() == 0uL) message = noRoom() }
- fun selectLayer(id: ULong) = layerChange { it.selectLayer(id) }
+ // no layer's pixels change, so the thumbnails stay (an artwork's hundreds would load again)
+ fun selectLayer(id: ULong) = layerChange(thumbs = false) { it.selectLayer(id) }
fun toggleVisible(l: LayerInfo) = layerChange { it.setLayerVisible(l.id, !l.visible) }
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 3a280bd..e133b57 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -38,7 +38,7 @@ mod transform;
mod window;
use std::collections::HashMap;
-use std::sync::{Arc, Condvar, Mutex, MutexGuard};
+use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use procraft_engine::brush::Brush;
@@ -639,8 +639,9 @@ fn close_stroke(canvas: &mut Canvas, mut builder: StrokeBuilder, guide: &procraf
}
pub(crate) struct Shared {
- state: Mutex<State>,
- pub(crate) wake: Condvar,
+ /// Fair when it matters: the render thread hands it on to whoever waits.
+ state: parking_lot::Mutex<State>,
+ pub(crate) wake: parking_lot::Condvar,
thread: Mutex<Option<JoinHandle<()>>>,
/// Project canvas's render thread.
projection: Mutex<Option<JoinHandle<()>>>,
@@ -649,8 +650,8 @@ pub(crate) struct Shared {
}
impl Shared {
- pub(crate) fn lock(&self) -> MutexGuard<'_, State> {
- self.state.lock().unwrap_or_else(|e| e.into_inner())
+ pub(crate) fn lock(&self) -> parking_lot::MutexGuard<'_, State> {
+ self.state.lock()
}
/// Lets the canvas finish remaking its caches a render at a time, the lock let go between,
@@ -666,8 +667,9 @@ impl Shared {
if !canvas.busy() {
return;
}
+ // whoever waits on the lock (a touch, the render thread) has it next
+ parking_lot::MutexGuard::unlock_fair(guard);
}
- // a moment for whoever waits on the lock (a touch, the render thread)
std::thread::sleep(std::time::Duration::from_millis(2));
}
}
@@ -675,7 +677,7 @@ impl Shared {
/// 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> {
+ pub(crate) fn lock_finished(&self) -> parking_lot::MutexGuard<'_, State> {
for _ in 0..1000 {
self.finish_caches();
let guard = self.lock();
@@ -931,8 +933,8 @@ impl CanvasSession {
};
Arc::new(CanvasSession {
shared: Arc::new(Shared {
- state: Mutex::new(state),
- wake: Condvar::new(),
+ state: parking_lot::Mutex::new(state),
+ wake: parking_lot::Condvar::new(),
thread: Mutex::new(None),
projection: Mutex::new(None),
saving: Mutex::new(project::Saved::default()),
diff --git a/apps/procraft-android/src/render.rs b/apps/procraft-android/src/render.rs
index 2c59bf1..e0c3105 100644
--- a/apps/procraft-android/src/render.rs
+++ b/apps/procraft-android/src/render.rs
@@ -58,7 +58,7 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {
let (size, reconfigure) = {
let mut st = shared.lock();
while !st.stop && !st.needs_frame {
- st = shared.wake.wait(st).unwrap_or_else(|e| e.into_inner());
+ shared.wake.wait(&mut st);
}
if st.stop {
break;
@@ -185,6 +185,9 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {
if held.elapsed() > SLOW_FRAME {
log::warn!("a frame held the canvas {} ms", held.elapsed().as_millis());
}
+ // whoever waited on the lock meanwhile (a touch) has it next, before this thread's
+ // next frame
+ parking_lot::MutexGuard::unlock_fair(guard);
}
queue.present(frame);
}
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rs
index 6e8ff4b..cf0aa77 100644
--- a/crates/gpu/src/canvas.rs
+++ b/crates/gpu/src/canvas.rs
@@ -4937,7 +4937,11 @@ impl Canvas {
/// 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(gpu, doc, None);
+ // while the caches are remade a render leaves the picture as it is: none, so this never
+ // does that work (on the main thread, as the artwork closes)
+ if !self.busy() {
+ self.render(gpu, doc, None);
+ }
self.sample_view(gpu, &self.composite_views[0], self.full(), max_side)
}
@@ -5901,7 +5905,7 @@ struct Caches {
}
/// How long each render goes on remaking caches before it lets the app go on.
-const REMAKE_SLICE: std::time::Duration = std::time::Duration::from_millis(40);
+const REMAKE_SLICE: std::time::Duration = std::time::Duration::from_millis(16);
/// Caches being remade a little each render: their textures, how far each has got (the next item
/// and where its stack's result is; none when done or not remade), what they are made from,