2012-07-06 08:22:22 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-08 11:53:56 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2012-07-06 08:22:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-03 04:13:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2012-07-06 08:22:22 +00:00
|
|
|
#include <gst/video/video.h>
|
|
|
|
|
|
|
|
#include "gstglmemory.h"
|
|
|
|
|
2012-09-27 05:53:46 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstglmemory
|
|
|
|
* @short_description: memory subclass for GL textures
|
|
|
|
* @see_also: #GstMemory, #GstAllocator, #GstGLBufferPool
|
|
|
|
*
|
|
|
|
* GstGLMemory is a #GstMemory subclass providing support for the mapping of
|
|
|
|
* GL textures.
|
|
|
|
*
|
|
|
|
* #GstGLMemory is created through gst_gl_memory_alloc() or system memory can
|
|
|
|
* be wrapped through gst_gl_memory_wrapped().
|
|
|
|
*
|
|
|
|
* Data is uploaded or downloaded from the GPU as is necessary.
|
|
|
|
*/
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
#define USING_OPENGL(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_OPENGL)
|
|
|
|
#define USING_OPENGL3(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_OPENGL3)
|
|
|
|
#define USING_GLES(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES)
|
|
|
|
#define USING_GLES2(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES2)
|
|
|
|
#define USING_GLES3(context) (gst_gl_context_get_gl_api (context) & GST_GL_API_GLES3)
|
2012-12-06 07:40:26 +00:00
|
|
|
|
2012-09-16 11:36:09 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_MEMORY);
|
2012-07-06 08:22:22 +00:00
|
|
|
#define GST_CAT_DEFUALT GST_CAT_GL_MEMORY
|
|
|
|
|
|
|
|
static GstAllocator *_gl_allocator;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstGLMemory *src;
|
|
|
|
GLuint tex_id;
|
|
|
|
} GstGLMemoryCopyParams;
|
|
|
|
|
|
|
|
static void
|
|
|
|
_gl_mem_init (GstGLMemory * mem, GstAllocator * allocator, GstMemory * parent,
|
2013-09-15 04:23:43 +00:00
|
|
|
GstGLContext * context, GstVideoFormat v_format, gsize width, gsize height,
|
2012-09-16 11:36:09 +00:00
|
|
|
gpointer user_data, GDestroyNotify notify)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
2012-09-16 11:36:09 +00:00
|
|
|
gsize maxsize;
|
|
|
|
GstVideoInfo info;
|
|
|
|
|
|
|
|
gst_video_info_set_format (&info, v_format, width, height);
|
|
|
|
|
|
|
|
maxsize = info.size;
|
|
|
|
|
2012-07-06 08:22:22 +00:00
|
|
|
gst_memory_init (GST_MEMORY_CAST (mem), GST_MEMORY_FLAG_NO_SHARE,
|
2012-09-16 11:36:09 +00:00
|
|
|
allocator, parent, maxsize, 0, 0, maxsize);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
mem->context = gst_object_ref (context);
|
2012-07-06 08:22:22 +00:00
|
|
|
mem->gl_format = GL_RGBA;
|
|
|
|
mem->v_format = v_format;
|
|
|
|
mem->width = width;
|
|
|
|
mem->height = height;
|
2012-09-16 11:36:09 +00:00
|
|
|
mem->notify = notify;
|
|
|
|
mem->user_data = user_data;
|
|
|
|
mem->wrapped = FALSE;
|
2013-09-15 04:23:43 +00:00
|
|
|
mem->upload = gst_gl_upload_new (context);
|
|
|
|
mem->download = gst_gl_download_new (context);
|
2012-09-16 11:36:09 +00:00
|
|
|
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_GL_MEMORY,
|
2013-03-09 11:53:41 +00:00
|
|
|
"new GL texture memory:%p format:%u dimensions:%" G_GSIZE_FORMAT
|
|
|
|
"x%" G_GSIZE_FORMAT, mem, v_format, width, height);
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstGLMemory *
|
|
|
|
_gl_mem_new (GstAllocator * allocator, GstMemory * parent,
|
2013-09-15 04:23:43 +00:00
|
|
|
GstGLContext * context, GstVideoFormat v_format, gsize width, gsize height,
|
2012-09-16 11:36:09 +00:00
|
|
|
gpointer user_data, GDestroyNotify notify)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
|
|
|
GstGLMemory *mem;
|
|
|
|
GLuint tex_id;
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_gen_texture (context, &tex_id, v_format, width, height);
|
2012-07-06 08:22:22 +00:00
|
|
|
if (!tex_id) {
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_WARNING (GST_CAT_GL_MEMORY,
|
2013-09-15 04:23:43 +00:00
|
|
|
"Could not create GL texture with context:%p", context);
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_TRACE (GST_CAT_GL_MEMORY, "created texture %u", tex_id);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
|
|
|
mem = g_slice_alloc (sizeof (GstGLMemory));
|
2013-09-15 04:23:43 +00:00
|
|
|
_gl_mem_init (mem, allocator, parent, context, v_format, width, height,
|
2012-09-16 11:36:09 +00:00
|
|
|
user_data, notify);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
|
|
|
mem->tex_id = tex_id;
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2012-08-08 06:08:40 +00:00
|
|
|
_gl_mem_map (GstGLMemory * gl_mem, gsize maxsize, GstMapFlags flags)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
2012-09-16 11:36:09 +00:00
|
|
|
gpointer data;
|
|
|
|
|
|
|
|
g_return_val_if_fail (maxsize == gl_mem->mem.maxsize, NULL);
|
|
|
|
|
|
|
|
if ((flags & GST_MAP_GL) == GST_MAP_GL) {
|
|
|
|
if ((flags & GST_MAP_READ) == GST_MAP_READ) {
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_TRACE (GST_CAT_GL_MEMORY, "mapping GL texture:%u for reading",
|
|
|
|
gl_mem->tex_id);
|
2012-09-16 11:36:09 +00:00
|
|
|
if (GST_GL_MEMORY_FLAG_IS_SET (gl_mem, GST_GL_MEMORY_FLAG_NEED_UPLOAD)) {
|
|
|
|
if (!GST_GL_MEMORY_FLAG_IS_SET (gl_mem,
|
|
|
|
GST_GL_MEMORY_FLAG_UPLOAD_INITTED)) {
|
2013-07-18 10:21:57 +00:00
|
|
|
if (!gst_gl_upload_init_format (gl_mem->upload, gl_mem->v_format,
|
|
|
|
gl_mem->width, gl_mem->height, gl_mem->width,
|
|
|
|
gl_mem->height)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2012-09-16 11:36:09 +00:00
|
|
|
GST_GL_MEMORY_FLAG_SET (gl_mem, GST_GL_MEMORY_FLAG_UPLOAD_INITTED);
|
|
|
|
}
|
|
|
|
|
2013-07-18 10:21:57 +00:00
|
|
|
if (!gst_gl_upload_perform_with_memory (gl_mem->upload, gl_mem)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2012-09-16 11:36:09 +00:00
|
|
|
}
|
2012-09-20 13:21:19 +00:00
|
|
|
} else {
|
|
|
|
GST_CAT_TRACE (GST_CAT_GL_MEMORY, "mapping GL texture:%u for writing",
|
|
|
|
gl_mem->tex_id);
|
2012-09-16 11:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data = &gl_mem->tex_id;
|
|
|
|
} else { /* not GL */
|
|
|
|
if ((flags & GST_MAP_READ) == GST_MAP_READ) {
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_TRACE (GST_CAT_GL_MEMORY,
|
|
|
|
"mapping GL texture:%u for reading from system memory",
|
|
|
|
gl_mem->tex_id);
|
2012-09-16 11:36:09 +00:00
|
|
|
if (GST_GL_MEMORY_FLAG_IS_SET (gl_mem, GST_GL_MEMORY_FLAG_NEED_DOWNLOAD)) {
|
|
|
|
if (!GST_GL_MEMORY_FLAG_IS_SET (gl_mem,
|
|
|
|
GST_GL_MEMORY_FLAG_DOWNLOAD_INITTED)) {
|
2013-07-18 10:21:57 +00:00
|
|
|
if (!gst_gl_download_init_format (gl_mem->download, gl_mem->v_format,
|
|
|
|
gl_mem->width, gl_mem->height)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2012-09-16 11:36:09 +00:00
|
|
|
GST_GL_MEMORY_FLAG_SET (gl_mem, GST_GL_MEMORY_FLAG_DOWNLOAD_INITTED);
|
|
|
|
}
|
|
|
|
|
2013-07-18 10:21:57 +00:00
|
|
|
if (!gst_gl_download_perform_with_memory (gl_mem->download, gl_mem)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2012-09-16 11:36:09 +00:00
|
|
|
}
|
2012-09-20 13:21:19 +00:00
|
|
|
} else {
|
|
|
|
GST_CAT_TRACE (GST_CAT_GL_MEMORY,
|
|
|
|
"mapping GL texture:%u for writing to system memory", gl_mem->tex_id);
|
2012-09-16 11:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data = gl_mem->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl_mem->map_flags = flags;
|
|
|
|
|
|
|
|
return data;
|
2013-07-18 10:21:57 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-08 06:08:40 +00:00
|
|
|
_gl_mem_unmap (GstGLMemory * gl_mem)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
2012-09-16 11:36:09 +00:00
|
|
|
if ((gl_mem->map_flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
|
|
|
|
if ((gl_mem->map_flags & GST_MAP_GL) == GST_MAP_GL) {
|
|
|
|
GST_GL_MEMORY_FLAG_SET (gl_mem, GST_GL_MEMORY_FLAG_NEED_DOWNLOAD);
|
|
|
|
} else {
|
|
|
|
GST_GL_MEMORY_FLAG_SET (gl_mem, GST_GL_MEMORY_FLAG_NEED_UPLOAD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gl_mem->map_flags = 0;
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-15 04:23:43 +00:00
|
|
|
_gl_mem_copy_thread (GstGLContext * context, gpointer data)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
|
|
|
GstGLMemoryCopyParams *copy_params;
|
|
|
|
GstGLMemory *src;
|
|
|
|
GLuint tex_id;
|
|
|
|
GLuint rboId, fboId;
|
|
|
|
gsize width, height;
|
|
|
|
GLuint gl_format;
|
|
|
|
GstVideoFormat v_format;
|
2013-09-15 04:23:43 +00:00
|
|
|
GstGLFuncs *gl;
|
2012-07-06 08:22:22 +00:00
|
|
|
|
|
|
|
copy_params = (GstGLMemoryCopyParams *) data;
|
|
|
|
src = copy_params->src;
|
|
|
|
width = src->width;
|
|
|
|
height = src->height;
|
|
|
|
v_format = src->v_format;
|
|
|
|
gl_format = src->gl_format;
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
gl = src->context->gl_vtable;
|
|
|
|
|
2013-01-16 04:21:44 +00:00
|
|
|
if (!gl->GenFramebuffers) {
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_set_error (src->context,
|
2012-07-06 08:22:22 +00:00
|
|
|
"Context, EXT_framebuffer_object not supported");
|
2013-09-15 04:23:43 +00:00
|
|
|
goto error;
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_gen_texture_thread (src->context, &tex_id, v_format, width,
|
2012-07-06 08:22:22 +00:00
|
|
|
height);
|
|
|
|
if (!tex_id) {
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_WARNING (GST_CAT_GL_MEMORY,
|
2013-09-15 04:23:43 +00:00
|
|
|
"Could not create GL texture with context:%p", src->context);
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_GL_MEMORY, "created texture %i", tex_id);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
|
|
|
/* create a framebuffer object */
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->GenFramebuffers (1, &fboId);
|
|
|
|
gl->BindFramebuffer (GL_FRAMEBUFFER, fboId);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
|
|
|
/* create a renderbuffer object */
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->GenRenderbuffers (1, &rboId);
|
|
|
|
gl->BindRenderbuffer (GL_RENDERBUFFER, rboId);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
if (USING_OPENGL (src->context)) {
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->RenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width,
|
2012-12-06 07:40:26 +00:00
|
|
|
height);
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->RenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH24_STENCIL8,
|
2012-12-06 07:40:26 +00:00
|
|
|
width, height);
|
|
|
|
}
|
2013-09-15 04:23:43 +00:00
|
|
|
if (USING_GLES2 (src->context)) {
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->RenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16,
|
2012-12-06 07:40:26 +00:00
|
|
|
width, height);
|
|
|
|
}
|
2012-07-06 08:22:22 +00:00
|
|
|
/* attach the renderbuffer to depth attachment point */
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->FramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
|
|
|
|
GL_RENDERBUFFER, rboId);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
if (USING_OPENGL (src->context)) {
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->FramebufferRenderbuffer (GL_FRAMEBUFFER,
|
|
|
|
GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboId);
|
2012-12-06 07:40:26 +00:00
|
|
|
}
|
2012-07-06 08:22:22 +00:00
|
|
|
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->FramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
2012-07-06 08:22:22 +00:00
|
|
|
GL_TEXTURE_RECTANGLE_ARB, src->tex_id, 0);
|
|
|
|
|
|
|
|
/* check FBO status */
|
2013-09-15 04:23:43 +00:00
|
|
|
if (!gst_gl_context_check_framebuffer_status (src->context))
|
2012-07-06 08:22:22 +00:00
|
|
|
goto fbo_error;
|
|
|
|
|
|
|
|
/* copy tex */
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_RECTANGLE_ARB, tex_id);
|
|
|
|
gl->CopyTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, gl_format, 0, 0,
|
2012-07-06 08:22:22 +00:00
|
|
|
width, height, 0);
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_RECTANGLE_ARB, 0);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->DeleteRenderbuffers (1, &rboId);
|
|
|
|
gl->DeleteFramebuffers (1, &fboId);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
|
|
|
copy_params->tex_id = tex_id;
|
|
|
|
|
2012-08-08 06:08:40 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* ERRORS */
|
2012-07-06 08:22:22 +00:00
|
|
|
fbo_error:
|
|
|
|
{
|
2013-01-16 04:21:44 +00:00
|
|
|
gl->DeleteRenderbuffers (1, &rboId);
|
|
|
|
gl->DeleteFramebuffers (1, &fboId);
|
2012-07-06 08:22:22 +00:00
|
|
|
|
|
|
|
copy_params->tex_id = 0;
|
2013-09-15 04:23:43 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
return;
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GstMemory *
|
2012-08-08 06:08:40 +00:00
|
|
|
_gl_mem_copy (GstGLMemory * src, gssize offset, gssize size)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
|
|
|
GstGLMemory *dest;
|
|
|
|
GstGLMemoryCopyParams copy_params;
|
|
|
|
|
2013-07-03 04:13:00 +00:00
|
|
|
if (GST_GL_MEMORY_FLAG_IS_SET (src, GST_GL_MEMORY_FLAG_NEED_UPLOAD)) {
|
2013-09-15 04:23:43 +00:00
|
|
|
dest = _gl_mem_new (src->mem.allocator, NULL, src->context, src->v_format,
|
2013-07-03 04:13:00 +00:00
|
|
|
src->width, src->height, NULL, NULL);
|
|
|
|
dest->data = g_malloc (src->mem.maxsize);
|
|
|
|
memcpy (dest->data, src->data, src->mem.maxsize);
|
|
|
|
GST_GL_MEMORY_FLAG_SET (dest, GST_GL_MEMORY_FLAG_NEED_UPLOAD);
|
|
|
|
} else {
|
|
|
|
copy_params = (GstGLMemoryCopyParams) {
|
|
|
|
src, 0,};
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_thread_add (src->context, _gl_mem_copy_thread, ©_params);
|
2013-07-03 04:13:00 +00:00
|
|
|
|
|
|
|
dest = g_slice_alloc (sizeof (GstGLMemory));
|
2013-09-15 04:23:43 +00:00
|
|
|
_gl_mem_init (dest, src->mem.allocator, NULL, src->context, src->v_format,
|
2013-07-03 04:13:00 +00:00
|
|
|
src->width, src->height, NULL, NULL);
|
|
|
|
|
|
|
|
if (!copy_params.tex_id)
|
|
|
|
GST_CAT_WARNING (GST_CAT_GL_MEMORY, "Could not copy GL Memory");
|
|
|
|
|
|
|
|
dest->tex_id = copy_params.tex_id;
|
|
|
|
dest->data = g_malloc (src->mem.maxsize);
|
|
|
|
if (dest->data == NULL) {
|
|
|
|
GST_CAT_WARNING (GST_CAT_GL_MEMORY, "Could not copy GL Memory");
|
|
|
|
gst_memory_unref ((GstMemory *) dest);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
GST_GL_MEMORY_FLAG_SET (dest, GST_GL_MEMORY_FLAG_NEED_DOWNLOAD);
|
2012-09-16 11:36:09 +00:00
|
|
|
}
|
2012-07-06 08:22:22 +00:00
|
|
|
|
2012-09-20 13:21:19 +00:00
|
|
|
GST_CAT_DEBUG (GST_CAT_GL_MEMORY, "copied texture:%u into texture %u",
|
|
|
|
src->tex_id, dest->tex_id);
|
|
|
|
|
2012-07-06 08:22:22 +00:00
|
|
|
return (GstMemory *) dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstMemory *
|
2012-08-08 06:08:40 +00:00
|
|
|
_gl_mem_share (GstGLMemory * mem, gssize offset, gssize size)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2012-08-08 06:08:40 +00:00
|
|
|
_gl_mem_is_span (GstGLMemory * mem1, GstGLMemory * mem2, gsize * offset)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2012-08-08 06:08:40 +00:00
|
|
|
GstMemory *
|
|
|
|
_gl_mem_alloc (GstAllocator * allocator, gsize size,
|
|
|
|
GstAllocationParams * params)
|
2012-07-06 08:22:22 +00:00
|
|
|
{
|
2012-08-08 06:08:40 +00:00
|
|
|
g_warning ("use gst_gl_memory_alloc () to allocate from this "
|
|
|
|
"GstGLMemory allocator");
|
2012-07-07 15:09:56 +00:00
|
|
|
|
2012-08-08 06:08:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gl_mem_free (GstAllocator * allocator, GstMemory * mem)
|
|
|
|
{
|
|
|
|
GstGLMemory *gl_mem = (GstGLMemory *) mem;
|
|
|
|
|
2012-09-16 11:36:09 +00:00
|
|
|
if (gl_mem->tex_id)
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_context_del_texture (gl_mem->context, &gl_mem->tex_id);
|
2012-08-08 06:08:40 +00:00
|
|
|
|
2013-06-13 04:18:24 +00:00
|
|
|
gst_object_unref (gl_mem->upload);
|
|
|
|
gst_object_unref (gl_mem->download);
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_object_unref (gl_mem->context);
|
2012-08-08 06:08:40 +00:00
|
|
|
|
2012-09-16 11:36:09 +00:00
|
|
|
if (gl_mem->notify)
|
|
|
|
gl_mem->notify (gl_mem->user_data);
|
|
|
|
|
|
|
|
if (gl_mem->data && !gl_mem->wrapped) {
|
|
|
|
g_free (gl_mem->data);
|
|
|
|
gl_mem->data = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-08 06:08:40 +00:00
|
|
|
g_slice_free (GstGLMemory, gl_mem);
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_gl_memory_alloc:
|
2013-09-15 04:23:43 +00:00
|
|
|
* @context:a #GstGLContext
|
2012-07-06 08:22:22 +00:00
|
|
|
* @format: the format for the texture
|
|
|
|
* @width: width of the texture
|
|
|
|
* @height: height of the texture
|
2012-09-27 05:53:46 +00:00
|
|
|
*
|
|
|
|
* Returns: a #GstMemory object with a GL texture specified by @format, @width and @height
|
2013-09-15 04:23:43 +00:00
|
|
|
* from @context
|
2012-07-06 08:22:22 +00:00
|
|
|
*/
|
|
|
|
GstMemory *
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_memory_alloc (GstGLContext * context, GstVideoFormat format,
|
2012-07-06 08:22:22 +00:00
|
|
|
gsize width, gsize height)
|
|
|
|
{
|
2012-09-16 11:36:09 +00:00
|
|
|
GstGLMemory *mem;
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
mem = _gl_mem_new (_gl_allocator, NULL, context, format, width, height,
|
2012-09-16 11:36:09 +00:00
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
mem->data = g_malloc (mem->mem.maxsize);
|
|
|
|
if (mem->data == NULL) {
|
|
|
|
gst_memory_unref ((GstMemory *) mem);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (GstMemory *) mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gst_gl_memory_wrapped
|
2013-09-15 04:23:43 +00:00
|
|
|
* @context:a #GstGLContext
|
2012-09-16 11:36:09 +00:00
|
|
|
* @format: the format for the texture
|
|
|
|
* @width: width of the texture
|
|
|
|
* @height: height of the texture
|
|
|
|
* @data: the data to wrap
|
|
|
|
* @user_data: data called with for @notify
|
|
|
|
* @notify: function called with @user_data when @data needs to be freed
|
|
|
|
*
|
2012-09-27 05:53:46 +00:00
|
|
|
* Returns: a #GstGLMemory object with a GL texture specified by @format, @width and @height
|
2013-09-15 04:23:43 +00:00
|
|
|
* from @context and contents specified by @data
|
2012-09-16 11:36:09 +00:00
|
|
|
*/
|
|
|
|
GstGLMemory *
|
2013-09-15 04:23:43 +00:00
|
|
|
gst_gl_memory_wrapped (GstGLContext * context, GstVideoFormat format,
|
2012-09-16 11:36:09 +00:00
|
|
|
guint width, guint height, gpointer data,
|
|
|
|
gpointer user_data, GDestroyNotify notify)
|
|
|
|
{
|
|
|
|
GstGLMemory *mem;
|
|
|
|
|
2013-09-15 04:23:43 +00:00
|
|
|
mem = _gl_mem_new (_gl_allocator, NULL, context, format, width, height,
|
2012-09-16 11:36:09 +00:00
|
|
|
user_data, notify);
|
|
|
|
|
|
|
|
mem->data = data;
|
|
|
|
mem->wrapped = TRUE;
|
|
|
|
|
|
|
|
GST_GL_MEMORY_FLAG_SET (mem, GST_GL_MEMORY_FLAG_NEED_UPLOAD);
|
|
|
|
|
|
|
|
return mem;
|
2012-07-06 08:22:22 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 06:08:40 +00:00
|
|
|
G_DEFINE_TYPE (GstGLAllocator, gst_gl_allocator, GST_TYPE_ALLOCATOR);
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_allocator_class_init (GstGLAllocatorClass * klass)
|
|
|
|
{
|
|
|
|
GstAllocatorClass *allocator_class;
|
|
|
|
|
|
|
|
allocator_class = (GstAllocatorClass *) klass;
|
|
|
|
|
|
|
|
allocator_class->alloc = _gl_mem_alloc;
|
|
|
|
allocator_class->free = _gl_mem_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_allocator_init (GstGLAllocator * allocator)
|
|
|
|
{
|
|
|
|
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
|
|
|
alloc->mem_type = GST_GL_MEMORY_ALLOCATOR;
|
|
|
|
alloc->mem_map = (GstMemoryMapFunction) _gl_mem_map;
|
|
|
|
alloc->mem_unmap = (GstMemoryUnmapFunction) _gl_mem_unmap;
|
|
|
|
alloc->mem_copy = (GstMemoryCopyFunction) _gl_mem_copy;
|
|
|
|
alloc->mem_share = (GstMemoryShareFunction) _gl_mem_share;
|
|
|
|
alloc->mem_is_span = (GstMemoryIsSpanFunction) _gl_mem_is_span;
|
|
|
|
}
|
|
|
|
|
2012-07-06 08:22:22 +00:00
|
|
|
/**
|
|
|
|
* gst_gl_memory_init:
|
|
|
|
*
|
2012-08-08 06:08:40 +00:00
|
|
|
* Initializes the GL Memory allocator. It is safe to call this function
|
|
|
|
* multiple times. This must be called before any other GstGLMemory operation.
|
2012-07-06 08:22:22 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gst_gl_memory_init (void)
|
|
|
|
{
|
|
|
|
static volatile gsize _init = 0;
|
|
|
|
|
|
|
|
if (g_once_init_enter (&_init)) {
|
2012-09-16 11:36:09 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_MEMORY, "glmemory", 0, "OpenGL Memory");
|
|
|
|
|
2012-08-08 06:08:40 +00:00
|
|
|
_gl_allocator = g_object_new (gst_gl_allocator_get_type (), NULL);
|
2012-07-07 15:09:56 +00:00
|
|
|
|
2012-08-08 06:08:40 +00:00
|
|
|
gst_allocator_register (GST_GL_MEMORY_ALLOCATOR,
|
|
|
|
gst_object_ref (_gl_allocator));
|
2012-07-06 08:22:22 +00:00
|
|
|
g_once_init_leave (&_init, 1);
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 06:08:40 +00:00
|
|
|
|
2012-09-16 11:36:09 +00:00
|
|
|
/**
|
|
|
|
* gst_is_gl_memory:
|
|
|
|
* @mem:a #GstMemory
|
|
|
|
*
|
2012-09-27 05:53:46 +00:00
|
|
|
* Returns: whether the memory at @mem is a #GstGLMemory
|
2012-09-16 11:36:09 +00:00
|
|
|
*/
|
2012-08-08 06:08:40 +00:00
|
|
|
gboolean
|
|
|
|
gst_is_gl_memory (GstMemory * mem)
|
|
|
|
{
|
2012-09-16 11:36:09 +00:00
|
|
|
return mem != NULL && mem->allocator == _gl_allocator;
|
2012-08-08 06:08:40 +00:00
|
|
|
}
|