QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstCudaVideoBuffer.cc
Go to the documentation of this file.
2
3#if defined(QGC_HAS_GST_CUDA_GPU_PATH)
4
5#include <atomic>
6#include <gst/allocators/gstdmabuf.h>
7#include <gst/cuda/gstcuda.h>
8#include <gst/video/video.h>
9#include <memory>
10
11#include <QtCore/QLoggingCategory>
12
14#include "GstHwPathRegistry.h"
15#include "GstHwPathTelemetry.h"
16#include "QGCLoggingCategory.h"
17
18QGC_LOGGING_CATEGORY(GstCudaLog, "Video.GStreamer.HwBuffers.GstCuda")
19
20namespace GstCudaVideoBuffer {
21namespace {
22
23// What the driver exports doesn't change while the process runs, so its first refusal turns the path off for good.
24std::atomic<bool> s_exportUnsupported{false};
25
26void disableExport(const char* why)
27{
28 if (!s_exportUnsupported.exchange(true, std::memory_order_relaxed)) {
31 qCWarning(GstCudaLog) << "CUDA frames take the CPU path for the rest of the process:" << why;
32 }
33}
34
37bool isExportable(GstMemory* mem)
38{
39 return gst_is_cuda_memory(mem) &&
40 (gst_cuda_memory_get_alloc_method(reinterpret_cast<GstCudaMemory*>(mem)) == GST_CUDA_MEMORY_ALLOC_MMAP);
41}
42
45int exportDmaBufFd(GstMemory* mem)
46{
47 auto* cudaMem = reinterpret_cast<GstCudaMemory*>(mem);
48
49 // The decoder's CUDA stream may still be writing; the EGL import has no ordering against it.
50 gst_cuda_memory_sync(cudaMem);
51
52 // Linux: os_handle is an int* receiving a POSIX file descriptor (CU_MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR).
53 int fd = -1;
54 if (!gst_cuda_memory_export(cudaMem, &fd) || (fd < 0)) {
55 disableExport("the driver doesn't export CUDA memory");
56 return -1;
57 }
58 // Wrapped as a dma-buf without being one, the fd would fail every EGL import, and those failures would count
59 // against the DMABuf path's own frames.
60 if (!GstDmaBufVideoBuffer::isDmaBufFd(fd)) {
61 disableExport("the handle the driver exports is not a dma-buf");
62 return -1;
63 }
64 return fd;
65}
66
67// Asked for every frame: the memory, Qt's EGLDisplay and the DMABuf importer's state all change within a stream, and
68// none of them says anything about what the driver exports.
69bool admitCuda(GstMemory* mem, const HwVideoBufferContext& context)
70{
71 // The exported fd is imported as an EGLImage, with OpenGL only: under Vulkan the importer is no EGLDisplay.
72 if (exportSupported() && isExportable(mem) && !GstDmaBufVideoBuffer::importDisabled() &&
73 (GstDmaBufVideoBuffer::eglDisplayFor(context) != EGL_NO_DISPLAY)) {
74 return true;
75 }
77 return false;
78}
79
80} // namespace
81
82bool exportSupported() noexcept
83{
84 return !s_exportUnsupported.load(std::memory_order_relaxed);
85}
86
87std::unique_ptr<GstHwVideoBuffer> exportToDmaBuf(GstSample* sample, const GstVideoInfo& info, QVideoFrameFormat format,
88 const HwVideoBufferContext& context)
89{
90 GstBuffer* buffer = gst_sample_get_buffer(sample);
91 if (!buffer) {
92 return nullptr;
93 }
94
95 // Single contiguous CUDA allocation per frame (NV12/P010 planes share one device buffer); multi-memory CUDA
96 // layouts are out of scope, so only memory 0 is exported.
97 GstMemory* mem0 = gst_buffer_peek_memory(buffer, 0);
98 if (!isExportable(mem0)) {
99 return nullptr;
100 }
101 const int fd = exportDmaBufFd(mem0);
102 if (fd < 0) {
103 return nullptr;
104 }
105
106 // Process-lifetime allocator; memories hold their own ref on it.
107 static GstAllocator* const s_dmabufAllocator = gst_dmabuf_allocator_new();
108 if (!s_dmabufAllocator) {
109 return nullptr;
110 }
111
112 // DONT_CLOSE: the fd belongs to the CUDA memory, which the parent-buffer meta below keeps alive.
113 GstMemory* dmaMem = gst_dmabuf_allocator_alloc_with_flags(
114 s_dmabufAllocator, fd, gst_memory_get_sizes(mem0, nullptr, nullptr), GST_FD_MEMORY_FLAG_DONT_CLOSE);
115 if (!dmaMem) {
116 return nullptr;
117 }
118
119 GstBuffer* dmaBuffer = gst_buffer_new();
120 if (!dmaBuffer) {
121 gst_memory_unref(dmaMem);
122 return nullptr;
123 }
124 gst_buffer_append_memory(dmaBuffer, dmaMem);
125 // Pin the CUDA buffer until the render thread drops this frame, else it returns to the decoder pool and is
126 // rewritten while still being sampled.
127 gst_buffer_add_parent_buffer_meta(dmaBuffer, buffer);
128
129 // Carry the source GstVideoMeta so GstDmaBufVideoBuffer reads correct per-plane offsets/strides without mmapping.
130 if (GstVideoMeta* srcMeta = gst_buffer_get_video_meta(buffer)) {
131 gst_buffer_add_video_meta_full(dmaBuffer, GST_VIDEO_FRAME_FLAG_NONE, srcMeta->format, srcMeta->width,
132 srcMeta->height, srcMeta->n_planes, srcMeta->offset, srcMeta->stride);
133 } else {
134 gst_buffer_add_video_meta(dmaBuffer, GST_VIDEO_FRAME_FLAG_NONE, GST_VIDEO_INFO_FORMAT(&info),
135 GST_VIDEO_INFO_WIDTH(&info), GST_VIDEO_INFO_HEIGHT(&info));
136 }
137
138 // Reuse the source caps: the exported fd is LINEAR device memory, so the original (non-DRM) caps drive a
139 // modifier-0 import in GstDmaBufVideoBuffer.
140 GstCaps* caps = gst_sample_get_caps(sample);
141 GstSample* dmaSample = gst_sample_new(dmaBuffer, caps, nullptr, nullptr);
142 gst_buffer_unref(dmaBuffer);
143 if (!dmaSample) {
144 return nullptr;
145 }
146
147 // GstHwVideoBuffer refs the sample; our local ref drops here.
148 auto buf =
149 std::make_unique<GstDmaBufVideoBuffer>(dmaSample, info, format, GstDmaBufVideoBuffer::eglDisplayFor(context));
150 gst_sample_unref(dmaSample);
151 return buf;
152}
153
154} // namespace GstCudaVideoBuffer
155
156constinit const HwPathDescriptor GstHwPaths::kCuda = {
158 .matches = [](GstMemory* mem) -> bool { return gst_is_cuda_memory(mem); },
159 .admits = &GstCudaVideoBuffer::admitCuda,
160 .construct = &GstCudaVideoBuffer::exportToDmaBuf,
161};
162
163#endif // QGC_HAS_GST_CUDA_GPU_PATH
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void recordFallbackReason(HwVideoBufferPath attemptedPath, HwFallbackReason reason) noexcept
Per-(path,reason) fallback accounting; lets a bug report show why a path demoted to CPU.