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
);
141 if (MESA_VERBOSE
& VERBOSE_API
)
143 "glCopyPixels(%d, %d, %d, %d, %s) // from %s to %s at %d, %d\n",
144 srcx
, srcy
, width
, height
,
145 _mesa_lookup_enum_by_nr(type
),
146 _mesa_lookup_enum_by_nr(ctx
->ReadBuffer
->ColorReadBuffer
),
147 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
148 IROUND(ctx
->Current
.RasterPos
[0]),
149 IROUND(ctx
->Current
.RasterPos
[1]));
151 if (width
< 0 || height
< 0) {
152 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
156 /* Note: more detailed 'type' checking is done by the
157 * _mesa_source/dest_buffer_exists() calls below. That's where we
158 * check if the stencil buffer exists, etc.
160 if (type
!= GL_COLOR
&&
162 type
!= GL_STENCIL
&&
163 type
!= GL_DEPTH_STENCIL
) {
164 _mesa_error(ctx
, GL_INVALID_ENUM
, "glCopyPixels(type=%s)",
165 _mesa_lookup_enum_by_nr(type
));
169 /* We're not using the current vertex program, and the driver may install
170 * it's own. Note: this may dirty some state.
172 _mesa_set_vp_override(ctx
, GL_TRUE
);
174 /* Note: this call does state validation */
175 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
176 goto end
; /* the error code was recorded */
179 /* Check read buffer's status (draw buffer was already checked) */
180 if (ctx
->ReadBuffer
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
181 _mesa_error(ctx
, GL_INVALID_FRAMEBUFFER_OPERATION_EXT
,
182 "glCopyPixels(incomplete framebuffer)" );
186 if (!_mesa_source_buffer_exists(ctx
, type
) ||
187 !_mesa_dest_buffer_exists(ctx
, type
)) {
188 _mesa_error(ctx
, GL_INVALID_OPERATION
,
189 "glCopyPixels(missing source or dest buffer)");
193 if (!ctx
->Current
.RasterPosValid
|| width
==0 || height
== 0) {
194 goto end
; /* no-op, not an error */
197 if (ctx
->RenderMode
== GL_RENDER
) {
198 /* Round to satisfy conformance tests (matches SGI's OpenGL) */
199 if (width
> 0 && height
> 0) {
200 GLint destx
= IROUND(ctx
->Current
.RasterPos
[0]);
201 GLint desty
= IROUND(ctx
->Current
.RasterPos
[1]);
202 ctx
->Driver
.CopyPixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
,
206 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
207 FLUSH_CURRENT( ctx
, 0 );
208 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_COPY_PIXEL_TOKEN
);
209 _mesa_feedback_vertex( ctx
,
210 ctx
->Current
.RasterPos
,
211 ctx
->Current
.RasterColor
,
212 ctx
->Current
.RasterTexCoords
[0] );
215 ASSERT(ctx
->RenderMode
== GL_SELECT
);
216 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
220 _mesa_set_vp_override(ctx
, GL_FALSE
);
224 static void GLAPIENTRY
225 _mesa_Bitmap( GLsizei width
, GLsizei height
,
226 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
227 const GLubyte
*bitmap
)
229 GET_CURRENT_CONTEXT(ctx
);
230 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
232 if (width
< 0 || height
< 0) {
233 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
237 if (!ctx
->Current
.RasterPosValid
) {
238 return; /* do nothing */
241 /* Note: this call does state validation */
242 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
243 /* the error code was recorded */
247 if (ctx
->RenderMode
== GL_RENDER
) {
248 /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
249 if (width
> 0 && height
> 0) {
250 const GLfloat epsilon
= 0.0001F
;
251 GLint x
= IFLOOR(ctx
->Current
.RasterPos
[0] + epsilon
- xorig
);
252 GLint y
= IFLOOR(ctx
->Current
.RasterPos
[1] + epsilon
- yorig
);
254 if (ctx
->Unpack
.BufferObj
->Name
) {
255 /* unpack from PBO */
256 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, width
, height
, 1,
257 GL_COLOR_INDEX
, GL_BITMAP
,
258 (GLvoid
*) bitmap
)) {
259 _mesa_error(ctx
, GL_INVALID_OPERATION
,
260 "glBitmap(invalid PBO access)");
263 if (_mesa_bufferobj_mapped(ctx
->Unpack
.BufferObj
)) {
264 /* buffer is mapped - that's an error */
265 _mesa_error(ctx
, GL_INVALID_OPERATION
,
266 "glBitmap(PBO is mapped)");
271 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
275 else if (ctx
->RenderMode
== GL_FEEDBACK
) {
276 FLUSH_CURRENT(ctx
, 0);
277 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_BITMAP_TOKEN
);
278 _mesa_feedback_vertex( ctx
,
279 ctx
->Current
.RasterPos
,
280 ctx
->Current
.RasterColor
,
281 ctx
->Current
.RasterTexCoords
[0] );
284 ASSERT(ctx
->RenderMode
== GL_SELECT
);
285 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
289 /* update raster position */
290 ctx
->Current
.RasterPos
[0] += xmove
;
291 ctx
->Current
.RasterPos
[1] += ymove
;
296 _mesa_init_drawpix_dispatch(struct _glapi_table
*disp
)
298 SET_Bitmap(disp
, _mesa_Bitmap
);
299 SET_CopyPixels(disp
, _mesa_CopyPixels
);
300 SET_DrawPixels(disp
, _mesa_DrawPixels
);
304 #endif /* FEATURE_drawpix */