1 /*
2  * GStreamer
3  * Copyright (C) 2012 Collabora Ltd.
4  *   @author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  * Copyright (C) 2014 Julien Isorce <julien.isorce@gmail.com>
6  * Copyright (C) 2015 Igalia
7  *    Author: Gwang Yoon Hwang <yoon@igalia.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 /**
26  * SECTION:gstglmemoryegl
27  * @short_description: memory subclass for EGLImage's
28  * @see_also: #GstGLMemory, #GstGLBaseMemoryAllocator, #GstGLBufferPool
29  *
30  * #GstGLMemoryEGL is created or wrapped through gst_gl_base_memory_alloc()
31  * with #GstGLVideoAllocationParams.
32  */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 #include <string.h>
39 
40 #include "gstglmemoryegl.h"
41 
42 #include <gst/gl/gstglfuncs.h>
43 
44 #include "gstegl.h"
45 #include "gsteglimage.h"
46 #include "gstglcontext_egl.h"
47 
48 static GstAllocator *_gl_memory_egl_allocator;
49 
50 GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_MEMORY);
51 #define GST_CAT_DEFAULT GST_CAT_GL_MEMORY
52 
53 #define parent_class gst_gl_memory_egl_allocator_parent_class
54 G_DEFINE_TYPE (GstGLMemoryEGLAllocator, gst_gl_memory_egl_allocator,
55     GST_TYPE_GL_MEMORY_ALLOCATOR);
56 
57 /**
58  * gst_is_gl_memory_egl:
59  * @mem: a #GstMemory to test
60  *
61  * Returns: whether @mem is a #GstGLMemoryEGL
62  *
63  * Since: 1.10
64  */
65 gboolean
gst_is_gl_memory_egl(GstMemory * mem)66 gst_is_gl_memory_egl (GstMemory * mem)
67 {
68   return mem != NULL && mem->allocator != NULL
69       && g_type_is_a (G_OBJECT_TYPE (mem->allocator),
70       GST_TYPE_GL_MEMORY_EGL_ALLOCATOR);
71 }
72 
73 static GstGLMemoryEGL *
_gl_mem_get_parent(GstGLMemoryEGL * gl_mem)74 _gl_mem_get_parent (GstGLMemoryEGL * gl_mem)
75 {
76   GstGLMemoryEGL *parent = (GstGLMemoryEGL *) gl_mem->mem.mem.mem.parent;
77   return parent ? parent : gl_mem;
78 }
79 
80 /**
81  * gst_gl_memory_egl_get_image:
82  * @mem: a #GstGLMemoryEGL
83  *
84  * Returns: The EGLImage held by @mem
85  *
86  * Since: 1.10
87  */
88 gpointer
gst_gl_memory_egl_get_image(GstGLMemoryEGL * mem)89 gst_gl_memory_egl_get_image (GstGLMemoryEGL * mem)
90 {
91   g_return_val_if_fail (gst_is_gl_memory_egl (GST_MEMORY_CAST (mem)),
92       EGL_NO_IMAGE_KHR);
93   return gst_egl_image_get_image (_gl_mem_get_parent (mem)->image);
94 }
95 
96 /**
97  * gst_gl_memory_egl_get_display:
98  * @mem: a #GstGLMemoryEGL
99  *
100  * Returns: The EGLDisplay @mem is associated with
101  *
102  * Since: 1.10
103  */
104 gpointer
gst_gl_memory_egl_get_display(GstGLMemoryEGL * mem)105 gst_gl_memory_egl_get_display (GstGLMemoryEGL * mem)
106 {
107   g_return_val_if_fail (gst_is_gl_memory_egl (GST_MEMORY_CAST (mem)), NULL);
108   return GST_GL_CONTEXT_EGL (_gl_mem_get_parent (mem)->mem.mem.
109       context)->egl_display;
110 }
111 
112 static GstMemory *
_gl_mem_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)113 _gl_mem_alloc (GstAllocator * allocator, gsize size,
114     GstAllocationParams * params)
115 {
116   g_warning ("Use gst_gl_base_memory_allocator_alloc() to allocate from this "
117       "GstGLMemoryEGL allocator");
118 
119   return NULL;
120 }
121 
122 static void
_gl_mem_destroy(GstGLMemoryEGL * mem)123 _gl_mem_destroy (GstGLMemoryEGL * mem)
124 {
125   if (mem->image)
126     gst_egl_image_unref (mem->image);
127 
128   GST_GL_BASE_MEMORY_ALLOCATOR_CLASS (parent_class)->destroy ((GstGLBaseMemory
129           *) mem);
130 }
131 
132 static GstGLMemoryEGL *
_gl_mem_egl_alloc(GstGLBaseMemoryAllocator * allocator,GstGLVideoAllocationParams * params)133 _gl_mem_egl_alloc (GstGLBaseMemoryAllocator * allocator,
134     GstGLVideoAllocationParams * params)
135 {
136   guint alloc_flags = params->parent.alloc_flags;
137   GstGLMemoryEGL *mem;
138 
139   g_return_val_if_fail (alloc_flags & GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_VIDEO,
140       NULL);
141   g_return_val_if_fail ((alloc_flags &
142           GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_WRAP_SYSMEM) == 0, NULL);
143   if (alloc_flags & GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_WRAP_GPU_HANDLE) {
144     g_return_val_if_fail (GST_IS_EGL_IMAGE (params->parent.gl_handle), NULL);
145   }
146 
147   mem = g_new0 (GstGLMemoryEGL, 1);
148   if (alloc_flags & GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_WRAP_GPU_HANDLE) {
149     if (params->target != GST_GL_TEXTURE_TARGET_2D) {
150       g_free (mem);
151       GST_CAT_ERROR (GST_CAT_GL_MEMORY, "GstGLMemoryEGL only supports wrapping "
152           "2D textures");
153       return NULL;
154     }
155     mem->image = gst_egl_image_ref (params->parent.gl_handle);
156   }
157 
158   gst_gl_memory_init (GST_GL_MEMORY_CAST (mem), GST_ALLOCATOR_CAST (allocator),
159       NULL, params->parent.context, params->target, params->tex_format,
160       params->parent.alloc_params, params->v_info, params->plane,
161       params->valign, params->parent.user_data, params->parent.notify);
162 
163   if (!mem->image) {
164     gst_allocator_free (GST_ALLOCATOR_CAST (allocator), GST_MEMORY_CAST (mem));
165     return NULL;
166   }
167 
168   return mem;
169 }
170 
171 static gboolean
_gl_mem_create(GstGLMemoryEGL * gl_mem,GError ** error)172 _gl_mem_create (GstGLMemoryEGL * gl_mem, GError ** error)
173 {
174   GstGLContext *context = gl_mem->mem.mem.context;
175   const GstGLFuncs *gl = context->gl_vtable;
176   GstGLBaseMemoryAllocatorClass *alloc_class;
177 
178   if (!gst_gl_context_check_feature (GST_GL_CONTEXT (context),
179           "EGL_KHR_image_base")) {
180     g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_WRONG_API,
181         "EGL_KHR_image_base is not supported");
182     return FALSE;
183   }
184 
185   alloc_class = GST_GL_BASE_MEMORY_ALLOCATOR_CLASS (parent_class);
186   if (!alloc_class->create ((GstGLBaseMemory *) gl_mem, error))
187     return FALSE;
188 
189   if (gl_mem->image == NULL) {
190     gl_mem->image = gst_egl_image_from_texture (context,
191         (GstGLMemory *) gl_mem, NULL);
192 
193     if (!gl_mem->image) {
194       g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
195           "Failed to create EGLImage");
196       return FALSE;
197     }
198   } else {
199     gl->ActiveTexture (GL_TEXTURE0 + gl_mem->mem.plane);
200     gl->BindTexture (GL_TEXTURE_2D, gl_mem->mem.tex_id);
201     gl->EGLImageTargetTexture2D (GL_TEXTURE_2D,
202         gst_egl_image_get_image (GST_EGL_IMAGE (gl_mem->image)));
203   }
204 
205   return TRUE;
206 }
207 
208 static GstMemory *
_gl_mem_copy(GstGLMemoryEGL * src,gssize offset,gssize size)209 _gl_mem_copy (GstGLMemoryEGL * src, gssize offset, gssize size)
210 {
211   GST_CAT_ERROR (GST_CAT_GL_MEMORY, "GstGLMemoryEGL does not support copy");
212   return NULL;
213 }
214 
215 static void
gst_gl_memory_egl_allocator_class_init(GstGLMemoryEGLAllocatorClass * klass)216 gst_gl_memory_egl_allocator_class_init (GstGLMemoryEGLAllocatorClass * klass)
217 {
218   GstGLBaseMemoryAllocatorClass *gl_base;
219   GstGLMemoryAllocatorClass *gl_tex;
220   GstAllocatorClass *allocator_class;
221 
222   gl_tex = (GstGLMemoryAllocatorClass *) klass;
223   gl_base = (GstGLBaseMemoryAllocatorClass *) klass;
224   allocator_class = (GstAllocatorClass *) klass;
225 
226   gl_base->alloc = (GstGLBaseMemoryAllocatorAllocFunction) _gl_mem_egl_alloc;
227   gl_base->create = (GstGLBaseMemoryAllocatorCreateFunction) _gl_mem_create;
228   gl_base->destroy = (GstGLBaseMemoryAllocatorDestroyFunction) _gl_mem_destroy;
229   gl_tex->copy = (GstGLBaseMemoryAllocatorCopyFunction) _gl_mem_copy;
230 
231   allocator_class->alloc = _gl_mem_alloc;
232 }
233 
234 static void
gst_gl_memory_egl_allocator_init(GstGLMemoryEGLAllocator * allocator)235 gst_gl_memory_egl_allocator_init (GstGLMemoryEGLAllocator * allocator)
236 {
237   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
238 
239   alloc->mem_type = GST_GL_MEMORY_EGL_ALLOCATOR_NAME;
240 
241   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
242 }
243 
244 /**
245  * gst_gl_memory_egl_init_once:
246  *
247  * Initializes the GL Memory allocator. It is safe to call this function
248  * multiple times.  This must be called before any other GstGLMemoryEGL operation.
249  *
250  * Since: 1.10
251  */
252 void
gst_gl_memory_egl_init_once(void)253 gst_gl_memory_egl_init_once (void)
254 {
255   static volatile gsize _init = 0;
256 
257   if (g_once_init_enter (&_init)) {
258     gst_gl_memory_init_once ();
259 
260     GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_MEMORY, "glmemory", 0,
261         "OpenGL Texture with EGLImage memory");
262 
263     _gl_memory_egl_allocator =
264         g_object_new (GST_TYPE_GL_MEMORY_EGL_ALLOCATOR, NULL);
265     gst_object_ref_sink (_gl_memory_egl_allocator);
266 
267     /* The allocator is never unreffed */
268     GST_OBJECT_FLAG_SET (_gl_memory_egl_allocator,
269         GST_OBJECT_FLAG_MAY_BE_LEAKED);
270 
271     gst_allocator_register (GST_GL_MEMORY_EGL_ALLOCATOR_NAME,
272         gst_object_ref (_gl_memory_egl_allocator));
273     g_once_init_leave (&_init, 1);
274   }
275 }
276