Add new pixman_image_create_bits_no_clear() API
authorSøren Sandmann Pedersen <ssp@redhat.com>
Fri, 12 Oct 2012 22:07:29 +0000 (18:07 -0400)
committerSøren Sandmann Pedersen <ssp@redhat.com>
Sun, 21 Oct 2012 08:13:36 +0000 (04:13 -0400)
When pixman_image_create_bits() function is given NULL for bits, it
will allocate a new buffer and initialize it to zero. However, in some
cases, only a small region of the image is actually used; in that case
it is wasteful to touch all of the memory.

The new pixman_image_create_bits_no_clear() works exactly like
_create_bits() except that it doesn't initialize any newly allocated
memory.

pixman/pixman-bits-image.c
pixman/pixman-fast-path.c
pixman/pixman-private.h
pixman/pixman.h

index 029093d..085dd16 100644 (file)
@@ -1388,7 +1388,8 @@ static uint32_t *
 create_bits (pixman_format_code_t format,
              int                  width,
              int                  height,
-             int *               rowstride_bytes)
+             int *               rowstride_bytes,
+            pixman_bool_t        clear)
 {
     int stride;
     size_t buf_size;
@@ -1420,7 +1421,10 @@ create_bits (pixman_format_code_t format,
     if (rowstride_bytes)
        *rowstride_bytes = stride;
 
-    return calloc (buf_size, 1);
+    if (clear)
+       return calloc (buf_size, 1);
+    else
+       return malloc (buf_size);
 }
 
 pixman_bool_t
@@ -1429,7 +1433,8 @@ _pixman_bits_image_init (pixman_image_t *     image,
                          int                  width,
                          int                  height,
                          uint32_t *           bits,
-                         int                  rowstride)
+                         int                  rowstride,
+                        pixman_bool_t        clear)
 {
     uint32_t *free_me = NULL;
 
@@ -1437,7 +1442,7 @@ _pixman_bits_image_init (pixman_image_t *     image,
     {
        int rowstride_bytes;
 
-       free_me = bits = create_bits (format, width, height, &rowstride_bytes);
+       free_me = bits = create_bits (format, width, height, &rowstride_bytes, clear);
 
        if (!bits)
            return FALSE;
@@ -1465,12 +1470,13 @@ _pixman_bits_image_init (pixman_image_t *     image,
     return TRUE;
 }
 
-PIXMAN_EXPORT pixman_image_t *
-pixman_image_create_bits (pixman_format_code_t format,
-                          int                  width,
-                          int                  height,
-                          uint32_t *           bits,
-                          int                  rowstride_bytes)
+static pixman_image_t *
+create_bits_image_internal (pixman_format_code_t format,
+                           int                  width,
+                           int                  height,
+                           uint32_t *           bits,
+                           int                  rowstride_bytes,
+                           pixman_bool_t        clear)
 {
     pixman_image_t *image;
 
@@ -1487,7 +1493,8 @@ pixman_image_create_bits (pixman_format_code_t format,
        return NULL;
 
     if (!_pixman_bits_image_init (image, format, width, height, bits,
-                                 rowstride_bytes / (int) sizeof (uint32_t)))
+                                 rowstride_bytes / (int) sizeof (uint32_t),
+                                 clear))
     {
        free (image);
        return NULL;
@@ -1495,3 +1502,28 @@ pixman_image_create_bits (pixman_format_code_t format,
 
     return image;
 }
+
+/* If bits is NULL, a buffer will be allocated and initialized to 0 */
+PIXMAN_EXPORT pixman_image_t *
+pixman_image_create_bits (pixman_format_code_t format,
+                          int                  width,
+                          int                  height,
+                          uint32_t *           bits,
+                          int                  rowstride_bytes)
+{
+    return create_bits_image_internal (
+       format, width, height, bits, rowstride_bytes, TRUE);
+}
+
+
+/* If bits is NULL, a buffer will be allocated and _not_ initialized */
+PIXMAN_EXPORT pixman_image_t *
+pixman_image_create_bits_no_clear (pixman_format_code_t format,
+                                  int                  width,
+                                  int                  height,
+                                  uint32_t *           bits,
+                                  int                  rowstride_bytes)
+{
+    return create_bits_image_internal (
+       format, width, height, bits, rowstride_bytes, FALSE);
+}
index a12c6cf..d95cb4d 100644 (file)
@@ -1296,7 +1296,8 @@ fast_composite_tiled_repeat (pixman_implementation_t *imp,
 
            /* Initialize/validate stack-allocated temporary image */
            _pixman_bits_image_init (&extended_src_image, src_image->bits.format,
-                                    src_width, 1, &extended_src[0], src_stride);
+                                    src_width, 1, &extended_src[0], src_stride,
+                                    FALSE);
            _pixman_image_validate (&extended_src_image);
 
            info2.src_image = &extended_src_image;
index dd03a93..c0a6bc0 100644 (file)
@@ -283,7 +283,8 @@ _pixman_bits_image_init (pixman_image_t *     image,
                          int                  width,
                          int                  height,
                          uint32_t *           bits,
-                         int                  rowstride);
+                         int                  rowstride,
+                        pixman_bool_t        clear);
 pixman_bool_t
 _pixman_image_fini (pixman_image_t *image);
 
index 1dc167a..c8723cf 100644 (file)
@@ -757,6 +757,11 @@ pixman_image_t *pixman_image_create_bits             (pixman_format_code_t
                                                      int                           height,
                                                      uint32_t                     *bits,
                                                      int                           rowstride_bytes);
+pixman_image_t *pixman_image_create_bits_no_clear    (pixman_format_code_t format,
+                                                     int                  width,
+                                                     int                  height,
+                                                     uint32_t *           bits,
+                                                     int                  rowstride_bytes);
 
 /* Destructor */
 pixman_image_t *pixman_image_ref                     (pixman_image_t               *image);