gallium: No longer allow CPU mapping surfaces directly.
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 /**
30 * glReadPixels interface to pipe
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/bufferobj.h"
38 #include "main/context.h"
39 #include "main/image.h"
40
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "pipe/p_inlines.h"
44 #include "util/u_tile.h"
45 #include "st_context.h"
46 #include "st_cb_bitmap.h"
47 #include "st_cb_readpixels.h"
48 #include "st_cb_fbo.h"
49 #include "st_format.h"
50 #include "st_public.h"
51
52
53 /**
54 * Special case for reading stencil buffer.
55 * For color/depth we use get_tile(). For stencil, map the stencil buffer.
56 */
57 void
58 st_read_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
59 GLsizei width, GLsizei height, GLenum type,
60 const struct gl_pixelstore_attrib *packing,
61 GLvoid *pixels)
62 {
63 struct gl_framebuffer *fb = ctx->ReadBuffer;
64 struct pipe_screen *screen = ctx->st->pipe->screen;
65 struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
66 struct pipe_transfer *pt;
67 ubyte *stmap;
68 GLint j;
69
70 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
71 y = ctx->DrawBuffer->Height - y - 1;
72 }
73
74 /* Create a read transfer from the renderbuffer's texture */
75 pt = screen->get_tex_transfer(screen, strb->texture, 0, 0, 0,
76 PIPE_TRANSFER_READ, x, y, width, height);
77
78 /* map the stencil buffer */
79 stmap = screen->transfer_map(screen, pt);
80
81 /* width should never be > MAX_WIDTH since we did clipping earlier */
82 ASSERT(width <= MAX_WIDTH);
83
84 /* process image row by row */
85 for (j = 0; j < height; j++) {
86 GLvoid *dest;
87 GLstencil values[MAX_WIDTH];
88 GLint srcY;
89
90 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
91 srcY = height - j - 1;
92 }
93 else {
94 srcY = j;
95 }
96
97 /* get stencil values */
98 switch (pt->format) {
99 case PIPE_FORMAT_S8_UNORM:
100 {
101 const ubyte *src = stmap + srcY * pt->stride;
102 memcpy(values, src, width);
103 }
104 break;
105 case PIPE_FORMAT_S8Z24_UNORM:
106 {
107 const uint *src = (uint *) (stmap + srcY * pt->stride);
108 GLint k;
109 for (k = 0; k < width; k++) {
110 values[k] = src[k] >> 24;
111 }
112 }
113 break;
114 case PIPE_FORMAT_Z24S8_UNORM:
115 {
116 const uint *src = (uint *) (stmap + srcY * pt->stride);
117 GLint k;
118 for (k = 0; k < width; k++) {
119 values[k] = src[k] & 0xff;
120 }
121 }
122 break;
123 default:
124 assert(0);
125 }
126
127 /* store */
128 dest = _mesa_image_address2d(packing, pixels, width, height,
129 GL_STENCIL_INDEX, type, j, 0);
130
131 _mesa_pack_stencil_span(ctx, width, type, dest, values, packing);
132 }
133
134
135 /* unmap the stencil buffer */
136 screen->transfer_unmap(screen, pt);
137 screen->tex_transfer_release(screen, &pt);
138 }
139
140
141 /**
142 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
143 * commands.
144 * Special care is needed for the front buffer.
145 */
146 struct st_renderbuffer *
147 st_get_color_read_renderbuffer(GLcontext *ctx)
148 {
149 struct gl_framebuffer *fb = ctx->ReadBuffer;
150 struct st_renderbuffer *strb =
151 st_renderbuffer(fb->_ColorReadBuffer);
152 struct st_renderbuffer *front =
153 st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
154
155 if (strb == front
156 && ctx->st->frontbuffer_status == FRONT_STATUS_COPY_OF_BACK) {
157 /* reading from front color buffer, which is a logical copy of the
158 * back color buffer.
159 */
160 struct st_renderbuffer *back =
161 st_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
162 strb = back;
163 }
164
165 return strb;
166 }
167
168
169 /**
170 * Try to do glReadPixels in a fast manner for common cases.
171 * \return GL_TRUE for success, GL_FALSE for failure
172 */
173 static GLboolean
174 st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
175 GLint x, GLint y, GLsizei width, GLsizei height,
176 GLenum format, GLenum type,
177 const struct gl_pixelstore_attrib *pack,
178 GLvoid *dest)
179 {
180 enum combination {
181 A8R8G8B8_UNORM_TO_RGBA_UBYTE,
182 A8R8G8B8_UNORM_TO_RGB_UBYTE,
183 A8R8G8B8_UNORM_TO_BGRA_UINT
184 } combo;
185
186 if (ctx->_ImageTransferState)
187 return GL_FALSE;
188
189 if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM &&
190 format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
191 combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE;
192 }
193 else if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM &&
194 format == GL_RGB && type == GL_UNSIGNED_BYTE) {
195 combo = A8R8G8B8_UNORM_TO_RGB_UBYTE;
196 }
197 else if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM &&
198 format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) {
199 combo = A8R8G8B8_UNORM_TO_BGRA_UINT;
200 }
201 else {
202 return GL_FALSE;
203 }
204
205 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
206
207 {
208 struct pipe_context *pipe = ctx->st->pipe;
209 struct pipe_screen *screen = pipe->screen;
210 struct pipe_transfer *trans;
211 const GLubyte *map;
212 GLubyte *dst;
213 GLint row, col, dy, dstStride;
214
215 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
216 y = strb->texture->height[0] - y - 1;
217 }
218
219 trans = screen->get_tex_transfer(screen, strb->texture, 0, 0, 0,
220 PIPE_TRANSFER_READ, x, y, width, height);
221 if (!trans) {
222 return GL_FALSE;
223 }
224
225 map = screen->transfer_map(screen, trans);
226 if (!map) {
227 screen->tex_transfer_release(screen, &trans);
228 return GL_FALSE;
229 }
230
231 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
232 y = height - y - 1;
233 dy = -1;
234 }
235 else {
236 y = 0;
237 dy = 1;
238 }
239
240 dst = _mesa_image_address2d(pack, dest, width, height,
241 format, type, 0, 0);
242 dstStride = _mesa_image_row_stride(pack, width, format, type);
243
244 switch (combo) {
245 case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
246 for (row = 0; row < height; row++) {
247 const GLubyte *src = map + y * trans->stride;
248 for (col = 0; col < width; col++) {
249 GLuint pixel = ((GLuint *) src)[col];
250 dst[col*4+0] = (pixel >> 16) & 0xff;
251 dst[col*4+1] = (pixel >> 8) & 0xff;
252 dst[col*4+2] = (pixel >> 0) & 0xff;
253 dst[col*4+3] = (pixel >> 24) & 0xff;
254 }
255 dst += dstStride;
256 y += dy;
257 }
258 break;
259 case A8R8G8B8_UNORM_TO_RGB_UBYTE:
260 for (row = 0; row < height; row++) {
261 const GLubyte *src = map + y * trans->stride;
262 for (col = 0; col < width; col++) {
263 GLuint pixel = ((GLuint *) src)[col];
264 dst[col*3+0] = (pixel >> 16) & 0xff;
265 dst[col*3+1] = (pixel >> 8) & 0xff;
266 dst[col*3+2] = (pixel >> 0) & 0xff;
267 }
268 dst += dstStride;
269 y += dy;
270 }
271 break;
272 case A8R8G8B8_UNORM_TO_BGRA_UINT:
273 for (row = 0; row < height; row++) {
274 const GLubyte *src = map + y * trans->stride;
275 memcpy(dst, src, 4 * width);
276 dst += dstStride;
277 y += dy;
278 }
279 break;
280 default:
281 ; /* nothing */
282 }
283
284 screen->transfer_unmap(screen, trans);
285 screen->tex_transfer_release(screen, &trans);
286 }
287
288 return GL_TRUE;
289 }
290
291
292 /**
293 * Do glReadPixels by getting rows from the framebuffer transfer with
294 * get_tile(). Convert to requested format/type with Mesa image routines.
295 * Image transfer ops are done in software too.
296 */
297 static void
298 st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
299 GLenum format, GLenum type,
300 const struct gl_pixelstore_attrib *pack,
301 GLvoid *dest)
302 {
303 struct pipe_context *pipe = ctx->st->pipe;
304 struct pipe_screen *screen = pipe->screen;
305 GLfloat temp[MAX_WIDTH][4];
306 const GLbitfield transferOps = ctx->_ImageTransferState;
307 GLsizei i, j;
308 GLint yStep, dfStride;
309 GLfloat *df;
310 struct st_renderbuffer *strb;
311 struct gl_pixelstore_attrib clippedPacking = *pack;
312 struct pipe_transfer *trans;
313
314 assert(ctx->ReadBuffer->Width > 0);
315
316 /* XXX convolution not done yet */
317 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
318
319 /* Do all needed clipping here, so that we can forget about it later */
320 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
321 /* The ReadPixels transfer is totally outside the window bounds */
322 return;
323 }
324
325 dest = _mesa_map_readpix_pbo(ctx, &clippedPacking, dest);
326 if (!dest)
327 return;
328
329 /* make sure rendering has completed */
330 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
331
332 if (format == GL_STENCIL_INDEX) {
333 st_read_stencil_pixels(ctx, x, y, width, height, type, pack, dest);
334 return;
335 }
336 else if (format == GL_DEPTH_COMPONENT) {
337 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
338 }
339 else {
340 /* Read color buffer */
341 strb = st_get_color_read_renderbuffer(ctx);
342 }
343
344 if (!strb)
345 return;
346
347 /* try a fast-path readpixels before anything else */
348 if (st_fast_readpixels(ctx, strb, x, y, width, height,
349 format, type, pack, dest)) {
350 /* success! */
351 _mesa_unmap_readpix_pbo(ctx, &clippedPacking);
352 return;
353 }
354
355 if (format == GL_RGBA && type == GL_FLOAT) {
356 /* write tile(row) directly into user's buffer */
357 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
358 height, format, type, 0, 0);
359 dfStride = width * 4;
360 }
361 else {
362 /* write tile(row) into temp row buffer */
363 df = (GLfloat *) temp;
364 dfStride = 0;
365 }
366
367 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
368 y = strb->Base.Height - 1 - y;
369 }
370
371 /* Create a read transfer from the renderbuffer's texture */
372 trans = screen->get_tex_transfer(screen, strb->texture, 0, 0, 0,
373 PIPE_TRANSFER_READ, x, y, width, height);
374
375 /* determine bottom-to-top vs. top-to-bottom order */
376 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
377 y = trans->height - 1 - y;
378 yStep = -1;
379 }
380 else {
381 y = 0;
382 yStep = 1;
383 }
384
385 /*
386 * Copy pixels from pipe_transfer to user memory
387 */
388 {
389 /* dest of first pixel in client memory */
390 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
391 height, format, type, 0, 0);
392 /* dest row stride */
393 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
394 format, type);
395
396 if (trans->format == PIPE_FORMAT_S8Z24_UNORM ||
397 trans->format == PIPE_FORMAT_X8Z24_UNORM) {
398 if (format == GL_DEPTH_COMPONENT) {
399 for (i = 0; i < height; i++) {
400 GLuint ztemp[MAX_WIDTH];
401 GLfloat zfloat[MAX_WIDTH];
402 const double scale = 1.0 / ((1 << 24) - 1);
403 pipe_get_tile_raw(trans, 0, y, width, 1, ztemp, 0);
404 y += yStep;
405 for (j = 0; j < width; j++) {
406 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
407 }
408 _mesa_pack_depth_span(ctx, width, dst, type,
409 zfloat, &clippedPacking);
410 dst += dstStride;
411 }
412 }
413 else {
414 /* untested, but simple: */
415 assert(format == GL_DEPTH_STENCIL_EXT);
416 for (i = 0; i < height; i++) {
417 pipe_get_tile_raw(trans, 0, y, width, 1, dst, 0);
418 y += yStep;
419 dst += dstStride;
420 }
421 }
422 }
423 else if (trans->format == PIPE_FORMAT_Z16_UNORM) {
424 for (i = 0; i < height; i++) {
425 GLushort ztemp[MAX_WIDTH];
426 GLfloat zfloat[MAX_WIDTH];
427 const double scale = 1.0 / 0xffff;
428 pipe_get_tile_raw(trans, 0, y, width, 1, ztemp, 0);
429 y += yStep;
430 for (j = 0; j < width; j++) {
431 zfloat[j] = (float) (scale * ztemp[j]);
432 }
433 _mesa_pack_depth_span(ctx, width, dst, type,
434 zfloat, &clippedPacking);
435 dst += dstStride;
436 }
437 }
438 else if (trans->format == PIPE_FORMAT_Z32_UNORM) {
439 for (i = 0; i < height; i++) {
440 GLuint ztemp[MAX_WIDTH];
441 GLfloat zfloat[MAX_WIDTH];
442 const double scale = 1.0 / 0xffffffff;
443 pipe_get_tile_raw(trans, 0, y, width, 1, ztemp, 0);
444 y += yStep;
445 for (j = 0; j < width; j++) {
446 zfloat[j] = (float) (scale * ztemp[j]);
447 }
448 _mesa_pack_depth_span(ctx, width, dst, type,
449 zfloat, &clippedPacking);
450 dst += dstStride;
451 }
452 }
453 else {
454 /* RGBA format */
455 /* Do a row at a time to flip image data vertically */
456 for (i = 0; i < height; i++) {
457 pipe_get_tile_rgba(trans, 0, y, width, 1, df);
458 y += yStep;
459 df += dfStride;
460 if (!dfStride) {
461 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
462 &clippedPacking, transferOps);
463 dst += dstStride;
464 }
465 }
466 }
467 }
468
469 screen->tex_transfer_release(screen, &trans);
470
471 _mesa_unmap_readpix_pbo(ctx, &clippedPacking);
472 }
473
474
475 void st_init_readpixels_functions(struct dd_function_table *functions)
476 {
477 functions->ReadPixels = st_readpixels;
478 }