Commit 6d6a277b authored by Robert Bragg's avatar Robert Bragg

Make cogl_texture_draw_and_read_to_bitmap public

This makes the internal api  _cogl_texture_draw_and_read public as
cogl_texture_draw_and_read_to_bitmap() so that applications can manually
use this api to handle fallbacks when it's not possible to directly read
a textures contents.

The intention is to no longer automatically render and read-back
textures as a fallback on drivers that don't support glGetTexImage2D
since this fallback depends on the idea of a current framebuffer which
we'd like to get rid of and there is no feedback that the contents of
the framebuffer have been destroyed if the fallback is hit.
Reviewed-by: 's avatarNeil Roberts <>
parent 507814d2
......@@ -32,6 +32,11 @@
#include <windows.h>
#endif /* COGL_HAS_WIN32_SUPPORT */
/* We forward declare the CoglFramebuffer type here to avoid some circular
* dependency issues with the following headers.
typedef struct _CoglFramebuffer CoglFramebuffer;
#include <cogl/cogl-path.h>
#include <cogl/cogl-pipeline.h>
#include <cogl/cogl-indices.h>
......@@ -80,8 +85,6 @@ COGL_BEGIN_DECLS
* configuration.
typedef struct _CoglFramebuffer CoglFramebuffer;
#define COGL_FRAMEBUFFER(X) ((CoglFramebuffer *)(X))
......@@ -587,20 +587,12 @@ do_texture_draw_and_read (CoglFramebuffer *fb,
return TRUE;
/* Reads back the contents of a texture by rendering it to the framebuffer
* and reading back the resulting pixels.
* NB: Normally this approach isn't normally used since we can just use
* glGetTexImage, but may be used as a fallback in some circumstances.
static CoglBool
_cogl_texture_draw_and_read (CoglTexture *texture,
CoglBitmap *target_bmp,
GLuint target_gl_format,
GLuint target_gl_type,
CoglError **error)
cogl_texture_draw_and_read_to_bitmap (CoglTexture *texture,
CoglFramebuffer *framebuffer,
CoglBitmap *target_bmp,
CoglError **error)
CoglFramebuffer *framebuffer = cogl_get_draw_framebuffer ();
CoglContext *ctx = framebuffer->context;
float save_viewport[4];
float viewport[4];
......@@ -1060,25 +1052,13 @@ cogl_texture_get_data (CoglTexture *texture,
tg_data.success = FALSE;
/* XXX: In some cases _cogl_texture_2d_download_from_gl may fail
* to read back the texture data; such as for GLES which doesn't
* support glGetTexImage, so here we fallback to drawing the
* texture and reading the pixels from the framebuffer. */
/* XXX: In some cases this api may fail to read back the texture
* data; such as for GLES which doesn't support glGetTexImage
if (!tg_data.success)
if (!_cogl_texture_draw_and_read (texture, target_bmp,
/* We have no more fallbacks so we just give up and
* hope for the best */
g_warning ("Failed to read texture since draw-and-read "
"fallback failed: %s", ignore_error->message);
cogl_error_free (ignore_error);
cogl_object_unref (target_bmp);
return 0;
cogl_object_unref (target_bmp);
return 0;
/* Was intermediate used? */
......@@ -37,6 +37,7 @@ typedef struct _CoglTexture CoglTexture;
#include <cogl/cogl-defines.h>
#include <cogl/cogl-pixel-buffer.h>
#include <cogl/cogl-bitmap.h>
#include <cogl/cogl-framebuffer.h>
......@@ -362,6 +363,41 @@ cogl_texture_get_data (CoglTexture *texture,
unsigned int rowstride,
uint8_t *data);
* cogl_texture_draw_and_read_to_bitmap:
* @texture: The #CoglTexture to read
* @framebuffer: The intermediate framebuffer needed to render the
* texture
* @target_bmp: The destination bitmap
* @error: A #CoglError to catch any exceptional errors
* Only to be used in exceptional circumstances, this api reads back
* the contents of a @texture by rendering it to the given
* @framebuffer and reading back the resulting pixels to be stored in
* @bitmap. If the @texture is larger than the given @framebuffer then
* multiple renders will be done to read the texture back in chunks.
* Any viewport, projection or modelview matrix state associated with
* @framebuffer will be saved and restored, but other state such as
* the color mask state is ignored and may affect the result of
* reading back the texture.
* This API should only be used in exceptional circumstances when
* alternative apis such as cogl_texture_get_data() have failed. For
* example compressed textures can not be read back directly and so
* a render is required if you want read back the image data. Ideally
* applications should aim to avoid needing to read back textures in
* the first place and perhaps only use this api for debugging
* purposes.
* Stability: unstable
cogl_texture_draw_and_read_to_bitmap (CoglTexture *texture,
CoglFramebuffer *framebuffer,
CoglBitmap *target_bmp,
CoglError **error);
* cogl_texture_set_region:
* @texture a #CoglTexture.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment