Skip to content

Truly independent web browser

LibWeb: Keep the hit-test list where a recording emits the same items

Zaggy1024b3776c009ac1
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 70c1b82f340..5aac32fb393 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -11684,7 +11684,7 @@ Painting::HitTestListAdoption Document::adopt_published_recording(Optional<Layou
if (!hit_test_list_read.has_value()) {
m_hit_test_display_list = nullptr;
adoption = Painting::HitTestListAdoption::Dropped;
- } else if (!recording_returned_the_paint_command_cache_source || !m_hit_test_display_list || !m_hit_test_display_list->is_current()) {
+ } else if (!m_hit_test_display_list || !m_hit_test_display_list->is_current()) {
m_hit_test_display_list = Painting::HitTestDisplayList::create_from_rust_recording(*hit_test_list_read, recording.visual_context_tree.structural_epoch(), layout_node_arena(), *m_chrome_widget_registry);
adoption = Painting::HitTestListAdoption::TookNew;
}
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
index 4cc52cd5b86..48cda4a13c4 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
@@ -179,6 +179,8 @@ pub fn rects_overlap_in_block_axis(a: CssPixelRect, b: CssPixelRect, writing_mod
#[derive(Default)]
pub struct HitTestList {
pub generation: u64,
+ /// The structural epoch of the visual context tree the items were recorded against, whose nodes they name.
+ pub recorded_structural_epoch: u64,
pub items: Arc<Vec<HitTestItem>>,
pub item_capacity_hint_from_previous_list: usize,
/// Point queries read only the spatial indexes, and caret navigation reads only the caret lines,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/publish.rs b/Libraries/LibWeb/Rust/src/painting/record/publish.rs
index 67315826575..134b34e81b0 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/publish.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/publish.rs
@@ -356,18 +356,33 @@ pub(crate) fn take_in_presented_output(
.as_ref()
.zip(recorder.published_hit_test_items.as_ref())
.is_some_and(|(list, source)| std::sync::Arc::ptr_eq(&list.items, &source.items));
- let hit_test_list_changed = !(output.is_identical_to_published_recording && previous_list_is_the_source);
+ // A recording that paints otherwise may still place the same items in the same contexts: the list kept stands,
+ // with the indexes built on it.
+ let items_stand = hit_test_list.as_ref().is_some_and(|previous| {
+ (output.is_identical_to_published_recording && previous_list_is_the_source)
+ || (previous.recorded_structural_epoch == output.recorded_structural_epoch
+ && previous.items.len() == list.items.len()
+ && *previous.items == *list.items)
+ });
+ let hit_test_list_changed = !items_stand;
if hit_test_list_changed {
- if publishes_recording {
+ *hit_test_list = Some(list);
+ }
+ if publishes_recording {
+ // A later recording copies its clean output from the published recording, with the published items: those of
+ // the list that stands with it.
+ let kept = hit_test_list.as_ref().expect("a list stands or was taken");
+ let published_items_stand = recorder
+ .published_hit_test_items
+ .as_ref()
+ .is_some_and(|source| std::sync::Arc::ptr_eq(&source.items, &kept.items));
+ if !published_items_stand {
recorder.published_hit_test_items =
Some(std::sync::Arc::new(crate::painting::record::PublishedHitTestItems {
- items: list.items.clone(),
+ items: kept.items.clone(),
structural_epoch: output.recorded_structural_epoch,
}));
}
- *hit_test_list = Some(list);
- }
- if publishes_recording {
recorder.published_recording = Some(output.clone());
}
take_in(output, hit_test_list_changed);
@@ -416,9 +431,12 @@ mod tests {
if read_write {
arena.note_publishing_paint_recording_started();
}
+ // Each recording is of another tree, so that it takes a new list.
let output = RecordingOutput {
+ recorded_structural_epoch: hit_test_generation,
hit_test_list: HitTestList {
generation: hit_test_generation,
+ recorded_structural_epoch: hit_test_generation,
..Default::default()
},
..Default::default()
@@ -456,4 +474,36 @@ mod tests {
}
}
}
+
+ #[test]
+ fn a_recording_that_emits_the_same_items_at_the_same_epoch_keeps_the_list() {
+ let mut arena = LayoutNodeArena::new();
+ let mut recorder = RecorderState::default();
+ let mut hit_test_list = None;
+ let mut first_items = None;
+ // Two recordings of one tree emit the same items: the first list stands, and the published items with it.
+ // A recording of another tree takes a new list.
+ for (structural_epoch, expected_generation) in [(7, 1), (7, 1), (8, 2)] {
+ arena.note_publishing_paint_recording_started();
+ let output = RecordingOutput {
+ recorded_structural_epoch: structural_epoch,
+ hit_test_list: HitTestList {
+ recorded_structural_epoch: structural_epoch,
+ ..Default::default()
+ },
+ ..Default::default()
+ };
+ let mut generation = 0;
+ take_in_published_output(&mut recorder, &mut hit_test_list, output, true, |output, changed| {
+ generation = take_in_recording(&mut arena, output, changed, true);
+ });
+ assert_eq!(generation, expected_generation);
+ let items = &hit_test_list.as_ref().unwrap().items;
+ let first_items = first_items.get_or_insert_with(|| items.clone());
+ 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/painting/record/traversal.rs b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
index 1aaefb0751f..30066d4dd71 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
@@ -175,6 +175,7 @@ fn record_display_list_impl<O: Observer>(
};
let mut hit_test_list = recorder.list;
hit_test_list.generation = paint_state.hit_test_list_generation + 1;
+ hit_test_list.recorded_structural_epoch = structural_epoch;
let output = RecordingOutput {
recorded_structural_epoch: structural_epoch,
frame_inputs,