ProCraft for Android: An Android native painting app written in Rust
Rust
perf(gpu): a remake part composites at most 8 items
Josh4d2928fb2bdf
diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rsindex eacb763..abceb6f 100644--- a/crates/gpu/src/canvas.rs+++ b/crates/gpu/src/canvas.rs@@ -1217,6 +1217,8 @@ pub struct Canvas {version: u64,/// Layers past the budget are still to be parked (a render parks only a few).parking_left: bool,+ /// How the last remake of the caches went.+ last_remake: Option<RemakeStats>,/// 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,@@ -1424,6 +1426,7 @@ impl Canvas {versions: HashMap::new(),version: 0,parking_left: false,+ last_remake: None,resident_budget: 0,needed_at: HashMap::new(),renders: 0,@@ -1897,6 +1900,11 @@ impl Canvas {self.busy() || self.parking_left}+ /// How the last remake of the caches went (none made yet).+ pub fn last_remake(&self) -> Option<RemakeStats> {+ self.last_remake+ }+/// Whether the composite is being made around pictures of what isn't worked on.pub fn composites_cached(&self) -> bool {self.caches.is_some()@@ -4016,6 +4024,7 @@ impl Canvas {keep,started: std::time::Instant::now(),slices: 0,+ largest_part: 0,restored: 0,restoring: std::time::Duration::ZERO,});@@ -4118,7 +4127,7 @@ impl Canvas {let device = &gpu.device;let full = self.full();let deadline = std::time::Instant::now() + REMAKE_SLICE;- let (mut restored, mut restoring) = (0, std::time::Duration::ZERO);+ let (mut restored, mut restoring, mut largest) = (0, std::time::Duration::ZERO, 0);loop {let (below, items, progress) = match (r.below, r.above) {(Some(p), _) => (true, &r.below_items, p),@@ -4139,8 +4148,11 @@ impl Canvas {while let Some(&item) = items.get(at) {let mut more = item_layers(doc, item);more.extend(doc.clip_base(item));+ // the time counts bringing layers back, not the compositing after: the count+ // keeps a part whose layers come back quickly from compositing too longlet late = std::time::Instant::now() >= deadline;- if !part.is_empty() && (late || layers.len() + more.len() > room) {+ let full_part = part.len() >= MOST_PART_ITEMS || layers.len() + more.len() > room;+ if !part.is_empty() && (late || full_part) {break;}at += 1;@@ -4166,6 +4178,7 @@ impl Canvas {layers.extend(more);part.push((item, rect));}+ largest = largest.max(part.len());let mut encoder =device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("cache part") });let mut uniforms = UniformData::default();@@ -4205,18 +4218,21 @@ impl Canvas {r.slices += 1;r.restored += restored;r.restoring += restoring;+ r.largest_part = r.largest_part.max(largest);if r.below.is_some() || r.above.is_some() {self.remake = Some(r);return;}log::info!(- "caches remade in {} ms over {} renders: {} items, {} layers brought back in {} ms",+ "caches remade in {} ms over {} renders: {} items (at most {} a part), {} layers brought back in {} ms",r.started.elapsed().as_millis(),r.slices,r.below_items.len() + r.above_items.len(),+ r.largest_part,r.restored,r.restoring.as_millis());+ self.last_remake = Some(RemakeStats { renders: r.slices, largest_part: r.largest_part });// made: they are the caches, and the whole picture is composited again around themlet Remake { mut caches, below_items, below_key, above_items, above_key, .. } = r;caches.below_items = below_items;@@ -5816,6 +5832,13 @@ fn painted_bounds(t: &Thumbnail, painted: impl Fn([u8; 4]) -> bool) -> Option<[u/// The longest side of a parked layer's small pictures, pixels: as big as any thumbnail asked for.const PARKED_PICTURE_SIDE: u32 = 192;+/// How a remake of the caches went: over how many renders, and the most items one part took.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub struct RemakeStats {+ pub renders: u32,+ pub largest_part: usize,+}+/// A layer's pixels made ready to park: compressed, with its small pictures. Made on any thread/// (the slow part of [`Canvas::park_pixels`]), then given to [`Canvas::park_ready`].pub struct ReadyToPark {@@ -5973,6 +5996,9 @@ struct Caches {const MOST_PARK_READS: usize = 4;const PARK_SLICE: std::time::Duration = std::time::Duration::from_millis(6);+/// The most items a part of a remake composites before it is sent (and the time looked at).+const MOST_PART_ITEMS: usize = 8;+/// 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);@@ -5992,6 +6018,7 @@ struct Remake {/// and the time that took.started: std::time::Instant,slices: u32,+ largest_part: usize,restored: usize,restoring: std::time::Duration,}diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rsindex c17d0ef..5d226f8 100644--- a/crates/gpu/src/lib.rs+++ b/crates/gpu/src/lib.rs@@ -13,8 +13,8 @@ use std::sync::atomic::{AtomicU64, Ordering};use std::sync::{Arc, OnceLock};pub use canvas::{- Backdrop, Canvas, DisplayLut, Filter, LiquifyMode, LiquifyStamp, ReadyToPark, Resample, SelectionLook, SmallScene,- Stripes, StrokeStyle, StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,+ Backdrop, Canvas, DisplayLut, Filter, LiquifyMode, LiquifyStamp, ReadyToPark, RemakeStats, Resample, SelectionLook,+ SmallScene, Stripes, StrokeStyle, StrokeTool, SurroundGrid, Target, Thumbnail, read_texture,};pub use lighting::Lighting;pub use scene::{Light, MOST_LIGHTS, MeshView, Orbit, SurfaceMap};diff --git a/crates/gpu/tests/layers.rs b/crates/gpu/tests/layers.rsindex 85d1242..1c064c2 100644--- a/crates/gpu/tests/layers.rs+++ b/crates/gpu/tests/layers.rs@@ -815,3 +815,38 @@ fn a_render_reads_back_only_a_few_layers_to_park() {assert!(canvas.parked_layers() >= 8, "parked in all: {}", canvas.parked_layers());t.same(&gpu, 2, "the frames shown, the rest parked");}++#[test]+fn a_remake_part_composites_a_few_items_at_most() {+ let Some((gpu, _, _)) = setup() else { return };+ let mut doc = Document::new(W, W);+ for _ in 0..39 {+ doc.add_layer();+ }+ let ids: Vec<LayerId> = doc.layers().iter().map(|l| l.id).collect();+ // each layer a short band of its own colour+ let pixels: Vec<Vec<u8>> = (0..ids.len())+ .map(|i| {+ let mut px = vec![0u8; (W * W * 4) as usize];+ let (y, x0) = (i / 8 * 3 + 1, i % 8 * 2);+ for x in x0..x0 + 3 {+ let o = (y * W as usize + x) * 4;+ px[o..o + 4].copy_from_slice(&[(i * 6) as u8, 200 - (i * 4) as u8, 90, 255]);+ }+ px+ })+ .collect();+ let docs = [doc.clone(), doc];+ let canvases = [Canvas::new(&gpu, &docs[0]).unwrap(), Canvas::new(&gpu, &docs[1]).unwrap()];+ let mut t = Twins { docs, canvases };+ // room for 30 of the 40, every one kept compressed (as an opening artwork's past what fits):+ // the caches are remade from layers quick to bring back, with room for many a part+ t.canvases[0].set_resident_budget(30 * u64::from(W * W * 4));+ for (id, px) in ids.iter().zip(&pixels) {+ t.canvases[0].park_pixels(*id, px).unwrap();+ t.canvases[1].upload_layer(&gpu, *id, px).unwrap();+ }+ t.same(&gpu, 2, "opened");+ let stats = t.canvases[0].last_remake().unwrap();+ assert!((1..=8).contains(&stats.largest_part), "{stats:?}");+}