Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qdmabufvulkanimporter.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtMultimedia/private/qmultimedia_vulkan_support_p.h>
7#include <QtMultimedia/private/qvideotexturehelper_p.h>
8
9#include <QtGui/qvulkanfunctions.h>
10#include <QtCore/qloggingcategory.h>
11#include <QtCore/qscopeguard.h>
12#include <QtCore/private/quniquehandle_types_p.h>
13
14#include <unistd.h>
15
16static_assert(QT_CONFIG(linux_dmabuf_vulkan));
17
19
20Q_STATIC_LOGGING_CATEGORY(qLDmaBufVulkan, "qt.multimedia.dmabuf.vulkan")
21
22namespace QtMultimediaPrivate {
23
24namespace {
25
26// Packed-RGBA is deliberately left unsupported: vaExportSurfaceHandle() never produces it.
27VkFormat vkFormatForDrmFourcc(DRMFormat format)
28{
29 switch (format) {
30 case DRMFormat::R8:
31 return VK_FORMAT_R8_UNORM;
32 case DRMFormat::R16:
33 return VK_FORMAT_R16_UNORM;
34 case DRMFormat::GR88:
35 case DRMFormat::RG88:
36 // No Vulkan format has swapped (G,R) byte order; chroma shaders must swap R/G themselves.
37 return VK_FORMAT_R8G8_UNORM;
38 case DRMFormat::GR1616:
39 case DRMFormat::RG1616:
40 return VK_FORMAT_R16G16_UNORM;
41 default:
42 return VK_FORMAT_UNDEFINED;
43 }
44}
45
46} // namespace
47
49{
50 if (!rhi || rhi->backend() != QRhi::Vulkan) {
51 qCDebug(qLDmaBufVulkan) << "no rhi or non-Vulkan RHI";
52 return;
53 }
54
55 const auto *handles = static_cast<const QRhiVulkanNativeHandles *>(rhi->nativeHandles());
56 if (!handles || !handles->inst || !handles->dev || !handles->physDev) {
57 qCDebug(qLDmaBufVulkan) << " missing Vulkan native handles, disabling";
58 return;
59 }
60
63
67 qCDebug(qLDmaBufVulkan) << " no QVulkanFunctions/QVulkanDeviceFunctions, disabling";
68 return;
69 }
70
72 m_functions->vkGetDeviceProcAddr(m_device, "vkGetMemoryFdPropertiesKHR"));
74 qCDebug(qLDmaBufVulkan) << " vkGetMemoryFdPropertiesKHR unavailable "
75 "(VK_KHR_external_memory_fd likely not enabled), disabling";
76 return;
77 }
78
79 m_valid = true;
80}
81
93
103
105importDmaBufTexturesVulkan(QRhi &rhi, const DmaBufVulkanContext &vulkanContext,
106 QSpan<const DmaBufPlane> planes, QVideoFrameFormat::PixelFormat qtFormat,
107 QSize frameSize, std::shared_ptr<void> parentKeepAlive)
108{
109 auto *desc = QVideoTextureHelper::textureDescription(qtFormat);
110 int nPlanes = desc->nplanes;
111 Q_ASSERT(planes.size() == nPlanes);
112
113 const VkDevice device = vulkanContext.device();
114 const VkPhysicalDevice physicalDevice = vulkanContext.physicalDevice();
115 QVulkanFunctions *functions = vulkanContext.functions();
116 QVulkanDeviceFunctions *deviceFunctions = vulkanContext.deviceFunctions();
117
118 VkPhysicalDeviceMemoryProperties memProperties{};
119 functions->vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
120
121 std::array<VulkanDmaBufTextureHandles::Plane, 4> resultPlanes{};
122
123 auto releasePlanes = qScopeGuard([&] {
124 for (int i = 0; i < nPlanes; ++i) {
125 if (resultPlanes[i].image != VK_NULL_HANDLE)
126 deviceFunctions->vkDestroyImage(device, resultPlanes[i].image, nullptr);
127 if (resultPlanes[i].memory != VK_NULL_HANDLE)
128 deviceFunctions->vkFreeMemory(device, resultPlanes[i].memory, nullptr);
129 }
130 });
131
132 for (int i = 0; i < nPlanes; ++i) {
133 const DmaBufPlane &plane = planes[i];
134 const QSize planeSize = desc->rhiPlaneSize(frameSize, i, &rhi);
135
136 const VkFormat format = vkFormatForDrmFourcc(plane.drmFormat);
137 if (format == VK_FORMAT_UNDEFINED) {
138 qCWarning(qLDmaBufVulkan) << "no VkFormat mapping for DRM format" << plane.drmFormat
139 << ", disabling Vulkan dma-buf import for this frame";
140 return q23::unexpected{ FailureSeverity::recoverable };
141 }
142
143 QUniqueFileDescriptorHandle dupFd{ dup(plane.fd) };
144 if (!dupFd) {
145 qCWarning(qLDmaBufVulkan) << "dup() failed for dma-buf plane" << i;
146 return q23::unexpected{ FailureSeverity::recoverable };
147 }
148
149 const uint64_t modifier = plane.modifier == DmaBufFormatModifierInvalid
150 ? uint64_t(DrmFormatModifierLinear)
151 : uint64_t(plane.modifier);
152
153 const VkSubresourceLayout planeLayout{
154 .offset = VkDeviceSize(plane.offset),
155 .size = 0,
156 .rowPitch = VkDeviceSize(plane.pitch),
157 .arrayPitch = 0,
158 .depthPitch = 0,
159 };
160
161 const VkImageDrmFormatModifierExplicitCreateInfoEXT modifierInfo{
162 .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT,
163 .pNext = nullptr,
164 .drmFormatModifier = modifier,
165 .drmFormatModifierPlaneCount = 1,
166 .pPlaneLayouts = &planeLayout
167 };
168
169 const VkExternalMemoryImageCreateInfo extMemImageInfo{
170 .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
171 .pNext = &modifierInfo,
172 .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
173 };
174
175 const VkImageCreateInfo imageInfo{
176 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
177 .pNext = &extMemImageInfo,
178 .flags = 0,
179 .imageType = VK_IMAGE_TYPE_2D,
180 .format = format,
181 .extent =
182 VkExtent3D{
183 .width = uint32_t(planeSize.width()),
184 .height = uint32_t(planeSize.height()),
185 .depth = 1u,
186 },
187 .mipLevels = 1,
188 .arrayLayers = 1,
189 .samples = VK_SAMPLE_COUNT_1_BIT,
190 .tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT,
191 .usage = VK_IMAGE_USAGE_SAMPLED_BIT,
192 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
193 .queueFamilyIndexCount = 0,
194 .pQueueFamilyIndices = nullptr,
195 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
196 };
197
198 VkImage image = VK_NULL_HANDLE;
199 const VkResult createImageResult =
200 deviceFunctions->vkCreateImage(device, &imageInfo, nullptr, &image);
201 if (createImageResult != VK_SUCCESS) {
202 // This device can't create a DRM-format-modifier-tiled image in this format at all;
203 // every subsequent frame of the same pixel format will fail identically.
204 qCWarning(qLDmaBufVulkan)
205 << "vkCreateImage failed for dma-buf plane" << i << ":" << createImageResult;
206 return q23::unexpected{ FailureSeverity::unrecoverable };
207 }
208 auto destroyImageOnFailure = qScopeGuard([&] {
209 deviceFunctions->vkDestroyImage(device, image, nullptr);
210 });
211
212 VkMemoryFdPropertiesKHR fdProperties{
213 .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
214 .pNext = nullptr,
215 .memoryTypeBits = 0,
216 };
217 const std::optional<VkResult> fdPropertiesResult = vulkanContext.vkGetMemoryFdPropertiesKHR(
218 VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, dupFd.get(), &fdProperties);
219 if (fdPropertiesResult != VK_SUCCESS) {
220 qCWarning(qLDmaBufVulkan) << "vkGetMemoryFdPropertiesKHR failed for dma-buf plane" << i
221 << ":" << fdPropertiesResult.value_or(VK_ERROR_UNKNOWN);
222 return q23::unexpected{ FailureSeverity::recoverable };
223 }
224
225 VkMemoryRequirements memRequirements{};
226 deviceFunctions->vkGetImageMemoryRequirements(device, image, &memRequirements);
227
228 const uint32_t typeBits = memRequirements.memoryTypeBits & fdProperties.memoryTypeBits;
229 int memoryTypeIndex = -1;
230 for (uint32_t t = 0; t < memProperties.memoryTypeCount; ++t) {
231 if (typeBits & (1u << t)) {
232 memoryTypeIndex = int(t);
233 break;
234 }
235 }
236 if (memoryTypeIndex < 0) {
237 // This device's memory heaps never overlap with what the exporting driver offers;
238 // every subsequent frame will hit the same mismatch.
239 qCWarning(qLDmaBufVulkan) << "no compatible Vulkan memory type for dma-buf plane" << i;
240 return q23::unexpected{ FailureSeverity::unrecoverable };
241 }
242
243 const VkImportMemoryFdInfoKHR importInfo{
244 .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
245 .pNext = nullptr,
246 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
247 .fd = dupFd.get(),
248 };
249
250 const VkMemoryDedicatedAllocateInfo dedicatedInfo{
251 .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
252 .pNext = &importInfo,
253 .image = image,
254 .buffer = VK_NULL_HANDLE,
255 };
256
257 const VkMemoryAllocateInfo allocInfo{
258 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
259 .pNext = &dedicatedInfo,
260 .allocationSize = memRequirements.size,
261 .memoryTypeIndex = uint32_t(memoryTypeIndex),
262 };
263
264 VkDeviceMemory memory = VK_NULL_HANDLE;
265 const VkResult allocateMemoryResult =
266 deviceFunctions->vkAllocateMemory(device, &allocInfo, nullptr, &memory);
267 if (allocateMemoryResult != VK_SUCCESS) {
268 qCWarning(qLDmaBufVulkan) << "vkAllocateMemory (dma-buf import) failed for plane" << i
269 << ":" << allocateMemoryResult;
270 return q23::unexpected{ FailureSeverity::recoverable };
271 }
272 std::ignore = dupFd.release(); // vkAllocateMemory took ownership of dupFd
273
274 const VkResult bindImageMemoryResult =
275 deviceFunctions->vkBindImageMemory(device, image, memory, 0);
276 if (bindImageMemoryResult != VK_SUCCESS) {
277 qCWarning(qLDmaBufVulkan) << "vkBindImageMemory failed for dma-buf plane" << i << ":"
278 << bindImageMemoryResult;
279 deviceFunctions->vkFreeMemory(device, memory, nullptr);
280 return q23::unexpected{ FailureSeverity::recoverable };
281 }
282
283 destroyImageOnFailure.dismiss();
284 resultPlanes[i] = VulkanDmaBufTextureHandles::Plane{
285 .image = image,
286 .memory = memory,
287 };
288 }
289
290 releasePlanes.dismiss();
291
292 return std::make_unique<VulkanDmaBufTextureHandles>(deviceFunctions, device, nPlanes,
293 resultPlanes, std::move(parentKeepAlive));
294}
295
296} // namespace QtMultimediaPrivate
297
298QT_END_NAMESPACE
q23::expected< QVideoFrameTexturesHandlesUPtr, FailureSeverity > importDmaBufTexturesVulkan(QRhi &rhi, const DmaBufVulkanContext &vulkanContext, QSpan< const DmaBufPlane > planes, QVideoFrameFormat::PixelFormat qtFormat, QSize frameSize, std::shared_ptr< void > parentKeepAlive)
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)