dmabuf: The dmabuf allocator has a custom alloc function, mark it as such
[platform/upstream/gstreamer.git] / gst-libs / gst / allocators / gstdmabuf.c
1 /* GStreamer dmabuf allocator
2  * Copyright (C) 2013 Linaro SA
3  * Author: Benjamin Gaignard <benjamin.gaignard@linaro.org> for Linaro.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstdmabuf.h"
26
27 /**
28  * SECTION:gstdmabuf
29  * @short_description: Memory wrapper for Linux dmabuf memory
30  * @see_also: #GstMemory
31  *
32  * Since: 1.2
33  */
34
35 #ifdef HAVE_MMAP
36 #include <sys/mman.h>
37 #include <unistd.h>
38
39 /*
40  * GstDmaBufMemory
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
46  */
47 typedef struct
48 {
49   GstMemory mem;
50
51   gint fd;
52   gpointer data;
53   gint mmapping_flags;
54   gint mmap_count;
55   GMutex lock;
56 } GstDmaBufMemory;
57
58 #define ALLOCATOR_NAME "dmabuf"
59
60 GST_DEBUG_CATEGORY_STATIC (dmabuf_debug);
61 #define GST_CAT_DEFAULT dmabuf_debug
62
63 static GstMemory *
64 gst_dmabuf_alloc (GstAllocator * allocator, gsize size,
65     GstAllocationParams * params)
66 {
67   g_warning ("Use dmabuf_mem_alloc() to allocate from this allocator");
68
69   return NULL;
70 }
71
72 static void
73 gst_dmabuf_free (GstAllocator * allocator, GstMemory * mem)
74 {
75   GstDmaBufMemory *dbmem = (GstDmaBufMemory *) mem;
76
77   if (dbmem->data)
78     g_warning ("Freeing memory still mapped");
79
80   close (dbmem->fd);
81   g_mutex_clear (&dbmem->lock);
82   g_slice_free (GstDmaBufMemory, dbmem);
83   GST_DEBUG ("%p: freed", dbmem);
84 }
85
86 static gpointer
87 gst_dmabuf_mem_map (GstDmaBufMemory * mem, gsize maxsize, GstMapFlags flags)
88 {
89   gint prot;
90   gpointer ret = NULL;
91
92   g_mutex_lock (&mem->lock);
93
94   prot = flags & GST_MAP_READ ? PROT_READ : 0;
95   prot |= flags & GST_MAP_WRITE ? PROT_WRITE : 0;
96
97   /* do not mmap twice the buffer */
98   if (mem->data) {
99     /* only return address if mapping flags are a subset
100      * of the previous flags */
101     if (mem->mmapping_flags & prot)
102       ret = mem->data;
103
104     goto out;
105   }
106
107   if (mem->fd != -1)
108     mem->data = mmap (0, maxsize, prot, MAP_SHARED, mem->fd, 0);
109
110   GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
111
112   if (mem->data) {
113     mem->mmapping_flags = prot;
114     mem->mem.size = maxsize;
115     mem->mmap_count++;
116     ret = mem->data;
117   }
118
119 out:
120   g_mutex_unlock (&mem->lock);
121   return ret;
122 }
123
124 static gboolean
125 gst_dmabuf_mem_unmap (GstDmaBufMemory * mem)
126 {
127   g_mutex_lock (&mem->lock);
128
129   if (mem->data && !(--mem->mmap_count)) {
130     munmap ((void *) mem->data, mem->mem.size);
131     mem->data = NULL;
132     mem->mem.size = 0;
133     mem->mmapping_flags = 0;
134     GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
135   }
136   g_mutex_unlock (&mem->lock);
137   return TRUE;
138 }
139
140 static GstDmaBufMemory *
141 gst_dmabuf_mem_share (GstDmaBufMemory * mem, gssize offset, gsize size)
142 {
143   GstDmaBufMemory *sub;
144   GstMemory *parent;
145
146   GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
147       size);
148
149   /* find the real parent */
150   if ((parent = mem->mem.parent) == NULL)
151     parent = (GstMemory *) mem;
152
153   if (size == -1)
154     size = mem->mem.size - offset;
155
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);
161
162   return sub;
163 }
164
165 static GstDmaBufMemory *
166 gst_dmabuf_mem_copy (GstDmaBufMemory * mem, gssize offset, gsize size)
167 {
168   gint newfd = dup (mem->fd);
169
170   if (newfd == -1) {
171     GST_WARNING ("Can't duplicate dmabuf file descriptor");
172     return NULL;
173   }
174
175   GST_DEBUG ("%p: copy %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
176       size);
177   return (GstDmaBufMemory *) gst_dmabuf_allocator_alloc (mem->mem.allocator,
178       newfd, size);
179 }
180
181 typedef struct
182 {
183   GstAllocator parent;
184 } dmabuf_mem_Allocator;
185
186 typedef struct
187 {
188   GstAllocatorClass parent_class;
189 } dmabuf_mem_AllocatorClass;
190
191 GType dmabuf_mem_allocator_get_type (void);
192 G_DEFINE_TYPE (dmabuf_mem_Allocator, dmabuf_mem_allocator, GST_TYPE_ALLOCATOR);
193
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))
196
197 static void
198 dmabuf_mem_allocator_class_init (dmabuf_mem_AllocatorClass * klass)
199 {
200   GstAllocatorClass *allocator_class;
201
202   allocator_class = (GstAllocatorClass *) klass;
203
204   allocator_class->alloc = gst_dmabuf_alloc;
205   allocator_class->free = gst_dmabuf_free;
206 }
207
208 static void
209 dmabuf_mem_allocator_init (dmabuf_mem_Allocator * allocator)
210 {
211   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
212
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;
218
219   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
220 }
221
222 static void
223 gst_dmabuf_mem_init (void)
224 {
225   GstAllocator *allocator =
226       g_object_new (dmabuf_mem_allocator_get_type (), NULL);
227   gst_allocator_register (ALLOCATOR_NAME, allocator);
228
229   GST_DEBUG_CATEGORY_INIT (dmabuf_debug, "dmabuf", 0, "dmabuf memory");
230 }
231
232 /**
233  * gst_dmabuf_allocator_obtain:
234  *
235  * Return a dmabuf allocator.
236  *
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
239  *    usage
240  *
241  * Since: 1.2
242  */
243 GstAllocator *
244 gst_dmabuf_allocator_obtain (void)
245 {
246   static GOnce dmabuf_allocator_once = G_ONCE_INIT;
247   GstAllocator *allocator;
248
249   g_once (&dmabuf_allocator_once, (GThreadFunc) gst_dmabuf_mem_init, NULL);
250
251   allocator = gst_allocator_find (ALLOCATOR_NAME);
252   if (!allocator)
253     GST_WARNING ("No allocator named %s found", ALLOCATOR_NAME);
254   return allocator;
255 }
256
257 /**
258  * gst_dmabuf_allocator_alloc:
259  * @allocator: (allow-none): allocator to be used for this memory
260  * @fd: dmabuf file descriptor
261  * @size: memory size
262  *
263  * Return a %GstMemory that wraps a dmabuf file descriptor.
264  *
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.
268  *
269  * Since: 1.2
270  */
271 GstMemory *
272 gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
273 {
274   GstDmaBufMemory *mem;
275
276   if (!allocator) {
277     allocator = gst_dmabuf_allocator_obtain ();
278   }
279
280   if (!GST_IS_DMABUF_ALLOCATOR (allocator)) {
281     GST_WARNING ("it isn't the correct allocator for dmabuf");
282     return NULL;
283   }
284
285   GST_DEBUG ("alloc from allocator %p", allocator);
286
287   mem = g_slice_new (GstDmaBufMemory);
288
289   gst_memory_init (GST_MEMORY_CAST (mem), 0, allocator, NULL, size, 0, 0, 0);
290
291   mem->fd = fd;
292   mem->data = NULL;
293   mem->mmapping_flags = 0;
294   mem->mmap_count = 0;
295   g_mutex_init (&mem->lock);
296
297   GST_DEBUG ("%p: fd: %d size %d", mem, mem->fd, mem->mem.maxsize);
298
299   return (GstMemory *) mem;
300 }
301
302 /**
303  * gst_dmabuf_memory_get_fd:
304  * @mem: the memory to get the file descriptor
305  *
306  * Return the file descriptor associated with @mem.
307  *
308  * Returns: the file descriptor associated with the memory, or -1
309  *
310  * Since: 1.2
311  */
312 gint
313 gst_dmabuf_memory_get_fd (GstMemory * mem)
314 {
315   GstDmaBufMemory *dbmem = (GstDmaBufMemory *) mem;
316
317   g_return_val_if_fail (gst_is_dmabuf_memory (mem), -1);
318
319   return dbmem->fd;
320 }
321
322 /**
323  * gst_is_dmabuf_memory:
324  * @mem: the memory to be check
325  *
326  * Check if @mem is dmabuf memory.
327  *
328  * Returns: %TRUE if @mem is dmabuf memory, otherwise %FALSE
329  *
330  * Since: 1.2
331  */
332 gboolean
333 gst_is_dmabuf_memory (GstMemory * mem)
334 {
335   g_return_val_if_fail (mem != NULL, FALSE);
336
337   return g_strcmp0 (mem->allocator->mem_type, ALLOCATOR_NAME) == 0;
338 }
339
340 #else /* !HAVE_MMAP */
341
342 GstAllocator *
343 gst_dmabuf_allocator_obtain (void)
344 {
345   return NULL;
346 }
347
348 GstMemory *
349 gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
350 {
351   return NULL;
352 }
353
354 gint
355 gst_dmabuf_memory_get_fd (GstMemory * mem)
356 {
357   return -1;
358 }
359
360 gboolean
361 gst_is_dmabuf_memory (GstMemory * mem)
362 {
363   return FALSE;
364 }
365
366 #endif /* HAVE_MMAP */