Skip to content

Truly independent web browser

LibGfx: Create a type for GPU device identity

Zaggy1024770cd46e7b6f
diff --git a/Libraries/LibGfx/CMakeLists.txt b/Libraries/LibGfx/CMakeLists.txt
index 2a4582cb9ea..dde0b47a654 100644
--- a/Libraries/LibGfx/CMakeLists.txt
+++ b/Libraries/LibGfx/CMakeLists.txt
@@ -26,6 +26,7 @@ set(SOURCES
Font/WOFF/Loader.cpp
Font/WOFF2/Loader.cpp
FontCascadeList.cpp
+ GpuDeviceIdentity.cpp
GradientPainting.cpp
Gradients.cpp
LinuxDmaBufHandle.cpp
diff --git a/Libraries/LibGfx/GpuDeviceIdentity.cpp b/Libraries/LibGfx/GpuDeviceIdentity.cpp
new file mode 100644
index 00000000000..77d71fbe28c
--- /dev/null
+++ b/Libraries/LibGfx/GpuDeviceIdentity.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Hex.h>
+#include <LibGfx/GpuDeviceIdentity.h>
+#include <LibIPC/Decoder.h>
+#include <LibIPC/Encoder.h>
+
+namespace Gfx {
+
+ByteString GpuDeviceIdentity::to_byte_string() const
+{
+ return encode_hex(bytes);
+}
+
+Optional<GpuDeviceIdentity> GpuDeviceIdentity::from_string(StringView string)
+{
+ auto bytes = decode_hex(string);
+ if (bytes.is_error() || bytes.value().is_empty() || bytes.value().size() > MAXIMUM_BYTE_COUNT)
+ return {};
+ return GpuDeviceIdentity { bytes.release_value() };
+}
+
+}
+
+namespace IPC {
+
+template<>
+ErrorOr<void> encode(Encoder& encoder, Gfx::GpuDeviceIdentity const& identity)
+{
+ return encoder.encode(identity.bytes);
+}
+
+template<>
+ErrorOr<Gfx::GpuDeviceIdentity> decode(Decoder& decoder)
+{
+ auto bytes = TRY(decoder.decode<ByteBuffer>());
+ if (bytes.is_empty() || bytes.size() > Gfx::GpuDeviceIdentity::MAXIMUM_BYTE_COUNT)
+ return Error::from_string_literal("GPU device identity has an unusable length");
+ return Gfx::GpuDeviceIdentity { move(bytes) };
+}
+
+}
diff --git a/Libraries/LibGfx/GpuDeviceIdentity.h b/Libraries/LibGfx/GpuDeviceIdentity.h
new file mode 100644
index 00000000000..8c0f0e1dec0
--- /dev/null
+++ b/Libraries/LibGfx/GpuDeviceIdentity.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/ByteBuffer.h>
+#include <AK/ByteString.h>
+#include <AK/Optional.h>
+#include <AK/StringView.h>
+#include <LibIPC/Forward.h>
+
+namespace Gfx {
+
+// Names a GPU across processes, since memory that one process shares from a GPU can only be imported on the same one.
+// The bytes are whatever the graphics API identifies the device and its driver by, and are only compared.
+struct GpuDeviceIdentity {
+ static constexpr size_t MAXIMUM_BYTE_COUNT = 64;
+
+ ByteBuffer bytes;
+
+ bool operator==(GpuDeviceIdentity const&) const = default;
+
+ ByteString to_byte_string() const;
+ static Optional<GpuDeviceIdentity> from_string(StringView);
+};
+
+}
+
+namespace IPC {
+
+template<>
+ErrorOr<void> encode(Encoder&, Gfx::GpuDeviceIdentity const&);
+
+template<>
+ErrorOr<Gfx::GpuDeviceIdentity> decode(Decoder&);
+
+}
diff --git a/Libraries/LibGfx/VulkanContext.cpp b/Libraries/LibGfx/VulkanContext.cpp
index 9a3fdee04ee..9e0bb87588d 100644
--- a/Libraries/LibGfx/VulkanContext.cpp
+++ b/Libraries/LibGfx/VulkanContext.cpp
@@ -176,6 +176,22 @@ static ErrorOr<VkCommandBuffer> allocate_command_buffer(VkDevice logical_device,
}
#endif
+GpuDeviceIdentity gpu_device_identity_of(VkPhysicalDevice physical_device)
+{
+ VkPhysicalDeviceIDProperties id_properties {};
+ id_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
+ VkPhysicalDeviceProperties2 properties {};
+ properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
+ properties.pNext = &id_properties;
+ vkGetPhysicalDeviceProperties2(physical_device, &properties);
+
+ // Memory exported from one device imports only where both the device and the driver match.
+ GpuDeviceIdentity identity;
+ identity.bytes.append(id_properties.deviceUUID, VK_UUID_SIZE);
+ identity.bytes.append(id_properties.driverUUID, VK_UUID_SIZE);
+ return identity;
+}
+
ErrorOr<VulkanContext> create_vulkan_context()
{
uint32_t const api_version = VK_API_VERSION_1_1; // v1.1 needed for vkGetPhysicalDeviceFormatProperties2
@@ -205,6 +221,7 @@ ErrorOr<VulkanContext> create_vulkan_context()
.api_version = api_version,
.instance = instance,
.physical_device = physical_device,
+ .device_identity = gpu_device_identity_of(physical_device),
.logical_device = logical_device,
.graphics_queue = graphics_queue,
.graphics_queue_family = graphics_queue_family,
diff --git a/Libraries/LibGfx/VulkanContext.h b/Libraries/LibGfx/VulkanContext.h
index db636abdd24..9b251ae256a 100644
--- a/Libraries/LibGfx/VulkanContext.h
+++ b/Libraries/LibGfx/VulkanContext.h
@@ -9,6 +9,7 @@
#ifdef USE_VULKAN
# include <AK/Error.h>
+# include <LibGfx/GpuDeviceIdentity.h>
# include <vulkan/vulkan.h>
namespace Gfx {
@@ -17,6 +18,7 @@ struct VulkanContext {
uint32_t api_version { VK_API_VERSION_1_0 };
VkInstance instance { VK_NULL_HANDLE };
VkPhysicalDevice physical_device { VK_NULL_HANDLE };
+ GpuDeviceIdentity device_identity;
VkDevice logical_device { VK_NULL_HANDLE };
VkQueue graphics_queue { VK_NULL_HANDLE };
uint32_t graphics_queue_family { 0 };
@@ -32,6 +34,7 @@ struct VulkanContext {
};
ErrorOr<VulkanContext> create_vulkan_context();
+GpuDeviceIdentity gpu_device_identity_of(VkPhysicalDevice);
}