Skip to content

Truly independent web browser

LibWeb: Apply a translucent root background only once

sideshowbarkerefd0ef58c1da
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs
index 843e10d6b2b..d23b15b5110 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background_resolution.rs
@@ -885,6 +885,16 @@ pub(crate) fn has_background_to_paint(
})
}
+/// The root element paints its background over the whole canvas itself, unless it's hidden, transformed, or sticky — in
+/// which case the canvas fill paints it as well, where the root's own paint doesn't reach.
+pub(crate) fn root_paint_covers_canvas(arena: &impl PaintRead, root: NodeSlotId) -> bool {
+ arena.node_style_if_live(root).is_some_and(|style| {
+ style.visibility() == css_enums::visibility::VISIBLE
+ && !style_queries::has_css_transform(arena, root, style)
+ && !style_queries::is_sticky_position(style)
+ })
+}
+
/// The root background covers the viewport and everything it can scroll to. Moving the viewport
/// inside that area does not change the recorded background; growing it does.
pub(crate) fn root_background_canvas_rect(
diff --git a/Libraries/LibWeb/Rust/src/painting/record/producers.rs b/Libraries/LibWeb/Rust/src/painting/record/producers.rs
index 064788a9613..5e386e22104 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/producers.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/producers.rs
@@ -21,6 +21,7 @@ use crate::painting::force_dark::ForceDarkRole;
use crate::painting::paint_order_plan::{PaintOrderItem, PaintProducer, PaintScope, PaintScopePlan};
use crate::painting::paint_read::{GeometryRead, PaintRead, PaintSource};
use crate::painting::record::change_ticks::RowChanges;
+use crate::painting::record::paint::background_resolution::root_paint_covers_canvas;
use crate::painting::record::svg_resources::MaskLayerSet;
use crate::painting::{node_painting, paintable_geometry};
use libgfx_rust::Color;
@@ -81,11 +82,18 @@ impl<O: Observer> PaintRecorder<'_, O> {
);
root_group_color = canvas_color;
}
- this.recorder.fill_rect(
- inputs.bitmap_rect,
- inputs.uncaptured.background_color,
- ForceDarkRole::Background,
- );
+ // The root element paints its background over the canvas as well, wherever its own paint covers the canvas.
+ // So there, fill with it only when it's opaque, since painting it twice changes nothing: A translucent one
+ // would otherwise be applied twice.
+ if inputs.uncaptured.background_color.alpha() == 255
+ || !root_paint_covers_canvas(this.source, inputs.uncaptured.root_background_source.root_layout_node)
+ {
+ this.recorder.fill_rect(
+ inputs.bitmap_rect,
+ inputs.uncaptured.background_color,
+ ForceDarkRole::Background,
+ );
+ }
root_group_color = root_group_color.blend(
this.recorder
.resolve_color(inputs.uncaptured.background_color, ForceDarkRole::Background),
diff --git a/Tests/LibWeb/Ref/expected/transformed-root-background-blend-mode-ref.html b/Tests/LibWeb/Ref/expected/transformed-root-background-blend-mode-ref.html
new file mode 100644
index 00000000000..2b4ca4fd456
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/transformed-root-background-blend-mode-ref.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<style>
+ html { background: black; }
+</style>
diff --git a/Tests/LibWeb/Ref/expected/transformed-root-background-filter-ref.html b/Tests/LibWeb/Ref/expected/transformed-root-background-filter-ref.html
new file mode 100644
index 00000000000..dbd241578b1
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/transformed-root-background-filter-ref.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<style>
+ html { background: rgb(255, 255, 0); }
+</style>
diff --git a/Tests/LibWeb/Ref/expected/translucent-root-background-ref.html b/Tests/LibWeb/Ref/expected/translucent-root-background-ref.html
new file mode 100644
index 00000000000..6d098527afa
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/translucent-root-background-ref.html
@@ -0,0 +1,4 @@
+<!DOCTYPE html>
+<style>
+ html { background: rgb(153, 153, 255); }
+</style>
diff --git a/Tests/LibWeb/Ref/input/transformed-root-background-blend-mode.html b/Tests/LibWeb/Ref/input/transformed-root-background-blend-mode.html
new file mode 100644
index 00000000000..1c3643674b9
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/transformed-root-background-blend-mode.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/transformed-root-background-blend-mode-ref.html">
+<style>
+ /* The root's image layers blend with its background color, transformed or not: Red over blue multiplies to black. */
+ html { height: 100%; background: blue linear-gradient(red, red); background-blend-mode: multiply; transform: translate(0); }
+</style>
diff --git a/Tests/LibWeb/Ref/input/transformed-root-background-filter.html b/Tests/LibWeb/Ref/input/transformed-root-background-filter.html
new file mode 100644
index 00000000000..60680af76a6
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/transformed-root-background-filter.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/transformed-root-background-filter-ref.html">
+<style>
+ /* The root's filter applies to its background, transformed or not: Blue inverts to yellow. */
+ html { height: 100%; background: blue; filter: invert(1); transform: translate(0); }
+</style>
diff --git a/Tests/LibWeb/Ref/input/translucent-body-background-propagated-to-root.html b/Tests/LibWeb/Ref/input/translucent-body-background-propagated-to-root.html
new file mode 100644
index 00000000000..2b960ddc2aa
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/translucent-body-background-propagated-to-root.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/translucent-root-background-ref.html">
+<style>
+ /* 40% blue over the white canvas, applied once, is rgb(153, 153, 255). */
+ body { background: rgba(0, 0, 255, 0.4); }
+</style>
diff --git a/Tests/LibWeb/Ref/input/translucent-root-background-hidden-root.html b/Tests/LibWeb/Ref/input/translucent-root-background-hidden-root.html
new file mode 100644
index 00000000000..c54e857da21
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/translucent-root-background-hidden-root.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/translucent-root-background-ref.html">
+<style>
+ /* 40% blue over the white canvas, applied once, is rgb(153, 153, 255). */
+ html { background: rgba(0, 0, 255, 0.4); visibility: hidden; }
+</style>
diff --git a/Tests/LibWeb/Ref/input/translucent-root-background.html b/Tests/LibWeb/Ref/input/translucent-root-background.html
new file mode 100644
index 00000000000..2d5444f1364
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/translucent-root-background.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/translucent-root-background-ref.html">
+<style>
+ /* 40% blue over the white canvas, applied once, is rgb(153, 153, 255). */
+ html { background: rgba(0, 0, 255, 0.4); }
+</style>
diff --git a/Tests/LibWeb/Text/expected/display_list/inline-body-background-propagation.txt b/Tests/LibWeb/Text/expected/display_list/inline-body-background-propagation.txt
index 6f5dfa72654..2ad4936a6e8 100644
--- a/Tests/LibWeb/Text/expected/display_list/inline-body-background-propagation.txt
+++ b/Tests/LibWeb/Text/expected/display_list/inline-body-background-propagation.txt
@@ -1 +1 @@
-background rects: 2
+background rects: 1