QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
gstqt6glutility.cc
Go to the documentation of this file.
1/*
2 * GStreamer
3 * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include "gstqt6glutility.h"
26#include <QtGui/QGuiApplication>
27#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
28#include <gst/gl/x11/gstgldisplay_x11.h>
29//#include <QtPlatformHeaders/QGLXNativeContext>
30#endif
31#if GST_GL_HAVE_PLATFORM_EGL && (defined (HAVE_QT_WAYLAND) || defined (HAVE_QT_EGLFS) || defined (HAVE_QT_ANDROID))
32#include <gst/gl/egl/gstegl.h>
33#ifdef HAVE_QT_QPA_HEADER
34#include <qpa/qplatformnativeinterface.h>
35#endif
36//#include <QtPlatformHeaders/QEGLNativeContext>
37#include <gst/gl/egl/gstgldisplay_egl.h>
38#endif
39
40#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
41#include <gst/gl/wayland/gstgldisplay_wayland.h>
42#endif
43#if 0
44#if GST_GL_HAVE_WINDOW_VIV_FB
45#include <gst/gl/viv-fb/gstgldisplay_viv_fb.h>
46#endif
47
48#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
49#include <windows.h>
50#include <QtPlatformHeaders/QWGLNativeContext>
51#endif
52#endif
53#include <gst/gl/gstglfuncs.h>
54
55#define GST_CAT_DEFAULT qml6_gl_utils_debug
57
58G_LOCK_DEFINE_STATIC (display_lock);
59static GWeakRef qt_display;
60static gboolean sink_retrieved = FALSE;
61
62GstGLDisplay *
64{
65 GstGLDisplay *display = NULL;
66 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
67 static gsize _debug;
68
69 g_assert (app != NULL);
70
71 if (g_once_init_enter (&_debug)) {
72 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0,
73 "Qt gl utility functions");
74 g_once_init_leave (&_debug, 1);
75 }
76
77 G_LOCK (display_lock);
78 /* XXX: this assumes that only one display will ever be created by Qt */
79 display = static_cast<GstGLDisplay *>(g_weak_ref_get (&qt_display));
80 if (display) {
81 if (sink_retrieved) {
82 GST_INFO ("returning previously created display");
83 G_UNLOCK (display_lock);
84 return display;
85 }
86 gst_clear_object (&display);
87 }
88 if (sink)
89 sink_retrieved = sink;
90
91 GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
92#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
93 if (QString::fromUtf8 ("xcb") == app->platformName()) {
94 auto x11_native = app->nativeInterface<QNativeInterface::QX11Application>();
95 if (x11_native) {
96 display = (GstGLDisplay *)
97 gst_gl_display_x11_new_with_display (x11_native->display());
98 }
99 }
100#endif
101#if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
102 if (QString::fromUtf8 ("wayland") == app->platformName()
103 || QString::fromUtf8 ("wayland-egl") == app->platformName()){
104 struct wl_display * wayland_display;
105 QPlatformNativeInterface *native =
106 QGuiApplication::platformNativeInterface();
107 wayland_display = (struct wl_display *)
108 native->nativeResourceForWindow("display", NULL);
109 display = (GstGLDisplay *)
110 gst_gl_display_wayland_new_with_display (wayland_display);
111 }
112#endif
113#if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID
114 if (QString::fromUtf8 ("android") == app->platformName()) {
115 EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
116 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
117 }
118#elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
119 if (QString::fromUtf8("eglfs") == app->platformName()) {
120#if GST_GL_HAVE_WINDOW_VIV_FB
121 /* FIXME: Could get the display directly from Qt like this
122 * QPlatformNativeInterface *native =
123 * QGuiApplication::platformNativeInterface();
124 * EGLDisplay egl_display = (EGLDisplay)
125 * native->nativeResourceForWindow("egldisplay", NULL);
126 *
127 * However we seem to have no way for getting the EGLNativeDisplayType, aka
128 * native_display, via public API. As such we have to assume that display 0
129 * is always used. Only way around that is parsing the index the same way as
130 * Qt does in QEGLDeviceIntegration::fbDeviceName(), so let's do that.
131 */
132 const gchar *fb_dev;
133 gint disp_idx = 0;
134
135 fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
136 if (fb_dev) {
137 if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
138 disp_idx = 0;
139 }
140
141 display = (GstGLDisplay *) gst_gl_display_viv_fb_new (disp_idx);
142#elif defined(HAVE_QT_QPA_HEADER)
143 QPlatformNativeInterface *native =
144 QGuiApplication::platformNativeInterface();
145 EGLDisplay egl_display = (EGLDisplay)
146 native->nativeResourceForWindow("egldisplay", NULL);
147 if (egl_display != EGL_NO_DISPLAY)
148 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
149#else
150 EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
151 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
152#endif
153 }
154#endif
155#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
156 if (QString::fromUtf8 ("cocoa") == app->platformName())
157 display = (GstGLDisplay *) gst_gl_display_new ();
158#endif
159#if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
160 if (QString::fromUtf8 ("ios") == app->platformName())
161 display = gst_gl_display_new ();
162#endif
163#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
164 if (QString::fromUtf8 ("windows") == app->platformName())
165 display = gst_gl_display_new ();
166#endif
167
168 if (!display)
169 display = gst_gl_display_new ();
170
171 g_weak_ref_set (&qt_display, display);
172 G_UNLOCK (display_lock);
173
174 return display;
175}
176
177gboolean
178gst_qml6_get_gl_wrapcontext (GstGLDisplay * display,
179 GstGLContext **wrap_glcontext, GstGLContext **context)
180{
181 GstGLPlatform G_GNUC_UNUSED platform = (GstGLPlatform) 0;
182 GstGLAPI G_GNUC_UNUSED gl_api;
183 guintptr G_GNUC_UNUSED gl_handle;
184 GstGLContext *current;
185 GError *error = NULL;
186
187 g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
188#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
189 if (GST_IS_GL_DISPLAY_X11 (display)) {
190#if GST_GL_HAVE_PLATFORM_GLX
191 platform = GST_GL_PLATFORM_GLX;
192#elif GST_GL_HAVE_PLATFORM_EGL
193 platform = GST_GL_PLATFORM_EGL;
194#endif
195 }
196#endif
197#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
198 if (GST_IS_GL_DISPLAY_WAYLAND (display)) {
199 platform = GST_GL_PLATFORM_EGL;
200 }
201#endif
202#if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
203#if GST_GL_HAVE_WINDOW_VIV_FB
204 if (GST_IS_GL_DISPLAY_VIV_FB (display)) {
205#else
206 if (GST_IS_GL_DISPLAY_EGL (display)) {
207#endif
208 platform = GST_GL_PLATFORM_EGL;
209 }
210#endif
211 if (platform == 0) {
212#if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
213 platform = GST_GL_PLATFORM_CGL;
214#elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
215 platform = GST_GL_PLATFORM_EAGL;
216#elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
217 platform = GST_GL_PLATFORM_WGL;
218#elif GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
219 platform = GST_GL_PLATFORM_EGL;
220#else
221 GST_ERROR ("Unknown platform");
222 return FALSE;
223#endif
224 }
225
226 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
227 gl_handle = gst_gl_context_get_current_gl_context (platform);
228
229 /* see if we already have a current GL context in GStreamer for this thread */
230 current = gst_gl_context_get_current ();
231 if (current && current->display == display) {
232 /* just use current context we found */
233 *wrap_glcontext = static_cast<GstGLContext *> (gst_object_ref (current));
234 }
235 else {
236 if (gl_handle)
237 *wrap_glcontext =
238 gst_gl_context_new_wrapped (display, gl_handle,
239 platform, gl_api);
240
241 if (!*wrap_glcontext) {
242 GST_ERROR ("cannot wrap qt OpenGL context");
243 return FALSE;
244 }
245
246 gst_gl_context_activate(*wrap_glcontext, TRUE);
247 if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) {
248 GST_ERROR ("failed to retrieve qt context info: %s", error->message);
249 gst_gl_context_activate(*wrap_glcontext, FALSE);
250 gst_clear_object (wrap_glcontext);
251 return FALSE;
252 }
253
254 gst_gl_display_filter_gl_api (display, gst_gl_context_get_gl_api (*wrap_glcontext));
255 gst_gl_context_activate (*wrap_glcontext, FALSE);
256 }
257#if 0
258#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
259 g_return_val_if_fail (context != NULL, FALSE);
260
261 G_STMT_START {
262 /* If there's no wglCreateContextAttribsARB() support, then we would fallback to
263 * wglShareLists() which will fail with ERROR_BUSY (0xaa) if either of the GL
264 * contexts are current in any other thread.
265 *
266 * The workaround here is to temporarily disable Qt's GL context while we
267 * set up our own.
268 *
269 * Sometimes wglCreateContextAttribsARB()
270 * exists, but isn't functional (some Intel drivers), so it's easiest to do this
271 * unconditionally.
272 */
273
274 /* retrieve Qt's GL device context as current device context */
275 HDC device = wglGetCurrentDC ();
276
277 *context = gst_gl_context_new (display);
278
279 wglMakeCurrent (NULL, NULL);
280 if (!gst_gl_context_create (*context, *wrap_glcontext, &error)) {
281 GST_ERROR ("failed to create shared GL context: %s", error->message);
282 gst_clear_object (wrap_glcontext);
283 gst_clear_object (context);
284 }
285 wglMakeCurrent (device, (HGLRC) gl_handle);
286
287 if (!*context)
288 return FALSE;
289
290 } G_STMT_END;
291#endif
292#endif
293 return TRUE;
294}
295#if 0
296QVariant
297qt_opengl_native_context_from_gst_gl_context (GstGLContext * context)
298{
299 guintptr handle;
300 GstGLPlatform platform;
301
302 handle = gst_gl_context_get_gl_context (context);
303 platform = gst_gl_context_get_gl_platform (context);
304
305#if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
306 if (platform == GST_GL_PLATFORM_GLX) {
307 GstGLDisplay *display = gst_gl_context_get_display (context);
308 GstGLWindow *window = gst_gl_context_get_window (context);
309 Display *xdisplay = (Display *) gst_gl_display_get_handle (display);
310 Window win = gst_gl_window_get_window_handle (window);
311 gst_object_unref (window);
312 gst_object_unref (display);
313 return QVariant::fromValue(QGLXNativeContext((GLXContext) handle, xdisplay, win));
314 }
315#endif
316#if GST_GL_HAVE_PLATFORM_EGL && (defined (HAVE_QT_WAYLAND) || defined (HAVE_QT_EGLFS) || defined (HAVE_QT_ANDROID))
317 if (platform == GST_GL_PLATFORM_EGL) {
318 EGLDisplay egl_display = EGL_DEFAULT_DISPLAY;
319 GstGLDisplay *display = gst_gl_context_get_display (context);
320 GstGLDisplayEGL *display_egl = gst_gl_display_egl_from_gl_display (display);
321#if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
322 if (gst_gl_display_get_handle_type (display) == GST_GL_DISPLAY_TYPE_WAYLAND) {
323#if 1
324 g_warning ("Qt does not support wrapping native OpenGL contexts "
325 "on wayland. See https://bugreports.qt.io/browse/QTBUG-82528");
326 gst_object_unref (display_egl);
327 gst_object_unref (display);
328 return QVariant::fromValue(nullptr);
329#else
330 if (display_egl)
331 egl_display = (EGLDisplay) gst_gl_display_get_handle ((GstGLDisplay *) display_egl);
332#endif
333 }
334#endif
335 gst_object_unref (display_egl);
336 gst_object_unref (display);
337 return QVariant::fromValue(QEGLNativeContext((EGLContext) handle, egl_display));
338 }
339#endif
340#if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
341 if (platform == GST_GL_PLATFORM_WGL) {
342 GstGLWindow *window = gst_gl_context_get_window (context);
343 guintptr hwnd = gst_gl_window_get_window_handle (window);
344 gst_object_unref (window);
345 return QVariant::fromValue(QWGLNativeContext((HGLRC) handle, (HWND) hwnd));
346 }
347#endif
348 {
349 gchar *platform_s = gst_gl_platform_to_string (platform);
350 g_warning ("Unimplemented configuration! This means either:\n"
351 "1. The qmlgl plugin was built without support for your platform.\n"
352 "2. The necessary code to convert from a GstGLContext to Qt's "
353 "native context type for \'%s\' currently does not exist.",
354 platform_s);
355 g_free (platform_s);
356 }
357 return QVariant::fromValue(nullptr);
358}
359#endif
Error error
gboolean gst_qml6_get_gl_wrapcontext(GstGLDisplay *display, GstGLContext **wrap_glcontext, GstGLContext **context)
GST_DEBUG_CATEGORY_STATIC(qml6_gl_utils_debug)
static gboolean sink_retrieved
G_LOCK_DEFINE_STATIC(display_lock)
GstGLDisplay * gst_qml6_get_gl_display(gboolean sink)
#define GST_CAT_DEFAULT
static GWeakRef qt_display