mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-27 03:06:57 -06:00
Fix #2820: images drawn small redraw from their kept mips while being moved
With image mips on (the canvas always sets them), drawing an image fetched its full decoded pixels first, decoding them again when the shared image store no longer held them, and only then looked up the mip it draws. The full pixels are only peeked, so they are the least recently used entries and the first the store drops over its budget (1 GiB on macOS and Windows). Once a document's full-size images exceed it (20 photos of 6000 x 4000 px are 1.9 GB as RGBA), every frame decoded every image again, though only their small mips were drawn: moving one image redrew seconds later. The store now remembers the size each blob's pixels decode to, so the mip level is found without the pixels, and a mip the store holds is drawn (and marked used) without fetching or decoding them. Without the mip, at full size, or with mips off (exports) drawing is unchanged. Measured on an Apple M4 (load average 15-24 from other work), 20 noise PNGs of 6000 x 4000 px fitted in a 2880 x 1800 view, one moved 5 pt per frame: before, 20 decodes and 1.1-3.0 s every frame; after, the first frame decodes 20 and the later ones none, in 3-5 ms.
This commit is contained in:
committed by
bflatastic
parent
4444450a44
commit
4e0cbfeede
+74
-17
@@ -1912,12 +1912,21 @@ impl Renderer {
|
||||
// Outline mode draws the image's frame, and with Document Setup → Show Images in Outline
|
||||
// Mode its pixels in greyscale under the frame.
|
||||
let outline = f.opts.outline;
|
||||
// With mips the full pixels are only looked at (not marked used) until they're drawn.
|
||||
// With mips the full pixels are only looked at (not marked used) until they're drawn, and
|
||||
// a mip the store holds is drawn without them: they may have been dropped (#2820).
|
||||
let mips = f.opts.image_mips;
|
||||
let pixels = if outline && !f.doc.setup.outline_images { None } else { self.image_pixmap(f.doc, &im.key, outline, !mips) };
|
||||
if let Some((key, pm)) = pixels {
|
||||
let (key, pm) = if outline { (key, pm) } else { ink::ink_image(&key, &pm, f.ink, f.doc.images.get(&im.key), !mips) };
|
||||
let pm = if mips { image_mip(&key, pm, f.view * im.xf * Affine::scale_non_uniform(im.width as f64, im.height as f64)) } else { pm };
|
||||
let unit = f.view * im.xf * Affine::scale_non_uniform(im.width as f64, im.height as f64);
|
||||
let shown = if outline && !f.doc.setup.outline_images {
|
||||
None
|
||||
} else if let Some(pm) = mips.then(|| self.kept_mip(f, &im.key, outline, unit)).flatten() {
|
||||
Some(pm)
|
||||
} else {
|
||||
self.image_pixmap(f.doc, &im.key, outline, !mips).map(|(key, pm)| {
|
||||
let (key, pm) = if outline { (key, pm) } else { ink::ink_image(&key, &pm, f.ink, f.doc.images.get(&im.key), !mips) };
|
||||
if mips { image_mip(&key, pm, unit) } else { pm }
|
||||
})
|
||||
};
|
||||
if let Some(pm) = shown {
|
||||
let sx = im.width as f64 / pm.width().max(1) as f64;
|
||||
let sy = im.height as f64 / pm.height().max(1) as f64;
|
||||
let quality = if f.opts.smooth_images { peniko::ImageQuality::Medium } else { peniko::ImageQuality::Low };
|
||||
@@ -2021,6 +2030,36 @@ impl Renderer {
|
||||
let pm = images::store().insert(key.clone(), Arc::new(pm));
|
||||
Some((key, pm))
|
||||
}
|
||||
|
||||
/// The key [`Self::image_pixmap`] gives the pixels of image `key` and their size, without
|
||||
/// making or looking at them: `None` until the image has been decoded.
|
||||
fn image_key(&self, doc: &Document, key: &str, grey: bool) -> Option<(images::ImageKey, (u16, u16))> {
|
||||
let (key, size) = match doc.images.get(key) {
|
||||
Some(blob) => {
|
||||
let (key, _, size) = blob_key(blob);
|
||||
(key, size?)
|
||||
}
|
||||
None => {
|
||||
let (src, hash, _) = self.adjusts.get(key)?;
|
||||
let (src_key, size) = self.image_key(doc, src, false)?;
|
||||
(images::ImageKey { adjust: Some(*hash), ..src_key }, size)
|
||||
}
|
||||
};
|
||||
Some((if grey { images::ImageKey { grey: true, ..key } } else { key }, size))
|
||||
}
|
||||
|
||||
/// The mip [`image_mip`] draws image `key` (in greyscale with `grey`, the outline view's) with
|
||||
/// through `m` when the store holds it, marked used there; `None` when it draws the full pixels
|
||||
/// or the mip is yet to be made.
|
||||
fn kept_mip(&self, f: &Frame, key: &str, grey: bool, m: Affine) -> Option<Arc<Pixmap>> {
|
||||
let (key, (w, h)) = self.image_key(f.doc, key, grey)?;
|
||||
let key = if grey { key } else { ink::ink_key(&key, f.ink) };
|
||||
let level = mip_level(m, w, h);
|
||||
if level == 0 {
|
||||
return None;
|
||||
}
|
||||
images::store().get(&images::ImageKey { mip: level, ..key })
|
||||
}
|
||||
}
|
||||
|
||||
/// `key`'s pixels in the shared image store; `touch` marks them used.
|
||||
@@ -2029,31 +2068,49 @@ fn lookup(key: &images::ImageKey, touch: bool) -> Option<Arc<Pixmap>> {
|
||||
if touch { s.get(key) } else { s.peek(key) }
|
||||
}
|
||||
|
||||
/// The decoded pixels of `blob` (a CMYK image's inks shown through the working CMYK profile, again
|
||||
/// when the colour settings change), from the shared store, and their key there.
|
||||
fn blob_pixmap(blob: &vectorcraft_doc::ImageBlob, touch: bool) -> Option<(images::ImageKey, Arc<Pixmap>)> {
|
||||
/// The key of `blob`'s decoded pixels in the shared store, the display table a CMYK image's inks
|
||||
/// show through (its generation is in the key), and the pixels' size once decoded.
|
||||
fn blob_key(blob: &vectorcraft_doc::ImageBlob) -> (images::ImageKey, Option<Arc<ink::InkLut>>, Option<(u16, u16)>) {
|
||||
let id = images::blob_id(blob);
|
||||
let lut = id.cmyk.then(ink::display_lut);
|
||||
let key =
|
||||
images::ImageKey { content: id.content, adjust: None, grey: false, ink: Ink::Display, luts: (lut.as_ref().map_or(0, |l| l.1), 0), mip: 0 };
|
||||
if let Some(pm) = lookup(&key, touch) {
|
||||
return Some((key, pm));
|
||||
}
|
||||
// Decoded outside the lock: other renderers carry on meanwhile.
|
||||
let pm = match lut.and_then(|(lut, _)| ink::cmyk_pixmap(blob, &lut)) {
|
||||
(key, lut.map(|l| l.0), id.size)
|
||||
}
|
||||
|
||||
/// The decoded pixels of `blob` (a CMYK image's inks shown through the working CMYK profile, again
|
||||
/// when the colour settings change), from the shared store, and their key there.
|
||||
fn blob_pixmap(blob: &vectorcraft_doc::ImageBlob, touch: bool) -> Option<(images::ImageKey, Arc<Pixmap>)> {
|
||||
let (key, lut, size) = blob_key(blob);
|
||||
let pm = match lookup(&key, touch) {
|
||||
Some(pm) => pm,
|
||||
None => paint::decode_pixmap(&blob.bytes)?,
|
||||
None => {
|
||||
// Decoded outside the lock: other renderers carry on meanwhile.
|
||||
let pm = match lut.and_then(|lut| ink::cmyk_pixmap(blob, &lut)) {
|
||||
Some(pm) => pm,
|
||||
None => paint::decode_pixmap(&blob.bytes)?,
|
||||
};
|
||||
images::store().insert(key.clone(), Arc::new(pm))
|
||||
}
|
||||
};
|
||||
let pm = images::store().insert(key.clone(), Arc::new(pm));
|
||||
let decoded = (pm.width(), pm.height());
|
||||
if size != Some(decoded) {
|
||||
images::remember_size(blob, decoded);
|
||||
}
|
||||
Some((key, pm))
|
||||
}
|
||||
|
||||
/// How many times to halve image pixels of `w`×`h` drawn through `m` (unit square → device
|
||||
/// pixels): [`images::mip_level`].
|
||||
fn mip_level(m: Affine, w: u16, h: u16) -> u8 {
|
||||
images::mip_level(m * Affine::scale_non_uniform(1.0 / w.max(1) as f64, 1.0 / h.max(1) as f64), w, h)
|
||||
}
|
||||
|
||||
/// Image pixels `pm` (stored as `key`) to draw through `m` (unit square → device pixels), shrunk
|
||||
/// by halves while still at least as fine as the device pixels ([`images::mip_level`]); the
|
||||
/// pixels drawn are marked used in the store.
|
||||
fn image_mip(key: &images::ImageKey, pm: Arc<Pixmap>, m: Affine) -> Arc<Pixmap> {
|
||||
let (w, h) = (pm.width(), pm.height());
|
||||
let level = images::mip_level(m * Affine::scale_non_uniform(1.0 / w.max(1) as f64, 1.0 / h.max(1) as f64), w, h);
|
||||
let level = mip_level(m, pm.width(), pm.height());
|
||||
let mut s = images::store();
|
||||
if level == 0 {
|
||||
let _ = s.get(key); // Marks the full pixels used (they were only looked at).
|
||||
|
||||
Reference in New Issue
Block a user