content/media/gstreamer/GStreamerAllocator.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/gstreamer/GStreamerAllocator.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     1.4 +#ifdef HAVE_CONFIG_H
     1.5 +#include "config.h"
     1.6 +#endif
     1.7 +
     1.8 +#include "GStreamerAllocator.h"
     1.9 +
    1.10 +#include <gst/video/video.h>
    1.11 +#include <gst/video/gstvideometa.h>
    1.12 +
    1.13 +#include "GStreamerLoader.h"
    1.14 +
    1.15 +using namespace mozilla::layers;
    1.16 +
    1.17 +namespace mozilla {
    1.18 +
    1.19 +typedef struct
    1.20 +{
    1.21 +  GstAllocator parent;
    1.22 +  GStreamerReader *reader;
    1.23 +} MozGfxMemoryAllocator;
    1.24 +
    1.25 +typedef struct
    1.26 +{
    1.27 +  GstAllocatorClass parent;
    1.28 +} MozGfxMemoryAllocatorClass;
    1.29 +
    1.30 +typedef struct
    1.31 +{
    1.32 +  GstMemory memory;
    1.33 +  PlanarYCbCrImage* image;
    1.34 +  guint8* data;
    1.35 +} MozGfxMemory;
    1.36 +
    1.37 +typedef struct
    1.38 +{
    1.39 +  GstMeta meta;
    1.40 +} MozGfxMeta;
    1.41 +
    1.42 +typedef struct
    1.43 +{
    1.44 +  GstVideoBufferPoolClass parent_class;
    1.45 +} MozGfxBufferPoolClass;
    1.46 +
    1.47 +typedef struct
    1.48 +{
    1.49 +  GstVideoBufferPool pool;
    1.50 +} MozGfxBufferPool;
    1.51 +
    1.52 +// working around GTK+ bug https://bugzilla.gnome.org/show_bug.cgi?id=723899
    1.53 +#pragma GCC diagnostic push
    1.54 +#pragma GCC diagnostic ignored "-Wunused-function"
    1.55 +G_DEFINE_TYPE(MozGfxMemoryAllocator, moz_gfx_memory_allocator, GST_TYPE_ALLOCATOR);
    1.56 +G_DEFINE_TYPE(MozGfxBufferPool, moz_gfx_buffer_pool, GST_TYPE_VIDEO_BUFFER_POOL);
    1.57 +#pragma GCC diagnostic pop
    1.58 +
    1.59 +void
    1.60 +moz_gfx_memory_reset(MozGfxMemory *mem)
    1.61 +{
    1.62 +  if (mem->image)
    1.63 +    mem->image->Release();
    1.64 +
    1.65 +  ImageContainer* container = ((MozGfxMemoryAllocator*) mem->memory.allocator)->reader->GetImageContainer();
    1.66 +  mem->image = reinterpret_cast<PlanarYCbCrImage*>(container->CreateImage(ImageFormat::PLANAR_YCBCR).take());
    1.67 +  mem->data = mem->image->AllocateAndGetNewBuffer(mem->memory.size);
    1.68 +}
    1.69 +
    1.70 +static GstMemory*
    1.71 +moz_gfx_memory_allocator_alloc(GstAllocator* aAllocator, gsize aSize,
    1.72 +    GstAllocationParams* aParams)
    1.73 +{
    1.74 +  MozGfxMemory* mem = g_slice_new (MozGfxMemory);
    1.75 +  gsize maxsize = aSize + aParams->prefix + aParams->padding;
    1.76 +  gst_memory_init(GST_MEMORY_CAST (mem),
    1.77 +                  (GstMemoryFlags)aParams->flags,
    1.78 +                  aAllocator, NULL, maxsize, aParams->align,
    1.79 +                  aParams->prefix, aSize);
    1.80 +  mem->image = NULL;
    1.81 +  moz_gfx_memory_reset(mem);
    1.82 +
    1.83 +  return (GstMemory *) mem;
    1.84 +}
    1.85 +
    1.86 +static void
    1.87 +moz_gfx_memory_allocator_free (GstAllocator * allocator, GstMemory * gmem)
    1.88 +{
    1.89 +  MozGfxMemory *mem = (MozGfxMemory *) gmem;
    1.90 +
    1.91 +  if (mem->memory.parent)
    1.92 +    goto sub_mem;
    1.93 +
    1.94 +  if (mem->image)
    1.95 +    mem->image->Release();
    1.96 +
    1.97 +sub_mem:
    1.98 +  g_slice_free (MozGfxMemory, mem);
    1.99 +}
   1.100 +
   1.101 +static gpointer
   1.102 +moz_gfx_memory_map (MozGfxMemory * mem, gsize maxsize, GstMapFlags flags)
   1.103 +{
   1.104 +  // check that the allocation didn't fail
   1.105 +  if (mem->data == nullptr)
   1.106 +    return nullptr;
   1.107 +
   1.108 +  return mem->data + mem->memory.offset;
   1.109 +}
   1.110 +
   1.111 +static gboolean
   1.112 +moz_gfx_memory_unmap (MozGfxMemory * mem)
   1.113 +{
   1.114 +  return TRUE;
   1.115 +}
   1.116 +
   1.117 +static MozGfxMemory *
   1.118 +moz_gfx_memory_share (MozGfxMemory * mem, gssize offset, gsize size)
   1.119 +{
   1.120 +  MozGfxMemory *sub;
   1.121 +  GstMemory *parent;
   1.122 +
   1.123 +  /* find the real parent */
   1.124 +  if ((parent = mem->memory.parent) == NULL)
   1.125 +    parent = (GstMemory *) mem;
   1.126 +
   1.127 +  if (size == (gsize) -1)
   1.128 +    size = mem->memory.size - offset;
   1.129 +
   1.130 +  /* the shared memory is always readonly */
   1.131 +  sub = g_slice_new (MozGfxMemory);
   1.132 +
   1.133 +  gst_memory_init (GST_MEMORY_CAST (sub),
   1.134 +      (GstMemoryFlags) (GST_MINI_OBJECT_FLAGS (parent) | GST_MINI_OBJECT_FLAG_LOCK_READONLY),
   1.135 +      mem->memory.allocator, &mem->memory, mem->memory.maxsize, mem->memory.align,
   1.136 +      mem->memory.offset + offset, size);
   1.137 +
   1.138 +  sub->image = mem->image;
   1.139 +  sub->data = mem->data;
   1.140 +
   1.141 +  return sub;
   1.142 +}
   1.143 +
   1.144 +static void
   1.145 +moz_gfx_memory_allocator_class_init (MozGfxMemoryAllocatorClass * klass)
   1.146 +{
   1.147 +  GstAllocatorClass *allocator_class;
   1.148 +
   1.149 +  allocator_class = (GstAllocatorClass *) klass;
   1.150 +
   1.151 +  allocator_class->alloc = moz_gfx_memory_allocator_alloc;
   1.152 +  allocator_class->free = moz_gfx_memory_allocator_free;
   1.153 +}
   1.154 +
   1.155 +static void
   1.156 +moz_gfx_memory_allocator_init (MozGfxMemoryAllocator * allocator)
   1.157 +{
   1.158 +  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
   1.159 +
   1.160 +  alloc->mem_type = "moz-gfx-image";
   1.161 +  alloc->mem_map = (GstMemoryMapFunction) moz_gfx_memory_map;
   1.162 +  alloc->mem_unmap = (GstMemoryUnmapFunction) moz_gfx_memory_unmap;
   1.163 +  alloc->mem_share = (GstMemoryShareFunction) moz_gfx_memory_share;
   1.164 +  /* fallback copy and is_span */
   1.165 +}
   1.166 +
   1.167 +void
   1.168 +moz_gfx_memory_allocator_set_reader(GstAllocator* aAllocator, GStreamerReader* aReader)
   1.169 +{
   1.170 +  MozGfxMemoryAllocator *allocator = (MozGfxMemoryAllocator *) aAllocator;
   1.171 +  allocator->reader = aReader;
   1.172 +}
   1.173 +
   1.174 +nsRefPtr<PlanarYCbCrImage>
   1.175 +moz_gfx_memory_get_image(GstMemory *aMemory)
   1.176 +{
   1.177 +  NS_ASSERTION(GST_IS_MOZ_GFX_MEMORY_ALLOCATOR(aMemory->allocator), "Should be a gfx image");
   1.178 +
   1.179 +  return ((MozGfxMemory *) aMemory)->image;
   1.180 +}
   1.181 +
   1.182 +void
   1.183 +moz_gfx_buffer_pool_reset_buffer (GstBufferPool* aPool, GstBuffer* aBuffer)
   1.184 +{
   1.185 +  GstMemory* mem = gst_buffer_peek_memory(aBuffer, 0);
   1.186 +
   1.187 +  NS_ASSERTION(GST_IS_MOZ_GFX_MEMORY_ALLOCATOR(mem->allocator), "Should be a gfx image");
   1.188 +  moz_gfx_memory_reset((MozGfxMemory *) mem);
   1.189 +  GST_BUFFER_POOL_CLASS(moz_gfx_buffer_pool_parent_class)->reset_buffer(aPool, aBuffer);
   1.190 +}
   1.191 +
   1.192 +static void
   1.193 +moz_gfx_buffer_pool_class_init (MozGfxBufferPoolClass * klass)
   1.194 +{
   1.195 +  GstBufferPoolClass *pool_class = (GstBufferPoolClass *) klass;
   1.196 +  pool_class->reset_buffer = moz_gfx_buffer_pool_reset_buffer;
   1.197 +}
   1.198 +
   1.199 +static void
   1.200 +moz_gfx_buffer_pool_init (MozGfxBufferPool * pool)
   1.201 +{
   1.202 +}
   1.203 +
   1.204 +} // namespace mozilla

mercurial