Truly independent web browser
LibGfx: Allow importing dma-bufs as Vulkan YCbCr images in Skia
Zaggy1024ed8971d9c9be
diff --git a/Libraries/LibGfx/CMakeLists.txt b/Libraries/LibGfx/CMakeLists.txtindex dde0b47a654..28bf16d998e 100644--- a/Libraries/LibGfx/CMakeLists.txt+++ b/Libraries/LibGfx/CMakeLists.txt@@ -69,7 +69,7 @@ if (HAS_DIRECTX)endif()if (HAS_VULKAN)- list(APPEND SOURCES SkiaVulkanMemoryAllocator.cpp VulkanContext.cpp VulkanImage.cpp)+ list(APPEND SOURCES ImportedYCbCrVulkanImage.cpp SkiaVulkanMemoryAllocator.cpp VulkanContext.cpp VulkanImage.cpp)endif()if (HAS_FONTCONFIG)diff --git a/Libraries/LibGfx/ImportedYCbCrVulkanImage.cpp b/Libraries/LibGfx/ImportedYCbCrVulkanImage.cppnew file mode 100644index 00000000000..8880a331b94--- /dev/null+++ b/Libraries/LibGfx/ImportedYCbCrVulkanImage.cpp@@ -0,0 +1,343 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#ifdef USE_VULKAN_DMABUF_IMAGES++# include <AK/Array.h>+# include <AK/BuiltinWrappers.h>+# include <AK/ScopeGuard.h>+# include <LibGfx/ImportedYCbCrVulkanImage.h>+# include <errno.h>+# include <fcntl.h>+# include <libdrm/drm_fourcc.h>+# include <unistd.h>++namespace Gfx {++static Optional<VkFormat> ycbcr_vulkan_format_for_drm_format(u32 drm_format)+{+ switch (drm_format) {+ case DRM_FORMAT_NV12:+ return VK_FORMAT_G8_B8R8_2PLANE_420_UNORM;+ case DRM_FORMAT_NV16:+ return VK_FORMAT_G8_B8R8_2PLANE_422_UNORM;+ case DRM_FORMAT_P010:+ return VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16;+ case DRM_FORMAT_P210:+ return VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16;+ case DRM_FORMAT_P012:+ return VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16;+ case DRM_FORMAT_P016:+ return VK_FORMAT_G16_B16R16_2PLANE_420_UNORM;+ case DRM_FORMAT_YUV420:+ return VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM;+ case DRM_FORMAT_YUV422:+ return VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM;+ case DRM_FORMAT_YUV444:+ return VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM;+ default:+ return {};+ }+}++static Optional<VkDrmFormatModifierPropertiesEXT> drm_format_modifier_properties(VkPhysicalDevice physical_device, VkFormat format, u64 modifier)+{+ VkDrmFormatModifierPropertiesListEXT modifier_list {};+ modifier_list.sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT;+ VkFormatProperties2 format_properties {};+ format_properties.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2;+ format_properties.pNext = &modifier_list;+ vkGetPhysicalDeviceFormatProperties2(physical_device, format, &format_properties);++ Vector<VkDrmFormatModifierPropertiesEXT> modifiers;+ modifiers.resize(modifier_list.drmFormatModifierCount);+ modifier_list.pDrmFormatModifierProperties = modifiers.data();+ vkGetPhysicalDeviceFormatProperties2(physical_device, format, &format_properties);++ for (size_t i = 0; i < modifier_list.drmFormatModifierCount; i++) {+ if (modifiers[i].drmFormatModifier == modifier)+ return modifiers[i];+ }+ return {};+}++static ErrorOr<VkExternalMemoryFeatureFlags> external_memory_features_for_import(VulkanContext const& context, VkFormat format, u64 modifier, VkImageCreateFlags flags, IntSize size)+{+ VkPhysicalDeviceImageDrmFormatModifierInfoEXT modifier_info {};+ modifier_info.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT;+ modifier_info.drmFormatModifier = modifier;+ modifier_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;++ VkPhysicalDeviceExternalImageFormatInfo external_info {};+ external_info.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO;+ external_info.pNext = &modifier_info;+ external_info.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;++ VkPhysicalDeviceImageFormatInfo2 format_info {};+ format_info.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2;+ format_info.pNext = &external_info;+ format_info.format = format;+ format_info.type = VK_IMAGE_TYPE_2D;+ format_info.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;+ format_info.usage = ImportedYCbCrVulkanImage::USAGE;+ format_info.flags = flags;++ VkExternalImageFormatProperties external_properties {};+ external_properties.sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES;+ VkImageFormatProperties2 properties {};+ properties.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2;+ properties.pNext = &external_properties;+ if (vkGetPhysicalDeviceImageFormatProperties2(context.physical_device, &format_info, &properties) != VK_SUCCESS)+ return Error::from_string_literal("The Vulkan device cannot create an image of the dma-buf's format and modifier");++ auto const& maximum_extent = properties.imageFormatProperties.maxExtent;+ if (static_cast<u32>(size.width()) > maximum_extent.width || static_cast<u32>(size.height()) > maximum_extent.height)+ return Error::from_string_literal("The dma-buf image is larger than the Vulkan device allows");++ auto features = external_properties.externalMemoryProperties.externalMemoryFeatures;+ if ((features & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT) == 0)+ return Error::from_string_literal("The Vulkan device cannot import the dma-buf's format and modifier");+ return features;+}++static ErrorOr<ImportedYCbCrVulkanImage::ImportedMemory> import_dma_buf_memory(VulkanContext const& context, int dma_buf_fd, VkMemoryRequirements const& requirements, VkImage dedicated_image)+{+ // The dma-buf comes from another process, so it must be shown to be large enough to back the image.+ auto dma_buf_size = lseek(dma_buf_fd, 0, SEEK_END);+ if (dma_buf_size < 0)+ return Error::from_errno(errno);+ if (static_cast<u64>(dma_buf_size) < requirements.size)+ return Error::from_string_literal("The dma-buf is smaller than the image it is said to hold");++ VkMemoryFdPropertiesKHR fd_properties {};+ fd_properties.sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR;+ if (context.ext_procs.get_memory_fd_properties(context.logical_device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, dma_buf_fd, &fd_properties) != VK_SUCCESS)+ return Error::from_string_literal("Could not query the dma-buf's memory properties");+ auto memory_type_bits = requirements.memoryTypeBits & fd_properties.memoryTypeBits;+ if (memory_type_bits == 0)+ return Error::from_string_literal("The dma-buf's memory cannot back the image");++ auto fd = fcntl(dma_buf_fd, F_DUPFD_CLOEXEC, 0);+ if (fd < 0)+ return Error::from_errno(errno);+ ArmedScopeGuard close_fd = [&] { close(fd); };++ VkMemoryDedicatedAllocateInfo dedicated_info {};+ dedicated_info.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;+ dedicated_info.image = dedicated_image;++ VkImportMemoryFdInfoKHR import_info {};+ import_info.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR;+ if (dedicated_image != VK_NULL_HANDLE)+ import_info.pNext = &dedicated_info;+ import_info.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;+ import_info.fd = fd;++ VkMemoryAllocateInfo allocate_info {};+ allocate_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;+ allocate_info.pNext = &import_info;+ allocate_info.allocationSize = requirements.size;+ allocate_info.memoryTypeIndex = count_trailing_zeroes(memory_type_bits);++ VkDeviceMemory memory = VK_NULL_HANDLE;+ if (vkAllocateMemory(context.logical_device, &allocate_info, nullptr, &memory) != VK_SUCCESS)+ return Error::from_string_literal("Could not import the dma-buf's memory");+ // A successful import takes ownership of the descriptor.+ close_fd.disarm();+ return ImportedYCbCrVulkanImage::ImportedMemory { memory, requirements.size };+}++enum class PlaneLayoutSource {+ ChosenByDriver,+ GivenExplicitly,+};++static ErrorOr<VkImage> create_image(VulkanContext const& context, VkFormat format, VkImageCreateFlags flags, LinuxDmaBufHandle const& dma_buf, PlaneLayoutSource layout_source)+{+ VkImageDrmFormatModifierListCreateInfoEXT modifier_list {};+ modifier_list.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT;+ modifier_list.drmFormatModifierCount = 1;+ modifier_list.pDrmFormatModifiers = &dma_buf.modifier;++ Array<VkSubresourceLayout, LinuxDmaBufHandle::MAXIMUM_PLANE_COUNT> plane_layouts {};+ for (size_t plane_index = 0; plane_index < dma_buf.planes.size(); plane_index++) {+ plane_layouts[plane_index].offset = dma_buf.planes[plane_index].offset;+ plane_layouts[plane_index].rowPitch = dma_buf.planes[plane_index].pitch;+ }+ VkImageDrmFormatModifierExplicitCreateInfoEXT explicit_modifier {};+ explicit_modifier.sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT;+ explicit_modifier.drmFormatModifier = dma_buf.modifier;+ explicit_modifier.drmFormatModifierPlaneCount = dma_buf.planes.size();+ explicit_modifier.pPlaneLayouts = plane_layouts.data();++ VkExternalMemoryImageCreateInfo external_info {};+ external_info.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;+ if (layout_source == PlaneLayoutSource::ChosenByDriver)+ external_info.pNext = &modifier_list;+ else+ external_info.pNext = &explicit_modifier;+ external_info.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;++ VkImageCreateInfo image_info {};+ image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;+ image_info.pNext = &external_info;+ image_info.flags = flags;+ image_info.imageType = VK_IMAGE_TYPE_2D;+ image_info.format = format;+ image_info.extent = { static_cast<u32>(dma_buf.size.width()), static_cast<u32>(dma_buf.size.height()), 1 };+ image_info.mipLevels = 1;+ image_info.arrayLayers = 1;+ image_info.samples = VK_SAMPLE_COUNT_1_BIT;+ image_info.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;+ image_info.usage = ImportedYCbCrVulkanImage::USAGE;+ image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;+ image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;++ VkImage image = VK_NULL_HANDLE;+ if (vkCreateImage(context.logical_device, &image_info, nullptr, &image) != VK_SUCCESS)+ return Error::from_string_literal("Could not create an image to import the dma-buf into");+ return image;+}++static bool image_is_laid_out_as_exported(VkDevice device, VkImage image, LinuxDmaBufHandle const& dma_buf)+{+ for (size_t plane_index = 0; plane_index < dma_buf.planes.size(); plane_index++) {+ VkImageSubresource subresource {};+ subresource.aspectMask = VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT << plane_index;+ VkSubresourceLayout layout {};+ vkGetImageSubresourceLayout(device, image, &subresource, &layout);+ auto const& plane = dma_buf.planes[plane_index];+ if (layout.offset != plane.offset || layout.rowPitch != plane.pitch)+ return false;+ }+ return true;+}++// A modifier fixes how a device lays out an image of a given size and format, so the driver's own layout matches the+// exporter's whenever both images were made alike. Explicit plane layouts are the only way to describe one that does+// not match, but NVIDIA's driver (615.71) misplaces the planes of an image given them under a tiled modifier, so they+// are given only for linear images, where the pitch is the exporter's to choose.+static ErrorOr<VkImage> create_image_laid_out_as_exported(VulkanContext const& context, VkFormat format, VkImageCreateFlags flags, LinuxDmaBufHandle const& dma_buf)+{+ auto image = TRY(create_image(context, format, flags, dma_buf, PlaneLayoutSource::ChosenByDriver));+ if (image_is_laid_out_as_exported(context.logical_device, image, dma_buf))+ return image;+ vkDestroyImage(context.logical_device, image, nullptr);++ if (dma_buf.modifier != DRM_FORMAT_MOD_LINEAR)+ return Error::from_string_literal("The Vulkan device lays out the dma-buf's tiled image differently from its exporter");+ image = TRY(create_image(context, format, flags, dma_buf, PlaneLayoutSource::GivenExplicitly));+ if (image_is_laid_out_as_exported(context.logical_device, image, dma_buf))+ return image;+ vkDestroyImage(context.logical_device, image, nullptr);+ return Error::from_string_literal("The Vulkan device cannot lay out the dma-buf's image as its exporter did");+}++ErrorOr<NonnullOwnPtr<ImportedYCbCrVulkanImage>> ImportedYCbCrVulkanImage::import_dma_buf(VulkanContext const& context, LinuxDmaBufHandle const& dma_buf)+{+ if (!context.can_sample_through_ycbcr_conversion)+ return Error::from_string_literal("The Vulkan device cannot sample through a YCbCr conversion");+ if (dma_buf.size.is_empty())+ return Error::from_string_literal("The dma-buf image is empty");+ auto format = ycbcr_vulkan_format_for_drm_format(dma_buf.drm_format);+ if (!format.has_value())+ return Error::from_string_literal("The dma-buf's format has no YCbCr Vulkan format");++ auto modifier_properties = drm_format_modifier_properties(context.physical_device, *format, dma_buf.modifier);+ if (!modifier_properties.has_value())+ return Error::from_string_literal("The Vulkan device does not support the dma-buf's modifier for its format");+ if (dma_buf.planes.size() != modifier_properties->drmFormatModifierPlaneCount)+ return Error::from_string_literal("The dma-buf image has the wrong number of planes for its format and modifier");+ auto format_features = modifier_properties->drmFormatModifierTilingFeatures;+ if ((format_features & REQUIRED_FORMAT_FEATURES) != REQUIRED_FORMAT_FEATURES)+ return Error::from_string_literal("The Vulkan device cannot sample the dma-buf's format and modifier");++ // Planes in one dma-buf share one memory, while planes in separate dma-bufs need an image that binds them apart.+ auto is_disjoint = false;+ for (auto const& plane : dma_buf.planes) {+ if (plane.object_index >= dma_buf.objects.size())+ return Error::from_string_literal("A dma-buf image plane refers to an object it does not have");+ if (plane.object_index != 0)+ is_disjoint = true;+ }+ if (is_disjoint && (format_features & VK_FORMAT_FEATURE_DISJOINT_BIT) == 0)+ return Error::from_string_literal("The Vulkan device cannot bind the dma-buf image's planes apart");+ VkImageCreateFlags flags = is_disjoint ? VK_IMAGE_CREATE_DISJOINT_BIT : 0;++ auto external_memory_features = TRY(external_memory_features_for_import(context, *format, dma_buf.modifier, flags, dma_buf.size));+ auto requires_dedicated_memory = (external_memory_features & VK_EXTERNAL_MEMORY_FEATURE_DEDICATED_ONLY_BIT) != 0;+ if (is_disjoint && requires_dedicated_memory)+ return Error::from_string_literal("The Vulkan device can import the dma-buf image only into dedicated memory, which its separate planes cannot have");++ auto imported = adopt_own(*new ImportedYCbCrVulkanImage(context.logical_device));+ imported->m_format = *format;+ imported->m_format_features = format_features;+ imported->m_is_disjoint = is_disjoint;++ imported->m_image = TRY(create_image_laid_out_as_exported(context, *format, flags, dma_buf));++ if (!is_disjoint) {+ VkImageMemoryRequirementsInfo2 requirements_info {};+ requirements_info.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;+ requirements_info.image = imported->m_image;+ VkMemoryDedicatedRequirements dedicated_requirements {};+ dedicated_requirements.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS;+ VkMemoryRequirements2 requirements {};+ requirements.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;+ requirements.pNext = &dedicated_requirements;+ vkGetImageMemoryRequirements2(context.logical_device, &requirements_info, &requirements);++ auto uses_dedicated_memory = requires_dedicated_memory || dedicated_requirements.requiresDedicatedAllocation || dedicated_requirements.prefersDedicatedAllocation;+ auto memory = TRY(import_dma_buf_memory(context, dma_buf.objects[0].fd(), requirements.memoryRequirements, uses_dedicated_memory ? imported->m_image : VK_NULL_HANDLE));+ imported->m_memories.append(memory);+ if (vkBindImageMemory(context.logical_device, imported->m_image, memory.memory, 0) != VK_SUCCESS)+ return Error::from_string_literal("Could not bind the dma-buf's memory to the image");+ return imported;+ }++ Array<VkBindImagePlaneMemoryInfo, LinuxDmaBufHandle::MAXIMUM_PLANE_COUNT> plane_bindings {};+ Array<VkBindImageMemoryInfo, LinuxDmaBufHandle::MAXIMUM_PLANE_COUNT> bindings {};+ for (size_t plane_index = 0; plane_index < dma_buf.planes.size(); plane_index++) {+ auto aspect = static_cast<VkImageAspectFlagBits>(VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT << plane_index);++ VkImagePlaneMemoryRequirementsInfo plane_requirements_info {};+ plane_requirements_info.sType = VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO;+ plane_requirements_info.planeAspect = aspect;+ VkImageMemoryRequirementsInfo2 requirements_info {};+ requirements_info.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;+ requirements_info.pNext = &plane_requirements_info;+ requirements_info.image = imported->m_image;+ VkMemoryRequirements2 requirements {};+ requirements.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;+ vkGetImageMemoryRequirements2(context.logical_device, &requirements_info, &requirements);++ auto const& plane = dma_buf.planes[plane_index];+ auto memory = TRY(import_dma_buf_memory(context, dma_buf.objects[plane.object_index].fd(), requirements.memoryRequirements, VK_NULL_HANDLE));+ imported->m_memories.append(memory);++ plane_bindings[plane_index].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO;+ plane_bindings[plane_index].planeAspect = aspect;+ bindings[plane_index].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;+ bindings[plane_index].pNext = &plane_bindings[plane_index];+ bindings[plane_index].image = imported->m_image;+ bindings[plane_index].memory = memory.memory;+ }+ if (vkBindImageMemory2(context.logical_device, dma_buf.planes.size(), bindings.data()) != VK_SUCCESS)+ return Error::from_string_literal("Could not bind the dma-buf's planes to the image");+ return imported;+}++ImportedYCbCrVulkanImage::~ImportedYCbCrVulkanImage()+{+ if (m_image != VK_NULL_HANDLE)+ vkDestroyImage(m_device, m_image, nullptr);+ for (auto const& memory : m_memories)+ vkFreeMemory(m_device, memory.memory, nullptr);+}++}++#endifdiff --git a/Libraries/LibGfx/ImportedYCbCrVulkanImage.h b/Libraries/LibGfx/ImportedYCbCrVulkanImage.hnew file mode 100644index 00000000000..0e028a5c839--- /dev/null+++ b/Libraries/LibGfx/ImportedYCbCrVulkanImage.h@@ -0,0 +1,68 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#ifdef USE_VULKAN_DMABUF_IMAGES++# include <AK/Noncopyable.h>+# include <AK/NonnullOwnPtr.h>+# include <AK/Vector.h>+# include <LibGfx/LinuxDmaBufHandle.h>+# include <LibGfx/VulkanContext.h>++namespace Gfx {++// A YCbCr image that another process decoded into dma-bufs, imported to be sampled through a YCbCr conversion. The+// imported memory keeps the dma-bufs alive for as long as the image exists.+class ImportedYCbCrVulkanImage {+ AK_MAKE_NONCOPYABLE(ImportedYCbCrVulkanImage);+ AK_MAKE_NONMOVABLE(ImportedYCbCrVulkanImage);++public:+ AK_ALLOC_WITH_KMALLOC;++ struct ImportedMemory {+ VkDeviceMemory memory { VK_NULL_HANDLE };+ VkDeviceSize size { 0 };+ };++ // Skia wraps an image only if it can also copy to and from it.+ static constexpr VkImageUsageFlags USAGE = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;+ // What the format must support under a modifier for an image to be imported with it, which a process exporting+ // images to import here can choose its modifiers by.+ static constexpr VkFormatFeatureFlags REQUIRED_FORMAT_FEATURES = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT;++ static ErrorOr<NonnullOwnPtr<ImportedYCbCrVulkanImage>> import_dma_buf(VulkanContext const&, LinuxDmaBufHandle const&);++ ~ImportedYCbCrVulkanImage();++ VkImage image() const { return m_image; }+ VkFormat format() const { return m_format; }+ // What the format can do under the dma-buf's modifier, which limits the chroma siting and filtering that a+ // conversion sampling it may use.+ VkFormatFeatureFlags format_features() const { return m_format_features; }+ // One memory per plane when the planes lie in separate dma-bufs, and one for the whole image otherwise.+ ReadonlySpan<ImportedMemory> memories() const { return m_memories; }+ bool is_disjoint() const { return m_is_disjoint; }++private:+ explicit ImportedYCbCrVulkanImage(VkDevice device)+ : m_device(device)+ {+ }++ VkDevice m_device { VK_NULL_HANDLE };+ VkImage m_image { VK_NULL_HANDLE };+ VkFormat m_format { VK_FORMAT_UNDEFINED };+ VkFormatFeatureFlags m_format_features { 0 };+ Vector<ImportedMemory, LinuxDmaBufHandle::MAXIMUM_PLANE_COUNT> m_memories;+ bool m_is_disjoint { false };+};++}++#endifdiff --git a/Libraries/LibGfx/SkiaBackendContext.cpp b/Libraries/LibGfx/SkiaBackendContext.cppindex 1907c0eb1bc..46be8d07047 100644--- a/Libraries/LibGfx/SkiaBackendContext.cpp+++ b/Libraries/LibGfx/SkiaBackendContext.cpp@@ -277,8 +277,18 @@ RefPtr<SkiaBackendContext> SkiaBackendContext::create_vulkan_context(VulkanConte};auto extensions = make<skgpu::VulkanExtensions>();+ extensions->init(backend_context.fGetProc, vulkan_context.instance, vulkan_context.physical_device, 0, nullptr, vulkan_context.enabled_device_extensions.size(), vulkan_context.enabled_device_extensions.data());backend_context.fVkExtensions = extensions.ptr();+ // Skia samples through a YCbCr conversion only once it knows the device has that enabled.+ VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcr_conversion_features {};+ ycbcr_conversion_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;+ ycbcr_conversion_features.samplerYcbcrConversion = vulkan_context.can_sample_through_ycbcr_conversion ? VK_TRUE : VK_FALSE;+ VkPhysicalDeviceFeatures2 features {};+ features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;+ features.pNext = &ycbcr_conversion_features;+ backend_context.fDeviceFeatures2 = &features;+backend_context.fMemoryAllocator = create_skia_vulkan_memory_allocator(vulkan_context);VERIFY(backend_context.fMemoryAllocator);diff --git a/Libraries/LibGfx/VulkanContext.cpp b/Libraries/LibGfx/VulkanContext.cppindex 9e0bb87588d..898e30dcddc 100644--- a/Libraries/LibGfx/VulkanContext.cpp+++ b/Libraries/LibGfx/VulkanContext.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Array.h>#include <AK/Debug.h>#include <AK/Format.h>#include <AK/Vector.h>@@ -80,7 +81,29 @@ static ErrorOr<VkPhysicalDevice> pick_physical_device(VkInstance instance)VERIFY_NOT_REACHED();}-static ErrorOr<VkDevice> create_logical_device(VkPhysicalDevice physical_device, uint32_t* graphics_queue_family)+#ifdef USE_VULKAN_DMABUF_IMAGES+static constexpr Array<char const*, 4> 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,+};+#else+static constexpr Array<char const*, 0> s_enabled_device_extensions;+#endif++static bool device_can_sample_through_ycbcr_conversion(VkPhysicalDevice physical_device)+{+ VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcr_conversion_features {};+ ycbcr_conversion_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;+ VkPhysicalDeviceFeatures2 features {};+ features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;+ features.pNext = &ycbcr_conversion_features;+ vkGetPhysicalDeviceFeatures2(physical_device, &features);+ return ycbcr_conversion_features.samplerYcbcrConversion == VK_TRUE;+}++static ErrorOr<VkDevice> create_logical_device(VkPhysicalDevice physical_device, uint32_t* graphics_queue_family, bool enable_sampling_through_ycbcr_conversion){VkDevice device;@@ -113,24 +136,21 @@ static ErrorOr<VkDevice> create_logical_device(VkPhysicalDevice physical_device,float const queue_priority = 1.0f;queue_create_info.pQueuePriorities = &queue_priority;- VkPhysicalDeviceFeatures deviceFeatures {};-#ifdef USE_VULKAN_DMABUF_IMAGES- Array<char const*, 4> 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,- };-#else- Array<char const*, 0> device_extensions;-#endif+ // Decoded video is sampled in place, converting from YCbCr as it is read.+ VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcr_conversion_features {};+ ycbcr_conversion_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES;+ ycbcr_conversion_features.samplerYcbcrConversion = enable_sampling_through_ycbcr_conversion ? VK_TRUE : VK_FALSE;+ VkPhysicalDeviceFeatures2 features {};+ features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;+ features.pNext = &ycbcr_conversion_features;+VkDeviceCreateInfo create_device_info {};create_device_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;+ create_device_info.pNext = &features;create_device_info.pQueueCreateInfos = &queue_create_info;create_device_info.queueCreateInfoCount = 1;- create_device_info.pEnabledFeatures = &deviceFeatures;- create_device_info.enabledExtensionCount = device_extensions.size();- create_device_info.ppEnabledExtensionNames = device_extensions.data();+ create_device_info.enabledExtensionCount = s_enabled_device_extensions.size();+ create_device_info.ppEnabledExtensionNames = s_enabled_device_extensions.data();if (vkCreateDevice(physical_device, &create_device_info, nullptr, &device) != VK_SUCCESS) {return Error::from_string_literal("Logical device creation failed");@@ -199,7 +219,8 @@ ErrorOr<VulkanContext> create_vulkan_context()auto* physical_device = TRY(pick_physical_device(instance));uint32_t graphics_queue_family = 0;- auto* logical_device = TRY(create_logical_device(physical_device, &graphics_queue_family));+ auto can_sample_through_ycbcr_conversion = device_can_sample_through_ycbcr_conversion(physical_device);+ auto* logical_device = TRY(create_logical_device(physical_device, &graphics_queue_family, can_sample_through_ycbcr_conversion));VkQueue graphics_queue;vkGetDeviceQueue(logical_device, graphics_queue_family, 0, &graphics_queue);@@ -211,6 +232,10 @@ ErrorOr<VulkanContext> create_vulkan_context()if (pfn_vk_get_memory_fd_khr == nullptr) {return Error::from_string_literal("vkGetMemoryFdKHR unavailable");}+ auto pfn_vk_get_memory_fd_properties_khr = reinterpret_cast<PFN_vkGetMemoryFdPropertiesKHR>(vkGetDeviceProcAddr(logical_device, "vkGetMemoryFdPropertiesKHR"));+ if (pfn_vk_get_memory_fd_properties_khr == nullptr) {+ return Error::from_string_literal("vkGetMemoryFdPropertiesKHR unavailable");+ }auto pfn_vk_get_image_drm_format_modifier_properties_khr = reinterpret_cast<PFN_vkGetImageDrmFormatModifierPropertiesEXT>(vkGetDeviceProcAddr(logical_device, "vkGetImageDrmFormatModifierPropertiesEXT"));if (pfn_vk_get_image_drm_format_modifier_properties_khr == nullptr) {return Error::from_string_literal("vkGetImageDrmFormatModifierPropertiesEXT unavailable");@@ -225,11 +250,14 @@ ErrorOr<VulkanContext> create_vulkan_context().logical_device = logical_device,.graphics_queue = graphics_queue,.graphics_queue_family = graphics_queue_family,+ .enabled_device_extensions = s_enabled_device_extensions,+ .can_sample_through_ycbcr_conversion = can_sample_through_ycbcr_conversion,#ifdef USE_VULKAN_DMABUF_IMAGES.command_pool = command_pool,.command_buffer = command_buffer,.ext_procs = {.get_memory_fd = pfn_vk_get_memory_fd_khr,+ .get_memory_fd_properties = pfn_vk_get_memory_fd_properties_khr,.get_image_drm_format_modifier_properties = pfn_vk_get_image_drm_format_modifier_properties_khr,},#endifdiff --git a/Libraries/LibGfx/VulkanContext.h b/Libraries/LibGfx/VulkanContext.hindex 9b251ae256a..246236d7634 100644--- a/Libraries/LibGfx/VulkanContext.h+++ b/Libraries/LibGfx/VulkanContext.h@@ -9,6 +9,7 @@#ifdef USE_VULKAN# include <AK/Error.h>+# include <AK/Span.h># include <LibGfx/GpuDeviceIdentity.h># include <vulkan/vulkan.h>@@ -22,12 +23,15 @@ struct VulkanContext {VkDevice logical_device { VK_NULL_HANDLE };VkQueue graphics_queue { VK_NULL_HANDLE };uint32_t graphics_queue_family { 0 };+ ReadonlySpan<char const*> enabled_device_extensions;+ bool can_sample_through_ycbcr_conversion { false };# ifdef USE_VULKAN_DMABUF_IMAGESVkCommandPool command_pool { VK_NULL_HANDLE };VkCommandBuffer command_buffer { VK_NULL_HANDLE };struct{PFN_vkGetMemoryFdKHR get_memory_fd { nullptr };+ PFN_vkGetMemoryFdPropertiesKHR get_memory_fd_properties { nullptr };PFN_vkGetImageDrmFormatModifierPropertiesEXT get_image_drm_format_modifier_properties { nullptr };} ext_procs;# endif