mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-26 10:47:11 -06:00
Merge pull request #3934 from dotCooCoo/feat/live-object-cache-checked-before-resource-walk
Render: an unchanged live object or effect art is not walked again for its resources
This commit is contained in:
+10
-1
@@ -1044,7 +1044,8 @@ impl Inputs {
|
||||
/// pixels, the address of the color settings, and whether the patterns' and symbols' art has a
|
||||
/// plug-in effect. Art, pixels and previews are hashed by the addresses of their shared values,
|
||||
/// and names, keys, tiles, tile types and overlaps are hashed by value. The live cache keeps these
|
||||
/// inputs with each evaluated live object and with each mesh's quads (`crate::live`), and
|
||||
/// inputs with each evaluated live object, with each mesh's quads and with what each walk of a
|
||||
/// live object or of effect art found when it found no placed file (`crate::live`), and
|
||||
/// [`Renderer::fx_art`] keeps them with art that can be made from evaluated live objects.
|
||||
pub(crate) struct DocInputs {
|
||||
patterns: u64,
|
||||
@@ -1060,6 +1061,14 @@ pub(crate) struct DocInputs {
|
||||
_held: Held,
|
||||
}
|
||||
|
||||
impl DocInputs {
|
||||
/// Whether `self` and `other` were made from the same patterns and symbols: their names,
|
||||
/// tilings and art.
|
||||
pub(crate) fn same_art(&self, other: &DocInputs) -> bool {
|
||||
(self.patterns, self.symbols) == (other.patterns, other.symbols)
|
||||
}
|
||||
}
|
||||
|
||||
/// The pattern and symbol art, the image pixels and the color settings, held weakly.
|
||||
type Held = (Vec<Weak<Node>>, Vec<Weak<Vec<u8>>>, Weak<Cms>);
|
||||
|
||||
|
||||
+82
-18
@@ -6,7 +6,11 @@
|
||||
//! the symbols, patterns and placed files it shows, the fonts for type, the plug-ins for plug-in
|
||||
//! effects, and the color settings (so the steps keep
|
||||
//! stable `Arc`s and hit the geometry cache frame after frame, and editing a symbol redraws an
|
||||
//! envelope around an instance of it). Gradient meshes are tessellated into many small solid-colour quads (vello
|
||||
//! envelope around an instance of it). What the walk behind that key found is kept with the node
|
||||
//! and with the document's symbols and patterns as the walk read them ([`Walk`]). A later frame
|
||||
//! makes the key from what was kept while the node and the document's symbols and patterns stay
|
||||
//! the same. A walk that found a placed file is not kept.
|
||||
//! Gradient meshes are tessellated into many small solid-colour quads (vello
|
||||
//! has no mesh shading), with the density chosen from the on-screen patch size; each quad is
|
||||
//! grown by half a device pixel along its edges so neighbours overlap and no antialiasing seams
|
||||
//! show. The quads are made again when the color settings change.
|
||||
@@ -33,16 +37,25 @@ type Quads = Arc<Vec<MeshQuad>>;
|
||||
pub(crate) type Found = (u64, Arc<DocInputs>, Vec<Weak<ReadDocument>>);
|
||||
/// A mesh node, its quads, and the [`DocInputs`] whose color settings colored them.
|
||||
type MeshEntry = (Arc<Node>, Quads, Arc<DocInputs>);
|
||||
/// A node, held weakly (the reference keeps its allocation, so no other node gets its address),
|
||||
/// what the walk of [`resources_key`] found in it, and the frame's [`DocInputs`] the walk read
|
||||
/// the symbols and patterns of.
|
||||
type Walk = (Weak<Node>, Walked, Arc<DocInputs>);
|
||||
|
||||
/// Per-renderer cache of evaluated live objects: (live node, its evaluation, [`Found`]), and a
|
||||
/// [`MeshEntry`] per mesh node and subdivision level.
|
||||
/// Per-renderer cache of evaluated live objects: (live node, its evaluation, [`Found`]), a
|
||||
/// [`MeshEntry`] per mesh node and subdivision level, and a [`Walk`] per node whose key
|
||||
/// [`Renderer::live_found`] found without finding a placed file.
|
||||
pub(crate) struct LiveCache {
|
||||
expanded: FrameCache<usize, (Arc<Node>, Expanded, Found)>,
|
||||
meshes: FrameCache<(usize, usize), MeshEntry>,
|
||||
walks: FrameCache<usize, Walk>,
|
||||
/// How many live objects [`Renderer::live_expanded`] evaluated and how many meshes
|
||||
/// [`Renderer::mesh_quads`] tessellated.
|
||||
#[cfg(test)]
|
||||
pub(crate) evaluated: usize,
|
||||
/// How many walks [`Renderer::live_found`] made.
|
||||
#[cfg(test)]
|
||||
pub(crate) walked: usize,
|
||||
}
|
||||
|
||||
impl Default for LiveCache {
|
||||
@@ -50,8 +63,11 @@ impl Default for LiveCache {
|
||||
Self {
|
||||
expanded: FrameCache::new(lru::LIVE_BUDGET, 256),
|
||||
meshes: FrameCache::new(lru::MESH_BUDGET, 256),
|
||||
walks: FrameCache::new(lru::WALK_BUDGET, 256),
|
||||
#[cfg(test)]
|
||||
evaluated: 0,
|
||||
#[cfg(test)]
|
||||
walked: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,6 +77,7 @@ impl LiveCache {
|
||||
pub(crate) fn sweep(&mut self, stamp: u64) {
|
||||
self.expanded.sweep(stamp);
|
||||
self.meshes.sweep(stamp);
|
||||
self.walks.sweep(stamp);
|
||||
}
|
||||
|
||||
/// A cache whose evaluations take at most `budget` bytes.
|
||||
@@ -85,19 +102,41 @@ pub fn expand_live(n: &Node) -> Vec<Node> {
|
||||
/// plug-in revision ([`crate::effects::plugin_revision`]) when it shows an object with a plug-in
|
||||
/// effect, and the color settings (`cms`, their address). Art is hashed by the addresses of its
|
||||
/// `Arc`s, which copies of the document share: a copy of the document has the same key. It also
|
||||
/// returns weak references to the reads found.
|
||||
fn resources_key(doc: &Document, cms: usize, n: &Node) -> (u64, Vec<Weak<ReadDocument>>) {
|
||||
/// returns weak references to the reads found, and what the walk found ([`Walked`]) when it found
|
||||
/// no placed file.
|
||||
fn resources_key(doc: &Document, cms: usize, n: &Node) -> (u64, Vec<Weak<ReadDocument>>, Option<Walked>) {
|
||||
let mut h = DefaultHasher::new();
|
||||
let mut shown = Shown::default();
|
||||
hash_resources(doc, n, &mut shown, &mut h);
|
||||
if shown.text {
|
||||
vectorcraft_text::FontDb::global().generation().hash(&mut h);
|
||||
let walked = Walked { hash: h.finish(), text: shown.text, plugins: shown.plugins };
|
||||
(walked.key(cms), shown.reads, (!shown.placed).then_some(walked))
|
||||
}
|
||||
|
||||
/// What [`hash_resources`] found in a node: the hash of what it walked, whether it found type or
|
||||
/// a placed file, and whether it found an object with a visible plug-in effect.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Walked {
|
||||
hash: u64,
|
||||
text: bool,
|
||||
plugins: bool,
|
||||
}
|
||||
|
||||
impl Walked {
|
||||
/// The [`resources_key`] of the node walked: the walk's hash, the font generation when it found
|
||||
/// type or a placed file, the plug-in revision when it found a plug-in effect, and the color
|
||||
/// settings `cms`.
|
||||
fn key(self, cms: usize) -> u64 {
|
||||
let mut h = DefaultHasher::new();
|
||||
self.hash.hash(&mut h);
|
||||
if self.text {
|
||||
vectorcraft_text::FontDb::global().generation().hash(&mut h);
|
||||
}
|
||||
if self.plugins {
|
||||
crate::effects::plugin_revision().hash(&mut h);
|
||||
}
|
||||
cms.hash(&mut h);
|
||||
h.finish()
|
||||
}
|
||||
if shown.plugins {
|
||||
crate::effects::plugin_revision().hash(&mut h);
|
||||
}
|
||||
cms.hash(&mut h);
|
||||
(h.finish(), shown.reads)
|
||||
}
|
||||
|
||||
/// What [`hash_resources`] has found so far.
|
||||
@@ -107,6 +146,8 @@ struct Shown {
|
||||
seen: Vec<usize>,
|
||||
/// Whether type or a placed file was found.
|
||||
text: bool,
|
||||
/// Whether a placed file was found.
|
||||
placed: bool,
|
||||
/// Whether an object with a visible plug-in effect was found.
|
||||
plugins: bool,
|
||||
/// Weak references to the reads of the placed files found.
|
||||
@@ -154,6 +195,7 @@ fn hash_resources<'a>(doc: &'a Document, n: &'a Node, shown: &mut Shown, h: &mut
|
||||
read.as_ref().map(|r| Arc::as_ptr(r) as usize).hash(h);
|
||||
shown.reads.extend(read.as_ref().map(Arc::downgrade));
|
||||
shown.text = true;
|
||||
shown.placed = true;
|
||||
}
|
||||
NodeKind::Text(_) => shown.text = true,
|
||||
_ => {}
|
||||
@@ -392,17 +434,39 @@ fn spread_to_edges(px: &mut [vello_cpu::color::PremulRgba8], filled: Vec<bool>,
|
||||
impl Renderer {
|
||||
/// The [`resources_key`] of `n` in `doc`, with the frame's [`DocInputs`] ([`Found`]): `n` is a
|
||||
/// live node, or an object with effects whose art ([`Renderer::fx_art`]) can be made from
|
||||
/// evaluated live objects, type, symbol art or colors shaded through the color settings.
|
||||
pub(crate) fn live_found(&mut self, doc: &Document, n: &Node) -> Found {
|
||||
/// evaluated live objects, type, symbol art or colors shaded through the color settings. The
|
||||
/// key is made from the [`Walk`] kept for `n` while `doc`'s symbols and patterns are those it
|
||||
/// read; `n` is walked again when they changed, and in every frame when the walk found a
|
||||
/// placed file.
|
||||
pub(crate) fn live_found(&mut self, doc: &Document, n: &Arc<Node>) -> Found {
|
||||
let inputs = self.doc_inputs(doc);
|
||||
let (key, reads) = resources_key(doc, inputs.cms, n);
|
||||
let at = Arc::as_ptr(n) as usize;
|
||||
if let Some((node, walked, read)) = self.live.walks.get(&at, self.stamp)
|
||||
&& std::ptr::eq(node.as_ptr(), Arc::as_ptr(n))
|
||||
&& read.same_art(&inputs)
|
||||
{
|
||||
return (walked.key(inputs.cms), inputs, vec![]);
|
||||
}
|
||||
#[cfg(test)]
|
||||
{
|
||||
self.live.walked += 1;
|
||||
}
|
||||
let (key, reads, walked) = resources_key(doc, inputs.cms, n);
|
||||
if let Some(walked) = walked {
|
||||
// The bytes count the key and the entry, and the node's allocation that the weak
|
||||
// reference keeps.
|
||||
let bytes = size_of::<(usize, Walk, Node)>();
|
||||
self.live.walks.insert(at, (Arc::downgrade(n), walked, inputs.clone()), bytes, self.stamp);
|
||||
}
|
||||
(key, inputs, reads)
|
||||
}
|
||||
|
||||
fn live_expanded(&mut self, doc: &Document, a: &Arc<Node>, cache: bool) -> Expanded {
|
||||
let key = Arc::as_ptr(a) as usize;
|
||||
let resources = self.live_found(doc, a);
|
||||
if cache
|
||||
// An evaluation the cache does not keep is made without the key, so the copy that
|
||||
// `draw_live_node` makes is not walked.
|
||||
let resources = cache.then(|| self.live_found(doc, a));
|
||||
if let Some(resources) = &resources
|
||||
&& let Some(e) = self.live.expanded.get(&key, self.stamp)
|
||||
&& Arc::ptr_eq(&e.0, a)
|
||||
&& e.2.0 == resources.0
|
||||
@@ -417,7 +481,7 @@ impl Renderer {
|
||||
Some(children) => Arc::new(children),
|
||||
None => Arc::new(crate::effects::expand_live(Some(doc), a).into_iter().map(Arc::new).collect()),
|
||||
};
|
||||
if cache {
|
||||
if let Some(resources) = resources {
|
||||
let bytes = size_of::<Expanded>() + v.iter().map(|n| lru::node_bytes(n)).sum::<usize>();
|
||||
self.live.expanded.insert(key, (a.clone(), v.clone(), resources), bytes, self.stamp);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ pub(crate) const PLACED_ART_BUDGET: usize = 64 << 20;
|
||||
pub(crate) const BRUSH_BUDGET: usize = 64 << 20;
|
||||
/// Evaluated blends, envelopes and repeats (`Renderer::live_expanded`).
|
||||
pub(crate) const LIVE_BUDGET: usize = 64 << 20;
|
||||
/// What the walks of live objects and effect art found (`Renderer::live_found`).
|
||||
pub(crate) const WALK_BUDGET: usize = 16 << 20;
|
||||
/// Tessellated gradient meshes (`Renderer::mesh_quads`).
|
||||
pub(crate) const MESH_BUDGET: usize = 32 << 20;
|
||||
/// Pattern tile rasters, per thread and ink plane (`pattern::set_pattern_paint`): a tile is at
|
||||
|
||||
@@ -1296,6 +1296,95 @@ fn live_objects_draw_symbol_and_pattern_art_edits() {
|
||||
edit_draws_as_new(with_art(d, vec![envelope]), red_bar);
|
||||
}
|
||||
|
||||
/// Symbol `S` ([`symbol_art`]) at twice its size: bent by an envelope on the page at (50, 50), as
|
||||
/// the opacity mask of a black square at (350, 50) and in a clip group at (450, 50); not bent, in a
|
||||
/// group with a drop shadow at (150, 50); and an instance at (250, 50), at twice its size, of
|
||||
/// symbol `Bent`, whose art is an envelope bending a copy of `S`'s art.
|
||||
fn symbol_in_live_and_effect_art() -> Document {
|
||||
use vectorcraft_geom::shapes::rectangle;
|
||||
let mut d = Document::new(500.0, 100.0);
|
||||
let art = symbol_art(&mut d);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "S".into(), art: Arc::new(art) });
|
||||
let bent = |d: &mut Document, x: f64| {
|
||||
let xf = Affine::translate((x, 50.0)) * Affine::scale(2.0);
|
||||
let s = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf });
|
||||
arc_envelope(d, vec![s], false)
|
||||
};
|
||||
let envelope = bent(&mut d, 50.0);
|
||||
let xf = Affine::translate((150.0, 50.0)) * Affine::scale(2.0);
|
||||
let shadowed = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "S".into(), xf });
|
||||
let mut group = Node::group(d.alloc_id(), vec![Arc::new(shadowed)]);
|
||||
group.appearance.effects.push(drop_shadow());
|
||||
let copy = symbol_art(&mut d);
|
||||
let art = arc_envelope(&mut d, vec![copy], false);
|
||||
d.symbols.push(vectorcraft_doc::Symbol { name: "Bent".into(), art: Arc::new(art) });
|
||||
let xf = Affine::translate((250.0, 50.0)) * Affine::scale(2.0);
|
||||
let symbol = Node::new(d.alloc_id(), NodeKind::SymbolInstance { symbol: "Bent".into(), xf });
|
||||
let mask = bent(&mut d, 350.0);
|
||||
let mut masked = filled(&mut d, rectangle(Rect::new(310.0, 10.0, 390.0, 90.0)), Paint::solid(Color::BLACK), vec![]);
|
||||
masked.extras_mut().mask = Some(Box::new(vectorcraft_doc::OpacityMask::new(mask, true)));
|
||||
let clip = filled(&mut d, rectangle(Rect::new(410.0, 10.0, 490.0, 90.0)), Paint::None, vec![]);
|
||||
let member = bent(&mut d, 450.0);
|
||||
let clipped = Node::new(d.alloc_id(), NodeKind::Group { children: vec![Arc::new(clip), Arc::new(member)], clip: true });
|
||||
with_art(d, vec![envelope, group, symbol, masked, clipped])
|
||||
}
|
||||
|
||||
/// A renderer draws [`symbol_in_live_and_effect_art`] a second time, in Preview and in Outline
|
||||
/// view, from what it kept, without walking any of its envelopes or its group with a drop shadow
|
||||
/// again.
|
||||
#[test]
|
||||
fn unchanged_live_objects_and_effect_art_are_not_walked_again() {
|
||||
let _fonts = fonts_steady();
|
||||
let d = symbol_in_live_and_effect_art();
|
||||
let preview = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
let outline = RenderOptions { outline: true, ..preview.clone() };
|
||||
for (view, opts) in [("Preview", &preview), ("Outline", &outline)] {
|
||||
let mut r = Renderer::new();
|
||||
let first = r.render(&d, 500, 100, Affine::IDENTITY, opts).pixels;
|
||||
let walked = r.live.walked;
|
||||
assert!(walked >= 3, "{view}: the first frame walks the envelopes: {walked}");
|
||||
let second = r.render(&d, 500, 100, Affine::IDENTITY, opts).pixels;
|
||||
assert_eq!(r.live.walked, walked, "{view}: the second frame walks nothing again");
|
||||
assert!(second == first, "{view}: the second frame draws the same");
|
||||
}
|
||||
}
|
||||
|
||||
/// A thumbnail of an envelope around a symbol instance evaluates a copy of the envelope that the
|
||||
/// live cache does not keep, without walking it for the symbols it shows.
|
||||
#[test]
|
||||
fn live_objects_evaluated_without_the_cache_are_not_walked() {
|
||||
let _fonts = fonts_steady();
|
||||
let d = symbol_in_live_and_effect_art();
|
||||
let envelope = d.layers[0].children().unwrap()[0].clone();
|
||||
let mut r = Renderer::new();
|
||||
r.render_node_thumbnail(&d, &envelope, 32, None).unwrap();
|
||||
assert!(r.live.evaluated >= 1, "the thumbnail evaluates the envelope");
|
||||
assert_eq!(r.live.walked, 0, "the thumbnail walks nothing");
|
||||
}
|
||||
|
||||
/// After two frames of [`symbol_in_live_and_effect_art`], the second drawn from what the renderer
|
||||
/// kept, an edit of `S`'s art draws in Preview and in Outline view as a new renderer draws it.
|
||||
#[test]
|
||||
fn live_objects_and_effect_art_kept_without_a_walk_draw_symbol_edits() {
|
||||
let _fonts = fonts_steady();
|
||||
let preview = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() };
|
||||
let outline = RenderOptions { outline: true, ..preview.clone() };
|
||||
for (view, opts) in [("Preview", &preview), ("Outline", &outline)] {
|
||||
let mut d = symbol_in_live_and_effect_art();
|
||||
let mut r = Renderer::new();
|
||||
r.render(&d, 500, 100, Affine::IDENTITY, opts);
|
||||
let before = r.render(&d, 500, 100, Affine::IDENTITY, opts).pixels;
|
||||
// `S` becomes a wide red bar.
|
||||
let bar = vectorcraft_geom::shapes::rectangle(Rect::new(-20.0, -4.0, 20.0, 4.0));
|
||||
let red = vectorcraft_doc::Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||||
d.symbols[0].art = Arc::new(Node::path(d.alloc_id(), bar, red));
|
||||
let after = r.render(&d, 500, 100, Affine::IDENTITY, opts).pixels;
|
||||
assert!(after != before, "{view}: the edit changes the frame");
|
||||
let fresh = Renderer::new().render(&d, 500, 100, Affine::IDENTITY, opts).pixels;
|
||||
assert!(after == fresh, "{view}: a new renderer draws the same");
|
||||
}
|
||||
}
|
||||
|
||||
/// Installs live-effect plug-in `id`, replacing one with the same id: its effect draws `shape` in
|
||||
/// place of the geometry it is given.
|
||||
fn install_shape(id: &str, shape: &vectorcraft_geom::PathData) {
|
||||
|
||||
@@ -993,6 +993,8 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
|
||||
- **Live objects in symbols and masks:** symbol instances and inline graphics whose symbol is a blend, an envelope, a repeat, an Intertwine object or a gradient mesh, or a group that holds one, draw the evaluation the renderer keeps for that object between frames, as the same object on the page does. So do opacity masks whose art is such an object, and such objects in clip groups in Outline view. The object is evaluated again when the symbols, patterns, fonts or color settings it shows change. Symbol art with effects of its own, or that holds a symbol instance, an image, a placed file, type with an inline graphic or a raster effect, is still placed and evaluated again in every frame. At 1000 × 1000 pixels, a page of 2,000 instances of a symbol that holds an envelope takes 28 ms a frame instead of 126 ms, with the same pixels.
|
||||
|
||||
- **Live objects in documents with many symbols:** a live object, or an object with effects, that shows symbols or patterns is not walked again in a later frame to find them while the document's symbols and patterns stay the same. One that shows a placed file is walked in every frame. At 1000 × 1000 pixels, a page of 2,000 envelopes around instances of the last 2,000 of 10,000 symbols takes 30 ms a frame, and 83 ms when every frame walks them again, with the same pixels.
|
||||
|
||||
- **Type drawn as glyph outlines:** under a geometry effect, in EMF and WMF export, in DXF export when it is written as outlines, flattened with Convert All Text to Outlines, and inside an envelope, type keeps its underline and strikethrough, the weight and dashes of its character strokes when it is scaled, and the placement of its gradients and patterns, as the canvas draws it. A group's or layer's own fills and strokes also paint the underline and strikethrough of the type in it.
|
||||
|
||||
- **Texture redraws (#1257):** Craquelure, Mosaic Tiles and Stained Glass compare squared distances when finding their nearest cells; rounding/tie boundaries use the original distance ordering. Brute-force candidate tests and three 1800×1500 viewport comparisons preserve the old pixels. A CMYK rectangle move at 190.07%/DPI 1.5 is reproducible with `cargo test -p vectorcraft-render texture_drag_perf -- --ignored --nocapture`; idle-machine Windows interaction is still unverified.
|
||||
|
||||
Reference in New Issue
Block a user