omxvideoenc: drain encoder on ALLOCATION and DRAIN queries
[platform/upstream/gstreamer.git] / omx / gstomxallocator.c
1 /*
2  * Copyright (C) 2019, Collabora Ltd.
3  *   Author: George Kiagiadakis <george.kiagiadakis@collabora.com>
4  *
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.
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  * Lesser General Public License for more details.
14  *
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
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstomxallocator.h"
26 #include <gst/allocators/gstdmabuf.h>
27
28 GST_DEBUG_CATEGORY_STATIC (gst_omx_allocator_debug_category);
29 #define GST_CAT_DEFAULT gst_omx_allocator_debug_category
30
31 #define DEBUG_INIT \
32   GST_DEBUG_CATEGORY_INIT (gst_omx_allocator_debug_category, "omxallocator", 0, \
33       "debug category for gst-omx allocator class");
34
35 G_DEFINE_TYPE_WITH_CODE (GstOMXAllocator, gst_omx_allocator, GST_TYPE_ALLOCATOR,
36     DEBUG_INIT);
37
38 enum
39 {
40   SIG_OMXBUF_RELEASED,
41   SIG_FOREIGN_MEM_RELEASED,
42   LAST_SIGNAL
43 };
44
45 static guint signals[LAST_SIGNAL] = { 0 };
46
47 /* Custom allocator for memory associated with OpenMAX buffers
48  *
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
51  * back to OpenMAX.
52  *
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.
56  *
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
62  *
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.
66  *
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.
72  *
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.
78  *
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.
84  */
85
86 /******************/
87 /** GstOMXMemory **/
88 /******************/
89
90 #define GST_OMX_MEMORY_TYPE "openmax"
91
92 GQuark
93 gst_omx_memory_quark (void)
94 {
95   static GQuark quark = 0;
96
97   if (quark == 0)
98     quark = g_quark_from_static_string ("GstOMXMemory");
99
100   return quark;
101 }
102
103 static GstOMXMemory *
104 gst_omx_memory_new (GstOMXAllocator * allocator, GstOMXBuffer * omx_buf,
105     GstMemoryFlags flags, GstMemory * parent, gssize offset, gssize size)
106 {
107   GstOMXMemory *mem;
108   gint align;
109   gsize maxsize;
110
111   /* GStreamer uses a bitmask for the alignment while
112    * OMX uses the alignment itself. So we have to convert
113    * here */
114   align = allocator->port->port_def.nBufferAlignment;
115   if (align > 0)
116     align -= 1;
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);
120     align = 0;
121   }
122
123   maxsize = omx_buf->omx_buf->nAllocLen;
124
125   if (size == -1) {
126     size = maxsize - offset;
127   }
128
129   mem = g_slice_new0 (GstOMXMemory);
130   gst_memory_init (GST_MEMORY_CAST (mem), flags, (GstAllocator *) allocator,
131       parent, maxsize, align, offset, size);
132
133   mem->buf = omx_buf;
134
135   return mem;
136 }
137
138 static gpointer
139 gst_omx_memory_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
140 {
141   GstOMXMemory *omem = (GstOMXMemory *) mem;
142
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);
146
147   return omem->buf->omx_buf->pBuffer;
148 }
149
150 static void
151 gst_omx_memory_unmap (GstMemory * mem)
152 {
153 }
154
155 static GstMemory *
156 gst_omx_memory_share (GstMemory * mem, gssize offset, gssize size)
157 {
158   GstOMXMemory *omem = (GstOMXMemory *) mem;
159   GstOMXMemory *sub;
160   GstMemory *parent;
161
162   /* find the real parent */
163   if ((parent = mem->parent) == NULL)
164     parent = mem;
165
166   if (size == -1)
167     size = mem->size - offset;
168
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);
173
174   return (GstMemory *) sub;
175 }
176
177 GstOMXBuffer *
178 gst_omx_memory_get_omx_buf (GstMemory * mem)
179 {
180   GstOMXMemory *omx_mem;
181
182   if (GST_IS_OMX_ALLOCATOR (mem->allocator))
183     omx_mem = (GstOMXMemory *) mem;
184   else
185     omx_mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
186         GST_OMX_MEMORY_QUARK);
187
188   if (!omx_mem)
189     return NULL;
190
191   return omx_mem->buf;
192 }
193
194 /*********************/
195 /** GstOMXAllocator **/
196 /*********************/
197
198 static void
199 gst_omx_allocator_init (GstOMXAllocator * allocator)
200 {
201   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
202
203   alloc->mem_type = GST_OMX_MEMORY_TYPE;
204
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 */
209
210   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
211
212   g_mutex_init (&allocator->lock);
213   g_cond_init (&allocator->cond);
214 }
215
216 GstOMXAllocator *
217 gst_omx_allocator_new (GstOMXComponent * component, GstOMXPort * port)
218 {
219   GstOMXAllocator *allocator;
220
221   allocator = g_object_new (gst_omx_allocator_get_type (), NULL);
222   allocator->component = gst_omx_component_ref (component);
223   allocator->port = port;
224
225   return allocator;
226 }
227
228 static void
229 gst_omx_allocator_finalize (GObject * object)
230 {
231   GstOMXAllocator *allocator = GST_OMX_ALLOCATOR (object);
232
233   gst_omx_component_unref (allocator->component);
234   g_mutex_clear (&allocator->lock);
235   g_cond_clear (&allocator->cond);
236 }
237
238 gboolean
239 gst_omx_allocator_configure (GstOMXAllocator * allocator, guint count,
240     GstOMXAllocatorForeignMemMode mode)
241 {
242   /* check if already configured */
243   if (allocator->n_memories > 0)
244     return FALSE;
245
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 ();
250
251   return TRUE;
252 }
253
254 /* must be protected with allocator->lock */
255 static void
256 gst_omx_allocator_dealloc (GstOMXAllocator * allocator)
257 {
258   /* might be called more than once */
259   if (!allocator->memories)
260     return;
261
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) {
265     gint i;
266     GstOMXMemory *m;
267
268     for (i = 0; i < allocator->memories->len; i++) {
269       m = g_ptr_array_index (allocator->memories, i);
270
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");
274         continue;
275       }
276
277       /* restore the original dispose function */
278       GST_MINI_OBJECT_CAST (m->foreign_mem)->dispose =
279           (GstMiniObjectDisposeFunction) m->foreign_dispose;
280
281       g_signal_emit (allocator, signals[SIG_FOREIGN_MEM_RELEASED], 0, i,
282           m->foreign_mem);
283     }
284   }
285
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;
294   }
295
296   g_cond_broadcast (&allocator->cond);
297 }
298
299 gboolean
300 gst_omx_allocator_set_active (GstOMXAllocator * allocator, gboolean active)
301 {
302   gboolean changed = FALSE;
303
304   /* on activation, _configure() must be called first */
305   g_return_val_if_fail (!active || allocator->n_memories > 0, FALSE);
306
307   g_mutex_lock (&allocator->lock);
308
309   if (allocator->active != active)
310     changed = TRUE;
311
312   if (changed) {
313     if (active) {
314       allocator->memories = g_ptr_array_sized_new (allocator->n_memories);
315       g_ptr_array_set_size (allocator->memories, allocator->n_memories);
316     } else {
317       if (g_atomic_int_get (&allocator->n_outstanding) == 0)
318         gst_omx_allocator_dealloc (allocator);
319     }
320   }
321
322   allocator->active = active;
323   g_mutex_unlock (&allocator->lock);
324
325   return changed;
326 }
327
328 void
329 gst_omx_allocator_wait_inactive (GstOMXAllocator * allocator)
330 {
331   g_mutex_lock (&allocator->lock);
332   while (allocator->memories)
333     g_cond_wait (&allocator->cond, &allocator->lock);
334   g_mutex_unlock (&allocator->lock);
335 }
336
337 static inline void
338 dec_outstanding (GstOMXAllocator * allocator)
339 {
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);
346
347     /* take the lock so that _set_active() is not run concurrently */
348     g_mutex_lock (&allocator->lock);
349
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);
354
355     g_mutex_unlock (&allocator->lock);
356     g_object_unref (allocator);
357   }
358 }
359
360 GstFlowReturn
361 gst_omx_allocator_acquire (GstOMXAllocator * allocator, GstMemory ** memory,
362     gint index, GstOMXBuffer * omx_buf)
363 {
364   GstFlowReturn ret = GST_FLOW_OK;
365   GstOMXMemory *omx_mem = NULL;
366
367   /* ensure memories are not going to disappear concurrently */
368   g_atomic_int_inc (&allocator->n_outstanding);
369
370   if (!allocator->active) {
371     ret = GST_FLOW_FLUSHING;
372     goto beach;
373   }
374
375   if (index >= 0 && index < allocator->n_memories)
376     omx_mem = g_ptr_array_index (allocator->memories, index);
377   else if (omx_buf) {
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)
381         break;
382     }
383   }
384
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;
388     goto beach;
389   }
390
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;
395     goto beach;
396   }
397
398   omx_mem->acquired = TRUE;
399
400   if (omx_mem->foreign_mem)
401     *memory = omx_mem->foreign_mem;
402   else
403     *memory = GST_MEMORY_CAST (omx_mem);
404
405 beach:
406   if (ret != GST_FLOW_OK)
407     dec_outstanding (allocator);
408   return ret;
409 }
410
411 /* installed as the GstMiniObject::dispose function of the acquired GstMemory */
412 static gboolean
413 gst_omx_allocator_memory_dispose (GstMemory * mem)
414 {
415   GstOMXMemory *omx_mem;
416   GstOMXAllocator *allocator;
417
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;
422   else
423     omx_mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
424         GST_OMX_MEMORY_QUARK);
425
426   if (omx_mem->acquired) {
427     /* keep the memory alive */
428     gst_memory_ref (mem);
429
430     omx_mem->acquired = FALSE;
431
432     allocator = GST_OMX_ALLOCATOR (GST_MEMORY_CAST (omx_mem)->allocator);
433
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);
436
437     dec_outstanding (allocator);
438
439     /* be careful here, both the memory and the allocator
440      * may have been free'd as part of the call to dec_outstanding() */
441
442     return FALSE;
443   }
444
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));
449
450   return TRUE;
451 }
452
453 static inline void
454 install_mem_dispose (GstOMXMemory * mem)
455 {
456   GstMemory *managed_mem = (GstMemory *) mem;
457
458   if (mem->foreign_mem) {
459     managed_mem = mem->foreign_mem;
460     mem->foreign_dispose = GST_MINI_OBJECT_CAST (managed_mem)->dispose;
461   }
462
463   GST_MINI_OBJECT_CAST (managed_mem)->dispose =
464       (GstMiniObjectDisposeFunction) gst_omx_allocator_memory_dispose;
465 }
466
467 /* the returned memory is transfer:none, ref still belongs to the allocator */
468 GstMemory *
469 gst_omx_allocator_allocate (GstOMXAllocator * allocator, gint index,
470     GstMemory * foreign_mem)
471 {
472   GstOMXMemory *mem;
473   GstOMXBuffer *omx_buf;
474
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);
483
484   omx_buf = g_ptr_array_index (allocator->port->buffers, index);
485   g_return_val_if_fail (omx_buf != NULL, NULL);
486
487   mem = gst_omx_memory_new (allocator, omx_buf, 0, NULL, 0, -1);
488
489   switch (allocator->foreign_mode) {
490     case GST_OMX_ALLOCATOR_FOREIGN_MEM_NONE:
491       install_mem_dispose (mem);
492       break;
493     case GST_OMX_ALLOCATOR_FOREIGN_MEM_DMABUF:
494     {
495       gint fd = GPOINTER_TO_INT (omx_buf->omx_buf->pBuffer);
496       mem->foreign_mem =
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);
502       break;
503     }
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);
509       break;
510     default:
511       g_assert_not_reached ();
512       break;
513   }
514
515   g_ptr_array_index (allocator->memories, index) = mem;
516   return mem->foreign_mem ? mem->foreign_mem : (GstMemory *) mem;
517 }
518
519 static void
520 gst_omx_allocator_free (GstAllocator * allocator, GstMemory * mem)
521 {
522   GstOMXMemory *omem = (GstOMXMemory *) mem;
523
524   g_warn_if_fail (!omem->acquired);
525
526   if (omem->foreign_mem)
527     gst_memory_unref (omem->foreign_mem);
528
529   g_slice_free (GstOMXMemory, omem);
530 }
531
532 static void
533 gst_omx_allocator_class_init (GstOMXAllocatorClass * klass)
534 {
535   GObjectClass *object_class;
536   GstAllocatorClass *allocator_class;
537
538   object_class = (GObjectClass *) klass;
539   allocator_class = (GstAllocatorClass *) klass;
540
541   object_class->finalize = gst_omx_allocator_finalize;
542   allocator_class->alloc = NULL;
543   allocator_class->free = gst_omx_allocator_free;
544
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);
548
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);
552 }