mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-08 18:46:03 +00:00
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:
parent
b73525ba0e
commit
082053d781
Notes:
github-actions[bot]
2025-07-04 14:14:12 +00:00
Author: https://github.com/kalenikaliaksandr
Commit: 082053d781
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5259
Reviewed-by: https://github.com/gmta ✅
23 changed files with 265 additions and 262 deletions
|
@ -684,6 +684,7 @@ set(SOURCES
|
|||
Painting/AudioPaintable.cpp
|
||||
Painting/BackgroundPainting.cpp
|
||||
Painting/BackingStore.cpp
|
||||
Painting/BackingStoreManager.cpp
|
||||
Painting/BorderPainting.cpp
|
||||
Painting/BorderRadiiData.cpp
|
||||
Painting/BorderRadiusCornerClipper.cpp
|
||||
|
|
|
@ -6444,7 +6444,7 @@ void Document::invalidate_display_list()
|
|||
}
|
||||
}
|
||||
|
||||
RefPtr<Painting::DisplayList> Document::record_display_list(PaintConfig config)
|
||||
RefPtr<Painting::DisplayList> Document::record_display_list(HTML::PaintConfig config)
|
||||
{
|
||||
if (m_cached_display_list && m_cached_display_list_paint_config == config) {
|
||||
return m_cached_display_list;
|
||||
|
|
|
@ -832,14 +832,7 @@ public:
|
|||
void set_needs_display(InvalidateDisplayList = InvalidateDisplayList::Yes);
|
||||
void set_needs_display(CSSPixelRect const&, InvalidateDisplayList = InvalidateDisplayList::Yes);
|
||||
|
||||
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;
|
||||
};
|
||||
RefPtr<Painting::DisplayList> record_display_list(PaintConfig);
|
||||
RefPtr<Painting::DisplayList> record_display_list(HTML::PaintConfig);
|
||||
|
||||
void invalidate_display_list();
|
||||
|
||||
|
@ -1235,7 +1228,7 @@ private:
|
|||
|
||||
bool m_enable_cookies_on_file_domains { false };
|
||||
|
||||
Optional<PaintConfig> m_cached_display_list_paint_config;
|
||||
Optional<HTML::PaintConfig> m_cached_display_list_paint_config;
|
||||
RefPtr<Painting::DisplayList> m_cached_display_list;
|
||||
|
||||
mutable OwnPtr<Unicode::Segmenter> m_grapheme_segmenter;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -301,16 +301,6 @@ private:
|
|||
bool m_listen_for_dom_mutations { false };
|
||||
};
|
||||
|
||||
struct PaintOptions {
|
||||
enum class PaintOverlay {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
PaintOverlay paint_overlay { PaintOverlay::Yes };
|
||||
bool should_show_line_box_borders { false };
|
||||
};
|
||||
|
||||
enum class DisplayListPlayerType {
|
||||
SkiaGPUIfAvailable,
|
||||
SkiaCPU,
|
||||
|
@ -320,6 +310,7 @@ class PageClient : public JS::Cell {
|
|||
GC_CELL(PageClient, JS::Cell);
|
||||
|
||||
public:
|
||||
virtual u64 id() const = 0;
|
||||
virtual Page& page() = 0;
|
||||
virtual Page const& page() const = 0;
|
||||
virtual bool is_connection_open() const = 0;
|
||||
|
@ -331,9 +322,6 @@ public:
|
|||
virtual CSS::PreferredColorScheme preferred_color_scheme() const = 0;
|
||||
virtual CSS::PreferredContrast preferred_contrast() const = 0;
|
||||
virtual CSS::PreferredMotion preferred_motion() const = 0;
|
||||
virtual void paint_next_frame() = 0;
|
||||
virtual void process_screenshot_requests() = 0;
|
||||
virtual void start_display_list_rendering(DevicePixelRect const&, Painting::BackingStore&, PaintOptions, Function<void()>&& callback) = 0;
|
||||
virtual Queue<QueuedInputEvent>& input_event_queue() = 0;
|
||||
virtual void report_finished_handling_input_event(u64 page_id, EventResult event_was_handled) = 0;
|
||||
virtual void page_did_change_title(ByteString const&) { }
|
||||
|
@ -415,9 +403,10 @@ public:
|
|||
|
||||
virtual void page_did_mutate_dom([[maybe_unused]] FlyString const& type, [[maybe_unused]] DOM::Node const& target, [[maybe_unused]] DOM::NodeList& added_nodes, [[maybe_unused]] DOM::NodeList& removed_nodes, [[maybe_unused]] GC::Ptr<DOM::Node> previous_sibling, [[maybe_unused]] GC::Ptr<DOM::Node> next_sibling, [[maybe_unused]] Optional<String> const& attribute_name) { }
|
||||
|
||||
virtual void received_message_from_web_ui([[maybe_unused]] String const& name, [[maybe_unused]] JS::Value data) { }
|
||||
virtual void page_did_paint([[maybe_unused]] Gfx::IntRect const& content_rect, [[maybe_unused]] i32 bitmap_id) { }
|
||||
virtual void page_did_take_screenshot(Gfx::ShareableBitmap const&) { }
|
||||
|
||||
virtual bool is_ready_to_paint() const = 0;
|
||||
virtual void received_message_from_web_ui([[maybe_unused]] String const& name, [[maybe_unused]] JS::Value data) { }
|
||||
|
||||
virtual DisplayListPlayerType display_list_player_type() const = 0;
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <AK/AtomicRefCounted.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/Size.h>
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
|
|
146
Libraries/LibWeb/Painting/BackingStoreManager.cpp
Normal file
146
Libraries/LibWeb/Painting/BackingStoreManager.cpp
Normal file
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/Timer.h>
|
||||
#include <LibWeb/HTML/TraversableNavigable.h>
|
||||
#include <LibWeb/Painting/BackingStoreManager.h>
|
||||
#include <WebContent/PageClient.h>
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
# include <LibCore/IOSurface.h>
|
||||
# include <LibCore/MachPort.h>
|
||||
# include <LibCore/Platform/MachMessageTypes.h>
|
||||
#endif
|
||||
|
||||
namespace Web::Painting {
|
||||
|
||||
GC_DEFINE_ALLOCATOR(BackingStoreManager);
|
||||
|
||||
#ifdef AK_OS_MACOS
|
||||
static Optional<Core::MachPort> s_browser_mach_port;
|
||||
void BackingStoreManager::set_browser_mach_port(Core::MachPort&& port)
|
||||
{
|
||||
s_browser_mach_port = move(port);
|
||||
}
|
||||
#endif
|
||||
|
||||
BackingStoreManager::BackingStoreManager(HTML::Navigable& navigable)
|
||||
: m_navigable(navigable)
|
||||
{
|
||||
m_backing_store_shrink_timer = Core::Timer::create_single_shot(3000, [this] {
|
||||
resize_backing_stores_if_needed(WindowResizingInProgress::No);
|
||||
});
|
||||
}
|
||||
|
||||
void BackingStoreManager::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_navigable);
|
||||
}
|
||||
|
||||
void BackingStoreManager::restart_resize_timer()
|
||||
{
|
||||
m_backing_store_shrink_timer->restart();
|
||||
}
|
||||
|
||||
void BackingStoreManager::reallocate_backing_stores(Gfx::IntSize size)
|
||||
{
|
||||
#ifdef AK_OS_MACOS
|
||||
if (m_navigable->is_top_level_traversable() && s_browser_mach_port.has_value()) {
|
||||
auto back_iosurface = Core::IOSurfaceHandle::create(size.width(), size.height());
|
||||
auto back_iosurface_port = back_iosurface.create_mach_port();
|
||||
|
||||
auto front_iosurface = Core::IOSurfaceHandle::create(size.width(), size.height());
|
||||
auto front_iosurface_port = front_iosurface.create_mach_port();
|
||||
|
||||
m_front_bitmap_id = m_next_bitmap_id++;
|
||||
m_back_bitmap_id = m_next_bitmap_id++;
|
||||
|
||||
auto& page_client = m_navigable->top_level_traversable()->page().client();
|
||||
|
||||
Core::Platform::BackingStoreMetadata metadata;
|
||||
metadata.page_id = page_client.id();
|
||||
metadata.front_backing_store_id = m_front_bitmap_id;
|
||||
metadata.back_backing_store_id = m_back_bitmap_id;
|
||||
|
||||
Core::Platform::MessageWithBackingStores message;
|
||||
|
||||
message.header.msgh_remote_port = s_browser_mach_port->port();
|
||||
message.header.msgh_local_port = MACH_PORT_NULL;
|
||||
message.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;
|
||||
message.header.msgh_size = sizeof(message);
|
||||
message.header.msgh_id = Core::Platform::BACKING_STORE_IOSURFACES_MESSAGE_ID;
|
||||
|
||||
message.body.msgh_descriptor_count = 2;
|
||||
|
||||
message.front_descriptor.name = front_iosurface_port.release();
|
||||
message.front_descriptor.disposition = MACH_MSG_TYPE_MOVE_SEND;
|
||||
message.front_descriptor.type = MACH_MSG_PORT_DESCRIPTOR;
|
||||
|
||||
message.back_descriptor.name = back_iosurface_port.release();
|
||||
message.back_descriptor.disposition = MACH_MSG_TYPE_MOVE_SEND;
|
||||
message.back_descriptor.type = MACH_MSG_PORT_DESCRIPTOR;
|
||||
|
||||
message.metadata = metadata;
|
||||
|
||||
mach_msg_timeout_t const timeout = 100; // milliseconds
|
||||
auto const send_result = mach_msg(&message.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT, message.header.msgh_size, 0, MACH_PORT_NULL, timeout, MACH_PORT_NULL);
|
||||
if (send_result != KERN_SUCCESS) {
|
||||
dbgln("Failed to send message to server: {}", mach_error_string(send_result));
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
m_front_store = IOSurfaceBackingStore::create(move(front_iosurface));
|
||||
m_back_store = IOSurfaceBackingStore::create(move(back_iosurface));
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
m_front_bitmap_id = m_next_bitmap_id++;
|
||||
m_back_bitmap_id = m_next_bitmap_id++;
|
||||
|
||||
auto front_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, size).release_value();
|
||||
auto back_bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, size).release_value();
|
||||
|
||||
m_front_store = BitmapBackingStore::create(front_bitmap);
|
||||
m_back_store = BitmapBackingStore::create(back_bitmap);
|
||||
|
||||
if (m_navigable->is_top_level_traversable()) {
|
||||
auto& page_client = m_navigable->top_level_traversable()->page().client();
|
||||
page_client.page_did_allocate_backing_stores(m_front_bitmap_id, front_bitmap->to_shareable_bitmap(), m_back_bitmap_id, back_bitmap->to_shareable_bitmap());
|
||||
}
|
||||
}
|
||||
|
||||
void BackingStoreManager::resize_backing_stores_if_needed(WindowResizingInProgress window_resize_in_progress)
|
||||
{
|
||||
auto viewport_size = m_navigable->page().css_to_device_rect(m_navigable->viewport_rect()).size();
|
||||
if (viewport_size.is_empty())
|
||||
return;
|
||||
|
||||
Web::DevicePixelSize minimum_needed_size;
|
||||
if (window_resize_in_progress == WindowResizingInProgress::Yes) {
|
||||
// Pad the minimum needed size so that we don't have to keep reallocating backing stores while the window is being resized.
|
||||
minimum_needed_size = { viewport_size.width() + 256, viewport_size.height() + 256 };
|
||||
} else {
|
||||
// If we're not in the middle of a resize, we can shrink the backing store size to match the viewport size.
|
||||
minimum_needed_size = viewport_size;
|
||||
m_front_store.clear();
|
||||
m_back_store.clear();
|
||||
}
|
||||
|
||||
if (!m_front_store || !m_back_store || !m_front_store->size().contains(minimum_needed_size.to_type<int>())) {
|
||||
reallocate_backing_stores(minimum_needed_size.to_type<int>());
|
||||
}
|
||||
}
|
||||
|
||||
void BackingStoreManager::swap_back_and_front()
|
||||
{
|
||||
swap(m_front_store, m_back_store);
|
||||
swap(m_front_bitmap_id, m_back_bitmap_id);
|
||||
}
|
||||
|
||||
}
|
63
Libraries/LibWeb/Painting/BackingStoreManager.h
Normal file
63
Libraries/LibWeb/Painting/BackingStoreManager.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/HTML/Navigable.h>
|
||||
#include <LibWeb/Painting/BackingStore.h>
|
||||
|
||||
namespace Web::Painting {
|
||||
|
||||
class BackingStoreManager : public JS::Cell {
|
||||
GC_CELL(BackingStoreManager, JS::Cell);
|
||||
GC_DECLARE_ALLOCATOR(BackingStoreManager);
|
||||
|
||||
public:
|
||||
#ifdef AK_OS_MACOS
|
||||
static void set_browser_mach_port(Core::MachPort&&);
|
||||
#endif
|
||||
|
||||
enum class WindowResizingInProgress {
|
||||
No,
|
||||
Yes
|
||||
};
|
||||
void resize_backing_stores_if_needed(WindowResizingInProgress window_resize_in_progress);
|
||||
void reallocate_backing_stores(Gfx::IntSize);
|
||||
void restart_resize_timer();
|
||||
|
||||
struct BackingStore {
|
||||
i32 bitmap_id { -1 };
|
||||
Web::Painting::BackingStore* store { nullptr };
|
||||
};
|
||||
|
||||
BackingStore acquire_store_for_next_frame()
|
||||
{
|
||||
BackingStore backing_store;
|
||||
backing_store.bitmap_id = m_back_bitmap_id;
|
||||
backing_store.store = m_back_store.ptr();
|
||||
swap_back_and_front();
|
||||
return backing_store;
|
||||
}
|
||||
|
||||
virtual void visit_edges(Cell::Visitor& visitor) override;
|
||||
|
||||
BackingStoreManager(HTML::Navigable&);
|
||||
|
||||
private:
|
||||
void swap_back_and_front();
|
||||
|
||||
GC::Ref<HTML::Navigable> m_navigable;
|
||||
|
||||
i32 m_front_bitmap_id { -1 };
|
||||
i32 m_back_bitmap_id { -1 };
|
||||
RefPtr<Painting::BackingStore> m_front_store;
|
||||
RefPtr<Painting::BackingStore> m_back_store;
|
||||
int m_next_bitmap_id { 0 };
|
||||
|
||||
RefPtr<Core::Timer> m_backing_store_shrink_timer;
|
||||
};
|
||||
|
||||
}
|
|
@ -55,7 +55,7 @@ void NavigableContainerViewportPaintable::paint(PaintContext& context, PaintPhas
|
|||
|
||||
context.display_list_recorder().add_clip_rect(clip_rect.to_type<int>());
|
||||
|
||||
DOM::Document::PaintConfig paint_config;
|
||||
HTML::PaintConfig paint_config;
|
||||
paint_config.paint_overlay = context.should_paint_overlay();
|
||||
paint_config.should_show_line_box_borders = context.should_show_line_box_borders();
|
||||
auto display_list = const_cast<DOM::Document*>(hosted_document)->record_display_list(paint_config);
|
||||
|
|
|
@ -66,6 +66,7 @@ public:
|
|||
GC::Ref<Page> m_host_page;
|
||||
GC::Ptr<Page> m_svg_page;
|
||||
|
||||
virtual u64 id() const override { VERIFY_NOT_REACHED(); }
|
||||
virtual Page& page() override { return *m_svg_page; }
|
||||
virtual Page const& page() const override { return *m_svg_page; }
|
||||
virtual bool is_connection_open() const override { return false; }
|
||||
|
@ -76,10 +77,6 @@ public:
|
|||
virtual CSS::PreferredContrast preferred_contrast() const override { return m_host_page->client().preferred_contrast(); }
|
||||
virtual CSS::PreferredMotion preferred_motion() const override { return m_host_page->client().preferred_motion(); }
|
||||
virtual void request_file(FileRequest) override { }
|
||||
virtual void paint_next_frame() override { }
|
||||
virtual void process_screenshot_requests() override { }
|
||||
virtual void start_display_list_rendering(DevicePixelRect const&, Painting::BackingStore&, PaintOptions, Function<void()>&&) override { }
|
||||
virtual bool is_ready_to_paint() const override { return true; }
|
||||
virtual Queue<QueuedInputEvent>& input_event_queue() override { VERIFY_NOT_REACHED(); }
|
||||
virtual void report_finished_handling_input_event([[maybe_unused]] u64 page_id, [[maybe_unused]] EventResult event_was_handled) override { }
|
||||
|
||||
|
|
|
@ -58,7 +58,8 @@ ErrorOr<GC::Ref<HTML::HTMLCanvasElement>, WebDriver::Error> draw_bounding_box_fr
|
|||
auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, canvas.surface()->size()));
|
||||
auto backing_store = Painting::BitmapBackingStore::create(bitmap);
|
||||
IGNORE_USE_IN_ESCAPING_LAMBDA bool did_paint = false;
|
||||
browsing_context.page().client().start_display_list_rendering(paint_rect.to_type<Web::DevicePixels>(), backing_store, {}, [&did_paint] {
|
||||
HTML::PaintConfig paint_config { .canvas_fill_rect = paint_rect };
|
||||
browsing_context.active_document()->navigable()->start_display_list_rendering(backing_store, paint_config, [&did_paint] {
|
||||
did_paint = true;
|
||||
});
|
||||
HTML::main_thread_event_loop().spin_until(GC::create_function(HTML::main_thread_event_loop().heap(), [&] {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue