Allow propogation of OOM errors to apps

This allows apps to catch out-of-memory errors when allocating textures.

Textures can be pretty huge at times and so it's quite possible for an
application to try and allocate more memory than is available. It's also
very possible that the application can take some action in response to
reduce memory pressure (such as freeing up texture caches perhaps) so
we shouldn't just automatically abort like we do for trivial heap
allocations.

These public functions now take a CoglError argument so applications can
catch out of memory errors:

cogl_buffer_map
cogl_buffer_map_range
cogl_buffer_set_data
cogl_framebuffer_read_pixels_into_bitmap
cogl_pixel_buffer_new
cogl_texture_new_from_data
cogl_texture_new_from_bitmap

Note: we've been quite conservative with how many apis we let throw OOM
CoglErrors since we don't really want to put a burdon on developers to
be checking for errors with every cogl api call. So long as there is
some lower level api for apps to use that let them catch OOM errors
for everything necessary that's enough and we don't have to make more
convenient apis more awkward to use.

The main focus is on bitmaps and texture allocations since they
can be particularly large and prone to failing.

A new cogl_attribute_buffer_new_with_size() function has been added in
case developers need to catch OOM errors when allocating attribute buffers
whereby they can first use _buffer_new_with_size() (which doesn't take a
CoglError) followed by cogl_buffer_set_data() which will lazily allocate
the buffer storage and report OOM errors.

Reviewed-by: Neil Roberts <neil@linux.intel.com>

(cherry picked from commit f7735e141ad537a253b02afa2a8238f96340b978)

Note: since we can't break the API for Cogl 1.x then actually the main
purpose of cherry picking this patch is to keep in-line with changes
on the master branch so that we can easily cherry-pick patches.

All the api changes relating stable apis released on the 1.12 branch
have been reverted as part of cherry-picking this patch so this most
just applies all the internal plumbing changes that enable us to
correctly propagate OOM errors.
This commit is contained in:
Robert Bragg
2012-11-08 17:54:10 +00:00
parent 63db64f426
commit f53fb5e2e0
45 changed files with 1920 additions and 969 deletions

View File

@ -83,7 +83,8 @@ malloc_map_range (CoglBuffer *buffer,
size_t offset,
size_t size,
CoglBufferAccess access,
CoglBufferMapHint hints)
CoglBufferMapHint hints,
CoglError **error)
{
buffer->flags |= COGL_BUFFER_FLAG_MAPPED;
return buffer->data + offset;
@ -96,10 +97,11 @@ malloc_unmap (CoglBuffer *buffer)
}
static CoglBool
malloc_set_data (CoglBuffer *buffer,
unsigned int offset,
const void *data,
unsigned int size)
malloc_set_data (CoglBuffer *buffer,
unsigned int offset,
const void *data,
unsigned int size,
CoglError **error)
{
memcpy (buffer->data + offset, data, size);
return TRUE;
@ -214,13 +216,28 @@ warn_about_midscene_changes (void)
}
void *
cogl_buffer_map (CoglBuffer *buffer,
CoglBufferAccess access,
CoglBufferMapHint hints)
_cogl_buffer_map (CoglBuffer *buffer,
CoglBufferAccess access,
CoglBufferMapHint hints,
CoglError **error)
{
_COGL_RETURN_VAL_IF_FAIL (cogl_is_buffer (buffer), NULL);
return cogl_buffer_map_range (buffer, 0, buffer->size, access, hints);
return cogl_buffer_map_range (buffer, 0, buffer->size, access, hints, error);
}
void *
cogl_buffer_map (CoglBuffer *buffer,
CoglBufferAccess access,
CoglBufferMapHint hints)
{
CoglError *ignore_error = NULL;
void *ptr =
cogl_buffer_map_range (buffer, 0, buffer->size, access, hints,
&ignore_error);
if (!ptr)
cogl_error_free (ignore_error);
return ptr;
}
void *
@ -228,21 +245,21 @@ cogl_buffer_map_range (CoglBuffer *buffer,
size_t offset,
size_t size,
CoglBufferAccess access,
CoglBufferMapHint hints)
CoglBufferMapHint hints,
CoglError **error)
{
_COGL_RETURN_VAL_IF_FAIL (cogl_is_buffer (buffer), NULL);
_COGL_RETURN_VAL_IF_FAIL (!(buffer->flags & COGL_BUFFER_FLAG_MAPPED), NULL);
if (G_UNLIKELY (buffer->immutable_ref))
warn_about_midscene_changes ();
if (buffer->flags & COGL_BUFFER_FLAG_MAPPED)
return buffer->data;
buffer->data = buffer->vtable.map_range (buffer,
offset,
size,
access,
hints);
hints,
error);
return buffer->data;
}
@ -272,6 +289,7 @@ _cogl_buffer_map_range_for_fill_or_fallback (CoglBuffer *buffer,
{
CoglContext *ctx = buffer->context;
void *ret;
CoglError *ignore_error = NULL;
_COGL_RETURN_VAL_IF_FAIL (!ctx->buffer_map_fallback_in_use, NULL);
@ -281,23 +299,24 @@ _cogl_buffer_map_range_for_fill_or_fallback (CoglBuffer *buffer,
offset,
size,
COGL_BUFFER_ACCESS_WRITE,
COGL_BUFFER_MAP_HINT_DISCARD);
COGL_BUFFER_MAP_HINT_DISCARD,
&ignore_error);
if (ret)
return ret;
else
{
/* If the map fails then we'll use a temporary buffer to fill
the data and then upload it using cogl_buffer_set_data when
the buffer is unmapped. The temporary buffer is shared to
avoid reallocating it every time */
g_byte_array_set_size (ctx->buffer_map_fallback_array, size);
ctx->buffer_map_fallback_offset = offset;
buffer->flags |= COGL_BUFFER_FLAG_MAPPED_FALLBACK;
cogl_error_free (ignore_error);
return ctx->buffer_map_fallback_array->data;
}
/* If the map fails then we'll use a temporary buffer to fill
the data and then upload it using cogl_buffer_set_data when
the buffer is unmapped. The temporary buffer is shared to
avoid reallocating it every time */
g_byte_array_set_size (ctx->buffer_map_fallback_array, size);
ctx->buffer_map_fallback_offset = offset;
buffer->flags |= COGL_BUFFER_FLAG_MAPPED_FALLBACK;
return ctx->buffer_map_fallback_array->data;
}
void
@ -311,29 +330,59 @@ _cogl_buffer_unmap_for_fill_or_fallback (CoglBuffer *buffer)
if ((buffer->flags & COGL_BUFFER_FLAG_MAPPED_FALLBACK))
{
cogl_buffer_set_data (buffer,
ctx->buffer_map_fallback_offset,
ctx->buffer_map_fallback_array->data,
ctx->buffer_map_fallback_array->len);
/* Note: don't try to catch OOM errors here since the use cases
* we currently have for this api (the journal and path stroke
* tesselator) don't have anything particularly sensible they
* can do in response to a failure anyway so it seems better to
* simply abort instead.
*
* If we find this is a problem for real world applications
* then in the path tesselation case we could potentially add an
* explicit cogl_path_tesselate_stroke() api that can throw an
* error for the app to cache. For the journal we could
* potentially flush the journal in smaller batches so we use
* smaller buffers, though that would probably not help for
* deferred renderers.
*/
_cogl_buffer_set_data (buffer,
ctx->buffer_map_fallback_offset,
ctx->buffer_map_fallback_array->data,
ctx->buffer_map_fallback_array->len,
NULL);
buffer->flags &= ~COGL_BUFFER_FLAG_MAPPED_FALLBACK;
}
else
cogl_buffer_unmap (buffer);
}
CoglBool
_cogl_buffer_set_data (CoglBuffer *buffer,
size_t offset,
const void *data,
size_t size,
CoglError **error)
{
_COGL_RETURN_VAL_IF_FAIL (cogl_is_buffer (buffer), FALSE);
_COGL_RETURN_VAL_IF_FAIL ((offset + size) <= buffer->size, FALSE);
if (G_UNLIKELY (buffer->immutable_ref))
warn_about_midscene_changes ();
return buffer->vtable.set_data (buffer, offset, data, size, error);
}
CoglBool
cogl_buffer_set_data (CoglBuffer *buffer,
size_t offset,
const void *data,
size_t size)
{
_COGL_RETURN_VAL_IF_FAIL (cogl_is_buffer (buffer), FALSE);
_COGL_RETURN_VAL_IF_FAIL ((offset + size) <= buffer->size, FALSE);
if (G_UNLIKELY (buffer->immutable_ref))
warn_about_midscene_changes ();
return buffer->vtable.set_data (buffer, offset, data, size);
CoglError *ignore_error = NULL;
CoglBool status =
_cogl_buffer_set_data (buffer, offset, data, size, &ignore_error);
if (!status)
cogl_error_free (ignore_error);
return status;
}
CoglBuffer *