QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstVulkanVideoBuffer.cc
Go to the documentation of this file.
2
3#include <QtGui/qtguiglobal.h>
4
5#include "GstHwPathRegistry.h"
7#include "GstHwVideoBuffer.h"
9
10#if defined(QGC_HAS_GST_VULKAN_GPU_PATH) && QT_CONFIG(vulkan)
11
12#include <rhi/qrhi.h>
13#include <rhi/qrhi_platform.h>
14#include <vulkan/vulkan.h>
15
16#include <QtCore/QLoggingCategory>
17
18// gst-vulkan's gstvkdecoder.h declares a struct member named `slots`, which collides with Qt's `slots` keyword macro
19// (qtmetamacros.h). Suppress the macro across the gst/vulkan includes only.
20#pragma push_macro("slots")
21#undef slots
22#include <gst/vulkan/gstvkdevice.h>
23#include <gst/vulkan/gstvkimagememory.h>
24#pragma pop_macro("slots")
25
26#include "QGCLoggingCategory.h"
27
28QGC_LOGGING_CATEGORY(GstVulkanBufLog, "Video.GStreamer.HwBuffers.GstVulkanBuf")
29
30namespace {
31
33struct VulkanDiagnostics : GstHw::MapDiagnostics
34{
35 std::atomic<bool> loggedDeviceMismatch{false};
36 std::atomic<bool> loggedMultiMemory{false};
37 std::atomic<bool> loggedNoSync{false};
38
39 void reset() noexcept
40 {
42 for (std::atomic<bool>* flag : {&loggedDeviceMismatch, &loggedMultiMemory, &loggedNoSync}) {
43 flag->store(false, std::memory_order_relaxed);
44 }
45 }
46};
47
48static_assert(sizeof(VulkanDiagnostics) == sizeof(GstHw::MapDiagnostics) + (3 * sizeof(std::atomic<bool>)),
49 "VulkanDiagnostics::reset() must list every flag");
50
51VulkanDiagnostics s_diag;
52
53} // namespace
54
55bool GstVulkanVideoBuffer::validatePlaneHandles() const
56{
57 return validatePlanes([](GstMemory* mem) { return mem && gst_is_vulkan_image_memory(mem); });
58}
59
60QVideoFrameTexturesUPtr GstVulkanVideoBuffer::mapTextures(QRhi& rhi, QVideoFrameTexturesUPtr& /*old*/)
61{
63 GstBuffer* buffer = nullptr;
64 if (!checkMapPreconditions(rhi, static_cast<int>(QRhi::Vulkan), GstVulkanBufLog(), s_diag, buffer)) {
66 }
67
68 // Vulkan video decode yields one multiplanar VkImage (NV12/P010); disjoint multi-memory layouts are out of scope.
69 if (gst_buffer_n_memory(buffer) != 1) {
70 QGC_HW_WARN_ONCE(GstVulkanBufLog, s_diag.loggedMultiMemory,
71 "Vulkan import: multi-memory buffer not supported; frame not imported");
73 }
74
75 GstMemory* mem0 = gst_buffer_peek_memory(buffer, 0);
76 if (!mem0 || !gst_is_vulkan_image_memory(mem0)) {
78 }
79 auto* vkMem = reinterpret_cast<GstVulkanImageMemory*>(mem0);
80
81 const auto* nh = static_cast<const QRhiVulkanNativeHandles*>(rhi.nativeHandles());
82 if (!nh || nh->dev == VK_NULL_HANDLE) {
84 }
85
86 // createFrom() requires the image to belong to QRhi's VkDevice; one of another VkDevice is unusable (UB). Every
87 // GstVulkanImageMemory is: gst-vulkan creates its own VkDevice and can't wrap Qt's (see GstVulkanContextBridge).
88 if (!vkMem->device || vkMem->device->device != nh->dev) {
91 QGC_HW_WARN_ONCE(GstVulkanBufLog, s_diag.loggedDeviceMismatch,
92 "Vulkan import: GstVulkanImageMemory VkDevice != QRhi VkDevice; frame not imported");
94 }
95
96 // Unreachable until gst-vulkan can wrap Qt's VkDevice. Sampling the image would then also need the decoder's
97 // writes to be complete: its timeline semaphore (GstVulkanOperation) can't be handed to QRhi's submission, a
98 // per-frame vkDeviceWaitIdle would stall the whole UI, and an unsynchronized VkImage would tear.
101 QGC_HW_WARN_ONCE(GstVulkanBufLog, s_diag.loggedNoSync,
102 "Vulkan import: no decoder-to-QRhi sync (timeline semaphore); frame not imported");
104}
105
106constinit const HwPathDescriptor GstHwPaths::kVulkan = {
108 .matches = [](GstMemory* mem) -> bool { return gst_is_vulkan_image_memory(mem); },
109 .construct = &GstHwPaths::construct<GstVulkanVideoBuffer>,
110 .resetCaches = [] { s_diag.reset(); },
111 .bridge = &GstVulkanContextBridge::instance,
112};
113
114#endif // QGC_HAS_GST_VULKAN_GPU_PATH
#define QGC_HW_WARN_ONCE(LOGCAT, FLAG,...)
Logs once via qCWarning(LOGCAT) the first time FLAG flips true; subsequent trips are silent.
#define QGC_LOGGING_CATEGORY(name, categoryStr)
RAII timer: records mapTextures() wall time into the path's EWMA on scope exit.
QVideoFrameTexturesUPtr fail(HwVideoBufferPath path) noexcept
Records a map failure for path and returns the empty bundle mapTextures() reports it with.
void recordFallbackReason(HwVideoBufferPath attemptedPath, HwFallbackReason reason) noexcept
Per-(path,reason) fallback accounting; lets a bug report show why a path demoted to CPU.
One-shot warning flags per failure cause; paths with extra causes (DMABuf, D3D, IOSurface) derive and...