Truly independent web browser
LibWeb: Ask which masks a box has once, when its facts are gathered
Aliaksandr Kalenik698a8529192a
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rsindex c834b061257..cb9200dfb11 100644--- a/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs@@ -44,9 +44,8 @@ pub(crate) fn stacking_context_paints(rows: &impl PaintRead, owner: NodeSlotId)/// Whether the SVG box paints nothing, and so can be hit nowhere, because a mask or clip path it references covers/// no area.fn svg_box_is_hidden_by_an_empty_mask_or_clip(rows: &impl PaintRead, svg_box: NodeSlotId) -> bool {- crate::painting::visual_context::node_values::mask_layer_presence(rows, svg_box, false)- .iter()- .any(|layer| layer.area.is_empty())+ rows.paintable_data(svg_box)+ .has_flag(PaintableFlag::HiddenByEmptySvgMaskOrClip)}/// The boxes below an SVG box that its producer walks: those a stacking context of their own does not paint.diff --git a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs b/Libraries/LibWeb/Rust/src/painting/paintable_data.rsindex f9af09f4377..425c5262d4b 100644--- a/Libraries/LibWeb/Rust/src/painting/paintable_data.rs+++ b/Libraries/LibWeb/Rust/src/painting/paintable_data.rs@@ -17,6 +17,8 @@ pub enum PaintableFlag {VerticalScrollbarHidden = 1 << 2,InLaterContainingBlockFragment = 1 << 3,HasLaterBoxFragments = 1 << 4,+ /// An SVG mask or clip path of the box covers no area, which leaves nothing of the box.+ HiddenByEmptySvgMaskOrClip = 1 << 5,}#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]diff --git a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rsindex 6a7533c2d61..eaf9c74b06e 100644--- a/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/svg_resources.rs@@ -16,10 +16,11 @@ use crate::painting::record::{PaintPhase, PaintRecorder};use crate::painting::visual_context::build::{BoxFacts, compute_svg_viewport_transform_data, svg_viewport_transform_of,};-use crate::painting::visual_context::node_values::{MaskLayerPresenceEntry, border_radii_data, mask_layer_presence};+use crate::painting::visual_context::node_values::border_radii_data;use crate::painting::visual_context::{ClipData, EffectNodeData, EffectNodeIndex, MaskData, MaskLayerOrigin};use libgfx_rust::path::Path;use libgfx_rust::{AffineTransform, CompositingAndBlendingOperator, FloatPoint, FloatRect, MaskKind, multiply_affine};+use smallvec::SmallVec;#[derive(Clone, Copy, PartialEq, Eq)]pub enum MaskLayerSet {@@ -107,61 +108,52 @@ fn transformed_int_rect_clip(transform: AffineTransform, rect: libgfx_rust::IntR}impl<O: Observer> PaintRecorder<'_, O> {- fn mask_layer_presence(&self, paintable: NodeSlotId, set: MaskLayerSet) -> Vec<MaskLayerPresenceEntry> {- mask_layer_presence(self.source, paintable, set == MaskLayerSet::CssAndSvg)- }-- fn mask_effect_of_layer(- &self,- paintable: NodeSlotId,- origin: MaskLayerOrigin,- ) -> Option<(EffectNodeIndex, MaskData)> {- let tree = self.paint_state.visual_context_tree.as_deref()?;+ /// The masks the visual context update gave `paintable` that are in `set`, each with the effect node that+ /// composites its content, outermost first.+ fn mask_effects_of(&self, paintable: NodeSlotId, set: MaskLayerSet) -> SmallVec<[(EffectNodeIndex, MaskData); 3]> {+ let Some(tree) = self.paint_state.visual_context_tree.as_deref() else {+ return SmallVec::new();+ };self.source.with_paintable_visual_context_node_handles(paintable, |handles| {handles.effects.iter()- .find_map(|effect| match &tree.effect_nodes[effect.0 as usize].data {- EffectNodeData::Mask(mask) if mask.origin == origin => Some((*effect, *mask)),+ .filter_map(|effect| match &tree.effect_nodes[effect.0 as usize].data {+ EffectNodeData::Mask(mask)+ if set == MaskLayerSet::CssAndSvg || mask.origin != MaskLayerOrigin::CssMaskLayers =>+ {+ Some((*effect, *mask))+ }_ => None,})+ .collect()})}+ /// Declares the content of the masks of `paintable` that are in `set`. Returns whether an SVG mask or clip path+ /// that covers no area leaves nothing of an SVG box, whose masks then declare no content.pub(crate) fn declare_mask_contents(&mut self, paintable: NodeSlotId, set: MaskLayerSet) -> bool {- let presence = self.mask_layer_presence(paintable, set);- if presence.is_empty() {- return false;- }- if set == MaskLayerSet::SvgOnly && presence.iter().any(|layer| layer.area.is_empty()) {+ let masks = self.mask_effects_of(paintable, set);+ if set == MaskLayerSet::SvgOnly && masks.iter().any(|(_, mask)| mask.rect.is_empty()) {return true;}- let stacking_context_site_consumes_the_layer = set == MaskLayerSet::CssAndSvg- && crate::painting::style_queries::establishes_stacking_context(self.source, paintable);- for layer in &presence {- if set == MaskLayerSet::SvgOnly && layer.origin == MaskLayerOrigin::CssMaskLayers {- continue;- }- let Some((effect, mask)) = self.mask_effect_of_layer(paintable, layer.origin) else {- continue;- };+ for (effect, mask) in masks {let group = self.recorder.begin_mask_content();- match layer.origin {+ match mask.origin {MaskLayerOrigin::CssMaskLayers => {+ let area = crate::painting::paintable_geometry::absolute_border_box_rect(self.source, paintable);self.trace_paint(Operation::Named(Some(paintable), "css-mask"), |this| {- this.paint_css_mask_layers(paintable, layer.area);+ this.paint_css_mask_layers(paintable, area);});}MaskLayerOrigin::SvgMask | MaskLayerOrigin::SvgClip => {- if !layer.area.is_empty() || stacking_context_site_consumes_the_layer {- self.record_referenced_svg_mask_or_clip_content(- paintable,- layer.origin,- AffineTransform::identity(),- mask.rect.to_float(),- );- }+ self.record_referenced_svg_mask_or_clip_content(+ paintable,+ mask.origin,+ AffineTransform::identity(),+ mask.rect.to_float(),+ );}}self.recorder.finish_mask_content(group, effect, mask.rect);@@ -321,15 +313,11 @@ impl<O: Observer> PaintRecorder<'_, O> {let walk = self.svg_resource_walk.expect("resource content paints inside a resource walk");- if self- .mask_layer_presence(svg_box, MaskLayerSet::SvgOnly)- .iter()- .any(|layer| layer.area.is_empty())- {- return;- }let (facts, svg_filter_bounds) =BoxFacts::gather_for_resource_content(self.source, svg_box, self.inputs.device_pixels_per_css_pixel);+ if facts.is_hidden_by_empty_svg_mask_or_clip() {+ return;+ }self.recorder.set_accumulated_visual_context(walk.enclosing_context);let effects_group = factsdiff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rsindex 6ba59500412..68373832928 100644--- a/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs@@ -32,6 +32,7 @@ pub(crate) struct PaintableVisualContextAssignment {pub accumulated_visual_context: ContextRef,pub accumulated_visual_context_for_descendants: ContextRef,pub has_non_invertible_css_transform: bool,+ pub hidden_by_empty_svg_mask_or_clip: bool,pub record: PaintableVisualContextRecord,pub node_handles: BoxVisualContextNodeHandles,/// The bounds of the SVG filter the box references, as the build gathered them, which the row takes as the@@ -49,6 +50,7 @@ impl PaintableVisualContextAssignment {accumulated_visual_context: data.accumulated_visual_context,accumulated_visual_context_for_descendants: data.accumulated_visual_context_for_descendants,has_non_invertible_css_transform: data.has_flag(PaintableFlag::HasNonInvertibleCssTransform),+ hidden_by_empty_svg_mask_or_clip: data.has_flag(PaintableFlag::HiddenByEmptySvgMaskOrClip),record,node_handles: BoxVisualContextNodeHandles::default(),gathered_svg_filter_bounds: None,@@ -87,6 +89,10 @@ impl PaintableVisualContextAssignment {PaintableFlag::HasNonInvertibleCssTransform,self.has_non_invertible_css_transform,);+ data.set_flag(+ PaintableFlag::HiddenByEmptySvgMaskOrClip,+ self.hidden_by_empty_svg_mask_or_clip,+ );}layout_arena.set_paintable_visual_context_record(self.slot, self.record, self.node_handles);if scroll_nodes_changed {@@ -470,6 +476,7 @@ pub(crate) fn build_box_visual_context_nodes<Arena: PaintableRowsRead>(for mask_layer in &facts.mask_layers {append_shared_effect!(EffectNodeData::Mask(*mask_layer));}+ assignment.hidden_by_empty_svg_mask_or_clip = facts.is_hidden_by_empty_svg_mask_or_clip();// https://drafts.fxtf.org/filter-effects-2/#backdrop-filter-operation// A backdrop filter is limited to the box's rounded border box in the box's own space, and a clipdiff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/build.rsindex 95c27d2940b..fd2ad561491 100644--- a/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs@@ -90,6 +90,13 @@ impl BoxFacts {})}+ /// Whether an SVG mask or clip path of the box covers no area, which leaves nothing of the box.+ pub(crate) fn is_hidden_by_empty_svg_mask_or_clip(&self) -> bool {+ self.mask_layers+ .iter()+ .any(|mask| mask.origin != super::MaskLayerOrigin::CssMaskLayers && mask.rect.is_empty())+ }+/// Whether the box's effects layer sits under its transform, clip and mask nodes, rather than above them: for a/// backdrop filter, and for a filter the box's clip-path, clip or mask has to apply to.pub(crate) fn effects_layer_is_inside_transform_and_clips(&self) -> bool {@@ -209,7 +216,7 @@ impl BoxFacts {.map(|(path, bounding_rect, fill_rule)| (std::sync::Arc::new(path), bounding_rect, fill_rule));let converter = crate::painting::display_list::device_pixels::DevicePixelConverter::new(pixel_ratio);let masks_backdrop = facts.effects_layer_is_inside_transform_and_clips();- facts.mask_layers = super::node_values::mask_layer_presence(layout_arena, slot, true)+ facts.mask_layers = super::node_values::mask_layer_presence(layout_arena, slot).into_iter().map(|layer| MaskData {rect: converter.enclosing_device_rect(layer.area),diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rsindex 8ececb5f5e8..7fad672a1ba 100644--- a/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/node_values.rs@@ -997,34 +997,29 @@ fn compute_backdrop_filter_data(})}-pub(crate) struct MaskLayerPresenceEntry {+pub(super) struct MaskLayerPresenceEntry {pub origin: super::MaskLayerOrigin,pub area: CssPixelRect,pub kind: libgfx_rust::MaskKind,}-pub(crate) fn mask_layer_presence(- layout_arena: &impl PaintRead,- slot: NodeSlotId,- include_css_mask_layers: bool,-) -> Vec<MaskLayerPresenceEntry> {+/// The masks of the box at `slot`, outermost first. Gathering the facts of a box is the one place that asks: what+/// records the content of the masks and what hit tests the box read the answer from there.+pub(super) fn mask_layer_presence(layout_arena: &impl PaintRead, slot: NodeSlotId) -> Vec<MaskLayerPresenceEntry> {use super::MaskLayerOrigin;let mut layers = Vec::new();- if include_css_mask_layers {- let node = slot;- if let Some(style) = layout_arena.node_style_if_live(node)- && style_queries::mask_layers_have_image(style.mask())- && layout_arena- .node_kind_if_live(node)- .is_some_and(node_facts::kind_is_box)- && style_queries::establishes_stacking_context(layout_arena, node)- {- layers.push(MaskLayerPresenceEntry {- origin: MaskLayerOrigin::CssMaskLayers,- area: paintable_geometry::absolute_border_box_rect(layout_arena, slot),- kind: libgfx_rust::MaskKind::Alpha,- });- }+ if let Some(style) = layout_arena.node_style_if_live(slot)+ && style_queries::mask_layers_have_image(style.mask())+ && layout_arena+ .node_kind_if_live(slot)+ .is_some_and(node_facts::kind_is_box)+ && style_queries::establishes_stacking_context(layout_arena, slot)+ {+ layers.push(MaskLayerPresenceEntry {+ origin: MaskLayerOrigin::CssMaskLayers,+ area: paintable_geometry::absolute_border_box_rect(layout_arena, slot),+ kind: libgfx_rust::MaskKind::Alpha,+ });}if layout_arena.node_kind_if_live(slot)