LibWeb: Make BackingStore atomic ref-counted

This is required to make sure rendering thread will keep backing stores
alive in case backing stores reallocation happens during rasterization.

Fixes https://github.com/LadybirdBrowser/ladybird/issues/4164
This commit is contained in:
Aliaksandr Kalenik 2025-04-01 01:44:35 +02:00
parent 1022566bff
commit 37603f332a
6 changed files with 28 additions and 17 deletions

View file

@ -1414,7 +1414,7 @@ NonnullRefPtr<Gfx::PaintingSurface> TraversableNavigable::painting_surface_for_b
#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 = [&bitmap = bitmap](auto& surface) { surface.read_into_bitmap(bitmap); };
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.

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/AtomicRefCounted.h>
#include <AK/Noncopyable.h>
#include <LibGfx/Size.h>
@ -15,7 +16,7 @@
namespace Web::Painting {
class BackingStore {
class BackingStore : public AtomicRefCounted<BackingStore> {
AK_MAKE_NONCOPYABLE(BackingStore);
public:
@ -28,19 +29,27 @@ public:
class BitmapBackingStore final : public BackingStore {
public:
BitmapBackingStore(RefPtr<Gfx::Bitmap>);
static NonnullRefPtr<BitmapBackingStore> create(RefPtr<Gfx::Bitmap> bitmap)
{
return adopt_ref(*new BitmapBackingStore(move(bitmap)));
}
Gfx::IntSize size() const override { return m_bitmap->size(); }
Gfx::Bitmap& bitmap() const override { return *m_bitmap; }
private:
BitmapBackingStore(RefPtr<Gfx::Bitmap>);
RefPtr<Gfx::Bitmap> m_bitmap;
};
#ifdef AK_OS_MACOS
class IOSurfaceBackingStore final : public BackingStore {
public:
IOSurfaceBackingStore(Core::IOSurfaceHandle&&);
static NonnullRefPtr<IOSurfaceBackingStore> create(Core::IOSurfaceHandle&& iosurface_handle)
{
return adopt_ref(*new IOSurfaceBackingStore(move(iosurface_handle)));
}
Gfx::IntSize size() const override;
@ -48,6 +57,8 @@ public:
Gfx::Bitmap& bitmap() const override { return *m_bitmap_wrapper; }
private:
IOSurfaceBackingStore(Core::IOSurfaceHandle&&);
Core::IOSurfaceHandle m_iosurface_handle;
RefPtr<Gfx::Bitmap> m_bitmap_wrapper;
};

View file

@ -56,7 +56,7 @@ ErrorOr<GC::Ref<HTML::HTMLCanvasElement>, WebDriver::Error> draw_bounding_box_fr
Gfx::IntRect paint_rect { rect.x(), rect.y(), paint_width, paint_height };
auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, canvas.surface()->size()));
auto backing_store = Web::Painting::BitmapBackingStore(bitmap);
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] {
did_paint = true;

View file

@ -83,8 +83,8 @@ void BackingStoreManager::reallocate_backing_stores(Gfx::IntSize size)
VERIFY_NOT_REACHED();
}
m_front_store = make<Web::Painting::IOSurfaceBackingStore>(move(front_iosurface));
m_back_store = make<Web::Painting::IOSurfaceBackingStore>(move(back_iosurface));
m_front_store = Web::Painting::IOSurfaceBackingStore::create(move(front_iosurface));
m_back_store = Web::Painting::IOSurfaceBackingStore::create(move(back_iosurface));
return;
}
@ -96,8 +96,8 @@ void BackingStoreManager::reallocate_backing_stores(Gfx::IntSize size)
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 = make<Web::Painting::BitmapBackingStore>(front_bitmap);
m_back_store = make<Web::Painting::BitmapBackingStore>(back_bitmap);
m_front_store = Web::Painting::BitmapBackingStore::create(front_bitmap);
m_back_store = Web::Painting::BitmapBackingStore::create(back_bitmap);
m_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());
}

View file

@ -38,8 +38,8 @@ private:
i32 m_front_bitmap_id { -1 };
i32 m_back_bitmap_id { -1 };
OwnPtr<Web::Painting::BackingStore> m_front_store;
OwnPtr<Web::Painting::BackingStore> m_back_store;
RefPtr<Web::Painting::BackingStore> m_front_store;
RefPtr<Web::Painting::BackingStore> m_back_store;
int m_next_bitmap_id { 0 };
RefPtr<Core::Timer> m_backing_store_shrink_timer;

View file

@ -205,16 +205,16 @@ void PageClient::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 = Web::Painting::BitmapBackingStore(*bitmap);
start_display_list_rendering(rect, backing_store, { .paint_overlay = Web::PaintOptions::PaintOverlay::No }, [this, bitmap] {
client().async_did_take_screenshot(m_id, bitmap->to_shareable_bitmap());
auto backing_store = Web::Painting::BitmapBackingStore::create(*bitmap);
start_display_list_rendering(rect, backing_store, { .paint_overlay = Web::PaintOptions::PaintOverlay::No }, [this, backing_store] {
client().async_did_take_screenshot(m_id, backing_store->bitmap().to_shareable_bitmap());
});
} else {
Web::DevicePixelRect rect { { 0, 0 }, content_size() };
auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, rect.size().to_type<int>()).release_value_but_fixme_should_propagate_errors();
auto backing_store = Web::Painting::BitmapBackingStore(*bitmap);
start_display_list_rendering(rect, backing_store, {}, [this, bitmap] {
client().async_did_take_screenshot(m_id, bitmap->to_shareable_bitmap());
auto backing_store = Web::Painting::BitmapBackingStore::create(*bitmap);
start_display_list_rendering(rect, backing_store, {}, [this, backing_store] {
client().async_did_take_screenshot(m_id, backing_store->bitmap().to_shareable_bitmap());
});
}
}