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)
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>
48 #define GST_CAT_DEFAULT qt_gl_utils_debug
49 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 gst_qt_get_gl_display ()
54 GstGLDisplay *display = NULL;
55 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
56 static volatile gsize _debug;
58 g_assert (app != NULL);
60 if (g_once_init_enter (&_debug)) {
61 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0,
62 "Qt gl utility functions");
63 g_once_init_leave (&_debug, 1);
65 GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
67 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
68 if (QString::fromUtf8 ("xcb") == app->platformName())
69 display = (GstGLDisplay *)
70 gst_gl_display_x11_new_with_display (QX11Info::display ());
72 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_WAYLAND)
73 if (QString::fromUtf8 ("wayland") == app->platformName()
74 || QString::fromUtf8 ("wayland-egl") == app->platformName()){
75 struct wl_display * wayland_display;
76 QPlatformNativeInterface *native =
77 QGuiApplication::platformNativeInterface();
78 wayland_display = (struct wl_display *)
79 native->nativeResourceForWindow("display", NULL);
80 display = (GstGLDisplay *)
81 gst_gl_display_wayland_new_with_display (wayland_display);
84 #if GST_GL_HAVE_PLATFORM_EGL && GST_GL_HAVE_WINDOW_ANDROID
85 if (QString::fromUtf8 ("android") == app->platformName()) {
86 EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
87 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
89 #elif GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
90 if (QString::fromUtf8("eglfs") == app->platformName()) {
91 #if GST_GL_HAVE_WINDOW_VIV_FB
92 /* FIXME: Could get the display directly from Qt like this
93 QPlatformNativeInterface *native =
94 QGuiApplication::platformNativeInterface();
95 EGLDisplay egl_display = (EGLDisplay)
96 native->nativeResourceForWindow("egldisplay", NULL);
98 However we seem to have no way for getting the EGLNativeDisplayType, aka
99 native_display, via public API. As such we have to assume that display 0
100 is always used. Only way around that is parsing the index the same way as
101 Qt does in QEGLDeviceIntegration::fbDeviceName(), so let's do that.
106 fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
108 if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
112 display = (GstGLDisplay *) gst_gl_display_viv_fb_new (disp_idx);
114 EGLDisplay egl_display = (EGLDisplay) gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
115 display = (GstGLDisplay *) gst_gl_display_egl_new_with_egl_display (egl_display);
120 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
121 if (QString::fromUtf8 ("cocoa") == app->platformName())
122 display = (GstGLDisplay *) gst_gl_display_new ();
124 #if GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
125 if (QString::fromUtf8 ("ios") == app->platformName())
126 display = gst_gl_display_new ();
128 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
129 if (QString::fromUtf8 ("windows") == app->platformName())
130 display = gst_gl_display_new ();
134 display = gst_gl_display_new ();
140 gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
141 GstGLContext **wrap_glcontext, GstGLContext **context)
143 GstGLPlatform platform = (GstGLPlatform) 0;
146 GError *error = NULL;
148 g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
150 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
151 if (GST_IS_GL_DISPLAY_X11 (display)) {
152 #if GST_GL_HAVE_PLATFORM_GLX
153 platform = GST_GL_PLATFORM_GLX;
154 #elif GST_GL_HAVE_PLATFORM_EGL
155 platform = GST_GL_PLATFORM_EGL;
159 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (HAVE_QT_WAYLAND)
160 if (GST_IS_GL_DISPLAY_WAYLAND (display)) {
161 platform = GST_GL_PLATFORM_EGL;
164 #if GST_GL_HAVE_PLATFORM_EGL && defined (HAVE_QT_EGLFS)
165 #if GST_GL_HAVE_WINDOW_VIV_FB
166 if (GST_IS_GL_DISPLAY_VIV_FB (display)) {
168 if (GST_IS_GL_DISPLAY_EGL (display)) {
170 platform = GST_GL_PLATFORM_EGL;
174 #if GST_GL_HAVE_WINDOW_COCOA && GST_GL_HAVE_PLATFORM_CGL && defined (HAVE_QT_MAC)
175 platform = GST_GL_PLATFORM_CGL;
176 #elif GST_GL_HAVE_WINDOW_EAGL && GST_GL_HAVE_PLATFORM_EAGL && defined (HAVE_QT_IOS)
177 platform = GST_GL_PLATFORM_EAGL;
178 #elif GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
179 platform = GST_GL_PLATFORM_WGL;
181 GST_ERROR ("Unknown platform");
186 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
187 gl_handle = gst_gl_context_get_current_gl_context (platform);
190 gst_gl_context_new_wrapped (display, gl_handle,
193 if (!*wrap_glcontext) {
194 GST_ERROR ("cannot wrap qt OpenGL context");
202 gst_gl_context_activate (*wrap_glcontext, TRUE);
203 if (!gst_gl_context_fill_info (*wrap_glcontext, &error)) {
204 GST_ERROR ("failed to retrieve qt context info: %s", error->message);
205 g_object_unref (*wrap_glcontext);
206 *wrap_glcontext = NULL;
209 gst_gl_display_filter_gl_api (display, gst_gl_context_get_gl_api (*wrap_glcontext));
210 #if GST_GL_HAVE_WINDOW_WIN32 && GST_GL_HAVE_PLATFORM_WGL && defined (HAVE_QT_WIN32)
211 g_return_val_if_fail (context != NULL, FALSE);
217 /* If there's no wglCreateContextAttribsARB() support, then we would fallback to
218 * wglShareLists() which will fail with ERROR_BUSY (0xaa) if either of the GL
219 * contexts are current in any other thread.
221 * The workaround here is to temporarily disable Qt's GL context while we
224 * Sometimes wglCreateContextAttribsARB()
225 * exists, but isn't functional (some Intel drivers), so it's easiest to do this
228 *context = gst_gl_context_new (display);
229 window = gst_gl_context_get_window (*context);
230 device = (HDC) gst_gl_window_get_display (window);
232 wglMakeCurrent (device, 0);
233 gst_object_unref (window);
234 if (!gst_gl_context_create (*context, *wrap_glcontext, &error)) {
235 GST_ERROR ("%p failed to create shared GL context: %s", this, error->message);
236 g_object_unref (*context);
238 g_object_unref (*wrap_glcontext);
239 *wrap_glcontext = NULL;
240 wglMakeCurrent (device, (HGLRC) gl_handle);
243 wglMakeCurrent (device, (HGLRC) gl_handle);
246 gst_gl_context_activate (*wrap_glcontext, FALSE);