Skip to content

Truly independent web browser

Compositor+LibWebView: Let the platform pick the GPU to paint on

Andreas Klingb10ce2db4dad
diff --git a/Libraries/LibGfx/VulkanContext.cpp b/Libraries/LibGfx/VulkanContext.cpp
index 898e30dcddc..767abc255f7 100644
--- a/Libraries/LibGfx/VulkanContext.cpp
+++ b/Libraries/LibGfx/VulkanContext.cpp
@@ -51,7 +51,7 @@ static ErrorOr<VkInstance> create_instance(uint32_t api_version)
return instance;
}
-static ErrorOr<VkPhysicalDevice> pick_physical_device(VkInstance instance)
+static ErrorOr<VkPhysicalDevice> pick_physical_device(VkInstance instance, Optional<GpuDeviceIdentity> const& preferred_device)
{
uint32_t device_count = 0;
vkEnumeratePhysicalDevices(instance, &device_count, nullptr);
@@ -63,6 +63,14 @@ static ErrorOr<VkPhysicalDevice> pick_physical_device(VkInstance instance)
devices.resize(device_count);
vkEnumeratePhysicalDevices(instance, &device_count, devices.data());
+ if (preferred_device.has_value()) {
+ for (auto const& device : devices) {
+ if (gpu_device_identity_of(device) == *preferred_device)
+ return device;
+ }
+ dbgln("The preferred GPU is not a Vulkan device here, picking another one");
+ }
+
VkPhysicalDevice picked_device = VK_NULL_HANDLE;
// Pick discrete GPU or the first device in the list
for (auto const& device : devices) {
@@ -212,11 +220,11 @@ GpuDeviceIdentity gpu_device_identity_of(VkPhysicalDevice physical_device)
return identity;
}
-ErrorOr<VulkanContext> create_vulkan_context()
+ErrorOr<VulkanContext> create_vulkan_context(Optional<GpuDeviceIdentity> const& preferred_device)
{
uint32_t const api_version = VK_API_VERSION_1_1; // v1.1 needed for vkGetPhysicalDeviceFormatProperties2
auto* instance = TRY(create_instance(api_version));
- auto* physical_device = TRY(pick_physical_device(instance));
+ auto* physical_device = TRY(pick_physical_device(instance, preferred_device));
uint32_t graphics_queue_family = 0;
auto can_sample_through_ycbcr_conversion = device_can_sample_through_ycbcr_conversion(physical_device);
diff --git a/Libraries/LibGfx/VulkanContext.h b/Libraries/LibGfx/VulkanContext.h
index 246236d7634..08f635cf538 100644
--- a/Libraries/LibGfx/VulkanContext.h
+++ b/Libraries/LibGfx/VulkanContext.h
@@ -37,7 +37,8 @@ struct VulkanContext {
# endif
};
-ErrorOr<VulkanContext> create_vulkan_context();
+// The preferred device is picked when it is present, since memory the context shares can only be imported on the same one.
+ErrorOr<VulkanContext> create_vulkan_context(Optional<GpuDeviceIdentity> const& preferred_device = {});
GpuDeviceIdentity gpu_device_identity_of(VkPhysicalDevice);
}
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index 72a2641aace..07cdab0e91a 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -146,6 +146,8 @@ public:
void platform_scrollbar_track_press_behavior_changed();
// The languages the user prefers, as the platform lists them (BCP 47 tags, most preferred first).
virtual Vector<String> platform_preferred_languages() const { return {}; }
+ // The GPU the platform presents on, which the compositor paints on too, so that it can share its frames without a copy.
+ virtual Optional<Gfx::GpuDeviceIdentity> platform_presentation_gpu_device() const { return {}; }
virtual OwnPtr<PictureInPictureWindow> create_picture_in_picture_window(WebContentPage& requesting_page, CanonicalTraversable&, Gfx::IntSize video_size);
diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cpp
index c0fd0de7891..7d8991d83a2 100644
--- a/Libraries/LibWebView/HelperProcess.cpp
+++ b/Libraries/LibWebView/HelperProcess.cpp
@@ -537,6 +537,10 @@ ErrorOr<NonnullRefPtr<WebView::CompositorClient>> launch_compositor_process()
arguments.append("--force-cpu-painting"sv);
if (web_content_options.force_fontconfig == WebView::ForceFontconfig::Yes)
arguments.append("--force-fontconfig"sv);
+ if (auto gpu_device = WebView::Application::the().platform_presentation_gpu_device(); gpu_device.has_value()) {
+ arguments.append("--gpu-device"sv);
+ arguments.append(gpu_device->to_byte_string());
+ }
if (auto server = mach_server_name(); server.has_value()) {
arguments.append("--mach-server-name"sv);
arguments.append(server.value());
diff --git a/Services/Compositor/SkiaBackendContext.cpp b/Services/Compositor/SkiaBackendContext.cpp
index 428973bdd8b..199a5c35b33 100644
--- a/Services/Compositor/SkiaBackendContext.cpp
+++ b/Services/Compositor/SkiaBackendContext.cpp
@@ -139,14 +139,14 @@ size_t SkiaBackendContext::resource_cache_bytes() const
return bytes;
}
-void SkiaBackendContext::initialize_gpu_backend()
+void SkiaBackendContext::initialize_gpu_backend(Optional<GpuDeviceIdentity> const& preferred_device)
{
VERIFY(!main_thread_context());
- main_thread_context() = create_independent_gpu_backend();
+ main_thread_context() = create_independent_gpu_backend(preferred_device);
}
-RefPtr<SkiaBackendContext> SkiaBackendContext::create_independent_gpu_backend()
+RefPtr<SkiaBackendContext> SkiaBackendContext::create_independent_gpu_backend([[maybe_unused]] Optional<GpuDeviceIdentity> const& preferred_device)
{
#ifdef AK_OS_MACOS
auto metal_context = get_metal_context();
@@ -161,7 +161,7 @@ RefPtr<SkiaBackendContext> SkiaBackendContext::create_independent_gpu_backend()
}
return create_direct3d_context(maybe_direct3d_context.release_value());
#elif defined(USE_VULKAN)
- auto maybe_vulkan_context = Gfx::create_vulkan_context();
+ auto maybe_vulkan_context = Gfx::create_vulkan_context(preferred_device);
if (maybe_vulkan_context.is_error()) {
dbgln("Vulkan context creation failed: {}", maybe_vulkan_context.error());
return {};
diff --git a/Services/Compositor/SkiaBackendContext.h b/Services/Compositor/SkiaBackendContext.h
index 806529ba1da..8a93975a4a7 100644
--- a/Services/Compositor/SkiaBackendContext.h
+++ b/Services/Compositor/SkiaBackendContext.h
@@ -9,6 +9,8 @@
#include <AK/AtomicRefCounted.h>
#include <AK/Function.h>
#include <AK/Noncopyable.h>
+#include <AK/Optional.h>
+#include <LibGfx/GpuDeviceIdentity.h>
#ifdef USE_DIRECTX
# include <LibGfx/Direct3DContext.h>
@@ -49,8 +51,9 @@ public:
static RefPtr<SkiaBackendContext> create_metal_context(NonnullRefPtr<MetalContext>);
#endif
- static void initialize_gpu_backend();
- static RefPtr<SkiaBackendContext> create_independent_gpu_backend();
+ // The preferred GPU is used when there is one, so that the backing stores can be shared with the presentation.
+ static void initialize_gpu_backend(Optional<GpuDeviceIdentity> const& preferred_device = {});
+ static RefPtr<SkiaBackendContext> create_independent_gpu_backend(Optional<GpuDeviceIdentity> const& preferred_device = {});
static RefPtr<SkiaBackendContext> the_main_thread_context();
SkiaBackendContext() { }
diff --git a/Services/Compositor/main.cpp b/Services/Compositor/main.cpp
index af360ab0b87..15ea71cb507 100644
--- a/Services/Compositor/main.cpp
+++ b/Services/Compositor/main.cpp
@@ -34,6 +34,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
bool force_cpu_painting = false;
bool force_fontconfig = false;
bool disable_sandbox = false;
+ StringView gpu_device;
int crash_report_fd = -1;
StringView stats_log_directory;
@@ -48,6 +49,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
args_parser.add_option(force_cpu_painting, "Force CPU painting", "force-cpu-painting");
args_parser.add_option(force_fontconfig, "Force using fontconfig for font loading", "force-fontconfig");
args_parser.add_option(disable_sandbox, "Disable process sandboxing", "disable-sandbox");
+ args_parser.add_option(gpu_device, "The GPU to paint on, as the presentation identifies it", "gpu-device", 0, "identity");
args_parser.parse(arguments);
auto stats_log = TRY(Core::StatsLog::create(stats_log_directory, "Compositor"sv));
@@ -55,6 +57,13 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
if (resource_root.is_empty())
return Error::from_string_literal("--resource-root is required");
+ Optional<Gfx::GpuDeviceIdentity> preferred_gpu_device;
+ if (!gpu_device.is_empty()) {
+ preferred_gpu_device = Gfx::GpuDeviceIdentity::from_string(gpu_device);
+ if (!preferred_gpu_device.has_value())
+ return Error::from_string_literal("Malformed GPU device identity");
+ }
+
if (crash_report_fd >= 0) {
if (auto result = Core::CrashHandler::initialize(crash_report_fd); result.is_error())
warnln("Could not install crash report handler: {}", result.error());
@@ -85,7 +94,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
#endif
if (!force_cpu_painting)
- Gfx::SkiaBackendContext::initialize_gpu_backend();
+ Gfx::SkiaBackendContext::initialize_gpu_backend(preferred_gpu_device);
auto skia_backend_context = Gfx::SkiaBackendContext::the_main_thread_context();
auto& event_loop = Core::EventLoop::initialize_for_current_thread();