[Cogl] [PATCH] bitmap: ret CoglError from _new_with_malloc_buffer

Robert Bragg robert at sixbynine.org
Mon Nov 19 08:33:20 PST 2012


From: Robert Bragg <robert at linux.intel.com>

_cogl_bitmap_new_with_malloc_buffer() now takes a CoglError for throwing
exceptional errors and all callers have been updated to pass through
any application error pointer as appropriate.
---
 cogl/cogl-bitmap-conversion.c                  |    5 +++-
 cogl/cogl-bitmap-private.h                     |    4 ++-
 cogl/cogl-bitmap.c                             |   19 +++++++++++++--
 cogl/cogl-framebuffer.c                        |    6 ++++-
 cogl/cogl-texture-3d.c                         |    5 +++-
 cogl/cogl-texture.c                            |   28 +++++++++++++++++++----
 cogl/driver/gl/gles/cogl-texture-driver-gles.c |   11 +++++++-
 7 files changed, 64 insertions(+), 14 deletions(-)

diff --git a/cogl/cogl-bitmap-conversion.c b/cogl/cogl-bitmap-conversion.c
index 3ce8c71..ccc32cd 100644
--- a/cogl/cogl-bitmap-conversion.c
+++ b/cogl/cogl-bitmap-conversion.c
@@ -487,7 +487,10 @@ _cogl_bitmap_convert (CoglBitmap *src_bmp,
 
   dst_bmp = _cogl_bitmap_new_with_malloc_buffer (ctx,
                                                  width, height,
-                                                 dst_format);
+                                                 dst_format,
+                                                 error);
+  if (!dst_bmp)
+    return NULL;
 
   if (!_cogl_bitmap_convert_into_bitmap (src_bmp, dst_bmp, error))
     {
diff --git a/cogl/cogl-bitmap-private.h b/cogl/cogl-bitmap-private.h
index ac1fd4b..ae96aaf 100644
--- a/cogl/cogl-bitmap-private.h
+++ b/cogl/cogl-bitmap-private.h
@@ -69,6 +69,7 @@ struct _CoglBitmap
  * @width: width of the bitmap in pixels
  * @height: height of the bitmap in pixels
  * @format: the format of the pixels the array will store
+ * @error: A #CoglError for catching exceptional errors or %NULL
  *
  * This is equivalent to cogl_bitmap_new_with_size() except that it
  * allocated the buffer using g_malloc() instead of creating a
@@ -84,7 +85,8 @@ CoglBitmap *
 _cogl_bitmap_new_with_malloc_buffer (CoglContext *context,
                                      unsigned int width,
                                      unsigned int height,
-                                     CoglPixelFormat format);
+                                     CoglPixelFormat format,
+                                     CoglError **error);
 
 /* The idea of this function is that it will create a bitmap that
    shares the actual data with another bitmap. This is needed for the
diff --git a/cogl/cogl-bitmap.c b/cogl/cogl-bitmap.c
index 3df6c9d..fa7f711 100644
--- a/cogl/cogl-bitmap.c
+++ b/cogl/cogl-bitmap.c
@@ -89,7 +89,10 @@ _cogl_bitmap_copy (CoglBitmap *src_bmp,
   dst_bmp =
     _cogl_bitmap_new_with_malloc_buffer (src_bmp->context,
                                          width, height,
-                                         src_format);
+                                         src_format,
+                                         error);
+  if (!dst_bmp)
+    return NULL;
 
   if (!_cogl_bitmap_copy_subregion (src_bmp,
                                     dst_bmp,
@@ -194,14 +197,24 @@ CoglBitmap *
 _cogl_bitmap_new_with_malloc_buffer (CoglContext *context,
                                      unsigned int width,
                                      unsigned int height,
-                                     CoglPixelFormat format)
+                                     CoglPixelFormat format,
+                                     CoglError **error)
 {
   static CoglUserDataKey bitmap_free_key;
   int bpp = _cogl_pixel_format_get_bytes_per_pixel (format);
   int rowstride = ((width * bpp) + 3) & ~3;
-  uint8_t *data = g_malloc (rowstride * height);
+  uint8_t *data = g_try_malloc (rowstride * height);
   CoglBitmap *bitmap;
 
+  if (!data)
+    {
+      _cogl_set_error (error,
+                       COGL_SYSTEM_ERROR,
+                       COGL_SYSTEM_ERROR_NO_MEMORY,
+                       "Failed to allocate memory for bitmap");
+      return NULL;
+    }
+
   bitmap = cogl_bitmap_new_for_data (context,
                                      width, height,
                                      format,
diff --git a/cogl/cogl-framebuffer.c b/cogl/cogl-framebuffer.c
index f98a0bb..53e4b65 100644
--- a/cogl/cogl-framebuffer.c
+++ b/cogl/cogl-framebuffer.c
@@ -1593,7 +1593,11 @@ cogl_framebuffer_read_pixels_into_bitmap (CoglFramebuffer *framebuffer,
 
       tmp_bmp = _cogl_bitmap_new_with_malloc_buffer (ctx,
                                                      width, height,
-                                                     read_format);
+                                                     read_format,
+                                                     error);
+      if (!tmp_bmp)
+        goto EXIT;
+
       bpp = _cogl_pixel_format_get_bytes_per_pixel (read_format);
       rowstride = cogl_bitmap_get_rowstride (tmp_bmp);
 
diff --git a/cogl/cogl-texture-3d.c b/cogl/cogl-texture-3d.c
index 964ead3..d023847 100644
--- a/cogl/cogl-texture-3d.c
+++ b/cogl/cogl-texture-3d.c
@@ -403,7 +403,10 @@ cogl_texture_3d_new_from_data (CoglContext *context,
       bitmap = _cogl_bitmap_new_with_malloc_buffer (context,
                                                     width,
                                                     depth * height,
-                                                    format);
+                                                    format,
+                                                    error);
+      if (!bitmap)
+        return NULL;
 
       bmp_data = _cogl_bitmap_map (bitmap,
                                    COGL_BUFFER_ACCESS_WRITE,
diff --git a/cogl/cogl-texture.c b/cogl/cogl-texture.c
index 87f9ca9..3b504d6 100644
--- a/cogl/cogl-texture.c
+++ b/cogl/cogl-texture.c
@@ -542,7 +542,10 @@ do_texture_draw_and_read (CoglFramebuffer *fb,
           rect_bmp = _cogl_bitmap_new_with_malloc_buffer
                                               (ctx,
                                                width, height,
-                                               COGL_PIXEL_FORMAT_RGBA_8888_PRE);
+                                               COGL_PIXEL_FORMAT_RGBA_8888_PRE,
+                                               error);
+          if (!rect_bmp)
+            return FALSE;
 
           if (!cogl_framebuffer_read_pixels_into_bitmap
                                                (fb,
@@ -670,7 +673,14 @@ _cogl_texture_draw_and_read (CoglTexture *texture,
         _cogl_bitmap_new_with_malloc_buffer (ctx,
                                              target_width,
                                              target_height,
-                                             COGL_PIXEL_FORMAT_RGBA_8888);
+                                             COGL_PIXEL_FORMAT_RGBA_8888,
+                                             error);
+      if (!alpha_bmp)
+        {
+          _cogl_bitmap_unmap (target_bmp);
+          goto EXIT;
+        }
+
 
       /* Draw alpha values into RGB channels */
       cogl_pipeline_set_layer_combine (ctx->texture_download_pipeline,
@@ -967,9 +977,17 @@ cogl_texture_get_data (CoglTexture *texture,
                                            rowstride,
                                            data);
   else
-    target_bmp = _cogl_bitmap_new_with_malloc_buffer (ctx,
-                                                      tex_width, tex_height,
-                                                      closest_format);
+    {
+      target_bmp = _cogl_bitmap_new_with_malloc_buffer (ctx,
+                                                        tex_width, tex_height,
+                                                        closest_format,
+                                                        &ignore_error);
+      if (!target_bmp)
+        {
+          cogl_error_free (ignore_error);
+          return 0;
+        }
+    }
 
   tg_data.target_bits = _cogl_bitmap_map (target_bmp, COGL_BUFFER_ACCESS_WRITE,
                                           COGL_BUFFER_MAP_HINT_DISCARD,
diff --git a/cogl/driver/gl/gles/cogl-texture-driver-gles.c b/cogl/driver/gl/gles/cogl-texture-driver-gles.c
index dea64d5..d8363d6 100644
--- a/cogl/driver/gl/gles/cogl-texture-driver-gles.c
+++ b/cogl/driver/gl/gles/cogl-texture-driver-gles.c
@@ -206,7 +206,11 @@ _cogl_texture_driver_upload_subregion_to_gl (CoglContext *ctx,
       slice_bmp =
         _cogl_bitmap_new_with_malloc_buffer (ctx,
                                              width, height,
-                                             source_format);
+                                             source_format,
+                                             error);
+      if (!slice_bmp)
+        return FALSE;
+
       if (!_cogl_bitmap_copy_subregion (source_bmp,
                                         slice_bmp,
                                         src_x, src_y,
@@ -388,7 +392,10 @@ _cogl_texture_driver_upload_to_gl_3d (CoglContext *ctx,
       bmp = _cogl_bitmap_new_with_malloc_buffer (ctx,
                                                  bmp_width,
                                                  height,
-                                                 source_bmp_format);
+                                                 source_bmp_format,
+                                                 error);
+      if (!bmp)
+        return FALSE;
 
       for (i = 0; i < depth; i++)
         {
-- 
1.7.7.6



More information about the Cogl mailing list