• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2016 Alessandro Decina <alessandro.d@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "glcontexthelper.h"
21 
22 static GstGLContext *
_find_local_gl_context(GstGLContextHelper * ctxh)23 _find_local_gl_context (GstGLContextHelper * ctxh)
24 {
25   GstGLContext *gl_context = NULL;
26 
27   gst_gl_query_local_gl_context (GST_ELEMENT (ctxh->element), GST_PAD_SRC,
28       &gl_context);
29 
30   return gl_context;
31 }
32 
33 GstGLContextHelper *
gst_gl_context_helper_new(GstElement * element)34 gst_gl_context_helper_new (GstElement * element)
35 {
36   GstGLContextHelper *ctxh = g_new0 (GstGLContextHelper, 1);
37   ctxh->element = gst_object_ref (element);
38 
39   return ctxh;
40 }
41 
42 void
gst_gl_context_helper_free(GstGLContextHelper * ctxh)43 gst_gl_context_helper_free (GstGLContextHelper * ctxh)
44 {
45   g_return_if_fail (ctxh != NULL);
46 
47   gst_object_unref (ctxh->element);
48 
49   if (ctxh->display)
50     gst_object_unref (ctxh->display);
51 
52   if (ctxh->context)
53     gst_object_unref (ctxh->context);
54 
55   if (ctxh->other_context)
56     gst_object_unref (ctxh->other_context);
57 
58   g_free (ctxh);
59 }
60 
61 void
gst_gl_context_helper_ensure_context(GstGLContextHelper * ctxh)62 gst_gl_context_helper_ensure_context (GstGLContextHelper * ctxh)
63 {
64   GError *error = NULL;
65   GstGLContext *context;
66 
67   g_return_if_fail (ctxh != NULL);
68 
69   if (!ctxh->display)
70     gst_gl_ensure_element_data (ctxh->element, &ctxh->display,
71         &ctxh->other_context);
72 
73   if (!ctxh->display)
74     goto display_error;
75 
76   context = _find_local_gl_context (ctxh);
77   if (context) {
78     GST_INFO_OBJECT (ctxh->element, "found local context %p, old context %p",
79         context, ctxh->context);
80     if (ctxh->context)
81       gst_object_unref (ctxh->context);
82     ctxh->context = context;
83   }
84 
85   if (!ctxh->context) {
86     GST_OBJECT_LOCK (ctxh->display);
87     do {
88       if (ctxh->context)
89         gst_object_unref (ctxh->context);
90       ctxh->context =
91           gst_gl_display_get_gl_context_for_thread (ctxh->display, NULL);
92       if (!ctxh->context) {
93         if (!gst_gl_display_create_context (ctxh->display,
94                 ctxh->other_context, &ctxh->context, &error)) {
95           GST_OBJECT_UNLOCK (ctxh->display);
96           goto context_error;
97         }
98       }
99     } while (!gst_gl_display_add_context (ctxh->display, ctxh->context));
100     GST_OBJECT_UNLOCK (ctxh->display);
101   }
102 
103   return;
104 
105 context_error:
106   {
107     GST_ELEMENT_ERROR (ctxh->element, RESOURCE, NOT_FOUND, ("%s",
108             error->message), (NULL));
109     g_clear_error (&error);
110 
111     return;
112   }
113 
114 display_error:
115   {
116     GST_ELEMENT_ERROR (ctxh->element, RESOURCE, NOT_FOUND,
117         ("Failed to obtain display"), (NULL));
118 
119     return;
120   }
121 }
122