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"
43 * Execute glDrawPixels
45 static void GLAPIENTRY
46 _mesa_DrawPixels( GLsizei width
, GLsizei height
,
47 GLenum format
, GLenum type
, const GLvoid
*pixels
)
49 GET_CURRENT_CONTEXT(ctx
);
50 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
52 if (width
< 0 || height
< 0) {
53 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDrawPixels(width or height < 0" );
57 /* We're not using the current vertex program, and the driver may install
58 * its own. Note: this may dirty some state.
60 _mesa_set_vp_override(ctx
, GL_TRUE
);
62 /* Note: this call does state validation */
63 if (!_mesa_valid_to_render(ctx
, "glDrawPixels")) {
64 goto end
; /* the error code was recorded */
67 if (_mesa_error_check_format_type(ctx
, format
, type
, GL_TRUE
)) {
68 goto end
; /* the error code was recorded */
71 if (!ctx
->Current
.RasterPosValid
) {
72 goto end
; /* no-op, not an error */
75 if (ctx
->RenderMode
== GL_RENDER
) {
76 if (width
> 0 && height
> 0) {
77 /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
78 GLint x
= IROUND(ctx
->Current
.RasterPos
[0]);
79 GLint y
= IROUND(ctx
->Current
.RasterPos
[1]);
81 if (ctx
->Unpack
.BufferObj
->Name
) {
83 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
, 1,
84 format
, type
, pixels
)) {
85 _mesa_error(ctx
, GL_INVALID_OPERATION
,
86 "glDrawPixels(invalid PBO access)");
89 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
90 /* buffer is mapped - that's an error */
91 _mesa_error(ctx
, GL_INVALID_OPERATION
,
92 "glDrawPixels(PBO is mapped)");
97 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
98 &ctx
->Unpack
, pixels
);
101 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
102 /* Feedback the current raster pos info */
103 FLUSH_CURRENT( ctx
, 0 );
104 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_DRAW_PIXEL_TOKEN
);
105 _mesa_feedback_vertex( ctx
,
106 ctx
->Current
.RasterPos
,
107 ctx
->Current
.RasterColor
,
108 ctx
->Current
.RasterTexCoords
[0] );
111 ASSERT(ctx
->RenderMode
== GL_SELECT
);
112 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
116 _mesa_set_vp_override(ctx
, GL_FALSE
);
120 static void GLAPIENTRY
121 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
124 GET_CURRENT_CONTEXT(ctx
);
125 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
127 if (width
< 0 || height
< 0) {
128 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
132 /* Note: more detailed 'type' checking is done by the
133 * _mesa_source/dest_buffer_exists() calls below. That's where we
134 * check if the stencil buffer exists, etc.
136 if (type
!= GL_COLOR
&&
138 type
!= GL_STENCIL
&&
139 type
!= GL_DEPTH_STENCIL
) {
140 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
141 _mesa_lookup_enum_by_nr(type
));
145 /* We're not using the current vertex program, and the driver may install
146 * it's own. Note: this may dirty some state.
148 _mesa_set_vp_override(ctx
, GL_TRUE
);
150 /* Note: this call does state validation */
151 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
152 goto end
; /* the error code was recorded */
155 /* Check read buffer's status (draw buffer was already checked) */
156 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
157 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
158 "glCopyPixels(incomplete framebuffer)" );
162 if (!_mesa_source_buffer_exists(ctx
, type
) ||
163 !_mesa_dest_buffer_exists(ctx
, type
)) {
164 _mesa_error(ctx
, GL_INVALID_OPERATION
,
165 "glCopyPixels(missing source or dest buffer)");
169 if (!ctx
->Current
.RasterPosValid
|| width
==0 || height
== 0) {
170 goto end
; /* no-op, not an error */
173 if (ctx
->RenderMode
== GL_RENDER
) {
174 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
175 if (width
> 0 && height
> 0) {
176 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
177 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
178 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
182 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
183 FLUSH_CURRENT( ctx
, 0 );
184 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
185 _mesa_feedback_vertex( ctx
,
186 ctx
->Current
.RasterPos
,
187 ctx
->Current
.RasterColor
,
188 ctx
->Current
.RasterTexCoords
[0] );
191 ASSERT(ctx
->RenderMode
== GL_SELECT
);
192 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
196 _mesa_set_vp_override(ctx
, GL_FALSE
);
200 static void GLAPIENTRY
201 _mesa_Bitmap( GLsizei width
, GLsizei height
,
202 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
203 const GLubyte
*bitmap
)
205 GET_CURRENT_CONTEXT(ctx
);
206 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
208 if (width
< 0 || height
< 0) {
209 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
213 if (!ctx
->Current
.RasterPosValid
) {
214 return; /* do nothing */
217 /* Note: this call does state validation */
218 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
219 /* the error code was recorded */
223 if (ctx
->RenderMode
== GL_RENDER
) {
224 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
225 if (width
> 0 && height
> 0) {
226 const GLfloat epsilon
= 0.0001F
;
227 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
228 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
230 if (ctx
->Unpack
.BufferObj
->Name
) {
231 /* unpack from PBO */
232 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
, 1,
233 GL_COLOR_INDEX
, GL_BITMAP
,
234 (GLvoid
*) bitmap
)) {
235 _mesa_error(ctx
, GL_INVALID_OPERATION
,
236 "glBitmap(invalid PBO access)");
239 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
240 /* buffer is mapped - that's an error */
241 _mesa_error(ctx
, GL_INVALID_OPERATION
,
242 "glBitmap(PBO is mapped)");
247 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
251 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
252 FLUSH_CURRENT(ctx
, 0);
253 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
254 _mesa_feedback_vertex( ctx
,
255 ctx
->Current
.RasterPos
,
256 ctx
->Current
.RasterColor
,
257 ctx
->Current
.RasterTexCoords
[0] );
260 ASSERT(ctx
->RenderMode
== GL_SELECT
);
261 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
265 /* update raster position */
266 ctx
->Current
.RasterPos
[0] += xmove
;
267 ctx
->Current
.RasterPos
[1] += ymove
;
272 _mesa_init_drawpix_dispatch(struct _glapi_table
*disp
)
274 SET_Bitmap(disp
, _mesa_Bitmap
);
275 SET_CopyPixels(disp
, _mesa_CopyPixels
);
276 SET_DrawPixels(disp
, _mesa_DrawPixels
);
280 #endif /* FEATURE_drawpix */