]> code.ossystems Code Review - meta-freescale.git/blob
80009a9ac6227fd7f03d42d3ea6a92904755b8d8
[meta-freescale.git] /
1 From 950fe3a224eb5339c946193413f9373c333ea1f0 Mon Sep 17 00:00:00 2001
2 From: Haihua Hu <jared.hu@nxp.com>
3 Date: Fri, 5 Aug 2016 17:08:40 +0800
4 Subject: [PATCH] [MMFMWK-7259] Remove dependence on imx plugin git.
5
6 Add physical memory allocator
7
8 Upstream-Status: Inappropriate [i.MX specific]
9
10 Signed-off-by: Haihua Hu <jared.hu@nxp.com>
11 ---
12  gst-libs/gst/allocators/Makefile.am          |   6 +-
13  gst-libs/gst/allocators/gstallocatorphymem.c | 314 +++++++++++++++++++++++++++
14  gst-libs/gst/allocators/gstallocatorphymem.h |  64 ++++++
15  3 files changed, 382 insertions(+), 2 deletions(-)
16  create mode 100755 gst-libs/gst/allocators/gstallocatorphymem.c
17  create mode 100755 gst-libs/gst/allocators/gstallocatorphymem.h
18
19 diff --git a/gst-libs/gst/allocators/Makefile.am b/gst-libs/gst/allocators/Makefile.am
20 index 5c11b8a..13ffd9c 100644
21 --- a/gst-libs/gst/allocators/Makefile.am
22 +++ b/gst-libs/gst/allocators/Makefile.am
23 @@ -5,13 +5,15 @@ libgstallocators_@GST_API_VERSION@_includedir = $(includedir)/gstreamer-@GST_API
24  libgstallocators_@GST_API_VERSION@_include_HEADERS = \
25         allocators.h \
26         gstfdmemory.h \
27 -       gstdmabuf.h
28 +       gstdmabuf.h \
29 +       gstallocatorphymem.h
30  
31  noinst_HEADERS =
32  
33  libgstallocators_@GST_API_VERSION@_la_SOURCES = \
34         gstfdmemory.c \
35 -       gstdmabuf.c 
36 +       gstdmabuf.c \
37 +       gstallocatorphymem.c
38  
39  libgstallocators_@GST_API_VERSION@_la_LIBADD = $(GST_LIBS) $(LIBM)
40  libgstallocators_@GST_API_VERSION@_la_CFLAGS = $(GST_PLUGINS_BASE_CFLAGS) $(GST_CFLAGS)
41 diff --git a/gst-libs/gst/allocators/gstallocatorphymem.c b/gst-libs/gst/allocators/gstallocatorphymem.c
42 new file mode 100755
43 index 0000000..cf5995e
44 --- /dev/null
45 +++ b/gst-libs/gst/allocators/gstallocatorphymem.c
46 @@ -0,0 +1,314 @@
47 +/*
48 + * Copyright (c) 2013-2015, Freescale Semiconductor, Inc. All rights reserved.
49 + *
50 + * This library is free software; you can redistribute it and/or
51 + * modify it under the terms of the GNU Library General Public
52 + * License as published by the Free Software Foundation; either
53 + * version 2 of the License, or (at your option) any later version.
54 + *
55 + * This library is distributed in the hope that it will be useful,
56 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
57 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
58 + * Library General Public License for more details.
59 + *
60 + * You should have received a copy of the GNU Library General Public
61 + * License along with this library; if not, write to the
62 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
63 + * Boston, MA 02111-1307, USA.
64 + */
65 +
66 +#include <stdio.h>
67 +#include <string.h>
68 +#include "gstallocatorphymem.h"
69 +
70 +typedef struct {
71 +  GstMemory mem;
72 +  guint8 *vaddr;
73 +  guint8 *paddr;
74 +  PhyMemBlock block;
75 +} GstMemoryPhy;
76 +
77 +static int
78 +default_copy (GstAllocatorPhyMem *allocator, PhyMemBlock *dst_mem,
79 +              PhyMemBlock *src_mem, guint offset, guint size)
80 +{
81 +  GST_WARNING ("No default copy implementation for physical memory allocator.\n");
82 +  return -1;
83 +}
84 +
85 +static gpointer
86 +gst_phymem_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
87 +{
88 +  GstMemoryPhy *phymem = (GstMemoryPhy*) mem;
89 +
90 +  if (GST_MEMORY_IS_READONLY(mem) && (flags & GST_MAP_WRITE)) {
91 +    GST_ERROR("memory is read only");
92 +    return NULL;
93 +  }
94 +
95 +  return phymem->vaddr;
96 +}
97 +
98 +static void
99 +gst_phymem_unmap (GstMemory * mem)
100 +{
101 +  return;
102 +}
103 +
104 +static GstMemory *
105 +gst_phymem_copy (GstMemory * mem, gssize offset, gssize size)
106 +{
107 +  GstAllocatorPhyMemClass *klass;
108 +  GstMemoryPhy *src_mem = (GstMemoryPhy *)mem;
109 +
110 +  GstMemoryPhy *dst_mem = g_slice_alloc(sizeof(GstMemoryPhy));
111 +  if(dst_mem == NULL) {
112 +    GST_ERROR("Can't allocate for GstMemoryPhy structure.\n");
113 +    return NULL;
114 +  }
115 +
116 +  klass = GST_ALLOCATOR_PHYMEM_CLASS(G_OBJECT_GET_CLASS(mem->allocator));
117 +  if(klass == NULL) {
118 +    GST_ERROR("Can't get class from allocator object.\n");
119 +    return NULL;
120 +  }
121 +
122 +  if(klass->copy_phymem((GstAllocatorPhyMem*)mem->allocator,
123 +                         &dst_mem->block, &src_mem->block, offset, size) < 0) {
124 +    GST_WARNING ("Copy phymem %d failed.\n", size);
125 +    return NULL;
126 +  }
127 +
128 +  GST_DEBUG ("copied phymem, vaddr(%p), paddr(%p), size(%d).\n",
129 +      dst_mem->block.vaddr, dst_mem->block.paddr, dst_mem->block.size);
130 +
131 +  dst_mem->vaddr = dst_mem->block.vaddr;
132 +  dst_mem->paddr = dst_mem->block.paddr;
133 +
134 +  gst_memory_init (GST_MEMORY_CAST (dst_mem),
135 +                   mem->mini_object.flags&(~GST_MEMORY_FLAG_READONLY),
136 +                   mem->allocator, NULL, mem->maxsize, mem->align,
137 +                   mem->offset, mem->size);
138 +
139 +  return (GstMemory*)dst_mem;
140 +}
141 +
142 +static GstMemory *
143 +gst_phymem_share (GstMemory * mem, gssize offset, gssize size)
144 +{
145 +  GST_ERROR("Not implemented mem_share in gstallocatorphymem.\n");
146 +  return NULL;
147 +}
148 +
149 +static gboolean
150 +gst_phymem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
151 +{
152 +  return FALSE;
153 +}
154 +
155 +static gpointer
156 +gst_phymem_get_phy (GstMemory * mem)
157 +{
158 +  GstMemoryPhy *phymem = (GstMemoryPhy*) mem;
159 +
160 +  return phymem->paddr;
161 +}
162 +
163 +static GstMemory *
164 +base_alloc (GstAllocator * allocator, gsize size,
165 +    GstAllocationParams * params)
166 +{
167 +  GstAllocatorPhyMemClass *klass;
168 +  GstMemoryPhy *mem;
169 +  gsize maxsize, aoffset, offset, align, padding;
170 +  guint8 *data;
171 +
172 +  mem = g_slice_alloc(sizeof(GstMemoryPhy));
173 +  if(mem == NULL) {
174 +    GST_ERROR("Can allocate for GstMemoryPhy structure.\n");
175 +    return NULL;
176 +  }
177 +
178 +  klass = GST_ALLOCATOR_PHYMEM_CLASS(G_OBJECT_GET_CLASS(allocator));
179 +  if(klass == NULL) {
180 +    GST_ERROR("Can't get class from allocator object.\n");
181 +    return NULL;
182 +  }
183 +
184 +  GST_DEBUG ("allocate params, prefix (%d), padding (%d), align (%d), flags (%x).\n",
185 +      params->prefix, params->padding, params->align, params->flags);
186 +
187 +  maxsize = size + params->prefix + params->padding;
188 +  mem->block.size = maxsize;
189 +  if(klass->alloc_phymem((GstAllocatorPhyMem*)allocator, &mem->block) < 0) {
190 +    GST_ERROR("Allocate phymem %d failed.\n", maxsize);
191 +    return NULL;
192 +  }
193 +
194 +  GST_DEBUG ("allocated phymem, vaddr(%p), paddr(%p), size(%d).\n", 
195 +      mem->block.vaddr, mem->block.paddr, mem->block.size);
196 +
197 +  data = mem->block.vaddr;
198 +  offset = params->prefix;
199 +  align = params->align;
200 +  /* do alignment */
201 +  if ((aoffset = ((guintptr)data & align))) {
202 +    aoffset = (align + 1) - aoffset;
203 +    data += aoffset;
204 +    maxsize -= aoffset;
205 +  }
206 +  mem->vaddr = mem->block.vaddr + aoffset;
207 +  mem->paddr = mem->block.paddr + aoffset;
208 +
209 +  GST_DEBUG ("aligned vaddr(%p), paddr(%p), size(%d).\n", 
210 +      mem->block.vaddr, mem->block.paddr, mem->block.size);
211 +
212 +  if (offset && (params->flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
213 +    memset (data, 0, offset);
214 +
215 +  padding = maxsize - (offset + size);
216 +  if (padding && (params->flags & GST_MEMORY_FLAG_ZERO_PADDED))
217 +    memset (data + offset + size, 0, padding);
218 +
219 +  gst_memory_init (GST_MEMORY_CAST (mem), params->flags, allocator, NULL, maxsize, align, offset, size);
220 +
221 +  return (GstMemory*)mem;
222 +}
223 +
224 +static void
225 +base_free (GstAllocator * allocator, GstMemory * mem)
226 +{
227 +  GstAllocatorPhyMemClass *klass;
228 +  GstMemoryPhy *phymem;
229 +
230 +  klass = GST_ALLOCATOR_PHYMEM_CLASS(G_OBJECT_GET_CLASS(allocator));
231 +  if(klass == NULL) {
232 +    GST_ERROR("Can't get class from allocator object, can't free %p\n", mem);
233 +    return;
234 +  }
235 +
236 +  phymem = (GstMemoryPhy*)mem;
237 +
238 +  GST_DEBUG ("free phymem, vaddr(%p), paddr(%p), size(%d).\n",
239 +      phymem->block.vaddr, phymem->block.paddr, phymem->block.size);
240 +
241 +  klass->free_phymem((GstAllocatorPhyMem*)allocator, &phymem->block);
242 +  g_slice_free1(sizeof(GstMemoryPhy), mem);
243 +
244 +  return;
245 +}
246 +
247 +static int
248 +default_alloc (GstAllocatorPhyMem *allocator, PhyMemBlock *phy_mem)
249 +{
250 +  GST_ERROR ("No default allocating implementation for physical memory allocation.\n");
251 +  return -1;
252 +}
253 +
254 +static int
255 +default_free (GstAllocatorPhyMem *allocator, PhyMemBlock *phy_mem)
256 +{
257 +  GST_ERROR ("No default free implementation for physical memory allocation.\n");
258 +  return -1;
259 +}
260 +
261 +G_DEFINE_TYPE (GstAllocatorPhyMem, gst_allocator_phymem, GST_TYPE_ALLOCATOR);
262 +
263 +static void
264 +gst_allocator_phymem_class_init (GstAllocatorPhyMemClass * klass)
265 +{
266 +  GstAllocatorClass *allocator_class;
267 +
268 +  allocator_class = (GstAllocatorClass *) klass;
269 +
270 +  allocator_class->alloc = base_alloc;
271 +  allocator_class->free = base_free;
272 +  klass->alloc_phymem = default_alloc;
273 +  klass->free_phymem = default_free;
274 +  klass->copy_phymem = default_copy;
275 +}
276 +
277 +static void
278 +gst_allocator_phymem_init (GstAllocatorPhyMem * allocator)
279 +{
280 +  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
281 +
282 +  alloc->mem_map =  gst_phymem_map;
283 +  alloc->mem_unmap =  gst_phymem_unmap;
284 +  alloc->mem_copy =  gst_phymem_copy;
285 +  alloc->mem_share =  gst_phymem_share;
286 +  alloc->mem_is_span =  gst_phymem_is_span;
287 +}
288 +
289 +
290 +//global functions
291 +
292 +gboolean
293 +gst_buffer_is_phymem (GstBuffer *buffer)
294 +{
295 +  gboolean ret = FALSE;
296 +  PhyMemBlock * memblk;
297 +  GstMemory *mem = gst_buffer_get_memory (buffer, 0);
298 +  if(mem == NULL) {
299 +    GST_ERROR ("Not get memory from buffer.\n");
300 +    return FALSE;
301 +  }
302 +
303 +  if(GST_IS_ALLOCATOR_PHYMEM(mem->allocator)) {
304 +    if (NULL == ((GstMemoryPhy*)mem)->block.paddr) {
305 +      GST_WARNING("physical address in memory block is invalid");
306 +      ret = FALSE;
307 +    } else {
308 +      ret = TRUE;
309 +    }
310 +  }
311 +
312 +  gst_memory_unref (mem);
313 +
314 +  return ret;
315 +}
316 +
317 +PhyMemBlock *
318 +gst_buffer_query_phymem_block (GstBuffer *buffer)
319 +{
320 +  GstMemory *mem;
321 +  GstMemoryPhy *memphy;
322 +  PhyMemBlock *memblk;
323 +
324 +  mem = gst_buffer_get_memory (buffer, 0);
325 +  if(mem == NULL) {
326 +    GST_ERROR ("Not get memory from buffer.\n");
327 +    return NULL;
328 +  }
329 +
330 +  if(!GST_IS_ALLOCATOR_PHYMEM(mem->allocator)) {
331 +    gst_memory_unref (mem);
332 +    return NULL;
333 +  }
334 +
335 +  memphy = (GstMemoryPhy*) mem;
336 +  memblk = &memphy->block;
337 +
338 +  gst_memory_unref (mem);
339 +
340 +  return memblk;
341 +}
342 +
343 +PhyMemBlock *
344 +gst_memory_query_phymem_block (GstMemory *mem)
345 +{
346 +  GstMemoryPhy *memphy;
347 +  PhyMemBlock *memblk;
348 +
349 +  if (!mem)
350 +    return NULL;
351 +
352 +  if (!GST_IS_ALLOCATOR_PHYMEM(mem->allocator))
353 +    return NULL;
354 +
355 +  memphy = (GstMemoryPhy*) mem;
356 +  memblk = &memphy->block;
357 +
358 +  return memblk;
359 +}
360 +
361 diff --git a/gst-libs/gst/allocators/gstallocatorphymem.h b/gst-libs/gst/allocators/gstallocatorphymem.h
362 new file mode 100755
363 index 0000000..f0833ae
364 --- /dev/null
365 +++ b/gst-libs/gst/allocators/gstallocatorphymem.h
366 @@ -0,0 +1,64 @@
367 +/*
368 + * Copyright (c) 2013-2015, Freescale Semiconductor, Inc. All rights reserved.
369 + *
370 + * This library is free software; you can redistribute it and/or
371 + * modify it under the terms of the GNU Library General Public
372 + * License as published by the Free Software Foundation; either
373 + * version 2 of the License, or (at your option) any later version.
374 + *
375 + * This library is distributed in the hope that it will be useful,
376 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
377 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
378 + * Library General Public License for more details.
379 + *
380 + * You should have received a copy of the GNU Library General Public
381 + * License along with this library; if not, write to the
382 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
383 + * Boston, MA 02111-1307, USA.
384 + */
385 +
386 +#ifndef __ALLOCATOR_PHYMEM_H__
387 +#define __ALLOCATOR_PHYMEM_H__
388 +
389 +#include <gst/gst.h>
390 +#include <gst/gstallocator.h>
391 +
392 +#define PAGE_ALIGN(x) (((x) + 4095) & ~4095)
393 +
394 +#define GST_TYPE_ALLOCATOR_PHYMEM             (gst_allocator_phymem_get_type())
395 +#define GST_ALLOCATOR_PHYMEM(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_ALLOCATOR_PHYMEM, GstAllocatorPhyMem))
396 +#define GST_ALLOCATOR_PHYMEM_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_ALLOCATOR_PHYMEM, GstAllocatorPhyMemClass))
397 +#define GST_IS_ALLOCATOR_PHYMEM(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_ALLOCATOR_PHYMEM))
398 +#define GST_IS_ALLOCATOR_PHYMEM_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_ALLOCATOR_PHYMEM))
399 +
400 +typedef struct _GstAllocatorPhyMem GstAllocatorPhyMem;
401 +typedef struct _GstAllocatorPhyMemClass GstAllocatorPhyMemClass;
402 +
403 +/* also change gst-libs/gst/gl/gstglvivdirecttexture.c in gst-plugins-bad git
404 + * if changed below structure */
405 +typedef struct {
406 +  guint8 *vaddr;
407 +  guint8 *paddr;
408 +  guint8 *caddr;
409 +  gsize size;
410 +  gpointer *user_data;
411 +} PhyMemBlock;
412 +
413 +struct _GstAllocatorPhyMem {
414 +  GstAllocator parent;
415 +};
416 +
417 +struct _GstAllocatorPhyMemClass {
418 +  GstAllocatorClass parent_class;
419 +  int (*alloc_phymem) (GstAllocatorPhyMem *allocator, PhyMemBlock *phy_mem);
420 +  int (*free_phymem) (GstAllocatorPhyMem *allocator, PhyMemBlock *phy_mem);
421 +  int (*copy_phymem) (GstAllocatorPhyMem *allocator, PhyMemBlock *det_mem,
422 +                      PhyMemBlock *src_mem, guint offset, guint size);
423 +};
424 +
425 +GType gst_allocator_phymem_get_type (void);
426 +gboolean gst_buffer_is_phymem (GstBuffer *buffer);
427 +PhyMemBlock *gst_buffer_query_phymem_block (GstBuffer *buffer);
428 +PhyMemBlock *gst_memory_query_phymem_block (GstMemory *mem);
429 +
430 +#endif
431 -- 
432 1.9.1
433