Skip to content

Truly independent web browser

LibGfx: Support dma-bufs in layouts that cannot be mapped

Andreas Kling65669d12b192
diff --git a/Libraries/LibGfx/SharedImageBuffer.cpp b/Libraries/LibGfx/SharedImageBuffer.cpp
index 78cffc63d10..66a5a1ab9e7 100644
--- a/Libraries/LibGfx/SharedImageBuffer.cpp
+++ b/Libraries/LibGfx/SharedImageBuffer.cpp
@@ -60,7 +60,7 @@ SharedImageBuffer::SharedImageBuffer(NonnullRefPtr<Bitmap> bitmap)
}
# ifdef USE_VULKAN_DMABUF_IMAGES
-SharedImageBuffer::SharedImageBuffer(NonnullRefPtr<Bitmap> bitmap, LinuxDmaBufHandle&& dmabuf)
+SharedImageBuffer::SharedImageBuffer(RefPtr<Bitmap> bitmap, LinuxDmaBufHandle&& dmabuf)
: m_linux_dmabuf_handle(move(dmabuf))
, m_bitmap(move(bitmap))
{
@@ -99,6 +99,9 @@ SharedImageBuffer SharedImageBuffer::import_from_shared_image(SharedImage shared
},
[](LinuxDmaBufHandle& dmabuf) -> SharedImageBuffer {
# ifdef USE_VULKAN_DMABUF_IMAGES
+ // NB: Only a linear layout can be read through a mapping. The pixels of a tiled one are only for the GPU.
+ if (dmabuf.modifier != DRM_FORMAT_MOD_LINEAR)
+ return SharedImageBuffer(nullptr, move(dmabuf));
auto bitmap = create_bitmap_from_linux_dmabuf(dmabuf);
return SharedImageBuffer(move(bitmap), move(dmabuf));
# else
diff --git a/Libraries/LibGfx/SharedImageBuffer.h b/Libraries/LibGfx/SharedImageBuffer.h
index 6e3fe63a7be..154409a38cd 100644
--- a/Libraries/LibGfx/SharedImageBuffer.h
+++ b/Libraries/LibGfx/SharedImageBuffer.h
@@ -55,7 +55,7 @@ private:
#else
explicit SharedImageBuffer(NonnullRefPtr<Bitmap>);
# ifdef USE_VULKAN_DMABUF_IMAGES
- SharedImageBuffer(NonnullRefPtr<Bitmap>, LinuxDmaBufHandle&&);
+ SharedImageBuffer(RefPtr<Bitmap>, LinuxDmaBufHandle&&);
Optional<LinuxDmaBufHandle> m_linux_dmabuf_handle;
# endif
# ifdef USE_DIRECTX
diff --git a/Libraries/LibGfx/VulkanImage.cpp b/Libraries/LibGfx/VulkanImage.cpp
index ad90beb18ec..bb4eb7fbe86 100644
--- a/Libraries/LibGfx/VulkanImage.cpp
+++ b/Libraries/LibGfx/VulkanImage.cpp
@@ -8,8 +8,13 @@
# include <AK/Array.h>
# include <AK/Format.h>
+# include <AK/ScopeGuard.h>
# include <AK/Vector.h>
+# include <LibGfx/Bitmap.h>
# include <LibGfx/VulkanImage.h>
+# include <fcntl.h>
+# include <string.h>
+# include <unistd.h>
namespace Gfx {
@@ -96,7 +101,53 @@ int VulkanImage::get_dma_buf_fd() const
return fd;
}
-ErrorOr<NonnullRefPtr<VulkanImage>> create_shared_vulkan_image(VulkanContext const& context, uint32_t width, uint32_t height, VkFormat format, ReadonlySpan<uint64_t> modifiers)
+static constexpr VkImageUsageFlags shared_image_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
+
+static bool supports_dmabuf_image(VkPhysicalDevice physical_device, VkFormat format, uint64_t modifier, VkImageUsageFlags usage, VkExternalMemoryFeatureFlags required_features)
+{
+ VkPhysicalDeviceImageDrmFormatModifierInfoEXT modifier_info = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_DRM_FORMAT_MODIFIER_INFO_EXT,
+ .pNext = nullptr,
+ .drmFormatModifier = modifier,
+ .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
+ .queueFamilyIndexCount = 0,
+ .pQueueFamilyIndices = nullptr,
+ };
+ VkPhysicalDeviceExternalImageFormatInfo external_image_info = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
+ .pNext = &modifier_info,
+ .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
+ };
+ VkPhysicalDeviceImageFormatInfo2 image_format_info = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
+ .pNext = &external_image_info,
+ .format = format,
+ .type = VK_IMAGE_TYPE_2D,
+ .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
+ .usage = usage,
+ .flags = 0,
+ };
+ VkExternalImageFormatProperties external_image_properties = {
+ .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES,
+ .pNext = nullptr,
+ .externalMemoryProperties = {},
+ };
+ VkImageFormatProperties2 image_format_properties = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
+ .pNext = &external_image_properties,
+ .imageFormatProperties = {},
+ };
+ if (vkGetPhysicalDeviceImageFormatProperties2(physical_device, &image_format_info, &image_format_properties) != VK_SUCCESS)
+ return false;
+ return (external_image_properties.externalMemoryProperties.externalMemoryFeatures & required_features) == required_features;
+}
+
+bool can_import_dmabuf_image(VkPhysicalDevice physical_device, VkFormat format, uint64_t modifier, VkImageUsageFlags usage)
+{
+ return supports_dmabuf_image(physical_device, format, modifier, usage, VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT);
+}
+
+Vector<uint64_t> renderable_drm_format_modifiers(VulkanContext const& context, VkFormat format)
{
VkDrmFormatModifierPropertiesListEXT format_mod_props_list = {};
format_mod_props_list.sType = VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT;
@@ -110,20 +161,31 @@ ErrorOr<NonnullRefPtr<VulkanImage>> create_shared_vulkan_image(VulkanContext con
format_mod_props_list.pDrmFormatModifierProperties = format_mod_props.data();
vkGetPhysicalDeviceFormatProperties2(context.physical_device, format, &format_props);
- // populate a list of all format modifiers that are both renderable, sampleable,
- // and accepted by the caller
Vector<uint64_t> format_mods;
for (VkDrmFormatModifierPropertiesEXT const& props : format_mod_props) {
VkFormatFeatureFlags required_features = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
- if ((props.drmFormatModifierTilingFeatures & required_features) == required_features && (props.drmFormatModifierPlaneCount == 1)) {
- if (modifiers.contains_slow(props.drmFormatModifier))
- format_mods.append(props.drmFormatModifier);
- }
+ if ((props.drmFormatModifierTilingFeatures & required_features) != required_features || props.drmFormatModifierPlaneCount != 1)
+ continue;
+ // NB: The image is shared as a dma-buf, which not every layout the device renders to can be.
+ if (supports_dmabuf_image(context.physical_device, format, props.drmFormatModifier, shared_image_usage, VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT))
+ format_mods.append(props.drmFormatModifier);
+ }
+ return format_mods;
+}
+
+ErrorOr<NonnullRefPtr<VulkanImage>> create_shared_vulkan_image(VulkanContext const& context, uint32_t width, uint32_t height, VkFormat format, ReadonlySpan<uint64_t> modifiers)
+{
+ // populate a list of all format modifiers that are both renderable, sampleable,
+ // and accepted by the caller
+ Vector<uint64_t> format_mods;
+ for (auto modifier : renderable_drm_format_modifiers(context, format)) {
+ if (modifiers.is_empty() || modifiers.contains_slow(modifier))
+ format_mods.append(modifier);
}
- // If the caller requested specific DRM modifiers and none are supported for a renderable image,
- // fail here so higher-level code can fall back to a different backing-store type.
- if (!modifiers.is_empty() && format_mods.is_empty())
+ // If no accepted DRM modifier supports a renderable image, fail here so higher-level
+ // code can fall back to a different backing-store type.
+ if (format_mods.is_empty())
return Error::from_string_literal("no supported DRM format modifiers for shared image");
NonnullRefPtr<VulkanImage> image = make_ref_counted<VulkanImage>(context);
@@ -154,7 +216,7 @@ ErrorOr<NonnullRefPtr<VulkanImage>> create_shared_vulkan_image(VulkanContext con
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
- .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
+ .usage = shared_image_usage,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.queueFamilyIndexCount = queue_families.size(),
.pQueueFamilyIndices = queue_families.data(),
@@ -231,6 +293,10 @@ ErrorOr<NonnullRefPtr<VulkanImage>> create_shared_vulkan_image(VulkanContext con
return Error::from_string_literal("image format modifier retrieval failed");
}
+ // NB: Our dma-bufs name a single plane at the start of its memory, as the importers expect.
+ if (subresource_layout.offset != 0)
+ return Error::from_string_literal("image plane does not start at the start of its memory");
+
// external APIs require general layout
VkImageLayout layout = VK_IMAGE_LAYOUT_GENERAL;
image->transition_layout(VK_IMAGE_LAYOUT_UNDEFINED, layout);
@@ -248,6 +314,254 @@ ErrorOr<NonnullRefPtr<VulkanImage>> create_shared_vulkan_image(VulkanContext con
return image;
}
+ErrorOr<ImportedDmaBufImage> import_linux_dmabuf_image(VkPhysicalDevice physical_device, VkDevice device, PFN_vkGetMemoryFdPropertiesKHR get_memory_fd_properties, LinuxDmaBufHandle const& dmabuf, VkFormat format, VkImageUsageFlags usage)
+{
+ if (dmabuf.objects.size() != 1 || dmabuf.planes.size() != 1 || dmabuf.size.is_empty())
+ return Error::from_string_literal("dma-buf is not a single-plane image");
+ if (!can_import_dmabuf_image(physical_device, format, dmabuf.modifier, usage))
+ return Error::from_string_literal("The device cannot import the dma-buf's layout");
+
+ ImportedDmaBufImage imported;
+ ArmedScopeGuard destroy_on_failure = [&] {
+ if (imported.image != VK_NULL_HANDLE)
+ vkDestroyImage(device, imported.image, nullptr);
+ if (imported.memory != VK_NULL_HANDLE)
+ vkFreeMemory(device, imported.memory, nullptr);
+ };
+
+ // The dma-buf names the exact layout of its pixels, so the image takes that layout rather than one the driver picks.
+ auto const& plane = dmabuf.planes.first();
+ VkSubresourceLayout plane_layout = {
+ .offset = plane.offset,
+ .size = 0,
+ .rowPitch = plane.pitch,
+ .arrayPitch = 0,
+ .depthPitch = 0,
+ };
+ VkImageDrmFormatModifierExplicitCreateInfoEXT explicit_modifier_info = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT,
+ .pNext = nullptr,
+ .drmFormatModifier = dmabuf.modifier,
+ .drmFormatModifierPlaneCount = 1,
+ .pPlaneLayouts = &plane_layout,
+ };
+ VkExternalMemoryImageCreateInfo external_memory_image_info = {
+ .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
+ .pNext = &explicit_modifier_info,
+ .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
+ };
+ VkImageCreateInfo image_info = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
+ .pNext = &external_memory_image_info,
+ .flags = 0,
+ .imageType = VK_IMAGE_TYPE_2D,
+ .format = format,
+ .extent = { .width = static_cast<uint32_t>(dmabuf.size.width()), .height = static_cast<uint32_t>(dmabuf.size.height()), .depth = 1 },
+ .mipLevels = 1,
+ .arrayLayers = 1,
+ .samples = VK_SAMPLE_COUNT_1_BIT,
+ .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
+ .usage = usage,
+ .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
+ .queueFamilyIndexCount = 0,
+ .pQueueFamilyIndices = nullptr,
+ .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
+ };
+ if (vkCreateImage(device, &image_info, nullptr, &imported.image) != VK_SUCCESS)
+ return Error::from_string_literal("dma-buf image creation failed");
+
+ VkMemoryRequirements memory_requirements;
+ vkGetImageMemoryRequirements(device, imported.image, &memory_requirements);
+ VkMemoryFdPropertiesKHR memory_fd_properties = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
+ .pNext = nullptr,
+ .memoryTypeBits = 0,
+ };
+ if (get_memory_fd_properties(device, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, dmabuf.objects.first().fd(), &memory_fd_properties) != VK_SUCCESS)
+ return Error::from_string_literal("dma-buf memory properties are unavailable");
+
+ VkPhysicalDeviceMemoryProperties memory_properties;
+ vkGetPhysicalDeviceMemoryProperties(physical_device, &memory_properties);
+ memory_requirements.memoryTypeBits &= memory_fd_properties.memoryTypeBits;
+ auto memory_type_index = find_memory_type_index(memory_properties, memory_requirements, 0);
+ if (memory_type_index == memory_properties.memoryTypeCount)
+ return Error::from_string_literal("dma-buf memory cannot be imported");
+
+ // NB: A successful import takes ownership of the descriptor it is given, so it is given a duplicate.
+ int imported_fd = fcntl(dmabuf.objects.first().fd(), F_DUPFD_CLOEXEC, 0);
+ if (imported_fd < 0)
+ return Error::from_errno(errno);
+ VkMemoryDedicatedAllocateInfo dedicated_allocate_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
+ .pNext = nullptr,
+ .image = imported.image,
+ .buffer = VK_NULL_HANDLE,
+ };
+ VkImportMemoryFdInfoKHR import_memory_info = {
+ .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
+ .pNext = &dedicated_allocate_info,
+ .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
+ .fd = imported_fd,
+ };
+ VkMemoryAllocateInfo allocate_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
+ .pNext = &import_memory_info,
+ .allocationSize = memory_requirements.size,
+ .memoryTypeIndex = memory_type_index,
+ };
+ if (vkAllocateMemory(device, &allocate_info, nullptr, &imported.memory) != VK_SUCCESS) {
+ ::close(imported_fd);
+ return Error::from_string_literal("dma-buf memory import failed");
+ }
+ if (vkBindImageMemory(device, imported.image, imported.memory, 0) != VK_SUCCESS)
+ return Error::from_string_literal("dma-buf memory binding failed");
+
+ destroy_on_failure.disarm();
+ return imported;
+}
+
+ErrorOr<NonnullRefPtr<Bitmap>> read_linux_dmabuf_into_bitmap(VulkanContext const& context, LinuxDmaBufHandle const& dmabuf)
+{
+ if (dmabuf.drm_format != DRM_FORMAT_ARGB8888)
+ return Error::from_string_literal("dma-buf is not a BGRA image");
+
+ auto width = static_cast<uint32_t>(dmabuf.size.width());
+ auto height = static_cast<uint32_t>(dmabuf.size.height());
+ auto device = context.logical_device;
+
+ auto imported = TRY(import_linux_dmabuf_image(context.physical_device, device, context.ext_procs.get_memory_fd_properties, dmabuf, VK_FORMAT_B8G8R8A8_UNORM, VK_IMAGE_USAGE_TRANSFER_SRC_BIT));
+ VkPhysicalDeviceMemoryProperties memory_properties;
+ vkGetPhysicalDeviceMemoryProperties(context.physical_device, &memory_properties);
+ auto image = imported.image;
+ auto image_memory = imported.memory;
+ VkBuffer buffer = VK_NULL_HANDLE;
+ VkDeviceMemory buffer_memory = VK_NULL_HANDLE;
+ ScopeGuard destroy_objects = [&] {
+ if (buffer != VK_NULL_HANDLE)
+ vkDestroyBuffer(device, buffer, nullptr);
+ if (buffer_memory != VK_NULL_HANDLE)
+ vkFreeMemory(device, buffer_memory, nullptr);
+ vkDestroyImage(device, image, nullptr);
+ vkFreeMemory(device, image_memory, nullptr);
+ };
+
+ auto row_size = static_cast<VkDeviceSize>(width) * 4;
+ VkBufferCreateInfo buffer_info = {
+ .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
+ .pNext = nullptr,
+ .flags = 0,
+ .size = row_size * height,
+ .usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT,
+ .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
+ .queueFamilyIndexCount = 0,
+ .pQueueFamilyIndices = nullptr,
+ };
+ if (vkCreateBuffer(device, &buffer_info, nullptr, &buffer) != VK_SUCCESS)
+ return Error::from_string_literal("readback buffer creation failed");
+ VkMemoryRequirements buffer_memory_requirements;
+ vkGetBufferMemoryRequirements(device, buffer, &buffer_memory_requirements);
+ // NB: The CPU reads uncached memory very slowly, so cached memory is preferred.
+ auto buffer_memory_type_index = find_memory_type_index(memory_properties, buffer_memory_requirements, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
+ if (buffer_memory_type_index == memory_properties.memoryTypeCount)
+ buffer_memory_type_index = find_memory_type_index(memory_properties, buffer_memory_requirements, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
+ if (buffer_memory_type_index == memory_properties.memoryTypeCount)
+ return Error::from_string_literal("no host-visible memory for the readback buffer");
+ bool buffer_memory_is_coherent = memory_properties.memoryTypes[buffer_memory_type_index].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
+ VkMemoryAllocateInfo buffer_allocate_info = {
+ .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
+ .pNext = nullptr,
+ .allocationSize = buffer_memory_requirements.size,
+ .memoryTypeIndex = buffer_memory_type_index,
+ };
+ if (vkAllocateMemory(device, &buffer_allocate_info, nullptr, &buffer_memory) != VK_SUCCESS)
+ return Error::from_string_literal("readback buffer memory allocation failed");
+ if (vkBindBufferMemory(device, buffer, buffer_memory, 0) != VK_SUCCESS)
+ return Error::from_string_literal("readback buffer memory binding failed");
+
+ vkResetCommandBuffer(context.command_buffer, 0);
+ VkCommandBufferBeginInfo begin_info = {
+ .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
+ .pNext = nullptr,
+ .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
+ .pInheritanceInfo = nullptr,
+ };
+ vkBeginCommandBuffer(context.command_buffer, &begin_info);
+ // The exporter leaves its images in the general layout, and hands them over from another instance of the driver.
+ VkImageMemoryBarrier acquire_barrier = {
+ .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
+ .pNext = nullptr,
+ .srcAccessMask = 0,
+ .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
+ .oldLayout = VK_IMAGE_LAYOUT_GENERAL,
+ .newLayout = VK_IMAGE_LAYOUT_GENERAL,
+ .srcQueueFamilyIndex = VK_QUEUE_FAMILY_EXTERNAL,
+ .dstQueueFamilyIndex = context.graphics_queue_family,
+ .image = image,
+ .subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 },
+ };
+ vkCmdPipelineBarrier(context.command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &acquire_barrier);
+ VkBufferImageCopy region = {
+ .bufferOffset = 0,
+ .bufferRowLength = 0,
+ .bufferImageHeight = 0,
+ .imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 },
+ .imageOffset = { 0, 0, 0 },
+ .imageExtent = { width, height, 1 },
+ };
+ vkCmdCopyImageToBuffer(context.command_buffer, image, VK_IMAGE_LAYOUT_GENERAL, buffer, 1, &region);
+ VkBufferMemoryBarrier host_barrier = {
+ .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
+ .pNext = nullptr,
+ .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
+ .dstAccessMask = VK_ACCESS_HOST_READ_BIT,
+ .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
+ .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
+ .buffer = buffer,
+ .offset = 0,
+ .size = VK_WHOLE_SIZE,
+ };
+ vkCmdPipelineBarrier(context.command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_HOST_BIT, 0, 0, nullptr, 1, &host_barrier, 0, nullptr);
+ vkEndCommandBuffer(context.command_buffer);
+
+ VkSubmitInfo submit_info = {
+ .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
+ .pNext = nullptr,
+ .waitSemaphoreCount = 0,
+ .pWaitSemaphores = nullptr,
+ .pWaitDstStageMask = nullptr,
+ .commandBufferCount = 1,
+ .pCommandBuffers = &context.command_buffer,
+ .signalSemaphoreCount = 0,
+ .pSignalSemaphores = nullptr,
+ };
+ if (vkQueueSubmit(context.graphics_queue, 1, &submit_info, VK_NULL_HANDLE) != VK_SUCCESS)
+ return Error::from_string_literal("readback submission failed");
+ if (vkQueueWaitIdle(context.graphics_queue) != VK_SUCCESS)
+ return Error::from_string_literal("readback did not finish");
+
+ void* pixels = nullptr;
+ if (vkMapMemory(device, buffer_memory, 0, VK_WHOLE_SIZE, 0, &pixels) != VK_SUCCESS)
+ return Error::from_string_literal("readback buffer mapping failed");
+ if (!buffer_memory_is_coherent) {
+ VkMappedMemoryRange range = {
+ .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
+ .pNext = nullptr,
+ .memory = buffer_memory,
+ .offset = 0,
+ .size = VK_WHOLE_SIZE,
+ };
+ vkInvalidateMappedMemoryRanges(device, 1, &range);
+ }
+ auto bitmap_or_error = Bitmap::create(BitmapFormat::BGRA8888, AlphaType::Premultiplied, dmabuf.size);
+ if (!bitmap_or_error.is_error()) {
+ auto& bitmap = *bitmap_or_error.value();
+ for (uint32_t y = 0; y < height; ++y)
+ memcpy(bitmap.scanline_u8(static_cast<int>(y)), static_cast<u8 const*>(pixels) + y * row_size, row_size);
+ }
+ vkUnmapMemory(device, buffer_memory);
+ return bitmap_or_error;
+}
+
}
#endif
diff --git a/Libraries/LibGfx/VulkanImage.h b/Libraries/LibGfx/VulkanImage.h
index a683c2186b9..a7596f6a081 100644
--- a/Libraries/LibGfx/VulkanImage.h
+++ b/Libraries/LibGfx/VulkanImage.h
@@ -12,6 +12,9 @@
# include <AK/NonnullRefPtr.h>
# include <AK/RefCounted.h>
# include <AK/Span.h>
+# include <AK/Vector.h>
+# include <LibGfx/Forward.h>
+# include <LibGfx/LinuxDmaBufHandle.h>
# include <LibGfx/VulkanContext.h>
# include <libdrm/drm_fourcc.h>
@@ -53,8 +56,24 @@ static inline uint32_t vk_format_to_drm_format(VkFormat format)
}
}
+// The DRM format modifiers of the single-plane layouts that the device can both render to and sample from.
+Vector<uint64_t> renderable_drm_format_modifiers(VulkanContext const& context, VkFormat format);
+// Whether the device can import a dma-buf in this format and layout as an image for these uses.
+bool can_import_dmabuf_image(VkPhysicalDevice, VkFormat, uint64_t modifier, VkImageUsageFlags);
+
ErrorOr<NonnullRefPtr<VulkanImage>> create_shared_vulkan_image(VulkanContext const& context, uint32_t width, uint32_t height, VkFormat format, ReadonlySpan<uint64_t> modifiers);
+struct ImportedDmaBufImage {
+ VkImage image { VK_NULL_HANDLE };
+ VkDeviceMemory memory { VK_NULL_HANDLE };
+};
+// Imports a single-plane dma-buf as an image in the exact layout it has, for these uses. The caller owns the image and
+// its memory.
+ErrorOr<ImportedDmaBufImage> import_linux_dmabuf_image(VkPhysicalDevice, VkDevice, PFN_vkGetMemoryFdPropertiesKHR, LinuxDmaBufHandle const&, VkFormat, VkImageUsageFlags);
+
+// Copies the pixels of a single-plane BGRA dma-buf that the device can import, in whatever layout it has, into a bitmap.
+ErrorOr<NonnullRefPtr<Bitmap>> read_linux_dmabuf_into_bitmap(VulkanContext const& context, LinuxDmaBufHandle const& dmabuf);
+
}
#endif