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 (MESA_VERBOSE
& VERBOSE_API
)
53 _mesa_debug(ctx
, "glDrawPixels(%d, %d, %s, %s, %p) // to %s at %d, %d\n",
55 _mesa_lookup_enum_by_nr(format
),
56 _mesa_lookup_enum_by_nr(type
),
58 _mesa_lookup_enum_by_nr(ctx
->DrawBuffer
->ColorDrawBuffer
[0]),
59 IROUND(ctx
->Current
.RasterPos
[0]),
60 IROUND(ctx
->Current
.RasterPos
[1]));
63 if (width
< 0 || height
< 0) {
64 _mesa_error( ctx
, GL_INVALID_VALUE
, "glDrawPixels(width or height < 0" );
68 /* We're not using the current vertex program, and the driver may install
69 * its own. Note: this may dirty some state.
71 _mesa_set_vp_override(ctx
, GL_TRUE
);
73 /* Note: this call does state validation */
74 if (!_mesa_valid_to_render(ctx
, "glDrawPixels")) {
75 goto end
; /* the error code was recorded */
78 if (_mesa_error_check_format_type(ctx
, format
, type
, GL_TRUE
)) {
79 goto end
; /* the error code was recorded */
82 if (!ctx
->Current
.RasterPosValid
) {
83 goto end
; /* no-op, not an error */
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]);
92 if (ctx
->Unpack
.BufferObj
->Name
) {
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)");
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)");
108 ctx
->Driver
.DrawPixels(ctx
, x
, y
, width
, height
, format
, type
,
109 &ctx
->Unpack
, pixels
);
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] );
122 ASSERT(ctx
->RenderMode
== GL_SELECT
);
123 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
127 _mesa_set_vp_override(ctx
, GL_FALSE
);
131 static void GLAPIENTRY
132 _mesa_CopyPixels( GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
135 GET_CURRENT_CONTEXT(ctx
);
136 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
140 if (MESA_VERBOSE
& VERBOSE_API
)
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]));
150 if (width
< 0 || height
< 0) {
151 _mesa_error(ctx
, GL_INVALID_VALUE
, "glCopyPixels(width or height < 0)");
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.
159 if (type
!= GL_COLOR
&&
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
));
168 /* We're not using the current vertex program, and the driver may install
169 * it's own. Note: this may dirty some state.
171 _mesa_set_vp_override(ctx
, GL_TRUE
);
173 /* Note: this call does state validation */
174 if (!_mesa_valid_to_render(ctx
, "glCopyPixels")) {
175 goto end
; /* the error code was recorded */
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)" );
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)");
192 if (!ctx
->Current
.RasterPosValid
|| width
==0 || height
== 0) {
193 goto end
; /* no-op, not an error */
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
,
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] );
214 ASSERT(ctx
->RenderMode
== GL_SELECT
);
215 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
219 _mesa_set_vp_override(ctx
, GL_FALSE
);
223 static void GLAPIENTRY
224 _mesa_Bitmap( GLsizei width
, GLsizei height
,
225 GLfloat xorig
, GLfloat yorig
, GLfloat xmove
, GLfloat ymove
,
226 const GLubyte
*bitmap
)
228 GET_CURRENT_CONTEXT(ctx
);
229 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
231 if (width
< 0 || height
< 0) {
232 _mesa_error( ctx
, GL_INVALID_VALUE
, "glBitmap(width or height < 0)" );
236 if (!ctx
->Current
.RasterPosValid
) {
237 return; /* do nothing */
240 /* Note: this call does state validation */
241 if (!_mesa_valid_to_render(ctx
, "glBitmap")) {
242 /* the error code was recorded */
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
);
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)");
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)");
270 ctx
->Driver
.Bitmap( ctx
, x
, y
, width
, height
, &ctx
->Unpack
, bitmap
);
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] );
283 ASSERT(ctx
->RenderMode
== GL_SELECT
);
284 /* Do nothing. See OpenGL Spec, Appendix B, Corollary 6. */
288 /* update raster position */
289 ctx
->Current
.RasterPos
[0] += xmove
;
290 ctx
->Current
.RasterPos
[1] += ymove
;
295 _mesa_init_drawpix_dispatch(struct _glapi_table
*disp
)
297 SET_Bitmap(disp
, _mesa_Bitmap
);
298 SET_CopyPixels(disp
, _mesa_CopyPixels
);
299 SET_DrawPixels(disp
, _mesa_DrawPixels
);
303 #endif /* FEATURE_drawpix */