Skip to content

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

Rust

feat(layers): a canvas holds half the device's memory in layers, those not worked on kept compressed off the GPU

Joshb9d18212f241
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 674d6a0..dd1fe3f 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
@@ -1894,10 +1894,14 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {
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
+ // layers may take half the device's memory, their textures a quarter: past that, those not
+ // worked on are kept compressed off the GPU until needed
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())
+ if (memory.totalMem > 0) {
+ io.github.artcraftandroid.procraft.core.setLayerBudget((memory.totalMem / 2).toULong())
+ io.github.artcraftandroid.procraft.core.setResidentBudget((memory.totalMem / 4).toULong())
+ }
s.setSelectionVisibility(selectionVisibility)
s.setBrushScaling(brushOnScreen)
s.setLightInterface(lightInterface)
diff --git a/apps/procraft-android/src/layers.rs b/apps/procraft-android/src/layers.rs
index 20d249b..c3aed4f 100644
--- a/apps/procraft-android/src/layers.rs
+++ b/apps/procraft-android/src/layers.rs
@@ -75,6 +75,20 @@ pub fn set_layer_budget(bytes: u64) {
procraft_engine::document::set_layer_budget(bytes);
}
+/// What the layers' textures may take on this device, bytes: past it, layers not worked on are
+/// kept compressed off the GPU until needed. 0 keeps them all on it.
+static RESIDENT_BUDGET: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
+
+/// Sets what the layers' textures may take, bytes (see [`RESIDENT_BUDGET`]).
+#[uniffi::export]
+pub fn set_resident_budget(bytes: u64) {
+ RESIDENT_BUDGET.store(bytes, std::sync::atomic::Ordering::Relaxed);
+}
+
+pub(crate) fn resident_budget() -> u64 {
+ RESIDENT_BUDGET.load(std::sync::atomic::Ordering::Relaxed)
+}
+
/// How many layers a canvas holds, has, and of which kinds.
#[derive(uniffi::Record)]
pub struct LayerStats {
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 9917e7b..c919591 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -555,9 +555,13 @@ impl State {
let Some(gpu) = self.gpu.as_ref() else { return };
match Canvas::new(gpu, &self.doc) {
Ok(mut c) => {
+ c.set_resident_budget(crate::layers::resident_budget());
let (w, h) = (self.doc.width(), self.doc.height());
for (id, pixels) in project::opened_pixels(std::mem::take(&mut self.pending_pixels), w, h) {
- if let Err(e) = c.upload_layer(gpu, id, &pixels) {
+ // past what fits, kept compressed until needed
+ let opened =
+ if c.has_room() { c.upload_layer(gpu, id, &pixels) } else { c.park_pixels(id, &pixels) };
+ if let Err(e) = opened {
log::error!("opening a layer: {e}");
}
}
@@ -664,6 +668,7 @@ impl Shared {
(gpu.clone(), st.doc.clone(), st.pending_pixels.iter().map(|(id, _)| *id).collect::<Vec<_>>())
};
let made = Canvas::new(&gpu, &doc).map(|mut c| {
+ c.set_resident_budget(crate::layers::resident_budget());
for (i, id) in ids.iter().enumerate() {
let Some(layer) = self.lock().pending_pixels.get(i).filter(|(at, _)| at == id).map(|(_, p)| p.clone())
else {
@@ -671,7 +676,10 @@ impl Shared {
};
match layer.pixels(doc.width(), doc.height()) {
Ok(pixels) => {
- if let Err(e) = c.upload_layer(&gpu, *id, &pixels) {
+ // past what fits, kept compressed until needed
+ let opened =
+ if c.has_room() { c.upload_layer(&gpu, *id, &pixels) } else { c.park_pixels(*id, &pixels) };
+ if let Err(e) = opened {
log::error!("opening a layer: {e}");
}
}
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rs
index 14b77f3..fb5d9ea 100644
--- a/crates/gpu/src/canvas.rs
+++ b/crates/gpu/src/canvas.rs
@@ -1963,9 +1963,16 @@ impl Canvas {
if self.resident_budget == 0 || self.layers.len() <= most {
return;
}
- let Some(c) = &self.caches else { return };
- let mut keep = c.live_layers.clone();
- keep.extend(c.live.first().and_then(|first| doc.clip_base(*first)));
+ let mut keep = match (&self.caches, self.shown_frames(doc)) {
+ (Some(c), _) => {
+ let mut keep = c.live_layers.clone();
+ keep.extend(c.live.first().and_then(|first| doc.clip_base(*first)));
+ keep
+ }
+ // the frames shown, while Animation Assist is on
+ (None, Some(shown)) => shown,
+ (None, None) => return,
+ };
keep.extend(self.active.map(|a| a.layer));
keep.extend(self.held.as_ref().map(|h| h.layer));
let mut gone: Vec<LayerId> = self.layers.keys().filter(|id| !keep.contains(*id)).copied().collect();
@@ -3772,9 +3779,13 @@ impl Canvas {
let device = &gpu.device;
let Some((tops, live)) = self.live_items(doc).filter(|_| self.cache_composites) else {
self.caches = None;
- // everything composites: every layer is needed
- let all: Vec<LayerId> = doc.pixel_layer_ids().collect();
- self.restore_all(gpu, all, false);
+ // everything shown composites: Animation Assist's frames around the current one, or
+ // every layer
+ let needed: Vec<LayerId> = match self.shown_frames(doc) {
+ Some(shown) => shown.into_iter().collect(),
+ None => doc.pixel_layer_ids().collect(),
+ };
+ self.restore_all(gpu, needed, false);
return;
};
let below_items = tops.get(..live.start).unwrap_or_default().to_vec();
@@ -3890,6 +3901,28 @@ impl Canvas {
self.caches = Some(caches);
}
+ /// With Animation Assist on (and nothing else under way), the layers of the frames shown: the
+ /// current (or played) one, its onion skins, and a background or foreground frame.
+ fn shown_frames(&self, doc: &Document) -> Option<HashSet<LayerId>> {
+ let a = doc.animation.as_ref()?;
+ let busy =
+ self.transforming.is_some() || self.liquify.is_some() || self.pencil.is_some() || self.held.is_some();
+ if busy || self.smudge.is_some() {
+ return None;
+ }
+ let frames = animation::frames(doc);
+ let focus = self.play_frame.or_else(|| animation::current_frame(doc))?;
+ let shown: HashSet<LayerId> = frames
+ .iter()
+ .enumerate()
+ .filter(|(i, _)| {
+ !matches!(animation::look(a, *i, frames.len(), focus, self.play_frame.is_some()), FrameLook::Hidden)
+ })
+ .flat_map(|(_, f)| item_layers(doc, *f))
+ .collect();
+ Some(shown)
+ }
+
/// How many layers' textures fit the budget (any number without one).
fn most_resident(&self) -> usize {
let per = u64::from(self.width) * u64::from(self.height) * 4;
diff --git a/crates/gpu/tests/layers.rs b/crates/gpu/tests/layers.rs
index cd26ca2..5b368fb 100644
--- a/crates/gpu/tests/layers.rs
+++ b/crates/gpu/tests/layers.rs
@@ -538,3 +538,45 @@ fn an_artwork_opening_with_more_layers_than_fit_parks_the_rest() {
t.same(&gpu, 2, "one opened parked, painted");
assert!(t.canvases[0].parked_layers() > 0);
}
+
+#[test]
+fn an_animation_keeps_only_the_frames_shown() {
+ let Some((gpu, _, _)) = setup() else { return };
+ let docs = [Document::new(W, W), Document::new(W, W)];
+ let canvases = [Canvas::new(&gpu, &docs[0]).unwrap(), Canvas::new(&gpu, &docs[1]).unwrap()];
+ let mut t = Twins { docs, canvases };
+ t.canvases[0].set_resident_budget(3 * u64::from(W * W * 4));
+ t.each(|doc, canvas| {
+ let first = doc.current();
+ paint(canvas, doc, first, [0, 0, 0], Vec2::new(2.0, 8.0), 2.0, StrokeTool::Paint);
+ for i in 1..10u8 {
+ let id = doc.add_layer();
+ paint(
+ canvas,
+ doc,
+ id,
+ [i * 25, 100, 200 - i * 20],
+ Vec2::new(2.0 + f32::from(i) * 1.3, 8.0),
+ 2.0,
+ StrokeTool::Paint,
+ );
+ }
+ doc.animation = Some(procraft_engine::animation::Animation { onion_frames: 1, ..Default::default() });
+ canvas.invalidate();
+ });
+ t.same(&gpu, 2, "the last frame");
+ t.same(&gpu, 2, "the last frame, the rest parked");
+ assert!(t.canvases[0].parked_layers() >= 5, "parked: {}", t.canvases[0].parked_layers());
+ let ids: Vec<LayerId> = t.docs[0].layers().iter().map(|l| l.id).collect();
+ t.each(|doc, canvas| {
+ doc.set_current(ids[3]);
+ paint(canvas, doc, ids[3], [255, 255, 0], Vec2::new(8.0, 4.0), 2.0, StrokeTool::Paint);
+ });
+ t.same(&gpu, 2, "another frame painted");
+ for f in [0, 6, 9] {
+ t.each(|_, canvas| canvas.set_play_frame(Some(f)));
+ t.same(&gpu, 2, "played");
+ }
+ t.each(|_, canvas| canvas.set_play_frame(None));
+ t.same(&gpu, 2, "stopped");
+}