3 * Copyright (C) 2014 Matthew Waters <ystreet00@gmail.com>
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.
22 * SECTION:gstgldisplay_egl
23 * @short_description: EGL Display connection
24 * @title: GstGLDisplayEGL
25 * @see_also: #GstGLDisplay
27 * #GstGLDisplayEGL represents a connection to an EGL `EGLDisplay` handle created
28 * internally (gst_gl_display_egl_new()) or wrapped by the application
29 * (gst_gl_display_egl_new_with_egl_display())
36 #include "gstgldisplay_egl.h"
38 #include <gst/gl/gstglfeature.h>
41 #include "gsteglimage.h"
42 #include "gstglmemoryegl.h"
44 GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
45 #define GST_CAT_DEFAULT gst_gl_display_debug
47 #ifndef EGL_PLATFORM_X11
48 #define EGL_PLATFORM_X11 0x31D5
50 #ifndef EGL_PLATFORM_WAYLAND
51 #define EGL_PLATFORM_WAYLAND 0x31D8
53 #ifndef EGL_PLATFORM_GBM_MESA
54 #define EGL_PLATFORM_GBM_MESA 0x31D7
56 #ifndef EGL_PLATFORM_ANDROID
57 #define EGL_PLATFORM_ANDROID 0x3141
60 typedef EGLDisplay (*_gst_eglGetPlatformDisplay_type) (EGLenum platform,
61 void *native_display, const EGLint * attrib_list);
63 G_DEFINE_TYPE (GstGLDisplayEGL, gst_gl_display_egl, GST_TYPE_GL_DISPLAY);
65 static void gst_gl_display_egl_finalize (GObject * object);
66 static guintptr gst_gl_display_egl_get_handle (GstGLDisplay * display);
69 gst_gl_display_egl_class_init (GstGLDisplayEGLClass * klass)
71 GST_GL_DISPLAY_CLASS (klass)->get_handle =
72 GST_DEBUG_FUNCPTR (gst_gl_display_egl_get_handle);
74 G_OBJECT_CLASS (klass)->finalize = gst_gl_display_egl_finalize;
78 gst_gl_display_egl_init (GstGLDisplayEGL * display_egl)
80 GstGLDisplay *display = (GstGLDisplay *) display_egl;
82 display->type = GST_GL_DISPLAY_TYPE_EGL;
83 display_egl->foreign_display = FALSE;
85 gst_gl_memory_egl_init_once ();
89 gst_gl_display_egl_finalize (GObject * object)
91 GstGLDisplayEGL *display_egl = GST_GL_DISPLAY_EGL (object);
93 if (display_egl->display && !display_egl->foreign_display) {
94 eglTerminate (display_egl->display);
95 display_egl->display = NULL;
98 G_OBJECT_CLASS (gst_gl_display_egl_parent_class)->finalize (object);
102 * gst_gl_display_egl_get_from_native:
103 * @type: a #GstGLDisplayType
104 * @display: pointer to a display (or 0)
106 * Attempts to create a new `EGLDisplay` from @display. If @type is
107 * %GST_GL_DISPLAY_TYPE_ANY, then @display must be 0. @type must not be
108 * %GST_GL_DISPLAY_TYPE_NONE.
110 * Returns: A `EGLDisplay` or `EGL_NO_DISPLAY`
115 gst_gl_display_egl_get_from_native (GstGLDisplayType type, guintptr display)
117 const gchar *egl_exts;
118 EGLDisplay ret = EGL_NO_DISPLAY;
119 _gst_eglGetPlatformDisplay_type _gst_eglGetPlatformDisplay;
121 g_return_val_if_fail (type != GST_GL_DISPLAY_TYPE_NONE, EGL_NO_DISPLAY);
122 g_return_val_if_fail ((type != GST_GL_DISPLAY_TYPE_ANY && display != 0)
123 || (type == GST_GL_DISPLAY_TYPE_ANY && display == 0), EGL_NO_DISPLAY);
125 /* given an EGLDisplay already */
126 if (type == GST_GL_DISPLAY_TYPE_EGL)
127 return (gpointer) display;
129 egl_exts = eglQueryString (EGL_NO_DISPLAY, EGL_EXTENSIONS);
130 GST_DEBUG ("egl no display extensions: %s", egl_exts);
132 if (eglGetError () != EGL_SUCCESS || !egl_exts)
133 goto default_display;
135 /* check if we can actually choose the egl display type */
136 if (!gst_gl_check_extension ("EGL_KHR_client_get_all_proc_addresses",
138 goto default_display;
139 if (!gst_gl_check_extension ("EGL_EXT_platform_base", egl_exts))
140 goto default_display;
142 _gst_eglGetPlatformDisplay = (_gst_eglGetPlatformDisplay_type)
143 eglGetProcAddress ("eglGetPlatformDisplay");
144 if (!_gst_eglGetPlatformDisplay)
145 _gst_eglGetPlatformDisplay = (_gst_eglGetPlatformDisplay_type)
146 eglGetProcAddress ("eglGetPlatformDisplayEXT");
147 if (!_gst_eglGetPlatformDisplay)
148 goto default_display;
150 /* try each platform in turn */
151 #if GST_GL_HAVE_WINDOW_X11
152 if (ret == EGL_NO_DISPLAY && (type & GST_GL_DISPLAY_TYPE_X11) &&
153 (gst_gl_check_extension ("EGL_KHR_platform_x11", egl_exts) ||
154 gst_gl_check_extension ("EGL_EXT_platform_x11", egl_exts))) {
155 ret = _gst_eglGetPlatformDisplay (EGL_PLATFORM_X11, (gpointer) display,
159 #if GST_GL_HAVE_WINDOW_WAYLAND
160 if (ret == EGL_NO_DISPLAY && (type & GST_GL_DISPLAY_TYPE_WAYLAND) &&
161 (gst_gl_check_extension ("EGL_KHR_platform_wayland", egl_exts) ||
162 gst_gl_check_extension ("EGL_EXT_platform_wayland", egl_exts))) {
163 ret = _gst_eglGetPlatformDisplay (EGL_PLATFORM_WAYLAND, (gpointer) display,
167 #if GST_GL_HAVE_WINDOW_GBM
168 if (ret == EGL_NO_DISPLAY && (type & GST_GL_DISPLAY_TYPE_GBM) &&
169 (gst_gl_check_extension ("EGL_MESA_platform_gbm", egl_exts) ||
170 gst_gl_check_extension ("EGL_KHR_platform_gbm", egl_exts))) {
171 ret = _gst_eglGetPlatformDisplay (EGL_PLATFORM_GBM_MESA, (gpointer) display,
175 /* android only has one winsys/display connection */
177 if (ret != EGL_NO_DISPLAY)
180 /* otherwise rely on the implementation to choose the correct display
181 * based on the pointer */
183 return (gpointer) eglGetDisplay ((EGLNativeDisplayType) display);
187 * gst_gl_display_egl_new:
189 * Create a new #GstGLDisplayEGL using the default EGL_DEFAULT_DISPLAY.
191 * Returns: (transfer full): a new #GstGLDisplayEGL or %NULL
194 gst_gl_display_egl_new (void)
196 GstGLDisplayEGL *ret;
198 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
200 ret = g_object_new (GST_TYPE_GL_DISPLAY_EGL, NULL);
201 gst_object_ref_sink (ret);
203 gst_gl_display_egl_get_from_native (GST_GL_DISPLAY_TYPE_ANY, 0);
206 GST_INFO ("Failed to open EGL display connection");
213 * gst_gl_display_egl_new_with_display:
214 * @display: an existing and connected EGLDisplay
216 * Creates a new display connection from a EGLDisplay.
218 * Returns: (transfer full): a new #GstGLDisplayEGL
223 gst_gl_display_egl_new_with_egl_display (gpointer display)
225 GstGLDisplayEGL *ret;
227 g_return_val_if_fail (display != NULL, NULL);
229 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
231 ret = g_object_new (GST_TYPE_GL_DISPLAY_EGL, NULL);
232 gst_object_ref_sink (ret);
234 ret->display = display;
235 ret->foreign_display = TRUE;
241 _ref_if_set (gpointer data, gpointer user_data)
244 gst_object_ref (data);
249 * gst_gl_display_egl_from_gl_display:
250 * @display: an existing #GstGLDisplay
252 * Creates a EGL display connection from a native Display.
254 * This function will return the same value for multiple calls with the same
257 * Returns: (transfer full): a new #GstGLDisplayEGL
262 gst_gl_display_egl_from_gl_display (GstGLDisplay * display)
264 GstGLDisplayEGL *ret;
265 GstGLDisplayType display_type;
266 guintptr native_display;
268 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), NULL);
270 GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
272 if (GST_IS_GL_DISPLAY_EGL (display)) {
273 GST_LOG_OBJECT (display, "display %" GST_PTR_FORMAT "is already a "
274 "GstGLDisplayEGL", display);
275 return gst_object_ref (display);
278 /* try to get a previously set GstGLDisplayEGL */
279 ret = g_object_dup_data (G_OBJECT (display), GST_GL_DISPLAY_EGL_NAME,
280 (GDuplicateFunc) _ref_if_set, NULL);
281 if (ret && GST_IS_GL_DISPLAY_EGL (ret)) {
282 GST_LOG_OBJECT (display, "display %" GST_PTR_FORMAT "already has a "
283 "GstGLDisplayEGL %" GST_PTR_FORMAT, display, ret);
288 gst_object_unref (ret);
290 display_type = gst_gl_display_get_handle_type (display);
291 native_display = gst_gl_display_get_handle (display);
293 g_return_val_if_fail (native_display != 0, NULL);
294 g_return_val_if_fail (display_type != GST_GL_DISPLAY_TYPE_NONE, NULL);
296 ret = g_object_new (GST_TYPE_GL_DISPLAY_EGL, NULL);
297 gst_object_ref_sink (ret);
300 gst_gl_display_egl_get_from_native (display_type, native_display);
303 GST_WARNING_OBJECT (ret, "failed to get EGLDisplay from native display");
304 gst_object_unref (ret);
307 g_object_set_data_full (G_OBJECT (display), GST_GL_DISPLAY_EGL_NAME,
308 gst_object_ref (ret), (GDestroyNotify) gst_object_unref);
314 gst_gl_display_egl_get_handle (GstGLDisplay * display)
316 return (guintptr) GST_GL_DISPLAY_EGL (display)->display;