e2ce264cf0bd828184867ca62a904fa9727b3cdb
[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 "image.h"
32 #include "state.h"
33
34
35 /**
36 * Do error checking of the format/type parameters to glReadPixels and
37 * glDrawPixels.
38 * \param drawing if GL_TRUE do checking for DrawPixels, else do checking
39 * for ReadPixels.
40 * \return GL_TRUE if error detected, GL_FALSE if no errors
41 */
42 GLboolean
43 _mesa_error_check_format_type(struct gl_context *ctx, GLenum format, GLenum type,
44 GLboolean drawing)
45 {
46 const char *readDraw = drawing ? "Draw" : "Read";
47 const GLboolean reading = !drawing;
48
49 /* state validation should have already been done */
50 ASSERT(ctx->NewState == 0x0);
51
52 if (ctx->Extensions.EXT_packed_depth_stencil
53 && type == GL_UNSIGNED_INT_24_8_EXT
54 && format != GL_DEPTH_STENCIL_EXT) {
55 _mesa_error(ctx, GL_INVALID_OPERATION,
56 "gl%sPixels(format is not GL_DEPTH_STENCIL_EXT)", readDraw);
57 return GL_TRUE;
58 }
59
60 /* basic combinations test */
61 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
62 _mesa_error(ctx, GL_INVALID_ENUM,
63 "gl%sPixels(format or type)", readDraw);
64 return GL_TRUE;
65 }
66
67 /* additional checks */
68 switch (format) {
69 case GL_RG:
70 case GL_RED:
71 case GL_GREEN:
72 case GL_BLUE:
73 case GL_ALPHA:
74 case GL_LUMINANCE:
75 case GL_LUMINANCE_ALPHA:
76 case GL_RGB:
77 case GL_BGR:
78 case GL_RGBA:
79 case GL_BGRA:
80 case GL_ABGR_EXT:
81 case GL_RED_INTEGER_EXT:
82 case GL_GREEN_INTEGER_EXT:
83 case GL_BLUE_INTEGER_EXT:
84 case GL_ALPHA_INTEGER_EXT:
85 case GL_RGB_INTEGER_EXT:
86 case GL_RGBA_INTEGER_EXT:
87 case GL_BGR_INTEGER_EXT:
88 case GL_BGRA_INTEGER_EXT:
89 case GL_LUMINANCE_INTEGER_EXT:
90 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
91 if (!drawing) {
92 /* reading */
93 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
94 _mesa_error(ctx, GL_INVALID_OPERATION,
95 "glReadPixels(no color buffer)");
96 return GL_TRUE;
97 }
98 }
99 break;
100 case GL_COLOR_INDEX:
101 if (drawing) {
102 if (ctx->PixelMaps.ItoR.Size == 0 ||
103 ctx->PixelMaps.ItoG.Size == 0 ||
104 ctx->PixelMaps.ItoB.Size == 0) {
105 _mesa_error(ctx, GL_INVALID_OPERATION,
106 "glDrawPixels(drawing color index pixels into RGB buffer)");
107 return GL_TRUE;
108 }
109 }
110 else {
111 /* reading */
112 if (!_mesa_source_buffer_exists(ctx, GL_COLOR)) {
113 _mesa_error(ctx, GL_INVALID_OPERATION,
114 "glReadPixels(no color buffer)");
115 return GL_TRUE;
116 }
117 /* We no longer support CI-mode color buffers so trying to read
118 * GL_COLOR_INDEX pixels is always an error.
119 */
120 _mesa_error(ctx, GL_INVALID_OPERATION,
121 "glReadPixels(color buffer is RGB)");
122 return GL_TRUE;
123 }
124 break;
125 case GL_STENCIL_INDEX:
126 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
127 (reading && !_mesa_source_buffer_exists(ctx, format))) {
128 _mesa_error(ctx, GL_INVALID_OPERATION,
129 "gl%sPixels(no stencil buffer)", readDraw);
130 return GL_TRUE;
131 }
132 break;
133 case GL_DEPTH_COMPONENT:
134 if ((drawing && !_mesa_dest_buffer_exists(ctx, format))) {
135 _mesa_error(ctx, GL_INVALID_OPERATION,
136 "gl%sPixels(no depth buffer)", readDraw);
137 return GL_TRUE;
138 }
139 break;
140 case GL_DEPTH_STENCIL_EXT:
141 if (!ctx->Extensions.EXT_packed_depth_stencil ||
142 type != GL_UNSIGNED_INT_24_8_EXT) {
143 _mesa_error(ctx, GL_INVALID_ENUM, "gl%sPixels(type)", readDraw);
144 return GL_TRUE;
145 }
146 if ((drawing && !_mesa_dest_buffer_exists(ctx, format)) ||
147 (reading && !_mesa_source_buffer_exists(ctx, format))) {
148 _mesa_error(ctx, GL_INVALID_OPERATION,
149 "gl%sPixels(no depth or stencil buffer)", readDraw);
150 return GL_TRUE;
151 }
152 break;
153 default:
154 /* this should have been caught in _mesa_is_legal_format_type() */
155 _mesa_problem(ctx, "unexpected format in _mesa_%sPixels", readDraw);
156 return GL_TRUE;
157 }
158
159 /* no errors */
160 return GL_FALSE;
161 }
162
163
164
165 void GLAPIENTRY
166 _mesa_ReadPixels( GLint x, GLint y, GLsizei width, GLsizei height,
167 GLenum format, GLenum type, GLvoid *pixels )
168 {
169 GET_CURRENT_CONTEXT(ctx);
170 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
171
172 FLUSH_CURRENT(ctx, 0);
173
174 if (width < 0 || height < 0) {
175 _mesa_error( ctx, GL_INVALID_VALUE,
176 "glReadPixels(width=%d height=%d)", width, height );
177 return;
178 }
179
180 if (ctx->NewState)
181 _mesa_update_state(ctx);
182
183 if (_mesa_error_check_format_type(ctx, format, type, GL_FALSE)) {
184 /* found an error */
185 return;
186 }
187
188 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
189 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
190 "glReadPixels(incomplete framebuffer)" );
191 return;
192 }
193
194 if (!_mesa_source_buffer_exists(ctx, format)) {
195 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(no readbuffer)");
196 return;
197 }
198
199 if (width == 0 || height == 0)
200 return; /* nothing to do */
201
202 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
203 if (!_mesa_validate_pbo_access(2, &ctx->Pack, width, height, 1,
204 format, type, pixels)) {
205 _mesa_error(ctx, GL_INVALID_OPERATION,
206 "glReadPixels(invalid PBO access)");
207 return;
208 }
209
210 if (_mesa_bufferobj_mapped(ctx->Pack.BufferObj)) {
211 /* buffer is mapped - that's an error */
212 _mesa_error(ctx, GL_INVALID_OPERATION, "glReadPixels(PBO is mapped)");
213 return;
214 }
215 }
216
217 ctx->Driver.ReadPixels(ctx, x, y, width, height,
218 format, type, &ctx->Pack, pixels);
219 }