Skip to content

Truly independent web browser

LibWeb: Hit test every clock lane by walking its rows

Aliaksandr Kalenikd2cc4e4623b1
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rs
index 1a7ca377478..8f9b05c0a1e 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/resolve.rs
@@ -56,17 +56,6 @@ pub(crate) fn resolve_hit(
result
}
-impl HitTestList {
- pub(crate) fn resolve_hit(
- &self,
- arena: &impl PaintRead,
- item_index: usize,
- local_point: CssPixelPoint,
- ) -> crate::painting::host::FfiResolvedHit {
- resolve_hit(arena, &self.items[item_index], local_point)
- }
-}
-
pub(crate) fn with_item_fragment<R>(
arena: &impl PaintRead,
item: &HitTestItem,
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs
index ac509f8cf61..5e93e82524f 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/walk.rs
@@ -16,13 +16,10 @@ use super::*;
use crate::layout::node_data::{NodeFlag, NodeSlotId};
use crate::painting::host::FfiHitTestQueryCallbacks;
use crate::painting::ink_overflow::{ScopeInkBound, scope_ink_bound};
-use crate::painting::paint_order_plan::{PaintOrderItem, PaintScope, PaintScopePlan};
+use crate::painting::paint_order_plan::{PaintOrderItem, PaintProducer, PaintScope, PaintScopePlan};
use crate::painting::paint_read::PaintRead;
use crate::painting::paintable_data::PaintableFlag;
use crate::painting::record::PaintPhase;
-use crate::painting::record::assemble::producer_kinds;
-use crate::painting::record::order_tree::ProducerKind;
-use crate::painting::record::producers::hit_phase;
use crate::painting::viewport_mapping::{ViewportMapping, ViewportSpace};
use crate::painting::visual_context::{
ClipBehavior, IncludeVisualViewportTransform, SpatialNodeIndex, VisualContextTree, WorldTransforms,
@@ -62,6 +59,32 @@ fn svg_children_painted_with_their_parent(rows: &impl PaintRead, svg_box: NodeSl
children
}
+/// What a producer of the paint order can be hit by: the items of one phase of its box, or an SVG box with the boxes
+/// painted with it.
+#[derive(Clone, Copy)]
+enum HitTestedPart {
+ Phase(PaintPhase),
+ SvgBox,
+}
+
+/// What `producer` can be hit by, in paint order.
+fn hit_tested_parts(producer: PaintProducer) -> SmallVec<[HitTestedPart; 2]> {
+ use HitTestedPart::*;
+ match producer {
+ PaintProducer::BoxPhase(phase @ (PaintPhase::Background | PaintPhase::Foreground | PaintPhase::Overlay)) => {
+ smallvec::smallvec![Phase(phase)]
+ }
+ PaintProducer::SvgRoot => smallvec::smallvec![Phase(PaintPhase::Background), SvgBox],
+ PaintProducer::SvgBoxForeground => smallvec::smallvec![SvgBox],
+ PaintProducer::ForegroundHitTest => smallvec::smallvec![Phase(PaintPhase::Foreground)],
+ PaintProducer::BoxPhase(_)
+ | PaintProducer::ForegroundCommands
+ | PaintProducer::InlinePiece(_)
+ | PaintProducer::TextFragment(_)
+ | PaintProducer::ScopePreamble => SmallVec::new(),
+ }
+}
+
// The scale of a space does not depend on where anything is scrolled to, so any table of the tree's spaces gives it.
fn own_accumulated_2d_scale(
rows: &impl PaintRead,
@@ -104,8 +127,8 @@ impl<R: PaintRead> WholeDocumentWalk<'_, R> {
for item in plan.items {
match item {
PaintOrderItem::Producer(producer) => {
- for kind in producer_kinds(producer) {
- self.producer(scope.owner, kind);
+ for part in hit_tested_parts(producer) {
+ self.part(scope.owner, part);
}
}
PaintOrderItem::Scope(child) => self.scope(child),
@@ -113,12 +136,11 @@ impl<R: PaintRead> WholeDocumentWalk<'_, R> {
}
}
- fn producer(&mut self, owner: NodeSlotId, kind: ProducerKind) {
+ fn part(&mut self, owner: NodeSlotId, part: HitTestedPart) {
let rows = self.rows;
- if let Some(phase) = hit_phase(kind) {
- hit_test_items_of_phase(rows, self, owner, phase);
- } else if kind == ProducerKind::Svg {
- self.svg_box(owner);
+ match part {
+ HitTestedPart::Phase(phase) => hit_test_items_of_phase(rows, self, owner, phase),
+ HitTestedPart::SvgBox => self.svg_box(owner),
}
}
@@ -319,10 +341,10 @@ impl<R: PaintRead> PointWalk<'_, R> {
}
for item in plan.items.iter().rev() {
let flow = match *item {
- PaintOrderItem::Producer(producer) => producer_kinds(producer)
+ PaintOrderItem::Producer(producer) => hit_tested_parts(producer)
.into_iter()
.rev()
- .map(|kind| self.producer(scope.owner, kind))
+ .map(|part| self.part(scope.owner, part))
.find(|flow| *flow == Flow::Stop)
.unwrap_or(Flow::Continue),
PaintOrderItem::Scope(child) => self.scope(child),
@@ -334,13 +356,10 @@ impl<R: PaintRead> PointWalk<'_, R> {
Flow::Continue
}
- fn producer(&mut self, owner: NodeSlotId, kind: ProducerKind) -> Flow {
- if let Some(phase) = hit_phase(kind) {
- self.items_of_phase(owner, phase)
- } else if kind == ProducerKind::Svg {
- self.svg_box(owner)
- } else {
- Flow::Continue
+ fn part(&mut self, owner: NodeSlotId, part: HitTestedPart) -> Flow {
+ match part {
+ HitTestedPart::Phase(phase) => self.items_of_phase(owner, phase),
+ HitTestedPart::SvgBox => self.svg_box(owner),
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
index 53e994c1b24..4e4d8b51e1b 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
@@ -83,11 +83,9 @@ pub(crate) struct RecordingResult {
pub(crate) resources: resources::RecordingResourceManifest,
}
-/// The hit-test items of the published recording, shared with the list that hit testing reads.
+/// The hit-test items of the published recording, which the next recording copies clean output from.
pub struct PublishedHitTestItems {
pub items: Arc<Vec<HitTestItem>>,
- /// The structural epoch of the visual context tree the items were recorded against, whose nodes they name.
- pub structural_epoch: u64,
}
// What a recording shares with hit testing and with later recordings.
diff --git a/Libraries/LibWeb/Rust/src/painting/record/publish.rs b/Libraries/LibWeb/Rust/src/painting/record/publish.rs
index 8314aa62425..6e512949fe6 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/publish.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/publish.rs
@@ -297,7 +297,6 @@ impl PresentedOutput {
pub(crate) fn published_hit_test_items(&self) -> std::sync::Arc<crate::painting::record::PublishedHitTestItems> {
std::sync::Arc::new(crate::painting::record::PublishedHitTestItems {
items: self.hit_test_list.items.clone(),
- structural_epoch: self.output.recorded_structural_epoch,
})
}
}
@@ -362,7 +361,6 @@ pub(crate) fn take_in_presented_output(
recorder.published_hit_test_items =
Some(std::sync::Arc::new(crate::painting::record::PublishedHitTestItems {
items: kept.items.clone(),
- structural_epoch: output.recorded_structural_epoch,
}));
}
recorder.published_recording = Some(output.clone());
@@ -532,7 +530,6 @@ mod tests {
assert_eq!(Arc::ptr_eq(first_items, items), structural_epoch == 7);
let published = recorder.published_hit_test_items.as_ref().unwrap();
assert!(Arc::ptr_eq(&published.items, items));
- assert_eq!(published.structural_epoch, structural_epoch);
}
}
}
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock.rs b/Libraries/LibWeb/Rust/src/render_state/clock.rs
index f2be61fbb6d..d6e8b7ee512 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock.rs
@@ -48,7 +48,7 @@ use crate::painting::record::damage::PaintDamage;
use crate::painting::record::inputs::UnframedRecordingInputs;
use crate::painting::record::publish::{published_vector_image_records, renders_vector_images, take_in_recording};
use crate::painting::record::recorder_state::RecorderState;
-use crate::painting::record::{PublishedHitTestItems, RecordingInputs, RecordingOutput};
+use crate::painting::record::{RecordingInputs, RecordingOutput};
use crate::painting::recording_slot::{
FrameInputs, FrozenFrame, RecordingAnswer, RecordingJob, freeze_recording_frame, present,
present_with_vector_images, record_frame, wait_while_recording_is_held_for_testing,
@@ -357,7 +357,7 @@ pub(crate) struct Lane {
/// the tick shows again.
samples_restored: bool,
/// The list a drag selection finds its caret positions in, for the frame whose items it holds.
- selection_hit_test_list: Option<crate::painting::hit_test::HitTestList>,
+ selection_hit_test_list: Option<crate::painting::hit_test::walk::WholeDocumentHitTestList>,
/// What a drag selection owes the rows of the fork of `::selection` styles, once a tick asked.
selection_styles: Option<crate::css::style::drag_selection::SelectionStyles>,
/// Where the highlight the fork holds ends, if a tick moved it.
@@ -453,9 +453,9 @@ pub(crate) struct ClockRecorder {
pub(super) recorder: RecorderState,
pub(super) presentation: Presentation,
pub(super) presented: TickPresented,
- /// The hit-test items of the frame a tick presented last. The recorder's stay those of the lane's frame, which every
- /// tick's frame copies clean output from.
- pub(super) presented_hit_test_items: Option<Arc<PublishedHitTestItems>>,
+ /// The structural epoch of the visual context tree of the frame on screen: the frame a tick presented last, or else
+ /// the lane's own. None once the lane left a frame to the host.
+ pub(super) structural_epoch_on_screen: Option<u64>,
}
/// What the recording of a tick's frame presented.
@@ -1866,7 +1866,7 @@ impl ClockRecorder {
),
None => present(&mut self.presentation, pending, &self.recorder, presenting),
};
- self.presented_hit_test_items = Some(output.published_hit_test_items());
+ self.structural_epoch_on_screen = Some(output.output.recorded_structural_epoch);
self.presented = TickPresented::Frame {
output: output.output,
hit_test_list_changed: true,
@@ -1875,10 +1875,10 @@ impl ClockRecorder {
self
}
- /// Leaves the frame to the host, which a lane that presents no other frame finds no more items to hit test.
+ /// Leaves the frame to the host, whose frame on screen a lane that presents no other frame cannot hit test.
fn leave_frame_to_host(&mut self) {
self.recorder.published_hit_test_items = None;
- self.presented_hit_test_items = None;
+ self.structural_epoch_on_screen = None;
self.presented = TickPresented::LeftToHost;
}
}
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs
index 0f9d46b9edc..1e7dd00e318 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock/hover.rs
@@ -22,10 +22,9 @@ use crate::css::style_compute::FfiEffectTiming;
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::layout::tree_mutation::{HostCalls, OwedHostWork};
use crate::painting::ffi::FfiChromeMetrics;
+use crate::painting::hit_test::HitTestItemKind;
use crate::painting::hit_test::walk::{PointQuery, topmost_hit};
-use crate::painting::hit_test::{HitTestItemKind, HitTestList};
use crate::painting::host::FfiHitTestQueryCallbacks;
-use crate::painting::record::PublishedHitTestItems;
use crate::painting::visual_context::VisualContextTree;
use crate::stage_thread::Riding;
use libgfx_rust::FloatPoint;
@@ -1322,9 +1321,7 @@ impl Lane {
}
let frame = plan.read_only_frame.as_ref()?;
let tree = frame.visual_context.tree.as_ref()?;
- let published = self.published_hit_test_items();
- let published = published?;
- if published.structural_epoch != tree.structural_epoch {
+ if self.structural_epoch_on_screen() != Some(tree.structural_epoch) {
return None;
}
// The screen shows the compositor's animations sampled at the frame's time, where the hit test finds them.
@@ -1451,30 +1448,27 @@ impl Lane {
}
}
- /// The hit-test items of the frame the lane presented last, which come back with its recording.
- fn published_hit_test_items(&mut self) -> Option<Arc<PublishedHitTestItems>> {
+ /// The structural epoch of the visual context tree of the frame on screen, which comes back with the lane's
+ /// recording: the frame a tick presented last, or else the lane's own. None once the lane left a frame to the host.
+ fn structural_epoch_on_screen(&mut self) -> Option<u64> {
let recorder = self.recording.0.take(WaitsForTickRecording(()));
- let presented = recorder
- .presented_hit_test_items
- .clone()
- .or_else(|| recorder.recorder.published_hit_test_items.clone());
+ let structural_epoch = recorder.structural_epoch_on_screen;
self.recording = TickRecording(Riding::landed(recorder));
- presented
+ structural_epoch
}
- /// The hit-test items of the frame the lane presented last, with the tree of visual contexts of `state` they name as
- /// the screen shows them at `frame_time_nanoseconds`, or why a hit test cannot read them.
- pub(super) fn presented_hit_test_items(
+ /// The tree of visual contexts of `state` as the screen shows it at `frame_time_nanoseconds`, where the frame on
+ /// screen was recorded against it, or why a hit test cannot place what the screen shows.
+ pub(super) fn tree_on_screen(
&mut self,
state: &RenderState,
frame_time_nanoseconds: i64,
- ) -> Result<(Arc<VisualContextTree>, Arc<PublishedHitTestItems>), &'static str> {
- let published = self.published_hit_test_items().ok_or("no hit-test list")?;
+ ) -> Result<Arc<VisualContextTree>, &'static str> {
+ let structural_epoch_on_screen = self.structural_epoch_on_screen().ok_or("no frame on screen")?;
let visual_context = &state.arena.arena().paint_state().visual_context;
let tree = visual_context.tree.clone().ok_or("no visual context tree")?;
- // The items name the visual contexts of the tree they were recorded against, which a hit test of the host's may
- // have built anew since without presenting it.
- if published.structural_epoch != tree.structural_epoch {
+ // A hit test of the host's may have built the tree anew since without presenting it.
+ if structural_epoch_on_screen != tree.structural_epoch {
return Err("visual contexts the frame does not show");
}
// The screen shows the compositor's animations sampled at the frame's time, where the hit test finds them.
@@ -1482,7 +1476,7 @@ impl Lane {
true => Arc::new(tree.with_visual_animation_samples(frame_time_nanoseconds)),
false => tree,
};
- Ok((tree, published))
+ Ok(tree)
}
/// Hit tests `position`, in device pixels, in the frame the lane presented last.
@@ -1493,15 +1487,12 @@ impl Lane {
position: FloatPoint,
frame_time_nanoseconds: i64,
) -> Result<HoverTarget, HoverDeclined> {
- let (tree, published) = self
- .presented_hit_test_items(state, frame_time_nanoseconds)
+ let tree = self
+ .tree_on_screen(state, frame_time_nanoseconds)
.map_err(HoverDeclined::Unplaced)?;
+ // The walk skips the boxes whose ink overflow the pointer misses.
+ state.arena.arena_mut().settle_ink_overflow();
let arena = state.arena.arena();
- let mut list = HitTestList {
- items: std::sync::Arc::clone(&published.items),
- ..HitTestList::default()
- };
- list.build_spatial_indexes_if_needed();
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);
@@ -1512,10 +1503,19 @@ impl Lane {
CssPixels::nearest_value_for_f32(position.x / pixel_ratio),
CssPixels::nearest_value_for_f32(position.y / pixel_ratio),
);
- let Some(topmost) = list.find_topmost_item(arena, &tree, &callbacks, point) else {
+ let Some(topmost) = topmost_hit(
+ arena,
+ PointQuery {
+ tree: &tree,
+ callbacks: &callbacks,
+ viewport: arena.layout_root(),
+ paint_overlay: FRAMES_PAINT_OVERLAYS,
+ point,
+ },
+ ) else {
return Ok(HoverTarget::Unchanged);
};
- let item = &list.items[topmost.index];
+ let item = &topmost.item;
// A scrollbar or a resizer hovers nothing the host would hover without asking it.
if item.kind == HitTestItemKind::ChromeWidget {
return Err(HoverDeclined::Unmoved("a scrollbar or resizer"));
@@ -1549,7 +1549,7 @@ impl Lane {
if paintable_kind == NodeKind::ImageBox && arena.image_map_areas().has_areas(paintable) {
return Err(HoverDeclined::Unmoved("an image map"));
}
- let resolved = list.resolve_hit(arena, topmost.index, topmost.local_point);
+ let resolved = crate::painting::hit_test::resolve::resolve_hit(arena, item, topmost.local_point);
let identity = if paintable_kind == NodeKind::Viewport {
// The viewport stands for the document, and a hit that falls through to it hits the root element, the root
// of the style updates the hover takes its own with. The cursor is the viewport's, as the host's is.
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs b/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs
index 8609eb5306c..ce2d4acb49f 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock/lane.rs
@@ -175,11 +175,15 @@ impl LaneDelivery {
},
None => TickPresented::Nothing,
};
+ let structural_epoch_on_screen = recorder
+ .published_recording
+ .as_ref()
+ .map(|published| published.recorded_structural_epoch);
let recorder = ClockRecorder {
recorder,
presentation,
presented,
- presented_hit_test_items: None,
+ structural_epoch_on_screen,
};
let (ticks, frame) = (Arc::clone(&self.ticks), self.frame);
super::super::post_to_render_side(move || {
diff --git a/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs b/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs
index 41658beea4d..505c7a9a20a 100644
--- a/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs
+++ b/Libraries/LibWeb/Rust/src/render_state/clock/selection.rs
@@ -26,8 +26,8 @@ use crate::css::style::tree::StyleNodeID;
use crate::layout::LayoutNodeArena;
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::layout::svg_formatting_context::FfiFloatRect;
-use crate::painting::hit_test::HitTestList;
use crate::painting::hit_test::resolve::row_dom_style_node;
+use crate::painting::hit_test::walk::WholeDocumentHitTestList;
use crate::painting::host::{FfiCaretBoundaryKind, FfiHitTestQueryCallbacks, FfiResolvedCaret};
use crate::painting::paint_read::GeometryRead;
use crate::painting::paintable_data::{
@@ -488,26 +488,16 @@ impl Lane {
if pointer.scrolled_since_frame && (self.scroll_offsets.is_empty() || drag.scrollport.is_some()) {
return Err(LeftToHost("scrolled since the frame"));
}
- let (tree, published) = self
- .presented_hit_test_items(state, frame_time_nanoseconds)
- .map_err(LeftToHost)?;
+ let tree = self.tree_on_screen(state, frame_time_nanoseconds).map_err(LeftToHost)?;
let arena = state.arena.arena();
let engine = state.engine_ref();
- // NB: The spatial indexes take a walk over every item, which a drag need not repeat over the same frame.
- let list = self
- .selection_hit_test_list
- .take()
- .filter(|list| Arc::ptr_eq(&list.items, &published.items))
- .unwrap_or_else(|| {
- let mut list = HitTestList {
- items: Arc::clone(&published.items),
- ..HitTestList::default()
- };
- list.build_spatial_indexes_if_needed();
- list
- });
let scroll_offsets = self.scroll_offsets_on_screen(arena, &arena.paint_state().visual_context, plan);
- let list = self.selection_hit_test_list.insert(list);
+ // NB: The list walks the rows and its spatial indexes walk every item, which a drag need not repeat over the
+ // same frame.
+ let list =
+ WholeDocumentHitTestList::current(&mut self.selection_hit_test_list, arena, &tree, arena.layout_root());
+ list.build_spatial_indexes_if_needed();
+ let list = &*list;
let callbacks = FfiHitTestQueryCallbacks::sealed(
plan.device_pixels_per_css_pixel(),
&scroll_offsets,