Skip to content

Truly independent web browser

LibWeb: Keep fixed backgrounds clipped to their box while scrolling

Andreas Klingb1fa190f2b37
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
index bdfb5ab7076..5e4a1d0361e 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
@@ -732,6 +732,10 @@ fn dump_inline_clips(output: &mut String, header: &DisplayListCommandHeader, pay
if clip.mode == ClipMode::Difference {
output.push_str(" mode=difference");
}
+ if clip.has_own_space {
+ output.push_str(" in ");
+ write_spatial_node_index(output, clip.space);
+ }
}
output.push(']');
}
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
index 7ca0bfc6c33..93e6830e66d 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
@@ -329,6 +329,26 @@ impl DisplayListRecorder {
self.ambient_inline_clips.extend_from_slice(inline_clips);
}
+ /// Whether one of the ambient inline clips already keeps what is recorded under it within `rect`.
+ pub fn has_ambient_inline_clip_within(&self, rect: FloatRect) -> bool {
+ self.ambient_inline_clips.iter().any(|clip| clip.lies_within(rect))
+ }
+
+ /// Makes every ambient inline clip apply in `space`, whatever space the commands recorded under them draw in,
+ /// and hands back the clips as they were, for the caller to restore with [`Self::replace_ambient_inline_clips`].
+ pub fn move_ambient_inline_clips_to_space(&mut self, space: SpatialNodeIndex) -> Vec<PendingInlineClip> {
+ let clips_in_space = self
+ .ambient_inline_clips
+ .iter()
+ .map(|clip| clip.in_space(space))
+ .collect();
+ self.replace_ambient_inline_clips(clips_in_space)
+ }
+
+ pub fn replace_ambient_inline_clips(&mut self, inline_clips: Vec<PendingInlineClip>) -> Vec<PendingInlineClip> {
+ std::mem::replace(&mut self.ambient_inline_clips, inline_clips)
+ }
+
pub fn truncate_ambient_inline_clips(&mut self, enclosing_scope_clip_count: usize) {
self.ambient_inline_clips.truncate(enclosing_scope_clip_count);
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
index 961e6745c60..b60230a4b1c 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs
@@ -511,6 +511,98 @@ fn paint_image_layer<O: Observer>(
compositing_and_blending_operator: CompositingAndBlendingOperator,
backdrop: LayerBackdrop,
visible_device_rect: Option<IntRect>,
+) {
+ let paint_in_current_space = |recorder: &mut PaintRecorder<'_, O>,
+ css_clip_rect: CssPixelRect,
+ clip_rect: IntRect,
+ visible_device_rect: Option<IntRect>| {
+ paint_image_layer_in_current_space(
+ recorder,
+ paintable,
+ layer,
+ image_rendering,
+ css_clip_rect,
+ clip_rect,
+ compositing_and_blending_operator,
+ backdrop,
+ visible_device_rect,
+ );
+ };
+
+ if layer.attachment != css_enums::background_attachment::FIXED
+ || recorder.recorder.is_recording_inside_group()
+ || !background_has_fixed_attachment(
+ recorder.source,
+ recorder.inputs.uncaptured.root_background_source,
+ paintable,
+ )
+ {
+ paint_in_current_space(recorder, css_clip_rect, clip_rect, visible_device_rect);
+ return;
+ }
+
+ // Select an existing space above scrolling, retaining the clips and effects of the background paint.
+ // Background attachment doesn't add AVC nodes.
+ let tree = recorder.paint_state.visual_context_tree.as_ref().unwrap();
+ let mut spatial = recorder.data(paintable).accumulated_visual_context.spatial;
+ let mut index = spatial;
+ while index != VISUAL_VIEWPORT_NODE_INDEX {
+ let node = &tree.spatial_nodes[index.0 as usize];
+ if node.data.is_scroll_like() {
+ spatial = node.parent;
+ }
+ index = node.parent;
+ }
+ let previous_context = recorder.recorder.accumulated_visual_context();
+ if spatial == previous_context.spatial {
+ paint_in_current_space(recorder, css_clip_rect, clip_rect, visible_device_rect);
+ return;
+ }
+ // NB: Tiling over the viewport would record draws that the empty clip of the layer then hides.
+ if clip_rect.is_empty() || visible_device_rect.is_some_and(|rect| rect.is_empty()) {
+ return;
+ }
+
+ // NB: The layer's clips stay in the space of the box, so the compositor keeps clipping the background to the box
+ // as it scrolls the box without a new recording. As the box can then be anywhere in the viewport, the tiles
+ // cover all of it, and the clip and visible rects of the layer, which are in the space of the box, become clips
+ // in that space too.
+ let viewport_rect = CssPixelRect {
+ x: CssPixels::from_raw(0),
+ y: CssPixels::from_raw(0),
+ ..recorder.inputs.css_viewport_rect
+ };
+ let viewport_device_rect = recorder.converter.rounded_device_rect(viewport_rect);
+
+ let box_space_clips: Vec<PendingInlineClip> = std::iter::once(clip_rect)
+ .chain(visible_device_rect)
+ .filter(|rect| !recorder.recorder.has_ambient_inline_clip_within(rect.to_float()))
+ .map(|rect| PendingInlineClip::intersecting_device_rect(rect).in_space(previous_context.spatial))
+ .collect();
+ let previous_inline_clips = recorder
+ .recorder
+ .move_ambient_inline_clips_to_space(previous_context.spatial);
+ recorder.recorder.push_ambient_inline_clips(&box_space_clips);
+ recorder.recorder.set_accumulated_visual_context(ContextRef {
+ spatial,
+ ..previous_context
+ });
+ paint_in_current_space(recorder, viewport_rect, viewport_device_rect, None);
+ recorder.recorder.set_accumulated_visual_context(previous_context);
+ recorder.recorder.replace_ambient_inline_clips(previous_inline_clips);
+}
+
+#[allow(clippy::too_many_arguments)]
+fn paint_image_layer_in_current_space<O: Observer>(
+ recorder: &mut PaintRecorder<'_, O>,
+ paintable: NodeSlotId,
+ layer: &ResolvedBackgroundLayer<'_>,
+ image_rendering: u8,
+ css_clip_rect: CssPixelRect,
+ clip_rect: IntRect,
+ compositing_and_blending_operator: CompositingAndBlendingOperator,
+ backdrop: LayerBackdrop,
+ visible_device_rect: Option<IntRect>,
) {
let converter = recorder.converter;
let image = layer.image.expect("an imageless layer never reaches the image paint");
@@ -525,32 +617,6 @@ fn paint_image_layer<O: Observer>(
let mut background_positioning_area = layer.background_positioning_area;
match layer.attachment {
- css_enums::background_attachment::FIXED => {
- if !recorder.recorder.is_recording_inside_group()
- && background_has_fixed_attachment(
- recorder.source,
- recorder.inputs.uncaptured.root_background_source,
- paintable,
- )
- {
- // Select an existing space above scrolling, retaining the clips and effects
- // of the background paint. Background attachment doesn't add AVC nodes.
- let tree = recorder.paint_state.visual_context_tree.as_ref().unwrap();
- let mut spatial = recorder.data(paintable).accumulated_visual_context.spatial;
- let mut index = spatial;
- while index != VISUAL_VIEWPORT_NODE_INDEX {
- let node = &tree.spatial_nodes[index.0 as usize];
- if node.data.is_scroll_like() {
- spatial = node.parent;
- }
- index = node.parent;
- }
- let context = recorder.recorder.accumulated_visual_context();
- recorder
- .recorder
- .set_accumulated_visual_context(ContextRef { spatial, ..context });
- }
- }
css_enums::background_attachment::LOCAL
if recorder.source.node_kind_if_live(paintable) != Some(NodeKind::Viewport) =>
{
diff --git a/Tests/LibWeb/Ref/expected/background-attachment-fixed-clipped-after-scroll-ref.html b/Tests/LibWeb/Ref/expected/background-attachment-fixed-clipped-after-scroll-ref.html
new file mode 100644
index 00000000000..bbf3afd09a6
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/background-attachment-fixed-clipped-after-scroll-ref.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<style>
+ html {
+ scrollbar-width: none;
+ }
+ body {
+ margin: 0;
+ height: 2200px;
+ }
+ #partially-visible {
+ position: absolute;
+ top: 150px;
+ width: 400px;
+ height: 200px;
+ background:
+ linear-gradient(blue, blue) 0 0 / 50px 50px no-repeat,
+ linear-gradient(green 50%, lime 50%) 0 150px / 100px 100px;
+ }
+</style>
+<div id="partially-visible"></div>
diff --git a/Tests/LibWeb/Ref/expected/background-attachment-fixed-fragmented-after-scroll-ref.html b/Tests/LibWeb/Ref/expected/background-attachment-fixed-fragmented-after-scroll-ref.html
new file mode 100644
index 00000000000..f6f15f54aeb
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/background-attachment-fixed-fragmented-after-scroll-ref.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ #spacer {
+ height: 1000px;
+ }
+ #columns {
+ columns: 2;
+ column-gap: 0;
+ width: 400px;
+ height: 200px;
+ }
+ #fragmented {
+ height: 400px;
+ background: green;
+ }
+ #tail {
+ height: 1000px;
+ }
+</style>
+<div id="spacer"></div>
+<div id="columns"><div id="fragmented"></div></div>
+<div id="tail"></div>
+<script>
+ window.scrollTo(0, 1000);
+</script>
diff --git a/Tests/LibWeb/Ref/input/background-attachment-fixed-clipped-after-scroll.html b/Tests/LibWeb/Ref/input/background-attachment-fixed-clipped-after-scroll.html
new file mode 100644
index 00000000000..6b1922b58c9
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/background-attachment-fixed-clipped-after-scroll.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/background-attachment-fixed-clipped-after-scroll-ref.html">
+<style>
+ html {
+ scrollbar-width: none;
+ }
+ body {
+ margin: 0;
+ }
+ #scrolled-out {
+ height: 300px;
+ background: linear-gradient(red, red) fixed;
+ }
+ #spacer {
+ height: 700px;
+ }
+ #partially-visible {
+ width: 400px;
+ height: 200px;
+ background:
+ linear-gradient(blue, blue) 0 0 / 50px 50px no-repeat,
+ linear-gradient(green 50%, lime 50%) 0 300px / 100px 100px fixed;
+ }
+ #tail {
+ height: 1000px;
+ }
+</style>
+<div id="scrolled-out"></div>
+<div id="spacer"></div>
+<div id="partially-visible"></div>
+<div id="tail"></div>
+<script>
+ window.scrollTo(0, 850);
+</script>
diff --git a/Tests/LibWeb/Ref/input/background-attachment-fixed-fragmented-after-scroll.html b/Tests/LibWeb/Ref/input/background-attachment-fixed-fragmented-after-scroll.html
new file mode 100644
index 00000000000..44f00e654a7
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/background-attachment-fixed-fragmented-after-scroll.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/background-attachment-fixed-fragmented-after-scroll-ref.html">
+<style>
+ body {
+ margin: 0;
+ }
+ #spacer {
+ height: 1000px;
+ }
+ #columns {
+ columns: 2;
+ column-gap: 0;
+ width: 400px;
+ height: 200px;
+ }
+ #fragmented {
+ height: 400px;
+ background: linear-gradient(green, green) fixed;
+ }
+ #tail {
+ height: 1000px;
+ }
+</style>
+<div id="spacer"></div>
+<div id="columns"><div id="fragmented"></div></div>
+<div id="tail"></div>
+<script>
+ window.scrollTo(0, 1000);
+</script>
diff --git a/Tests/LibWeb/Text/expected/display_list/background-attachment-change-repaints.txt b/Tests/LibWeb/Text/expected/display_list/background-attachment-change-repaints.txt
index fa22166ecf6..29f04930b43 100644
--- a/Tests/LibWeb/Text/expected/display_list/background-attachment-change-repaints.txt
+++ b/Tests/LibWeb/Text/expected/display_list/background-attachment-change-repaints.txt
@@ -17,5 +17,5 @@ AccumulatedVisualContext Tree:
[e0 in s0] effects=[]
DisplayList:
-PaintLinearGradient@s0/e0 rect=[0,0 800x600] inline_clips=[clip=[0,0 200x200]]
+PaintLinearGradient@s0/e0 rect=[0,0 800x600] inline_clips=[clip=[0,0 200x200] in s1]
diff --git a/Tests/LibWeb/Text/expected/display_list/background-attachment-fixed.txt b/Tests/LibWeb/Text/expected/display_list/background-attachment-fixed.txt
index 919b6a1f1d5..e34d12d1d3b 100644
--- a/Tests/LibWeb/Text/expected/display_list/background-attachment-fixed.txt
+++ b/Tests/LibWeb/Text/expected/display_list/background-attachment-fixed.txt
@@ -6,5 +6,5 @@ AccumulatedVisualContext Tree:
DisplayList:
CompositorScrollNode@s0/e0 scroll_node_index=s1 parent_scroll_node_index=s0 scrollport_rect=[0,0 800x600] min_scroll_offset=[0,0] max_scroll_offset=[0,613] is_viewport=true
-PaintLinearGradient@s0/e0 rect=[0,0 800x600]
+PaintLinearGradient@s0/e0 rect=[0,0 800x600] inline_clips=[clip=[0,0 800x1213] in s1]