1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2021 Rockchip Electronics Co., Ltd
3*4882a593Smuzhiyun * Author: Jeffy Chen <jeffy.chen@rock-chips.com>
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This library is free software; you can redistribute it and/or
6*4882a593Smuzhiyun * modify it under the terms of the GNU Library General Public
7*4882a593Smuzhiyun * License as published by the Free Software Foundation; either
8*4882a593Smuzhiyun * version 2 of the License, or (at your option) any later version.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This library is distributed in the hope that it will be useful,
11*4882a593Smuzhiyun * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*4882a593Smuzhiyun * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*4882a593Smuzhiyun * Library General Public License for more details.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * You should have received a copy of the GNU Library General Public
16*4882a593Smuzhiyun * License along with this library; if not, write to the
17*4882a593Smuzhiyun * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18*4882a593Smuzhiyun * Boston, MA 02110-1301, USA.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #ifdef HAVE_CONFIG_H
23*4882a593Smuzhiyun #include "config.h"
24*4882a593Smuzhiyun #endif
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <unistd.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include <gst/allocators/gstdmabuf.h>
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #include "gstmppallocator.h"
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define GST_TYPE_MPP_ALLOCATOR (gst_mpp_allocator_get_type())
33*4882a593Smuzhiyun G_DECLARE_FINAL_TYPE (GstMppAllocator, gst_mpp_allocator, GST,
34*4882a593Smuzhiyun MPP_ALLOCATOR, GstDmaBufAllocator);
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define GST_MPP_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
37*4882a593Smuzhiyun GST_TYPE_MPP_ALLOCATOR, GstMppAllocator))
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define GST_CAT_DEFAULT mppallocator_debug
40*4882a593Smuzhiyun GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define GST_ALLOCATOR_MPP "mpp"
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct _GstMppAllocator
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun GstDmaBufAllocator parent;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* group for buffer-alloc */
49*4882a593Smuzhiyun MppBufferGroup group;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* group for buffer-import */
52*4882a593Smuzhiyun MppBufferGroup ext_group;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* unique group ID */
55*4882a593Smuzhiyun gint index;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun /* cache buffers */
58*4882a593Smuzhiyun gboolean cacheable;
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #define gst_mpp_allocator_parent_class parent_class
62*4882a593Smuzhiyun G_DEFINE_TYPE (GstMppAllocator, gst_mpp_allocator, GST_TYPE_DMABUF_ALLOCATOR);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun static GQuark
gst_mpp_buffer_quark(void)65*4882a593Smuzhiyun gst_mpp_buffer_quark (void)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun static GQuark quark = 0;
68*4882a593Smuzhiyun if (quark == 0)
69*4882a593Smuzhiyun quark = g_quark_from_string ("mpp-buf");
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun return quark;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun static GQuark
gst_mpp_ext_buffer_quark(void)75*4882a593Smuzhiyun gst_mpp_ext_buffer_quark (void)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun static GQuark quark = 0;
78*4882a593Smuzhiyun if (quark == 0)
79*4882a593Smuzhiyun quark = g_quark_from_string ("mpp-ext-buf");
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return quark;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun void
gst_mpp_allocator_set_cacheable(GstAllocator * allocator,gboolean cacheable)85*4882a593Smuzhiyun gst_mpp_allocator_set_cacheable (GstAllocator * allocator, gboolean cacheable)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
88*4882a593Smuzhiyun self->cacheable = cacheable;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* Clear cached buffers */
91*4882a593Smuzhiyun if (!cacheable)
92*4882a593Smuzhiyun mpp_buffer_group_clear (self->group);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun gint
gst_mpp_allocator_get_index(GstAllocator * allocator)96*4882a593Smuzhiyun gst_mpp_allocator_get_index (GstAllocator * allocator)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
99*4882a593Smuzhiyun return self->index;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun MppBufferGroup
gst_mpp_allocator_get_mpp_group(GstAllocator * allocator)103*4882a593Smuzhiyun gst_mpp_allocator_get_mpp_group (GstAllocator * allocator)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
106*4882a593Smuzhiyun return self->group;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun MppBuffer
gst_mpp_mpp_buffer_from_gst_memory(GstMemory * mem)110*4882a593Smuzhiyun gst_mpp_mpp_buffer_from_gst_memory (GstMemory * mem)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun if (mem->parent)
113*4882a593Smuzhiyun return gst_mpp_mpp_buffer_from_gst_memory (mem->parent);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun return gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
116*4882a593Smuzhiyun gst_mpp_buffer_quark ());
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun static void
gst_mpp_mem_destroy(gpointer ptr)120*4882a593Smuzhiyun gst_mpp_mem_destroy (gpointer ptr)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun MppBuffer mbuf = ptr;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun mpp_buffer_put (mbuf);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun static GstMemory *
gst_mpp_allocator_import_dmafd(GstAllocator * allocator,gint fd,guint size)128*4882a593Smuzhiyun gst_mpp_allocator_import_dmafd (GstAllocator * allocator, gint fd, guint size)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
131*4882a593Smuzhiyun GstMemory *mem;
132*4882a593Smuzhiyun MppBufferInfo info = { 0, };
133*4882a593Smuzhiyun MppBuffer mbuf = NULL;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun GST_DEBUG_OBJECT (self, "import dmafd: %d (%d)", fd, size);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun info.type = MPP_BUFFER_TYPE_DRM;
138*4882a593Smuzhiyun info.size = size;
139*4882a593Smuzhiyun info.fd = fd;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun mpp_buffer_import_with_tag (self->ext_group, &info, &mbuf, NULL, __func__);
142*4882a593Smuzhiyun if (!mbuf)
143*4882a593Smuzhiyun return NULL;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun mpp_buffer_set_index (mbuf, self->index);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun mem = gst_mpp_allocator_import_mppbuf (allocator, mbuf);
148*4882a593Smuzhiyun mpp_buffer_put (mbuf);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun return mem;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun GstMemory *
gst_mpp_allocator_import_mppbuf(GstAllocator * allocator,MppBuffer mbuf)154*4882a593Smuzhiyun gst_mpp_allocator_import_mppbuf (GstAllocator * allocator, MppBuffer mbuf)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
157*4882a593Smuzhiyun GstMemory *mem;
158*4882a593Smuzhiyun GQuark quark;
159*4882a593Smuzhiyun guint size;
160*4882a593Smuzhiyun gint fd;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun GST_DEBUG_OBJECT (self, "import MPP buffer");
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun fd = mpp_buffer_get_fd (mbuf);
165*4882a593Smuzhiyun if (fd < 0) {
166*4882a593Smuzhiyun GST_ERROR_OBJECT (self, "failed to get dmafd");
167*4882a593Smuzhiyun return NULL;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* HACK: DRM buffers are actually aligned to 4096 (page) */
171*4882a593Smuzhiyun size = GST_ROUND_UP_N (mpp_buffer_get_size (mbuf), 4096);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (mpp_buffer_get_index (mbuf) != self->index) {
174*4882a593Smuzhiyun GST_DEBUG_OBJECT (self, "import from other group");
175*4882a593Smuzhiyun mem = gst_mpp_allocator_import_dmafd (allocator, fd, size);
176*4882a593Smuzhiyun quark = gst_mpp_ext_buffer_quark ();
177*4882a593Smuzhiyun } else {
178*4882a593Smuzhiyun mem = gst_fd_allocator_alloc (allocator, dup (fd), size,
179*4882a593Smuzhiyun GST_FD_MEMORY_FLAG_KEEP_MAPPED);
180*4882a593Smuzhiyun quark = gst_mpp_buffer_quark ();
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun mpp_buffer_inc_ref (mbuf);
184*4882a593Smuzhiyun gst_mini_object_set_qdata (GST_MINI_OBJECT (mem), quark, mbuf,
185*4882a593Smuzhiyun gst_mpp_mem_destroy);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun return mem;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun GstMemory *
gst_mpp_allocator_import_gst_memory(GstAllocator * allocator,GstMemory * mem)191*4882a593Smuzhiyun gst_mpp_allocator_import_gst_memory (GstAllocator * allocator, GstMemory * mem)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
194*4882a593Smuzhiyun MppBuffer mbuf;
195*4882a593Smuzhiyun gsize offset;
196*4882a593Smuzhiyun guint size;
197*4882a593Smuzhiyun gint fd;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun GST_DEBUG_OBJECT (self, "import gst memory");
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun if (!gst_is_dmabuf_memory (mem))
202*4882a593Smuzhiyun return NULL;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun mbuf = gst_mpp_mpp_buffer_from_gst_memory (mem);
205*4882a593Smuzhiyun if (mbuf)
206*4882a593Smuzhiyun return gst_mpp_allocator_import_mppbuf (allocator, mbuf);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun fd = gst_dmabuf_memory_get_fd (mem);
209*4882a593Smuzhiyun if (fd < 0) {
210*4882a593Smuzhiyun GST_ERROR_OBJECT (self, "failed to get dmafd");
211*4882a593Smuzhiyun return NULL;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun size = gst_memory_get_sizes (mem, &offset, NULL);
215*4882a593Smuzhiyun if (offset)
216*4882a593Smuzhiyun return NULL;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun return gst_mpp_allocator_import_dmafd (allocator, fd, size);
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun MppBuffer
gst_mpp_allocator_alloc_mppbuf(GstAllocator * allocator,gsize size)222*4882a593Smuzhiyun gst_mpp_allocator_alloc_mppbuf (GstAllocator * allocator, gsize size)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
225*4882a593Smuzhiyun MppBuffer mbuf = NULL;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun mpp_buffer_get (self->group, &mbuf, size);
228*4882a593Smuzhiyun mpp_buffer_set_index (mbuf, self->index);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun return mbuf;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun static GstMemory *
gst_mpp_allocator_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params UNUSED)234*4882a593Smuzhiyun gst_mpp_allocator_alloc (GstAllocator * allocator, gsize size,
235*4882a593Smuzhiyun GstAllocationParams * params UNUSED)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun GstMemory *mem;
238*4882a593Smuzhiyun MppBuffer mbuf;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun mbuf = gst_mpp_allocator_alloc_mppbuf (allocator, size);
241*4882a593Smuzhiyun if (!mbuf)
242*4882a593Smuzhiyun return NULL;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun mem = gst_mpp_allocator_import_mppbuf (allocator, mbuf);
245*4882a593Smuzhiyun mpp_buffer_put (mbuf);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun gst_memory_resize (mem, 0, size);
248*4882a593Smuzhiyun return mem;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun static gpointer
gst_mpp_mem_map_full(GstMemory * mem,GstMapInfo * info,gsize size)252*4882a593Smuzhiyun gst_mpp_mem_map_full (GstMemory * mem, GstMapInfo * info, gsize size)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun if (mem->parent)
255*4882a593Smuzhiyun return gst_mpp_mem_map_full (mem->parent, info, size);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun if (GST_MEMORY_IS_NOT_MAPPABLE (mem))
258*4882a593Smuzhiyun return NULL;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun return mem->allocator->mem_map (mem, size, info->flags);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun static void
gst_mpp_allocator_free(GstAllocator * allocator,GstMemory * gmem)264*4882a593Smuzhiyun gst_mpp_allocator_free (GstAllocator * allocator, GstMemory * gmem)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (allocator);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* Avoid caching external buffers */
269*4882a593Smuzhiyun mpp_buffer_group_clear (self->ext_group);
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* Clear cached buffers */
272*4882a593Smuzhiyun if (!self->cacheable)
273*4882a593Smuzhiyun mpp_buffer_group_clear (self->group);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun GST_ALLOCATOR_CLASS (parent_class)->free (allocator, gmem);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun GstAllocator *
gst_mpp_allocator_new(void)279*4882a593Smuzhiyun gst_mpp_allocator_new (void)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun GstMppAllocator *alloc;
282*4882a593Smuzhiyun MppBufferGroup group, ext_group;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun static gint num_mpp_alloc = 0;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (mpp_buffer_group_get_internal (&group, MPP_BUFFER_TYPE_DRM))
287*4882a593Smuzhiyun return FALSE;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun if (mpp_buffer_group_get_external (&ext_group, MPP_BUFFER_TYPE_DRM)) {
290*4882a593Smuzhiyun mpp_buffer_group_put (group);
291*4882a593Smuzhiyun return FALSE;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun alloc = g_object_new (GST_TYPE_MPP_ALLOCATOR, NULL);
295*4882a593Smuzhiyun gst_object_ref_sink (alloc);
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun alloc->group = group;
298*4882a593Smuzhiyun alloc->ext_group = ext_group;
299*4882a593Smuzhiyun alloc->index = num_mpp_alloc++;
300*4882a593Smuzhiyun alloc->cacheable = TRUE;
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun return GST_ALLOCATOR_CAST (alloc);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun static void
gst_mpp_allocator_finalize(GObject * obj)306*4882a593Smuzhiyun gst_mpp_allocator_finalize (GObject * obj)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun GstMppAllocator *self = GST_MPP_ALLOCATOR (obj);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun mpp_buffer_group_put (self->group);
311*4882a593Smuzhiyun mpp_buffer_group_put (self->ext_group);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun G_OBJECT_CLASS (parent_class)->finalize (obj);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun static void
gst_mpp_allocator_class_init(GstMppAllocatorClass * klass)317*4882a593Smuzhiyun gst_mpp_allocator_class_init (GstMppAllocatorClass * klass)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
320*4882a593Smuzhiyun GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "mppallocator", 0, "MPP allocator");
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun allocator_class->alloc = GST_DEBUG_FUNCPTR (gst_mpp_allocator_alloc);
325*4882a593Smuzhiyun allocator_class->free = GST_DEBUG_FUNCPTR (gst_mpp_allocator_free);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_mpp_allocator_finalize);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun static void
gst_mpp_allocator_init(GstMppAllocator * allocator)331*4882a593Smuzhiyun gst_mpp_allocator_init (GstMppAllocator * allocator)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun alloc->mem_type = GST_ALLOCATOR_MPP;
336*4882a593Smuzhiyun alloc->mem_map_full = GST_DEBUG_FUNCPTR (gst_mpp_mem_map_full);
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
339*4882a593Smuzhiyun }
340