mesa: Pass GL context to _mesa_create_save_table
[mesa.git] / src / mesa / main / readpix.c
index 7ac8774973850d09f83664619908bb6f972c641f..f6680c91f44171adacc5df9dd044556e140703b4 100644 (file)
@@ -137,7 +137,7 @@ read_depth_pixels( struct gl_context *ctx,
       return;
    }
 
-   depthValues = (GLfloat *) malloc(width * sizeof(GLfloat));
+   depthValues = malloc(width * sizeof(GLfloat));
 
    if (depthValues) {
       /* General case (slower) */
@@ -185,7 +185,7 @@ read_stencil_pixels( struct gl_context *ctx,
       return;
    }
 
-   stencil = (GLubyte *) malloc(width * sizeof(GLubyte));
+   stencil = malloc(width * sizeof(GLubyte));
 
    if (stencil) {
       /* process image row by row */
@@ -322,6 +322,8 @@ slow_read_rgba_pixels( struct gl_context *ctx,
    void *rgba;
    GLubyte *dst, *map;
    int dstStride, stride, j;
+   GLboolean dst_is_integer = _mesa_is_enum_format_integer(format);
+   GLboolean dst_is_uint = _mesa_is_format_unsigned(rbFormat);
 
    dstStride = _mesa_image_row_stride(packing, width, format, type);
    dst = (GLubyte *) _mesa_image_address2d(packing, pixels, width, height,
@@ -339,12 +341,17 @@ slow_read_rgba_pixels( struct gl_context *ctx,
       goto done;
 
    for (j = 0; j < height; j++) {
-      if (_mesa_is_enum_format_integer(format)) {
+      if (dst_is_integer) {
         _mesa_unpack_uint_rgba_row(rbFormat, width, map, (GLuint (*)[4]) rgba);
          _mesa_rebase_rgba_uint(width, (GLuint (*)[4]) rgba,
                                 rb->_BaseFormat);
-        _mesa_pack_rgba_span_int(ctx, width, (GLuint (*)[4]) rgba, format,
-                                  type, dst);
+         if (dst_is_uint) {
+            _mesa_pack_rgba_span_from_uints(ctx, width, (GLuint (*)[4]) rgba, format,
+                                            type, dst);
+         } else {
+            _mesa_pack_rgba_span_from_ints(ctx, width, (GLint (*)[4]) rgba, format,
+                                           type, dst);
+         }
       } else {
         _mesa_unpack_rgba_row(rbFormat, width, map, (GLfloat (*)[4]) rgba);
          _mesa_rebase_rgba_float(width, (GLfloat (*)[4]) rgba,
@@ -473,7 +480,7 @@ fast_read_depth_stencil_pixels_separate(struct gl_context *ctx,
       return GL_TRUE;  /* don't bother trying the slow path */
    }
 
-   stencilVals = (GLubyte *) malloc(width * sizeof(GLubyte));
+   stencilVals = malloc(width * sizeof(GLubyte));
 
    if (stencilVals) {
       for (j = 0; j < height; j++) {
@@ -544,8 +551,8 @@ slow_read_depth_stencil_pixels_separate(struct gl_context *ctx,
       stencilStride = depthStride;
    }
 
-   stencilVals = (GLubyte *) malloc(width * sizeof(GLubyte));
-   depthVals = (GLfloat *) malloc(width * sizeof(GLfloat));
+   stencilVals = malloc(width * sizeof(GLubyte));
+   depthVals = malloc(width * sizeof(GLfloat));
 
    if (stencilVals && depthVals) {
       for (j = 0; j < height; j++) {
@@ -692,6 +699,33 @@ _mesa_ReadnPixelsARB( GLint x, GLint y, GLsizei width, GLsizei height,
       return;
    }
 
+   /* OpenGL ES 1.x and OpenGL ES 2.0 impose additional restrictions on the
+    * combinations of format and type that can be used.
+    *
+    * Technically, only two combinations are actually allowed:
+    * GL_RGBA/GL_UNSIGNED_BYTE, and some implementation-specific internal
+    * preferred combination.  This code doesn't know what that preferred
+    * combination is, and Mesa can handle anything valid.  Just work instead.
+    */
+   if (_mesa_is_gles(ctx) && ctx->Version < 30) {
+      err = _mesa_es_error_check_format_and_type(format, type, 2);
+      if (err == GL_NO_ERROR) {
+         if (type == GL_FLOAT || type == GL_HALF_FLOAT_OES) {
+            err = GL_INVALID_OPERATION;
+         } else if (format == GL_DEPTH_COMPONENT
+                    || format == GL_DEPTH_STENCIL) {
+            err = GL_INVALID_ENUM;
+         }
+      }
+
+      if (err != GL_NO_ERROR) {
+         _mesa_error(ctx, err, "glReadPixels(invalid format %s and/or type %s)",
+                     _mesa_lookup_enum_by_nr(format),
+                     _mesa_lookup_enum_by_nr(type));
+         return;
+      }
+   }
+
    if (ctx->NewState)
       _mesa_update_state(ctx);