summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/pixelstore.c
blob: fc815337b8dfd6bcfd1e11b1a51413a13e174ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file pixelstore.c
 * glPixelStore functions.
 */


#include "glheader.h"
#include "bufferobj.h"
#include "context.h"
#include "pixelstore.h"
#include "mtypes.h"


void GLAPIENTRY
_mesa_PixelStorei( GLenum pname, GLint param )
{
   /* NOTE: this call can't be compiled into the display list */
   GET_CURRENT_CONTEXT(ctx);

   switch (pname) {
      case GL_PACK_SWAP_BYTES:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         ctx->Pack.SwapBytes = param ? GL_TRUE : GL_FALSE;
         break;
      case GL_PACK_LSB_FIRST:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         ctx->Pack.LsbFirst = param ? GL_TRUE : GL_FALSE;
         break;
      case GL_PACK_ROW_LENGTH:
         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.RowLength = param;
         break;
      case GL_PACK_IMAGE_HEIGHT:
         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.ImageHeight = param;
         break;
      case GL_PACK_SKIP_PIXELS:
         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.SkipPixels = param;
         break;
      case GL_PACK_SKIP_ROWS:
         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.SkipRows = param;
         break;
      case GL_PACK_SKIP_IMAGES:
         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.SkipImages = param;
         break;
      case GL_PACK_ALIGNMENT:
         if (param!=1 && param!=2 && param!=4 && param!=8)
            goto invalid_value_error;
         ctx->Pack.Alignment = param;
         break;
      case GL_PACK_INVERT_MESA:
         if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.MESA_pack_invert)
            goto invalid_enum_error;
         ctx->Pack.Invert = param;
         break;
      case GL_PACK_COMPRESSED_BLOCK_WIDTH:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.CompressedBlockWidth = param;
         break;
      case GL_PACK_COMPRESSED_BLOCK_HEIGHT:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.CompressedBlockHeight = param;
         break;
      case GL_PACK_COMPRESSED_BLOCK_DEPTH:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.CompressedBlockDepth = param;
         break;
      case GL_PACK_COMPRESSED_BLOCK_SIZE:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Pack.CompressedBlockSize = param;
         break;

      case GL_UNPACK_SWAP_BYTES:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         ctx->Unpack.SwapBytes = param ? GL_TRUE : GL_FALSE;
         break;
      case GL_UNPACK_LSB_FIRST:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         ctx->Unpack.LsbFirst = param ? GL_TRUE : GL_FALSE;
         break;
      case GL_UNPACK_ROW_LENGTH:
         if (ctx->API == API_OPENGLES)
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.RowLength = param;
         break;
      case GL_UNPACK_IMAGE_HEIGHT:
         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.ImageHeight = param;
         break;
      case GL_UNPACK_SKIP_PIXELS:
         if (ctx->API == API_OPENGLES)
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.SkipPixels = param;
         break;
      case GL_UNPACK_SKIP_ROWS:
         if (ctx->API == API_OPENGLES)
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.SkipRows = param;
         break;
      case GL_UNPACK_SKIP_IMAGES:
         if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
            goto invalid_enum_error;
         if (param < 0)
            goto invalid_value_error;
         ctx->Unpack.SkipImages = param;
         break;
      case GL_UNPACK_ALIGNMENT:
         if (param!=1 && param!=2 && param!=4 && param!=8)
            goto invalid_value_error;
         ctx->Unpack.Alignment = param;
         break;
      case GL_UNPACK_COMPRESSED_BLOCK_WIDTH:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.CompressedBlockWidth = param;
         break;
      case GL_UNPACK_COMPRESSED_BLOCK_HEIGHT:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.CompressedBlockHeight = param;
         break;
      case GL_UNPACK_COMPRESSED_BLOCK_DEPTH:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.CompressedBlockDepth = param;
         break;
      case GL_UNPACK_COMPRESSED_BLOCK_SIZE:
         if (!_mesa_is_desktop_gl(ctx))
            goto invalid_enum_error;
         if (param<0)
            goto invalid_value_error;
         ctx->Unpack.CompressedBlockSize = param;
         break;
      default:
         goto invalid_enum_error;
   }

   return;

invalid_enum_error:
   _mesa_error( ctx, GL_INVALID_ENUM, "glPixelStore" );
   return;

invalid_value_error:
   _mesa_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
   return;
}


void GLAPIENTRY
_mesa_PixelStoref( GLenum pname, GLfloat param )
{
   _mesa_PixelStorei( pname, IROUND(param) );
}



/**
 * Initialize the context's pixel store state.
 */
void
_mesa_init_pixelstore( struct gl_context *ctx )
{
   /* Pixel transfer */
   ctx->Pack.Alignment = 4;
   ctx->Pack.RowLength = 0;
   ctx->Pack.ImageHeight = 0;
   ctx->Pack.SkipPixels = 0;
   ctx->Pack.SkipRows = 0;
   ctx->Pack.SkipImages = 0;
   ctx->Pack.SwapBytes = GL_FALSE;
   ctx->Pack.LsbFirst = GL_FALSE;
   ctx->Pack.Invert = GL_FALSE;
   ctx->Pack.CompressedBlockWidth = 0;
   ctx->Pack.CompressedBlockHeight = 0;
   ctx->Pack.CompressedBlockDepth = 0;
   ctx->Pack.CompressedBlockSize = 0;
   _mesa_reference_buffer_object(ctx, &ctx->Pack.BufferObj,
                                 ctx->Shared->NullBufferObj);
   ctx->Unpack.Alignment = 4;
   ctx->Unpack.RowLength = 0;
   ctx->Unpack.ImageHeight = 0;
   ctx->Unpack.SkipPixels = 0;
   ctx->Unpack.SkipRows = 0;
   ctx->Unpack.SkipImages = 0;
   ctx->Unpack.SwapBytes = GL_FALSE;
   ctx->Unpack.LsbFirst = GL_FALSE;
   ctx->Unpack.Invert = GL_FALSE;
   ctx->Unpack.CompressedBlockWidth = 0;
   ctx->Unpack.CompressedBlockHeight = 0;
   ctx->Unpack.CompressedBlockDepth = 0;
   ctx->Unpack.CompressedBlockSize = 0;
   _mesa_reference_buffer_object(ctx, &ctx->Unpack.BufferObj,
                                 ctx->Shared->NullBufferObj);

   /*
    * _mesa_unpack_image() returns image data in this format.  When we
    * execute image commands (glDrawPixels(), glTexImage(), etc) from
    * within display lists we have to be sure to set the current
    * unpacking parameters to these values!
    */
   ctx->DefaultPacking.Alignment = 1;
   ctx->DefaultPacking.RowLength = 0;
   ctx->DefaultPacking.SkipPixels = 0;
   ctx->DefaultPacking.SkipRows = 0;
   ctx->DefaultPacking.ImageHeight = 0;
   ctx->DefaultPacking.SkipImages = 0;
   ctx->DefaultPacking.SwapBytes = GL_FALSE;
   ctx->DefaultPacking.LsbFirst = GL_FALSE;
   ctx->DefaultPacking.Invert = GL_FALSE;
   _mesa_reference_buffer_object(ctx, &ctx->DefaultPacking.BufferObj,
                                 ctx->Shared->NullBufferObj);
}


/**
 * Check if the given compressed pixel storage parameters are legal.
 * Record a GL error if illegal.
 * \return  true if legal, false if illegal
 */
bool
_mesa_compressed_pixel_storage_error_check(
   struct gl_context *ctx,
   GLint dimensions,
   const struct gl_pixelstore_attrib *packing,
   const char *caller)
{
   if (!_mesa_is_desktop_gl(ctx) || !packing->CompressedBlockSize)
      return true;

   if (packing->CompressedBlockWidth &&
       packing->SkipPixels % packing->CompressedBlockWidth) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(skip-pixels %% block-width)", caller);
      return false;
   }

   if (dimensions > 1 &&
       packing->CompressedBlockHeight &&
       packing->SkipRows % packing->CompressedBlockHeight) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(skip-rows %% block-height)", caller);
      return false;
   }

   if (dimensions > 2 &&
       packing->CompressedBlockDepth &&
       packing->SkipImages % packing->CompressedBlockDepth) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "%s(skip-images %% block-depth)", caller);
      return false;
   }

   return true;
}