qt: don't use libEGL functions when we don't link to libEGL
[platform/upstream/gst-plugins-good.git] / ext / qt / gstqtglutility.cc
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)
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 #endif
46 #endif
47
48 #define GST_CAT_DEFAULT qt_gl_utils_debug
49 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
50
51 GstGLDisplay *
52 gst_qt_get_gl_display ()
53 {
54   GstGLDisplay *display = NULL;
55   QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
56   static volatile gsize _debug;
57
58   g_assert (app != NULL);
59
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);
64   }
65   GST_INFO ("QGuiApplication::instance()->platformName() %s", app->platformName().toUtf8().data());
66
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 ());
71 #endif
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);
82   }
83 #endif
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);
88   }
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);
97
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.
102     */
103     const gchar *fb_dev;
104     gint disp_idx = 0;
105
106     fb_dev = g_getenv ("QT_QPA_EGLFS_FB");
107     if (fb_dev) {
108       if (sscanf (fb_dev, "/dev/fb%d", &disp_idx) != 1)
109         disp_idx = 0;
110     }
111
112     display = (GstGLDisplay *) gst_gl_display_viv_fb_new (disp_idx);
113 #else
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);
116 #endif
117   }
118 #endif
119
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 ();
123 #endif
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 ();
127 #endif
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 ();
131 #endif
132
133   if (!display)
134     display = gst_gl_display_new ();
135
136   return display;
137 }
138
139 gboolean
140 gst_qt_get_gl_wrapcontext (GstGLDisplay * display,
141     GstGLContext **wrap_glcontext, GstGLContext **context)
142 {
143   GstGLPlatform platform = (GstGLPlatform) 0;
144   GstGLAPI gl_api;
145   guintptr gl_handle;
146   GError *error = NULL;
147
148   g_return_val_if_fail (display != NULL && wrap_glcontext != NULL, FALSE);
149
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;
156 #endif
157   }
158 #endif
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;
162   }
163 #endif
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)) {
167 #else
168   if (GST_IS_GL_DISPLAY_EGL (display)) {
169 #endif
170     platform = GST_GL_PLATFORM_EGL;
171   }
172 #endif
173   if (platform == 0) {
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;
180 #else
181     GST_ERROR ("Unknown platform");
182     return FALSE;
183 #endif
184   }
185
186   gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
187   gl_handle = gst_gl_context_get_current_gl_context (platform);
188   if (gl_handle)
189     *wrap_glcontext =
190         gst_gl_context_new_wrapped (display, gl_handle,
191         platform, gl_api);
192
193   if (!*wrap_glcontext) {
194     GST_ERROR ("cannot wrap qt OpenGL context");
195     return FALSE;
196   }
197  
198   (void) platform;
199   (void) gl_api;
200   (void) gl_handle;
201
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;
207     return FALSE;
208   } else {
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);
212
213     G_STMT_START {
214       GstGLWindow *window;
215       HDC device;
216
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.
220        *
221        * The workaround here is to temporarily disable Qt's GL context while we
222        * set up our own.
223        *
224        * Sometimes wglCreateContextAttribsARB()
225        * exists, but isn't functional (some Intel drivers), so it's easiest to do this
226        * unconditionally.
227        */
228       *context = gst_gl_context_new (display);
229       window = gst_gl_context_get_window (*context);
230       device = (HDC) gst_gl_window_get_display (window);
231
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);
237         *context = NULL;
238         g_object_unref (*wrap_glcontext);
239         *wrap_glcontext = NULL;
240         wglMakeCurrent (device, (HGLRC) gl_handle);
241         return FALSE;
242       }
243       wglMakeCurrent (device, (HGLRC) gl_handle);
244     }
245 #endif
246     gst_gl_context_activate (*wrap_glcontext, FALSE);
247   } G_STMT_END;
248
249   return TRUE;
250 }