12#if defined(QGC_HAS_GST_GLMEMORY_GPU_PATH)
15#include <gst/gl/gstglbasememory.h>
16#include <gst/gl/gstglmemory.h>
17#include <gst/gl/gstglsyncmeta.h>
18#include <gst/video/video.h>
19#include <private/qvideotexturehelper_p.h>
22#include <QtCore/QLoggingCategory>
23#include <QtCore/QScopeGuard>
24#include <QtCore/QSize>
34#include <QtGui/qopengl.h>
36#ifndef GL_TIMEOUT_IGNORED
37#define GL_TIMEOUT_IGNORED 0xFFFFFFFFFFFFFFFFull
40#include <QtGui/QOpenGLContext>
41#include <QtGui/QOpenGLExtraFunctions>
52 FrameTextures(QRhi* rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, std::span<const GLuint> names)
57 ~FrameTextures()
override { gst_clear_buffer(&_syncBuffer); }
60 void setSyncBuffer(GstBuffer* buffer)
noexcept
62 gst_clear_buffer(&_syncBuffer);
67 GstBuffer* releaseSyncBuffer() noexcept {
return std::exchange(_syncBuffer,
nullptr); }
70 GstBuffer* _syncBuffer =
nullptr;
79GstBuffer* takeSyncBuffer(QVideoFrameTexturesUPtr& old, GstGLContext* context)
82 GstBuffer* buffer = previous->releaseSyncBuffer();
83 const GstGLSyncMeta*
const meta = buffer ? gst_buffer_get_gl_sync_meta(buffer) : nullptr;
84 if (meta && (meta->context == context)) {
87 gst_clear_buffer(&buffer);
89 GstBuffer* buffer = gst_buffer_new();
90 if (!gst_buffer_add_gl_sync_meta(context, buffer)) {
91 gst_clear_buffer(&buffer);
99void waitInQtContext(GstGLSyncMeta* syncMeta, GstGLContext* glCtx)
101 QOpenGLContext* qtCtx = QOpenGLContext::currentContext();
102 const auto sync =
static_cast<GLsync
>(syncMeta->data);
104 const QSurfaceFormat
format = qtCtx->format();
105 const bool hasSyncObjects = qtCtx->isOpenGLES() ?
format.majorVersion() >= 3
106 : (
format.version() >= qMakePair(3, 2) ||
107 qtCtx->hasExtension(QByteArrayLiteral(
"GL_ARB_sync")));
108 QOpenGLExtraFunctions* gl = hasSyncObjects ? qtCtx->extraFunctions() :
nullptr;
109 if (gl && gl->glIsSync(sync)) {
110 gl->glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
115 gst_gl_sync_meta_wait_cpu(syncMeta, glCtx);
127 if ((backend >= 0) && (backend !=
static_cast<int>(QRhi::OpenGLES2))) {
128 static std::atomic<bool> s_warned{
false};
129 QGC_HW_WARN_ONCE(GstGlBufLog, s_warned,
"GLMemory frame but QRhi backend is not OpenGL — routing to CPU pool");
130 }
else if (!GstGlContextBridge::sharesQtContext(GST_GL_BASE_MEMORY_CAST(mem)->context)) {
131 static std::atomic<bool> s_warnedUnshared{
false};
133 "GLMemory from a GL context that doesn't share with Qt's — routing to CPU pool");
144bool GstGlVideoBuffer::validatePlaneHandles()
const
147 return validatePlanes([](GstMemory* mem) {
return mem && gst_is_gl_memory(mem); });
150QVideoFrameTexturesUPtr GstGlVideoBuffer::mapTextures(QRhi& rhi, QVideoFrameTexturesUPtr& old)
154 GstBuffer* buffer =
nullptr;
155 if (!checkMapPreconditions(rhi,
static_cast<int>(QRhi::OpenGLES2), GstGlBufLog(), s_diag, buffer)) {
160 rhi.makeThreadLocalNativeContextCurrent();
162 GstMemory* mem0 = gst_buffer_peek_memory(buffer, 0);
163 if (!mem0 || !gst_is_gl_memory(mem0)) {
169 if (!mapping.
mapFrame(buffer, _videoInfo,
static_cast<GstMapFlags
>(GST_MAP_READ | GST_MAP_GL))) {
170 qCWarning(GstGlBufLog) <<
"gst_video_frame_map(GST_MAP_READ | GST_MAP_GL) failed";
178 GstGLContext* glCtx = GST_GL_BASE_MEMORY_CAST(mem0)->context;
179 GstBuffer* syncBuffer = glCtx ? takeSyncBuffer(old, glCtx) : nullptr;
180 const auto dropSyncBuffer = qScopeGuard([&syncBuffer] { gst_clear_buffer(&syncBuffer); });
182 GstGLSyncMeta*
const syncMeta = gst_buffer_get_gl_sync_meta(syncBuffer);
183 gst_gl_sync_meta_set_sync_point(syncMeta, glCtx);
184 waitInQtContext(syncMeta, glCtx);
188 const int planeCount = std::clamp(
int(GST_VIDEO_FRAME_N_PLANES(&frame)), 1, kMaxPlanes);
189 std::array<GLuint, kMaxPlanes> names{};
190 for (
int i = 0; i < planeCount; ++i) {
192 const GLuint* texIdPtr =
static_cast<const GLuint*
>(GST_VIDEO_FRAME_PLANE_DATA(&frame, i));
193 names[i] = texIdPtr ? *texIdPtr : 0;
197 const std::span<const GLuint> planeNames(names.data(), std::size_t(planeCount));
200 .frameSize = _format.frameSize(),
201 .pixelFormat = _format.pixelFormat(),
202 .log = &GstGlBufLog(),
206 *
this, rhi, old, tail,
207 [&](
const FrameTextures& prev) {
210 [&] {
return std::make_unique<FrameTextures>(&rhi, tail.
frameSize, tail.
pixelFormat, planeNames); });
212 bundle->setSyncBuffer(std::exchange(syncBuffer,
nullptr));
217constinit const HwPathDescriptor GstHwPaths::kGlMemory = {
219 .matches = [](GstMemory* mem) ->
bool {
return gst_is_gl_memory(mem); },
220 .admits = &admitGlMemory,
221 .construct = &GstHwPaths::construct<GstGlVideoBuffer>,
222 .bridge = &GstGlContextBridge::instance,
#define QGC_HW_WARN_ONCE(LOGCAT, FLAG,...)
Logs once via qCWarning(LOGCAT) the first time FLAG flips true; subsequent trips are silent.
#define QGC_LOGGING_CATEGORY(name, categoryStr)
bool mapFrame(GstBuffer *buffer, const GstVideoInfo &info, GstMapFlags flags=GST_MAP_READ)
Maps buffer with flags on the first call; false when that fails.
const GstVideoFrame & frame() const
The mapped frame; valid while mapFrame() succeeded and unmap() hasn't run.
void wrapPlanes(QRhi *rhi, QSize size, QVideoFrameFormat::PixelFormat pixelFormat, const Handles &handles, int layout=0, QRhiTexture::Flags flags={})
static QVideoFrameTexturesUPtr finishMap(GstHwVideoBuffer &source, QRhi &rhi, QVideoFrameTexturesUPtr &old, const GstHwMapTail &tail, Reuse &&reuse, Make &&make)
static FT * reusableBundle(QVideoFrameTexturesUPtr &old)
RAII timer: records mapTextures() wall time into the path's EWMA on scope exit.
QVideoFrameTexturesUPtr fail(HwVideoBufferPath path) noexcept
Records a map failure for path and returns the empty bundle mapTextures() reports it with.
@ GpuWaits
GPU-side waits on the producer's writes before sampling a frame.
@ CpuWaits
CPU-blocking waits on the producer's writes before sampling a frame.
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 ...
std::optional< Frame > frame(std::string_view text)
DeviceSnapshot & deviceSnapshot() noexcept
Returns the global snapshot. Atomic fields make individual reads thread-safe.
Per-call inputs of GstHwFrameTexturesBase::finishMap().
QVideoFrameFormat::PixelFormat pixelFormat
One-shot warning flags per failure cause; paths with extra causes (DMABuf, D3D, IOSurface) derive and...
std::atomic< int > backend
QRhi::Implementation cast to int (-1 = unset).