QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstHwVideoBuffer.cc
Go to the documentation of this file.
1#include "GstHwVideoBuffer.h"
2
3#include <rhi/qrhi.h>
4
6
7QVideoFrameTexturesUPtr GstHwPathTelemetry::fail(HwVideoBufferPath path) noexcept
8{
10 return {};
11}
12
13bool GstHw::texturePadsPicture(QSize textureSize, QSize picture, bool evenSizes)
14{
15 const auto rounded = [evenSizes](int extent) { return evenSizes ? ((extent + 1) & ~1) : extent; };
16 return (textureSize.width() > rounded(picture.width())) || (textureSize.height() > rounded(picture.height()));
17}
18
19GstHwVideoBuffer::GstHwVideoBuffer(GstSample* sample, const GstVideoInfo& videoInfo, QVideoFrameFormat format)
20 : QHwVideoBuffer(QVideoFrame::RhiTextureHandle)
21 , _sample(sample ? gst_sample_ref(sample) : nullptr)
22 , _videoInfo(videoInfo)
23 , _format(std::move(format))
24 , _memoryPixelFormat(_format.pixelFormat())
25{
26 // Crop is applied by the renderer via QVideoFrameFormat::viewport(); see frameGeometry (GstQgcFrameMap).
27}
28
30{
32 if (_sample) {
33 gst_sample_unref(_sample);
34 }
35}
36
37QAbstractVideoBuffer::MapData GstHwVideoBuffer::map(QVideoFrame::MapMode mode)
38{
39 // An external-OES frame has no CPU layout Qt could upload (it would read it as RGBA8), nor does a frame advertising
40 // another format than its memory holds (Qt would read past the end of a narrower layout).
41 const QVideoFrameFormat::PixelFormat advertised = _format.pixelFormat();
42 if ((mode != QVideoFrame::ReadOnly) || (advertised == QVideoFrameFormat::Format_SamplerExternalOES) ||
43 (advertised != _memoryPixelFormat)) {
44 return {};
45 }
46 return _cpuView.map(_sample ? gst_sample_get_buffer(_sample) : nullptr, _videoInfo);
47}
48
50{
51 _cpuView.unmap();
52}
53
54bool GstHwVideoBuffer::checkMapPreconditions(const QRhi& rhi, int expectedBackend, const QLoggingCategory& cat,
55 GstHw::MapDiagnostics& diag, GstBuffer*& outBuffer) const
56{
57 if (!_sample) {
58 QGC_HW_WARN_ONCE(cat, diag.loggedNullSample, "mapTextures: GstSample is null");
59 return false;
60 }
61 if (!rhi.thread()->isCurrentThread()) {
62 QGC_HW_WARN_ONCE(cat, diag.loggedWrongThread, "mapTextures: called outside QRhi render thread");
63 return false;
64 }
65 if (static_cast<int>(rhi.backend()) != expectedBackend) {
67 cat, diag.loggedBadBackend,
68 "mapTextures: QRhi backend is" << rhi.backendName() << "(backend id" << expectedBackend << "required)");
69 return false;
70 }
71 outBuffer = gst_sample_get_buffer(_sample);
72 if (!outBuffer) {
73 QGC_HW_WARN_ONCE(cat, diag.loggedNullBuffer, "mapTextures: GstSample has no buffer");
74 return false;
75 }
76 return true;
77}
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 unmap() override
GstVideoInfo _videoInfo
GstHwVideoBuffer(GstSample *sample, const GstVideoInfo &videoInfo, QVideoFrameFormat format)
MapData map(QVideoFrame::MapMode mode) override
~GstHwVideoBuffer() override
QVideoFrameFormat format() const override
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.
QVideoFrameTexturesUPtr fail(HwVideoBufferPath path) noexcept
Records a map failure for path and returns the empty bundle mapTextures() reports it with.
@ MapFailures
mapTextures() returned an invalid bundle (GPU import failed).
void record(HwVideoBufferPath path, Counter counter) noexcept
bool texturePadsPicture(QSize textureSize, QSize picture, bool evenSizes)
One-shot warning flags per failure cause; paths with extra causes (DMABuf, D3D, IOSurface) derive and...
std::atomic< bool > loggedNullSample
std::atomic< bool > loggedNullBuffer
std::atomic< bool > loggedBadBackend
std::atomic< bool > loggedWrongThread