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.
 
   6 Add physical memory allocator
 
   8 Upstream-Status: Inappropriate [i.MX specific]
 
  10 Signed-off-by: Haihua Hu <jared.hu@nxp.com>
 
  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
 
  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 = \
 
  29 +       gstallocatorphymem.h
 
  33  libgstallocators_@GST_API_VERSION@_la_SOURCES = \
 
  37 +       gstallocatorphymem.c
 
  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
 
  43 index 0000000..cf5995e
 
  45 +++ b/gst-libs/gst/allocators/gstallocatorphymem.c
 
  48 + * Copyright (c) 2013-2015, Freescale Semiconductor, Inc. All rights reserved.
 
  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.
 
  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.
 
  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.
 
  68 +#include "gstallocatorphymem.h"
 
  78 +default_copy (GstAllocatorPhyMem *allocator, PhyMemBlock *dst_mem,
 
  79 +              PhyMemBlock *src_mem, guint offset, guint size)
 
  81 +  GST_WARNING ("No default copy implementation for physical memory allocator.\n");
 
  86 +gst_phymem_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
 
  88 +  GstMemoryPhy *phymem = (GstMemoryPhy*) mem;
 
  90 +  if (GST_MEMORY_IS_READONLY(mem) && (flags & GST_MAP_WRITE)) {
 
  91 +    GST_ERROR("memory is read only");
 
  95 +  return phymem->vaddr;
 
  99 +gst_phymem_unmap (GstMemory * mem)
 
 105 +gst_phymem_copy (GstMemory * mem, gssize offset, gssize size)
 
 107 +  GstAllocatorPhyMemClass *klass;
 
 108 +  GstMemoryPhy *src_mem = (GstMemoryPhy *)mem;
 
 110 +  GstMemoryPhy *dst_mem = g_slice_alloc(sizeof(GstMemoryPhy));
 
 111 +  if(dst_mem == NULL) {
 
 112 +    GST_ERROR("Can't allocate for GstMemoryPhy structure.\n");
 
 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");
 
 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);
 
 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);
 
 131 +  dst_mem->vaddr = dst_mem->block.vaddr;
 
 132 +  dst_mem->paddr = dst_mem->block.paddr;
 
 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);
 
 139 +  return (GstMemory*)dst_mem;
 
 143 +gst_phymem_share (GstMemory * mem, gssize offset, gssize size)
 
 145 +  GST_ERROR("Not implemented mem_share in gstallocatorphymem.\n");
 
 150 +gst_phymem_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
 
 156 +gst_phymem_get_phy (GstMemory * mem)
 
 158 +  GstMemoryPhy *phymem = (GstMemoryPhy*) mem;
 
 160 +  return phymem->paddr;
 
 164 +base_alloc (GstAllocator * allocator, gsize size,
 
 165 +    GstAllocationParams * params)
 
 167 +  GstAllocatorPhyMemClass *klass;
 
 169 +  gsize maxsize, aoffset, offset, align, padding;
 
 172 +  mem = g_slice_alloc(sizeof(GstMemoryPhy));
 
 174 +    GST_ERROR("Can allocate for GstMemoryPhy structure.\n");
 
 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");
 
 184 +  GST_DEBUG ("allocate params, prefix (%d), padding (%d), align (%d), flags (%x).\n",
 
 185 +      params->prefix, params->padding, params->align, params->flags);
 
 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);
 
 194 +  GST_DEBUG ("allocated phymem, vaddr(%p), paddr(%p), size(%d).\n", 
 
 195 +      mem->block.vaddr, mem->block.paddr, mem->block.size);
 
 197 +  data = mem->block.vaddr;
 
 198 +  offset = params->prefix;
 
 199 +  align = params->align;
 
 201 +  if ((aoffset = ((guintptr)data & align))) {
 
 202 +    aoffset = (align + 1) - aoffset;
 
 204 +    maxsize -= aoffset;
 
 206 +  mem->vaddr = mem->block.vaddr + aoffset;
 
 207 +  mem->paddr = mem->block.paddr + aoffset;
 
 209 +  GST_DEBUG ("aligned vaddr(%p), paddr(%p), size(%d).\n", 
 
 210 +      mem->block.vaddr, mem->block.paddr, mem->block.size);
 
 212 +  if (offset && (params->flags & GST_MEMORY_FLAG_ZERO_PREFIXED))
 
 213 +    memset (data, 0, offset);
 
 215 +  padding = maxsize - (offset + size);
 
 216 +  if (padding && (params->flags & GST_MEMORY_FLAG_ZERO_PADDED))
 
 217 +    memset (data + offset + size, 0, padding);
 
 219 +  gst_memory_init (GST_MEMORY_CAST (mem), params->flags, allocator, NULL, maxsize, align, offset, size);
 
 221 +  return (GstMemory*)mem;
 
 225 +base_free (GstAllocator * allocator, GstMemory * mem)
 
 227 +  GstAllocatorPhyMemClass *klass;
 
 228 +  GstMemoryPhy *phymem;
 
 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);
 
 236 +  phymem = (GstMemoryPhy*)mem;
 
 238 +  GST_DEBUG ("free phymem, vaddr(%p), paddr(%p), size(%d).\n",
 
 239 +      phymem->block.vaddr, phymem->block.paddr, phymem->block.size);
 
 241 +  klass->free_phymem((GstAllocatorPhyMem*)allocator, &phymem->block);
 
 242 +  g_slice_free1(sizeof(GstMemoryPhy), mem);
 
 248 +default_alloc (GstAllocatorPhyMem *allocator, PhyMemBlock *phy_mem)
 
 250 +  GST_ERROR ("No default allocating implementation for physical memory allocation.\n");
 
 255 +default_free (GstAllocatorPhyMem *allocator, PhyMemBlock *phy_mem)
 
 257 +  GST_ERROR ("No default free implementation for physical memory allocation.\n");
 
 261 +G_DEFINE_TYPE (GstAllocatorPhyMem, gst_allocator_phymem, GST_TYPE_ALLOCATOR);
 
 264 +gst_allocator_phymem_class_init (GstAllocatorPhyMemClass * klass)
 
 266 +  GstAllocatorClass *allocator_class;
 
 268 +  allocator_class = (GstAllocatorClass *) klass;
 
 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;
 
 278 +gst_allocator_phymem_init (GstAllocatorPhyMem * allocator)
 
 280 +  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
 
 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;
 
 293 +gst_buffer_is_phymem (GstBuffer *buffer)
 
 295 +  gboolean ret = FALSE;
 
 296 +  PhyMemBlock * memblk;
 
 297 +  GstMemory *mem = gst_buffer_get_memory (buffer, 0);
 
 299 +    GST_ERROR ("Not get memory from buffer.\n");
 
 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");
 
 312 +  gst_memory_unref (mem);
 
 318 +gst_buffer_query_phymem_block (GstBuffer *buffer)
 
 321 +  GstMemoryPhy *memphy;
 
 322 +  PhyMemBlock *memblk;
 
 324 +  mem = gst_buffer_get_memory (buffer, 0);
 
 326 +    GST_ERROR ("Not get memory from buffer.\n");
 
 330 +  if(!GST_IS_ALLOCATOR_PHYMEM(mem->allocator)) {
 
 331 +    gst_memory_unref (mem);
 
 335 +  memphy = (GstMemoryPhy*) mem;
 
 336 +  memblk = &memphy->block;
 
 338 +  gst_memory_unref (mem);
 
 344 +gst_memory_query_phymem_block (GstMemory *mem)
 
 346 +  GstMemoryPhy *memphy;
 
 347 +  PhyMemBlock *memblk;
 
 352 +  if (!GST_IS_ALLOCATOR_PHYMEM(mem->allocator))
 
 355 +  memphy = (GstMemoryPhy*) mem;
 
 356 +  memblk = &memphy->block;
 
 361 diff --git a/gst-libs/gst/allocators/gstallocatorphymem.h b/gst-libs/gst/allocators/gstallocatorphymem.h
 
 363 index 0000000..f0833ae
 
 365 +++ b/gst-libs/gst/allocators/gstallocatorphymem.h
 
 368 + * Copyright (c) 2013-2015, Freescale Semiconductor, Inc. All rights reserved.
 
 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.
 
 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.
 
 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.
 
 386 +#ifndef __ALLOCATOR_PHYMEM_H__
 
 387 +#define __ALLOCATOR_PHYMEM_H__
 
 389 +#include <gst/gst.h>
 
 390 +#include <gst/gstallocator.h>
 
 392 +#define PAGE_ALIGN(x) (((x) + 4095) & ~4095)
 
 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))
 
 400 +typedef struct _GstAllocatorPhyMem GstAllocatorPhyMem;
 
 401 +typedef struct _GstAllocatorPhyMemClass GstAllocatorPhyMemClass;
 
 403 +/* also change gst-libs/gst/gl/gstglvivdirecttexture.c in gst-plugins-bad git
 
 404 + * if changed below structure */
 
 410 +  gpointer *user_data;
 
 413 +struct _GstAllocatorPhyMem {
 
 414 +  GstAllocator parent;
 
 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);
 
 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);