ProCraft for Android: An Android native painting app written in Rust
Rust
perf: faster artwork opening; Actions panel scrolls
Joshfeef89c42704
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.ktindex 627d8dc..b120748 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@@ -753,6 +753,8 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {init {refreshGallery()+ // the GPU made while the gallery shows, so the first artwork opens at once+ viewModelScope.launch(Dispatchers.Default) { io.github.artcraftandroid.procraft.core.warmGpu() }}// ---- gallery ----@@ -858,12 +860,21 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {/** A new artwork made as preset [p] says, opened on the canvas. */fun newPresetArtwork(p: CanvasPreset) {+ if (opening || session != null) return+ opening = trueval rgb = listOf(((p.background shr 16) and 255) / 255f, ((p.background shr 8) and 255) / 255f, (p.background and 255) / 255f)- val s = CanvasSession.newCustom(p.width.toUInt(), p.height.toUInt(), p.dpi, rgb, !p.backgroundHidden, "Untitled Artwork")- s.setNewProfile(p.profile)- start(s, store.newFolder(galleryDir))- timeLapse?.settings = p.timeLapse- startedNew()+ viewModelScope.launch {+ val s = withContext(Dispatchers.IO) {+ CanvasSession.newCustom(p.width.toUInt(), p.height.toUInt(), p.dpi, rgb, !p.backgroundHidden, "Untitled Artwork").also {+ it.setNewProfile(p.profile)+ loadBrushes(it)+ }+ }+ opening = false+ start(s, store.newFolder(galleryDir), brushesLoaded = true)+ timeLapse?.settings = p.timeLapse+ startedNew()+ }}/** The layers a canvas [width] × [height] holds on this device. */@@ -882,10 +893,19 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {/** A new artwork of `width` × `height` pixels (the screen's size), in Display P3, opened on the canvas. */fun newArtwork(width: Int, height: Int) {- val s = CanvasSession(width.coerceAtLeast(1).toUInt(), height.coerceAtLeast(1).toUInt())- s.setNewProfile(io.github.artcraftandroid.procraft.gallery.CanvasPreset.P3)- start(s, store.newFolder(galleryDir))- startedNew()+ if (opening || session != null) return+ opening = true+ viewModelScope.launch {+ val s = withContext(Dispatchers.IO) {+ CanvasSession(width.coerceAtLeast(1).toUInt(), height.coerceAtLeast(1).toUInt()).also {+ it.setNewProfile(io.github.artcraftandroid.procraft.gallery.CanvasPreset.P3)+ loadBrushes(it)+ }+ }+ opening = false+ start(s, store.newFolder(galleryDir), brushesLoaded = true)+ startedNew()+ }}// the app's own settings (the settings page beside the canvas): read when the app comes back@@ -972,9 +992,13 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {message = "This picture can't be opened."return@launch}- val s = CanvasSession.fromImage(image, "Untitled Artwork")- if (p3) s.setNewProfile(io.github.artcraftandroid.procraft.gallery.CanvasPreset.P3)- start(s, store.newFolder(galleryDir))+ val s = withContext(Dispatchers.IO) {+ CanvasSession.fromImage(image, "Untitled Artwork").also {+ if (p3) it.setNewProfile(io.github.artcraftandroid.procraft.gallery.CanvasPreset.P3)+ loadBrushes(it)+ }+ }+ start(s, store.newFolder(galleryDir), brushesLoaded = true)startedNew()}}@@ -1258,14 +1282,15 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {val s = withContext(Dispatchers.IO) {runCatching {val file = resolver.openInputStream(uri)?.use { it.readBytes() } ?: return@runCatching null- CanvasSession.openModel(file, name, null, name.substringBeforeLast('.').ifBlank { "Untitled Artwork" }, 0u).also(::loadModelTextures)+ CanvasSession.openModel(file, name, null, name.substringBeforeLast('.').ifBlank { "Untitled Artwork" }, 0u)+ .also(::loadModelTextures).also(::loadBrushes)}.onFailure { android.util.Log.e("ProCraft", "opening a model", it) }.getOrNull()}if (s == null) {message = "This model can't be opened."return}- start(s, store.newFolder(galleryDir))+ start(s, store.newFolder(galleryDir), brushesLoaded = true)startedNew()}@@ -1438,17 +1463,18 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {/** Opens artwork [a], the gallery zooming into its cell; then [then]. */fun openArtwork(a: Artwork, then: (() -> Unit)? = null) {- if (opening) return+ // one artwork open at a time: a second session on it would leave the screen on the first+ if (opening || session != null) returnopening = trueval cell = thumbAt[a.dir.name]viewModelScope.launch {- val s = withContext(Dispatchers.IO) { runCatching { CanvasSession.open(a.dir.path) } }+ val s = withContext(Dispatchers.IO) { runCatching { CanvasSession.open(a.dir.path).also(::loadBrushes) } }opening = falses.onSuccess {if (!orientationMemory) it.forgetSavedView()val m = it.openingViewMatrix(windowPx.width.toUInt(), windowPx.height.toUInt())galleryZoom = cell?.let { c -> GalleryZoom(c, canvasRect(it, m), opening = true) }- start(it, a.dir)+ start(it, a.dir, brushesLoaded = true)then?.invoke()}.onFailure { message = "This artwork can't be opened." }}@@ -1662,20 +1688,12 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {viewModelScope.launch(Dispatchers.IO) { runCatching { s.save(dir.path) } }}- private fun start(s: CanvasSession, dir: File) {- // the canvas's colours are P3 or sRGB numbers: the colour panel and swatches follow- refreshCanvasSpace(s)- // layers may take a quarter of the device's memory: every layer stays on the GPU- val memory = android.app.ActivityManager.MemoryInfo()- getApplication<Application>().getSystemService(android.app.ActivityManager::class.java)?.getMemoryInfo(memory)- if (memory.totalMem > 0) io.github.artcraftandroid.procraft.core.setLayerBudget((memory.totalMem / 4).toULong())- s.setSelectionVisibility(selectionVisibility)- s.setBrushScaling(brushOnScreen)- // as it was left, or fitted- if (!orientationMemory) s.forgetSavedView()- s.setInputSettings(inputStabilization, inputMotionFiltering, inputMotionExpression, inputPressureSmoothing, inputCurve.flatMap { listOf(it.x, it.y) })- applyAppSettings(s)- timeLapse = TimeLapse(dir).also { timeLapseRecording = it.recording }+ /**+ * Gives [s] the app's brushes as they were kept: the library (else the edits earlier versions+ * kept), brushes shipped since joining it once, and the brushes in use. Reading the library+ * takes half a second, so callers off the main thread do this before [start].+ */+ private fun loadBrushes(s: CanvasSession) {// the library as kept, else edits kept by earlier versionsval kept = brushPrefs.getString(BRUSH_LIBRARY, null)val loaded = kept != null && s.loadBrushLibrary(kept)@@ -1692,6 +1710,23 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {}// the brushes in use as they were left (they go by place in the kept library)if (loaded) brushPrefs.getString(BRUSH_CHOICES, null)?.let { s.loadBrushChoices(it) }+ }++ private fun start(s: CanvasSession, dir: File, brushesLoaded: Boolean = false) {+ // the canvas's colours are P3 or sRGB numbers: the colour panel and swatches follow+ refreshCanvasSpace(s)+ // layers may take a quarter of the device's memory: every layer stays on the GPU+ val memory = android.app.ActivityManager.MemoryInfo()+ getApplication<Application>().getSystemService(android.app.ActivityManager::class.java)?.getMemoryInfo(memory)+ if (memory.totalMem > 0) io.github.artcraftandroid.procraft.core.setLayerBudget((memory.totalMem / 4).toULong())+ s.setSelectionVisibility(selectionVisibility)+ s.setBrushScaling(brushOnScreen)+ // as it was left, or fitted+ if (!orientationMemory) s.forgetSavedView()+ s.setInputSettings(inputStabilization, inputMotionFiltering, inputMotionExpression, inputPressureSmoothing, inputCurve.flatMap { listOf(it.x, it.y) })+ applyAppSettings(s)+ timeLapse = TimeLapse(dir).also { timeLapseRecording = it.recording }+ if (!brushesLoaded) loadBrushes(s)session = sval model = s.modelInfo()modelOpen = model != nulldiff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.ktindex 7b3f735..0a32c15 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/AppRoot.kt@@ -165,7 +165,8 @@ fun AppRoot(vm: CanvasViewModel) {}Panel.COLOR -> ColorPanel(vm, place.arrowX, maxHeight, at)Panel.BRUSHES -> BrushLibrary(vm, place.arrowX, at.height(maxHeight - Pc.BarHeight - 25.dp))- Panel.ACTIONS -> ActionsPanel(vm, place.arrowX, at)+ // ending 25 above the screen's edge, its rows scrolling (as measured)+ Panel.ACTIONS -> ActionsPanel(vm, place.arrowX, at.heightIn(max = maxHeight - Pc.BarHeight - 25.dp))Panel.ADJUSTMENTS -> AdjustmentsPanel(vm, place.arrowX, at.heightIn(max = maxHeight - Pc.BarHeight - Pc.PopoverGap))Panel.NONE -> Unit}diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ZoomUi.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ZoomUi.ktindex bc0e968..be022ec 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ZoomUi.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/ZoomUi.kt@@ -17,6 +17,7 @@ import androidx.compose.ui.geometry.Rectimport androidx.compose.ui.geometry.lerpimport androidx.compose.ui.graphics.TransformOriginimport androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.input.pointer.PointerEventPassimport androidx.compose.ui.input.pointer.pointerInputimport androidx.compose.ui.unit.IntOffsetimport androidx.compose.ui.unit.IntSize@@ -76,8 +77,16 @@ fun ZoomingGallery(vm: CanvasViewModel, overlay: Boolean) {else -> cover.value}}- // nothing in it is touched while it moves- .then(if (overlay) Modifier.pointerInput(Unit) { awaitEachGesture { while (true) awaitPointerEvent().changes.forEach { it.consume() } } } else Modifier),+ // nothing in it is touched while it moves: taken before the gallery in it sees them+ .then(+ if (overlay) {+ Modifier.pointerInput(Unit) {+ awaitEachGesture { while (true) awaitPointerEvent(PointerEventPass.Initial).changes.forEach { it.consume() } }+ }+ } else {+ Modifier+ },+ ),) { GalleryScreen(vm) }// closing, the canvas fades away over its cellval picture = z.picturediff --git a/apps/procraft-android/src/device.rs b/apps/procraft-android/src/device.rsnew file mode 100644index 0000000..676f7bf--- /dev/null+++ b/apps/procraft-android/src/device.rs@@ -0,0 +1,44 @@+//! The app's GPU device: one for every artwork, as making one compiles the canvases' pipelines (a+//! second and a half on a tablet).++use std::sync::{Mutex, MutexGuard, PoisonError};++use procraft_gpu::Gpu;++static DEVICE: Mutex<Option<Gpu>> = Mutex::new(None);++/// The app's device, held while one is made so that two aren't made at once.+pub(crate) fn kept() -> MutexGuard<'static, Option<Gpu>> {+ DEVICE.lock().unwrap_or_else(PoisonError::into_inner)+}++/// Makes the app's device and its canvases' pipelines ahead of the first artwork, so that it opens+/// at once; nothing if it is made. Slow: call it off the main thread.+#[uniffi::export]+pub fn warm_gpu() {+ let mut kept = kept();+ if kept.is_some() {+ return;+ }+ match Gpu::headless() {+ Ok(gpu) => {+ gpu.prepare_canvases();+ *kept = Some(gpu);+ }+ Err(e) => log::error!("making the GPU device: {e}"),+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn warming_makes_the_apps_device_once() {+ warm_gpu();+ let Some(first) = kept().clone() else { return eprintln!("skipped: no GPU") };+ warm_gpu();+ let again = kept().clone().unwrap();+ assert!(first.device == again.device);+ }+}diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rsindex d20f1b3..5e8948e 100644--- a/apps/procraft-android/src/lib.rs+++ b/apps/procraft-android/src/lib.rs@@ -16,6 +16,7 @@ mod brushes;mod clipboard;mod colour;mod crop;+mod device;mod guide;mod layer_ops;mod layers;diff --git a/apps/procraft-android/src/render.rs b/apps/procraft-android/src/render.rsindex 009f269..f9a1e07 100644--- a/apps/procraft-android/src/render.rs+++ b/apps/procraft-android/src/render.rs@@ -15,17 +15,32 @@ use crate::{};pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {- let instance = shared.lock().gpu.as_ref().map_or_else(Gpu::instance, |g| g.instance.clone());+ let own = shared.lock().gpu.as_ref().map(|g| g.instance.clone());+ // the app's device (waited for while it is being made), else one made for this window and+ // kept for every artwork after+ let mut kept = crate::device::kept();+ let instance = own.clone().or_else(|| kept.as_ref().map(|g| g.instance.clone())).unwrap_or_else(Gpu::instance);let surface = match crate::window::create_surface(&instance, &window) {Ok(s) => s,Err(e) => return fail(&shared, e),};- if shared.lock().gpu.is_none() {- // the device is made once, for the first window, and kept with the layers it holds+ let device = if own.is_some() {+ None+ } else if let Some(gpu) = kept.as_ref() {+ Some(gpu.clone())+ } else {match Gpu::for_surface(instance, &surface) {- Ok(gpu) => shared.lock().gpu = Some(gpu),+ Ok(gpu) => {+ *kept = Some(gpu.clone());+ Some(gpu)+ }Err(e) => return fail(&shared, e.to_string()),}+ };+ drop(kept);+ if let Some(gpu) = device {+ // it holds the session's layers from now on+ shared.lock().gpu.get_or_insert(gpu);}let queue = {let mut st = shared.lock();diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rsindex 8f33f98..2e46173 100644--- a/crates/gpu/src/canvas.rs+++ b/crates/gpu/src/canvas.rs@@ -1036,7 +1036,9 @@ impl Step {}}-struct Pipelines {+/// The pipelines every canvas on a device draws with, made once for the device (see+/// [`crate::Gpu`]): compiling them takes a tablet a second and a half.+pub(crate) struct Pipelines {/// A 3D colour table and its sampler, for the display.lut_layout: wgpu::BindGroupLayout,/// The display's: the canvas, the uniforms and the colour table.@@ -1068,23 +1070,28 @@ struct Pipelines {/// Liquify stamps added into the field, and reconstruct's fading of it.liquify_add: wgpu::RenderPipeline,liquify_fade: wgpu::RenderPipeline,- /// The selection's look, per target format and whether it multiplies.- selection: HashMap<(wgpu::TextureFormat, bool), wgpu::RenderPipeline>,selection_module: wgpu::ShaderModule,smudge_layout: wgpu::BindGroupLayout,smudge_carry: wgpu::ComputePipeline,smudge_deposit: wgpu::ComputePipeline,display_module: wgpu::ShaderModule,display_pipeline_layout: wgpu::PipelineLayout,- display: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,surround_module: wgpu::ShaderModule,surround_pipeline_layout: wgpu::PipelineLayout,- surround: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,stamp_layout: wgpu::PipelineLayout,overlay_module: wgpu::ShaderModule,- overlay: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,- /// Frosted panels: the blur passes and the regions drawn, per target format.+ /// Frosted panels: the blur passes and the regions drawn.backdrop_module: wgpu::ShaderModule,+}++/// A canvas's own pipelines for drawing to a window, made for each target format as it comes.+#[derive(Default)]+struct ScreenPipelines {+ display: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,+ surround: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,+ overlay: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,+ /// The selection's look, per target format and whether it multiplies.+ selection: HashMap<(wgpu::TextureFormat, bool), wgpu::RenderPipeline>,blur: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,frost: HashMap<wgpu::TextureFormat, wgpu::RenderPipeline>,}@@ -1092,7 +1099,8 @@ struct Pipelines {pub struct Canvas {width: u32,height: u32,- pipelines: Pipelines,+ pipelines: std::sync::Arc<Pipelines>,+ screen: ScreenPipelines,layers: HashMap<LayerId, LayerGpu>,stroke_view: wgpu::TextureView,/// Ping-pong pair; the finished composite is always left in the first.@@ -1243,7 +1251,7 @@ impl Canvas {return Err(GpuError::TooLarge(width, height, max));}let device = &gpu.device;- let pipelines = Pipelines::new(device);+ let pipelines = gpu.canvas_pipelines();let stroke_texture = texture(device, "stroke", width, height, STROKE_FORMAT);let stroke_view = stroke_texture.create_view(&wgpu::TextureViewDescriptor::default());let composite = [@@ -1305,6 +1313,7 @@ impl Canvas {width,height,pipelines,+ screen: ScreenPipelines::default(),layers: HashMap::new(),stroke_view,composite,@@ -3575,20 +3584,18 @@ impl Canvas {) {let srgb = target.format.is_srgb();let Pipelines {- display,display_module,display_pipeline_layout,display_lut_layout,- surround: surround_pipelines,surround_module,surround_pipeline_layout,- overlay,overlay_module,stamp_layout,- selection: selection_pipelines,selection_module,..- } = &mut self.pipelines;+ } = &*self.pipelines;+ let ScreenPipelines { display, surround: surround_pipelines, overlay, selection: selection_pipelines, .. } =+ &mut self.screen;let pipeline = display.entry(target.format).or_insert_with(|| {sampled_pipeline(device,@@ -3779,16 +3786,9 @@ impl Canvas {}let Some(small) = self.backdrop.as_ref() else { return };let srgb = target.format.is_srgb();- let Pipelines {- display,- display_module,- display_pipeline_layout,- display_lut_layout,- backdrop_module,- blur,- frost,- ..- } = &mut self.pipelines;+ let Pipelines { display_module, display_pipeline_layout, display_lut_layout, backdrop_module, .. } =+ &*self.pipelines;+ let ScreenPipelines { display, blur, frost, .. } = &mut self.screen;let display = display.entry(target.format).or_insert_with(|| {sampled_pipeline(device,@@ -4123,7 +4123,7 @@ impl Canvas {}impl Pipelines {- fn new(device: &wgpu::Device) -> Pipelines {+ pub(crate) fn new(device: &wgpu::Device) -> Pipelines {let tex_entry = |binding| wgpu::BindGroupLayoutEntry {binding,visibility: wgpu::ShaderStages::FRAGMENT,@@ -4561,18 +4561,16 @@ impl Pipelines {uniform_layout,display_module,display_pipeline_layout,- display: HashMap::new(),+surround_module,surround_pipeline_layout,- surround: HashMap::new(),+stamp_layout,overlay_module,- overlay: HashMap::new(),+selection_module,- selection: HashMap::new(),+backdrop_module,- blur: HashMap::new(),- frost: HashMap::new(),}}}diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rsindex bb2aca6..1445f01 100644--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@ -8,8 +8,8 @@ mod canvas;mod lighting;mod scene;-use std::sync::Arc;use std::sync::atomic::{AtomicU64, Ordering};+use std::sync::{Arc, OnceLock};pub use canvas::{Backdrop, Canvas, DisplayLut, Filter, LiquifyMode, LiquifyStamp, Resample, SelectionLook, Stripes, StrokeStyle,@@ -33,13 +33,16 @@ pub enum GpuError {Readback(String),}-/// The device everything renders with.+/// The device everything renders with. A clone is the same device, sharing what is made for it.+#[derive(Clone)]pub struct Gpu {pub instance: wgpu::Instance,pub adapter: wgpu::Adapter,pub device: wgpu::Device,pub queue: wgpu::Queue,errors: Arc<AtomicU64>,+ /// The canvases' pipelines, once the first canvas has made them.+ canvas_pipelines: Arc<OnceLock<Arc<canvas::Pipelines>>>,}impl Gpu {@@ -91,7 +94,18 @@ impl Gpu {counter.fetch_add(1, Ordering::Relaxed);log::error!("GPU error: {e}");}));- Ok(Gpu { instance, adapter, device, queue, errors })+ Ok(Gpu { instance, adapter, device, queue, errors, canvas_pipelines: Arc::default() })+ }++ /// Makes the pipelines canvases draw with now, so the first canvas on this device is made at+ /// once (slow: off the main thread).+ pub fn prepare_canvases(&self) {+ let _ = self.canvas_pipelines();+ }++ /// The pipelines canvases draw with, made by the first canvas on this device.+ pub(crate) fn canvas_pipelines(&self) -> Arc<canvas::Pipelines> {+ Arc::clone(self.canvas_pipelines.get_or_init(|| Arc::new(canvas::Pipelines::new(&self.device))))}/// GPU validation errors so far (each one is logged); tests expect none.@@ -109,3 +123,24 @@ impl Gpu {self.device.limits().max_texture_dimension_2d}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn canvases_on_one_device_share_their_pipelines() {+ let gpu = match Gpu::headless() {+ Ok(g) => g,+ Err(e) => return eprintln!("skipped: {e}"),+ };+ let first = gpu.canvas_pipelines();+ // the same once made, and for a clone of the device too+ assert!(Arc::ptr_eq(&first, &gpu.canvas_pipelines()));+ assert!(Arc::ptr_eq(&first, &gpu.clone().canvas_pipelines()));+ // another device makes its own+ if let Ok(other) = Gpu::headless() {+ assert!(!Arc::ptr_eq(&first, &other.canvas_pipelines()));+ }+ }+}