Blame gst-libs/gst/allocators/gstdmabuf.c

Packit 971217
/* GStreamer dmabuf allocator
Packit 971217
 * Copyright (C) 2013 Linaro SA
Packit 971217
 * Author: Benjamin Gaignard <benjamin.gaignard@linaro.org> for Linaro.
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for mordetails.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 971217
 * Boston, MA 02111-1307, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include "gstfdmemory.h"
Packit 971217
#include "gstdmabuf.h"
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:gstdmabuf
Packit 971217
 * @title: GstDmaBufAllocator
Packit 971217
 * @short_description: Memory wrapper for Linux dmabuf memory
Packit 971217
 * @see_also: #GstMemory
Packit 971217
 *
Packit 971217
 * Since: 1.2
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_MMAP
Packit 971217
#include <sys/mman.h>
Packit 971217
#include <unistd.h>
Packit 971217
#endif
Packit 971217
Packit 971217
GST_DEBUG_CATEGORY_STATIC (dmabuf_debug);
Packit 971217
#define GST_CAT_DEFAULT dmabuf_debug
Packit 971217
Packit 971217
G_DEFINE_TYPE (GstDmaBufAllocator, gst_dmabuf_allocator, GST_TYPE_FD_ALLOCATOR);
Packit 971217
Packit 971217
static void
Packit 971217
gst_dmabuf_allocator_class_init (GstDmaBufAllocatorClass * klass)
Packit 971217
{
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_dmabuf_allocator_init (GstDmaBufAllocator * allocator)
Packit 971217
{
Packit 971217
  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
Packit 971217
Packit 971217
  alloc->mem_type = GST_ALLOCATOR_DMABUF;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_dmabuf_allocator_new:
Packit 971217
 *
Packit 971217
 * Return a new dmabuf allocator.
Packit 971217
 *
Packit 971217
 * Returns: (transfer full): a new dmabuf allocator, or NULL if the allocator
Packit 971217
 *    isn't available. Use gst_object_unref() to release the allocator after
Packit 971217
 *    usage
Packit 971217
 *
Packit 971217
 * Since: 1.2
Packit 971217
 */
Packit 971217
GstAllocator *
Packit 971217
gst_dmabuf_allocator_new (void)
Packit 971217
{
Packit 971217
  GstAllocator *alloc;
Packit 971217
Packit 971217
  GST_DEBUG_CATEGORY_INIT (dmabuf_debug, "dmabuf", 0, "dmabuf memory");
Packit 971217
Packit 971217
  alloc = g_object_new (GST_TYPE_DMABUF_ALLOCATOR, NULL);
Packit 971217
  gst_object_ref_sink (alloc);
Packit 971217
Packit 971217
  return alloc;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_dmabuf_allocator_alloc:
Packit 971217
 * @allocator: allocator to be used for this memory
Packit 971217
 * @fd: dmabuf file descriptor
Packit 971217
 * @size: memory size
Packit 971217
 *
Packit 971217
 * Return a %GstMemory that wraps a dmabuf file descriptor.
Packit 971217
 *
Packit 971217
 * Returns: (transfer full): a GstMemory based on @allocator.
Packit 971217
 * When the buffer will be released dmabuf allocator will close the @fd.
Packit 971217
 * The memory is only mmapped on gst_buffer_mmap() request.
Packit 971217
 *
Packit 971217
 * Since: 1.2
Packit 971217
 */
Packit 971217
GstMemory *
Packit 971217
gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (GST_IS_DMABUF_ALLOCATOR (allocator), NULL);
Packit 971217
Packit 971217
  return gst_fd_allocator_alloc (allocator, fd, size, GST_FD_MEMORY_FLAG_NONE);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_dmabuf_memory_get_fd:
Packit 971217
 * @mem: the memory to get the file descriptor
Packit 971217
 *
Packit 971217
 * Return the file descriptor associated with @mem.
Packit 971217
 *
Packit 971217
 * Returns: the file descriptor associated with the memory, or -1.  The file
Packit 971217
 *     descriptor is still owned by the GstMemory.  Use dup to take a copy
Packit 971217
 *     if you intend to use it beyond the lifetime of this GstMemory.
Packit 971217
 *
Packit 971217
 * Since: 1.2
Packit 971217
 */
Packit 971217
gint
Packit 971217
gst_dmabuf_memory_get_fd (GstMemory * mem)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (gst_is_dmabuf_memory (mem), -1);
Packit 971217
Packit 971217
  return gst_fd_memory_get_fd (mem);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_is_dmabuf_memory:
Packit 971217
 * @mem: the memory to be check
Packit 971217
 *
Packit 971217
 * Check if @mem is dmabuf memory.
Packit 971217
 *
Packit 971217
 * Returns: %TRUE if @mem is dmabuf memory, otherwise %FALSE
Packit 971217
 *
Packit 971217
 * Since: 1.2
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_is_dmabuf_memory (GstMemory * mem)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (mem != NULL, FALSE);
Packit 971217
Packit 971217
  return GST_IS_DMABUF_ALLOCATOR (mem->allocator);
Packit 971217
}