Skip to content

Truly independent web browser

LibCompositing: Cull 3D contexts nested in a hidden backface

sideshowbarker830bdd3775c5
diff --git a/Libraries/LibCompositing/Rust/src/display_list/replay.rs b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
index 0312aac2e82..b0d3f9ae13c 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
@@ -18,7 +18,7 @@ use crate::display_list::ffi_bytes::FfiBytes;
use crate::display_list::nested_records::span_bytes;
use crate::visual_context::queries::TreeCullingScratch;
use crate::visual_context::{
- ClipNodeData, ContextRef, EffectNodeData, SpatialData, VisualContextTree, device_offset_for_index,
+ ClipNodeData, ContextRef, EffectNodeData, SpatialData, SpatialNode, VisualContextTree, device_offset_for_index,
resolve_leaf_to_context_matrices, should_cull_back_face,
};
use libgfx_rust::path::Path;
@@ -368,6 +368,27 @@ fn int_rect_enclosing(rect: FloatRect) -> Option<IntRect> {
.then(|| enclosing_int_rect(rect))
}
+// The cull state of the element that establishes the 3D rendering context rooted at root: the cull state of the
+// plane the context is flattened into.
+fn backface_culled_outside_sorting_context(
+ spatial_nodes: &[SpatialNode],
+ backface_culled: &[bool],
+ root: SpatialNodeIndex,
+) -> bool {
+ let mut index = root.0 as usize;
+ loop {
+ if let SpatialData::Transform(transform) = &spatial_nodes[index].data
+ && transform.establishes_sorting_context
+ {
+ return backface_culled[index];
+ }
+ if index == 0 {
+ return false;
+ }
+ index = spatial_nodes[index].parent.0 as usize;
+ }
+}
+
fn fill_replay_palette_in_dependency_order(
tree: &VisualContextTree,
spatial_dependency_order: &[u32],
@@ -427,8 +448,16 @@ fn fill_replay_palette_in_dependency_order(
transform.matrix_including_origin(),
transform.flattens_inherited_transform,
);
- if transform.sorting_context_root_index.is_some() || transform.establishes_sorting_context {
- palette.backface_culled[i] = false;
+ // https://drafts.csswg.org/css-transforms-2/#3d-rendering-contexts
+ // A participant in a 3D rendering context is its own plane, so the hidden backfaces of other planes in
+ // the context don't cull it. But a context is part of the content flattened into the plane its
+ // establishing element sits in, so whatever culls that plane culls the whole context: A participant
+ // starts from the cull state of the element that establishes its context. The spec doesn't yet say if a
+ // hidden backface culls the 3D rendering contexts nested in it:
+ // https://github.com/w3c/csswg-drafts/issues/918
+ if let Some(root) = transform.sorting_context_root_index {
+ palette.backface_culled[i] =
+ backface_culled_outside_sorting_context(spatial_nodes, &palette.backface_culled, root);
}
}
SpatialData::Perspective(perspective) => {
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs b/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
index c0c1367b90c..409b6bcd6a1 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
@@ -68,6 +68,9 @@ struct SpatialChainWalk {
needs_accumulated_matrices: bool,
has_3d_transform: bool,
culled: bool,
+ // The cull state of the plane the current 3D rendering context is flattened into. A root path runs through at
+ // most one context at a time, since a nested context only starts below an element that flattens.
+ culled_outside_sorting_context: bool,
accumulated_matrices: Vec<FloatMatrix4x4>,
point: FloatPoint,
applied_steps: usize,
@@ -174,6 +177,7 @@ impl VisualContextTree {
needs_accumulated_matrices,
has_3d_transform,
culled: false,
+ culled_outside_sorting_context: false,
accumulated_matrices,
point: screen_point,
applied_steps: 0,
@@ -205,10 +209,17 @@ impl VisualContextTree {
}
}
- if let SpatialData::Transform(transform) = &node.data
- && (transform.sorting_context_root_index.is_some() || transform.establishes_sorting_context)
- {
- walk.culled = false;
+ // https://drafts.csswg.org/css-transforms-2/#3d-rendering-contexts
+ // A participant in a 3D rendering context is its own plane, culled only by its own hidden backface or by
+ // whatever culls the plane its context is flattened into. The spec doesn't yet say if a hidden backface culls
+ // the 3D rendering contexts nested in it:
+ // https://github.com/w3c/csswg-drafts/issues/918
+ if let SpatialData::Transform(transform) = &node.data {
+ if transform.establishes_sorting_context {
+ walk.culled_outside_sorting_context = walk.culled;
+ } else if transform.sorting_context_root_index.is_some() {
+ walk.culled = walk.culled_outside_sorting_context;
+ }
}
if walk.has_3d_transform && !matches!(node.data, SpatialData::BackfaceVisibility(_)) {
diff --git a/Tests/LibWeb/Ref/expected/css/backface-hidden-ancestor-culls-descendant-3d-contexts-ref.html b/Tests/LibWeb/Ref/expected/css/backface-hidden-ancestor-culls-descendant-3d-contexts-ref.html
new file mode 100644
index 00000000000..63ae2aedd92
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css/backface-hidden-ancestor-culls-descendant-3d-contexts-ref.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ background: white;
+ }
+ div {
+ position: absolute;
+ top: 50px;
+ left: 50px;
+ width: 300px;
+ height: 300px;
+ background: green;
+ }
+</style>
+<div></div>
diff --git a/Tests/LibWeb/Ref/expected/css/backface-hidden-ancestor-keeps-descendant-3d-context-ref.html b/Tests/LibWeb/Ref/expected/css/backface-hidden-ancestor-keeps-descendant-3d-context-ref.html
deleted file mode 100644
index f356d05d46c..00000000000
--- a/Tests/LibWeb/Ref/expected/css/backface-hidden-ancestor-keeps-descendant-3d-context-ref.html
+++ /dev/null
@@ -1,34 +0,0 @@
-<!DOCTYPE html>
-<style>
- body {
- margin: 0;
- background: white;
- }
- div {
- position: absolute;
- }
- .context {
- top: 50px;
- left: 150px;
- width: 200px;
- height: 200px;
- background: green;
- }
- .participant {
- top: 50px;
- left: 250px;
- width: 100px;
- height: 100px;
- background: orange;
- }
- .lone-context {
- top: 50px;
- left: 70px;
- width: 60px;
- height: 60px;
- background: purple;
- }
-</style>
-<div class="context"></div>
-<div class="participant"></div>
-<div class="lone-context"></div>
diff --git a/Tests/LibWeb/Ref/expected/css/backface-hidden-participant-culls-nested-3d-context-ref.html b/Tests/LibWeb/Ref/expected/css/backface-hidden-participant-culls-nested-3d-context-ref.html
new file mode 100644
index 00000000000..5c2b88ec082
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css/backface-hidden-participant-culls-nested-3d-context-ref.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ div {
+ position: absolute;
+ top: 50px;
+ left: 50px;
+ width: 200px;
+ height: 200px;
+ background: green;
+ }
+</style>
+<div></div>
diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-transforms/composited-under-rotateY-180deg-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-transforms/composited-under-rotateY-180deg-ref.html
new file mode 100644
index 00000000000..ebd2d860cec
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-transforms/composited-under-rotateY-180deg-ref.html
@@ -0,0 +1,2 @@
+<!DOCTYPE html>
+<div style="width: 100px; height: 100px; background: green"></div>
diff --git a/Tests/LibWeb/Ref/input/css/backface-hidden-ancestor-keeps-descendant-3d-context.html b/Tests/LibWeb/Ref/input/css/backface-hidden-ancestor-culls-descendant-3d-contexts.html
similarity index 67%
rename from Tests/LibWeb/Ref/input/css/backface-hidden-ancestor-keeps-descendant-3d-context.html
rename to Tests/LibWeb/Ref/input/css/backface-hidden-ancestor-culls-descendant-3d-contexts.html
index e6d81588bfc..b7e3ab27a0d 100644
--- a/Tests/LibWeb/Ref/input/css/backface-hidden-ancestor-keeps-descendant-3d-context.html
+++ b/Tests/LibWeb/Ref/input/css/backface-hidden-ancestor-culls-descendant-3d-contexts.html
@@ -1,17 +1,25 @@
<!DOCTYPE html>
-<link rel="match" href="../../expected/css/backface-hidden-ancestor-keeps-descendant-3d-context-ref.html" />
+<link rel="match" href="../../expected/css/backface-hidden-ancestor-culls-descendant-3d-contexts-ref.html" />
<style>
body {
margin: 0;
background: white;
}
+ .pass {
+ position: absolute;
+ top: 50px;
+ left: 50px;
+ width: 300px;
+ height: 300px;
+ background: green;
+ }
.flipped {
position: absolute;
top: 50px;
left: 50px;
width: 300px;
height: 300px;
- background: blue;
+ background: red;
transform: rotateY(180deg);
backface-visibility: hidden;
}
@@ -19,12 +27,12 @@
transform-style: preserve-3d;
width: 200px;
height: 200px;
- background: green;
+ background: red;
}
.participant {
width: 100px;
height: 100px;
- background: orange;
+ background: red;
transform: translateZ(10px);
}
.lone-context {
@@ -33,10 +41,12 @@
left: 220px;
width: 60px;
height: 60px;
- background: purple;
+ background: red;
transform-style: preserve-3d;
}
</style>
+<!-- The flipped element hides its backface, so neither it nor the 3D rendering contexts flattened into it paint over the green square. -->
+<div class="pass"></div>
<div class="flipped">
<div class="context">
<div class="participant"></div>
diff --git a/Tests/LibWeb/Ref/input/css/backface-hidden-participant-culls-nested-3d-context.html b/Tests/LibWeb/Ref/input/css/backface-hidden-participant-culls-nested-3d-context.html
new file mode 100644
index 00000000000..5f9a98bcba9
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css/backface-hidden-participant-culls-nested-3d-context.html
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<link rel="match" href="../../expected/css/backface-hidden-participant-culls-nested-3d-context-ref.html" />
+<style>
+ body {
+ margin: 0;
+ }
+ .scene {
+ position: absolute;
+ top: 50px;
+ left: 50px;
+ width: 200px;
+ height: 200px;
+ perspective: 1000px;
+ }
+ .card {
+ position: relative;
+ width: 200px;
+ height: 200px;
+ transform-style: preserve-3d;
+ }
+ .front {
+ width: 200px;
+ height: 200px;
+ background: green;
+ backface-visibility: hidden;
+ }
+ .back {
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 200px;
+ height: 200px;
+ transform: rotateY(180deg);
+ backface-visibility: hidden;
+ }
+ .button {
+ width: 100px;
+ height: 100px;
+ background: red;
+ transform-style: preserve-3d;
+ }
+</style>
+<div class="scene">
+ <div class="card">
+ <div class="front" id="front"></div>
+ <div class="back"><div class="button" id="button"></div></div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-transforms/composited-under-rotateY-180deg-preserve-3d.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-transforms/composited-under-rotateY-180deg-preserve-3d.html
new file mode 100644
index 00000000000..03fde7ed388
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-transforms/composited-under-rotateY-180deg-preserve-3d.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<title>CSS Test (Transforms): composited under backface-visibility:hidden, rotateY(180deg) and preserve-3d</title>
+<link rel="help" href="http://www.w3.org/TR/css-transforms-2">
+<meta name="assert" content="This tests that a composited element with default
+ backface-visibility is visible under backface-visibility:hidden, rotateY(180deg) and preserve-3d.">
+<link rel="match" href="../../../../expected/wpt-import/css/css-transforms/composited-under-rotateY-180deg-ref.html">
+<div style="transform: rotateY(180deg); width: 100px; backface-visibility: hidden; transform-style: preserve-3d">
+ <div style="width: 100px; height: 100px; background: green; will-change: transform"></div>
+</div>
diff --git a/Tests/LibWeb/Text/expected/css/backface-hidden-participant-hit-test-skips-nested-3d-context.txt b/Tests/LibWeb/Text/expected/css/backface-hidden-participant-hit-test-skips-nested-3d-context.txt
new file mode 100644
index 00000000000..b5f8b44fd8f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/backface-hidden-participant-hit-test-skips-nested-3d-context.txt
@@ -0,0 +1,2 @@
+left half: front
+right half: front
diff --git a/Tests/LibWeb/Text/input/css/backface-hidden-participant-hit-test-skips-nested-3d-context.html b/Tests/LibWeb/Text/input/css/backface-hidden-participant-hit-test-skips-nested-3d-context.html
new file mode 100644
index 00000000000..8ae043d6f7a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/backface-hidden-participant-hit-test-skips-nested-3d-context.html
@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ .scene {
+ position: absolute;
+ top: 50px;
+ left: 50px;
+ width: 200px;
+ height: 200px;
+ perspective: 1000px;
+ }
+ .card {
+ position: relative;
+ width: 200px;
+ height: 200px;
+ transform-style: preserve-3d;
+ }
+ .front {
+ width: 200px;
+ height: 200px;
+ background: green;
+ backface-visibility: hidden;
+ }
+ .back {
+ position: absolute;
+ top: 0;
+ left: 0;
+ width: 200px;
+ height: 200px;
+ transform: rotateY(180deg);
+ backface-visibility: hidden;
+ }
+ .button {
+ width: 100px;
+ height: 100px;
+ background: red;
+ transform-style: preserve-3d;
+ }
+</style>
+<div class="scene">
+ <div class="card">
+ <div class="front" id="front"></div>
+ <div class="back"><div class="button" id="button"></div></div>
+ </div>
+</div>
+<script>
+ test(() => {
+ // The button sits in the card's back face, which faces away, so a point over it hits the front face.
+ // The flipped back face mirrors the button to the card's right half.
+ println(`left half: ${document.elementFromPoint(100, 100).id}`);
+ println(`right half: ${document.elementFromPoint(200, 100).id}`);
+ });
+</script>