memory: add NO_SHARE flag to memory

Add a NO_SHARE flag to memory to indicate that it should not be shared
between buffers.
This commit is contained in:
Wim Taymans 2011-04-07 16:02:43 +02:00
parent a13740db3f
commit a5e1ec0edc
2 changed files with 33 additions and 10 deletions

View file

@ -309,26 +309,32 @@ gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
if (flags & GST_BUFFER_COPY_MEMORY) { if (flags & GST_BUFFER_COPY_MEMORY) {
GstMemory *mem; GstMemory *mem;
gsize left, len, i, bsize; gsize skip, left, len, i, bsize;
len = GST_BUFFER_MEM_LEN (src); len = GST_BUFFER_MEM_LEN (src);
left = size; left = size;
skip = offset;
/* copy and make regions of the memory */ /* copy and make regions of the memory */
for (i = 0; i < len && left > 0; i++) { for (i = 0; i < len && left > 0; i++) {
mem = GST_BUFFER_MEM_PTR (src, i); mem = GST_BUFFER_MEM_PTR (src, i);
bsize = gst_memory_get_sizes (mem, NULL); bsize = gst_memory_get_sizes (mem, NULL);
if (bsize <= offset) { if (bsize <= skip) {
/* don't copy buffer */ /* don't copy buffer */
offset -= bsize; skip -= bsize;
} else { } else {
gsize tocopy; gsize tocopy;
tocopy = MIN (bsize - offset, left); tocopy = MIN (bsize - skip, left);
if (tocopy < bsize) { if (mem->flags & GST_MEMORY_FLAG_NO_SHARE) {
/* no share, always copy then */
mem = gst_memory_copy (mem, skip, tocopy);
skip = 0;
} else if (tocopy < bsize) {
/* we need to clip something */ /* we need to clip something */
mem = gst_memory_share (mem, offset, tocopy); mem = gst_memory_share (mem, skip, tocopy);
skip = 0;
} else { } else {
mem = gst_memory_ref (mem); mem = gst_memory_ref (mem);
} }
@ -604,6 +610,8 @@ _get_memory (GstBuffer * buffer, guint idx, gboolean write)
if (G_UNLIKELY (write && !GST_MEMORY_IS_WRITABLE (mem))) { if (G_UNLIKELY (write && !GST_MEMORY_IS_WRITABLE (mem))) {
GstMemory *copy; GstMemory *copy;
GST_CAT_LOG (GST_CAT_BUFFER,
"making writable copy of memory %p in buffer %p", mem, buffer);
/* replace with a writable copy */ /* replace with a writable copy */
copy = gst_memory_copy (mem, 0, -1); copy = gst_memory_copy (mem, 0, -1);
GST_BUFFER_MEM_PTR (buffer, idx) = copy; GST_BUFFER_MEM_PTR (buffer, idx) = copy;
@ -758,10 +766,16 @@ gst_buffer_resize (GstBuffer * buffer, gsize offset, gsize size)
gst_memory_resize (mem, offset, tocopy); gst_memory_resize (mem, offset, tocopy);
} else { } else {
GstMemory *tmp; GstMemory *tmp;
if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
tmp = gst_memory_copy (mem, offset, tocopy);
else
tmp = gst_memory_share (mem, offset, tocopy); tmp = gst_memory_share (mem, offset, tocopy);
gst_memory_unref (mem); gst_memory_unref (mem);
mem = tmp; mem = tmp;
} }
offset = 0;
} }
GST_BUFFER_MEM_PTR (buffer, di++) = mem; GST_BUFFER_MEM_PTR (buffer, di++) = mem;
size -= tocopy; size -= tocopy;
@ -1137,6 +1151,9 @@ _gst_buffer_arr_span (GstMemory ** mem[], gsize len[], guint n, gsize offset,
if (!writable if (!writable
&& _gst_buffer_arr_is_span_fast (mem, len, n, &poffset, &parent)) { && _gst_buffer_arr_is_span_fast (mem, len, n, &poffset, &parent)) {
if (parent->flags & GST_MEMORY_FLAG_NO_SHARE)
span = gst_memory_copy (parent, offset + poffset, size);
else
span = gst_memory_share (parent, offset + poffset, size); span = gst_memory_share (parent, offset + poffset, size);
} else { } else {
gsize count, left; gsize count, left;

View file

@ -35,12 +35,18 @@ typedef struct _GstMemoryImpl GstMemoryImpl;
/** /**
* GstMemoryFlags: * GstMemoryFlags:
* @GST_MEMORY_FLAG_READONLY: memory is readonly * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
* memory with #GST_MAP_WRITE.
* @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
* made when this memory needs to be shared between buffers.
* *
* Flags for wrapped memory. * Flags for wrapped memory.
*/ */
typedef enum { typedef enum {
GST_MEMORY_FLAG_READONLY = (1 << 0) GST_MEMORY_FLAG_READONLY = (1 << 0),
GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
GST_MEMORY_FLAG_LAST = (1 << 24)
} GstMemoryFlags; } GstMemoryFlags;
/** /**