Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / mesa / main / readpix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "glheader.h"
26 #include "imports.h"
27 #include "bufferobj.h"
28 #include "context.h"
29 #include "readpix.h"
30 #include "framebuffer.h"
31 #include "formats.h"
32 #include "image.h"
33 #include "mtypes.h"
34 #include "state.h"
35
36
37 /**
38 * Do error checking of the format/type parameters to glReadPixels and
39 * glDrawPixels.
40 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
41 * for ReadPixels.
42 * \return GL_TRUE if error detected, GL_FALSE if no errors
43 */
44 GLboolean
45 _mesa_error_check_format_type(struct gl_context *ctx, GLenum format, GLenum type,
46 GLboolean drawing)
47 {
48 const char *readDraw = drawing ? "Draw" : "Read";
49 const GLboolean reading = !drawing;
50
51 /* state validation should have already been done */
52 ASSERT(ctx->NewState == 0x0);
53
54 if (ctx->Extensions.EXT_packed_depth_stencil
55 && type == GL_UNSIGNED_INT_24_8_EXT
56 && format != GL_DEPTH_STENCIL_EXT) {
57 _mesa_error(ctx, GL_INVALID_OPERATION,
58 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
59 return GL_TRUE;
60 }
61
62 /* basic combinations test */
63 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
64 _mesa_error(ctx, GL_INVALID_ENUM,
65 "gl%sPixels(format or type)", readDraw);
66 return GL_TRUE;
67 }
68
69 /* additional checks */
70 switch (format) {
71 case GL_RG:
72 case GL_RED:
73 case GL_GREEN:
74 case GL_BLUE:
75 case GL_ALPHA:
76 case GL_LUMINANCE:
77 case GL_LUMINANCE_ALPHA:
78 case GL_RGB:
79 case GL_BGR:
80 case GL_RGBA:
81 case GL_BGRA:
82 case GL_ABGR_EXT:
83 case GL_RED_INTEGER_EXT:
84 case GL_GREEN_INTEGER_EXT:
85 case GL_BLUE_INTEGER_EXT:
86 case GL_ALPHA_INTEGER_EXT:
87 case GL_RGB_INTEGER_EXT:
88 case GL_RGBA_INTEGER_EXT:
89 case GL_BGR_INTEGER_EXT:
90 case GL_BGRA_INTEGER_EXT:
91 case GL_LUMINANCE_INTEGER_EXT:
92 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
93 if (!drawing) {
94 /* reading */
95 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
96 _mesa_error(ctx, GL_INVALID_OPERATION,
97 "glReadPixels(no color buffer)");
98 return GL_TRUE;
99 }
100 }
101 break;
102 case GL_COLOR_INDEX:
103 if (drawing) {
104 if (ctx->PixelMaps.ItoR.Size == 0 ||
105 ctx->PixelMaps.ItoG.Size == 0 ||
106 ctx->PixelMaps.ItoB.Size == 0) {
107 _mesa_error(ctx, GL_INVALID_OPERATION,
108 "glDrawPixels(drawing color index pixels into RGB buffer)");
109 return GL_TRUE;
110 }
111 }
112 else {
113 /* reading */
114 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
115 _mesa_error(ctx, GL_INVALID_OPERATION,
116 "glReadPixels(no color buffer)");
117 return GL_TRUE;
118 }
119 /* We no longer support CI-mode color buffers so trying to read
120 * GL_COLOR_INDEX pixels is always an error.
121 */
122 _mesa_error(ctx, GL_INVALID_OPERATION,
123 "glReadPixels(color buffer is RGB)");
124 return GL_TRUE;
125 }
126 break;
127 case GL_STENCIL_INDEX:
128 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
129 (reading && !_mesa_source_buffer_exists(ctx, format))) {
130 _mesa_error(ctx, GL_INVALID_OPERATION,
131 "gl%sPixels(no stencil buffer)", readDraw);
132 return GL_TRUE;
133 }
134 break;
135 case GL_DEPTH_COMPONENT:
136 if ((drawing && !_mesa_dest_buffer_exists(ctx, format))) {
137 _mesa_error(ctx, GL_INVALID_OPERATION,
138 "gl%sPixels(no depth buffer)", readDraw);
139 return GL_TRUE;
140 }
141 break;
142 case GL_DEPTH_STENCIL_EXT:
143 if (!ctx->Extensions.EXT_packed_depth_stencil ||
144 type != GL_UNSIGNED_INT_24_8_EXT) {
145 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
146 return GL_TRUE;
147 }
148 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
149 (reading && !_mesa_source_buffer_exists(ctx, format))) {
150 _mesa_error(ctx, GL_INVALID_OPERATION,
151 "gl%sPixels(no depth or stencil buffer)", readDraw);
152 return GL_TRUE;
153 }
154 break;
155 default:
156 /* this should have been caught in _mesa_is_legal_format_type() */
157 _mesa_problem(ctx, "unexpected format in _mesa_%sPixels", readDraw);
158 return GL_TRUE;
159 }
160
161 /* no errors */
162 return GL_FALSE;
163 }
164
165
166
167 void GLAPIENTRY
168 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
169 GLenum format, GLenum type, GLvoid *pixels )
170 {
171 GET_CURRENT_CONTEXT(ctx);
172 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
173
174 FLUSH_CURRENT(ctx, 0);
175
176 if (width < 0 || height < 0) {
177 _mesa_error( ctx, GL_INVALID_VALUE,
178 "glReadPixels(width=%d height=%d)", width, height );
179 return;
180 }
181
182 if (ctx->NewState)
183 _mesa_update_state(ctx);
184
185 if (_mesa_error_check_format_type(ctx, format, type, GL_FALSE)) {
186 /* found an error */
187 return;
188 }
189
190 /* Check that the destination format and source buffer are both
191 * integer-valued or both non-integer-valued.
192 */
193 if (ctx->Extensions.EXT_texture_integer && _mesa_is_color_format(format)) {
194 const struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
195 const GLboolean srcInteger = _mesa_is_format_integer_color(rb->Format);
196 const GLboolean dstInteger = _mesa_is_integer_format(format);
197 if (dstInteger != srcInteger) {
198 _mesa_error(ctx, GL_INVALID_OPERATION,
199 "glReadPixels(integer / non-integer format mismatch");
200 return;
201 }
202 }
203
204 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
205 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
206 "glReadPixels(incomplete framebuffer)" );
207 return;
208 }
209
210 if (!_mesa_source_buffer_exists(ctx, format)) {
211 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
212 return;
213 }
214
215 if (width == 0 || height == 0)
216 return; /* nothing to do */
217
218 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
219 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
220 format, type, pixels)) {
221 _mesa_error(ctx, GL_INVALID_OPERATION,
222 "glReadPixels(invalid PBO access)");
223 return;
224 }
225
226 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
227 /* buffer is mapped - that's an error */
228 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
229 return;
230 }
231 }
232
233 ctx->Driver.ReadPixels(ctx, x, y, width, height,
234 format, type, &ctx->Pack, pixels);
235 }