OSDN Git Service

Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[android-x86/external-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 "image.h"
34 #include "readpix.h"
35 #include "state.h"
36 #include "glapi/dispatch.h"
37
38
39 #if FEATURE_drawpix
40
41
42 /**
43  * If a fragment program is enabled, check that it's valid.
44  * \return GL_TRUE if valid, GL_FALSE otherwise
45  */
46 static GLboolean
47 valid_fragment_program(GLcontext *ctx)
48 {
49    return !(ctx->FragmentProgram.Enabled && !ctx->FragmentProgram._Enabled);
50 }
51
52
53 /*
54  * Execute glDrawPixels
55  */
56 static void GLAPIENTRY
57 _mesa_DrawPixels( GLsizei width, GLsizei height,
58                   GLenum format, GLenum type, const GLvoid *pixels )
59 {
60    GET_CURRENT_CONTEXT(ctx);
61    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
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     * it's own.
70     */
71    _mesa_set_vp_override(ctx, GL_TRUE);
72
73    if (ctx->NewState) {
74       _mesa_update_state(ctx);
75    }
76
77    if (!valid_fragment_program(ctx)) {
78       _mesa_error(ctx, GL_INVALID_OPERATION,
79                   "glDrawPixels (invalid fragment program)");
80       goto end;
81    }
82
83    if (_mesa_error_check_format_type(ctx, format, type, GL_TRUE)) {
84       /* the error was already recorded */
85       goto end;
86    }
87
88    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
89       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
90                   "glDrawPixels(incomplete framebuffer)" );
91       goto end;
92    }
93
94    if (!ctx->Current.RasterPosValid) {
95       goto end; /* no-op, not an error */
96    }
97
98    if (ctx->RenderMode == GL_RENDER) {
99       if (width > 0 && height > 0) {
100          /* Round, to satisfy conformance tests (matches SGI's OpenGL) */
101          GLint x = IROUND(ctx->Current.RasterPos[0]);
102          GLint y = IROUND(ctx->Current.RasterPos[1]);
103
104          if (ctx->Unpack.BufferObj->Name) {
105             /* unpack from PBO */
106             if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
107                                            format, type, pixels)) {
108                _mesa_error(ctx, GL_INVALID_OPERATION,
109                            "glDrawPixels(invalid PBO access)");
110                goto end;
111             }
112             if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
113                /* buffer is mapped - that's an error */
114                _mesa_error(ctx, GL_INVALID_OPERATION,
115                            "glDrawPixels(PBO is mapped)");
116                goto end;
117             }
118          }
119
120          ctx->Driver.DrawPixels(ctx, x, y, width, height, format, type,
121                                 &ctx->Unpack, pixels);
122       }
123    }
124    else if (ctx->RenderMode == GL_FEEDBACK) {
125       /* Feedback the current raster pos info */
126       FLUSH_CURRENT( ctx, 0 );
127       _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
128       _mesa_feedback_vertex( ctx,
129                              ctx->Current.RasterPos,
130                              ctx->Current.RasterColor,
131                              ctx->Current.RasterIndex,
132                              ctx->Current.RasterTexCoords[0] );
133    }
134    else {
135       ASSERT(ctx->RenderMode == GL_SELECT);
136       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
137    }
138
139 end:
140    _mesa_set_vp_override(ctx, GL_FALSE);
141 }
142
143
144 static void GLAPIENTRY
145 _mesa_CopyPixels( GLint srcx, GLint srcy, GLsizei width, GLsizei height,
146                   GLenum type )
147 {
148    GET_CURRENT_CONTEXT(ctx);
149    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
150
151    if (width < 0 || height < 0) {
152       _mesa_error(ctx, GL_INVALID_VALUE, "glCopyPixels(width or height < 0)");
153       return;
154    }
155
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.
159     */
160    if (type != GL_COLOR &&
161        type != GL_DEPTH &&
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));
166       return;
167    }
168
169    /* We're not using the current vertex program, and the driver may install
170     * it's own.
171     */
172    _mesa_set_vp_override(ctx, GL_TRUE);
173
174    if (ctx->NewState) {
175       _mesa_update_state(ctx);
176    }
177
178    if (!valid_fragment_program(ctx)) {
179       _mesa_error(ctx, GL_INVALID_OPERATION,
180                   "glCopyPixels (invalid fragment program)");
181       goto end;
182    }
183
184    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
185        ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
186       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
187                   "glCopyPixels(incomplete framebuffer)" );
188       goto end;
189    }
190
191    if (!_mesa_source_buffer_exists(ctx, type) ||
192        !_mesa_dest_buffer_exists(ctx, type)) {
193       _mesa_error(ctx, GL_INVALID_OPERATION,
194                   "glCopyPixels(missing source or dest buffer)");
195       goto end;
196    }
197
198    if (!ctx->Current.RasterPosValid || width ==0 || height == 0) {
199       goto end; /* no-op, not an error */
200    }
201
202    if (ctx->RenderMode == GL_RENDER) {
203       /* Round to satisfy conformance tests (matches SGI's OpenGL) */
204       if (width > 0 && height > 0) {
205          GLint destx = IROUND(ctx->Current.RasterPos[0]);
206          GLint desty = IROUND(ctx->Current.RasterPos[1]);
207          ctx->Driver.CopyPixels( ctx, srcx, srcy, width, height, destx, desty,
208                                  type );
209       }
210    }
211    else if (ctx->RenderMode == GL_FEEDBACK) {
212       FLUSH_CURRENT( ctx, 0 );
213       _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_COPY_PIXEL_TOKEN );
214       _mesa_feedback_vertex( ctx, 
215                              ctx->Current.RasterPos,
216                              ctx->Current.RasterColor,
217                              ctx->Current.RasterIndex,
218                              ctx->Current.RasterTexCoords[0] );
219    }
220    else {
221       ASSERT(ctx->RenderMode == GL_SELECT);
222       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
223    }
224
225 end:
226    _mesa_set_vp_override(ctx, GL_FALSE);
227 }
228
229
230 static void GLAPIENTRY
231 _mesa_Bitmap( GLsizei width, GLsizei height,
232               GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove,
233               const GLubyte *bitmap )
234 {
235    GET_CURRENT_CONTEXT(ctx);
236    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
237
238    if (width < 0 || height < 0) {
239       _mesa_error( ctx, GL_INVALID_VALUE, "glBitmap(width or height < 0)" );
240       return;
241    }
242
243    if (!ctx->Current.RasterPosValid) {
244       return;    /* do nothing */
245    }
246
247    if (ctx->NewState) {
248       _mesa_update_state(ctx);
249    }
250
251    if (!valid_fragment_program(ctx)) {
252       _mesa_error(ctx, GL_INVALID_OPERATION,
253                   "glBitmap (invalid fragment program)");
254       return;
255    }
256
257    if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
258       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
259                   "glBitmap(incomplete framebuffer)");
260       return;
261    }
262
263    if (ctx->RenderMode == GL_RENDER) {
264       /* Truncate, to satisfy conformance tests (matches SGI's OpenGL). */
265       if (width > 0 && height > 0) {
266          const GLfloat epsilon = 0.0001F;
267          GLint x = IFLOOR(ctx->Current.RasterPos[0] + epsilon - xorig);
268          GLint y = IFLOOR(ctx->Current.RasterPos[1] + epsilon - yorig);
269
270          if (ctx->Unpack.BufferObj->Name) {
271             /* unpack from PBO */
272             if (!_mesa_validate_pbo_access(2, &ctx->Unpack, width, height, 1,
273                                            GL_COLOR_INDEX, GL_BITMAP,
274                                            (GLvoid *) bitmap)) {
275                _mesa_error(ctx, GL_INVALID_OPERATION,
276                            "glBitmap(invalid PBO access)");
277                return;
278             }
279             if (_mesa_bufferobj_mapped(ctx->Unpack.BufferObj)) {
280                /* buffer is mapped - that's an error */
281                _mesa_error(ctx, GL_INVALID_OPERATION,
282                            "glBitmap(PBO is mapped)");
283                return;
284             }
285          }
286
287          ctx->Driver.Bitmap( ctx, x, y, width, height, &ctx->Unpack, bitmap );
288       }
289    }
290 #if _HAVE_FULL_GL
291    else if (ctx->RenderMode == GL_FEEDBACK) {
292       FLUSH_CURRENT(ctx, 0);
293       _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_BITMAP_TOKEN );
294       _mesa_feedback_vertex( ctx,
295                              ctx->Current.RasterPos,
296                              ctx->Current.RasterColor,
297                              ctx->Current.RasterIndex, 
298                              ctx->Current.RasterTexCoords[0] );
299    }
300    else {
301       ASSERT(ctx->RenderMode == GL_SELECT);
302       /* Do nothing.  See OpenGL Spec, Appendix B, Corollary 6. */
303    }
304 #endif
305
306    /* update raster position */
307    ctx->Current.RasterPos[0] += xmove;
308    ctx->Current.RasterPos[1] += ymove;
309 }
310
311
312 void
313 _mesa_init_drawpix_dispatch(struct _glapi_table *disp)
314 {
315    SET_Bitmap(disp, _mesa_Bitmap);
316    SET_CopyPixels(disp, _mesa_CopyPixels);
317    SET_DrawPixels(disp, _mesa_DrawPixels);
318 }
319
320
321 #endif /* FEATURE_drawpix */