QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwFrameTexturesBase.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <gst/gst.h>
7#include <memory>
8#include <private/qhwvideobuffer_p.h>
9#include <private/qvideotexturehelper_p.h>
10#include <rhi/qrhi.h>
11#include <span>
12#include <type_traits>
13
14#include <QtCore/QLoggingCategory>
15#include <QtCore/QSize>
16#include <QtMultimedia/QVideoFrame>
17#include <QtMultimedia/QVideoFrameFormat>
18
20#include "GstHwPathTelemetry.h"
21#include "GstHwVideoBuffer.h"
23
26{
28 QSize frameSize{};
29 QVideoFrameFormat::PixelFormat pixelFormat = QVideoFrameFormat::Format_Invalid;
30 QRhiTexture::Flags textureFlags{};
32 const QLoggingCategory* log = nullptr;
35 bool preflight = true;
37 const char* successTag = nullptr;
38};
39
44class GstHwFrameTexturesBase : public QVideoFrameTextures
45{
46public:
47 ~GstHwFrameTexturesBase() override { g_clear_pointer(&_srcSample, gst_sample_unref); }
48
49 QRhiTexture* texture(uint plane) const override
50 {
51 return plane < static_cast<uint>(_count) ? _textures[plane].get() : nullptr;
52 }
53
55 bool allPlanesValid() const noexcept
56 {
57 return _count > 0 && std::all_of(_textures, _textures + _count, [](const auto& t) { return t != nullptr; });
58 }
59
61 void setSourceSample(GstSample* s) noexcept
62 {
63 g_clear_pointer(&_srcSample, gst_sample_unref);
64 _srcSample = s;
65 }
66
69 template <class Handles>
70 bool matchesImport(const QRhi* rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat,
71 const Handles& handles) const noexcept
72 {
73 if ((_rhi != rhi) || (_size != size) || (_pixelFormat != pixelFormat) ||
74 (std::size(handles) != static_cast<std::size_t>(_count)) || !allPlanesValid()) {
75 return false;
76 }
77 for (int i = 0; i < _count; ++i) {
78 if (_nativeHandles[std::size_t(i)] != nativeHandle(handles[std::size_t(i)])) {
79 return false;
80 }
81 }
82 return true;
83 }
84
87 template <class FT>
88 static FT* reusableBundle(QVideoFrameTexturesUPtr& old)
89 {
90 return dynamic_cast<FT*>(old.get());
91 }
92
97 template <class FT, class Reuse, class Make>
98 static QVideoFrameTexturesUPtr finishMap(GstHwVideoBuffer& source, QRhi& rhi, QVideoFrameTexturesUPtr& old,
99 const GstHwMapTail& tail, Reuse&& reuse, Make&& make)
100 {
101 if constexpr (!std::is_null_pointer_v<std::remove_cvref_t<Reuse>>) {
102 if (FT* prev = reusableBundle<FT>(old); prev && reuse(*prev)) {
104 prev->setSourceSample(source.refSample());
105 return std::move(old);
106 }
107 }
109 tail.textureFlags)) {
110 return GstHwPathTelemetry::fail(tail.path);
111 }
112 std::unique_ptr<FT> textures = make();
113 // NV12 chroma can fail while luma succeeds; a partial bundle would render with missing planes.
114 if (!textures || !textures->allPlanesValid()) {
117 *tail.log, tail.diag->loggedTextureCreateFail,
118 "QRhiTexture::createFrom failed (format=" << int(tail.pixelFormat) << "size=" << tail.frameSize << ")");
119 return GstHwPathTelemetry::fail(tail.path);
120 }
121 if (tail.successTag && !tail.diag->loggedFirstSuccess.exchange(true, std::memory_order_relaxed)) {
122 qCInfo(*tail.log) << "First" << tail.successTag << "zero-copy mapTextures success: size=" << tail.frameSize
123 << "format=" << int(tail.pixelFormat) << "planes=" << textures->_count;
124 }
125 textures->setSourceSample(source.refSample());
126 return textures;
127 }
128
129protected:
134 template <class Handles>
135 void wrapPlanes(QRhi* rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, const Handles& handles,
136 int layout = 0, QRhiTexture::Flags flags = {})
137 {
138 _rhi = rhi;
139 _size = size;
140 _pixelFormat = pixelFormat;
141 _count = static_cast<int>((std::min) (std::size(handles), std::size_t(GstHw::kMaxPlanes)));
142 for (int i = 0; i < _count; ++i) {
143 _nativeHandles[std::size_t(i)] = nativeHandle(handles[std::size_t(i)]);
144 }
145 const auto* desc = QVideoTextureHelper::textureDescription(pixelFormat);
146 if (!desc) {
147 return;
148 }
149 for (int i = 0; i < _count; ++i) {
150 const quint64 handle = _nativeHandles[std::size_t(i)];
151 if (handle == 0) {
152 continue;
153 }
154 _textures[i].reset(rhi->newTexture(
155 desc->rhiTextureFormat(i, rhi, QVideoTextureHelper::TextureDescription::FallbackPolicy::Disable),
156 desc->rhiPlaneSize(size, i, rhi), 1, flags));
157 if (_textures[i] && !_textures[i]->createFrom({handle, layout})) {
158 _textures[i].reset();
159 }
160 }
161 }
162
163 QRhi* _rhi = nullptr;
164 int _count = 0;
165 std::unique_ptr<QRhiTexture> _textures[GstHw::kMaxPlanes];
166 GstSample* _srcSample = nullptr;
167
168private:
170 template <class Handle>
171 static quint64 nativeHandle(Handle handle) noexcept
172 {
173 if constexpr (std::is_pointer_v<Handle>) {
174 return static_cast<quint64>(reinterpret_cast<quintptr>(handle));
175 } else {
176 return static_cast<quint64>(handle);
177 }
178 }
179
180 QSize _size;
181 QVideoFrameFormat::PixelFormat _pixelFormat = QVideoFrameFormat::Format_Invalid;
182 std::array<quint64, GstHw::kMaxPlanes> _nativeHandles{};
183};
HwVideoBufferPath
GPU zero-copy path a frame is imported through; None means the frame takes the CPU path.
Definition GstHwTypes.h:7
#define QGC_HW_WARN_ONCE(LOGCAT, FLAG,...)
Logs once via qCWarning(LOGCAT) the first time FLAG flips true; subsequent trips are silent.
void wrapPlanes(QRhi *rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, const Handles &handles, int layout=0, QRhiTexture::Flags flags={})
bool matchesImport(const QRhi *rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, const Handles &handles) const noexcept
QRhiTexture * texture(uint plane) const override
void setSourceSample(GstSample *s) noexcept
Transfers a ref into the bundle. Caller must have a fresh ref.
std::unique_ptr< QRhiTexture > _textures[GstHw::kMaxPlanes]
static QVideoFrameTexturesUPtr finishMap(GstHwVideoBuffer &source, QRhi &rhi, QVideoFrameTexturesUPtr &old, const GstHwMapTail &tail, Reuse &&reuse, Make &&make)
bool allPlanesValid() const noexcept
True when the bundle has planes and every one of them wrapped its native handle.
static FT * reusableBundle(QVideoFrameTexturesUPtr &old)
Common base for GStreamer-backed QHwVideoBuffer subclasses.
GstSample * refSample() noexcept
bool preflightOrRecord(QRhi &rhi, HwVideoBufferPath path, QVideoFrameFormat::PixelFormat pixelFormat, const QSize &size, QRhiTexture::Flags flags)
QVideoFrameTexturesUPtr fail(HwVideoBufferPath path) noexcept
Records a map failure for path and returns the empty bundle mapTextures() reports it with.
@ TextureReuse
Prior frame's QRhiTexture wrappers reused (decoder pool returned same native handle).
void recordFallbackReason(HwVideoBufferPath attemptedPath, HwFallbackReason reason) noexcept
Per-(path,reason) fallback accounting; lets a bug report show why a path demoted to CPU.
void record(HwVideoBufferPath path, Counter counter) noexcept
constexpr int kMaxPlanes
Matches GST_VIDEO_MAX_PLANES (gst-video pins it at 4); single source of truth for every per-platform ...
Per-call inputs of GstHwFrameTexturesBase::finishMap().
bool preflight
False when the caller already preflighted before its costly native import work.
QVideoFrameFormat::PixelFormat pixelFormat
const QLoggingCategory * log
Required: where finishMap() logs, and its once-only flags.
const char * successTag
Logs the one-shot first-success line under this tag when set.
QRhiTexture::Flags textureFlags
GstHw::MapDiagnostics * diag
HwVideoBufferPath path
One-shot warning flags per failure cause; paths with extra causes (DMABuf, D3D, IOSurface) derive and...
std::atomic< bool > loggedTextureCreateFail
std::atomic< bool > loggedFirstSuccess