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>
33 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
34 #include <qpa/qplatformnativeinterface.h>
35 #include <gst/gl/wayland/gstgldisplay_wayland.h>
38 #if GST_GL_HAVE_PLATFORM_EGL && (defined (HAVE_QT_EGLFS) || defined (HAVE_QT_ANDROID))
39 #if GST_GL_HAVE_WINDOW_VIV_FB
40 #include <qpa/qplatformnativeinterface.h>
41 #include <gst/gl/viv-fb/gstgldisplay_viv_fb.h>
43 #include <gst/gl/egl/gstegl.h>
44 #include <gst/gl/egl/gstgldisplay_egl.h>
45 #ifdef HAVE_QT_QPA_HEADER
46 #include <qpa/qplatformnativeinterface.h>
51 #include <gst/gl/gstglfuncs.h>
53 #define GST_CAT_DEFAULT qt_gl_utils_debug
54 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
57 gst_qt_get_gl_display ()
59 GstGLDisplay *display = NULL;
60 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
61 static volatile gsize _debug;
63 g_assert (app != NULL);
65 if (g_once_init_enter (&_debug)) {
66 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0,
67 "Qt gl utility functions");
68 g_once_init_leave (&_debug, 1);
70 GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
72 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
73 if (QString::fromUtf8 ("xcb") == app->platformName())
74 display = (GstGLDisplay *)
75 gst_gl_display_x11_new_with_display (QX11Info::display ());
77 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
78 if (QString::fromUtf8 ("wayland") == app->platformName()
79 || QString::fromUtf8 ("wayland-egl") == app->platformName()){
80 struct wl_display * wayland_display;
81 QPlatformNativeInterface *native =
82 QGuiApplication::platformNativeInterface();
83 wayland_display = (struct wl_display *)
84 native->nativeResourceForWindow("display", NULL);
85 display = (GstGLDisplay *)
86 gst_gl_display_wayland_new_with_display (wayland_display);
89 #if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID
90 if (QString::fromUtf8 ("android") == app->platformName()) {
91 EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
92 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
94 #elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
95 if (QString::fromUtf8("eglfs") == app->platformName()) {
96 #if GST_GL_HAVE_WINDOW_VIV_FB
97 /* FIXME: Could get the display directly from Qt like this
98 QPlatformNativeInterface *native =
99 QGuiApplication::platformNativeInterface();
100 EGLDisplay egl_display = (EGLDisplay)
101 native->nativeResourceForWindow("egldisplay", NULL);
103 However we seem to have no way for getting the EGLNativeDisplayType, aka
104 native_display, via public API. As such we have to assume that display 0
105 is always used. Only way around that is parsing the index the same way as
106 Qt does in QEGLDeviceIntegration::fbDeviceName(), so let's do that.
111 fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
113 if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
117 display = (GstGLDisplay *) gst_gl_display_viv_fb_new (disp_idx);
118 #elif defined(HAVE_QT_QPA_HEADER)
119 QPlatformNativeInterface *native =
120 QGuiApplication::platformNativeInterface();
121 EGLDisplay egl_display = (EGLDisplay)
122 native->nativeResourceForWindow("egldisplay", NULL);
123 if (egl_display != EGL_NO_DISPLAY)
124 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
126 EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
127 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
132 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
133 if (QString::fromUtf8 ("cocoa") == app->platformName())
134 display = (GstGLDisplay *) gst_gl_display_new ();
136 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
137 if (QString::fromUtf8 ("ios") == app->platformName())
138 display = gst_gl_display_new ();
140 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
141 if (QString::fromUtf8 ("windows") == app->platformName())
142 display = gst_gl_display_new ();
146 display = gst_gl_display_new ();
152 gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
153 GstGLContext **wrap_glcontext, GstGLContext **context)
155 GstGLPlatform platform = (GstGLPlatform) 0;
158 GError *error = NULL;
160 g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
162 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
163 if (GST_IS_GL_DISPLAY_X11 (display)) {
164 #if GST_GL_HAVE_PLATFORM_GLX
165 platform = GST_GL_PLATFORM_GLX;
166 #elif GST_GL_HAVE_PLATFORM_EGL
167 platform = GST_GL_PLATFORM_EGL;
171 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
172 if (GST_IS_GL_DISPLAY_WAYLAND (display)) {
173 platform = GST_GL_PLATFORM_EGL;
176 #if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
177 #if GST_GL_HAVE_WINDOW_VIV_FB
178 if (GST_IS_GL_DISPLAY_VIV_FB (display)) {
180 if (GST_IS_GL_DISPLAY_EGL (display)) {
182 platform = GST_GL_PLATFORM_EGL;
186 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
187 platform = GST_GL_PLATFORM_CGL;
188 #elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
189 platform = GST_GL_PLATFORM_EAGL;
190 #elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
191 platform = GST_GL_PLATFORM_WGL;
192 #elif GST_GL_HAVE_WINDOW_ANDROID && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_ANDROID)
193 platform = GST_GL_PLATFORM_EGL;
195 GST_ERROR ("Unknown platform");
200 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
201 gl_handle = gst_gl_context_get_current_gl_context (platform);
204 gst_gl_context_new_wrapped (display, gl_handle,
207 if (!*wrap_glcontext) {
208 GST_ERROR ("cannot wrap qt OpenGL context");
216 gst_gl_context_activate(*wrap_glcontext, TRUE);
217 if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) {
218 GST_ERROR ("failed to retrieve qt context info: %s", error->message);
219 g_object_unref (*wrap_glcontext);
220 *wrap_glcontext = NULL;
223 gst_gl_display_filter_gl_api (display, gst_gl_context_get_gl_api (*wrap_glcontext));
224 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
225 g_return_val_if_fail (context != NULL, FALSE);
231 /* If there's no wglCreateContextAttribsARB() support, then we would fallback to
232 * wglShareLists() which will fail with ERROR_BUSY (0xaa) if either of the GL
233 * contexts are current in any other thread.
235 * The workaround here is to temporarily disable Qt's GL context while we
238 * Sometimes wglCreateContextAttribsARB()
239 * exists, but isn't functional (some Intel drivers), so it's easiest to do this
242 *context = gst_gl_context_new (display);
243 window = gst_gl_context_get_window (*context);
244 device = (HDC) gst_gl_window_get_display (window);
246 wglMakeCurrent (device, 0);
247 gst_object_unref (window);
248 if (!gst_gl_context_create (*context, *wrap_glcontext, &error)) {
249 GST_ERROR ("failed to create shared GL context: %s", error->message);
250 g_object_unref (*context);
252 g_object_unref (*wrap_glcontext);
253 *wrap_glcontext = NULL;
254 wglMakeCurrent (device, (HGLRC) gl_handle);
257 wglMakeCurrent (device, (HGLRC) gl_handle);
260 gst_gl_context_activate (*wrap_glcontext, FALSE);