3 * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
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.
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.
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.
25 #include "gstqtglutility.h"
26 #include <QtGui/QGuiApplication>
28 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
30 #include <gst/gl/x11/gstgldisplay_x11.h>
31 #include <QtPlatformHeaders/QGLXNativeContext>
34 #if GST_GL_HAVE_PLATFORM_EGL && (defined (HAVE_QT_WAYLAND) || defined (HAVE_QT_EGLFS) || defined (HAVE_QT_ANDROID))
35 #include <gst/gl/egl/gstegl.h>
36 #ifdef HAVE_QT_QPA_HEADER
37 #include QT_QPA_HEADER
39 #include <QtPlatformHeaders/QEGLNativeContext>
40 #include <gst/gl/egl/gstgldisplay_egl.h>
43 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
44 #include <gst/gl/wayland/gstgldisplay_wayland.h>
47 #if GST_GL_HAVE_WINDOW_VIV_FB
48 #include <gst/gl/viv-fb/gstgldisplay_viv_fb.h>
51 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
53 #include <QtPlatformHeaders/QWGLNativeContext>
56 #include <gst/gl/gstglfuncs.h>
58 #define GST_CAT_DEFAULT qt_gl_utils_debug
59 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
61 G_LOCK_DEFINE_STATIC (display_lock);
62 static GWeakRef qt_display;
63 static gboolean sink_retrieved = FALSE;
66 gst_qt_get_gl_display (gboolean sink)
68 GstGLDisplay *display = NULL;
69 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
72 g_assert (app != NULL);
74 if (g_once_init_enter (&_debug)) {
75 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0,
76 "Qt gl utility functions");
77 g_once_init_leave (&_debug, 1);
80 G_LOCK (display_lock);
81 /* XXX: this assumes that only one display will ever be created by Qt */
82 display = static_cast<GstGLDisplay *>(g_weak_ref_get (&qt_display));
85 GST_INFO ("returning previously created display");
86 G_UNLOCK (display_lock);
89 gst_clear_object (&display);
92 sink_retrieved = sink;
94 GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
96 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
97 if (QString::fromUtf8 ("xcb") == app->platformName())
98 display = (GstGLDisplay *)
99 gst_gl_display_x11_new_with_display (QX11Info::display ());
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);
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);
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);
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.
135 fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
137 if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
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);
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);
156 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
157 if (QString::fromUtf8 ("cocoa") == app->platformName())
158 display = (GstGLDisplay *) gst_gl_display_new ();
160 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
161 if (QString::fromUtf8 ("ios") == app->platformName())
162 display = gst_gl_display_new ();
164 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
165 if (QString::fromUtf8 ("windows") == app->platformName())
166 display = gst_gl_display_new ();
170 display = gst_gl_display_new ();
172 g_weak_ref_set (&qt_display, display);
173 G_UNLOCK (display_lock);
179 gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
180 GstGLContext **wrap_glcontext, GstGLContext **context)
182 GstGLPlatform G_GNUC_UNUSED platform = (GstGLPlatform) 0;
183 GstGLAPI G_GNUC_UNUSED gl_api;
184 guintptr G_GNUC_UNUSED gl_handle;
185 GstGLContext *current;
188 g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
190 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
191 if (GST_IS_GL_DISPLAY_X11 (display)) {
192 #if GST_GL_HAVE_PLATFORM_GLX
193 platform = GST_GL_PLATFORM_GLX;
194 #elif GST_GL_HAVE_PLATFORM_EGL
195 platform = GST_GL_PLATFORM_EGL;
199 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
200 if (GST_IS_GL_DISPLAY_WAYLAND (display)) {
201 platform = GST_GL_PLATFORM_EGL;
204 #if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
205 #if GST_GL_HAVE_WINDOW_VIV_FB
206 if (GST_IS_GL_DISPLAY_VIV_FB (display)) {
208 if (GST_IS_GL_DISPLAY_EGL (display)) {
210 platform = GST_GL_PLATFORM_EGL;
214 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
215 platform = GST_GL_PLATFORM_CGL;
216 #elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
217 platform = GST_GL_PLATFORM_EAGL;
218 #elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
219 platform = GST_GL_PLATFORM_WGL;
220 #elif GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
221 platform = GST_GL_PLATFORM_EGL;
223 GST_ERROR ("Unknown platform");
228 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
229 gl_handle = gst_gl_context_get_current_gl_context (platform);
231 /* see if we already have a current GL context in GStreamer for this thread */
232 current = gst_gl_context_get_current ();
233 if (current && current->display == display) {
234 /* just use current context we found */
235 *wrap_glcontext = static_cast<GstGLContext *> (gst_object_ref (current));
240 gst_gl_context_new_wrapped (display, gl_handle,
243 if (!*wrap_glcontext) {
244 GST_ERROR ("cannot wrap qt OpenGL context");
248 gst_gl_context_activate(*wrap_glcontext, TRUE);
249 if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) {
250 GST_ERROR ("failed to retrieve qt context info: %s", error->message);
251 gst_clear_object (wrap_glcontext);
255 gst_gl_display_filter_gl_api (display, gst_gl_context_get_gl_api (*wrap_glcontext));
256 gst_gl_context_activate (*wrap_glcontext, FALSE);
259 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
260 g_return_val_if_fail (context != NULL, FALSE);
263 /* If there's no wglCreateContextAttribsARB() support, then we would fallback to
264 * wglShareLists() which will fail with ERROR_BUSY (0xaa) if either of the GL
265 * contexts are current in any other thread.
267 * The workaround here is to temporarily disable Qt's GL context while we
270 * Sometimes wglCreateContextAttribsARB()
271 * exists, but isn't functional (some Intel drivers), so it's easiest to do this
275 /* retrieve Qt's GL device context as current device context */
276 HDC device = wglGetCurrentDC ();
278 *context = gst_gl_context_new (display);
280 wglMakeCurrent (NULL, NULL);
281 if (!gst_gl_context_create (*context, *wrap_glcontext, &error)) {
282 GST_ERROR ("failed to create shared GL context: %s", error->message);
283 gst_clear_object (wrap_glcontext);
284 gst_clear_object (context);
286 wglMakeCurrent (device, (HGLRC) gl_handle);
298 qt_opengl_native_context_from_gst_gl_context (GstGLContext * context)
301 GstGLPlatform platform;
303 handle = gst_gl_context_get_gl_context (context);
304 platform = gst_gl_context_get_gl_platform (context);
306 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
307 if (platform == GST_GL_PLATFORM_GLX) {
308 GstGLDisplay *display = gst_gl_context_get_display (context);
309 GstGLWindow *window = gst_gl_context_get_window (context);
310 Display *xdisplay = (Display *) gst_gl_display_get_handle (display);
311 Window win = gst_gl_window_get_window_handle (window);
312 gst_object_unref (window);
313 gst_object_unref (display);
314 return QVariant::fromValue(QGLXNativeContext((GLXContext) handle, xdisplay, win));
317 #if GST_GL_HAVE_PLATFORM_EGL && (defined (HAVE_QT_WAYLAND) || defined (HAVE_QT_EGLFS) || defined (HAVE_QT_ANDROID))
318 if (platform == GST_GL_PLATFORM_EGL) {
319 EGLDisplay egl_display = EGL_DEFAULT_DISPLAY;
320 GstGLDisplay *display = gst_gl_context_get_display (context);
321 GstGLDisplayEGL *display_egl = gst_gl_display_egl_from_gl_display (display);
322 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
323 if (gst_gl_display_get_handle_type (display) == GST_GL_DISPLAY_TYPE_WAYLAND) {
325 g_warning ("Qt does not support wrapping native OpenGL contexts "
326 "on wayland. See https://bugreports.qt.io/browse/QTBUG-82528");
327 gst_object_unref (display_egl);
328 gst_object_unref (display);
329 return QVariant::fromValue(nullptr);
332 egl_display = (EGLDisplay) gst_gl_display_get_handle ((GstGLDisplay *) display_egl);
336 gst_object_unref (display_egl);
337 gst_object_unref (display);
338 return QVariant::fromValue(QEGLNativeContext((EGLContext) handle, egl_display));
341 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
342 if (platform == GST_GL_PLATFORM_WGL) {
343 GstGLWindow *window = gst_gl_context_get_window (context);
344 guintptr hwnd = gst_gl_window_get_window_handle (window);
345 gst_object_unref (window);
346 return QVariant::fromValue(QWGLNativeContext((HGLRC) handle, (HWND) hwnd));
350 gchar *platform_s = gst_gl_platform_to_string (platform);
351 g_warning ("Unimplemented configuration! This means either:\n"
352 "1. The qmlgl plugin was built without support for your platform.\n"
353 "2. The necessary code to convert from a GstGLContext to Qt's "
354 "native context type for \'%s\' currently does not exist.",
358 return QVariant::fromValue(nullptr);