QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GstGlContextBridge.cc
Go to the documentation of this file.
2
3#if defined(QGC_HAS_GST_GLMEMORY_GPU_PATH)
4
5#include <gst/gl/gl.h>
6#include <utility>
7
8#include <QtCore/QLoggingCategory>
9#include <QtCore/QMutexLocker>
10#include <QtGui/QGuiApplication>
11#include <QtGui/QOpenGLContext>
12#include <QtGui/QSurfaceFormat>
13#include <QtGui/qguiapplication_platform.h>
14
15#include "QGCLoggingCategory.h"
16// EGL on Linux and Android; GLX/Wayland/X11 wrapping below only where their headers exist.
17#include <gst/gl/egl/gstgldisplay_egl.h>
18
19#include <EGL/egl.h>
20// Qt 6 on xcb uses GLX by default — fall back to GLX context wrapping when EGL isn't available.
21#if __has_include(<gst/gl/x11/gstgldisplay_x11.h>) && __has_include(<QtGui/qopenglcontext_platform.h>)
22#include <gst/gl/x11/gstgldisplay_x11.h>
23
24#include <QtGui/qopenglcontext_platform.h>
25
26#include <X11/Xlib.h>
27#define QGC_GST_BRIDGE_HAS_GLX 1
28#endif
29// Wayland: downstream elements probe GST_IS_GL_DISPLAY_WAYLAND; wl_display must be tagged on GstGLDisplay or zero-copy
30// paths are silently missed. Only from 1.26, where the EGL display gst derives from it can be marked foreign.
31#if __has_include(<gst/gl/wayland/gstgldisplay_wayland.h>) && GST_CHECK_VERSION(1, 26, 0)
32#include <gst/gl/wayland/gstgldisplay_wayland.h>
33#define QGC_GST_BRIDGE_HAS_WAYLAND 1
34#endif
35
36QGC_LOGGING_CATEGORY(GstGlBridgeLog, "Video.GStreamer.HwBuffers.GstGlBridge")
37
38namespace {
39
40// The API of Qt's context: what the wrap is declared as, and all primeLocked() lets gst-gl create contexts in.
41GstGLAPI wrappedGlApi(const QOpenGLContext& qtCtx)
42{
43 if (qtCtx.isOpenGLES()) {
44 return GST_GL_API_GLES2;
45 }
46 return (qtCtx.format().profile() == QSurfaceFormat::CoreProfile) ? GST_GL_API_OPENGL3 : GST_GL_API_OPENGL;
47}
48
49constexpr char kGlAppContextType[] = "gst.gl.app_context";
50// A pipeline is given these in this order: a gst-gl element creates its GL context as soon as it is handed another
51// display, sharing with the app context it holds by then.
52constexpr const char* kContextTypes[] = {kGlAppContextType, GST_GL_DISPLAY_CONTEXT_TYPE};
53
54class GlContextBridge final : public GstContextBridgeBase
55{
56public:
57 constexpr GlContextBridge() noexcept
58 : GstContextBridgeBase("GL", kContextTypes, &GstGlBridgeLog)
59 {}
60
61 bool shares(GstGLContext* context);
62
63private:
64 bool primeLocked() override;
65 GstObject* refObject(const char* contextType) override;
66 GstContext* buildContext(const char* contextType, GstObject* object) override;
67 void clearLocked() override;
68
69 GstGLDisplay* _display = nullptr;
70 GstGLContext* _context = nullptr;
71 // The wrap the last reset() dropped. The contexts gst-gl created from it still share with Qt's context, which
72 // outlives every scene graph, so a running stream's textures stay importable.
73 GstGLContext* _retiredContext = nullptr;
74};
75
76constinit GlContextBridge s_bridge;
77
78bool GlContextBridge::primeLocked()
79{
80 QOpenGLContext* qtCtx = QOpenGLContext::globalShareContext();
81 if (!qtCtx) {
82 if (retryWithinBudget()) {
83 qCInfo(GstGlBridgeLog) << "globalShareContext() is null — Qt GL not initialized yet"
84 << "(attempt" << failedPrimes() << "/" << kMaxPrimeRetries << ")";
85 } else if (retriesJustExhausted()) {
86 qCWarning(GstGlBridgeLog) << "globalShareContext() still null after" << kMaxPrimeRetries
87 << "retries; GL bridge giving up";
88 }
89 return false;
90 }
91
92 // Try EGL first (Wayland, eglfs, xcb_egl, Android); fall back to GLX (Linux xcb default on Qt 6).
93 const auto* const egl = qtCtx->nativeInterface<QNativeInterface::QEGLContext>();
94 const EGLContext eglCtx = egl ? egl->nativeContext() : EGL_NO_CONTEXT;
95 // QEGLContext::display() is the only handle that guarantees EGLImage import/sample compatibility; fall back to
96 // EGL_DEFAULT_DISPLAY when unavailable.
97 EGLDisplay eglDisp = (eglCtx != EGL_NO_CONTEXT) ? egl->display() : EGL_NO_DISPLAY;
98 if ((eglCtx != EGL_NO_CONTEXT) && (eglDisp == EGL_NO_DISPLAY)) {
99 eglDisp = eglGetDisplay(EGL_DEFAULT_DISPLAY);
100 }
101 if (eglCtx != EGL_NO_CONTEXT && eglDisp != EGL_NO_DISPLAY) {
102#if defined(QGC_GST_BRIDGE_HAS_WAYLAND)
103 // On Wayland, primary display must be GstGLDisplayWayland; derived EGL display is marked foreign so unref
104 // doesn't tear down Qt's EGLDisplay. Before 1.26 that derived display can't be marked foreign and its finalize
105 // eglTerminate()s Qt's display, so older GStreamer takes the foreign EGL wrap below instead.
106 const QString platformName = QGuiApplication::platformName();
107 if (platformName == QLatin1String("wayland") || platformName == QLatin1String("wayland-egl")) {
108 struct wl_display* wlDisp = nullptr;
109 if (auto* wl = qGuiApp->nativeInterface<QNativeInterface::QWaylandApplication>()) {
110 wlDisp = wl->display();
111 }
112 if (wlDisp) {
113 GstGLDisplayWayland* displayWl = gst_gl_display_wayland_new_with_display(wlDisp);
114 if (displayWl) {
115 _display = GST_GL_DISPLAY(displayWl);
116 if (const std::unique_ptr<GstGLDisplayEGL, GStreamer::GstObjectDeleter> derived{
117 gst_gl_display_egl_from_gl_display(_display)}) {
118 gst_gl_display_egl_set_foreign(derived.get(), TRUE);
119 }
120 }
121 }
122 }
123#endif
124 if (!_display) {
125 // Marked foreign: finalize never eglTerminate()s Qt's EGLDisplay, and gst_gl_display_egl_from_gl_display()
126 // returns this same object rather than deriving a non-foreign one.
127 GstGLDisplayEGL* displayEgl = gst_gl_display_egl_new_with_egl_display(eglDisp);
128 if (!displayEgl) {
129 qCWarning(GstGlBridgeLog) << "gst_gl_display_egl_new_with_egl_display failed";
130 return retryLater();
131 }
132 _display = GST_GL_DISPLAY(displayEgl);
133 }
134
135 _context = gst_gl_context_new_wrapped(_display, reinterpret_cast<guintptr>(eglCtx), GST_GL_PLATFORM_EGL,
136 wrappedGlApi(*qtCtx));
137 if (!_context) {
138 qCWarning(GstGlBridgeLog) << "gst_gl_context_new_wrapped (EGL) failed";
139 gst_clear_object(&_display);
140 return retryLater();
141 }
142#if defined(QGC_GST_BRIDGE_HAS_WAYLAND)
143 const bool isWayland = GST_IS_GL_DISPLAY_WAYLAND(_display);
144 qCInfo(GstGlBridgeLog) << (isWayland ? "GL bridge primed (Wayland+EGL)" : "GL bridge primed (EGL)");
145#else
146 qCInfo(GstGlBridgeLog) << "GL bridge primed (EGL)";
147#endif
148 } else {
149#if defined(QGC_GST_BRIDGE_HAS_GLX)
150 // Qt's QGLXContext exposes the X11 Display* + GLXContext we need to wrap.
151 auto* glx = qtCtx->nativeInterface<QNativeInterface::QGLXContext>();
152 // What Qt's context is can't change while the application runs, so these stay off rather than retry (and
153 // warn) on every context request.
154 if (!glx) {
155 qCWarning(GstGlBridgeLog) << "Qt GL context exposes neither EGL nor GLX; GL bridge disabled";
156 return false;
157 }
158 Display* xdisp = nullptr;
159 if (auto* x11 = qGuiApp->nativeInterface<QNativeInterface::QX11Application>()) {
160 xdisp = x11->display();
161 }
162 if (!xdisp) {
163 qCWarning(GstGlBridgeLog) << "X11 Display unresolvable; GL bridge disabled";
164 return false;
165 }
166 GstGLDisplayX11* displayX11 = gst_gl_display_x11_new_with_display(xdisp);
167 if (!displayX11) {
168 qCWarning(GstGlBridgeLog) << "gst_gl_display_x11_new_with_display failed";
169 return retryLater();
170 }
171 _display = GST_GL_DISPLAY(displayX11);
172 _context = gst_gl_context_new_wrapped(_display, reinterpret_cast<guintptr>(glx->nativeContext()),
173 GST_GL_PLATFORM_GLX, wrappedGlApi(*qtCtx));
174 if (!_context) {
175 qCWarning(GstGlBridgeLog) << "gst_gl_context_new_wrapped (GLX) failed";
176 gst_clear_object(&_display);
177 return retryLater();
178 }
179 qCInfo(GstGlBridgeLog) << "GL bridge primed (GLX)";
180#else
181 // Permanent for the application: Qt's context won't turn into an EGL one.
182 qCWarning(GstGlBridgeLog) << "Qt EGLContext unresolvable and GLX bridge not built; GL bridge disabled";
183 return false;
184#endif
185 }
186
187 // gst-gl takes the API of a context it creates from the display, not from the context it shares with, and would
188 // pick OpenGL ES or desktop OpenGL by its own order of preference. EGL defines sharing within one client API only.
189 gst_gl_display_filter_gl_api(_display, gst_gl_context_get_gl_api(_context));
190
191 // No gst_gl_context_fill_info(): it reads the context current on the calling thread, and Qt's global share context
192 // is never current on any; nothing fills a wrap later either. gst-gl only shares with the wrap (handle, platform
193 // and API, all set above) and runs no GL through it. Unfilled, it carries no EGLConfig for gst-gl to copy, so
194 // gst-gl chooses the config of its own contexts, one that fits the surface it creates for them.
195 qCDebug(GstGlBridgeLog) << "GL bridge primed: display=" << _display << "context=" << _context;
196 return true;
197}
198
199GstObject* GlContextBridge::refObject(const char* contextType)
200{
201 if (g_strcmp0(contextType, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
202 return _display ? GST_OBJECT(gst_object_ref(_display)) : nullptr;
203 }
204 return _context ? GST_OBJECT(gst_object_ref(_context)) : nullptr;
205}
206
207GstContext* GlContextBridge::buildContext(const char* contextType, GstObject* object)
208{
209 if (g_strcmp0(contextType, GST_GL_DISPLAY_CONTEXT_TYPE) == 0) {
210 GstContext* ctx = gst_context_new(GST_GL_DISPLAY_CONTEXT_TYPE, TRUE);
211 gst_context_set_gl_display(ctx, GST_GL_DISPLAY(object));
212 return ctx;
213 }
214 GstContext* ctx = gst_context_new(kGlAppContextType, TRUE);
215 GstStructure* s = gst_context_writable_structure(ctx);
216 gst_structure_set(s, "context", GST_TYPE_GL_CONTEXT, GST_GL_CONTEXT(object), NULL);
217 return ctx;
218}
219
220void GlContextBridge::clearLocked()
221{
222 if (_context) {
223 gst_clear_object(&_retiredContext);
224 _retiredContext = std::exchange(_context, nullptr);
225 }
226 gst_clear_object(&_display);
227}
228
229bool GlContextBridge::shares(GstGLContext* context)
230{
231 const QMutexLocker lock(&_mutex);
232 const auto sharesWith = [context](GstGLContext* wrapped) {
233 return wrapped && ((wrapped == context) || gst_gl_context_can_share(wrapped, context));
234 };
235 return context && (sharesWith(_context) || sharesWith(_retiredContext));
236}
237
238} // namespace
239
240GstContextBridgeBase& GstGlContextBridge::instance()
241{
242 return s_bridge;
243}
244
245bool GstGlContextBridge::sharesQtContext(GstGLContext* context)
246{
247 return s_bridge.shares(context);
248}
249
250#endif // QGC_HAS_GST_GLMEMORY_GPU_PATH
#define qGuiApp
#define QGC_LOGGING_CATEGORY(name, categoryStr)