QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwVideoBuffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <atomic>
5#include <gst/gst.h>
6#include <gst/video/video-info.h>
7#include <private/qhwvideobuffer_p.h>
8
9#include <QtCore/QLoggingCategory>
10#include <QtCore/QSize>
11#include <QtCore/qglobal.h>
12#include <QtMultimedia/QVideoFrame>
13#include <QtMultimedia/QVideoFrameFormat>
14
15#include "CpuVideoFramePool.h"
16#include "GstHwTypes.h"
17
18namespace GstHw {
19
21constexpr int kMaxPlanes = 4;
22
25{
26 std::atomic<bool> loggedFirstSuccess{false};
27 std::atomic<bool> loggedNullSample{false};
28 std::atomic<bool> loggedWrongThread{false};
29 std::atomic<bool> loggedBadBackend{false};
30 std::atomic<bool> loggedNullBuffer{false};
31 std::atomic<bool> loggedTextureCreateFail{false};
32
35 void reset() noexcept
36 {
37 for (std::atomic<bool>* flag : {&loggedFirstSuccess, &loggedNullSample, &loggedWrongThread, &loggedBadBackend,
39 flag->store(false, std::memory_order_relaxed);
40 }
41 }
42};
43
44// A flag added without its line in reset() would stay latched for the process.
45static_assert(sizeof(MapDiagnostics) == 6 * sizeof(std::atomic<bool>), "MapDiagnostics::reset() must list every flag");
46
50bool texturePadsPicture(QSize textureSize, QSize picture, bool evenSizes);
51
52} // namespace GstHw
53
54namespace GstHwPathTelemetry {
55
57QVideoFrameTexturesUPtr fail(HwVideoBufferPath path) noexcept;
58
59} // namespace GstHwPathTelemetry
60
62#define QGC_HW_WARN_ONCE(LOGCAT, FLAG, ...) \
63 do { \
64 if (!(FLAG).exchange(true, std::memory_order_relaxed)) { \
65 qCWarning(LOGCAT) << __VA_ARGS__; \
66 } \
67 } while (0)
68
70class GstHwVideoBuffer : public QHwVideoBuffer
71{
72public:
73 GstHwVideoBuffer(GstSample* sample, const GstVideoInfo& videoInfo, QVideoFrameFormat format);
74 ~GstHwVideoBuffer() override;
75
76 QVideoFrameFormat format() const override { return _format; }
77
82 MapData map(QVideoFrame::MapMode mode) override;
83 void unmap() override;
84
86 virtual bool validatePlaneHandles() const { return true; }
87
91 GstSample* refSample() noexcept { return _sample ? gst_sample_ref(_sample) : nullptr; }
92
93protected:
95 template <class PlanePred>
96 bool validatePlanes(PlanePred&& planePred) const
97 {
98 if (!_sample)
99 return false;
100 GstBuffer* buffer = gst_sample_get_buffer(_sample);
101 if (!buffer)
102 return false;
103 const int memCount = (std::min) (int(gst_buffer_n_memory(buffer)), GstHw::kMaxPlanes);
104 if (memCount <= 0)
105 return false;
106 for (int i = 0; i < memCount; ++i) {
107 GstMemory* mem = gst_buffer_peek_memory(buffer, i);
108 if (!planePred(mem))
109 return false;
110 }
111 return true;
112 }
113
115 bool checkMapPreconditions(const QRhi& rhi, int expectedBackend, const QLoggingCategory& cat,
116 GstHw::MapDiagnostics& diag, GstBuffer*& outBuffer) const;
117
118 GstSample* _sample = nullptr;
119 GstVideoInfo _videoInfo = {};
120 QVideoFrameFormat _format;
121
122private:
125 QVideoFrameFormat::PixelFormat _memoryPixelFormat;
127};
HwVideoBufferPath
GPU zero-copy path a frame is imported through; None means the frame takes the CPU path.
Definition GstHwTypes.h:7
void unmap() override
GstVideoInfo _videoInfo
GstHwVideoBuffer(GstSample *sample, const GstVideoInfo &videoInfo, QVideoFrameFormat format)
MapData map(QVideoFrame::MapMode mode) override
~GstHwVideoBuffer() override
QVideoFrameFormat format() const override
GstSample * refSample() noexcept
QVideoFrameFormat _format
bool checkMapPreconditions(const QRhi &rhi, int expectedBackend, const QLoggingCategory &cat, GstHw::MapDiagnostics &diag, GstBuffer *&outBuffer) const
Shared mapTextures preamble; warns once per cause via diag flags, returns false on first failure.
bool validatePlanes(PlanePred &&planePred) const
Validate each plane satisfies planePred; predicate runs once per plane on the streaming thread.
virtual bool validatePlaneHandles() const
Streaming-thread sanity check on per-plane handles; failure routes to CPU memcpy.
QVideoFrameTexturesUPtr fail(HwVideoBufferPath path) noexcept
Records a map failure for path and returns the empty bundle mapTextures() reports it with.
Defined only in builds with the DMABuf path (QGC_HAS_GST_DMABUF_GPU_PATH) on GStreamer 1....
bool texturePadsPicture(QSize textureSize, QSize picture, bool evenSizes)
constexpr int kMaxPlanes
Matches GST_VIDEO_MAX_PLANES (gst-video pins it at 4); single source of truth for every per-platform ...
One-shot warning flags per failure cause; paths with extra causes (DMABuf, D3D, IOSurface) derive and...
std::atomic< bool > loggedNullSample
std::atomic< bool > loggedTextureCreateFail
std::atomic< bool > loggedNullBuffer
std::atomic< bool > loggedBadBackend
std::atomic< bool > loggedFirstSuccess
std::atomic< bool > loggedWrongThread