diff --git a/crates/render/src/fx/pixels.rs b/crates/render/src/fx/pixels.rs index 972d3e4f04..0fd0720e7e 100644 --- a/crates/render/src/fx/pixels.rs +++ b/crates/render/src/fx/pixels.rs @@ -449,8 +449,9 @@ impl Renderer { /// pixels of a frame through the same view ([`PixelEntry::shown`]) is drawn instead, made again /// for a frame it does not serve. A copy that alone exceeds [`FX_RASTER_BUDGET`] is not made; /// the raster draws scaled. When the cache exceeds its budget, [`Self::trim_fx_rasters`] - /// replaces whole rasters with their view copies before evicting entries. A raster that covers - /// none of `screen`'s pixels draws scaled. + /// replaces a frame's whole rasters with their view copies before it evicts that frame's + /// entries, least recently drawn frames first. + /// A raster that covers none of `screen`'s pixels draws scaled. fn draw_kept( &mut self, ctx: &mut RenderContext, @@ -593,10 +594,11 @@ impl Renderer { } /// The inputs a raster of `n`'s content, which reads `reads`, is drawn with in frame `f`: the - /// color settings, the edge smoothing mode, the hidden objects, whether guides and template - /// layers are left out, the guide color and style, whether Layer Options show on screen, - /// whether dashed strokes draw as solid lines, the document's raster effects resolution (which - /// a Rasterize or De-Interlace anywhere in the content follows), the knockout `n` inherits and, + /// color settings, the render mode ([`crate::RenderOptions::precise`]), the edge smoothing mode, + /// the hidden objects, whether guides and template layers are left out, the guide color and + /// style, whether Layer Options show on screen, whether dashed strokes draw as solid lines, + /// the document's raster effects resolution (which a Rasterize or De-Interlace anywhere in the + /// content follows), the knockout `n` inherits and, /// while `n` holds the isolated group, isolation mode; the pattern definitions, symbols and /// brush library it reads; for images and placed documents their pixels and files, the /// layer's Dim Images and the options they are drawn with (with @@ -613,7 +615,7 @@ impl Renderer { pub(crate) fn raster_inputs(&mut self, f: &Frame, n: &Node, reads: Reads) -> Inputs { let doc = self.doc_inputs(f.doc); let mut h = DefaultHasher::new(); - (doc.cms, std::mem::discriminant(&f.opts.anti_alias), self.knockout, f.doc.raster_effects_ppi.to_bits()).hash(&mut h); + (doc.cms, f.opts.precise, std::mem::discriminant(&f.opts.anti_alias), self.knockout, f.doc.raster_effects_ppi.to_bits()).hash(&mut h); // Cached effects on containers can include paths made into guides, drawn in the guide // color and style, template layers, and sublayers drawn as their Layer Options show them. let o = f.opts; @@ -717,46 +719,46 @@ impl Renderer { d } - /// Replace whole rasters that have view copies with those copies, least recently drawn first, - /// then evict the least recently drawn shadow and filtered-content entries until they fit - /// `budget` bytes ([`FX_RASTER_BUDGET`]). + /// Trim the shadow and filtered-content entries to `budget` bytes ([`FX_RASTER_BUDGET`]), one + /// frame stamp at a time from the least recently drawn: replace that stamp's whole rasters that + /// have view copies with those copies, then, while the entries still exceed the budget, evict + /// that stamp's entries, reduced ones included. pub(super) fn trim_fx_rasters(&mut self, budget: usize) { let mut total: usize = self.shadows.values().map(|e| e.bytes()).sum::() + self.pixel_fx.values().map(|e| e.bytes()).sum::(); if total <= budget { return; } - let mut copies: Vec<_> = self.pixel_fx.values_mut().filter(|e| e.pan.is_some() && e.shown.is_some()).collect(); - copies.sort_unstable_by_key(|e| e.stamp); - for e in copies { - if total <= budget { - break; - } - if let Some((view, copy, rel)) = e.shown.take() { - total -= raster_bytes(&e.image); - e.image = copy; - e.k = 1.0; - e.rel = rel; - e.view = Some(view); - e.pan = None; - } - } - if total <= budget { - return; - } - let shadows = self.shadows.iter().map(|(k, e)| (e.stamp, false, *k, e.bytes())); - let pixels = self.pixel_fx.iter().map(|(k, e)| (e.stamp, true, *k, e.bytes())); + let shadows = self.shadows.iter().map(|(k, e)| (e.stamp, false, *k)); + let pixels = self.pixel_fx.iter().map(|(k, e)| (e.stamp, true, *k)); let mut all: Vec<_> = shadows.chain(pixels).collect(); all.sort_unstable_by_key(|e| e.0); - for (_, pixel, key, bytes) in all { - if total <= budget { - break; + for group in all.chunk_by(|a, b| a.0 == b.0) { + for &(_, pixel, key) in group { + if total <= budget { + return; + } + if pixel + && let Some(e) = self.pixel_fx.get_mut(&key).filter(|e| e.pan.is_some()) + && let Some((view, copy, rel)) = e.shown.take() + { + total -= raster_bytes(&e.image); + e.image = copy; + e.k = 1.0; + e.rel = rel; + e.view = Some(view); + e.pan = None; + } } - if pixel { - self.pixel_fx.remove(&key); - } else { - self.shadows.remove(&key); + for &(_, pixel, key) in group { + if total <= budget { + return; + } + if pixel { + total -= self.pixel_fx.remove(&key).map_or(0, |e| e.bytes()); + } else { + total -= self.shadows.remove(&key).map_or(0, |e| e.bytes()); + } } - total -= bytes; } } diff --git a/crates/render/src/fx/raster.rs b/crates/render/src/fx/raster.rs index 7cd2dc9916..89cb9046eb 100644 --- a/crates/render/src/fx/raster.rs +++ b/crates/render/src/fx/raster.rs @@ -112,7 +112,8 @@ pub(crate) struct PixelEntry { /// the view and rect they were made for, and their top-left against the anchor's view position: /// a frame they serve copies them ([`Renderer::draw_kept`]). Copies that alone exceed the cache /// budget are not made. When the cache exceeds its budget, [`Renderer::trim_fx_rasters`] - /// replaces whole rasters with their view copies before evicting entries. + /// goes through the frames least recently drawn first, and replaces a frame's whole rasters + /// with their view copies before it evicts that frame's entries. pub(super) shown: Option<(ViewCrop, std::sync::Arc, (f64, f64))>, pub(super) made: Made, /// What the content read when the raster was made, with the channels the effects ran on and, diff --git a/crates/render/src/fx/tests.rs b/crates/render/src/fx/tests.rs index 0b1da35bd7..1913b30b4a 100644 --- a/crates/render/src/fx/tests.rs +++ b/crates/render/src/fx/tests.rs @@ -2127,6 +2127,24 @@ fn guide_style_and_layer_options_filter_the_whole_object_raster_again_after_a_pa } } +/// Switching the render mode ([`crate::RenderOptions::precise`]) filters the whole-object raster +/// again after a pan by whole pixels. +#[test] +fn render_mode_filters_the_whole_object_raster_again_after_a_pan() { + let d = doc_with(Rect::new(100.0, 100.0, 300.0, 250.0), Color::rgb(0.9, 0.4, 0.1), vec![("sketch.tornEdges", json!({}))]); + let opts = RenderOptions { precise: false, background: Some([255, 255, 255, 255]), ..Default::default() }; + let precise = RenderOptions { precise: true, ..opts.clone() }; + let view = |dx: f64| Affine::translate((640.0 + dx, 400.0)) * Affine::scale(64.0) * Affine::translate((-295.0, -175.0)); + let mut r = Renderer::new(); + let _ = r.render(&d, 1280, 800, view(0.0), &opts); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.k < 1.0 && kept.pan.is_some(), "a raster of the whole object at a lower resolution"); + let image = kept.image.clone(); + let pixels = r.render(&d, 1280, 800, view(40.0), &precise).pixels; + assert!(!r.pixel_fx.values().any(|e| Arc::ptr_eq(&e.image, &image)), "the precise render mode filters the content again"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(40.0), &precise).pixels, "in the precise render mode"); +} + /// Art and Type edge smoothing share a coverage threshold, but Type snaps glyphs to the pixel /// grid: switching between them filters the whole-object raster of a group with type again after /// a pan by whole pixels. @@ -2189,6 +2207,103 @@ fn a_lower_resolution_whole_raster_keeps_its_view_copy_under_the_cache_budget() assert!(pixels == Renderer::new().render(&d, 1280, 800, view, &opts).pixels, "the copy draws the same pixels as a new renderer"); } +/// An older shadow gives way before the newest whole-object raster loses its whole image. +#[test] +fn an_older_shadow_is_evicted_before_the_newest_whole_raster_loses_its_whole_image() { + let shadow_fx = ("stylize.dropShadow", json!({"x": 4, "y": 3, "blur": 2, "opacity": 80})); + let shadow = doc_with(Rect::new(20.0, 20.0, 40.0, 40.0), Color::BLACK, vec![shadow_fx]); + let d = doc_with(Rect::new(100.0, 100.0, 300.0, 250.0), Color::rgb(0.9, 0.4, 0.1), vec![("sketch.tornEdges", json!({}))]); + let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() }; + let view = Affine::translate((640.0, 400.0)) * Affine::scale(64.0) * Affine::translate((-295.0, -175.0)); + let mut r = Renderer::new(); + let _ = r.render(&shadow, 200, 200, Affine::scale(2.0), &opts); + assert_eq!(r.shadows.len(), 1, "the first frame keeps a shadow"); + let _ = r.render(&d, 1280, 800, view, &opts); + assert_eq!(r.pixel_fx.len(), 1); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.pan.is_some() && kept.k < 1.0 && kept.shown.is_some(), "the whole raster holds its view copy"); + assert!(r.shadows.values().all(|e| e.stamp < kept.stamp), "the shadow is older than the whole raster"); + let image = kept.image.clone(); + let budget = kept.bytes(); + assert!(r.shadows.values().all(|e| e.bytes() > 0), "the shadow and the whole raster together exceed the budget"); + r.trim_fx_rasters(budget); + assert!(r.shadows.is_empty(), "the older shadow is evicted first"); + assert_eq!(r.pixel_fx.len(), 1); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.pan.is_some() && kept.view.is_none(), "the newest raster stays whole"); + assert!(Arc::ptr_eq(&kept.image, &image), "the newest raster keeps its whole image"); + assert!(kept.shown.is_some(), "the newest raster keeps its view copy"); + assert!(kept.bytes() <= budget, "the remaining entry fits the budget"); +} + +/// An older whole raster and its view copy give way before a newer whole raster is reduced. A +/// frame's start holds the previous frame's rasters, so the test marks one of two rasters older. +#[test] +fn an_older_whole_raster_is_evicted_before_a_newer_whole_raster_is_reduced() { + let mut d = doc_with(Rect::new(100.0, 100.0, 300.0, 250.0), Color::BLACK, vec![("sketch.tornEdges", json!({}))]); + let next = doc_with(Rect::new(300.0, 100.0, 500.0, 250.0), Color::rgb(0.9, 0.4, 0.1), vec![("sketch.tornEdges", json!({}))]); + let mut n = (**next.layers[0].children().unwrap().first().unwrap()).clone(); + n.id = d.alloc_id(); + d.insert(Some(d.layers[0].id), 0, n).unwrap(); + let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() }; + let view = Affine::translate((640.0, 400.0)) * Affine::scale(64.0) * Affine::translate((-300.0, -175.0)); + let mut r = Renderer::new(); + let _ = r.render(&d, 1280, 800, view, &opts); + assert_eq!(r.pixel_fx.len(), 2, "the view shows both objects"); + assert!(r.pixel_fx.values().all(|e| e.pan.is_some() && e.shown.is_some()), "both whole rasters hold view copies"); + let older_stamp = r.stamp - 1; + if let Some(e) = r.pixel_fx.values_mut().next() { + e.stamp = older_stamp; + } + let old = r.pixel_fx.values().find(|e| e.stamp == older_stamp).unwrap(); + let kept = r.pixel_fx.values().find(|e| e.stamp > older_stamp).unwrap(); + let newest_stamp = kept.stamp; + let image = kept.image.clone(); + let copy = kept.shown.as_ref().unwrap().1.clone(); + let budget = kept.bytes(); + assert!(old.bytes() > super::entries::raster_bytes(&old.image), "reducing the older raster leaves the cache over budget"); + r.trim_fx_rasters(budget); + assert_eq!(r.pixel_fx.len(), 1, "the older entry is evicted"); + let kept = r.pixel_fx.values().next().unwrap(); + assert_eq!(kept.stamp, newest_stamp); + assert!(kept.pan.is_some() && kept.view.is_none(), "the newer raster stays whole"); + assert!(Arc::ptr_eq(&kept.image, &image), "the newer raster keeps its whole image"); + assert!(Arc::ptr_eq(&kept.shown.as_ref().unwrap().1, ©), "the newer raster keeps its view copy"); + assert!(kept.bytes() <= budget, "the remaining entry fits the budget"); +} + +/// A whole raster is reduced before a shadow drawn in the same frame is evicted. +#[test] +fn a_whole_raster_is_reduced_before_a_shadow_of_the_same_stamp_is_evicted() { + let shadow_fx = ("stylize.dropShadow", json!({"x": 0.25, "y": 0.25, "blur": 0.5, "opacity": 80})); + let shadow = doc_with(Rect::new(292.0, 174.0, 293.0, 175.0), Color::BLACK, vec![shadow_fx]); + let mut d = doc_with(Rect::new(100.0, 100.0, 300.0, 250.0), Color::rgb(0.9, 0.4, 0.1), vec![("sketch.tornEdges", json!({}))]); + let mut n = (**shadow.layers[0].children().unwrap().first().unwrap()).clone(); + n.id = d.alloc_id(); + d.insert(Some(d.layers[0].id), 0, n).unwrap(); + let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() }; + let view = Affine::translate((640.0, 400.0)) * Affine::scale(64.0) * Affine::translate((-295.0, -175.0)); + let mut r = Renderer::new(); + let _ = r.render(&d, 1280, 800, view, &opts); + assert_eq!(r.shadows.len(), 1); + assert_eq!(r.pixel_fx.len(), 1); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.pan.is_some() && kept.shown.is_some(), "the whole raster holds its view copy"); + let shadow = r.shadows.values().next().unwrap(); + assert_eq!(shadow.stamp, kept.stamp, "both effects are drawn in the same frame"); + let copy = kept.shown.as_ref().unwrap().1.clone(); + let budget = shadow.bytes() + kept.bytes() - super::entries::raster_bytes(&kept.image); + assert!(shadow.bytes() > 0, "the shadow occupies cache space"); + r.trim_fx_rasters(budget); + assert_eq!(r.shadows.len(), 1, "the shadow stays cached"); + assert_eq!(r.pixel_fx.len(), 1, "the reduced raster stays cached"); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.view.is_some() && kept.pan.is_none() && kept.shown.is_none(), "the entry holds only its view copy"); + assert_eq!(kept.k, 1.0); + assert!(Arc::ptr_eq(&kept.image, ©), "the copy replaces the whole raster"); + assert!(kept.bytes() + r.shadows.values().map(|e| e.bytes()).sum::() <= budget, "both entries fit the budget"); +} + #[test] fn a_drag_glow_fallback_cannot_be_spliced_into_a_still_frame() { let doc = doc_with(