Skip to content

Truly independent web browser

LibCompositing: Build async scrolling state from the tree's scroll nodes

Aliaksandr Kalenik2f1aac13c06e
diff --git a/Libraries/LibCompositing/Rust/src/ffi.rs b/Libraries/LibCompositing/Rust/src/ffi.rs
index b627ae00c1c..dc29ef2a8cf 100644
--- a/Libraries/LibCompositing/Rust/src/ffi.rs
+++ b/Libraries/LibCompositing/Rust/src/ffi.rs
@@ -459,20 +459,26 @@ const ENCODED_IN_THIS_PROCESS: &str = "a value this process encoded for itself d
///
/// # Safety
///
-/// The storage of `display_list` must be a live display list storage handle. `receive` must copy the UTF-8 bytes it
-/// keeps synchronously.
+/// The storage of `display_list` must be a live display list storage handle, and `visual_context_tree` a live tree
+/// handle where there is a display list. `receive` must copy the UTF-8 bytes it keeps synchronously.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn recorded_display_list_async_scrolling_state_json(
display_list: Option<&FfiDisplayList<'_>>,
+ visual_context_tree: *const c_void,
receiver: *mut c_void,
receive: unsafe extern "C" fn(*mut c_void, ak::Span<'_, u8>),
) {
let state = match display_list {
None => crate::scrolling::AsyncScrollingState::default(),
Some(display_list) => {
- // SAFETY: The caller lends a live storage handle.
- let display_list = unsafe { display_list.share() }.expect(ENCODED_IN_THIS_PROCESS);
- crate::scrolling::async_scrolling_state_from_display_list(&display_list)
+ // SAFETY: The caller lends live storage and tree handles.
+ let (display_list, visual_context_tree) = unsafe {
+ (
+ display_list.share().expect(ENCODED_IN_THIS_PROCESS),
+ tree_sharing_handle(visual_context_tree),
+ )
+ };
+ crate::scrolling::async_scrolling_state_from(&display_list, visual_context_tree.as_arc())
}
};
let json = crate::scrolling::async_scrolling_state_to_json(&state);
@@ -507,7 +513,8 @@ unsafe fn recorded_scroll_tree_of(
.decode::<crate::scrolling::ScrollStateSnapshot>()
.expect(ENCODED_IN_THIS_PROCESS);
snapshot.set_node_count(visual_context_tree.spatial_node_count());
- let async_scrolling_state = crate::scrolling::async_scrolling_state_from_display_list(&display_list);
+ let async_scrolling_state =
+ crate::scrolling::async_scrolling_state_from(&display_list, visual_context_tree.as_arc());
let admission = crate::scrolling::wheel_routing_admission_for_with_generation(
&async_scrolling_state,
newest_wheel_event_listener_state_generation,
diff --git a/Libraries/LibCompositing/Rust/src/scrolling/async_scrolling_state.rs b/Libraries/LibCompositing/Rust/src/scrolling/async_scrolling_state.rs
index 0a3a8d63f29..05670b55574 100644
--- a/Libraries/LibCompositing/Rust/src/scrolling/async_scrolling_state.rs
+++ b/Libraries/LibCompositing/Rust/src/scrolling/async_scrolling_state.rs
@@ -15,6 +15,10 @@ use super::{
use crate::css_pixels::{CssPixelPoint, CssPixelRect};
use crate::display_list::builder::read_command;
use crate::display_list::commands::*;
+use crate::visual_context::{
+ CompositorScrollNodeRecord, CompositorSnapAreaRecord, CompositorSnapContainerRecord, VISUAL_VIEWPORT_NODE_INDEX,
+ VisualContextTree,
+};
use libgfx_rust::{Color, CornerRadii, FloatPoint, FloatRect, IntRect};
use std::io::Write;
@@ -230,7 +234,84 @@ fn stable_scroll_node_id_for(
}
}
-pub fn async_scrolling_state_from_display_list(display_list: &DisplayList) -> AsyncScrollingState {
+fn async_scroll_node_from(node: &CompositorScrollNodeRecord) -> AsyncScrollNode {
+ let parent_scroll_node_index =
+ (node.parent_scroll_node_index != VISUAL_VIEWPORT_NODE_INDEX).then_some(node.parent_scroll_node_index);
+ AsyncScrollNode {
+ scroll_node_index: node.scroll_node_index,
+ stable_node_id: stable_scroll_node_id_for(
+ node.scrollable_node_id,
+ node.scroll_node_kind,
+ node.pseudo_element_type,
+ ),
+ parent_scroll_node_index,
+ scrollport_rect: node.scrollport_rect,
+ min_scroll_offset: node.min_scroll_offset,
+ max_scroll_offset: node.max_scroll_offset,
+ is_viewport: node.is_viewport,
+ can_be_wheel_scrolled_horizontally: node.can_be_wheel_scrolled_horizontally,
+ can_be_wheel_scrolled_vertically: node.can_be_wheel_scrolled_vertically,
+ chains_scroll_to_ancestors_horizontally: node.chains_scroll_to_ancestors_horizontally,
+ chains_scroll_to_ancestors_vertically: node.chains_scroll_to_ancestors_vertically,
+ shows_overscroll_effect_horizontally: node.shows_overscroll_effect_horizontally,
+ shows_overscroll_effect_vertically: node.shows_overscroll_effect_vertically,
+ }
+}
+
+fn snap_strictness_from(value: u8) -> Option<SnapStrictness> {
+ match value {
+ 0 => Some(SnapStrictness::None),
+ 1 => Some(SnapStrictness::Proximity),
+ 2 => Some(SnapStrictness::Mandatory),
+ _ => None,
+ }
+}
+
+fn snap_align_from(value: u8) -> Option<SnapAlign> {
+ match value {
+ 0 => Some(SnapAlign::None),
+ 1 => Some(SnapAlign::Start),
+ 2 => Some(SnapAlign::End),
+ 3 => Some(SnapAlign::Center),
+ _ => None,
+ }
+}
+
+fn async_snap_container_from(container: &CompositorSnapContainerRecord) -> Option<AsyncSnapContainer> {
+ Some(AsyncSnapContainer {
+ scroll_node_index: container.scroll_node_index,
+ geometry: SnapContainerGeometry {
+ snapport: container.snapport.into(),
+ min_scroll_offset: container.min_scroll_offset.into(),
+ max_scroll_offset: container.max_scroll_offset.into(),
+ strictness: snap_strictness_from(container.strictness)?,
+ axes: SnapAxes {
+ x: container.snaps_x,
+ y: container.snaps_y,
+ },
+ horizontal_writing_mode: container.horizontal_writing_mode,
+ },
+ areas: Vec::new(),
+ })
+}
+
+fn snap_area_geometry_from(area: &CompositorSnapAreaRecord) -> Option<SnapAreaGeometry> {
+ Some(SnapAreaGeometry {
+ identity: SnapAreaIdentity {
+ node_id: area.area_node_id,
+ pseudo_element_type: area.pseudo_element_type,
+ },
+ rect: area.rect.into(),
+ align_x: snap_align_from(area.align_x)?,
+ align_y: snap_align_from(area.align_y)?,
+ always_stop: area.always_stop,
+ })
+}
+
+pub fn async_scrolling_state_from(
+ display_list: &DisplayList,
+ visual_context_tree: &VisualContextTree,
+) -> AsyncScrollingState {
let mut async_scrolling_state = AsyncScrollingState::default();
let mut next_paint_order_index = 0u32;
if let Some(metadata) = display_list.async_scrolling_metadata() {
@@ -244,6 +325,25 @@ pub fn async_scrolling_state_from_display_list(display_list: &DisplayList) -> As
async_scrolling_state.viewport_keeps_vertical_scrolls = metadata.viewport_keeps_vertical_scrolls;
async_scrolling_state.device_pixels_per_css_pixel = metadata.device_pixels_per_css_pixel;
}
+ let scroll_table = visual_context_tree.compositor_scroll_table();
+ // Fall back to commands until WebContent fills the tree's compositor scroll table.
+ let use_scroll_table = !scroll_table.nodes.is_empty();
+ if use_scroll_table {
+ async_scrolling_state
+ .scroll_nodes
+ .extend(scroll_table.nodes.iter().map(async_scroll_node_from));
+ for container in scroll_table.snap_containers.iter() {
+ let Some(mut snap_container) = async_snap_container_from(container) else {
+ continue;
+ };
+ let first_area_index = container.first_area_index as usize;
+ let areas = &scroll_table.snap_areas[first_area_index..first_area_index + container.area_count as usize];
+ snap_container
+ .areas
+ .extend(areas.iter().filter_map(snap_area_geometry_from));
+ async_scrolling_state.snap_containers.push(snap_container);
+ }
+ }
display_list.for_each_compositor_metadata(|context, command_type, payload| {
let mut append_wheel_hit_test_target = |target_scroll_node_index: SpatialNodeIndex, rect, corner_radii| {
let target_scroll_node_index = if target_scroll_node_index.0 != 0 {
@@ -272,6 +372,9 @@ pub fn async_scrolling_state_from_display_list(display_list: &DisplayList) -> As
});
}
DisplayListCommandType::CompositorScrollNode => {
+ if use_scroll_table {
+ return;
+ }
let command = read_command::<CompositorScrollNode>(payload);
let parent_scroll_node_index = if command.parent_scroll_node_index.0 != 0 {
Some(command.parent_scroll_node_index)
@@ -353,12 +456,12 @@ pub fn async_scrolling_state_from_display_list(display_list: &DisplayList) -> As
next_paint_order_index = next_paint_order_index.wrapping_add(1);
}
DisplayListCommandType::CompositorSnapContainer => {
+ if use_scroll_table {
+ return;
+ }
let command = read_command::<CompositorSnapContainer>(payload);
- let strictness = match command.strictness {
- 0 => SnapStrictness::None,
- 1 => SnapStrictness::Proximity,
- 2 => SnapStrictness::Mandatory,
- _ => return,
+ let Some(strictness) = snap_strictness_from(command.strictness) else {
+ return;
};
async_scrolling_state.snap_containers.push(AsyncSnapContainer {
scroll_node_index: command.scroll_node_index,
@@ -377,6 +480,9 @@ pub fn async_scrolling_state_from_display_list(display_list: &DisplayList) -> As
});
}
DisplayListCommandType::CompositorSnapArea => {
+ if use_scroll_table {
+ return;
+ }
let command = read_command::<CompositorSnapArea>(payload);
let snap_containers = &mut async_scrolling_state.snap_containers;
// A snap container's areas are recorded right after it.
@@ -390,14 +496,9 @@ pub fn async_scrolling_state_from_display_list(display_list: &DisplayList) -> As
);
return;
};
- let align = |value| match value {
- 0 => Some(SnapAlign::None),
- 1 => Some(SnapAlign::Start),
- 2 => Some(SnapAlign::End),
- 3 => Some(SnapAlign::Center),
- _ => None,
- };
- let (Some(align_x), Some(align_y)) = (align(command.align_x), align(command.align_y)) else {
+ let (Some(align_x), Some(align_y)) =
+ (snap_align_from(command.align_x), snap_align_from(command.align_y))
+ else {
return;
};
container.areas.push(SnapAreaGeometry {
diff --git a/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.cpp b/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.cpp
index cddf561707d..a8633b2f972 100644
--- a/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.cpp
+++ b/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.cpp
@@ -14,19 +14,24 @@ namespace Compositing {
using EncodedAsyncScrollingMetadata = IPC::Encoded<Optional<DisplayList::AsyncScrollingMetadata>>;
-String async_scrolling_state_as_json(DisplayList const* display_list)
+static void receive_json(void* receiver, ReadonlyBytes bytes)
+{
+ *static_cast<String*>(receiver) = MUST(String::from_utf8(bytes));
+}
+
+String async_scrolling_state_as_json(DisplayList const& display_list, AccumulatedVisualContextTree const& visual_context_tree)
{
String json;
- auto receive_json = [](void* receiver, ReadonlyBytes bytes) {
- *static_cast<String*>(receiver) = MUST(String::from_utf8(bytes));
- };
- if (!display_list) {
- RustFFI::recorded_display_list_async_scrolling_state_json(nullptr, &json, receive_json);
- return json;
- }
- EncodedAsyncScrollingMetadata async_scrolling_metadata { display_list->async_scrolling_metadata() };
- auto display_list_for_scroll_logic = display_list->for_scroll_logic(async_scrolling_metadata.bytes());
- RustFFI::recorded_display_list_async_scrolling_state_json(&display_list_for_scroll_logic, &json, receive_json);
+ EncodedAsyncScrollingMetadata async_scrolling_metadata { display_list.async_scrolling_metadata() };
+ auto display_list_for_scroll_logic = display_list.for_scroll_logic(async_scrolling_metadata.bytes());
+ RustFFI::recorded_display_list_async_scrolling_state_json(&display_list_for_scroll_logic, visual_context_tree.rust_handle(), &json, receive_json);
+ return json;
+}
+
+String empty_async_scrolling_state_as_json()
+{
+ String json;
+ RustFFI::recorded_display_list_async_scrolling_state_json(nullptr, nullptr, &json, receive_json);
return json;
}
diff --git a/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.h b/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.h
index 6a110aa530f..38a67e350de 100644
--- a/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.h
+++ b/Libraries/LibCompositing/Scrolling/RecordedDisplayListScrolling.h
@@ -14,7 +14,8 @@
namespace Compositing {
-COMPOSITING_API String async_scrolling_state_as_json(DisplayList const*);
+COMPOSITING_API String async_scrolling_state_as_json(DisplayList const&, AccumulatedVisualContextTree const&);
+COMPOSITING_API String empty_async_scrolling_state_as_json();
// The scroll state is the document's, as the scroll logic, which is Rust, encoded it.
COMPOSITING_API FirstWheelStepRouteForTesting route_first_wheel_step_through_recorded_display_list(DisplayList const&, AccumulatedVisualContextTree const&, ReadonlyBytes encoded_scroll_state, u64 newest_wheel_event_listener_state_generation, Gfx::FloatPoint position, Gfx::FloatPoint delta_in_css_pixels);
COMPOSITING_API bool recorded_display_list_blocks_wheel_event_at(DisplayList const&, AccumulatedVisualContextTree const&, ReadonlyBytes encoded_scroll_state, Gfx::FloatPoint position);
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 4fd6459f061..1afd36007c9 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -1090,7 +1090,7 @@ GC::Ref<JS::Object> Internals::compositor_scroll_state(GC::Ptr<DOM::Element> scr
scroll_state = navigable->compositor_context().scroll_state_for_testing(stretched_scroller);
auto report = JsonValue::from_string(scroll_state);
if (report.is_error() || !report.value().is_object())
- report = JsonValue::from_string(Compositing::async_scrolling_state_as_json(nullptr));
+ report = JsonValue::from_string(Compositing::empty_async_scrolling_state_as_json());
return scrolling_state_report_to_object(window(), report.value().as_object());
}
@@ -4051,8 +4051,13 @@ GC::Ref<JS::Object> Internals::style_invalidation_counters_object() const
GC::Ref<JS::Object> Internals::recorded_async_scrolling_state_object()
{
- auto* display_list = window().associated_document().paint_state().display_list_used_as_paint_command_cache_source();
- auto report = MUST(JsonValue::from_string(Compositing::async_scrolling_state_as_json(display_list)));
+ auto& document = window().associated_document();
+ Layout::ForcedReadScope read { document };
+ auto* display_list = document.paint_state().display_list_used_as_paint_command_cache_source();
+ auto json = display_list
+ ? Compositing::async_scrolling_state_as_json(*display_list, document.paint_state().visual_context_tree_without_update(read, document))
+ : Compositing::empty_async_scrolling_state_as_json();
+ auto report = MUST(JsonValue::from_string(json));
return scrolling_state_report_to_object(window(), report.as_object());
}
diff --git a/Services/Compositor/Rust/src/context_state/frames_and_viewport.rs b/Services/Compositor/Rust/src/context_state/frames_and_viewport.rs
index 155a062f2ac..38e0f20da92 100644
--- a/Services/Compositor/Rust/src/context_state/frames_and_viewport.rs
+++ b/Services/Compositor/Rust/src/context_state/frames_and_viewport.rs
@@ -147,12 +147,13 @@ impl ContextState {
// The installed scrolling state, its routing admission, and how far the given scroller is stretched, as JSON.
pub fn scroll_state_for_testing(&self, scroller: AsyncScrollNodeStableID) -> String {
- let display_list = self.scene.display_list();
- let mut scroll_state = JsonWriter(async_scrolling_state_to_json(
- &display_list.as_ref().map_or_else(AsyncScrollingState::default, |list| {
- async_scrolling_state_from_display_list(list)
- }),
- ));
+ let scroll_state = match (self.scene.display_list(), self.scene.visual_context_tree()) {
+ (Some(display_list), Some(visual_context_tree)) => {
+ async_scrolling_state_from(display_list, visual_context_tree.as_arc())
+ }
+ _ => AsyncScrollingState::default(),
+ };
+ let mut scroll_state = JsonWriter(async_scrolling_state_to_json(&scroll_state));
scroll_state.0.pop();
let admission = self.wheel_gesture_routing.admission();
let admission_name = if admission == WheelRoutingAdmission::Accepted {
diff --git a/Services/Compositor/Rust/src/context_state/mod.rs b/Services/Compositor/Rust/src/context_state/mod.rs
index 9895018131b..65136ace74a 100644
--- a/Services/Compositor/Rust/src/context_state/mod.rs
+++ b/Services/Compositor/Rust/src/context_state/mod.rs
@@ -727,7 +727,7 @@ impl ContextState {
display_list.compatible_visual_context_tree_structural_epoch(),
visual_context_tree.structural_epoch()
);
- let async_scrolling_state = async_scrolling_state_from_display_list(&display_list);
+ let async_scrolling_state = async_scrolling_state_from(&display_list, visual_context_tree.as_arc());
if !scroll_state_snapshot.has_only_finite_offsets()
|| !async_scrolling_state.has_only_numbers_scrolls_can_be_calculated_with()
{