QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstEglHelpers.cc
Go to the documentation of this file.
1#include "GstEglHelpers.h"
2
3#if defined(QGC_HAS_GST_DMABUF_GPU_PATH) || defined(QGC_HAS_GST_AHARDWAREBUFFER_GPU_PATH)
4
5#include <cstring>
6#include <rhi/qrhi.h>
7#include <rhi/qrhi_platform.h>
8#include <utility>
9
10#include <QtCore/QByteArray>
11#include <QtCore/QHash>
12#include <QtCore/QMutex>
13#include <QtCore/QMutexLocker>
14#include <QtGui/QOpenGLContext>
15#include <QtGui/qopenglcontext_platform.h>
16
17namespace GstEglHelpers {
18
19namespace {
20
22EGLDisplay resolveEglDisplay(QOpenGLContext* qtCtx)
23{
24 if (qtCtx) {
25 if (auto* egl = qtCtx->nativeInterface<QNativeInterface::QEGLContext>()) {
26 const EGLDisplay d = egl->display();
27 if (d != EGL_NO_DISPLAY)
28 return d;
29 }
30 }
31 return eglGetCurrentDisplay();
32}
33
34} // namespace
35
36QOpenGLContext* bindRhiContext(QRhi& rhi)
37{
38 if (!rhi.makeThreadLocalNativeContextCurrent()) {
39 return nullptr;
40 }
41 const auto* handles = static_cast<const QRhiGles2NativeHandles*>(rhi.nativeHandles());
42 return handles ? handles->context : nullptr;
43}
44
45EGLDisplay renderDisplay(QOpenGLContext* qtCtx, EGLDisplay fallback)
46{
47 const EGLDisplay display = resolveEglDisplay(qtCtx);
48 return display != EGL_NO_DISPLAY ? display : fallback;
49}
50
51namespace {
52
53QMutex s_extMutex;
54// Hash key = (display, extension name); names are static literals, copy cost paid once per miss.
55QHash<std::pair<EGLDisplay, QByteArray>, bool> s_extCache;
56
57} // namespace
58
59bool displaySupportsExtension(EGLDisplay display, const char* extension)
60{
61 if (display == EGL_NO_DISPLAY || !extension)
62 return false;
63 // Non-owning lookup key; the owning copy is made once per miss on insert.
64 const QByteArray extKey = QByteArray::fromRawData(extension, static_cast<qsizetype>(std::strlen(extension)));
65 // Lock held across read->query->write to prevent concurrent insertion of a conflicting result for the same key.
66 QMutexLocker lock(&s_extMutex);
67 auto it = s_extCache.constFind(std::make_pair(display, extKey));
68 if (it != s_extCache.constEnd())
69 return it.value();
70 // Must not eglInitialize Qt's display (a stray eglTerminate drops Qt's state); uninitialized returns nullptr.
71 const char* exts = eglQueryString(display, EGL_EXTENSIONS);
72 // Token check required: strstr would falsely match EGL_EXT_image_dma_buf_import inside
73 // EGL_EXT_image_dma_buf_import_modifiers.
74 bool supported = false;
75 if (exts && extension) {
76 const std::size_t extLen = std::strlen(extension);
77 for (const char* p = exts; (p = std::strstr(p, extension)) != nullptr; p += extLen) {
78 const char before = p == exts ? ' ' : *(p - 1);
79 const char after = *(p + extLen);
80 if ((before == ' ' || before == '\0') && (after == ' ' || after == '\0')) {
81 supported = true;
82 break;
83 }
84 }
85 }
86 s_extCache.insert(std::make_pair(display, QByteArray(extension)), supported);
87 return supported;
88}
89
90void resetExtensionCache()
91{
92 QMutexLocker lock(&s_extMutex);
93 s_extCache.clear();
94}
95
96PFNGLEGLIMAGETARGETTEXTURE2DOESPROC imageTargetTexture2D() noexcept
97{
98 static const auto fn =
99 reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
100 return fn;
101}
102
103PFNEGLDESTROYIMAGEKHRPROC destroyImageKHR() noexcept
104{
105 static const auto fn = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
106 return fn;
107}
108
109} // namespace GstEglHelpers
110
111#endif // QGC_HAS_GST_DMABUF_GPU_PATH || QGC_HAS_GST_AHARDWAREBUFFER_GPU_PATH