LibWeb+WebContent+WebWorker: Move backing store allocation in Navigable

Making navigables responsible for backing store allocation will allow us
to have separate backing stores for iframes and run paint updates for
them independently, which is a step toward isolating them into separate
processes.

Another nice side effect is that now Skia backend context is ready by
the time backing stores are allocated, so we will be able to get rid of
BackingStore class in the upcoming changes and allocate PaintingSurface
directly.
This commit is contained in:
Aliaksandr Kalenik 2025-06-26 22:33:58 +02:00 committed by Jelle Raaijmakers
commit 082053d781
Notes: github-actions[bot] 2025-07-04 14:14:12 +00:00
23 changed files with 265 additions and 262 deletions

View file

@ -468,16 +468,14 @@ void EventLoop::update_the_rendering()
// 22. For each doc of docs, update the rendering or user interface of doc and its node navigable to reflect the current state.
for (auto& document : docs) {
document->page().client().process_screenshot_requests();
auto navigable = document->navigable();
if (!navigable->is_traversable())
continue;
auto traversable = navigable->traversable_navigable();
if (traversable && traversable->needs_repaint()) {
auto& page = traversable->page();
VERIFY(page.client().is_ready_to_paint());
page.client().paint_next_frame();
}
traversable->process_screenshot_requests();
if (!navigable->needs_repaint())
continue;
navigable->paint_next_frame();
}
// 23. For each doc of docs, process top layer removals given doc.

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2022-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2023-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2025, Luke Wilde <luke@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -109,11 +109,50 @@ bool Navigable::is_ancestor_of(GC::Ref<Navigable> other) const
return false;
}
Navigable::Navigable(GC::Ref<Page> page)
static RefPtr<Gfx::SkiaBackendContext> g_cached_skia_backend_context;
static RefPtr<Gfx::SkiaBackendContext> get_skia_backend_context()
{
if (!g_cached_skia_backend_context) {
#ifdef AK_OS_MACOS
auto metal_context = Gfx::get_metal_context();
g_cached_skia_backend_context = Gfx::SkiaBackendContext::create_metal_context(*metal_context);
#elif USE_VULKAN
auto maybe_vulkan_context = Gfx::create_vulkan_context();
if (maybe_vulkan_context.is_error()) {
dbgln("Vulkan context creation failed: {}", maybe_vulkan_context.error());
return {};
}
auto vulkan_context = maybe_vulkan_context.release_value();
g_cached_skia_backend_context = Gfx::SkiaBackendContext::create_vulkan_context(vulkan_context);
#endif
}
return g_cached_skia_backend_context;
}
Navigable::Navigable(GC::Ref<Page> page, bool is_svg_page)
: m_page(page)
, m_event_handler({}, *this)
, m_is_svg_page(is_svg_page)
, m_backing_store_manager(heap().allocate<Painting::BackingStoreManager>(*this))
{
all_navigables().set(*this);
if (!m_is_svg_page) {
auto display_list_player_type = page->client().display_list_player_type();
OwnPtr<Painting::DisplayListPlayerSkia> skia_player;
if (display_list_player_type == DisplayListPlayerType::SkiaGPUIfAvailable) {
m_skia_backend_context = get_skia_backend_context();
skia_player = make<Painting::DisplayListPlayerSkia>(m_skia_backend_context);
} else {
skia_player = make<Painting::DisplayListPlayerSkia>();
}
m_rendering_thread.set_skia_player(move(skia_player));
m_rendering_thread.set_skia_backend_context(m_skia_backend_context);
m_rendering_thread.start(display_list_player_type);
}
}
Navigable::~Navigable() = default;
@ -133,6 +172,7 @@ void Navigable::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_active_session_history_entry);
visitor.visit(m_container);
visitor.visit(m_navigation_observers);
visitor.visit(m_backing_store_manager);
m_event_handler.visit_edges(visitor);
for (auto& navigation_params : m_pending_navigations) {
@ -2287,6 +2327,14 @@ void Navigable::set_viewport_size(CSSPixelSize size)
if (m_viewport_size == size)
return;
m_rendering_thread.clear_bitmap_to_surface_cache();
if (!m_is_svg_page) {
m_backing_store_manager->restart_resize_timer();
m_backing_store_manager->resize_backing_stores_if_needed(Web::Painting::BackingStoreManager::WindowResizingInProgress::Yes);
m_pending_set_browser_zoom_request = false;
}
m_viewport_size = size;
if (auto document = active_document()) {
// NOTE: Resizing the viewport changes the reference value for viewport-relative CSS lengths.
@ -2338,10 +2386,7 @@ bool Navigable::has_a_rendering_opportunity() const
// Rendering opportunities typically occur at regular intervals.
// FIXME: Return `false` here if we're an inactive browser tab.
auto browsing_context = const_cast<Navigable*>(this)->active_browsing_context();
if (!browsing_context)
return false;
return browsing_context->page().client().is_ready_to_paint();
return is_ready_to_paint();
}
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#inform-the-navigation-api-about-child-navigable-destruction
@ -2499,4 +2544,52 @@ void Navigable::set_has_session_history_entry_and_ready_for_navigation()
}
}
bool Navigable::is_ready_to_paint() const
{
return m_number_of_queued_rasterization_tasks <= 1;
}
void Navigable::ready_to_paint()
{
m_number_of_queued_rasterization_tasks--;
VERIFY(m_number_of_queued_rasterization_tasks >= 0 && m_number_of_queued_rasterization_tasks < 2);
}
void Navigable::paint_next_frame()
{
auto [backing_store_id, back_store] = m_backing_store_manager->acquire_store_for_next_frame();
if (!back_store)
return;
VERIFY(m_number_of_queued_rasterization_tasks <= 1);
m_number_of_queued_rasterization_tasks++;
auto viewport_rect = page().css_to_device_rect(this->viewport_rect());
PaintConfig paint_config { .paint_overlay = true, .should_show_line_box_borders = m_should_show_line_box_borders, .canvas_fill_rect = Gfx::IntRect { {}, viewport_rect.size().to_type<int>() } };
start_display_list_rendering(*back_store, paint_config, [this, viewport_rect, backing_store_id] {
if (!is_top_level_traversable())
return;
auto& traversable = *page().top_level_traversable();
traversable.page().client().page_did_paint(viewport_rect.to_type<int>(), backing_store_id);
});
}
void Navigable::start_display_list_rendering(Painting::BackingStore& target, PaintConfig paint_config, Function<void()>&& callback)
{
m_needs_repaint = false;
auto document = active_document();
if (!document) {
callback();
return;
}
document->paintable()->refresh_scroll_state();
auto display_list = document->record_display_list(paint_config);
if (!display_list) {
callback();
return;
}
auto scroll_state_snapshot = document->paintable()->scroll_state().snapshot();
m_rendering_thread.enqueue_rendering_task(*display_list, move(scroll_state_snapshot), target, move(callback));
}
}

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2023-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -17,12 +17,14 @@
#include <LibWeb/HTML/HistoryHandlingBehavior.h>
#include <LibWeb/HTML/NavigationParams.h>
#include <LibWeb/HTML/POSTResource.h>
#include <LibWeb/HTML/RenderingThread.h>
#include <LibWeb/HTML/SandboxingFlagSet.h>
#include <LibWeb/HTML/SourceSnapshotParams.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HTML/TokenizedFeatures.h>
#include <LibWeb/InvalidateDisplayList.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Painting/BackingStoreManager.h>
#include <LibWeb/PixelUnits.h>
#include <LibWeb/XHR/FormDataEntry.h>
@ -38,6 +40,14 @@ struct TargetSnapshotParams {
SandboxingFlagSet sandboxing_flags {};
};
struct PaintConfig {
bool paint_overlay { false };
bool should_show_line_box_borders { false };
Optional<Gfx::IntRect> canvas_fill_rect {};
bool operator==(PaintConfig const& other) const = default;
};
// https://html.spec.whatwg.org/multipage/document-sequences.html#navigable
class Navigable : public JS::Cell
, public Weakable<Navigable> {
@ -170,7 +180,7 @@ public:
CSSPixelPoint viewport_scroll_offset() const { return m_viewport_scroll_offset; }
CSSPixelRect viewport_rect() const { return { m_viewport_scroll_offset, m_viewport_size }; }
CSSPixelSize viewport_size() const { return m_viewport_size; }
virtual void set_viewport_size(CSSPixelSize);
void set_viewport_size(CSSPixelSize);
void perform_scroll_of_viewport(CSSPixelPoint position);
// https://html.spec.whatwg.org/multipage/webappapis.html#rendering-opportunity
@ -195,11 +205,26 @@ public:
bool has_pending_navigations() const { return !m_pending_navigations.is_empty(); }
bool is_ready_to_paint() const;
void ready_to_paint();
void paint_next_frame();
void start_display_list_rendering(Painting::BackingStore&, PaintConfig, Function<void()>&& callback);
bool needs_repaint() const { return m_needs_repaint; }
void set_needs_repaint() { m_needs_repaint = true; }
RefPtr<Gfx::SkiaBackendContext> skia_backend_context() const { return m_skia_backend_context; }
void set_pending_set_browser_zoom_request(bool value) { m_pending_set_browser_zoom_request = value; }
bool pending_set_browser_zoom_request() const { return m_pending_set_browser_zoom_request; }
void set_should_show_line_box_borders(bool value) { m_should_show_line_box_borders = value; }
template<typename T>
bool fast_is() const = delete;
protected:
explicit Navigable(GC::Ref<Page>);
explicit Navigable(GC::Ref<Page>, bool is_svg_page);
virtual void visit_edges(Cell::Visitor&) override;
virtual void finalize() override;
@ -253,6 +278,15 @@ private:
bool m_has_session_history_entry_and_ready_for_navigation { false };
Vector<NavigateParams> m_pending_navigations;
bool m_is_svg_page { false };
bool m_needs_repaint { true };
bool m_pending_set_browser_zoom_request { false };
bool m_should_show_line_box_borders { false };
i32 m_number_of_queued_rasterization_tasks { 0 };
GC::Ref<Painting::BackingStoreManager> m_backing_store_manager;
RefPtr<Gfx::SkiaBackendContext> m_skia_backend_context;
RenderingThread m_rendering_thread;
};
HashTable<GC::RawRef<Navigable>>& all_navigables();

View file

@ -95,7 +95,7 @@ WebIDL::ExceptionOr<void> NavigableContainer::create_new_child_navigable(GC::Ptr
document_state->set_about_base_url(document->about_base_url());
// 7. Let navigable be a new navigable.
GC::Ref<Navigable> navigable = *heap().allocate<Navigable>(page);
GC::Ref<Navigable> navigable = *heap().allocate<Navigable>(page, false);
// 8. Initialize the navigable navigable given documentState and parentNavigable.
TRY_OR_THROW_OOM(vm(), navigable->initialize_navigable(document_state, parent_navigable));

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
* Copyright (c) 2023-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -18,6 +19,7 @@
#include <LibWeb/HTML/SessionHistoryEntry.h>
#include <LibWeb/HTML/TraversableNavigable.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Painting/BackingStore.h>
#include <LibWeb/Painting/ViewportPaintable.h>
@ -27,47 +29,11 @@ namespace Web::HTML {
GC_DEFINE_ALLOCATOR(TraversableNavigable);
static RefPtr<Gfx::SkiaBackendContext> g_cached_skia_backend_context;
static RefPtr<Gfx::SkiaBackendContext> get_skia_backend_context()
{
if (!g_cached_skia_backend_context) {
#ifdef AK_OS_MACOS
auto metal_context = Gfx::get_metal_context();
g_cached_skia_backend_context = Gfx::SkiaBackendContext::create_metal_context(*metal_context);
#elif USE_VULKAN
auto maybe_vulkan_context = Gfx::create_vulkan_context();
if (maybe_vulkan_context.is_error()) {
dbgln("Vulkan context creation failed: {}", maybe_vulkan_context.error());
return {};
}
auto vulkan_context = maybe_vulkan_context.release_value();
g_cached_skia_backend_context = Gfx::SkiaBackendContext::create_vulkan_context(vulkan_context);
#endif
}
return g_cached_skia_backend_context;
}
TraversableNavigable::TraversableNavigable(GC::Ref<Page> page)
: Navigable(page)
: Navigable(page, page->client().is_svg_page_client())
, m_storage_shed(StorageAPI::StorageShed::create(page->heap()))
, m_session_history_traversal_queue(vm().heap().allocate<SessionHistoryTraversalQueue>())
{
if (!page->client().is_svg_page_client()) {
auto display_list_player_type = page->client().display_list_player_type();
OwnPtr<Painting::DisplayListPlayerSkia> skia_player;
if (display_list_player_type == DisplayListPlayerType::SkiaGPUIfAvailable) {
m_skia_backend_context = get_skia_backend_context();
skia_player = make<Painting::DisplayListPlayerSkia>(m_skia_backend_context);
} else {
skia_player = make<Painting::DisplayListPlayerSkia>();
}
m_rendering_thread.set_skia_player(move(skia_player));
m_rendering_thread.set_skia_backend_context(m_skia_backend_context);
m_rendering_thread.start(display_list_player_type);
}
}
TraversableNavigable::~TraversableNavigable() = default;
@ -1420,40 +1386,6 @@ GC::Ptr<DOM::Node> TraversableNavigable::currently_focused_area()
return candidate;
}
void TraversableNavigable::set_viewport_size(CSSPixelSize size)
{
Navigable::set_viewport_size(size);
// Invalidate the surface cache if the traversable changed size.
m_rendering_thread.clear_bitmap_to_surface_cache();
}
RefPtr<Painting::DisplayList> TraversableNavigable::record_display_list(DevicePixelRect const& content_rect, PaintOptions paint_options)
{
m_needs_repaint = false;
auto document = active_document();
if (!document)
return {};
for (auto& navigable : all_navigables()) {
if (auto active_document = navigable->active_document(); active_document && active_document->paintable())
active_document->paintable()->refresh_scroll_state();
}
DOM::Document::PaintConfig paint_config;
paint_config.paint_overlay = paint_options.paint_overlay == PaintOptions::PaintOverlay::Yes;
paint_config.should_show_line_box_borders = paint_options.should_show_line_box_borders;
paint_config.canvas_fill_rect = Gfx::IntRect { {}, content_rect.size() };
return document->record_display_list(paint_config);
}
void TraversableNavigable::start_display_list_rendering(NonnullRefPtr<Painting::DisplayList> display_list, NonnullRefPtr<Painting::BackingStore> backing_store, Function<void()>&& callback)
{
auto scroll_state_snapshot = active_document()->paintable()->scroll_state().snapshot();
m_rendering_thread.enqueue_rendering_task(move(display_list), move(scroll_state_snapshot), move(backing_store), move(callback));
}
// https://w3c.github.io/geolocation/#dfn-emulated-position-data
Geolocation::EmulatedPositionData const& TraversableNavigable::emulated_position_data() const
{
@ -1468,4 +1400,35 @@ void TraversableNavigable::set_emulated_position_data(Geolocation::EmulatedPosit
m_emulated_position_data = data;
}
void TraversableNavigable::process_screenshot_requests()
{
auto& client = page().client();
while (!m_screenshot_tasks.is_empty()) {
auto task = m_screenshot_tasks.dequeue();
if (task.node_id.has_value()) {
auto* dom_node = DOM::Node::from_unique_id(*task.node_id);
if (!dom_node || !dom_node->paintable_box()) {
client.page_did_take_screenshot({});
continue;
}
auto rect = page().enclosing_device_rect(dom_node->paintable_box()->absolute_border_box_rect());
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
auto backing_store = Painting::BitmapBackingStore::create(*bitmap);
PaintConfig paint_config { .canvas_fill_rect = rect.to_type<int>() };
start_display_list_rendering(backing_store, paint_config, [backing_store, &client] {
client.page_did_take_screenshot(backing_store->bitmap().to_shareable_bitmap());
});
} else {
auto scrollable_overflow_rect = active_document()->layout_node()->paintable_box()->scrollable_overflow_rect();
auto rect = page().enclosing_device_rect(scrollable_overflow_rect.value());
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
auto backing_store = Painting::BitmapBackingStore::create(*bitmap);
PaintConfig paint_config { .paint_overlay = true, .canvas_fill_rect = rect.to_type<int>() };
start_display_list_rendering(backing_store, paint_config, [backing_store, &client] {
client.page_did_take_screenshot(backing_store->bitmap().to_shareable_bitmap());
});
}
}
}
}

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
* Copyright (c) 2023-2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -102,9 +103,6 @@ public:
[[nodiscard]] GC::Ptr<DOM::Node> currently_focused_area();
RefPtr<Painting::DisplayList> record_display_list(DevicePixelRect const&, PaintOptions);
void start_display_list_rendering(NonnullRefPtr<Painting::DisplayList>, NonnullRefPtr<Painting::BackingStore>, Function<void()>&& callback);
enum class CheckIfUnloadingIsCanceledResult {
CanceledByBeforeUnload,
CanceledByNavigate,
@ -112,20 +110,20 @@ public:
};
CheckIfUnloadingIsCanceledResult check_if_unloading_is_canceled(Vector<GC::Root<Navigable>> navigables_that_need_before_unload);
RefPtr<Gfx::SkiaBackendContext> skia_backend_context() const { return m_skia_backend_context; }
StorageAPI::StorageShed& storage_shed() { return m_storage_shed; }
StorageAPI::StorageShed const& storage_shed() const { return m_storage_shed; }
void set_viewport_size(CSSPixelSize) override;
bool needs_repaint() const { return m_needs_repaint; }
void set_needs_repaint() { m_needs_repaint = true; }
// https://w3c.github.io/geolocation/#dfn-emulated-position-data
Geolocation::EmulatedPositionData const& emulated_position_data() const;
void set_emulated_position_data(Geolocation::EmulatedPositionData data);
void process_screenshot_requests();
void queue_screenshot_task(Optional<UniqueNodeID> node_id)
{
m_screenshot_tasks.enqueue({ node_id });
set_needs_repaint();
}
private:
TraversableNavigable(GC::Ref<Page>);
@ -149,8 +147,6 @@ private:
[[nodiscard]] bool can_go_forward() const;
RenderingThread m_rendering_thread;
// https://html.spec.whatwg.org/multipage/document-sequences.html#tn-current-session-history-step
int m_current_session_history_step { 0 };
@ -176,12 +172,13 @@ private:
String m_window_handle;
RefPtr<Gfx::SkiaBackendContext> m_skia_backend_context;
bool m_needs_repaint { true };
// https://w3c.github.io/geolocation/#dfn-emulated-position-data
Geolocation::EmulatedPositionData m_emulated_position_data;
struct ScreenshotTask {
Optional<Web::UniqueNodeID> node_id;
};
Queue<ScreenshotTask> m_screenshot_tasks;
};
struct BrowsingContextAndDocument {