Skip to content

Truly independent web browser

Compositor: Run the scroll logic of a context once per display tick

Aliaksandr Kalenik0335f4c10e50
diff --git a/Services/Compositor/CompositorCoreHost.h b/Services/Compositor/CompositorCoreHost.h
index d7a0f7f5a58..c768250ea32 100644
--- a/Services/Compositor/CompositorCoreHost.h
+++ b/Services/Compositor/CompositorCoreHost.h
@@ -26,6 +26,13 @@ struct PendingFrame {
static PendingFrame repainting_changes(Gfx::IntRect viewport_rect) { return { viewport_rect, {} }; }
};
+enum class PresentScheduling : u8 {
+ // The frame is presented on the next tick of the context's display, which the request asks for.
+ OnNextDisplayTick,
+ // The frame joins what the display tick in progress presents.
+ WithDisplayTickInProgress,
+};
+
// A frame that the scroll and input logic of a context wants presented. The compositor schedules it once the call that
// asked for it has returned.
struct PresentRequest {
@@ -33,6 +40,22 @@ struct PresentRequest {
// Empty for a frame at the viewport rect the context is presented at.
Optional<Gfx::IntRect> viewport_rect;
Gfx::IntRect forced_damage_rect;
+ PresentScheduling scheduling { PresentScheduling::OnNextDisplayTick };
+};
+
+// What the presentation knows of a context as a tick of a display begins.
+struct DisplayTickInputs {
+ bool context_is_on_tick_display { false };
+ bool present_is_scheduled_on_tick_display { false };
+ bool ui_overlay_fades_need_frame { false };
+ // Set when the render clock of the context is due a tick now.
+ Optional<double> clock_tick_interval_in_milliseconds;
+};
+
+struct DisplayTickResult {
+ bool presents_on_display { false };
+ // As of the end of the tick, and without the fades of the UI overlays.
+ bool needs_animation_frames { false };
};
enum class ContextTimer : u8 {
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index a7732f6108c..ef78239bf20 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -1866,7 +1866,14 @@ void CompositorState::schedule_requested_presents()
viewport_rect = context->presenter().viewport_rect_for_ui_overlay();
if (!viewport_rect.has_value())
continue;
- schedule_present_frame(request.context_id, *context, PendingFrame { *viewport_rect, request.forced_damage_rect });
+ switch (request.scheduling) {
+ case PresentScheduling::OnNextDisplayTick:
+ 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 });
+ break;
+ }
}
}
@@ -1967,14 +1974,44 @@ 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) {
- auto now = MonotonicTime::now();
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();
continue;
@@ -1991,56 +2028,40 @@ void CompositorState::present_pending_frames_on_vsync(Optional<u64> display_id,
}
}
- auto has_active_animation_on_display = needs_animation_frames(context, now) && display_id_for_context(context) == display_id;
- auto presents_on_display = has_pending_present_frame_scheduled_on(context, display_id) || has_active_animation_on_display;
- if (presents_on_display) {
- if (display_id_for_context(context) == display_id)
- dispatch_scroll_fling_step(context_id, context, context.take_scroll_fling_step(frame_time), frame_time);
- schedule_requested_presents();
- if (auto animation_frame = context.advance_smooth_scroll_animations(frame_time); animation_frame.has_value())
- context.presenter().queue_present_frame(PendingFrame::repainting_changes(*animation_frame));
- if (auto rebound_frame = context.advance_elastic_overscroll_rebounds(frame_time); rebound_frame.has_value())
- context.presenter().queue_present_frame(PendingFrame::repainting_changes(*rebound_frame));
- 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 damage_rect = context.advance_scrollbar_fades_and_return_damage_rect(frame_time); !damage_rect.is_empty()) {
- if (auto viewport_rect = context.presenter().viewport_rect_for_ui_overlay(); viewport_rect.has_value())
- context.presenter().queue_present_frame({ *viewport_rect, damage_rect });
- }
- }
-
- // 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 (context.clock_tick_requested() && display_id_for_context(context) == display_id) {
+ auto context_is_on_tick_display = display_id_for_context(context) == 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(),
+ .clock_tick_interval_in_milliseconds = {},
+ };
+ if (context.clock_tick_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)) {
- auto frame_interval = context.clock_tick_interval(display_refresh_rate);
+ inputs.clock_tick_interval_in_milliseconds = context.clock_tick_interval(display_refresh_rate);
context.did_deliver_clock_tick(frame_time);
- web_content_client_of(context).clock_tick(context_id, frame_time.nanoseconds(), frame_interval, context.scroll_offsets());
} else {
vsync_scheduler_for_display(display_id).schedule(display_refresh_rate);
}
}
- if (!presents_on_display)
+ VERIFY(m_present_requests.is_empty());
+ auto tick = display_tick(context_id, context, frame_time, now, inputs);
+ schedule_requested_presents();
+ if (!tick.presents_on_display)
continue;
-
- publish_pending_async_scroll_updates(context_id, context);
- if (context.visual_animations_need_frame(now)) {
- context.advance_visual_animations(frame_time);
- if (auto viewport_rect = context.presenter().viewport_rect_for_ui_overlay(); viewport_rect.has_value())
- context.presenter().queue_present_frame(PendingFrame::repainting_changes(*viewport_rect));
- }
+ 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();
if (!pending_present_frame.has_value()) {
- has_active_animation_on_display = needs_animation_frames(context, now) && display_id_for_context(context) == display_id;
- if (has_pending_present_frame_scheduled_on(context, display_id) || has_active_animation_on_display)
+ 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));
continue;
}
- if (needs_animation_frames(context, now))
+ if (needs_animation_frames)
schedule_present_frame(context_id, context, PendingFrame::repainting_changes(pending_present_frame->viewport_rect));
present_frame(context_id, context, *pending_present_frame);
}
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index b1e5fcb60b8..484a4048aca 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -60,7 +60,6 @@ public:
virtual void post_message_to_web_content(IPC::MessageBuffer&) = 0;
virtual void post_message_to_render_clock(IPC::MessageBuffer&) = 0;
virtual void rendering_opportunity(Web::CompositorContextId, i64 frame_time_nanoseconds, double frame_interval_milliseconds) = 0;
- virtual void clock_tick(Web::CompositorContextId, i64 frame_time_nanoseconds, double frame_interval_milliseconds, Vector<Web::CompositorScrollOffset> const& scroll_offsets) = 0;
virtual void create_video_edge(Media::VideoSinkHandle) = 0;
virtual void release_video_edge(Media::VideoSinkHandle) = 0;
virtual void video_sink_presented_frame(Media::VideoSinkHandle, RefPtr<Media::VideoFrame>, Media::DisplayingVideoSinkFrameCounts) { }
@@ -329,6 +328,7 @@ private:
void schedule_pending_present_frame_if_unblocked(Web::CompositorContextId, ContextState&);
bool try_present_frame_during_resize(Web::CompositorContextId, ContextState&);
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&&);
BackingStoreManager::GpuSharing gpu_sharing_for_context(Web::CompositorContextId) const;
diff --git a/Services/Compositor/ConnectionFromWebContent.cpp b/Services/Compositor/ConnectionFromWebContent.cpp
index 04245a5223b..1bcbced0030 100644
--- a/Services/Compositor/ConnectionFromWebContent.cpp
+++ b/Services/Compositor/ConnectionFromWebContent.cpp
@@ -67,12 +67,6 @@ void ConnectionFromWebContent::install_render_clock_channel(IPC::TransportHandle
};
}
-void ConnectionFromWebContent::clock_tick(Web::CompositorContextId context_id, i64 frame_time_nanoseconds, double frame_interval_milliseconds, Vector<Web::CompositorScrollOffset> const& scroll_offsets)
-{
- if (m_render_clock_connection)
- m_render_clock_connection->async_clock_tick(context_id, frame_time_nanoseconds, frame_interval_milliseconds, scroll_offsets);
-}
-
void ConnectionFromWebContent::drop_image_decoder()
{
if (!m_image_decoder)
diff --git a/Services/Compositor/ConnectionFromWebContent.h b/Services/Compositor/ConnectionFromWebContent.h
index 1a435242cdd..cc86557724e 100644
--- a/Services/Compositor/ConnectionFromWebContent.h
+++ b/Services/Compositor/ConnectionFromWebContent.h
@@ -105,7 +105,6 @@ private:
virtual void post_message_to_web_content(IPC::MessageBuffer&) override;
virtual void post_message_to_render_clock(IPC::MessageBuffer&) override;
virtual void rendering_opportunity(Web::CompositorContextId, i64 frame_time_nanoseconds, double frame_interval_milliseconds) override;
- virtual void clock_tick(Web::CompositorContextId, i64 frame_time_nanoseconds, double frame_interval_milliseconds, Vector<Web::CompositorScrollOffset> const& scroll_offsets) override;
virtual void create_video_edge(Media::VideoSinkHandle) override;
virtual void release_video_edge(Media::VideoSinkHandle) override;
virtual void request_animated_image_frames(AnimatedImage::FrameRequest const&) override;
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 20acb2e4540..d167d8f70ea 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -124,6 +124,11 @@ void ContextState::request_rendering_update()
post_to_web_content<Messages::CompositorWebContentClient::RequestRenderingUpdate>();
}
+void ContextState::post_clock_tick(MonotonicTime frame_time, double frame_interval_milliseconds)
+{
+ post_to_render_clock_of_web_content<Messages::RenderClockClient::ClockTick>(m_context_id, frame_time.nanoseconds(), frame_interval_milliseconds, scroll_offsets());
+}
+
void ContextState::dispatch_mouse_event_to_web_content(Web::MouseEvent const& event, bool nested_context_scrolled_since_last_frame)
{
VERIFY(m_page_id.has_value());
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index fee68f88389..63fa859def1 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -348,6 +348,7 @@ public:
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(); }