diff --git a/cogl/cogl-atlas-texture.c b/cogl/cogl-atlas-texture.c index 4cc76b4d6..f84594762 100644 --- a/cogl/cogl-atlas-texture.c +++ b/cogl/cogl-atlas-texture.c @@ -949,15 +949,8 @@ _cogl_atlas_texture_new_from_bitmap (CoglHandle bmp_handle, COGL_NOTE (ATLAS, "Adding texture of size %ix%i", bmp->width, bmp->height); - if (!_cogl_texture_prepare_for_upload (bmp, - internal_format, - &internal_format, - NULL, - NULL, - &gl_intformat, - &gl_format, - &gl_type)) - return COGL_INVALID_HANDLE; + internal_format = _cogl_texture_determine_internal_format (bmp->format, + internal_format); /* If the texture is in a strange format then we can't use it */ if (internal_format != COGL_PIXEL_FORMAT_RGB_888 && diff --git a/cogl/cogl-texture-private.h b/cogl/cogl-texture-private.h index 857e3f6a8..920618529 100644 --- a/cogl/cogl-texture-private.h +++ b/cogl/cogl-texture-private.h @@ -150,6 +150,13 @@ _cogl_texture_ensure_mipmaps (CoglHandle handle); void _cogl_texture_ensure_non_quad_rendering (CoglHandle handle); +/* Utility function to determine which pixel format to use when + dst_format is COGL_PIXEL_FORMAT_ANY. If dst_format is not ANY then + it will just be returned directly */ +CoglPixelFormat +_cogl_texture_determine_internal_format (CoglPixelFormat src_format, + CoglPixelFormat dst_format); + /* Utility function to help uploading a bitmap. If the bitmap needs premult conversion then it will be copied and *copied_bitmap will be set to TRUE. Otherwise dst_bmp will be set to a shallow copy of diff --git a/cogl/cogl-texture.c b/cogl/cogl-texture.c index 7166e5406..da55fd004 100644 --- a/cogl/cogl-texture.c +++ b/cogl/cogl-texture.c @@ -100,6 +100,36 @@ cogl_texture_unref (CoglHandle handle) cogl_handle_unref (handle); } +static gboolean +_cogl_texture_needs_premult_conversion (CoglPixelFormat src_format, + CoglPixelFormat dst_format) +{ + return ((src_format & COGL_A_BIT) && + src_format != COGL_PIXEL_FORMAT_A_8 && + (src_format & COGL_PREMULT_BIT) != + (dst_format & COGL_PREMULT_BIT)); +} + +CoglPixelFormat +_cogl_texture_determine_internal_format (CoglPixelFormat src_format, + CoglPixelFormat dst_format) +{ + /* If the application hasn't specified a specific format then we'll + * pick the most appropriate. By default Cogl will use a + * premultiplied internal format. Later we will add control over + * this. */ + if (dst_format == COGL_PIXEL_FORMAT_ANY) + { + if ((src_format & COGL_A_BIT) && + src_format != COGL_PIXEL_FORMAT_A_8) + return src_format | COGL_PREMULT_BIT; + else + return src_format; + } + else + return dst_format; +} + gboolean _cogl_texture_prepare_for_upload (CoglBitmap *src_bmp, CoglPixelFormat dst_format, @@ -110,42 +140,27 @@ _cogl_texture_prepare_for_upload (CoglBitmap *src_bmp, GLenum *out_glformat, GLenum *out_gltype) { - /* If the application hasn't specified a specific format then we'll - * pick the most appropriate. By default Cogl will use a - * premultiplied internal format. Later we will add control over - * this. */ - if (dst_format == COGL_PIXEL_FORMAT_ANY) - { - if ((src_bmp->format & COGL_A_BIT) && - src_bmp->format != COGL_PIXEL_FORMAT_A_8) - dst_format = src_bmp->format | COGL_PREMULT_BIT; - else - dst_format = src_bmp->format; - } + dst_format = _cogl_texture_determine_internal_format (src_bmp->format, + dst_format); - if (dst_bmp) - { - *copied_bitmap = FALSE; - *dst_bmp = *src_bmp; + *copied_bitmap = FALSE; + *dst_bmp = *src_bmp; - /* If the source format does not have the same premult flag as the - dst format then we need to copy and convert it */ - if ((src_bmp->format & COGL_A_BIT) && - src_bmp->format != COGL_PIXEL_FORMAT_A_8 && - (src_bmp->format & COGL_PREMULT_BIT) != - (dst_format & COGL_PREMULT_BIT)) + /* If the source format does not have the same premult flag as the + dst format then we need to copy and convert it */ + if (_cogl_texture_needs_premult_conversion (src_bmp->format, + dst_format)) + { + dst_bmp->data = g_memdup (dst_bmp->data, + dst_bmp->height * dst_bmp->rowstride); + *copied_bitmap = TRUE; + + if (!_cogl_bitmap_convert_premult_status (dst_bmp, + src_bmp->format ^ + COGL_PREMULT_BIT)) { - dst_bmp->data = g_memdup (dst_bmp->data, - dst_bmp->height * dst_bmp->rowstride); - *copied_bitmap = TRUE; - - if (!_cogl_bitmap_convert_premult_status (dst_bmp, - src_bmp->format ^ - COGL_PREMULT_BIT)) - { - g_free (dst_bmp->data); - return FALSE; - } + g_free (dst_bmp->data); + return FALSE; } }