Skip to content

Truly independent web browser

LibCompositing: Bound effects drawn in 3D-transformed spaces

Aliaksandr Kalenik592abcc4afe3
diff --git a/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs b/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
index 78b7dbd45f2..9212f8982b5 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
@@ -25,8 +25,8 @@ use libgfx_rust::filter::Filter;
use libgfx_rust::{FloatMatrix4x4, FloatRect, IntRect};
use std::collections::HashMap;
-/// Where content lies in an effect's space. It reaches anywhere through a filter that paints everywhere or through a
-/// space that does not map onto the effect's as a two-dimensional transform.
+/// Where content lies in an effect's space. It reaches anywhere through a filter that paints everywhere, or from a space
+/// where it lies partly behind the eye or does not project back onto the effect's plane in front of the eye.
pub type Extent = Reach<FloatRect>;
pub(crate) const LARGEST_FILTER_READ_REACH_IN_DEVICE_PIXELS: f32 = 2048.0;
@@ -102,7 +102,7 @@ fn extent_mapped_between_spaces(
) -> Extent {
match extent {
Extent::Within(rect) => world
- .map_rect_between_spaces(rect, from, to)
+ .bounds_of_rect_mapped_between_spaces(rect, from, to)
.map_or(Extent::Anywhere, Extent::Within),
extent => extent,
}
@@ -468,11 +468,12 @@ pub fn compute_effect_geometry<'a>(
mod tests {
use super::*;
use crate::display_list::commands::{ClipNodeIndex, ContextRef, EffectNodeIndex, VISUAL_VIEWPORT_NODE_INDEX};
+ use crate::visual_context::world_transforms::bounds_of_projected_rect_if_corners_in_front_of_eye;
use crate::visual_context::{EffectsData, SpatialData, TransformData, TransformDataRole};
use libgfx_rust::filter::Filter;
use libgfx_rust::{
- Color, ColorFilterType, CompositingAndBlendingOperator, FloatMatrix4x4, FloatPoint, IntRect, rotation_matrix,
- scale_matrix, translation_matrix,
+ Color, ColorFilterType, CompositingAndBlendingOperator, FloatMatrix4x4, FloatPoint, IntRect,
+ perspective_matrix, rotation_matrix, scale_matrix, translation_matrix,
};
use std::sync::Arc;
@@ -659,7 +660,7 @@ mod tests {
}
#[test]
- fn unbounded_filters_and_three_dimensional_spaces_reach_anywhere() {
+ fn filters_that_paint_everywhere_reach_anywhere() {
let mut tree = identity_tree();
let outer = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
let flood = effect(
@@ -671,21 +672,78 @@ mod tests {
outer,
VISUAL_VIEWPORT_NODE_INDEX,
);
- let tilted = tree.append_spatial(
- SpatialData::Transform(transform(rotation_matrix([0.0, 1.0, 0.0], 0.5))),
- VISUAL_VIEWPORT_NODE_INDEX,
- );
- let flat = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
let unused = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
- let runs = [
- run(VISUAL_VIEWPORT_NODE_INDEX, flood, IntRect::new(0, 0, 10, 10)),
- run(tilted, flat, IntRect::new(0, 0, 10, 10)),
- ];
+ let runs = [run(VISUAL_VIEWPORT_NODE_INDEX, flood, IntRect::new(0, 0, 10, 10))];
let geometry = geometry_of(&tree, &runs);
assert_eq!(geometry[flood.0 as usize].content, within(0.0, 0.0, 10.0, 10.0));
assert_eq!(geometry[flood.0 as usize].output, Extent::Anywhere);
assert_eq!(geometry[outer.0 as usize].output, Extent::Anywhere);
- assert_eq!(geometry[flat.0 as usize].content, Extent::Anywhere);
assert_eq!(geometry[unused.0 as usize].output, Extent::Nothing);
}
+
+ fn assert_approximately_within(extent: Extent, expected: FloatRect) {
+ let Extent::Within(rect) = extent else {
+ panic!("{extent:?} is not within {expected:?}");
+ };
+ let edges = |rect: FloatRect| [rect.x, rect.y, rect.right(), rect.bottom()];
+ for (edge, expected_edge) in edges(rect).into_iter().zip(edges(expected)) {
+ assert!((edge - expected_edge).abs() < 0.001, "{rect:?} is not {expected:?}");
+ }
+ }
+
+ #[test]
+ fn content_drawn_in_a_three_dimensional_space_is_bounded_where_it_projects() {
+ let mut tree = identity_tree();
+ let tilted = tree.append_spatial(
+ SpatialData::Transform(transform(rotation_matrix([0.0, 1.0, 0.0], 0.5))),
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let flat = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
+ let runs = [run(tilted, flat, IntRect::new(0, 0, 10, 10))];
+ assert_approximately_within(
+ geometry_of(&tree, &runs)[flat.0 as usize].content,
+ FloatRect::new(0.0, 0.0, 10.0 * 0.5f32.cos(), 10.0),
+ );
+ }
+
+ #[test]
+ fn content_drawn_on_a_plane_in_perspective_is_bounded_on_the_plane_and_where_the_plane_projects() {
+ let mut tree = identity_tree();
+ let plane_to_root = perspective_matrix(100.0).multiplied(rotation_matrix([1.0, 0.0, 0.0], 1.0));
+ let plane = tree.append_spatial(
+ SpatialData::Transform(transform(plane_to_root)),
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let moved_on_plane = tree.append_spatial(
+ SpatialData::Transform(transform(translation_matrix(5.0, 0.0, 0.0))),
+ plane,
+ );
+ let flat = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
+ let on_plane = effect(&mut tree, None, flat, plane);
+ let runs = [run(moved_on_plane, on_plane, IntRect::new(0, 0, 10, 10))];
+ let geometry = geometry_of(&tree, &runs);
+ assert_approximately_within(
+ geometry[on_plane.0 as usize].content,
+ FloatRect::new(5.0, 0.0, 10.0, 10.0),
+ );
+ assert_approximately_within(
+ geometry[flat.0 as usize].content,
+ bounds_of_projected_rect_if_corners_in_front_of_eye(plane_to_root, FloatRect::new(5.0, 0.0, 10.0, 10.0))
+ .unwrap(),
+ );
+ }
+
+ #[test]
+ fn content_partly_behind_the_eye_reaches_anywhere() {
+ let mut tree = identity_tree();
+ let plane = tree.append_spatial(
+ SpatialData::Transform(transform(
+ perspective_matrix(100.0).multiplied(rotation_matrix([1.0, 0.0, 0.0], 1.4)),
+ )),
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let flat = effect(&mut tree, None, EffectNodeIndex::NONE, VISUAL_VIEWPORT_NODE_INDEX);
+ let runs = [run(plane, flat, IntRect::new(0, -1000, 10, 2000))];
+ assert_eq!(geometry_of(&tree, &runs)[flat.0 as usize].content, Extent::Anywhere);
+ }
}
diff --git a/Libraries/LibCompositing/Rust/src/display_list/placement.rs b/Libraries/LibCompositing/Rust/src/display_list/placement.rs
index 8f86e62c956..cb6f46ccfc4 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/placement.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/placement.rs
@@ -20,7 +20,9 @@ use crate::display_list::effect_clip_plan::EffectClipPlan;
use crate::display_list::effect_geometry::{Extent, FiltersOfEffects, changed_extent_through_filters};
use crate::display_list::reach::Reach;
use crate::visual_context::queries::TreeCullingScratch;
-use crate::visual_context::world_transforms::translation_on_whole_device_pixels;
+use crate::visual_context::world_transforms::{
+ bounds_of_projected_rect_if_corners_in_front_of_eye, translation_on_whole_device_pixels,
+};
use crate::visual_context::{
ClipNodeData, CompositorScrollNodeRecord, EffectNodeData, RasterRoot, SpatialData, StickyData, VisualContextTree,
WorldTransforms,
@@ -815,31 +817,6 @@ impl RasterSpaces {
}
}
-fn bounds_of_projected_rect_if_corners_in_front_of_eye(matrix: FloatMatrix4x4, rect: FloatRect) -> Option<FloatRect> {
- const SMALLEST_W_IN_FRONT_OF_EYE: f32 = 0.00001;
- let corners = [
- (rect.x, rect.y),
- (rect.right(), rect.y),
- (rect.x, rect.bottom()),
- (rect.right(), rect.bottom()),
- ]
- .map(|(x, y)| matrix.map_vector4([x, y, 0.0, 1.0]));
- if corners.iter().any(|corner| corner[3] <= SMALLEST_W_IN_FRONT_OF_EYE) {
- return None;
- }
- let xs = corners.map(|corner| corner[0] / corner[3]);
- let ys = corners.map(|corner| corner[1] / corner[3]);
- let (left, right) = (
- xs.into_iter().fold(f32::INFINITY, f32::min),
- xs.into_iter().fold(f32::NEG_INFINITY, f32::max),
- );
- let (top, bottom) = (
- ys.into_iter().fold(f32::INFINITY, f32::min),
- ys.into_iter().fold(f32::NEG_INFINITY, f32::max),
- );
- Some(FloatRect::new(left, top, right - left, bottom - top))
-}
-
pub(crate) fn is_finite_rect(rect: FloatRect) -> bool {
rect.x.is_finite() && rect.y.is_finite() && rect.width.is_finite() && rect.height.is_finite()
}
diff --git a/Libraries/LibCompositing/Rust/src/display_list/replay.rs b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
index 4c6ae0ad32f..eb320e29119 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
@@ -996,7 +996,11 @@ impl<Painter: ReplayPainter + ?Sized> ReplayDriver<'_, Painter> {
};
let effect_space_to_root = self.world.draw_matrix(effect_space);
if !effect_space_to_root.is_2d_affine() {
- return None;
+ let content_in_root = self.world.bounds_in_root_of_rect_drawn_in(effect_space, content)?;
+ let margin = 2.0 * LAYER_CONTENT_MARGIN_IN_DEVICE_PIXELS;
+ return self
+ .world
+ .bounds_in_space_of_root_rect(effect_space, content_in_root.inflated(margin, margin));
}
let root_to_layer = effect_space_to_root.extract_2d_affine().inverse()?;
let margin_x = LAYER_CONTENT_MARGIN_IN_DEVICE_PIXELS * (root_to_layer.a().abs() + root_to_layer.c().abs());
@@ -1787,7 +1791,8 @@ mod tests {
};
use ak::math;
use libgfx_rust::{
- CompositingAndBlendingOperator, CornerRadii, FloatRect, MaskKind, scale_matrix, translation_matrix,
+ CompositingAndBlendingOperator, CornerRadii, FloatRect, MaskKind, perspective_matrix, scale_matrix,
+ translation_matrix,
};
#[test]
@@ -2797,10 +2802,21 @@ mod tests {
);
}
+ fn assert_layer_bounds_are_approximately(layer_content_bounds: &[Option<FloatRect>], expected: &[FloatRect]) {
+ assert_eq!(layer_content_bounds.len(), expected.len(), "{layer_content_bounds:?}");
+ let edges = |rect: FloatRect| [rect.x, rect.y, rect.right(), rect.bottom()];
+ for (bounds, expected) in layer_content_bounds.iter().zip(expected) {
+ let bounds = bounds.unwrap_or_else(|| panic!("a layer has no bounds, not {expected:?}"));
+ for (edge, expected_edge) in edges(bounds).into_iter().zip(edges(*expected)) {
+ assert!((edge - expected_edge).abs() < 0.001, "{bounds:?} is not {expected:?}");
+ }
+ }
+ }
+
#[test]
- fn a_layer_whose_content_can_land_anywhere_has_no_bounds() {
+ fn a_layer_over_or_in_a_tilted_space_is_bounded_where_its_content_projects() {
let mut tree = identity_tree();
- let tilted = tree.append_spatial(transform(rotate_y(30.0), None), VISUAL_VIEWPORT_NODE_INDEX);
+ let tilted = tree.append_spatial(transform(rotate_y(60.0), None), VISUAL_VIEWPORT_NODE_INDEX);
let flat_layer = effect_under(&mut tree, effects(0.5), EffectNodeIndex::NONE, ClipNodeIndex::NONE);
let tilted_layer = tree.append_effect(effects(0.5), EffectNodeIndex::NONE, tilted, ClipNodeIndex::NONE);
let runs = [
@@ -2809,6 +2825,30 @@ mod tests {
];
let mut painter = RecordingPainter::new();
replay(&tree, &runs, &mut painter);
+ assert_layer_bounds_are_approximately(
+ &painter.layer_content_bounds,
+ &[
+ inflated_by_margin(FloatRect::new(0.0, 0.0, 5.0, 10.0), 2.0),
+ FloatRect::new(-4.0, -2.0, 18.0, 14.0),
+ ],
+ );
+ }
+
+ #[test]
+ fn a_layer_whose_content_lies_partly_behind_the_eye_has_no_bounds() {
+ let mut tree = identity_tree();
+ let tilted = tree.append_spatial(
+ transform(perspective_matrix(100.0).multiplied(rotate_y(60.0)), None),
+ VISUAL_VIEWPORT_NODE_INDEX,
+ );
+ let flat_layer = effect_under(&mut tree, effects(0.5), EffectNodeIndex::NONE, ClipNodeIndex::NONE);
+ let tilted_layer = tree.append_effect(effects(0.5), EffectNodeIndex::NONE, tilted, ClipNodeIndex::NONE);
+ let runs = [
+ run(tilted, effect_context(flat_layer), IntRect::new(-1000, 0, 2000, 10)),
+ run(tilted, effect_context(tilted_layer), IntRect::new(-1000, 0, 2000, 10)),
+ ];
+ let mut painter = RecordingPainter::new();
+ replay(&tree, &runs, &mut painter);
assert_eq!(painter.layer_content_bounds, [None, None]);
}
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs b/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs
index d33523c5a6d..7c4a399b2e2 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs
@@ -8,6 +8,7 @@ use super::{
IncludeVisualViewportTransform, LocalTransformKind, SpatialData, SpatialNodeIndex, VISUAL_VIEWPORT_NODE_INDEX,
VisualContextTree, should_cull_back_face,
};
+use crate::display_list::placement::is_finite_rect;
use libgfx_rust::{
AffineTransform, FloatMatrix4x4, FloatPoint, FloatRect, FloatSize, map_rect_through_matrix, multiply_affine,
scale_matrix, translation_matrix,
@@ -300,6 +301,97 @@ impl WorldTransforms {
let root_to_target = to_matrix.extract_2d_affine().inverse()?;
Some(multiply_affine(root_to_target, from_matrix.extract_2d_affine()).map_rect(rect))
}
+
+ /// As `map_rect_between_spaces`, with spaces in perspective too, unless a corner of `rect` lies behind the eye or
+ /// does not project back onto `to`'s plane in front of the eye.
+ pub fn bounds_of_rect_mapped_between_spaces(
+ &self,
+ rect: FloatRect,
+ from: SpatialNodeIndex,
+ to: SpatialNodeIndex,
+ ) -> Option<FloatRect> {
+ if let Some(mapped) = self.map_rect_between_spaces(rect, from, to) {
+ return Some(mapped);
+ }
+ let corners_in_root = corners_projected_if_in_front_of_eye(self.draw_matrix(from), corners_of(rect))?;
+ let corners_in_to = corners_on_plane_projecting_onto(self.draw_matrix(to), corners_in_root)?;
+ Some(bounds_of_corners(corners_in_to)).filter(|bounds| is_finite_rect(*bounds))
+ }
+
+ pub fn bounds_in_root_of_rect_drawn_in(&self, space: SpatialNodeIndex, rect: FloatRect) -> Option<FloatRect> {
+ bounds_of_projected_rect_if_corners_in_front_of_eye(self.draw_matrix(space), rect)
+ .filter(|bounds| is_finite_rect(*bounds))
+ }
+
+ pub fn bounds_in_space_of_root_rect(&self, space: SpatialNodeIndex, rect: FloatRect) -> Option<FloatRect> {
+ let corners = corners_on_plane_projecting_onto(self.draw_matrix(space), corners_of(rect))?;
+ Some(bounds_of_corners(corners)).filter(|bounds| is_finite_rect(*bounds))
+ }
+}
+
+fn corners_of(rect: FloatRect) -> [(f32, f32); 4] {
+ [
+ (rect.x, rect.y),
+ (rect.right(), rect.y),
+ (rect.x, rect.bottom()),
+ (rect.right(), rect.bottom()),
+ ]
+}
+
+fn bounds_of_corners(corners: [(f32, f32); 4]) -> FloatRect {
+ let (left, right) = corners
+ .iter()
+ .fold((f32::INFINITY, f32::NEG_INFINITY), |(left, right), &(x, _)| {
+ (left.min(x), right.max(x))
+ });
+ let (top, bottom) = corners
+ .iter()
+ .fold((f32::INFINITY, f32::NEG_INFINITY), |(top, bottom), &(_, y)| {
+ (top.min(y), bottom.max(y))
+ });
+ FloatRect::new(left, top, right - left, bottom - top)
+}
+
+fn corners_projected_if_in_front_of_eye(matrix: FloatMatrix4x4, corners: [(f32, f32); 4]) -> Option<[(f32, f32); 4]> {
+ const SMALLEST_W_IN_FRONT_OF_EYE: f32 = 0.00001;
+ let corners = corners.map(|(x, y)| matrix.map_vector4([x, y, 0.0, 1.0]));
+ if corners.iter().any(|corner| corner[3] <= SMALLEST_W_IN_FRONT_OF_EYE) {
+ return None;
+ }
+ Some(corners.map(|corner| (corner[0] / corner[3], corner[1] / corner[3])))
+}
+
+pub(crate) fn bounds_of_projected_rect_if_corners_in_front_of_eye(
+ matrix: FloatMatrix4x4,
+ rect: FloatRect,
+) -> Option<FloatRect> {
+ corners_projected_if_in_front_of_eye(matrix, corners_of(rect)).map(bounds_of_corners)
+}
+
+/// The points of the z = 0 plane that project through `matrix` onto `points`, if each is where a point in front of the
+/// eye projects. The points of the plane then bound every point that projects into the quad of `points`.
+fn corners_on_plane_projecting_onto(matrix: FloatMatrix4x4, points: [(f32, f32); 4]) -> Option<[(f32, f32); 4]> {
+ let m = &matrix.elements;
+ let plane_to_root = [0, 1, 3].map(|row| [0, 1, 3].map(|column| m[row][column] as f64));
+ let root_to_plane = inverse_of_3x3(plane_to_root)?;
+ let points = points.map(|(x, y)| root_to_plane.map(|row| row[0] * x as f64 + row[1] * y as f64 + row[2]));
+ if points.iter().any(|point| point[2] <= 0.0) {
+ return None;
+ }
+ Some(points.map(|point| ((point[0] / point[2]) as f32, (point[1] / point[2]) as f32)))
+}
+
+fn inverse_of_3x3(m: [[f64; 3]; 3]) -> Option<[[f64; 3]; 3]> {
+ let cofactor = |row: usize, column: usize| {
+ let (r0, r1) = ((row + 1) % 3, (row + 2) % 3);
+ let (c0, c1) = ((column + 1) % 3, (column + 2) % 3);
+ m[r0][c0] * m[r1][c1] - m[r0][c1] * m[r1][c0]
+ };
+ let determinant = m[0][0] * cofactor(0, 0) + m[0][1] * cofactor(0, 1) + m[0][2] * cofactor(0, 2);
+ if determinant == 0.0 || !determinant.is_finite() {
+ return None;
+ }
+ Some([0, 1, 2].map(|row| [0, 1, 2].map(|column| cofactor(column, row) / determinant)))
}
fn map_rect_through(matrix: FloatMatrix4x4, rect: FloatRect) -> FloatRect {
diff --git a/Tests/LibWeb/Ref/expected/css/effect-bounds-around-perspective-content-ref.html b/Tests/LibWeb/Ref/expected/css/effect-bounds-around-perspective-content-ref.html
new file mode 100644
index 00000000000..63ef2a8d607
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/css/effect-bounds-around-perspective-content-ref.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+
+ .stage {
+ position: relative;
+ width: 400px;
+ height: 300px;
+ perspective: 300px;
+ }
+
+ .tilted {
+ position: absolute;
+ left: 100px;
+ top: 50px;
+ width: 200px;
+ height: 200px;
+ transform: rotateX(50deg) rotate(30deg);
+ }
+
+ svg {
+ display: block;
+ width: 200px;
+ height: 200px;
+ }
+</style>
+<div>
+ <div class="stage">
+ <div class="tilted">
+ <svg viewBox="0 0 20 20">
+ <rect width="20" height="20" fill="green" />
+ <circle cx="3" cy="3" r="3" fill="blue" />
+ <circle cx="17" cy="17" r="3" fill="blue" />
+ <circle cx="17" cy="3" r="3" fill="blue" />
+ <circle cx="3" cy="17" r="3" fill="blue" />
+ </svg>
+ </div>
+ </div>
+</div>
diff --git a/Tests/LibWeb/Ref/input/css/effect-bounds-around-perspective-content.html b/Tests/LibWeb/Ref/input/css/effect-bounds-around-perspective-content.html
new file mode 100644
index 00000000000..1d94e0ac676
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/css/effect-bounds-around-perspective-content.html
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<link rel="match" href="../../expected/css/effect-bounds-around-perspective-content-ref.html" />
+<meta name="fuzzy" content="maxDifference=0-8;totalPixels=0-1000" />
+<style>
+ body {
+ margin: 0;
+ }
+
+ .group {
+ opacity: 0.999;
+ }
+
+ .stage {
+ position: relative;
+ width: 400px;
+ height: 300px;
+ perspective: 300px;
+ }
+
+ .tilted {
+ position: absolute;
+ left: 100px;
+ top: 50px;
+ width: 200px;
+ height: 200px;
+ opacity: 0.999;
+ transform: rotateX(50deg) rotate(30deg);
+ }
+
+ svg {
+ display: block;
+ width: 200px;
+ height: 200px;
+ }
+</style>
+<div class="group">
+ <div class="stage">
+ <div class="tilted">
+ <svg viewBox="0 0 20 20">
+ <rect width="20" height="20" fill="green" opacity="0.999" />
+ <circle cx="3" cy="3" r="3" fill="blue" opacity="0.999" />
+ <circle cx="17" cy="17" r="3" fill="blue" opacity="0.999" />
+ <circle cx="17" cy="3" r="3" fill="blue" opacity="0.999" />
+ <circle cx="3" cy="17" r="3" fill="blue" opacity="0.999" />
+ </svg>
+ </div>
+ </div>
+</div>