Truly independent web browser
Hub+LibWebView+UI/Qt: Hold a view's backing stores in the UI
Andreas Kling060451a20001
diff --git a/Libraries/LibWebView/PageBackingStores.cpp b/Libraries/LibWebView/PageBackingStores.cppindex 93e93b9d4f1..047dfa862ad 100644--- a/Libraries/LibWebView/PageBackingStores.cpp+++ b/Libraries/LibWebView/PageBackingStores.cpp@@ -10,12 +10,10 @@ namespace WebView {Optional<PageBackingStores::Frame> PageBackingStores::displayed_frame() const{- if (m_has_usable_front_bitmap) {- VERIFY(m_front_bitmap.shared_image_buffer);- return Frame { *m_front_bitmap.shared_image_buffer, m_front_bitmap.last_painted_size.to_type<int>(), m_front_bitmap.last_painted_device_pixel_ratio, m_front_bitmap.id };- }- if (m_backup.shared_image_buffer)- return Frame { *m_backup.shared_image_buffer, m_backup.last_painted_size.to_type<int>(), m_backup.last_painted_device_pixel_ratio, {} };+ if (m_has_usable_front_bitmap)+ return Frame { m_front_bitmap.shared_image_buffer.ptr(), m_front_bitmap.last_painted_size.to_type<int>(), m_front_bitmap.last_painted_device_pixel_ratio, m_front_bitmap.id };+ if (m_has_backup)+ return Frame { m_backup.shared_image_buffer.ptr(), m_backup.last_painted_size.to_type<int>(), m_backup.last_painted_device_pixel_ratio, {} };return {};}@@ -33,37 +31,42 @@ Vector<Gfx::SharedImageBuffer const*> PageBackingStores::shared_image_buffers()return shared_image_buffers;}-void PageBackingStores::install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+void PageBackingStores::track_installed_backing_stores(ReadonlySpan<i32> bitmap_ids){- VERIFY(bitmap_ids.size() == backing_stores.size());VERIFY(!bitmap_ids.is_empty());// NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.- keep_front_as_backup();+ PageBackingStores::keep_front_as_backup();m_has_usable_front_bitmap = false;m_front_bitmap.id = bitmap_ids[0];- m_front_bitmap.shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[0])));+ m_front_bitmap.shared_image_buffer = nullptr;m_other_bitmaps.clear();- m_other_bitmaps.ensure_capacity(backing_stores.size() - 1);- for (size_t i = 1; i < backing_stores.size(); ++i) {- m_other_bitmaps.append({- .id = bitmap_ids[i],- .last_painted_size = {},- .shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[i]))),- });- }+ track_added_backing_stores(bitmap_ids.slice(1));+}++void PageBackingStores::track_added_backing_stores(ReadonlySpan<i32> bitmap_ids)+{+ m_other_bitmaps.ensure_capacity(m_other_bitmaps.size() + bitmap_ids.size());+ for (auto bitmap_id : bitmap_ids)+ m_other_bitmaps.append({ .id = bitmap_id, .last_painted_size = {}, .shared_image_buffer = nullptr });+}++void PageBackingStores::install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+{+ VERIFY(bitmap_ids.size() == backing_stores.size());+ track_installed_backing_stores(bitmap_ids);+ m_front_bitmap.shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[0])));+ for (size_t i = 1; i < backing_stores.size(); ++i)+ m_other_bitmaps[i - 1].shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[i])));}void PageBackingStores::add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){VERIFY(bitmap_ids.size() == backing_stores.size());- for (size_t i = 0; i < backing_stores.size(); ++i) {- m_other_bitmaps.append({- .id = bitmap_ids[i],- .last_painted_size = {},- .shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[i]))),- });- }+ auto first_new_bitmap = m_other_bitmaps.size();+ track_added_backing_stores(bitmap_ids);+ for (size_t i = 0; i < backing_stores.size(); ++i)+ m_other_bitmaps[first_new_bitmap + i].shared_image_buffer = make<Gfx::SharedImageBuffer>(Gfx::SharedImageBuffer::import_from_shared_image(move(backing_stores[i])));}void PageBackingStores::retire(ReadonlySpan<i32> bitmap_ids)@@ -84,7 +87,7 @@ PageBackingStores::PresentationResult PageBackingStores::present(i32 bitmap_id,m_other_bitmaps[*bitmap_index].last_painted_size = size.to_type<Web::DevicePixels>();m_other_bitmaps[*bitmap_index].last_painted_device_pixel_ratio = device_pixel_ratio;swap(m_other_bitmaps[*bitmap_index], m_front_bitmap);- clear_backup();+ PageBackingStores::clear_backup();return { .did_swap = true, .bitmap_id_to_release = previous_front_bitmap_id };}@@ -95,6 +98,13 @@ void PageBackingStores::keep_front_as_backup()m_backup.shared_image_buffer = move(m_front_bitmap.shared_image_buffer);m_backup.last_painted_size = m_front_bitmap.last_painted_size;m_backup.last_painted_device_pixel_ratio = m_front_bitmap.last_painted_device_pixel_ratio;+ m_has_backup = true;+}++void PageBackingStores::clear_backup()+{+ m_backup = {};+ m_has_backup = false;}void PageBackingStores::clear()diff --git a/Libraries/LibWebView/PageBackingStores.h b/Libraries/LibWebView/PageBackingStores.hindex e5007ab3fa2..eaa77f427f5 100644--- a/Libraries/LibWebView/PageBackingStores.h+++ b/Libraries/LibWebView/PageBackingStores.h@@ -22,8 +22,13 @@ namespace WebView {// is the front backing store, which holds the frame on display. The Compositor paints into the others.class WEBVIEW_API PageBackingStores {public:+ AK_ALLOC_WITH_KMALLOC;++ virtual ~PageBackingStores() = default;+struct Frame {- Gfx::SharedImageBuffer const& shared_image_buffer;+ // None where the frames are shown in another process, which holds the images.+ Gfx::SharedImageBuffer const* shared_image_buffer { nullptr };Gfx::IntSize size;double device_pixel_ratio { 1.0 };// Empty for a frame kept from backing stores that are gone.@@ -38,9 +43,9 @@ public:Vector<Gfx::SharedImageBuffer const*> shared_image_buffers() const;// Replaces every backing store. The first of the new ones becomes the front backing store.- void install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);- void add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);- void retire(ReadonlySpan<i32> bitmap_ids);+ virtual void install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);+ virtual void add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>);+ virtual void retire(ReadonlySpan<i32> bitmap_ids);struct PresentationResult {bool did_swap { false };@@ -48,14 +53,19 @@ public:i32 bitmap_id_to_release { -1 };};// Makes the backing store with this ID the front backing store, unless it is no longer one of ours.- PresentationResult present(i32 bitmap_id, Gfx::IntSize, double device_pixel_ratio);+ virtual PresentationResult present(i32 bitmap_id, Gfx::IntSize, double device_pixel_ratio);// Keeps the frame on display, so there is something to show until a new frame arrives.- void keep_front_as_backup();- void clear_backup() { m_backup = {}; }+ virtual void keep_front_as_backup();+ virtual void clear_backup();// Forgets every backing store, other than the frame kept as a backup.- void clear();+ virtual void clear();++protected:+ // These keep track of backing stores whose images another process holds.+ void track_installed_backing_stores(ReadonlySpan<i32> bitmap_ids);+ void track_added_backing_stores(ReadonlySpan<i32> bitmap_ids);private:struct SharedBitmap {@@ -69,6 +79,7 @@ private:Vector<SharedBitmap> m_other_bitmaps;bool m_has_usable_front_bitmap { false };SharedBitmap m_backup;+ bool m_has_backup { false };};}diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex d90a41d2af5..847e589b73f 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -450,10 +450,10 @@ void ViewImplementation::set_favicon(Optional<Gfx::Bitmap const&> favicon)// it there, before a document activates in the new page.void ViewImplementation::server_did_paint(Badge<WebContentPage>, i32 bitmap_id, Gfx::IntSize size, Gfx::IntRect damage_rect, double device_pixel_ratio){- auto [did_swap_bitmap, bitmap_id_to_release] = m_backing_stores.present(bitmap_id, size, device_pixel_ratio);+ auto [did_swap_bitmap, bitmap_id_to_release] = m_backing_stores->present(bitmap_id, size, device_pixel_ratio);dbgln_if(COMPOSITOR_DEBUG, "[Compositor] UI received presented bitmap {} for page {} size={}x{} did_swap={} front={}",- bitmap_id, page().id(), size.width(), size.height(), did_swap_bitmap, m_backing_stores.front_bitmap_id());+ bitmap_id, page().id(), size.width(), size.height(), did_swap_bitmap, m_backing_stores->front_bitmap_id());release_backing_store(bitmap_id_to_release);@@ -2430,7 +2430,7 @@ ViewImplementation::UIOverlayPosition ViewImplementation::ui_overlay_position(We{auto view_size = viewport_size().to_type<int>();auto view_position = position.to_type<int>();- auto frame = m_backing_stores.displayed_frame();+ auto frame = m_backing_stores->displayed_frame();if (!frame.has_value() || frame->size.is_empty())return { view_position, view_size, m_device_pixel_ratio };if (!m_scales_frames_to_fit || frame->size == view_size)@@ -3075,19 +3075,19 @@ void ViewImplementation::did_allocate_backing_stores(Badge<WebContentPage>, Vectvoid ViewImplementation::did_add_backing_stores(Badge<WebContentPage>, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){- m_backing_stores.add(move(bitmap_ids), move(backing_stores));+ m_backing_stores->add(move(bitmap_ids), move(backing_stores));}void ViewImplementation::did_retire_backing_stores(Badge<WebContentPage>, ReadonlySpan<i32> bitmap_ids){- m_backing_stores.retire(bitmap_ids);+ m_backing_stores->retire(bitmap_ids);}void ViewImplementation::install_backing_stores(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){dbgln_if(COMPOSITOR_DEBUG, "[Compositor] UI installing {} backing stores for page {} had_usable_bitmap={}",- backing_stores.size(), page().id(), m_backing_stores.has_usable_front_bitmap());- m_backing_stores.install(move(bitmap_ids), move(backing_stores));+ backing_stores.size(), page().id(), m_backing_stores->has_usable_front_bitmap());+ m_backing_stores->install(move(bitmap_ids), move(backing_stores));}void ViewImplementation::update_zoom()@@ -3611,7 +3611,7 @@ void ViewImplementation::display_page_changed(RefPtr<WebContentPage> previous_pareset_page_media_state();// Keep showing the outgoing page until the page paints its first frame.- m_backing_stores.keep_front_as_backup();+ m_backing_stores->keep_front_as_backup();if (previous_page->is_open()) {Application::the().update_compositor_context_visibility(previous_page->compositor_context_id(), Web::HTML::VisibilityState::Hidden);previous_page->async_set_has_devtools_client(false);@@ -3634,7 +3634,7 @@ void ViewImplementation::display_page_changed(RefPtr<WebContentPage> previous_pacomplete_webdriver_content_commands_after_process_replacement(pending_webdriver_commands);complete_webdriver_content_commands_after_process_replacement(pending_webdriver_crash_commands);}- m_backing_stores.clear();+ m_backing_stores->clear();if (auto backing_stores = page.take_presented_backing_stores(); backing_stores.has_value())install_backing_stores(move(backing_stores->bitmap_ids), move(backing_stores->backing_stores));@@ -4339,7 +4339,7 @@ void ViewImplementation::respawn_web_content_process_after_crash()queue_promise->resolve({});cancel_all_native_geolocation_requests();// Don't keep a stale backup bitmap around.- m_backing_stores.clear_backup();+ m_backing_stores->clear_backup();// The tab is displayed by a page of a new process, standing in for the document the crashed process destroyed// until a document activates in the tab: the traversal recovering the tab populates one in that page.@@ -4802,8 +4802,8 @@ NonnullRefPtr<Core::Promise<LexicalPath>> ViewImplementation::take_screenshot(Scswitch (type) {case ScreenshotType::Visible: {RefPtr<Gfx::Bitmap> visible_bitmap;- if (auto frame = m_backing_stores.displayed_frame(); frame.has_value())- visible_bitmap = frame->shared_image_buffer.bitmap_if_present();+ if (auto frame = m_backing_stores->displayed_frame(); frame.has_value() && frame->shared_image_buffer)+ visible_bitmap = frame->shared_image_buffer->bitmap_if_present();if (visible_bitmap)return write_screenshot(visible_bitmap.ptr());diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 68f0eafed02..8c621f6dea5 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -338,7 +338,7 @@ public:Optional<ByteString> const& page_tooltip_for_testing() const { return m_page_tooltip; }Optional<double> displayed_frame_device_pixel_ratio_for_testing() const{- return m_backing_stores.displayed_frame().map([](auto const& frame) { return frame.device_pixel_ratio; });+ return m_backing_stores->displayed_frame().map([](auto const& frame) { return frame.device_pixel_ratio; });}String force_dark_host() const;@@ -561,7 +561,7 @@ public:void did_change_background_color(Badge<WebContentPage>, Gfx::Color);Gfx::Color page_background_color() const { return m_page_background_color; }- PageBackingStores const& backing_stores() const { return m_backing_stores; }+ PageBackingStores const& backing_stores() const { return *m_backing_stores; }void did_allocate_backing_stores(Badge<WebContentPage>, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores);void did_add_backing_stores(Badge<WebContentPage>, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores);void did_retire_backing_stores(Badge<WebContentPage>, ReadonlySpan<i32> bitmap_ids);@@ -887,7 +887,7 @@ protected:void download_url(URL::URL const&, PromptForPath, Optional<String> const& image_mime_type = {});String m_window_handle;- PageBackingStores m_backing_stores;+ NonnullOwnPtr<PageBackingStores> m_backing_stores { make<PageBackingStores>() };bool m_pinch_gesture_was_canceled { false };bool m_presents_from_cpu_memory { false };diff --git a/Services/Hub/ConnectionFromUI.cpp b/Services/Hub/ConnectionFromUI.cppindex e75cd0c0b19..972040e4029 100644--- a/Services/Hub/ConnectionFromUI.cpp+++ b/Services/Hub/ConnectionFromUI.cpp@@ -88,6 +88,17 @@ void ConnectionFromUI::view_enqueue_pinch_event(u64 view_id, Web::PinchEvent eve}}+void ConnectionFromUI::release_backing_store(u64 page_id, i32 bitmap_id)+{+ // NB: A page can outlive its view, and a view can stop showing a page, so this goes by the page.+ Web::PageId id { page_id };+ auto client = WebView::WebContentClient::client_for_compositor_context_id(Web::compositor_context_id_for_page(id));+ if (!client.has_value())+ return;+ if (auto* page = client->page(id))+ page->release_presented_bitmap(bitmap_id);+}+View* ConnectionFromUI::find_view(u64 view_id){auto view = WebView::ViewImplementation::find_view_by_id(view_id);diff --git a/Services/Hub/ConnectionFromUI.h b/Services/Hub/ConnectionFromUI.hindex db8138ea1a4..67d3a4b5951 100644--- a/Services/Hub/ConnectionFromUI.h+++ b/Services/Hub/ConnectionFromUI.h@@ -33,6 +33,7 @@ private:static View* find_view(u64 view_id);// ^HubServerEndpoint+ virtual void release_backing_store(u64 page_id, i32 bitmap_id) override;virtual void view_enqueue_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) override;virtual void view_enqueue_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event) override;virtual void view_enqueue_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) override;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex c6a3f539e6a..8eaec792340 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -2,6 +2,8 @@#include <LibGfx/Color.h>#include <LibGfx/Cursor.h>#include <LibGfx/Point.h>+#include <LibGfx/Rect.h>+#include <LibGfx/SharedImage.h>#include <LibGfx/Size.h>#include <LibURL/URL.h>#include <LibWebCommon/HTML/FileFilter.h>@@ -92,6 +94,14 @@ struct Hub::ViewState {// What the Hub tells the UI.endpoint HubClient{+ view_install_backing_stores(u64 view_id, u64 page_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) =|+ view_add_backing_stores(u64 view_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) =|+ view_retire_backing_stores(u64 view_id, Vector<i32> bitmap_ids) =|+ view_present_backing_store(u64 view_id, u64 page_id, i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio) =|+ view_accept_presented_backing_store(u64 view_id, i32 bitmap_id, Gfx::IntRect damage_rect) =|+ 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) =|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.ipcindex 327cd642ed8..c21cd85916a 100644--- a/Services/Hub/HubServer.ipc+++ b/Services/Hub/HubServer.ipc@@ -11,6 +11,7 @@// What the UI asks of the Hub.endpoint HubServer{+ release_backing_store(u64 page_id, i32 bitmap_id) =|view_enqueue_key_event(u64 view_id, u64 input_id, Web::KeyEvent event) =|view_enqueue_mouse_event(u64 view_id, u64 input_id, Web::MouseEvent event) =|view_enqueue_drag_event(u64 view_id, u64 input_id, Web::DragEvent event) =|diff --git a/Services/Hub/View.cpp b/Services/Hub/View.cppindex afd8af767e3..4ec60355b75 100644--- a/Services/Hub/View.cpp+++ b/Services/Hub/View.cpp@@ -9,6 +9,74 @@namespace Hub {+// The backing stores of a view, as far as the view needs to know. The UI holds the images and shows them, and hears of+// each step so that it comes to the same result.+class BackingStoresInUI final : public WebView::PageBackingStores {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit BackingStoresInUI(View& view)+ : m_view(view)+ {+ }++private:+ virtual void install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override+ {+ 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));+ }++ virtual void add(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override+ {+ track_added_backing_stores(bitmap_ids);+ if (auto* ui = ConnectionFromUI::the())+ ui->async_view_add_backing_stores(m_view.view_id(), move(bitmap_ids), move(backing_stores));+ }++ virtual void retire(ReadonlySpan<i32> bitmap_ids) override+ {+ PageBackingStores::retire(bitmap_ids);+ if (auto* ui = ConnectionFromUI::the()) {+ Vector<i32> retired_bitmap_ids;+ retired_bitmap_ids.append(bitmap_ids.data(), bitmap_ids.size());+ ui->async_view_retire_backing_stores(m_view.view_id(), move(retired_bitmap_ids));+ }+ }++ virtual PresentationResult present(i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio) override+ {+ auto result = PageBackingStores::present(bitmap_id, size, device_pixel_ratio);+ if (auto* ui = ConnectionFromUI::the())+ ui->async_view_present_backing_store(m_view.view_id(), m_view.page().id().value(), bitmap_id, size, device_pixel_ratio);+ return result;+ }++ virtual void keep_front_as_backup() override+ {+ PageBackingStores::keep_front_as_backup();+ if (auto* ui = ConnectionFromUI::the())+ ui->async_view_keep_front_backing_store_as_backup(m_view.view_id());+ }++ virtual void clear_backup() override+ {+ PageBackingStores::clear_backup();+ if (auto* ui = ConnectionFromUI::the())+ ui->async_view_clear_backup_backing_store(m_view.view_id());+ }++ virtual void clear() override+ {+ PageBackingStores::clear();+ if (auto* ui = ConnectionFromUI::the())+ ui->async_view_clear_backing_stores(m_view.view_id());+ }++ View& m_view;+};+View::View(ViewHost& host, InitialState initial_state): ViewImplementation(initial_state.is_private), m_host(&host)@@ -16,6 +84,7 @@ View::View(ViewHost& host, InitialState initial_state)m_device_pixel_ratio = initial_state.device_pixel_ratio;m_maximum_frames_per_second = initial_state.maximum_frames_per_second;m_display_id = initial_state.display_id;+ m_backing_stores = make<BackingStoresInUI>(*this);forward_callbacks_to_ui();}@@ -440,6 +509,12 @@ void View::push_state_to_ui()ui->async_view_state_changed(view_id(), move(state));}+void View::did_accept_presented_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect)+{+ if (auto* ui = ConnectionFromUI::the())+ ui->async_view_accept_presented_backing_store(view_id(), bitmap_id, damage_rect);+}+void View::prepare_page_for_navigable(WebView::WebContentPage& page){ViewImplementation::prepare_page_for_navigable(page);@@ -475,18 +550,4 @@ void View::show_context_menu(WebView::Menu& menu, Gfx::IntPoint widget_position)m_host->show_context_menu(menu, widget_position);}-void View::did_accept_presented_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect)-{- if (m_host)- m_host->did_accept_presented_backing_store(bitmap_id, damage_rect);-}--void View::release_backing_store(i32 bitmap_id)-{- if (m_host)- m_host->release_backing_store(bitmap_id);- else- ViewImplementation::release_backing_store(bitmap_id);-}-}diff --git a/Services/Hub/View.h b/Services/Hub/View.hindex 0baa48c04e5..749d6e9ce17 100644--- a/Services/Hub/View.h+++ b/Services/Hub/View.h@@ -21,8 +21,6 @@ public:virtual Web::DevicePixelSize viewport_size() const = 0;virtual void update_zoom() = 0;virtual void show_context_menu(WebView::Menu&, Gfx::IntPoint widget_position) = 0;- virtual void did_accept_presented_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect) = 0;- virtual void release_backing_store(i32 bitmap_id) = 0;};// A tab's view. It holds the tab's pages and everything else about them, and leaves showing them to its ViewHost.@@ -51,7 +49,6 @@ public:void set_device_pixel_ratio(double device_pixel_ratio) { m_device_pixel_ratio = device_pixel_ratio; }bool scales_frames_to_fit() const { return m_scales_frames_to_fit; }- void release_backing_store_to_page(i32 bitmap_id) { ViewImplementation::release_backing_store(bitmap_id); }using ViewImplementation::handle_resize;using ViewImplementation::initialize_tab;@@ -71,7 +68,8 @@ private:virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const override { return content_position; }virtual void show_context_menu(WebView::Menu&, Gfx::IntPoint widget_position) override;virtual void did_accept_presented_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect) override;- virtual void release_backing_store(i32 bitmap_id) 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 { }void forward_callbacks_to_ui();diff --git a/UI/Qt/HubConnection.cpp b/UI/Qt/HubConnection.cppindex 0488b25b8ac..471db048253 100644--- a/UI/Qt/HubConnection.cpp+++ b/UI/Qt/HubConnection.cpp@@ -27,10 +27,67 @@ HubConnection& HubConnection::the()return *s_the;}+HubConnection* HubConnection::the_if_connected()+{+ return s_the;+}+void HubConnection::die(){}+void HubConnection::view_install_backing_stores(u64 view_id, u64 page_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_install_backing_stores(page_id, move(bitmap_ids), move(backing_stores));+ else if (!bitmap_ids.is_empty())+ RemoteView::release_backing_store_to_page(page_id, bitmap_ids[0]);+}++void HubConnection::view_add_backing_stores(u64 view_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_add_backing_stores(move(bitmap_ids), move(backing_stores));+}++void HubConnection::view_retire_backing_stores(u64 view_id, Vector<i32> bitmap_ids)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_retire_backing_stores(bitmap_ids);+}++void HubConnection::view_present_backing_store(u64 view_id, u64 page_id, i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_present_backing_store(page_id, bitmap_id, size, device_pixel_ratio);+ else+ RemoteView::release_backing_store_to_page(page_id, bitmap_id);+}++void HubConnection::view_accept_presented_backing_store(u64 view_id, i32 bitmap_id, Gfx::IntRect damage_rect)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_accept_backing_store(bitmap_id, damage_rect);+}++void HubConnection::view_keep_front_backing_store_as_backup(u64 view_id)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_keep_front_backing_store_as_backup();+}++void HubConnection::view_clear_backup_backing_store(u64 view_id)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_clear_backup_backing_store();+}++void HubConnection::view_clear_backing_stores(u64 view_id)+{+ if (auto* view = RemoteView::find(view_id))+ view->did_clear_backing_stores();+}+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.hindex e05389f6231..5d911939877 100644--- a/UI/Qt/HubConnection.h+++ b/UI/Qt/HubConnection.h@@ -23,11 +23,20 @@ public:virtual ~HubConnection() override;static HubConnection& the();+ static HubConnection* the_if_connected();private:virtual void die() override;// ^HubClientEndpoint+ virtual void view_install_backing_stores(u64 view_id, u64 page_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override;+ virtual void view_add_backing_stores(u64 view_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override;+ virtual void view_retire_backing_stores(u64 view_id, Vector<i32> bitmap_ids) override;+ virtual void view_present_backing_store(u64 view_id, u64 page_id, i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio) override;+ virtual void view_accept_presented_backing_store(u64 view_id, i32 bitmap_id, Gfx::IntRect damage_rect) override;+ 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_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.cppindex 4b9932d5151..3188819a653 100644--- a/UI/Qt/RemoteView.cpp+++ b/UI/Qt/RemoteView.cpp@@ -23,6 +23,33 @@ RemoteView::~RemoteView()m_view->detach_from_host();}+void RemoteView::did_install_backing_stores(u64 page_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+{+ m_backing_stores_page_id = page_id;+ m_backing_stores.install(move(bitmap_ids), move(backing_stores));+}++void RemoteView::did_present_backing_store(u64 page_id, i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio)+{+ // NB: The Compositor may paint into the backing store this frame replaced as soon as it gets it back, so that must+ // wait until nothing here reads from it anymore.+ auto result = m_backing_stores.present(bitmap_id, size, device_pixel_ratio);+ release_backing_store(page_id, result.bitmap_id_to_release);+}++void RemoteView::did_accept_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect)+{+ did_accept_presented_backing_store(bitmap_id, damage_rect);+ 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())+ hub->async_release_backing_store(page_id, bitmap_id);+}+void RemoteView::enqueue_input_event(Web::InputEvent event){auto input_id = m_next_input_id++;diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.hindex c17a50cd121..247e8ac480f 100644--- a/UI/Qt/RemoteView.h+++ b/UI/Qt/RemoteView.h@@ -140,7 +140,7 @@ public:VERIFY_NOT_REACHED();}Gfx::Color page_background_color() const { return m_state.page_background_color; }- WebView::PageBackingStores const& backing_stores() const { return m_view->backing_stores(); }+ WebView::PageBackingStores const& backing_stores() const { return m_backing_stores; }void take_and_save_screenshot(ScreenshotType screenshot_type) { HubConnection::the().async_view_take_and_save_screenshot(view_id(), move(screenshot_type)); }void save_screenshot(Gfx::Bitmap const& bitmap) { m_view->save_screenshot(bitmap); }void request_close() { HubConnection::the().async_view_request_close(view_id()); }@@ -169,10 +169,25 @@ public:bool scales_frames_to_fit() const { return m_view->scales_frames_to_fit(); }- // ^Hub::ViewHost- virtual void release_backing_store(i32 bitmap_id) override { m_view->release_backing_store_to_page(bitmap_id); }+ void did_install_backing_stores(u64 page_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores);+ void did_add_backing_stores(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) { m_backing_stores.add(move(bitmap_ids), move(backing_stores)); }+ void did_retire_backing_stores(ReadonlySpan<i32> bitmap_ids) { m_backing_stores.retire(bitmap_ids); }+ void did_present_backing_store(u64 page_id, i32 bitmap_id, Gfx::IntSize, double device_pixel_ratio);+ void did_accept_backing_store(i32 bitmap_id, Gfx::IntRect damage_rect);+ 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(); }++ // 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);++ // The page that presented the backing stores that the view holds.+ u64 backing_stores_page_id() const { return m_backing_stores_page_id; }protected:+ virtual void did_accept_presented_backing_store(i32, Gfx::IntRect) { }+ virtual void release_backing_store(u64 page_id, i32 bitmap_id) { release_backing_store_to_page(page_id, bitmap_id); }+void initialize_tab(Web::HTML::VisibilityState state, Optional<WebView::CanonicalTraversable&> traversable = {}){m_view->initialize_tab(state, traversable);@@ -187,6 +202,8 @@ protected:private:NonnullOwnPtr<Hub::View> m_view;Hub::ViewState m_state;+ WebView::PageBackingStores m_backing_stores;+ u64 m_backing_stores_page_id { 0 };// What only the UI can hold of the input events that the Hub has yet to finish with.HashMap<u64, OwnPtr<Web::BrowserInputData>> m_input_in_hub;@@ -195,7 +212,7 @@ private:public:decltype(Hub::View::on_load_start) on_load_start;decltype(Hub::View::on_load_finish) on_load_finish;- decltype(Hub::View::on_ready_to_paint)& on_ready_to_paint { m_view->on_ready_to_paint };+ decltype(Hub::View::on_ready_to_paint) on_ready_to_paint;decltype(Hub::View::on_performance_stats) on_performance_stats;decltype(Hub::View::on_new_web_view)& on_new_web_view { m_view->on_new_web_view };decltype(Hub::View::on_activate_tab) on_activate_tab;diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex 95db1d9db08..983d313a2e5 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -1070,10 +1070,10 @@ Optional<WebContentView::Paintable> WebContentView::current_paintable() constif (!frame.has_value())return {};return Paintable {- .shared_image_buffer = &frame->shared_image_buffer,+ .shared_image_buffer = frame->shared_image_buffer,.bitmap_size = frame->size,.bitmap_id = frame->bitmap_id.value_or(-1),- .page_id = frame->bitmap_id.has_value() ? page().id().value() : 0,+ .page_id = frame->bitmap_id.has_value() ? backing_stores_page_id() : 0,};}diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex 0b46280147f..845356bc7a9 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -223,6 +223,8 @@ private:virtual void update_zoom() override;virtual Web::DevicePixelSize viewport_size() const override;virtual void show_context_menu(WebView::Menu&, Gfx::IntPoint widget_position) override;++ // ^RemoteViewvirtual void did_accept_presented_backing_store(i32, Gfx::IntRect) override;#ifdef LADYBIRD_QT_USE_RHI_WIDGET@@ -429,7 +431,7 @@ private:bool vulkan_window_update_would_only_resize() const;void remap_unexposed_vulkan_window();- virtual void release_backing_store(i32 bitmap_id) override;+ virtual void release_backing_store(u64 page_id, i32 bitmap_id) override;# ifdef LADYBIRD_HAS_WAYLAND_GPU_QUERYbool create_page_surface();@@ -457,7 +459,7 @@ private:// The backing stores the view is done with that the compositor of the desktop may still read. The page they belong// to can stop displaying the tab before the compositor is done.struct BackingStoreWaitingForRelease {- NonnullRefPtr<WebView::WebContentPage> page;+ u64 page_id { 0 };i32 bitmap_id { -1 };};Vector<BackingStoreWaitingForRelease> m_backing_stores_waiting_for_release;diff --git a/UI/Qt/WebContentViewLinux.cpp b/UI/Qt/WebContentViewLinux.cppindex 052b5b640e5..1dd2c87056b 100644--- a/UI/Qt/WebContentViewLinux.cpp+++ b/UI/Qt/WebContentViewLinux.cpp@@ -18,7 +18,6 @@#include <LibGfx/VulkanContext.h>#include <LibGfx/VulkanImage.h>#include <LibWebView/Application.h>-#include <LibWebView/WebContentClient.h>#include <UI/Qt/NativeWindowContainer.h>#include <UI/Qt/WebContentView.h>#ifdef LADYBIRD_HAS_WAYLAND_GPU_QUERY@@ -1250,10 +1249,10 @@ void WebContentView::schedule_vulkan_window_update()update();}-void WebContentView::release_backing_store(i32 bitmap_id)+void WebContentView::release_backing_store(u64 page_id, i32 bitmap_id){#ifdef LADYBIRD_HAS_WAYLAND_GPU_QUERY- if (auto index = m_retired_backing_stores.find_first_index_if([&](auto const& store) { return store.page_id == page().id().value() && store.bitmap_id == bitmap_id; }); index.has_value()) {+ if (auto index = m_retired_backing_stores.find_first_index_if([&](auto const& store) { return store.page_id == page_id && store.bitmap_id == bitmap_id; }); index.has_value()) {auto& release = *m_retired_backing_stores[*index].release;auto handled = release.view_finished_reading();if (release.is_released())@@ -1263,14 +1262,13 @@ void WebContentView::release_backing_store(i32 bitmap_id)}// NB: The compositor of the desktop may read a frame until it releases its buffer, which it does once another frame// replaced it on screen. The Compositor must not paint into the backing store until then.- if (m_page_surface && m_page_surface->holds_backing_store(page().id().value(), bitmap_id)) {- auto& page = this->page();- if (!m_backing_stores_waiting_for_release.contains([&](auto const& store) { return store.page.ptr() == &page && store.bitmap_id == bitmap_id; }))- m_backing_stores_waiting_for_release.append({ page, bitmap_id });+ if (m_page_surface && m_page_surface->holds_backing_store(page_id, bitmap_id)) {+ if (!m_backing_stores_waiting_for_release.contains([&](auto const& store) { return store.page_id == page_id && store.bitmap_id == bitmap_id; }))+ m_backing_stores_waiting_for_release.append({ page_id, bitmap_id });return;}#endif- RemoteView::release_backing_store(bitmap_id);+ RemoteView::release_backing_store(page_id, bitmap_id);}#ifdef LADYBIRD_HAS_WAYLAND_GPU_QUERY@@ -1282,12 +1280,12 @@ bool WebContentView::create_page_surface()m_page_surface->on_backing_store_released = [this](u64 page_id, i32 bitmap_id) {auto index = m_backing_stores_waiting_for_release.find_first_index_if([&](auto const& store) {- return store.page->id().value() == page_id && store.bitmap_id == bitmap_id;+ return store.page_id == page_id && store.bitmap_id == bitmap_id;});if (!index.has_value())return;auto store = m_backing_stores_waiting_for_release.take(*index);- store.page->release_presented_bitmap(store.bitmap_id);+ release_backing_store_to_page(store.page_id, store.bitmap_id);};m_page_surface->on_ready_to_present = [this] {schedule_vulkan_window_update();@@ -1432,17 +1430,14 @@ void WebContentView::hide_image_over_page()void WebContentView::destroy_page_surface(){m_page_surface->retire_buffers([this](WaylandPageSurface::HeldBackingStore const& held) -> Function<void()> {- RefPtr<WebView::WebContentPage> owner;bool view_finished_reading = false;- if (auto index = m_backing_stores_waiting_for_release.find_first_index_if([&](auto const& store) { return store.page->id().value() == held.page_id && store.bitmap_id == held.bitmap_id; }); index.has_value()) {- owner = m_backing_stores_waiting_for_release.take(*index).page;+ if (auto index = m_backing_stores_waiting_for_release.find_first_index_if([&](auto const& store) { return store.page_id == held.page_id && store.bitmap_id == held.bitmap_id; }); index.has_value()) {+ m_backing_stores_waiting_for_release.remove(*index);view_finished_reading = true;- } else if (auto client = WebView::WebContentClient::client_for_compositor_context_id(Web::compositor_context_id_for_page(Web::PageId { held.page_id })); client.has_value()) {- owner = client->page(Web::PageId { held.page_id });}- if (!owner || held.bitmap_id < 0)+ if (held.bitmap_id < 0)return {};- auto release = make_ref_counted<BackingStoreRelease>([owner = owner.release_nonnull(), bitmap_id = held.bitmap_id] { owner->release_presented_bitmap(bitmap_id); });+ auto release = make_ref_counted<BackingStoreRelease>([page_id = held.page_id, bitmap_id = held.bitmap_id] { release_backing_store_to_page(page_id, bitmap_id); });if (view_finished_reading)release->view_finished_reading();else@@ -1453,7 +1448,7 @@ void WebContentView::destroy_page_surface()m_page_surface_rect = {};// NB: Unheld stores do not need to wait for a Wayland release. Held stores moved their acknowledgements above.for (auto& store : exchange(m_backing_stores_waiting_for_release, {}))- store.page->release_presented_bitmap(store.bitmap_id);+ release_backing_store_to_page(store.page_id, store.bitmap_id);release_retired_backing_stores_no_longer_painted();}