3 * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
4 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
5 * Copyright (C) 2008 Filippo Argiolas <filippo.argiolas@gmail.com>
6 * Copyright (C) 2013 Matthew Waters <ystreet00@gmail.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:gstgldisplay
26 * @short_description: window system display connection abstraction
27 * @title: GstGLDisplay
28 * @see_also: #GstContext, #GstGLContext, #GstGLWindow
30 * #GstGLDisplay represents a connection to the underlying windowing system.
31 * Elements are required to make use of #GstContext to share and propogate
34 * There are a number of environment variables that influence the choice of
35 * platform and window system specific functionality.
36 * - GST_GL_WINDOW influences the window system to use. Common values are
37 * 'x11', 'wayland', 'win32' or 'cocoa'.
38 * - GST_GL_PLATFORM influences the OpenGL platform to use. Common values are
39 * 'egl', 'glx', 'wgl' or 'cgl'.
40 * - GST_GL_API influences the OpenGL API requested by the OpenGL platform.
41 * Common values are 'opengl' and 'gles2'.
43 * > Certain window systems require a special function to be called to
44 * > initialize threading support. As this GStreamer GL library does not preclude
45 * > concurrent access to the windowing system, it is strongly advised that
46 * > applications ensure that threading support has been initialized before any
47 * > other toolkit/library functionality is accessed. Failure to do so could
48 * > result in sudden application abortion during execution. The most notably
49 * > example of such a function is X11's XInitThreads\().
57 #include "gstgldisplay.h"
59 #if GST_GL_HAVE_WINDOW_COCOA
60 #include <gst/gl/cocoa/gstgldisplay_cocoa.h>
62 #if GST_GL_HAVE_WINDOW_X11
63 #include <gst/gl/x11/gstgldisplay_x11.h>
65 #if GST_GL_HAVE_WINDOW_WAYLAND
66 #include <gst/gl/wayland/gstgldisplay_wayland.h>
68 #if GST_GL_HAVE_PLATFORM_EGL
69 #include <gst/gl/egl/gstgldisplay_egl.h>
70 #include <gst/gl/egl/gsteglimage.h>
71 #include <gst/gl/egl/gstglmemoryegl.h>
73 #if GST_GL_HAVE_WINDOW_VIV_FB
74 #include <gst/gl/viv-fb/gstgldisplay_viv_fb.h>
77 GST_DEBUG_CATEGORY_STATIC (gst_context);
78 GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
79 #define GST_CAT_DEFAULT gst_gl_display_debug
82 GST_DEBUG_CATEGORY_INIT (gst_gl_display_debug, "gldisplay", 0, "opengl display"); \
83 GST_DEBUG_CATEGORY_GET (gst_context, "GST_CONTEXT");
85 G_DEFINE_TYPE_WITH_CODE (GstGLDisplay, gst_gl_display, GST_TYPE_OBJECT,
88 #define GST_GL_DISPLAY_GET_PRIVATE(o) \
89 (G_TYPE_INSTANCE_GET_PRIVATE((o), GST_TYPE_GL_DISPLAY, GstGLDisplayPrivate))
98 static guint gst_gl_display_signals[LAST_SIGNAL] = { 0 };
101 static void gst_gl_display_dispose (GObject * object);
102 static void gst_gl_display_finalize (GObject * object);
103 static guintptr gst_gl_display_default_get_handle (GstGLDisplay * display);
104 static GstGLWindow *gst_gl_display_default_create_window (GstGLDisplay *
107 struct _GstGLDisplayPrivate
113 GThread *event_thread;
120 _unlock_main_thread (GstGLDisplay * display)
122 g_mutex_unlock (&display->priv->thread_lock);
124 return G_SOURCE_REMOVE;
128 _event_thread_main (GstGLDisplay * display)
130 g_mutex_lock (&display->priv->thread_lock);
132 display->main_context = g_main_context_new ();
133 display->main_loop = g_main_loop_new (display->main_context, FALSE);
135 g_main_context_invoke (display->main_context,
136 (GSourceFunc) _unlock_main_thread, display);
138 g_cond_broadcast (&display->priv->thread_cond);
140 g_main_loop_run (display->main_loop);
142 g_mutex_lock (&display->priv->thread_lock);
143 g_main_loop_unref (display->main_loop);
144 g_main_context_unref (display->main_context);
146 display->main_loop = NULL;
147 display->main_context = NULL;
149 g_cond_broadcast (&display->priv->thread_cond);
150 g_mutex_unlock (&display->priv->thread_lock);
156 gst_gl_display_class_init (GstGLDisplayClass * klass)
158 g_type_class_add_private (klass, sizeof (GstGLDisplayPrivate));
161 * GstGLDisplay::create-context:
162 * @object: the #GstGLDisplay
163 * @context: (transfer none): other context to share resources with.
165 * Overrides the @GstGLContext creation mechanism.
166 * It can be called in any thread and it is emitted with
167 * display's object lock held.
169 * Returns: (transfer full): the new context.
171 gst_gl_display_signals[CREATE_CONTEXT] =
172 g_signal_new ("create-context", G_TYPE_FROM_CLASS (klass),
173 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
174 GST_TYPE_GL_CONTEXT, 1, GST_TYPE_GL_CONTEXT);
176 klass->get_handle = gst_gl_display_default_get_handle;
177 klass->create_window = gst_gl_display_default_create_window;
179 G_OBJECT_CLASS (klass)->finalize = gst_gl_display_finalize;
180 G_OBJECT_CLASS (klass)->dispose = gst_gl_display_dispose;
184 gst_gl_display_init (GstGLDisplay * display)
186 display->priv = GST_GL_DISPLAY_GET_PRIVATE (display);
188 display->type = GST_GL_DISPLAY_TYPE_ANY;
189 display->priv->gl_api = GST_GL_API_ANY;
191 g_mutex_init (&display->priv->thread_lock);
192 g_cond_init (&display->priv->thread_cond);
194 display->priv->event_thread = g_thread_new ("gldisplay-event",
195 (GThreadFunc) _event_thread_main, display);
197 g_mutex_lock (&display->priv->thread_lock);
198 while (!display->main_loop)
199 g_cond_wait (&display->priv->thread_cond, &display->priv->thread_lock);
200 g_mutex_unlock (&display->priv->thread_lock);
202 GST_TRACE ("init %p", display);
204 gst_gl_buffer_init_once ();
205 gst_gl_memory_pbo_init_once ();
206 gst_gl_renderbuffer_init_once ();
208 #if GST_GL_HAVE_PLATFORM_EGL
209 gst_gl_memory_egl_init_once ();
214 gst_gl_display_dispose (GObject * object)
216 GstGLDisplay *display = GST_GL_DISPLAY (object);
218 if (display->main_loop)
219 g_main_loop_quit (display->main_loop);
221 if (display->priv->event_thread) {
222 /* can't use g_thread_join() as we could lose the last ref from a user
224 g_mutex_lock (&display->priv->thread_lock);
225 while (display->main_loop)
226 g_cond_wait (&display->priv->thread_cond, &display->priv->thread_lock);
227 g_mutex_unlock (&display->priv->thread_lock);
228 g_thread_unref (display->priv->event_thread);
230 display->priv->event_thread = NULL;
232 if (display->event_source) {
233 g_source_destroy (display->event_source);
234 g_source_unref (display->event_source);
236 display->event_source = NULL;
238 G_OBJECT_CLASS (gst_gl_display_parent_class)->dispose (object);
242 gst_gl_display_finalize (GObject * object)
244 GstGLDisplay *display = GST_GL_DISPLAY (object);
247 GST_TRACE_OBJECT (object, "finalizing");
249 for (l = display->priv->contexts; l; l = l->next) {
250 g_weak_ref_clear ((GWeakRef *) l->data);
254 g_list_free (display->windows);
255 g_list_free (display->priv->contexts);
257 g_cond_clear (&display->priv->thread_cond);
258 g_mutex_clear (&display->priv->thread_lock);
260 G_OBJECT_CLASS (gst_gl_display_parent_class)->finalize (object);
264 * gst_gl_display_new:
266 * Returns: (transfer full): a new #GstGLDisplay
271 gst_gl_display_new (void)
273 GstGLDisplay *display = NULL;
274 const gchar *user_choice, *platform_choice;
275 static volatile gsize _init = 0;
277 if (g_once_init_enter (&_init)) {
278 GST_DEBUG_CATEGORY_INIT (gst_gl_display_debug, "gldisplay", 0,
279 "gldisplay element");
280 g_once_init_leave (&_init, 1);
283 user_choice = g_getenv ("GST_GL_WINDOW");
284 platform_choice = g_getenv ("GST_GL_PLATFORM");
285 GST_INFO ("creating a display, user choice:%s (platform: %s)",
286 GST_STR_NULL (user_choice), GST_STR_NULL (platform_choice));
288 #if GST_GL_HAVE_WINDOW_COCOA
289 if (!display && (!user_choice || g_strstr_len (user_choice, 5, "cocoa"))) {
290 display = GST_GL_DISPLAY (gst_gl_display_cocoa_new ());
295 #if GST_GL_HAVE_WINDOW_X11
296 if (!display && (!user_choice || g_strstr_len (user_choice, 3, "x11")))
297 display = GST_GL_DISPLAY (gst_gl_display_x11_new (NULL));
299 #if GST_GL_HAVE_WINDOW_WAYLAND
300 if (!display && (!user_choice || g_strstr_len (user_choice, 7, "wayland")))
301 display = GST_GL_DISPLAY (gst_gl_display_wayland_new (NULL));
303 #if GST_GL_HAVE_WINDOW_VIV_FB
304 if (!display && (!user_choice || g_strstr_len (user_choice, 6, "viv-fb"))) {
305 const gchar *disp_idx_str = NULL;
307 disp_idx_str = g_getenv ("GST_GL_VIV_FB");
309 gint64 v = g_ascii_strtoll (disp_idx_str, NULL, 10);
310 if (v >= G_MININT && v <= G_MAXINT)
313 display = GST_GL_DISPLAY (gst_gl_display_viv_fb_new (disp_idx));
316 #if GST_GL_HAVE_PLATFORM_EGL
317 if (!display && (!platform_choice
318 || g_strstr_len (platform_choice, 3, "egl")))
319 display = GST_GL_DISPLAY (gst_gl_display_egl_new ());
322 GST_INFO ("Could not create platform/winsys display. user specified %s "
323 "(platform: %s), creating dummy",
324 GST_STR_NULL (user_choice), GST_STR_NULL (platform_choice));
326 display = g_object_new (GST_TYPE_GL_DISPLAY, NULL);
327 gst_object_ref_sink (display);
334 * gst_gl_display_get_handle:
335 * @display: a #GstGLDisplay
337 * Returns: the native handle for the display
342 gst_gl_display_get_handle (GstGLDisplay * display)
344 GstGLDisplayClass *klass;
346 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), 0);
347 klass = GST_GL_DISPLAY_GET_CLASS (display);
348 g_return_val_if_fail (klass->get_handle != NULL, 0);
350 return klass->get_handle (display);
354 gst_gl_display_default_get_handle (GstGLDisplay * display)
360 * gst_gl_display_filter_gl_api:
361 * @display: a #GstGLDisplay
362 * @gl_api: a #GstGLAPI to filter with
364 * limit the use of OpenGL to the requested @gl_api. This is intended to allow
365 * application and elements to request a specific set of OpenGL API's based on
366 * what they support. See gst_gl_context_get_gl_api() for the retreiving the
367 * API supported by a #GstGLContext.
370 gst_gl_display_filter_gl_api (GstGLDisplay * display, GstGLAPI gl_api)
374 g_return_if_fail (GST_IS_GL_DISPLAY (display));
376 gl_api_s = gst_gl_api_to_string (gl_api);
377 GST_TRACE_OBJECT (display, "filtering with api %s", gl_api_s);
380 GST_OBJECT_LOCK (display);
381 display->priv->gl_api &= gl_api;
382 GST_OBJECT_UNLOCK (display);
386 gst_gl_display_get_gl_api_unlocked (GstGLDisplay * display)
388 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), GST_GL_API_NONE);
390 return display->priv->gl_api;
394 * gst_gl_display_get_gl_api:
395 * @display: a #GstGLDisplay
397 * see gst_gl_display_filter_gl_api() for what the returned value represents
399 * Returns: the #GstGLAPI configured for @display
402 gst_gl_display_get_gl_api (GstGLDisplay * display)
406 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), GST_GL_API_NONE);
408 GST_OBJECT_LOCK (display);
409 ret = display->priv->gl_api;
410 GST_OBJECT_UNLOCK (display);
416 * gst_gl_display_get_handle_type:
417 * @display: a #GstGLDisplay
419 * Returns: the #GstGLDisplayType of @display
424 gst_gl_display_get_handle_type (GstGLDisplay * display)
426 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), GST_GL_DISPLAY_TYPE_NONE);
428 return display->type;
432 * gst_context_set_gl_display:
433 * @context: a #GstContext
434 * @display: (transfer none): resulting #GstGLDisplay
436 * Sets @display on @context
441 gst_context_set_gl_display (GstContext * context, GstGLDisplay * display)
445 g_return_if_fail (context != NULL);
448 GST_CAT_LOG (gst_context,
449 "setting GstGLDisplay(%" GST_PTR_FORMAT ") on context(%" GST_PTR_FORMAT
450 ")", display, context);
452 s = gst_context_writable_structure (context);
453 gst_structure_set (s, GST_GL_DISPLAY_CONTEXT_TYPE, GST_TYPE_GL_DISPLAY,
458 * gst_context_get_gl_display:
459 * @context: a #GstContext
460 * @display: (transfer full): resulting #GstGLDisplay
462 * Returns: Whether @display was in @context
467 gst_context_get_gl_display (GstContext * context, GstGLDisplay ** display)
469 const GstStructure *s;
472 g_return_val_if_fail (display != NULL, FALSE);
473 g_return_val_if_fail (context != NULL, FALSE);
475 s = gst_context_get_structure (context);
476 ret = gst_structure_get (s, GST_GL_DISPLAY_CONTEXT_TYPE,
477 GST_TYPE_GL_DISPLAY, display, NULL);
479 GST_CAT_LOG (gst_context, "got GstGLDisplay(%p) from context(%p)", *display,
486 * gst_gl_display_create_context:
487 * @display: a #GstGLDisplay
488 * @other_context: (transfer none): other #GstGLContext to share resources with.
489 * @p_context: (transfer full) (out): resulting #GstGLContext
490 * @error: (allow-none): resulting #GError
492 * It requires the display's object lock to be held.
494 * Returns: whether a new context could be created.
499 gst_gl_display_create_context (GstGLDisplay * display,
500 GstGLContext * other_context, GstGLContext ** p_context, GError ** error)
502 GstGLContext *context = NULL;
503 gboolean ret = FALSE;
505 g_return_val_if_fail (display != NULL, FALSE);
506 g_return_val_if_fail (p_context != NULL, FALSE);
507 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
509 g_signal_emit (display, gst_gl_display_signals[CREATE_CONTEXT], 0,
510 other_context, &context);
513 *p_context = context;
517 context = gst_gl_context_new (display);
519 g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
520 "Failed to create GL context");
524 GST_DEBUG_OBJECT (display,
525 "creating context %" GST_PTR_FORMAT " from other context %"
526 GST_PTR_FORMAT, context, other_context);
528 ret = gst_gl_context_create (context, other_context, error);
531 *p_context = context;
537 * gst_gl_display_create_window:
538 * @display: a #GstGLDisplay
540 * It requires the display's object lock to be held.
542 * Returns: (transfer full): a new #GstGLWindow for @display or %NULL.
545 gst_gl_display_create_window (GstGLDisplay * display)
547 GstGLDisplayClass *klass;
550 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), NULL);
551 klass = GST_GL_DISPLAY_GET_CLASS (display);
552 g_return_val_if_fail (klass->create_window != NULL, NULL);
554 window = klass->create_window (display);
557 display->windows = g_list_prepend (display->windows, window);
563 gst_gl_display_default_create_window (GstGLDisplay * display)
565 return gst_gl_window_new (display);
569 * gst_gl_display_remove_window:
570 * @display: a #GstGLDisplay
571 * @window: a #GstGLWindow to remove
573 * Returns: if @window could be removed from @display
578 gst_gl_display_remove_window (GstGLDisplay * display, GstGLWindow * window)
580 gboolean ret = FALSE;
583 GST_OBJECT_LOCK (display);
584 l = g_list_find (display->windows, window);
586 display->windows = g_list_delete_link (display->windows, l);
589 GST_OBJECT_UNLOCK (display);
595 * gst_gl_display_find_window:
596 * @display: a #GstGLDisplay
597 * @data: (closure): some data to pass to @compare_func
598 * @compare_func: (scope call): a comparison function to run
600 * Execute @compare_func over the list of windows stored by @display. The
601 * first argment to @compare_func is the #GstGLWindow being checked and the
602 * second argument is @data.
604 * Returns: (transfer none): The first #GstGLWindow that causes a match
610 gst_gl_display_find_window (GstGLDisplay * display, gpointer data,
611 GCompareFunc compare_func)
613 GstGLWindow *ret = NULL;
616 GST_OBJECT_LOCK (display);
617 l = g_list_find_custom (display->windows, data, compare_func);
620 GST_OBJECT_UNLOCK (display);
625 static GstGLContext *
626 _get_gl_context_for_thread_unlocked (GstGLDisplay * display, GThread * thread)
628 GstGLContext *context = NULL;
629 GList *prev = NULL, *l = display->priv->contexts;
632 GWeakRef *ref = l->data;
633 GThread *context_thread;
635 context = g_weak_ref_get (ref);
637 /* remove dead contexts */
638 g_weak_ref_clear (l->data);
640 display->priv->contexts = g_list_delete_link (display->priv->contexts, l);
641 l = prev ? prev->next : display->priv->contexts;
645 if (thread == NULL) {
646 GST_DEBUG_OBJECT (display, "Returning GL context %" GST_PTR_FORMAT " for "
647 "NULL thread", context);
651 context_thread = gst_gl_context_get_thread (context);
652 if (thread != context_thread) {
653 g_thread_unref (context_thread);
654 gst_object_unref (context);
661 g_thread_unref (context_thread);
663 GST_DEBUG_OBJECT (display, "Returning GL context %" GST_PTR_FORMAT " for "
664 "thread %p", context, thread);
668 GST_DEBUG_OBJECT (display, "No GL context for thread %p", thread);
673 * gst_gl_display_get_gl_context_for_thread:
674 * @display: a #GstGLDisplay
675 * @thread: a #GThread
677 * Returns: (transfer full): the #GstGLContext current on @thread or %NULL
679 * Must be called with the object lock held.
684 gst_gl_display_get_gl_context_for_thread (GstGLDisplay * display,
687 GstGLContext *context;
689 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), NULL);
691 context = _get_gl_context_for_thread_unlocked (display, thread);
692 GST_DEBUG_OBJECT (display, "returning context %" GST_PTR_FORMAT " for thread "
693 "%p", context, thread);
699 _check_collision (GstGLContext * context, GstGLContext * collision)
701 GThread *thread, *collision_thread;
702 gboolean ret = FALSE;
704 if (!context || !collision)
707 thread = gst_gl_context_get_thread (context);
708 collision_thread = gst_gl_context_get_thread (collision);
710 if (!thread || !collision_thread) {
715 if (thread == collision_thread) {
722 g_thread_unref (thread);
723 if (collision_thread)
724 g_thread_unref (collision_thread);
730 * gst_gl_display_add_context:
731 * @display: a #GstGLDisplay
732 * @context: (transfer none): a #GstGLContext
734 * Returns: whether @context was successfully added. %FALSE may be returned
735 * if there already exists another context for @context's active thread.
737 * Must be called with the object lock held.
742 gst_gl_display_add_context (GstGLDisplay * display, GstGLContext * context)
744 GstGLContext *collision = NULL;
745 GstGLDisplay *context_display;
750 g_return_val_if_fail (GST_IS_GL_DISPLAY (display), FALSE);
751 g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);
753 context_display = gst_gl_context_get_display (context);
754 g_assert (context_display == display);
755 gst_object_unref (context_display);
757 thread = gst_gl_context_get_thread (context);
759 collision = _get_gl_context_for_thread_unlocked (display, thread);
760 g_thread_unref (thread);
762 /* adding the same context is a no-op */
763 if (context == collision) {
764 GST_LOG_OBJECT (display, "Attempting to add the same GL context %"
765 GST_PTR_FORMAT ". Ignoring", context);
770 if (_check_collision (context, collision)) {
771 GST_DEBUG_OBJECT (display, "Collision detected adding GL context "
772 "%" GST_PTR_FORMAT, context);
778 ref = g_new0 (GWeakRef, 1);
779 g_weak_ref_init (ref, context);
781 GST_DEBUG_OBJECT (display, "Adding GL context %" GST_PTR_FORMAT, context);
782 display->priv->contexts = g_list_prepend (display->priv->contexts, ref);
786 gst_object_unref (collision);
788 GST_DEBUG_OBJECT (display, "%ssuccessfully inserted context %" GST_PTR_FORMAT,
789 ret ? "" : "un", context);