summaryrefslogtreecommitdiff
path: root/recipes-multimedia/gstreamer/gstreamer1.0-plugins-bad/0021-ionmemory-dmabuf-memory-allocator-based-on-ion-drive.patch
blob: c344b2f50354026c08e247475ce3cae57346b4ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
From b245b00342439a0fc2b3e7892dceb1f595fbf67e Mon Sep 17 00:00:00 2001
From: Song Bing <bing.song@nxp.com>
Date: Tue, 12 Sep 2017 13:42:01 +0800
Subject: [PATCH 2/3] ionmemory: dmabuf memory allocator based on ion driver.

Upstream Status: Pending

https://bugzilla.gnome.org/show_bug.cgi?id=768794
---
 configure.ac                           |   6 +
 gst-libs/gst/allocators/Makefile.am    |  14 ++
 gst-libs/gst/allocators/gstionmemory.c | 241 +++++++++++++++++++++++++++++++++
 gst-libs/gst/allocators/gstionmemory.h |  65 +++++++++
 4 files changed, 326 insertions(+)
 create mode 100755 gst-libs/gst/allocators/gstionmemory.c
 create mode 100755 gst-libs/gst/allocators/gstionmemory.h

diff --git a/configure.ac b/configure.ac
index d17bb64..30acf1c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -565,6 +565,12 @@ if test "x$HAVE_WINSOCK2_H" = "xyes"; then
   AC_SUBST(WINSOCK2_LIBS)
 fi
 
+dnl check for ion
+translit(dnm, m, l) AM_CONDITIONAL(USE_ION, true)
+AG_GST_CHECK_FEATURE(ION, [ion], ion, [
+  AC_CHECK_HEADER(linux/ion.h, HAVE_ION="yes", HAVE_ION="no")
+])
+
 dnl *** opengl ***
 AC_ARG_ENABLE([opengl],
      [  --enable-opengl         Enable Desktop OpenGL support @<:@default=auto@:>@],
diff --git a/gst-libs/gst/allocators/Makefile.am b/gst-libs/gst/allocators/Makefile.am
index 46c75c0..07d86f9 100644
--- a/gst-libs/gst/allocators/Makefile.am
+++ b/gst-libs/gst/allocators/Makefile.am
@@ -7,16 +7,30 @@ libgstbadallocators_@GST_API_VERSION@_include_HEADERS = \
 	gstphysmemory.h \
 	gstallocatorphymem.h
 
+if USE_ION
+libgstbadallocators_@GST_API_VERSION@_include_HEADERS += \
+	gstionmemory.h
+endif
+
 noinst_HEADERS =
 
 libgstbadallocators_@GST_API_VERSION@_la_SOURCES = \
 	gstphysmemory.c \
 	gstallocatorphymem.c
 
+if USE_ION
+libgstbadallocators_@GST_API_VERSION@_la_SOURCES += \
+	gstionmemory.c
+endif
+
 libgstbadallocators_@GST_API_VERSION@_la_LIBADD = $(GST_LIBS) $(LIBM)
 libgstbadallocators_@GST_API_VERSION@_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS)
 libgstbadallocators_@GST_API_VERSION@_la_LDFLAGS = $(GST_LIB_LDFLAGS) $(GST_ALL_LDFLAGS) $(GST_LT_LDFLAGS)
 
+if USE_ION
+libgstbadallocators_@GST_API_VERSION@_la_LIBADD += -lgstallocators-$(GST_API_VERSION)
+endif
+
 if HAVE_INTROSPECTION
 BUILT_GIRSOURCES = GstBadAllocators-@GST_API_VERSION@.gir
 
diff --git a/gst-libs/gst/allocators/gstionmemory.c b/gst-libs/gst/allocators/gstionmemory.c
new file mode 100755
index 0000000..bfe13ad
--- /dev/null
+++ b/gst-libs/gst/allocators/gstionmemory.c
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2016, Freescale Semiconductor, Inc. All rights reserved.
+ * Copyright 2017 NXP
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <linux/ion.h>
+
+#include <gst/allocators/gstdmabuf.h>
+#include "gstionmemory.h"
+
+GST_DEBUG_CATEGORY_STATIC (ion_allocator_debug);
+#define GST_CAT_DEFAULT ion_allocator_debug
+
+#define gst_ion_allocator_parent_class parent_class
+
+G_DEFINE_TYPE (GstIONAllocator, gst_ion_allocator, GST_TYPE_DMABUF_ALLOCATOR)
+
+#define DEFAULT_HEAP_ID  0
+#define DEFAULT_FLAG     0
+
+enum
+{
+  PROP_0,
+  PROP_HEAP_ID,
+  PROP_FLAG,
+  PROP_LAST
+};
+
+static gint
+gst_ion_ioctl (gint fd, gint req, void *arg)
+{
+  gint ret = ioctl (fd, req, arg);
+  if (ret < 0) {
+    GST_ERROR ("ioctl %x failed with code %d: %s\n", req, ret,
+        strerror (errno));
+  }
+  return ret;
+}
+
+static void
+gst_ion_mem_init (void)
+{
+  GstAllocator *allocator = g_object_new (gst_ion_allocator_get_type (), NULL);
+  GstIONAllocator *self = GST_ION_ALLOCATOR (allocator);
+  gint fd;
+
+  fd = open ("/dev/ion", O_RDWR);
+  if (fd < 0) {
+    GST_WARNING ("Could not open ion driver");
+    g_object_unref (self);
+    return;
+  }
+
+  self->fd = fd;
+
+  gst_allocator_register (GST_ALLOCATOR_ION, allocator);
+}
+
+GstAllocator *
+gst_ion_allocator_obtain (void)
+{
+  static GOnce ion_allocator_once = G_ONCE_INIT;
+  GstAllocator *allocator;
+
+  g_once (&ion_allocator_once, (GThreadFunc) gst_ion_mem_init, NULL);
+
+  allocator = gst_allocator_find (GST_ALLOCATOR_ION);
+  if (allocator == NULL)
+    GST_WARNING ("No allocator named %s found", GST_ALLOCATOR_ION);
+
+  return allocator;
+}
+
+static GstMemory *
+gst_ion_alloc_alloc (GstAllocator * allocator, gsize size,
+    GstAllocationParams * params)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (allocator);
+  struct ion_allocation_data allocation_data = { 0 };
+  struct ion_fd_data fd_data = { 0 };
+  struct ion_handle_data handle_data = { 0 };
+  ion_user_handle_t ion_handle;
+  GstMemory *mem;
+  gsize ion_size;
+  gint dma_fd = -1;
+  gint ret;
+
+  if (self->fd < 0) {
+    GST_ERROR ("ion allocate param wrong");
+    return NULL;
+  }
+
+  ion_size = size + params->prefix + params->padding;
+  allocation_data.len = ion_size;
+  allocation_data.align = params->align;
+  allocation_data.heap_id_mask = 1 << self->heap_id;
+  allocation_data.flags = self->flags;
+  if (gst_ion_ioctl (self->fd, ION_IOC_ALLOC, &allocation_data) < 0) {
+    GST_ERROR ("ion allocate failed.");
+    return NULL;
+  }
+  ion_handle = allocation_data.handle;
+
+  fd_data.handle = ion_handle;
+  ret = gst_ion_ioctl (self->fd, ION_IOC_MAP, &fd_data);
+  if (ret < 0 || fd_data.fd < 0) {
+    GST_ERROR ("map ioctl failed or returned negative fd");
+    goto bail;
+  }
+  dma_fd = fd_data.fd;
+
+  handle_data.handle = ion_handle;
+  gst_ion_ioctl (self->fd, ION_IOC_FREE, &handle_data);
+
+  mem = gst_dmabuf_allocator_alloc (allocator, dma_fd, size);
+
+  GST_DEBUG ("ion allocated size: %" G_GSIZE_FORMAT "DMA FD: %d", ion_size,
+      dma_fd);
+
+  return mem;
+
+bail:
+  if (dma_fd >= 0) {
+    close (dma_fd);
+  }
+  handle_data.handle = ion_handle;
+  gst_ion_ioctl (self->fd, ION_IOC_FREE, &handle_data);
+
+  return NULL;
+}
+
+static void
+gst_ion_allocator_dispose (GObject * object)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (object);
+
+  if (self->fd > 0) {
+    close (self->fd);
+    self->fd = -1;
+  }
+
+  G_OBJECT_CLASS (parent_class)->dispose (object);
+}
+
+static void
+gst_ion_allocator_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (object);
+
+  switch (prop_id) {
+    case PROP_HEAP_ID:
+      self->heap_id = g_value_get_uint (value);
+      break;
+    case PROP_FLAG:
+      self->flags = g_value_get_uint (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_ion_allocator_get_property (GObject * object, guint prop_id, GValue * value,
+    GParamSpec * pspec)
+{
+  GstIONAllocator *self = GST_ION_ALLOCATOR (object);
+
+  switch (prop_id) {
+    case PROP_HEAP_ID:
+      g_value_set_uint (value, self->heap_id);
+      break;
+    case PROP_FLAG:
+      g_value_set_uint (value, self->flags);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_ion_allocator_class_init (GstIONAllocatorClass * klass)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
+
+  gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_ion_allocator_dispose);
+  gobject_class->set_property = gst_ion_allocator_set_property;
+  gobject_class->get_property = gst_ion_allocator_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_HEAP_ID,
+      g_param_spec_uint ("heap-id", "Heap ID",
+        "ION heap id", 0, G_MAXUINT32, DEFAULT_HEAP_ID,
+        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_property (gobject_class, PROP_FLAG,
+      g_param_spec_uint ("flags", "Flags",
+        "ION memory flags", 0, G_MAXUINT32, DEFAULT_FLAG,
+        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  allocator_class->alloc = GST_DEBUG_FUNCPTR (gst_ion_alloc_alloc);
+
+  GST_DEBUG_CATEGORY_INIT (ion_allocator_debug, "ionmemory", 0,
+      "DMA FD memory allocator based on ion");
+}
+
+static void
+gst_ion_allocator_init (GstIONAllocator * self)
+{
+  GstAllocator *allocator = GST_ALLOCATOR (self);
+
+  allocator->mem_type = GST_ALLOCATOR_ION;
+
+  self->heap_id = DEFAULT_HEAP_ID;
+  self->flags = DEFAULT_FLAG;
+}
diff --git a/gst-libs/gst/allocators/gstionmemory.h b/gst-libs/gst/allocators/gstionmemory.h
new file mode 100755
index 0000000..be45722
--- /dev/null
+++ b/gst-libs/gst/allocators/gstionmemory.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016, Freescale Semiconductor, Inc. All rights reserved.
+ * Copyright 2017 NXP
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GST_IONMEMORY_H__
+#define __GST_IONMEMORY_H__
+
+#include <gst/gst.h>
+#include <gst/allocators/gstdmabuf.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GstIONAllocator GstIONAllocator;
+typedef struct _GstIONAllocatorClass GstIONAllocatorClass;
+typedef struct _GstIONMemory GstIONMemory;
+
+#define GST_ALLOCATOR_ION "ionmem"
+
+#define GST_TYPE_ION_ALLOCATOR gst_ion_allocator_get_type ()
+#define GST_IS_ION_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+    GST_TYPE_ION_ALLOCATOR))
+#define GST_ION_ALLOCATOR(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_ION_ALLOCATOR, GstIONAllocator))
+#define GST_ION_ALLOCATOR_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_ION_ALLOCATOR, GstIONAllocatorClass))
+#define GST_ION_ALLOCATOR_CAST(obj) ((GstIONAllocator *)(obj))
+
+#define GST_ION_MEMORY_QUARK gst_ion_memory_quark ()
+
+struct _GstIONAllocator
+{
+  GstDmaBufAllocator parent;
+
+  gint fd;
+  guint heap_id;
+  guint flags;
+};
+
+struct _GstIONAllocatorClass
+{
+  GstDmaBufAllocatorClass parent;
+};
+
+GType gst_ion_allocator_get_type (void);
+GstAllocator* gst_ion_allocator_obtain (void);
+
+G_END_DECLS
+
+#endif /* __GST_IONMEMORY_H__ */
-- 
2.7.4