3#if defined(QGC_HAS_GST_CUDA_GPU_PATH)
6#include <gst/allocators/gstdmabuf.h>
7#include <gst/cuda/gstcuda.h>
8#include <gst/video/video.h>
11#include <QtCore/QLoggingCategory>
20namespace GstCudaVideoBuffer {
24std::atomic<bool> s_exportUnsupported{
false};
26void disableExport(
const char* why)
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;
37bool isExportable(GstMemory* mem)
39 return gst_is_cuda_memory(mem) &&
40 (gst_cuda_memory_get_alloc_method(
reinterpret_cast<GstCudaMemory*
>(mem)) == GST_CUDA_MEMORY_ALLOC_MMAP);
45int exportDmaBufFd(GstMemory* mem)
47 auto* cudaMem =
reinterpret_cast<GstCudaMemory*
>(mem);
50 gst_cuda_memory_sync(cudaMem);
54 if (!gst_cuda_memory_export(cudaMem, &fd) || (fd < 0)) {
55 disableExport(
"the driver doesn't export CUDA memory");
60 if (!GstDmaBufVideoBuffer::isDmaBufFd(fd)) {
61 disableExport(
"the handle the driver exports is not a dma-buf");
72 if (exportSupported() && isExportable(mem) && !GstDmaBufVideoBuffer::importDisabled() &&
73 (GstDmaBufVideoBuffer::eglDisplayFor(context) != EGL_NO_DISPLAY)) {
82bool exportSupported() noexcept
84 return !s_exportUnsupported.load(std::memory_order_relaxed);
87std::unique_ptr<GstHwVideoBuffer> exportToDmaBuf(GstSample* sample,
const GstVideoInfo& info, QVideoFrameFormat format,
90 GstBuffer* buffer = gst_sample_get_buffer(sample);
97 GstMemory* mem0 = gst_buffer_peek_memory(buffer, 0);
98 if (!isExportable(mem0)) {
101 const int fd = exportDmaBufFd(mem0);
107 static GstAllocator*
const s_dmabufAllocator = gst_dmabuf_allocator_new();
108 if (!s_dmabufAllocator) {
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);
119 GstBuffer* dmaBuffer = gst_buffer_new();
121 gst_memory_unref(dmaMem);
124 gst_buffer_append_memory(dmaBuffer, dmaMem);
127 gst_buffer_add_parent_buffer_meta(dmaBuffer, buffer);
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);
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));
140 GstCaps* caps = gst_sample_get_caps(sample);
141 GstSample* dmaSample = gst_sample_new(dmaBuffer, caps,
nullptr,
nullptr);
142 gst_buffer_unref(dmaBuffer);
149 std::make_unique<GstDmaBufVideoBuffer>(dmaSample, info, format, GstDmaBufVideoBuffer::eglDisplayFor(context));
150 gst_sample_unref(dmaSample);
156constinit const HwPathDescriptor GstHwPaths::kCuda = {
158 .matches = [](GstMemory* mem) ->
bool {
return gst_is_cuda_memory(mem); },
159 .admits = &GstCudaVideoBuffer::admitCuda,
160 .construct = &GstCudaVideoBuffer::exportToDmaBuf,
#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.