QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwVideoBufferFactory.cc
Go to the documentation of this file.
2
3#include <private/qhwvideobuffer_p.h>
4#include <utility>
5
6#include "GstHwPathRegistry.h"
8#include "GstHwVideoBuffer.h"
10
11QGC_LOGGING_CATEGORY(GstHwBufFactoryLog, "Video.GStreamer.HwBuffers.Factory")
12
13std::unique_ptr<QHwVideoBuffer> makeHwVideoBuffer(GstBuffer* buffer, GstCaps* caps, const GstVideoInfo& info,
14 QVideoFrameFormat format, const HwVideoBufferContext& context,
15 HwVideoBufferPath& matchedPath, HwResolvedPathCache* cache)
16{
17 matchedPath = HwVideoBufferPath::None;
18 if (!context.gpuEnabled || !buffer) {
19 return nullptr;
20 }
21
22 // Plane 0's allocator is the dispatch key; gst-video buffers from a single decoder use one allocator type across
23 // all planes.
24 GstMemory* mem0 = gst_buffer_peek_memory(buffer, 0);
25 if (!mem0) {
26 return nullptr;
27 }
28 const GType allocatorType = mem0->allocator ? G_OBJECT_TYPE(mem0->allocator) : G_TYPE_INVALID;
29 if (cache && (allocatorType != G_TYPE_INVALID) && (allocatorType == cache->unmatchedAllocator)) {
30 return nullptr;
31 }
32
33 // Validate before commit — failure resets matchedPath so per-path counters don't double-count.
34 auto buildOrFallback = [&matchedPath, &info, cache](auto&& buf) -> std::unique_ptr<QHwVideoBuffer> {
35 if (!buf || !buf->validatePlaneHandles()) {
37 // Some imports fail on every frame of a stream (e.g. multi-planar DMABuf under a Vulkan QRhi): warn once
38 // per caps epoch, not per frame.
39 if (!cache || !std::exchange(cache->validationFailureLogged, true)) {
40 qCWarning(GstHwBufFactoryLog)
41 << GstHw::toString(matchedPath) << "buffer failed plane-handle validation — CPU fallback";
42 }
43 if (cache) {
44 cache->validated = false;
45 }
46 matchedPath = HwVideoBufferPath::None;
47 return nullptr;
48 }
49 if (cache && !cache->validated) {
50 qCDebug(GstHwBufFactoryLog).noquote()
51 << "zero-copy path selected:" << GstHw::toString(matchedPath)
52 << "format=" << gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(&info)) << GST_VIDEO_INFO_WIDTH(&info)
53 << "x" << GST_VIDEO_INFO_HEIGHT(&info);
54 }
55 if (cache) {
56 cache->path = matchedPath;
57 cache->validated = true;
58 }
59 return buf;
60 };
61
62 // A matched path owns the frame: when it declines or fails, the frame goes to the CPU without trying later paths.
63 auto importThrough = [&](const HwPathDescriptor& path) -> std::unique_ptr<QHwVideoBuffer> {
64 if (path.admits && !path.admits(mem0, context)) {
65 if (cache) {
66 cache->validated = false;
67 }
68 return nullptr;
69 }
70 matchedPath = path.path;
71 // The sample a backend buffer holds, created only now that a path has matched the memory.
72 GstSample* const sample = gst_sample_new(buffer, caps, nullptr, nullptr);
73 auto imported = buildOrFallback(path.construct(sample, info, format, context));
74 gst_sample_unref(sample);
75 return imported;
76 };
77
78 const auto paths = GstHwPathRegistry::paths();
79
80 // Fast path: only the paths behind a sample validated earlier this epoch are tried. Allocator and handle checks
81 // stay per-buffer: decoders can swap memory or recycle bad handles under stable caps.
82 if (cache && cache->validated && cache->path != HwVideoBufferPath::None) {
83 for (const HwPathDescriptor* path : paths) {
84 if ((path->path == cache->path) && path->matches(mem0)) {
85 return importThrough(*path);
86 }
87 }
88 cache->validated = false;
89 }
90
91 for (const HwPathDescriptor* path : paths) {
92 if (path->matches(mem0)) {
93 return importThrough(*path);
94 }
95 }
96
97 // No compiled path imports this allocator's memory; that holds for the whole caps epoch.
98 if (cache) {
99 cache->unmatchedAllocator = allocatorType;
100 }
101 qCDebug(GstHwBufFactoryLog) << "no zero-copy path for memory type"
102 << (mem0->allocator ? mem0->allocator->mem_type : "<no-allocator>")
103 << "— falling back to CPU";
106 return nullptr;
107}
HwVideoBufferPath
GPU zero-copy path a frame is imported through; None means the frame takes the CPU path.
Definition GstHwTypes.h:7
std::unique_ptr< QHwVideoBuffer > makeHwVideoBuffer(GstBuffer *buffer, GstCaps *caps, const GstVideoInfo &info, QVideoFrameFormat format, const HwVideoBufferContext &context, HwVideoBufferPath &matchedPath, HwResolvedPathCache *cache)
#define QGC_LOGGING_CATEGORY(name, categoryStr)
std::span< const HwPathDescriptor *const > paths() noexcept
void recordFallbackReason(HwVideoBufferPath attemptedPath, HwFallbackReason reason) noexcept
Per-(path,reason) fallback accounting; lets a bug report show why a path demoted to CPU.
constexpr const char * toString(HwVideoBufferPath path) noexcept
Definition GstHwTypes.h:37