mesa: generate error if pbo offset is not aligned with the size of specified type
[mesa.git] / src / mesa / main / pbo.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009-2011 VMware, Inc. 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 * THE AUTHORS 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
26 /**
27 * \file pbo.c
28 * \brief Functions related to Pixel Buffer Objects.
29 */
30
31
32
33 #include "glheader.h"
34 #include "bufferobj.h"
35 #include "image.h"
36 #include "imports.h"
37 #include "mtypes.h"
38 #include "pbo.h"
39
40
41
42 /**
43 * When we're about to read pixel data out of a PBO (via glDrawPixels,
44 * glTexImage, etc) or write data into a PBO (via glReadPixels,
45 * glGetTexImage, etc) we call this function to check that we're not
46 * going to read/write out of bounds.
47 *
48 * XXX This would also be a convenient time to check that the PBO isn't
49 * currently mapped. Whoever calls this function should check for that.
50 * Remember, we can't use a PBO when it's mapped!
51 *
52 * If we're not using a PBO, this is a no-op.
53 *
54 * \param width width of image to read/write
55 * \param height height of image to read/write
56 * \param depth depth of image to read/write
57 * \param format format of image to read/write
58 * \param type datatype of image to read/write
59 * \param clientMemSize the maximum number of bytes to read/write
60 * \param ptr the user-provided pointer/offset
61 * \return GL_TRUE if the buffer access is OK, GL_FALSE if the access would
62 * go out of bounds.
63 */
64 GLboolean
65 _mesa_validate_pbo_access(GLuint dimensions,
66 const struct gl_pixelstore_attrib *pack,
67 GLsizei width, GLsizei height, GLsizei depth,
68 GLenum format, GLenum type, GLsizei clientMemSize,
69 const GLvoid *ptr)
70 {
71 const GLvoid *start, *end, *offset;
72 const GLubyte *sizeAddr; /* buffer size, cast to a pointer */
73
74 /* If no PBO is bound, 'ptr' is a pointer to client memory containing
75 'clientMemSize' bytes.
76 If a PBO is bound, 'ptr' is an offset into the bound PBO.
77 In that case 'clientMemSize' is ignored: we just use the PBO's size.
78 */
79 if (!_mesa_is_bufferobj(pack->BufferObj)) {
80 offset = 0;
81 sizeAddr = ((const GLubyte *) 0) + clientMemSize;
82 } else {
83 offset = ptr;
84 sizeAddr = ((const GLubyte *) 0) + pack->BufferObj->Size;
85 /* The ARB_pixel_buffer_object spec says:
86 * "INVALID_OPERATION is generated by ColorTable, ColorSubTable,
87 * ConvolutionFilter2D, ConvolutionFilter1D, SeparableFilter2D,
88 * TexImage1D, TexImage2D, TexImage3D, TexSubImage1D,
89 * TexSubImage2D, TexSubImage3D, and DrawPixels if the current
90 * PIXEL_UNPACK_BUFFER_BINDING_ARB value is non-zero and the data
91 * parameter is not evenly divisible into the number of basic machine
92 * units needed to store in memory a datum indicated by the type
93 * parameter."
94 */
95 if (type != GL_BITMAP &&
96 ((GLintptr)offset % _mesa_sizeof_packed_type(type)))
97 return GL_FALSE;
98 }
99
100 if (sizeAddr == 0)
101 /* no buffer! */
102 return GL_FALSE;
103
104 /* get the offset to the first pixel we'll read/write */
105 start = _mesa_image_address(dimensions, pack, offset, width, height,
106 format, type, 0, 0, 0);
107
108 /* get the offset to just past the last pixel we'll read/write */
109 end = _mesa_image_address(dimensions, pack, offset, width, height,
110 format, type, depth-1, height-1, width);
111
112 if ((const GLubyte *) start > sizeAddr) {
113 /* This will catch negative values / wrap-around */
114 return GL_FALSE;
115 }
116 if ((const GLubyte *) end > sizeAddr) {
117 /* Image read/write goes beyond end of buffer */
118 return GL_FALSE;
119 }
120
121 /* OK! */
122 return GL_TRUE;
123 }
124
125
126 /**
127 * For commands that read from a PBO (glDrawPixels, glTexImage,
128 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
129 * and return the pointer into the PBO. If we're not reading from a
130 * PBO, return \p src as-is.
131 * If non-null return, must call _mesa_unmap_pbo_source() when done.
132 *
133 * \return NULL if error, else pointer to start of data
134 */
135 const GLvoid *
136 _mesa_map_pbo_source(struct gl_context *ctx,
137 const struct gl_pixelstore_attrib *unpack,
138 const GLvoid *src)
139 {
140 const GLubyte *buf;
141
142 if (_mesa_is_bufferobj(unpack->BufferObj)) {
143 /* unpack from PBO */
144 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
145 unpack->BufferObj->Size,
146 GL_MAP_READ_BIT,
147 unpack->BufferObj);
148 if (!buf)
149 return NULL;
150
151 buf = ADD_POINTERS(buf, src);
152 }
153 else {
154 /* unpack from normal memory */
155 buf = src;
156 }
157
158 return buf;
159 }
160
161
162 /**
163 * Combine PBO-read validation and mapping.
164 * If any GL errors are detected, they'll be recorded and NULL returned.
165 * \sa _mesa_validate_pbo_access
166 * \sa _mesa_map_pbo_source
167 * A call to this function should have a matching call to
168 * _mesa_unmap_pbo_source().
169 */
170 const GLvoid *
171 _mesa_map_validate_pbo_source(struct gl_context *ctx,
172 GLuint dimensions,
173 const struct gl_pixelstore_attrib *unpack,
174 GLsizei width, GLsizei height, GLsizei depth,
175 GLenum format, GLenum type, GLsizei clientMemSize,
176 const GLvoid *ptr, const char *where)
177 {
178 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
179
180 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
181 format, type, clientMemSize, ptr)) {
182 if (_mesa_is_bufferobj(unpack->BufferObj)) {
183 _mesa_error(ctx, GL_INVALID_OPERATION,
184 "%s(out of bounds PBO access)", where);
185 } else {
186 _mesa_error(ctx, GL_INVALID_OPERATION,
187 "%s(out of bounds access: bufSize (%d) is too small)",
188 where, clientMemSize);
189 }
190 return NULL;
191 }
192
193 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
194 /* non-PBO access: no further validation to be done */
195 return ptr;
196 }
197
198 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
199 /* buffer is already mapped - that's an error */
200 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
201 return NULL;
202 }
203
204 ptr = _mesa_map_pbo_source(ctx, unpack, ptr);
205 return ptr;
206 }
207
208
209 /**
210 * Counterpart to _mesa_map_pbo_source()
211 */
212 void
213 _mesa_unmap_pbo_source(struct gl_context *ctx,
214 const struct gl_pixelstore_attrib *unpack)
215 {
216 ASSERT(unpack != &ctx->Pack); /* catch pack/unpack mismatch */
217 if (_mesa_is_bufferobj(unpack->BufferObj)) {
218 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
219 }
220 }
221
222
223 /**
224 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
225 * if we're writing to a PBO, map it write-only and return the pointer
226 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
227 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
228 *
229 * \return NULL if error, else pointer to start of data
230 */
231 void *
232 _mesa_map_pbo_dest(struct gl_context *ctx,
233 const struct gl_pixelstore_attrib *pack,
234 GLvoid *dest)
235 {
236 void *buf;
237
238 if (_mesa_is_bufferobj(pack->BufferObj)) {
239 /* pack into PBO */
240 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
241 pack->BufferObj->Size,
242 GL_MAP_WRITE_BIT,
243 pack->BufferObj);
244 if (!buf)
245 return NULL;
246
247 buf = ADD_POINTERS(buf, dest);
248 }
249 else {
250 /* pack to normal memory */
251 buf = dest;
252 }
253
254 return buf;
255 }
256
257
258 /**
259 * Combine PBO-write validation and mapping.
260 * If any GL errors are detected, they'll be recorded and NULL returned.
261 * \sa _mesa_validate_pbo_access
262 * \sa _mesa_map_pbo_dest
263 * A call to this function should have a matching call to
264 * _mesa_unmap_pbo_dest().
265 */
266 GLvoid *
267 _mesa_map_validate_pbo_dest(struct gl_context *ctx,
268 GLuint dimensions,
269 const struct gl_pixelstore_attrib *unpack,
270 GLsizei width, GLsizei height, GLsizei depth,
271 GLenum format, GLenum type, GLsizei clientMemSize,
272 GLvoid *ptr, const char *where)
273 {
274 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
275
276 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
277 format, type, clientMemSize, ptr)) {
278 if (_mesa_is_bufferobj(unpack->BufferObj)) {
279 _mesa_error(ctx, GL_INVALID_OPERATION,
280 "%s(out of bounds PBO access)", where);
281 } else {
282 _mesa_error(ctx, GL_INVALID_OPERATION,
283 "%s(out of bounds access: bufSize (%d) is too small)",
284 where, clientMemSize);
285 }
286 return NULL;
287 }
288
289 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
290 /* non-PBO access: no further validation to be done */
291 return ptr;
292 }
293
294 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
295 /* buffer is already mapped - that's an error */
296 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
297 return NULL;
298 }
299
300 ptr = _mesa_map_pbo_dest(ctx, unpack, ptr);
301 return ptr;
302 }
303
304
305 /**
306 * Counterpart to _mesa_map_pbo_dest()
307 */
308 void
309 _mesa_unmap_pbo_dest(struct gl_context *ctx,
310 const struct gl_pixelstore_attrib *pack)
311 {
312 ASSERT(pack != &ctx->Unpack); /* catch pack/unpack mismatch */
313 if (_mesa_is_bufferobj(pack->BufferObj)) {
314 ctx->Driver.UnmapBuffer(ctx, pack->BufferObj);
315 }
316 }
317
318
319 /**
320 * Check if an unpack PBO is active prior to fetching a texture image.
321 * If so, do bounds checking and map the buffer into main memory.
322 * Any errors detected will be recorded.
323 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
324 */
325 const GLvoid *
326 _mesa_validate_pbo_teximage(struct gl_context *ctx, GLuint dimensions,
327 GLsizei width, GLsizei height, GLsizei depth,
328 GLenum format, GLenum type, const GLvoid *pixels,
329 const struct gl_pixelstore_attrib *unpack,
330 const char *funcName)
331 {
332 GLubyte *buf;
333
334 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
335 /* no PBO */
336 return pixels;
337 }
338 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
339 format, type, INT_MAX, pixels)) {
340 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access)");
341 return NULL;
342 }
343
344 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0, unpack->BufferObj->Size,
345 GL_MAP_READ_BIT,
346 unpack->BufferObj);
347 if (!buf) {
348 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped)");
349 return NULL;
350 }
351
352 return ADD_POINTERS(buf, pixels);
353 }
354
355
356 /**
357 * Check if an unpack PBO is active prior to fetching a compressed texture
358 * image.
359 * If so, do bounds checking and map the buffer into main memory.
360 * Any errors detected will be recorded.
361 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
362 */
363 const GLvoid *
364 _mesa_validate_pbo_compressed_teximage(struct gl_context *ctx,
365 GLsizei imageSize, const GLvoid *pixels,
366 const struct gl_pixelstore_attrib *packing,
367 const char *funcName)
368 {
369 GLubyte *buf;
370
371 if (!_mesa_is_bufferobj(packing->BufferObj)) {
372 /* not using a PBO - return pointer unchanged */
373 return pixels;
374 }
375 if ((const GLubyte *) pixels + imageSize >
376 ((const GLubyte *) 0) + packing->BufferObj->Size) {
377 /* out of bounds read! */
378 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access)");
379 return NULL;
380 }
381
382 buf = (GLubyte*) ctx->Driver.MapBufferRange(ctx, 0,
383 packing->BufferObj->Size,
384 GL_MAP_READ_BIT,
385 packing->BufferObj);
386 if (!buf) {
387 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped");
388 return NULL;
389 }
390
391 return ADD_POINTERS(buf, pixels);
392 }
393
394
395 /**
396 * This function must be called after either of the validate_pbo_*_teximage()
397 * functions. It unmaps the PBO buffer if it was mapped earlier.
398 */
399 void
400 _mesa_unmap_teximage_pbo(struct gl_context *ctx,
401 const struct gl_pixelstore_attrib *unpack)
402 {
403 if (_mesa_is_bufferobj(unpack->BufferObj)) {
404 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
405 }
406 }
407
408