Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / mesa / main / drawpix.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 "drawpix.h"
30 #include "enums.h"
31 #include "feedback.h"
32 #include "framebuffer.h"
33 #include "mfeatures.h"
34 #include "readpix.h"
35 #include "state.h"
36 #include "dispatch.h"
37
38
39 #if FEATURE_drawpix
40
41
42 /*
43 * Execute glDrawPixels
44 */
45 static void GLAPIENTRY
46 _mesa_DrawPixels( GLsizei width, GLsizei height,
47 GLenum format, GLenum type, const GLvoid *pixels )
48 {
49 GET_CURRENT_CONTEXT(ctx);
50 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
51
52 if (MESA_VERBOSE & VERBOSE_API)
53 _mesa_debug(ctx, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
54 width, height,
55 _mesa_lookup_enum_by_nr(format),
56 _mesa_lookup_enum_by_nr(type),
57 pixels,
58 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer[0]),
59 IROUND(ctx->Current.RasterPos[0]),
60 IROUND(ctx->Current.RasterPos[1]));
61
62
63 if (width < 0 || height < 0) {
64 _mesa_error( ctx, GL_INVALID_VALUE, "glDrawPixels(width or height < 0" );
65 return;
66 }
67
68 /* We're not using the current vertex program, and the driver may install
69 * its own. Note: this may dirty some state.
70 */
71 _mesa_set_vp_override(ctx, GL_TRUE);
72
73 /* Note: this call does state validation */
74 if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
75 goto end; /* the error code was recorded */
76 }
77
78 if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
79 goto end; /* the error code was recorded */
80 }
81
82 if (!ctx->Current.RasterPosValid) {
83 goto end; /* no-op, not an error */
84 }
85
86 if (ctx->RenderMode == GL_RENDER) {
87 if (width > 0 && height > 0) {
88 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
89 GLint x = IROUND(ctx->Current.RasterPos[0]);
90 GLint y = IROUND(ctx->Current.RasterPos[1]);
91
92 if (ctx->Unpack.BufferObj->Name) {
93 /* unpack from PBO */
94 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
95 format, type, pixels)) {
96 _mesa_error(ctx, GL_INVALID_OPERATION,
97 "glDrawPixels(invalid PBO access)");
98 goto end;
99 }
100 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
101 /* buffer is mapped - that's an error */
102 _mesa_error(ctx, GL_INVALID_OPERATION,
103 "glDrawPixels(PBO is mapped)");
104 goto end;
105 }
106 }
107
108 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
109 &ctx->Unpack, pixels);
110 }
111 }
112 else if (ctx->RenderMode == GL_FEEDBACK) {
113 /* Feedback the current raster pos info */
114 FLUSH_CURRENT( ctx, 0 );
115 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
116 _mesa_feedback_vertex( ctx,
117 ctx->Current.RasterPos,
118 ctx->Current.RasterColor,
119 ctx->Current.RasterTexCoords[0] );
120 }
121 else {
122 ASSERT(ctx->RenderMode == GL_SELECT);
123 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
124 }
125
126 end:
127 _mesa_set_vp_override(ctx, GL_FALSE);
128 }
129
130
131 static void GLAPIENTRY
132 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
133 GLenum type )
134 {
135 GET_CURRENT_CONTEXT(ctx);
136 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
137
138 _mesa_finish(ctx);
139
140 if (MESA_VERBOSE & VERBOSE_API)
141 _mesa_debug(ctx,
142 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
143 srcx, srcy, width, height,
144 _mesa_lookup_enum_by_nr(type),
145 _mesa_lookup_enum_by_nr(ctx->ReadBuffer->ColorReadBuffer),
146 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer[0]),
147 IROUND(ctx->Current.RasterPos[0]),
148 IROUND(ctx->Current.RasterPos[1]));
149
150 if (width < 0 || height < 0) {
151 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
152 return;
153 }
154
155 /* Note: more detailed 'type' checking is done by the
156 * _mesa_source/dest_buffer_exists() calls below. That's where we
157 * check if the stencil buffer exists, etc.
158 */
159 if (type != GL_COLOR &&
160 type != GL_DEPTH &&
161 type != GL_STENCIL &&
162 type != GL_DEPTH_STENCIL) {
163 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
164 _mesa_lookup_enum_by_nr(type));
165 return;
166 }
167
168 /* We're not using the current vertex program, and the driver may install
169 * it's own. Note: this may dirty some state.
170 */
171 _mesa_set_vp_override(ctx, GL_TRUE);
172
173 /* Note: this call does state validation */
174 if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
175 goto end; /* the error code was recorded */
176 }
177
178 /* Check read buffer's status (draw buffer was already checked) */
179 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
180 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
181 "glCopyPixels(incomplete framebuffer)" );
182 goto end;
183 }
184
185 if (!_mesa_source_buffer_exists(ctx, type) ||
186 !_mesa_dest_buffer_exists(ctx, type)) {
187 _mesa_error(ctx, GL_INVALID_OPERATION,
188 "glCopyPixels(missing source or dest buffer)");
189 goto end;
190 }
191
192 if (!ctx->Current.RasterPosValid || width ==0 || height == 0) {
193 goto end; /* no-op, not an error */
194 }
195
196 if (ctx->RenderMode == GL_RENDER) {
197 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
198 if (width > 0 && height > 0) {
199 GLint destx = IROUND(ctx->Current.RasterPos[0]);
200 GLint desty = IROUND(ctx->Current.RasterPos[1]);
201 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
202 type );
203 }
204 }
205 else if (ctx->RenderMode == GL_FEEDBACK) {
206 FLUSH_CURRENT( ctx, 0 );
207 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
208 _mesa_feedback_vertex( ctx,
209 ctx->Current.RasterPos,
210 ctx->Current.RasterColor,
211 ctx->Current.RasterTexCoords[0] );
212 }
213 else {
214 ASSERT(ctx->RenderMode == GL_SELECT);
215 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
216 }
217
218 end:
219 _mesa_set_vp_override(ctx, GL_FALSE);
220 }
221
222
223 static void GLAPIENTRY
224 _mesa_Bitmap( GLsizei width, GLsizei height,
225 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
226 const GLubyte *bitmap )
227 {
228 GET_CURRENT_CONTEXT(ctx);
229 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
230
231 if (width < 0 || height < 0) {
232 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
233 return;
234 }
235
236 if (!ctx->Current.RasterPosValid) {
237 return; /* do nothing */
238 }
239
240 /* Note: this call does state validation */
241 if (!_mesa_valid_to_render(ctx, "glBitmap")) {
242 /* the error code was recorded */
243 return;
244 }
245
246 if (ctx->RenderMode == GL_RENDER) {
247 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
248 if (width > 0 && height > 0) {
249 const GLfloat epsilon = 0.0001F;
250 GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
251 GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
252
253 if (ctx->Unpack.BufferObj->Name) {
254 /* unpack from PBO */
255 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
256 GL_COLOR_INDEX, GL_BITMAP,
257 (GLvoid *) bitmap)) {
258 _mesa_error(ctx, GL_INVALID_OPERATION,
259 "glBitmap(invalid PBO access)");
260 return;
261 }
262 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
263 /* buffer is mapped - that's an error */
264 _mesa_error(ctx, GL_INVALID_OPERATION,
265 "glBitmap(PBO is mapped)");
266 return;
267 }
268 }
269
270 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
271 }
272 }
273 #if _HAVE_FULL_GL
274 else if (ctx->RenderMode == GL_FEEDBACK) {
275 FLUSH_CURRENT(ctx, 0);
276 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
277 _mesa_feedback_vertex( ctx,
278 ctx->Current.RasterPos,
279 ctx->Current.RasterColor,
280 ctx->Current.RasterTexCoords[0] );
281 }
282 else {
283 ASSERT(ctx->RenderMode == GL_SELECT);
284 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
285 }
286 #endif
287
288 /* update raster position */
289 ctx->Current.RasterPos[0] += xmove;
290 ctx->Current.RasterPos[1] += ymove;
291 }
292
293
294 void
295 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
296 {
297 SET_Bitmap(disp, _mesa_Bitmap);
298 SET_CopyPixels(disp, _mesa_CopyPixels);
299 SET_DrawPixels(disp, _mesa_DrawPixels);
300 }
301
302
303 #endif /* FEATURE_drawpix */