Skip to content

Truly independent web browser

Compositor: Adopt scroll nodes when only the visual context tree changes

Aliaksandr Kalenik179c88390252
diff --git a/Libraries/LibCompositing/Rust/src/scrolling/async_scroll_tree.rs b/Libraries/LibCompositing/Rust/src/scrolling/async_scroll_tree.rs
index 6b58e7a61f1..548694c8cef 100644
--- a/Libraries/LibCompositing/Rust/src/scrolling/async_scroll_tree.rs
+++ b/Libraries/LibCompositing/Rust/src/scrolling/async_scroll_tree.rs
@@ -116,48 +116,15 @@ pub enum ScrollChaining {
None,
}
+#[derive(Default)]
pub struct AsyncScrollTree {
- document_id: Option<UniqueNodeId>,
- scroll_nodes: Vec<AsyncScrollNode>,
- snap_containers: Vec<AsyncSnapContainer>,
- device_pixels_per_css_pixel: f64,
- wheel_hit_test_regions: Vec<WheelHitTestTarget>,
- main_thread_wheel_event_regions: Vec<MainThreadWheelEventRegion>,
- nested_context_wheel_regions: Vec<NestedContextWheelRegion>,
+ state: AsyncScrollingState,
cached_wheel_hit_test_targets: Vec<CachedWheelHitTestTarget>,
- blocking_wheel_event_regions: Vec<BlockingWheelEventRegion>,
cached_main_thread_wheel_event_targets: Vec<CachedMainThreadWheelEventTarget>,
cached_nested_context_wheel_regions: Vec<CachedNestedContextWheelRegion>,
cached_blocking_wheel_event_targets: Vec<CachedBlockingWheelEventTarget>,
visual_context_tree_structural_epoch: Option<u64>,
scroll_state_snapshot: ScrollStateSnapshot,
- has_blocking_wheel_event_region_covering_viewport: bool,
- viewport_keeps_horizontal_scrolls: bool,
- viewport_keeps_vertical_scrolls: bool,
-}
-
-impl Default for AsyncScrollTree {
- fn default() -> Self {
- Self {
- document_id: None,
- scroll_nodes: Vec::new(),
- snap_containers: Vec::new(),
- device_pixels_per_css_pixel: 1.0,
- wheel_hit_test_regions: Vec::new(),
- main_thread_wheel_event_regions: Vec::new(),
- nested_context_wheel_regions: Vec::new(),
- cached_wheel_hit_test_targets: Vec::new(),
- blocking_wheel_event_regions: Vec::new(),
- cached_main_thread_wheel_event_targets: Vec::new(),
- cached_nested_context_wheel_regions: Vec::new(),
- cached_blocking_wheel_event_targets: Vec::new(),
- visual_context_tree_structural_epoch: None,
- scroll_state_snapshot: ScrollStateSnapshot::default(),
- has_blocking_wheel_event_region_covering_viewport: false,
- viewport_keeps_horizontal_scrolls: false,
- viewport_keeps_vertical_scrolls: false,
- }
- }
}
struct NestedContextWheelRegionHit<'a> {
@@ -167,43 +134,36 @@ struct NestedContextWheelRegionHit<'a> {
impl AsyncScrollTree {
pub fn document_id(&self) -> Option<UniqueNodeId> {
- self.document_id
+ self.state.document_id
}
pub fn scroll_nodes(&self) -> &[AsyncScrollNode] {
- &self.scroll_nodes
+ &self.state.scroll_nodes
}
// Scroll offsets are held in the device pixels of the display list; snap geometry is in the CSS pixels it was
// recorded from, at the scale the display list carries.
pub fn device_pixels_per_css_pixel(&self) -> f64 {
- self.device_pixels_per_css_pixel
+ self.state.device_pixels_per_css_pixel
}
pub fn has_nested_contexts(&self) -> bool {
- !self.nested_context_wheel_regions.is_empty()
+ !self.state.nested_context_wheel_regions.is_empty()
}
pub fn has_wheel_hit_test_targets_for(&self, visual_context_tree: &AccumulatedVisualContextTree) -> bool {
self.visual_context_tree_structural_epoch == Some(visual_context_tree.structural_epoch())
}
pub fn set_state(&mut self, state: AsyncScrollingState) {
- self.document_id = state.document_id;
- self.scroll_nodes = state.scroll_nodes;
- self.snap_containers = state.snap_containers;
- self.device_pixels_per_css_pixel = state.device_pixels_per_css_pixel;
- self.wheel_hit_test_regions = state.wheel_hit_test_targets;
- self.main_thread_wheel_event_regions = state.main_thread_wheel_event_regions;
- self.nested_context_wheel_regions = state.nested_context_wheel_regions;
- self.blocking_wheel_event_regions = state.blocking_wheel_event_regions;
- self.has_blocking_wheel_event_region_covering_viewport =
- state.has_blocking_wheel_event_region_covering_viewport;
- self.viewport_keeps_horizontal_scrolls = state.viewport_keeps_horizontal_scrolls;
- self.viewport_keeps_vertical_scrolls = state.viewport_keeps_vertical_scrolls;
+ self.state = state;
self.cached_wheel_hit_test_targets.clear();
self.cached_main_thread_wheel_event_targets.clear();
self.cached_nested_context_wheel_regions.clear();
self.cached_blocking_wheel_event_targets.clear();
self.visual_context_tree_structural_epoch = None;
}
+
+ pub fn state(&self) -> &AsyncScrollingState {
+ &self.state
+ }
}
// A first step of a wheel gesture is latched to the scroller the hit test found, and is otherwise left to the page for
@@ -222,7 +182,8 @@ pub fn input_routing_decision_for_first_wheel_step(outcome: WheelHitTestOutcome)
impl AsyncScrollTree {
pub fn scroll_node_for_index(&self, scroll_node_index: SpatialNodeIndex) -> Option<&AsyncScrollNode> {
- self.scroll_nodes
+ self.state
+ .scroll_nodes
.iter()
.find(|node| node.scroll_node_index == scroll_node_index)
}
@@ -303,7 +264,7 @@ impl AsyncScrollTree {
) -> Option<SpatialNodeIndex> {
let mut outermost_scroll_node_index = None;
let mut scroll_node_index = innermost_scroll_node_index;
- for _ in 0..self.scroll_nodes.len() {
+ for _ in 0..self.state.scroll_nodes.len() {
let Some(node) = self.scroll_node_for_index(scroll_node_index) else {
break;
};
@@ -326,13 +287,15 @@ impl AsyncScrollTree {
}
pub fn snap_container_for_node(&self, scroll_node_index: SpatialNodeIndex) -> Option<&AsyncSnapContainer> {
- self.snap_containers
+ self.state
+ .snap_containers
.iter()
.find(|snap_container| snap_container.scroll_node_index == scroll_node_index)
}
fn scroll_node_for_stable_id(&self, stable_node_id: AsyncScrollNodeStableID) -> Option<&AsyncScrollNode> {
- self.scroll_nodes
+ self.state
+ .scroll_nodes
.iter()
.find(|node| node.stable_node_id == stable_node_id)
}
@@ -452,7 +415,7 @@ impl AsyncScrollTree {
) -> Option<AsyncScrollOffset> {
// The compositor can advance only the scroll offsets it owns in this snapshot. Hit testing already selects an
// ancestor when the target cannot scroll in the wheel direction at all, so once a node moves it consumes the event.
- for _ in 0..self.scroll_nodes.len() {
+ for _ in 0..self.state.scroll_nodes.len() {
if delta.is_zero() {
break;
}
@@ -496,7 +459,7 @@ impl AsyncScrollTree {
) -> Option<WheelScrollTarget> {
// Each box is measured from where its scroll in flight is headed, so a box already headed for its extent passes the
// first step of a gesture on to an ancestor.
- for _ in 0..self.scroll_nodes.len() {
+ for _ in 0..self.state.scroll_nodes.len() {
let node = self.scroll_node_for_index(scroll_node_index)?;
let start_offset = in_flight_destination_for(node.stable_node_id)
.unwrap_or(Self::scroll_offset_for_scroll_node(node, scroll_state_snapshot));
@@ -564,8 +527,8 @@ impl AsyncScrollTree {
))
};
self.cached_wheel_hit_test_targets
- .reserve(self.wheel_hit_test_regions.len());
- for target in &self.wheel_hit_test_regions {
+ .reserve(self.state.wheel_hit_test_targets.len());
+ for target in &self.state.wheel_hit_test_targets {
if !context_is_valid(target.context) {
continue;
}
@@ -579,8 +542,8 @@ impl AsyncScrollTree {
});
}
self.cached_main_thread_wheel_event_targets
- .reserve(self.main_thread_wheel_event_regions.len());
- for region in &self.main_thread_wheel_event_regions {
+ .reserve(self.state.main_thread_wheel_event_regions.len());
+ for region in &self.state.main_thread_wheel_event_regions {
if !context_is_valid(region.context) {
continue;
}
@@ -592,8 +555,8 @@ impl AsyncScrollTree {
});
}
self.cached_nested_context_wheel_regions
- .reserve(self.nested_context_wheel_regions.len());
- for region in &self.nested_context_wheel_regions {
+ .reserve(self.state.nested_context_wheel_regions.len());
+ for region in &self.state.nested_context_wheel_regions {
if !context_is_valid(region.context) {
continue;
}
@@ -603,7 +566,7 @@ impl AsyncScrollTree {
viewport_rect: viewport_rect_for_context(region.context, region.rect),
});
}
- for region in &self.blocking_wheel_event_regions {
+ for region in &self.state.blocking_wheel_event_regions {
// Keep invalid blockers so hit testing fails closed when their extent cannot be determined.
self.cached_blocking_wheel_event_targets
.push(CachedBlockingWheelEventTarget {
@@ -653,15 +616,15 @@ impl AsyncScrollTree {
pub fn css_pixels_from_device_offset(&self, device_offset: FloatPoint) -> CssPixelPoint {
CssPixelPoint {
- x: CssPixels::nearest_value_for(device_offset.x as f64 / self.device_pixels_per_css_pixel),
- y: CssPixels::nearest_value_for(device_offset.y as f64 / self.device_pixels_per_css_pixel),
+ x: CssPixels::nearest_value_for(device_offset.x as f64 / self.state.device_pixels_per_css_pixel),
+ y: CssPixels::nearest_value_for(device_offset.y as f64 / self.state.device_pixels_per_css_pixel),
}
}
pub fn device_offset_from_css_pixels(&self, offset: CssPixelPoint) -> FloatPoint {
FloatPoint {
- x: (offset.x.to_double() * self.device_pixels_per_css_pixel) as f32,
- y: (offset.y.to_double() * self.device_pixels_per_css_pixel) as f32,
+ x: (offset.x.to_double() * self.state.device_pixels_per_css_pixel) as f32,
+ y: (offset.y.to_double() * self.state.device_pixels_per_css_pixel) as f32,
}
}
@@ -683,7 +646,7 @@ impl AsyncScrollTree {
}
pub fn viewport_scroll_node_index(&self) -> Option<SpatialNodeIndex> {
- for node in &self.scroll_nodes {
+ for node in &self.state.scroll_nodes {
if node.is_viewport {
return Some(node.scroll_node_index);
}
@@ -703,7 +666,7 @@ impl AsyncScrollTree {
visual_context_tree: &AccumulatedVisualContextTree,
position: FloatPoint,
) -> bool {
- if self.has_blocking_wheel_event_region_covering_viewport
+ if self.state.has_blocking_wheel_event_region_covering_viewport
|| !self.has_wheel_hit_test_targets_for(visual_context_tree)
{
return true;
@@ -933,7 +896,7 @@ impl AsyncScrollTree {
let context_is_valid = |context| visual_context_tree.context_is_valid(context);
// Neither the listeners nor the main thread have anything left to say about a step the page let through.
let page_has_yet_to_see_the_step = wheel_step_dispatch == WheelStepDispatch::NotYetDispatched;
- if page_has_yet_to_see_the_step && self.has_blocking_wheel_event_region_covering_viewport {
+ if page_has_yet_to_see_the_step && self.state.has_blocking_wheel_event_region_covering_viewport {
return WheelHitTestResult {
outcome: WheelHitTestOutcome::BlockedByWheelEventRegion,
..Default::default()
@@ -1105,9 +1068,9 @@ impl AsyncScrollTree {
// scroll node.
pub fn viewport_keeps_scrolls_along_main_axis_of(&self, delta: FloatPoint) -> bool {
if delta_is_mainly_vertical(delta) {
- self.viewport_keeps_vertical_scrolls
+ self.state.viewport_keeps_vertical_scrolls
} else {
- self.viewport_keeps_horizontal_scrolls
+ self.state.viewport_keeps_horizontal_scrolls
}
}
diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
index a16bc6d1c6a..26bbe47f99b 100644
--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp
+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp
@@ -219,7 +219,7 @@ bool CompositorConnectionFrameSink::submit(CompositorFrame&& frame)
if (auto& update = frame.visual_context_tree_update; update.has_value()) {
if (!post_resource_additions_in_batches(context_id, update->resource_transaction))
return false;
- auto encoded_message = MUST(Messages::CompositorWebContentServer::UpdateVisualContextTree::static_encode(context_id, update->visual_context_tree, update->resource_transaction));
+ auto encoded_message = MUST(Messages::CompositorWebContentServer::UpdateVisualContextTree::static_encode(context_id, update->visual_context_tree, update->resource_transaction, update->encoded_scroll_state, IPC::Encoded { update->keyboard_scroll_state }));
if (!post(encoded_message))
return false;
}
diff --git a/Libraries/LibWeb/Compositor/CompositorFrame.h b/Libraries/LibWeb/Compositor/CompositorFrame.h
index 5cedf307d5e..0cf552e1896 100644
--- a/Libraries/LibWeb/Compositor/CompositorFrame.h
+++ b/Libraries/LibWeb/Compositor/CompositorFrame.h
@@ -35,13 +35,15 @@ struct CompositorFrame {
ByteBuffer encoded_scroll_state;
};
- // A new visual context tree for the display list the compositor already has.
+ // A new visual context tree for the installed display list, with its resources, scroll snapshot and keyboard scroll state.
struct VisualContextTreeUpdate {
Compositing::AccumulatedVisualContextTree visual_context_tree;
Compositing::DisplayListResourceTransaction resource_transaction;
+ ByteBuffer encoded_scroll_state;
+ Compositing::KeyboardScrollState keyboard_scroll_state;
};
- // The scroll state of the display list the compositor already has.
+ // Updated scroll offsets and keyboard scroll state for the installed display list and visual context tree.
struct ScrollStateUpdate {
ByteBuffer encoded_scroll_state;
Compositing::KeyboardScrollState keyboard_scroll_state;
diff --git a/Libraries/LibWeb/Compositor/NavigablePresenter.cpp b/Libraries/LibWeb/Compositor/NavigablePresenter.cpp
index c1753a6d6ba..0ecba782823 100644
--- a/Libraries/LibWeb/Compositor/NavigablePresenter.cpp
+++ b/Libraries/LibWeb/Compositor/NavigablePresenter.cpp
@@ -132,15 +132,18 @@ CompositorFrame NavigablePresenter::build_frame(SealedPresentation const& sealed
frame.visual_context_tree_update = CompositorFrame::VisualContextTreeUpdate {
.visual_context_tree = visual_context_tree,
.resource_transaction = m_resource_storage.create_transaction(m_compositor_display_list_resources, resources),
+ .encoded_scroll_state = MUST(ByteBuffer::copy(encoded_scroll_state)),
+ .keyboard_scroll_state = move(keyboard_scroll_state),
};
m_resource_storage.retain_only(resources);
m_compositor_display_list_resources = move(resources);
m_compositor_visual_context_tree = visual_context_tree;
+ } else {
+ frame.scroll_state_update = CompositorFrame::ScrollStateUpdate {
+ .encoded_scroll_state = MUST(ByteBuffer::copy(encoded_scroll_state)),
+ .keyboard_scroll_state = move(keyboard_scroll_state),
+ };
}
- frame.scroll_state_update = CompositorFrame::ScrollStateUpdate {
- .encoded_scroll_state = MUST(ByteBuffer::copy(encoded_scroll_state)),
- .keyboard_scroll_state = move(keyboard_scroll_state),
- };
return frame;
}
diff --git a/Services/Compositor/CompositorCore.cpp b/Services/Compositor/CompositorCore.cpp
index f52c8c7126c..64f05d904e7 100644
--- a/Services/Compositor/CompositorCore.cpp
+++ b/Services/Compositor/CompositorCore.cpp
@@ -129,9 +129,9 @@ bool CompositorCore::install_display_list_update(Web::CompositorContextId contex
return RustFFI::compositor_core_install_display_list_update(m_core, context_id.value(), &display_list_for_scroll_logic, visual_context_tree.rust_handle(), encoded_scroll_state, now.nanoseconds());
}
-bool CompositorCore::update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree const& visual_context_tree, MonotonicTime now)
+CompositorCore::VisualContextTreeUpdateResult CompositorCore::update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree const& visual_context_tree, ReadonlyBytes encoded_scroll_state, IPC::Encoded<Compositing::KeyboardScrollState> const& keyboard_scroll_state, MonotonicTime now)
{
- return RustFFI::compositor_core_update_visual_context_tree(m_core, context_id.value(), visual_context_tree.rust_handle(), now.nanoseconds());
+ return RustFFI::compositor_core_update_visual_context_tree(m_core, context_id.value(), visual_context_tree.rust_handle(), encoded_scroll_state, keyboard_scroll_state.bytes(), now.nanoseconds());
}
bool CompositorCore::needs_animation_frames(Web::CompositorContextId context_id, MonotonicTime now)
diff --git a/Services/Compositor/CompositorCore.h b/Services/Compositor/CompositorCore.h
index 4e92e50ecd6..604d07ecb95 100644
--- a/Services/Compositor/CompositorCore.h
+++ b/Services/Compositor/CompositorCore.h
@@ -32,6 +32,7 @@ public:
~CompositorCore();
using ContextOwnerCheckResult = RustFFI::ContextOwnerCheckResult;
+ using VisualContextTreeUpdateResult = RustFFI::VisualContextTreeUpdateResult;
// What the presentation a context is shown through makes of the user's scrolling.
using ScrollCapabilities = RustFFI::FfiScrollCapabilities;
@@ -50,8 +51,9 @@ public:
// Answers false, changing nothing, for async scrolling metadata or a scroll state that do not decode.
bool install_display_list_update(Web::CompositorContextId, Compositing::DisplayList const&, IPC::Encoded<Optional<Compositing::DisplayList::AsyncScrollingMetadata>> const&, Compositing::AccumulatedVisualContextTree const&, ReadonlyBytes encoded_scroll_state, MonotonicTime now);
- // Returns false, leaving everything as it was, for a tree that does not fit the installed display list.
- bool update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree const&, MonotonicTime now);
+ // Changes nothing for a scroll state or keyboard scroll state that do not decode. Of a tree that does not fit the
+ // installed display list, only the scroll state is taken.
+ VisualContextTreeUpdateResult update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree const&, ReadonlyBytes encoded_scroll_state, IPC::Encoded<Compositing::KeyboardScrollState> const&, MonotonicTime now);
bool needs_animation_frames(Web::CompositorContextId, MonotonicTime now);
// Each frame shows the scrollbars as of when it is painted, so this updates which of them are shown. Empty until a
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index d09d72e3ce2..c0c63ae802e 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -409,15 +409,23 @@ void CompositorState::update_display_list_resources(Web::CompositorContextId con
show_animated_image_frames_in(*context);
}
-void CompositorState::update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction&& resource_transaction)
+bool CompositorState::update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction&& resource_transaction, ReadonlyBytes encoded_scroll_state, IPC::Encoded<Compositing::KeyboardScrollState> const& keyboard_scroll_state)
{
auto now = MonotonicTime::now();
auto* context = presented_context_if_present(context_id);
VERIFY(context);
- if (m_core.update_visual_context_tree(context_id, visual_context_tree, now))
+ switch (m_core.update_visual_context_tree(context_id, visual_context_tree, encoded_scroll_state, keyboard_scroll_state, now)) {
+ case CompositorCore::VisualContextTreeUpdateResult::TreeReplaced:
context->presenter.apply_resource_transaction(move(resource_transaction));
+ break;
+ case CompositorCore::VisualContextTreeUpdateResult::TreeDropped:
+ break;
+ case CompositorCore::VisualContextTreeUpdateResult::InvalidScrollState:
+ return false;
+ }
show_animated_image_frames_in(*context);
+ return true;
}
CompositorState::VideoSinkState* CompositorState::video_sink_state(CompositorStateWebContentClient& client, Media::VideoSinkHandle handle)
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index b8a3390f115..9f0a02e299d 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -116,7 +116,8 @@ public:
// Answers false, changing nothing, for async scrolling metadata or a scroll state that do not decode.
bool update_display_list(Web::CompositorContextId, NonnullRefPtr<Compositing::DisplayList>, IPC::Encoded<Optional<Compositing::DisplayList::AsyncScrollingMetadata>> const&, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction&&, ReadonlyBytes encoded_scroll_state);
void update_display_list_resources(Web::CompositorContextId, Compositing::DisplayListResourceTransaction&&);
- void update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction&&);
+ // Answers false, changing nothing, for a scroll state or keyboard scroll state that do not decode.
+ bool update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction&&, ReadonlyBytes encoded_scroll_state, IPC::Encoded<Compositing::KeyboardScrollState> const&);
void add_video_sink(CompositorStateWebContentClient&, Media::VideoSinkHandle);
// For a presentation channel that arrives after the sinks did, or that replaces the one their edges were on.
void create_video_edges(CompositorStateWebContentClient&);
diff --git a/Services/Compositor/CompositorWebContentServer.ipc b/Services/Compositor/CompositorWebContentServer.ipc
index ddff7194c73..dcc2d089335 100644
--- a/Services/Compositor/CompositorWebContentServer.ipc
+++ b/Services/Compositor/CompositorWebContentServer.ipc
@@ -42,11 +42,11 @@ endpoint CompositorWebContentServer
stop_presenting_to_client(Web::CompositorContextId context_id) =|
destroy_context(Web::CompositorContextId context_id) =|
- // The async scrolling metadata and the scroll state go encoded: the scroll logic, which is Rust, decodes them, and
- // Rust encoded the scroll state.
+ // The async scrolling metadata, the scroll state and the keyboard scroll state that comes with a visual context tree
+ // go encoded: the scroll logic, which is Rust, decodes them, and Rust encoded the scroll state.
update_display_list(Web::CompositorContextId context_id, Core::AnonymousBuffer display_list_buffer, u64 tape_size, u64 run_count, Compositing::DisplayList::Properties display_list_properties, IPC::Encoded<Optional<Compositing::DisplayList::AsyncScrollingMetadata>> async_scrolling_metadata, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction resource_transaction, ByteBuffer scroll_state) =|
update_display_list_resources(Web::CompositorContextId context_id, Compositing::DisplayListResourceTransaction resource_transaction) =|
- update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction resource_transaction) =|
+ update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction resource_transaction, ByteBuffer scroll_state, IPC::Encoded<Compositing::KeyboardScrollState> keyboard_scroll_state) =|
[RustReceiver] update_scroll_state(Web::CompositorContextId context_id, ByteBuffer scroll_state, Compositing::KeyboardScrollState keyboard_scroll_state) =|
// Hands WebContent a range of canvas IDs it may name its own 2D contexts with, so creating one needs no round trip.
diff --git a/Services/Compositor/ConnectionFromWebContent.cpp b/Services/Compositor/ConnectionFromWebContent.cpp
index 9ee061ae0ec..6dfa45acabf 100644
--- a/Services/Compositor/ConnectionFromWebContent.cpp
+++ b/Services/Compositor/ConnectionFromWebContent.cpp
@@ -317,11 +317,12 @@ void ConnectionFromWebContent::update_display_list_resources(Web::CompositorCont
m_compositor_state->update_display_list_resources(context_id, move(resource_transaction));
}
-void ConnectionFromWebContent::update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction resource_transaction)
+void ConnectionFromWebContent::update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction resource_transaction, ByteBuffer scroll_state, IPC::Encoded<Compositing::KeyboardScrollState> keyboard_scroll_state)
{
if (!context_is_owned_by_this_connection(context_id))
return;
- m_compositor_state->update_visual_context_tree(context_id, move(visual_context_tree), move(resource_transaction));
+ if (!m_compositor_state->update_visual_context_tree(context_id, move(visual_context_tree), move(resource_transaction), scroll_state.bytes(), keyboard_scroll_state))
+ did_misbehave("WebContent published a visual context tree with a scroll state the compositor cannot use");
}
Messages::CompositorWebContentServer::ReserveCanvasIdsResponse ConnectionFromWebContent::reserve_canvas_ids()
diff --git a/Services/Compositor/ConnectionFromWebContent.h b/Services/Compositor/ConnectionFromWebContent.h
index 501b8127484..fe89f52facb 100644
--- a/Services/Compositor/ConnectionFromWebContent.h
+++ b/Services/Compositor/ConnectionFromWebContent.h
@@ -58,7 +58,7 @@ private:
virtual void stop_presenting_to_client(Web::CompositorContextId) override;
virtual void destroy_context(Web::CompositorContextId) override;
virtual void update_display_list(Web::CompositorContextId, Core::AnonymousBuffer display_list_buffer, u64 tape_size, u64 run_count, Compositing::DisplayList::Properties, IPC::Encoded<Optional<Compositing::DisplayList::AsyncScrollingMetadata>>, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction, ByteBuffer scroll_state) override;
- virtual void update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction) override;
+ virtual void update_visual_context_tree(Web::CompositorContextId, Compositing::AccumulatedVisualContextTree, Compositing::DisplayListResourceTransaction, ByteBuffer scroll_state, IPC::Encoded<Compositing::KeyboardScrollState>) override;
virtual void update_display_list_resources(Web::CompositorContextId, Compositing::DisplayListResourceTransaction) override;
virtual Messages::CompositorWebContentServer::ReserveCanvasIdsResponse reserve_canvas_ids() override;
virtual void create_canvas_2d_context(Compositing::CanvasId, Gfx::IntSize, bool alpha, bool will_read_frequently, Gfx::ColorSpace) override;
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 38e0f20da92..55cd9bcd8ad 100644
--- a/Services/Compositor/Rust/src/context_state/frames_and_viewport.rs
+++ b/Services/Compositor/Rust/src/context_state/frames_and_viewport.rs
@@ -147,13 +147,7 @@ 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 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));
+ let mut scroll_state = JsonWriter(async_scrolling_state_to_json(self.async_scroll_tree.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 65136ace74a..3f58cf17e29 100644
--- a/Services/Compositor/Rust/src/context_state/mod.rs
+++ b/Services/Compositor/Rust/src/context_state/mod.rs
@@ -741,9 +741,6 @@ impl ContextState {
.set_node_count(self.scene.visual_context_tree().as_ref().unwrap().spatial_node_count());
let display_list = self.scene.display_list().as_ref().unwrap().clone();
self.restart_caret_blink_for(&display_list, now);
- let mut async_scrolling_viewport_rect = async_scrolling_state.viewport_rect;
- self.wheel_gesture_routing
- .did_install_scrolling_state(&async_scrolling_state);
// The visual viewport of another document is where that document's visual context tree has it.
if self.scene.has_async_visual_viewport_transform()
&& async_scrolling_state.document_id != self.visual_viewport_document_id
@@ -755,6 +752,21 @@ impl ContextState {
if async_scrolling_state.document_id != self.async_scroll_tree.document_id() {
self.pinch_gesture_was_let_through = false;
}
+ self.adopt_scrolling_state(async_scrolling_state, &scroll_offsets_of_known_nodes);
+ if let Some(metadata) = self.scene.display_list().as_ref().unwrap().async_scrolling_metadata() {
+ self.adopt_keyboard_scroll_state(metadata.keyboard_scroll_state.clone());
+ }
+ true
+ }
+
+ fn adopt_scrolling_state(
+ &mut self,
+ async_scrolling_state: AsyncScrollingState,
+ scroll_offsets_of_known_nodes: &HashMap<AsyncScrollNodeStableID, FloatPoint>,
+ ) {
+ let mut async_scrolling_viewport_rect = async_scrolling_state.viewport_rect;
+ self.wheel_gesture_routing
+ .did_install_scrolling_state(&async_scrolling_state);
let box_of_dragged_scrollbar = self.box_of_captured_scrollbar();
self.scrollbar_controller
.set_scrollbars(&async_scrolling_state.scrollbars);
@@ -767,17 +779,13 @@ impl ContextState {
.did_install_scrolling_state(&self.async_scroll_tree);
self.scroll_snap_controller
.did_install_scrolling_state(&self.async_scroll_tree);
- if let Some(offset) = self.keep_scroll_offsets_of_known_nodes(&scroll_offsets_of_known_nodes) {
+ if let Some(offset) = self.keep_scroll_offsets_of_known_nodes(scroll_offsets_of_known_nodes) {
async_scrolling_viewport_rect.x = offset.x as i32;
async_scrolling_viewport_rect.y = offset.y as i32;
}
self.rebuild_wheel_hit_test_targets();
self.async_scrolling_viewport_rect = async_scrolling_viewport_rect;
self.has_async_scrolling_state = true;
- if let Some(metadata) = self.scene.display_list().as_ref().unwrap().async_scrolling_metadata() {
- self.adopt_keyboard_scroll_state(metadata.keyboard_scroll_state.clone());
- }
- true
}
// Repaints the blinking caret of a display list each time its blink cycle turns it on or off.
@@ -848,24 +856,37 @@ impl ContextState {
damage_rect
}
- // Returns false, leaving everything as it was, for a tree that does not fit the installed display list.
+ // Returns false for a tree that does not fit the installed display list, which keeps the tree it has and takes only
+ // the scroll state that pairs with the new one.
pub fn update_visual_context_tree(
&mut self,
visual_context_tree: AccumulatedVisualContextTree,
+ scroll_state_snapshot: ScrollStateSnapshot,
+ keyboard_scroll_state: KeyboardScrollState,
now: MonotonicTime,
) -> bool {
+ let scroll_offsets_of_known_nodes = self.scroll_offsets_by_stable_id();
+ let previous_visual_context_tree = self.scene.visual_context_tree().clone();
if !self.scene.replace_visual_context_tree(visual_context_tree, now) {
+ self.update_scroll_state(scroll_state_snapshot, keyboard_scroll_state);
return false;
}
- // A constraints refresh changes sticky payloads without a new snapshot, and the snapshot that
- // pairs with this tree arrives in a separate message.
- resolve_sticky_offsets(
- self.scene.visual_context_tree().as_ref().unwrap(),
- &mut self.scroll_state_snapshot,
+ let compositor_scroll_table_changed = previous_visual_context_tree.is_some_and(|previous| {
+ previous.as_arc().compositor_scroll_table()
+ != self
+ .scene
+ .visual_context_tree()
+ .as_ref()
+ .unwrap()
+ .as_arc()
+ .compositor_scroll_table()
+ });
+ self.update_scroll_state_with_known_offsets(
+ scroll_state_snapshot,
+ keyboard_scroll_state,
+ &scroll_offsets_of_known_nodes,
+ compositor_scroll_table_changed,
);
- if self.has_async_scrolling_state {
- self.rebuild_wheel_hit_test_targets();
- }
true
}
@@ -874,8 +895,24 @@ impl ContextState {
scroll_state_snapshot: ScrollStateSnapshot,
keyboard_scroll_state: KeyboardScrollState,
) {
- self.scene.forget_whether_animated_content_may_affect_viewport();
let scroll_offsets_of_known_nodes = self.scroll_offsets_by_stable_id();
+ self.update_scroll_state_with_known_offsets(
+ scroll_state_snapshot,
+ keyboard_scroll_state,
+ &scroll_offsets_of_known_nodes,
+ false,
+ );
+ }
+
+ fn update_scroll_state_with_known_offsets(
+ &mut self,
+ scroll_state_snapshot: ScrollStateSnapshot,
+ keyboard_scroll_state: KeyboardScrollState,
+ scroll_offsets_of_known_nodes: &HashMap<AsyncScrollNodeStableID, FloatPoint>,
+ compositor_scroll_table_changed: bool,
+ ) {
+ self.scene.forget_whether_animated_content_may_affect_viewport();
+ // Newly adopted stable ids must start at the offsets in the snapshot that pairs with this tree.
self.scroll_state_snapshot = scroll_state_snapshot;
self.scroll_state_snapshot.set_node_count(
self.scene
@@ -886,11 +923,19 @@ impl ContextState {
if !self.has_async_scrolling_state {
return;
}
- let kept_viewport_scroll_offset = self.keep_scroll_offsets_of_known_nodes(&scroll_offsets_of_known_nodes);
- self.rebuild_wheel_hit_test_targets();
- if let Some(offset) = kept_viewport_scroll_offset {
- self.async_scrolling_viewport_rect.x = offset.x as i32;
- self.async_scrolling_viewport_rect.y = offset.y as i32;
+ if compositor_scroll_table_changed {
+ let async_scrolling_state = async_scrolling_state_from(
+ self.scene.display_list().as_ref().unwrap(),
+ self.scene.visual_context_tree().as_ref().unwrap().as_arc(),
+ );
+ self.adopt_scrolling_state(async_scrolling_state, scroll_offsets_of_known_nodes);
+ } else {
+ let kept_viewport_scroll_offset = self.keep_scroll_offsets_of_known_nodes(scroll_offsets_of_known_nodes);
+ self.rebuild_wheel_hit_test_targets();
+ if let Some(offset) = kept_viewport_scroll_offset {
+ self.async_scrolling_viewport_rect.x = offset.x as i32;
+ self.async_scrolling_viewport_rect.y = offset.y as i32;
+ }
}
self.adopt_keyboard_scroll_state(keyboard_scroll_state);
}
diff --git a/Services/Compositor/Rust/src/core/mod.rs b/Services/Compositor/Rust/src/core/mod.rs
index 4c9b51bbcfe..2c747d771d2 100644
--- a/Services/Compositor/Rust/src/core/mod.rs
+++ b/Services/Compositor/Rust/src/core/mod.rs
@@ -22,7 +22,7 @@ use crate::host::{
use crate::wheel_gesture_routing::WheelGestureIdAllocator;
use libcompositing_rust::ipc_types::Compositing_ContextVisibility as ContextVisibility;
use libcompositing_rust::scrolling::{
- AccumulatedVisualContextTree, CompositorContextId, DisplayList, ScrollStateSnapshot,
+ AccumulatedVisualContextTree, CompositorContextId, DisplayList, KeyboardScrollState, ScrollStateSnapshot,
};
use libcompositing_rust::time::MonotonicTime;
use libgfx_rust::{FloatPoint, IntSize};
@@ -47,6 +47,16 @@ pub enum ContextOwnerCheckResult {
ConflictingOwner,
}
+// What became of a visual context tree that came with the scroll state pairing with it.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+#[repr(u8)]
+pub enum VisualContextTreeUpdateResult {
+ TreeReplaced,
+ // The tree does not fit the installed display list, so only its scroll state was taken.
+ TreeDropped,
+ InvalidScrollState,
+}
+
#[derive(Clone, Copy, PartialEq, Eq)]
enum WheelStepOrigin {
UI,
@@ -228,19 +238,24 @@ impl CompositorCore {
.install_display_list_update(display_list, visual_context_tree, scroll_state_snapshot, now)
}
- // Returns false, leaving everything as it was, for a tree that does not fit the installed display list.
+ // Returns false for a tree that does not fit the installed display list, of which only the scroll state is taken.
pub fn update_visual_context_tree(
&self,
context_id: CompositorContextId,
visual_context_tree: AccumulatedVisualContextTree,
+ scroll_state_snapshot: ScrollStateSnapshot,
+ keyboard_scroll_state: KeyboardScrollState,
now: MonotonicTime,
) -> bool {
let _entered = self.enter();
let context = self.context_if_present(context_id).expect("context exists");
- context
- .borrow_mut()
- .update_visual_context_tree(visual_context_tree, now)
+ context.borrow_mut().update_visual_context_tree(
+ visual_context_tree,
+ scroll_state_snapshot,
+ keyboard_scroll_state,
+ now,
+ )
}
pub fn needs_animation_frames(&self, context_id: CompositorContextId, now: MonotonicTime) -> bool {
diff --git a/Services/Compositor/Rust/src/ffi.rs b/Services/Compositor/Rust/src/ffi.rs
index 8dea9239889..733d8d0aadd 100644
--- a/Services/Compositor/Rust/src/ffi.rs
+++ b/Services/Compositor/Rust/src/ffi.rs
@@ -8,7 +8,7 @@
use crate::context_state::ScrollCapabilities;
use crate::core::messages::{MessagesFromPresentation, MessagesFromWebContent};
-use crate::core::{CompositorCore, ContextOwnerCheckResult};
+use crate::core::{CompositorCore, ContextOwnerCheckResult, VisualContextTreeUpdateResult};
use crate::frame_inputs::{FrameInputs, ScrollbarOfFrame};
use crate::host::{
ContextTimer, DisplayTickInputs, DisplayTickResult, FfiCompositorCoreHost, Host, PresentScheduling, PresentationId,
@@ -26,8 +26,8 @@ use libcompositing_rust::host::FfiDisplayListReplayCallbacks;
use libcompositing_rust::ipc_types::{
Compositing_ContextVisibility as ContextVisibility, compositor_presentation_server, compositor_web_content_server,
};
-use libcompositing_rust::scrolling::ScrollStateSnapshot;
use libcompositing_rust::scrolling::wheel_gesture_identity::TouchpadGestureEnd;
+use libcompositing_rust::scrolling::{KeyboardScrollState, ScrollStateSnapshot};
use libcompositing_rust::time::MonotonicTime;
use libgfx_rust::{Color, IntRect, IntSize};
use libipc_rust::cpp_connection::{EncodedValue, RawMessage};
@@ -220,7 +220,8 @@ pub unsafe extern "C" fn compositor_core_install_display_list_update(
)
}
-/// Answers false, leaving everything as it was, for a tree that does not fit the installed display list.
+/// Changes nothing for a scroll state or keyboard scroll state that do not decode, or a scroll state with offsets that
+/// are not finite.
///
/// # Safety
///
@@ -230,14 +231,32 @@ pub unsafe extern "C" fn compositor_core_update_visual_context_tree(
core: &CompositorCore,
context_id: u64,
visual_context_tree: *const c_void,
+ scroll_state: EncodedValue<'_>,
+ keyboard_scroll_state: EncodedValue<'_>,
now_nanoseconds: i64,
-) -> bool {
- core.update_visual_context_tree(
+) -> VisualContextTreeUpdateResult {
+ let (Ok(scroll_state_snapshot), Ok(keyboard_scroll_state)) = (
+ scroll_state.decode::<ScrollStateSnapshot>(),
+ keyboard_scroll_state.decode::<KeyboardScrollState>(),
+ ) else {
+ return VisualContextTreeUpdateResult::InvalidScrollState;
+ };
+ if !scroll_state_snapshot.has_only_finite_offsets() {
+ return VisualContextTreeUpdateResult::InvalidScrollState;
+ }
+ let tree_was_replaced = core.update_visual_context_tree(
CompositorContextId(context_id),
// SAFETY: The caller lends a strong reference to the tree.
unsafe { tree_sharing_handle(visual_context_tree) },
+ scroll_state_snapshot,
+ keyboard_scroll_state,
MonotonicTime::from_nanoseconds(now_nanoseconds),
- )
+ );
+ if tree_was_replaced {
+ VisualContextTreeUpdateResult::TreeReplaced
+ } else {
+ VisualContextTreeUpdateResult::TreeDropped
+ }
}
#[unsafe(no_mangle)]