Mesa: fix bug with glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES)

When querying the prefered pixel format without a current color
buffer, mesa will crash. This has been fixed in upstream mesa with
two patches, which this CL backports:

commit f8e7aa2827e2bdb1ed238cbdd351be3c8a6e9b12
Author: Jordan Justen <[email protected]>
Date:   Sat Jan 12 01:11:21 2013 -0800

framebuffer: update allowed implementation format/type

Allow additional format/type combinations based on the
color render buffer to fix failures with gles3-gtf.

Reviewed-by: Ian Romanick <[email protected]>
Signed-off-by: Jordan Justen <[email protected]>

and:

commit e20a2df4017ab10dd7199936948c6ac809bfacb6
Author: Brian Paul <[email protected]>
Date:   Sun Jun 2 18:07:55 2013 -0600

mesa: handle missing read buffer in _mesa_get_color_read_format/type()

We were crashing when GL_READ_BUFFER == GL_NONE.  Check for NULL
pointers and reorganize the code.  The spec doesn't say which error
to generate in this situation, but NVIDIA raises GL_INVALID_OPERATION.

Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=65173
NOTE: This is a candidate for the stable branches.

Tested-by: Vedran Rodic <[email protected]>
Reviewed-by: José Fonseca <[email protected]>

BUG=chromium:443074
TEST=trybots



git-svn-id: svn://svn.chromium.org/chrome/trunk/deps/third_party/mesa@293450 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/src/mesa/main/framebuffer.c b/src/mesa/main/framebuffer.c
index 13887f8..46e1fcd 100644
--- a/src/mesa/main/framebuffer.c
+++ b/src/mesa/main/framebuffer.c
@@ -870,13 +870,31 @@
 GLenum
 _mesa_get_color_read_format(struct gl_context *ctx)
 {
-   switch (ctx->ReadBuffer->_ColorReadBuffer->Format) {
-   case MESA_FORMAT_ARGB8888:
-      return GL_BGRA;
-   case MESA_FORMAT_RGB565:
-      return GL_BGR;
-   default:
-      return GL_RGBA;
+   if (!ctx->ReadBuffer || !ctx->ReadBuffer->_ColorReadBuffer) {
+      /* The spec is unclear how to handle this case, but NVIDIA's
+       * driver generates GL_INVALID_OPERATION.
+       */
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT: "
+                  "no GL_READ_BUFFER)");
+      return GL_NONE;
+   }
+   else {
+      const GLenum format = ctx->ReadBuffer->_ColorReadBuffer->Format;
+      const GLenum data_type = _mesa_get_format_datatype(format);
+
+      if (format == MESA_FORMAT_ARGB8888)
+         return GL_BGRA;
+      else if (format == MESA_FORMAT_RGB565)
+         return GL_BGR;
+
+      switch (data_type) {
+      case GL_UNSIGNED_INT:
+      case GL_INT:
+         return GL_RGBA_INTEGER;
+      default:
+         return GL_RGBA;
+      }
    }
 }
 
@@ -887,13 +905,33 @@
 GLenum
 _mesa_get_color_read_type(struct gl_context *ctx)
 {
-   switch (ctx->ReadBuffer->_ColorReadBuffer->Format) {
-   case MESA_FORMAT_ARGB8888:
-      return GL_UNSIGNED_BYTE;
-   case MESA_FORMAT_RGB565:
-      return GL_UNSIGNED_SHORT_5_6_5_REV;
-   default:
-      return GL_UNSIGNED_BYTE;
+   if (!ctx->ReadBuffer || !ctx->ReadBuffer->_ColorReadBuffer) {
+      /* The spec is unclear how to handle this case, but NVIDIA's
+       * driver generates GL_INVALID_OPERATION.
+       */
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE: "
+                  "no GL_READ_BUFFER)");
+      return GL_NONE;
+   }
+   else {
+      const GLenum format = ctx->ReadBuffer->_ColorReadBuffer->Format;
+      const GLenum data_type = _mesa_get_format_datatype(format);
+
+      if (format == MESA_FORMAT_RGB565)
+         return GL_UNSIGNED_SHORT_5_6_5_REV;
+
+      switch (data_type) {
+      case GL_SIGNED_NORMALIZED:
+         return GL_BYTE;
+      case GL_UNSIGNED_INT:
+      case GL_INT:
+      case GL_FLOAT:
+         return data_type;
+      case GL_UNSIGNED_NORMALIZED:
+      default:
+         return GL_UNSIGNED_BYTE;
+      }
    }
 }