Truly independent web browser
LibCompositing+Compositor: Bound effect layers by their content
Aliaksandr Kalenik5d2c83b0b87b
diff --git a/Libraries/LibCompositing/Rust/src/display_list/commands.rs b/Libraries/LibCompositing/Rust/src/display_list/commands.rsindex ca7257ce23e..445dfa221fe 100644--- a/Libraries/LibCompositing/Rust/src/display_list/commands.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/commands.rs@@ -211,6 +211,8 @@ pub struct ReplayLayer {pub backdrop_region: IntRect,pub backdrop_corner_radii: CornerRadii,pub effect: EffectNodeIndex,+ /// Bounds of everything replay draws into the layer, in the effect's space, when known.+ pub content_bounds: OptionalFloatRect,}#[derive(Clone, Copy, Debug, PartialEq, Eq)]diff --git a/Libraries/LibCompositing/Rust/src/display_list/replay.rs b/Libraries/LibCompositing/Rust/src/display_list/replay.rsindex 204a67226ae..d64510e4c6c 100644--- a/Libraries/LibCompositing/Rust/src/display_list/replay.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/replay.rs@@ -6,8 +6,8 @@use crate::display_list::builder::{for_each_command, inline_clips_of, inline_transform_of, read_command};use crate::display_list::commands::{- ClipNodeIndex, DisplayListCommandRun, DisplayListCommandType, EffectNodeIndex, InlineClipKind, PaintScrollBar,- ReplayClip, ReplayLayer, ReplayMask, SpatialNodeIndex,+ ClipNodeIndex, DisplayListCommandRun, DisplayListCommandType, EffectNodeIndex, InlineClipKind, OptionalFloatRect,+ PaintScrollBar, ReplayClip, ReplayLayer, ReplayMask, SpatialNodeIndex,};use crate::display_list::depth_sorted_plan::{DepthSortedReplayStepKind, build_depth_sorted_replay_plan};use crate::display_list::effect_clip_plan::EffectClipPlan;@@ -21,8 +21,8 @@ use crate::visual_context::{};use libgfx_rust::path::OwnedPath;use libgfx_rust::{- AffineTransform, CornerRadii, FloatMatrix4x4, FloatPoint, FloatVector3, IntRect, WindingRule, enclosing_int_rect,- translation_matrix,+ AffineTransform, CornerRadii, FloatMatrix4x4, FloatPoint, FloatRect, FloatVector3, IntRect, WindingRule,+ enclosing_int_rect, translation_matrix,};use std::cell::RefCell;@@ -125,6 +125,10 @@ impl SpaceMatrices for &ReplayPaletteStorage {}}+// Recorded bounds are whole device pixels around geometry that antialiasing and glyph rounding+// can push past them by up to this much, and a layer's bounds clip what is drawn into it.+const LAYER_CONTENT_MARGIN_IN_DEVICE_PIXELS: f32 = 2.0;+// One entry of the canvas save stack: a clip or an effect the replay has entered.#[derive(Clone, Copy, Debug, PartialEq, Eq)]enum Applied {@@ -213,7 +217,11 @@ fn replay_clip_of(clip: &crate::visual_context::ClipData) -> ReplayClip {}}-fn replay_layer_of(effects: &crate::visual_context::EffectsData, effect: EffectNodeIndex) -> ReplayLayer {+fn replay_layer_of(+ effects: &crate::visual_context::EffectsData,+ effect: EffectNodeIndex,+ content_bounds: Option<FloatRect>,+) -> ReplayLayer {let (filter_bytes, filter_bytes_size) = match &effects.filter {Some(bytes) => (bytes.as_ptr(), bytes.len()),None => (std::ptr::null(), 0),@@ -238,6 +246,7 @@ fn replay_layer_of(effects: &crate::visual_context::EffectsData, effect: EffectNbackdrop_region,backdrop_corner_radii,effect,+ content_bounds: OptionalFloatRect::from(content_bounds),}}@@ -395,6 +404,31 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {self.effect_geometry[effect.0 as usize]}+ // The bounds hint for an effect's layer, in the effect's space.+ fn layer_content_bounds(&mut self, effect: EffectNodeIndex, space: SpatialNodeIndex) -> Option<FloatRect> {+ let Extent::Within(content) = self.effect_geometry(effect).content else {+ return None;+ };+ let to_root_matrix = (&self.palette).to_root_matrix(space);+ if !to_root_matrix.is_2d_affine() {+ return None;+ }+ let root_to_layer = to_root_matrix.extract_2d_affine().inverse()?;+ let margin_x = LAYER_CONTENT_MARGIN_IN_DEVICE_PIXELS * (root_to_layer.a().abs() + root_to_layer.c().abs());+ let margin_y = LAYER_CONTENT_MARGIN_IN_DEVICE_PIXELS * (root_to_layer.b().abs() + root_to_layer.d().abs());+ let bounds = FloatRect::new(+ content.x - margin_x,+ content.y - margin_y,+ content.width + 2.0 * margin_x,+ content.height + 2.0 * margin_y,+ );+ // Bounds that overflowed would clip away everything drawn into the layer.+ [bounds.x, bounds.y, bounds.right(), bounds.bottom()]+ .into_iter()+ .all(f32::is_finite)+ .then_some(bounds)+ }+// Merge the clip and effect chains from leaf to root. Each effect goes immediately// inside its output clip; reversing once produces the canvas stack in linear time.fn build_target(&mut self, context: ContextRef) {@@ -549,7 +583,9 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {EffectNodeData::Effects(effects) => {if tree.effect_pushes_layer_at_replay(effect) {self.ensure_ctm_space(node.spatial);- self.painter.push_layer(&replay_layer_of(effects, effect));+ let content_bounds = self.layer_content_bounds(effect, node.spatial);+ self.painter+ .push_layer(&replay_layer_of(effects, effect, content_bounds));}}EffectNodeData::Mask(mask) => {@@ -837,6 +873,7 @@ mod tests {events: Vec<PainterEvent>,base_matrix: FloatMatrix4x4,fully_clipped_x: i32,+ layer_content_bounds: Vec<Option<FloatRect>>,}impl RecordingPainter {@@ -845,6 +882,7 @@ mod tests {events: Vec::new(),base_matrix: translation_matrix(100.0, 200.0, 0.0),fully_clipped_x: i32::MIN,+ layer_content_bounds: Vec::new(),}}}@@ -867,6 +905,7 @@ mod tests {}fn push_layer(&mut self, layer: &ReplayLayer) {self.events.push(PainterEvent::PushLayer(layer.opacity));+ self.layer_content_bounds.push(layer.content_bounds.get());}fn push_mask(&mut self, mask: &ReplayMask) {self.events.push(PainterEvent::PushMask(mask.rect));@@ -1158,6 +1197,94 @@ mod tests {);}+ fn effect_context(effect: EffectNodeIndex) -> ContextRef {+ ContextRef {+ clip: ClipNodeIndex::NONE,+ effect,+ ..ContextRef::default()+ }+ }++ // A layer's bounds are its content plus the margin it keeps for antialiasing, in its own units.+ fn with_margin(rect: FloatRect, margin: f32) -> FloatRect {+ FloatRect::new(+ rect.x - margin,+ rect.y - margin,+ rect.width + 2.0 * margin,+ rect.height + 2.0 * margin,+ )+ }++ #[test]+ fn a_layer_is_bounded_by_everything_drawn_into_it() {+ let mut tree = identity_tree();+ let outer = effect_under(&mut tree, effects(0.5), EffectNodeIndex::NONE, ClipNodeIndex::NONE);+ let blur = effect_under(+ &mut tree,+ EffectNodeData::Effects(EffectsData {+ opacity: 1.0,+ blend_mode: CompositingAndBlendingOperator::Normal,+ filter: Some(std::sync::Arc::new(+ libgfx_rust::filter::Filter::blur(1.0, 1.0, None).serialize(),+ )),+ backdrop_filter: None,+ }),+ outer,+ ClipNodeIndex::NONE,+ );+ let runs = [+ run(+ VISUAL_VIEWPORT_NODE_INDEX,+ effect_context(blur),+ IntRect::new(0, 0, 10, 10),+ ),+ run(+ VISUAL_VIEWPORT_NODE_INDEX,+ effect_context(outer),+ IntRect::new(20, 5, 10, 10),+ ),+ ];+ let mut painter = RecordingPainter::new();+ replay(&tree, &runs, &mut painter);+ assert_eq!(+ painter.layer_content_bounds,+ [+ Some(with_margin(FloatRect::new(-3.0, -3.0, 33.0, 18.0), 2.0)),+ Some(with_margin(FloatRect::new(0.0, 0.0, 10.0, 10.0), 2.0)),+ ]+ );+ }++ #[test]+ fn a_scaled_layer_widens_its_margin_to_keep_two_device_pixels() {+ let mut tree = identity_tree();+ let half = tree.append_spatial(transform(scale_matrix(0.5, 0.5, 1.0), None), VISUAL_VIEWPORT_NODE_INDEX);+ let double = tree.append_spatial(transform(scale_matrix(4.0, 4.0, 1.0), None), half);+ let layer = tree.append_effect(effects(0.5), EffectNodeIndex::NONE, half, ClipNodeIndex::NONE);+ let runs = [run(double, effect_context(layer), IntRect::new(0, 0, 10, 10))];+ let mut painter = RecordingPainter::new();+ replay(&tree, &runs, &mut painter);+ assert_eq!(+ painter.layer_content_bounds,+ [Some(with_margin(FloatRect::new(0.0, 0.0, 40.0, 40.0), 4.0))]+ );+ }++ #[test]+ fn a_layer_whose_content_can_land_anywhere_has_no_bounds() {+ let mut tree = identity_tree();+ let tilted = tree.append_spatial(transform(rotate_y(30.0), None), VISUAL_VIEWPORT_NODE_INDEX);+ let flat_layer = effect_under(&mut tree, effects(0.5), EffectNodeIndex::NONE, ClipNodeIndex::NONE);+ let tilted_layer = tree.append_effect(effects(0.5), EffectNodeIndex::NONE, tilted, ClipNodeIndex::NONE);+ let runs = [+ run(tilted, effect_context(flat_layer), IntRect::new(0, 0, 10, 10)),+ run(tilted, effect_context(tilted_layer), IntRect::new(0, 0, 10, 10)),+ ];+ let mut painter = RecordingPainter::new();+ replay(&tree, &runs, &mut painter);+ assert_eq!(painter.layer_content_bounds, [None, None]);+ }+#[test]fn mask_effects_pop_with_their_effect_index() {let mut tree = identity_tree();@@ -1496,7 +1623,7 @@ mod tests {corner_radii: radii,}),};- let layer = replay_layer_of(&with_backdrop, EffectNodeIndex(3));+ let layer = replay_layer_of(&with_backdrop, EffectNodeIndex(3), None);assert_eq!(layer.opacity, 0.5);assert_eq!(layer.backdrop_filter_bytes, filter.as_ptr());assert_eq!(layer.backdrop_filter_bytes_size, 3);@@ -1507,7 +1634,7 @@ mod tests {let EffectNodeData::Effects(without_backdrop) = effects(0.5) else {unreachable!()};- let layer = replay_layer_of(&without_backdrop, EffectNodeIndex(4));+ let layer = replay_layer_of(&without_backdrop, EffectNodeIndex(4), None);assert!(layer.backdrop_filter_bytes.is_null());assert_eq!(layer.backdrop_filter_bytes_size, 0);assert_eq!(layer.backdrop_region, IntRect::default());diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cppindex 6f4c49b35d7..28248e39fee 100644--- a/Services/Compositor/DisplayListPlayerSkia.cpp+++ b/Services/Compositor/DisplayListPlayerSkia.cpp@@ -1488,7 +1488,12 @@ void DisplayListPlayerSkia::push_layer(ReplayLayer const& layer)}if (!layer.backdrop_filter_bytes_size) {- canvas.saveLayer(nullptr, &paint);+ if (layer.content_bounds.has_value()) {+ auto content_bounds = to_skia_rect(*layer.content_bounds);+ canvas.saveLayer(&content_bounds, &paint);+ } else {+ canvas.saveLayer(nullptr, &paint);+ }return;}diff --git a/Tests/LibWeb/Ref/input/scrollable-contains-box-with-gradient-background.html b/Tests/LibWeb/Ref/input/scrollable-contains-box-with-gradient-background.htmlindex 9802526844f..29c0c471a31 100644--- a/Tests/LibWeb/Ref/input/scrollable-contains-box-with-gradient-background.html+++ b/Tests/LibWeb/Ref/input/scrollable-contains-box-with-gradient-background.html@@ -1,6 +1,6 @@<!DOCTYPE html><link rel="match" href="../expected/scrollable-contains-box-with-gradient-background-ref.html" />-<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-50">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-100"><style type="text/css">* {scrollbar-width: none;diff --git a/Tests/LibWeb/Ref/input/svg-path-incomplete-args.html b/Tests/LibWeb/Ref/input/svg-path-incomplete-args.htmlindex 30a8c378bf1..187c78f5cfa 100644--- a/Tests/LibWeb/Ref/input/svg-path-incomplete-args.html+++ b/Tests/LibWeb/Ref/input/svg-path-incomplete-args.html@@ -1,5 +1,6 @@<!DOCTYPE html><link rel="match" href="../expected/svg-path-incomplete-args-ref.html" />+<meta name="fuzzy" content="maxDifference=0-4;totalPixels=0-4"><div><svg width="800" height="100"><g stroke="black">diff --git a/Tests/LibWeb/Screenshot/expected-macos/color-scheme.png b/Tests/LibWeb/Screenshot/expected-macos/color-scheme.pngindex 4c6dd9a201a..96ae9e0e06f 100644Binary files a/Tests/LibWeb/Screenshot/expected-macos/color-scheme.png and b/Tests/LibWeb/Screenshot/expected-macos/color-scheme.png differdiff --git a/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.png b/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.pngindex e7f46ec8b1d..f5060ca6651 100644Binary files a/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.png and b/Tests/LibWeb/Screenshot/expected/checkbox-accent-color-and-color-scheme.png differdiff --git a/Tests/LibWeb/Screenshot/expected/clip-path-basic-shapes.png b/Tests/LibWeb/Screenshot/expected/clip-path-basic-shapes.pngindex abc914a71bc..5789f772363 100644Binary files a/Tests/LibWeb/Screenshot/expected/clip-path-basic-shapes.png and b/Tests/LibWeb/Screenshot/expected/clip-path-basic-shapes.png differdiff --git a/Tests/LibWeb/Screenshot/expected/color-scheme.png b/Tests/LibWeb/Screenshot/expected/color-scheme.pngindex d505c42da29..4e64977e510 100644Binary files a/Tests/LibWeb/Screenshot/expected/color-scheme.png and b/Tests/LibWeb/Screenshot/expected/color-scheme.png differdiff --git a/Tests/LibWeb/Screenshot/expected/css-background-clip-text.png b/Tests/LibWeb/Screenshot/expected/css-background-clip-text.pngindex 0db27a53025..1c38176682e 100644Binary files a/Tests/LibWeb/Screenshot/expected/css-background-clip-text.png and b/Tests/LibWeb/Screenshot/expected/css-background-clip-text.png differdiff --git a/Tests/LibWeb/Screenshot/expected/gradient-interpolation-method.png b/Tests/LibWeb/Screenshot/expected/gradient-interpolation-method.pngindex 2d900532858..a24383f194d 100644Binary files a/Tests/LibWeb/Screenshot/expected/gradient-interpolation-method.png and b/Tests/LibWeb/Screenshot/expected/gradient-interpolation-method.png differdiff --git a/Tests/LibWeb/Screenshot/expected/non-square-checkboxes.png b/Tests/LibWeb/Screenshot/expected/non-square-checkboxes.pngindex b429e02649b..07ae878fba9 100644Binary files a/Tests/LibWeb/Screenshot/expected/non-square-checkboxes.png and b/Tests/LibWeb/Screenshot/expected/non-square-checkboxes.png differdiff --git a/Tests/LibWeb/Screenshot/expected/non-square-radio-buttons.png b/Tests/LibWeb/Screenshot/expected/non-square-radio-buttons.pngindex 55cab3c377d..2ecf688f770 100644Binary files a/Tests/LibWeb/Screenshot/expected/non-square-radio-buttons.png and b/Tests/LibWeb/Screenshot/expected/non-square-radio-buttons.png differdiff --git a/Tests/LibWeb/Screenshot/expected/object-fit-position.png b/Tests/LibWeb/Screenshot/expected/object-fit-position.pngindex bbd893be081..45293fc16a1 100644Binary files a/Tests/LibWeb/Screenshot/expected/object-fit-position.png and b/Tests/LibWeb/Screenshot/expected/object-fit-position.png differdiff --git a/Tests/LibWeb/Screenshot/expected/svg-feDisplacementMap-channel-selectors.png b/Tests/LibWeb/Screenshot/expected/svg-feDisplacementMap-channel-selectors.pngindex abf896dd887..bf4c6c719e4 100644Binary files a/Tests/LibWeb/Screenshot/expected/svg-feDisplacementMap-channel-selectors.png and b/Tests/LibWeb/Screenshot/expected/svg-feDisplacementMap-channel-selectors.png differdiff --git a/Tests/LibWeb/Screenshot/expected/svg-filters-lb-website.png b/Tests/LibWeb/Screenshot/expected/svg-filters-lb-website.pngindex 4fce1393f91..156138c5b3d 100644Binary files a/Tests/LibWeb/Screenshot/expected/svg-filters-lb-website.png and b/Tests/LibWeb/Screenshot/expected/svg-filters-lb-website.png differdiff --git a/Tests/LibWeb/Screenshot/input/gradient-interpolation-method.html b/Tests/LibWeb/Screenshot/input/gradient-interpolation-method.htmlindex 50287661fb9..4887737d7d6 100644--- a/Tests/LibWeb/Screenshot/input/gradient-interpolation-method.html+++ b/Tests/LibWeb/Screenshot/input/gradient-interpolation-method.html@@ -1,5 +1,5 @@<!DOCTYPE html>-<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-12">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-13"><style>div {width: 480px;diff --git a/Tests/LibWeb/Screenshot/input/svg-filters-lb-website.html b/Tests/LibWeb/Screenshot/input/svg-filters-lb-website.htmlindex 8be1c5ce691..96a352e44bc 100644--- a/Tests/LibWeb/Screenshot/input/svg-filters-lb-website.html+++ b/Tests/LibWeb/Screenshot/input/svg-filters-lb-website.html@@ -1,5 +1,5 @@<!DOCTYPE html>-<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-214364">+<meta name="fuzzy" content="maxDifference=0-1;totalPixels=0-214366"><svg width="714" height="749" viewBox="0 0 714 749" fill="none" xmlns="http://www.w3.org/2000/svg"><g filter="url(#filter0_f_27_183)"><circle cx="88.5" cy="100.5" r="278.5" fill="#8A64E5" fill-opacity="0.21"/>