1 /* GStreamer dmabuf allocator
2 * Copyright (C) 2013 Linaro SA
3 * Author: Benjamin Gaignard <benjamin.gaignard@linaro.org> for Linaro.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for mordetails.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gstdmabuf.h"
29 * @short_description: Memory wrapper for Linux dmabuf memory
30 * @see_also: #GstMemory
41 * @fd: the file descriptor associated this memory
42 * @data: mmapped address
43 * @mmapping_flags: mmapping flags
44 * @mmap_count: mmapping counter
45 * @lock: a mutex to make mmapping thread safe
58 #define ALLOCATOR_NAME "dmabuf"
60 GST_DEBUG_CATEGORY_STATIC (dmabuf_debug);
61 #define GST_CAT_DEFAULT dmabuf_debug
64 gst_dmabuf_alloc (GstAllocator * allocator, gsize size,
65 GstAllocationParams * params)
67 g_warning ("Use dmabuf_mem_alloc() to allocate from this allocator");
73 gst_dmabuf_free (GstAllocator * allocator, GstMemory * mem)
75 GstDmaBufMemory *dbmem = (GstDmaBufMemory *) mem;
78 g_warning ("Freeing memory still mapped");
81 g_mutex_clear (&dbmem->lock);
82 g_slice_free (GstDmaBufMemory, dbmem);
83 GST_DEBUG ("%p: freed", dbmem);
87 gst_dmabuf_mem_map (GstDmaBufMemory * mem, gsize maxsize, GstMapFlags flags)
92 g_mutex_lock (&mem->lock);
94 prot = flags & GST_MAP_READ ? PROT_READ : 0;
95 prot |= flags & GST_MAP_WRITE ? PROT_WRITE : 0;
97 /* do not mmap twice the buffer */
99 /* only return address if mapping flags are a subset
100 * of the previous flags */
101 if (mem->mmapping_flags & prot)
108 mem->data = mmap (0, maxsize, prot, MAP_SHARED, mem->fd, 0);
110 GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
113 mem->mmapping_flags = prot;
114 mem->mem.size = maxsize;
120 g_mutex_unlock (&mem->lock);
125 gst_dmabuf_mem_unmap (GstDmaBufMemory * mem)
127 g_mutex_lock (&mem->lock);
129 if (mem->data && !(--mem->mmap_count)) {
130 munmap ((void *) mem->data, mem->mem.size);
133 mem->mmapping_flags = 0;
134 GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
136 g_mutex_unlock (&mem->lock);
140 static GstDmaBufMemory *
141 gst_dmabuf_mem_share (GstDmaBufMemory * mem, gssize offset, gsize size)
143 GstDmaBufMemory *sub;
146 GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
149 /* find the real parent */
150 if ((parent = mem->mem.parent) == NULL)
151 parent = (GstMemory *) mem;
154 size = mem->mem.size - offset;
156 sub = g_slice_new (GstDmaBufMemory);
157 /* the shared memory is always readonly */
158 gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
159 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
160 mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
165 static GstDmaBufMemory *
166 gst_dmabuf_mem_copy (GstDmaBufMemory * mem, gssize offset, gsize size)
168 gint newfd = dup (mem->fd);
171 GST_WARNING ("Can't duplicate dmabuf file descriptor");
175 GST_DEBUG ("%p: copy %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
177 return (GstDmaBufMemory *) gst_dmabuf_allocator_alloc (mem->mem.allocator,
184 } dmabuf_mem_Allocator;
188 GstAllocatorClass parent_class;
189 } dmabuf_mem_AllocatorClass;
191 GType dmabuf_mem_allocator_get_type (void);
192 G_DEFINE_TYPE (dmabuf_mem_Allocator, dmabuf_mem_allocator, GST_TYPE_ALLOCATOR);
194 #define GST_TYPE_DMABUF_ALLOCATOR (dmabuf_mem_allocator_get_type())
195 #define GST_IS_DMABUF_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_DMABUF_ALLOCATOR))
198 dmabuf_mem_allocator_class_init (dmabuf_mem_AllocatorClass * klass)
200 GstAllocatorClass *allocator_class;
202 allocator_class = (GstAllocatorClass *) klass;
204 allocator_class->alloc = gst_dmabuf_alloc;
205 allocator_class->free = gst_dmabuf_free;
209 dmabuf_mem_allocator_init (dmabuf_mem_Allocator * allocator)
211 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
213 alloc->mem_type = ALLOCATOR_NAME;
214 alloc->mem_map = (GstMemoryMapFunction) gst_dmabuf_mem_map;
215 alloc->mem_unmap = (GstMemoryUnmapFunction) gst_dmabuf_mem_unmap;
216 alloc->mem_share = (GstMemoryShareFunction) gst_dmabuf_mem_share;
217 alloc->mem_copy = (GstMemoryCopyFunction) gst_dmabuf_mem_copy;
219 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
223 gst_dmabuf_mem_init (void)
225 GstAllocator *allocator =
226 g_object_new (dmabuf_mem_allocator_get_type (), NULL);
227 gst_allocator_register (ALLOCATOR_NAME, allocator);
229 GST_DEBUG_CATEGORY_INIT (dmabuf_debug, "dmabuf", 0, "dmabuf memory");
233 * gst_dmabuf_allocator_obtain:
235 * Return a dmabuf allocator.
237 * Returns: (transfer full): a dmabuf allocator, or NULL if the allocator
238 * isn't available. Use gst_object_unref() to release the allocator after
244 gst_dmabuf_allocator_obtain (void)
246 static GOnce dmabuf_allocator_once = G_ONCE_INIT;
247 GstAllocator *allocator;
249 g_once (&dmabuf_allocator_once, (GThreadFunc) gst_dmabuf_mem_init, NULL);
251 allocator = gst_allocator_find (ALLOCATOR_NAME);
253 GST_WARNING ("No allocator named %s found", ALLOCATOR_NAME);
258 * gst_dmabuf_allocator_alloc:
259 * @allocator: (allow-none): allocator to be used for this memory
260 * @fd: dmabuf file descriptor
263 * Return a %GstMemory that wraps a dmabuf file descriptor.
265 * Returns: (transfer full): a GstMemory based on @allocator.
266 * When the buffer will be released dmabuf allocator will close the @fd.
267 * The memory is only mmapped on gst_buffer_mmap() request.
272 gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
274 GstDmaBufMemory *mem;
277 allocator = gst_dmabuf_allocator_obtain ();
280 if (!GST_IS_DMABUF_ALLOCATOR (allocator)) {
281 GST_WARNING ("it isn't the correct allocator for dmabuf");
285 GST_DEBUG ("alloc from allocator %p", allocator);
287 mem = g_slice_new (GstDmaBufMemory);
289 gst_memory_init (GST_MEMORY_CAST (mem), 0, allocator, NULL, size, 0, 0, 0);
293 mem->mmapping_flags = 0;
295 g_mutex_init (&mem->lock);
297 GST_DEBUG ("%p: fd: %d size %d", mem, mem->fd, mem->mem.maxsize);
299 return (GstMemory *) mem;
303 * gst_dmabuf_memory_get_fd:
304 * @mem: the memory to get the file descriptor
306 * Return the file descriptor associated with @mem.
308 * Returns: the file descriptor associated with the memory, or -1
313 gst_dmabuf_memory_get_fd (GstMemory * mem)
315 GstDmaBufMemory *dbmem = (GstDmaBufMemory *) mem;
317 g_return_val_if_fail (gst_is_dmabuf_memory (mem), -1);
323 * gst_is_dmabuf_memory:
324 * @mem: the memory to be check
326 * Check if @mem is dmabuf memory.
328 * Returns: %TRUE if @mem is dmabuf memory, otherwise %FALSE
333 gst_is_dmabuf_memory (GstMemory * mem)
335 g_return_val_if_fail (mem != NULL, FALSE);
337 return g_strcmp0 (mem->allocator->mem_type, ALLOCATOR_NAME) == 0;
340 #else /* !HAVE_MMAP */
343 gst_dmabuf_allocator_obtain (void)
349 gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
355 gst_dmabuf_memory_get_fd (GstMemory * mem)
361 gst_is_dmabuf_memory (GstMemory * mem)
366 #endif /* HAVE_MMAP */