Truly independent web browser
LibWebView+UI/Qt: Move a view's backing stores into a class of their own
Andreas Klinge154ab48d64e
diff --git a/Libraries/LibWebView/CMakeLists.txt b/Libraries/LibWebView/CMakeLists.txtindex ff5c0bc1752..33f69f0a4a8 100644--- a/Libraries/LibWebView/CMakeLists.txt+++ b/Libraries/LibWebView/CMakeLists.txt@@ -53,6 +53,7 @@ set(SOURCESNotificationAttribution.cppNotificationFreedesktopArguments.cppNotificationManager.cpp+ PageBackingStores.cppPageTranslation.cppPictureInPictureManager.cppPictureInPictureWindow.cppdiff --git a/Libraries/LibWebView/PageBackingStores.cpp b/Libraries/LibWebView/PageBackingStores.cppnew file mode 100644index 00000000000..93e93b9d4f1--- /dev/null+++ b/Libraries/LibWebView/PageBackingStores.cpp@@ -0,0 +1,107 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibWebView/PageBackingStores.h>++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, {} };+ return {};+}++Vector<Gfx::SharedImageBuffer const*> PageBackingStores::shared_image_buffers() const+{+ Vector<Gfx::SharedImageBuffer const*> shared_image_buffers;+ if (m_front_bitmap.shared_image_buffer)+ shared_image_buffers.append(m_front_bitmap.shared_image_buffer.ptr());+ for (auto const& bitmap : m_other_bitmaps) {+ if (bitmap.shared_image_buffer)+ shared_image_buffers.append(bitmap.shared_image_buffer.ptr());+ }+ if (m_backup.shared_image_buffer)+ shared_image_buffers.append(m_backup.shared_image_buffer.ptr());+ return shared_image_buffers;+}++void PageBackingStores::install(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores)+{+ 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();+ 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_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]))),+ });+ }+}++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]))),+ });+ }+}++void PageBackingStores::retire(ReadonlySpan<i32> bitmap_ids)+{+ m_other_bitmaps.remove_all_matching([&](auto const& bitmap) {+ return bitmap_ids.contains_slow(bitmap.id);+ });+}++PageBackingStores::PresentationResult PageBackingStores::present(i32 bitmap_id, Gfx::IntSize size, double device_pixel_ratio)+{+ auto bitmap_index = m_other_bitmaps.find_first_index_if([bitmap_id](auto const& bitmap) { return bitmap_id == bitmap.id; });+ if (!bitmap_index.has_value())+ return { .did_swap = false, .bitmap_id_to_release = bitmap_id };++ auto previous_front_bitmap_id = m_front_bitmap.id;+ m_has_usable_front_bitmap = true;+ 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();+ return { .did_swap = true, .bitmap_id_to_release = previous_front_bitmap_id };+}++void PageBackingStores::keep_front_as_backup()+{+ if (!m_has_usable_front_bitmap)+ return;+ 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;+}++void PageBackingStores::clear()+{+ m_front_bitmap = {};+ m_other_bitmaps.clear();+ m_has_usable_front_bitmap = false;+}++}diff --git a/Libraries/LibWebView/PageBackingStores.h b/Libraries/LibWebView/PageBackingStores.hnew file mode 100644index 00000000000..e5007ab3fa2--- /dev/null+++ b/Libraries/LibWebView/PageBackingStores.h@@ -0,0 +1,74 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Optional.h>+#include <AK/OwnPtr.h>+#include <AK/Span.h>+#include <AK/Vector.h>+#include <LibGfx/SharedImage.h>+#include <LibGfx/SharedImageBuffer.h>+#include <LibGfx/Size.h>+#include <LibWebCommon/PixelUnits.h>+#include <LibWebView/Forward.h>++namespace WebView {++// The backing stores that the Compositor presents a page's frames in, as seen by whoever shows those frames. One of them+// is the front backing store, which holds the frame on display. The Compositor paints into the others.+class WEBVIEW_API PageBackingStores {+public:+ struct Frame {+ Gfx::SharedImageBuffer const& shared_image_buffer;+ Gfx::IntSize size;+ double device_pixel_ratio { 1.0 };+ // Empty for a frame kept from backing stores that are gone.+ Optional<i32> bitmap_id;+ };++ // The frame to show: the one in the front backing store, or until that has one, the frame kept as a backup.+ Optional<Frame> displayed_frame() const;++ bool has_usable_front_bitmap() const { return m_has_usable_front_bitmap; }+ i32 front_bitmap_id() const { return m_front_bitmap.id; }+ 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);++ struct PresentationResult {+ bool did_swap { false };+ // The backing store the Compositor may paint into again.+ 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);++ // 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 = {}; }++ // Forgets every backing store, other than the frame kept as a backup.+ void clear();++private:+ struct SharedBitmap {+ i32 id { -1 };+ Web::DevicePixelSize last_painted_size;+ double last_painted_device_pixel_ratio { 1.0 };+ OwnPtr<Gfx::SharedImageBuffer> shared_image_buffer;+ };++ SharedBitmap m_front_bitmap;+ Vector<SharedBitmap> m_other_bitmaps;+ bool m_has_usable_front_bitmap { false };+ SharedBitmap m_backup;+};++}diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex b3b50aefa67..5771d1e0c4e 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -450,22 +450,12 @@ 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){- bool did_swap_bitmap = false;- auto previous_front_bitmap_id = m_front_bitmap.id;- auto bitmap_index = m_other_bitmaps.find_first_index_if([bitmap_id](auto const& bitmap) { return bitmap_id == bitmap.id; });- if (bitmap_index.has_value()) {- m_has_usable_bitmap = true;- 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);- m_backup_shared_image_buffer = nullptr;- did_swap_bitmap = true;- }+ 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_front_bitmap.id);+ bitmap_id, page().id(), size.width(), size.height(), did_swap_bitmap, m_backing_stores.front_bitmap_id());- release_backing_store(did_swap_bitmap ? previous_front_bitmap_id : bitmap_id);+ release_backing_store(bitmap_id_to_release);if (did_swap_bitmap)did_accept_presented_backing_store(bitmap_id, damage_rect);@@ -2434,22 +2424,13 @@ void ViewImplementation::did_change_picture_in_picture_player_controls_visibilitupdate_ui_overlays();}-Optional<ViewImplementation::DisplayedFrame> ViewImplementation::displayed_frame() const-{- if (m_has_usable_bitmap)- return DisplayedFrame { m_front_bitmap.last_painted_size.to_type<int>(), m_front_bitmap.last_painted_device_pixel_ratio };- if (m_backup_shared_image_buffer)- return DisplayedFrame { m_backup_bitmap_size.to_type<int>(), m_backup_bitmap_device_pixel_ratio };- return {};-}-// The compositor draws the browser's overlays over the frame it presents, at the pixel ratio it painted the frame at,// and the frame may be scaled to fit the view.ViewImplementation::UIOverlayPosition ViewImplementation::ui_overlay_position(Web::DevicePixelPoint position) const{auto view_size = viewport_size().to_type<int>();auto view_position = position.to_type<int>();- auto frame = 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)@@ -3094,47 +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){- 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]))),- });- }+ m_backing_stores.add(move(bitmap_ids), move(backing_stores));}void ViewImplementation::did_retire_backing_stores(Badge<WebContentPage>, ReadonlySpan<i32> bitmap_ids){- m_other_bitmaps.remove_all_matching([&](auto const& bitmap) {- return bitmap_ids.contains_slow(bitmap.id);- });+ m_backing_stores.retire(bitmap_ids);}void ViewImplementation::install_backing_stores(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores){- VERIFY(bitmap_ids.size() == backing_stores.size());- VERIFY(!bitmap_ids.is_empty());dbgln_if(COMPOSITOR_DEBUG, "[Compositor] UI installing {} backing stores for page {} had_usable_bitmap={}",- backing_stores.size(), page().id(), m_has_usable_bitmap);- if (m_has_usable_bitmap) {- // NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.- m_backup_shared_image_buffer = move(m_front_bitmap.shared_image_buffer);- m_backup_bitmap_size = m_front_bitmap.last_painted_size;- m_backup_bitmap_device_pixel_ratio = m_front_bitmap.last_painted_device_pixel_ratio;- }- m_has_usable_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_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]))),- });- }+ 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()@@ -3658,11 +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.- if (m_has_usable_bitmap) {- m_backup_shared_image_buffer = move(m_front_bitmap.shared_image_buffer);- m_backup_bitmap_size = m_front_bitmap.last_painted_size;- m_backup_bitmap_device_pixel_ratio = m_front_bitmap.last_painted_device_pixel_ratio;- }+ 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);@@ -3685,9 +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_front_bitmap = {};- m_other_bitmaps.clear();- m_has_usable_bitmap = false;+ 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));@@ -4392,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_backup_shared_image_buffer = nullptr;+ 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.@@ -4855,12 +4802,8 @@ NonnullRefPtr<Core::Promise<LexicalPath>> ViewImplementation::take_screenshot(Scswitch (type) {case ScreenshotType::Visible: {RefPtr<Gfx::Bitmap> visible_bitmap;- if (m_has_usable_bitmap) {- VERIFY(m_front_bitmap.shared_image_buffer);- visible_bitmap = m_front_bitmap.shared_image_buffer->bitmap_if_present();- } else if (m_backup_shared_image_buffer) {- visible_bitmap = m_backup_shared_image_buffer->bitmap_if_present();- }+ if (auto frame = m_backing_stores.displayed_frame(); frame.has_value())+ 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 37fd7622864..68f0eafed02 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -77,6 +77,7 @@#include <LibWebView/Forward.h>#include <LibWebView/HistoryVisitTransition.h>#include <LibWebView/MicrophoneAuthorizer.h>+#include <LibWebView/PageBackingStores.h>#include <LibWebView/PermissionManager.h>#include <LibWebView/SessionHistory.h>#include <LibWebView/SessionStore.h>@@ -337,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 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;@@ -560,6 +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; }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);@@ -804,11 +806,6 @@ protected:bool handle_picture_in_picture_controls_mouse_event(Web::MouseEvent const&);void set_picture_in_picture_controls_shown(bool);bool picture_in_picture_controls_are_shown() const { return m_picture_in_picture_controls_shown || m_picture_in_picture_player_controls_shown; }- struct DisplayedFrame {- Gfx::IntSize size;- double device_pixel_ratio { 1.0 };- };- Optional<DisplayedFrame> displayed_frame() const;struct UIOverlayPosition {Gfx::IntPoint position;Gfx::IntSize viewport_size;@@ -889,17 +886,8 @@ protected:};void download_url(URL::URL const&, PromptForPath, Optional<String> const& image_mime_type = {});- struct SharedBitmap {- i32 id { -1 };- Web::DevicePixelSize last_painted_size;- double last_painted_device_pixel_ratio { 1.0 };- OwnPtr<Gfx::SharedImageBuffer> shared_image_buffer;- };-String m_window_handle;- SharedBitmap m_front_bitmap;- Vector<SharedBitmap> m_other_bitmaps;- bool m_has_usable_bitmap { false };+ PageBackingStores m_backing_stores;bool m_pinch_gesture_was_canceled { false };bool m_presents_from_cpu_memory { false };@@ -1034,9 +1022,6 @@ protected:RefPtr<Core::Timer> m_backing_store_shrink_timer;- OwnPtr<Gfx::SharedImageBuffer> m_backup_shared_image_buffer;- Web::DevicePixelSize m_backup_bitmap_size;- double m_backup_bitmap_device_pixel_ratio { 1.0 };Gfx::Color m_page_background_color { 255, 255, 255 };Gfx::Color m_system_canvas_background_color { 255, 255, 255 };Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto };diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex ac4d246751b..9fd63da0276 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -1065,25 +1065,15 @@ void WebContentView::update_page_focus()Optional<WebContentView::Paintable> WebContentView::current_paintable() const{- Gfx::SharedImageBuffer const* shared_image_buffer = nullptr;- Gfx::IntSize bitmap_size;- i32 bitmap_id = -1;- u64 page_id = 0;-- if (m_has_usable_bitmap) {- VERIFY(m_front_bitmap.shared_image_buffer);- shared_image_buffer = m_front_bitmap.shared_image_buffer.ptr();- bitmap_size = m_front_bitmap.last_painted_size.to_type<int>();- bitmap_id = m_front_bitmap.id;- page_id = page().id().value();- } else if (m_backup_shared_image_buffer) {- shared_image_buffer = m_backup_shared_image_buffer.ptr();- bitmap_size = m_backup_bitmap_size.to_type<int>();- }-- if (!shared_image_buffer)+ auto frame = backing_stores().displayed_frame();+ if (!frame.has_value())return {};- return Paintable { shared_image_buffer, bitmap_size, bitmap_id, page_id };+ return Paintable {+ .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,+ };}void WebContentView::schedule_repaint()diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex 5643c68e13a..5fa264b456d 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -448,7 +448,6 @@ private:void destroy_page_surface();void release_retired_backing_stores_no_longer_painted();void release_all_retired_backing_stores();- Vector<Gfx::SharedImageBuffer const*> backing_stores() const;QRect rect_shown_in_window() const;// Shows the page's frames on Wayland by handing their dma-bufs to the compositor of the desktop. Without it, thediff --git a/UI/Qt/WebContentViewLinux.cpp b/UI/Qt/WebContentViewLinux.cppindex 6904fb34d87..ea213a55810 100644--- a/UI/Qt/WebContentViewLinux.cpp+++ b/UI/Qt/WebContentViewLinux.cpp@@ -1375,7 +1375,7 @@ void WebContentView::update_page_surface(PageSurfaceContent content)if (m_page_surface->is_showing() && m_page_surface->would_change_placement(frame))m_page_surface->set_synchronized(true);- m_page_surface->forget_buffers_except(backing_stores());+ m_page_surface->forget_buffers_except(backing_stores().shared_image_buffers());auto result = m_page_surface->present(*parent, frame);if (result == WaylandPageSurface::PresentResult::Failed) {m_page_surface_failed = true;@@ -1411,20 +1411,6 @@ QRect WebContentView::rect_shown_in_window() constreturn shown;}-Vector<Gfx::SharedImageBuffer const*> WebContentView::backing_stores() const-{- Vector<Gfx::SharedImageBuffer const*> backing_stores;- if (m_front_bitmap.shared_image_buffer)- backing_stores.append(m_front_bitmap.shared_image_buffer.ptr());- for (auto const& bitmap : m_other_bitmaps) {- if (bitmap.shared_image_buffer)- backing_stores.append(bitmap.shared_image_buffer.ptr());- }- if (m_backup_shared_image_buffer)- backing_stores.append(m_backup_shared_image_buffer.ptr());- return backing_stores;-}-bool WebContentView::show_image_over_page(QImage const& image, QPoint position){auto* parent = window()->windowHandle();