OSDN Git Service

4689a0032b72145cca033f7ad67835f6a2d30497
[android-x86/external-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 #include "main/pack.h"
41
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "util/u_format.h"
45 #include "util/u_inlines.h"
46 #include "util/u_tile.h"
47
48 #include "st_debug.h"
49 #include "st_context.h"
50 #include "st_atom.h"
51 #include "st_cb_bitmap.h"
52 #include "st_cb_readpixels.h"
53 #include "st_cb_fbo.h"
54
55 /**
56  * Special case for reading stencil buffer.
57  * For color/depth we use get_tile().  For stencil, map the stencil buffer.
58  */
59 void
60 st_read_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
61                        GLsizei width, GLsizei height,
62                        GLenum format, GLenum type,
63                        const struct gl_pixelstore_attrib *packing,
64                        GLvoid *pixels)
65 {
66    struct gl_framebuffer *fb = ctx->ReadBuffer;
67    struct pipe_context *pipe = st_context(ctx)->pipe;
68    struct st_renderbuffer *strb = st_renderbuffer(fb->_StencilBuffer);
69    struct pipe_transfer *pt;
70    ubyte *stmap;
71    GLint j;
72
73    if (strb->Base.Wrapped) {
74       strb = st_renderbuffer(strb->Base.Wrapped);
75    }
76
77    if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
78       y = ctx->DrawBuffer->Height - y - height;
79    }
80
81    /* Create a read transfer from the renderbuffer's texture */
82
83    pt = pipe_get_transfer(pipe, strb->texture,
84                           0, 0,
85                           PIPE_TRANSFER_READ,
86                           x, y, width, height);
87
88    /* map the stencil buffer */
89    stmap = pipe_transfer_map(pipe, pt);
90
91    /* width should never be > MAX_WIDTH since we did clipping earlier */
92    ASSERT(width <= MAX_WIDTH);
93
94    /* process image row by row */
95    for (j = 0; j < height; j++) {
96       GLvoid *dest;
97       GLstencil sValues[MAX_WIDTH];
98       GLfloat zValues[MAX_WIDTH];
99       GLint srcY;
100
101       if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
102          srcY = height - j - 1;
103       }
104       else {
105          srcY = j;
106       }
107
108       /* get stencil (and Z) values */
109       switch (pt->resource->format) {
110       case PIPE_FORMAT_S8_USCALED:
111          {
112             const ubyte *src = stmap + srcY * pt->stride;
113             memcpy(sValues, src, width);
114          }
115          break;
116       case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
117          if (format == GL_DEPTH_STENCIL) {
118             const uint *src = (uint *) (stmap + srcY * pt->stride);
119             const GLfloat scale = 1.0f / (0xffffff);
120             GLint k;
121             for (k = 0; k < width; k++) {
122                sValues[k] = src[k] >> 24;
123                zValues[k] = (src[k] & 0xffffff) * scale;
124             }
125          }
126          else {
127             const uint *src = (uint *) (stmap + srcY * pt->stride);
128             GLint k;
129             for (k = 0; k < width; k++) {
130                sValues[k] = src[k] >> 24;
131             }
132          }
133          break;
134       case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
135          if (format == GL_DEPTH_STENCIL) {
136             const uint *src = (uint *) (stmap + srcY * pt->stride);
137             const GLfloat scale = 1.0f / (0xffffff);
138             GLint k;
139             for (k = 0; k < width; k++) {
140                sValues[k] = src[k] & 0xff;
141                zValues[k] = (src[k] >> 8) * scale;
142             }
143          }
144          else {
145             const uint *src = (uint *) (stmap + srcY * pt->stride);
146             GLint k;
147             for (k = 0; k < width; k++) {
148                sValues[k] = src[k] & 0xff;
149             }
150          }
151          break;
152       default:
153          assert(0);
154       }
155
156       /* store */
157       dest = _mesa_image_address2d(packing, pixels, width, height,
158                                    format, type, j, 0);
159       if (format == GL_DEPTH_STENCIL) {
160          _mesa_pack_depth_stencil_span(ctx, width, dest,
161                                        zValues, sValues, packing);
162       }
163       else {
164          _mesa_pack_stencil_span(ctx, width, type, dest, sValues, packing);
165       }
166    }
167
168    /* unmap the stencil buffer */
169    pipe_transfer_unmap(pipe, pt);
170    pipe->transfer_destroy(pipe, pt);
171 }
172
173
174 /**
175  * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
176  * commands.
177  */
178 struct st_renderbuffer *
179 st_get_color_read_renderbuffer(struct gl_context *ctx)
180 {
181    struct gl_framebuffer *fb = ctx->ReadBuffer;
182    struct st_renderbuffer *strb =
183       st_renderbuffer(fb->_ColorReadBuffer);
184
185    return strb;
186 }
187
188
189 /**
190  * Try to do glReadPixels in a fast manner for common cases.
191  * \return GL_TRUE for success, GL_FALSE for failure
192  */
193 static GLboolean
194 st_fast_readpixels(struct gl_context *ctx, struct st_renderbuffer *strb,
195                    GLint x, GLint y, GLsizei width, GLsizei height,
196                    GLenum format, GLenum type,
197                    const struct gl_pixelstore_attrib *pack,
198                    GLvoid *dest)
199 {
200    enum combination {
201       A8R8G8B8_UNORM_TO_RGBA_UBYTE,
202       A8R8G8B8_UNORM_TO_RGB_UBYTE,
203       A8R8G8B8_UNORM_TO_BGRA_UINT
204    } combo;
205
206    if (ctx->_ImageTransferState)
207       return GL_FALSE;
208
209    if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
210        format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
211       combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE;
212    }
213    else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
214             format == GL_RGB && type == GL_UNSIGNED_BYTE) {
215       combo = A8R8G8B8_UNORM_TO_RGB_UBYTE;
216    }
217    else if (strb->format == PIPE_FORMAT_B8G8R8A8_UNORM &&
218             format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) {
219       combo = A8R8G8B8_UNORM_TO_BGRA_UINT;
220    }
221    else {
222       return GL_FALSE;
223    }
224
225    /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
226
227    {
228       struct pipe_context *pipe = st_context(ctx)->pipe;
229       struct pipe_transfer *trans;
230       const GLubyte *map;
231       GLubyte *dst;
232       GLint row, col, dy, dstStride;
233
234       if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
235          /* convert GL Y to Gallium Y */
236          y = strb->texture->height0 - y - height;
237       }
238
239       trans = pipe_get_transfer(pipe, strb->texture,
240                                 0, 0,
241                                 PIPE_TRANSFER_READ,
242                                 x, y, width, height);
243       if (!trans) {
244          return GL_FALSE;
245       }
246
247       map = pipe_transfer_map(pipe, trans);
248       if (!map) {
249          pipe->transfer_destroy(pipe, trans);
250          return GL_FALSE;
251       }
252
253       /* We always write to the user/dest buffer from low addr to high addr
254        * but the read order depends on renderbuffer orientation
255        */
256       if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
257          /* read source rows from bottom to top */
258          y = height - 1;
259          dy = -1;
260       }
261       else {
262          /* read source rows from top to bottom */
263          y = 0;
264          dy = 1;
265       }
266
267       dst = _mesa_image_address2d(pack, dest, width, height,
268                                   format, type, 0, 0);
269       dstStride = _mesa_image_row_stride(pack, width, format, type);
270
271       switch (combo) {
272       case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
273          for (row = 0; row < height; row++) {
274             const GLubyte *src = map + y * trans->stride;
275             for (col = 0; col < width; col++) {
276                GLuint pixel = ((GLuint *) src)[col];
277                dst[col*4+0] = (pixel >> 16) & 0xff;
278                dst[col*4+1] = (pixel >>  8) & 0xff;
279                dst[col*4+2] = (pixel >>  0) & 0xff;
280                dst[col*4+3] = (pixel >> 24) & 0xff;
281             }
282             dst += dstStride;
283             y += dy;
284          }
285          break;
286       case A8R8G8B8_UNORM_TO_RGB_UBYTE:
287          for (row = 0; row < height; row++) {
288             const GLubyte *src = map + y * trans->stride;
289             for (col = 0; col < width; col++) {
290                GLuint pixel = ((GLuint *) src)[col];
291                dst[col*3+0] = (pixel >> 16) & 0xff;
292                dst[col*3+1] = (pixel >>  8) & 0xff;
293                dst[col*3+2] = (pixel >>  0) & 0xff;
294             }
295             dst += dstStride;
296             y += dy;
297          }
298          break;
299       case A8R8G8B8_UNORM_TO_BGRA_UINT:
300          for (row = 0; row < height; row++) {
301             const GLubyte *src = map + y * trans->stride;
302             memcpy(dst, src, 4 * width);
303             dst += dstStride;
304             y += dy;
305          }
306          break;
307       default:
308          ; /* nothing */
309       }
310
311       pipe_transfer_unmap(pipe, trans);
312       pipe->transfer_destroy(pipe, trans);
313    }
314
315    return GL_TRUE;
316 }
317
318
319 /**
320  * Do glReadPixels by getting rows from the framebuffer transfer with
321  * get_tile().  Convert to requested format/type with Mesa image routines.
322  * Image transfer ops are done in software too.
323  */
324 static void
325 st_readpixels(struct gl_context *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
326               GLenum format, GLenum type,
327               const struct gl_pixelstore_attrib *pack,
328               GLvoid *dest)
329 {
330    struct st_context *st = st_context(ctx);
331    struct pipe_context *pipe = st->pipe;
332    GLfloat (*temp)[4];
333    const GLbitfield transferOps = ctx->_ImageTransferState;
334    GLsizei i, j;
335    GLint yStep, dfStride;
336    GLfloat *df;
337    struct st_renderbuffer *strb;
338    struct gl_pixelstore_attrib clippedPacking = *pack;
339    struct pipe_transfer *trans;
340    enum pipe_format pformat;
341
342    assert(ctx->ReadBuffer->Width > 0);
343
344    st_validate_state(st);
345
346    /* Do all needed clipping here, so that we can forget about it later */
347    if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
348       /* The ReadPixels transfer is totally outside the window bounds */
349       return;
350    }
351
352    st_flush_bitmap_cache(st);
353
354    dest = _mesa_map_pbo_dest(ctx, &clippedPacking, dest);
355    if (!dest)
356       return;
357
358    if (format == GL_STENCIL_INDEX ||
359        format == GL_DEPTH_STENCIL) {
360       st_read_stencil_pixels(ctx, x, y, width, height,
361                              format, type, pack, dest);
362       return;
363    }
364    else if (format == GL_DEPTH_COMPONENT) {
365       strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
366       if (strb->Base.Wrapped) {
367          strb = st_renderbuffer(strb->Base.Wrapped);
368       }
369    }
370    else {
371       /* Read color buffer */
372       strb = st_get_color_read_renderbuffer(ctx);
373    }
374
375    if (!strb)
376       return;
377
378    /* try a fast-path readpixels before anything else */
379    if (st_fast_readpixels(ctx, strb, x, y, width, height,
380                           format, type, pack, dest)) {
381       /* success! */
382       _mesa_unmap_pbo_dest(ctx, &clippedPacking);
383       return;
384    }
385
386    /* allocate temp pixel row buffer */
387    temp = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
388    if (!temp) {
389       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glReadPixels");
390       return;
391    }
392
393    if (format == GL_RGBA && type == GL_FLOAT) {
394       /* write tile(row) directly into user's buffer */
395       df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
396                                              height, format, type, 0, 0);
397       dfStride = width * 4;
398    }
399    else {
400       /* write tile(row) into temp row buffer */
401       df = (GLfloat *) temp;
402       dfStride = 0;
403    }
404
405    if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
406       /* convert GL Y to Gallium Y */
407       y = strb->Base.Height - y - height;
408    }
409
410    /* Create a read transfer from the renderbuffer's texture */
411    trans = pipe_get_transfer(pipe, strb->texture,
412                              0, 0,
413                              PIPE_TRANSFER_READ,
414                              x, y, width, height);
415
416    /* determine bottom-to-top vs. top-to-bottom order */
417    if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
418       y = height - 1;
419       yStep = -1;
420    }
421    else {
422       y = 0;
423       yStep = 1;
424    }
425
426    /* possibly convert sRGB format to linear RGB format */
427    pformat = util_format_linear(trans->resource->format);
428
429    if (ST_DEBUG & DEBUG_FALLBACK)
430       debug_printf("%s: fallback processing\n", __FUNCTION__);
431
432    /*
433     * Copy pixels from pipe_transfer to user memory
434     */
435    {
436       /* dest of first pixel in client memory */
437       GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
438                                            height, format, type, 0, 0);
439       /* dest row stride */
440       const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
441                                                      format, type);
442
443       if (pformat == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
444           pformat == PIPE_FORMAT_Z24X8_UNORM) {
445          if (format == GL_DEPTH_COMPONENT) {
446             for (i = 0; i < height; i++) {
447                GLuint ztemp[MAX_WIDTH];
448                GLfloat zfloat[MAX_WIDTH];
449                const double scale = 1.0 / ((1 << 24) - 1);
450                pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
451                y += yStep;
452                for (j = 0; j < width; j++) {
453                   zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
454                }
455                _mesa_pack_depth_span(ctx, width, dst, type,
456                                      zfloat, &clippedPacking);
457                dst += dstStride;
458             }
459          }
460          else {
461             /* XXX: unreachable code -- should be before st_read_stencil_pixels */
462             assert(format == GL_DEPTH_STENCIL_EXT);
463             for (i = 0; i < height; i++) {
464                GLuint *zshort = (GLuint *)dst;
465                pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
466                y += yStep;
467                /* Reverse into 24/8 */
468                for (j = 0; j < width; j++) {
469                   zshort[j] = (zshort[j] << 8) | (zshort[j] >> 24);
470                }
471                dst += dstStride;
472             }
473          }
474       }
475       else if (pformat == PIPE_FORMAT_S8_USCALED_Z24_UNORM ||
476                pformat == PIPE_FORMAT_X8Z24_UNORM) {
477          if (format == GL_DEPTH_COMPONENT) {
478             for (i = 0; i < height; i++) {
479                GLuint ztemp[MAX_WIDTH];
480                GLfloat zfloat[MAX_WIDTH];
481                const double scale = 1.0 / ((1 << 24) - 1);
482                pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
483                y += yStep;
484                for (j = 0; j < width; j++) {
485                   zfloat[j] = (float) (scale * ((ztemp[j] >> 8) & 0xffffff));
486                }
487                _mesa_pack_depth_span(ctx, width, dst, type,
488                                      zfloat, &clippedPacking);
489                dst += dstStride;
490             }
491          }
492          else {
493             /* XXX: unreachable code -- should be before st_read_stencil_pixels */
494             assert(format == GL_DEPTH_STENCIL_EXT);
495             for (i = 0; i < height; i++) {
496                pipe_get_tile_raw(pipe, trans, 0, y, width, 1, dst, 0);
497                y += yStep;
498                dst += dstStride;
499             }
500          }
501       }
502       else if (pformat == PIPE_FORMAT_Z16_UNORM) {
503          for (i = 0; i < height; i++) {
504             GLushort ztemp[MAX_WIDTH];
505             GLfloat zfloat[MAX_WIDTH];
506             const double scale = 1.0 / 0xffff;
507             pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
508             y += yStep;
509             for (j = 0; j < width; j++) {
510                zfloat[j] = (float) (scale * ztemp[j]);
511             }
512             _mesa_pack_depth_span(ctx, width, dst, type,
513                                   zfloat, &clippedPacking);
514             dst += dstStride;
515          }
516       }
517       else if (pformat == PIPE_FORMAT_Z32_UNORM) {
518          for (i = 0; i < height; i++) {
519             GLuint ztemp[MAX_WIDTH];
520             GLfloat zfloat[MAX_WIDTH];
521             const double scale = 1.0 / 0xffffffff;
522             pipe_get_tile_raw(pipe, trans, 0, y, width, 1, ztemp, 0);
523             y += yStep;
524             for (j = 0; j < width; j++) {
525                zfloat[j] = (float) (scale * ztemp[j]);
526             }
527             _mesa_pack_depth_span(ctx, width, dst, type,
528                                   zfloat, &clippedPacking);
529             dst += dstStride;
530          }
531       }
532       else {
533          /* RGBA format */
534          /* Do a row at a time to flip image data vertically */
535          for (i = 0; i < height; i++) {
536             pipe_get_tile_rgba_format(pipe, trans, 0, y, width, 1,
537                                       pformat, df);
538             y += yStep;
539             df += dfStride;
540             if (!dfStride) {
541                _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
542                                           &clippedPacking, transferOps);
543                dst += dstStride;
544             }
545          }
546       }
547    }
548
549    free(temp);
550
551    pipe->transfer_destroy(pipe, trans);
552
553    _mesa_unmap_pbo_dest(ctx, &clippedPacking);
554 }
555
556
557 void st_init_readpixels_functions(struct dd_function_table *functions)
558 {
559    functions->ReadPixels = st_readpixels;
560 }