1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <string.h>
26 
27 #include "gstglbuffer.h"
28 
29 #include "gstglcontext.h"
30 #include "gstglfuncs.h"
31 #include "gstglutils.h"
32 
33 /**
34  * SECTION:gstglbuffer
35  * @title: GstGLBuffer
36  * @short_description: memory subclass for GL buffers
37  * @see_also: #GstMemory, #GstAllocator
38  *
39  * GstGLBuffer is a #GstMemory subclass providing support for the mapping of
40  * GL buffers.
41  *
42  * Data is uploaded or downloaded from the GPU as is necessary.
43  */
44 
45 /* Implementation notes:
46  *
47  * Currently does not take into account GLES2 differences (no mapbuffer)
48  */
49 
50 #define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
51 #define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
52 #define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
53 #define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
54 #define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
55 
56 /* compatibility definitions... */
57 #ifndef GL_MAP_READ_BIT
58 #define GL_MAP_READ_BIT 0x0001
59 #endif
60 #ifndef GL_MAP_WRITE_BIT
61 #define GL_MAP_WRITE_BIT 0x0002
62 #endif
63 #ifndef GL_COPY_READ_BUFFER
64 #define GL_COPY_READ_BUFFER 0x8F36
65 #endif
66 #ifndef GL_COPY_WRITE_BUFFER
67 #define GL_COPY_WRITE_BUFFER 0x8F37
68 #endif
69 
70 GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_BUFFER);
71 #define GST_CAT_DEFUALT GST_CAT_GL_BUFFER
72 
73 static GstAllocator *_gl_buffer_allocator;
74 
75 static gboolean
_gl_buffer_create(GstGLBuffer * gl_mem,GError ** error)76 _gl_buffer_create (GstGLBuffer * gl_mem, GError ** error)
77 {
78   const GstGLFuncs *gl = gl_mem->mem.context->gl_vtable;
79 
80   gl->GenBuffers (1, &gl_mem->id);
81   gl->BindBuffer (gl_mem->target, gl_mem->id);
82   gl->BufferData (gl_mem->target, gl_mem->mem.mem.maxsize, NULL,
83       gl_mem->usage_hints);
84   gl->BindBuffer (gl_mem->target, 0);
85 
86   return TRUE;
87 }
88 
89 struct create_data
90 {
91   GstGLBuffer *mem;
92   gboolean result;
93 };
94 
95 static void
_gl_buffer_init(GstGLBuffer * mem,GstAllocator * allocator,GstMemory * parent,GstGLContext * context,guint gl_target,guint gl_usage,GstAllocationParams * params,gsize size)96 _gl_buffer_init (GstGLBuffer * mem, GstAllocator * allocator,
97     GstMemory * parent, GstGLContext * context, guint gl_target, guint gl_usage,
98     GstAllocationParams * params, gsize size)
99 {
100   mem->target = gl_target;
101   mem->usage_hints = gl_usage;
102 
103   gst_gl_base_memory_init ((GstGLBaseMemory *) mem, allocator, parent, context,
104       params, size, NULL, NULL);
105 
106   GST_CAT_DEBUG (GST_CAT_GL_BUFFER, "new GL buffer memory:%p size:%"
107       G_GSIZE_FORMAT, mem, mem->mem.mem.maxsize);
108 }
109 
110 static GstGLBuffer *
_gl_buffer_new(GstAllocator * allocator,GstMemory * parent,GstGLContext * context,guint gl_target,guint gl_usage,GstAllocationParams * params,gsize size)111 _gl_buffer_new (GstAllocator * allocator, GstMemory * parent,
112     GstGLContext * context, guint gl_target, guint gl_usage,
113     GstAllocationParams * params, gsize size)
114 {
115   GstGLBuffer *ret = g_new0 (GstGLBuffer, 1);
116   _gl_buffer_init (ret, allocator, parent, context, gl_target, gl_usage,
117       params, size);
118 
119   return ret;
120 }
121 
122 static gpointer
gst_gl_buffer_cpu_access(GstGLBuffer * mem,GstMapInfo * info,gsize size)123 gst_gl_buffer_cpu_access (GstGLBuffer * mem, GstMapInfo * info, gsize size)
124 {
125   const GstGLFuncs *gl = mem->mem.context->gl_vtable;
126   gpointer data, ret;
127 
128   if (!gst_gl_base_memory_alloc_data (GST_GL_BASE_MEMORY_CAST (mem)))
129     return NULL;
130 
131   ret = mem->mem.data;
132 
133   GST_CAT_LOG (GST_CAT_GL_BUFFER, "mapping id %d size %" G_GSIZE_FORMAT,
134       mem->id, size);
135 
136   /* The extra data pointer indirection/memcpy is needed for coherent across
137    * concurrent map()'s in both GL and CPU */
138   if (GST_MEMORY_FLAG_IS_SET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_DOWNLOAD)
139       && (info->flags & GST_MAP_GL) == 0 && (info->flags & GST_MAP_READ) != 0) {
140     gl->BindBuffer (mem->target, mem->id);
141 
142     if (gl->MapBufferRange) {
143       /* FIXME: optionally remove this with a flag and return the
144        * glMapBufferRange pointer (requires
145        * GL_ARB_buffer_storage/GL4/GL_COHERENT_BIT) */
146       guint gl_map_flags = GL_MAP_READ_BIT;
147 
148       data = gl->MapBufferRange (mem->target, 0, size, gl_map_flags);
149 
150       if (data)
151         memcpy (mem->mem.data, data, size);
152 
153       gl->UnmapBuffer (mem->target);
154       ret = mem->mem.data;
155     } else if (gl->GetBufferSubData) {
156       gl->GetBufferSubData (mem->target, 0, size, mem->mem.data);
157       ret = mem->mem.data;
158     } else {
159       ret = NULL;
160     }
161     gl->BindBuffer (mem->target, 0);
162   }
163 
164   return ret;
165 }
166 
167 static void
gst_gl_buffer_upload_cpu_write(GstGLBuffer * mem,GstMapInfo * info,gsize size)168 gst_gl_buffer_upload_cpu_write (GstGLBuffer * mem, GstMapInfo * info,
169     gsize size)
170 {
171   const GstGLFuncs *gl = mem->mem.context->gl_vtable;
172   gpointer data;
173 
174   if (!mem->mem.data)
175     /* no data pointer has been written */
176     return;
177 
178   /* The extra data pointer indirection/memcpy is needed for coherent across
179    * concurrent map()'s in both GL and CPU */
180   /* FIXME: uploading potentially half-written data for libav pushing READWRITE
181    * mapped buffers */
182   if (GST_MEMORY_FLAG_IS_SET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_UPLOAD)
183       || (mem->mem.map_flags & GST_MAP_WRITE) != 0) {
184     gl->BindBuffer (mem->target, mem->id);
185 
186     if (gl->MapBufferRange) {
187       /* FIXME: optionally remove this with a flag and return the
188        * glMapBufferRange pointer (requires
189        * GL_ARB_buffer_storage/GL4/GL_COHERENT_BIT) */
190       guint gl_map_flags = GL_MAP_WRITE_BIT;
191 
192       data = gl->MapBufferRange (mem->target, 0, size, gl_map_flags);
193 
194       if (data)
195         memcpy (data, mem->mem.data, size);
196 
197       gl->UnmapBuffer (mem->target);
198     } else if (gl->BufferSubData) {
199       gl->BufferSubData (mem->target, 0, size, mem->mem.data);
200     }
201     gl->BindBuffer (mem->target, 0);
202   }
203 }
204 
205 static gpointer
_gl_buffer_map(GstGLBuffer * mem,GstMapInfo * info,gsize size)206 _gl_buffer_map (GstGLBuffer * mem, GstMapInfo * info, gsize size)
207 {
208   const GstGLFuncs *gl = mem->mem.context->gl_vtable;
209 
210   if ((info->flags & GST_MAP_GL) != 0) {
211     if (info->flags & GST_MAP_READ) {
212       gst_gl_buffer_upload_cpu_write (mem, info, size);
213     }
214     gl->BindBuffer (mem->target, mem->id);
215     return &mem->id;
216   } else {
217     return gst_gl_buffer_cpu_access (mem, info, size);
218   }
219 
220   return NULL;
221 }
222 
223 static void
_gl_buffer_unmap(GstGLBuffer * mem,GstMapInfo * info)224 _gl_buffer_unmap (GstGLBuffer * mem, GstMapInfo * info)
225 {
226   const GstGLFuncs *gl = mem->mem.context->gl_vtable;
227 
228   if ((info->flags & GST_MAP_GL) != 0) {
229     gl->BindBuffer (mem->target, 0);
230   }
231   /* XXX: optimistically transfer data */
232 }
233 
234 /**
235  * gst_gl_buffer_copy_buffer_sub_data:
236  * @src: the source #GstGLBuffer
237  * @dest: the destination #GstGLBuffer
238  * @offset: the offset to copy from @src
239  * @size: the size to copy from @src
240  *
241  * Copies @src into @dest using glCopyBufferSubData().
242  *
243  * Returns: whether the copy operation succeeded
244  *
245  * Since: 1.8
246  */
247 static gboolean
gst_gl_buffer_copy_buffer_sub_data(GstGLBuffer * src,GstGLBuffer * dest,gssize offset,gssize size)248 gst_gl_buffer_copy_buffer_sub_data (GstGLBuffer * src,
249     GstGLBuffer * dest, gssize offset, gssize size)
250 {
251   const GstGLFuncs *gl = src->mem.context->gl_vtable;
252   GstMapInfo sinfo, dinfo;
253 
254   if (!gl->CopyBufferSubData)
255     /* This is GL(ES) 3.0+ only */
256     return FALSE;
257 
258   if (!gst_memory_map ((GstMemory *) src, &sinfo, GST_MAP_READ | GST_MAP_GL)) {
259     GST_CAT_WARNING (GST_CAT_GL_BUFFER,
260         "failed to read map source memory %p", src);
261     return FALSE;
262   }
263 
264   if (!gst_memory_map ((GstMemory *) dest, &dinfo, GST_MAP_WRITE | GST_MAP_GL)) {
265     GST_CAT_WARNING (GST_CAT_GL_BUFFER,
266         "failed to write map destination memory %p", dest);
267     gst_memory_unmap ((GstMemory *) src, &sinfo);
268     return FALSE;
269   }
270 
271   gl->BindBuffer (GL_COPY_READ_BUFFER, src->id);
272   gl->BindBuffer (GL_COPY_WRITE_BUFFER, dest->id);
273   gl->CopyBufferSubData (GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
274       offset, 0, size);
275 
276   gst_memory_unmap ((GstMemory *) src, &sinfo);
277   gst_memory_unmap ((GstMemory *) dest, &dinfo);
278 
279   return TRUE;
280 }
281 
282 static GstGLBuffer *
_gl_buffer_copy(GstGLBuffer * src,gssize offset,gssize size)283 _gl_buffer_copy (GstGLBuffer * src, gssize offset, gssize size)
284 {
285   GstAllocator *allocator = src->mem.mem.allocator;
286   GstAllocationParams params = { 0, src->mem.mem.align, 0, 0 };
287   GstGLBuffer *dest = NULL;
288 
289   dest = _gl_buffer_new (allocator, NULL, src->mem.context,
290       src->target, src->usage_hints, &params, src->mem.mem.maxsize);
291 
292   /* If not doing a full copy, then copy to sysmem, the 2D represention of the
293    * texture would become wrong */
294   if (GST_MEMORY_FLAG_IS_SET (src, GST_GL_BASE_MEMORY_TRANSFER_NEED_UPLOAD)) {
295     if (!gst_gl_base_memory_memcpy (GST_GL_BASE_MEMORY_CAST (src),
296             GST_GL_BASE_MEMORY_CAST (dest), offset, size)) {
297       GST_CAT_WARNING (GST_CAT_GL_BUFFER, "Could not copy GL Buffer");
298       gst_memory_unref (GST_MEMORY_CAST (dest));
299       dest = NULL;
300     }
301   } else {
302     if (!gst_gl_buffer_copy_buffer_sub_data (src, dest, offset, size)) {
303       if (!gst_gl_base_memory_memcpy (GST_GL_BASE_MEMORY_CAST (src),
304               GST_GL_BASE_MEMORY_CAST (dest), offset, size)) {
305         GST_CAT_WARNING (GST_CAT_GL_BUFFER, "Could not copy GL Buffer");
306         gst_memory_unref (GST_MEMORY_CAST (dest));
307         dest = NULL;
308       }
309     }
310   }
311 
312   return dest;
313 }
314 
315 static GstMemory *
_gl_buffer_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)316 _gl_buffer_alloc (GstAllocator * allocator, gsize size,
317     GstAllocationParams * params)
318 {
319   g_critical ("Need to use gst_gl_base_memory_alloc() to allocate from "
320       "this allocator");
321 
322   return NULL;
323 }
324 
325 static void
_gl_buffer_destroy(GstGLBuffer * mem)326 _gl_buffer_destroy (GstGLBuffer * mem)
327 {
328   const GstGLFuncs *gl = mem->mem.context->gl_vtable;
329 
330   gl->DeleteBuffers (1, &mem->id);
331 }
332 
333 static void
_gst_gl_buffer_allocation_params_copy_data(GstGLBufferAllocationParams * src,GstGLBufferAllocationParams * dest)334 _gst_gl_buffer_allocation_params_copy_data (GstGLBufferAllocationParams * src,
335     GstGLBufferAllocationParams * dest)
336 {
337   memset (dest, 0, sizeof (*dest));
338 
339   gst_gl_allocation_params_copy_data (&src->parent, &dest->parent);
340 
341   dest->gl_target = src->gl_target;
342   dest->gl_usage = src->gl_usage;
343 }
344 
345 static void
_gst_gl_buffer_allocation_params_free_data(GstGLBufferAllocationParams * params)346 _gst_gl_buffer_allocation_params_free_data (GstGLBufferAllocationParams *
347     params)
348 {
349   gst_gl_allocation_params_free_data (&params->parent);
350 }
351 
352 G_DEFINE_BOXED_TYPE (GstGLBufferAllocationParams,
353     gst_gl_buffer_allocation_params,
354     (GBoxedCopyFunc) gst_gl_allocation_params_copy,
355     (GBoxedFreeFunc) gst_gl_allocation_params_free);
356 
357 /**
358  * gst_gl_buffer_allocation_params_new:
359  * @context: a #GstGLContext
360  * @alloc_size: the size in bytes to allocate
361  * @alloc_params: (allow-none): the #GstAllocationParams for @tex_id
362  * @gl_target: the OpenGL target to allocate
363  * @gl_usage: the OpenGL usage hint to allocate with
364  *
365  * Returns: a new #GstGLBufferAllocationParams for allocating OpenGL buffer
366  *          objects
367  *
368  * Since: 1.8
369  */
370 GstGLBufferAllocationParams *
gst_gl_buffer_allocation_params_new(GstGLContext * context,gsize alloc_size,GstAllocationParams * alloc_params,guint gl_target,guint gl_usage)371 gst_gl_buffer_allocation_params_new (GstGLContext * context, gsize alloc_size,
372     GstAllocationParams * alloc_params, guint gl_target, guint gl_usage)
373 {
374   GstGLBufferAllocationParams *params;
375 
376   g_return_val_if_fail (GST_IS_GL_CONTEXT (context), NULL);
377   g_return_val_if_fail (alloc_size > 0, NULL);
378 
379   params = g_new0 (GstGLBufferAllocationParams, 1);
380 
381   if (!gst_gl_allocation_params_init (&params->parent, sizeof (*params),
382           GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_BUFFER |
383           GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_ALLOC,
384           (GstGLAllocationParamsCopyFunc)
385           _gst_gl_buffer_allocation_params_copy_data,
386           (GstGLAllocationParamsFreeFunc)
387           _gst_gl_buffer_allocation_params_free_data, context, alloc_size,
388           alloc_params, NULL, 0, NULL, NULL)) {
389     g_free (params);
390     return NULL;
391   }
392 
393   params->gl_target = gl_target;
394   params->gl_usage = gl_usage;
395 
396   return params;
397 }
398 
399 static GstGLBuffer *
_gl_buffer_alloc_mem(GstGLBufferAllocator * allocator,GstGLBufferAllocationParams * params)400 _gl_buffer_alloc_mem (GstGLBufferAllocator * allocator,
401     GstGLBufferAllocationParams * params)
402 {
403   guint alloc_flags = params->parent.alloc_flags;
404 
405   g_return_val_if_fail (alloc_flags &
406       GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_BUFFER, NULL);
407   g_return_val_if_fail (alloc_flags & GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_ALLOC,
408       NULL);
409 
410   return _gl_buffer_new (GST_ALLOCATOR (allocator), NULL,
411       params->parent.context, params->gl_target, params->gl_usage,
412       params->parent.alloc_params, params->parent.alloc_size);
413 }
414 
415 G_DEFINE_TYPE (GstGLBufferAllocator, gst_gl_buffer_allocator,
416     GST_TYPE_GL_BASE_MEMORY_ALLOCATOR);
417 
418 static void
gst_gl_buffer_allocator_class_init(GstGLBufferAllocatorClass * klass)419 gst_gl_buffer_allocator_class_init (GstGLBufferAllocatorClass * klass)
420 {
421   GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
422   GstGLBaseMemoryAllocatorClass *gl_base;
423 
424   gl_base = (GstGLBaseMemoryAllocatorClass *) klass;
425 
426   gl_base->alloc = (GstGLBaseMemoryAllocatorAllocFunction) _gl_buffer_alloc_mem;
427   gl_base->create = (GstGLBaseMemoryAllocatorCreateFunction) _gl_buffer_create;
428   gl_base->map = (GstGLBaseMemoryAllocatorMapFunction) _gl_buffer_map;
429   gl_base->unmap = (GstGLBaseMemoryAllocatorUnmapFunction) _gl_buffer_unmap;
430   gl_base->copy = (GstGLBaseMemoryAllocatorCopyFunction) _gl_buffer_copy;
431   gl_base->destroy =
432       (GstGLBaseMemoryAllocatorDestroyFunction) _gl_buffer_destroy;
433 
434   allocator_class->alloc = _gl_buffer_alloc;
435 }
436 
437 static void
gst_gl_buffer_allocator_init(GstGLBufferAllocator * allocator)438 gst_gl_buffer_allocator_init (GstGLBufferAllocator * allocator)
439 {
440   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
441 
442   alloc->mem_type = GST_GL_BUFFER_ALLOCATOR_NAME;
443 
444   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
445 }
446 
447 /**
448  * gst_gl_buffer_init_once:
449  *
450  * Initializes the GL Buffer allocator. It is safe to call this function
451  * multiple times.  This must be called before any other #GstGLBuffer operation.
452  *
453  * Since: 1.8
454  */
455 void
gst_gl_buffer_init_once(void)456 gst_gl_buffer_init_once (void)
457 {
458   static volatile gsize _init = 0;
459 
460   if (g_once_init_enter (&_init)) {
461     gst_gl_base_memory_init_once ();
462 
463     GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_BUFFER, "glbuffer", 0, "OpenGL Buffer");
464 
465     _gl_buffer_allocator =
466         g_object_new (gst_gl_buffer_allocator_get_type (), NULL);
467     gst_object_ref_sink (_gl_buffer_allocator);
468 
469     /* The allocator is never unreffed */
470     GST_OBJECT_FLAG_SET (_gl_buffer_allocator, GST_OBJECT_FLAG_MAY_BE_LEAKED);
471 
472     gst_allocator_register (GST_GL_BUFFER_ALLOCATOR_NAME,
473         gst_object_ref (_gl_buffer_allocator));
474     g_once_init_leave (&_init, 1);
475   }
476 }
477 
478 /**
479  * gst_is_gl_buffer:
480  * @mem:a #GstMemory
481  *
482  * Returns: whether the memory at @mem is a #GstGLBuffer
483  *
484  * Since: 1.8
485  */
486 gboolean
gst_is_gl_buffer(GstMemory * mem)487 gst_is_gl_buffer (GstMemory * mem)
488 {
489   return mem != NULL && mem->allocator != NULL &&
490       g_type_is_a (G_OBJECT_TYPE (mem->allocator),
491       GST_TYPE_GL_BUFFER_ALLOCATOR);
492 }
493