• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @title: GstGLMemoryEGL
29  * @see_also: #GstGLMemory, #GstGLBaseMemoryAllocator, #GstGLBufferPool
30  *
31  * #GstGLMemoryEGL is created or wrapped through gst_gl_base_memory_alloc()
32  * with #GstGLVideoAllocationParams.
33  */
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include <string.h>
40 
41 #include "gstglmemoryegl.h"
42 
43 #include <gst/gl/gstglfuncs.h>
44 
45 #include "gstegl.h"
46 #include "gsteglimage.h"
47 #include "gstglcontext_egl.h"
48 
49 static GstAllocator *_gl_memory_egl_allocator;
50 
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_MEMORY);
52 #define GST_CAT_DEFAULT GST_CAT_GL_MEMORY
53 
54 #define parent_class gst_gl_memory_egl_allocator_parent_class
55 G_DEFINE_TYPE (GstGLMemoryEGLAllocator, gst_gl_memory_egl_allocator,
56     GST_TYPE_GL_MEMORY_ALLOCATOR);
57 
58 GST_DEFINE_MINI_OBJECT_TYPE (GstGLMemoryEGL, gst_gl_memory_egl);
59 
60 /**
61  * gst_is_gl_memory_egl:
62  * @mem: a #GstMemory to test
63  *
64  * Returns: whether @mem is a #GstGLMemoryEGL
65  *
66  * Since: 1.10
67  */
68 gboolean
gst_is_gl_memory_egl(GstMemory * mem)69 gst_is_gl_memory_egl (GstMemory * mem)
70 {
71   return mem != NULL && mem->allocator != NULL
72       && g_type_is_a (G_OBJECT_TYPE (mem->allocator),
73       GST_TYPE_GL_MEMORY_EGL_ALLOCATOR);
74 }
75 
76 static GstGLMemoryEGL *
_gl_mem_get_parent(GstGLMemoryEGL * gl_mem)77 _gl_mem_get_parent (GstGLMemoryEGL * gl_mem)
78 {
79   GstGLMemoryEGL *parent = (GstGLMemoryEGL *) gl_mem->mem.mem.mem.parent;
80   return parent ? parent : gl_mem;
81 }
82 
83 /**
84  * gst_gl_memory_egl_get_image:
85  * @mem: a #GstGLMemoryEGL
86  *
87  * Returns: The EGLImage held by @mem
88  *
89  * Since: 1.10
90  */
91 gpointer
gst_gl_memory_egl_get_image(GstGLMemoryEGL * mem)92 gst_gl_memory_egl_get_image (GstGLMemoryEGL * mem)
93 {
94   g_return_val_if_fail (gst_is_gl_memory_egl (GST_MEMORY_CAST (mem)),
95       EGL_NO_IMAGE_KHR);
96   return gst_egl_image_get_image (_gl_mem_get_parent (mem)->image);
97 }
98 
99 /**
100  * gst_gl_memory_egl_get_display:
101  * @mem: a #GstGLMemoryEGL
102  *
103  * Returns: The EGLDisplay @mem is associated with
104  *
105  * Since: 1.10
106  */
107 gpointer
gst_gl_memory_egl_get_display(GstGLMemoryEGL * mem)108 gst_gl_memory_egl_get_display (GstGLMemoryEGL * mem)
109 {
110   g_return_val_if_fail (gst_is_gl_memory_egl (GST_MEMORY_CAST (mem)), NULL);
111   return GST_GL_CONTEXT_EGL (_gl_mem_get_parent (mem)->mem.mem.
112       context)->egl_display;
113 }
114 
115 static GstMemory *
_gl_mem_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)116 _gl_mem_alloc (GstAllocator * allocator, gsize size,
117     GstAllocationParams * params)
118 {
119   g_warning ("Use gst_gl_base_memory_allocator_alloc() to allocate from this "
120       "GstGLMemoryEGL allocator");
121 
122   return NULL;
123 }
124 
125 static void
_gl_mem_destroy(GstGLMemoryEGL * mem)126 _gl_mem_destroy (GstGLMemoryEGL * mem)
127 {
128   if (mem->image)
129     gst_egl_image_unref (mem->image);
130 
131   GST_GL_BASE_MEMORY_ALLOCATOR_CLASS (parent_class)->destroy ((GstGLBaseMemory
132           *) mem);
133 }
134 
135 static GstGLMemoryEGL *
_gl_mem_egl_alloc(GstGLBaseMemoryAllocator * allocator,GstGLVideoAllocationParams * params)136 _gl_mem_egl_alloc (GstGLBaseMemoryAllocator * allocator,
137     GstGLVideoAllocationParams * params)
138 {
139   guint alloc_flags = params->parent.alloc_flags;
140   GstGLMemoryEGL *mem;
141 
142   g_return_val_if_fail (alloc_flags & GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_VIDEO,
143       NULL);
144   g_return_val_if_fail ((alloc_flags &
145           GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_WRAP_SYSMEM) == 0, NULL);
146   if (alloc_flags & GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_WRAP_GPU_HANDLE) {
147     g_return_val_if_fail (GST_IS_EGL_IMAGE (params->parent.gl_handle), NULL);
148   }
149 
150   mem = g_new0 (GstGLMemoryEGL, 1);
151   if (alloc_flags & GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_WRAP_GPU_HANDLE) {
152     if (params->target != GST_GL_TEXTURE_TARGET_2D &&
153         params->target != GST_GL_TEXTURE_TARGET_EXTERNAL_OES) {
154       g_free (mem);
155       GST_CAT_ERROR (GST_CAT_GL_MEMORY, "GstGLMemoryEGL only supports wrapping "
156           "2D and external-oes textures");
157       return NULL;
158     }
159     mem->mem.tex_target = params->target;
160     mem->image = gst_egl_image_ref (params->parent.gl_handle);
161   }
162 
163   gst_gl_memory_init (GST_GL_MEMORY_CAST (mem), GST_ALLOCATOR_CAST (allocator),
164       NULL, params->parent.context, params->target, params->tex_format,
165       params->parent.alloc_params, params->v_info, params->plane,
166       params->valign, params->parent.user_data, params->parent.notify);
167 
168   if (!mem->image) {
169     gst_allocator_free (GST_ALLOCATOR_CAST (allocator), GST_MEMORY_CAST (mem));
170     return NULL;
171   }
172 
173   return mem;
174 }
175 
176 static gboolean
_gl_mem_create(GstGLMemoryEGL * gl_mem,GError ** error)177 _gl_mem_create (GstGLMemoryEGL * gl_mem, GError ** error)
178 {
179   GstGLContext *context = gl_mem->mem.mem.context;
180   const GstGLFuncs *gl = context->gl_vtable;
181   GstGLBaseMemoryAllocatorClass *alloc_class;
182 
183   if (!gst_gl_context_check_feature (GST_GL_CONTEXT (context),
184           "EGL_KHR_image_base")) {
185     g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_WRONG_API,
186         "EGL_KHR_image_base is not supported");
187     return FALSE;
188   }
189 
190   alloc_class = GST_GL_BASE_MEMORY_ALLOCATOR_CLASS (parent_class);
191   if (!alloc_class->create ((GstGLBaseMemory *) gl_mem, error))
192     return FALSE;
193 
194   if (gl_mem->image == NULL) {
195     gl_mem->image = gst_egl_image_from_texture (context,
196         (GstGLMemory *) gl_mem, NULL);
197 
198     if (!gl_mem->image) {
199       g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
200           "Failed to create EGLImage");
201       return FALSE;
202     }
203   } else {
204     guint gl_target = gst_gl_texture_target_to_gl (gl_mem->mem.tex_target);
205 
206     if (!gl->EGLImageTargetTexture2D) {
207       g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
208           "Required function glEGLImageTargetTexture2D() is not available for "
209           "attaching an EGLImage to a texture");
210       return FALSE;
211     }
212 
213     gl->ActiveTexture (GL_TEXTURE0 + gl_mem->mem.plane);
214     gl->BindTexture (gl_target, gl_mem->mem.tex_id);
215     gl->EGLImageTargetTexture2D (gl_target,
216         gst_egl_image_get_image (GST_EGL_IMAGE (gl_mem->image)));
217   }
218 
219   return TRUE;
220 }
221 
222 static GstMemory *
_gl_mem_copy(GstGLMemoryEGL * src,gssize offset,gssize size)223 _gl_mem_copy (GstGLMemoryEGL * src, gssize offset, gssize size)
224 {
225   GST_CAT_ERROR (GST_CAT_GL_MEMORY, "GstGLMemoryEGL does not support copy");
226   return NULL;
227 }
228 
229 static void
gst_gl_memory_egl_allocator_class_init(GstGLMemoryEGLAllocatorClass * klass)230 gst_gl_memory_egl_allocator_class_init (GstGLMemoryEGLAllocatorClass * klass)
231 {
232   GstGLBaseMemoryAllocatorClass *gl_base;
233   GstGLMemoryAllocatorClass *gl_tex;
234   GstAllocatorClass *allocator_class;
235 
236   gl_tex = (GstGLMemoryAllocatorClass *) klass;
237   gl_base = (GstGLBaseMemoryAllocatorClass *) klass;
238   allocator_class = (GstAllocatorClass *) klass;
239 
240   gl_base->alloc = (GstGLBaseMemoryAllocatorAllocFunction) _gl_mem_egl_alloc;
241   gl_base->create = (GstGLBaseMemoryAllocatorCreateFunction) _gl_mem_create;
242   gl_base->destroy = (GstGLBaseMemoryAllocatorDestroyFunction) _gl_mem_destroy;
243   gl_tex->copy = (GstGLBaseMemoryAllocatorCopyFunction) _gl_mem_copy;
244 
245   allocator_class->alloc = _gl_mem_alloc;
246 }
247 
248 static void
gst_gl_memory_egl_allocator_init(GstGLMemoryEGLAllocator * allocator)249 gst_gl_memory_egl_allocator_init (GstGLMemoryEGLAllocator * allocator)
250 {
251   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
252 
253   alloc->mem_type = GST_GL_MEMORY_EGL_ALLOCATOR_NAME;
254 
255   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
256 }
257 
258 /**
259  * gst_gl_memory_egl_init_once:
260  *
261  * Initializes the GL Memory allocator. It is safe to call this function
262  * multiple times.  This must be called before any other GstGLMemoryEGL operation.
263  *
264  * Since: 1.10
265  */
266 void
gst_gl_memory_egl_init_once(void)267 gst_gl_memory_egl_init_once (void)
268 {
269   static gsize _init = 0;
270 
271   if (g_once_init_enter (&_init)) {
272     gst_gl_memory_init_once ();
273 
274     GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_MEMORY, "glmemory", 0,
275         "OpenGL Texture with EGLImage memory");
276 
277     _gl_memory_egl_allocator =
278         g_object_new (GST_TYPE_GL_MEMORY_EGL_ALLOCATOR, NULL);
279     gst_object_ref_sink (_gl_memory_egl_allocator);
280 
281     /* The allocator is never unreffed */
282     GST_OBJECT_FLAG_SET (_gl_memory_egl_allocator,
283         GST_OBJECT_FLAG_MAY_BE_LEAKED);
284 
285     gst_allocator_register (GST_GL_MEMORY_EGL_ALLOCATOR_NAME,
286         gst_object_ref (_gl_memory_egl_allocator));
287     g_once_init_leave (&_init, 1);
288   }
289 }
290