ProCraft for Android: An Android native painting app written in Rust
Rust
perf(gpu): park a few layers a render, not all past the budget at once
Josh13ca655764ee
diff --git a/apps/procraft-android/src/render.rs b/apps/procraft-android/src/render.rsindex e0c3105..e315894 100644--- a/apps/procraft-android/src/render.rs+++ b/apps/procraft-android/src/render.rs@@ -168,7 +168,7 @@ pub(crate) fn run(shared: Arc<Shared>, window: NativeWindow) {);// caches still being remade: another frame soon, and the artwork isn't shown yetlet busy = canvas.busy();- if animating || busy {+ if animating || canvas.wants_render() {st.needs_frame = true;}// counted only when the canvas was drawn, so a first frame means the artwork showsdiff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rsindex 2279b99..eacb763 100644--- a/crates/gpu/src/canvas.rs+++ b/crates/gpu/src/canvas.rs@@ -1215,6 +1215,8 @@ pub struct Canvas {/// read again only when theirs has.versions: HashMap<LayerId, u64>,version: u64,+ /// Layers past the budget are still to be parked (a render parks only a few).+ parking_left: bool,/// 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,@@ -1421,6 +1423,7 @@ impl Canvas {changed: HashSet::new(),versions: HashMap::new(),version: 0,+ parking_left: false,resident_budget: 0,needed_at: HashMap::new(),renders: 0,@@ -1888,6 +1891,12 @@ impl Canvas {}}+ /// Whether the canvas has work that more renders do (caches being remade, layers being+ /// parked): it wants another render soon.+ pub fn wants_render(&self) -> bool {+ self.busy() || self.parking_left+ }+/// Whether the composite is being made around pictures of what isn't worked on.pub fn composites_cached(&self) -> bool {self.caches.is_some()@@ -2041,6 +2050,7 @@ impl Canvas {/// worked on (and what they are clipped to); none while the composite isn't made around/// pictures of the others.fn park_extra(&mut self, gpu: &Gpu, doc: &Document) {+ self.parking_left = false;let per = u64::from(self.width) * u64::from(self.height) * 4;let most = usize::try_from(self.resident_budget / per.max(1)).unwrap_or(usize::MAX);if self.resident_budget == 0 || self.layers.len() <= most {@@ -2060,11 +2070,20 @@ impl Canvas {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();gone.sort_by_key(|id| self.needed_at.get(id).copied().unwrap_or(0));+ // a layer with no compressed copy yet is read back first, the slow part: a few a render+ // (at least one), the rest left for the renders after+ let started = std::time::Instant::now();+ let mut reads = 0;for id in gone {if self.layers.len() <= most {break;}if !self.parked.contains_key(&id) {+ if reads > 0 && (reads >= MOST_PARK_READS || started.elapsed() >= PARK_SLICE) {+ self.parking_left = true;+ break;+ }+ reads += 1;let Ok(pixels) = self.read_layer(gpu, id) else { continue };let pictures = Pictures::of(&pixels, self.width, self.height);self.parked.insert(id, Parked::of(&pixels, self.width, pictures.bounds));@@ -5949,6 +5968,11 @@ struct Caches {above_key: u64,}+/// How many layers a render reads back to park, at most, and how long it goes on reading them+/// (after the first).+const MOST_PARK_READS: usize = 4;+const PARK_SLICE: std::time::Duration = std::time::Duration::from_millis(6);+/// 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(16);diff --git a/crates/gpu/tests/layers.rs b/crates/gpu/tests/layers.rsindex b83220d..85d1242 100644--- a/crates/gpu/tests/layers.rs+++ b/crates/gpu/tests/layers.rs@@ -336,14 +336,15 @@ impl Twins {let mut pictures = Vec::new();for (doc, canvas) in self.docs.iter_mut().zip(self.canvases.iter_mut()) {canvas.render(gpu, doc, None);- // caches remade a little each render: until they are, the composite waits+ // caches remade and layers parked a little each render: until the caches are, the+ // composite waitsfor _ in 0..1000 {- if !canvas.busy() {+ if !canvas.wants_render() {break;}canvas.render(gpu, doc, None);}- assert!(!canvas.busy(), "{what}: caches still being remade");+ assert!(!canvas.wants_render(), "{what}: caches still being remade or layers parked");assert_eq!(gpu.errors(), 0, "GPU validation errors");pictures.push(canvas.read_composite(gpu).unwrap());}@@ -774,3 +775,43 @@ fn transforms_and_adjustments_on_the_layer_worked_on_keep_the_caches() {t.same(&gpu, 2, "cancelled");assert!(t.canvases[0].composites_cached());}++#[test]+fn a_render_reads_back_only_a_few_layers_to_park() {+ 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 };+ // drawn with room for every layer, so none has a compressed copy yet+ 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..12u8 {+ let id = doc.add_layer();+ let at = Vec2::new(1.0 + f32::from(i), 8.0);+ paint(canvas, doc, id, [i * 20, 100, 200 - i * 15], at, 2.0, StrokeTool::Paint);+ }+ canvas.invalidate();+ });+ t.same(&gpu, 2, "made");+ // then room for three, and Animation Assist on: the frames not shown are parked, a few a render+ t.canvases[0].set_resident_budget(3 * u64::from(W * W * 4));+ t.each(|doc, canvas| {+ doc.animation = Some(procraft_engine::animation::Animation { onion_frames: 1, ..Default::default() });+ canvas.invalidate();+ });+ let (doc, canvas) = (&mut t.docs[0], &mut t.canvases[0]);+ canvas.render(&gpu, doc, None);+ let first = canvas.parked_layers();+ assert!((1..=4).contains(&first), "parked in one render: {first}");+ assert!(canvas.wants_render(), "more to park");+ for _ in 0..100 {+ if !canvas.wants_render() {+ break;+ }+ canvas.render(&gpu, doc, None);+ }+ assert!(!canvas.wants_render());+ assert!(canvas.parked_layers() >= 8, "parked in all: {}", canvas.parked_layers());+ t.same(&gpu, 2, "the frames shown, the rest parked");+}