Skip to content

Truly independent web browser

LibCompositing: Answer point and rect queries from WorldTransforms

Aliaksandr Kalenik37320976226e
diff --git a/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp b/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp
index d05e67cddbd..378d8ac9951 100644
--- a/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp
+++ b/Libraries/LibCompositing/DisplayList/AccumulatedVisualContext.cpp
@@ -175,9 +175,8 @@ bool AccumulatedVisualContextTree::can_snap_composited_context_origin(SpatialNod
Optional<Gfx::FloatPoint> AccumulatedVisualContextTree::transform_point_for_hit_test(ContextRef context, Gfx::FloatPoint screen_point, ScrollStateSnapshot const& scroll_state, ClipBehavior clip_behavior) const
{
- auto scroll_offsets = scroll_state.device_offsets();
Gfx::FloatPoint local_point;
- if (!Compositing::RustFFI::visual_context_tree_transform_point_for_hit_test(m_rust_tree, context, screen_point, scroll_offsets.data(), scroll_offsets.size(), clip_behavior == ClipBehavior::Respect, &local_point))
+ if (!Compositing::RustFFI::visual_context_tree_transform_point_for_hit_test(m_rust_tree, scroll_state.world_transforms(*this), context, screen_point, clip_behavior == ClipBehavior::Respect, &local_point))
return {};
return local_point;
}
@@ -191,16 +190,14 @@ Gfx::FloatPoint AccumulatedVisualContextTree::inverse_transform_point(SpatialNod
Gfx::FloatRect AccumulatedVisualContextTree::transform_rect_to_viewport(SpatialNodeIndex index, Gfx::FloatRect const& source_rect, ScrollStateSnapshot const& scroll_state, IncludeVisualViewportTransform include_visual_viewport_transform) const
{
- auto scroll_offsets = scroll_state.device_offsets();
Gfx::FloatRect rect;
- Compositing::RustFFI::visual_context_tree_transform_rect_to_viewport(m_rust_tree, index, source_rect, scroll_offsets.data(), scroll_offsets.size(), include_visual_viewport_transform == IncludeVisualViewportTransform::Yes, &rect);
+ Compositing::RustFFI::world_transforms_transform_rect_to_viewport(scroll_state.world_transforms(*this), index, source_rect, include_visual_viewport_transform == IncludeVisualViewportTransform::Yes, &rect);
return rect;
}
Gfx::FloatMatrix4x4 AccumulatedVisualContextTree::accumulated_matrix(SpatialNodeIndex index, ScrollStateSnapshot const& scroll_state, IncludeVisualViewportTransform include_visual_viewport_transform) const
{
- auto scroll_offsets = scroll_state.device_offsets();
- return Compositing::RustFFI::visual_context_tree_accumulated_matrix(m_rust_tree, index, scroll_offsets.data(), scroll_offsets.size(), include_visual_viewport_transform == IncludeVisualViewportTransform::Yes);
+ return Compositing::RustFFI::world_transforms_accumulated_matrix(scroll_state.world_transforms(*this), index, include_visual_viewport_transform == IncludeVisualViewportTransform::Yes);
}
void AccumulatedVisualContextTree::for_each_effects_filter_image_frame_id(Function<void(u64)> const& visit) const
diff --git a/Libraries/LibCompositing/Rust/src/display_list/damage.rs b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
index 84feb47f00a..568e541a0d6 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/damage.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
@@ -512,7 +512,7 @@ impl<'b> TreeSide<'b> {
WorldTransforms::new(
self.tree,
self.scroll_offsets,
- FloatMatrix4x4::identity(),
+ Some(FloatMatrix4x4::identity()),
ScrollSnap::Truncate,
)
})
@@ -1392,7 +1392,12 @@ pub fn animated_content_may_affect_viewport(
let mut rect = header.bounding_rect.to_float();
if has_inline_clip_in_its_own_space && header.has_bounding_rect {
let world = world.get_or_init(|| {
- WorldTransforms::new(tree, scroll_offsets, FloatMatrix4x4::identity(), ScrollSnap::Truncate)
+ WorldTransforms::new(
+ tree,
+ scroll_offsets,
+ Some(FloatMatrix4x4::identity()),
+ ScrollSnap::Truncate,
+ )
});
if let Some(command_to_root) = world.draw_matrix_if_known(run.context.spatial)
&& let Some(OwnSpaceClipExtent::Within(clipped_rect)) =
diff --git a/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs b/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
index f100d71a53d..95035f5f66a 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/effect_geometry.rs
@@ -261,7 +261,7 @@ mod tests {
fn geometry_of(tree: &VisualContextTree, runs: &[DisplayListCommandRun]) -> Vec<EffectGeometry> {
let mut geometry = Vec::new();
- let world = WorldTransforms::new(tree, &[], FloatMatrix4x4::identity(), ScrollSnap::None);
+ let world = WorldTransforms::new(tree, &[], Some(FloatMatrix4x4::identity()), ScrollSnap::None);
compute_effect_geometry(tree, runs, &world, &mut geometry);
geometry
}
@@ -376,7 +376,7 @@ mod tests {
assert_eq!(nearest[opacity.0 as usize], blur);
assert_eq!(nearest[plain.0 as usize], EffectNodeIndex::NONE);
let content = FloatRect::new(10.0, 20.0, 30.0, 40.0);
- let world = WorldTransforms::new(&tree, &[], FloatMatrix4x4::identity(), ScrollSnap::None);
+ let world = WorldTransforms::new(&tree, &[], Some(FloatMatrix4x4::identity()), ScrollSnap::None);
assert_eq!(
changed_extent_through_filters(&tree, &nearest, scaled, content, shadow, &world),
(VISUAL_VIEWPORT_NODE_INDEX, within(-10.0, 10.0, 120.0, 160.0))
diff --git a/Libraries/LibCompositing/Rust/src/display_list/replay.rs b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
index 69e77e9b22f..f9cdd40479c 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/replay.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/replay.rs
@@ -815,7 +815,7 @@ pub fn replay_display_list(
tree,
&driver.spatial_dependency_order,
scroll_offsets,
- replay_base_matrix,
+ Some(replay_base_matrix),
ScrollSnap::Truncate,
);
driver.execute();
@@ -894,8 +894,8 @@ mod tests {
permuted.spatial_nodes[permuted_child.0 as usize].parent = permuted_parent;
let base = scale_matrix(2.0, 2.0, 1.0);
- let in_order_palette = WorldTransforms::new(&in_order, &[], base, ScrollSnap::Truncate);
- let permuted_palette = WorldTransforms::new(&permuted, &[], base, ScrollSnap::Truncate);
+ let in_order_palette = WorldTransforms::new(&in_order, &[], Some(base), ScrollSnap::Truncate);
+ let permuted_palette = WorldTransforms::new(&permuted, &[], Some(base), ScrollSnap::Truncate);
let pairs = [
(VISUAL_VIEWPORT_NODE_INDEX, VISUAL_VIEWPORT_NODE_INDEX),
(parent, permuted_parent),
diff --git a/Libraries/LibCompositing/Rust/src/ffi.rs b/Libraries/LibCompositing/Rust/src/ffi.rs
index b552aeaa6b3..2ca0243cb88 100644
--- a/Libraries/LibCompositing/Rust/src/ffi.rs
+++ b/Libraries/LibCompositing/Rust/src/ffi.rs
@@ -312,23 +312,61 @@ pub unsafe extern "C" fn display_list_references_only_live_visual_context_nodes(
/// # Safety
///
-/// `tree` must be a live retained tree handle; `scroll_offsets` must address `scroll_offsets_len`
-/// points and `out_local_point` must be writable.
+/// `world` must be a live handle from `world_transforms_create`.
+pub unsafe fn world_transforms_from_handle<'a>(world: *const c_void) -> &'a crate::visual_context::WorldTransforms {
+ // SAFETY: The caller guarantees `world` is a live handle.
+ unsafe { &*world.cast::<crate::visual_context::WorldTransforms>() }
+}
+
+/// The world transforms of `tree` under the scroll offsets, in the layout viewport's space, until
+/// `world_transforms_destroy` frees them.
+///
+/// # Safety
+///
+/// `tree` must be a live retained tree handle; `scroll_offsets` must address `scroll_offsets_len` points.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn world_transforms_create(
+ tree: *const c_void,
+ scroll_offsets: *const libgfx_rust::FloatPoint,
+ scroll_offsets_len: usize,
+) -> *mut c_void {
+ let tree = unsafe { tree_from_handle(tree) };
+ // SAFETY: The caller guarantees the offsets address `scroll_offsets_len` points.
+ let scroll_offsets = unsafe { ak::slice_from_raw_parts(scroll_offsets, scroll_offsets_len) };
+ let world = crate::visual_context::WorldTransforms::new(
+ tree,
+ scroll_offsets,
+ None,
+ crate::visual_context::ScrollSnap::None,
+ );
+ Box::into_raw(Box::new(world)).cast()
+}
+
+/// # Safety
+///
+/// `world` must be a handle from `world_transforms_create` that nothing uses afterwards.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn world_transforms_destroy(world: *mut c_void) {
+ // SAFETY: The caller hands over the handle `world_transforms_create` made.
+ drop(unsafe { Box::from_raw(world.cast::<crate::visual_context::WorldTransforms>()) });
+}
+
+/// # Safety
+///
+/// `tree` must be a live retained tree handle, `world` a live handle made from it, and `out_local_point` must be
+/// writable.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn visual_context_tree_transform_point_for_hit_test(
tree: *const c_void,
+ world: *const c_void,
context: crate::display_list::commands::ContextRef,
screen_point: libgfx_rust::FloatPoint,
- scroll_offsets: *const libgfx_rust::FloatPoint,
- scroll_offsets_len: usize,
respect_clip: bool,
out_local_point: *mut libgfx_rust::FloatPoint,
) -> bool {
- let tree = unsafe { tree_from_handle(tree) };
- // SAFETY: The caller guarantees the offsets address `scroll_offsets_len` points.
- let scroll_offsets = unsafe { ak::slice_from_raw_parts(scroll_offsets, scroll_offsets_len) };
+ let (tree, world) = unsafe { (tree_from_handle(tree), world_transforms_from_handle(world)) };
let clip_behavior = crate::visual_context::ClipBehavior::from_respect_clip(respect_clip);
- match tree.transform_point_for_hit_test(context, screen_point, scroll_offsets, clip_behavior) {
+ match tree.transform_point_for_hit_test(world, context, screen_point, clip_behavior) {
Some(local_point) => {
// SAFETY: The caller guarantees `out_local_point` is writable.
unsafe { *out_local_point = local_point };
@@ -361,45 +399,33 @@ fn include_visual_viewport_transform_from(include: bool) -> crate::visual_contex
/// # Safety
///
-/// `tree` must be a live retained tree handle; `scroll_offsets` must address `scroll_offsets_len` points.
+/// `world` must be a live handle from `world_transforms_create`.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn visual_context_tree_transform_rect_to_viewport(
- tree: *const c_void,
+pub unsafe extern "C" fn world_transforms_transform_rect_to_viewport(
+ world: *const c_void,
spatial: SpatialNodeIndex,
rect: libgfx_rust::FloatRect,
- scroll_offsets: *const libgfx_rust::FloatPoint,
- scroll_offsets_len: usize,
include_visual_viewport_transform: bool,
out: &mut libgfx_rust::FloatRect,
) {
- let tree = unsafe { tree_from_handle(tree) };
- // SAFETY: The caller guarantees the offsets address `scroll_offsets_len` points.
- let scroll_offsets = unsafe { ak::slice_from_raw_parts(scroll_offsets, scroll_offsets_len) };
- *out = tree.transform_rect_to_viewport(
+ *out = unsafe { world_transforms_from_handle(world) }.transform_rect_to_viewport(
spatial,
rect,
- scroll_offsets,
include_visual_viewport_transform_from(include_visual_viewport_transform),
);
}
/// # Safety
///
-/// `tree` must be a live retained tree handle; `scroll_offsets` must address `scroll_offsets_len` points.
+/// `world` must be a live handle from `world_transforms_create`.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn visual_context_tree_accumulated_matrix(
- tree: *const c_void,
+pub unsafe extern "C" fn world_transforms_accumulated_matrix(
+ world: *const c_void,
spatial: SpatialNodeIndex,
- scroll_offsets: *const libgfx_rust::FloatPoint,
- scroll_offsets_len: usize,
include_visual_viewport_transform: bool,
) -> libgfx_rust::FloatMatrix4x4 {
- let tree = unsafe { tree_from_handle(tree) };
- // SAFETY: The caller guarantees the offsets address `scroll_offsets_len` points.
- let scroll_offsets = unsafe { ak::slice_from_raw_parts(scroll_offsets, scroll_offsets_len) };
- tree.accumulated_matrix(
+ unsafe { world_transforms_from_handle(world) }.to_viewport_matrix(
spatial,
- scroll_offsets,
include_visual_viewport_transform_from(include_visual_viewport_transform),
)
}
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/mod.rs b/Libraries/LibCompositing/Rust/src/visual_context/mod.rs
index edd7c2334ec..b9b3e4baa57 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/mod.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/mod.rs
@@ -1277,56 +1277,6 @@ impl VisualContextTree {
local
}
- pub fn accumulated_matrix(
- &self,
- index: SpatialNodeIndex,
- scroll_offsets: &[FloatPoint],
- include_visual_viewport_transform: IncludeVisualViewportTransform,
- ) -> FloatMatrix4x4 {
- let chain = self.ancestor_chain(index);
- let mut matrix = FloatMatrix4x4::identity();
- for node_index in chain.into_iter().rev() {
- if node_index == VISUAL_VIEWPORT_NODE_INDEX
- && include_visual_viewport_transform == IncludeVisualViewportTransform::No
- {
- continue;
- }
- matrix = self
- .local_transform(node_index, scroll_offsets, ScrollSnap::None)
- .applied_to(matrix);
- }
- matrix
- }
-
- pub fn accumulated_2d_scale(
- &self,
- index: SpatialNodeIndex,
- scroll_offsets: &[FloatPoint],
- include_visual_viewport_transform: IncludeVisualViewportTransform,
- ) -> FloatSize {
- let affine = self
- .accumulated_matrix(index, scroll_offsets, include_visual_viewport_transform)
- .extract_2d_affine();
- FloatSize {
- width: affine.x_scale(),
- height: affine.y_scale(),
- }
- }
-
- pub fn plane_depth_at_point_for_hit_test(
- &self,
- plane_node_index: SpatialNodeIndex,
- screen_point: FloatPoint,
- scroll_offsets: &[FloatPoint],
- ) -> Option<f32> {
- let inverse = self
- .accumulated_matrix(plane_node_index, scroll_offsets, IncludeVisualViewportTransform::Yes)
- .inverse()?;
- let matrix = &inverse.elements;
- let depth = -(screen_point.x * matrix[2][0] + screen_point.y * matrix[2][1] + matrix[2][3]) / matrix[2][2];
- depth.is_finite().then_some(depth)
- }
-
pub fn spatial_dependency_order_with_back_edges(&self) -> NodeDependencyOrder {
dependency_order(
self.spatial_nodes.len(),
@@ -1597,26 +1547,27 @@ mod tests {
let child = tree.append_spatial(SpatialData::Transform(scaled(3.0)), scroll);
let scroll_offsets = [FloatPoint::default(), FloatPoint { x: -10.0, y: -20.0 }];
- let without_viewport = tree.accumulated_matrix(child, &scroll_offsets, IncludeVisualViewportTransform::No);
+ let world = WorldTransforms::new(&tree, &scroll_offsets, None, ScrollSnap::None);
+ let without_viewport = world.to_viewport_matrix(child, IncludeVisualViewportTransform::No);
assert_eq!(
without_viewport,
translation_matrix(-10.0, -20.0, 0.0).multiplied(libgfx_rust::scale_matrix(3.0, 3.0, 1.0))
);
- let with_viewport = tree.accumulated_matrix(child, &scroll_offsets, IncludeVisualViewportTransform::Yes);
+ let with_viewport = world.to_viewport_matrix(child, IncludeVisualViewportTransform::Yes);
assert_eq!(
with_viewport,
libgfx_rust::scale_matrix(2.0, 2.0, 1.0).multiplied(without_viewport)
);
assert_eq!(
- tree.accumulated_2d_scale(child, &scroll_offsets, IncludeVisualViewportTransform::No),
+ world.accumulated_2d_scale(child, IncludeVisualViewportTransform::No),
FloatSize {
width: 3.0,
height: 3.0
}
);
assert_eq!(
- tree.accumulated_2d_scale(child, &[], IncludeVisualViewportTransform::Yes),
+ world.accumulated_2d_scale(child, IncludeVisualViewportTransform::Yes),
FloatSize {
width: 6.0,
height: 6.0
@@ -1634,11 +1585,12 @@ mod tests {
}),
VISUAL_VIEWPORT_NODE_INDEX,
);
- let matrix = tree.accumulated_matrix(child, &[], IncludeVisualViewportTransform::No);
+ let world = WorldTransforms::new(&tree, &[], None, ScrollSnap::None);
+ let matrix = world.to_viewport_matrix(child, IncludeVisualViewportTransform::No);
assert_eq!(matrix.map_vector4([50.0, 50.0, 0.0, 1.0]), [50.0, 50.0, 0.0, 1.0]);
assert_eq!(matrix.map_vector4([0.0, 0.0, 0.0, 1.0]), [-50.0, -50.0, 0.0, 1.0]);
assert_eq!(
- tree.accumulated_2d_scale(child, &[], IncludeVisualViewportTransform::No),
+ world.accumulated_2d_scale(child, IncludeVisualViewportTransform::No),
FloatSize {
width: 2.0,
height: 2.0
@@ -2059,7 +2011,8 @@ mod tests {
fn an_untransformed_plane_has_zero_depth() {
let tree = tree();
assert_eq!(
- tree.plane_depth_at_point_for_hit_test(VISUAL_VIEWPORT_NODE_INDEX, FloatPoint { x: 25.0, y: 50.0 }, &[]),
+ WorldTransforms::new(&tree, &[], None, ScrollSnap::None)
+ .plane_depth_at_point_for_hit_test(VISUAL_VIEWPORT_NODE_INDEX, FloatPoint { x: 25.0, y: 50.0 }),
Some(0.0)
);
}
@@ -2075,7 +2028,8 @@ mod tests {
VISUAL_VIEWPORT_NODE_INDEX,
);
assert_eq!(
- tree.plane_depth_at_point_for_hit_test(plane, FloatPoint { x: 25.0, y: 50.0 }, &[]),
+ WorldTransforms::new(&tree, &[], None, ScrollSnap::None)
+ .plane_depth_at_point_for_hit_test(plane, FloatPoint { x: 25.0, y: 50.0 }),
Some(75.0)
);
}
@@ -2097,8 +2051,8 @@ mod tests {
}),
perspective,
);
- let depth = tree
- .plane_depth_at_point_for_hit_test(plane, FloatPoint { x: 25.0, y: 50.0 }, &[])
+ let depth = WorldTransforms::new(&tree, &[], None, ScrollSnap::None)
+ .plane_depth_at_point_for_hit_test(plane, FloatPoint { x: 25.0, y: 50.0 })
.unwrap();
assert!((depth - 1000.0 / 9.0).abs() < 0.0001);
}
@@ -2114,7 +2068,8 @@ mod tests {
VISUAL_VIEWPORT_NODE_INDEX,
);
assert_eq!(
- tree.plane_depth_at_point_for_hit_test(plane, FloatPoint { x: 25.0, y: 50.0 }, &[]),
+ WorldTransforms::new(&tree, &[], None, ScrollSnap::None)
+ .plane_depth_at_point_for_hit_test(plane, FloatPoint { x: 25.0, y: 50.0 }),
None
);
}
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs b/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
index 6ee81b55c1d..17e82b79da3 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/queries.rs
@@ -8,12 +8,10 @@ use super::world_transforms::MINIMUM_PROJECTION_W;
use super::{
ClipData, ClipMode, ClipNode, ClipNodeData, ClipNodeIndex, ContextRef, EffectNodeData, EffectNodeIndex,
IncludeVisualViewportTransform, LocalTransform, LocalTransformKind, ScrollSnap, SpatialData, SpatialNodeIndex,
- TransformDataRole, VISUAL_VIEWPORT_NODE_INDEX, VisualContextTree, device_offset_for_index,
+ TransformDataRole, VISUAL_VIEWPORT_NODE_INDEX, VisualContextTree, WorldTransforms, device_offset_for_index,
};
use ak::math;
-use libgfx_rust::{
- CompositingAndBlendingOperator, FloatMatrix4x4, FloatPoint, FloatRect, IntPoint, IntRect, map_rect_through_matrix,
-};
+use libgfx_rust::{CompositingAndBlendingOperator, FloatMatrix4x4, FloatPoint, FloatRect, IntPoint, IntRect};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ClipBehavior {
@@ -59,18 +57,13 @@ impl ClipData {
}
}
-// One root path of spatial nodes with the screen point mapped down it one inverse step at a time. Clips attach
-// to a path in non-decreasing depth order, so the steps still to apply before a clip always start where the
-// previous clip left off.
+// One root path of spatial nodes with the screen point mapped down it one step at a time. Clips attach to a path in
+// non-decreasing depth order, so the steps still to apply before a clip always start where the previous clip left off.
struct SpatialChainWalk {
chain: Vec<SpatialNodeIndex>,
- 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>,
+ // A path through a 3D transform maps the screen point onto each node's plane through the node's flattened matrix,
+ // where per-node 2D inverses would lose the depth.
+ projective: bool,
point: FloatPoint,
applied_steps: usize,
}
@@ -147,91 +140,42 @@ impl VisualContextTree {
}
}
- fn begin_hit_test_walk(&self, leaf: SpatialNodeIndex, screen_point: FloatPoint) -> SpatialChainWalk {
+ fn begin_hit_test_walk(
+ &self,
+ world: &WorldTransforms,
+ leaf: SpatialNodeIndex,
+ screen_point: FloatPoint,
+ ) -> SpatialChainWalk {
let mut chain = self.ancestor_chain(leaf);
chain.reverse();
- let chain_has_backface_marker = chain.iter().any(|index| {
- self.local_transform(*index, &[], ScrollSnap::None)
- .backface_plane_root
- .is_some()
- });
- let has_3d_transform = self.chain_contains_3d_transform(leaf);
- let needs_accumulated_matrices = chain_has_backface_marker || has_3d_transform;
- let accumulated_matrices = if needs_accumulated_matrices {
- Vec::with_capacity(chain.len())
- } else {
- Vec::new()
- };
SpatialChainWalk {
chain,
- needs_accumulated_matrices,
- has_3d_transform,
- culled: false,
- culled_outside_sorting_context: false,
- accumulated_matrices,
+ projective: !world.to_root[leaf.0 as usize].is_2d_affine(),
point: screen_point,
applied_steps: 0,
}
}
fn apply_hit_test_spatial_step(
- &self,
+ world: &WorldTransforms,
walk: &mut SpatialChainWalk,
position: usize,
screen_point: FloatPoint,
- scroll_offsets: &[FloatPoint],
) -> bool {
- let local = self.local_transform(walk.chain[position], scroll_offsets, ScrollSnap::None);
-
- if walk.needs_accumulated_matrices {
- let parent_matrix = walk
- .accumulated_matrices
- .last()
- .copied()
- .unwrap_or(FloatMatrix4x4::identity());
- walk.accumulated_matrices.push(local.applied_to(parent_matrix));
- }
-
- // 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 local.establishes_sorting_context {
- walk.culled_outside_sorting_context = walk.culled;
- } else if local.sorting_context_root.is_some() {
- walk.culled = walk.culled_outside_sorting_context;
- }
-
- if let Some(plane_root) = local.backface_plane_root {
- if !walk.culled {
- let plane_root_position = walk
- .chain
- .iter()
- .position(|index| *index == plane_root)
- .expect("the plane root precedes the backface marker on its root path");
- walk.culled = should_cull_back_face(
- *walk
- .accumulated_matrices
- .last()
- .expect("backface walk accumulates matrices"),
- walk.accumulated_matrices[plane_root_position],
- );
- }
- return true;
- }
-
- if !walk.has_3d_transform {
- let Some(inverse) = local.matrix.extract_2d_affine().inverse() else {
+ let node = walk.chain[position];
+ if !walk.projective {
+ let Some(inverse) = world.local[node.0 as usize].extract_2d_affine().inverse() else {
return false;
};
walk.point = inverse.map_point(walk.point);
return true;
}
- let Some(inverse) = walk
- .accumulated_matrices
- .last()
- .expect("3D walk accumulates matrices")
+ // Content directly under a backface marker lies in the plane of the marker's parent.
+ if world.draw_space[node.0 as usize] != node {
+ return true;
+ }
+ let Some(inverse) = world
+ .to_viewport_matrix(node, IncludeVisualViewportTransform::Yes)
.flattened()
.inverse()
else {
@@ -249,18 +193,17 @@ impl VisualContextTree {
}
fn apply_hit_test_spatial_steps_through_node(
- &self,
+ world: &WorldTransforms,
walk: &mut SpatialChainWalk,
node_index: SpatialNodeIndex,
screen_point: FloatPoint,
- scroll_offsets: &[FloatPoint],
) -> bool {
if walk.applied_steps > 0 && walk.chain[walk.applied_steps - 1] == node_index {
return true;
}
loop {
assert!(walk.applied_steps < walk.chain.len());
- if !self.apply_hit_test_spatial_step(walk, walk.applied_steps, screen_point, scroll_offsets) {
+ if !Self::apply_hit_test_spatial_step(world, walk, walk.applied_steps, screen_point) {
return false;
}
if walk.chain[walk.applied_steps] == node_index {
@@ -326,9 +269,9 @@ impl VisualContextTree {
pub fn transform_point_for_hit_test(
&self,
+ world: &WorldTransforms,
context: ContextRef,
screen_point: FloatPoint,
- scroll_offsets: &[FloatPoint],
clip_behavior: ClipBehavior,
) -> Option<FloatPoint> {
// Effects never clip a point, so only the clip chain takes part.
@@ -340,10 +283,7 @@ impl VisualContextTree {
}
clip_chain.reverse();
- // The backface test needs forward matrices, but this walk only applies inverses. When the chain contains
- // backface markers, we accumulate the forward matrices as we walk, from the root down, so a marker can look up
- // the matrix at its plane root by chain position.
- let mut context_walk = self.begin_hit_test_walk(context.spatial, screen_point);
+ let mut context_walk = self.begin_hit_test_walk(world, context.spatial, screen_point);
for clip_index in clip_chain {
let clip = &self.clip_nodes[clip_index.0 as usize];
let mut clip_walk;
@@ -352,21 +292,17 @@ impl VisualContextTree {
} else {
// A fixed-background context is rooted above the scroll nodes its clips record in, so such a clip
// hangs below the context's node and takes its own walk from the root.
- clip_walk = self.begin_hit_test_walk(clip.spatial, screen_point);
+ clip_walk = self.begin_hit_test_walk(world, clip.spatial, screen_point);
&mut clip_walk
};
- if !self.apply_hit_test_spatial_steps_through_node(walk, clip.spatial, screen_point, scroll_offsets)
+ if !Self::apply_hit_test_spatial_steps_through_node(world, walk, clip.spatial, screen_point)
|| !Self::point_passes_clip(clip, walk.point, clip_behavior)
{
return None;
}
}
- if !self.apply_hit_test_spatial_steps_through_node(
- &mut context_walk,
- context.spatial,
- screen_point,
- scroll_offsets,
- ) || context_walk.culled
+ if !Self::apply_hit_test_spatial_steps_through_node(world, &mut context_walk, context.spatial, screen_point)
+ || world.backface_culled[context.spatial.0 as usize]
{
return None;
}
@@ -409,65 +345,6 @@ impl VisualContextTree {
})
}
- pub fn transform_rect_to_viewport(
- &self,
- index: SpatialNodeIndex,
- source_rect: FloatRect,
- scroll_offsets: &[FloatPoint],
- include_visual_viewport_transform: IncludeVisualViewportTransform,
- ) -> FloatRect {
- // A chain with three-dimensional transforms cannot be applied one two-dimensional projection at a time.
- if self.chain_contains_3d_transform(index) {
- return map_rect_through_matrix(
- self.accumulated_matrix(index, scroll_offsets, include_visual_viewport_transform),
- source_rect,
- MINIMUM_PROJECTION_W,
- );
- }
-
- let mut rect = source_rect;
- let mut current = index;
- loop {
- let node = &self.spatial_nodes[current.0 as usize];
- if current != VISUAL_VIEWPORT_NODE_INDEX
- || include_visual_viewport_transform == IncludeVisualViewportTransform::Yes
- {
- rect = self
- .local_transform(current, scroll_offsets, ScrollSnap::None)
- .matrix
- .extract_2d_affine()
- .map_rect(rect);
- }
- if current == VISUAL_VIEWPORT_NODE_INDEX {
- break;
- }
- current = node.parent;
- }
-
- rect
- }
-
- /// How far the nodes on the spatial chain from `index` up to the viewport move a box when its transforms are left
- /// out, in device pixels.
- pub fn offset_to_viewport_ignoring_transforms(
- &self,
- index: SpatialNodeIndex,
- scroll_offsets: &[FloatPoint],
- ) -> FloatPoint {
- let mut offset = FloatPoint::default();
- let mut current = index;
- while current != VISUAL_VIEWPORT_NODE_INDEX {
- if let LocalTransformKind::Translation(node_offset) =
- self.local_transform(current, scroll_offsets, ScrollSnap::None).kind
- {
- offset.x += node_offset.x;
- offset.y += node_offset.y;
- }
- current = self.spatial_nodes[current.0 as usize].parent;
- }
- offset
- }
-
/// The sum of the resolved offsets of the sticky nodes on the spatial chain from `index` up to the viewport, in
/// device pixels.
pub fn accumulated_sticky_offset(&self, index: SpatialNodeIndex, resolved_offsets: &[FloatPoint]) -> FloatPoint {
@@ -830,6 +707,31 @@ mod tests {
}
}
+ fn hit_test_point(
+ tree: &VisualContextTree,
+ context: ContextRef,
+ screen_point: FloatPoint,
+ scroll_offsets: &[FloatPoint],
+ clip_behavior: ClipBehavior,
+ ) -> Option<FloatPoint> {
+ let world = WorldTransforms::new(tree, scroll_offsets, None, ScrollSnap::None);
+ tree.transform_point_for_hit_test(&world, context, screen_point, clip_behavior)
+ }
+
+ fn rect_to_viewport(
+ tree: &VisualContextTree,
+ node: SpatialNodeIndex,
+ rect: FloatRect,
+ scroll_offsets: &[FloatPoint],
+ include_visual_viewport_transform: IncludeVisualViewportTransform,
+ ) -> FloatRect {
+ WorldTransforms::new(tree, scroll_offsets, None, ScrollSnap::None).transform_rect_to_viewport(
+ node,
+ rect,
+ include_visual_viewport_transform,
+ )
+ }
+
fn point(x: f32, y: f32) -> FloatPoint {
FloatPoint { x, y }
}
@@ -1022,14 +924,8 @@ mod tests {
VISUAL_VIEWPORT_NODE_INDEX,
);
let context = context_of(VISUAL_VIEWPORT_NODE_INDEX, context);
- assert!(
- tree.transform_point_for_hit_test(context, point(15.0, 15.0), &[], ClipBehavior::Respect)
- .is_none()
- );
- assert!(
- tree.transform_point_for_hit_test(context, point(5.0, 5.0), &[], ClipBehavior::Respect)
- .is_some()
- );
+ assert!(hit_test_point(&tree, context, point(15.0, 15.0), &[], ClipBehavior::Respect).is_none());
+ assert!(hit_test_point(&tree, context, point(5.0, 5.0), &[], ClipBehavior::Respect).is_some());
}
#[test]
@@ -1041,14 +937,8 @@ mod tests {
VISUAL_VIEWPORT_NODE_INDEX,
);
let context = context_of(VISUAL_VIEWPORT_NODE_INDEX, context);
- assert!(
- tree.transform_point_for_hit_test(context, point(10.25, 15.0), &[], ClipBehavior::Respect)
- .is_none()
- );
- assert!(
- tree.transform_point_for_hit_test(context, point(10.75, 15.0), &[], ClipBehavior::Respect)
- .is_some()
- );
+ assert!(hit_test_point(&tree, context, point(10.25, 15.0), &[], ClipBehavior::Respect).is_none());
+ assert!(hit_test_point(&tree, context, point(10.75, 15.0), &[], ClipBehavior::Respect).is_some());
}
#[test]
@@ -1060,18 +950,9 @@ mod tests {
VISUAL_VIEWPORT_NODE_INDEX,
);
let context = context_of(VISUAL_VIEWPORT_NODE_INDEX, context);
- assert!(
- tree.transform_point_for_hit_test(context, point(9.5, 15.0), &[], ClipBehavior::Respect)
- .is_none()
- );
- assert!(
- tree.transform_point_for_hit_test(context, point(29.5, 15.0), &[], ClipBehavior::Respect)
- .is_some()
- );
- assert!(
- tree.transform_point_for_hit_test(context, point(9.5, 15.0), &[], ClipBehavior::Ignore)
- .is_some()
- );
+ assert!(hit_test_point(&tree, context, point(9.5, 15.0), &[], ClipBehavior::Respect).is_none());
+ assert!(hit_test_point(&tree, context, point(29.5, 15.0), &[], ClipBehavior::Respect).is_some());
+ assert!(hit_test_point(&tree, context, point(9.5, 15.0), &[], ClipBehavior::Ignore).is_some());
}
#[test]
@@ -1087,18 +968,9 @@ mod tests {
VISUAL_VIEWPORT_NODE_INDEX,
);
let context = context_of(VISUAL_VIEWPORT_NODE_INDEX, context);
- assert!(
- tree.transform_point_for_hit_test(context, point(1.0, 1.0), &[], ClipBehavior::Respect)
- .is_none()
- );
- assert!(
- tree.transform_point_for_hit_test(context, point(50.0, 1.0), &[], ClipBehavior::Respect)
- .is_some()
- );
- assert!(
- tree.transform_point_for_hit_test(context, point(98.0, 98.0), &[], ClipBehavior::Respect)
- .is_none()
- );
+ assert!(hit_test_point(&tree, context, point(1.0, 1.0), &[], ClipBehavior::Respect).is_none());
+ assert!(hit_test_point(&tree, context, point(50.0, 1.0), &[], ClipBehavior::Respect).is_some());
+ assert!(hit_test_point(&tree, context, point(98.0, 98.0), &[], ClipBehavior::Respect).is_none());
}
#[test]
@@ -1119,14 +991,14 @@ mod tests {
let mut scroll_offsets = vec![FloatPoint::default(); 4];
scroll_offsets[scroll_node.0 as usize] = point(0.0, -100.0);
- let local = tree
- .transform_point_for_hit_test(
- context_of(scroll_node, ContextRef::default()),
- point(40.0, 60.0),
- &scroll_offsets,
- ClipBehavior::Respect,
- )
- .expect("the point maps");
+ let local = hit_test_point(
+ &tree,
+ context_of(scroll_node, ContextRef::default()),
+ point(40.0, 60.0),
+ &scroll_offsets,
+ ClipBehavior::Respect,
+ )
+ .expect("the point maps");
assert_eq!(local, point(20.0, 125.0));
assert_eq!(
tree.inverse_transform_point(scroll_node, point(40.0, 60.0)),
@@ -1151,21 +1023,22 @@ mod tests {
)),
perspective,
);
- let local = tree
- .transform_point_for_hit_test(
- context_of(lifted, ContextRef::default()),
- point(100.0, 100.0),
- &[],
- ClipBehavior::Respect,
- )
- .expect("the point maps");
+ let local = hit_test_point(
+ &tree,
+ context_of(lifted, ContextRef::default()),
+ point(100.0, 100.0),
+ &[],
+ ClipBehavior::Respect,
+ )
+ .expect("the point maps");
assert_eq!(local, point(50.0, 50.0));
assert_eq!(
tree.inverse_transform_point(lifted, point(100.0, 100.0)),
point(50.0, 50.0)
);
assert_eq!(
- tree.transform_rect_to_viewport(
+ rect_to_viewport(
+ &tree,
lifted,
FloatRect::new(0.0, 0.0, 10.0, 10.0),
&[],
@@ -1192,7 +1065,8 @@ mod tests {
)),
perspective,
);
- let rect = tree.transform_rect_to_viewport(
+ let rect = rect_to_viewport(
+ &tree,
on_the_eye_plane,
FloatRect::new(0.0, 0.0, 10.0, 10.0),
&[],
@@ -1201,7 +1075,8 @@ mod tests {
assert!(rect.width.is_finite() && rect.height.is_finite());
assert_eq!(rect.width, 100000.0);
assert!(
- tree.transform_point_for_hit_test(
+ hit_test_point(
+ &tree,
context_of(on_the_eye_plane, ContextRef::default()),
point(100.0, 100.0),
&[],
@@ -1226,7 +1101,8 @@ mod tests {
flipped,
);
assert!(
- tree.transform_point_for_hit_test(
+ hit_test_point(
+ &tree,
context_of(marker, ContextRef::default()),
point(0.0, 0.0),
&[],
@@ -1244,7 +1120,8 @@ mod tests {
VISUAL_VIEWPORT_NODE_INDEX,
);
assert_eq!(
- front_facing_tree.transform_point_for_hit_test(
+ hit_test_point(
+ &front_facing_tree,
context_of(front_marker, ContextRef::default()),
point(3.0, 4.0),
&[],
@@ -1268,7 +1145,8 @@ mod tests {
let fixed_background_context = context_of(VISUAL_VIEWPORT_NODE_INDEX, context);
assert!(
- tree.transform_point_for_hit_test(
+ hit_test_point(
+ &tree,
fixed_background_context,
point(10.0, 120.0),
&scroll_offsets,
@@ -1277,7 +1155,8 @@ mod tests {
.is_none()
);
assert_eq!(
- tree.transform_point_for_hit_test(
+ hit_test_point(
+ &tree,
fixed_background_context,
point(10.0, -60.0),
&scroll_offsets,
@@ -1300,11 +1179,23 @@ mod tests {
let rect = FloatRect::new(0.0, 0.0, 10.0, 10.0);
assert_eq!(
- tree.transform_rect_to_viewport(scroll_node, rect, &scroll_offsets, IncludeVisualViewportTransform::No),
+ rect_to_viewport(
+ &tree,
+ scroll_node,
+ rect,
+ &scroll_offsets,
+ IncludeVisualViewportTransform::No
+ ),
FloatRect::new(9.0, 18.0, 10.0, 10.0)
);
assert_eq!(
- tree.transform_rect_to_viewport(scroll_node, rect, &scroll_offsets, IncludeVisualViewportTransform::Yes),
+ rect_to_viewport(
+ &tree,
+ scroll_node,
+ rect,
+ &scroll_offsets,
+ IncludeVisualViewportTransform::Yes
+ ),
FloatRect::new(18.0, 36.0, 20.0, 20.0)
);
}
@@ -1615,13 +1506,20 @@ mod tests {
let rect = FloatRect::new(0.0, 0.0, 10.0, 10.0);
for node in [backface, inner_sticky, sorted_transform] {
assert_eq!(
- permuted.transform_rect_to_viewport(
+ rect_to_viewport(
+ &permuted,
mapped(node),
rect,
&permuted_offsets,
IncludeVisualViewportTransform::Yes
),
- in_order.transform_rect_to_viewport(node, rect, &in_order_offsets, IncludeVisualViewportTransform::Yes)
+ rect_to_viewport(
+ &in_order,
+ node,
+ rect,
+ &in_order_offsets,
+ IncludeVisualViewportTransform::Yes
+ )
);
}
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs b/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs
index 76d25de363d..3f6cf32b83d 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/serialize.rs
@@ -1729,12 +1729,12 @@ mod tests {
assert_eq!(decoded.clip_dependency_order(), vec![1, 0]);
assert_eq!(decoded.effect_dependency_order(), vec![1, 0]);
assert_eq!(
- decoded.transform_rect_to_viewport(
- SpatialNodeIndex(1),
- FloatRect::new(0.0, 0.0, 10.0, 10.0),
- &[],
- super::super::IncludeVisualViewportTransform::Yes
- ),
+ super::super::WorldTransforms::new(&decoded, &[], None, super::super::ScrollSnap::None)
+ .transform_rect_to_viewport(
+ SpatialNodeIndex(1),
+ FloatRect::new(0.0, 0.0, 10.0, 10.0),
+ super::super::IncludeVisualViewportTransform::Yes
+ ),
FloatRect::new(5.0, 5.0, 10.0, 10.0)
);
}
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/visual_animations.rs b/Libraries/LibCompositing/Rust/src/visual_context/visual_animations.rs
index fd9f8babb71..4665e2c8677 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/visual_animations.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/visual_animations.rs
@@ -7,8 +7,8 @@
//! The compositor animations a visual context tree carries, and the trees derived by sampling them.
use super::{
- ClipBehavior, ClipNodeIndex, ContextRef, EffectNodeData, EffectNodeIndex, EffectsData, SpatialData,
- SpatialNodeIndex, VisualContextTree,
+ ClipBehavior, ClipNodeIndex, ContextRef, EffectNodeData, EffectNodeIndex, EffectsData, ScrollSnap, SpatialData,
+ SpatialNodeIndex, VisualContextTree, WorldTransforms,
};
use crate::host::{FfiVisualAnimationSummary, FfiVisualAnimationTargetKind, FfiVisualAnimationTransformOperationKind};
use crate::visual_animation::{VisualAnimation, VisualAnimationSample, VisualAnimationValue};
@@ -115,6 +115,7 @@ impl VisualContextTree {
screen_point: FloatPoint,
scroll_offsets: &[FloatPoint],
) -> bool {
+ let world = std::cell::OnceCell::new();
self.visual_animations.iter().any(|animation| {
animation.target_kind == FfiVisualAnimationTargetKind::Transform
&& animation.is_active_at(sample_time_ns)
@@ -129,7 +130,9 @@ impl VisualContextTree {
clip: ClipNodeIndex::NONE,
effect: EffectNodeIndex::NONE,
};
- self.transform_point_for_hit_test(context, screen_point, scroll_offsets, ClipBehavior::Ignore)
+ let world =
+ world.get_or_init(|| WorldTransforms::new(self, scroll_offsets, None, ScrollSnap::None));
+ self.transform_point_for_hit_test(world, context, screen_point, ClipBehavior::Ignore)
.is_some_and(|local_point| transform.bounds.contains_point(local_point))
})
})
diff --git a/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs b/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs
index a3e9d6b5b5e..5d05f924c23 100644
--- a/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs
+++ b/Libraries/LibCompositing/Rust/src/visual_context/world_transforms.rs
@@ -4,8 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use super::{ScrollSnap, SpatialNodeIndex, VisualContextTree, should_cull_back_face};
-use libgfx_rust::{FloatMatrix4x4, FloatPoint, FloatRect, map_rect_through_matrix, multiply_affine};
+use super::{
+ IncludeVisualViewportTransform, LocalTransformKind, ScrollSnap, SpatialNodeIndex, VISUAL_VIEWPORT_NODE_INDEX,
+ VisualContextTree, should_cull_back_face,
+};
+use libgfx_rust::{FloatMatrix4x4, FloatPoint, FloatRect, FloatSize, map_rect_through_matrix, multiply_affine};
// Homogeneous coordinates this close to the eye plane have no meaningful projection.
pub const MINIMUM_PROJECTION_W: f32 = 0.0001;
@@ -13,7 +16,8 @@ pub const MINIMUM_PROJECTION_W: f32 = 0.0001;
/// The to-root matrix of every spatial node under one set of scroll offsets, filled in dependency order so that any
/// node's space is entered with a single matrix. A backface marker's entry carries the flattened matrix that feeds its
/// cull test and its descendants, while content recorded directly under the marker belongs to its parent's plane, so
-/// `draw_space` redirects the marker to the parent's entry.
+/// `draw_space` redirects the marker to the parent's entry. A table filled without a root parent matrix is in the
+/// layout viewport's space, and the queries below add the visual viewport transform where asked.
#[derive(Default)]
pub struct WorldTransforms {
pub local: Vec<FloatMatrix4x4>,
@@ -49,7 +53,7 @@ impl WorldTransforms {
pub fn new(
tree: &VisualContextTree,
scroll_offsets: &[FloatPoint],
- root_parent_matrix: FloatMatrix4x4,
+ root_parent_matrix: Option<FloatMatrix4x4>,
scroll_snap: ScrollSnap,
) -> Self {
let mut world = Self::default();
@@ -71,19 +75,20 @@ impl WorldTransforms {
self.draw_space.clear();
}
- /// Fills the table, with `root_parent_matrix` as what the visual viewport node's transform applies on top of.
+ /// Fills the table, with `root_parent_matrix` as what the visual viewport node's transform applies on top of, or
+ /// with that transform left out.
pub fn fill(
&mut self,
tree: &VisualContextTree,
spatial_dependency_order: &[u32],
scroll_offsets: &[FloatPoint],
- root_parent_matrix: FloatMatrix4x4,
+ root_parent_matrix: Option<FloatMatrix4x4>,
scroll_snap: ScrollSnap,
) {
let node_count = tree.spatial_nodes.len();
self.clear();
self.local.resize(node_count, FloatMatrix4x4::identity());
- self.to_root.resize(node_count, root_parent_matrix);
+ self.to_root.resize(node_count, FloatMatrix4x4::identity());
self.flattens_inherited_transform.resize(node_count, false);
self.backface_culled.resize(node_count, false);
self.draw_space
@@ -92,12 +97,11 @@ impl WorldTransforms {
let i = index as usize;
let parent = tree.spatial_nodes[i].parent.0 as usize;
let local = tree.local_transform(SpatialNodeIndex(index), scroll_offsets, scroll_snap);
- let parent_matrix = if i == 0 {
- root_parent_matrix
- } else {
- self.to_root[parent]
+ self.to_root[i] = match (i, root_parent_matrix) {
+ (0, None) => FloatMatrix4x4::identity(),
+ (0, Some(root_parent_matrix)) => local.applied_to(root_parent_matrix),
+ _ => local.applied_to(self.to_root[parent]),
};
- self.to_root[i] = local.applied_to(parent_matrix);
self.local[i] = local.matrix;
self.flattens_inherited_transform[i] = local.flattens_inherited_transform;
let mut culled = i != 0 && self.backface_culled[parent];
@@ -130,11 +134,73 @@ impl WorldTransforms {
/// Where content drawn within `rect` in `space` lands in the root's space.
pub fn map_rect_to_root(&self, space: SpatialNodeIndex, rect: FloatRect) -> FloatRect {
- let matrix = self.draw_matrix(space);
- if matrix.is_2d_affine() {
- return matrix.extract_2d_affine().map_rect(rect);
+ map_rect_through(self.draw_matrix(space), rect)
+ }
+
+ /// The matrix from `node`'s space to the viewport, in a table filled without a root parent matrix.
+ pub fn to_viewport_matrix(
+ &self,
+ node: SpatialNodeIndex,
+ include_visual_viewport_transform: IncludeVisualViewportTransform,
+ ) -> FloatMatrix4x4 {
+ debug_assert!(self.to_root[0] == FloatMatrix4x4::identity());
+ let matrix = self.to_root[node.0 as usize];
+ match include_visual_viewport_transform {
+ IncludeVisualViewportTransform::No => matrix,
+ IncludeVisualViewportTransform::Yes => self.local[0].multiplied(matrix),
+ }
+ }
+
+ pub fn transform_rect_to_viewport(
+ &self,
+ node: SpatialNodeIndex,
+ rect: FloatRect,
+ include_visual_viewport_transform: IncludeVisualViewportTransform,
+ ) -> FloatRect {
+ map_rect_through(self.to_viewport_matrix(node, include_visual_viewport_transform), rect)
+ }
+
+ pub fn accumulated_2d_scale(
+ &self,
+ node: SpatialNodeIndex,
+ include_visual_viewport_transform: IncludeVisualViewportTransform,
+ ) -> FloatSize {
+ let affine = self
+ .to_viewport_matrix(node, include_visual_viewport_transform)
+ .extract_2d_affine();
+ FloatSize {
+ width: affine.x_scale(),
+ height: affine.y_scale(),
}
- map_rect_through_matrix(matrix, rect, MINIMUM_PROJECTION_W)
+ }
+
+ pub fn plane_depth_at_point_for_hit_test(&self, plane: SpatialNodeIndex, screen_point: FloatPoint) -> Option<f32> {
+ let inverse = self
+ .to_viewport_matrix(plane, IncludeVisualViewportTransform::Yes)
+ .inverse()?;
+ let matrix = &inverse.elements;
+ let depth = -(screen_point.x * matrix[2][0] + screen_point.y * matrix[2][1] + matrix[2][3]) / matrix[2][2];
+ depth.is_finite().then_some(depth)
+ }
+
+ /// How far the nodes on the spatial chain from `node` up to the viewport move a box when its transforms are left
+ /// out, in device pixels.
+ pub fn offset_to_viewport_ignoring_transforms(
+ &self,
+ tree: &VisualContextTree,
+ node: SpatialNodeIndex,
+ ) -> FloatPoint {
+ let mut offset = FloatPoint::default();
+ let mut current = node;
+ while current != VISUAL_VIEWPORT_NODE_INDEX {
+ if let LocalTransformKind::Translation(_) = tree.local_transform(current, &[], ScrollSnap::None).kind {
+ let local = &self.local[current.0 as usize].elements;
+ offset.x += local[0][3];
+ offset.y += local[1][3];
+ }
+ current = tree.spatial_nodes[current.0 as usize].parent;
+ }
+ offset
}
/// Maps `rect` from `from`'s space into `to`'s, or gives `None` when either space is not a two-dimensional
@@ -157,3 +223,10 @@ impl WorldTransforms {
Some(multiply_affine(root_to_target, from_matrix.extract_2d_affine()).map_rect(rect))
}
}
+
+fn map_rect_through(matrix: FloatMatrix4x4, rect: FloatRect) -> FloatRect {
+ if matrix.is_2d_affine() {
+ return matrix.extract_2d_affine().map_rect(rect);
+ }
+ map_rect_through_matrix(matrix, rect, MINIMUM_PROJECTION_W)
+}
diff --git a/Libraries/LibCompositing/Scrolling/ScrollState.cpp b/Libraries/LibCompositing/Scrolling/ScrollState.cpp
index 33b894bcbf9..d08a21ad7c2 100644
--- a/Libraries/LibCompositing/Scrolling/ScrollState.cpp
+++ b/Libraries/LibCompositing/Scrolling/ScrollState.cpp
@@ -6,10 +6,34 @@
#include <AK/NumericLimits.h>
#include <AK/Vector.h>
+#include <LibCompositing/DisplayList/AccumulatedVisualContext.h>
+#include <LibCompositing/RustFFI.h>
#include <LibCompositing/Scrolling/ScrollState.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
+namespace Compositing {
+
+void const* WorldTransformsCache::get_or_build(AccumulatedVisualContextTree const& tree, ReadonlySpan<Gfx::FloatPoint> device_offsets)
+{
+ if (m_world_transforms && m_retained_tree == tree.rust_handle())
+ return m_world_transforms;
+ clear();
+ m_retained_tree = RustFFI::visual_context_tree_retain(tree.rust_handle());
+ m_world_transforms = RustFFI::world_transforms_create(m_retained_tree, device_offsets.data(), device_offsets.size());
+ return m_world_transforms;
+}
+
+void WorldTransformsCache::clear()
+{
+ if (m_world_transforms)
+ RustFFI::world_transforms_destroy(exchange(m_world_transforms, nullptr));
+ if (m_retained_tree)
+ RustFFI::visual_context_tree_release(exchange(m_retained_tree, nullptr));
+}
+
+}
+
namespace IPC {
// The dense in-process vector spans the whole spatial node index space, but only scroll and
diff --git a/Libraries/LibCompositing/Scrolling/ScrollState.h b/Libraries/LibCompositing/Scrolling/ScrollState.h
index 67fdeafb190..7c26ef26c84 100644
--- a/Libraries/LibCompositing/Scrolling/ScrollState.h
+++ b/Libraries/LibCompositing/Scrolling/ScrollState.h
@@ -19,6 +19,28 @@
namespace Compositing {
+// The world transforms built for one visual context tree under a snapshot's offsets. A copy starts empty, so the table
+// never outlives the offsets it was built for.
+class COMPOSITING_API WorldTransformsCache {
+public:
+ WorldTransformsCache() = default;
+ WorldTransformsCache(WorldTransformsCache const&) { }
+ WorldTransformsCache& operator=(WorldTransformsCache const&)
+ {
+ clear();
+ return *this;
+ }
+ ~WorldTransformsCache() { clear(); }
+
+ void const* get_or_build(AccumulatedVisualContextTree const&, ReadonlySpan<Gfx::FloatPoint> device_offsets);
+ void clear();
+
+private:
+ // The cache retains the tree, so the tree it describes cannot change under it: a write to a shared tree copies it.
+ void const* m_retained_tree { nullptr };
+ void* m_world_transforms { nullptr };
+};
+
// Device-pixel offsets keyed by SpatialNodeIndex: the scroll containers' offsets as produced by
// the document, plus the sticky nodes' offsets derived from them and the tree (by the document's
// scroll state refresh in process, by resolve_sticky_offsets() in the compositor). Stored dense in process so display list replay and hit testing index it directly;
@@ -34,6 +56,12 @@ class ScrollStateSnapshot {
public:
ReadonlySpan<Gfx::FloatPoint> device_offsets() const { return m_device_offsets; }
+ // The world transforms of the tree under these offsets, in the layout viewport's space, kept until either changes.
+ void const* world_transforms(AccumulatedVisualContextTree const& tree) const
+ {
+ return m_world_transforms_cache.get_or_build(tree, m_device_offsets);
+ }
+
Gfx::FloatPoint device_offset_for_index(SpatialNodeIndex index) const
{
if (index.value() >= m_device_offsets.size())
@@ -45,6 +73,7 @@ public:
{
if (index.value() >= m_node_count)
return;
+ m_world_transforms_cache.clear();
if (index.value() >= m_device_offsets.size()) {
m_device_offsets.grow_capacity(index.value() + 1);
m_device_offsets.resize(index.value() + 1);
@@ -55,6 +84,7 @@ public:
// Replaces every offset with a freshly derived dense array, keeping the node count bound.
void assign_device_offsets(ReadonlySpan<Gfx::FloatPoint> offsets)
{
+ m_world_transforms_cache.clear();
m_device_offsets.clear_with_capacity();
m_device_offsets.append(offsets.data(), min(offsets.size(), m_node_count));
}
@@ -65,6 +95,7 @@ public:
// And any offset already stored past the count is discarded. Every later mutation stays bounded by the count.
void set_node_count(size_t node_count)
{
+ m_world_transforms_cache.clear();
m_node_count = node_count;
if (m_device_offsets.size() > node_count)
m_device_offsets.resize(node_count);
@@ -89,6 +120,7 @@ private:
Vector<Gfx::FloatPoint> m_device_offsets;
Vector<StagedOffset> m_staged_offsets;
size_t m_node_count { NumericLimits<size_t>::max() };
+ mutable WorldTransformsCache m_world_transforms_cache;
};
}
diff --git a/Libraries/LibWeb/Painting/BoxViews.cpp b/Libraries/LibWeb/Painting/BoxViews.cpp
index 71de7828d55..ca21001e552 100644
--- a/Libraries/LibWeb/Painting/BoxViews.cpp
+++ b/Libraries/LibWeb/Painting/BoxViews.cpp
@@ -851,9 +851,11 @@ Layout::RustFFI::FfiRectToViewportTransform rect_to_viewport_transform(DOM::Docu
{
if (!document.has_committed_viewport_box())
return identity_rect_to_viewport_transform();
- auto scroll_offsets = document.scroll_state_snapshot().device_offsets();
+ auto const& scroll_state = document.scroll_state_snapshot();
+ auto scroll_offsets = scroll_state.device_offsets();
return {
.visual_context_tree = visual_context_tree.rust_handle(),
+ .world_transforms = scroll_state.world_transforms(visual_context_tree),
.scroll_offsets = scroll_offsets.data(),
.scroll_offsets_len = scroll_offsets.size(),
.device_pixels_per_css_pixel = static_cast<float>(document.page().client().device_pixels_per_css_pixel()),
diff --git a/Libraries/LibWeb/Painting/HitTestDisplayList.cpp b/Libraries/LibWeb/Painting/HitTestDisplayList.cpp
index e2926ec1aaa..74a87d4ec90 100644
--- a/Libraries/LibWeb/Painting/HitTestDisplayList.cpp
+++ b/Libraries/LibWeb/Painting/HitTestDisplayList.cpp
@@ -83,6 +83,7 @@ struct HitTestDisplayList::QueryContext {
.device_pixels_per_css_pixel = query ? query->device_pixels_per_css_pixel() : 1,
.scroll_offsets = scroll_offsets.data(),
.scroll_offsets_len = scroll_offsets.size(),
+ .world_transforms = nullptr,
.has_chrome_metrics = query != nullptr,
.chrome_metrics = {},
.has_visual_animation_sample_time = query && query->visual_animation_sample_time_nanoseconds().has_value(),
diff --git a/Libraries/LibWeb/Rust/src/layout/script_entries.rs b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
index c7f280cbe3c..988d63292b6 100644
--- a/Libraries/LibWeb/Rust/src/layout/script_entries.rs
+++ b/Libraries/LibWeb/Rust/src/layout/script_entries.rs
@@ -451,9 +451,8 @@ fn spatial_chain_offset_in_css_pixels(
slot: NodeSlotId,
rect_to_viewport_transform: FfiRectToViewportTransform<'_>,
device_offset_of_spatial_chain: impl Fn(
- &crate::painting::visual_context::VisualContextTree,
+ &crate::painting::rect_to_viewport_transform::RectToViewportTransform<'_>,
crate::painting::visual_context::SpatialNodeIndex,
- &[libgfx_rust::FloatPoint],
) -> libgfx_rust::FloatPoint,
) -> crate::painting::ffi::FfiOptionalCssPixelPoint {
read_rows(host, |arena| {
@@ -469,7 +468,7 @@ fn spatial_chain_offset_in_css_pixels(
return result;
}
let spatial = arena.paintable_data(slot).accumulated_visual_context.spatial;
- let offset = device_offset_of_spatial_chain(transform.visual_context_tree, spatial, transform.scroll_offsets);
+ let offset = device_offset_of_spatial_chain(&transform, spatial);
let inverse_pixel_ratio = 1.0 / transform.device_pixels_per_css_pixel;
result.has_value = true;
result.x = CssPixels::nearest_value_for_f32(offset.x * inverse_pixel_ratio);
@@ -487,12 +486,11 @@ pub extern "C" fn layout_script_accumulated_sticky_offset(
rect_to_viewport_transform: FfiRectToViewportTransform<'_>,
out: &mut crate::painting::ffi::FfiOptionalCssPixelPoint,
) {
- *out = spatial_chain_offset_in_css_pixels(
- host,
- slot,
- rect_to_viewport_transform,
- crate::painting::visual_context::VisualContextTree::accumulated_sticky_offset,
- );
+ *out = spatial_chain_offset_in_css_pixels(host, slot, rect_to_viewport_transform, |transform, spatial| {
+ transform
+ .visual_context_tree
+ .accumulated_sticky_offset(spatial, transform.scroll_offsets)
+ });
}
/// The offset by which scrolling, sticky positioning and anchor scroll shifts move the box, without its transforms.
@@ -503,12 +501,11 @@ pub extern "C" fn layout_script_offset_to_viewport_ignoring_transforms(
rect_to_viewport_transform: FfiRectToViewportTransform<'_>,
out: &mut crate::painting::ffi::FfiOptionalCssPixelPoint,
) {
- *out = spatial_chain_offset_in_css_pixels(
- host,
- slot,
- rect_to_viewport_transform,
- crate::painting::visual_context::VisualContextTree::offset_to_viewport_ignoring_transforms,
- );
+ *out = spatial_chain_offset_in_css_pixels(host, slot, rect_to_viewport_transform, |transform, spatial| {
+ transform
+ .world_transforms
+ .offset_to_viewport_ignoring_transforms(transform.visual_context_tree, spatial)
+ });
}
#[unsafe(no_mangle)]
diff --git a/Libraries/LibWeb/Rust/src/painting/ffi.rs b/Libraries/LibWeb/Rust/src/painting/ffi.rs
index 7fa7840590f..880b38ad9fd 100644
--- a/Libraries/LibWeb/Rust/src/painting/ffi.rs
+++ b/Libraries/LibWeb/Rust/src/painting/ffi.rs
@@ -22,7 +22,7 @@ use crate::painting::rect_to_viewport_transform::RectToViewportTransform;
use crate::painting::svg_filter::SvgFilterPrimitive;
use crate::painting::visual_animation_builder::CompositorAnimationEffectState;
use crate::render_state::{DocumentHost, RenderWait};
-use libcompositing_rust::ffi::tree_from_handle;
+use libcompositing_rust::ffi::{tree_from_handle, world_transforms_from_handle};
use std::ffi::c_void;
mod main_thread_entries;
@@ -757,10 +757,12 @@ pub struct FfiEmptyLineCaretRect {
}
/// What maps a rect to the viewport, as C++ lends it for `'a`: a non-null `visual_context_tree` is a live retained tree
-/// handle, and `scroll_offsets` addresses `scroll_offsets_len` points. Rust never makes one.
+/// handle with `world_transforms` a live table built from it, and `scroll_offsets` addresses `scroll_offsets_len`
+/// points. Rust never makes one.
#[repr(C)]
pub struct FfiRectToViewportTransform<'a> {
visual_context_tree: *const c_void,
+ world_transforms: *const c_void,
scroll_offsets: *const libgfx_rust::FloatPoint,
scroll_offsets_len: usize,
device_pixels_per_css_pixel: f32,
@@ -777,6 +779,7 @@ impl<'a> FfiRectToViewportTransform<'a> {
unsafe {
Some(RectToViewportTransform {
visual_context_tree: tree_from_handle(self.visual_context_tree),
+ world_transforms: world_transforms_from_handle(self.world_transforms),
scroll_offsets: ak::slice_from_raw_parts(self.scroll_offsets, self.scroll_offsets_len),
device_pixels_per_css_pixel: self.device_pixels_per_css_pixel,
})
diff --git a/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs b/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs
index 9ca9afa9b96..19bd76c32f3 100644
--- a/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs
+++ b/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs
@@ -1025,7 +1025,8 @@ pub unsafe extern "C" fn layout_hit_test_find_topmost_item(
|list, tree, arena| {
let mut sampled = None;
let tree = tree_as_the_query_reads_it(tree, &callbacks, &mut sampled);
- ffi_topmost(list.find_topmost_item(arena, tree, &callbacks, point.into()))
+ let world = callbacks.build_world_transforms(tree);
+ ffi_topmost(list.find_topmost_item(arena, tree, &callbacks.with_world_transforms(&world), point.into()))
},
);
}
@@ -1050,7 +1051,8 @@ pub unsafe extern "C" fn layout_hit_test_all(
|list, tree, arena| {
let mut sampled = None;
let tree = tree_as_the_query_reads_it(tree, &callbacks, &mut sampled);
- list.hit_test_all(arena, tree, &callbacks, point.into())
+ let world = callbacks.build_world_transforms(tree);
+ list.hit_test_all(arena, tree, &callbacks.with_world_transforms(&world), point.into())
},
);
for index in indices {
@@ -1079,6 +1081,7 @@ pub unsafe extern "C" fn layout_hit_test_caret_position_from_point(
let main_thread = unsafe { main_thread(host) };
let mode = crate::painting::hit_test::caret::CaretPositionMode::from_u8(mode);
*out = with_hit_test_list_spatial_indexes_and_visual_context_tree(host, read, true, None, |list, tree, arena| {
+ let world = callbacks.build_world_transforms(tree);
list.caret_position_from_point(
&HostNodeScopes {
main_thread: &main_thread,
@@ -1086,7 +1089,7 @@ pub unsafe extern "C" fn layout_hit_test_caret_position_from_point(
},
arena,
tree,
- &callbacks,
+ &callbacks.with_world_transforms(&world),
point.into(),
mode,
constraint_scope,
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
index 215a28fbe43..b8ef6655e6b 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/caret.rs
@@ -839,6 +839,7 @@ impl FfiCaretAt {
if self.caret.has_debug_rect {
let transform = RectToViewportTransform {
visual_context_tree: tree,
+ world_transforms: callbacks.world_transforms(),
scroll_offsets: callbacks.scroll_offsets(),
device_pixels_per_css_pixel: callbacks.device_pixels_per_css_pixel as f32,
};
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
index 48cda4a13c4..db2015c5592 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
@@ -440,9 +440,9 @@ pub(crate) fn local_float_point(
};
let clip_behavior = ClipBehavior::from_respect_clip(respect_clip);
let local = visual_context_tree.transform_point_for_hit_test(
+ callbacks.world_transforms(),
context,
screen_point,
- callbacks.scroll_offsets(),
clip_behavior,
)?;
Some((local.x / pixel_ratio, local.y / pixel_ratio))
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/query.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/query.rs
index 8cb7dc09997..60fcb7dd4db 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/query.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/query.rs
@@ -9,14 +9,16 @@ use crate::painting::chrome_geometry::{ChromeGeometry, scrollbar_is_hidden};
use crate::painting::ffi::ScrollDirection;
use crate::painting::host::FfiHitTestQueryCallbacks;
use crate::painting::paint_read::PaintRead;
-use crate::painting::visual_context::{NO_SORTING_CONTEXT, SortingContexts, SpatialNodeIndex, VisualContextTree};
+use crate::painting::visual_context::{
+ NO_SORTING_CONTEXT, SortingContexts, SpatialNodeIndex, VisualContextTree, WorldTransforms,
+};
use std::collections::HashMap;
struct DepthSortingState<'a> {
tree: &'a VisualContextTree,
point: CssPixelPoint,
device_pixels_per_css_pixel: f32,
- scroll_offsets: &'a [libgfx_rust::FloatPoint],
+ world_transforms: &'a WorldTransforms,
sorting_contexts: Option<SortingContexts>,
depth_key_by_plane: HashMap<u32, Option<i64>>,
}
@@ -27,7 +29,7 @@ impl<'a> DepthSortingState<'a> {
tree,
point,
device_pixels_per_css_pixel: callbacks.device_pixels_per_css_pixel as f32,
- scroll_offsets: callbacks.scroll_offsets(),
+ world_transforms: callbacks.world_transforms(),
sorting_contexts: None,
depth_key_by_plane: HashMap::new(),
}
@@ -69,8 +71,8 @@ impl<'a> DepthSortingState<'a> {
y: self.point.y.to_float() * self.device_pixels_per_css_pixel,
};
let depth = self
- .tree
- .plane_depth_at_point_for_hit_test(leaf, screen_point, self.scroll_offsets);
+ .world_transforms
+ .plane_depth_at_point_for_hit_test(leaf, screen_point);
const DEPTH_LIMIT: f32 = 16777216.0;
let depth_key = depth.map(|depth| (depth.clamp(-DEPTH_LIMIT, DEPTH_LIMIT) * 8.0).round() as i64);
self.depth_key_by_plane.insert(leaf.0, depth_key);
diff --git a/Libraries/LibWeb/Rust/src/painting/host/hit_test.rs b/Libraries/LibWeb/Rust/src/painting/host/hit_test.rs
index 0dcea107dec..1578720c718 100644
--- a/Libraries/LibWeb/Rust/src/painting/host/hit_test.rs
+++ b/Libraries/LibWeb/Rust/src/painting/host/hit_test.rs
@@ -16,6 +16,8 @@ pub struct FfiHitTestQueryCallbacks {
pub device_pixels_per_css_pixel: f64,
pub scroll_offsets: *const libgfx_rust::FloatPoint,
pub scroll_offsets_len: usize,
+ /// Private: the world transforms a Rust entry point builds for the query, from the tree it reads and the offsets.
+ world_transforms: *const c_void,
pub has_chrome_metrics: bool,
pub chrome_metrics: crate::painting::ffi::FfiChromeMetrics,
/// Where set, the query reads the compositor's animations at this time, as a frame of the compositor's shows them.
@@ -41,6 +43,7 @@ impl FfiHitTestQueryCallbacks {
device_pixels_per_css_pixel,
scroll_offsets: scroll_offsets.as_ptr(),
scroll_offsets_len: scroll_offsets.len(),
+ world_transforms: std::ptr::null(),
has_chrome_metrics: true,
chrome_metrics,
has_visual_animation_sample_time: false,
@@ -63,6 +66,34 @@ impl FfiHitTestQueryCallbacks {
) -> bool {
(self.contains)(self.context, ancestor, node)
}
+ /// The world transforms of `tree`, the tree the query reads, under these callbacks' offsets.
+ pub(crate) fn build_world_transforms(
+ &self,
+ tree: &crate::painting::visual_context::VisualContextTree,
+ ) -> crate::painting::visual_context::WorldTransforms {
+ crate::painting::visual_context::WorldTransforms::new(
+ tree,
+ self.scroll_offsets(),
+ None,
+ crate::painting::visual_context::ScrollSnap::None,
+ )
+ }
+
+ /// These callbacks with the world transforms the query reads, which `world` holds for the query.
+ pub(crate) fn with_world_transforms(mut self, world: &crate::painting::visual_context::WorldTransforms) -> Self {
+ self.world_transforms = std::ptr::from_ref(world).cast();
+ self
+ }
+
+ pub(crate) fn world_transforms(&self) -> &crate::painting::visual_context::WorldTransforms {
+ assert!(
+ !self.world_transforms.is_null(),
+ "the query's entry point builds its world transforms"
+ );
+ // SAFETY: The entry point keeps the table alive for the synchronous query.
+ unsafe { libcompositing_rust::ffi::world_transforms_from_handle(self.world_transforms) }
+ }
+
pub(crate) fn scroll_offsets(&self) -> &[libgfx_rust::FloatPoint] {
if self.scroll_offsets.is_null() {
return &[];
diff --git a/Libraries/LibWeb/Rust/src/painting/published_frame.rs b/Libraries/LibWeb/Rust/src/painting/published_frame.rs
index d1766ef83d1..565b6f3aca2 100644
--- a/Libraries/LibWeb/Rust/src/painting/published_frame.rs
+++ b/Libraries/LibWeb/Rust/src/painting/published_frame.rs
@@ -32,8 +32,8 @@ use crate::painting::selection::{
};
use crate::painting::stacking_context::entries::StackingContextEntries;
use crate::painting::svg_paint_resources::SvgPaintResourceRows;
-use crate::painting::visual_context::VisualContextTree;
use crate::painting::visual_context::{BoxVisualContextNodeHandles, EMPTY_BOX_VISUAL_CONTEXT_NODE_HANDLES};
+use crate::painting::visual_context::{ScrollSnap, VisualContextTree, WorldTransforms};
use std::sync::Arc;
/// The document's text rows and its replaced, layer image and SVG paint resource tables, as they
@@ -158,6 +158,7 @@ impl PublishedRows {
/// What a recording reads of the document's paint state, as it was when the frame was published.
pub(crate) struct PublishedPaintState {
pub(crate) visual_context_tree: Option<Arc<VisualContextTree>>,
+ unscrolled_world_transforms: std::sync::OnceLock<WorldTransforms>,
/// Each scroll state slot's own scroll offset, shared with the document's scroll state.
scroll_own_offsets: Arc<Vec<CssPixelPoint>>,
pub(crate) has_non_viewport_wheel_scroll_target_candidate: bool,
@@ -176,6 +177,17 @@ pub(crate) struct PublishedPaintState {
}
impl PublishedPaintState {
+ /// The tree's world transforms without scroll offsets, which is all that the scale of a space depends on.
+ pub(crate) fn unscrolled_world_transforms(&self) -> &WorldTransforms {
+ self.unscrolled_world_transforms.get_or_init(|| {
+ let tree = self
+ .visual_context_tree
+ .as_deref()
+ .expect("recording runs against a visual context tree");
+ WorldTransforms::new(tree, &[], None, ScrollSnap::None)
+ })
+ }
+
pub(crate) fn highlight_pseudo_styles(&self, highlight: HighlightPseudoElement) -> &SelectionPseudoStyles {
match highlight {
HighlightPseudoElement::Selection => &self.selection_pseudo_styles,
@@ -189,6 +201,7 @@ impl PublishedPaintState {
let visual_context = &paint_state.visual_context;
Self {
visual_context_tree: visual_context.tree.clone(),
+ unscrolled_world_transforms: std::sync::OnceLock::new(),
scroll_own_offsets: visual_context.scroll_state.own_offsets().clone(),
has_non_viewport_wheel_scroll_target_candidate: visual_context
.scroll_state
diff --git a/Libraries/LibWeb/Rust/src/painting/record/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
index 5138671197c..e30dd9f3453 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
@@ -795,14 +795,8 @@ impl<O: Observer> PaintRecorder<'_, O> {
height: transform.y_scale(),
};
}
- let tree = self
- .paint_state
- .visual_context_tree
- .as_deref()
- .expect("recording runs against a visual context tree");
- tree.accumulated_2d_scale(
+ self.paint_state.unscrolled_world_transforms().accumulated_2d_scale(
spatial,
- &[],
crate::painting::visual_context::IncludeVisualViewportTransform::No,
)
}
diff --git a/Libraries/LibWeb/Rust/src/painting/rect_to_viewport_transform.rs b/Libraries/LibWeb/Rust/src/painting/rect_to_viewport_transform.rs
index 240da1d1ac8..ee66c54ccd0 100644
--- a/Libraries/LibWeb/Rust/src/painting/rect_to_viewport_transform.rs
+++ b/Libraries/LibWeb/Rust/src/painting/rect_to_viewport_transform.rs
@@ -7,11 +7,14 @@
use crate::css::css_pixels::{CssPixelRect, CssPixels};
use crate::layout::node_data::NodeSlotId;
use crate::painting::paint_read::PaintRead;
-use crate::painting::visual_context::{IncludeVisualViewportTransform, SpatialNodeIndex, VisualContextTree};
+use crate::painting::visual_context::{
+ IncludeVisualViewportTransform, SpatialNodeIndex, VisualContextTree, WorldTransforms,
+};
use libgfx_rust::{FloatPoint, FloatRect};
pub(crate) struct RectToViewportTransform<'a> {
pub visual_context_tree: &'a VisualContextTree,
+ pub world_transforms: &'a WorldTransforms,
pub scroll_offsets: &'a [FloatPoint],
pub device_pixels_per_css_pixel: f32,
}
@@ -44,12 +47,9 @@ impl RectToViewportTransform<'_> {
width: rect.width.to_float() * pixel_ratio,
height: rect.height.to_float() * pixel_ratio,
};
- let transformed = self.visual_context_tree.transform_rect_to_viewport(
- spatial,
- device_rect,
- self.scroll_offsets,
- include_visual_viewport_transform,
- );
+ let transformed =
+ self.world_transforms
+ .transform_rect_to_viewport(spatial, device_rect, include_visual_viewport_transform);
let inverse_pixel_ratio = 1.0 / pixel_ratio;
CssPixelRect {
x: CssPixels::nearest_value_for_f32(transformed.x * inverse_pixel_ratio),
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs
index e73fa62c119..1adbbd741ff 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs
@@ -1343,6 +1343,8 @@ impl Lane {
let offsets = self.scroll_offsets_on_screen(&source, &frame.visual_context, &plan);
let callbacks =
FfiHitTestQueryCallbacks::sealed(plan.device_pixels_per_css_pixel, &offsets, plan.chrome_metrics);
+ let world = callbacks.build_world_transforms(tree);
+ let callbacks = callbacks.with_world_transforms(&world);
let ratio = plan.device_pixels_per_css_pixel as f32;
let point = CssPixelPoint::new(
CssPixels::nearest_value_for_f32(position.x / ratio),
@@ -1496,6 +1498,8 @@ impl Lane {
let scroll_offsets = self.scroll_offsets_on_screen(arena, &arena.paint_state().visual_context, plan);
let callbacks =
FfiHitTestQueryCallbacks::sealed(plan.device_pixels_per_css_pixel, &scroll_offsets, plan.chrome_metrics);
+ let world = callbacks.build_world_transforms(&tree);
+ let callbacks = callbacks.with_world_transforms(&world);
let pixel_ratio = plan.device_pixels_per_css_pixel as f32;
let point = CssPixelPoint::new(
CssPixels::nearest_value_for_f32(position.x / pixel_ratio),
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs b/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs
index c9339a7bc49..75c0ffdd985 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs
@@ -515,6 +515,8 @@ impl Lane {
&scroll_offsets,
plan.chrome_metrics(),
);
+ let world = callbacks.build_world_transforms(&tree);
+ let callbacks = callbacks.with_world_transforms(&world);
let pixel_ratio = plan.device_pixels_per_css_pixel() as f32;
let css_point =
|x, y| CssPixelPoint::new(CssPixels::nearest_value_for_f32(x), CssPixels::nearest_value_for_f32(y));