cogl-bitmap: Encapsulate the CoglBitmap even internally

The CoglBitmap struct is now only defined within cogl-bitmap.c so that
all of its members can now only be accessed with accessor
functions. To get to the data pointer for the bitmap image you must
first call _cogl_bitmap_map and later call _cogl_bitmap_unmap. The map
function takes the same arguments as cogl_pixel_array_map so that
eventually we can make a bitmap optionally internally divert to a
pixel array.

There is a _cogl_bitmap_new_from_data function which constructs a new
bitmap object and takes ownership of the data pointer. The function
gets passed a destroy callback which gets called when the bitmap is
freed. This is similar to how gdk_pixbuf_new_from_data
works. Alternatively NULL can be passed for the destroy function which
means that the caller will manage the life of the pointer (but must
guarantee that it stays alive at least until the bitmap is
freed). This mechanism is used instead of the old approach of creating
a CoglBitmap struct on the stack and manually filling in the
members. It could also later be used to create a CoglBitmap that owns
a GdkPixbuf ref so that we don't necessarily have to copy the
GdkPixbuf data when converting to a bitmap.

There is also _cogl_bitmap_new_shared. This creates a bitmap using a
reference to another CoglBitmap for the data. This is a bit of a hack
but it is needed by the atlas texture backend which wants to divert
the set_region virtual to another texture but it needs to override the
format of the bitmap to ignore the premult flag.
This commit is contained in:
Neil Roberts
2010-07-07 18:44:16 +01:00
parent 41cd2ae2c8
commit ccc3068ffd
19 changed files with 1415 additions and 914 deletions

View File

@ -55,12 +55,11 @@ _cogl_bitmap_can_premult (CoglPixelFormat format)
return FALSE;
}
gboolean
_cogl_bitmap_convert (const CoglBitmap *bmp,
CoglBitmap *dst_bmp,
CoglBitmap *
_cogl_bitmap_convert (CoglBitmap *bmp,
CoglPixelFormat dst_format)
{
return FALSE;
return NULL;
}
gboolean
@ -92,12 +91,10 @@ _cogl_bitmap_get_size_from_file (const char *filename,
}
/* the error does not contain the filename as the caller already has it */
gboolean
_cogl_bitmap_from_file (CoglBitmap *bmp,
const char *filename,
CoglBitmap *
_cogl_bitmap_from_file (const char *filename,
GError **error)
{
g_assert (bmp != NULL);
g_assert (filename != NULL);
g_assert (error == NULL || *error == NULL);
@ -110,7 +107,7 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
/* doesn't exist, not readable, etc. */
g_set_error (error, COGL_BITMAP_ERROR, COGL_BITMAP_ERROR_FAILED,
"%s", g_strerror (save_errno));
return FALSE;
return NULL;
}
/* Unknown images would be cleanly caught as zero width/height below, but try
@ -122,7 +119,7 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
CFRelease (image_source);
g_set_error (error, COGL_BITMAP_ERROR, COGL_BITMAP_ERROR_UNKNOWN_TYPE,
"Unknown image type");
return FALSE;
return NULL;
}
CFRelease (type);
@ -137,7 +134,7 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
CFRelease (image);
g_set_error (error, COGL_BITMAP_ERROR, COGL_BITMAP_ERROR_CORRUPT_IMAGE,
"Image has zero width or height");
return FALSE;
return NULL;
}
/* allocate buffer big enough to hold pixel data */
@ -160,13 +157,12 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
CGContextRelease (bitmap_context);
/* store bitmap info */
bmp->data = out_data;
bmp->format = COGL_PIXEL_FORMAT_ARGB_8888;
bmp->width = width;
bmp->height = height;
bmp->rowstride = rowstride;
return TRUE;
return _cogl_bitmap_new_from_data (out_data,
COGL_PIXEL_FORMAT_ARGB_8888,
width, height,
rowstride,
(CoglBitmapDestroyNotify) g_free,
NULL);
}
#elif defined(USE_GDKPIXBUF)
@ -184,9 +180,8 @@ _cogl_bitmap_get_size_from_file (const char *filename,
return FALSE;
}
gboolean
_cogl_bitmap_from_file (CoglBitmap *bmp,
const char *filename,
CoglBitmap *
_cogl_bitmap_from_file (const char *filename,
GError **error)
{
GdkPixbuf *pixbuf;
@ -206,9 +201,6 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (bmp == NULL)
return FALSE;
/* Load from file using GdkPixbuf */
pixbuf = gdk_pixbuf_new_from_file (filename, error);
if (pixbuf == NULL)
@ -272,16 +264,16 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
g_object_unref (pixbuf);
/* Store bitmap info */
bmp->data = out_data; /* The stored data the same alignment constraints as a
* gdkpixbuf but stores a full rowstride in the last
* scanline
*/
bmp->format = pixel_format;
bmp->width = width;
bmp->height = height;
bmp->rowstride = rowstride;
return TRUE;
/* The stored data the same alignment constraints as a gdkpixbuf but
* stores a full rowstride in the last scanline
*/
return _cogl_bitmap_new_from_data (out_data,
pixel_format,
width,
height,
rowstride,
(CoglBitmapDestroyNotify) g_free,
NULL);
}
#else
@ -302,11 +294,11 @@ _cogl_bitmap_get_size_from_file (const char *filename,
return TRUE;
}
gboolean
_cogl_bitmap_from_file (CoglBitmap *bmp,
const char *filename,
CoglBitmap *
_cogl_bitmap_from_file (const char *filename,
GError **error)
{
CoglBitmap *bmp;
int stb_pixel_format;
int width;
int height;
@ -314,9 +306,6 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (bmp == NULL)
return FALSE;
/* Load from file using stb */
pixels = stbi_load (filename,
&width, &height, &stb_pixel_format,
@ -325,14 +314,15 @@ _cogl_bitmap_from_file (CoglBitmap *bmp,
return FALSE;
/* Store bitmap info */
bmp->data = g_memdup (pixels, height * width * 4);
bmp->format = COGL_PIXEL_FORMAT_RGBA_8888;
bmp->width = width;
bmp->height = height;
bmp->rowstride = width * 4;
bmp = _cogl_bitmap_new_from_data (g_memdup (pixels, height * width * 4),
COGL_PIXEL_FORMAT_RGBA_8888,
width, height,
width * 4,
(CoglBitmapDestroyNotify) g_free,
NULL);
free (pixels);
return TRUE;
return bmp;
}
#endif