--- /dev/null
+lib_LTLIBRARIES = libgstallocators-@GST_API_VERSION@.la
+
+libgstallocators_@GST_API_VERSION@_includedir = $(includedir)/gstreamer-@GST_API_VERSION@/gst/allocators
+
+libgstallocators_@GST_API_VERSION@_include_HEADERS = \
+ gstdmabuf.h
+
+noinst_HEADERS =
+
+libgstallocators_@GST_API_VERSION@_la_SOURCES = \
+ gstdmabuf.c
+
+libgstallocators_@GST_API_VERSION@_la_LIBADD = $(GST_LIBS) $(LIBM)
+libgstallocators_@GST_API_VERSION@_la_CFLAGS = $(GST_CFLAGS)
+libgstallocators_@GST_API_VERSION@_la_LDFLAGS = $(GST_LIB_LDFLAGS) $(GST_ALL_LDFLAGS) $(GST_LT_LDFLAGS)
+
+if HAVE_INTROSPECTION
+BUILT_GIRSOURCES = GstAllocators-@GST_API_VERSION@.gir
+
+gir_headers=$(patsubst %,$(srcdir)/%, $(libgstallocators_@GST_API_VERSION@_include_HEADERS))
+gir_sources=$(patsubst %,$(srcdir)/%, $(libgstallocators_@GST_API_VERSION@_la_SOURCES))
+gir_cincludes=$(patsubst %,--c-include='gst/allocators/%',$(libgstallocators_@GST_API_VERSION@_include_HEADERS))
+
+GstAllocators-@GST_API_VERSION@.gir: $(INTROSPECTION_SCANNER) libgstallocators-@GST_API_VERSION@.la
+ $(AM_V_GEN)PKG_CONFIG_PATH="$(GST_PKG_CONFIG_PATH)" \
+ GST_PLUGIN_SYSTEM_PATH="" GST_PLUGIN_PATH="" GST_REGISTRY_UPDATE=no \
+ $(INTROSPECTION_SCANNER) -v --namespace GstAllocators \
+ --nsversion=@GST_API_VERSION@ \
+ --strip-prefix=Gst \
+ --warn-all \
+ $(gir_cincludes) \
+ --add-include-path=`PKG_CONFIG_PATH="$(GST_PKG_CONFIG_PATH)" $(PKG_CONFIG) --variable=girdir gstreamer-@GST_API_VERSION@` \
+ --library=libgstallocators-@GST_API_VERSION@.la \
+ --include=Gst-@GST_API_VERSION@ \
+ --libtool="$(top_builddir)/libtool" \
+ --pkg gstreamer-@GST_API_VERSION@ \
+ --pkg-export gstreamer-allocators-@GST_API_VERSION@ \
+ --output $@ \
+ $(gir_headers) \
+ $(gir_sources)
+
+# INTROSPECTION_GIRDIR/INTROSPECTION_TYPELIBDIR aren't the right place to
+# install anything - we need to install inside our prefix.
+girdir = $(datadir)/gir-1.0
+gir_DATA = $(BUILT_GIRSOURCES)
+
+typelibsdir = $(libdir)/girepository-1.0/
+
+typelibs_DATA = $(BUILT_GIRSOURCES:.gir=.typelib)
+
+%.typelib: %.gir $(INTROSPECTION_COMPILER)
+ $(AM_V_GEN)PKG_CONFIG_PATH="$(GST_PKG_CONFIG_PATH)" \
+ $(INTROSPECTION_COMPILER) \
+ --includedir=$(srcdir) \
+ --includedir=$(builddir) \
+ --includedir=`PKG_CONFIG_PATH="$(GST_PKG_CONFIG_PATH)" $(PKG_CONFIG) --variable=girdir gstreamer-@GST_API_VERSION@` \
+ $(INTROSPECTION_COMPILER_OPTS) $< -o $(@F)
+
+CLEANFILES = $(BUILT_GIRSOURCES) $(typelibs_DATA)
+endif
+
+Android.mk: Makefile.am
+ androgenizer -:PROJECT libgstallocators -:SHARED libgstallocators-@GST_API_VERSION@ \
+ -:TAGS eng debug \
+ -:REL_TOP $(top_srcdir) -:ABS_TOP $(abs_top_srcdir) \
+ -:SOURCES $(libgstallocators_@GST_API_VERSION@_la_SOURCES) \
+ -:CFLAGS $(DEFS) $(libgstallocators_@GST_API_VERSION@_la_CFLAGS) \
+ -:LDFLAGS $(libgstallocators_@GST_API_VERSION@_la_LDFLAGS) \
+ $(libgstallocators_@GST_API_VERSION@_la_LIBADD) \
+ -ldl \
+ -:HEADER_TARGET gstreamer-@GST_API_VERSION@/gst/allocators \
+ -:HEADERS $(libgstallocatorsinclude_HEADERS) \
+ -:PASSTHROUGH LOCAL_ARM_MODE:=arm \
+ > $@
--- /dev/null
+/*
+ * gstdmabuf.c
+ *
+ * Copyright (C) Linaro SA 2013
+ * 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.
+ */
+
+#include "gstdmabuf.h"
+
+#ifdef HAVE_MMAP
+#include <sys/mman.h>
+#include <unistd.h>
+
+/**
+ * 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;
+ GMutex lock;
+} GstDmaBufMemory;
+
+#define ALLOCATOR_NAME "dmabuf"
+
+GST_DEBUG_CATEGORY_STATIC (dmabuf_debug);
+#define GST_CAT_DEFAULT dmabuf_debug
+
+static GstMemory *
+_dmabuf_alloc (GstAllocator * allocator, gsize size,
+ GstAllocationParams * params)
+{
+ g_warning ("Use dmabuf_mem_alloc() to allocate from this allocator");
+
+ return NULL;
+}
+
+static void
+_dmabuf_free (GstAllocator * allocator, GstMemory * mem)
+{
+ GstDmaBufMemory *dbmem = (GstDmaBufMemory *) mem;
+
+ if (dbmem->data)
+ g_warning ("Freeing memory still mapped");
+
+ close (dbmem->fd);
+ g_mutex_clear (&dbmem->lock);
+ g_slice_free (GstDmaBufMemory, dbmem);
+ GST_DEBUG ("%p: freed", dbmem);
+}
+
+static gpointer
+_dmabuf_mem_map (GstDmaBufMemory * mem, gsize maxsize, GstMapFlags flags)
+{
+ 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 */
+ if (mem->mmapping_flags & prot)
+ ret = mem->data;
+
+ goto out;
+ }
+
+ if (mem->fd != -1)
+ mem->data = mmap (0, maxsize, prot, MAP_SHARED, mem->fd, 0);
+
+ GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
+
+ if (mem->data) {
+ mem->mmapping_flags = prot;
+ mem->mem.size = maxsize;
+ mem->mmap_count++;
+ ret = mem->data;
+ }
+
+out:
+ g_mutex_unlock (&mem->lock);
+ return ret;
+}
+
+static gboolean
+_dmabuf_mem_unmap (GstDmaBufMemory * mem)
+{
+ g_mutex_lock (&mem->lock);
+
+ if (mem->data && !(--mem->mmap_count)) {
+ munmap ((void *) mem->data, mem->mem.size);
+ mem->data = NULL;
+ mem->mem.size = 0;
+ mem->mmapping_flags = 0;
+ GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
+ }
+ g_mutex_unlock (&mem->lock);
+ return TRUE;
+}
+
+static GstDmaBufMemory *
+_dmabuf_mem_share (GstDmaBufMemory * mem, gssize offset, gsize size)
+{
+ GstDmaBufMemory *sub;
+ GstMemory *parent;
+ 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)
+ size = mem->mem.size - offset;
+
+ sub = g_slice_new (GstDmaBufMemory);
+ /* 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);
+
+ return sub;
+}
+
+static GstDmaBufMemory *
+_dmabuf_mem_copy (GstDmaBufMemory * mem, gssize offset, gsize size)
+{
+ gint newfd = dup (mem->fd);
+
+ if (newfd == -1) {
+ GST_WARNING ("Can't duplicate dmabuf file descriptor");
+ return NULL;
+ }
+
+ GST_DEBUG ("%p: copy %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
+ size);
+ return (GstDmaBufMemory *) gst_dmabuf_allocator_alloc (mem->mem.allocator,
+ newfd, size);
+}
+
+typedef struct
+{
+ GstAllocator parent;
+} dmabuf_mem_Allocator;
+
+typedef struct
+{
+ GstAllocatorClass parent_class;
+} dmabuf_mem_AllocatorClass;
+
+GType dmabuf_mem_allocator_get_type (void);
+G_DEFINE_TYPE (dmabuf_mem_Allocator, dmabuf_mem_allocator, GST_TYPE_ALLOCATOR);
+
+#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
+dmabuf_mem_allocator_class_init (dmabuf_mem_AllocatorClass * klass)
+{
+ GstAllocatorClass *allocator_class;
+
+ allocator_class = (GstAllocatorClass *) klass;
+
+ allocator_class->alloc = _dmabuf_alloc;
+ allocator_class->free = _dmabuf_free;
+}
+
+static void
+dmabuf_mem_allocator_init (dmabuf_mem_Allocator * allocator)
+{
+ GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
+
+ alloc->mem_type = ALLOCATOR_NAME;
+ alloc->mem_map = (GstMemoryMapFunction) _dmabuf_mem_map;
+ alloc->mem_unmap = (GstMemoryUnmapFunction) _dmabuf_mem_unmap;
+ alloc->mem_share = (GstMemoryShareFunction) _dmabuf_mem_share;
+ alloc->mem_copy = (GstMemoryCopyFunction) _dmabuf_mem_copy;
+}
+
+static void
+_dmabuf_mem_init (void)
+{
+ GstAllocator *allocator =
+ g_object_new (dmabuf_mem_allocator_get_type (), NULL);
+ gst_allocator_register (ALLOCATOR_NAME, allocator);
+
+ GST_DEBUG_CATEGORY_INIT (dmabuf_debug, "dmabuf", 0, "dmabuf memory");
+}
+
+/**
+ * gst_dmabuf_allocator_obtain
+ * return a dmabuf allocator or NULL if the allocator isn't found
+ * Use gst_object_unref() to release the allocator after usage.
+ */
+GstAllocator *
+gst_dmabuf_allocator_obtain (void)
+{
+ static GOnce dmabuf_allocator_once = G_ONCE_INIT;
+ GstAllocator *allocator;
+ g_once (&dmabuf_allocator_once, (GThreadFunc) _dmabuf_mem_init, NULL);
+
+ allocator = gst_allocator_find (ALLOCATOR_NAME);
+ if (!allocator)
+ GST_WARNING ("No allocator named %s found", ALLOCATOR_NAME);
+ return allocator;
+}
+
+/*
+ * gst_dmabuf_allocator_alloc
+ * @allocator: allocator to be used for this memory
+ * @fd: dmabuf file descriptor
+ * @size: memory size
+ * return a GstMemory based on @allocator.
+ * When the buffer will be released dmabuf allocator will close the @fd.
+ * The memory is only mmapped on gst_buffer_mmap request.
+ */
+GstMemory *
+gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
+{
+ GstDmaBufMemory *mem;
+
+ if (!allocator) {
+ allocator = gst_dmabuf_allocator_obtain();
+ }
+
+ 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);
+
+ mem = g_slice_new (GstDmaBufMemory);
+
+ gst_memory_init (GST_MEMORY_CAST (mem), 0, allocator, NULL, size, 0, 0, 0);
+
+ mem->fd = fd;
+ mem->data = NULL;
+ mem->mmapping_flags = 0;
+ mem->mmap_count = 0;
+ g_mutex_init (&mem->lock);
+
+ GST_DEBUG ("%p: fd: %d size %d", mem, mem->fd, mem->mem.maxsize);
+ return (GstMemory *) mem;
+}
+
+/**
+ * gst_dmabuf_memory_get_fd
+ * @mem: the memory to get the file descriptor
+ * return the file descriptor associated with the memory
+ * else return -1
+ */
+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;
+}
+
+/**
+ * gst_is_dmabuf_memory
+ * @mem: the memory to be check
+ * return true is the memory allocator is the dmabuf one
+ */
+gboolean
+gst_is_dmabuf_memory (GstMemory * mem)
+{
+ return g_strcmp0 (mem->allocator->mem_type, ALLOCATOR_NAME) == 0;
+}
+
+#else
+
+GstAllocator * gst_dmabuf_allocator_obtain(void)
+{
+ return NULL;
+}
+
+GstMemory * gst_dmabuf_allocator_alloc(GstAllocator * allocator, gint fd, gsize size)
+{
+ return NULL;
+}
+
+gint gst_dmabuf_memory_get_fd(GstMemory * mem)
+{
+ return -1;
+}
+
+gboolean gst_is_dmabuf_memory(GstMemory * mem)
+{
+ return FALSE;
+}
+
+#endif /* HAVE_MMAP */