ProCraft for Android: An Android native painting app written in Rust
Rust
perf(layers): reload only the thumbnails of layers whose pixels changed
Josh6451050846dc
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 b6dd36a..5900ab1 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@@ -693,6 +693,8 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {var canRedo by mutableStateOf(false)private setval thumbnails = mutableStateMapOf<ULong, ImageBitmap>()+ /** The pixels' version each thumbnail was read at: it is read again only once its layer's is new. */+ private val thumbnailVersions = HashMap<ULong, ULong>()var brushSets by mutableStateOf<List<BrushSetInfo>>(emptyList())private set@@ -1952,6 +1954,7 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {attachProjection()activeSince = android.os.SystemClock.elapsedRealtime()thumbnails.clear()+ thumbnailVersions.clear()previews.clear()shadedNames.clear()panel = Panel.NONE@@ -2799,6 +2802,7 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {}if (s.cropCanvas(r.left, r.top, r.width, r.height, cropAngle, cropResample, w.toUInt(), h.toUInt())) {thumbnails.clear()+ thumbnailVersions.clear()refresh()refreshView()if (panel == Panel.LAYERS) refreshThumbnails()@@ -5786,11 +5790,20 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {hasSelection = session?.hasSelection() ?: false}+ /** Reads again the thumbnails of the layers whose pixels changed since theirs was read (or have none). */private fun refreshThumbnails() {- for (l in layers) loadThumbnail(l.id)+ val s = session ?: return+ viewModelScope.launch {+ val versions = withContext(Dispatchers.Default) { runCatching { s.layerVersions() }.getOrNull() } ?: return@launch+ if (session !== s) return@launch+ for (v in versions) {+ if (v.id !in thumbnails || thumbnailVersions[v.id] != v.version) loadThumbnail(v.id, v.version)+ }+ }}- private fun loadThumbnail(id: ULong) {+ /** Reads layer [id]'s thumbnail; [version], its pixels' version when asked (none: not known). */+ private fun loadThumbnail(id: ULong, version: ULong? = null) {val s = session ?: returnviewModelScope.launch {val image = withContext(Dispatchers.Default) {@@ -5799,7 +5812,10 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(t.rgba))bitmap.asImageBitmap()}- if (image != null) thumbnails[id] = image+ if (image != null && session === s) {+ thumbnails[id] = image+ if (version != null) thumbnailVersions[id] = version else thumbnailVersions.remove(id)+ }}}diff --git a/apps/procraft-android/src/layers.rs b/apps/procraft-android/src/layers.rsindex c3aed4f..225bb7e 100644--- a/apps/procraft-android/src/layers.rs+++ b/apps/procraft-android/src/layers.rs@@ -39,6 +39,13 @@ pub struct LayerInfo {pub can_clip: bool,}+/// A layer's pixels' version: new whenever they may have changed.+#[derive(Clone, Debug, PartialEq, uniffi::Record)]+pub struct LayerVersion {+ pub id: u64,+ pub version: u64,+}+/// What a layer in the list is.#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]pub enum LayerType {@@ -565,6 +572,21 @@ impl CanvasSession {}/// The layer scaled to fit `max_side` pixels, or none before the canvas exists.+ /// Each paint layer's and mask's pixels' version, the queued work done first: a thumbnail+ /// need only be read again when its layer's is new.+ pub fn layer_versions(&self) -> Vec<LayerVersion> {+ let mut guard = self.shared.lock();+ let st = &mut *guard;+ st.flush();+ let Some(canvas) = st.canvas.as_ref() else { return Vec::new() };+ st.doc+ .layers()+ .iter()+ .filter(|l| l.kind != LayerKind::Group)+ .map(|l| LayerVersion { id: l.id.0, version: canvas.layer_version(l.id) })+ .collect()+ }+pub fn layer_thumbnail(&self, id: u64, max_side: u32) -> Option<ThumbnailImage> {let mut guard = self.shared.lock();let st = &mut *guard;diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rsindex cf0aa77..36bbe10 100644--- a/crates/gpu/src/canvas.rs+++ b/crates/gpu/src/canvas.rs@@ -1211,6 +1211,10 @@ pub struct Canvas {/// Layers whose pixels changed since [`Canvas::take_changed`] took them, so a save reads only/// those.changed: HashSet<LayerId>,+ /// Each layer's pixels' version, from a count that goes up with every change: thumbnails are+ /// read again only when theirs has.+ versions: HashMap<LayerId, u64>,+ version: u64,/// What the layers' textures may take before those needed least lately are parked (0: none/// are), and when each was last needed, counted in renders.resident_budget: u64,@@ -1415,6 +1419,8 @@ impl Canvas {parked: HashMap::new(),pictures: HashMap::new(),changed: HashSet::new(),+ versions: HashMap::new(),+ version: 0,resident_budget: 0,needed_at: HashMap::new(),renders: 0,@@ -1909,6 +1915,20 @@ impl Canvas {std::mem::take(&mut self.changed)}+ /// Layer `id`'s pixels are changing: counted for the next save, and given a new version.+ fn touched(&mut self, id: LayerId) {+ self.changed.insert(id);+ self.version += 1;+ self.versions.insert(id, self.version);+ }++ /// Layer `id`'s pixels' version as of the last render: it is new whenever they may have+ /// changed (painting, undo, uploads, every other change), so a thumbnail of the same version+ /// is still right.+ pub fn layer_version(&self, id: LayerId) -> u64 {+ self.versions.get(&id).copied().unwrap_or(0)+ }+/// Layers to count as changed again (a save of them that failed).pub fn mark_changed(&mut self, ids: impl IntoIterator<Item = LayerId>) {self.changed.extend(ids);@@ -1966,7 +1986,7 @@ impl Canvas {if written {self.parked.remove(&id);self.pictures.remove(&id);- self.changed.insert(id);+ self.touched(id);}self.needed_at.insert(id, self.renders);// the staged pixels sent on now and then, so many in a row don't pile up@@ -4996,9 +5016,9 @@ impl Canvas {let view = texture.create_view(&wgpu::TextureViewDescriptor::default());self.layers.insert(id, LayerGpu { texture, view });}+ self.touched(id);let Some(layer) = self.layers.get(&id) else { return Ok(()) };self.caches = None;- self.changed.insert(id);// its compressed copy no longer what it holdsself.parked.remove(&id);self.pictures.remove(&id);diff --git a/crates/gpu/tests/layers.rs b/crates/gpu/tests/layers.rsindex 3875dbd..b4ce5a9 100644--- a/crates/gpu/tests/layers.rs+++ b/crates/gpu/tests/layers.rs@@ -700,3 +700,31 @@ fn the_picture_read_back_waits_for_the_caches() {assert!(worst <= 2, "read while the caches are remade: differs by {worst}");assert!(!t.canvases[0].busy());}++#[test]+fn a_layers_version_changes_with_its_pixels_only() {+ let Some((gpu, mut doc, mut canvas)) = setup() else { return };+ let a = doc.current();+ let b = doc.add_layer();+ canvas.render(&gpu, &mut doc, None);+ let (va, vb) = (canvas.layer_version(a), canvas.layer_version(b));+ paint(&mut canvas, &doc, b, [200, 0, 0], Vec2::new(8.0, 8.0), 3.0, StrokeTool::Paint);+ canvas.render(&gpu, &mut doc, None);+ assert_eq!(canvas.layer_version(a), va, "the other layer's stays");+ let painted = canvas.layer_version(b);+ assert_ne!(painted, vb, "the painted one's is new");+ // hidden, and another chosen: its pixels are as they were+ let before = doc.clone();+ doc.set_visible(b, false);+ canvas.record_document(before);+ doc.set_current(a);+ canvas.render(&gpu, &mut doc, None);+ assert_eq!(canvas.layer_version(b), painted, "hidden and left");+ // shown again, then the stroke undone: new again+ canvas.undo();+ canvas.render(&gpu, &mut doc, None);+ canvas.undo();+ canvas.render(&gpu, &mut doc, None);+ assert_ne!(canvas.layer_version(b), painted, "undone");+ assert_eq!(gpu.errors(), 0);+}