Example #1
0
gboolean
cogl_texture_set_region (CoglHandle       handle,
			 int              src_x,
			 int              src_y,
			 int              dst_x,
			 int              dst_y,
			 unsigned int     dst_width,
			 unsigned int     dst_height,
			 int              width,
			 int              height,
			 CoglPixelFormat  format,
			 unsigned int     rowstride,
			 const guint8    *data)
{
  CoglBitmap *source_bmp;
  gboolean    ret;

  /* Check for valid format */
  if (format == COGL_PIXEL_FORMAT_ANY)
    return FALSE;

  /* Rowstride from width if none specified */
  if (rowstride == 0)
    rowstride = _cogl_get_format_bpp (format) * width;

  /* Init source bitmap */
  source_bmp = _cogl_bitmap_new_from_data ((guint8 *) data,
                                           format,
                                           width,
                                           height,
                                           rowstride,
                                           NULL, /* destroy_fn */
                                           NULL); /* destroy_fn_data */

  ret = _cogl_texture_set_region_from_bitmap (handle,
                                              src_x, src_y,
                                              dst_x, dst_y,
                                              dst_width, dst_height,
                                              source_bmp);

  cogl_object_unref (source_bmp);

  return ret;
}
Example #2
0
CoglHandle
cogl_texture_new_from_data (unsigned int      width,
			    unsigned int      height,
                            CoglTextureFlags  flags,
			    CoglPixelFormat   format,
			    CoglPixelFormat   internal_format,
			    unsigned int      rowstride,
			    const guint8     *data)
{
  CoglBitmap *bmp;
  CoglHandle tex;

  if (format == COGL_PIXEL_FORMAT_ANY)
    return COGL_INVALID_HANDLE;

  if (data == NULL)
    return COGL_INVALID_HANDLE;

  /* Rowstride from width if not given */
  if (rowstride == 0)
    rowstride = width * _cogl_get_format_bpp (format);

  /* Wrap the data into a bitmap */
  bmp = _cogl_bitmap_new_from_data ((guint8 *) data,
                                    format,
                                    width,
                                    height,
                                    rowstride,
                                    NULL, NULL);

  tex = cogl_texture_new_from_bitmap (bmp, flags, internal_format);

  cogl_object_unref (bmp);

  return tex;
}
void
_cogl_texture_driver_upload_to_gl_3d (GLenum       gl_target,
                                      GLuint       gl_handle,
                                      gboolean     is_foreign,
                                      GLint        height,
                                      GLint        depth,
                                      CoglBitmap  *source_bmp,
                                      GLint        internal_gl_format,
                                      GLuint       source_gl_format,
                                      GLuint       source_gl_type)
{
    int bpp = _cogl_get_format_bpp (_cogl_bitmap_get_format (source_bmp));
    int rowstride = _cogl_bitmap_get_rowstride (source_bmp);
    int bmp_width = _cogl_bitmap_get_width (source_bmp);
    int bmp_height = _cogl_bitmap_get_height (source_bmp);
    guint8 *data;

    _COGL_GET_CONTEXT (ctx, NO_RETVAL);

    _cogl_bind_gl_texture_transient (gl_target, gl_handle, is_foreign);

    /* If the rowstride or image height can't be specified with just
       GL_ALIGNMENT alone then we need to copy the bitmap because there
       is no GL_ROW_LENGTH */
    if (rowstride / bpp != bmp_width ||
            height != bmp_height / depth)
    {
        CoglBitmap *bmp;
        int image_height = bmp_height / depth;
        int i;

        _cogl_texture_driver_prep_gl_for_pixels_upload (bmp_width * bpp, bpp);

        /* Initialize the texture with empty data and then upload each
           image with a sub-region update */

        GE( glTexImage3D (gl_target,
                          0, /* level */
                          internal_gl_format,
                          bmp_width,
                          height,
                          depth,
                          0,
                          source_gl_format,
                          source_gl_type,
                          NULL) );

        bmp = _cogl_bitmap_new_from_data (g_malloc (bpp * bmp_width * height),
                                          _cogl_bitmap_get_format (source_bmp),
                                          bmp_width,
                                          height,
                                          bpp * bmp_width,
                                          (CoglBitmapDestroyNotify) g_free,
                                          NULL);

        for (i = 0; i < depth; i++)
        {
            _cogl_bitmap_copy_subregion (source_bmp,
                                         bmp,
                                         0, image_height * i,
                                         0, 0,
                                         bmp_width,
                                         height);

            data = _cogl_bitmap_bind (bmp,
                                      COGL_BUFFER_ACCESS_READ, 0);

            GE( glTexSubImage3D (gl_target,
                                 0, /* level */
                                 0, /* xoffset */
                                 0, /* yoffset */
                                 i, /* zoffset */
                                 bmp_width, /* width */
                                 height, /* height */
                                 1, /* depth */
                                 source_gl_format,
                                 source_gl_type,
                                 data) );

            _cogl_bitmap_unbind (bmp);
        }

        cogl_object_unref (bmp);
    }
    else
    {
        data = _cogl_bitmap_bind (source_bmp, COGL_BUFFER_ACCESS_READ, 0);

        _cogl_texture_driver_prep_gl_for_pixels_upload (rowstride, bpp);

        GE( glTexImage3D (gl_target,
                          0, /* level */
                          internal_gl_format,
                          bmp_width,
                          height,
                          depth,
                          0,
                          source_gl_format,
                          source_gl_type,
                          data) );

        _cogl_bitmap_unbind (source_bmp);
    }
}
void
_cogl_texture_driver_upload_subregion_to_gl (GLenum       gl_target,
        GLuint       gl_handle,
        gboolean     is_foreign,
        int          src_x,
        int          src_y,
        int          dst_x,
        int          dst_y,
        int          width,
        int          height,
        CoglBitmap  *source_bmp,
        GLuint       source_gl_format,
        GLuint       source_gl_type)
{
    guint8 *data;
    CoglPixelFormat source_format = _cogl_bitmap_get_format (source_bmp);
    int bpp = _cogl_get_format_bpp (source_format);
    CoglBitmap *slice_bmp;
    int rowstride;

    /* If we are copying a sub region of the source bitmap then we need
       to copy it because GLES does not support GL_UNPACK_ROW_LENGTH */
    if (src_x != 0 || src_y != 0 ||
            width != _cogl_bitmap_get_width (source_bmp) ||
            height != _cogl_bitmap_get_height (source_bmp))
    {
        rowstride = bpp * width;
        rowstride = (rowstride + 3) & ~3;
        slice_bmp =
            _cogl_bitmap_new_from_data (g_malloc (height * rowstride),
                                        source_format,
                                        width, height,
                                        rowstride,
                                        (CoglBitmapDestroyNotify) g_free,
                                        NULL);
        _cogl_bitmap_copy_subregion (source_bmp,
                                     slice_bmp,
                                     src_x, src_y,
                                     0, 0, /* dst_x/y */
                                     width, height);
    }
    else
    {
        slice_bmp = prepare_bitmap_alignment_for_upload (source_bmp);
        rowstride = _cogl_bitmap_get_rowstride (slice_bmp);
    }

    /* Setup gl alignment to match rowstride and top-left corner */
    _cogl_texture_driver_prep_gl_for_pixels_upload (rowstride, bpp);

    data = _cogl_bitmap_bind (slice_bmp, COGL_BUFFER_ACCESS_READ, 0);

    _cogl_bind_gl_texture_transient (gl_target, gl_handle, is_foreign);

    GE( glTexSubImage2D (gl_target, 0,
                         dst_x, dst_y,
                         width, height,
                         source_gl_format,
                         source_gl_type,
                         data) );

    _cogl_bitmap_unbind (slice_bmp);

    cogl_object_unref (slice_bmp);
}
Example #5
0
/* Reads back the contents of a texture by rendering it to the framebuffer
 * and reading back the resulting pixels.
 *
 * It will perform multiple renders if the texture is larger than the
 * current glViewport.
 *
 * It assumes the projection and modelview have already been setup so
 * that rendering to 0,0 with the same width and height of the viewport
 * will exactly cover the viewport.
 *
 * 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 void
do_texture_draw_and_read (CoglHandle   handle,
                          CoglBitmap  *target_bmp,
                          GLint       *viewport)
{
  int         bpp;
  float       rx1, ry1;
  float       rx2, ry2;
  float       tx1, ty1;
  float       tx2, ty2;
  int         bw,  bh;
  CoglBitmap  *rect_bmp;
  unsigned int  tex_width, tex_height;

  bpp = _cogl_get_format_bpp (COGL_PIXEL_FORMAT_RGBA_8888);

  tex_width = cogl_texture_get_width (handle);
  tex_height = cogl_texture_get_height (handle);

  ry2 = 0;
  ty2 = 0;

  /* Walk Y axis until whole bitmap height consumed */
  for (bh = tex_height; bh > 0; bh -= viewport[3])
    {
      /* Rectangle Y coords */
      ry1 = ry2;
      ry2 += (bh < viewport[3]) ? bh : viewport[3];

      /* Normalized texture Y coords */
      ty1 = ty2;
      ty2 = (ry2 / (float) tex_height);

      rx2 = 0;
      tx2 = 0;

      /* Walk X axis until whole bitmap width consumed */
      for (bw = tex_width; bw > 0; bw-=viewport[2])
        {
          int width;
          int height;
          int rowstride;
          guint8 *data;

          /* Rectangle X coords */
          rx1 = rx2;
          rx2 += (bw < viewport[2]) ? bw : viewport[2];

          width = rx2 - rx1;
          height = ry2 - ry1;
          rowstride = width * bpp;

          /* Normalized texture X coords */
          tx1 = tx2;
          tx2 = (rx2 / (float) tex_width);

          /* Draw a portion of texture */
          cogl_rectangle_with_texture_coords (0, 0,
                                              rx2 - rx1,
                                              ry2 - ry1,
                                              tx1, ty1,
                                              tx2, ty2);

          data = g_malloc (height * rowstride);

          /* Read into a temporary bitmap */
          rect_bmp =
            _cogl_bitmap_new_from_data (data,
                                        COGL_PIXEL_FORMAT_RGBA_8888,
                                        width,
                                        height,
                                        rowstride,
                                        (CoglBitmapDestroyNotify) g_free,
                                        NULL);

          cogl_read_pixels (viewport[0], viewport[1],
                            width,
                            height,
                            COGL_READ_PIXELS_COLOR_BUFFER,
                            COGL_PIXEL_FORMAT_RGBA_8888_PRE,
                            data);

          /* Copy to target bitmap */
          _cogl_bitmap_copy_subregion (rect_bmp,
                                       target_bmp,
                                       0,0,
                                       rx1,ry1,
                                       width,
                                       height);

          /* Free temp bitmap */
          cogl_object_unref (rect_bmp);
        }
    }
}
Example #6
0
int
cogl_texture_get_data (CoglHandle       handle,
		       CoglPixelFormat  format,
		       unsigned int     rowstride,
		       guint8          *data)
{
  CoglTexture     *tex;
  int              bpp;
  int              byte_size;
  CoglPixelFormat  closest_format;
  int              closest_bpp;
  GLenum           closest_gl_format;
  GLenum           closest_gl_type;
  CoglBitmap      *target_bmp;
  CoglBitmap      *new_bmp;
  guint8          *src;
  guint8          *dst;
  int              y;
  int              tex_width;
  int              tex_height;

  CoglTextureGetData tg_data;

  if (!cogl_is_texture (handle))
    return 0;

  tex = COGL_TEXTURE (handle);

  /* Default to internal format if none specified */
  if (format == COGL_PIXEL_FORMAT_ANY)
    format = cogl_texture_get_format (handle);

  tex_width = cogl_texture_get_width (handle);
  tex_height = cogl_texture_get_height (handle);

  /* Rowstride from texture width if none specified */
  bpp = _cogl_get_format_bpp (format);
  if (rowstride == 0)
    rowstride = tex_width * bpp;

  /* Return byte size if only that requested */
  byte_size = tex_height * rowstride;
  if (data == NULL)
    return byte_size;

  closest_format =
    _cogl_texture_driver_find_best_gl_get_data_format (format,
                                                       &closest_gl_format,
                                                       &closest_gl_type);
  closest_bpp = _cogl_get_format_bpp (closest_format);

  /* Is the requested format supported? */
  if (closest_format == format)
    /* Target user data directly */
    target_bmp = _cogl_bitmap_new_from_data (data,
                                             format,
                                             tex_width,
                                             tex_height,
                                             rowstride,
                                             NULL, NULL);
  else
    {
      int target_rowstride = tex_width * closest_bpp;
      guint8 *target_data = g_malloc (tex_height * target_rowstride);
      target_bmp = _cogl_bitmap_new_from_data (target_data,
                                               closest_format,
                                               tex_width,
                                               tex_height,
                                               target_rowstride,
                                               (CoglBitmapDestroyNotify) g_free,
                                               NULL);
    }

  tg_data.orig_width = tex_width;
  tg_data.orig_height = tex_height;
  tg_data.target_bmp = target_bmp;
  tg_data.target_bits = _cogl_bitmap_map (target_bmp, COGL_BUFFER_ACCESS_WRITE,
                                          COGL_BUFFER_MAP_HINT_DISCARD);
  if (tg_data.target_bits == NULL)
    {
      cogl_object_unref (target_bmp);
      return 0;
    }
  tg_data.success = TRUE;

  /* Iterating through the subtextures allows piecing together
   * the data for a sliced texture, and allows us to do the
   * read-from-framebuffer logic here in a simple fashion rather than
   * passing offsets down through the code. */
  _cogl_texture_foreach_sub_texture_in_region (handle,
                                               0, 0, 1, 1,
                                               texture_get_cb,
                                               &tg_data);

  _cogl_bitmap_unmap (target_bmp);

  /* 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. */
  if (!tg_data.success)
    _cogl_texture_draw_and_read (tex, target_bmp,
                                 closest_gl_format,
                                 closest_gl_type);

  /* Was intermediate used? */
  if (closest_format != format)
    {
      guint8 *new_bmp_data;
      int new_bmp_rowstride;

      /* Convert to requested format */
      new_bmp = _cogl_bitmap_convert_format_and_premult (target_bmp,
                                                         format);

      /* Free intermediate data and return if failed */
      cogl_object_unref (target_bmp);

      if (new_bmp == NULL)
        return 0;

      new_bmp_rowstride = _cogl_bitmap_get_rowstride (new_bmp);
      new_bmp_data = _cogl_bitmap_map (new_bmp, COGL_BUFFER_ACCESS_WRITE,
                                       COGL_BUFFER_MAP_HINT_DISCARD);

      if (new_bmp_data == NULL)
        {
          cogl_object_unref (new_bmp);
          return 0;
        }

      /* Copy to user buffer */
      for (y = 0; y < tex_height; ++y)
        {
          src = new_bmp_data + y * new_bmp_rowstride;
          dst = data + y * rowstride;
          memcpy (dst, src, tex_width * bpp);
        }

      _cogl_bitmap_unmap (new_bmp);

      /* Free converted data */
      cogl_object_unref (new_bmp);
    }

  return byte_size;
}
Example #7
0
/* 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.
 */
gboolean
_cogl_texture_draw_and_read (CoglHandle   handle,
                             CoglBitmap  *target_bmp,
                             GLuint       target_gl_format,
                             GLuint       target_gl_type)
{
  int        bpp;
  CoglFramebuffer *framebuffer;
  int        viewport[4];
  CoglBitmap *alpha_bmp;
  CoglMatrixStack *projection_stack;
  CoglMatrixStack *modelview_stack;
  int target_width = _cogl_bitmap_get_width (target_bmp);
  int target_height = _cogl_bitmap_get_height (target_bmp);
  int target_rowstride = _cogl_bitmap_get_rowstride (target_bmp);

  _COGL_GET_CONTEXT (ctx, FALSE);

  bpp = _cogl_get_format_bpp (COGL_PIXEL_FORMAT_RGBA_8888);

  framebuffer = _cogl_get_draw_buffer ();
  /* Viewport needs to have some size and be inside the window for this */
  _cogl_framebuffer_get_viewport4fv (framebuffer, viewport);
  if (viewport[0] <  0 || viewport[1] <  0 ||
      viewport[2] <= 0 || viewport[3] <= 0)
    return FALSE;

  /* Setup orthographic projection into current viewport (0,0 in top-left
   * corner to draw the texture upside-down so we match the way cogl_read_pixels
   * works)
   */

  projection_stack = _cogl_framebuffer_get_projection_stack (framebuffer);
  _cogl_matrix_stack_push (projection_stack);
  _cogl_matrix_stack_load_identity (projection_stack);
  _cogl_matrix_stack_ortho (projection_stack,
                            0, (float)(viewport[2]),
                            (float)(viewport[3]), 0,
                            (float)(0),
                            (float)(100));

  modelview_stack = _cogl_framebuffer_get_modelview_stack (framebuffer);
  _cogl_matrix_stack_push (modelview_stack);
  _cogl_matrix_stack_load_identity (modelview_stack);

  /* Direct copy operation */

  if (ctx->texture_download_pipeline == COGL_INVALID_HANDLE)
    {
      ctx->texture_download_pipeline = cogl_pipeline_new ();
      cogl_pipeline_set_blend (ctx->texture_download_pipeline,
                               "RGBA = ADD (SRC_COLOR, 0)",
                               NULL);
    }

  cogl_push_source (ctx->texture_download_pipeline);

  cogl_pipeline_set_layer_texture (ctx->texture_download_pipeline, 0, handle);

  cogl_pipeline_set_layer_combine (ctx->texture_download_pipeline,
                                   0, /* layer */
                                   "RGBA = REPLACE (TEXTURE)",
                                   NULL);

  cogl_pipeline_set_layer_filters (ctx->texture_download_pipeline, 0,
                                   COGL_PIPELINE_FILTER_NEAREST,
                                   COGL_PIPELINE_FILTER_NEAREST);

  do_texture_draw_and_read (handle, target_bmp, viewport);

  /* Check whether texture has alpha and framebuffer not */
  /* FIXME: For some reason even if ALPHA_BITS is 8, the framebuffer
     still doesn't seem to have an alpha buffer. This might be just
     a PowerVR issue.
  GLint r_bits, g_bits, b_bits, a_bits;
  GE( glGetIntegerv (GL_ALPHA_BITS, &a_bits) );
  GE( glGetIntegerv (GL_RED_BITS, &r_bits) );
  GE( glGetIntegerv (GL_GREEN_BITS, &g_bits) );
  GE( glGetIntegerv (GL_BLUE_BITS, &b_bits) );
  printf ("R bits: %d\n", r_bits);
  printf ("G bits: %d\n", g_bits);
  printf ("B bits: %d\n", b_bits);
  printf ("A bits: %d\n", a_bits); */
  if ((cogl_texture_get_format (handle) & COGL_A_BIT)/* && a_bits == 0*/)
    {
      guint8 *srcdata;
      guint8 *dstdata;
      guint8 *srcpixel;
      guint8 *dstpixel;
      int     x,y;
      int     alpha_rowstride = bpp * target_width;

      if ((dstdata = _cogl_bitmap_map (target_bmp,
                                       COGL_BUFFER_ACCESS_WRITE,
                                       COGL_BUFFER_MAP_HINT_DISCARD)) == NULL)
        return FALSE;

      srcdata = g_malloc (alpha_rowstride * target_height);

      /* Create temp bitmap for alpha values */
      alpha_bmp = _cogl_bitmap_new_from_data (srcdata,
                                              COGL_PIXEL_FORMAT_RGBA_8888,
                                              target_width, target_height,
                                              alpha_rowstride,
                                              (CoglBitmapDestroyNotify) g_free,
                                              NULL);

      /* Draw alpha values into RGB channels */
      cogl_pipeline_set_layer_combine (ctx->texture_download_pipeline,
                                       0, /* layer */
                                       "RGBA = REPLACE (TEXTURE[A])",
                                       NULL);

      do_texture_draw_and_read (handle, alpha_bmp, viewport);

      /* Copy temp R to target A */

      for (y=0; y<target_height; ++y)
        {
          for (x=0; x<target_width; ++x)
            {
              srcpixel = srcdata + x*bpp;
              dstpixel = dstdata + x*bpp;
              dstpixel[3] = srcpixel[0];
            }
          srcdata += alpha_rowstride;
          dstdata += target_rowstride;
        }

      _cogl_bitmap_unmap (target_bmp);

      cogl_object_unref (alpha_bmp);
    }

  /* Restore old state */
  _cogl_matrix_stack_pop (modelview_stack);
  _cogl_matrix_stack_pop (projection_stack);

  /* restore the original pipeline */
  cogl_pop_source ();

  return TRUE;
}
Example #8
0
File: cogl.c Project: collects/cogl
void
_cogl_read_pixels_with_rowstride (int x,
                                  int y,
                                  int width,
                                  int height,
                                  CoglReadPixelsFlags source,
                                  CoglPixelFormat format,
                                  guint8 *pixels,
                                  int rowstride)
{
    CoglFramebuffer *framebuffer = _cogl_get_read_framebuffer ();
    int              framebuffer_height;
    int              bpp;
    CoglBitmap      *bmp;
    GLenum           gl_intformat;
    GLenum           gl_format;
    GLenum           gl_type;
    CoglPixelFormat  bmp_format;
    gboolean         pack_invert_set;

    _COGL_GET_CONTEXT (ctx, NO_RETVAL);

    _COGL_RETURN_IF_FAIL (source == COGL_READ_PIXELS_COLOR_BUFFER);

    if (width == 1 && height == 1 && !framebuffer->clear_clip_dirty)
    {
        /* If everything drawn so far for this frame is still in the
         * Journal then if all of the rectangles only have a flat
         * opaque color we have a fast-path for reading a single pixel
         * that avoids the relatively high cost of flushing primitives
         * to be drawn on the GPU (considering how simple the geometry
         * is in this case) and then blocking on the long GPU pipelines
         * for the result.
         */
        if (_cogl_framebuffer_try_fast_read_pixel (framebuffer,
                x, y, source, format,
                pixels))
            return;
    }

    /* make sure any batched primitives get emitted to the GL driver
     * before issuing our read pixels...
     *
     * XXX: Note we currently use cogl_flush to ensure *all* journals
     * are flushed here and not _cogl_journal_flush because we don't
     * track the dependencies between framebuffers so we don't know if
     * the current framebuffer depends on the contents of other
     * framebuffers which could also have associated journal entries.
     */
    cogl_flush ();

    _cogl_framebuffer_flush_state (cogl_get_draw_framebuffer (),
                                   framebuffer,
                                   COGL_FRAMEBUFFER_STATE_BIND);

    framebuffer_height = cogl_framebuffer_get_height (framebuffer);

    /* The y co-ordinate should be given in OpenGL's coordinate system
     * so 0 is the bottom row
     *
     * NB: all offscreen rendering is done upside down so no conversion
     * is necissary in this case.
     */
    if (!cogl_is_offscreen (framebuffer))
        y = framebuffer_height - y - height;

    /* Initialise the CoglBitmap */
    bpp = _cogl_get_format_bpp (format);
    bmp_format = format;

    if ((format & COGL_A_BIT))
    {
        /* We match the premultiplied state of the target buffer to the
         * premultiplied state of the framebuffer so that it will get
         * converted to the right format below */

        if ((framebuffer->format & COGL_PREMULT_BIT))
            bmp_format |= COGL_PREMULT_BIT;
        else
            bmp_format &= ~COGL_PREMULT_BIT;
    }

    bmp = _cogl_bitmap_new_from_data (pixels,
                                      bmp_format, width, height, rowstride,
                                      NULL, NULL);

    ctx->texture_driver->pixel_format_to_gl (format,
            &gl_intformat,
            &gl_format,
            &gl_type);

    /* NB: All offscreen rendering is done upside down so there is no need
     * to flip in this case... */
    if ((ctx->private_feature_flags & COGL_PRIVATE_FEATURE_MESA_PACK_INVERT) &&
            !cogl_is_offscreen (framebuffer))
    {
        GE (ctx, glPixelStorei (GL_PACK_INVERT_MESA, TRUE));
        pack_invert_set = TRUE;
    }
    else
        pack_invert_set = FALSE;

    /* Under GLES only GL_RGBA with GL_UNSIGNED_BYTE as well as an
       implementation specific format under
       GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES and
       GL_IMPLEMENTATION_COLOR_READ_TYPE_OES is supported. We could try
       to be more clever and check if the requested type matches that
       but we would need some reliable functions to convert from GL
       types to Cogl types. For now, lets just always read in
       GL_RGBA/GL_UNSIGNED_BYTE and convert if necessary. We also need
       to use this intermediate buffer if the rowstride has padding
       because GLES does not support setting GL_ROW_LENGTH */
    if (ctx->driver != COGL_DRIVER_GL &&
            (gl_format != GL_RGBA || gl_type != GL_UNSIGNED_BYTE ||
             rowstride != 4 * width))
    {
        CoglBitmap *tmp_bmp, *dst_bmp;
        guint8 *tmp_data = g_malloc (width * height * 4);

        tmp_bmp = _cogl_bitmap_new_from_data (tmp_data,
                                              COGL_PIXEL_FORMAT_RGBA_8888 |
                                              (bmp_format & COGL_PREMULT_BIT),
                                              width, height, 4 * width,
                                              (CoglBitmapDestroyNotify) g_free,
                                              NULL);

        ctx->texture_driver->prep_gl_for_pixels_download (4 * width, 4);

        GE( ctx, glReadPixels (x, y, width, height,
                               GL_RGBA, GL_UNSIGNED_BYTE,
                               tmp_data) );

        /* CoglBitmap doesn't currently have a way to convert without
           allocating its own buffer so we have to copy the data
           again */
        if ((dst_bmp = _cogl_bitmap_convert_format_and_premult (tmp_bmp,
                       format)))
        {
            _cogl_bitmap_copy_subregion (dst_bmp,
                                         bmp,
                                         0, 0,
                                         0, 0,
                                         width, height);
            cogl_object_unref (dst_bmp);
        }
        else
        {
            /* FIXME: there's no way to report an error here so we'll
               just have to leave the data initialised */
        }

        cogl_object_unref (tmp_bmp);
    }
    else
    {
        ctx->texture_driver->prep_gl_for_pixels_download (rowstride, bpp);

        GE( ctx, glReadPixels (x, y, width, height, gl_format, gl_type, pixels) );

        /* Convert to the premult format specified by the caller
           in-place. This will do nothing if the premult status is already
           correct. */
        _cogl_bitmap_convert_premult_status (bmp, format);
    }

    /* Currently this function owns the pack_invert state and we don't want this
     * to interfere with other Cogl components so all other code can assume that
     * we leave the pack_invert state off. */
    if (pack_invert_set)
        GE (ctx, glPixelStorei (GL_PACK_INVERT_MESA, FALSE));

    /* NB: All offscreen rendering is done upside down so there is no need
     * to flip in this case... */
    if (!cogl_is_offscreen (framebuffer) && !pack_invert_set)
    {
        guint8 *temprow = g_alloca (rowstride * sizeof (guint8));

        /* vertically flip the buffer in-place */
        for (y = 0; y < height / 2; y++)
        {
            if (y != height - y - 1) /* skip center row */
            {
                memcpy (temprow,
                        pixels + y * rowstride, rowstride);
                memcpy (pixels + y * rowstride,
                        pixels + (height - y - 1) * rowstride, rowstride);
                memcpy (pixels + (height - y - 1) * rowstride,
                        temprow,
                        rowstride);
            }
        }
    }

    cogl_object_unref (bmp);
}