gl/viv-fb: Fix user-choice string comparisons
[platform/upstream/gstreamer.git] / gst-libs / gst / gl / gstgldisplay.c
1 /*
2  * GStreamer
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>
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 /**
25  * SECTION:gstgldisplay
26  * @short_description: window system display connection abstraction
27  * @title: GstGLDisplay
28  * @see_also: #GstContext, #GstGLContext, #GstGLWindow
29  *
30  * #GstGLDisplay represents a connection to the underlying windowing system.
31  * Elements are required to make use of #GstContext to share and propogate
32  * a #GstGLDisplay.
33  *
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'.
42  *
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\().
50  */
51
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
55
56 #include "gl.h"
57 #include "gstgldisplay.h"
58
59 #if GST_GL_HAVE_WINDOW_COCOA
60 #include <gst/gl/cocoa/gstgldisplay_cocoa.h>
61 #endif
62 #if GST_GL_HAVE_WINDOW_X11
63 #include <gst/gl/x11/gstgldisplay_x11.h>
64 #endif
65 #if GST_GL_HAVE_WINDOW_WAYLAND
66 #include <gst/gl/wayland/gstgldisplay_wayland.h>
67 #endif
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>
72 #endif
73 #if GST_GL_HAVE_WINDOW_VIV_FB
74 #include <gst/gl/viv-fb/gstgldisplay_viv_fb.h>
75 #endif
76
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
80
81 #define DEBUG_INIT \
82   GST_DEBUG_CATEGORY_INIT (gst_gl_display_debug, "gldisplay", 0, "opengl display"); \
83   GST_DEBUG_CATEGORY_GET (gst_context, "GST_CONTEXT");
84
85 G_DEFINE_TYPE_WITH_CODE (GstGLDisplay, gst_gl_display, GST_TYPE_OBJECT,
86     DEBUG_INIT);
87
88 #define GST_GL_DISPLAY_GET_PRIVATE(o) \
89   (G_TYPE_INSTANCE_GET_PRIVATE((o), GST_TYPE_GL_DISPLAY, GstGLDisplayPrivate))
90
91 enum
92 {
93   SIGNAL_0,
94   CREATE_CONTEXT,
95   LAST_SIGNAL
96 };
97
98 static guint gst_gl_display_signals[LAST_SIGNAL] = { 0 };
99
100
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 *
105     display);
106
107 struct _GstGLDisplayPrivate
108 {
109   GstGLAPI gl_api;
110
111   GList *contexts;
112
113   GThread *event_thread;
114
115   GMutex thread_lock;
116   GCond thread_cond;
117 };
118
119 static gboolean
120 _unlock_main_thread (GstGLDisplay * display)
121 {
122   g_mutex_unlock (&display->priv->thread_lock);
123
124   return G_SOURCE_REMOVE;
125 }
126
127 static gpointer
128 _event_thread_main (GstGLDisplay * display)
129 {
130   g_mutex_lock (&display->priv->thread_lock);
131
132   display->main_context = g_main_context_new ();
133   display->main_loop = g_main_loop_new (display->main_context, FALSE);
134
135   g_main_context_invoke (display->main_context,
136       (GSourceFunc) _unlock_main_thread, display);
137
138   g_cond_broadcast (&display->priv->thread_cond);
139
140   g_main_loop_run (display->main_loop);
141
142   g_mutex_lock (&display->priv->thread_lock);
143   g_main_loop_unref (display->main_loop);
144   g_main_context_unref (display->main_context);
145
146   display->main_loop = NULL;
147   display->main_context = NULL;
148
149   g_cond_broadcast (&display->priv->thread_cond);
150   g_mutex_unlock (&display->priv->thread_lock);
151
152   return NULL;
153 }
154
155 static void
156 gst_gl_display_class_init (GstGLDisplayClass * klass)
157 {
158   g_type_class_add_private (klass, sizeof (GstGLDisplayPrivate));
159
160   /**
161    * GstGLDisplay::create-context:
162    * @object: the #GstGLDisplay
163    * @context: (transfer none): other context to share resources with.
164    *
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.
168    *
169    * Returns: (transfer full): the new context.
170    */
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);
175
176   klass->get_handle = gst_gl_display_default_get_handle;
177   klass->create_window = gst_gl_display_default_create_window;
178
179   G_OBJECT_CLASS (klass)->finalize = gst_gl_display_finalize;
180   G_OBJECT_CLASS (klass)->dispose = gst_gl_display_dispose;
181 }
182
183 static void
184 gst_gl_display_init (GstGLDisplay * display)
185 {
186   display->priv = GST_GL_DISPLAY_GET_PRIVATE (display);
187
188   display->type = GST_GL_DISPLAY_TYPE_ANY;
189   display->priv->gl_api = GST_GL_API_ANY;
190
191   g_mutex_init (&display->priv->thread_lock);
192   g_cond_init (&display->priv->thread_cond);
193
194   display->priv->event_thread = g_thread_new ("gldisplay-event",
195       (GThreadFunc) _event_thread_main, display);
196
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);
201
202   GST_TRACE ("init %p", display);
203
204   gst_gl_buffer_init_once ();
205   gst_gl_memory_pbo_init_once ();
206   gst_gl_renderbuffer_init_once ();
207
208 #if GST_GL_HAVE_PLATFORM_EGL
209   gst_gl_memory_egl_init_once ();
210 #endif
211 }
212
213 static void
214 gst_gl_display_dispose (GObject * object)
215 {
216   GstGLDisplay *display = GST_GL_DISPLAY (object);
217
218   if (display->main_loop)
219     g_main_loop_quit (display->main_loop);
220
221   if (display->priv->event_thread) {
222     /* can't use g_thread_join() as we could lose the last ref from a user
223      * function */
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);
229   }
230   display->priv->event_thread = NULL;
231
232   if (display->event_source) {
233     g_source_destroy (display->event_source);
234     g_source_unref (display->event_source);
235   }
236   display->event_source = NULL;
237
238   G_OBJECT_CLASS (gst_gl_display_parent_class)->dispose (object);
239 }
240
241 static void
242 gst_gl_display_finalize (GObject * object)
243 {
244   GstGLDisplay *display = GST_GL_DISPLAY (object);
245   GList *l;
246
247   GST_TRACE_OBJECT (object, "finalizing");
248
249   for (l = display->priv->contexts; l; l = l->next) {
250     g_weak_ref_clear ((GWeakRef *) l->data);
251     g_free (l->data);
252   }
253
254   g_list_free (display->windows);
255   g_list_free (display->priv->contexts);
256
257   g_cond_clear (&display->priv->thread_cond);
258   g_mutex_clear (&display->priv->thread_lock);
259
260   G_OBJECT_CLASS (gst_gl_display_parent_class)->finalize (object);
261 }
262
263 /**
264  * gst_gl_display_new:
265  *
266  * Returns: (transfer full): a new #GstGLDisplay
267  *
268  * Since: 1.4
269  */
270 GstGLDisplay *
271 gst_gl_display_new (void)
272 {
273   GstGLDisplay *display = NULL;
274   const gchar *user_choice, *platform_choice;
275   static volatile gsize _init = 0;
276
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);
281   }
282
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));
287
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 ());
291     if (!display)
292       return NULL;
293   }
294 #endif
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));
298 #endif
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));
302 #endif
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;
306     gint disp_idx = 0;
307     disp_idx_str = g_getenv ("GST_GL_VIV_FB");
308     if (disp_idx_str) {
309       gint64 v = g_ascii_strtoll (disp_idx_str, NULL, 10);
310       if (v >= G_MININT && v <= G_MAXINT)
311         disp_idx = v;
312     }
313     display = GST_GL_DISPLAY (gst_gl_display_viv_fb_new (disp_idx));
314   }
315 #endif
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 ());
320 #endif
321   if (!display) {
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));
325
326     display = g_object_new (GST_TYPE_GL_DISPLAY, NULL);
327     gst_object_ref_sink (display);
328   }
329
330   return display;
331 }
332
333 /**
334  * gst_gl_display_get_handle:
335  * @display: a #GstGLDisplay
336  *
337  * Returns: the native handle for the display
338  *
339  * Since: 1.4
340  */
341 guintptr
342 gst_gl_display_get_handle (GstGLDisplay * display)
343 {
344   GstGLDisplayClass *klass;
345
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);
349
350   return klass->get_handle (display);
351 }
352
353 static guintptr
354 gst_gl_display_default_get_handle (GstGLDisplay * display)
355 {
356   return 0;
357 }
358
359 /**
360  * gst_gl_display_filter_gl_api:
361  * @display: a #GstGLDisplay
362  * @gl_api: a #GstGLAPI to filter with
363  *
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.
368  */
369 void
370 gst_gl_display_filter_gl_api (GstGLDisplay * display, GstGLAPI gl_api)
371 {
372   gchar *gl_api_s;
373
374   g_return_if_fail (GST_IS_GL_DISPLAY (display));
375
376   gl_api_s = gst_gl_api_to_string (gl_api);
377   GST_TRACE_OBJECT (display, "filtering with api %s", gl_api_s);
378   g_free (gl_api_s);
379
380   GST_OBJECT_LOCK (display);
381   display->priv->gl_api &= gl_api;
382   GST_OBJECT_UNLOCK (display);
383 }
384
385 GstGLAPI
386 gst_gl_display_get_gl_api_unlocked (GstGLDisplay * display)
387 {
388   g_return_val_if_fail (GST_IS_GL_DISPLAY (display), GST_GL_API_NONE);
389
390   return display->priv->gl_api;
391 }
392
393 /**
394  * gst_gl_display_get_gl_api:
395  * @display: a #GstGLDisplay
396  *
397  * see gst_gl_display_filter_gl_api() for what the returned value represents
398  *
399  * Returns: the #GstGLAPI configured for @display
400  */
401 GstGLAPI
402 gst_gl_display_get_gl_api (GstGLDisplay * display)
403 {
404   GstGLAPI ret;
405
406   g_return_val_if_fail (GST_IS_GL_DISPLAY (display), GST_GL_API_NONE);
407
408   GST_OBJECT_LOCK (display);
409   ret = display->priv->gl_api;
410   GST_OBJECT_UNLOCK (display);
411
412   return ret;
413 }
414
415 /**
416  * gst_gl_display_get_handle_type:
417  * @display: a #GstGLDisplay
418  *
419  * Returns: the #GstGLDisplayType of @display
420  *
421  * Since: 1.4
422  */
423 GstGLDisplayType
424 gst_gl_display_get_handle_type (GstGLDisplay * display)
425 {
426   g_return_val_if_fail (GST_IS_GL_DISPLAY (display), GST_GL_DISPLAY_TYPE_NONE);
427
428   return display->type;
429 }
430
431 /**
432  * gst_context_set_gl_display:
433  * @context: a #GstContext
434  * @display: (transfer none): resulting #GstGLDisplay
435  *
436  * Sets @display on @context
437  *
438  * Since: 1.4
439  */
440 void
441 gst_context_set_gl_display (GstContext * context, GstGLDisplay * display)
442 {
443   GstStructure *s;
444
445   g_return_if_fail (context != NULL);
446
447   if (display)
448     GST_CAT_LOG (gst_context,
449         "setting GstGLDisplay(%" GST_PTR_FORMAT ") on context(%" GST_PTR_FORMAT
450         ")", display, context);
451
452   s = gst_context_writable_structure (context);
453   gst_structure_set (s, GST_GL_DISPLAY_CONTEXT_TYPE, GST_TYPE_GL_DISPLAY,
454       display, NULL);
455 }
456
457 /**
458  * gst_context_get_gl_display:
459  * @context: a #GstContext
460  * @display: (transfer full): resulting #GstGLDisplay
461  *
462  * Returns: Whether @display was in @context
463  *
464  * Since: 1.4
465  */
466 gboolean
467 gst_context_get_gl_display (GstContext * context, GstGLDisplay ** display)
468 {
469   const GstStructure *s;
470   gboolean ret;
471
472   g_return_val_if_fail (display != NULL, FALSE);
473   g_return_val_if_fail (context != NULL, FALSE);
474
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);
478
479   GST_CAT_LOG (gst_context, "got GstGLDisplay(%p) from context(%p)", *display,
480       context);
481
482   return ret;
483 }
484
485 /**
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
491  *
492  * It requires the display's object lock to be held.
493  *
494  * Returns: whether a new context could be created.
495  *
496  * Since: 1.6
497  */
498 gboolean
499 gst_gl_display_create_context (GstGLDisplay * display,
500     GstGLContext * other_context, GstGLContext ** p_context, GError ** error)
501 {
502   GstGLContext *context = NULL;
503   gboolean ret = FALSE;
504
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);
508
509   g_signal_emit (display, gst_gl_display_signals[CREATE_CONTEXT], 0,
510       other_context, &context);
511
512   if (context) {
513     *p_context = context;
514     return TRUE;
515   }
516
517   context = gst_gl_context_new (display);
518   if (!context) {
519     g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
520         "Failed to create GL context");
521     return FALSE;
522   }
523
524   GST_DEBUG_OBJECT (display,
525       "creating context %" GST_PTR_FORMAT " from other context %"
526       GST_PTR_FORMAT, context, other_context);
527
528   ret = gst_gl_context_create (context, other_context, error);
529
530   if (ret)
531     *p_context = context;
532
533   return ret;
534 }
535
536 /**
537  * gst_gl_display_create_window:
538  * @display: a #GstGLDisplay
539  *
540  * It requires the display's object lock to be held.
541  *
542  * Returns: (transfer full): a new #GstGLWindow for @display or %NULL.
543  */
544 GstGLWindow *
545 gst_gl_display_create_window (GstGLDisplay * display)
546 {
547   GstGLDisplayClass *klass;
548   GstGLWindow *window;
549
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);
553
554   window = klass->create_window (display);
555
556   if (window)
557     display->windows = g_list_prepend (display->windows, window);
558
559   return window;
560 }
561
562 static GstGLWindow *
563 gst_gl_display_default_create_window (GstGLDisplay * display)
564 {
565   return gst_gl_window_new (display);
566 }
567
568 /**
569  * gst_gl_display_remove_window:
570  * @display: a #GstGLDisplay
571  * @window: a #GstGLWindow to remove
572  *
573  * Returns: if @window could be removed from @display
574  *
575  * Since: 1.12
576  */
577 gboolean
578 gst_gl_display_remove_window (GstGLDisplay * display, GstGLWindow * window)
579 {
580   gboolean ret = FALSE;
581   GList *l;
582
583   GST_OBJECT_LOCK (display);
584   l = g_list_find (display->windows, window);
585   if (l) {
586     display->windows = g_list_delete_link (display->windows, l);
587     ret = TRUE;
588   }
589   GST_OBJECT_UNLOCK (display);
590
591   return ret;
592 }
593
594 /**
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
599  *
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.
603  *
604  * Returns: (transfer none): The first #GstGLWindow that causes a match
605  *          from @compare_func
606  *
607  * Since: 1.12
608  */
609 GstGLWindow *
610 gst_gl_display_find_window (GstGLDisplay * display, gpointer data,
611     GCompareFunc compare_func)
612 {
613   GstGLWindow *ret = NULL;
614   GList *l;
615
616   GST_OBJECT_LOCK (display);
617   l = g_list_find_custom (display->windows, data, compare_func);
618   if (l)
619     ret = l->data;
620   GST_OBJECT_UNLOCK (display);
621
622   return ret;
623 }
624
625 static GstGLContext *
626 _get_gl_context_for_thread_unlocked (GstGLDisplay * display, GThread * thread)
627 {
628   GstGLContext *context = NULL;
629   GList *prev = NULL, *l = display->priv->contexts;
630
631   while (l) {
632     GWeakRef *ref = l->data;
633     GThread *context_thread;
634
635     context = g_weak_ref_get (ref);
636     if (!context) {
637       /* remove dead contexts */
638       g_weak_ref_clear (l->data);
639       g_free (l->data);
640       display->priv->contexts = g_list_delete_link (display->priv->contexts, l);
641       l = prev ? prev->next : display->priv->contexts;
642       continue;
643     }
644
645     if (thread == NULL) {
646       GST_DEBUG_OBJECT (display, "Returning GL context %" GST_PTR_FORMAT " for "
647           "NULL thread", context);
648       return context;
649     }
650
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);
655       prev = l;
656       l = l->next;
657       continue;
658     }
659
660     if (context_thread)
661       g_thread_unref (context_thread);
662
663     GST_DEBUG_OBJECT (display, "Returning GL context %" GST_PTR_FORMAT " for "
664         "thread %p", context, thread);
665     return context;
666   }
667
668   GST_DEBUG_OBJECT (display, "No GL context for thread %p", thread);
669   return NULL;
670 }
671
672 /**
673  * gst_gl_display_get_gl_context_for_thread:
674  * @display: a #GstGLDisplay
675  * @thread: a #GThread
676  *
677  * Returns: (transfer full): the #GstGLContext current on @thread or %NULL
678  *
679  * Must be called with the object lock held.
680  *
681  * Since: 1.6
682  */
683 GstGLContext *
684 gst_gl_display_get_gl_context_for_thread (GstGLDisplay * display,
685     GThread * thread)
686 {
687   GstGLContext *context;
688
689   g_return_val_if_fail (GST_IS_GL_DISPLAY (display), NULL);
690
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);
694
695   return context;
696 }
697
698 static gboolean
699 _check_collision (GstGLContext * context, GstGLContext * collision)
700 {
701   GThread *thread, *collision_thread;
702   gboolean ret = FALSE;
703
704   if (!context || !collision)
705     return FALSE;
706
707   thread = gst_gl_context_get_thread (context);
708   collision_thread = gst_gl_context_get_thread (collision);
709
710   if (!thread || !collision_thread) {
711     ret = FALSE;
712     goto out;
713   }
714
715   if (thread == collision_thread) {
716     ret = TRUE;
717     goto out;
718   }
719
720 out:
721   if (thread)
722     g_thread_unref (thread);
723   if (collision_thread)
724     g_thread_unref (collision_thread);
725
726   return ret;
727 }
728
729 /**
730  * gst_gl_display_add_context:
731  * @display: a #GstGLDisplay
732  * @context: (transfer none): a #GstGLContext
733  *
734  * Returns: whether @context was successfully added. %FALSE may be returned
735  * if there already exists another context for @context's active thread.
736  *
737  * Must be called with the object lock held.
738  *
739  * Since: 1.6
740  */
741 gboolean
742 gst_gl_display_add_context (GstGLDisplay * display, GstGLContext * context)
743 {
744   GstGLContext *collision = NULL;
745   GstGLDisplay *context_display;
746   gboolean ret = TRUE;
747   GThread *thread;
748   GWeakRef *ref;
749
750   g_return_val_if_fail (GST_IS_GL_DISPLAY (display), FALSE);
751   g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);
752
753   context_display = gst_gl_context_get_display (context);
754   g_assert (context_display == display);
755   gst_object_unref (context_display);
756
757   thread = gst_gl_context_get_thread (context);
758   if (thread) {
759     collision = _get_gl_context_for_thread_unlocked (display, thread);
760     g_thread_unref (thread);
761
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);
766       ret = TRUE;
767       goto out;
768     }
769
770     if (_check_collision (context, collision)) {
771       GST_DEBUG_OBJECT (display, "Collision detected adding GL context "
772           "%" GST_PTR_FORMAT, context);
773       ret = FALSE;
774       goto out;
775     }
776   }
777
778   ref = g_new0 (GWeakRef, 1);
779   g_weak_ref_init (ref, context);
780
781   GST_DEBUG_OBJECT (display, "Adding GL context %" GST_PTR_FORMAT, context);
782   display->priv->contexts = g_list_prepend (display->priv->contexts, ref);
783
784 out:
785   if (collision)
786     gst_object_unref (collision);
787
788   GST_DEBUG_OBJECT (display, "%ssuccessfully inserted context %" GST_PTR_FORMAT,
789       ret ? "" : "un", context);
790
791   return ret;
792 }