From 39c6bf59cbb571538acbda51714375f0d65098b9 Mon Sep 17 00:00:00 2001 From: Neil Roberts Date: Thu, 23 Feb 2012 17:42:01 +0000 Subject: [PATCH] texture: Handle premult conversions when getting texture data cogl_texture_get_data uses find_best_gl_get_data_format from the texture driver which returns the closest format to use for retrieving pixel data given an intended format. However this function doesn't know about the texture we are reading data from so it doesn't know that the data we will actually receive will have the same premult status as the texture's format. With the GL driver, this function ends up returning exactly the same format as passed in which means it will never do a premult conversion. Under GLES it always returns COGL_PIXEL_FORMAT_RGBA_8888 so it will always make the data unpremult even if the final requested format is premultiplied. This patch fixes it so that it copies the premult status of the closest_format from the format of the underlying texture. That way it will later convert or not depending on the requested target format. Note this patch breaks test-sub-texture with the GL driver because that is incorrectly trying to read the texture data back as RGBA_8888 even though it depends on it not doing a premult conversion. The test was already broken with GLES2 and remains broken. Reviewed-by: Robert Bragg --- cogl/cogl-texture.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cogl/cogl-texture.c b/cogl/cogl-texture.c index 9cb3a6af7..79f507095 100644 --- a/cogl/cogl-texture.c +++ b/cogl/cogl-texture.c @@ -1198,14 +1198,17 @@ cogl_texture_get_data (CoglTexture *texture, int y; int tex_width; int tex_height; + CoglPixelFormat texture_format; CoglTextureGetData tg_data; _COGL_GET_CONTEXT (ctx, 0); + texture_format = cogl_texture_get_format (texture); + /* Default to internal format if none specified */ if (format == COGL_PIXEL_FORMAT_ANY) - format = cogl_texture_get_format (texture); + format = texture_format; tex_width = cogl_texture_get_width (texture); tex_height = cogl_texture_get_height (texture); @@ -1226,6 +1229,12 @@ cogl_texture_get_data (CoglTexture *texture, &closest_gl_type); closest_bpp = _cogl_pixel_format_get_bytes_per_pixel (closest_format); + /* We can assume that whatever data GL gives us will have the + premult status of the original texture */ + if ((closest_format & COGL_A_BIT)) + closest_format = ((closest_format & ~COGL_PREMULT_BIT) | + (texture_format & COGL_PREMULT_BIT)); + /* Is the requested format supported? */ if (closest_format == format) /* Target user data directly */