mesa: Eliminate dd_function_table::MapBuffer
[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 }
86
87 if (sizeAddr == 0)
88 /* no buffer! */
89 return GL_FALSE;
90
91 /* get the offset to the first pixel we'll read/write */
92 start = _mesa_image_address(dimensions, pack, offset, width, height,
93 format, type, 0, 0, 0);
94
95 /* get the offset to just past the last pixel we'll read/write */
96 end = _mesa_image_address(dimensions, pack, offset, width, height,
97 format, type, depth-1, height-1, width);
98
99 if ((const GLubyte *) start > sizeAddr) {
100 /* This will catch negative values / wrap-around */
101 return GL_FALSE;
102 }
103 if ((const GLubyte *) end > sizeAddr) {
104 /* Image read/write goes beyond end of buffer */
105 return GL_FALSE;
106 }
107
108 /* OK! */
109 return GL_TRUE;
110 }
111
112
113 /**
114 * For commands that read from a PBO (glDrawPixels, glTexImage,
115 * glPolygonStipple, etc), if we're reading from a PBO, map it read-only
116 * and return the pointer into the PBO. If we're not reading from a
117 * PBO, return \p src as-is.
118 * If non-null return, must call _mesa_unmap_pbo_source() when done.
119 *
120 * \return NULL if error, else pointer to start of data
121 */
122 const GLvoid *
123 _mesa_map_pbo_source(struct gl_context *ctx,
124 const struct gl_pixelstore_attrib *unpack,
125 const GLvoid *src)
126 {
127 const GLubyte *buf;
128
129 if (_mesa_is_bufferobj(unpack->BufferObj)) {
130 /* unpack from PBO */
131 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
132 unpack->BufferObj->Size,
133 GL_MAP_READ_BIT,
134 unpack->BufferObj);
135 if (!buf)
136 return NULL;
137
138 buf = ADD_POINTERS(buf, src);
139 }
140 else {
141 /* unpack from normal memory */
142 buf = src;
143 }
144
145 return buf;
146 }
147
148
149 /**
150 * Combine PBO-read validation and mapping.
151 * If any GL errors are detected, they'll be recorded and NULL returned.
152 * \sa _mesa_validate_pbo_access
153 * \sa _mesa_map_pbo_source
154 * A call to this function should have a matching call to
155 * _mesa_unmap_pbo_source().
156 */
157 const GLvoid *
158 _mesa_map_validate_pbo_source(struct gl_context *ctx,
159 GLuint dimensions,
160 const struct gl_pixelstore_attrib *unpack,
161 GLsizei width, GLsizei height, GLsizei depth,
162 GLenum format, GLenum type, GLsizei clientMemSize,
163 const GLvoid *ptr, const char *where)
164 {
165 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
166
167 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
168 format, type, clientMemSize, ptr)) {
169 if (_mesa_is_bufferobj(unpack->BufferObj)) {
170 _mesa_error(ctx, GL_INVALID_OPERATION,
171 "%s(out of bounds PBO access)", where);
172 } else {
173 _mesa_error(ctx, GL_INVALID_OPERATION,
174 "%s(out of bounds access: bufSize (%d) is too small)",
175 where, clientMemSize);
176 }
177 return NULL;
178 }
179
180 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
181 /* non-PBO access: no further validation to be done */
182 return ptr;
183 }
184
185 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
186 /* buffer is already mapped - that's an error */
187 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
188 return NULL;
189 }
190
191 ptr = _mesa_map_pbo_source(ctx, unpack, ptr);
192 return ptr;
193 }
194
195
196 /**
197 * Counterpart to _mesa_map_pbo_source()
198 */
199 void
200 _mesa_unmap_pbo_source(struct gl_context *ctx,
201 const struct gl_pixelstore_attrib *unpack)
202 {
203 ASSERT(unpack != &ctx->Pack); /* catch pack/unpack mismatch */
204 if (_mesa_is_bufferobj(unpack->BufferObj)) {
205 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
206 }
207 }
208
209
210 /**
211 * For commands that write to a PBO (glReadPixels, glGetColorTable, etc),
212 * if we're writing to a PBO, map it write-only and return the pointer
213 * into the PBO. If we're not writing to a PBO, return \p dst as-is.
214 * If non-null return, must call _mesa_unmap_pbo_dest() when done.
215 *
216 * \return NULL if error, else pointer to start of data
217 */
218 void *
219 _mesa_map_pbo_dest(struct gl_context *ctx,
220 const struct gl_pixelstore_attrib *pack,
221 GLvoid *dest)
222 {
223 void *buf;
224
225 if (_mesa_is_bufferobj(pack->BufferObj)) {
226 /* pack into PBO */
227 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0,
228 pack->BufferObj->Size,
229 GL_MAP_WRITE_BIT,
230 pack->BufferObj);
231 if (!buf)
232 return NULL;
233
234 buf = ADD_POINTERS(buf, dest);
235 }
236 else {
237 /* pack to normal memory */
238 buf = dest;
239 }
240
241 return buf;
242 }
243
244
245 /**
246 * Combine PBO-write validation and mapping.
247 * If any GL errors are detected, they'll be recorded and NULL returned.
248 * \sa _mesa_validate_pbo_access
249 * \sa _mesa_map_pbo_dest
250 * A call to this function should have a matching call to
251 * _mesa_unmap_pbo_dest().
252 */
253 GLvoid *
254 _mesa_map_validate_pbo_dest(struct gl_context *ctx,
255 GLuint dimensions,
256 const struct gl_pixelstore_attrib *unpack,
257 GLsizei width, GLsizei height, GLsizei depth,
258 GLenum format, GLenum type, GLsizei clientMemSize,
259 GLvoid *ptr, const char *where)
260 {
261 ASSERT(dimensions == 1 || dimensions == 2 || dimensions == 3);
262
263 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
264 format, type, clientMemSize, ptr)) {
265 if (_mesa_is_bufferobj(unpack->BufferObj)) {
266 _mesa_error(ctx, GL_INVALID_OPERATION,
267 "%s(out of bounds PBO access)", where);
268 } else {
269 _mesa_error(ctx, GL_INVALID_OPERATION,
270 "%s(out of bounds access: bufSize (%d) is too small)",
271 where, clientMemSize);
272 }
273 return NULL;
274 }
275
276 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
277 /* non-PBO access: no further validation to be done */
278 return ptr;
279 }
280
281 if (_mesa_bufferobj_mapped(unpack->BufferObj)) {
282 /* buffer is already mapped - that's an error */
283 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(PBO is mapped)", where);
284 return NULL;
285 }
286
287 ptr = _mesa_map_pbo_dest(ctx, unpack, ptr);
288 return ptr;
289 }
290
291
292 /**
293 * Counterpart to _mesa_map_pbo_dest()
294 */
295 void
296 _mesa_unmap_pbo_dest(struct gl_context *ctx,
297 const struct gl_pixelstore_attrib *pack)
298 {
299 ASSERT(pack != &ctx->Unpack); /* catch pack/unpack mismatch */
300 if (_mesa_is_bufferobj(pack->BufferObj)) {
301 ctx->Driver.UnmapBuffer(ctx, pack->BufferObj);
302 }
303 }
304
305
306 /**
307 * Check if an unpack PBO is active prior to fetching a texture image.
308 * If so, do bounds checking and map the buffer into main memory.
309 * Any errors detected will be recorded.
310 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
311 */
312 const GLvoid *
313 _mesa_validate_pbo_teximage(struct gl_context *ctx, GLuint dimensions,
314 GLsizei width, GLsizei height, GLsizei depth,
315 GLenum format, GLenum type, const GLvoid *pixels,
316 const struct gl_pixelstore_attrib *unpack,
317 const char *funcName)
318 {
319 GLubyte *buf;
320
321 if (!_mesa_is_bufferobj(unpack->BufferObj)) {
322 /* no PBO */
323 return pixels;
324 }
325 if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
326 format, type, INT_MAX, pixels)) {
327 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access)");
328 return NULL;
329 }
330
331 buf = (GLubyte *) ctx->Driver.MapBufferRange(ctx, 0, unpack->BufferObj->Size,
332 GL_MAP_READ_BIT,
333 unpack->BufferObj);
334 if (!buf) {
335 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped)");
336 return NULL;
337 }
338
339 return ADD_POINTERS(buf, pixels);
340 }
341
342
343 /**
344 * Check if an unpack PBO is active prior to fetching a compressed texture
345 * image.
346 * If so, do bounds checking and map the buffer into main memory.
347 * Any errors detected will be recorded.
348 * The caller _must_ call _mesa_unmap_teximage_pbo() too!
349 */
350 const GLvoid *
351 _mesa_validate_pbo_compressed_teximage(struct gl_context *ctx,
352 GLsizei imageSize, const GLvoid *pixels,
353 const struct gl_pixelstore_attrib *packing,
354 const char *funcName)
355 {
356 GLubyte *buf;
357
358 if (!_mesa_is_bufferobj(packing->BufferObj)) {
359 /* not using a PBO - return pointer unchanged */
360 return pixels;
361 }
362 if ((const GLubyte *) pixels + imageSize >
363 ((const GLubyte *) 0) + packing->BufferObj->Size) {
364 /* out of bounds read! */
365 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access)");
366 return NULL;
367 }
368
369 buf = (GLubyte*) ctx->Driver.MapBufferRange(ctx, 0,
370 packing->BufferObj->Size,
371 GL_MAP_READ_BIT,
372 packing->BufferObj);
373 if (!buf) {
374 _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped");
375 return NULL;
376 }
377
378 return ADD_POINTERS(buf, pixels);
379 }
380
381
382 /**
383 * This function must be called after either of the validate_pbo_*_teximage()
384 * functions. It unmaps the PBO buffer if it was mapped earlier.
385 */
386 void
387 _mesa_unmap_teximage_pbo(struct gl_context *ctx,
388 const struct gl_pixelstore_attrib *unpack)
389 {
390 if (_mesa_is_bufferobj(unpack->BufferObj)) {
391 ctx->Driver.UnmapBuffer(ctx, unpack->BufferObj);
392 }
393 }
394
395