Skip to content

Truly independent web browser

Everywhere: Show a window resize together with the page's new frame

Aliaksandr Kalenik2bf3ea2c6955
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index a868ae0b562..c98486815ba 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -1509,6 +1509,33 @@ void Application::set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSiz
m_compositor_client->async_set_remote_layer_fit(remote_layer_id, fit_within);
}
+void Application::begin_synchronized_resize(u64 remote_layer_id, u64 token)
+{
+ if (!can_send_compositor_process_ipc(m_compositor_client))
+ return;
+ VERIFY(m_compositor_client);
+
+ m_compositor_client->async_begin_synchronized_resize(remote_layer_id, token);
+}
+
+void Application::await_synchronized_resize_frame(u64 remote_layer_id, u64 token, Gfx::IntSize size)
+{
+ if (!can_send_compositor_process_ipc(m_compositor_client))
+ return;
+ VERIFY(m_compositor_client);
+
+ m_compositor_client->async_await_synchronized_resize_frame(remote_layer_id, token, size);
+}
+
+void Application::commit_synchronized_resize(u64 remote_layer_id, u64 token)
+{
+ if (!can_send_compositor_process_ipc(m_compositor_client))
+ return;
+ VERIFY(m_compositor_client);
+
+ m_compositor_client->async_commit_synchronized_resize(remote_layer_id, token);
+}
+
void Application::take_frame_snapshot(Web::CompositorContextId context_id, Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)> callback)
{
if (!m_compositor_presentation_client) {
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 2261bae5537..1eef1f396a8 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -276,6 +276,9 @@ public:
void destroy_remote_layer(u64 remote_layer_id);
void show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId>);
void set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within);
+ void begin_synchronized_resize(u64 remote_layer_id, u64 token);
+ void await_synchronized_resize_frame(u64 remote_layer_id, u64 token, Gfx::IntSize);
+ void commit_synchronized_resize(u64 remote_layer_id, u64 token);
void take_frame_snapshot(Web::CompositorContextId, Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)>);
bool handle_and_dispatch_key_event_in_compositor(Web::CompositorContextId, Web::KeyEvent const&, bool allow_keyboard_scroll);
bool handle_and_dispatch_pinch_event_in_compositor(Web::CompositorContextId, Web::PinchEvent const&);
diff --git a/Libraries/LibWebView/CompositorPresentationClient.cpp b/Libraries/LibWebView/CompositorPresentationClient.cpp
index cd1390470f9..5aa8e348f1c 100644
--- a/Libraries/LibWebView/CompositorPresentationClient.cpp
+++ b/Libraries/LibWebView/CompositorPresentationClient.cpp
@@ -7,6 +7,7 @@
#include <LibWebView/CompositorPresentationClient.h>
#include <LibCore/EventLoop.h>
+#include <LibWebView/ViewImplementation.h>
#include <LibWebView/WebContentClient.h>
#include <LibWebView/WebContentPage.h>
@@ -98,6 +99,18 @@ void CompositorPresentationClient::did_take_frame_snapshot(u64 request_id, Optio
(*callback)(move(snapshot));
}
+void CompositorPresentationClient::did_hold_synchronized_resize_frame(u64 remote_layer_id, u64 token)
+{
+ if (auto* view = ViewImplementation::find_view_showing_remote_layer(remote_layer_id))
+ view->synchronized_resize_frame_held({}, token);
+}
+
+void CompositorPresentationClient::did_commit_synchronized_resize(u64 remote_layer_id, u64 token)
+{
+ if (auto* view = ViewImplementation::find_view_showing_remote_layer(remote_layer_id))
+ view->synchronized_resize_committed({}, token);
+}
+
void CompositorPresentationClient::did_add_backing_stores(Web::CompositorContextId context_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)
{
if (auto* page = page_for_compositor_context_id(context_id))
diff --git a/Libraries/LibWebView/CompositorPresentationClient.h b/Libraries/LibWebView/CompositorPresentationClient.h
index 09f02ab403b..7e4aeacdb92 100644
--- a/Libraries/LibWebView/CompositorPresentationClient.h
+++ b/Libraries/LibWebView/CompositorPresentationClient.h
@@ -48,6 +48,8 @@ private:
virtual void did_not_dispatch_input_event(Web::CompositorContextId, u64 event_id) override;
virtual void did_dispatch_input_event(Web::CompositorContextId, u64 event_id, bool performed_default_action) override;
virtual void did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap>) override;
+ virtual void did_hold_synchronized_resize_frame(u64 remote_layer_id, u64 token) override;
+ virtual void did_commit_synchronized_resize(u64 remote_layer_id, u64 token) override;
HashMap<u64, FrameSnapshotCallback> m_pending_frame_snapshots;
u64 m_next_frame_snapshot_request_id { 1 };
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 332279fc2bc..59a3f35b045 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -485,6 +485,39 @@ void ViewImplementation::take_frame_snapshot(Optional<Gfx::IntSize> maximum_size
Application::the().take_frame_snapshot(page().compositor_context_id(), maximum_size, move(callback));
}
+ViewImplementation* ViewImplementation::find_view_showing_remote_layer(u64 remote_layer_id)
+{
+ for (auto& [_, view] : all_views()) {
+ if (view->m_remote_layer_id == remote_layer_id)
+ return view;
+ }
+ return nullptr;
+}
+
+void ViewImplementation::begin_synchronized_resize(u64 token)
+{
+ if (m_remote_layer_id.has_value())
+ Application::the().begin_synchronized_resize(*m_remote_layer_id, token);
+}
+
+void ViewImplementation::await_synchronized_resize_frame(u64 token, Gfx::IntSize size)
+{
+ if (!m_remote_layer_id.has_value()) {
+ did_hold_synchronized_resize_frame(token);
+ return;
+ }
+ Application::the().await_synchronized_resize_frame(*m_remote_layer_id, token, size);
+}
+
+void ViewImplementation::commit_synchronized_resize(u64 token)
+{
+ if (!m_remote_layer_id.has_value()) {
+ did_commit_synchronized_resize(token);
+ return;
+ }
+ Application::the().commit_synchronized_resize(*m_remote_layer_id, token);
+}
+
void ViewImplementation::set_scales_frames_to_fit(bool scales_frames_to_fit)
{
m_scales_frames_to_fit = scales_frames_to_fit;
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 3fb8cff07b0..aef42cea855 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -241,6 +241,15 @@ public:
void take_frame_snapshot(Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)>);
// The Compositor's remote layers went with its process, so the view asks the new one for another.
void recreate_remote_layer_after_compositor_reconnect(Badge<WebContentClient>);
+ static ViewImplementation* find_view_showing_remote_layer(u64 remote_layer_id);
+
+ // The UI shows a resize of the view along with the page's frame of the new size. A view without a remote layer
+ // has nothing to wait for, so it answers at once.
+ void begin_synchronized_resize(u64 token);
+ void await_synchronized_resize_frame(u64 token, Gfx::IntSize);
+ void commit_synchronized_resize(u64 token);
+ void synchronized_resize_frame_held(Badge<CompositorPresentationClient>, u64 token) { did_hold_synchronized_resize_frame(token); }
+ void synchronized_resize_committed(Badge<CompositorPresentationClient>, u64 token) { did_commit_synchronized_resize(token); }
void set_has_system_focus(bool);
void load(URL::URL const&, Web::Bindings::NavigationHistoryBehavior = Web::Bindings::NavigationHistoryBehavior::Auto);
@@ -770,6 +779,8 @@ protected:
// stores instead.
virtual void did_create_remote_layer(u32 core_animation_context_id) { (void)core_animation_context_id; }
virtual void did_present_frame_in_remote_layer(Gfx::IntSize, double device_pixel_ratio) { (void)device_pixel_ratio; }
+ virtual void did_hold_synchronized_resize_frame(u64 token) { (void)token; }
+ virtual void did_commit_synchronized_resize(u64 token) { (void)token; }
// Lets the Compositor paint into a backing store again. A view whose frames something else still reads may wait
// for that to finish first.
virtual void release_backing_store(i32 bitmap_id);
diff --git a/Services/Compositor/CompositorControlServer.ipc b/Services/Compositor/CompositorControlServer.ipc
index 33c026da33a..9b968d4f249 100644
--- a/Services/Compositor/CompositorControlServer.ipc
+++ b/Services/Compositor/CompositorControlServer.ipc
@@ -20,6 +20,9 @@ endpoint CompositorControlServer
destroy_remote_layer(u64 remote_layer_id) =|
show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId> context_id) =|
set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within) =|
+ begin_synchronized_resize(u64 remote_layer_id, u64 token) =|
+ await_synchronized_resize_frame(u64 remote_layer_id, u64 token, Gfx::IntSize size) =|
+ commit_synchronized_resize(u64 remote_layer_id, u64 token) =|
create_context(Web::CompositorContextId context_id, Optional<u64> page_id, i32 web_content_connection_id) => ()
diff --git a/Services/Compositor/CompositorPresentationClient.ipc b/Services/Compositor/CompositorPresentationClient.ipc
index e50b075092e..2f50bac710e 100644
--- a/Services/Compositor/CompositorPresentationClient.ipc
+++ b/Services/Compositor/CompositorPresentationClient.ipc
@@ -11,6 +11,8 @@ endpoint CompositorPresentationClient
did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) =|
did_present_frame_in_remote_layer(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, double device_pixel_ratio) =|
did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap> snapshot) =|
+ did_hold_synchronized_resize_frame(u64 remote_layer_id, u64 token) =|
+ did_commit_synchronized_resize(u64 remote_layer_id, u64 token) =|
[RustSender] did_consume_input_event(Web::CompositorContextId context_id, u64 event_id) =|
[RustSender] did_not_dispatch_input_event(Web::CompositorContextId context_id, u64 event_id) =|
[RustSender] did_dispatch_input_event(Web::CompositorContextId context_id, u64 event_id, bool performed_default_action) =|
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index a8ea0bb2d46..6e7f12b641a 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -204,7 +204,7 @@ u32 CompositorState::create_remote_layer(CompositorStateClient& presentation, u6
if (!layer)
return 0;
auto core_animation_context_id = layer->core_animation_context_id();
- m_remote_layers.set(remote_layer_id, { .presentation = &presentation, .layer = move(layer), .shown_context_id = {}, .fit_within = {} });
+ m_remote_layers.set(remote_layer_id, { .presentation = &presentation, .layer = move(layer), .shown_context_id = {}, .fit_within = {}, .size_of_shown_frame_laid_out_by_page = {} });
return core_animation_context_id;
}
@@ -212,6 +212,10 @@ void CompositorState::destroy_remote_layer(u64 remote_layer_id)
{
show_context_in_remote_layer(remote_layer_id, {});
m_remote_layers.remove(remote_layer_id);
+ auto had_synchronized_resizes = !m_synchronized_resizes.is_empty();
+ m_synchronized_resizes.remove_all_matching([&](auto const& resize) { return resize.remote_layer_id == remote_layer_id; });
+ if (had_synchronized_resizes && m_synchronized_resizes.is_empty())
+ release_synchronized_resize_hold();
}
void CompositorState::show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId> context_id)
@@ -222,6 +226,7 @@ void CompositorState::show_context_in_remote_layer(u64 remote_layer_id, Optional
if (remote_layer->value.shown_context_id == context_id)
return;
+ remote_layer->value.size_of_shown_frame_laid_out_by_page = {};
if (auto shown_context_id = exchange(remote_layer->value.shown_context_id, {}); shown_context_id.has_value()) {
if (auto* shown_context = presented_context_if_present(*shown_context_id))
stop_showing_context_in_remote_layer(*shown_context_id, *shown_context);
@@ -235,8 +240,10 @@ void CompositorState::show_context_in_remote_layer(u64 remote_layer_id, Optional
return;
}
if (context->remote_layer_id.has_value()) {
- if (auto other_remote_layer = m_remote_layers.find(*context->remote_layer_id); other_remote_layer != m_remote_layers.end())
+ if (auto other_remote_layer = m_remote_layers.find(*context->remote_layer_id); other_remote_layer != m_remote_layers.end()) {
other_remote_layer->value.shown_context_id = {};
+ other_remote_layer->value.size_of_shown_frame_laid_out_by_page = {};
+ }
}
remote_layer->value.shown_context_id = context_id;
@@ -258,6 +265,70 @@ void CompositorState::set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::In
remote_layer->value.layer->place_frame(fit_within);
}
+// A UI that never commits its resize, as one that crashed, would otherwise freeze every window of the browser.
+static constexpr auto SYNCHRONIZED_RESIZE_HOLD_LIMIT = AK::Duration::from_seconds(1);
+
+void CompositorState::begin_synchronized_resize(u64 remote_layer_id, u64 token)
+{
+ if (!m_remote_layers.contains(remote_layer_id))
+ return;
+ if (!m_remote_layer_changes_are_held) {
+ m_remote_layer_changes_are_held = true;
+ RemoteLayer::hold_changes_on_screen();
+ if (!m_synchronized_resize_hold_timeout) {
+ m_synchronized_resize_hold_timeout = Core::Timer::create_single_shot(SYNCHRONIZED_RESIZE_HOLD_LIMIT, [this] {
+ dbgln("Compositor: Releasing the window resize that was never committed");
+ release_synchronized_resize_hold();
+ });
+ }
+ m_synchronized_resize_hold_timeout->start();
+ }
+ m_synchronized_resizes.append({ .remote_layer_id = remote_layer_id, .token = token, .awaited_size = {}, .frame_is_held = false });
+}
+
+void CompositorState::await_synchronized_resize_frame(u64 remote_layer_id, u64 token, Gfx::IntSize size)
+{
+ for (auto& resize : m_synchronized_resizes) {
+ if (resize.remote_layer_id == remote_layer_id && resize.token == token)
+ resize.awaited_size = size;
+ }
+ report_held_synchronized_resize_frames(remote_layer_id);
+}
+
+void CompositorState::report_held_synchronized_resize_frames(u64 remote_layer_id)
+{
+ auto remote_layer = m_remote_layers.find(remote_layer_id);
+ if (remote_layer == m_remote_layers.end())
+ return;
+ auto shown_size = remote_layer->value.size_of_shown_frame_laid_out_by_page;
+ for (auto& resize : m_synchronized_resizes) {
+ if (resize.remote_layer_id != remote_layer_id || resize.frame_is_held || !resize.awaited_size.has_value() || resize.awaited_size != shown_size)
+ continue;
+ resize.frame_is_held = true;
+ remote_layer->value.presentation->did_hold_synchronized_resize_frame(remote_layer_id, resize.token);
+ }
+}
+
+void CompositorState::commit_synchronized_resize(u64 remote_layer_id, u64 token)
+{
+ m_synchronized_resizes.remove_all_matching([&](auto const& resize) {
+ return resize.remote_layer_id == remote_layer_id && resize.token == token;
+ });
+ if (m_synchronized_resizes.is_empty())
+ release_synchronized_resize_hold();
+ if (auto remote_layer = m_remote_layers.find(remote_layer_id); remote_layer != m_remote_layers.end())
+ remote_layer->value.presentation->did_commit_synchronized_resize(remote_layer_id, token);
+}
+
+void CompositorState::release_synchronized_resize_hold()
+{
+ if (m_synchronized_resize_hold_timeout)
+ m_synchronized_resize_hold_timeout->stop();
+ m_synchronized_resizes.clear();
+ if (exchange(m_remote_layer_changes_are_held, false))
+ RemoteLayer::release_changes_held_off_screen();
+}
+
void CompositorState::stop_showing_context_in_remote_layer(Web::CompositorContextId context_id, PresentedContext& context)
{
context.remote_layer_id = {};
@@ -1119,6 +1190,7 @@ void CompositorState::present_frame(Web::CompositorContextId context_id, Gfx::In
{
auto* context = presented_context_if_present(context_id);
VERIFY(context);
+ context->size_laid_out_by_page = viewport_rect.size();
// The frame joins whatever is queued, so a frame queued earlier cannot be presented after it.
context->presenter.queue_present_frame({ viewport_rect, {} });
if (try_present_frame_during_resize(context_id, *context))
@@ -1427,7 +1499,10 @@ void CompositorState::show_frame_in_remote_layer(Web::CompositorContextId contex
auto const* shared_image_buffer = context.presenter.shared_image_buffer_of_latest_rendered_store();
VERIFY(shared_image_buffer);
- remote_layer->value.layer->show_frame(*shared_image_buffer, pending_present.viewport_rect.size(), pending_present.device_pixel_ratio, remote_layer->value.fit_within);
+ auto frame_size = pending_present.viewport_rect.size();
+ remote_layer->value.layer->show_frame(*shared_image_buffer, frame_size, pending_present.device_pixel_ratio, remote_layer->value.fit_within);
+ remote_layer->value.size_of_shown_frame_laid_out_by_page = frame_size == context.size_laid_out_by_page ? Optional<Gfx::IntSize> { frame_size } : OptionalNone {};
+ report_held_synchronized_resize_frames(remote_layer->key);
remote_layer->value.presentation->did_present_frame_in_remote_layer(context_id, pending_present.viewport_rect, pending_present.damage_rect, pending_present.device_pixel_ratio);
}
diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.h
index 3442d761a21..a8a687bfc3a 100644
--- a/Services/Compositor/CompositorState.h
+++ b/Services/Compositor/CompositorState.h
@@ -53,6 +53,8 @@ public:
virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) = 0;
// The frame is on screen already, in the remote layer that shows the context.
virtual void did_present_frame_in_remote_layer(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, double device_pixel_ratio) = 0;
+ virtual void did_hold_synchronized_resize_frame(u64 remote_layer_id, u64 token) = 0;
+ virtual void did_commit_synchronized_resize(u64 remote_layer_id, u64 token) = 0;
};
class CompositorStateWebContentClient {
@@ -166,6 +168,13 @@ public:
// A view that scales the frames to fit it gives its size, in device pixels.
void set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within);
+ // A window resize is shown together with the page's frame of the new size: the remote layers hold their changes
+ // off screen from when the resize begins until the window commits it, and the presentation hears once the layer
+ // holds a frame that the page laid out at the size it awaits.
+ void begin_synchronized_resize(u64 remote_layer_id, u64 token);
+ void await_synchronized_resize_frame(u64 remote_layer_id, u64 token, Gfx::IntSize);
+ void commit_synchronized_resize(u64 remote_layer_id, u64 token);
+
struct PlaceholderCanvasAllocation {
Compositing::CanvasId canvas_id;
u64 secret { 0 };
@@ -211,6 +220,8 @@ private:
// 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 };
Optional<u64> remote_layer_id;
+ // The viewport size of the frame that the page presented last, which it laid out at.
+ Gfx::IntSize size_laid_out_by_page;
PacedTickRequest rendering_opportunity_request;
PacedTickRequest clock_tick_request;
};
@@ -321,7 +332,21 @@ private:
OwnPtr<RemoteLayer> layer;
Optional<Web::CompositorContextId> shown_context_id;
Optional<Gfx::IntSize> fit_within;
+ // The size the page laid out the frame on display at, if it is the size of the frame.
+ Optional<Gfx::IntSize> size_of_shown_frame_laid_out_by_page;
+ };
+
+ struct SynchronizedResize {
+ u64 remote_layer_id { 0 };
+ u64 token { 0 };
+ Optional<Gfx::IntSize> awaited_size;
+ bool frame_is_held { false };
};
+ void report_held_synchronized_resize_frames(u64 remote_layer_id);
+ void release_synchronized_resize_hold();
+ Vector<SynchronizedResize> m_synchronized_resizes;
+ bool m_remote_layer_changes_are_held { false };
+ RefPtr<Core::Timer> m_synchronized_resize_hold_timeout;
HashMap<u64, RemoteLayerState> m_remote_layers;
struct VideoSinkState {
diff --git a/Services/Compositor/ConnectionFromClient.h b/Services/Compositor/ConnectionFromClient.h
index 878ef380eb1..6e89cb2e1e6 100644
--- a/Services/Compositor/ConnectionFromClient.h
+++ b/Services/Compositor/ConnectionFromClient.h
@@ -39,6 +39,9 @@ private:
virtual void destroy_remote_layer(u64 remote_layer_id) override;
virtual void show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId>) override;
virtual void set_remote_layer_fit(u64 remote_layer_id, Optional<Gfx::IntSize> fit_within) override;
+ virtual void begin_synchronized_resize(u64 remote_layer_id, u64 token) override { m_compositor_state->begin_synchronized_resize(remote_layer_id, token); }
+ virtual void await_synchronized_resize_frame(u64 remote_layer_id, u64 token, Gfx::IntSize size) override { m_compositor_state->await_synchronized_resize_frame(remote_layer_id, token, size); }
+ virtual void commit_synchronized_resize(u64 remote_layer_id, u64 token) override { m_compositor_state->commit_synchronized_resize(remote_layer_id, token); }
virtual void create_context(Web::CompositorContextId, Optional<u64> page_id, i32 web_content_connection_id) override;
virtual void viewport_size_updated(Web::CompositorContextId, Gfx::IntSize, double device_pixel_ratio, Compositing::WindowResizingInProgress) override;
virtual void set_ui_overlays(Web::CompositorContextId, Compositing::UIOverlays) override;
diff --git a/Services/Compositor/ConnectionFromPresentation.h b/Services/Compositor/ConnectionFromPresentation.h
index 52ed2a85aaa..30afd662488 100644
--- a/Services/Compositor/ConnectionFromPresentation.h
+++ b/Services/Compositor/ConnectionFromPresentation.h
@@ -37,6 +37,8 @@ private:
virtual void did_retire_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids) override;
virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) override;
virtual void did_present_frame_in_remote_layer(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, double device_pixel_ratio) override;
+ virtual void did_hold_synchronized_resize_frame(u64 remote_layer_id, u64 token) override { async_did_hold_synchronized_resize_frame(remote_layer_id, token); }
+ virtual void did_commit_synchronized_resize(u64 remote_layer_id, u64 token) override { async_did_commit_synchronized_resize(remote_layer_id, token); }
virtual ErrorOr<OwnPtr<IPC::MessageBuffer>> handle_raw(IPC::RawMessage&) override;
virtual void presented_bitmap_ready_to_paint(Web::CompositorContextId, i32 bitmap_id) override;
diff --git a/Services/Compositor/RemoteLayer.h b/Services/Compositor/RemoteLayer.h
index 048d325ed50..e71d7d75587 100644
--- a/Services/Compositor/RemoteLayer.h
+++ b/Services/Compositor/RemoteLayer.h
@@ -38,6 +38,11 @@ public:
// Places the frame on display again, as when the view that it is scaled into changes size.
void place_frame(Optional<Gfx::IntSize> fit_within);
+ // While held, what every remote layer of this process shows changes nowhere on screen, and changes all at once
+ // when released.
+ static void hold_changes_on_screen();
+ static void release_changes_held_off_screen();
+
private:
RemoteLayer() = default;
@@ -68,6 +73,14 @@ inline void RemoteLayer::show_frame(Gfx::SharedImageBuffer const&, Gfx::IntSize,
inline void RemoteLayer::place_frame(Optional<Gfx::IntSize>)
{
}
+
+inline void RemoteLayer::hold_changes_on_screen()
+{
+}
+
+inline void RemoteLayer::release_changes_held_off_screen()
+{
+}
#endif
}
diff --git a/Services/Compositor/RemoteLayer.mm b/Services/Compositor/RemoteLayer.mm
index 4c2e7b81ca5..5dcffa48ddf 100644
--- a/Services/Compositor/RemoteLayer.mm
+++ b/Services/Compositor/RemoteLayer.mm
@@ -64,6 +64,19 @@ static void commit_layer_changes(Callback callback)
}
}
+// The transactions of each change nest in this one until it is committed, so none of them reaches the window server
+// before then.
+void RemoteLayer::hold_changes_on_screen()
+{
+ [CATransaction begin];
+}
+
+void RemoteLayer::release_changes_held_off_screen()
+{
+ [CATransaction commit];
+ [CATransaction flush];
+}
+
static CALayer* as_layer(void* layer)
{
return static_cast<CALayer*>(layer);
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index f0960c46e09..64ecc600a1f 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -935,6 +935,29 @@ void ConnectionFromUI::view_take_frame_snapshot(u64 view_id, u64 request_id, Opt
});
}
+void ConnectionFromUI::view_begin_synchronized_resize(u64 view_id, u64 token)
+{
+ if (auto* view = find_view(view_id))
+ view->begin_synchronized_resize(token);
+}
+
+// The UI waits for the answer on its main thread, so a view that is gone answers as well.
+void ConnectionFromUI::view_await_synchronized_resize_frame(u64 view_id, u64 token, Gfx::IntSize size)
+{
+ if (auto* view = find_view(view_id))
+ view->await_synchronized_resize_frame(token, size);
+ else
+ async_view_did_hold_synchronized_resize_frame(view_id, token);
+}
+
+void ConnectionFromUI::view_commit_synchronized_resize(u64 view_id, u64 token)
+{
+ if (auto* view = find_view(view_id))
+ view->commit_synchronized_resize(token);
+ else
+ async_view_did_commit_synchronized_resize(view_id, token);
+}
+
void ConnectionFromUI::view_set_presents_from_cpu_memory(u64 view_id, bool presents_from_cpu_memory)
{
if (auto* view = find_view(view_id)) {
diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.h
index 9a2a57bbfed..4e3e55d124d 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -162,6 +162,9 @@ private:
virtual void view_set_hosts_remote_layer(u64 view_id, bool hosts_remote_layer) override;
virtual void view_set_wants_frame_notifications(u64 view_id, bool wants_frame_notifications) override;
virtual void view_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::IntSize> maximum_size) override;
+ virtual void view_begin_synchronized_resize(u64 view_id, u64 token) override;
+ virtual void view_await_synchronized_resize_frame(u64 view_id, u64 token, Gfx::IntSize size) override;
+ virtual void view_commit_synchronized_resize(u64 view_id, u64 token) override;
virtual void view_set_has_system_focus(u64 view_id, bool has_system_focus) override;
virtual void view_open_url_in_new_tab(u64 view_id, URL::URL url, Web::HTML::ActivateTab activate_tab) override;
virtual void view_open_url_in_new_window(u64 view_id, URL::URL url, WebView::IsPrivate is_private) override;
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index 1d88b98ef16..65dc0660347 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -329,6 +329,8 @@ endpoint HubClient
view_set_remote_layer(u64 view_id, u32 core_animation_context_id) =|
view_did_present_frame_in_remote_layer(u64 view_id, Gfx::IntSize size, double device_pixel_ratio) =|
view_did_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::ShareableBitmap> snapshot) =|
+ view_did_hold_synchronized_resize_frame(u64 view_id, u64 token) =|
+ view_did_commit_synchronized_resize(u64 view_id, u64 token) =|
view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) =|
view_finish_handling_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event, Web::EventResult result) =|
view_finish_handling_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) =|
diff --git a/Services/Hub/HubServer.ipc b/Services/Hub/HubServer.ipc
index 313c27d5222..55f67529b3d 100644
--- a/Services/Hub/HubServer.ipc
+++ b/Services/Hub/HubServer.ipc
@@ -240,6 +240,9 @@ endpoint HubServer
// The UI hears of the first frame shown in a new remote layer, and of every frame while it wants to.
view_set_wants_frame_notifications(u64 view_id, bool wants_frame_notifications) =|
view_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::IntSize> maximum_size) =|
+ view_begin_synchronized_resize(u64 view_id, u64 token) =|
+ view_await_synchronized_resize_frame(u64 view_id, u64 token, Gfx::IntSize size) =|
+ view_commit_synchronized_resize(u64 view_id, u64 token) =|
view_set_has_system_focus(u64 view_id, bool has_system_focus) =|
view_open_url_in_new_tab(u64 view_id, URL::URL url, Web::HTML::ActivateTab activate_tab) =|
view_open_url_in_new_window(u64 view_id, URL::URL url, WebView::IsPrivate is_private) =|
diff --git a/Services/Hub/View.cpp b/Services/Hub/View.cpp
index e38162bb741..806b29a2273 100644
--- a/Services/Hub/View.cpp
+++ b/Services/Hub/View.cpp
@@ -710,6 +710,18 @@ void View::did_present_frame_in_remote_layer(Gfx::IntSize size, double device_pi
ui->async_view_did_present_frame_in_remote_layer(view_id(), size, device_pixel_ratio);
}
+void View::did_hold_synchronized_resize_frame(u64 token)
+{
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_view_did_hold_synchronized_resize_frame(view_id(), token);
+}
+
+void View::did_commit_synchronized_resize(u64 token)
+{
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_view_did_commit_synchronized_resize(view_id(), token);
+}
+
void View::prepare_page_for_navigable(WebView::WebContentPage& page)
{
ViewImplementation::prepare_page_for_navigable(page);
diff --git a/Services/Hub/View.h b/Services/Hub/View.h
index 711c1be08f0..2b2c290f40c 100644
--- a/Services/Hub/View.h
+++ b/Services/Hub/View.h
@@ -76,6 +76,8 @@ private:
virtual void release_backing_store(i32) override { }
virtual void did_create_remote_layer(u32 core_animation_context_id) override;
virtual void did_present_frame_in_remote_layer(Gfx::IntSize, double device_pixel_ratio) override;
+ virtual void did_hold_synchronized_resize_frame(u64 token) override;
+ virtual void did_commit_synchronized_resize(u64 token) override;
void forward_callbacks_to_ui(bool is_tab);
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index a607dd612e3..4f621e4e7e7 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -168,6 +168,9 @@ private:
virtual void view_set_remote_layer(u64 view_id, u32 core_animation_context_id) override;
virtual void view_did_present_frame_in_remote_layer(u64 view_id, Gfx::IntSize size, double device_pixel_ratio) override;
virtual void view_did_take_frame_snapshot(u64 view_id, u64 request_id, Optional<Gfx::ShareableBitmap> snapshot) override;
+ // NB: A view waits for these while CoreAnimation commits a resize, and ignores those that come too late.
+ virtual void view_did_hold_synchronized_resize_frame(u64, u64) override { }
+ virtual void view_did_commit_synchronized_resize(u64, u64) override { }
virtual void view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) override;
virtual void view_finish_handling_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event, Web::EventResult result) override;
virtual void view_finish_handling_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) override;
diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.h
index d8364fbdcaa..0c0580e36e3 100644
--- a/UI/Qt/RemoteView.h
+++ b/UI/Qt/RemoteView.h
@@ -96,6 +96,9 @@ public:
void set_wants_frame_notifications(bool wants_frame_notifications) { HubConnection::the().async_view_set_wants_frame_notifications(view_id(), wants_frame_notifications); }
// What the view shows, from the Compositor, scaled down to fit within the size given.
void take_frame_snapshot(Optional<Gfx::IntSize> maximum_size, Function<void(Optional<Gfx::ShareableBitmap>)>);
+ void begin_synchronized_resize(u64 token) { HubConnection::the().async_view_begin_synchronized_resize(view_id(), token); }
+ void await_synchronized_resize_frame(u64 token, Gfx::IntSize size) { HubConnection::the().async_view_await_synchronized_resize_frame(view_id(), token, size); }
+ void commit_synchronized_resize(u64 token) { HubConnection::the().async_view_commit_synchronized_resize(view_id(), token); }
void did_take_frame_snapshot(u64 request_id, Optional<Gfx::ShareableBitmap>);
void set_has_system_focus(bool has_system_focus) { HubConnection::the().async_view_set_has_system_focus(view_id(), has_system_focus); }
void load(URL::URL const& url, Web::Bindings::NavigationHistoryBehavior navigation_history_behavior = Web::Bindings::NavigationHistoryBehavior::Auto) { HubConnection::the().async_view_load(view_id(), url, navigation_history_behavior); }
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 90ce72bd960..12f9db0cd79 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -1875,6 +1875,7 @@ void WebContentView::resizeEvent(QResizeEvent* event)
WebContentViewBase::resizeEvent(event);
#ifdef LADYBIRD_QT_USE_IOSURFACE_LAYER
update_iosurface_layer_frame();
+ synchronize_resize_with_remote_layer();
#endif
for (auto* overlay : overlays()) {
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index 3b7e3436028..36f027b9c29 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -404,6 +404,12 @@ private:
static bool can_host_remote_layers();
virtual void remote_layer_changed(u32 core_animation_context_id) override;
void update_remote_layer_host();
+ void synchronize_resize_with_remote_layer();
+ void await_resized_frame_before_commit(u64 token);
+ void release_resized_frame_with_commit(u64 token);
+ Optional<u64> m_synchronized_resize_token;
+ MonotonicTime m_last_resize_time { MonotonicTime::now_coarse() };
+ bool m_page_lags_behind_resize { false };
void* m_remote_layer_host { nullptr };
u32 m_remote_layer_context_id { 0 };
bool m_asked_for_remote_layer { false };
diff --git a/UI/Qt/WebContentViewMac.mm b/UI/Qt/WebContentViewMac.mm
index dcc47ae98b4..50169487592 100644
--- a/UI/Qt/WebContentViewMac.mm
+++ b/UI/Qt/WebContentViewMac.mm
@@ -11,6 +11,8 @@
#include <UI/Qt/WebContentView.h>
#include <QChildEvent>
+#include <QPointer>
+#include <QScreen>
#include <QWindow>
#import <AppKit/AppKit.h>
@@ -23,6 +25,18 @@
@property uint32_t contextId;
@end
+// CoreAnimation calls these handlers while it commits a transaction: before the new layer tree goes to the window
+// server, and after it has gone but before the window shows it. Neither is public API.
+typedef NS_ENUM(unsigned int, LadybirdTransactionPhase) {
+ LadybirdTransactionPhasePreLayout,
+ LadybirdTransactionPhasePreCommit,
+ LadybirdTransactionPhasePostCommit,
+};
+
+@interface CATransaction (LadybirdCommitHandlers)
++ (void)addCommitHandler:(void (^)(void))block forPhase:(LadybirdTransactionPhase)phase;
+@end
+
namespace Ladybird {
static CALayer* as_layer(void* layer)
@@ -48,6 +62,95 @@ bool WebContentView::can_host_remote_layers()
return core_animation_layer_host_class() != nil;
}
+// A resize commits the window's new size before the page has a frame of that size, unless the commit waits for it. The
+// Compositor holds its layers' changes from the first resize of a transaction, the commit waits for a frame that the
+// page laid out at the new size before it sends the window's layer tree, and the Compositor lets its changes go after
+// that, when the window server shows the two together.
+//
+// The commit waits no longer than a frame of the display, as the window could not resize any faster than the page lays
+// out otherwise. A page that misses that frame is not waited for again until the resizing pauses, and lags behind it
+// until then, as without the wait.
+static constexpr auto HELD_FRAME_RELEASE_WAIT_LIMIT = AK::Duration::from_milliseconds(50);
+static constexpr int FRAMES_WITHOUT_RESIZE_THAT_END_A_RESIZE = 4;
+
+static AK::Duration display_frame_interval(QWidget const& widget)
+{
+ auto refresh_rate = widget.screen() ? widget.screen()->refreshRate() : 60.0;
+ if (!(refresh_rate > 0))
+ refresh_rate = 60.0;
+ return AK::Duration::from_nanoseconds(static_cast<i64>(1'000'000'000.0 / refresh_rate));
+}
+
+void WebContentView::synchronize_resize_with_remote_layer()
+{
+ if (m_remote_layer_context_id == 0 || m_synchronized_resize_token.has_value() || !isVisible())
+ return;
+
+ auto now = MonotonicTime::now();
+ auto frame_interval = display_frame_interval(*this);
+ auto resizing_paused = now - m_last_resize_time >= AK::Duration::from_nanoseconds(frame_interval.to_nanoseconds() * FRAMES_WITHOUT_RESIZE_THAT_END_A_RESIZE);
+ m_last_resize_time = now;
+ if (resizing_paused)
+ m_page_lags_behind_resize = false;
+ if (m_page_lags_behind_resize)
+ return;
+
+ if (![CATransaction respondsToSelector:@selector(addCommitHandler:forPhase:)])
+ return;
+
+ static u64 s_next_token = 1;
+ auto token = s_next_token++;
+ m_synchronized_resize_token = token;
+ begin_synchronized_resize(token);
+
+ QPointer<WebContentView> view { this };
+ [CATransaction
+ addCommitHandler:^{
+ if (view)
+ view->await_resized_frame_before_commit(token);
+ }
+ forPhase:LadybirdTransactionPhasePreCommit];
+ [CATransaction
+ addCommitHandler:^{
+ if (view)
+ view->release_resized_frame_with_commit(token);
+ }
+ forPhase:LadybirdTransactionPhasePostCommit];
+}
+
+void WebContentView::await_resized_frame_before_commit(u64 token)
+{
+ if (m_viewport_push_pending) {
+ m_viewport_push_pending = false;
+ update_viewport_size();
+ }
+ await_synchronized_resize_frame(token, m_viewport_size);
+
+ auto deadline = MonotonicTime::now() + display_frame_interval(*this);
+ for (;;) {
+ auto message = HubConnection::the().wait_for_specific_endpoint_message_until<Messages::HubClient::ViewDidHoldSynchronizedResizeFrame>(deadline);
+ if (!message) {
+ m_page_lags_behind_resize = true;
+ return;
+ }
+ if (message->view_id() == view_id() && message->token() == token)
+ return;
+ }
+}
+
+void WebContentView::release_resized_frame_with_commit(u64 token)
+{
+ m_synchronized_resize_token = {};
+ commit_synchronized_resize(token);
+
+ auto deadline = MonotonicTime::now() + HELD_FRAME_RELEASE_WAIT_LIMIT;
+ for (;;) {
+ auto message = HubConnection::the().wait_for_specific_endpoint_message_until<Messages::HubClient::ViewDidCommitSynchronizedResize>(deadline);
+ if (!message || (message->view_id() == view_id() && message->token() == token))
+ return;
+ }
+}
+
void WebContentView::remote_layer_changed(u32 core_animation_context_id)
{
m_remote_layer_context_id = core_animation_context_id;