diff --git a/crates/render/src/frame.rs b/crates/render/src/frame.rs index 49e252d206..0c58cdf1b4 100644 --- a/crates/render/src/frame.rs +++ b/crates/render/src/frame.rs @@ -1,3 +1,5 @@ +use std::hash::{DefaultHasher, Hash, Hasher}; + use super::options::{AntiAlias, RenderOptions}; use super::threads::single_threaded_context; use super::{ @@ -224,6 +226,8 @@ pub(crate) struct Frame<'a> { /// Size of one of this frame's pixels in document units. pub(crate) px: f64, pub(crate) opts: &'a RenderOptions, + /// The order-independent fingerprint of `opts.hidden`, shared by this frame's raster lookups. + pub(crate) hidden_inputs: u64, /// What colours paint as: screen colours, or one ink plane of a CMYK document (see [`ink`]). pub(crate) ink: Ink, /// Document → pixels of the output being drawn: `view` itself, except while an export draws @@ -236,6 +240,15 @@ pub(crate) struct Frame<'a> { } impl Frame<'_> { + /// Hash the hidden objects once for the options a frame draws with. + pub(crate) fn hidden_inputs(opts: &RenderOptions) -> u64 { + let mut hidden: Vec<_> = opts.hidden.iter().collect(); + hidden.sort_unstable(); + let mut h = DefaultHasher::new(); + hidden.hash(&mut h); + h.finish() + } + pub(crate) fn reject_patch(&self) -> bool { self.patch.is_some_and(|hit| { hit.set(true); diff --git a/crates/render/src/fx/pixels.rs b/crates/render/src/fx/pixels.rs index da169f4d68..7eb8f220e3 100644 --- a/crates/render/src/fx/pixels.rs +++ b/crates/render/src/fx/pixels.rs @@ -1,10 +1,10 @@ use super::bounds::{apply_carrying, item_effects, item_raster}; -use super::entries::{Content, DocInputs, Drawn, FX_RASTER_BUDGET, Inputs, ItemGeom, KeptImage, Made, Reads, feather_outline}; +use super::entries::{Content, DocInputs, Drawn, FX_RASTER_BUDGET, Inputs, ItemGeom, KeptImage, Made, Reads, feather_outline, raster_bytes}; #[cfg(test)] use super::raster::RASTERIZED; use super::raster::{ DRAG_PIXEL_BUDGET, PIXEL_BUDGET, PixelEntry, ViewCrop, content_changing, doc_raster_ppi, drag_scale, draw_placed, draw_scaled, filter_region, - filter_region_within, hard_edged, image_size, linear_key, looks_around, pan_margin, + filter_region_within, hard_edged, image_size, linear_key, looks_around, pan_margin, panned_whole, }; use super::{ Affine, AppearanceItem, Arc, BezPath, Channels, DefaultHasher, Document, FillRule, Frame, GeomContext, Hash, Hasher, Ink, Node, PixelFx, @@ -69,14 +69,18 @@ impl Renderer { /// raster holds at most [`PIXEL_BUDGET`] pixels and [`crate::MAX_RASTER_SIDE`] a side: content /// over the budget is cropped to the view (content with a [`Kept`] image to its window) when /// every effect only looks nearby, and content still over either limit is rendered at a lower - /// resolution. A whole raster at full resolution is cached while the object, the - /// zoom and what the content reads ([`Inputs`]) stay the same (panning only moves it), and - /// while interacting after the content only moved ([`Self::move_pixel_content`]). A cropped or - /// lower-resolution raster is cached while the object, the zoom and what the content reads stay - /// the same and a pan keeps the view inside the rect it was made for ([`PixelEntry::view`]): the - /// view, and for a raster cropped to it that is made again at the same zoom and rotation, outside - /// a drag and for content without a [`Kept`] image, the view with [`pan_margin`] around it. A - /// lower-resolution one is kept resampled to the view's pixels ([`Self::resampled`]). + /// resolution. A raster of the whole content is cached while the object, the zoom and what the + /// content reads ([`Inputs`]) stay the same (panning only moves it, and at a lower resolution + /// only by whole view and output pixels with the same output linear coefficients and the same + /// raster origin, scale and size as a fresh render, [`PixelEntry::pan`]), and at full resolution + /// while interacting after the content only moved ([`Self::move_pixel_content`]). One at a + /// lower resolution draws through its copy resampled + /// to the view's pixels ([`Self::draw_kept`]). A cropped raster is cached while the object, the + /// zoom and what the content reads stay the same and a pan keeps the view inside the rect it + /// was made for ([`PixelEntry::view`]): the view, and for a raster cropped to it that is made + /// again at the same zoom and rotation, outside a drag and for content without a [`Kept`] image, + /// the view with [`pan_margin`] around it. A cropped one at a lower resolution is kept + /// resampled to the view's pixels ([`Self::resampled`]). /// While dragging, content without such a raster to draw renders within [`DRAG_PIXEL_BUDGET`] /// pixels and is not cached, except content cropped to the view whose raster can move: its /// raster covers the view with [`pan_margin`] around it, and the drag's later frames move it with @@ -173,11 +177,13 @@ impl Renderer { let (aw, ah) = (around.width().ceil() + 1.0, around.height().ceil() + 1.0); (DRAG_PIXEL_BUDGET / (aw * ah)).sqrt().min(side / aw).min(side / ah).min(1.0) }); - // The cached raster: of the whole content at full size at this zoom, else of this view (and - // the margin around it). + // The cached raster: of the whole content at this zoom (at full size, or at a lower + // resolution when an effect looks anywhere in the object), else of this view (and the margin + // around it). + let entire = fits || margin.is_none(); let whole = fits && k >= 1.0; self.stats.view_sized += usize::from(!whole); - let in_view = (!whole).then(|| ViewCrop::of(f.view, room)); + let in_view = (!entire).then(|| ViewCrop::of(f.view, room)); let stamp = self.stamp; let channels = match f.ink.base() { Ink::Cmy => effects::pixel::Channels::CmyPlane, @@ -197,7 +203,18 @@ impl Renderer { }); let interacting = f.opts.interacting; let in_place = |e: &PixelEntry| match e.view { - None => whole, + None => e.pan.map_or(whole, |(pan, output_pan, output_linear)| { + let p = f.view * e.anchor; + entire + && panned_whole(pan, f.view) + && output_linear == linear_key(f.output) + && panned_whole(output_pan, f.output) + && e.k.to_bits() == k.to_bits() + && f64::from(e.image.width()) == w + && f64::from(e.image.height()) == h + && (p.x + e.rel.0).round() == x0 + && (p.y + e.rel.1).round() == y0 + }), Some(v) => !whole && (e.valid.is_some() || v.serves(f.view, screen)), }; // A raster a drag kept draws during a drag, while what the frame shows of the content lies @@ -229,7 +246,7 @@ impl Renderer { if e.image_left_out { self.image_left_out = self.image_left_out.map(|_| true); } - draw_scaled(ctx, image, at, k); + self.draw_kept(ctx, f, key, screen, (image, at, k)); return; } // During a drag, content without a raster to draw renders within the drag's budget (#2558). @@ -368,6 +385,10 @@ impl Renderer { Some((image, x, y)) => (image, x, y, 1.0), None => (image, x0, y0, k), }; + // A raster of the whole content at a lower resolution is kept whole for pans, and drawn + // through its copy resampled to the view's pixels ([`Self::draw_kept`]). + let low = key.is_some() && in_view.is_none() && k < 1.0 && content.kept.is_none_or(|k| k.shared) && !drag_keep; + let mut kept_low = None; // A cancelled export drew part of the content: that raster isn't kept for later frames. if !vectorcraft_doc::cancel::is_cancelled() && let Some(((key, made), inputs)) = key.zip(made).zip(inputs) @@ -386,8 +407,10 @@ impl Renderer { rel, k, image: image.clone(), - view: in_view, + view: (!whole && !low).then(|| ViewCrop::of(f.view, room)), valid, + pan: low.then(|| (f.view.translation(), f.output.translation(), linear_key(f.output))), + shown: None, made, inputs, moved, @@ -395,8 +418,8 @@ impl Renderer { image_left_out, stamp, }; - self.pixel_fx.insert(key, entry); - self.trim_fx_rasters(FX_RASTER_BUDGET); + self.keep_pixel_content(key, entry); + kept_low = low.then_some(key); } // An image over the limits was drawn at a frame's resolution: its raster is kept. The // placements of a copied image each draw their own (`Kept::shared`). @@ -406,7 +429,65 @@ impl Renderer { *kept.image.borrow_mut() = Some(KeptImage::Raster(image.clone(), place, failed, image_left_out)); if f.view == call { draw_scaled(ctx, image, (x0, y0), k) } else { draw_placed(ctx, image, call * place) } } - None => draw_scaled(ctx, image, (x0, y0), k), + None => self.draw_kept(ctx, f, kept_low, screen, (image, (x0, y0), k)), + } + } + + /// Keep `entry` for `key` and trim the cache, deferring trimming for a whole raster at a lower + /// resolution until [`Self::draw_kept`] draws it. + fn keep_pixel_content(&mut self, key: (usize, usize, Ink), entry: PixelEntry) { + let low = entry.pan.is_some(); + self.pixel_fx.insert(key, entry); + if !low { + self.trim_fx_rasters(FX_RASTER_BUDGET); + } + } + + /// Draw `(image, at, k)` as [`draw_scaled`] draws `image` at `at`, `1 / k` device pixels a + /// pixel. When it is the raster kept for `key` of the whole content at a lower resolution + /// ([`PixelEntry::pan`]), in the frame `f` onto `screen`, its copy resampled to the device + /// 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. + fn draw_kept( + &mut self, + ctx: &mut RenderContext, + f: &Frame, + key: Option<(usize, usize, Ink)>, + screen: Rect, + (image, at, k): (std::sync::Arc, (f64, f64), f64), + ) { + let Some((key, e)) = key.and_then(|key| self.pixel_fx.get(&key).filter(|e| e.pan.is_some()).map(|e| (key, e))) else { + return draw_scaled(ctx, image, at, k); + }; + let p = f.view * e.anchor; + if let Some((_, copy, rel)) = e.shown.as_ref().filter(|(v, ..)| v.serves(f.view, screen)) { + draw_scaled(ctx, copy.clone(), ((p.x + rel.0).round(), (p.y + rel.1).round()), 1.0); + return; + } + let covered = Rect::new(at.0, at.1, at.0 + f64::from(image.width()) / k, at.1 + f64::from(image.height()) / k).intersect(screen); + let (w, h) = (covered.x1.ceil() - covered.x0.floor(), covered.y1.ceil() - covered.y0.floor()); + if w * h * 4.0 > FX_RASTER_BUDGET as f64 { + if let Some(e) = self.pixel_fx.get_mut(&key) { + e.shown = None; + } + self.trim_fx_rasters(FX_RASTER_BUDGET); + return draw_scaled(ctx, image, at, k); + } + match self.resampled(f, image.clone(), at, k, screen) { + Some((copy, x, y)) => { + if let Some(e) = self.pixel_fx.get_mut(&key) { + e.shown = Some((ViewCrop::of(f.view, screen), copy.clone(), (x - p.x, y - p.y))); + } + self.trim_fx_rasters(FX_RASTER_BUDGET); + draw_scaled(ctx, copy, (x, y), 1.0); + } + None => { + self.trim_fx_rasters(FX_RASTER_BUDGET); + draw_scaled(ctx, image, at, k); + } } } @@ -512,9 +593,10 @@ 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, 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 + /// color settings, the edge smoothing, the hidden objects, 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 /// [`crate::RenderOptions::kept_images_only`], also which pixels and undecodable notes the @@ -532,6 +614,7 @@ impl Renderer { f.opts.hide_guides.hash(&mut h); // Cached effects can include dashed strokes drawn as solid lines. f.opts.solid_dashes.hash(&mut h); + f.hidden_inputs.hash(&mut h); if let Some((isolated, path)) = self.isolation_path(f) && n.id != isolated && path.contains(&n.id) @@ -620,16 +703,35 @@ impl Renderer { d } - /// Drop the least recently drawn shadow and filtered-content rasters beyond `budget` bytes - /// ([`FX_RASTER_BUDGET`]). + /// 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`]). pub(super) fn trim_fx_rasters(&mut self, budget: usize) { - 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 mut all: Vec<_> = shadows.chain(pixels).collect(); - let mut total: usize = all.iter().map(|e| e.3).sum(); + 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 mut all: Vec<_> = shadows.chain(pixels).collect(); all.sort_unstable_by_key(|e| e.0); for (_, pixel, key, bytes) in all { if total <= budget { diff --git a/crates/render/src/fx/raster.rs b/crates/render/src/fx/raster.rs index 02bf918089..5b9d8ca120 100644 --- a/crates/render/src/fx/raster.rs +++ b/crates/render/src/fx/raster.rs @@ -90,16 +90,28 @@ pub(crate) struct PixelEntry { pub(super) rel: (f64, f64), pub(super) k: f64, pub(crate) image: std::sync::Arc, - /// For a raster of the content's whole reach at full size, `None`: it draws after a pan too. - /// For one cropped to the view, or made at a lower resolution, the view and the rect it was - /// made for: it draws only there, so dragging other art over the object keeps it (a wide blur, - /// or any blur zoomed in, reaches past the pixel budget). + /// For a raster of the content's whole reach, `None`: it draws after a pan too. For one + /// cropped to the view, the view and the rect it was made for: it draws only there, so + /// dragging other art over the object keeps it (a wide blur, or any blur zoomed in, reaches + /// past the pixel budget). pub(super) view: Option, /// For a raster a drag's frame kept of content cropped to the view, the device pixels (against /// the anchor's view position) whose pixels the effects decided with all they look at: the /// drag's later frames move the raster with its object while what they show of it lies within /// ([`Renderer::move_pixel_content`]). `None` for other rasters. pub(super) valid: Option, + /// For a raster of the content's whole reach made at a lower resolution than the view's, the + /// pans of the view and output it was made through, and the output's linear coefficients: it + /// draws when both grids pan by whole pixels ([`panned_whole`]), the output's linear + /// coefficients stay the same, its rounded origin matches a fresh render's floored origin, + /// and its scale and size match a fresh render's. `None` for other rasters. + pub(super) pan: Option<(Vec2, Vec2, [u64; 4])>, + /// For such a raster, its pixels resampled to the device pixels of the last frame that drew it, + /// 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. + 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, /// under De-Interlace, the raster resolution. @@ -121,10 +133,16 @@ pub(crate) struct PixelEntry { impl PixelEntry { /// The bytes it holds, for [`FX_RASTER_BUDGET`]. pub(crate) fn bytes(&self) -> usize { - raster_bytes(&self.image) + self.made.bytes() + raster_bytes(&self.image) + self.shown.as_ref().map_or(0, |(_, copy, _)| raster_bytes(copy)) + self.made.bytes() } } +/// Is `view` panned by whole device pixels from a view whose pan was `pan`? +pub(super) fn panned_whole(pan: Vec2, view: Affine) -> bool { + let d = view.translation() - pan; + (d - Vec2::new(d.x.round(), d.y.round())).hypot() < 1e-6 +} + /// The four linear coefficients of `view` (zoom and rotation, not the pan), as cache keys. pub(crate) fn linear_key(view: Affine) -> [u64; 4] { let c = view.as_coeffs(); @@ -153,7 +171,7 @@ impl ViewCrop { let d = view.translation() - self.pan; let whole = Vec2::new(d.x.round(), d.y.round()); let s = screen - whole; - (d - whole).hypot() < 1e-6 && s.x0 >= self.screen.x0 && s.y0 >= self.screen.y0 && s.x1 <= self.screen.x1 && s.y1 <= self.screen.y1 + panned_whole(self.pan, view) && s.x0 >= self.screen.x0 && s.y0 >= self.screen.y0 && s.x1 <= self.screen.x1 && s.y1 <= self.screen.y1 } /// The rect the raster was made for, in the device pixels of a frame through `view` that it diff --git a/crates/render/src/fx/renderer.rs b/crates/render/src/fx/renderer.rs index c119ef8b64..ebc82abeb1 100644 --- a/crates/render/src/fx/renderer.rs +++ b/crates/render/src/fx/renderer.rs @@ -1142,6 +1142,7 @@ mod tests { visible: Rect::new(0.0, 0.0, 100.0, 100.0), px: 1.0, opts: &opts, + hidden_inputs: Frame::hidden_inputs(&opts), ink: Ink::Display, output: Affine::IDENTITY, plates: None, diff --git a/crates/render/src/fx/tests.rs b/crates/render/src/fx/tests.rs index be44374572..727a09996f 100644 --- a/crates/render/src/fx/tests.rs +++ b/crates/render/src/fx/tests.rs @@ -1889,6 +1889,115 @@ fn a_zoomed_drag_keeps_the_sampling_footprint_inside_the_valid_crop() { assert_eq!(r.stats.previewed, 1, "the sampling footprint outside the valid crop filters the content again"); } +/// At 6400% a 200 × 150 pt rectangle with Torn Edges, which looks anywhere in the object, covers +/// 12800 × 9600 px, past the pixel budget, so its raster holds the whole object at a lower +/// resolution. The view shows its torn right edge. Pans by whole pixels with the same sampling +/// grid draw that raster again, and a frame through the same view copies its pixels resampled to +/// the view; a new fill, color mode or zoom, or a pan by part of a pixel, filters the content +/// again. +#[test] +fn pans_draw_the_lower_resolution_raster_of_the_whole_object_again() { + 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 id = d.layers[0].children().unwrap()[0].id; + let opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() }; + let view = |zoom: f64, dx: f64| Affine::translate((640.0 + dx, 400.0)) * Affine::scale(zoom) * Affine::translate((-295.0, -175.0)); + let mut r = Renderer::new(); + let first = r.render(&d, 1280, 800, view(64.0, 0.0), &opts); + assert!(first.pixels.as_chunks::<4>().0.iter().any(|pixel| *pixel != first.pixel(0, 0)), "the view shows the torn edge"); + assert_eq!(r.pixel_fx.len(), 1); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.k < 1.0 && kept.view.is_none(), "a raster of the whole object at a lower resolution"); + let image = kept.image.clone(); + let drawn = |r: &Renderer, image: &Arc| r.pixel_fx.values().any(|e| Arc::ptr_eq(&e.image, image)); + for dx in [40.0, 80.0, -40.0] { + let pixels = r.render(&d, 1280, 800, view(64.0, dx), &opts).pixels; + assert!(drawn(&r, &image), "a pan of {dx} px draws the kept raster"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(64.0, dx), &opts).pixels, "a pan of {dx} px as a new renderer draws it"); + } + let copy = |r: &Renderer| r.pixel_fx.values().next().and_then(|e| e.shown.as_ref()).map(|(_, copy, _)| copy.clone()).unwrap(); + let before = copy(&r); + let _ = r.render(&d, 1280, 800, view(64.0, -40.0), &opts); + assert!(Arc::ptr_eq(&before, ©(&r)), "a frame through the same view copies the resampled pixels"); + for pan in [(39.9999999, 0.0), (0.0, 39.9999999), (40.0000001, 0.0), (0.0, 40.0000001)] { + let _ = r.render(&d, 1280, 800, view(64.0, 0.0), &opts); + let image = r.pixel_fx.values().next().unwrap().image.clone(); + let shifted = Affine::translate(pan) * view(64.0, 0.0); + let pixels = r.render(&d, 1280, 800, shifted, &opts).pixels; + assert!(!drawn(&r, &image), "a nearly whole-pixel pan with a different raster origin or sampling grid filters the content again"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, shifted, &opts).pixels, "after a nearly whole-pixel pan"); + } + let _ = r.render(&d, 1280, 800, view(64.0, 40.0), &opts); + let image = r.pixel_fx.values().next().unwrap().image.clone(); + let n = d.node_mut(id).unwrap(); + let effects = std::mem::take(&mut n.appearance.effects); + n.appearance = Appearance::basic(Paint::solid(Color::rgb(0.1, 0.4, 0.9)), Paint::None, 0.0).into(); + n.appearance.effects = effects; + let pixels = r.render(&d, 1280, 800, view(64.0, 40.0), &opts).pixels; + assert!(!drawn(&r, &image), "a new fill filters the content again"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(64.0, 40.0), &opts).pixels, "with the new fill"); + let image = r.pixel_fx.values().next().unwrap().image.clone(); + d.color_mode = vectorcraft_doc::ColorMode::Cmyk; + let pixels = r.render(&d, 1280, 800, view(64.0, 40.0), &opts).pixels; + assert!(!drawn(&r, &image), "a new color mode filters the content again"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(64.0, 40.0), &opts).pixels, "in the new color mode"); + let image = r.pixel_fx.values().next().unwrap().image.clone(); + let pixels = r.render(&d, 1280, 800, view(48.0, 40.0), &opts).pixels; + assert!(!drawn(&r, &image), "a new zoom filters the content again"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(48.0, 40.0), &opts).pixels, "at the new zoom"); + let image = r.pixel_fx.values().next().unwrap().image.clone(); + let pixels = r.render(&d, 1280, 800, view(48.0, 40.5), &opts).pixels; + assert!(!drawn(&r, &image), "a pan by part of a pixel filters the content again"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(48.0, 40.5), &opts).pixels, "after a pan by part of a pixel"); +} + +/// Hiding a member of a group filters its whole-object raster again after a pan by whole pixels. +#[test] +fn hiding_a_member_filters_the_whole_object_raster_again_after_a_pan() { + let mut d = Document::new(100.0, 100.0); + let mut square = + |r: Rect, c: Color| Arc::new(Node::path(d.alloc_id(), shapes::rectangle(r), Appearance::basic(Paint::solid(c), Paint::None, 0.0))); + let members = vec![square(Rect::new(100.0, 100.0, 300.0, 250.0), Color::WHITE), square(Rect::new(200.0, 100.0, 300.0, 250.0), Color::BLACK)]; + let id = members[1].id; + let mut g = Node::group(d.alloc_id(), members); + g.appearance.effects = vec![Effect { id: "sketch.tornEdges".into(), params: json!({}), visible: true }]; + let l = d.layers[0].id; + d.insert(Some(l), 0, g).unwrap(); + let mut opts = RenderOptions { background: Some([255, 255, 255, 255]), ..Default::default() }; + 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.view.is_none(), "a raster of the whole object at a lower resolution"); + opts.hidden.insert(id); + let pixels = r.render(&d, 1280, 800, view(40.0), &opts).pixels; + assert!(pixels == Renderer::new().render(&d, 1280, 800, view(40.0), &opts).pixels, "hiding a member filters the content again"); +} + +/// A whole-object raster gives way to its view copy when the cache budget cannot hold both. +/// The same view draws the copy again without filtering the content. +#[test] +fn a_lower_resolution_whole_raster_keeps_its_view_copy_under_the_cache_budget() { + 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(&d, 1280, 800, view, &opts); + let _ = r.render(&d, 1280, 800, view, &opts); + let kept = r.pixel_fx.values().next().unwrap(); + assert!(kept.pan.is_some() && kept.k < 1.0, "the whole-object raster is kept at a lower resolution"); + let copy = kept.shown.as_ref().unwrap().1.clone(); + let budget = kept.bytes() - super::entries::raster_bytes(&kept.image); + r.trim_fx_rasters(budget); + assert_eq!(r.pixel_fx.len(), 1); + 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-object raster"); + let pixels = r.render(&d, 1280, 800, view, &opts).pixels; + assert!(Arc::ptr_eq(&r.pixel_fx.values().next().unwrap().image, ©), "the same view draws the copy without filtering again"); + assert!(pixels == Renderer::new().render(&d, 1280, 800, view, &opts).pixels, "the copy draws the same pixels as a new renderer"); +} + #[test] fn a_drag_glow_fallback_cannot_be_spliced_into_a_still_frame() { let doc = doc_with( diff --git a/crates/render/src/pattern.rs b/crates/render/src/pattern.rs index 9e44f5c655..2fa90d50da 100644 --- a/crates/render/src/pattern.rs +++ b/crates/render/src/pattern.rs @@ -194,6 +194,7 @@ fn rasterize( // A tile drawn for a proof that preserves the colors' numbers shows its images through that // proof too. let plates = crate::proof::ink_plate_lut(ink, doc); + let hidden_inputs = Frame::hidden_inputs(&opts); for o in def.offsets_covering(region) { let v = view * def.instance_xf(o); let frame = Frame { @@ -205,6 +206,7 @@ fn rasterize( visible: v.inverse().transform_rect_bbox(px_rect), px: 1.0 / v.determinant().abs().sqrt().max(1e-12), opts: &opts, + hidden_inputs, ink, output: v, plates: plates.as_deref(), diff --git a/crates/render/src/renderer.rs b/crates/render/src/renderer.rs index 9eb6082e37..bbe2485647 100644 --- a/crates/render/src/renderer.rs +++ b/crates/render/src/renderer.rs @@ -198,7 +198,21 @@ impl Renderer { let symbols = doc.symbol_boxes().quick(); let output = Affine::scale(1.0 / f64::from(cell.max(1))) * view; let plates = proof::plate_lut(opts, doc); - let frame = Frame { mt: threads > 0, patch, doc, symbols: &symbols, view, visible, px, opts, ink: screen, output, plates: plates.as_deref() }; + let hidden_inputs = Frame::hidden_inputs(opts); + let frame = Frame { + mt: threads > 0, + patch, + doc, + symbols: &symbols, + view, + visible, + px, + opts, + hidden_inputs, + ink: screen, + output, + plates: plates.as_deref(), + }; let _crossed = self.cross_placed(&frame); self.begin_frame(); // View Opacity Mask: the mask's coverage in greyscale instead of the artwork. @@ -490,7 +504,9 @@ impl Renderer { ctx.set_aliasing_threshold(opts.anti_alias.threshold()); let symbols = doc.symbol_boxes().quick(); let output = Affine::scale(1.0 / f64::from(cell.max(1))) * view; - let frame = Frame { mt: false, patch: None, doc, symbols: &symbols, view, visible, px, opts, ink: Ink::Display, output, plates: None }; + let hidden_inputs = Frame::hidden_inputs(opts); + let frame = + Frame { mt: false, patch: None, doc, symbols: &symbols, view, visible, px, opts, hidden_inputs, ink: Ink::Display, output, plates: None }; let raster = std::mem::replace(&mut self.raster, raster_settings(opts)); let _crossed = self.cross_placed(&frame); self.begin_frame();