2013-02-19 09:40:31 +00:00
|
|
|
/* GStreamer dmabuf allocator
|
|
|
|
* Copyright (C) 2013 Linaro SA
|
2013-02-18 14:18:38 +00:00
|
|
|
* Author: Benjamin Gaignard <benjamin.gaignard@linaro.org> for Linaro.
|
|
|
|
*
|
|
|
|
* 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 mordetails.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2013-02-19 10:52:22 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-02-18 14:18:38 +00:00
|
|
|
#include "gstdmabuf.h"
|
|
|
|
|
2013-02-19 09:05:17 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstdmabuf
|
|
|
|
* @short_description: Memory wrapper for Linux dmabuf memory
|
|
|
|
* @see_also: #GstMemory
|
|
|
|
*
|
2013-02-19 09:40:31 +00:00
|
|
|
* Since: 1.2
|
2013-02-19 09:05:17 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-19 14:33:29 +00:00
|
|
|
#ifdef HAVE_MMAP
|
2013-02-18 14:18:38 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2013-09-19 14:33:29 +00:00
|
|
|
#endif
|
2013-02-18 14:18:38 +00:00
|
|
|
|
2013-02-19 08:35:51 +00:00
|
|
|
/*
|
2013-02-18 14:18:38 +00:00
|
|
|
* GstDmaBufMemory
|
|
|
|
* @fd: the file descriptor associated this memory
|
|
|
|
* @data: mmapped address
|
|
|
|
* @mmapping_flags: mmapping flags
|
|
|
|
* @mmap_count: mmapping counter
|
|
|
|
* @lock: a mutex to make mmapping thread safe
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstMemory mem;
|
|
|
|
|
|
|
|
gint fd;
|
|
|
|
gpointer data;
|
|
|
|
gint mmapping_flags;
|
|
|
|
gint mmap_count;
|
2013-05-03 09:23:59 +00:00
|
|
|
gsize mmap_size;
|
2013-02-18 14:18:38 +00:00
|
|
|
GMutex lock;
|
|
|
|
} GstDmaBufMemory;
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (dmabuf_debug);
|
|
|
|
#define GST_CAT_DEFAULT dmabuf_debug
|
|
|
|
|
|
|
|
static void
|
2013-05-17 07:16:08 +00:00
|
|
|
gst_dmabuf_allocator_free (GstAllocator * allocator, GstMemory * gmem)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
2013-09-19 14:33:29 +00:00
|
|
|
#ifdef HAVE_MMAP
|
2013-05-17 07:16:08 +00:00
|
|
|
GstDmaBufMemory *mem = (GstDmaBufMemory *) gmem;
|
2013-02-18 14:18:38 +00:00
|
|
|
|
2013-05-17 07:16:08 +00:00
|
|
|
if (mem->data) {
|
|
|
|
g_warning (G_STRLOC ":%s: Freeing memory %p still mapped", G_STRFUNC, mem);
|
|
|
|
munmap ((void *) mem->data, mem->mmap_size);
|
|
|
|
}
|
|
|
|
close (mem->fd);
|
|
|
|
g_mutex_clear (&mem->lock);
|
|
|
|
g_slice_free (GstDmaBufMemory, mem);
|
|
|
|
GST_DEBUG ("%p: freed", mem);
|
2013-09-19 14:33:29 +00:00
|
|
|
#endif
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gpointer
|
2013-03-30 11:08:06 +00:00
|
|
|
gst_dmabuf_mem_map (GstMemory * gmem, gsize maxsize, GstMapFlags flags)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
2013-09-05 00:21:54 +00:00
|
|
|
#ifdef HAVE_MMAP
|
2013-03-30 11:08:06 +00:00
|
|
|
GstDmaBufMemory *mem = (GstDmaBufMemory *) gmem;
|
2013-02-18 14:18:38 +00:00
|
|
|
gint prot;
|
|
|
|
gpointer ret = NULL;
|
|
|
|
|
|
|
|
g_mutex_lock (&mem->lock);
|
|
|
|
|
|
|
|
prot = flags & GST_MAP_READ ? PROT_READ : 0;
|
|
|
|
prot |= flags & GST_MAP_WRITE ? PROT_WRITE : 0;
|
|
|
|
|
|
|
|
/* do not mmap twice the buffer */
|
|
|
|
if (mem->data) {
|
|
|
|
/* only return address if mapping flags are a subset
|
|
|
|
* of the previous flags */
|
2013-08-23 16:06:36 +00:00
|
|
|
if ((mem->mmapping_flags & prot) && (mem->mmap_size >= maxsize)) {
|
2013-02-18 14:18:38 +00:00
|
|
|
ret = mem->data;
|
2013-08-23 16:06:36 +00:00
|
|
|
mem->mmap_count++;
|
|
|
|
}
|
2013-02-18 14:18:38 +00:00
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-05-02 13:37:14 +00:00
|
|
|
if (mem->fd != -1) {
|
2013-02-18 14:18:38 +00:00
|
|
|
mem->data = mmap (0, maxsize, prot, MAP_SHARED, mem->fd, 0);
|
2013-05-02 13:37:14 +00:00
|
|
|
if (mem->data == MAP_FAILED) {
|
|
|
|
mem->data = NULL;
|
|
|
|
GST_ERROR ("%p: fd %d: mmap failed: %s", mem, mem->fd,
|
2013-05-03 09:29:05 +00:00
|
|
|
g_strerror (errno));
|
2013-05-02 13:37:14 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 14:18:38 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
|
|
|
|
|
|
|
|
if (mem->data) {
|
|
|
|
mem->mmapping_flags = prot;
|
2013-05-03 09:23:59 +00:00
|
|
|
mem->mmap_size = maxsize;
|
2013-02-18 14:18:38 +00:00
|
|
|
mem->mmap_count++;
|
|
|
|
ret = mem->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_mutex_unlock (&mem->lock);
|
|
|
|
return ret;
|
2013-09-05 00:21:54 +00:00
|
|
|
#else /* !HAVE_MMAP */
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 11:08:06 +00:00
|
|
|
static void
|
|
|
|
gst_dmabuf_mem_unmap (GstMemory * gmem)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
2013-09-19 14:33:29 +00:00
|
|
|
#ifdef HAVE_MMAP
|
2013-03-30 11:08:06 +00:00
|
|
|
GstDmaBufMemory *mem = (GstDmaBufMemory *) gmem;
|
2013-02-18 14:18:38 +00:00
|
|
|
g_mutex_lock (&mem->lock);
|
|
|
|
|
|
|
|
if (mem->data && !(--mem->mmap_count)) {
|
2013-05-03 09:23:59 +00:00
|
|
|
munmap ((void *) mem->data, mem->mmap_size);
|
2013-02-18 14:18:38 +00:00
|
|
|
mem->data = NULL;
|
2013-05-03 09:23:59 +00:00
|
|
|
mem->mmap_size = 0;
|
2013-02-18 14:18:38 +00:00
|
|
|
mem->mmapping_flags = 0;
|
|
|
|
GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
|
|
|
|
}
|
|
|
|
g_mutex_unlock (&mem->lock);
|
2013-09-05 00:21:54 +00:00
|
|
|
#endif
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 11:08:06 +00:00
|
|
|
static GstMemory *
|
|
|
|
gst_dmabuf_mem_share (GstMemory * gmem, gssize offset, gssize size)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
2013-09-19 14:33:29 +00:00
|
|
|
#ifdef HAVE_MMAP
|
2013-03-30 11:08:06 +00:00
|
|
|
GstDmaBufMemory *mem = (GstDmaBufMemory *) gmem;
|
2013-02-18 14:18:38 +00:00
|
|
|
GstDmaBufMemory *sub;
|
|
|
|
GstMemory *parent;
|
2013-02-19 10:21:40 +00:00
|
|
|
|
2013-02-18 14:18:38 +00:00
|
|
|
GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
|
|
|
|
size);
|
|
|
|
|
|
|
|
/* find the real parent */
|
|
|
|
if ((parent = mem->mem.parent) == NULL)
|
|
|
|
parent = (GstMemory *) mem;
|
|
|
|
|
|
|
|
if (size == -1)
|
2013-05-03 09:23:59 +00:00
|
|
|
size = gmem->maxsize - offset;
|
2013-02-18 14:18:38 +00:00
|
|
|
|
2013-05-03 09:12:04 +00:00
|
|
|
sub = g_slice_new0 (GstDmaBufMemory);
|
2013-02-18 14:18:38 +00:00
|
|
|
/* the shared memory is always readonly */
|
|
|
|
gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
|
|
|
|
GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
|
|
|
|
mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
|
|
|
|
|
2013-05-03 09:12:04 +00:00
|
|
|
sub->fd = dup (mem->fd);
|
|
|
|
g_mutex_init (&sub->lock);
|
|
|
|
|
2013-03-30 11:08:06 +00:00
|
|
|
return GST_MEMORY_CAST (sub);
|
2013-09-19 14:33:29 +00:00
|
|
|
#else /* !HAVE_MMAP */
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstAllocator parent;
|
2013-03-30 11:08:06 +00:00
|
|
|
} GstDmaBufAllocator;
|
2013-02-18 14:18:38 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstAllocatorClass parent_class;
|
2013-03-30 11:08:06 +00:00
|
|
|
} GstDmaBufAllocatorClass;
|
2013-02-18 14:18:38 +00:00
|
|
|
|
|
|
|
GType dmabuf_mem_allocator_get_type (void);
|
2013-03-30 11:08:06 +00:00
|
|
|
G_DEFINE_TYPE (GstDmaBufAllocator, dmabuf_mem_allocator, GST_TYPE_ALLOCATOR);
|
2013-02-18 14:18:38 +00:00
|
|
|
|
|
|
|
#define GST_TYPE_DMABUF_ALLOCATOR (dmabuf_mem_allocator_get_type())
|
|
|
|
#define GST_IS_DMABUF_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_DMABUF_ALLOCATOR))
|
|
|
|
|
|
|
|
static void
|
2013-03-30 11:08:06 +00:00
|
|
|
dmabuf_mem_allocator_class_init (GstDmaBufAllocatorClass * klass)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
|
|
|
GstAllocatorClass *allocator_class;
|
|
|
|
|
|
|
|
allocator_class = (GstAllocatorClass *) klass;
|
|
|
|
|
2013-03-30 11:08:06 +00:00
|
|
|
allocator_class->alloc = NULL;
|
|
|
|
allocator_class->free = gst_dmabuf_allocator_free;
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-03-30 11:08:06 +00:00
|
|
|
dmabuf_mem_allocator_init (GstDmaBufAllocator * allocator)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
|
|
|
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
2013-07-15 13:23:17 +00:00
|
|
|
alloc->mem_type = GST_ALLOCATOR_DMABUF;
|
2013-03-30 11:08:06 +00:00
|
|
|
alloc->mem_map = gst_dmabuf_mem_map;
|
|
|
|
alloc->mem_unmap = gst_dmabuf_mem_unmap;
|
|
|
|
alloc->mem_share = gst_dmabuf_mem_share;
|
2013-04-25 15:04:50 +00:00
|
|
|
/* Use the default, fallback copy function */
|
2013-02-22 08:07:06 +00:00
|
|
|
|
|
|
|
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-05 00:21:54 +00:00
|
|
|
* gst_dmabuf_allocator_new:
|
2013-02-19 08:35:51 +00:00
|
|
|
*
|
2013-09-05 00:21:54 +00:00
|
|
|
* Return a new dmabuf allocator.
|
2013-02-19 08:35:51 +00:00
|
|
|
*
|
2013-09-05 00:21:54 +00:00
|
|
|
* Returns: (transfer full): a new dmabuf allocator, or NULL if the allocator
|
2013-02-19 09:40:31 +00:00
|
|
|
* isn't available. Use gst_object_unref() to release the allocator after
|
|
|
|
* usage
|
|
|
|
*
|
|
|
|
* Since: 1.2
|
2013-02-18 14:18:38 +00:00
|
|
|
*/
|
|
|
|
GstAllocator *
|
2013-09-05 00:21:54 +00:00
|
|
|
gst_dmabuf_allocator_new (void)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
2013-09-05 00:21:54 +00:00
|
|
|
GST_DEBUG_CATEGORY_INIT (dmabuf_debug, "dmabuf", 0, "dmabuf memory");
|
2013-02-18 14:18:38 +00:00
|
|
|
|
2013-09-05 00:21:54 +00:00
|
|
|
return g_object_new (GST_TYPE_DMABUF_ALLOCATOR, NULL);
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-19 09:40:31 +00:00
|
|
|
/**
|
2013-02-19 08:35:51 +00:00
|
|
|
* gst_dmabuf_allocator_alloc:
|
2013-02-19 10:21:40 +00:00
|
|
|
* @allocator: (allow-none): allocator to be used for this memory
|
2013-02-18 14:18:38 +00:00
|
|
|
* @fd: dmabuf file descriptor
|
|
|
|
* @size: memory size
|
2013-02-19 08:35:51 +00:00
|
|
|
*
|
2013-02-19 09:40:31 +00:00
|
|
|
* Return a %GstMemory that wraps a dmabuf file descriptor.
|
2013-02-19 08:35:51 +00:00
|
|
|
*
|
|
|
|
* Returns: (transfer full): a GstMemory based on @allocator.
|
2013-02-18 14:18:38 +00:00
|
|
|
* When the buffer will be released dmabuf allocator will close the @fd.
|
2013-02-19 08:35:51 +00:00
|
|
|
* The memory is only mmapped on gst_buffer_mmap() request.
|
2013-02-19 09:40:31 +00:00
|
|
|
*
|
|
|
|
* Since: 1.2
|
2013-02-18 14:18:38 +00:00
|
|
|
*/
|
|
|
|
GstMemory *
|
|
|
|
gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
|
|
|
|
{
|
2013-09-19 14:33:29 +00:00
|
|
|
#ifdef HAVE_MMAP
|
2013-02-18 14:18:38 +00:00
|
|
|
GstDmaBufMemory *mem;
|
|
|
|
|
|
|
|
if (!GST_IS_DMABUF_ALLOCATOR (allocator)) {
|
|
|
|
GST_WARNING ("it isn't the correct allocator for dmabuf");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG ("alloc from allocator %p", allocator);
|
|
|
|
|
2013-05-03 09:12:04 +00:00
|
|
|
mem = g_slice_new0 (GstDmaBufMemory);
|
2013-02-18 14:18:38 +00:00
|
|
|
|
2013-05-16 07:07:46 +00:00
|
|
|
gst_memory_init (GST_MEMORY_CAST (mem), 0, allocator, NULL, size, 0, 0, size);
|
2013-02-18 14:18:38 +00:00
|
|
|
|
|
|
|
mem->fd = fd;
|
|
|
|
g_mutex_init (&mem->lock);
|
|
|
|
|
2013-03-10 17:05:28 +00:00
|
|
|
GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
|
|
|
|
mem->mem.maxsize);
|
2013-02-19 08:35:51 +00:00
|
|
|
|
2013-02-18 14:18:38 +00:00
|
|
|
return (GstMemory *) mem;
|
2013-09-19 14:33:29 +00:00
|
|
|
#else /* !HAVE_MMAP */
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-19 08:35:51 +00:00
|
|
|
* gst_dmabuf_memory_get_fd:
|
2013-02-18 14:18:38 +00:00
|
|
|
* @mem: the memory to get the file descriptor
|
2013-02-19 08:35:51 +00:00
|
|
|
*
|
2013-02-19 09:40:31 +00:00
|
|
|
* Return the file descriptor associated with @mem.
|
2013-02-19 08:35:51 +00:00
|
|
|
*
|
2013-02-19 09:40:31 +00:00
|
|
|
* Returns: the file descriptor associated with the memory, or -1
|
|
|
|
*
|
|
|
|
* Since: 1.2
|
2013-02-18 14:18:38 +00:00
|
|
|
*/
|
|
|
|
gint
|
|
|
|
gst_dmabuf_memory_get_fd (GstMemory * mem)
|
|
|
|
{
|
|
|
|
GstDmaBufMemory *dbmem = (GstDmaBufMemory *) mem;
|
|
|
|
|
|
|
|
g_return_val_if_fail (gst_is_dmabuf_memory (mem), -1);
|
|
|
|
|
|
|
|
return dbmem->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-19 08:35:51 +00:00
|
|
|
* gst_is_dmabuf_memory:
|
2013-02-18 14:18:38 +00:00
|
|
|
* @mem: the memory to be check
|
2013-02-19 08:35:51 +00:00
|
|
|
*
|
|
|
|
* Check if @mem is dmabuf memory.
|
|
|
|
*
|
2013-02-19 09:40:31 +00:00
|
|
|
* Returns: %TRUE if @mem is dmabuf memory, otherwise %FALSE
|
|
|
|
*
|
|
|
|
* Since: 1.2
|
2013-02-18 14:18:38 +00:00
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_is_dmabuf_memory (GstMemory * mem)
|
|
|
|
{
|
2013-07-15 13:23:17 +00:00
|
|
|
return gst_memory_is_type (mem, GST_ALLOCATOR_DMABUF);
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|