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