Skip to content

Truly independent web browser

Compositor: Release shared surfaces to the external queue family

Andreas Kling6335a61570ff
diff --git a/Libraries/LibGfx/VulkanContext.cpp b/Libraries/LibGfx/VulkanContext.cpp
index 4bc186a170f..79c6594b5b5 100644
--- a/Libraries/LibGfx/VulkanContext.cpp
+++ b/Libraries/LibGfx/VulkanContext.cpp
@@ -4,11 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/AnyOf.h>
#include <AK/Array.h>
#include <AK/Debug.h>
#include <AK/Format.h>
#include <AK/Vector.h>
#include <LibGfx/VulkanContext.h>
+#include <string.h>
namespace Gfx {
@@ -90,14 +92,33 @@ static ErrorOr<VkPhysicalDevice> pick_physical_device(VkInstance instance, Optio
}
#ifdef USE_VULKAN_DMABUF_IMAGES
-static constexpr Array<char const*, 4> s_enabled_device_extensions = {
+// NB: The last one is optional, and is only enabled on devices that have it.
+static constexpr Array<char const*, 5> s_enabled_device_extensions = {
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME,
VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
+ VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME,
};
+
+static ReadonlySpan<char const*> device_extensions_to_enable(VkPhysicalDevice physical_device)
+{
+ uint32_t extension_count = 0;
+ vkEnumerateDeviceExtensionProperties(physical_device, nullptr, &extension_count, nullptr);
+ Vector<VkExtensionProperties> extensions;
+ extensions.resize(extension_count);
+ vkEnumerateDeviceExtensionProperties(physical_device, nullptr, &extension_count, extensions.data());
+
+ ReadonlySpan<char const*> enabled_extensions = s_enabled_device_extensions;
+ if (any_of(extensions, [](auto const& extension) { return strcmp(extension.extensionName, VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME) == 0; }))
+ return enabled_extensions;
+ return enabled_extensions.trim(enabled_extensions.size() - 1);
+}
#else
-static constexpr Array<char const*, 0> s_enabled_device_extensions;
+static ReadonlySpan<char const*> device_extensions_to_enable(VkPhysicalDevice)
+{
+ return {};
+}
#endif
static bool device_can_sample_through_ycbcr_conversion(VkPhysicalDevice physical_device)
@@ -157,8 +178,9 @@ static ErrorOr<VkDevice> create_logical_device(VkPhysicalDevice physical_device,
create_device_info.pNext = &features;
create_device_info.pQueueCreateInfos = &queue_create_info;
create_device_info.queueCreateInfoCount = 1;
- create_device_info.enabledExtensionCount = s_enabled_device_extensions.size();
- create_device_info.ppEnabledExtensionNames = s_enabled_device_extensions.data();
+ auto enabled_extensions = device_extensions_to_enable(physical_device);
+ create_device_info.enabledExtensionCount = enabled_extensions.size();
+ create_device_info.ppEnabledExtensionNames = enabled_extensions.data();
if (vkCreateDevice(physical_device, &create_device_info, nullptr, &device) != VK_SUCCESS) {
return Error::from_string_literal("Logical device creation failed");
@@ -259,7 +281,7 @@ ErrorOr<VulkanContext> create_vulkan_context(Optional<GpuDeviceIdentity> const&
.logical_device = logical_device,
.graphics_queue = graphics_queue,
.graphics_queue_family = graphics_queue_family,
- .enabled_device_extensions = s_enabled_device_extensions,
+ .enabled_device_extensions = device_extensions_to_enable(physical_device),
.can_sample_through_ycbcr_conversion = can_sample_through_ycbcr_conversion,
#ifdef USE_VULKAN_DMABUF_IMAGES
.command_pool = command_pool,
diff --git a/Services/Compositor/SkiaBackendContext.cpp b/Services/Compositor/SkiaBackendContext.cpp
index 199a5c35b33..740b1037138 100644
--- a/Services/Compositor/SkiaBackendContext.cpp
+++ b/Services/Compositor/SkiaBackendContext.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/AnyOf.h>
#include <AK/NeverDestroyed.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefPtr.h>
@@ -27,9 +28,12 @@
#ifdef USE_VULKAN
# include <Compositor/SkiaVulkanMemoryAllocator.h>
+# include <gpu/MutableTextureState.h>
# include <gpu/ganesh/vk/GrVkDirectContext.h>
# include <gpu/vk/VulkanBackendContext.h>
# include <gpu/vk/VulkanExtensions.h>
+# include <gpu/vk/VulkanMutableTextureState.h>
+# include <string.h>
template<>
constexpr bool AllocatedWithSystemAllocator<skgpu::VulkanExtensions> = true;
@@ -80,10 +84,38 @@ void SkiaBackendContext::check_async_work_completion()
context->checkAsyncWorkCompletion();
}
+#ifdef USE_VULKAN
+// The external queue family only reaches the same device and driver. Anything else takes the foreign one, which not
+// every device has.
+static u32 queue_family_receiving_shared_images(VulkanContext const& vulkan_context)
+{
+ auto has_foreign_queue_family = any_of(vulkan_context.enabled_device_extensions, [](char const* extension) {
+ return strcmp(extension, VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME) == 0;
+ });
+ return !vulkan_context.is_preferred_device && has_foreign_queue_family ? VK_QUEUE_FAMILY_FOREIGN_EXT : VK_QUEUE_FAMILY_EXTERNAL;
+}
+#endif
+
+static void flush_surface(SkiaBackendContext& backend_context, PaintingSurface& surface, GrFlushInfo const& flush_info)
+{
+ auto& context = *backend_context.sk_context();
+#ifdef USE_VULKAN
+ // NB: A driver can keep an image compressed in memory that only it knows about, even when the image's layout has
+ // no compression. It only writes the actual pixels out once the image is released to another owner, so without
+ // this, whoever else reads the shared image's memory sees the compressed data.
+ if (surface.wraps_shared_image()) {
+ auto const handed_over_state = skgpu::MutableTextureStates::MakeVulkan(VK_IMAGE_LAYOUT_GENERAL, queue_family_receiving_shared_images(backend_context.vulkan_context()));
+ context.flush(&surface.sk_surface(), flush_info, &handed_over_state);
+ return;
+ }
+#endif
+ context.flush(&surface.sk_surface(), backend_access_for(surface), flush_info);
+}
+
void SkiaBackendContext::flush_and_submit(PaintingSurface& surface)
{
auto& context = *sk_context();
- context.flush(&surface.sk_surface(), backend_access_for(surface), {});
+ flush_surface(*this, surface, {});
context.submit(GrSyncCpu::kYes);
perform_post_flush_cleanup();
@@ -95,7 +127,7 @@ void SkiaBackendContext::flush_and_submit_async(PaintingSurface& surface, Functi
GrFlushInfo flush_info {};
flush_info.fFinishedProc = invoke_async_flush_callback;
flush_info.fFinishedContext = new Function<void()>(move(callback));
- context.flush(&surface.sk_surface(), backend_access_for(surface), flush_info);
+ flush_surface(*this, surface, flush_info);
VERIFY(context.submit(GrSyncCpu::kNo));
perform_post_flush_cleanup();