Skip to content

Truly independent web browser

LibCompositing: Cull layers by where their filtered output lands

Aliaksandr Kalenikaa1557e1eb57
diff --git a/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs b/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
new file mode 100644
index 00000000000..1cb238aa817
--- /dev/null
+++ b/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
@@ -0,0 +1,351 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! Where each effect's content lies and where its output lands, in the effect's own space.
+//!
+//! A filter applies in its effect node's space, while what the effect composites can be drawn in
+//! descendant spaces, or in the space of a positioned descendant that escapes the effect's
+//! subtree. Content is therefore mapped into each effect's space before that effect's filter
+//! spreads it, and an effect's output becomes part of its parent's content.
+
+use crate::display_list::commands::{DisplayListCommandRun, SpatialNodeIndex};
+use crate::filter_bytes;
+use crate::visual_context::{EffectNode, EffectNodeData, VisualContextTree};
+use libgfx_rust::{FloatMatrix4x4, FloatRect, multiply_affine};
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum Extent {
+ Nothing,
+ Within(FloatRect),
+ /// Reaches anywhere, through a filter that paints everywhere or through a space that does not
+ /// map onto the effect's as a two-dimensional transform.
+ Anywhere,
+}
+
+impl Extent {
+ pub fn united(self, other: Self) -> Self {
+ match (self, other) {
+ (Self::Nothing, extent) | (extent, Self::Nothing) => extent,
+ (Self::Within(rect), Self::Within(other)) => Self::Within(rect.united(other)),
+ _ => Self::Anywhere,
+ }
+ }
+
+ fn mapped_between_spaces(
+ self,
+ spaces: &mut impl SpaceMatrices,
+ from: SpatialNodeIndex,
+ to: SpatialNodeIndex,
+ ) -> Self {
+ match self {
+ Self::Within(rect) => map_rect_between_spaces(spaces, rect, from, to).map_or(Self::Anywhere, Self::Within),
+ extent => extent,
+ }
+ }
+}
+
+pub trait SpaceMatrices {
+ /// The matrix that takes coordinates in `space` to the root's.
+ fn to_root_matrix(&mut self, space: SpatialNodeIndex) -> FloatMatrix4x4;
+}
+
+/// Maps `rect` from `from`'s space into `to`'s, or gives `None` when either space is not a
+/// two-dimensional transform of the root or `to`'s cannot be inverted.
+pub fn map_rect_between_spaces(
+ spaces: &mut impl SpaceMatrices,
+ rect: FloatRect,
+ from: SpatialNodeIndex,
+ to: SpatialNodeIndex,
+) -> Option<FloatRect> {
+ if from == to {
+ return Some(rect);
+ }
+ let from_matrix = spaces.to_root_matrix(from);
+ let to_matrix = spaces.to_root_matrix(to);
+ if !from_matrix.is_2d_affine() || !to_matrix.is_2d_affine() {
+ return None;
+ }
+ let root_to_target = to_matrix.extract_2d_affine().inverse()?;
+ Some(multiply_affine(root_to_target, from_matrix.extract_2d_affine()).map_rect(rect))
+}
+
+/// Where an effect paints what it composites, given that content in the effect's space.
+pub fn effect_output_extent(node: &EffectNode, content: Extent) -> Extent {
+ let Extent::Within(rect) = content else {
+ return content;
+ };
+ let EffectNodeData::Effects(effects) = &node.data else {
+ return content;
+ };
+ let Some(filter) = effects.filter.as_deref() else {
+ return content;
+ };
+ if !filter_bytes::may_affect_output_bounds(filter) {
+ return content;
+ }
+ filter_bytes::output_bounds(filter, rect).map_or(Extent::Anywhere, Extent::Within)
+}
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct EffectGeometry {
+ /// Everything composited into the effect: its runs' ink and its child effects' output.
+ pub content: Extent,
+ /// The content once the effect's filter has spread it.
+ pub output: Extent,
+}
+
+/// Fills `geometry` with every effect's geometry, indexed by effect.
+pub fn compute_effect_geometry(
+ tree: &VisualContextTree,
+ command_runs: &[DisplayListCommandRun],
+ spaces: &mut impl SpaceMatrices,
+ geometry: &mut Vec<EffectGeometry>,
+) {
+ geometry.clear();
+ geometry.resize(
+ tree.effect_nodes.len(),
+ EffectGeometry {
+ content: Extent::Nothing,
+ output: Extent::Nothing,
+ },
+ );
+
+ // Neighboring runs mostly differ only in their clip, so their ink is united before it is
+ // mapped into the effect's space.
+ let mut pending: Option<(DisplayListCommandRun, FloatRect)> = None;
+ let mut add_ink = |geometry: &mut Vec<EffectGeometry>, run: &DisplayListCommandRun, ink: FloatRect| {
+ let node = &tree.effect_nodes[run.context.effect.0 as usize];
+ let landed = Extent::Within(ink).mapped_between_spaces(spaces, run.context.spatial, node.spatial);
+ let content = &mut geometry[run.context.effect.0 as usize].content;
+ *content = content.united(landed);
+ };
+ for run in command_runs {
+ if run.ink_bounds.is_empty() || run.context.effect.is_none() {
+ continue;
+ }
+ let ink = run.ink_bounds.to_float();
+ match &mut pending {
+ Some((pending_run, pending_ink))
+ if pending_run.context.effect == run.context.effect
+ && pending_run.context.spatial == run.context.spatial =>
+ {
+ *pending_ink = pending_ink.united(ink);
+ }
+ _ => {
+ if let Some((pending_run, pending_ink)) = pending.replace((*run, ink)) {
+ add_ink(geometry, &pending_run, pending_ink);
+ }
+ }
+ }
+ }
+ if let Some((pending_run, pending_ink)) = pending {
+ add_ink(geometry, &pending_run, pending_ink);
+ }
+
+ let mut effects_parents_first = Vec::with_capacity(tree.effect_nodes.len());
+ tree.visit_effect_nodes_parents_first(|index| effects_parents_first.push(index));
+ for &index in effects_parents_first.iter().rev() {
+ let node = &tree.effect_nodes[index];
+ let output = effect_output_extent(node, geometry[index].content);
+ geometry[index].output = output;
+ if node.parent.is_none() {
+ continue;
+ }
+ let parent_spatial = tree.effect_nodes[node.parent.0 as usize].spatial;
+ let landed = output.mapped_between_spaces(spaces, node.spatial, parent_spatial);
+ let parent_content = &mut geometry[node.parent.0 as usize].content;
+ *parent_content = parent_content.united(landed);
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::display_list::commands::{ClipNodeIndex, ContextRef, EffectNodeIndex, VISUAL_VIEWPORT_NODE_INDEX};
+ use crate::visual_context::{
+ EffectsData, IncludeVisualViewportTransform, SpatialData, TransformData, TransformDataRole,
+ };
+ use libgfx_rust::filter::Filter;
+ use libgfx_rust::{
+ Color, ColorFilterType, CompositingAndBlendingOperator, FloatPoint, IntRect, rotation_matrix, scale_matrix,
+ translation_matrix,
+ };
+ use std::sync::Arc;
+
+ struct TreeSpaces<'a>(&'a VisualContextTree);
+
+ impl SpaceMatrices for TreeSpaces<'_> {
+ fn to_root_matrix(&mut self, space: SpatialNodeIndex) -> FloatMatrix4x4 {
+ self.0
+ .accumulated_matrix(space, &[], IncludeVisualViewportTransform::Yes)
+ }
+ }
+
+ fn transform(matrix: FloatMatrix4x4) -> TransformData {
+ TransformData {
+ matrix,
+ origin: FloatPoint::default(),
+ sorting_context_root_index: None,
+ flattens_inherited_transform: false,
+ role: TransformDataRole::CssTransform,
+ synthetic_plane: false,
+ establishes_sorting_context: false,
+ }
+ }
+
+ fn identity_tree() -> VisualContextTree {
+ VisualContextTree::create(transform(FloatMatrix4x4::identity()))
+ }
+
+ fn effect(
+ tree: &mut VisualContextTree,
+ filter: Option<Filter>,
+ parent: EffectNodeIndex,
+ spatial: SpatialNodeIndex,
+ ) -> EffectNodeIndex {
+ tree.append_effect(
+ EffectNodeData::Effects(EffectsData {
+ opacity: 0.5,
+ blend_mode: CompositingAndBlendingOperator::Normal,
+ filter: filter.map(|filter| Arc::new(filter.serialize())),
+ backdrop_filter: None,
+ }),
+ parent,
+ spatial,
+ ClipNodeIndex::NONE,
+ )
+ }
+
+ fn run(spatial: SpatialNodeIndex, effect: EffectNodeIndex, ink_bounds: IntRect) -> DisplayListCommandRun {
+ DisplayListCommandRun {
+ offset: 0,
+ size: 0,
+ context: ContextRef {
+ spatial,
+ clip: ClipNodeIndex::NONE,
+ effect,
+ },
+ ink_bounds,
+ has_compositor_metadata: false,
+ }
+ }
+
+ fn geometry_of(tree: &VisualContextTree, runs: &[DisplayListCommandRun]) -> Vec<EffectGeometry> {
+ let mut geometry = Vec::new();
+ compute_effect_geometry(tree, runs, &mut TreeSpaces(tree), &mut geometry);
+ geometry
+ }
+
+ fn within(x: f32, y: f32, width: f32, height: f32) -> Extent {
+ Extent::Within(FloatRect::new(x, y, width, height))
+ }
+
+ #[test]
+ fn an_effect_holds_its_runs_and_a_blur_spreads_them_into_its_output() {
+ let mut tree = identity_tree();
+ let blur = effect(
+ &mut tree,
+ Some(Filter::blur(2.0, 2.0, None)),
+ EffectNodeIndex::NONE,
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let runs = [
+ run(VISUAL_VIEWPORT_NODE_INDEX, blur, IntRect::new(10, 20, 30, 40)),
+ run(VISUAL_VIEWPORT_NODE_INDEX, blur, IntRect::new(50, 20, 10, 10)),
+ ];
+ assert_eq!(
+ geometry_of(&tree, &runs)[blur.0 as usize],
+ EffectGeometry {
+ content: within(10.0, 20.0, 50.0, 40.0),
+ output: within(4.0, 14.0, 62.0, 52.0),
+ }
+ );
+ }
+
+ #[test]
+ fn runs_in_transformed_spaces_are_mapped_into_the_effect_space() {
+ let mut tree = identity_tree();
+ let scaled = tree.append_spatial(
+ SpatialData::Transform(transform(
+ translation_matrix(5.0, 0.0, 0.0).multiplied(scale_matrix(2.0, 2.0, 1.0)),
+ )),
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let layer = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
+ let runs = [run(scaled, layer, IntRect::new(10, 20, 30, 40))];
+ assert_eq!(
+ geometry_of(&tree, &runs)[layer.0 as usize].content,
+ within(25.0, 40.0, 60.0, 80.0)
+ );
+ }
+
+ #[test]
+ fn a_child_effect_contributes_its_output_to_its_parent() {
+ let mut tree = identity_tree();
+ let outer = effect(
+ &mut tree,
+ Some(Filter::color(ColorFilterType::Invert, 1.0, None)),
+ EffectNodeIndex::NONE,
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let inner = effect(
+ &mut tree,
+ Some(Filter::drop_shadow(
+ 0.0,
+ 10.0,
+ 0.0,
+ Color::from_rgba(0, 0, 0, 0xff),
+ None,
+ )),
+ outer,
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let runs = [
+ run(VISUAL_VIEWPORT_NODE_INDEX, inner, IntRect::new(10, 20, 30, 40)),
+ run(VISUAL_VIEWPORT_NODE_INDEX, outer, IntRect::new(0, 0, 5, 5)),
+ ];
+ let geometry = geometry_of(&tree, &runs);
+ assert_eq!(geometry[inner.0 as usize].output, within(10.0, 20.0, 30.0, 50.0));
+ assert_eq!(
+ geometry[outer.0 as usize],
+ EffectGeometry {
+ content: within(0.0, 0.0, 40.0, 70.0),
+ output: within(0.0, 0.0, 40.0, 70.0),
+ }
+ );
+ }
+
+ #[test]
+ fn unbounded_filters_and_three_dimensional_spaces_reach_anywhere() {
+ let mut tree = identity_tree();
+ let outer = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
+ let flood = effect(
+ &mut tree,
+ Some(Filter::Flood {
+ color: Color::from_rgba(0, 0, 0, 0xff),
+ opacity: 1.0,
+ }),
+ outer,
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let tilted = tree.append_spatial(
+ SpatialData::Transform(transform(rotation_matrix([0.0, 1.0, 0.0], 0.5))),
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let flat = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
+ let unused = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
+ let runs = [
+ run(VISUAL_VIEWPORT_NODE_INDEX, flood, IntRect::new(0, 0, 10, 10)),
+ run(tilted, flat, IntRect::new(0, 0, 10, 10)),
+ ];
+ let geometry = geometry_of(&tree, &runs);
+ assert_eq!(geometry[flood.0 as usize].content, within(0.0, 0.0, 10.0, 10.0));
+ assert_eq!(geometry[flood.0 as usize].output, Extent::Anywhere);
+ assert_eq!(geometry[outer.0 as usize].output, Extent::Anywhere);
+ assert_eq!(geometry[flat.0 as usize].content, Extent::Anywhere);
+ assert_eq!(geometry[unused.0 as usize].output, Extent::Nothing);
+ }
+}
diff --git a/Libraries/LibCompositing/Rust/src/display_list/mod.rs b/Libraries/LibCompositing/Rust/src/display_list/mod.rs
index 5dc253c731d..ac2d4048c36 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/mod.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/mod.rs
@@ -10,6 +10,7 @@ pub mod damage;
pub mod depth_sorted_plan;
pub mod device_pixels;
pub mod effect_clip_plan;
+pub mod effect_geometry;
pub mod ffi_bytes;
pub mod nested_records;
pub mod replay;
diff --git a/Libraries/LibCompositing/Rust/src/display_list/replay.rs b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
index 7cb7514532f..204a67226ae 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
@@ -11,6 +11,7 @@ use crate::display_list::commands::{
};
use crate::display_list::depth_sorted_plan::{DepthSortedReplayStepKind, build_depth_sorted_replay_plan};
use crate::display_list::effect_clip_plan::EffectClipPlan;
+use crate::display_list::effect_geometry::{EffectGeometry, Extent, SpaceMatrices, compute_effect_geometry};
use crate::display_list::ffi_bytes::FfiBytes;
use crate::display_list::nested_records::span_bytes;
use crate::visual_context::queries::TreeCullingScratch;
@@ -20,7 +21,8 @@ use crate::visual_context::{
};
use libgfx_rust::path::OwnedPath;
use libgfx_rust::{
- AffineTransform, CornerRadii, FloatMatrix4x4, FloatPoint, FloatVector3, IntRect, WindingRule, translation_matrix,
+ AffineTransform, CornerRadii, FloatMatrix4x4, FloatPoint, FloatVector3, IntRect, WindingRule, enclosing_int_rect,
+ translation_matrix,
};
use std::cell::RefCell;
@@ -117,6 +119,12 @@ struct ReplayPaletteStorage {
flattens_inherited_transform: Vec<bool>,
}
+impl SpaceMatrices for &ReplayPaletteStorage {
+ fn to_root_matrix(&mut self, space: SpatialNodeIndex) -> FloatMatrix4x4 {
+ self.to_root_matrices[self.draw_spaces[space.0 as usize].0 as usize]
+ }
+}
+
// One entry of the canvas save stack: a clip or an effect the replay has entered.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Applied {
@@ -133,6 +141,7 @@ struct ReplayScratchStorage {
culling: TreeCullingScratch,
applied: Vec<Applied>,
target: Vec<Applied>,
+ effect_geometry: Vec<EffectGeometry>,
}
thread_local! {
@@ -152,6 +161,7 @@ fn return_replay_scratch_storage(mut storage: ReplayScratchStorage) {
storage.culling.clear();
storage.applied.clear();
storage.target.clear();
+ storage.effect_geometry.clear();
WARM_REPLAY_SCRATCH_STORAGE.with(|slot| {
let mut slot = slot.borrow_mut();
let slot_has_larger_palette = slot.as_ref().is_some_and(|kept| {
@@ -190,6 +200,9 @@ struct ReplayDriver<'a, Painter: ReplayPainter> {
applied: Vec<Applied>,
target: Vec<Applied>,
applied_context: Option<(ClipNodeIndex, EffectNodeIndex)>,
+ // Filled when the first layer is entered, since only replays that enter layers need it.
+ effect_geometry: Vec<EffectGeometry>,
+ effect_geometry_is_known: bool,
}
fn replay_clip_of(clip: &crate::visual_context::ClipData) -> ReplayClip {
@@ -235,6 +248,22 @@ fn replay_mask_of(mask: &crate::visual_context::MaskData) -> ReplayMask {
}
}
+// The whole device pixels covering `rect`, unless they reach past what an IntRect can hold, where
+// converting would clamp the edges apart from each other.
+fn int_rect_enclosing(rect: FloatRect) -> Option<IntRect> {
+ let (left, top, right, bottom) = (
+ rect.x.floor(),
+ rect.y.floor(),
+ rect.right().ceil(),
+ rect.bottom().ceil(),
+ );
+ let fits = |value: f32| value.is_finite() && value >= i32::MIN as f32 && value < i32::MAX as f32;
+ [left, top, right, bottom, right - left, bottom - top]
+ .into_iter()
+ .all(fits)
+ .then(|| enclosing_int_rect(rect))
+}
+
fn fill_replay_palette_in_dependency_order(
tree: &VisualContextTree,
spatial_dependency_order: &[u32],
@@ -353,6 +382,19 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
self.current_ctm_space = Some(space);
}
+ fn effect_geometry(&mut self, effect: EffectNodeIndex) -> EffectGeometry {
+ if !self.effect_geometry_is_known {
+ compute_effect_geometry(
+ self.tree,
+ self.command_runs,
+ &mut &self.palette,
+ &mut self.effect_geometry,
+ );
+ self.effect_geometry_is_known = true;
+ }
+ self.effect_geometry[effect.0 as usize]
+ }
+
// 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) {
@@ -442,9 +484,9 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
// OPTIMIZATION: When walking down to layer-pushing effects (opacity/blend/filter layers and masks), check
// culling before pushing each one. The clips entered so far are those outside the layer, so
- // testing against them is conservative and valid. This avoids expensive saveLayer/restore
- // cycles for off-screen elements.
- fn switch_to_context(&mut self, context: ContextRef, bounding_rect: Option<IntRect>) -> SwitchResult {
+ // testing the layer's whole output against them is conservative and valid. This avoids
+ // expensive saveLayer/restore cycles for off-screen elements.
+ fn switch_to_context(&mut self, context: ContextRef, may_cull_layers: bool) -> SwitchResult {
if self.applied_context == Some((context.clip, context.effect)) {
return SwitchResult::Switched;
}
@@ -483,14 +525,18 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
}
Applied::Effect(effect) => {
let node = &tree.effect_nodes[effect.0 as usize];
- if node.data.pushes_layer()
- && let Some(bounding_rect) = bounding_rect
- {
- let mut culled_by_layer = bounding_rect.is_empty();
- if !culled_by_layer {
- self.ensure_ctm_space(context.spatial);
- culled_by_layer = self.painter.would_be_fully_clipped_by_painter(bounding_rect);
- }
+ if node.data.pushes_layer() && may_cull_layers {
+ let culled_by_layer = match self.effect_geometry(effect).output {
+ Extent::Nothing => true,
+ Extent::Within(output) => match int_rect_enclosing(output) {
+ Some(output) => {
+ self.ensure_ctm_space(node.spatial);
+ self.painter.would_be_fully_clipped_by_painter(output)
+ }
+ None => false,
+ },
+ Extent::Anywhere => false,
+ };
if culled_by_layer {
self.restore_to_length(common_prefix_length);
// The canvas is unwound to the shared prefix; clearing the applied
@@ -522,8 +568,8 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
SwitchResult::Switched
}
- // A run enters its context once, and offers its ink bounds to the layer cull. Skipping a run
- // with nothing to draw before entering its context spares the pushes.
+ // A run enters its context once. Skipping a run with nothing to draw before entering its
+ // context spares the pushes.
fn execute_run(&mut self, run_index: usize) {
let run = self.command_runs[run_index];
if self.palette.backface_culled[run.context.spatial.0 as usize] {
@@ -535,7 +581,7 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
if run.ink_bounds.is_empty() {
return;
}
- if self.switch_to_context(run.context, Some(run.ink_bounds)) == SwitchResult::CulledByEffect {
+ if self.switch_to_context(run.context, true) == SwitchResult::CulledByEffect {
return;
}
self.ensure_ctm_space(run.context.spatial);
@@ -591,7 +637,7 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
clip: ClipNodeIndex::NONE,
effect: root_isolation_effect,
},
- None,
+ false,
);
}
self.restore_to_length(plane_clip_base_length);
@@ -647,6 +693,8 @@ pub fn replay_display_list(
applied: scratch.applied,
target: scratch.target,
applied_context: None,
+ effect_geometry: scratch.effect_geometry,
+ effect_geometry_is_known: false,
};
driver.build_transform_palette(scroll_offsets);
driver.execute();
@@ -655,6 +703,7 @@ pub fn replay_display_list(
culling: std::mem::take(&mut driver.culling),
applied: std::mem::take(&mut driver.applied),
target: std::mem::take(&mut driver.target),
+ effect_geometry: std::mem::take(&mut driver.effect_geometry),
});
}
@@ -1004,16 +1053,17 @@ mod tests {
fn a_fully_clipped_layer_is_culled_and_the_next_run_pushes_the_stack_again() {
let mut tree = identity_tree();
let clip_node = root_clip(&mut tree, FloatRect::new(0.0, 0.0, 50.0, 50.0));
+ let off_screen_layer = effect_under(&mut tree, effects(0.5), EffectNodeIndex::NONE, clip_node);
let layer = effect_under(&mut tree, effects(0.25), EffectNodeIndex::NONE, clip_node);
- let layer_context = ContextRef {
+ let context_in = |effect| ContextRef {
clip: clip_node,
- effect: layer,
+ effect,
..ContextRef::default()
};
let off_screen = IntRect::new(999, 0, 10, 10);
let runs = [
- run(VISUAL_VIEWPORT_NODE_INDEX, layer_context, off_screen),
- run(VISUAL_VIEWPORT_NODE_INDEX, layer_context, visible_bounds()),
+ run(VISUAL_VIEWPORT_NODE_INDEX, context_in(off_screen_layer), off_screen),
+ run(VISUAL_VIEWPORT_NODE_INDEX, context_in(layer), visible_bounds()),
];
let mut painter = RecordingPainter::new();
painter.fully_clipped_x = 999;
@@ -1036,6 +1086,78 @@ mod tests {
);
}
+ // A layer spanning content far apart must not be culled through a clamped conversion of its
+ // bounds, whose edges would no longer enclose the visible part.
+ #[test]
+ fn a_layer_too_wide_for_device_pixel_bounds_is_not_culled() {
+ let mut tree = identity_tree();
+ let far_left = tree.append_spatial(
+ transform(translation_matrix(-4.0e9, 0.0, 0.0), None),
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let layer = effect_under(&mut tree, effects(0.5), EffectNodeIndex::NONE, ClipNodeIndex::NONE);
+ let layer_context = ContextRef {
+ clip: ClipNodeIndex::NONE,
+ effect: layer,
+ ..ContextRef::default()
+ };
+ let runs = [
+ run(VISUAL_VIEWPORT_NODE_INDEX, layer_context, visible_bounds()),
+ run(far_left, layer_context, visible_bounds()),
+ ];
+ let mut painter = RecordingPainter::new();
+ painter.fully_clipped_x = i32::MIN;
+ replay(&tree, &runs, &mut painter);
+ assert!(painter.events.contains(&PainterEvent::Run(0)));
+ assert!(painter.events.contains(&PainterEvent::Run(1)));
+ }
+
+ #[test]
+ fn a_layer_whose_blur_reaches_the_clip_is_not_culled_by_its_unblurred_content() {
+ let mut tree = identity_tree();
+ let clip_node = root_clip(&mut tree, FloatRect::new(0.0, 0.0, 50.0, 50.0));
+ 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(2.0, 2.0, None).serialize(),
+ )),
+ backdrop_filter: None,
+ }),
+ EffectNodeIndex::NONE,
+ clip_node,
+ );
+ let blur_context = ContextRef {
+ clip: clip_node,
+ effect: blur,
+ ..ContextRef::default()
+ };
+ let runs = [run(
+ VISUAL_VIEWPORT_NODE_INDEX,
+ blur_context,
+ IntRect::new(999, 0, 10, 10),
+ )];
+ let mut painter = RecordingPainter::new();
+ painter.fully_clipped_x = 999;
+ let base = painter.base_matrix;
+ replay(&tree, &runs, &mut painter);
+ // Inside the layer the painter still culls the run by its own ink; a real canvas clip has
+ // grown by the blur there.
+ assert_eq!(
+ painter.events,
+ vec![
+ PainterEvent::SetMatrix(base),
+ PainterEvent::PushClip(FloatRect::new(0.0, 0.0, 50.0, 50.0)),
+ PainterEvent::PushLayer(1.0),
+ PainterEvent::Pop,
+ PainterEvent::Pop,
+ PainterEvent::SetMatrix(base),
+ ]
+ );
+ }
+
#[test]
fn mask_effects_pop_with_their_effect_index() {
let mut tree = identity_tree();
@@ -1443,15 +1565,15 @@ mod tests {
let clip_node = root_clip(&mut tree, FloatRect::new(0.0, 0.0, 50.0, 50.0));
let outer = effect_under(&mut tree, effects(0.5), EffectNodeIndex::NONE, clip_node);
let inner = effect_under(&mut tree, effects(0.25), outer, clip_node);
- let inner_context = ContextRef {
+ let context_in = |effect| ContextRef {
clip: clip_node,
- effect: inner,
+ effect,
..ContextRef::default()
};
let off_screen = IntRect::new(999, 0, 10, 10);
let runs = [
- run(VISUAL_VIEWPORT_NODE_INDEX, inner_context, off_screen),
- run(VISUAL_VIEWPORT_NODE_INDEX, inner_context, visible_bounds()),
+ run(VISUAL_VIEWPORT_NODE_INDEX, context_in(inner), off_screen),
+ run(VISUAL_VIEWPORT_NODE_INDEX, context_in(outer), visible_bounds()),
];
let mut painter = RecordingPainter::new();
painter.fully_clipped_x = 999;
@@ -1462,15 +1584,15 @@ mod tests {
vec![
PainterEvent::SetMatrix(base),
PainterEvent::PushClip(FloatRect::new(0.0, 0.0, 50.0, 50.0)),
+ PainterEvent::PushLayer(0.5),
+ PainterEvent::Pop,
PainterEvent::Pop,
PainterEvent::SetMatrix(base),
PainterEvent::PushClip(FloatRect::new(0.0, 0.0, 50.0, 50.0)),
PainterEvent::PushLayer(0.5),
- PainterEvent::PushLayer(0.25),
PainterEvent::Run(1),
PainterEvent::Pop,
PainterEvent::Pop,
- PainterEvent::Pop,
PainterEvent::SetMatrix(base),
]
);
diff --git a/Libraries/LibCompositing/Rust/src/filter_bytes.rs b/Libraries/LibCompositing/Rust/src/filter_bytes.rs
index c23b217f525..ec678bc7256 100644
--- a/Libraries/LibCompositing/Rust/src/filter_bytes.rs
+++ b/Libraries/LibCompositing/Rust/src/filter_bytes.rs
@@ -7,6 +7,7 @@
//! The filter graphs painting hands over as bytes, and the device-pixel filter functions hosts
//! build them from outside the style system.
+use libgfx_rust::FloatRect;
use libgfx_rust::filter::Filter;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -67,6 +68,12 @@ pub fn may_affect_output_bounds(bytes: &[u8]) -> bool {
Filter::serialized_may_affect_output_bounds(bytes)
}
+/// Where a serialized filter paints when what it is applied to paints within `source`, or `None`
+/// when it can paint everywhere.
+pub fn output_bounds(bytes: &[u8], source: FloatRect) -> Option<FloatRect> {
+ Filter::serialized_output_bounds(bytes, source)
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/Tests/LibWeb/Ref/expected/blurred-content-outside-overflow-clip-bleeds-in-ref.html b/Tests/LibWeb/Ref/expected/blurred-content-outside-overflow-clip-bleeds-in-ref.html
new file mode 100644
index 00000000000..f1335bfe04c
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/blurred-content-outside-overflow-clip-bleeds-in-ref.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<style>
+ html { background: white; }
+ body { margin: 0; }
+ #unclipped { width: 100px; height: 100px; }
+ #blurred { margin-left: 102px; width: 50px; height: 100px; background: blue; filter: blur(4px); }
+ .cover { position: absolute; background: white; }
+</style>
+<div id="unclipped"><div id="blurred"></div></div>
+<div class="cover" style="left: 100px; top: 0; width: 200px; height: 200px"></div>
+<div class="cover" style="left: 0; top: 100px; width: 300px; height: 100px"></div>
diff --git a/Tests/LibWeb/Ref/input/blurred-content-outside-overflow-clip-bleeds-in.html b/Tests/LibWeb/Ref/input/blurred-content-outside-overflow-clip-bleeds-in.html
new file mode 100644
index 00000000000..b239cd4e8de
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/blurred-content-outside-overflow-clip-bleeds-in.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/blurred-content-outside-overflow-clip-bleeds-in-ref.html">
+<style>
+ html { background: white; }
+ body { margin: 0; }
+ #clip { overflow: hidden; width: 100px; height: 100px; }
+ #blurred { margin-left: 102px; width: 50px; height: 100px; background: blue; filter: blur(4px); }
+</style>
+<!-- The box lies wholly outside the clip, but its blur spreads back inside it. -->
+<div id="clip"><div id="blurred"></div></div>