2
* Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.be>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
20
#include "my-vidmem.h"
22
static GstAllocator *_my_allocator;
37
_my_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params)
39
g_warning ("Use my_vidmem_alloc() to allocate from this allocator");
45
_my_free (GstAllocator * allocator, GstMemory * mem)
47
MyVidmem *vmem = (MyVidmem *) mem;
50
g_slice_free (MyVidmem, vmem);
51
GST_DEBUG ("%p: freed", vmem);
55
_my_vidmem_map (MyVidmem * mem, gsize maxsize, GstMapFlags flags)
60
if ((res = g_atomic_pointer_get (&mem->data)) != NULL)
63
res = g_malloc (maxsize);
65
if (g_atomic_pointer_compare_and_exchange (&mem->data, NULL, res))
71
GST_DEBUG ("%p: mapped %p", mem, res);
77
_my_vidmem_unmap (MyVidmem * mem)
79
GST_DEBUG ("%p: unmapped", mem);
84
_my_vidmem_share (MyVidmem * mem, gssize offset, gsize size)
89
GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
92
/* find the real parent */
93
if ((parent = mem->mem.parent) == NULL)
94
parent = (GstMemory *) mem;
97
size = mem->mem.size - offset;
99
sub = g_slice_new (MyVidmem);
100
/* the shared memory is always readonly */
101
gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
102
GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
103
mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
105
/* install pointer */
106
sub->data = _my_vidmem_map (mem, mem->mem.maxsize, GST_MAP_READ);
118
GstAllocatorClass parent_class;
119
} MyVidmemAllocatorClass;
121
GType my_vidmem_allocator_get_type (void);
122
G_DEFINE_TYPE (MyVidmemAllocator, my_vidmem_allocator, GST_TYPE_ALLOCATOR);
125
my_vidmem_allocator_class_init (MyVidmemAllocatorClass * klass)
127
GstAllocatorClass *allocator_class;
129
allocator_class = (GstAllocatorClass *) klass;
131
allocator_class->alloc = _my_alloc;
132
allocator_class->free = _my_free;
136
my_vidmem_allocator_init (MyVidmemAllocator * allocator)
138
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
140
alloc->mem_type = "MyVidmem";
141
alloc->mem_map = (GstMemoryMapFunction) _my_vidmem_map;
142
alloc->mem_unmap = (GstMemoryUnmapFunction) _my_vidmem_unmap;
143
alloc->mem_share = (GstMemoryShareFunction) _my_vidmem_share;
147
my_vidmem_init (void)
149
_my_allocator = g_object_new (my_vidmem_allocator_get_type (), NULL);
151
gst_allocator_register ("MyVidmem", gst_object_ref (_my_allocator));
155
my_vidmem_alloc (guint format, guint width, guint height)
160
GST_DEBUG ("alloc frame format %u %ux%u", format, width, height);
162
maxsize = (GST_ROUND_UP_4 (width) * height);
164
mem = g_slice_new (MyVidmem);
166
gst_memory_init (GST_MEMORY_CAST (mem), 0, _my_allocator, NULL,
167
maxsize, 31, 0, maxsize);
169
mem->format = format;
171
mem->height = height;
174
return (GstMemory *) mem;
178
my_is_vidmem (GstMemory * mem)
180
return mem->allocator == _my_allocator;
184
my_vidmem_get_format (GstMemory * mem, guint * format, guint * width,
187
MyVidmem *vmem = (MyVidmem *) mem;
189
*format = vmem->format;
190
*width = vmem->width;
191
*height = vmem->height;