2 * Copyright (C) 2019, Collabora Ltd.
3 * Author: George Kiagiadakis <george.kiagiadakis@collabora.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation
8 * version 2.1 of the License.
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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "gstomxallocator.h"
26 #include <gst/allocators/gstdmabuf.h>
28 GST_DEBUG_CATEGORY_STATIC (gst_omx_allocator_debug_category);
29 #define GST_CAT_DEFAULT gst_omx_allocator_debug_category
32 GST_DEBUG_CATEGORY_INIT (gst_omx_allocator_debug_category, "omxallocator", 0, \
33 "debug category for gst-omx allocator class");
35 G_DEFINE_TYPE_WITH_CODE (GstOMXAllocator, gst_omx_allocator, GST_TYPE_ALLOCATOR,
41 SIG_FOREIGN_MEM_RELEASED,
45 static guint signals[LAST_SIGNAL] = { 0 };
47 /* Custom allocator for memory associated with OpenMAX buffers
49 * The main purpose of this allocator is to track memory that is associated
50 * with OpenMAX buffers, so that we know when the buffers can be released
53 * This allocator looks and behaves more like a buffer pool. It allocates
54 * the memory objects before starting and sets a miniobject dispose function
55 * on them, which allows them to return when their last ref count is dropped.
57 * The type of memory that this allocator manages is GstOMXMemory. However, it
58 * is possible to manage a different type of memory, in which case the
59 * GstOMXMemory object is used only internally. There are two supported cases:
60 * - Allocate memory from the dmabuf allocator
61 * - Take memory that was allocated externally and manage it here
63 * In both cases, this allocator will replace the miniobject dispose function
64 * of these memory objects, so if they were acquired from here, they will also
65 * return here on their last unref.
67 * The caller initially needs to configure how many memory objects will be
68 * managed here by calling configure(). After that it needs to call
69 * set_active(TRUE) and finally allocate() for each memory. Allocation is done
70 * like this to facilitate calling allocate() from the alloc() function of
71 * the buffer pool for each OMX buffer on the port.
73 * After the allocator has been activated and all buffers have been allocated,
74 * the acquire() method can be called to retrieve a memory object. acquire() can
75 * be given an OMX buffer index or pointer to locate and return the memory
76 * object that corresponds to this OMX buffer. If the buffer is already
77 * acquired, this will result in a GST_FLOW_ERROR.
79 * When the last reference count is dropped on a memory that was acquired from
80 * here, its dispose function will ref it again and allow it to be acquired
81 * again. In addition, the omxbuf-released signal is fired to let the caller
82 * know that it can return this OMX buffer to the port, as it is no longer
83 * used outside this allocator.
90 #define GST_OMX_MEMORY_TYPE "openmax"
93 gst_omx_memory_quark (void)
95 static GQuark quark = 0;
98 quark = g_quark_from_static_string ("GstOMXMemory");
103 static GstOMXMemory *
104 gst_omx_memory_new (GstOMXAllocator * allocator, GstOMXBuffer * omx_buf,
105 GstMemoryFlags flags, GstMemory * parent, gssize offset, gssize size)
111 /* GStreamer uses a bitmask for the alignment while
112 * OMX uses the alignment itself. So we have to convert
114 align = allocator->port->port_def.nBufferAlignment;
117 if (((align + 1) & align) != 0) {
118 GST_WARNING ("Invalid alignment that is not a power of two: %u",
119 (guint) allocator->port->port_def.nBufferAlignment);
123 maxsize = omx_buf->omx_buf->nAllocLen;
126 size = maxsize - offset;
129 mem = g_slice_new0 (GstOMXMemory);
130 gst_memory_init (GST_MEMORY_CAST (mem), flags, (GstAllocator *) allocator,
131 parent, maxsize, align, offset, size);
139 gst_omx_memory_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
141 GstOMXMemory *omem = (GstOMXMemory *) mem;
143 /* if we are using foreign_mem, the GstOMXMemory should never appear
144 * anywhere outside this allocator, therefore it should never be mapped */
145 g_return_val_if_fail (!omem->foreign_mem, NULL);
147 return omem->buf->omx_buf->pBuffer;
151 gst_omx_memory_unmap (GstMemory * mem)
156 gst_omx_memory_share (GstMemory * mem, gssize offset, gssize size)
158 GstOMXMemory *omem = (GstOMXMemory *) mem;
162 /* find the real parent */
163 if ((parent = mem->parent) == NULL)
167 size = mem->size - offset;
169 /* the shared memory is always readonly */
170 sub = gst_omx_memory_new ((GstOMXAllocator *) mem->allocator, omem->buf,
171 GST_MINI_OBJECT_FLAGS (parent) | GST_MINI_OBJECT_FLAG_LOCK_READONLY,
172 parent, offset, size);
174 return (GstMemory *) sub;
178 gst_omx_memory_get_omx_buf (GstMemory * mem)
180 GstOMXMemory *omx_mem;
182 if (GST_IS_OMX_ALLOCATOR (mem->allocator))
183 omx_mem = (GstOMXMemory *) mem;
185 omx_mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
186 GST_OMX_MEMORY_QUARK);
194 /*********************/
195 /** GstOMXAllocator **/
196 /*********************/
199 gst_omx_allocator_init (GstOMXAllocator * allocator)
201 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
203 alloc->mem_type = GST_OMX_MEMORY_TYPE;
205 alloc->mem_map = gst_omx_memory_map;
206 alloc->mem_unmap = gst_omx_memory_unmap;
207 alloc->mem_share = gst_omx_memory_share;
208 /* default copy & is_span */
210 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
212 g_mutex_init (&allocator->lock);
213 g_cond_init (&allocator->cond);
217 gst_omx_allocator_new (GstOMXComponent * component, GstOMXPort * port)
219 GstOMXAllocator *allocator;
221 allocator = g_object_new (gst_omx_allocator_get_type (), NULL);
222 allocator->component = gst_omx_component_ref (component);
223 allocator->port = port;
229 gst_omx_allocator_finalize (GObject * object)
231 GstOMXAllocator *allocator = GST_OMX_ALLOCATOR (object);
233 gst_omx_component_unref (allocator->component);
234 g_mutex_clear (&allocator->lock);
235 g_cond_clear (&allocator->cond);
239 gst_omx_allocator_configure (GstOMXAllocator * allocator, guint count,
240 GstOMXAllocatorForeignMemMode mode)
242 /* check if already configured */
243 if (allocator->n_memories > 0)
246 allocator->n_memories = count;
247 allocator->foreign_mode = mode;
248 if (mode == GST_OMX_ALLOCATOR_FOREIGN_MEM_DMABUF)
249 allocator->foreign_allocator = gst_dmabuf_allocator_new ();
254 /* must be protected with allocator->lock */
256 gst_omx_allocator_dealloc (GstOMXAllocator * allocator)
258 /* might be called more than once */
259 if (!allocator->memories)
262 /* return foreign memory back to whoever lended it to us.
263 * the signal handler is expected to increase the ref count of foreign_mem */
264 if (allocator->foreign_mode == GST_OMX_ALLOCATOR_FOREIGN_MEM_OTHER_POOL) {
268 for (i = 0; i < allocator->memories->len; i++) {
269 m = g_ptr_array_index (allocator->memories, i);
271 /* this should not happen, but let's not crash for this */
272 if (!m->foreign_mem) {
273 GST_WARNING_OBJECT (allocator, "no foreign_mem to release");
277 /* restore the original dispose function */
278 GST_MINI_OBJECT_CAST (m->foreign_mem)->dispose =
279 (GstMiniObjectDisposeFunction) m->foreign_dispose;
281 g_signal_emit (allocator, signals[SIG_FOREIGN_MEM_RELEASED], 0, i,
286 g_ptr_array_foreach (allocator->memories, (GFunc) gst_memory_unref, NULL);
287 g_ptr_array_free (allocator->memories, TRUE);
288 allocator->memories = NULL;
289 allocator->n_memories = 0;
290 allocator->foreign_mode = GST_OMX_ALLOCATOR_FOREIGN_MEM_NONE;
291 if (allocator->foreign_allocator) {
292 g_object_unref (allocator->foreign_allocator);
293 allocator->foreign_allocator = NULL;
296 g_cond_broadcast (&allocator->cond);
300 gst_omx_allocator_set_active (GstOMXAllocator * allocator, gboolean active)
302 gboolean changed = FALSE;
304 /* on activation, _configure() must be called first */
305 g_return_val_if_fail (!active || allocator->n_memories > 0, FALSE);
307 g_mutex_lock (&allocator->lock);
309 if (allocator->active != active)
314 allocator->memories = g_ptr_array_sized_new (allocator->n_memories);
315 g_ptr_array_set_size (allocator->memories, allocator->n_memories);
317 if (g_atomic_int_get (&allocator->n_outstanding) == 0)
318 gst_omx_allocator_dealloc (allocator);
322 allocator->active = active;
323 g_mutex_unlock (&allocator->lock);
329 gst_omx_allocator_wait_inactive (GstOMXAllocator * allocator)
331 g_mutex_lock (&allocator->lock);
332 while (allocator->memories)
333 g_cond_wait (&allocator->cond, &allocator->lock);
334 g_mutex_unlock (&allocator->lock);
338 dec_outstanding (GstOMXAllocator * allocator)
340 if (g_atomic_int_dec_and_test (&allocator->n_outstanding)) {
341 /* keep a ref to the allocator because _dealloc() will free
342 * all the memories and the memories might be the only thing holding
343 * a reference to the allocator; we need to keep it alive until the
344 * end of this function call */
345 g_object_ref (allocator);
347 /* take the lock so that _set_active() is not run concurrently */
348 g_mutex_lock (&allocator->lock);
350 /* now that we have the lock, check if we have been de-activated with
351 * outstanding buffers */
352 if (!allocator->active)
353 gst_omx_allocator_dealloc (allocator);
355 g_mutex_unlock (&allocator->lock);
356 g_object_unref (allocator);
361 gst_omx_allocator_acquire (GstOMXAllocator * allocator, GstMemory ** memory,
362 gint index, GstOMXBuffer * omx_buf)
364 GstFlowReturn ret = GST_FLOW_OK;
365 GstOMXMemory *omx_mem = NULL;
367 /* ensure memories are not going to disappear concurrently */
368 g_atomic_int_inc (&allocator->n_outstanding);
370 if (!allocator->active) {
371 ret = GST_FLOW_FLUSHING;
375 if (index >= 0 && index < allocator->n_memories)
376 omx_mem = g_ptr_array_index (allocator->memories, index);
378 for (index = 0; index < allocator->n_memories; index++) {
379 omx_mem = g_ptr_array_index (allocator->memories, index);
380 if (omx_mem->buf == omx_buf)
385 if (G_UNLIKELY (!omx_mem || index >= allocator->n_memories)) {
386 GST_ERROR_OBJECT (allocator, "Failed to find OMX memory");
387 ret = GST_FLOW_ERROR;
391 if (G_UNLIKELY (omx_mem->buf->used)) {
392 GST_ERROR_OBJECT (allocator,
393 "Trying to acquire a buffer that is being used by the OMX port");
394 ret = GST_FLOW_ERROR;
398 omx_mem->acquired = TRUE;
400 if (omx_mem->foreign_mem)
401 *memory = omx_mem->foreign_mem;
403 *memory = GST_MEMORY_CAST (omx_mem);
406 if (ret != GST_FLOW_OK)
407 dec_outstanding (allocator);
411 /* installed as the GstMiniObject::dispose function of the acquired GstMemory */
413 gst_omx_allocator_memory_dispose (GstMemory * mem)
415 GstOMXMemory *omx_mem;
416 GstOMXAllocator *allocator;
418 /* memory may be from our allocator, but
419 * may as well be from the dmabuf allocator */
420 if (GST_IS_OMX_ALLOCATOR (mem->allocator))
421 omx_mem = (GstOMXMemory *) mem;
423 omx_mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
424 GST_OMX_MEMORY_QUARK);
426 if (omx_mem->acquired) {
427 /* keep the memory alive */
428 gst_memory_ref (mem);
430 omx_mem->acquired = FALSE;
432 allocator = GST_OMX_ALLOCATOR (GST_MEMORY_CAST (omx_mem)->allocator);
434 /* inform the upper layer that we are no longer using this GstOMXBuffer */
435 g_signal_emit (allocator, signals[SIG_OMXBUF_RELEASED], 0, omx_mem->buf);
437 dec_outstanding (allocator);
439 /* be careful here, both the memory and the allocator
440 * may have been free'd as part of the call to dec_outstanding() */
445 /* if the foreign memory had a dispose function, let that one decide
446 * the fate of this memory. We are no longer going to be using it here */
447 if (omx_mem->foreign_dispose)
448 return omx_mem->foreign_dispose (GST_MINI_OBJECT_CAST (mem));
454 install_mem_dispose (GstOMXMemory * mem)
456 GstMemory *managed_mem = (GstMemory *) mem;
458 if (mem->foreign_mem) {
459 managed_mem = mem->foreign_mem;
460 mem->foreign_dispose = GST_MINI_OBJECT_CAST (managed_mem)->dispose;
463 GST_MINI_OBJECT_CAST (managed_mem)->dispose =
464 (GstMiniObjectDisposeFunction) gst_omx_allocator_memory_dispose;
467 /* the returned memory is transfer:none, ref still belongs to the allocator */
469 gst_omx_allocator_allocate (GstOMXAllocator * allocator, gint index,
470 GstMemory * foreign_mem)
473 GstOMXBuffer *omx_buf;
475 g_return_val_if_fail (allocator->port->buffers, NULL);
476 g_return_val_if_fail (allocator->memories, NULL);
477 g_return_val_if_fail (index >= 0 && index < allocator->n_memories, NULL);
478 g_return_val_if_fail ((foreign_mem == NULL &&
479 allocator->foreign_mode != GST_OMX_ALLOCATOR_FOREIGN_MEM_OTHER_POOL)
480 || (foreign_mem != NULL
481 && allocator->foreign_mode ==
482 GST_OMX_ALLOCATOR_FOREIGN_MEM_OTHER_POOL), NULL);
484 omx_buf = g_ptr_array_index (allocator->port->buffers, index);
485 g_return_val_if_fail (omx_buf != NULL, NULL);
487 mem = gst_omx_memory_new (allocator, omx_buf, 0, NULL, 0, -1);
489 switch (allocator->foreign_mode) {
490 case GST_OMX_ALLOCATOR_FOREIGN_MEM_NONE:
491 install_mem_dispose (mem);
493 case GST_OMX_ALLOCATOR_FOREIGN_MEM_DMABUF:
495 gint fd = GPOINTER_TO_INT (omx_buf->omx_buf->pBuffer);
497 gst_dmabuf_allocator_alloc (allocator->foreign_allocator, fd,
498 omx_buf->omx_buf->nAllocLen);
499 gst_mini_object_set_qdata (GST_MINI_OBJECT (mem->foreign_mem),
500 GST_OMX_MEMORY_QUARK, mem, NULL);
501 install_mem_dispose (mem);
504 case GST_OMX_ALLOCATOR_FOREIGN_MEM_OTHER_POOL:
505 mem->foreign_mem = foreign_mem;
506 gst_mini_object_set_qdata (GST_MINI_OBJECT (mem->foreign_mem),
507 GST_OMX_MEMORY_QUARK, mem, NULL);
508 install_mem_dispose (mem);
511 g_assert_not_reached ();
515 g_ptr_array_index (allocator->memories, index) = mem;
516 return mem->foreign_mem ? mem->foreign_mem : (GstMemory *) mem;
520 gst_omx_allocator_free (GstAllocator * allocator, GstMemory * mem)
522 GstOMXMemory *omem = (GstOMXMemory *) mem;
524 g_warn_if_fail (!omem->acquired);
526 if (omem->foreign_mem)
527 gst_memory_unref (omem->foreign_mem);
529 g_slice_free (GstOMXMemory, omem);
533 gst_omx_allocator_class_init (GstOMXAllocatorClass * klass)
535 GObjectClass *object_class;
536 GstAllocatorClass *allocator_class;
538 object_class = (GObjectClass *) klass;
539 allocator_class = (GstAllocatorClass *) klass;
541 object_class->finalize = gst_omx_allocator_finalize;
542 allocator_class->alloc = NULL;
543 allocator_class->free = gst_omx_allocator_free;
545 signals[SIG_OMXBUF_RELEASED] = g_signal_new ("omxbuf-released",
546 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0,
547 NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_POINTER);
549 signals[SIG_FOREIGN_MEM_RELEASED] = g_signal_new ("foreign-mem-released",
550 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0,
551 NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_POINTER);