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 "gstqtglutility.h"
26 #include <QtGui/QGuiApplication>
27
28 #if GST_GL_HAVE_WINDOW_X11 && defined (HAVE_QT_X11)
29 #include <QX11Info>
30 #include <gst/gl/x11/gstgldisplay_x11.h>
31 #endif
32
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>
36 #endif
37
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>
42 #else
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>
47 #endif
48 #endif
49 #endif
50
51 #include <gst/gl/gstglfuncs.h>
52
53 #define GST_CAT_DEFAULT qt_gl_utils_debug
54 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
55
56 GstGLDisplay *
gst_qt_get_gl_display()57 gst_qt_get_gl_display ()
58 {
59 GstGLDisplay *display = NULL;
60 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
61 static volatile gsize _debug;
62
63 g_assert (app != NULL);
64
65 if (g_once_init_enter ((unsigned long *)&_debug)) {
66 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglutility", 0,
67 "Qt gl utility functions");
68 g_once_init_leave ((unsigned long *)&_debug, 1);
69 }
70 GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
71
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 ());
76 #endif
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);
87 }
88 #endif
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);
93 }
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);
102
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.
107 */
108 const gchar *fb_dev;
109 gint disp_idx = 0;
110
111 fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
112 if (fb_dev) {
113 if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
114 disp_idx = 0;
115 }
116
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);
125 #else
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);
128 #endif
129 }
130 #endif
131
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 ();
135 #endif
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 ();
139 #endif
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 ();
143 #endif
144
145 if (!display)
146 display = gst_gl_display_new ();
147
148 return display;
149 }
150
151 gboolean
gst_qt_get_gl_wrapcontext(GstGLDisplay * display,GstGLContext ** wrap_glcontext,GstGLContext ** context)152 gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
153 GstGLContext **wrap_glcontext, GstGLContext **context)
154 {
155 GstGLPlatform platform = (GstGLPlatform) 0;
156 GstGLAPI gl_api;
157 guintptr gl_handle;
158 GError *error = NULL;
159
160 g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
161
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;
168 #endif
169 }
170 #endif
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;
174 }
175 #endif
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)) {
179 #else
180 if (GST_IS_GL_DISPLAY_EGL (display)) {
181 #endif
182 platform = GST_GL_PLATFORM_EGL;
183 }
184 #endif
185 if (platform == 0) {
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;
194 #else
195 GST_ERROR ("Unknown platform");
196 return FALSE;
197 #endif
198 }
199
200 gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
201 gl_handle = gst_gl_context_get_current_gl_context (platform);
202 if (gl_handle)
203 *wrap_glcontext =
204 gst_gl_context_new_wrapped (display, gl_handle,
205 platform, gl_api);
206
207 if (!*wrap_glcontext) {
208 GST_ERROR ("cannot wrap qt OpenGL context");
209 return FALSE;
210 }
211
212 (void) platform;
213 (void) gl_api;
214 (void) gl_handle;
215
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;
221 return FALSE;
222 } else {
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);
226
227 G_STMT_START {
228 GstGLWindow *window;
229 HDC device;
230
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.
234 *
235 * The workaround here is to temporarily disable Qt's GL context while we
236 * set up our own.
237 *
238 * Sometimes wglCreateContextAttribsARB()
239 * exists, but isn't functional (some Intel drivers), so it's easiest to do this
240 * unconditionally.
241 */
242 *context = gst_gl_context_new (display);
243 window = gst_gl_context_get_window (*context);
244 device = (HDC) gst_gl_window_get_display (window);
245
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);
251 *context = NULL;
252 g_object_unref (*wrap_glcontext);
253 *wrap_glcontext = NULL;
254 wglMakeCurrent (device, (HGLRC) gl_handle);
255 return FALSE;
256 }
257 wglMakeCurrent (device, (HGLRC) gl_handle);
258 } G_STMT_END;
259 #endif
260 gst_gl_context_activate (*wrap_glcontext, FALSE);
261 }
262
263 return TRUE;
264 }
265