Skip to content

Truly independent web browser

LibWebView+Hub+UI: Let views ask for a remote layer to show their page

Aliaksandr Kalenikd6786152d7f2
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index d47c4d7b5bc..7da1eb1e7aa 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -1466,6 +1466,40 @@ void Application::update_compositor_context_visibility(Web::CompositorContextId
m_compositor_client->async_set_context_visibility(context_id, context_visibility);
}
+Optional<Application::RemoteLayer> Application::create_remote_layer()
+{
+ if (!m_compositor_presentation_client || !can_send_compositor_process_ipc(m_compositor_client))
+ return {};
+ VERIFY(m_compositor_client);
+
+ auto remote_layer_id = m_next_remote_layer_id++;
+ auto response = m_compositor_client->try_create_remote_layer(m_compositor_presentation_id, remote_layer_id);
+ if (response.is_error())
+ return {};
+ auto core_animation_context_id = response.release_value();
+ if (core_animation_context_id == 0)
+ return {};
+ return RemoteLayer { .id = remote_layer_id, .core_animation_context_id = core_animation_context_id };
+}
+
+void Application::destroy_remote_layer(u64 remote_layer_id)
+{
+ if (!can_send_compositor_process_ipc(m_compositor_client))
+ return;
+ VERIFY(m_compositor_client);
+
+ m_compositor_client->async_destroy_remote_layer(remote_layer_id);
+}
+
+void Application::show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId> context_id)
+{
+ if (!can_send_compositor_process_ipc(m_compositor_client))
+ return;
+ VERIFY(m_compositor_client);
+
+ m_compositor_client->async_show_context_in_remote_layer(remote_layer_id, context_id);
+}
+
void Application::update_compositor_context_presents_from_cpu_memory(Web::CompositorContextId context_id, bool presents_from_cpu_memory)
{
if (!can_send_compositor_process_ipc(m_compositor_client))
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 646495aa81e..bd7a593385b 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -266,6 +266,15 @@ public:
void update_compositor_display_metadata(Web::CompositorContextId, Optional<u64> display_id, double refresh_rate);
void update_compositor_context_visibility(Web::CompositorContextId, Web::HTML::VisibilityState);
void update_compositor_context_presents_from_cpu_memory(Web::CompositorContextId, bool);
+
+ struct RemoteLayer {
+ u64 id { 0 };
+ u32 core_animation_context_id { 0 };
+ };
+ // None where the Compositor offers no remote layers, or is gone.
+ Optional<RemoteLayer> create_remote_layer();
+ void destroy_remote_layer(u64 remote_layer_id);
+ void show_context_in_remote_layer(u64 remote_layer_id, Optional<Web::CompositorContextId>);
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&);
bool handle_and_dispatch_mouse_event_in_compositor(Web::CompositorContextId, Web::MouseEvent const&);
@@ -594,6 +603,8 @@ private:
RefPtr<CompositorClient> m_compositor_client;
RefPtr<CompositorPresentationClient> m_compositor_presentation_client;
u64 m_compositor_presentation_id { 0 };
+ // IDs are never reused, so one from before a Compositor restart names no layer of the new process.
+ u64 m_next_remote_layer_id { 1 };
bool m_reported_compositor_gpu_presentation_unavailable { false };
// The last identity a Compositor reported, which outlives that Compositor until its replacement reports.
Optional<Optional<Gfx::GpuDeviceIdentity>> m_compositor_gpu_device_identity;
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index ff37b1d48b7..ae7143cb214 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -148,6 +148,9 @@ ViewImplementation::~ViewImplementation()
reset_spell_checking();
+ if (m_remote_layer_id.has_value())
+ Application::the().destroy_remote_layer(*m_remote_layer_id);
+
// NB: WebDriver BiDi may activate another top-level traversable, which must not be this one that is going away.
if (!m_window_handle.is_empty() && !m_owner_view_id.has_value()) {
if (auto* webdriver_bidi = WebDriverBiDi::the())
@@ -448,6 +451,40 @@ void ViewImplementation::server_did_present_frame_in_remote_layer(Badge<WebConte
if (m_crash_state.has_value() && m_crash_state->recovery_started)
set_crash_state({});
TabPerformanceMonitor::did_present(view_id());
+ did_present_frame_in_remote_layer(size, device_pixel_ratio);
+}
+
+void ViewImplementation::set_hosts_remote_layer(bool hosts_remote_layer)
+{
+ if (m_hosts_remote_layer == hosts_remote_layer)
+ return;
+ m_hosts_remote_layer = hosts_remote_layer;
+
+ if (!hosts_remote_layer) {
+ if (auto remote_layer_id = exchange(m_remote_layer_id, {}); remote_layer_id.has_value()) {
+ Application::the().destroy_remote_layer(*remote_layer_id);
+ did_create_remote_layer(0);
+ }
+ return;
+ }
+
+ auto remote_layer = Application::the().create_remote_layer();
+ if (!remote_layer.has_value()) {
+ did_create_remote_layer(0);
+ return;
+ }
+ m_remote_layer_id = remote_layer->id;
+ Application::the().show_context_in_remote_layer(remote_layer->id, page().compositor_context_id());
+ did_create_remote_layer(remote_layer->core_animation_context_id);
+}
+
+void ViewImplementation::recreate_remote_layer_after_compositor_reconnect(Badge<WebContentClient>)
+{
+ if (!m_hosts_remote_layer)
+ return;
+ m_hosts_remote_layer = false;
+ m_remote_layer_id = {};
+ set_hosts_remote_layer(true);
}
void ViewImplementation::release_backing_store(i32 bitmap_id)
@@ -3581,6 +3618,9 @@ void ViewImplementation::display_page_changed(RefPtr<WebContentPage> previous_pa
install_backing_stores(move(backing_stores->bitmap_ids), move(backing_stores->backing_stores));
auto compositor_context_id = page.compositor_context_id();
+ // The remote layer goes on showing the outgoing page's last frame until the page presents its first one.
+ if (m_remote_layer_id.has_value())
+ Application::the().show_context_in_remote_layer(*m_remote_layer_id, compositor_context_id);
Application::the().update_compositor_viewport(compositor_context_id, viewport_size().to_type<int>(), m_device_pixel_ratio);
Application::the().update_compositor_context_visibility(compositor_context_id, traversable().system_visibility_state());
Application::the().update_compositor_context_presents_from_cpu_memory(compositor_context_id, m_presents_from_cpu_memory);
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 67856a0bdf3..fa89b61fa30 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -234,6 +234,11 @@ public:
// in GPU memory that the view would have to read back for every frame.
void set_presents_from_cpu_memory(bool);
bool presents_from_cpu_memory() const { return m_presents_from_cpu_memory; }
+ // Whether the UI shows the view's frames in a remote layer, which the Compositor commits them into itself, rather
+ // than in backing stores the view receives.
+ void set_hosts_remote_layer(bool);
+ // 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>);
void set_has_system_focus(bool);
void load(URL::URL const&, Web::Bindings::NavigationHistoryBehavior = Web::Bindings::NavigationHistoryBehavior::Auto);
@@ -759,6 +764,10 @@ protected:
virtual void insert_clipboard_item(Web::Clipboard::SystemClipboardItem);
virtual void did_accept_presented_backing_store(i32, Gfx::IntRect) { }
+ // The view's remote layer was made, or made again. The ID is 0 when there is none, and frames arrive in backing
+ // 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; }
// 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);
@@ -888,6 +897,8 @@ protected:
NonnullOwnPtr<PageBackingStores> m_backing_stores { make<PageBackingStores>() };
bool m_pinch_gesture_was_canceled { false };
bool m_presents_from_cpu_memory { false };
+ bool m_hosts_remote_layer { false };
+ Optional<u64> m_remote_layer_id;
// Set once an assistive technology asked for the page tree; every display page the view shows from then on gets
// asked for its tree too.
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index 63e5c97dda0..97b90eccbf6 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -569,6 +569,7 @@ void WebContentClient::replay_compositor_view_state_after_reconnect(Badge<Applic
if (!m_compositor_contexts.contains(context_id))
continue;
auto& view = page->view();
+ view.recreate_remote_layer_after_compositor_reconnect({});
Application::the().update_compositor_viewport(context_id, view.viewport_size().to_type<int>(), view.device_pixel_ratio());
Application::the().update_compositor_display_metadata(context_id, view.display_id(), view.maximum_frames_per_second());
Application::the().update_compositor_context_visibility(context_id, view.traversable().system_visibility_state());
diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cpp
index 6609442cd5e..298d4f82530 100644
--- a/Services/Hub/ConnectionFromUI.cpp
+++ b/Services/Hub/ConnectionFromUI.cpp
@@ -910,6 +910,18 @@ void ConnectionFromUI::view_set_system_visibility_state(u64 view_id, Web::HTML::
}
}
+void ConnectionFromUI::view_set_hosts_remote_layer(u64 view_id, bool hosts_remote_layer)
+{
+ if (auto* view = find_view(view_id))
+ view->set_hosts_remote_layer(hosts_remote_layer);
+}
+
+void ConnectionFromUI::view_set_wants_frame_notifications(u64 view_id, bool wants_frame_notifications)
+{
+ if (auto* view = find_view(view_id))
+ view->set_wants_frame_notifications(wants_frame_notifications);
+}
+
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 01454ecf3c8..ae07af5fb93 100644
--- a/Services/Hub/ConnectionFromUI.h
+++ b/Services/Hub/ConnectionFromUI.h
@@ -159,6 +159,8 @@ private:
virtual void view_set_window_size(u64 view_id, Gfx::IntSize size) override;
virtual void view_set_system_visibility_state(u64 view_id, Web::HTML::VisibilityState visibility_state) override;
virtual void view_set_presents_from_cpu_memory(u64 view_id, bool presents_from_cpu_memory) override;
+ 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_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 bbb2edda955..98d92e5e12b 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -325,6 +325,9 @@ endpoint HubClient
view_keep_front_backing_store_as_backup(u64 view_id) =|
view_clear_backup_backing_store(u64 view_id) =|
view_clear_backing_stores(u64 view_id) =|
+ // The ID is 0 when the view has no remote layer, and the UI goes on showing backing stores.
+ 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_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 ee26d549867..2c665f2c3e8 100644
--- a/Services/Hub/HubServer.ipc
+++ b/Services/Hub/HubServer.ipc
@@ -236,6 +236,9 @@ endpoint HubServer
view_set_window_size(u64 view_id, Gfx::IntSize size) =|
view_set_system_visibility_state(u64 view_id, Web::HTML::VisibilityState visibility_state) =|
view_set_presents_from_cpu_memory(u64 view_id, bool presents_from_cpu_memory) =|
+ view_set_hosts_remote_layer(u64 view_id, bool hosts_remote_layer) =|
+ // 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_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 46252c092f3..e38162bb741 100644
--- a/Services/Hub/View.cpp
+++ b/Services/Hub/View.cpp
@@ -28,6 +28,7 @@ private:
track_installed_backing_stores(bitmap_ids);
if (auto* ui = ConnectionFromUI::the())
ui->async_view_install_backing_stores(m_view.view_id(), m_view.page().id().value(), move(bitmap_ids), move(backing_stores));
+ m_view.did_install_backing_stores_in_ui();
}
virtual void add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override
@@ -686,6 +687,29 @@ void View::did_accept_presented_backing_store(i32 bitmap_id, Gfx::IntRect damage
ui->async_view_accept_presented_backing_store(view_id(), bitmap_id, damage_rect);
}
+void View::did_create_remote_layer(u32 core_animation_context_id)
+{
+ m_ui_hosts_remote_layer = core_animation_context_id != 0;
+ m_ui_awaits_first_frame_in_remote_layer = m_ui_hosts_remote_layer;
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_view_set_remote_layer(view_id(), core_animation_context_id);
+}
+
+// The UI shows the backing stores it was given, as when a page from another process comes to display the tab, until it
+// hears of a frame shown in the remote layer again.
+void View::did_install_backing_stores_in_ui()
+{
+ m_ui_awaits_first_frame_in_remote_layer = m_ui_hosts_remote_layer;
+}
+
+void View::did_present_frame_in_remote_layer(Gfx::IntSize size, double device_pixel_ratio)
+{
+ if (!exchange(m_ui_awaits_first_frame_in_remote_layer, false) && !m_wants_frame_notifications)
+ return;
+ if (auto* ui = ConnectionFromUI::the())
+ ui->async_view_did_present_frame_in_remote_layer(view_id(), size, device_pixel_ratio);
+}
+
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 70ab13ca9da..711c1be08f0 100644
--- a/Services/Hub/View.h
+++ b/Services/Hub/View.h
@@ -48,6 +48,8 @@ public:
void set_system_theme(Core::AnonymousBuffer);
void set_screen_rects(ScreenRects);
bool scales_frames_to_fit() const { return m_scales_frames_to_fit; }
+ void set_wants_frame_notifications(bool wants_frame_notifications) { m_wants_frame_notifications = wants_frame_notifications; }
+ void did_install_backing_stores_in_ui();
using ViewImplementation::microphone_authorizer;
using ViewImplementation::rect_fitting_frame;
@@ -72,6 +74,8 @@ private:
virtual void did_accept_presented_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect) override;
// NB: The UI hands a backing store back, once it no longer shows what is in it.
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;
void forward_callbacks_to_ui(bool is_tab);
@@ -80,6 +84,9 @@ private:
Web::DevicePixelSize m_viewport_size;
bool m_websites_prefer_dark { false };
ByteBuffer m_last_state_pushed_to_ui;
+ bool m_wants_frame_notifications { false };
+ bool m_ui_hosts_remote_layer { false };
+ bool m_ui_awaits_first_frame_in_remote_layer { false };
// What the view waits to hear from the user, through the UI.
HashMap<u64, Function<void(bool)>> m_pending_answers;
diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cpp
index b6e00a54e65..e3c10abd770 100644
--- a/UI/Qt/HubConnection.cpp
+++ b/UI/Qt/HubConnection.cpp
@@ -148,6 +148,18 @@ void HubConnection::view_clear_backing_stores(u64 view_id)
view->did_clear_backing_stores();
}
+void HubConnection::view_set_remote_layer(u64 view_id, u32 core_animation_context_id)
+{
+ if (auto* view = RemoteView::find(view_id))
+ view->did_set_remote_layer(core_animation_context_id);
+}
+
+void HubConnection::view_did_present_frame_in_remote_layer(u64 view_id, Gfx::IntSize size, double device_pixel_ratio)
+{
+ if (auto* view = RemoteView::find(view_id))
+ view->did_present_frame_in_remote_layer(size, device_pixel_ratio);
+}
+
void HubConnection::view_finish_handling_key_event(u64 view_id, u64 input_id, Web::KeyEvent event)
{
if (auto* view = RemoteView::find(view_id))
diff --git a/UI/Qt/HubConnection.h b/UI/Qt/HubConnection.h
index ea8191d5163..eac317034a7 100644
--- a/UI/Qt/HubConnection.h
+++ b/UI/Qt/HubConnection.h
@@ -165,6 +165,8 @@ private:
virtual void view_keep_front_backing_store_as_backup(u64 view_id) override;
virtual void view_clear_backup_backing_store(u64 view_id) override;
virtual void view_clear_backing_stores(u64 view_id) override;
+ 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_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.cpp b/UI/Qt/RemoteView.cpp
index 40c97b94c4a..6cfee44c697 100644
--- a/UI/Qt/RemoteView.cpp
+++ b/UI/Qt/RemoteView.cpp
@@ -176,6 +176,13 @@ void RemoteView::did_accept_backing_store(i32 bitmap_id, Gfx::IntRect damage_rec
on_ready_to_paint();
}
+void RemoteView::did_present_frame_in_remote_layer(Gfx::IntSize size, double device_pixel_ratio)
+{
+ m_backing_stores.present_frame_shown_in_remote_layer(size, device_pixel_ratio);
+ if (on_ready_to_paint)
+ on_ready_to_paint();
+}
+
void RemoteView::release_backing_store_to_page(u64 page_id, i32 bitmap_id)
{
if (auto* hub = HubConnection::the_if_connected())
diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.h
index d43ba8a6eec..fcf464031aa 100644
--- a/UI/Qt/RemoteView.h
+++ b/UI/Qt/RemoteView.h
@@ -92,6 +92,8 @@ public:
void set_window_size(Gfx::IntSize size) { HubConnection::the().async_view_set_window_size(view_id(), size); }
void set_system_visibility_state(Web::HTML::VisibilityState visibility_state) { HubConnection::the().async_view_set_system_visibility_state(view_id(), move(visibility_state)); }
void set_presents_from_cpu_memory(bool presents_from_cpu_memory) { HubConnection::the().async_view_set_presents_from_cpu_memory(view_id(), presents_from_cpu_memory); }
+ void set_hosts_remote_layer(bool hosts_remote_layer) { HubConnection::the().async_view_set_hosts_remote_layer(view_id(), hosts_remote_layer); }
+ void set_wants_frame_notifications(bool wants_frame_notifications) { HubConnection::the().async_view_set_wants_frame_notifications(view_id(), wants_frame_notifications); }
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); }
void load_from_user_input(URL::URL const&);
@@ -239,6 +241,8 @@ public:
void did_keep_front_backing_store_as_backup() { m_backing_stores.keep_front_as_backup(); }
void did_clear_backup_backing_store() { m_backing_stores.clear_backup(); }
void did_clear_backing_stores() { m_backing_stores.clear(); }
+ void did_set_remote_layer(u32 core_animation_context_id) { remote_layer_changed(core_animation_context_id); }
+ void did_present_frame_in_remote_layer(Gfx::IntSize, double device_pixel_ratio);
// Hands a backing store back to the page that presented it, which can have outlived the view.
static void release_backing_store_to_page(u64 page_id, i32 bitmap_id);
@@ -249,6 +253,8 @@ public:
protected:
virtual void show_context_menu(Menu&, Gfx::IntPoint) { }
virtual void did_accept_presented_backing_store(i32, Gfx::IntRect) { }
+ // The ID is 0 when the view has no remote layer, and goes on showing backing stores.
+ virtual void remote_layer_changed(u32 core_animation_context_id) { (void)core_animation_context_id; }
virtual void release_backing_store(u64 page_id, i32 bitmap_id) { release_backing_store_to_page(page_id, bitmap_id); }
void set_page_background_color_to_system_canvas(bool dark) { HubConnection::the().async_view_set_page_background_color_to_system_canvas(view_id(), dark); }