LibWeb: Cache Skia backend context in TraversableNavigable

We just need to create the backend context once and let Skia handle the
context's state.

On my machine, this reduces the load time for https://tweakers.net from
7.5s to 3.5s.
This commit is contained in:
Jelle Raaijmakers 2025-01-28 11:07:42 +01:00 committed by Alexander Kalenik
parent 1d81c4d8eb
commit 7b3d4a9edb
Notes: github-actions[bot] 2025-01-31 12:29:26 +00:00

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -25,29 +26,35 @@ 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)
, m_session_history_traversal_queue(vm().heap().allocate<SessionHistoryTraversalQueue>())
{
#ifdef USE_VULKAN
auto display_list_player_type = page->client().display_list_player_type();
if (display_list_player_type == DisplayListPlayerType::SkiaGPUIfAvailable) {
auto maybe_vulkan_context = Gfx::create_vulkan_context();
if (!maybe_vulkan_context.is_error()) {
auto vulkan_context = maybe_vulkan_context.release_value();
m_skia_backend_context = Gfx::SkiaBackendContext::create_vulkan_context(vulkan_context);
} else {
dbgln("Vulkan context creation failed: {}", maybe_vulkan_context.error());
}
}
#endif
#ifdef AK_OS_MACOS
auto display_list_player_type = page->client().display_list_player_type();
if (display_list_player_type == DisplayListPlayerType::SkiaGPUIfAvailable)
auto metal_context = Gfx::get_metal_context();
m_skia_backend_context = Gfx::SkiaBackendContext::create_metal_context(*metal_context);
#endif
m_skia_backend_context = get_skia_backend_context();
}
TraversableNavigable::~TraversableNavigable() = default;