Skip to content

Truly independent web browser

Compositor: Lift the scroll and input logic into CompositorCore

Aliaksandr Kalenik350179735165
diff --git a/Services/Compositor/CMakeLists.txt b/Services/Compositor/CMakeLists.txt
index ad7db32ddac..ef3b19d58d6 100644
--- a/Services/Compositor/CMakeLists.txt
+++ b/Services/Compositor/CMakeLists.txt
@@ -6,6 +6,7 @@ set(SOURCES
Canvas2DCommandPlayer.cpp
CanvasHost.cpp
ColorSpaceSkia.cpp
+ CompositorCore.cpp
CompositorState.cpp
ContextPresenter.cpp
ContextScene.cpp
diff --git a/Services/Compositor/CompositorCore.cpp b/Services/Compositor/CompositorCore.cpp
new file mode 100644
index 00000000000..e792cfa5320
--- /dev/null
+++ b/Services/Compositor/CompositorCore.cpp
@@ -0,0 +1,1180 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/StdLibExtras.h>
+#include <AK/TemporaryChange.h>
+#include <Compositor/CompositorCore.h>
+#include <Compositor/CompositorPresentationClientEndpoint.h>
+
+namespace Compositor {
+
+CompositorCore::CompositorCore(CompositorCoreHost& host)
+ : m_host(host)
+{
+}
+
+void CompositorCore::create_context(Web::CompositorContextId context_id, Optional<u64> page_id, WebContentClientId web_content_client_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ VERIFY(!m_contexts.contains(context_id));
+ if (page_id.has_value())
+ VERIFY(context_id == Web::compositor_context_id_for_page(*page_id));
+
+ m_contexts.ensure(context_id, [&] {
+ return make<ContextState>(context_id, page_id, web_content_client_id, m_host);
+ });
+}
+
+void CompositorCore::destroy_context(Web::CompositorContextId context_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ VERIFY(context_if_present(context_id));
+ for (auto& context_entry : m_contexts) {
+ if (context_entry.key == context_id)
+ continue;
+ auto& possible_child_context = *context_entry.value;
+ auto parent_context_id = possible_child_context.parent_context_id();
+ if (parent_context_id.has_value() && *parent_context_id == context_id)
+ possible_child_context.set_parent_context({});
+ }
+ m_contexts.remove(context_id);
+}
+
+CompositorCore::ContextOwnerCheckResult CompositorCore::check_context_owner(Web::CompositorContextId context_id, WebContentClientId client) const
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto const* context = context_if_present(context_id);
+ if (!context)
+ return ContextOwnerCheckResult::ContextUnavailable;
+ if (!context->is_owned_by(client))
+ return ContextOwnerCheckResult::ConflictingOwner;
+
+ return ContextOwnerCheckResult::OwnedByClient;
+}
+
+void CompositorCore::set_parent_context(Web::CompositorContextId context_id, Optional<Web::CompositorContextId> parent_context_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ context->set_parent_context(parent_context_id);
+}
+
+void CompositorCore::stop_presenting_to_client(Web::CompositorContextId context_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ context->stop_presenting_to_client();
+}
+
+void CompositorCore::viewport_size_updated(Web::CompositorContextId context_id, Gfx::IntSize viewport_size)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ if (!context)
+ return;
+ context->viewport_size_updated(viewport_size);
+}
+
+void CompositorCore::set_paused_debugger_overlay_is_shown(Web::CompositorContextId context_id, bool is_shown)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ if (!context)
+ return;
+ context->set_paused_debugger_overlay_is_shown(is_shown);
+}
+
+void CompositorCore::set_bound_presentation(Web::CompositorContextId context_id, Optional<PresentationId> presentation_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ context->set_bound_presentation(presentation_id);
+}
+
+void CompositorCore::set_scroll_capabilities(Web::CompositorContextId context_id, ContextState::ScrollCapabilities const& capabilities)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ context->set_scroll_capabilities(capabilities);
+}
+
+void CompositorCore::install_display_list_update(Web::CompositorContextId context_id, NonnullRefPtr<Compositing::DisplayList> display_list, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::ScrollStateSnapshot&& scroll_state_snapshot, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ context->install_display_list_update(move(display_list), move(visual_context_tree), move(scroll_state_snapshot), now);
+}
+
+bool CompositorCore::update_visual_context_tree(Web::CompositorContextId context_id, Compositing::AccumulatedVisualContextTree visual_context_tree, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ return context->update_visual_context_tree(move(visual_context_tree), now);
+}
+
+bool CompositorCore::needs_animation_frames(Web::CompositorContextId context_id, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ return context->needs_animation_frames(now);
+}
+
+Optional<FrameInputs> CompositorCore::frame_inputs(Web::CompositorContextId context_id, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ return context->frame_inputs(now);
+}
+
+ContextState::ScreenshotInputs CompositorCore::frame_inputs_for_screenshot(Web::CompositorContextId context_id, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ return context->frame_inputs_for_screenshot(now);
+}
+
+void CompositorCore::set_visibility(Web::CompositorContextId context_id, Compositing::ContextVisibility visibility, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ if (!context)
+ return;
+ if (visibility == Compositing::ContextVisibility::Hidden)
+ dispatch_scroll_fling_step(context_id, *context, context->end_scroll_fling(), now);
+ context->set_visibility(visibility);
+}
+
+void CompositorCore::timer_fired(Web::CompositorContextId context_id, ContextTimer timer, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ if (!context)
+ return;
+ auto caret_damage_rect = context->timer_fired(timer, now);
+ if (caret_damage_rect.has_value())
+ m_present_requests.append({ context_id, {}, *caret_damage_rect });
+}
+
+Vector<PresentRequest> CompositorCore::take_present_requests()
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ return move(m_present_requests);
+}
+
+void CompositorCore::update_scroll_state(Web::CompositorContextId context_id, Compositing::ScrollStateSnapshot&& scroll_state_snapshot, Compositing::KeyboardScrollState keyboard_scroll_state)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+
+ context->update_scroll_state(move(scroll_state_snapshot), move(keyboard_scroll_state));
+}
+
+void CompositorCore::invalidate_wheel_event_listener_state(Web::CompositorContextId context_id, u64 generation)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ context->invalidate_wheel_event_listener_state(generation);
+}
+
+void CompositorCore::invalidate_keyboard_scroll_state(Web::CompositorContextId context_id, u64 generation)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ if (auto* context = context_if_present(context_id))
+ context->invalidate_keyboard_scroll_state(generation);
+}
+
+void CompositorCore::handle_and_dispatch_key_event(Web::CompositorContextId context_id, Web::KeyEvent event, bool allow_keyboard_scroll, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ // NB: The presentation connection only passes on input for the contexts bound to it.
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ VERIFY(context->bound_presentation().has_value());
+ auto presentation_id = *context->bound_presentation();
+ if (!context->can_dispatch_input_to_web_content()) {
+ // No context could take the event; the UI sends it to WebContent itself.
+ post_to_presentation<Messages::CompositorPresentationClient::DidNotDispatchInputEvent>(presentation_id, context_id, event.id);
+ return;
+ }
+
+ bool later_keys_need_not_wait = false;
+ if (allow_keyboard_scroll) {
+ if (auto box = context->box_scrolled_for_key_event(event); box.has_value())
+ dispatch_scroll_fling_step(context_id, *context, context->end_momentum_scrolling(box.value()), now);
+ auto result = context->handle_key_event(event, now);
+ later_keys_need_not_wait = apply_context_update_result(context_id, *context, result) && event.type == Web::KeyEvent::Type::KeyDown;
+ event.async_scroll_performed_default_action = later_keys_need_not_wait && !result.key_found_nothing_to_scroll;
+ if (later_keys_need_not_wait && result.key_found_nothing_to_scroll)
+ event.user_input_scroll_count_with_nothing_to_scroll = context->user_input_scroll_count();
+ }
+
+ // The scroll updates the event produced reach WebContent ahead of the event itself on the same connection.
+ publish_pending_async_scroll_updates(context_id, *context);
+ context->dispatch_key_event_to_web_content(event);
+ // The compositor forwarded the key event to WebContent, after performing its default action by scrolling if it says so.
+ post_to_presentation<Messages::CompositorPresentationClient::DidDispatchInputEvent>(presentation_id, context_id, event.id, later_keys_need_not_wait);
+}
+
+void CompositorCore::handle_and_dispatch_mouse_event(Web::CompositorContextId context_id, Web::MouseEvent event, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ // NB: The presentation connection only passes on input for the contexts bound to it.
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ VERIFY(context->bound_presentation().has_value());
+ auto presentation_id = *context->bound_presentation();
+ auto record_routing = [&](Web::InputRoutingDecision decision, Optional<Web::AsyncScrollNodeStableID> scroller = {}) {
+ context->input_trace().record({ .input_event_id = event.id, .stage = Web::InputTraceStage::Compositor, .decision = decision, .scroller = scroller });
+ };
+ if (!context->can_dispatch_input_to_web_content()) {
+ record_routing(Web::InputRoutingDecision::NotDispatchedByCompositor);
+ post_to_presentation<Messages::CompositorPresentationClient::DidNotDispatchInputEvent>(presentation_id, context_id, event.id);
+ return;
+ }
+
+ if (event.type == Web::MouseEvent::Type::MouseMove)
+ context->note_pointer_position(Gfx::FloatPoint { static_cast<float>(event.position.x().value()), static_cast<float>(event.position.y().value()) });
+ else if (first_is_one_of(event.type, Web::MouseEvent::Type::MouseLeave, Web::MouseEvent::Type::MouseCancel))
+ context->note_pointer_position({});
+
+ if (event.type == Web::MouseEvent::Type::MouseDown)
+ dispatch_scroll_fling_step(context_id, *context, context->end_scroll_fling(), now);
+
+ auto is_wheel_event = event.type == Web::MouseEvent::Type::MouseWheel;
+ bool is_first_wheel_event_with_delta = false;
+ if (is_wheel_event) {
+ // A key, or a script's smooth scroll, takes over from momentum, until wheel input other than the momentum takes
+ // over again.
+ if (context->absorbs_wheel_event_of_interrupted_momentum(event)) {
+ record_routing(Web::InputRoutingDecision::MomentumInterrupted);
+ // The compositor performed the event's whole default action, so the UI hears nothing more about it.
+ post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
+ return;
+ }
+ auto noted = context->note_wheel_event_from_ui(event, now);
+ event.wheel_gesture_mark = noted.wheel_gesture_mark;
+ is_first_wheel_event_with_delta = noted.is_first_wheel_event_with_delta;
+
+ // The UI can send the end of a gesture more than once. An end that arrives during a fling belongs to the
+ // flicked gesture, so it does not stop the fling.
+ if (event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended && context->has_active_scroll_fling()) {
+ record_routing(Web::InputRoutingDecision::GestureEndAbsorbedByFling);
+ post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
+ return;
+ }
+
+ // A new gesture must not continue the gesture of the fling, so the fling ends first.
+ dispatch_scroll_fling_step(context_id, *context, context->interrupt_scroll_fling(event, now), now);
+
+ // If the gesture continues as a fling, the end of the fling is the end of the gesture.
+ if (context->presentation_synthesizes_scroll_momentum() && event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended && context->start_scroll_fling_if_flicked(event, now)) {
+ record_routing(Web::InputRoutingDecision::FlingStarted);
+ post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
+ context->request_display_ticks_if_needed(now);
+ return;
+ }
+ }
+
+ ContextState::ContextUpdateResult result;
+ if (is_wheel_event)
+ result = scroll_for_wheel_step(context_id, *context, event, WheelStepOrigin::UI, is_first_wheel_event_with_delta, Compositing::AsyncScrollOperationTracking::No, now);
+ else
+ result = handle_mouse_event_in_nested_contexts(context_id, *context, event, now);
+ if (result.nested_context_hand_off.has_value())
+ record_routing(*result.nested_context_hand_off);
+ if (result.routing_decision.has_value()) {
+ dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Context {} routed input event {}: {}", context_id, event.id, Web::input_routing_decision_name(*result.routing_decision));
+ record_routing(*result.routing_decision, result.routed_scroller);
+ }
+
+ // Asks for the present, publishes the scroll updates the event produced and asks for a rendering update, the last
+ // two of which reach WebContent ahead of the event itself on the same connection. Those of a nested context
+ // another process owns go on that process's connection instead, while the event reaches it through the UI.
+ auto handled = apply_context_update_result(context_id, *context, result);
+ // A wheel event that lets go of a stretched scroller presents nothing itself; the rebound does from here on. A press
+ // beside the thumb of a scrollbar scrolls there in an animation too.
+ context->request_display_ticks_if_needed(now);
+ if (is_wheel_event) {
+ event.async_scroll_performed_default_action = handled;
+ if (!handled)
+ context->note_wheel_event_left_to_the_page(event);
+ } else if (handled) {
+ // The page still sees the events of a drag the compositor scrolls for a scrollbar the display list paints.
+ if (!result.scrollbar_dragged_by_compositor.has_value()) {
+ record_routing(Web::InputRoutingDecision::ConsumedByCompositor);
+ post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
+ return;
+ }
+ event.scrollbar_dragged_by_compositor = result.scrollbar_dragged_by_compositor;
+ }
+ record_routing(Web::InputRoutingDecision::ForwardedToPage);
+ context->dispatch_mouse_event_to_web_content(event, nested_context_scrolled_since_last_frame(context_id));
+}
+
+// The content box of a nested context is drawn scaled to the size of its viewport.
+static Optional<Gfx::FloatPoint> position_in_nested_context(Compositing::NestedContextHit const& hit, ContextState const& nested_context)
+{
+ auto viewport_size = nested_context.viewport_size().to_type<float>();
+ if (hit.content_box_size.is_empty() || viewport_size.is_empty())
+ return {};
+ return Gfx::FloatPoint {
+ hit.position_in_content_box.x() * viewport_size.width() / hit.content_box_size.width(),
+ hit.position_in_content_box.y() * viewport_size.height() / hit.content_box_size.height(),
+ };
+}
+
+ContextState* CompositorCore::context_still_nested_in(Web::CompositorContextId containing_context_id, Optional<Web::CompositorContextId> context_id)
+{
+ if (!context_id.has_value())
+ return nullptr;
+ auto* context = context_if_present(*context_id);
+ if (!context || !is_nested_in(containing_context_id, *context))
+ return nullptr;
+ return context;
+}
+
+Optional<Gfx::FloatPoint> CompositorCore::position_in_nested_context_ignoring_clips(Web::CompositorContextId presenting_context_id, ContextState& presenting_context, ContextState& nested_context, Gfx::FloatPoint position)
+{
+ Vector<ContextState*, 4> contexts_from_nested_context_outward;
+ auto* context = &nested_context;
+ // A walk up the parents that outlasts the number of contexts went around a loop.
+ for (size_t remaining_contexts = m_contexts.size(); context != &presenting_context; --remaining_contexts) {
+ if (remaining_contexts == 0)
+ return {};
+ contexts_from_nested_context_outward.append(context);
+ auto parent_context_id = context->parent_context_id();
+ if (!parent_context_id.has_value())
+ return {};
+ context = *parent_context_id == presenting_context_id ? &presenting_context : context_if_present(*parent_context_id);
+ if (!context)
+ return {};
+ }
+
+ auto* containing_context = &presenting_context;
+ for (auto* context_in_between : contexts_from_nested_context_outward.in_reverse()) {
+ auto hit = containing_context->position_in_content_of_nested_context(context_in_between->context_id(), position);
+ if (!hit.has_value())
+ return {};
+ auto position_in_context = position_in_nested_context(*hit, *context_in_between);
+ if (!position_in_context.has_value())
+ return {};
+ position = *position_in_context;
+ containing_context = context_in_between;
+ }
+ return position;
+}
+
+ContextState::ContextUpdateResult CompositorCore::handle_mouse_event_in_nested_contexts(Web::CompositorContextId context_id, ContextState& context, Web::MouseEvent const& event, MonotonicTime now)
+{
+ using Type = Web::MouseEvent::Type;
+ auto& pointer = context.pointer_in_nested_contexts();
+
+ auto* context_with_captured_scrollbar = context_still_nested_in(context_id, pointer.context_with_captured_scrollbar);
+ if (!context_with_captured_scrollbar || !context_with_captured_scrollbar->has_captured_scrollbar()) {
+ pointer.context_with_captured_scrollbar.clear();
+ context_with_captured_scrollbar = nullptr;
+ }
+
+ // The nested context presents and publishes what the event changed, and this context reports how the event went.
+ auto result_of_nested_context = [&](Web::CompositorContextId nested_context_id, ContextState& nested_context, ContextState::ContextUpdateResult const& nested_result, Optional<PendingFrame> frame_of_this_context) {
+ apply_context_update_result(nested_context_id, nested_context, nested_result);
+ nested_context.request_display_ticks_if_needed(now);
+ // Node ids are only unique within the process that made them.
+ bool ids_are_of_this_process = nested_context.is_owned_by(context.web_content_client_id());
+ return ContextState::ContextUpdateResult {
+ .accepted = nested_result.accepted,
+ .frame_to_present = move(frame_of_this_context),
+ .scrollbar_dragged_by_compositor = nested_result.scrollbar_dragged_by_compositor,
+ .routing_decision = nested_result.routing_decision,
+ .routed_scroller = ids_are_of_this_process ? nested_result.routed_scroller : Optional<Web::AsyncScrollNodeStableID> {},
+ .nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext,
+ };
+ };
+
+ auto send_to_context_with_captured_scrollbar = [&](Web::MouseEvent const& event_to_send) {
+ auto nested_context_id = *pointer.context_with_captured_scrollbar;
+ auto position = Gfx::FloatPoint {
+ static_cast<float>(event_to_send.position.x().value()),
+ static_cast<float>(event_to_send.position.y().value()),
+ };
+ // A leave or a cancel has no position of its own.
+ if (first_is_one_of(event_to_send.type, Type::MouseDown, Type::MouseMove, Type::MouseUp)) {
+ if (auto position_in_nested_context = position_in_nested_context_ignoring_clips(context_id, context, *context_with_captured_scrollbar, position); position_in_nested_context.has_value())
+ pointer.last_position_in_context_with_captured_scrollbar = *position_in_nested_context;
+ }
+ auto nested_result = context_with_captured_scrollbar->handle_mouse_event_from_containing_context(event_to_send, pointer.last_position_in_context_with_captured_scrollbar, now);
+ auto result = result_of_nested_context(nested_context_id, *context_with_captured_scrollbar, nested_result, {});
+ if (!context_with_captured_scrollbar->has_captured_scrollbar()) {
+ pointer.context_with_captured_scrollbar.clear();
+ context_with_captured_scrollbar = nullptr;
+ pointer.context_under_pointer = nested_context_id;
+ }
+ return result;
+ };
+
+ // The scrollbar the pointer was over in the nested context it left is no longer hovered.
+ auto note_context_under_pointer = [&](Optional<Web::CompositorContextId> nested_context_id) {
+ if (pointer.context_under_pointer == nested_context_id)
+ return;
+ if (auto* context_left = context_still_nested_in(context_id, pointer.context_under_pointer)) {
+ context_left->note_pointer_position({});
+ if (auto frame_to_present = context_left->clear_scrollbar_hover(); frame_to_present.has_value())
+ request_present(*pointer.context_under_pointer, *frame_to_present);
+ }
+ pointer.context_under_pointer = nested_context_id;
+ };
+
+ switch (event.type) {
+ case Type::MouseDown:
+ case Type::MouseMove: {
+ if (context_with_captured_scrollbar) {
+ if (event.type == Type::MouseMove)
+ return send_to_context_with_captured_scrollbar(event);
+ // A press while a drag is still held means the release of that drag never came, so the drag ends there.
+ auto cancel_event = event.clone_without_browser_data();
+ cancel_event.type = Type::MouseCancel;
+ (void)send_to_context_with_captured_scrollbar(cancel_event);
+ }
+
+ auto result = context.handle_mouse_event(event, now);
+ if (!result.nested_context_mouse_hit.has_value()) {
+ note_context_under_pointer({});
+ return result;
+ }
+
+ auto frame_of_this_context = result.frame_to_present;
+ auto containing_context_id = context_id;
+ auto* containing_context = &context;
+ Gfx::FloatPoint position;
+ while (result.nested_context_mouse_hit.has_value()) {
+ auto hit = *result.nested_context_mouse_hit;
+ // WebContent names the contexts its display lists draw, so a context only reaches those nested in it.
+ auto* nested_context = context_if_present(hit.child_context_id);
+ Optional<Gfx::FloatPoint> position_in_nested_context_content;
+ if (nested_context && nested_context->parent_context_id() == containing_context_id)
+ position_in_nested_context_content = position_in_nested_context(hit, *nested_context);
+ if (!position_in_nested_context_content.has_value()) {
+ note_context_under_pointer({});
+ auto declined = ContextState::ContextUpdateResult::declined(Web::InputRoutingDecision::NestedContextUnavailable);
+ declined.frame_to_present = frame_of_this_context;
+ declined.nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext;
+ return declined;
+ }
+ if (containing_context != &context) {
+ apply_context_update_result(containing_context_id, *containing_context, result);
+ containing_context->request_display_ticks_if_needed(now);
+ }
+ containing_context_id = hit.child_context_id;
+ containing_context = nested_context;
+ position = *position_in_nested_context_content;
+ result = nested_context->handle_mouse_event_from_containing_context(event, position, now);
+ }
+
+ note_context_under_pointer(containing_context_id);
+ if (event.type == Type::MouseDown && containing_context->has_captured_scrollbar()) {
+ pointer.context_with_captured_scrollbar = containing_context_id;
+ pointer.last_position_in_context_with_captured_scrollbar = position;
+ }
+ return result_of_nested_context(containing_context_id, *containing_context, result, move(frame_of_this_context));
+ }
+ case Type::MouseUp:
+ if (context_with_captured_scrollbar)
+ return send_to_context_with_captured_scrollbar(event);
+ return context.handle_mouse_event(event, now);
+ case Type::MouseLeave:
+ case Type::MouseCancel: {
+ // The pointer leaves every nested context it was in, and a cancel ends the drag of one, as in this context.
+ Optional<ContextState::ContextUpdateResult> result_of_context_with_captured_scrollbar;
+ Optional<Web::CompositorContextId> context_with_captured_scrollbar_id;
+ if (context_with_captured_scrollbar) {
+ context_with_captured_scrollbar_id = pointer.context_with_captured_scrollbar;
+ result_of_context_with_captured_scrollbar = send_to_context_with_captured_scrollbar(event);
+ }
+ if (auto* context_under_pointer = context_still_nested_in(context_id, pointer.context_under_pointer); context_under_pointer && pointer.context_under_pointer != context_with_captured_scrollbar_id) {
+ auto nested_result = context_under_pointer->handle_mouse_event_from_containing_context(event, {}, now);
+ apply_context_update_result(*pointer.context_under_pointer, *context_under_pointer, nested_result);
+ context_under_pointer->request_display_ticks_if_needed(now);
+ }
+ pointer.context_under_pointer.clear();
+
+ auto result = context.handle_mouse_event(event, now);
+ if (!result_of_context_with_captured_scrollbar.has_value())
+ return result;
+ result_of_context_with_captured_scrollbar->frame_to_present = result.frame_to_present;
+ return result_of_context_with_captured_scrollbar.release_value();
+ }
+ case Type::MouseWheel:
+ return context.handle_mouse_event(event, now);
+ }
+ VERIFY_NOT_REACHED();
+}
+
+// Whether a context nested in the context `context_id` scrolled since the last frame its WebContent presented: the lane
+// of the context's document hands a pointer move to the lane of a same-process iframe, which hit tests it in the iframe.
+bool CompositorCore::nested_context_scrolled_since_last_frame(Web::CompositorContextId context_id) const
+{
+ for (auto const& entry : m_contexts) {
+ if (entry.value->scrolled_since_last_frame() && is_nested_in(context_id, *entry.value))
+ return true;
+ }
+ return false;
+}
+
+bool CompositorCore::is_nested_in(Web::CompositorContextId containing_context_id, ContextState const& context) const
+{
+ // A walk up the parents that outlasts the number of contexts went around a loop.
+ auto const* ancestor = &context;
+ for (size_t remaining_ancestors = m_contexts.size(); ancestor && remaining_ancestors > 0; --remaining_ancestors) {
+ auto parent_context_id = ancestor->parent_context_id();
+ if (!parent_context_id.has_value())
+ return false;
+ if (*parent_context_id == containing_context_id)
+ return true;
+ ancestor = context_if_present(*parent_context_id);
+ }
+ return false;
+}
+
+ContextState::ContextUpdateResult CompositorCore::scroll_for_wheel_step(Web::CompositorContextId context_id, ContextState& context, Web::MouseEvent const& event, WheelStepOrigin origin, bool is_first_wheel_event_with_delta, Compositing::AsyncScrollOperationTracking operation_tracking, MonotonicTime now)
+{
+ auto gesture_id = context.wheel_gesture_id();
+ auto listener_context_id = context.wheel_event_listener_context().value_or(context_id);
+ auto* listener_context = listener_context_id == context_id ? &context : context_still_nested_in(context_id, listener_context_id);
+ auto listener_gate = listener_context
+ ? listener_context->wheel_event_listener_gate(gesture_id)
+ : ContextState::WheelEventListenerGate { .reason_to_hold_latched_steps_back = Web::InputRoutingDecision::NestedContextUnavailable };
+
+ ContextState::ContextUpdateResult result;
+ bool presents_frame = false;
+ if (listener_context && listener_context != &context && listener_gate.page_let_the_gesture_through && !context.has_wheel_latch_for_gesture(gesture_id)) {
+ // The page of a nested context let the gesture through while nothing took its steps, so they go to that context
+ // again rather than to the page.
+ auto step = ContextState::WheelStep::for_wheel_event(event, gesture_id, operation_tracking);
+ auto position = position_in_nested_context_ignoring_clips(context_id, context, *listener_context, step.position);
+ if (!position.has_value())
+ return ContextState::ContextUpdateResult::declined(Web::InputRoutingDecision::NestedContextUnavailable);
+ step.position = *position;
+ auto applied_step = route_wheel_step_the_page_let_through(context_id, context, listener_context_id, *listener_context, step, {}, {}, now);
+ result = move(applied_step.result);
+ result.nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext;
+ presents_frame = applied_step.presents_frame;
+ } else {
+ switch (origin) {
+ case WheelStepOrigin::UI:
+ result = context.handle_wheel_event(event, is_first_wheel_event_with_delta, listener_gate, now);
+ break;
+ case WheelStepOrigin::Fling:
+ result = context.scroll_for_fling_step(event, listener_gate, now);
+ break;
+ case WheelStepOrigin::MainThread:
+ result = context.scroll_for_wheel_event_from_main_thread(event, is_first_wheel_event_with_delta, listener_gate, operation_tracking, now);
+ break;
+ }
+ presents_frame = result.frame_to_present.has_value();
+ if (result.nested_context_wheel_step.has_value()) {
+ auto applied_step = hand_wheel_step_to_nested_context(context_id, context, event, *result.nested_context_wheel_step, listener_gate, operation_tracking, now);
+ result = move(applied_step.result);
+ presents_frame = applied_step.presents_frame;
+ }
+ }
+
+ // If the compositor accepts a momentum step of the fling but does not scroll, the scroller is at its edge.
+ // Alternatively, a snap scroll uses the step. The next display tick ends the fling.
+ if (origin == WheelStepOrigin::Fling && event.scroll_gesture_phase == Web::ScrollGesturePhase::Momentum && result.accepted && !presents_frame)
+ context.end_scroll_fling_on_next_step();
+ return result;
+}
+
+CompositorCore::AppliedWheelStep CompositorCore::apply_wheel_step_result(Web::CompositorContextId context_id, ContextState& context, ContextState::ContextUpdateResult const& result, WebContentClientId reporting_client, MonotonicTime now)
+{
+ apply_context_update_result(context_id, context, result);
+ context.request_display_ticks_if_needed(now);
+ // Node ids are only unique within the process that made them, and only that process follows the operations of the
+ // context.
+ bool ids_are_of_reporting_process = context.is_owned_by(reporting_client);
+ return {
+ .result = {
+ .accepted = result.accepted,
+ .routing_decision = result.routing_decision,
+ .routed_scroller = ids_are_of_reporting_process ? result.routed_scroller : Optional<Web::AsyncScrollNodeStableID> {},
+ .tracked_scroll_operation = ids_are_of_reporting_process ? result.tracked_scroll_operation : Optional<ContextState::TrackedScrollOperation> {},
+ },
+ .presents_frame = result.frame_to_present.has_value(),
+ };
+}
+
+CompositorCore::AppliedWheelStep CompositorCore::hand_wheel_step_to_nested_context(Web::CompositorContextId context_id, ContextState& context, Web::MouseEvent const& event, ContextState::NestedContextWheelStep const& nested_step, ContextState::WheelEventListenerGate const& listener_gate, Compositing::AsyncScrollOperationTracking operation_tracking, MonotonicTime now)
+{
+ auto step = ContextState::WheelStep::for_wheel_event(event, context.wheel_gesture_id(), operation_tracking);
+
+ if (!nested_step.hit.has_value()) {
+ auto* nested_context = context_if_present(nested_step.context_id);
+ if (!nested_context || !is_nested_in(context_id, *nested_context)) {
+ context.forget_wheel_gesture_latch();
+ return { ContextState::ContextUpdateResult::declined(Web::InputRoutingDecision::NestedContextUnavailable) };
+ }
+ // A blocking listener added during the gesture holds its later steps back until the page lets one through.
+ if (listener_gate.reason_to_hold_latched_steps_back.has_value())
+ return { ContextState::ContextUpdateResult::declined(*listener_gate.reason_to_hold_latched_steps_back) };
+ auto admission = listener_gate.page_let_the_gesture_through ? ContextState::WheelStepAdmission::LetThroughByThePage : ContextState::WheelStepAdmission::ByThisPage;
+ auto nested_result = nested_context->take_latched_wheel_step_from_containing_context(step, admission, now);
+ if (nested_result.routing_decision == Web::InputRoutingDecision::NestedContextUnavailable)
+ context.forget_wheel_gesture_latch();
+ return apply_wheel_step_result(nested_step.context_id, *nested_context, nested_result, context.web_content_client_id(), now);
+ }
+
+ auto first_step = first_wheel_step_in_nested_contexts(context_id, *nested_step.hit, step.delta_in_css_pixels, step.wheel_delta_precision);
+ context.set_wheel_event_listener_context(first_step.innermost_context_id);
+ auto& taking_context = first_step.taking_context;
+ if (!taking_context.has_value()) {
+ auto result = context.scroll_for_wheel_event_passed_on_by_nested_context(event, operation_tracking, now);
+ result.nested_context_hand_off = Web::InputRoutingDecision::NestedContextPassedStepOn;
+ return { .result = result, .presents_frame = result.frame_to_present.has_value() };
+ }
+
+ AppliedWheelStep applied_step;
+ if (taking_context->context && taking_context->route.kind != ContextState::FirstWheelStepRoute::Kind::Declined) {
+ step.position = taking_context->position;
+ auto nested_result = taking_context->context->take_first_wheel_step_from_containing_context(taking_context->route, step, now);
+ applied_step = apply_wheel_step_result(taking_context->context_id, *taking_context->context, nested_result, context.web_content_client_id(), now);
+ context.latch_wheel_gesture_to_nested_context(taking_context->context_id, applied_step.result.routed_scroller, step.scroll_gesture_phase);
+ } else {
+ applied_step.result = ContextState::ContextUpdateResult::declined(taking_context->route.decision);
+ }
+ applied_step.result.nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext;
+ return applied_step;
+}
+
+CompositorCore::FirstWheelStepInNestedContexts CompositorCore::first_wheel_step_in_nested_contexts(Web::CompositorContextId context_id, Compositing::NestedContextHit hit, Gfx::FloatPoint delta_in_css_pixels, Web::WheelDeltaPrecision wheel_delta_precision)
+{
+ using Kind = ContextState::FirstWheelStepRoute::Kind;
+ // The contexts the step went into, outermost first, each nested in the one before.
+ Vector<NestedContextTakingWheelStep, 4> contexts;
+ auto containing_context_id = context_id;
+ while (true) {
+ auto declined_by_unavailable_context = [&] {
+ return FirstWheelStepInNestedContexts {
+ .innermost_context_id = hit.child_context_id,
+ .taking_context = NestedContextTakingWheelStep { .context_id = hit.child_context_id, .route = { Kind::Declined, Web::InputRoutingDecision::NestedContextUnavailable } },
+ };
+ };
+ // WebContent names the contexts its display lists draw, so a context only reaches those nested in it.
+ auto* nested_context = context_if_present(hit.child_context_id);
+ if (!nested_context || nested_context->parent_context_id() != containing_context_id)
+ return declined_by_unavailable_context();
+ auto position = position_in_nested_context(hit, *nested_context);
+ if (!position.has_value())
+ return declined_by_unavailable_context();
+ auto route = nested_context->route_first_wheel_step_from_containing_context(*position, delta_in_css_pixels, wheel_delta_precision, Compositing::NestedContextRegions::HandToNestedContext, ContextState::WheelStepAdmission::ByThisPage);
+ contexts.append({ .context_id = hit.child_context_id, .context = nested_context, .position = *position, .route = route });
+ if (route.kind != Kind::NestedContext)
+ break;
+ containing_context_id = hit.child_context_id;
+ hit = *route.nested_context_hit;
+ }
+
+ // What a nested context passes on goes to whatever is painted under it in the context it is nested in, whose page
+ // does not see the step.
+ auto innermost_context_id = contexts.last().context_id;
+ while (contexts.last().route.kind == Kind::PassedOn) {
+ contexts.take_last();
+ if (contexts.is_empty())
+ return { .innermost_context_id = innermost_context_id };
+ auto& containing_context = contexts.last();
+ containing_context.route = containing_context.context->route_first_wheel_step_from_containing_context(containing_context.position, delta_in_css_pixels, wheel_delta_precision, Compositing::NestedContextRegions::Ignore, ContextState::WheelStepAdmission::PassedOnByNestedContext);
+ }
+ return { .innermost_context_id = innermost_context_id, .taking_context = contexts.take_last() };
+}
+
+// The page of one process scrolls a context of another process only by what the UI left to the page in the gesture, so
+// that a page cannot scroll the document around it at will.
+static bool page_may_scroll_in(ContextState const& context, ContextState& routing_context, Optional<WebContentClientId> page_client, ContextState::WheelStep const& step)
+{
+ return !page_client.has_value() || context.is_owned_by(*page_client) || routing_context.take_wheel_delta_left_to_the_page(step.delta_in_css_pixels);
+}
+
+CompositorCore::AppliedWheelStep CompositorCore::route_wheel_step_the_page_let_through(Web::CompositorContextId routing_context_id, ContextState& routing_context, Web::CompositorContextId listener_context_id, ContextState& listener_context, ContextState::WheelStep step, ReadonlySpan<Web::AsyncScrollNodeStableID> scroll_chain, Optional<WebContentClientId> page_client, MonotonicTime now)
+{
+ using Kind = ContextState::FirstWheelStepRoute::Kind;
+ auto const& reporting_client = page_client.has_value() ? *page_client : routing_context.web_content_client_id();
+ auto declined = [](Web::InputRoutingDecision decision) {
+ return AppliedWheelStep { .result = ContextState::ContextUpdateResult::declined(decision) };
+ };
+
+ auto context_id = listener_context_id;
+ auto* context = &listener_context;
+ // A walk up the parents that outlasts the number of contexts went around a loop.
+ for (size_t remaining_contexts = m_contexts.size(); remaining_contexts > 0; --remaining_contexts) {
+ if (context == &routing_context) {
+ if (!page_may_scroll_in(routing_context, routing_context, page_client, step))
+ return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
+ auto result = routing_context.scroll_for_first_wheel_step_the_page_let_through(step, scroll_chain, now);
+ return apply_wheel_step_result(routing_context_id, routing_context, result, reporting_client, now);
+ }
+
+ auto route = context->route_first_wheel_step_from_containing_context(step.position, step.delta_in_css_pixels, step.wheel_delta_precision, Compositing::NestedContextRegions::Ignore, ContextState::WheelStepAdmission::LetThroughByThePage, scroll_chain);
+ if (first_is_one_of(route.kind, Kind::ScrollNode, Kind::Kept)) {
+ if (!page_may_scroll_in(*context, routing_context, page_client, step))
+ return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
+ auto nested_result = context->take_first_wheel_step_from_containing_context(route, step, now);
+ auto latched_scroller = context->is_owned_by(routing_context.web_content_client_id()) ? nested_result.routed_scroller : Optional<Web::AsyncScrollNodeStableID> {};
+ routing_context.latch_wheel_gesture_to_nested_context(context_id, latched_scroller, step.scroll_gesture_phase);
+ return apply_wheel_step_result(context_id, *context, nested_result, reporting_client, now);
+ }
+ if (route.kind != Kind::PassedOn)
+ return declined(route.decision);
+
+ // What a nested context passes on goes to whatever is painted under it in the context it is nested in.
+ auto parent_context_id = context->parent_context_id();
+ auto* parent_context = parent_context_id.has_value() ? context_if_present(*parent_context_id) : nullptr;
+ if (!parent_context)
+ return declined(Web::InputRoutingDecision::NestedContextUnavailable);
+ auto position = parent_context->position_of_nested_context_position(context_id, step.position, context->viewport_size());
+ if (!position.has_value())
+ return declined(Web::InputRoutingDecision::NestedContextUnavailable);
+ step.position = *position;
+ scroll_chain = {};
+ context_id = *parent_context_id;
+ context = parent_context;
+ }
+ return declined(Web::InputRoutingDecision::NestedContextUnavailable);
+}
+
+Optional<Web::CompositorContextId> CompositorCore::routing_context_of_wheel_gesture(Web::CompositorContextId context_id, u64 gesture_id)
+{
+ // A walk up the parents that outlasts the number of contexts went around a loop.
+ Optional<Web::CompositorContextId> candidate_context_id = context_id;
+ for (size_t remaining_contexts = m_contexts.size(); candidate_context_id.has_value() && remaining_contexts > 0; --remaining_contexts) {
+ auto* candidate_context = context_if_present(*candidate_context_id);
+ if (!candidate_context)
+ return {};
+ if (candidate_context->wheel_gesture_id() == gesture_id)
+ return candidate_context_id;
+ candidate_context_id = candidate_context->parent_context_id();
+ }
+ return {};
+}
+
+void CompositorCore::dispatch_scroll_fling_step(Web::CompositorContextId context_id, ContextState& context, Optional<Web::MouseEvent> event, MonotonicTime now)
+{
+ // Send each fling step on the same route as a wheel event from the UI.
+ if (!event.has_value())
+ return;
+ auto result = scroll_for_wheel_step(context_id, context, *event, WheelStepOrigin::Fling, false, Compositing::AsyncScrollOperationTracking::No, now);
+ if (result.nested_context_hand_off.has_value())
+ context.input_trace().record({ .input_event_id = event->id, .stage = Web::InputTraceStage::Compositor, .decision = *result.nested_context_hand_off });
+ if (result.routing_decision.has_value())
+ context.input_trace().record({ .input_event_id = event->id, .stage = Web::InputTraceStage::Compositor, .decision = *result.routing_decision, .scroller = result.routed_scroller });
+ event->async_scroll_performed_default_action = apply_context_update_result(context_id, context, result);
+ if (!event->async_scroll_performed_default_action)
+ context.note_wheel_event_left_to_the_page(*event);
+ context.request_display_ticks_if_needed(now);
+ context.dispatch_mouse_event_to_web_content(*event);
+}
+
+// A pinch is a ctrl+wheel event first, to the document under it, whose blocking wheel event listeners may cancel it
+// before anything zooms. Only the documents at the position can see that event.
+bool CompositorCore::page_under_pinch_may_cancel_it(Web::CompositorContextId context_id, ContextState& context, Gfx::FloatPoint position)
+{
+ auto containing_context_id = context_id;
+ auto* containing_context = &context;
+ // A walk down the nested contexts that outlasts the number of contexts went around a loop.
+ for (size_t remaining_contexts = m_contexts.size(); remaining_contexts > 0; --remaining_contexts) {
+ if (containing_context->page_may_cancel_pinch_at(position))
+ return true;
+ auto hit = containing_context->nested_context_for_wheel_at(position);
+ if (!hit.has_value())
+ return false;
+ // NB: Nothing is known yet of the listeners of a context that is not available.
+ auto* nested_context = context_if_present(hit->child_context_id);
+ if (!nested_context || nested_context->parent_context_id() != containing_context_id)
+ return true;
+ auto position_in_nested = position_in_nested_context(*hit, *nested_context);
+ if (!position_in_nested.has_value())
+ return true;
+ containing_context_id = hit->child_context_id;
+ containing_context = nested_context;
+ position = *position_in_nested;
+ }
+ return true;
+}
+
+void CompositorCore::handle_and_dispatch_pinch_event(Web::CompositorContextId context_id, Web::PinchEvent event)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ // NB: The presentation connection only passes on input for the contexts bound to it.
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+ VERIFY(context->bound_presentation().has_value());
+ auto presentation_id = *context->bound_presentation();
+ if (!context->can_dispatch_input_to_web_content()) {
+ post_to_presentation<Messages::CompositorPresentationClient::DidNotDispatchInputEvent>(presentation_id, context_id, event.id);
+ return;
+ }
+
+ // The first step of a gesture decides for the steps after it: a gesture the page let through zooms from here on,
+ // whatever listeners have appeared since, and any other leaves each step to the page, wherever the pinch has moved.
+ // A page that already let this step through leaves only the zoom to do.
+ Gfx::FloatPoint position { static_cast<float>(event.position.x().value()), static_cast<float>(event.position.y().value()) };
+ auto may_zoom = event.continues_gesture ? context->pinch_gesture_was_let_through() : !page_under_pinch_may_cancel_it(context_id, *context, position);
+ if (event.wheel_event_was_dispatched || may_zoom)
+ event.async_zoom_performed_default_action = apply_context_update_result(context_id, *context, context->handle_pinch_event(event));
+ context->set_pinch_gesture_was_let_through(event.async_zoom_performed_default_action);
+
+ // The scroll updates the event produced reach WebContent ahead of the event itself on the same connection.
+ publish_pending_async_scroll_updates(context_id, *context);
+ context->dispatch_pinch_event_to_web_content(event);
+ post_to_presentation<Messages::CompositorPresentationClient::DidDispatchInputEvent>(presentation_id, context_id, event.id, event.async_zoom_performed_default_action);
+}
+
+Compositing::WheelEventAnswer CompositorCore::wheel_event_answer_for(AppliedWheelStep const& applied_step, Optional<Web::WheelGestureMark> wheel_gesture_mark)
+{
+ auto const& tracked_scroll_operation = applied_step.result.tracked_scroll_operation;
+ return {
+ .performed_default_action = applied_step.result.accepted,
+ .wheel_gesture_mark = wheel_gesture_mark,
+ .operation_id = tracked_scroll_operation.map([](auto const& operation) { return operation.operation_id; }),
+ .operation_context_id = tracked_scroll_operation.map([](auto const& operation) { return operation.context_id; }),
+ .routing_decision = applied_step.result.routing_decision,
+ .routed_scroller = applied_step.result.routed_scroller,
+ };
+}
+
+Compositing::WheelEventAnswer CompositorCore::take_wheel_event_from_main_thread(Web::CompositorContextId context_id, Web::MouseEvent const& event, Compositing::AsyncScrollOperationTracking operation_tracking, Compositing::WheelEventRouting routing, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+
+ auto noted = context->note_wheel_event_from_main_thread(event, now);
+ if (routing == Compositing::WheelEventRouting::AfterDispatch)
+ return { .wheel_gesture_mark = noted.wheel_gesture_mark };
+ auto result = scroll_for_wheel_step(context_id, *context, event, WheelStepOrigin::MainThread, noted.is_first_wheel_event_with_delta, operation_tracking, now);
+ return wheel_event_answer_for(apply_wheel_step_result(context_id, *context, result, context->web_content_client_id(), now), noted.wheel_gesture_mark);
+}
+
+Compositing::WheelEventAnswer CompositorCore::scroll_for_wheel_event_the_page_let_through(Web::CompositorContextId listener_context_id, Compositing::WheelStepLetThrough const& let_through, Compositing::AsyncScrollOperationTracking operation_tracking, WebContentClientId page_client, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* listener_context = context_if_present(listener_context_id);
+ VERIFY(listener_context);
+ if (listener_context->document_id() != let_through.document_id)
+ return { .routing_decision = Web::InputRoutingDecision::ScrollerInOtherDocument };
+
+ auto gesture_id = let_through.wheel_gesture_mark.gesture_id;
+ // A step that moves along no axis goes to the page as no wheel event, so the page had no say in letting it through,
+ // and it decides nothing for the steps after it.
+ if (!let_through.delta_in_css_pixels.is_zero())
+ listener_context->note_page_let_wheel_gesture_through(gesture_id);
+ ContextState::WheelStep step {
+ .position = let_through.position,
+ .delta_in_css_pixels = let_through.delta_in_css_pixels,
+ .wheel_delta_precision = let_through.wheel_delta_precision,
+ .scroll_gesture_phase = let_through.scroll_gesture_phase,
+ .gesture_id = gesture_id,
+ .operation_tracking = operation_tracking,
+ };
+ auto scroll_chain = let_through.scroll_chain.span();
+ auto answer = [&](AppliedWheelStep const& applied_step) { return wheel_event_answer_for(applied_step, let_through.wheel_gesture_mark); };
+ auto declined = [&](Web::InputRoutingDecision decision) { return answer({ .result = ContextState::ContextUpdateResult::declined(decision) }); };
+
+ // A step of a gesture that the contexts around the page have moved past still scrolls, as the page let it.
+ auto routing_context_id = routing_context_of_wheel_gesture(listener_context_id, gesture_id);
+ auto* routing_context = routing_context_id.has_value() ? context_if_present(*routing_context_id) : nullptr;
+ if (!routing_context || routing_context->wheel_step_was_overtaken(step.scroll_gesture_phase, let_through.wheel_gesture_mark.step_number)) {
+ auto result = listener_context->scroll_for_overtaken_wheel_step(step, scroll_chain, now);
+ return answer(apply_wheel_step_result(listener_context_id, *listener_context, result, page_client, now));
+ }
+
+ routing_context->set_wheel_event_listener_context(listener_context_id);
+ // The end of a gesture lets go of the scroller it stretched.
+ if (step.scroll_gesture_phase == Web::ScrollGesturePhase::Ended)
+ routing_context->end_wheel_gesture_stretch(now);
+
+ // A latched scroller that the page no longer has around the target of the step is gone from the page, and the step
+ // is routed afresh.
+ bool latch_is_to_scroller_of_this_page = *routing_context_id == listener_context_id && !routing_context->nested_context_of_wheel_latch().has_value();
+ if (latch_is_to_scroller_of_this_page && !scroll_chain.is_empty()) {
+ if (auto latched_scroller = routing_context->wheel_latched_scroller(); latched_scroller.has_value() && !scroll_chain.contains_slow(*latched_scroller))
+ routing_context->forget_wheel_latch_of_gesture(gesture_id);
+ }
+
+ if (routing_context->has_wheel_latch_for_gesture(gesture_id)) {
+ if (auto nested_context_id = routing_context->nested_context_of_wheel_latch(); nested_context_id.has_value()) {
+ auto* nested_context = context_still_nested_in(*routing_context_id, nested_context_id);
+ if (!nested_context) {
+ routing_context->forget_wheel_latch_of_gesture(gesture_id);
+ return declined(Web::InputRoutingDecision::NestedContextUnavailable);
+ }
+ if (!page_may_scroll_in(*nested_context, *routing_context, page_client, step))
+ return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
+ auto nested_result = nested_context->take_latched_wheel_step_from_containing_context(step, ContextState::WheelStepAdmission::LetThroughByThePage, now);
+ return answer(apply_wheel_step_result(*nested_context_id, *nested_context, nested_result, page_client, now));
+ }
+ if (!page_may_scroll_in(*routing_context, *routing_context, page_client, step))
+ return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
+ if (auto result = routing_context->scroll_for_latched_wheel_step_the_page_let_through(step, now); result.has_value())
+ return answer(apply_wheel_step_result(*routing_context_id, *routing_context, *result, page_client, now));
+ }
+
+ return answer(route_wheel_step_the_page_let_through(*routing_context_id, *routing_context, listener_context_id, *listener_context, step, scroll_chain, page_client, now));
+}
+
+void CompositorCore::forget_wheel_gesture_the_page_canceled(Web::CompositorContextId listener_context_id, u64 gesture_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* listener_context = context_if_present(listener_context_id);
+ if (!listener_context)
+ return;
+ listener_context->forget_page_let_wheel_gesture_through(gesture_id);
+ // The listener scrolls by itself, so the next step of the gesture is routed afresh.
+ if (auto routing_context_id = routing_context_of_wheel_gesture(listener_context_id, gesture_id); routing_context_id.has_value())
+ context_if_present(*routing_context_id)->forget_wheel_latch_of_gesture(gesture_id);
+}
+
+static bool scroll_request_smoothly_scrolls_for_script(Compositing::ScrollRequest const& scroll_request)
+{
+ return scroll_request.smooth_scroll.has_value() && scroll_request.smooth_scroll->initiator == Compositing::SmoothScrollInitiator::Programmatic;
+}
+
+void CompositorCore::apply_scroll_requests(Web::CompositorContextId context_id, ReadonlySpan<Compositing::ScrollRequest> scroll_requests, MonotonicTime now)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ if (!context)
+ return;
+
+ // A script's smooth scroll of a box ends the momentum scrolling it, as a key's does. A script's instant scroll only
+ // moves the box, and the momentum goes on from there: pages keep their content in place under a flick that way.
+ for (auto const& scroll_request : scroll_requests) {
+ if (scroll_request.takes_over_from_momentum || scroll_request_smoothly_scrolls_for_script(scroll_request))
+ dispatch_scroll_fling_step(context_id, *context, context->end_momentum_scrolling(scroll_request.stable_node_id), now);
+ }
+ if (auto frame_to_present = context->apply_scroll_requests(scroll_requests, now); frame_to_present.has_value())
+ request_present(context_id, frame_to_present.value());
+ context->request_display_ticks_if_needed(now);
+ publish_pending_async_scroll_updates(context_id, *context);
+}
+
+Compositing::PendingAsyncScrollUpdates CompositorCore::take_pending_async_scroll_updates(Web::CompositorContextId context_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ VERIFY(context);
+
+ return context->take_pending_async_scroll_updates();
+}
+
+void CompositorCore::publish_pending_async_scroll_updates(Web::CompositorContextId, ContextState& context)
+{
+ context.publish_pending_async_scroll_updates();
+}
+
+void CompositorCore::request_present(Web::CompositorContextId context_id, PendingFrame const& frame)
+{
+ m_present_requests.append({ context_id, frame.viewport_rect, frame.forced_damage_rect });
+}
+
+DisplayTickResult CompositorCore::display_tick(Web::CompositorContextId context_id, MonotonicTime frame_time, MonotonicTime now, DisplayTickInputs const& inputs)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context_pointer = context_if_present(context_id);
+ VERIFY(context_pointer);
+ auto& context = *context_pointer;
+
+ auto has_active_animation_on_display = (context.needs_animation_frames(now) || inputs.ui_overlay_fades_need_frame) && inputs.context_is_on_tick_display;
+ auto presents_on_display = inputs.present_is_scheduled_on_tick_display || has_active_animation_on_display;
+ if (presents_on_display) {
+ if (inputs.context_is_on_tick_display)
+ dispatch_scroll_fling_step(context_id, context, context.take_scroll_fling_step(frame_time), frame_time);
+ if (auto animation_frame = context.advance_smooth_scroll_animations(frame_time); animation_frame.has_value())
+ m_present_requests.append({ context_id, *animation_frame, {}, PresentScheduling::WithDisplayTickInProgress });
+ if (auto rebound_frame = context.advance_elastic_overscroll_rebounds(frame_time); rebound_frame.has_value())
+ m_present_requests.append({ context_id, *rebound_frame, {}, PresentScheduling::WithDisplayTickInProgress });
+ if (auto damage_rect = context.advance_scrollbar_fades_and_return_damage_rect(frame_time); !damage_rect.is_empty())
+ m_present_requests.append({ context_id, {}, damage_rect, PresentScheduling::WithDisplayTickInProgress });
+ }
+
+ // A render clock tick goes to the process's render clock thread, beside what its main thread is doing, with
+ // where the compositor has scrolled to for this vsync, which that main thread may not have taken in yet.
+ if (inputs.clock_tick_interval_in_milliseconds.has_value())
+ context.post_clock_tick(frame_time, *inputs.clock_tick_interval_in_milliseconds);
+
+ if (presents_on_display) {
+ publish_pending_async_scroll_updates(context_id, context);
+ if (context.visual_animations_need_frame(now)) {
+ context.advance_visual_animations(frame_time);
+ m_present_requests.append({ context_id, {}, {}, PresentScheduling::WithDisplayTickInProgress });
+ }
+ }
+ return { .presents_on_display = presents_on_display, .needs_animation_frames = context.needs_animation_frames(now) };
+}
+
+ContextState* CompositorCore::context_if_present(Web::CompositorContextId context_id)
+{
+ auto it = m_contexts.find(context_id);
+ if (it == m_contexts.end())
+ return nullptr;
+ return it->value.ptr();
+}
+
+ContextState const* CompositorCore::context_if_present(Web::CompositorContextId context_id) const
+{
+ auto it = m_contexts.find(context_id);
+ if (it == m_contexts.end())
+ return nullptr;
+ return it->value.ptr();
+}
+
+String CompositorCore::scroll_state_for_testing(Web::CompositorContextId context_id, Web::AsyncScrollNodeStableID stretched_scroller) const
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto const* context = context_if_present(context_id);
+ if (!context)
+ return {};
+ return context->scroll_state_for_testing(stretched_scroller);
+}
+
+Optional<Compositing::FirstWheelStepRouteForTesting> CompositorCore::route_first_wheel_step_for_testing(Web::CompositorContextId context_id, Gfx::FloatPoint position, Gfx::FloatPoint delta_in_css_pixels)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ if (!context)
+ return {};
+ if (!context->presents_to_client() || !context->may_hand_first_wheel_steps_to_nested_contexts())
+ return context->route_first_wheel_step_for_testing(position, delta_in_css_pixels, Compositing::NestedContextRegions::LeaveToMainThread);
+ auto hit = context->nested_context_for_wheel_at(position);
+ if (!hit.has_value())
+ return context->route_first_wheel_step_for_testing(position, delta_in_css_pixels, Compositing::NestedContextRegions::HandToNestedContext);
+ auto taking_context = first_wheel_step_in_nested_contexts(context_id, *hit, delta_in_css_pixels, Web::WheelDeltaPrecision::Precise).taking_context;
+ if (!taking_context.has_value())
+ return context->route_first_wheel_step_for_testing(position, delta_in_css_pixels, Compositing::NestedContextRegions::Ignore);
+ // Node ids are only unique within the process that made them.
+ bool ids_are_of_this_process = taking_context->context && taking_context->context->is_owned_by(context->web_content_client_id());
+ Compositing::FirstWheelStepRouteForTesting route { .route = { taking_context->route.decision }, .document_id = {} };
+ if (ids_are_of_this_process) {
+ route.route.scroller = taking_context->route.scroller;
+ route.document_id = taking_context->context->document_id();
+ }
+ return route;
+}
+
+void CompositorCore::begin_input_trace_for_testing(Web::CompositorContextId context_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ if (auto* context = context_if_present(context_id))
+ context->input_trace().begin();
+}
+
+void CompositorCore::clear_per_test_input_state_for_testing(Web::CompositorContextId context_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ if (auto* context = context_if_present(context_id))
+ context->clear_per_test_input_state();
+}
+
+Vector<Web::InputTraceRecord> CompositorCore::take_input_trace_for_testing(Web::CompositorContextId context_id)
+{
+ VERIFY(!m_is_entered);
+ TemporaryChange entered { m_is_entered, true };
+
+ auto* context = context_if_present(context_id);
+ if (!context)
+ return {};
+ return context->input_trace().take();
+}
+
+bool CompositorCore::apply_context_update_result(
+ Web::CompositorContextId context_id,
+ ContextState& context,
+ ContextState::ContextUpdateResult const& result)
+{
+ if (result.frame_to_present.has_value())
+ request_present(context_id, *result.frame_to_present);
+ publish_pending_async_scroll_updates(context_id, context);
+ if (result.should_request_rendering_update)
+ context.request_rendering_update();
+ return result.accepted;
+}
+
+}
diff --git a/Services/Compositor/CompositorCore.h b/Services/Compositor/CompositorCore.h
new file mode 100644
index 00000000000..6dfc374d2af
--- /dev/null
+++ b/Services/Compositor/CompositorCore.h
@@ -0,0 +1,150 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/HashMap.h>
+#include <AK/Noncopyable.h>
+#include <AK/OwnPtr.h>
+#include <AK/Vector.h>
+#include <Compositor/CompositorCoreHost.h>
+#include <Compositor/ContextState.h>
+
+namespace Compositor {
+
+class CompositorCore {
+ AK_MAKE_NONCOPYABLE(CompositorCore);
+ AK_MAKE_NONMOVABLE(CompositorCore);
+
+public:
+ explicit CompositorCore(CompositorCoreHost&);
+
+ enum class ContextOwnerCheckResult : u8 {
+ OwnedByClient,
+ ContextUnavailable,
+ ConflictingOwner,
+ };
+
+ void create_context(Web::CompositorContextId, Optional<u64> page_id, WebContentClientId);
+ // Also leaves the contexts nested in it without a parent.
+ void destroy_context(Web::CompositorContextId);
+ ContextOwnerCheckResult check_context_owner(Web::CompositorContextId, WebContentClientId) const;
+ void set_parent_context(Web::CompositorContextId, Optional<Web::CompositorContextId>);
+ void stop_presenting_to_client(Web::CompositorContextId);
+ // Ends the fling of a context that is hidden, whether or not it was visible.
+ void set_visibility(Web::CompositorContextId, Compositing::ContextVisibility, MonotonicTime now);
+ void viewport_size_updated(Web::CompositorContextId, Gfx::IntSize);
+ void set_paused_debugger_overlay_is_shown(Web::CompositorContextId, bool);
+ void set_bound_presentation(Web::CompositorContextId, Optional<PresentationId>);
+ void set_scroll_capabilities(Web::CompositorContextId, ContextState::ScrollCapabilities const&);
+
+ void install_display_list_update(Web::CompositorContextId, NonnullRefPtr<Compositing::DisplayList>, Compositing::AccumulatedVisualContextTree, Compositing::ScrollStateSnapshot&&, 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, MonotonicTime now);
+
+ bool needs_animation_frames(Web::CompositorContextId, MonotonicTime now);
+ Optional<FrameInputs> frame_inputs(Web::CompositorContextId, MonotonicTime now);
+ ContextState::ScreenshotInputs frame_inputs_for_screenshot(Web::CompositorContextId, MonotonicTime now);
+ DisplayTickResult display_tick(Web::CompositorContextId, MonotonicTime frame_time, MonotonicTime now, DisplayTickInputs const&);
+ void timer_fired(Web::CompositorContextId, ContextTimer, MonotonicTime now);
+
+ // The frames asked for since the last call, in the order they were asked for.
+ Vector<PresentRequest> take_present_requests();
+ void handle_and_dispatch_key_event(Web::CompositorContextId context_id, Web::KeyEvent event, bool allow_keyboard_scroll, MonotonicTime now);
+ void handle_and_dispatch_mouse_event(Web::CompositorContextId context_id, Web::MouseEvent event, MonotonicTime now);
+ void handle_and_dispatch_pinch_event(Web::CompositorContextId context_id, Web::PinchEvent event);
+ Compositing::WheelEventAnswer take_wheel_event_from_main_thread(Web::CompositorContextId context_id, Web::MouseEvent const& event, Compositing::AsyncScrollOperationTracking operation_tracking, Compositing::WheelEventRouting routing, MonotonicTime now);
+ Compositing::WheelEventAnswer scroll_for_wheel_event_the_page_let_through(Web::CompositorContextId listener_context_id, Compositing::WheelStepLetThrough const& let_through, Compositing::AsyncScrollOperationTracking operation_tracking, WebContentClientId page_client, MonotonicTime now);
+ void forget_wheel_gesture_the_page_canceled(Web::CompositorContextId listener_context_id, u64 gesture_id);
+ void apply_scroll_requests(Web::CompositorContextId context_id, ReadonlySpan<Compositing::ScrollRequest> scroll_requests, MonotonicTime now);
+ Compositing::PendingAsyncScrollUpdates take_pending_async_scroll_updates(Web::CompositorContextId context_id);
+ void invalidate_wheel_event_listener_state(Web::CompositorContextId context_id, u64 generation);
+ void invalidate_keyboard_scroll_state(Web::CompositorContextId context_id, u64 generation);
+ void update_scroll_state(Web::CompositorContextId context_id, Compositing::ScrollStateSnapshot&& scroll_state_snapshot, Compositing::KeyboardScrollState keyboard_scroll_state);
+ String scroll_state_for_testing(Web::CompositorContextId context_id, Web::AsyncScrollNodeStableID stretched_scroller) const;
+ Optional<Compositing::FirstWheelStepRouteForTesting> route_first_wheel_step_for_testing(Web::CompositorContextId context_id, Gfx::FloatPoint position, Gfx::FloatPoint delta_in_css_pixels);
+ void begin_input_trace_for_testing(Web::CompositorContextId context_id);
+ void clear_per_test_input_state_for_testing(Web::CompositorContextId context_id);
+ Vector<Web::InputTraceRecord> take_input_trace_for_testing(Web::CompositorContextId context_id);
+
+private:
+ template<typename Message, typename... Arguments>
+ void post_to_presentation(PresentationId presentation_id, Arguments&&... arguments)
+ {
+ auto buffer = MUST(Message::static_encode(forward<Arguments>(arguments)...));
+ m_host.post_message_to_presentation(presentation_id, buffer);
+ }
+
+ bool apply_context_update_result(
+ Web::CompositorContextId,
+ ContextState&,
+ ContextState::ContextUpdateResult const&);
+ enum class WheelStepOrigin : u8 {
+ UI,
+ Fling,
+ MainThread,
+ };
+ // A fling step is scrolled at the frame time the fling synthesized it for.
+ ContextState::ContextUpdateResult scroll_for_wheel_step(Web::CompositorContextId, ContextState&, Web::MouseEvent const&, WheelStepOrigin, bool is_first_wheel_event_with_delta, Compositing::AsyncScrollOperationTracking, MonotonicTime now);
+ // How a wheel step went in a context that presented and published what the step changed itself.
+ struct AppliedWheelStep {
+ // What is left for the context that routes the step to present and report.
+ ContextState::ContextUpdateResult result;
+ bool presents_frame { false };
+ };
+ // A process only knows the ids of its own nodes, and only follows the operations of its own contexts.
+ AppliedWheelStep apply_wheel_step_result(Web::CompositorContextId, ContextState&, ContextState::ContextUpdateResult const&, WebContentClientId reporting_client, MonotonicTime now);
+ static Compositing::WheelEventAnswer wheel_event_answer_for(AppliedWheelStep const&, Optional<Web::WheelGestureMark>);
+ AppliedWheelStep hand_wheel_step_to_nested_context(Web::CompositorContextId, ContextState&, Web::MouseEvent const&, ContextState::NestedContextWheelStep const&, ContextState::WheelEventListenerGate const&, Compositing::AsyncScrollOperationTracking, MonotonicTime now);
+ struct NestedContextTakingWheelStep {
+ Web::CompositorContextId context_id;
+ // Null when the step cannot be handed to the context.
+ ContextState* context { nullptr };
+ Gfx::FloatPoint position {};
+ ContextState::FirstWheelStepRoute route;
+ };
+ struct FirstWheelStepInNestedContexts {
+ // The innermost context that the step went into, whose page sees the step.
+ Web::CompositorContextId innermost_context_id;
+ // The context that takes the step, which is empty when each of them passes the step on.
+ Optional<NestedContextTakingWheelStep> taking_context {};
+ };
+ // Where the first step of a gesture over the content of a context nested in the context `context_id` goes, among
+ // that context and the contexts nested in it.
+ FirstWheelStepInNestedContexts first_wheel_step_in_nested_contexts(Web::CompositorContextId, Compositing::NestedContextHit, Gfx::FloatPoint delta_in_css_pixels, Web::WheelDeltaPrecision);
+ // Routes the first step of a gesture that the page of the listener context let through: in that context, from the
+ // scrollers the page found around the target, and on to the contexts around it for what a context passes on, up to
+ // the routing context, which latches the gesture. A page of another process than a context's scrolls it only by
+ // what the UI left to the page in the gesture.
+ AppliedWheelStep route_wheel_step_the_page_let_through(Web::CompositorContextId routing_context_id, ContextState& routing_context, Web::CompositorContextId listener_context_id, ContextState& listener_context, ContextState::WheelStep, ReadonlySpan<Web::AsyncScrollNodeStableID> scroll_chain, Optional<WebContentClientId> page_client, MonotonicTime now);
+ // The nearest context, from the given one out to the presenting one, that noted the gesture.
+ Optional<Web::CompositorContextId> routing_context_of_wheel_gesture(Web::CompositorContextId, u64 gesture_id);
+ // The scrollbars of a context nested in the presenting context `context_id` take the pointer over that context's
+ // content, and a drag of one of them takes the pointer wherever it goes until the drag ends.
+ ContextState::ContextUpdateResult handle_mouse_event_in_nested_contexts(Web::CompositorContextId, ContextState&, Web::MouseEvent const&, MonotonicTime now);
+ ContextState* context_still_nested_in(Web::CompositorContextId containing_context_id, Optional<Web::CompositorContextId>);
+ // Where the position in the presenting context lands in the nested context, through each context in between and
+ // without regard to where their content is or what clips it.
+ Optional<Gfx::FloatPoint> position_in_nested_context_ignoring_clips(Web::CompositorContextId presenting_context_id, ContextState& presenting_context, ContextState& nested_context, Gfx::FloatPoint position);
+ bool is_nested_in(Web::CompositorContextId containing_context_id, ContextState const&) const;
+ bool page_under_pinch_may_cancel_it(Web::CompositorContextId, ContextState&, Gfx::FloatPoint position);
+ void dispatch_scroll_fling_step(Web::CompositorContextId, ContextState&, Optional<Web::MouseEvent>, MonotonicTime now);
+ ContextState* context_if_present(Web::CompositorContextId);
+ // Hands the context's async scroll updates to its WebContent process as soon as they exist,
+ // so a rendering update reads them locally instead of asking for them over a synchronous call.
+ void publish_pending_async_scroll_updates(Web::CompositorContextId, ContextState&);
+
+ ContextState const* context_if_present(Web::CompositorContextId) const;
+ bool nested_context_scrolled_since_last_frame(Web::CompositorContextId) const;
+ void request_present(Web::CompositorContextId, PendingFrame const&);
+
+ CompositorCoreHost& m_host;
+ HashMap<Web::CompositorContextId, OwnPtr<ContextState>> m_contexts;
+ Vector<PresentRequest> m_present_requests;
+ mutable bool m_is_entered { false };
+};
+
+}
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index ef78239bf20..cda0c609153 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -6,10 +6,8 @@
#include <AK/Math.h>
#include <AK/Random.h>
-#include <AK/ScopeGuard.h>
#include <AK/StdLibExtras.h>
#include <Compositor/CanvasHost.h>
-#include <Compositor/CompositorPresentationClientEndpoint.h>
#include <Compositor/CompositorState.h>
#include <LibCore/EventLoop.h>
#include <LibCore/StatsLog.h>
@@ -31,6 +29,13 @@ CompositorState::CompositorState(RefPtr<Gfx::SkiaBackendContext> skia_backend_co
{
}
+CompositorState::PresentedContext::PresentedContext(Optional<u64> page_id, CompositorStateWebContentClient& web_content_client, OwnedCanvasSurfaces canvas_surfaces)
+ : page_id(page_id)
+ , web_content_client(web_content_client)
+ , presenter(canvas_surfaces, page_id.has_value())
+{
+}
+
CompositorState::~CompositorState()
{
if (!m_gpu_completion_timer)
@@ -51,13 +56,6 @@ CompositorStateWebContentClient* CompositorState::web_content_client_if_present(
return m_web_content_clients.get(client_id).value_or(nullptr);
}
-CompositorStateWebContentClient& CompositorState::web_content_client_of(ContextState const& context)
-{
- auto* client = web_content_client_if_present(context.web_content_client_id());
- VERIFY(client);
- return *client;
-}
-
void CompositorState::post_message_to_web_content(WebContentClientId client_id, IPC::MessageBuffer& buffer)
{
if (auto* client = web_content_client_if_present(client_id))
@@ -78,7 +76,7 @@ void CompositorState::post_message_to_presentation(PresentationId presentation_i
void CompositorState::request_display_ticks(Web::CompositorContextId context_id)
{
- if (auto* context = context_if_present(context_id); context && context_is_effectively_visible(*context))
+ if (auto* context = presented_context_if_present(context_id); context && context_is_effectively_visible(*context))
vsync_scheduler_for_display(display_id_for_context(*context)).schedule(display_refresh_rate_for_context(*context));
}
@@ -103,16 +101,10 @@ void CompositorState::cancel_timer(Web::CompositorContextId context_id, ContextT
void CompositorState::context_timer_fired(Web::CompositorContextId context_id, ContextTimer timer)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
- auto* context = context_if_present(context_id);
- if (!context)
+ if (!presented_context_if_present(context_id))
return;
- auto now = MonotonicTime::now();
- auto caret_damage_rect = context->timer_fired(timer, now);
- if (caret_damage_rect.has_value())
- m_present_requests.append({ context_id, {}, *caret_damage_rect });
+ m_core.timer_fired(context_id, timer, MonotonicTime::now());
+ schedule_requested_presents();
}
PresentationId CompositorState::register_presentation(CompositorStateClient& presentation, PresentationCapabilities capabilities)
@@ -139,8 +131,8 @@ void CompositorState::unregister_presentation(CompositorStateClient& presentatio
void CompositorState::bind_presentation(Web::CompositorContextId context_id, CompositorStateClient& presentation)
{
VERIFY(m_presentations.contains(&presentation));
- auto* context = context_if_present(context_id);
- if (!context || !context->presents_to_client()) {
+ auto* context = presented_context_if_present(context_id);
+ if (!context || !context->presenter.presents_to_client()) {
dbgln("Compositor: Ignoring presentation binding for context {}, which is not a root context", context_id);
return;
}
@@ -149,11 +141,11 @@ void CompositorState::bind_presentation(Web::CompositorContextId context_id, Com
unbind_presentation(context_id);
m_context_presentations.set(context_id, &presentation);
- context->set_bound_presentation(presentation.presentation_id());
+ m_core.set_bound_presentation(context_id, presentation.presentation_id());
update_scroll_capabilities();
// The new presentation has seen none of the context's buffers, so it gets buffers of its own.
- context->presenter().invalidate_backing_stores();
+ context->presenter.invalidate_backing_stores();
resize_backing_stores_if_needed(context_id, *context);
}
@@ -161,19 +153,19 @@ void CompositorState::unbind_presentation(Web::CompositorContextId context_id)
{
if (!m_context_presentations.remove(context_id))
return;
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
- context->set_bound_presentation({});
+ m_core.set_bound_presentation(context_id, {});
for (auto& pending_present : m_pending_async_presents) {
if (pending_present.context_id == context_id)
pending_present.publish_to_presentation = false;
}
// Nobody is left to release the buffers the presentation was showing.
- context->presenter().acknowledge_all_presented_bitmaps();
+ context->presenter.acknowledge_all_presented_bitmaps();
update_scroll_capabilities();
- context->presenter().invalidate_backing_stores();
+ context->presenter.invalidate_backing_stores();
resize_backing_stores_if_needed(context_id, *context);
schedule_pending_present_frame_if_unblocked(context_id, *context);
}
@@ -185,20 +177,22 @@ CompositorStateClient* CompositorState::presentation_for_context(Web::Compositor
CompositorState::ContextOwnerCheckResult CompositorState::check_context_owner(Web::CompositorContextId context_id, WebContentClientId client)
{
- auto* context = context_if_present(context_id);
- if (!context)
+ switch (m_core.check_context_owner(context_id, client)) {
+ case CompositorCore::ContextOwnerCheckResult::OwnedByClient:
+ return ContextOwnerCheckResult::OwnedByClient;
+ case CompositorCore::ContextOwnerCheckResult::ContextUnavailable:
return ContextOwnerCheckResult::ContextUnavailable;
- if (!context->is_owned_by(client))
+ case CompositorCore::ContextOwnerCheckResult::ConflictingOwner:
return ContextOwnerCheckResult::ConflictingOwner;
-
- return ContextOwnerCheckResult::OwnedByClient;
+ }
+ VERIFY_NOT_REACHED();
}
void CompositorState::destroy_contexts_for_web_content_client(CompositorStateWebContentClient& client)
{
Vector<Web::CompositorContextId> context_ids;
- for (auto& context : m_contexts) {
- if (context.value->is_owned_by(client.web_content_client_id()))
+ for (auto& context : m_presented_contexts) {
+ if (context.value->web_content_client.web_content_client_id() == client.web_content_client_id())
context_ids.append(context.key);
}
@@ -273,30 +267,27 @@ CompositorState::PlaceholderCanvasPixels CompositorState::read_placeholder_canva
void CompositorState::create_context(Web::CompositorContextId context_id, Optional<u64> page_id, CompositorStateWebContentClient& web_content_client)
{
- VERIFY(!m_contexts.contains(context_id));
- if (page_id.has_value())
- VERIFY(context_id == Web::compositor_context_id_for_page(*page_id));
-
- auto& context = *m_contexts.ensure(context_id, [&] {
- return make<ContextState>(context_id, page_id, web_content_client.web_content_client_id(), *this, OwnedCanvasSurfaces { m_canvas_surface_registry, web_content_client.canvas_surface_owner_id() });
+ m_core.create_context(context_id, page_id, web_content_client.web_content_client_id());
+ auto& context = *m_presented_contexts.ensure(context_id, [&] {
+ return make<PresentedContext>(page_id, web_content_client, OwnedCanvasSurfaces { m_canvas_surface_registry, web_content_client.canvas_surface_owner_id() });
});
resize_backing_stores_if_needed(context_id, context);
}
void CompositorState::destroy_context(Web::CompositorContextId context_id)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
cancel_pending_async_presents_for_context(context_id);
- clear_parent_context(*context);
- for (auto& context_entry : m_contexts) {
+ clear_parent_context(context_id, *context);
+ for (auto& context_entry : m_presented_contexts) {
if (context_entry.key == context_id)
continue;
auto& possible_child_context = *context_entry.value;
- auto parent_context_id = possible_child_context.parent_context_id();
+ auto parent_context_id = possible_child_context.parent_context_id;
if (parent_context_id.has_value() && *parent_context_id == context_id)
- possible_child_context.set_parent_context({});
+ possible_child_context.parent_context_id = {};
}
m_context_presentations.remove(context_id);
if (auto timers = m_context_timers.find(context_id); timers != m_context_timers.end()) {
@@ -306,61 +297,63 @@ void CompositorState::destroy_context(Web::CompositorContextId context_id)
}
m_context_timers.remove(timers);
}
- m_contexts.remove(context_id);
+ m_core.destroy_context(context_id);
+ m_presented_contexts.remove(context_id);
update_scroll_capabilities();
update_unpainted_video_update_scheduling();
}
void CompositorState::set_parent_context(Web::CompositorContextId context_id, Optional<Web::CompositorContextId> parent_context_id)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
if (parent_context_id.has_value()) {
- VERIFY(!context->presents_to_client());
+ VERIFY(!context->presenter.presents_to_client());
VERIFY(*parent_context_id != context_id);
// The parent's context belongs to the process hosting the parent, which can destroy it before this context's
// process hears that the parent is gone. The context is left without a parent then, as destroying the parent
// would have left it.
// NB: The UI process creates a context here before it hands the context's ID out, so a parent this doesn't know
// has been destroyed.
- if (!context_if_present(*parent_context_id))
+ if (!presented_context_if_present(*parent_context_id))
parent_context_id = {};
}
- auto current_parent_context_id = context->parent_context_id();
+ auto current_parent_context_id = context->parent_context_id;
if (current_parent_context_id.has_value() == parent_context_id.has_value()
&& (!current_parent_context_id.has_value() || *current_parent_context_id == *parent_context_id))
return;
- clear_parent_context(*context);
- context->set_parent_context(parent_context_id);
+ clear_parent_context(context_id, *context);
+ context->parent_context_id = parent_context_id;
+ m_core.set_parent_context(context_id, parent_context_id);
update_scroll_capabilities();
if (!parent_context_id.has_value())
return;
- if (!context->presenter().latest_rendered_surface() && !context->presenter().needs_rasterization())
+ if (!context->presenter.latest_rendered_surface() && !context->presenter.needs_rasterization())
return;
- auto* parent_context = context_if_present(*parent_context_id);
+ auto* parent_context = presented_context_if_present(*parent_context_id);
VERIFY(parent_context);
present_current_frame(*parent_context_id, *parent_context);
}
void CompositorState::stop_presenting_to_client(Web::CompositorContextId context_id)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
unbind_presentation(context_id);
- context->stop_presenting_to_client();
- context->presenter().stop_presenting_to_client();
+ m_core.stop_presenting_to_client(context_id);
+ context->presenter.stop_presenting_to_client();
}
void CompositorState::update_display_list(Web::CompositorContextId context_id, NonnullRefPtr<Compositing::DisplayList> display_list, Compositing::AccumulatedVisualContextTree visual_context_tree, Compositing::DisplayListResourceTransaction&& resource_transaction, Compositing::ScrollStateSnapshot&& scroll_state_snapshot)
{
auto now = MonotonicTime::now();
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
if (display_list->compatible_visual_context_tree_structural_epoch() != visual_context_tree.structural_epoch()) {
@@ -374,9 +367,9 @@ void CompositorState::update_display_list(Web::CompositorContextId context_id, N
return;
}
- context->presenter().apply_resource_transaction(move(resource_transaction));
- context->presenter().set_display_list(display_list);
- context->install_display_list_update(move(display_list), move(visual_context_tree), move(scroll_state_snapshot), now);
+ context->presenter.apply_resource_transaction(move(resource_transaction));
+ context->presenter.set_display_list(display_list);
+ m_core.install_display_list_update(context_id, move(display_list), move(visual_context_tree), move(scroll_state_snapshot), now);
resolve_video_sinks(*context);
show_animated_image_frames_in(*context);
@@ -385,29 +378,27 @@ void CompositorState::update_display_list(Web::CompositorContextId context_id, N
void CompositorState::update_display_list_resources(Web::CompositorContextId context_id, Compositing::DisplayListResourceTransaction&& resource_transaction)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
- context->presenter().apply_resource_transaction(move(resource_transaction));
+ context->presenter.apply_resource_transaction(move(resource_transaction));
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)
{
auto now = MonotonicTime::now();
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
- if (context->update_visual_context_tree(move(visual_context_tree), now))
- context->presenter().apply_resource_transaction(move(resource_transaction));
+ if (m_core.update_visual_context_tree(context_id, move(visual_context_tree), now))
+ context->presenter.apply_resource_transaction(move(resource_transaction));
show_animated_image_frames_in(*context);
}
void CompositorState::update_scroll_state(Web::CompositorContextId context_id, Compositing::ScrollStateSnapshot&& scroll_state_snapshot, Compositing::KeyboardScrollState keyboard_scroll_state)
{
- auto* context = context_if_present(context_id);
- VERIFY(context);
-
- context->update_scroll_state(move(scroll_state_snapshot), move(keyboard_scroll_state));
+ m_core.update_scroll_state(context_id, move(scroll_state_snapshot), move(keyboard_scroll_state));
+ schedule_requested_presents();
}
CompositorState::VideoSinkState* CompositorState::video_sink_state(CompositorStateWebContentClient& client, Media::VideoSinkHandle handle)
@@ -480,8 +471,8 @@ void CompositorState::on_video_sink_ready(CompositorStateWebContentClient& clien
present_contexts_drawing_video_sink(client, handle);
update_unpainted_video_update_scheduling();
});
- for (auto& context_entry : m_contexts) {
- if (context_entry.value->web_content_client_id() == client.web_content_client_id())
+ for (auto& context_entry : m_presented_contexts) {
+ if (context_entry.value->web_content_client.web_content_client_id() == client.web_content_client_id())
resolve_video_sinks(*context_entry.value);
}
present_contexts_drawing_video_sink(client, handle);
@@ -577,9 +568,9 @@ void CompositorState::animated_image_decoder_lost(CompositorStateWebContentClien
bool CompositorState::animated_image_is_drawn(CompositorStateWebContentClient* client, Compositing::ImageFrameResourceId id)
{
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto& context = *context_entry.value;
- if (context.web_content_client_id() == client->web_content_client_id() && context_is_effectively_visible(context) && context.presenter().resource_storage().has_image_frame(id))
+ if (context.web_content_client.web_content_client_id() == client->web_content_client_id() && context_is_effectively_visible(context) && context.presenter.resource_storage().has_image_frame(id))
return true;
}
return false;
@@ -587,28 +578,28 @@ bool CompositorState::animated_image_is_drawn(CompositorStateWebContentClient* c
void CompositorState::show_animated_image_frame(CompositorStateWebContentClient* client, Compositing::ImageFrameResourceId id, Gfx::DecodedImageFrame const& frame)
{
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto& context = *context_entry.value;
- if (context.web_content_client_id() != client->web_content_client_id() || !context.presenter().resource_storage().has_image_frame(id))
+ if (context.web_content_client.web_content_client_id() != client->web_content_client_id() || !context.presenter.resource_storage().has_image_frame(id))
continue;
- context.presenter().resource_storage().replace_image_frame_pixels(id, frame);
+ context.presenter.resource_storage().replace_image_frame_pixels(id, frame);
if (!context_is_effectively_visible(context))
continue;
- if (auto rect = context.presenter().self_present_rect(); rect.has_value())
+ if (auto rect = context.presenter.self_present_rect(); rect.has_value())
schedule_present_frame(context_entry.key, context, PendingFrame::repainting_changes(*rect));
}
}
-void CompositorState::show_animated_image_frames_in(ContextState& context)
+void CompositorState::show_animated_image_frames_in(PresentedContext& context)
{
- auto images = m_animated_images.find(&web_content_client_of(context));
+ auto images = m_animated_images.find(&context.web_content_client);
if (images == m_animated_images.end())
return;
for (auto& image_entry : images->value) {
Compositing::ImageFrameResourceId id { image_entry.key };
auto const& frame = image_entry.value->shown_frame();
- if (frame.has_value() && context.presenter().resource_storage().has_image_frame(id) && &context.presenter().resource_storage().image_frame(id).bitmap() != &frame->bitmap())
- context.presenter().resource_storage().replace_image_frame_pixels(id, *frame);
+ if (frame.has_value() && context.presenter.resource_storage().has_image_frame(id) && &context.presenter.resource_storage().image_frame(id).bitmap() != &frame->bitmap())
+ context.presenter.resource_storage().replace_image_frame_pixels(id, *frame);
}
// An image that nothing drew has been waiting for a context to draw it.
if (!images->value.is_empty())
@@ -660,15 +651,15 @@ void CompositorState::schedule_animated_image_update(AK::Duration delay)
void CompositorState::present_contexts_drawing_video_sink(CompositorStateWebContentClient& client, Media::VideoSinkHandle handle)
{
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto& context = *context_entry.value;
- if (context.web_content_client_id() != client.web_content_client_id())
+ if (context.web_content_client.web_content_client_id() != client.web_content_client_id())
continue;
if (!context_is_effectively_visible(context))
continue;
- for (auto const& resource_entry : context.presenter().resource_storage().video_sink_handles()) {
+ for (auto const& resource_entry : context.presenter.resource_storage().video_sink_handles()) {
if (resource_entry.value == handle) {
- if (auto rect = context.presenter().self_present_rect(); rect.has_value())
+ if (auto rect = context.presenter.self_present_rect(); rect.has_value())
schedule_present_frame(context_entry.key, context, PendingFrame::repainting_changes(*rect));
break;
}
@@ -678,33 +669,33 @@ void CompositorState::present_contexts_drawing_video_sink(CompositorStateWebCont
void CompositorState::present_contexts_drawing_canvas(CompositorStateWebContentClient& client, Compositing::CanvasId canvas_id)
{
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto& context = *context_entry.value;
- if (context.web_content_client_id() != client.web_content_client_id())
+ if (context.web_content_client.web_content_client_id() != client.web_content_client_id())
continue;
- if (!context_is_effectively_visible(context) || !context.presenter().draws_canvas(canvas_id))
+ if (!context_is_effectively_visible(context) || !context.presenter.draws_canvas(canvas_id))
continue;
- if (auto rect = context.presenter().self_present_rect(); rect.has_value())
+ if (auto rect = context.presenter.self_present_rect(); rect.has_value())
schedule_present_frame(context_entry.key, context, PendingFrame::repainting_changes(*rect));
}
}
-void CompositorState::resolve_video_sinks(ContextState& context)
+void CompositorState::resolve_video_sinks(PresentedContext& context)
{
- auto& client = web_content_client_of(context);
- for (auto const& resource_entry : context.presenter().resource_storage().video_sink_handles()) {
+ auto& client = context.web_content_client;
+ for (auto const& resource_entry : context.presenter.resource_storage().video_sink_handles()) {
auto* sink_state = video_sink_state(client, resource_entry.value);
- context.presenter().resource_storage().set_video_sink(Compositing::VideoSinkResourceId { resource_entry.key }, sink_state ? sink_state->sink : nullptr);
+ context.presenter.resource_storage().set_video_sink(Compositing::VideoSinkResourceId { resource_entry.key }, sink_state ? sink_state->sink : nullptr);
}
}
bool CompositorState::video_sink_is_painted_by_any_context(CompositorStateWebContentClient* client, Media::VideoSinkHandle handle) const
{
- for (auto const& context_entry : m_contexts) {
+ for (auto const& context_entry : m_presented_contexts) {
auto const& context = *context_entry.value;
- if (context.web_content_client_id() != client->web_content_client_id())
+ if (context.web_content_client.web_content_client_id() != client->web_content_client_id())
continue;
- for (auto const& resource_entry : context.presenter().resource_storage().video_sink_handles()) {
+ for (auto const& resource_entry : context.presenter.resource_storage().video_sink_handles()) {
if (resource_entry.value == handle)
return true;
}
@@ -715,8 +706,8 @@ bool CompositorState::video_sink_is_painted_by_any_context(CompositorStateWebCon
int CompositorState::unpainted_video_update_interval_ms() const
{
auto max_refresh_rate = 60.0;
- for (auto const& context_entry : m_contexts)
- max_refresh_rate = max(max_refresh_rate, context_entry.value->display_refresh_rate());
+ for (auto const& context_entry : m_presented_contexts)
+ max_refresh_rate = max(max_refresh_rate, context_entry.value->display_refresh_rate);
return max(1, static_cast<int>(1000.0 / max_refresh_rate));
}
@@ -764,14 +755,14 @@ void CompositorState::update_video_sinks_for_display(Optional<u64> display_id)
update_all_video_sinks();
// Keep this display's vsync ticking while any sink painted on it may require updates.
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto& context = *context_entry.value;
if (display_id_for_context(context) != display_id)
continue;
if (!context_is_effectively_visible(context))
continue;
- for (auto const& resource_entry : context.presenter().resource_storage().video_sink_handles()) {
- auto* sink_state = video_sink_state(web_content_client_of(context), resource_entry.value);
+ for (auto const& resource_entry : context.presenter.resource_storage().video_sink_handles()) {
+ auto* sink_state = video_sink_state(context.web_content_client, resource_entry.value);
if (sink_state != nullptr && sink_state->requires_updates) {
vsync_scheduler_for_display(display_id).schedule(display_refresh_rate_for_context(context));
break;
@@ -780,908 +771,177 @@ void CompositorState::update_video_sinks_for_display(Optional<u64> display_id)
}
}
-Optional<u64> CompositorState::display_id_for_context(ContextState const& context) const
+Optional<u64> CompositorState::display_id_for_context(PresentedContext const& context) const
{
auto current_context = &context;
while (current_context) {
- if (current_context->display_id().has_value())
- return current_context->display_id();
- auto parent_context_id = current_context->parent_context_id();
+ if (current_context->display_id.has_value())
+ return current_context->display_id;
+ auto parent_context_id = current_context->parent_context_id;
if (!parent_context_id.has_value())
break;
- current_context = context_if_present(*parent_context_id);
+ current_context = presented_context_if_present(*parent_context_id);
}
return {};
}
-ContextState const* CompositorState::root_context_of(ContextState const& context) const
+CompositorState::PresentedContext const* CompositorState::root_context_of(PresentedContext const& context) const
{
auto const* current_context = &context;
while (true) {
- auto parent_context_id = current_context->parent_context_id();
+ auto parent_context_id = current_context->parent_context_id;
if (!parent_context_id.has_value())
return current_context;
- auto const* parent_context = context_if_present(*parent_context_id);
+ auto const* parent_context = presented_context_if_present(*parent_context_id);
if (!parent_context)
return current_context;
current_context = parent_context;
}
}
-bool CompositorState::context_is_effectively_visible(ContextState const& context) const
+Web::CompositorContextId CompositorState::root_context_id_of(Web::CompositorContextId context_id) const
{
- return root_context_of(context)->visibility() == Compositing::ContextVisibility::Visible;
+ auto const* current_context = presented_context_if_present(context_id);
+ VERIFY(current_context);
+ while (true) {
+ auto parent_context_id = current_context->parent_context_id;
+ if (!parent_context_id.has_value())
+ return context_id;
+ auto const* parent_context = presented_context_if_present(*parent_context_id);
+ if (!parent_context)
+ return context_id;
+ context_id = *parent_context_id;
+ current_context = parent_context;
+ }
}
-double CompositorState::display_refresh_rate_for_context(ContextState const& context) const
+bool CompositorState::context_is_effectively_visible(PresentedContext const& context) const
+{
+ return root_context_of(context)->visibility == Compositing::ContextVisibility::Visible;
+}
+
+double CompositorState::display_refresh_rate_for_context(PresentedContext const& context) const
{
auto current_context = &context;
while (current_context) {
- if (current_context->display_id().has_value())
- return current_context->display_refresh_rate();
- auto parent_context_id = current_context->parent_context_id();
+ if (current_context->display_id.has_value())
+ return current_context->display_refresh_rate;
+ auto parent_context_id = current_context->parent_context_id;
if (!parent_context_id.has_value())
break;
- current_context = context_if_present(*parent_context_id);
+ current_context = presented_context_if_present(*parent_context_id);
}
- return context.display_refresh_rate();
+ return context.display_refresh_rate;
}
void CompositorState::invalidate_wheel_event_listener_state(Web::CompositorContextId context_id, u64 generation)
{
- auto* context = context_if_present(context_id);
- VERIFY(context);
- context->invalidate_wheel_event_listener_state(generation);
+ m_core.invalidate_wheel_event_listener_state(context_id, generation);
+ schedule_requested_presents();
}
void CompositorState::invalidate_keyboard_scroll_state(Web::CompositorContextId context_id, u64 generation)
{
- if (auto* context = context_if_present(context_id))
- context->invalidate_keyboard_scroll_state(generation);
+ m_core.invalidate_keyboard_scroll_state(context_id, generation);
+ schedule_requested_presents();
}
void CompositorState::handle_and_dispatch_key_event(Web::CompositorContextId context_id, Web::KeyEvent event, bool allow_keyboard_scroll)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
auto now = MonotonicTime::now();
- // NB: The presentation connection only passes on input for the contexts bound to it.
- auto* context = context_if_present(context_id);
- VERIFY(context);
- VERIFY(context->bound_presentation().has_value());
- auto presentation_id = *context->bound_presentation();
- if (!context->can_dispatch_input_to_web_content()) {
- // No context could take the event; the UI sends it to WebContent itself.
- post_to_presentation<Messages::CompositorPresentationClient::DidNotDispatchInputEvent>(presentation_id, context_id, event.id);
- return;
- }
-
- bool later_keys_need_not_wait = false;
- if (allow_keyboard_scroll) {
- if (auto box = context->box_scrolled_for_key_event(event); box.has_value())
- dispatch_scroll_fling_step(context_id, *context, context->end_momentum_scrolling(box.value()), now);
- auto result = context->handle_key_event(event, now);
- later_keys_need_not_wait = apply_context_update_result(context_id, *context, result) && event.type == Web::KeyEvent::Type::KeyDown;
- event.async_scroll_performed_default_action = later_keys_need_not_wait && !result.key_found_nothing_to_scroll;
- if (later_keys_need_not_wait && result.key_found_nothing_to_scroll)
- event.user_input_scroll_count_with_nothing_to_scroll = context->user_input_scroll_count();
- }
-
- // The scroll updates the event produced reach WebContent ahead of the event itself on the same connection.
- publish_pending_async_scroll_updates(context_id, *context);
- context->dispatch_key_event_to_web_content(event);
- // The compositor forwarded the key event to WebContent, after performing its default action by scrolling if it says so.
- post_to_presentation<Messages::CompositorPresentationClient::DidDispatchInputEvent>(presentation_id, context_id, event.id, later_keys_need_not_wait);
+ m_core.handle_and_dispatch_key_event(context_id, move(event), allow_keyboard_scroll, now);
+ schedule_requested_presents();
}
void CompositorState::handle_and_dispatch_mouse_event(Web::CompositorContextId context_id, Web::MouseEvent event)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
auto now = MonotonicTime::now();
- // NB: The presentation connection only passes on input for the contexts bound to it.
- auto* context = context_if_present(context_id);
- VERIFY(context);
- VERIFY(context->bound_presentation().has_value());
- auto presentation_id = *context->bound_presentation();
- auto record_routing = [&](Web::InputRoutingDecision decision, Optional<Web::AsyncScrollNodeStableID> scroller = {}) {
- context->input_trace().record({ .input_event_id = event.id, .stage = Web::InputTraceStage::Compositor, .decision = decision, .scroller = scroller });
- };
- if (!context->can_dispatch_input_to_web_content()) {
- record_routing(Web::InputRoutingDecision::NotDispatchedByCompositor);
- post_to_presentation<Messages::CompositorPresentationClient::DidNotDispatchInputEvent>(presentation_id, context_id, event.id);
- return;
- }
-
- if (event.type == Web::MouseEvent::Type::MouseMove)
- context->note_pointer_position(Gfx::FloatPoint { static_cast<float>(event.position.x().value()), static_cast<float>(event.position.y().value()) });
- else if (first_is_one_of(event.type, Web::MouseEvent::Type::MouseLeave, Web::MouseEvent::Type::MouseCancel))
- context->note_pointer_position({});
-
- if (event.type == Web::MouseEvent::Type::MouseDown)
- dispatch_scroll_fling_step(context_id, *context, context->end_scroll_fling(), now);
-
- auto is_wheel_event = event.type == Web::MouseEvent::Type::MouseWheel;
- bool is_first_wheel_event_with_delta = false;
- if (is_wheel_event) {
- // A key, or a script's smooth scroll, takes over from momentum, until wheel input other than the momentum takes
- // over again.
- if (context->absorbs_wheel_event_of_interrupted_momentum(event)) {
- record_routing(Web::InputRoutingDecision::MomentumInterrupted);
- // The compositor performed the event's whole default action, so the UI hears nothing more about it.
- post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
- return;
- }
- auto noted = context->note_wheel_event_from_ui(event, now);
- event.wheel_gesture_mark = noted.wheel_gesture_mark;
- is_first_wheel_event_with_delta = noted.is_first_wheel_event_with_delta;
-
- // The UI can send the end of a gesture more than once. An end that arrives during a fling belongs to the
- // flicked gesture, so it does not stop the fling.
- if (event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended && context->has_active_scroll_fling()) {
- record_routing(Web::InputRoutingDecision::GestureEndAbsorbedByFling);
- post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
- return;
- }
-
- // A new gesture must not continue the gesture of the fling, so the fling ends first.
- dispatch_scroll_fling_step(context_id, *context, context->interrupt_scroll_fling(event, now), now);
-
- // If the gesture continues as a fling, the end of the fling is the end of the gesture.
- if (context->presentation_synthesizes_scroll_momentum() && event.scroll_gesture_phase == Web::ScrollGesturePhase::Ended && context->start_scroll_fling_if_flicked(event, now)) {
- record_routing(Web::InputRoutingDecision::FlingStarted);
- post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
- context->request_display_ticks_if_needed(now);
- return;
- }
- }
-
- ContextState::ContextUpdateResult result;
- if (is_wheel_event)
- result = scroll_for_wheel_step(context_id, *context, event, WheelStepOrigin::UI, is_first_wheel_event_with_delta, Compositing::AsyncScrollOperationTracking::No, now);
- else
- result = handle_mouse_event_in_nested_contexts(context_id, *context, event, now);
- if (result.nested_context_hand_off.has_value())
- record_routing(*result.nested_context_hand_off);
- if (result.routing_decision.has_value()) {
- dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Context {} routed input event {}: {}", context_id, event.id, Web::input_routing_decision_name(*result.routing_decision));
- record_routing(*result.routing_decision, result.routed_scroller);
- }
-
- // Asks for the present, publishes the scroll updates the event produced and asks for a rendering update, the last
- // two of which reach WebContent ahead of the event itself on the same connection. Those of a nested context
- // another process owns go on that process's connection instead, while the event reaches it through the UI.
- auto handled = apply_context_update_result(context_id, *context, result);
- // A wheel event that lets go of a stretched scroller presents nothing itself; the rebound does from here on. A press
- // beside the thumb of a scrollbar scrolls there in an animation too.
- context->request_display_ticks_if_needed(now);
- if (is_wheel_event) {
- event.async_scroll_performed_default_action = handled;
- if (!handled)
- context->note_wheel_event_left_to_the_page(event);
- } else if (handled) {
- // The page still sees the events of a drag the compositor scrolls for a scrollbar the display list paints.
- if (!result.scrollbar_dragged_by_compositor.has_value()) {
- record_routing(Web::InputRoutingDecision::ConsumedByCompositor);
- post_to_presentation<Messages::CompositorPresentationClient::DidConsumeInputEvent>(presentation_id, context_id, event.id);
- return;
- }
- event.scrollbar_dragged_by_compositor = result.scrollbar_dragged_by_compositor;
- }
- record_routing(Web::InputRoutingDecision::ForwardedToPage);
- context->dispatch_mouse_event_to_web_content(event, nested_context_scrolled_since_last_frame(context_id));
-}
-
-// The content box of a nested context is drawn scaled to the size of its viewport.
-static Optional<Gfx::FloatPoint> position_in_nested_context(Compositing::NestedContextHit const& hit, ContextState const& nested_context)
-{
- auto viewport_size = nested_context.viewport_size().to_type<float>();
- if (hit.content_box_size.is_empty() || viewport_size.is_empty())
- return {};
- return Gfx::FloatPoint {
- hit.position_in_content_box.x() * viewport_size.width() / hit.content_box_size.width(),
- hit.position_in_content_box.y() * viewport_size.height() / hit.content_box_size.height(),
- };
-}
-
-ContextState* CompositorState::context_still_nested_in(Web::CompositorContextId containing_context_id, Optional<Web::CompositorContextId> context_id)
-{
- if (!context_id.has_value())
- return nullptr;
- auto* context = context_if_present(*context_id);
- if (!context || !is_nested_in(containing_context_id, *context))
- return nullptr;
- return context;
-}
-
-Optional<Gfx::FloatPoint> CompositorState::position_in_nested_context_ignoring_clips(Web::CompositorContextId presenting_context_id, ContextState& presenting_context, ContextState& nested_context, Gfx::FloatPoint position)
-{
- Vector<ContextState*, 4> contexts_from_nested_context_outward;
- auto* context = &nested_context;
- // A walk up the parents that outlasts the number of contexts went around a loop.
- for (size_t remaining_contexts = m_contexts.size(); context != &presenting_context; --remaining_contexts) {
- if (remaining_contexts == 0)
- return {};
- contexts_from_nested_context_outward.append(context);
- auto parent_context_id = context->parent_context_id();
- if (!parent_context_id.has_value())
- return {};
- context = *parent_context_id == presenting_context_id ? &presenting_context : context_if_present(*parent_context_id);
- if (!context)
- return {};
- }
-
- auto* containing_context = &presenting_context;
- for (auto* context_in_between : contexts_from_nested_context_outward.in_reverse()) {
- auto hit = containing_context->position_in_content_of_nested_context(context_in_between->context_id(), position);
- if (!hit.has_value())
- return {};
- auto position_in_context = position_in_nested_context(*hit, *context_in_between);
- if (!position_in_context.has_value())
- return {};
- position = *position_in_context;
- containing_context = context_in_between;
- }
- return position;
-}
-
-ContextState::ContextUpdateResult CompositorState::handle_mouse_event_in_nested_contexts(Web::CompositorContextId context_id, ContextState& context, Web::MouseEvent const& event, MonotonicTime now)
-{
- using Type = Web::MouseEvent::Type;
- auto& pointer = context.pointer_in_nested_contexts();
-
- auto* context_with_captured_scrollbar = context_still_nested_in(context_id, pointer.context_with_captured_scrollbar);
- if (!context_with_captured_scrollbar || !context_with_captured_scrollbar->has_captured_scrollbar()) {
- pointer.context_with_captured_scrollbar.clear();
- context_with_captured_scrollbar = nullptr;
- }
-
- // The nested context presents and publishes what the event changed, and this context reports how the event went.
- auto result_of_nested_context = [&](Web::CompositorContextId nested_context_id, ContextState& nested_context, ContextState::ContextUpdateResult const& nested_result, Optional<PendingFrame> frame_of_this_context) {
- apply_context_update_result(nested_context_id, nested_context, nested_result);
- nested_context.request_display_ticks_if_needed(now);
- // Node ids are only unique within the process that made them.
- bool ids_are_of_this_process = nested_context.is_owned_by(context.web_content_client_id());
- return ContextState::ContextUpdateResult {
- .accepted = nested_result.accepted,
- .frame_to_present = move(frame_of_this_context),
- .scrollbar_dragged_by_compositor = nested_result.scrollbar_dragged_by_compositor,
- .routing_decision = nested_result.routing_decision,
- .routed_scroller = ids_are_of_this_process ? nested_result.routed_scroller : Optional<Web::AsyncScrollNodeStableID> {},
- .nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext,
- };
- };
-
- auto send_to_context_with_captured_scrollbar = [&](Web::MouseEvent const& event_to_send) {
- auto nested_context_id = *pointer.context_with_captured_scrollbar;
- auto position = Gfx::FloatPoint {
- static_cast<float>(event_to_send.position.x().value()),
- static_cast<float>(event_to_send.position.y().value()),
- };
- // A leave or a cancel has no position of its own.
- if (first_is_one_of(event_to_send.type, Type::MouseDown, Type::MouseMove, Type::MouseUp)) {
- if (auto position_in_nested_context = position_in_nested_context_ignoring_clips(context_id, context, *context_with_captured_scrollbar, position); position_in_nested_context.has_value())
- pointer.last_position_in_context_with_captured_scrollbar = *position_in_nested_context;
- }
- auto nested_result = context_with_captured_scrollbar->handle_mouse_event_from_containing_context(event_to_send, pointer.last_position_in_context_with_captured_scrollbar, now);
- auto result = result_of_nested_context(nested_context_id, *context_with_captured_scrollbar, nested_result, {});
- if (!context_with_captured_scrollbar->has_captured_scrollbar()) {
- pointer.context_with_captured_scrollbar.clear();
- context_with_captured_scrollbar = nullptr;
- pointer.context_under_pointer = nested_context_id;
- }
- return result;
- };
-
- // The scrollbar the pointer was over in the nested context it left is no longer hovered.
- auto note_context_under_pointer = [&](Optional<Web::CompositorContextId> nested_context_id) {
- if (pointer.context_under_pointer == nested_context_id)
- return;
- if (auto* context_left = context_still_nested_in(context_id, pointer.context_under_pointer)) {
- context_left->note_pointer_position({});
- if (auto frame_to_present = context_left->clear_scrollbar_hover(); frame_to_present.has_value())
- request_present(*pointer.context_under_pointer, *frame_to_present);
- }
- pointer.context_under_pointer = nested_context_id;
- };
-
- switch (event.type) {
- case Type::MouseDown:
- case Type::MouseMove: {
- if (context_with_captured_scrollbar) {
- if (event.type == Type::MouseMove)
- return send_to_context_with_captured_scrollbar(event);
- // A press while a drag is still held means the release of that drag never came, so the drag ends there.
- auto cancel_event = event.clone_without_browser_data();
- cancel_event.type = Type::MouseCancel;
- (void)send_to_context_with_captured_scrollbar(cancel_event);
- }
-
- auto result = context.handle_mouse_event(event, now);
- if (!result.nested_context_mouse_hit.has_value()) {
- note_context_under_pointer({});
- return result;
- }
-
- auto frame_of_this_context = result.frame_to_present;
- auto containing_context_id = context_id;
- auto* containing_context = &context;
- Gfx::FloatPoint position;
- while (result.nested_context_mouse_hit.has_value()) {
- auto hit = *result.nested_context_mouse_hit;
- // WebContent names the contexts its display lists draw, so a context only reaches those nested in it.
- auto* nested_context = context_if_present(hit.child_context_id);
- Optional<Gfx::FloatPoint> position_in_nested_context_content;
- if (nested_context && nested_context->parent_context_id() == containing_context_id)
- position_in_nested_context_content = position_in_nested_context(hit, *nested_context);
- if (!position_in_nested_context_content.has_value()) {
- note_context_under_pointer({});
- auto declined = ContextState::ContextUpdateResult::declined(Web::InputRoutingDecision::NestedContextUnavailable);
- declined.frame_to_present = frame_of_this_context;
- declined.nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext;
- return declined;
- }
- if (containing_context != &context) {
- apply_context_update_result(containing_context_id, *containing_context, result);
- containing_context->request_display_ticks_if_needed(now);
- }
- containing_context_id = hit.child_context_id;
- containing_context = nested_context;
- position = *position_in_nested_context_content;
- result = nested_context->handle_mouse_event_from_containing_context(event, position, now);
- }
-
- note_context_under_pointer(containing_context_id);
- if (event.type == Type::MouseDown && containing_context->has_captured_scrollbar()) {
- pointer.context_with_captured_scrollbar = containing_context_id;
- pointer.last_position_in_context_with_captured_scrollbar = position;
- }
- return result_of_nested_context(containing_context_id, *containing_context, result, move(frame_of_this_context));
- }
- case Type::MouseUp:
- if (context_with_captured_scrollbar)
- return send_to_context_with_captured_scrollbar(event);
- return context.handle_mouse_event(event, now);
- case Type::MouseLeave:
- case Type::MouseCancel: {
- // The pointer leaves every nested context it was in, and a cancel ends the drag of one, as in this context.
- Optional<ContextState::ContextUpdateResult> result_of_context_with_captured_scrollbar;
- Optional<Web::CompositorContextId> context_with_captured_scrollbar_id;
- if (context_with_captured_scrollbar) {
- context_with_captured_scrollbar_id = pointer.context_with_captured_scrollbar;
- result_of_context_with_captured_scrollbar = send_to_context_with_captured_scrollbar(event);
- }
- if (auto* context_under_pointer = context_still_nested_in(context_id, pointer.context_under_pointer); context_under_pointer && pointer.context_under_pointer != context_with_captured_scrollbar_id) {
- auto nested_result = context_under_pointer->handle_mouse_event_from_containing_context(event, {}, now);
- apply_context_update_result(*pointer.context_under_pointer, *context_under_pointer, nested_result);
- context_under_pointer->request_display_ticks_if_needed(now);
- }
- pointer.context_under_pointer.clear();
-
- auto result = context.handle_mouse_event(event, now);
- if (!result_of_context_with_captured_scrollbar.has_value())
- return result;
- result_of_context_with_captured_scrollbar->frame_to_present = result.frame_to_present;
- return result_of_context_with_captured_scrollbar.release_value();
- }
- case Type::MouseWheel:
- return context.handle_mouse_event(event, now);
- }
- VERIFY_NOT_REACHED();
-}
-
-// Whether a context nested in the context `context_id` scrolled since the last frame its WebContent presented: the lane
-// of the context's document hands a pointer move to the lane of a same-process iframe, which hit tests it in the iframe.
-bool CompositorState::nested_context_scrolled_since_last_frame(Web::CompositorContextId context_id) const
-{
- for (auto const& entry : m_contexts) {
- if (entry.value->scrolled_since_last_frame() && is_nested_in(context_id, *entry.value))
- return true;
- }
- return false;
-}
-
-bool CompositorState::is_nested_in(Web::CompositorContextId containing_context_id, ContextState const& context) const
-{
- // A walk up the parents that outlasts the number of contexts went around a loop.
- auto const* ancestor = &context;
- for (size_t remaining_ancestors = m_contexts.size(); ancestor && remaining_ancestors > 0; --remaining_ancestors) {
- auto parent_context_id = ancestor->parent_context_id();
- if (!parent_context_id.has_value())
- return false;
- if (*parent_context_id == containing_context_id)
- return true;
- ancestor = context_if_present(*parent_context_id);
- }
- return false;
-}
-
-ContextState::ContextUpdateResult CompositorState::scroll_for_wheel_step(Web::CompositorContextId context_id, ContextState& context, Web::MouseEvent const& event, WheelStepOrigin origin, bool is_first_wheel_event_with_delta, Compositing::AsyncScrollOperationTracking operation_tracking, MonotonicTime now)
-{
- auto gesture_id = context.wheel_gesture_id();
- auto listener_context_id = context.wheel_event_listener_context().value_or(context_id);
- auto* listener_context = listener_context_id == context_id ? &context : context_still_nested_in(context_id, listener_context_id);
- auto listener_gate = listener_context
- ? listener_context->wheel_event_listener_gate(gesture_id)
- : ContextState::WheelEventListenerGate { .reason_to_hold_latched_steps_back = Web::InputRoutingDecision::NestedContextUnavailable };
-
- ContextState::ContextUpdateResult result;
- bool presents_frame = false;
- if (listener_context && listener_context != &context && listener_gate.page_let_the_gesture_through && !context.has_wheel_latch_for_gesture(gesture_id)) {
- // The page of a nested context let the gesture through while nothing took its steps, so they go to that context
- // again rather than to the page.
- auto step = ContextState::WheelStep::for_wheel_event(event, gesture_id, operation_tracking);
- auto position = position_in_nested_context_ignoring_clips(context_id, context, *listener_context, step.position);
- if (!position.has_value())
- return ContextState::ContextUpdateResult::declined(Web::InputRoutingDecision::NestedContextUnavailable);
- step.position = *position;
- auto applied_step = route_wheel_step_the_page_let_through(context_id, context, listener_context_id, *listener_context, step, {}, {}, now);
- result = move(applied_step.result);
- result.nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext;
- presents_frame = applied_step.presents_frame;
- } else {
- switch (origin) {
- case WheelStepOrigin::UI:
- result = context.handle_wheel_event(event, is_first_wheel_event_with_delta, listener_gate, now);
- break;
- case WheelStepOrigin::Fling:
- result = context.scroll_for_fling_step(event, listener_gate, now);
- break;
- case WheelStepOrigin::MainThread:
- result = context.scroll_for_wheel_event_from_main_thread(event, is_first_wheel_event_with_delta, listener_gate, operation_tracking, now);
- break;
- }
- presents_frame = result.frame_to_present.has_value();
- if (result.nested_context_wheel_step.has_value()) {
- auto applied_step = hand_wheel_step_to_nested_context(context_id, context, event, *result.nested_context_wheel_step, listener_gate, operation_tracking, now);
- result = move(applied_step.result);
- presents_frame = applied_step.presents_frame;
- }
- }
-
- // If the compositor accepts a momentum step of the fling but does not scroll, the scroller is at its edge.
- // Alternatively, a snap scroll uses the step. The next display tick ends the fling.
- if (origin == WheelStepOrigin::Fling && event.scroll_gesture_phase == Web::ScrollGesturePhase::Momentum && result.accepted && !presents_frame)
- context.end_scroll_fling_on_next_step();
- return result;
-}
-
-CompositorState::AppliedWheelStep CompositorState::apply_wheel_step_result(Web::CompositorContextId context_id, ContextState& context, ContextState::ContextUpdateResult const& result, WebContentClientId reporting_client, MonotonicTime now)
-{
- apply_context_update_result(context_id, context, result);
- context.request_display_ticks_if_needed(now);
- // Node ids are only unique within the process that made them, and only that process follows the operations of the
- // context.
- bool ids_are_of_reporting_process = context.is_owned_by(reporting_client);
- return {
- .result = {
- .accepted = result.accepted,
- .routing_decision = result.routing_decision,
- .routed_scroller = ids_are_of_reporting_process ? result.routed_scroller : Optional<Web::AsyncScrollNodeStableID> {},
- .tracked_scroll_operation = ids_are_of_reporting_process ? result.tracked_scroll_operation : Optional<ContextState::TrackedScrollOperation> {},
- },
- .presents_frame = result.frame_to_present.has_value(),
- };
-}
-
-CompositorState::AppliedWheelStep CompositorState::hand_wheel_step_to_nested_context(Web::CompositorContextId context_id, ContextState& context, Web::MouseEvent const& event, ContextState::NestedContextWheelStep const& nested_step, ContextState::WheelEventListenerGate const& listener_gate, Compositing::AsyncScrollOperationTracking operation_tracking, MonotonicTime now)
-{
- auto step = ContextState::WheelStep::for_wheel_event(event, context.wheel_gesture_id(), operation_tracking);
-
- if (!nested_step.hit.has_value()) {
- auto* nested_context = context_if_present(nested_step.context_id);
- if (!nested_context || !is_nested_in(context_id, *nested_context)) {
- context.forget_wheel_gesture_latch();
- return { ContextState::ContextUpdateResult::declined(Web::InputRoutingDecision::NestedContextUnavailable) };
- }
- // A blocking listener added during the gesture holds its later steps back until the page lets one through.
- if (listener_gate.reason_to_hold_latched_steps_back.has_value())
- return { ContextState::ContextUpdateResult::declined(*listener_gate.reason_to_hold_latched_steps_back) };
- auto admission = listener_gate.page_let_the_gesture_through ? ContextState::WheelStepAdmission::LetThroughByThePage : ContextState::WheelStepAdmission::ByThisPage;
- auto nested_result = nested_context->take_latched_wheel_step_from_containing_context(step, admission, now);
- if (nested_result.routing_decision == Web::InputRoutingDecision::NestedContextUnavailable)
- context.forget_wheel_gesture_latch();
- return apply_wheel_step_result(nested_step.context_id, *nested_context, nested_result, context.web_content_client_id(), now);
- }
-
- auto first_step = first_wheel_step_in_nested_contexts(context_id, *nested_step.hit, step.delta_in_css_pixels, step.wheel_delta_precision);
- context.set_wheel_event_listener_context(first_step.innermost_context_id);
- auto& taking_context = first_step.taking_context;
- if (!taking_context.has_value()) {
- auto result = context.scroll_for_wheel_event_passed_on_by_nested_context(event, operation_tracking, now);
- result.nested_context_hand_off = Web::InputRoutingDecision::NestedContextPassedStepOn;
- return { .result = result, .presents_frame = result.frame_to_present.has_value() };
- }
-
- AppliedWheelStep applied_step;
- if (taking_context->context && taking_context->route.kind != ContextState::FirstWheelStepRoute::Kind::Declined) {
- step.position = taking_context->position;
- auto nested_result = taking_context->context->take_first_wheel_step_from_containing_context(taking_context->route, step, now);
- applied_step = apply_wheel_step_result(taking_context->context_id, *taking_context->context, nested_result, context.web_content_client_id(), now);
- context.latch_wheel_gesture_to_nested_context(taking_context->context_id, applied_step.result.routed_scroller, step.scroll_gesture_phase);
- } else {
- applied_step.result = ContextState::ContextUpdateResult::declined(taking_context->route.decision);
- }
- applied_step.result.nested_context_hand_off = Web::InputRoutingDecision::HandedToNestedContext;
- return applied_step;
-}
-
-CompositorState::FirstWheelStepInNestedContexts CompositorState::first_wheel_step_in_nested_contexts(Web::CompositorContextId context_id, Compositing::NestedContextHit hit, Gfx::FloatPoint delta_in_css_pixels, Web::WheelDeltaPrecision wheel_delta_precision)
-{
- using Kind = ContextState::FirstWheelStepRoute::Kind;
- // The contexts the step went into, outermost first, each nested in the one before.
- Vector<NestedContextTakingWheelStep, 4> contexts;
- auto containing_context_id = context_id;
- while (true) {
- auto declined_by_unavailable_context = [&] {
- return FirstWheelStepInNestedContexts {
- .innermost_context_id = hit.child_context_id,
- .taking_context = NestedContextTakingWheelStep { .context_id = hit.child_context_id, .route = { Kind::Declined, Web::InputRoutingDecision::NestedContextUnavailable } },
- };
- };
- // WebContent names the contexts its display lists draw, so a context only reaches those nested in it.
- auto* nested_context = context_if_present(hit.child_context_id);
- if (!nested_context || nested_context->parent_context_id() != containing_context_id)
- return declined_by_unavailable_context();
- auto position = position_in_nested_context(hit, *nested_context);
- if (!position.has_value())
- return declined_by_unavailable_context();
- auto route = nested_context->route_first_wheel_step_from_containing_context(*position, delta_in_css_pixels, wheel_delta_precision, Compositing::NestedContextRegions::HandToNestedContext, ContextState::WheelStepAdmission::ByThisPage);
- contexts.append({ .context_id = hit.child_context_id, .context = nested_context, .position = *position, .route = route });
- if (route.kind != Kind::NestedContext)
- break;
- containing_context_id = hit.child_context_id;
- hit = *route.nested_context_hit;
- }
-
- // What a nested context passes on goes to whatever is painted under it in the context it is nested in, whose page
- // does not see the step.
- auto innermost_context_id = contexts.last().context_id;
- while (contexts.last().route.kind == Kind::PassedOn) {
- contexts.take_last();
- if (contexts.is_empty())
- return { .innermost_context_id = innermost_context_id };
- auto& containing_context = contexts.last();
- containing_context.route = containing_context.context->route_first_wheel_step_from_containing_context(containing_context.position, delta_in_css_pixels, wheel_delta_precision, Compositing::NestedContextRegions::Ignore, ContextState::WheelStepAdmission::PassedOnByNestedContext);
- }
- return { .innermost_context_id = innermost_context_id, .taking_context = contexts.take_last() };
-}
-
-// The page of one process scrolls a context of another process only by what the UI left to the page in the gesture, so
-// that a page cannot scroll the document around it at will.
-static bool page_may_scroll_in(ContextState const& context, ContextState& routing_context, Optional<WebContentClientId> page_client, ContextState::WheelStep const& step)
-{
- return !page_client.has_value() || context.is_owned_by(*page_client) || routing_context.take_wheel_delta_left_to_the_page(step.delta_in_css_pixels);
-}
-
-CompositorState::AppliedWheelStep CompositorState::route_wheel_step_the_page_let_through(Web::CompositorContextId routing_context_id, ContextState& routing_context, Web::CompositorContextId listener_context_id, ContextState& listener_context, ContextState::WheelStep step, ReadonlySpan<Web::AsyncScrollNodeStableID> scroll_chain, Optional<WebContentClientId> page_client, MonotonicTime now)
-{
- using Kind = ContextState::FirstWheelStepRoute::Kind;
- auto const& reporting_client = page_client.has_value() ? *page_client : routing_context.web_content_client_id();
- auto declined = [](Web::InputRoutingDecision decision) {
- return AppliedWheelStep { .result = ContextState::ContextUpdateResult::declined(decision) };
- };
-
- auto context_id = listener_context_id;
- auto* context = &listener_context;
- // A walk up the parents that outlasts the number of contexts went around a loop.
- for (size_t remaining_contexts = m_contexts.size(); remaining_contexts > 0; --remaining_contexts) {
- if (context == &routing_context) {
- if (!page_may_scroll_in(routing_context, routing_context, page_client, step))
- return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
- auto result = routing_context.scroll_for_first_wheel_step_the_page_let_through(step, scroll_chain, now);
- return apply_wheel_step_result(routing_context_id, routing_context, result, reporting_client, now);
- }
-
- auto route = context->route_first_wheel_step_from_containing_context(step.position, step.delta_in_css_pixels, step.wheel_delta_precision, Compositing::NestedContextRegions::Ignore, ContextState::WheelStepAdmission::LetThroughByThePage, scroll_chain);
- if (first_is_one_of(route.kind, Kind::ScrollNode, Kind::Kept)) {
- if (!page_may_scroll_in(*context, routing_context, page_client, step))
- return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
- auto nested_result = context->take_first_wheel_step_from_containing_context(route, step, now);
- auto latched_scroller = context->is_owned_by(routing_context.web_content_client_id()) ? nested_result.routed_scroller : Optional<Web::AsyncScrollNodeStableID> {};
- routing_context.latch_wheel_gesture_to_nested_context(context_id, latched_scroller, step.scroll_gesture_phase);
- return apply_wheel_step_result(context_id, *context, nested_result, reporting_client, now);
- }
- if (route.kind != Kind::PassedOn)
- return declined(route.decision);
-
- // What a nested context passes on goes to whatever is painted under it in the context it is nested in.
- auto parent_context_id = context->parent_context_id();
- auto* parent_context = parent_context_id.has_value() ? context_if_present(*parent_context_id) : nullptr;
- if (!parent_context)
- return declined(Web::InputRoutingDecision::NestedContextUnavailable);
- auto position = parent_context->position_of_nested_context_position(context_id, step.position, context->viewport_size());
- if (!position.has_value())
- return declined(Web::InputRoutingDecision::NestedContextUnavailable);
- step.position = *position;
- scroll_chain = {};
- context_id = *parent_context_id;
- context = parent_context;
- }
- return declined(Web::InputRoutingDecision::NestedContextUnavailable);
-}
-
-Optional<Web::CompositorContextId> CompositorState::routing_context_of_wheel_gesture(Web::CompositorContextId context_id, u64 gesture_id)
-{
- // A walk up the parents that outlasts the number of contexts went around a loop.
- Optional<Web::CompositorContextId> candidate_context_id = context_id;
- for (size_t remaining_contexts = m_contexts.size(); candidate_context_id.has_value() && remaining_contexts > 0; --remaining_contexts) {
- auto* candidate_context = context_if_present(*candidate_context_id);
- if (!candidate_context)
- return {};
- if (candidate_context->wheel_gesture_id() == gesture_id)
- return candidate_context_id;
- candidate_context_id = candidate_context->parent_context_id();
- }
- return {};
-}
-
-void CompositorState::dispatch_scroll_fling_step(Web::CompositorContextId context_id, ContextState& context, Optional<Web::MouseEvent> event, MonotonicTime now)
-{
- // Send each fling step on the same route as a wheel event from the UI.
- if (!event.has_value())
- return;
- auto result = scroll_for_wheel_step(context_id, context, *event, WheelStepOrigin::Fling, false, Compositing::AsyncScrollOperationTracking::No, now);
- if (result.nested_context_hand_off.has_value())
- context.input_trace().record({ .input_event_id = event->id, .stage = Web::InputTraceStage::Compositor, .decision = *result.nested_context_hand_off });
- if (result.routing_decision.has_value())
- context.input_trace().record({ .input_event_id = event->id, .stage = Web::InputTraceStage::Compositor, .decision = *result.routing_decision, .scroller = result.routed_scroller });
- event->async_scroll_performed_default_action = apply_context_update_result(context_id, context, result);
- if (!event->async_scroll_performed_default_action)
- context.note_wheel_event_left_to_the_page(*event);
- context.request_display_ticks_if_needed(now);
- context.dispatch_mouse_event_to_web_content(*event);
-}
-
-// A pinch is a ctrl+wheel event first, to the document under it, whose blocking wheel event listeners may cancel it
-// before anything zooms. Only the documents at the position can see that event.
-bool CompositorState::page_under_pinch_may_cancel_it(Web::CompositorContextId context_id, ContextState& context, Gfx::FloatPoint position)
-{
- auto containing_context_id = context_id;
- auto* containing_context = &context;
- // A walk down the nested contexts that outlasts the number of contexts went around a loop.
- for (size_t remaining_contexts = m_contexts.size(); remaining_contexts > 0; --remaining_contexts) {
- if (containing_context->page_may_cancel_pinch_at(position))
- return true;
- auto hit = containing_context->nested_context_for_wheel_at(position);
- if (!hit.has_value())
- return false;
- // NB: Nothing is known yet of the listeners of a context that is not available.
- auto* nested_context = context_if_present(hit->child_context_id);
- if (!nested_context || nested_context->parent_context_id() != containing_context_id)
- return true;
- auto position_in_nested = position_in_nested_context(*hit, *nested_context);
- if (!position_in_nested.has_value())
- return true;
- containing_context_id = hit->child_context_id;
- containing_context = nested_context;
- position = *position_in_nested;
- }
- return true;
+ m_core.handle_and_dispatch_mouse_event(context_id, move(event), now);
+ schedule_requested_presents();
}
void CompositorState::handle_and_dispatch_pinch_event(Web::CompositorContextId context_id, Web::PinchEvent event)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
- // NB: The presentation connection only passes on input for the contexts bound to it.
- auto* context = context_if_present(context_id);
- VERIFY(context);
- VERIFY(context->bound_presentation().has_value());
- auto presentation_id = *context->bound_presentation();
- if (!context->can_dispatch_input_to_web_content()) {
- post_to_presentation<Messages::CompositorPresentationClient::DidNotDispatchInputEvent>(presentation_id, context_id, event.id);
- return;
- }
-
- // The first step of a gesture decides for the steps after it: a gesture the page let through zooms from here on,
- // whatever listeners have appeared since, and any other leaves each step to the page, wherever the pinch has moved.
- // A page that already let this step through leaves only the zoom to do.
- Gfx::FloatPoint position { static_cast<float>(event.position.x().value()), static_cast<float>(event.position.y().value()) };
- auto may_zoom = event.continues_gesture ? context->pinch_gesture_was_let_through() : !page_under_pinch_may_cancel_it(context_id, *context, position);
- if (event.wheel_event_was_dispatched || may_zoom)
- event.async_zoom_performed_default_action = apply_context_update_result(context_id, *context, context->handle_pinch_event(event));
- context->set_pinch_gesture_was_let_through(event.async_zoom_performed_default_action);
-
- // The scroll updates the event produced reach WebContent ahead of the event itself on the same connection.
- publish_pending_async_scroll_updates(context_id, *context);
- context->dispatch_pinch_event_to_web_content(event);
- post_to_presentation<Messages::CompositorPresentationClient::DidDispatchInputEvent>(presentation_id, context_id, event.id, event.async_zoom_performed_default_action);
-}
-
-Compositing::WheelEventAnswer CompositorState::wheel_event_answer_for(AppliedWheelStep const& applied_step, Optional<Web::WheelGestureMark> wheel_gesture_mark)
-{
- auto const& tracked_scroll_operation = applied_step.result.tracked_scroll_operation;
- return {
- .performed_default_action = applied_step.result.accepted,
- .wheel_gesture_mark = wheel_gesture_mark,
- .operation_id = tracked_scroll_operation.map([](auto const& operation) { return operation.operation_id; }),
- .operation_context_id = tracked_scroll_operation.map([](auto const& operation) { return operation.context_id; }),
- .routing_decision = applied_step.result.routing_decision,
- .routed_scroller = applied_step.result.routed_scroller,
- };
+ m_core.handle_and_dispatch_pinch_event(context_id, move(event));
+ schedule_requested_presents();
}
Compositing::WheelEventAnswer CompositorState::take_wheel_event_from_main_thread(Web::CompositorContextId context_id, Web::MouseEvent const& event, Compositing::AsyncScrollOperationTracking operation_tracking, Compositing::WheelEventRouting routing)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
auto now = MonotonicTime::now();
- auto* context = context_if_present(context_id);
- VERIFY(context);
-
- auto noted = context->note_wheel_event_from_main_thread(event, now);
- if (routing == Compositing::WheelEventRouting::AfterDispatch)
- return { .wheel_gesture_mark = noted.wheel_gesture_mark };
- auto result = scroll_for_wheel_step(context_id, *context, event, WheelStepOrigin::MainThread, noted.is_first_wheel_event_with_delta, operation_tracking, now);
- return wheel_event_answer_for(apply_wheel_step_result(context_id, *context, result, context->web_content_client_id(), now), noted.wheel_gesture_mark);
+ auto answer = m_core.take_wheel_event_from_main_thread(context_id, event, operation_tracking, routing, now);
+ schedule_requested_presents();
+ return answer;
}
Compositing::WheelEventAnswer CompositorState::scroll_for_wheel_event_the_page_let_through(Web::CompositorContextId listener_context_id, Compositing::WheelStepLetThrough const& let_through, Compositing::AsyncScrollOperationTracking operation_tracking, WebContentClientId page_client)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
auto now = MonotonicTime::now();
- auto* listener_context = context_if_present(listener_context_id);
- VERIFY(listener_context);
- if (listener_context->document_id() != let_through.document_id)
- return { .routing_decision = Web::InputRoutingDecision::ScrollerInOtherDocument };
-
- auto gesture_id = let_through.wheel_gesture_mark.gesture_id;
- // A step that moves along no axis goes to the page as no wheel event, so the page had no say in letting it through,
- // and it decides nothing for the steps after it.
- if (!let_through.delta_in_css_pixels.is_zero())
- listener_context->note_page_let_wheel_gesture_through(gesture_id);
- ContextState::WheelStep step {
- .position = let_through.position,
- .delta_in_css_pixels = let_through.delta_in_css_pixels,
- .wheel_delta_precision = let_through.wheel_delta_precision,
- .scroll_gesture_phase = let_through.scroll_gesture_phase,
- .gesture_id = gesture_id,
- .operation_tracking = operation_tracking,
- };
- auto scroll_chain = let_through.scroll_chain.span();
- auto answer = [&](AppliedWheelStep const& applied_step) { return wheel_event_answer_for(applied_step, let_through.wheel_gesture_mark); };
- auto declined = [&](Web::InputRoutingDecision decision) { return answer({ .result = ContextState::ContextUpdateResult::declined(decision) }); };
-
- // A step of a gesture that the contexts around the page have moved past still scrolls, as the page let it.
- auto routing_context_id = routing_context_of_wheel_gesture(listener_context_id, gesture_id);
- auto* routing_context = routing_context_id.has_value() ? context_if_present(*routing_context_id) : nullptr;
- if (!routing_context || routing_context->wheel_step_was_overtaken(step.scroll_gesture_phase, let_through.wheel_gesture_mark.step_number)) {
- auto result = listener_context->scroll_for_overtaken_wheel_step(step, scroll_chain, now);
- return answer(apply_wheel_step_result(listener_context_id, *listener_context, result, page_client, now));
- }
-
- routing_context->set_wheel_event_listener_context(listener_context_id);
- // The end of a gesture lets go of the scroller it stretched.
- if (step.scroll_gesture_phase == Web::ScrollGesturePhase::Ended)
- routing_context->end_wheel_gesture_stretch(now);
-
- // A latched scroller that the page no longer has around the target of the step is gone from the page, and the step
- // is routed afresh.
- bool latch_is_to_scroller_of_this_page = *routing_context_id == listener_context_id && !routing_context->nested_context_of_wheel_latch().has_value();
- if (latch_is_to_scroller_of_this_page && !scroll_chain.is_empty()) {
- if (auto latched_scroller = routing_context->wheel_latched_scroller(); latched_scroller.has_value() && !scroll_chain.contains_slow(*latched_scroller))
- routing_context->forget_wheel_latch_of_gesture(gesture_id);
- }
-
- if (routing_context->has_wheel_latch_for_gesture(gesture_id)) {
- if (auto nested_context_id = routing_context->nested_context_of_wheel_latch(); nested_context_id.has_value()) {
- auto* nested_context = context_still_nested_in(*routing_context_id, nested_context_id);
- if (!nested_context) {
- routing_context->forget_wheel_latch_of_gesture(gesture_id);
- return declined(Web::InputRoutingDecision::NestedContextUnavailable);
- }
- if (!page_may_scroll_in(*nested_context, *routing_context, page_client, step))
- return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
- auto nested_result = nested_context->take_latched_wheel_step_from_containing_context(step, ContextState::WheelStepAdmission::LetThroughByThePage, now);
- return answer(apply_wheel_step_result(*nested_context_id, *nested_context, nested_result, page_client, now));
- }
- if (!page_may_scroll_in(*routing_context, *routing_context, page_client, step))
- return declined(Web::InputRoutingDecision::StepExceedsInputLeftToThePage);
- if (auto result = routing_context->scroll_for_latched_wheel_step_the_page_let_through(step, now); result.has_value())
- return answer(apply_wheel_step_result(*routing_context_id, *routing_context, *result, page_client, now));
- }
-
- return answer(route_wheel_step_the_page_let_through(*routing_context_id, *routing_context, listener_context_id, *listener_context, step, scroll_chain, page_client, now));
+ auto answer = m_core.scroll_for_wheel_event_the_page_let_through(listener_context_id, let_through, operation_tracking, page_client, now);
+ schedule_requested_presents();
+ return answer;
}
void CompositorState::forget_wheel_gesture_the_page_canceled(Web::CompositorContextId listener_context_id, u64 gesture_id)
{
- auto* listener_context = context_if_present(listener_context_id);
- if (!listener_context)
- return;
- listener_context->forget_page_let_wheel_gesture_through(gesture_id);
- // The listener scrolls by itself, so the next step of the gesture is routed afresh.
- if (auto routing_context_id = routing_context_of_wheel_gesture(listener_context_id, gesture_id); routing_context_id.has_value())
- context_if_present(*routing_context_id)->forget_wheel_latch_of_gesture(gesture_id);
-}
-
-static bool scroll_request_smoothly_scrolls_for_script(Compositing::ScrollRequest const& scroll_request)
-{
- return scroll_request.smooth_scroll.has_value() && scroll_request.smooth_scroll->initiator == Compositing::SmoothScrollInitiator::Programmatic;
+ m_core.forget_wheel_gesture_the_page_canceled(listener_context_id, gesture_id);
+ schedule_requested_presents();
}
void CompositorState::apply_scroll_requests(Web::CompositorContextId context_id, ReadonlySpan<Compositing::ScrollRequest> scroll_requests)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
auto now = MonotonicTime::now();
- auto* context = context_if_present(context_id);
- if (!context)
- return;
-
- // A script's smooth scroll of a box ends the momentum scrolling it, as a key's does. A script's instant scroll only
- // moves the box, and the momentum goes on from there: pages keep their content in place under a flick that way.
- for (auto const& scroll_request : scroll_requests) {
- if (scroll_request.takes_over_from_momentum || scroll_request_smoothly_scrolls_for_script(scroll_request))
- dispatch_scroll_fling_step(context_id, *context, context->end_momentum_scrolling(scroll_request.stable_node_id), now);
- }
- if (auto frame_to_present = context->apply_scroll_requests(scroll_requests, now); frame_to_present.has_value())
- request_present(context_id, frame_to_present.value());
- context->request_display_ticks_if_needed(now);
- publish_pending_async_scroll_updates(context_id, *context);
+ m_core.apply_scroll_requests(context_id, scroll_requests, now);
+ schedule_requested_presents();
}
Compositing::PendingAsyncScrollUpdates CompositorState::take_pending_async_scroll_updates(Web::CompositorContextId context_id)
{
- auto* context = context_if_present(context_id);
- VERIFY(context);
-
- return context->take_pending_async_scroll_updates();
-}
-
-void CompositorState::publish_pending_async_scroll_updates(Web::CompositorContextId, ContextState& context)
-{
- context.publish_pending_async_scroll_updates();
+ auto answer = m_core.take_pending_async_scroll_updates(context_id);
+ schedule_requested_presents();
+ return answer;
}
void CompositorState::viewport_size_updated(Web::CompositorContextId context_id, Gfx::IntSize viewport_size, double device_pixel_ratio, Compositing::WindowResizingInProgress window_resize_in_progress)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
if (!context)
return;
- auto only_the_pixel_ratio_changed = viewport_size == context->presenter().viewport_size() && device_pixel_ratio != context->presenter().device_pixel_ratio();
- context->viewport_size_updated(viewport_size);
- auto is_page_presentation_context = context->page_id().has_value() && !context->parent_context_id().has_value();
- context->presenter().set_viewport_size(viewport_size, device_pixel_ratio, is_page_presentation_context ? window_resize_in_progress : Compositing::WindowResizingInProgress::No);
+ auto only_the_pixel_ratio_changed = viewport_size == context->presenter.viewport_size() && device_pixel_ratio != context->presenter.device_pixel_ratio();
+ m_core.viewport_size_updated(context_id, viewport_size);
+ auto is_page_presentation_context = context->page_id.has_value() && !context->parent_context_id.has_value();
+ context->presenter.set_viewport_size(viewport_size, device_pixel_ratio, is_page_presentation_context ? window_resize_in_progress : Compositing::WindowResizingInProgress::No);
resize_backing_stores_if_needed(context_id, *context);
// The paused debugger's page will not paint again. Any other page's next frame damages the whole viewport when its
// size changes, but a new pixel ratio alone need not damage the overlays.
- auto const& overlays = context->presenter().ui_overlays();
+ auto const& overlays = context->presenter.ui_overlays();
if (overlays.paused_debugger.has_value() || (only_the_pixel_ratio_changed && overlays.picture_in_picture_controls.has_value())) {
- if (auto viewport_rect = context->presenter().viewport_rect_for_ui_overlay(); viewport_rect.has_value())
+ if (auto viewport_rect = context->presenter.viewport_rect_for_ui_overlay(); viewport_rect.has_value())
schedule_present_frame(context_id, *context, *viewport_rect);
}
- if (context->presenter().should_shrink_backing_stores_after_resize())
+ if (context->presenter.should_shrink_backing_stores_after_resize())
schedule_backing_store_shrink(context_id, *context);
}
void CompositorState::set_ui_overlays(Web::CompositorContextId context_id, Compositing::UIOverlays overlays)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
if (!context)
return;
- if (context->presenter().ui_overlays() == overlays)
+ if (context->presenter.ui_overlays() == overlays)
return;
- context->set_paused_debugger_overlay_is_shown(overlays.paused_debugger.has_value());
- context->presenter().set_ui_overlays(move(overlays));
+ m_core.set_paused_debugger_overlay_is_shown(context_id, overlays.paused_debugger.has_value());
+ context->presenter.set_ui_overlays(move(overlays));
- if (auto viewport_rect = context->presenter().viewport_rect_for_ui_overlay(); viewport_rect.has_value())
+ if (auto viewport_rect = context->presenter.viewport_rect_for_ui_overlay(); viewport_rect.has_value())
schedule_present_frame(context_id, *context, *viewport_rect);
}
void CompositorState::set_display_metadata(Web::CompositorContextId context_id, Optional<u64> display_id, double refresh_rate)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
if (!context)
return;
@@ -1689,30 +949,29 @@ void CompositorState::set_display_metadata(Web::CompositorContextId context_id,
VERIFY(refresh_rate > 0);
VERIFY(refresh_rate < AK::Infinity<double>);
- context->set_display_metadata(display_id, refresh_rate);
- if (context->presenter().pending_present_frame_scheduled()) {
+ context->display_id = display_id;
+ context->display_refresh_rate = refresh_rate;
+ if (context->presenter.pending_present_frame_scheduled()) {
schedule_pending_present_frame(context_id, *context);
if (m_unpainted_video_update_timer)
m_unpainted_video_update_timer->set_interval(AK::Duration::from_milliseconds(unpainted_video_update_interval_ms()));
}
- if (context->display_tick_requested() && context_is_effectively_visible(*context))
+ if ((context->rendering_opportunity_request.is_requested() || context->clock_tick_request.is_requested()) && context_is_effectively_visible(*context))
vsync_scheduler_for_display(display_id_for_context(*context)).schedule(display_refresh_rate_for_context(*context));
}
void CompositorState::set_context_visibility(Web::CompositorContextId context_id, Compositing::ContextVisibility visibility)
{
- VERIFY(m_present_requests.is_empty());
- ScopeGuard schedule_presents_on_exit = [&] { schedule_requested_presents(); };
-
auto now = MonotonicTime::now();
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
if (!context)
return;
- if (visibility == Compositing::ContextVisibility::Hidden)
- dispatch_scroll_fling_step(context_id, *context, context->end_scroll_fling(), now);
- if (!context->set_visibility(visibility))
+ m_core.set_visibility(context_id, visibility, now);
+ schedule_requested_presents();
+ if (context->visibility == visibility)
return;
+ context->visibility = visibility;
dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Context {} became {}", context_id, visibility == Compositing::ContextVisibility::Visible ? "visible" : "hidden");
@@ -1722,43 +981,43 @@ void CompositorState::set_context_visibility(Web::CompositorContextId context_id
void CompositorState::set_context_presents_from_cpu_memory(Web::CompositorContextId context_id, bool presents_from_cpu_memory)
{
- auto* context = context_if_present(context_id);
- if (!context || context->presents_from_cpu_memory() == presents_from_cpu_memory)
+ auto* context = presented_context_if_present(context_id);
+ if (!context || context->presents_from_cpu_memory == presents_from_cpu_memory)
return;
auto previous_gpu_sharing = gpu_sharing_for_context(context_id);
- context->set_presents_from_cpu_memory(presents_from_cpu_memory);
+ context->presents_from_cpu_memory = presents_from_cpu_memory;
// NB: Without a GPU, the backing stores are in CPU memory either way.
if (!m_skia_backend_context || gpu_sharing_for_context(context_id) == previous_gpu_sharing)
return;
// NB: Otherwise, the presentation would read every frame back from GPU memory on its main thread.
- context->presenter().invalidate_backing_stores();
+ context->presenter.invalidate_backing_stores();
resize_backing_stores_if_needed(context_id, *context);
}
-void CompositorState::resume_presentation_after_becoming_visible(Web::CompositorContextId root_context_id, ContextState& root_context)
+void CompositorState::resume_presentation_after_becoming_visible(Web::CompositorContextId root_context_id, PresentedContext& root_context)
{
schedule_animated_image_update({});
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto& context = *context_entry.value;
if (root_context_of(context) != &root_context)
continue;
- schedule_animation_frames_if_needed(context);
- if (context.display_tick_requested())
+ schedule_animation_frames_if_needed(context_entry.key, context);
+ if ((context.rendering_opportunity_request.is_requested() || context.clock_tick_request.is_requested()))
vsync_scheduler_for_display(display_id_for_context(context)).schedule(display_refresh_rate_for_context(context));
}
- if (auto frame_rect_to_present = root_context.presenter().frame_rect_to_repaint(); frame_rect_to_present.has_value())
+ if (auto frame_rect_to_present = root_context.presenter.frame_rect_to_repaint(); frame_rect_to_present.has_value())
schedule_present_frame(root_context_id, root_context, *frame_rect_to_present);
}
void CompositorState::request_rendering_opportunity(Web::CompositorContextId context_id, double maximum_frames_per_second)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
- if (!context->request_rendering_opportunity(maximum_frames_per_second))
+ if (!context->rendering_opportunity_request.request(maximum_frames_per_second))
return;
if (!context_is_effectively_visible(*context))
return;
@@ -1769,10 +1028,10 @@ void CompositorState::request_rendering_opportunity(Web::CompositorContextId con
// INTEROP: Like Chromium's missed BeginFrame delivery, reuse a recent display tick when a context requests its
// next opportunity after the tick has already happened. This keeps heavy frames from waiting an extra tick.
if (auto frame_time = scheduler.most_recent_tick_time(MonotonicTime::now(), display_refresh_rate);
- frame_time.has_value() && context->rendering_opportunity_is_due(*frame_time, display_refresh_rate)) {
- auto frame_interval = context->rendering_opportunity_frame_interval(display_refresh_rate);
- context->did_deliver_rendering_opportunity(*frame_time);
- web_content_client_of(*context).rendering_opportunity(context_id, frame_time->nanoseconds(), frame_interval);
+ frame_time.has_value() && context->rendering_opportunity_request.is_due(*frame_time, display_refresh_rate)) {
+ auto frame_interval = context->rendering_opportunity_request.frame_interval(display_refresh_rate);
+ context->rendering_opportunity_request.did_deliver(*frame_time);
+ context->web_content_client.rendering_opportunity(context_id, frame_time->nanoseconds(), frame_interval);
return;
}
@@ -1781,39 +1040,39 @@ void CompositorState::request_rendering_opportunity(Web::CompositorContextId con
void CompositorState::request_clock_tick(Web::CompositorContextId context_id, double maximum_frames_per_second)
{
- auto* context = context_if_present(context_id);
- if (!context || !context->request_clock_tick(maximum_frames_per_second) || !context_is_effectively_visible(*context))
+ auto* context = presented_context_if_present(context_id);
+ if (!context || !context->clock_tick_request.request(maximum_frames_per_second) || !context_is_effectively_visible(*context))
return;
vsync_scheduler_for_display(display_id_for_context(*context)).schedule(display_refresh_rate_for_context(*context));
}
void CompositorState::hurry_rendering_opportunity(Web::CompositorContextId context_id)
{
- auto* context = context_if_present(context_id);
- if (!context || !context->rendering_opportunity_requested() || !context_is_effectively_visible(*context))
+ auto* context = presented_context_if_present(context_id);
+ if (!context || !context->rendering_opportunity_request.is_requested() || !context_is_effectively_visible(*context))
return;
auto display_refresh_rate = display_refresh_rate_for_context(*context);
- auto frame_interval = context->rendering_opportunity_frame_interval(display_refresh_rate);
+ auto frame_interval = context->rendering_opportunity_request.frame_interval(display_refresh_rate);
auto frame_time = MonotonicTime::now();
- context->did_deliver_rendering_opportunity(frame_time);
- web_content_client_of(*context).rendering_opportunity(context_id, frame_time.nanoseconds(), frame_interval);
+ context->rendering_opportunity_request.did_deliver(frame_time);
+ context->web_content_client.rendering_opportunity(context_id, frame_time.nanoseconds(), frame_interval);
}
void CompositorState::present_frame(Web::CompositorContextId context_id, Gfx::IntRect viewport_rect)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
// The frame joins whatever is queued, so a frame queued earlier cannot be presented after it.
- context->presenter().queue_present_frame({ viewport_rect, {} });
+ context->presenter.queue_present_frame({ viewport_rect, {} });
if (try_present_frame_during_resize(context_id, *context))
return;
schedule_pending_present_frame(context_id, *context);
}
-void CompositorState::present_frame(Web::CompositorContextId context_id, ContextState& context, PendingFrame pending_frame)
+void CompositorState::present_frame(Web::CompositorContextId context_id, PresentedContext& context, PendingFrame pending_frame)
{
auto composited_context_resolver = resolver_for(context_id);
- auto prepared_frame = context.presenter().prepare_frame(*m_display_list_player, pending_frame, context.frame_inputs(MonotonicTime::now()), &composited_context_resolver);
+ auto prepared_frame = context.presenter.prepare_frame(*m_display_list_player, pending_frame, m_core.frame_inputs(context_id, MonotonicTime::now()), &composited_context_resolver);
if (!prepared_frame.has_value())
return;
@@ -1828,12 +1087,12 @@ void CompositorState::present_frame(Web::CompositorContextId context_id, Context
self->did_finish_async_present(*pending_present);
});
});
- context.presenter().did_submit_prepared_frame(pending_frame.viewport_rect);
+ context.presenter.did_submit_prepared_frame(pending_frame.viewport_rect);
#ifdef AK_OS_MACOS
// The window server's reads of an IOSurface wait for GPU work that is already scheduled to write it. Handing the
// frame to the client before that work finishes keeps it ahead of the window server's deadline for the next refresh.
// A client that reads frames from CPU memory does not wait for that work, so it gets each frame once it is done.
- if (context.presents_to_client() && m_skia_backend_context && !context.presents_from_cpu_memory()) {
+ if (context.presenter.presents_to_client() && m_skia_backend_context && !context.presents_from_cpu_memory) {
m_skia_backend_context->metal_context().when_committed_work_is_scheduled([self = NonnullRefPtr { *this }, &event_loop, pending_present]() mutable {
event_loop.deferred_invoke([self = move(self), pending_present] {
for (auto& entry : self->m_pending_async_presents) {
@@ -1849,21 +1108,16 @@ void CompositorState::present_frame(Web::CompositorContextId context_id, Context
schedule_gpu_completion_check();
}
-void CompositorState::request_present(Web::CompositorContextId context_id, PendingFrame const& frame)
-{
- m_present_requests.append({ context_id, frame.viewport_rect, frame.forced_damage_rect });
-}
-
void CompositorState::schedule_requested_presents()
{
- auto requests = move(m_present_requests);
+ auto requests = m_core.take_present_requests();
for (auto const& request : requests) {
- auto* context = context_if_present(request.context_id);
+ auto* context = presented_context_if_present(request.context_id);
if (!context)
continue;
auto viewport_rect = request.viewport_rect;
if (!viewport_rect.has_value())
- viewport_rect = context->presenter().viewport_rect_for_ui_overlay();
+ viewport_rect = context->presenter.viewport_rect_for_ui_overlay();
if (!viewport_rect.has_value())
continue;
switch (request.scheduling) {
@@ -1871,77 +1125,77 @@ void CompositorState::schedule_requested_presents()
schedule_present_frame(request.context_id, *context, PendingFrame { *viewport_rect, request.forced_damage_rect });
break;
case PresentScheduling::WithDisplayTickInProgress:
- context->presenter().queue_present_frame(PendingFrame { *viewport_rect, request.forced_damage_rect });
+ context->presenter.queue_present_frame(PendingFrame { *viewport_rect, request.forced_damage_rect });
break;
}
}
}
-void CompositorState::schedule_present_frame(Web::CompositorContextId context_id, ContextState& context, PendingFrame pending_frame)
+void CompositorState::schedule_present_frame(Web::CompositorContextId context_id, PresentedContext& context, PendingFrame pending_frame)
{
- context.presenter().queue_present_frame(pending_frame);
+ context.presenter.queue_present_frame(pending_frame);
schedule_pending_present_frame(context_id, context);
}
-void CompositorState::schedule_present_frame(Web::CompositorContextId context_id, ContextState& context, Gfx::IntRect viewport_rect)
+void CompositorState::schedule_present_frame(Web::CompositorContextId context_id, PresentedContext& context, Gfx::IntRect viewport_rect)
{
schedule_present_frame(context_id, context, PendingFrame::repainting_everything(viewport_rect));
}
-void CompositorState::schedule_pending_present_frame(Web::CompositorContextId context_id, ContextState& context)
+void CompositorState::schedule_pending_present_frame(Web::CompositorContextId context_id, PresentedContext& context)
{
- if (!context.presents_to_client()) {
+ if (!context.presenter.presents_to_client()) {
schedule_containing_context_present(context);
// A nested context is presented through its containing context, but its
// own async animations still need a vsync source. The containing frame
// may already be up to date (and therefore not schedule a new present),
// so explicitly keep the effective display's scheduler ticking while
// a nested animation is active.
- schedule_animation_frames_if_needed(context);
+ schedule_animation_frames_if_needed(context_id, context);
return;
}
schedule_pending_present_frame_on_vsync(context_id, context);
}
-static bool needs_animation_frames(ContextState& context, MonotonicTime now)
+bool CompositorState::needs_animation_frames(Web::CompositorContextId context_id, PresentedContext const& context, MonotonicTime now)
{
- return context.needs_animation_frames(now) || context.presenter().ui_overlay_fades_need_frame();
+ return m_core.needs_animation_frames(context_id, now) || context.presenter.ui_overlay_fades_need_frame();
}
-static bool has_pending_present_frame_scheduled_on(ContextState const& context, Optional<u64> display_id)
+bool CompositorState::has_pending_present_frame_scheduled_on(PresentedContext const& context, Optional<u64> display_id)
{
- return context.presenter().pending_present_frame_scheduled() && context.display_id() == display_id;
+ return context.presenter.pending_present_frame_scheduled() && context.display_id == display_id;
}
-void CompositorState::schedule_animation_frames_if_needed(ContextState& context)
+void CompositorState::schedule_animation_frames_if_needed(Web::CompositorContextId context_id, PresentedContext const& context)
{
- if (needs_animation_frames(context, MonotonicTime::now()) && context_is_effectively_visible(context))
+ if (needs_animation_frames(context_id, context, MonotonicTime::now()) && context_is_effectively_visible(context))
vsync_scheduler_for_display(display_id_for_context(context)).schedule(display_refresh_rate_for_context(context));
}
-void CompositorState::schedule_pending_present_frame_on_vsync(Web::CompositorContextId, ContextState& context)
+void CompositorState::schedule_pending_present_frame_on_vsync(Web::CompositorContextId, PresentedContext& context)
{
if (!context_is_effectively_visible(context))
return;
- context.presenter().mark_pending_present_frame_scheduled();
- vsync_scheduler_for_display(context.display_id()).schedule(context.display_refresh_rate());
+ context.presenter.mark_pending_present_frame_scheduled();
+ vsync_scheduler_for_display(context.display_id).schedule(context.display_refresh_rate);
}
-void CompositorState::schedule_containing_context_present(ContextState& context)
+void CompositorState::schedule_containing_context_present(PresentedContext& context)
{
- auto parent_context_id = context.parent_context_id();
+ auto parent_context_id = context.parent_context_id;
if (!parent_context_id.has_value())
return;
- auto* parent_context = context_if_present(*parent_context_id);
+ auto* parent_context = presented_context_if_present(*parent_context_id);
VERIFY(parent_context);
present_current_frame(*parent_context_id, *parent_context);
}
-void CompositorState::schedule_pending_present_frame_if_unblocked(Web::CompositorContextId context_id, ContextState& context)
+void CompositorState::schedule_pending_present_frame_if_unblocked(Web::CompositorContextId context_id, PresentedContext& context)
{
- if (!context.presenter().can_schedule_pending_present_frame_if_unblocked())
+ if (!context.presenter.can_schedule_pending_present_frame_if_unblocked())
return;
if (try_present_frame_during_resize(context_id, context))
@@ -1949,11 +1203,11 @@ void CompositorState::schedule_pending_present_frame_if_unblocked(Web::Composito
schedule_pending_present_frame(context_id, context);
}
-bool CompositorState::try_present_frame_during_resize(Web::CompositorContextId context_id, ContextState& context)
+bool CompositorState::try_present_frame_during_resize(Web::CompositorContextId context_id, PresentedContext& context)
{
- if (!context.presenter().window_resize_in_progress() || !context.presents_to_client() || !context_is_effectively_visible(context))
+ if (!context.presenter.window_resize_in_progress() || !context.presenter.presents_to_client() || !context_is_effectively_visible(context))
return false;
- auto pending_frame = context.presenter().take_pending_present_frame_if_unblocked();
+ auto pending_frame = context.presenter.take_pending_present_frame_if_unblocked();
if (!pending_frame.has_value())
return false;
@@ -1974,55 +1228,25 @@ VSyncScheduler& CompositorState::vsync_scheduler_for_display(Optional<u64> displ
});
}
-DisplayTickResult CompositorState::display_tick(Web::CompositorContextId context_id, ContextState& context, MonotonicTime frame_time, MonotonicTime now, DisplayTickInputs const& inputs)
-{
- auto has_active_animation_on_display = (context.needs_animation_frames(now) || inputs.ui_overlay_fades_need_frame) && inputs.context_is_on_tick_display;
- auto presents_on_display = inputs.present_is_scheduled_on_tick_display || has_active_animation_on_display;
- if (presents_on_display) {
- if (inputs.context_is_on_tick_display)
- dispatch_scroll_fling_step(context_id, context, context.take_scroll_fling_step(frame_time), frame_time);
- if (auto animation_frame = context.advance_smooth_scroll_animations(frame_time); animation_frame.has_value())
- m_present_requests.append({ context_id, *animation_frame, {}, PresentScheduling::WithDisplayTickInProgress });
- if (auto rebound_frame = context.advance_elastic_overscroll_rebounds(frame_time); rebound_frame.has_value())
- m_present_requests.append({ context_id, *rebound_frame, {}, PresentScheduling::WithDisplayTickInProgress });
- if (auto damage_rect = context.advance_scrollbar_fades_and_return_damage_rect(frame_time); !damage_rect.is_empty())
- m_present_requests.append({ context_id, {}, damage_rect, PresentScheduling::WithDisplayTickInProgress });
- }
-
- // A render clock tick goes to the process's render clock thread, beside what its main thread is doing, with
- // where the compositor has scrolled to for this vsync, which that main thread may not have taken in yet.
- if (inputs.clock_tick_interval_in_milliseconds.has_value())
- context.post_clock_tick(frame_time, *inputs.clock_tick_interval_in_milliseconds);
-
- if (presents_on_display) {
- publish_pending_async_scroll_updates(context_id, context);
- if (context.visual_animations_need_frame(now)) {
- context.advance_visual_animations(frame_time);
- m_present_requests.append({ context_id, {}, {}, PresentScheduling::WithDisplayTickInProgress });
- }
- }
- return { .presents_on_display = presents_on_display, .needs_animation_frames = context.needs_animation_frames(now) };
-}
-
void CompositorState::present_pending_frames_on_vsync(Optional<u64> display_id, MonotonicTime frame_time)
{
update_video_sinks_for_display(display_id);
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto context_id = context_entry.key;
auto& context = *context_entry.value;
auto now = MonotonicTime::now();
if (!context_is_effectively_visible(context)) {
- context.presenter().unschedule_pending_present_frame();
+ context.presenter.unschedule_pending_present_frame();
continue;
}
- if (context.rendering_opportunity_requested() && display_id_for_context(context) == display_id) {
+ if (context.rendering_opportunity_request.is_requested() && display_id_for_context(context) == display_id) {
auto display_refresh_rate = display_refresh_rate_for_context(context);
- if (context.rendering_opportunity_is_due(frame_time, display_refresh_rate)) {
- auto frame_interval = context.rendering_opportunity_frame_interval(display_refresh_rate);
- context.did_deliver_rendering_opportunity(frame_time);
- web_content_client_of(context).rendering_opportunity(context_id, frame_time.nanoseconds(), frame_interval);
+ if (context.rendering_opportunity_request.is_due(frame_time, display_refresh_rate)) {
+ auto frame_interval = context.rendering_opportunity_request.frame_interval(display_refresh_rate);
+ context.rendering_opportunity_request.did_deliver(frame_time);
+ context.web_content_client.rendering_opportunity(context_id, frame_time.nanoseconds(), frame_interval);
} else {
vsync_scheduler_for_display(display_id).schedule(display_refresh_rate);
}
@@ -2032,30 +1256,29 @@ void CompositorState::present_pending_frames_on_vsync(Optional<u64> display_id,
DisplayTickInputs inputs {
.context_is_on_tick_display = context_is_on_tick_display,
.present_is_scheduled_on_tick_display = has_pending_present_frame_scheduled_on(context, display_id),
- .ui_overlay_fades_need_frame = context.presenter().ui_overlay_fades_need_frame(),
+ .ui_overlay_fades_need_frame = context.presenter.ui_overlay_fades_need_frame(),
.clock_tick_interval_in_milliseconds = {},
};
- if (context.clock_tick_requested() && context_is_on_tick_display) {
+ if (context.clock_tick_request.is_requested() && context_is_on_tick_display) {
auto display_refresh_rate = display_refresh_rate_for_context(context);
- if (context.clock_tick_is_due(frame_time, display_refresh_rate)) {
- inputs.clock_tick_interval_in_milliseconds = context.clock_tick_interval(display_refresh_rate);
- context.did_deliver_clock_tick(frame_time);
+ if (context.clock_tick_request.is_due(frame_time, display_refresh_rate)) {
+ inputs.clock_tick_interval_in_milliseconds = context.clock_tick_request.frame_interval(display_refresh_rate);
+ context.clock_tick_request.did_deliver(frame_time);
} else {
vsync_scheduler_for_display(display_id).schedule(display_refresh_rate);
}
}
- VERIFY(m_present_requests.is_empty());
- auto tick = display_tick(context_id, context, frame_time, now, inputs);
+ auto tick = m_core.display_tick(context_id, frame_time, now, inputs);
schedule_requested_presents();
if (!tick.presents_on_display)
continue;
- if (auto fade_frame = context.presenter().advance_ui_overlay_fades(frame_time); fade_frame.has_value())
- context.presenter().queue_present_frame(*fade_frame);
+ if (auto fade_frame = context.presenter.advance_ui_overlay_fades(frame_time); fade_frame.has_value())
+ context.presenter.queue_present_frame(*fade_frame);
add_backing_store_for_pending_frame_if_needed(context_id, context);
- auto needs_animation_frames = tick.needs_animation_frames || context.presenter().ui_overlay_fades_need_frame();
- auto pending_present_frame = context.presenter().take_pending_present_frame_if_unblocked();
+ auto needs_animation_frames = tick.needs_animation_frames || context.presenter.ui_overlay_fades_need_frame();
+ auto pending_present_frame = context.presenter.take_pending_present_frame_if_unblocked();
if (!pending_present_frame.has_value()) {
if (has_pending_present_frame_scheduled_on(context, display_id) || (needs_animation_frames && context_is_on_tick_display))
vsync_scheduler_for_display(display_id).schedule(display_refresh_rate_for_context(context));
@@ -2070,17 +1293,17 @@ void CompositorState::present_pending_frames_on_vsync(Optional<u64> display_id,
bool CompositorState::request_screenshot(Web::CompositorContextId context_id, Gfx::ShareableBitmap& target_bitmap)
{
auto now = MonotonicTime::now();
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
- if (!context->presenter().has_display_list() || !target_bitmap.is_valid() || !target_bitmap.bitmap())
+ if (!context->presenter.has_display_list() || !target_bitmap.is_valid() || !target_bitmap.bitmap())
return false;
auto composited_context_resolver = resolver_for(context_id);
- auto screenshot_inputs = context->frame_inputs_for_screenshot(now);
- context->presenter().paint_screenshot(*m_display_list_player, target_bitmap, screenshot_inputs.frame_inputs, &composited_context_resolver);
+ auto screenshot_inputs = m_core.frame_inputs_for_screenshot(context_id, now);
+ context->presenter.paint_screenshot(*m_display_list_player, target_bitmap, screenshot_inputs.frame_inputs, &composited_context_resolver);
if (!screenshot_inputs.scrollbar_fade_damage_rect.is_empty()) {
- if (auto viewport_rect = context->presenter().viewport_rect_for_ui_overlay(); viewport_rect.has_value())
+ if (auto viewport_rect = context->presenter.viewport_rect_for_ui_overlay(); viewport_rect.has_value())
schedule_present_frame(context_id, *context, { *viewport_rect, screenshot_inputs.scrollbar_fade_damage_rect });
}
return true;
@@ -2088,11 +1311,11 @@ bool CompositorState::request_screenshot(Web::CompositorContextId context_id, Gf
void CompositorState::presented_bitmap_ready_to_paint(Web::CompositorContextId context_id, i32 bitmap_id)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
if (!context)
return;
- if (!context->presenter().acknowledge_presented_bitmap(bitmap_id))
+ if (!context->presenter.acknowledge_presented_bitmap(bitmap_id))
return;
schedule_pending_present_frame_if_unblocked(context_id, *context);
@@ -2121,21 +1344,21 @@ void CompositorState::deliver_rendered_frame(PendingAsyncPresent& pending_presen
return;
auto context_id = pending_present.context_id;
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
VERIFY(context);
- context->presenter().did_finish_gpu_present(pending_present.bitmap_id);
+ context->presenter.did_finish_gpu_present(pending_present.bitmap_id);
auto* presentation = pending_present.publish_to_presentation ? presentation_for_context(context_id) : nullptr;
- if (context->presents_to_client() && !presentation) {
+ if (context->presenter.presents_to_client() && !presentation) {
// Nobody shows the frame, so it is released here, on another turn, as a presentation would release it.
Core::deferred_invoke([self = NonnullRefPtr { *this }, context_id, bitmap_id = pending_present.bitmap_id] {
self->presented_bitmap_ready_to_paint(context_id, bitmap_id);
});
- } else if (context->presents_to_client()) {
- if (auto* log = Core::StatsLog::the(); log && context->page_id().has_value()) {
+ } else if (context->presenter.presents_to_client()) {
+ if (auto* log = Core::StatsLog::the(); log && context->page_id.has_value()) {
auto const& damage = pending_present.damage_rect;
JsonObject data;
- data.set("page_id"sv, *context->page_id());
+ data.set("page_id"sv, *context->page_id);
data.set("changed"sv, !damage.is_empty());
data.set("damage_x"sv, damage.x());
data.set("damage_y"sv, damage.y());
@@ -2146,8 +1369,8 @@ void CompositorState::deliver_rendered_frame(PendingAsyncPresent& pending_presen
presentation->did_present_frame(context_id, pending_present.viewport_rect, pending_present.damage_rect, pending_present.bitmap_id, pending_present.device_pixel_ratio);
}
resize_backing_stores_if_needed(context_id, *context);
- if (auto parent_context_id = context->parent_context_id(); parent_context_id.has_value()) {
- auto* parent_context = context_if_present(*parent_context_id);
+ if (auto parent_context_id = context->parent_context_id; parent_context_id.has_value()) {
+ auto* parent_context = presented_context_if_present(*parent_context_id);
VERIFY(parent_context);
present_current_frame(*parent_context_id, *parent_context);
}
@@ -2193,30 +1416,31 @@ void CompositorState::check_gpu_completions()
m_gpu_completion_timer->stop();
}
-ContextState* CompositorState::context_if_present(Web::CompositorContextId context_id)
+CompositorState::PresentedContext* CompositorState::presented_context_if_present(Web::CompositorContextId context_id)
{
- auto it = m_contexts.find(context_id);
- if (it == m_contexts.end())
+ auto it = m_presented_contexts.find(context_id);
+ if (it == m_presented_contexts.end())
return nullptr;
return it->value.ptr();
}
-ContextState const* CompositorState::context_if_present(Web::CompositorContextId context_id) const
+CompositorState::PresentedContext const* CompositorState::presented_context_if_present(Web::CompositorContextId context_id) const
{
- auto it = m_contexts.find(context_id);
- if (it == m_contexts.end())
+ auto it = m_presented_contexts.find(context_id);
+ if (it == m_presented_contexts.end())
return nullptr;
return it->value.ptr();
}
-void CompositorState::clear_parent_context(ContextState& context)
+void CompositorState::clear_parent_context(Web::CompositorContextId context_id, PresentedContext& context)
{
- auto parent_context_id = context.parent_context_id();
+ auto parent_context_id = context.parent_context_id;
if (!parent_context_id.has_value())
return;
- context.set_parent_context({});
- auto* parent_context = context_if_present(*parent_context_id);
+ context.parent_context_id = {};
+ m_core.set_parent_context(context_id, {});
+ auto* parent_context = presented_context_if_present(*parent_context_id);
if (!parent_context)
return;
present_current_frame(*parent_context_id, *parent_context);
@@ -2231,31 +1455,31 @@ CompositedContextResolver CompositorState::resolver_for(Web::CompositorContextId
CompositedContextSurface CompositorState::resolve_composited_context(Web::CompositorContextId parent_context_id, Web::CompositorContextId child_context_id, Gfx::FloatRect destination_rect, Gfx::FloatMatrix4x4 const& canvas_transform)
{
- auto* child_context = context_if_present(child_context_id);
+ auto* child_context = presented_context_if_present(child_context_id);
if (!child_context)
return {};
- auto child_parent_context_id = child_context->parent_context_id();
+ auto child_parent_context_id = child_context->parent_context_id;
if (!child_parent_context_id.has_value() || *child_parent_context_id != parent_context_id)
return {};
- if (child_context->presenter().update_composited_raster_transform(destination_rect, canvas_transform)) {
- auto publication = child_context->presenter().resize_backing_stores_if_needed(m_skia_backend_context, BackingStoreManager::GpuSharing::Disallowed, BackingStoreManager::PublicationMode::Unpublished);
+ if (child_context->presenter.update_composited_raster_transform(destination_rect, canvas_transform)) {
+ auto publication = child_context->presenter.resize_backing_stores_if_needed(m_skia_backend_context, BackingStoreManager::GpuSharing::Disallowed, BackingStoreManager::PublicationMode::Unpublished);
VERIFY(!publication.has_value());
}
- if (child_context->presenter().needs_rasterization()) {
+ if (child_context->presenter.needs_rasterization()) {
auto composited_context_resolver = resolver_for(child_context_id);
DisplayListPlayerSkia display_list_player { m_skia_backend_context };
- child_context->presenter().present_synchronously(display_list_player, child_context->frame_inputs(MonotonicTime::now()), &composited_context_resolver);
+ child_context->presenter.present_synchronously(display_list_player, m_core.frame_inputs(child_context_id, MonotonicTime::now()), &composited_context_resolver);
}
- return child_context->presenter().composited_surface();
+ return child_context->presenter.composited_surface();
}
-void CompositorState::resize_backing_stores_if_needed(Web::CompositorContextId context_id, ContextState& context)
+void CompositorState::resize_backing_stores_if_needed(Web::CompositorContextId context_id, PresentedContext& context)
{
auto publication_mode = presentation_for_context(context_id) ? BackingStoreManager::PublicationMode::Published : BackingStoreManager::PublicationMode::Unpublished;
- if (auto publication = context.presenter().resize_backing_stores_if_needed(m_skia_backend_context, gpu_sharing_for_context(context_id), publication_mode); publication.has_value()) {
+ if (auto publication = context.presenter.resize_backing_stores_if_needed(m_skia_backend_context, gpu_sharing_for_context(context_id), publication_mode); publication.has_value()) {
publish_backing_stores(context_id, context, publication.release_value());
present_current_frame(context_id, context);
}
@@ -2263,7 +1487,7 @@ void CompositorState::resize_backing_stores_if_needed(Web::CompositorContextId c
BackingStoreManager::GpuSharing CompositorState::gpu_sharing_for_context(Web::CompositorContextId context_id) const
{
- if (auto const* context = context_if_present(context_id); context && context->presents_from_cpu_memory())
+ if (auto const* context = presented_context_if_present(context_id); context && context->presents_from_cpu_memory)
return BackingStoreManager::GpuSharing::Disallowed;
#ifdef USE_DIRECTX
// Shared Direct3D textures can only be opened by the presentation if it presents on the same adapter.
@@ -2281,9 +1505,9 @@ BackingStoreManager::GpuSharing CompositorState::gpu_sharing_for_context(Web::Co
// its root context does. A platform that reports the momentum of a flick reports when the fingers lift for it.
void CompositorState::update_scroll_capabilities()
{
- for (auto& [context_id, context] : m_contexts) {
+ for (auto& [context_id, context] : m_presented_contexts) {
ContextState::ScrollCapabilities capabilities;
- if (auto* presentation = presentation_for_context(root_context_of(*context)->context_id())) {
+ if (auto* presentation = presentation_for_context(root_context_id_of(context_id))) {
auto const& presentation_capabilities = *m_presentations.get(presentation);
capabilities.synthesizes_scroll_momentum = presentation_capabilities.synthesizes_scroll_momentum;
capabilities.scrolls_elastically = presentation_capabilities.scrolls_elastically;
@@ -2291,7 +1515,7 @@ void CompositorState::update_scroll_capabilities()
if (!presentation_capabilities.synthesizes_scroll_momentum)
capabilities.touchpad_gesture_end = Compositing::TouchpadGestureEnd::WhenFingersLift;
}
- context->set_scroll_capabilities(capabilities);
+ m_core.set_scroll_capabilities(context_id, capabilities);
}
}
@@ -2303,53 +1527,33 @@ void CompositorState::set_scrollbar_track_press_jumps(CompositorStateClient& pre
String CompositorState::scroll_state_for_testing(Web::CompositorContextId context_id, Web::AsyncScrollNodeStableID stretched_scroller) const
{
- auto const* context = context_if_present(context_id);
- if (!context)
- return {};
- return context->scroll_state_for_testing(stretched_scroller);
+ return m_core.scroll_state_for_testing(context_id, stretched_scroller);
}
Optional<Compositing::FirstWheelStepRouteForTesting> CompositorState::route_first_wheel_step_for_testing(Web::CompositorContextId context_id, Gfx::FloatPoint position, Gfx::FloatPoint delta_in_css_pixels)
{
- auto* context = context_if_present(context_id);
- if (!context)
- return {};
- if (!context->presents_to_client() || !context->may_hand_first_wheel_steps_to_nested_contexts())
- return context->route_first_wheel_step_for_testing(position, delta_in_css_pixels, Compositing::NestedContextRegions::LeaveToMainThread);
- auto hit = context->nested_context_for_wheel_at(position);
- if (!hit.has_value())
- return context->route_first_wheel_step_for_testing(position, delta_in_css_pixels, Compositing::NestedContextRegions::HandToNestedContext);
- auto taking_context = first_wheel_step_in_nested_contexts(context_id, *hit, delta_in_css_pixels, Web::WheelDeltaPrecision::Precise).taking_context;
- if (!taking_context.has_value())
- return context->route_first_wheel_step_for_testing(position, delta_in_css_pixels, Compositing::NestedContextRegions::Ignore);
- // Node ids are only unique within the process that made them.
- bool ids_are_of_this_process = taking_context->context && taking_context->context->is_owned_by(context->web_content_client_id());
- Compositing::FirstWheelStepRouteForTesting route { .route = { taking_context->route.decision }, .document_id = {} };
- if (ids_are_of_this_process) {
- route.route.scroller = taking_context->route.scroller;
- route.document_id = taking_context->context->document_id();
- }
- return route;
+ auto answer = m_core.route_first_wheel_step_for_testing(context_id, position, delta_in_css_pixels);
+ schedule_requested_presents();
+ return answer;
}
void CompositorState::begin_input_trace_for_testing(Web::CompositorContextId context_id)
{
- if (auto* context = context_if_present(context_id))
- context->input_trace().begin();
+ m_core.begin_input_trace_for_testing(context_id);
+ schedule_requested_presents();
}
void CompositorState::clear_per_test_input_state_for_testing(Web::CompositorContextId context_id)
{
- if (auto* context = context_if_present(context_id))
- context->clear_per_test_input_state();
+ m_core.clear_per_test_input_state_for_testing(context_id);
+ schedule_requested_presents();
}
Vector<Web::InputTraceRecord> CompositorState::take_input_trace_for_testing(Web::CompositorContextId context_id)
{
- auto* context = context_if_present(context_id);
- if (!context)
- return {};
- return context->input_trace().take();
+ auto answer = m_core.take_input_trace_for_testing(context_id);
+ schedule_requested_presents();
+ return answer;
}
void CompositorState::set_client_gpu_presentation_capability(CompositorStateClient& presentation, bool supported, u64 adapter_luid)
@@ -2364,38 +1568,38 @@ void CompositorState::set_client_gpu_presentation_capability(CompositorStateClie
capabilities.cannot_present_gpu_images = !supported;
// Reallocate the backing stores of the presentation's contexts so they match the new capability.
- for (auto& context_entry : m_contexts) {
+ for (auto& context_entry : m_presented_contexts) {
auto& context = *context_entry.value;
if (presentation_for_context(context_entry.key) != &presentation)
continue;
- context.presenter().invalidate_backing_stores();
+ context.presenter.invalidate_backing_stores();
resize_backing_stores_if_needed(context_entry.key, context);
}
}
-void CompositorState::schedule_backing_store_shrink(Web::CompositorContextId context_id, ContextState& context)
+void CompositorState::schedule_backing_store_shrink(Web::CompositorContextId context_id, PresentedContext& context)
{
- context.presenter().schedule_backing_store_shrink([this, context_id] {
+ context.presenter.schedule_backing_store_shrink([this, context_id] {
shrink_backing_stores_after_resize(context_id);
});
}
void CompositorState::shrink_backing_stores_after_resize(Web::CompositorContextId context_id)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
if (!context)
return;
- context->presenter().finish_window_resize();
+ context->presenter.finish_window_resize();
resize_backing_stores_if_needed(context_id, *context);
}
-void CompositorState::add_backing_store_for_pending_frame_if_needed(Web::CompositorContextId context_id, ContextState& context)
+void CompositorState::add_backing_store_for_pending_frame_if_needed(Web::CompositorContextId context_id, PresentedContext& context)
{
auto* presentation = presentation_for_context(context_id);
if (!presentation)
return;
- auto publication = context.presenter().add_backing_store_for_pending_frame_if_needed(m_skia_backend_context);
+ auto publication = context.presenter.add_backing_store_for_pending_frame_if_needed(m_skia_backend_context);
if (!publication.has_value())
return;
@@ -2404,52 +1608,39 @@ void CompositorState::add_backing_store_for_pending_frame_if_needed(Web::Composi
schedule_surplus_backing_store_retirement(context_id, context);
}
-void CompositorState::schedule_surplus_backing_store_retirement(Web::CompositorContextId context_id, ContextState& context)
+void CompositorState::schedule_surplus_backing_store_retirement(Web::CompositorContextId context_id, PresentedContext& context)
{
- context.presenter().schedule_surplus_backing_store_retirement([this, context_id] {
+ context.presenter.schedule_surplus_backing_store_retirement([this, context_id] {
retire_idle_surplus_backing_stores(context_id);
});
}
void CompositorState::retire_idle_surplus_backing_stores(Web::CompositorContextId context_id)
{
- auto* context = context_if_present(context_id);
+ auto* context = presented_context_if_present(context_id);
if (!context)
return;
- auto retired_bitmap_ids = context->presenter().retire_idle_surplus_backing_stores();
+ auto retired_bitmap_ids = context->presenter.retire_idle_surplus_backing_stores();
if (auto* presentation = presentation_for_context(context_id); presentation && !retired_bitmap_ids.is_empty()) {
dbgln_if(COMPOSITOR_DEBUG, "[Compositor] Context {} retired idle backing stores {}", context_id, retired_bitmap_ids);
presentation->did_retire_backing_stores(context_id, move(retired_bitmap_ids));
}
- if (context->presenter().has_surplus_backing_stores())
+ if (context->presenter.has_surplus_backing_stores())
schedule_surplus_backing_store_retirement(context_id, *context);
}
-void CompositorState::present_current_frame(Web::CompositorContextId context_id, ContextState& context)
+void CompositorState::present_current_frame(Web::CompositorContextId context_id, PresentedContext& context)
{
- if (auto frame_to_present = context.presenter().frame_rect_to_repaint(); frame_to_present.has_value())
+ if (auto frame_to_present = context.presenter.frame_rect_to_repaint(); frame_to_present.has_value())
schedule_present_frame(context_id, context, *frame_to_present);
}
-bool CompositorState::apply_context_update_result(
- Web::CompositorContextId context_id,
- ContextState& context,
- ContextState::ContextUpdateResult const& result)
-{
- if (result.frame_to_present.has_value())
- request_present(context_id, *result.frame_to_present);
- publish_pending_async_scroll_updates(context_id, context);
- if (result.should_request_rendering_update)
- context.request_rendering_update();
- return result.accepted;
-}
-
-void CompositorState::publish_backing_stores(Web::CompositorContextId context_id, ContextState& context, BackingStoreManager::Publication&& publication)
+void CompositorState::publish_backing_stores(Web::CompositorContextId context_id, PresentedContext& context, BackingStoreManager::Publication&& publication)
{
auto* presentation = presentation_for_context(context_id);
VERIFY(presentation);
- VERIFY(context.presents_to_client());
+ VERIFY(context.presenter.presents_to_client());
presentation->did_allocate_backing_stores(context_id, move(publication.bitmap_ids), move(publication.shared_images));
}
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index 484a4048aca..90b19cf3751 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -15,9 +15,10 @@
#include <AK/RefCounted.h>
#include <Compositor/AnimatedImage.h>
#include <Compositor/CanvasSurfaceRegistry.h>
-#include <Compositor/CompositorCoreHost.h>
-#include <Compositor/ContextState.h>
+#include <Compositor/CompositorCore.h>
+#include <Compositor/ContextPresenter.h>
#include <Compositor/DisplayListPlayerSkia.h>
+#include <Compositor/FramePacer.h>
#include <Compositor/SkiaBackendContext.h>
#include <Compositor/VSyncScheduler.h>
#include <LibCompositing/DisplayList/AccumulatedVisualContext.h>
@@ -182,15 +183,7 @@ public:
private:
CompositorState(RefPtr<Gfx::SkiaBackendContext>);
- template<typename Message, typename... Arguments>
- void post_to_presentation(PresentationId presentation_id, Arguments&&... arguments)
- {
- auto buffer = MUST(Message::static_encode(forward<Arguments>(arguments)...));
- post_message_to_presentation(presentation_id, buffer);
- }
-
CompositorStateWebContentClient* web_content_client_if_present(WebContentClientId) const;
- CompositorStateWebContentClient& web_content_client_of(ContextState const&);
virtual void post_message_to_web_content(WebContentClientId, IPC::MessageBuffer&) override;
virtual void post_message_to_render_clock_of_web_content(WebContentClientId, IPC::MessageBuffer&) override;
virtual void post_message_to_presentation(PresentationId, IPC::MessageBuffer&) override;
@@ -199,6 +192,29 @@ private:
virtual void cancel_timer(Web::CompositorContextId, ContextTimer) override;
void context_timer_fired(Web::CompositorContextId, ContextTimer);
+ // What the compositor keeps of a context to present it.
+ struct PresentedContext {
+ AK_MAKE_NONCOPYABLE(PresentedContext);
+ AK_MAKE_NONMOVABLE(PresentedContext);
+
+ public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ PresentedContext(Optional<u64> page_id, CompositorStateWebContentClient&, OwnedCanvasSurfaces);
+
+ Optional<u64> page_id;
+ CompositorStateWebContentClient& web_content_client;
+ ContextPresenter presenter;
+ Optional<Web::CompositorContextId> parent_context_id;
+ Compositing::ContextVisibility visibility { Compositing::ContextVisibility::Visible };
+ Optional<u64> display_id;
+ double display_refresh_rate { 60.0 };
+ // Whether the presentation shows this context's frames from CPU memory for now, as when the UI paints it itself.
+ bool presents_from_cpu_memory { false };
+ PacedTickRequest rendering_opportunity_request;
+ PacedTickRequest clock_tick_request;
+ };
+
struct PendingAsyncPresent {
PendingAsyncPresent(Web::CompositorContextId context_id, Gfx::IntRect viewport_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio)
: context_id(context_id)
@@ -220,11 +236,6 @@ private:
bool publish_to_presentation { true };
};
- ContextState* context_if_present(Web::CompositorContextId);
- // Hands the context's async scroll updates to its WebContent process as soon as they exist,
- // so a rendering update reads them locally instead of asking for them over a synchronous call.
- void publish_pending_async_scroll_updates(Web::CompositorContextId, ContextState&);
-
public:
void present_pending_frames_for_testing() { present_pending_frames_on_vsync({}, MonotonicTime::now()); }
size_t pending_async_present_count_for_testing() const { return m_pending_async_presents.size(); }
@@ -233,24 +244,25 @@ public:
Compositing::PendingAsyncScrollUpdates take_pending_async_scroll_updates(Web::CompositorContextId);
private:
- ContextState const* context_if_present(Web::CompositorContextId) const;
- Optional<u64> display_id_for_context(ContextState const&) const;
- ContextState const* root_context_of(ContextState const&) const;
- bool nested_context_scrolled_since_last_frame(Web::CompositorContextId) const;
- bool context_is_effectively_visible(ContextState const&) const;
- void resume_presentation_after_becoming_visible(Web::CompositorContextId root_context_id, ContextState& root_context);
- double display_refresh_rate_for_context(ContextState const&) const;
- void clear_parent_context(ContextState&);
+ PresentedContext* presented_context_if_present(Web::CompositorContextId);
+ PresentedContext const* presented_context_if_present(Web::CompositorContextId) const;
+ Optional<u64> display_id_for_context(PresentedContext const&) const;
+ PresentedContext const* root_context_of(PresentedContext const&) const;
+ Web::CompositorContextId root_context_id_of(Web::CompositorContextId) const;
+ bool context_is_effectively_visible(PresentedContext const&) const;
+ void resume_presentation_after_becoming_visible(Web::CompositorContextId root_context_id, PresentedContext& root_context);
+ double display_refresh_rate_for_context(PresentedContext const&) const;
+ void clear_parent_context(Web::CompositorContextId, PresentedContext&);
CompositedContextResolver resolver_for(Web::CompositorContextId parent_context_id);
CompositedContextSurface resolve_composited_context(Web::CompositorContextId parent_context_id, Web::CompositorContextId child_context_id, Gfx::FloatRect destination_rect, Gfx::FloatMatrix4x4 const& canvas_transform);
- void schedule_backing_store_shrink(Web::CompositorContextId, ContextState&);
+ void schedule_backing_store_shrink(Web::CompositorContextId, PresentedContext&);
void shrink_backing_stores_after_resize(Web::CompositorContextId);
- void resize_backing_stores_if_needed(Web::CompositorContextId, ContextState&);
- void add_backing_store_for_pending_frame_if_needed(Web::CompositorContextId, ContextState&);
- void schedule_surplus_backing_store_retirement(Web::CompositorContextId, ContextState&);
+ void resize_backing_stores_if_needed(Web::CompositorContextId, PresentedContext&);
+ void add_backing_store_for_pending_frame_if_needed(Web::CompositorContextId, PresentedContext&);
+ void schedule_surplus_backing_store_retirement(Web::CompositorContextId, PresentedContext&);
void retire_idle_surplus_backing_stores(Web::CompositorContextId);
- void present_current_frame(Web::CompositorContextId, ContextState&);
- void resolve_video_sinks(ContextState&);
+ void present_current_frame(Web::CompositorContextId, PresentedContext&);
+ void resolve_video_sinks(PresentedContext&);
enum class VideoSinkUpdateResult : u8 {
NoUnpaintedSinkRequiresUpdates,
UnpaintedSinkRequiresUpdates,
@@ -262,75 +274,21 @@ private:
int unpainted_video_update_interval_ms() const;
void present_contexts_drawing_video_sink(CompositorStateWebContentClient&, Media::VideoSinkHandle);
void present_contexts_drawing_canvas(CompositorStateWebContentClient&, Compositing::CanvasId);
- bool apply_context_update_result(
- Web::CompositorContextId,
- ContextState&,
- ContextState::ContextUpdateResult const&);
- void schedule_animation_frames_if_needed(ContextState&);
- enum class WheelStepOrigin : u8 {
- UI,
- Fling,
- MainThread,
- };
- // A fling step is scrolled at the frame time the fling synthesized it for.
- ContextState::ContextUpdateResult scroll_for_wheel_step(Web::CompositorContextId, ContextState&, Web::MouseEvent const&, WheelStepOrigin, bool is_first_wheel_event_with_delta, Compositing::AsyncScrollOperationTracking, MonotonicTime now);
- // How a wheel step went in a context that presented and published what the step changed itself.
- struct AppliedWheelStep {
- // What is left for the context that routes the step to present and report.
- ContextState::ContextUpdateResult result;
- bool presents_frame { false };
- };
- // A process only knows the ids of its own nodes, and only follows the operations of its own contexts.
- AppliedWheelStep apply_wheel_step_result(Web::CompositorContextId, ContextState&, ContextState::ContextUpdateResult const&, WebContentClientId reporting_client, MonotonicTime now);
- static Compositing::WheelEventAnswer wheel_event_answer_for(AppliedWheelStep const&, Optional<Web::WheelGestureMark>);
- AppliedWheelStep hand_wheel_step_to_nested_context(Web::CompositorContextId, ContextState&, Web::MouseEvent const&, ContextState::NestedContextWheelStep const&, ContextState::WheelEventListenerGate const&, Compositing::AsyncScrollOperationTracking, MonotonicTime now);
- struct NestedContextTakingWheelStep {
- Web::CompositorContextId context_id;
- // Null when the step cannot be handed to the context.
- ContextState* context { nullptr };
- Gfx::FloatPoint position {};
- ContextState::FirstWheelStepRoute route;
- };
- struct FirstWheelStepInNestedContexts {
- // The innermost context that the step went into, whose page sees the step.
- Web::CompositorContextId innermost_context_id;
- // The context that takes the step, which is empty when each of them passes the step on.
- Optional<NestedContextTakingWheelStep> taking_context {};
- };
- // Where the first step of a gesture over the content of a context nested in the context `context_id` goes, among
- // that context and the contexts nested in it.
- FirstWheelStepInNestedContexts first_wheel_step_in_nested_contexts(Web::CompositorContextId, Compositing::NestedContextHit, Gfx::FloatPoint delta_in_css_pixels, Web::WheelDeltaPrecision);
- // Routes the first step of a gesture that the page of the listener context let through: in that context, from the
- // scrollers the page found around the target, and on to the contexts around it for what a context passes on, up to
- // the routing context, which latches the gesture. A page of another process than a context's scrolls it only by
- // what the UI left to the page in the gesture.
- AppliedWheelStep route_wheel_step_the_page_let_through(Web::CompositorContextId routing_context_id, ContextState& routing_context, Web::CompositorContextId listener_context_id, ContextState& listener_context, ContextState::WheelStep, ReadonlySpan<Web::AsyncScrollNodeStableID> scroll_chain, Optional<WebContentClientId> page_client, MonotonicTime now);
- // The nearest context, from the given one out to the presenting one, that noted the gesture.
- Optional<Web::CompositorContextId> routing_context_of_wheel_gesture(Web::CompositorContextId, u64 gesture_id);
- // The scrollbars of a context nested in the presenting context `context_id` take the pointer over that context's
- // content, and a drag of one of them takes the pointer wherever it goes until the drag ends.
- ContextState::ContextUpdateResult handle_mouse_event_in_nested_contexts(Web::CompositorContextId, ContextState&, Web::MouseEvent const&, MonotonicTime now);
- ContextState* context_still_nested_in(Web::CompositorContextId containing_context_id, Optional<Web::CompositorContextId>);
- // Where the position in the presenting context lands in the nested context, through each context in between and
- // without regard to where their content is or what clips it.
- Optional<Gfx::FloatPoint> position_in_nested_context_ignoring_clips(Web::CompositorContextId presenting_context_id, ContextState& presenting_context, ContextState& nested_context, Gfx::FloatPoint position);
- bool is_nested_in(Web::CompositorContextId containing_context_id, ContextState const&) const;
- bool page_under_pinch_may_cancel_it(Web::CompositorContextId, ContextState&, Gfx::FloatPoint position);
- void dispatch_scroll_fling_step(Web::CompositorContextId, ContextState&, Optional<Web::MouseEvent>, MonotonicTime now);
- void present_frame(Web::CompositorContextId, ContextState&, PendingFrame);
- void request_present(Web::CompositorContextId, PendingFrame const&);
+ bool needs_animation_frames(Web::CompositorContextId, PresentedContext const&, MonotonicTime now);
+ static bool has_pending_present_frame_scheduled_on(PresentedContext const&, Optional<u64> display_id);
+ void schedule_animation_frames_if_needed(Web::CompositorContextId, PresentedContext const&);
+ void present_frame(Web::CompositorContextId, PresentedContext&, PendingFrame);
void schedule_requested_presents();
- void schedule_present_frame(Web::CompositorContextId, ContextState&, PendingFrame);
- void schedule_present_frame(Web::CompositorContextId, ContextState&, Gfx::IntRect viewport_rect);
- void schedule_pending_present_frame(Web::CompositorContextId, ContextState&);
- void schedule_pending_present_frame_on_vsync(Web::CompositorContextId, ContextState&);
- void schedule_containing_context_present(ContextState&);
- void schedule_pending_present_frame_if_unblocked(Web::CompositorContextId, ContextState&);
- bool try_present_frame_during_resize(Web::CompositorContextId, ContextState&);
+ void schedule_present_frame(Web::CompositorContextId, PresentedContext&, PendingFrame);
+ void schedule_present_frame(Web::CompositorContextId, PresentedContext&, Gfx::IntRect viewport_rect);
+ void schedule_pending_present_frame(Web::CompositorContextId, PresentedContext&);
+ void schedule_pending_present_frame_on_vsync(Web::CompositorContextId, PresentedContext&);
+ void schedule_containing_context_present(PresentedContext&);
+ void schedule_pending_present_frame_if_unblocked(Web::CompositorContextId, PresentedContext&);
+ bool try_present_frame_during_resize(Web::CompositorContextId, PresentedContext&);
VSyncScheduler& vsync_scheduler_for_display(Optional<u64> display_id);
- DisplayTickResult display_tick(Web::CompositorContextId, ContextState&, MonotonicTime frame_time, MonotonicTime now, DisplayTickInputs const&);
void present_pending_frames_on_vsync(Optional<u64> display_id, MonotonicTime frame_time);
- void publish_backing_stores(Web::CompositorContextId, ContextState&, BackingStoreManager::Publication&&);
+ void publish_backing_stores(Web::CompositorContextId, PresentedContext&, BackingStoreManager::Publication&&);
BackingStoreManager::GpuSharing gpu_sharing_for_context(Web::CompositorContextId) const;
void unbind_presentation(Web::CompositorContextId);
void update_scroll_capabilities();
@@ -340,11 +298,10 @@ private:
void schedule_gpu_completion_check();
void check_gpu_completions();
- HashMap<Web::CompositorContextId, OwnPtr<ContextState>> m_contexts;
+ HashMap<Web::CompositorContextId, NonnullOwnPtr<PresentedContext>> m_presented_contexts;
u64 m_next_web_content_client_id { 1 };
HashMap<WebContentClientId, CompositorStateWebContentClient*> m_web_content_clients;
HashMap<Web::CompositorContextId, HashMap<ContextTimer, NonnullRefPtr<Core::Timer>>> m_context_timers;
- Vector<PresentRequest> m_present_requests;
DoublyLinkedList<PendingAsyncPresent> m_pending_async_presents;
RefPtr<Gfx::SkiaBackendContext> m_skia_backend_context;
@@ -374,7 +331,7 @@ private:
bool animated_image_is_drawn(CompositorStateWebContentClient*, Compositing::ImageFrameResourceId);
void show_animated_image_frame(CompositorStateWebContentClient*, Compositing::ImageFrameResourceId, Gfx::DecodedImageFrame const&);
// A frame that names an animated image brings frame 0 with it, whichever frame the image is at.
- void show_animated_image_frames_in(ContextState&);
+ void show_animated_image_frames_in(PresentedContext&);
void update_animated_images();
void schedule_animated_image_update(AK::Duration delay);
HashMap<CompositorStateWebContentClient*, HashMap<u64, NonnullOwnPtr<AnimatedImage>>> m_animated_images;
@@ -389,6 +346,8 @@ private:
bool origin_clean { true };
};
HashMap<Compositing::CanvasId, PlaceholderCanvas> m_placeholder_canvases;
+
+ CompositorCore m_core { *this };
};
}
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index d167d8f70ea..ada362b6c70 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -11,16 +11,8 @@
#include <AK/StdLibExtras.h>
#include <Compositor/CompositorWebContentClientEndpoint.h>
#include <Compositor/ContextState.h>
-#include <Compositor/DisplayListPlayerSkia.h>
-#include <Compositor/PaintingSurface.h>
-#include <Compositor/PausedDebuggerOverlay.h>
#include <Compositor/RenderClockClientEndpoint.h>
-#include <Compositor/SkiaUtils.h>
-#include <LibCompositing/DisplayList/DisplayListDamage.h>
-#include <LibGfx/Bitmap.h>
#include <LibWebCommon/Page/InputEvent.h>
-#include <core/SkCanvas.h>
-#include <core/SkImage.h>
#include <math.h>
namespace Compositor {
@@ -59,12 +51,11 @@ static void clamp_visual_viewport_transform_to_viewport(Compositing::TransformWi
transform.matrix[1, 3] = clamp(transform.matrix[1, 3], min_y, 0.0f);
}
-ContextState::ContextState(Web::CompositorContextId context_id, Optional<u64> page_id, WebContentClientId web_content_client_id, CompositorCoreHost& host, OwnedCanvasSurfaces canvas_surfaces)
+ContextState::ContextState(Web::CompositorContextId context_id, Optional<u64> page_id, WebContentClientId web_content_client_id, CompositorCoreHost& host)
: m_host(host)
, m_web_content_client_id(web_content_client_id)
, m_context_id(context_id)
, m_page_id(page_id)
- , m_presenter(canvas_surfaces, page_id.has_value())
, m_presents_to_client(page_id.has_value())
{
}
@@ -2076,12 +2067,6 @@ void ContextState::set_paused_debugger_overlay_is_shown(bool is_shown)
m_paused_debugger_overlay_is_shown = is_shown;
}
-void ContextState::set_display_metadata(Optional<u64> display_id, double refresh_rate)
-{
- m_display_id = display_id;
- m_display_refresh_rate = refresh_rate;
-}
-
bool ContextState::set_visibility(Compositing::ContextVisibility visibility)
{
if (m_visibility == visibility)
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index 63fa859def1..6bab8371a88 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -16,32 +16,26 @@
#include <AK/Try.h>
#include <AK/Vector.h>
#include <Compositor/AsyncScrollUpdateOutbox.h>
-#include <Compositor/CompositedContext.h>
#include <Compositor/CompositorCoreHost.h>
-#include <Compositor/ContextPresenter.h>
#include <Compositor/ContextScene.h>
#include <Compositor/ElasticOverscrollController.h>
#include <Compositor/Forward.h>
-#include <Compositor/FramePacer.h>
+#include <Compositor/FrameInputs.h>
#include <Compositor/KeyboardScrollController.h>
-#include <Compositor/PaintingSurface.h>
#include <Compositor/ScrollFlingController.h>
#include <Compositor/ScrollSnapController.h>
#include <Compositor/ScrollbarController.h>
#include <Compositor/WheelGestureRouting.h>
#include <LibCompositing/DisplayList/AccumulatedVisualContext.h>
#include <LibCompositing/DisplayList/DisplayList.h>
-#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>
#include <LibCompositing/Forward.h>
#include <LibCompositing/Scrolling/AsyncScrollTree.h>
#include <LibCompositing/Scrolling/AsyncScrollingState.h>
#include <LibCompositing/Scrolling/ScrollState.h>
#include <LibCompositing/Scrolling/SmoothScrollAnimation.h>
#include <LibCompositing/Types.h>
-#include <LibCore/Forward.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
-#include <LibGfx/ShareableBitmap.h>
#include <LibGfx/Size.h>
#include <LibWebCommon/Page/InputEvent.h>
#include <LibWebCommon/Page/InputTrace.h>
@@ -157,7 +151,7 @@ public:
Optional<Compositing::NestedContextHit> nested_context_hit {};
};
- ContextState(Web::CompositorContextId, Optional<u64> page_id, WebContentClientId, CompositorCoreHost&, OwnedCanvasSurfaces);
+ ContextState(Web::CompositorContextId, Optional<u64> page_id, WebContentClientId, CompositorCoreHost&);
bool is_owned_by(WebContentClientId) const;
WebContentClientId web_content_client_id() const { return m_web_content_client_id; }
@@ -178,8 +172,6 @@ public:
void set_parent_context(Optional<Web::CompositorContextId>);
Web::CompositorContextId context_id() const { return m_context_id; }
Optional<Web::CompositorContextId> parent_context_id() const { return m_parent_context_id; }
- ContextPresenter& presenter() { return m_presenter; }
- ContextPresenter const& presenter() const { return m_presenter; }
void install_display_list_update(
NonnullRefPtr<Compositing::DisplayList>,
@@ -328,30 +320,11 @@ public:
void viewport_size_updated(Gfx::IntSize);
void set_paused_debugger_overlay_is_shown(bool);
- void set_display_metadata(Optional<u64> display_id, double refresh_rate);
- Optional<u64> display_id() const { return m_display_id; }
- double display_refresh_rate() const { return m_display_refresh_rate; }
bool set_visibility(Compositing::ContextVisibility);
Compositing::ContextVisibility visibility() const { return m_visibility; }
- // Whether the presentation shows this context's frames from CPU memory for now, as when the UI paints it itself.
- bool presents_from_cpu_memory() const { return m_presents_from_cpu_memory; }
- void set_presents_from_cpu_memory(bool presents_from_cpu_memory) { m_presents_from_cpu_memory = presents_from_cpu_memory; }
-
- bool request_rendering_opportunity(double maximum_frames_per_second) { return m_rendering_opportunity_request.request(maximum_frames_per_second); }
- bool rendering_opportunity_requested() const { return m_rendering_opportunity_request.is_requested(); }
- double rendering_opportunity_frame_interval(double display_refresh_rate) const { return m_rendering_opportunity_request.frame_interval(display_refresh_rate); }
- bool rendering_opportunity_is_due(MonotonicTime frame_time, double display_refresh_rate) const { return m_rendering_opportunity_request.is_due(frame_time, display_refresh_rate); }
- void did_deliver_rendering_opportunity(MonotonicTime frame_time) { m_rendering_opportunity_request.did_deliver(frame_time); }
-
- bool request_clock_tick(double maximum_frames_per_second) { return m_clock_tick_request.request(maximum_frames_per_second); }
- bool clock_tick_requested() const { return m_clock_tick_request.is_requested(); }
- double clock_tick_interval(double display_refresh_rate) const { return m_clock_tick_request.frame_interval(display_refresh_rate); }
- bool clock_tick_is_due(MonotonicTime frame_time, double display_refresh_rate) const { return m_clock_tick_request.is_due(frame_time, display_refresh_rate); }
- void did_deliver_clock_tick(MonotonicTime frame_time) { m_clock_tick_request.did_deliver(frame_time); }
void post_clock_tick(MonotonicTime frame_time, double frame_interval_milliseconds);
// Where the compositor has scrolled each scroll node to.
Vector<Web::CompositorScrollOffset> scroll_offsets() const;
- bool display_tick_requested() const { return rendering_opportunity_requested() || clock_tick_requested(); }
// Each frame shows the scrollbars as of when it is painted, so this updates which of them are shown. Empty until a
// display list is installed.
@@ -505,7 +478,6 @@ private:
Optional<Web::CompositorContextId> m_parent_context_id;
ContextScene m_scene;
- ContextPresenter m_presenter;
bool m_presents_to_client { false };
Gfx::IntSize m_viewport_size;
bool m_paused_debugger_overlay_is_shown { false };
@@ -556,13 +528,7 @@ private:
Optional<i64> m_caret_blink_cycle_start_time_ns;
Web::InputTraceRecorder m_input_trace;
- Optional<u64> m_display_id;
- double m_display_refresh_rate { 60.0 };
Compositing::ContextVisibility m_visibility { Compositing::ContextVisibility::Visible };
- bool m_presents_from_cpu_memory { false };
-
- PacedTickRequest m_rendering_opportunity_request;
- PacedTickRequest m_clock_tick_request;
};
}