Skip to content

Truly independent web browser

LibWeb+LibCompositing: Apply a box's own mask to its filtered backdrop

Andreas Kling651cec9d4206
diff --git a/Libraries/LibCompositing/Rust/src/display_list/damage.rs b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
index 899e0048035..bfff96c8671 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/damage.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
@@ -1825,6 +1825,7 @@ mod tests {
rect,
kind: MaskKind::Alpha,
origin: MaskLayerOrigin::CssMaskLayers,
+ masks_backdrop: false,
})
}
diff --git a/Libraries/LibCompositing/Rust/src/display_list/replay.rs b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
index 3cc14ea5bfe..d929fbc2c11 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
@@ -33,7 +33,8 @@ pub trait ReplayPainter {
fn push_clip(&mut self, clip: &ReplayClip);
fn push_clip_path(&mut self, path: &OwnedPath, winding_rule: WindingRule);
fn push_layer(&mut self, layer: &ReplayLayer);
- fn push_mask(&mut self, mask: &ReplayMask);
+ // `backdrop`, when given, carries the backdrop filter the mask's layer starts with.
+ fn push_mask(&mut self, mask: &ReplayMask, backdrop: Option<&ReplayLayer>);
fn pop_mask(&mut self, mask: &ReplayMask, effect: EffectNodeIndex);
fn pop(&mut self);
fn push_device_space_plane_clip(&mut self, vertices: &[FloatVector3]);
@@ -250,6 +251,14 @@ fn replay_layer_of(
}
}
+// Whether the backdrop of a backdrop-filter effect is drawn by its box's masks rather than by the effect's own layer.
+fn backdrop_is_drawn_by_masks(tree: &VisualContextTree, effect: EffectNodeIndex) -> bool {
+ let node = &tree.effect_nodes[effect.0 as usize];
+ matches!(&node.data, EffectNodeData::Effects(effects) if effects.backdrop_filter.is_some())
+ && !node.parent.is_none()
+ && matches!(&tree.effect_nodes[node.parent.0 as usize].data, EffectNodeData::Mask(mask) if mask.masks_backdrop)
+}
+
fn replay_mask_of(mask: &crate::visual_context::MaskData) -> ReplayMask {
ReplayMask {
rect: mask.rect,
@@ -429,6 +438,42 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
.then_some(bounds)
}
+ // https://drafts.fxtf.org/filter-effects-2/#backdrop-filter-operation
+ // The masks of a box with a backdrop filter apply to the filtered backdrop too. Skia reads a layer's
+ // backdrop from the layer's parent, so the outermost of the box's mask layers starts with the
+ // filtered backdrop, and the box's effects layer below the masks draws none. Opacity and blending
+ // come after masking, so the outermost mask layer applies them too.
+ fn backdrop_layer_of_outermost_mask(
+ &self,
+ target_index: usize,
+ mask: &crate::visual_context::MaskData,
+ ) -> Option<ReplayLayer> {
+ if !mask.masks_backdrop {
+ return None;
+ }
+ let tree = self.tree;
+ let Applied::Effect(mask_effect) = self.target[target_index] else {
+ unreachable!("a mask is an effect");
+ };
+ let parent = tree.effect_nodes[mask_effect.0 as usize].parent;
+ if !parent.is_none()
+ && matches!(&tree.effect_nodes[parent.0 as usize].data, EffectNodeData::Mask(parent) if parent.masks_backdrop)
+ {
+ return None;
+ }
+ self.target[target_index + 1..].iter().find_map(|entry| {
+ let Applied::Effect(effect) = *entry else {
+ return None;
+ };
+ match &tree.effect_nodes[effect.0 as usize].data {
+ EffectNodeData::Effects(effects) if backdrop_is_drawn_by_masks(tree, effect) => {
+ Some(replay_layer_of(effects, effect, None))
+ }
+ _ => None,
+ }
+ })
+ }
+
// 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) {
@@ -584,13 +629,22 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {
if tree.effect_pushes_layer_at_replay(effect) {
self.ensure_ctm_space(node.spatial);
let content_bounds = self.layer_content_bounds(effect, node.spatial);
- self.painter
- .push_layer(&replay_layer_of(effects, effect, content_bounds));
+ let mut layer = replay_layer_of(effects, effect, content_bounds);
+ if backdrop_is_drawn_by_masks(tree, effect) {
+ // NB: The outermost mask layer draws the backdrop, and applies the opacity and
+ // blend mode to it and the masked content together.
+ layer.backdrop_filter_bytes = std::ptr::null();
+ layer.backdrop_filter_bytes_size = 0;
+ layer.opacity = 1.0;
+ layer.blend_mode = libgfx_rust::CompositingAndBlendingOperator::Normal;
+ }
+ self.painter.push_layer(&layer);
}
}
EffectNodeData::Mask(mask) => {
self.ensure_ctm_space(node.spatial);
- self.painter.push_mask(&replay_mask_of(mask));
+ let backdrop = self.backdrop_layer_of_outermost_mask(i, mask);
+ self.painter.push_mask(&replay_mask_of(mask), backdrop.as_ref());
}
EffectNodeData::BackgroundColorAnimation => {}
EffectNodeData::Dead => unreachable!("a run never records under a tombstoned effect"),
@@ -907,7 +961,7 @@ mod tests {
self.events.push(PainterEvent::PushLayer(layer.opacity));
self.layer_content_bounds.push(layer.content_bounds.get());
}
- fn push_mask(&mut self, mask: &ReplayMask) {
+ fn push_mask(&mut self, mask: &ReplayMask, _backdrop: Option<&ReplayLayer>) {
self.events.push(PainterEvent::PushMask(mask.rect));
}
fn pop_mask(&mut self, _mask: &ReplayMask, effect: EffectNodeIndex) {
@@ -972,6 +1026,7 @@ mod tests {
rect,
kind: MaskKind::Alpha,
origin: MaskLayerOrigin::CssMaskLayers,
+ masks_backdrop: false,
})
}
diff --git a/Libraries/LibCompositing/Rust/src/host/replay.rs b/Libraries/LibCompositing/Rust/src/host/replay.rs
index c0a1b4d7dd3..b99d5e92746 100644
--- a/Libraries/LibCompositing/Rust/src/host/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/host/replay.rs
@@ -20,7 +20,8 @@ pub struct FfiDisplayListReplayCallbacks {
pub push_clip: unsafe extern "C" fn(*mut c_void, *const ReplayClip),
pub push_clip_path: unsafe extern "C" fn(*mut c_void, *const c_void, WindingRule),
pub push_layer: unsafe extern "C" fn(*mut c_void, *const ReplayLayer),
- pub push_mask: unsafe extern "C" fn(*mut c_void, *const ReplayMask),
+ // Arguments: the mask, and the layer whose backdrop filter the mask's layer starts with, or null.
+ pub push_mask: unsafe extern "C" fn(*mut c_void, *const ReplayMask, *const ReplayLayer),
pub pop_mask: unsafe extern "C" fn(*mut c_void, *const ReplayMask, EffectNodeIndex),
pub pop: unsafe extern "C" fn(*mut c_void),
pub push_device_space_plane_clip: unsafe extern "C" fn(*mut c_void, *const FloatVector3, usize),
@@ -61,9 +62,11 @@ impl ReplayPainter for FfiDisplayListReplayCallbacks {
unsafe { (self.push_layer)(self.context, layer) };
}
- fn push_mask(&mut self, mask: &ReplayMask) {
- // SAFETY: The C++ painter reads the mask synchronously.
- unsafe { (self.push_mask)(self.context, mask) };
+ fn push_mask(&mut self, mask: &ReplayMask, backdrop: Option<&ReplayLayer>) {
+ let backdrop = backdrop.map_or(std::ptr::null(), |backdrop| backdrop as *const ReplayLayer);
+ // SAFETY: The C++ painter reads the mask, the layer and its filter bytes synchronously; the tree keeps
+ // them alive.
+ unsafe { (self.push_mask)(self.context, mask, backdrop) };
}
fn pop_mask(&mut self, mask: &ReplayMask, effect: EffectNodeIndex) {
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/mod.rs b/Libraries/LibCompositing/Rust/src/visual_context/mod.rs
index 181aa8b97cd..e811ae2516a 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/mod.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/mod.rs
@@ -140,6 +140,9 @@ pub struct MaskData {
pub rect: IntRect,
pub kind: MaskKind,
pub origin: MaskLayerOrigin,
+ /// Whether the mask belongs to a box with a backdrop filter, whose effects node then sits
+ /// under the box's masks so that they mask the filtered backdrop along with the content.
+ pub masks_backdrop: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs b/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs
index e31cdd28704..e5e6523df3c 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs
@@ -28,7 +28,7 @@ use libgfx_rust::{
};
const SERIALIZED_TREE_MAGIC: u32 = 0x5443_5641;
-const SERIALIZED_TREE_FORMAT: u32 = 7;
+const SERIALIZED_TREE_FORMAT: u32 = 8;
const SPATIAL_KIND_SCROLL: u8 = 0;
const SPATIAL_KIND_STICKY: u8 = 1;
@@ -472,6 +472,7 @@ fn write_effect_data(writer: &mut TreeByteWriter, data: &EffectNodeData) {
writer.int_rect(mask.rect);
writer.i32(mask.kind as i32);
writer.u8(mask.origin as u8);
+ writer.bool(mask.masks_backdrop);
}
EffectNodeData::BackgroundColorAnimation => writer.u8(EFFECT_KIND_BACKGROUND_COLOR_ANIMATION),
EffectNodeData::Dead => writer.u8(EFFECT_KIND_DEAD),
@@ -506,6 +507,7 @@ fn read_effect_data(reader: &mut TreeByteReader<'_>) -> Option<EffectNodeData> {
rect: reader.int_rect()?,
kind: reader.mask_kind()?,
origin: reader.mask_layer_origin()?,
+ masks_backdrop: reader.bool()?,
}),
EFFECT_KIND_BACKGROUND_COLOR_ANIMATION => EffectNodeData::BackgroundColorAnimation,
EFFECT_KIND_DEAD => EffectNodeData::Dead,
@@ -1122,6 +1124,7 @@ mod tests {
rect: IntRect::new(1, 2, 3, 4),
kind: MaskKind::Luminance,
origin: MaskLayerOrigin::SvgClip,
+ masks_backdrop: true,
}),
effects,
sorting_root,
@@ -1878,6 +1881,7 @@ mod tests {
rect: IntRect::new(0, 0, 1, 1),
kind: MaskKind::Alpha,
origin: MaskLayerOrigin::CssMaskLayers,
+ masks_backdrop: false,
});
isolation_effect_is_a_mask.root_isolation_effect = Some(EffectNodeIndex(0));
rejected(&isolation_effect_is_a_mask);
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs
index 36ecc9f20a4..41b28412a00 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/box_build.rs
@@ -501,17 +501,19 @@ pub(crate) fn build_box_visual_context_nodes<Arena: PaintableRowsRead>(
append_clip_to_own_and_positioned_descendant_contexts!(ClipNodeData::Path(facts.clip_path_data().unwrap()));
}
+ for mask_layer in &facts.mask_layers {
+ append_shared_effect!(EffectNodeData::Mask(*mask_layer));
+ }
+
+ // 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 clip
// path clips the filtered backdrop along with the content, so its layer sits under the transform
- // and clip nodes rather than above them.
+ // and clip nodes rather than above them. Masks apply to the filtered backdrop too, so the layer
+ // also sits under the box's masks, and replay draws the backdrop into the outermost mask's layer.
if effects_layer_is_inside_transform_and_clips {
append_shared_effect!(EffectNodeData::Effects(facts.effects_data().unwrap()));
}
- for mask_layer in &facts.mask_layers {
- append_shared_effect!(EffectNodeData::Mask(*mask_layer));
- }
-
if facts.needs_compositor_background_color_effect {
append_shared_effect!(EffectNodeData::BackgroundColorAnimation);
}
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs
index 0599b4826cf..afa0ccc4594 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/build.rs
@@ -175,12 +175,14 @@ impl BoxFacts {
facts.clip_path = super::basic_shapes::compute_basic_shape_clip_path_data(layout_arena, slot, pixel_ratio)
.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)
.into_iter()
.map(|layer| MaskData {
rect: converter.enclosing_device_rect(layer.area),
kind: layer.kind,
origin: layer.origin,
+ masks_backdrop,
})
.collect();
facts.css_clip = super::node_values::compute_css_clip_data(layout_arena, slot, pixel_ratio);
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rs b/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rs
index 852efa82166..9971b0efff4 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_context/dump.rs
@@ -246,10 +246,11 @@ impl VisualContextTreeDump for VisualContextTree {
};
let _ = write!(
text,
- "mask=[{}] kind={} origin={}",
+ "mask=[{}] kind={} origin={}{}",
format_int_rect_components(mask.rect),
kind,
- origin
+ origin,
+ if mask.masks_backdrop { " masks-backdrop" } else { "" }
);
}
EffectNodeData::BackgroundColorAnimation => text.push_str("background-color-animation"),
@@ -600,6 +601,7 @@ mod node_dump_tests {
rect: IntRect::new(1, 2, 30, 40),
kind: MaskKind::Luminance,
origin: MaskLayerOrigin::SvgMask,
+ masks_backdrop: false,
}),
EffectNodeIndex::NONE,
VISUAL_VIEWPORT_NODE_INDEX,
diff --git a/Services/Compositor/DisplayListPlayer.cpp b/Services/Compositor/DisplayListPlayer.cpp
index 10cbd890f0a..d25f0dbe18f 100644
--- a/Services/Compositor/DisplayListPlayer.cpp
+++ b/Services/Compositor/DisplayListPlayer.cpp
@@ -80,7 +80,7 @@ struct DisplayListPlayer::ReplayCallbacks {
.push_clip = [](void* context, ReplayClip const* clip) { static_cast<DisplayListPlayer*>(context)->push_clip(*clip); },
.push_clip_path = [](void* context, void const* path, Gfx::WindingRule winding_rule) { static_cast<DisplayListPlayer*>(context)->push_clip_path(*static_cast<Gfx::Path const*>(path), winding_rule); },
.push_layer = [](void* context, ReplayLayer const* layer) { static_cast<DisplayListPlayer*>(context)->push_layer(*layer); },
- .push_mask = [](void* context, ReplayMask const* mask) { static_cast<DisplayListPlayer*>(context)->push_mask(*mask); },
+ .push_mask = [](void* context, ReplayMask const* mask, ReplayLayer const* backdrop) { static_cast<DisplayListPlayer*>(context)->push_mask(*mask, backdrop); },
.pop_mask = [](void* context, ReplayMask const* mask, EffectNodeIndex effect) { static_cast<DisplayListPlayer*>(context)->pop_mask(*mask, effect); },
.pop = [](void* context) { static_cast<DisplayListPlayer*>(context)->pop(); },
.push_device_space_plane_clip = [](void* context, Gfx::FloatVector3 const* vertices, size_t vertex_count) {
diff --git a/Services/Compositor/DisplayListPlayer.h b/Services/Compositor/DisplayListPlayer.h
index 4110ca0e07c..466c81d227c 100644
--- a/Services/Compositor/DisplayListPlayer.h
+++ b/Services/Compositor/DisplayListPlayer.h
@@ -72,7 +72,7 @@ private:
virtual void push_clip_path(Gfx::Path const&, Gfx::WindingRule) = 0;
virtual void push_transform(Gfx::AffineTransform const&) = 0;
virtual void push_layer(Compositing::ReplayLayer const&) = 0;
- virtual void push_mask(Compositing::ReplayMask const&) = 0;
+ virtual void push_mask(Compositing::ReplayMask const&, Compositing::ReplayLayer const* backdrop) = 0;
virtual void pop_mask(Compositing::ReplayMask const&, Compositing::EffectNodeIndex) = 0;
virtual void pop() = 0;
virtual void push_device_space_plane_clip(Gfx::Path const&) = 0;
diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cpp
index 8154be49299..57677c442bd 100644
--- a/Services/Compositor/DisplayListPlayerSkia.cpp
+++ b/Services/Compositor/DisplayListPlayerSkia.cpp
@@ -1564,11 +1564,23 @@ sk_sp<SkImageFilter> DisplayListPlayerSkia::image_filter_from_bytes(ReadonlyByte
});
}
-void DisplayListPlayerSkia::push_mask(ReplayMask const& mask)
+// A mask that applies to a box's filtered backdrop starts its layer with that backdrop, read from the
+// layer's parent, as push_layer() does for an unmasked box. The box's opacity and blend mode then apply
+// to the masked backdrop and content together.
+void DisplayListPlayerSkia::push_mask(ReplayMask const& mask, ReplayLayer const* backdrop)
{
auto& canvas = surface().canvas();
canvas.save();
canvas.clipRect(to_skia_rect(mask.rect.to_type<float>()), true);
+ if (backdrop && backdrop->backdrop_filter_bytes_size) {
+ SkPaint paint;
+ if (backdrop->opacity < 1.0f)
+ paint.setAlphaf(backdrop->opacity);
+ if (backdrop->blend_mode != Gfx::CompositingAndBlendingOperator::Normal)
+ paint.setBlender(Gfx::to_skia_blender(backdrop->blend_mode));
+ canvas.saveLayer(SkCanvas::SaveLayerRec(nullptr, &paint, backdrop_image_filter(*backdrop, true).get(), 0));
+ return;
+ }
canvas.saveLayer(nullptr, nullptr);
}
diff --git a/Services/Compositor/DisplayListPlayerSkia.h b/Services/Compositor/DisplayListPlayerSkia.h
index 386d7edf747..8edd6c02873 100644
--- a/Services/Compositor/DisplayListPlayerSkia.h
+++ b/Services/Compositor/DisplayListPlayerSkia.h
@@ -66,7 +66,7 @@ private:
void push_clip_path(Gfx::Path const&, Gfx::WindingRule) override;
void push_transform(Gfx::AffineTransform const&) override;
void push_layer(Compositing::ReplayLayer const&) override;
- void push_mask(Compositing::ReplayMask const&) override;
+ void push_mask(Compositing::ReplayMask const&, Compositing::ReplayLayer const* backdrop) override;
void pop_mask(Compositing::ReplayMask const&, Compositing::EffectNodeIndex) override;
void pop() override;
void push_device_space_plane_clip(Gfx::Path const&) override;
diff --git a/Tests/LibWeb/Ref/expected/visual-context/backdrop-filter-with-own-mask-ref.html b/Tests/LibWeb/Ref/expected/visual-context/backdrop-filter-with-own-mask-ref.html
new file mode 100644
index 00000000000..077f78ea62a
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/visual-context/backdrop-filter-with-own-mask-ref.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<style>
+body {
+ margin: 0;
+ background: white;
+}
+.backdrop {
+ position: absolute;
+ left: 0;
+ top: 0;
+ width: 400px;
+ height: 300px;
+ background: rgb(0, 255, 0);
+}
+.inverted {
+ position: absolute;
+ top: 50px;
+ width: 100px;
+ height: 100px;
+ background: rgb(255, 0, 255);
+}
+.tinted {
+ width: 100%;
+ height: 100%;
+ background: rgba(0, 0, 255, 0.5);
+}
+#a { left: 0; }
+#b { left: 150px; }
+#c {
+ left: 270px;
+ opacity: 0.5;
+}
+</style>
+<div class="backdrop"></div>
+<div id="a" class="inverted"></div>
+<div id="b" class="inverted"><div class="tinted"></div></div>
+<div id="c" class="inverted"></div>
diff --git a/Tests/LibWeb/Ref/input/visual-context/backdrop-filter-with-own-mask.html b/Tests/LibWeb/Ref/input/visual-context/backdrop-filter-with-own-mask.html
new file mode 100644
index 00000000000..fe6845c78cb
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/visual-context/backdrop-filter-with-own-mask.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<link rel="match" href="../../expected/visual-context/backdrop-filter-with-own-mask-ref.html">
+<style>
+body {
+ margin: 0;
+ background: white;
+}
+.backdrop {
+ position: absolute;
+ left: 0;
+ top: 0;
+ width: 400px;
+ height: 300px;
+ background: rgb(0, 255, 0);
+}
+.filtered {
+ position: absolute;
+ top: 50px;
+ width: 100px;
+ height: 200px;
+ backdrop-filter: invert(1);
+ mask-image: linear-gradient(to bottom, black 0, black 100px, transparent 100px);
+}
+#a { left: 0; }
+#b {
+ left: 150px;
+ background: rgba(0, 0, 255, 0.5);
+}
+#c {
+ left: 270px;
+ opacity: 0.5;
+}
+</style>
+<!-- A box's own mask applies to its filtered backdrop along with its content, so only the top half
+ of each box inverts the green behind it. The box's opacity applies to the masked backdrop too. -->
+<div class="backdrop"></div>
+<div id="a" class="filtered"></div>
+<div id="b" class="filtered"></div>
+<div id="c" class="filtered"></div>