LibWeb+LibGfx: Replace BackingStore with PaintingSurface

Now, when Skia backend context is available by the time backing stores
are allocated, there is no need to have a separate BackingStore class.

This allows us to get rid of BackingStore -> PaintingSurface cache.
This commit is contained in:
Aliaksandr Kalenik 2025-06-27 04:39:16 +02:00 committed by Jelle Raaijmakers
commit c18314b942
Notes: github-actions[bot] 2025-07-04 14:14:05 +00:00
14 changed files with 52 additions and 189 deletions

View file

@ -2327,8 +2327,6 @@ 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);
@ -2557,8 +2555,8 @@ void Navigable::ready_to_paint()
void Navigable::paint_next_frame()
{
auto [backing_store_id, back_store] = m_backing_store_manager->acquire_store_for_next_frame();
if (!back_store)
auto [backing_store_id, painting_surface] = m_backing_store_manager->acquire_store_for_next_frame();
if (!painting_surface)
return;
VERIFY(m_number_of_queued_rasterization_tasks <= 1);
@ -2566,7 +2564,7 @@ void Navigable::paint_next_frame()
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] {
start_display_list_rendering(*painting_surface, paint_config, [this, viewport_rect, backing_store_id] {
if (!is_top_level_traversable())
return;
auto& traversable = *page().top_level_traversable();
@ -2574,7 +2572,7 @@ void Navigable::paint_next_frame()
});
}
void Navigable::start_display_list_rendering(Painting::BackingStore& target, PaintConfig paint_config, Function<void()>&& callback)
void Navigable::start_display_list_rendering(Gfx::PaintingSurface& painting_surface, PaintConfig paint_config, Function<void()>&& callback)
{
m_needs_repaint = false;
auto document = active_document();
@ -2589,7 +2587,7 @@ void Navigable::start_display_list_rendering(Painting::BackingStore& target, Pai
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));
m_rendering_thread.enqueue_rendering_task(*display_list, move(scroll_state_snapshot), painting_surface, move(callback));
}
}

View file

@ -208,7 +208,7 @@ public:
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);
void start_display_list_rendering(Gfx::PaintingSurface&, PaintConfig, Function<void()>&& callback);
bool needs_repaint() const { return m_needs_repaint; }
void set_needs_repaint() { m_needs_repaint = true; }

View file

@ -7,7 +7,6 @@
#include <LibCore/EventLoop.h>
#include <LibWeb/HTML/RenderingThread.h>
#include <LibWeb/HTML/TraversableNavigable.h>
#include <LibWeb/Painting/BackingStore.h>
namespace Web::HTML {
@ -49,10 +48,6 @@ void RenderingThread::rendering_thread_loop()
while (true) {
auto task = [this]() -> Optional<Task> {
Threading::MutexLocker const locker { m_rendering_task_mutex };
if (m_needs_to_clear_bitmap_to_surface_cache) {
m_bitmap_to_surface.clear();
m_needs_to_clear_bitmap_to_surface_cache = false;
}
while (m_rendering_tasks.is_empty() && !m_exit) {
m_rendering_task_ready_wake_condition.wait();
}
@ -66,8 +61,7 @@ void RenderingThread::rendering_thread_loop()
break;
}
auto painting_surface = painting_surface_for_backing_store(task->backing_store);
m_skia_player->execute(*task->display_list, task->scroll_state_snapshot, painting_surface);
m_skia_player->execute(*task->display_list, task->scroll_state_snapshot, task->painting_surface);
if (m_exit)
break;
m_main_thread_event_loop.deferred_invoke([callback = move(task->callback)] {
@ -76,48 +70,11 @@ void RenderingThread::rendering_thread_loop()
}
}
void RenderingThread::enqueue_rendering_task(NonnullRefPtr<Painting::DisplayList> display_list, Painting::ScrollStateSnapshot&& scroll_state_snapshot, NonnullRefPtr<Painting::BackingStore> backing_store, Function<void()>&& callback)
void RenderingThread::enqueue_rendering_task(NonnullRefPtr<Painting::DisplayList> display_list, Painting::ScrollStateSnapshot&& scroll_state_snapshot, NonnullRefPtr<Gfx::PaintingSurface> painting_surface, Function<void()>&& callback)
{
Threading::MutexLocker const locker { m_rendering_task_mutex };
m_rendering_tasks.enqueue(Task { move(display_list), move(scroll_state_snapshot), move(backing_store), move(callback) });
m_rendering_tasks.enqueue(Task { move(display_list), move(scroll_state_snapshot), move(painting_surface), move(callback) });
m_rendering_task_ready_wake_condition.signal();
}
NonnullRefPtr<Gfx::PaintingSurface> RenderingThread::painting_surface_for_backing_store(Painting::BackingStore& backing_store)
{
auto& bitmap = backing_store.bitmap();
auto cached_surface = m_bitmap_to_surface.find(&bitmap);
if (cached_surface != m_bitmap_to_surface.end())
return cached_surface->value;
RefPtr<Gfx::PaintingSurface> new_surface;
if (m_display_list_player_type == DisplayListPlayerType::SkiaGPUIfAvailable && m_skia_backend_context) {
#ifdef USE_VULKAN
// Vulkan: Try to create an accelerated surface.
new_surface = Gfx::PaintingSurface::create_with_size(m_skia_backend_context, backing_store.size(), Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied);
new_surface->on_flush = [backing_store = static_cast<NonnullRefPtr<Painting::BackingStore>>(backing_store)](auto& surface) { surface.read_into_bitmap(backing_store->bitmap()); };
#endif
#ifdef AK_OS_MACOS
// macOS: Wrap an IOSurface if available.
if (is<Painting::IOSurfaceBackingStore>(backing_store)) {
auto& iosurface_backing_store = static_cast<Painting::IOSurfaceBackingStore&>(backing_store);
new_surface = Gfx::PaintingSurface::wrap_iosurface(iosurface_backing_store.iosurface_handle(), *m_skia_backend_context);
}
#endif
}
// CPU and fallback: wrap the backing store bitmap directly.
if (!new_surface)
new_surface = Gfx::PaintingSurface::wrap_bitmap(bitmap);
m_bitmap_to_surface.set(&bitmap, *new_surface);
return *new_surface;
}
void RenderingThread::clear_bitmap_to_surface_cache()
{
Threading::MutexLocker const locker { m_rendering_task_mutex };
m_needs_to_clear_bitmap_to_surface_cache = true;
}
}

View file

@ -29,12 +29,10 @@ public:
void start(DisplayListPlayerType);
void set_skia_player(OwnPtr<Painting::DisplayListPlayerSkia>&& player) { m_skia_player = move(player); }
void set_skia_backend_context(RefPtr<Gfx::SkiaBackendContext> context) { m_skia_backend_context = move(context); }
void enqueue_rendering_task(NonnullRefPtr<Painting::DisplayList>, Painting::ScrollStateSnapshot&&, NonnullRefPtr<Painting::BackingStore>, Function<void()>&& callback);
void clear_bitmap_to_surface_cache();
void enqueue_rendering_task(NonnullRefPtr<Painting::DisplayList>, Painting::ScrollStateSnapshot&&, NonnullRefPtr<Gfx::PaintingSurface>, Function<void()>&& callback);
private:
void rendering_thread_loop();
NonnullRefPtr<Gfx::PaintingSurface> painting_surface_for_backing_store(Painting::BackingStore& backing_store);
Core::EventLoop& m_main_thread_event_loop;
DisplayListPlayerType m_display_list_player_type;
@ -49,7 +47,7 @@ private:
struct Task {
NonnullRefPtr<Painting::DisplayList> display_list;
Painting::ScrollStateSnapshot scroll_state_snapshot;
NonnullRefPtr<Painting::BackingStore> backing_store;
NonnullRefPtr<Gfx::PaintingSurface> painting_surface;
Function<void()> callback;
};
// NOTE: Queue will only contain multiple items in case tasks were scheduled by screenshot requests.
@ -57,9 +55,6 @@ private:
Queue<Task> m_rendering_tasks;
Threading::Mutex m_rendering_task_mutex;
Threading::ConditionVariable m_rendering_task_ready_wake_condition { m_rendering_task_mutex };
HashMap<Gfx::Bitmap*, NonnullRefPtr<Gfx::PaintingSurface>> m_bitmap_to_surface;
bool m_needs_to_clear_bitmap_to_surface_cache { false };
};
}

View file

@ -21,8 +21,7 @@
#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>
#include <LibWeb/Painting/PaintableBox.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
namespace Web::HTML {
@ -1413,19 +1412,19 @@ void TraversableNavigable::process_screenshot_requests()
}
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);
auto painting_surface = Gfx::PaintingSurface::wrap_bitmap(*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());
start_display_list_rendering(painting_surface, paint_config, [bitmap, &client] {
client.page_did_take_screenshot(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);
auto painting_surface = Gfx::PaintingSurface::wrap_bitmap(*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());
start_display_list_rendering(painting_surface, paint_config, [bitmap, &client] {
client.page_did_take_screenshot(bitmap->to_shareable_bitmap());
});
}
}