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
|
|
|
|
|
2015-03-15 14:16:23 +00:00
|
|
|
#include "gstfdmemory.h"
|
2013-02-18 14:18:38 +00:00
|
|
|
#include "gstdmabuf.h"
|
|
|
|
|
2013-02-19 09:05:17 +00:00
|
|
|
/**
|
|
|
|
* SECTION:gstdmabuf
|
2017-01-23 19:36:11 +00:00
|
|
|
* @title: GstDmaBufAllocator
|
2013-02-19 09:05:17 +00:00
|
|
|
* @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
|
|
|
*/
|
|
|
|
|
2018-03-10 02:24:52 +00:00
|
|
|
#ifdef HAVE_LINUX_DMA_BUF_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/dma-buf.h>
|
2013-09-19 14:33:29 +00:00
|
|
|
#endif
|
2013-02-18 14:18:38 +00:00
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (dmabuf_debug);
|
|
|
|
#define GST_CAT_DEFAULT dmabuf_debug
|
|
|
|
|
2016-10-24 15:00:07 +00:00
|
|
|
G_DEFINE_TYPE (GstDmaBufAllocator, gst_dmabuf_allocator, GST_TYPE_FD_ALLOCATOR);
|
2013-02-18 14:18:38 +00:00
|
|
|
|
2018-03-10 02:24:52 +00:00
|
|
|
static gpointer
|
|
|
|
gst_dmabuf_mem_map (GstMemory * gmem, GstMapInfo * info, gsize maxsize)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator = gmem->allocator;
|
2018-03-21 22:15:49 +00:00
|
|
|
gpointer ret;
|
|
|
|
|
2018-03-10 02:24:52 +00:00
|
|
|
#ifdef HAVE_LINUX_DMA_BUF_H
|
|
|
|
struct dma_buf_sync sync = { DMA_BUF_SYNC_START };
|
|
|
|
|
|
|
|
if (info->flags & GST_MAP_READ)
|
|
|
|
sync.flags |= DMA_BUF_SYNC_READ;
|
|
|
|
|
|
|
|
if (info->flags & GST_MAP_WRITE)
|
|
|
|
sync.flags |= DMA_BUF_SYNC_WRITE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = allocator->mem_map (gmem, maxsize, info->flags);
|
|
|
|
|
|
|
|
#ifdef HAVE_LINUX_DMA_BUF_H
|
|
|
|
if (ret) {
|
|
|
|
if (ioctl (gst_fd_memory_get_fd (gmem), DMA_BUF_IOCTL_SYNC, &sync) < 0)
|
|
|
|
GST_WARNING_OBJECT (allocator, "Failed to synchronize DMABuf: %s (%i)",
|
|
|
|
g_strerror (errno), errno);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_dmabuf_mem_unmap (GstMemory * gmem, GstMapInfo * info)
|
|
|
|
{
|
|
|
|
GstAllocator *allocator = gmem->allocator;
|
|
|
|
#ifdef HAVE_LINUX_DMA_BUF_H
|
|
|
|
struct dma_buf_sync sync = { DMA_BUF_SYNC_END };
|
|
|
|
|
|
|
|
if (info->flags & GST_MAP_READ)
|
|
|
|
sync.flags |= DMA_BUF_SYNC_READ;
|
|
|
|
|
|
|
|
if (info->flags & GST_MAP_WRITE)
|
|
|
|
sync.flags |= DMA_BUF_SYNC_WRITE;
|
|
|
|
|
|
|
|
if (ioctl (gst_fd_memory_get_fd (gmem), DMA_BUF_IOCTL_SYNC, &sync) < 0)
|
|
|
|
GST_WARNING_OBJECT (allocator, "Failed to synchronize DMABuf: %s (%i)",
|
|
|
|
g_strerror (errno), errno);
|
|
|
|
#else
|
|
|
|
GST_WARNING_OBJECT (allocator, "Using DMABuf without synchronization.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
allocator->mem_unmap (gmem);
|
|
|
|
}
|
|
|
|
|
2013-02-18 14:18:38 +00:00
|
|
|
static void
|
2016-10-24 15:00:07 +00:00
|
|
|
gst_dmabuf_allocator_class_init (GstDmaBufAllocatorClass * klass)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-10-24 15:00:07 +00:00
|
|
|
gst_dmabuf_allocator_init (GstDmaBufAllocator * allocator)
|
2013-02-18 14:18:38 +00:00
|
|
|
{
|
|
|
|
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
|
|
|
|
|
2015-03-18 14:12:03 +00:00
|
|
|
alloc->mem_type = GST_ALLOCATOR_DMABUF;
|
2018-03-10 02:24:52 +00:00
|
|
|
alloc->mem_map_full = gst_dmabuf_mem_map;
|
|
|
|
alloc->mem_unmap_full = gst_dmabuf_mem_unmap;
|
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
|
|
|
*
|
2017-05-15 16:47:22 +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
|
|
|
{
|
2017-05-15 16:47:22 +00:00
|
|
|
GstAllocator *alloc;
|
|
|
|
|
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
|
|
|
|
2017-05-15 16:47:22 +00:00
|
|
|
alloc = g_object_new (GST_TYPE_DMABUF_ALLOCATOR, NULL);
|
|
|
|
gst_object_ref_sink (alloc);
|
|
|
|
|
|
|
|
return alloc;
|
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:
|
2015-09-10 13:08:35 +00:00
|
|
|
* @allocator: 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.
|
2018-04-02 06:34:58 +00:00
|
|
|
* The memory is only mmapped on gst_buffer_map() 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)
|
|
|
|
{
|
2015-04-17 07:31:40 +00:00
|
|
|
g_return_val_if_fail (GST_IS_DMABUF_ALLOCATOR (allocator), NULL);
|
2015-03-18 14:12:03 +00:00
|
|
|
|
2015-04-17 07:31:40 +00:00
|
|
|
return gst_fd_allocator_alloc (allocator, fd, size, GST_FD_MEMORY_FLAG_NONE);
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 06:41:15 +00:00
|
|
|
/**
|
|
|
|
* gst_dmabuf_allocator_alloc_with_flags:
|
|
|
|
* @allocator: allocator to be used for this memory
|
|
|
|
* @fd: dmabuf file descriptor
|
|
|
|
* @size: memory size
|
|
|
|
* @flags: extra #GstFdMemoryFlags
|
|
|
|
*
|
|
|
|
* Return a %GstMemory that wraps a dmabuf file descriptor.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): a GstMemory based on @allocator.
|
|
|
|
*
|
|
|
|
* When the buffer will be released the allocator will close the @fd unless
|
|
|
|
* the %GST_FD_MEMORY_FLAG_DONT_CLOSE flag is specified.
|
|
|
|
* The memory is only mmapped on gst_buffer_mmap() request.
|
|
|
|
*
|
|
|
|
* Since: 1.16
|
|
|
|
*/
|
|
|
|
GstMemory *
|
|
|
|
gst_dmabuf_allocator_alloc_with_flags (GstAllocator * allocator, gint fd,
|
|
|
|
gsize size, GstFdMemoryFlags flags)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GST_IS_DMABUF_ALLOCATOR (allocator), NULL);
|
|
|
|
|
|
|
|
return gst_fd_allocator_alloc (allocator, fd, size, flags);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2014-10-22 12:37:40 +00:00
|
|
|
* Returns: the file descriptor associated with the memory, or -1. The file
|
|
|
|
* descriptor is still owned by the GstMemory. Use dup to take a copy
|
|
|
|
* if you intend to use it beyond the lifetime of this GstMemory.
|
2013-02-19 09:40:31 +00:00
|
|
|
*
|
|
|
|
* Since: 1.2
|
2013-02-18 14:18:38 +00:00
|
|
|
*/
|
|
|
|
gint
|
|
|
|
gst_dmabuf_memory_get_fd (GstMemory * mem)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (gst_is_dmabuf_memory (mem), -1);
|
|
|
|
|
2015-03-18 14:12:03 +00:00
|
|
|
return gst_fd_memory_get_fd (mem);
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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)
|
|
|
|
{
|
2016-10-24 15:00:07 +00:00
|
|
|
g_return_val_if_fail (mem != NULL, FALSE);
|
|
|
|
|
|
|
|
return GST_IS_DMABUF_ALLOCATOR (mem->allocator);
|
2013-02-18 14:18:38 +00:00
|
|
|
}
|