2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 #include "bufferobj.h"
32 #include "framebuffer.h"
33 #include "mfeatures.h"
44 * Execute glDrawPixels
46 static void GLAPIENTRY
47 _mesa_DrawPixels( GLsizei width, GLsizei height,
48 GLenum format, GLenum type, const GLvoid *pixels )
50 GET_CURRENT_CONTEXT(ctx);
51 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
53 if (MESA_VERBOSE & VERBOSE_API)
54 _mesa_debug(ctx, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
56 _mesa_lookup_enum_by_nr(format),
57 _mesa_lookup_enum_by_nr(type),
59 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer[0]),
60 IROUND(ctx->Current.RasterPos[0]),
61 IROUND(ctx->Current.RasterPos[1]));
64 if (width < 0 || height < 0) {
65 _mesa_error( ctx, GL_INVALID_VALUE, "glDrawPixels(width or height < 0" );
69 /* We're not using the current vertex program, and the driver may install
70 * its own. Note: this may dirty some state.
72 _mesa_set_vp_override(ctx, GL_TRUE);
74 /* Note: this call does state validation */
75 if (!_mesa_valid_to_render(ctx, "glDrawPixels")) {
76 goto end; /* the error code was recorded */
79 if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
80 goto end; /* the error code was recorded */
83 if (!ctx->Current.RasterPosValid) {
84 goto end; /* no-op, not an error */
87 if (ctx->RenderMode == GL_RENDER) {
88 if (width > 0 && height > 0) {
89 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
90 GLint x = IROUND(ctx->Current.RasterPos[0]);
91 GLint y = IROUND(ctx->Current.RasterPos[1]);
93 if (ctx->Unpack.BufferObj->Name) {
95 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
96 format, type, pixels)) {
97 _mesa_error(ctx, GL_INVALID_OPERATION,
98 "glDrawPixels(invalid PBO access)");
101 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
102 /* buffer is mapped - that's an error */
103 _mesa_error(ctx, GL_INVALID_OPERATION,
104 "glDrawPixels(PBO is mapped)");
109 ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
110 &ctx->Unpack, pixels);
113 else if (ctx->RenderMode == GL_FEEDBACK) {
114 /* Feedback the current raster pos info */
115 FLUSH_CURRENT( ctx, 0 );
116 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
117 _mesa_feedback_vertex( ctx,
118 ctx->Current.RasterPos,
119 ctx->Current.RasterColor,
120 ctx->Current.RasterTexCoords[0] );
123 ASSERT(ctx->RenderMode == GL_SELECT);
124 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
128 _mesa_set_vp_override(ctx, GL_FALSE);
132 static void GLAPIENTRY
133 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
136 GET_CURRENT_CONTEXT(ctx);
137 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
139 if (MESA_VERBOSE & VERBOSE_API)
141 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
142 srcx, srcy, width, height,
143 _mesa_lookup_enum_by_nr(type),
144 _mesa_lookup_enum_by_nr(ctx->ReadBuffer->ColorReadBuffer),
145 _mesa_lookup_enum_by_nr(ctx->DrawBuffer->ColorDrawBuffer[0]),
146 IROUND(ctx->Current.RasterPos[0]),
147 IROUND(ctx->Current.RasterPos[1]));
149 if (width < 0 || height < 0) {
150 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
154 /* Note: more detailed 'type' checking is done by the
155 * _mesa_source/dest_buffer_exists() calls below. That's where we
156 * check if the stencil buffer exists, etc.
158 if (type != GL_COLOR &&
160 type != GL_STENCIL &&
161 type != GL_DEPTH_STENCIL) {
162 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyPixels(type=%s)",
163 _mesa_lookup_enum_by_nr(type));
167 /* We're not using the current vertex program, and the driver may install
168 * it's own. Note: this may dirty some state.
170 _mesa_set_vp_override(ctx, GL_TRUE);
172 /* Note: this call does state validation */
173 if (!_mesa_valid_to_render(ctx, "glCopyPixels")) {
174 goto end; /* the error code was recorded */
177 /* Check read buffer's status (draw buffer was already checked) */
178 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
179 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
180 "glCopyPixels(incomplete framebuffer)" );
184 if (!_mesa_source_buffer_exists(ctx, type) ||
185 !_mesa_dest_buffer_exists(ctx, type)) {
186 _mesa_error(ctx, GL_INVALID_OPERATION,
187 "glCopyPixels(missing source or dest buffer)");
191 if (!ctx->Current.RasterPosValid || width ==0 || height == 0) {
192 goto end; /* no-op, not an error */
195 if (ctx->RenderMode == GL_RENDER) {
196 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
197 if (width > 0 && height > 0) {
198 GLint destx = IROUND(ctx->Current.RasterPos[0]);
199 GLint desty = IROUND(ctx->Current.RasterPos[1]);
200 ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
204 else if (ctx->RenderMode == GL_FEEDBACK) {
205 FLUSH_CURRENT( ctx, 0 );
206 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
207 _mesa_feedback_vertex( ctx,
208 ctx->Current.RasterPos,
209 ctx->Current.RasterColor,
210 ctx->Current.RasterTexCoords[0] );
213 ASSERT(ctx->RenderMode == GL_SELECT);
214 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
218 _mesa_set_vp_override(ctx, GL_FALSE);
222 static void GLAPIENTRY
223 _mesa_Bitmap( GLsizei width, GLsizei height,
224 GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
225 const GLubyte *bitmap )
227 GET_CURRENT_CONTEXT(ctx);
228 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
230 if (width < 0 || height < 0) {
231 _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
235 if (!ctx->Current.RasterPosValid) {
236 return; /* do nothing */
239 /* Note: this call does state validation */
240 if (!_mesa_valid_to_render(ctx, "glBitmap")) {
241 /* the error code was recorded */
245 if (ctx->RenderMode == GL_RENDER) {
246 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
247 if (width > 0 && height > 0) {
248 const GLfloat epsilon = 0.0001F;
249 GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
250 GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
252 if (ctx->Unpack.BufferObj->Name) {
253 /* unpack from PBO */
254 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
255 GL_COLOR_INDEX, GL_BITMAP,
256 (GLvoid *) bitmap)) {
257 _mesa_error(ctx, GL_INVALID_OPERATION,
258 "glBitmap(invalid PBO access)");
261 if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
262 /* buffer is mapped - that's an error */
263 _mesa_error(ctx, GL_INVALID_OPERATION,
264 "glBitmap(PBO is mapped)");
269 ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
273 else if (ctx->RenderMode == GL_FEEDBACK) {
274 FLUSH_CURRENT(ctx, 0);
275 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
276 _mesa_feedback_vertex( ctx,
277 ctx->Current.RasterPos,
278 ctx->Current.RasterColor,
279 ctx->Current.RasterTexCoords[0] );
282 ASSERT(ctx->RenderMode == GL_SELECT);
283 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
287 /* update raster position */
288 ctx->Current.RasterPos[0] += xmove;
289 ctx->Current.RasterPos[1] += ymove;
294 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
296 SET_Bitmap(disp, _mesa_Bitmap);
297 SET_CopyPixels(disp, _mesa_CopyPixels);
298 SET_DrawPixels(disp, _mesa_DrawPixels);
302 #endif /* FEATURE_drawpix */