Merge commit 'origin/master' into gallium-0.2
[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_surface *ps;
67 ubyte *stmap;
68 GLint j;
69
70 /* Create a CPU-READ surface/view into the renderbuffer's texture */
71 ps = screen->get_tex_surface(screen, strb->texture, 0, 0, 0,
72 PIPE_BUFFER_USAGE_CPU_READ);
73
74 /* map the stencil buffer */
75 stmap = screen->surface_map(screen, ps, PIPE_BUFFER_USAGE_CPU_READ);
76
77 /* width should never be > MAX_WIDTH since we did clipping earlier */
78 ASSERT(width <= MAX_WIDTH);
79
80 /* process image row by row */
81 for (j = 0; j < height; j++, y++) {
82 GLvoid *dest;
83 GLstencil values[MAX_WIDTH];
84 GLint srcY;
85
86 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
87 srcY = ctx->DrawBuffer->Height - y - 1;
88 }
89 else {
90 srcY = y;
91 }
92
93 /* get stencil values */
94 switch (ps->format) {
95 case PIPE_FORMAT_S8_UNORM:
96 {
97 const ubyte *src = stmap + srcY * ps->stride + x;
98 memcpy(values, src, width);
99 }
100 break;
101 case PIPE_FORMAT_S8Z24_UNORM:
102 {
103 const uint *src = (uint *) (stmap + srcY * ps->stride + x*4);
104 GLint k;
105 for (k = 0; k < width; k++) {
106 values[k] = src[k] >> 24;
107 }
108 }
109 break;
110 case PIPE_FORMAT_Z24S8_UNORM:
111 {
112 const uint *src = (uint *) (stmap + srcY * ps->stride + x*4);
113 GLint k;
114 for (k = 0; k < width; k++) {
115 values[k] = src[k] & 0xff;
116 }
117 }
118 break;
119 default:
120 assert(0);
121 }
122
123 /* store */
124 dest = _mesa_image_address2d(packing, pixels, width, height,
125 GL_STENCIL_INDEX, type, j, 0);
126
127 _mesa_pack_stencil_span(ctx, width, type, dest, values, packing);
128 }
129
130
131 /* unmap the stencil buffer */
132 screen->surface_unmap(screen, ps);
133 pipe_surface_reference(&ps, NULL);
134 }
135
136
137 /**
138 * Return renderbuffer to use for reading color pixels for glRead/CopyPixel
139 * commands.
140 * Special care is needed for the front buffer.
141 */
142 struct st_renderbuffer *
143 st_get_color_read_renderbuffer(GLcontext *ctx)
144 {
145 struct gl_framebuffer *fb = ctx->ReadBuffer;
146 struct st_renderbuffer *strb =
147 st_renderbuffer(fb->_ColorReadBuffer);
148 struct st_renderbuffer *front =
149 st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
150
151 if (strb == front
152 && ctx->st->frontbuffer_status == FRONT_STATUS_COPY_OF_BACK) {
153 /* reading from front color buffer, which is a logical copy of the
154 * back color buffer.
155 */
156 struct st_renderbuffer *back =
157 st_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
158 strb = back;
159 }
160
161 return strb;
162 }
163
164
165 /**
166 * Try to do glReadPixels in a fast manner for common cases.
167 * \return GL_TRUE for success, GL_FALSE for failure
168 */
169 static GLboolean
170 st_fast_readpixels(GLcontext *ctx, struct st_renderbuffer *strb,
171 GLint x, GLint y, GLsizei width, GLsizei height,
172 GLenum format, GLenum type,
173 const struct gl_pixelstore_attrib *pack,
174 GLvoid *dest)
175 {
176 enum combination {
177 A8R8G8B8_UNORM_TO_RGBA_UBYTE,
178 A8R8G8B8_UNORM_TO_RGB_UBYTE,
179 A8R8G8B8_UNORM_TO_BGRA_UINT
180 } combo;
181
182 if (ctx->_ImageTransferState)
183 return GL_FALSE;
184
185 if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM &&
186 format == GL_RGBA && type == GL_UNSIGNED_BYTE) {
187 combo = A8R8G8B8_UNORM_TO_RGBA_UBYTE;
188 }
189 else if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM &&
190 format == GL_RGB && type == GL_UNSIGNED_BYTE) {
191 combo = A8R8G8B8_UNORM_TO_RGB_UBYTE;
192 }
193 else if (strb->format == PIPE_FORMAT_A8R8G8B8_UNORM &&
194 format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) {
195 combo = A8R8G8B8_UNORM_TO_BGRA_UINT;
196 }
197 else {
198 return GL_FALSE;
199 }
200
201 /*printf("st_fast_readpixels combo %d\n", (GLint) combo);*/
202
203 {
204 struct pipe_context *pipe = ctx->st->pipe;
205 struct pipe_screen *screen = pipe->screen;
206 struct pipe_surface *surf;
207 const GLubyte *map;
208 GLubyte *dst;
209 GLint row, col, dy, dstStride;
210
211 surf = screen->get_tex_surface(screen, strb->texture, 0, 0, 0,
212 PIPE_BUFFER_USAGE_CPU_READ);
213 if (!surf) {
214 return GL_FALSE;
215 }
216
217 map = screen->surface_map(screen, surf, PIPE_BUFFER_USAGE_CPU_READ);
218 if (!map) {
219 pipe_surface_reference(&surf, NULL);
220 return GL_FALSE;
221 }
222
223 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
224 y = surf->height - y - 1;
225 dy = -1;
226 }
227 else {
228 dy = 1;
229 }
230
231 dst = _mesa_image_address2d(pack, dest, width, height,
232 format, type, 0, 0);
233 dstStride = _mesa_image_row_stride(pack, width, format, type);
234
235 switch (combo) {
236 case A8R8G8B8_UNORM_TO_RGBA_UBYTE:
237 for (row = 0; row < height; row++) {
238 const GLubyte *src = map + y * surf->stride + x * 4;
239 for (col = 0; col < width; col++) {
240 GLuint pixel = ((GLuint *) src)[col];
241 dst[col*4+0] = (pixel >> 16) & 0xff;
242 dst[col*4+1] = (pixel >> 8) & 0xff;
243 dst[col*4+2] = (pixel >> 0) & 0xff;
244 dst[col*4+3] = (pixel >> 24) & 0xff;
245 }
246 dst += dstStride;
247 y += dy;
248 }
249 break;
250 case A8R8G8B8_UNORM_TO_RGB_UBYTE:
251 for (row = 0; row < height; row++) {
252 const GLubyte *src = map + y * surf->stride + x * 4;
253 for (col = 0; col < width; col++) {
254 GLuint pixel = ((GLuint *) src)[col];
255 dst[col*3+0] = (pixel >> 16) & 0xff;
256 dst[col*3+1] = (pixel >> 8) & 0xff;
257 dst[col*3+2] = (pixel >> 0) & 0xff;
258 }
259 dst += dstStride;
260 y += dy;
261 }
262 break;
263 case A8R8G8B8_UNORM_TO_BGRA_UINT:
264 for (row = 0; row < height; row++) {
265 const GLubyte *src = map + y * surf->stride + x * 4;
266 memcpy(dst, src, 4 * width);
267 dst += dstStride;
268 y += dy;
269 }
270 break;
271 default:
272 ; /* nothing */
273 }
274
275 screen->surface_unmap(screen, surf);
276 pipe_surface_reference(&surf, NULL);
277 }
278
279 return GL_TRUE;
280 }
281
282
283 /**
284 * Do glReadPixels by getting rows from the framebuffer surface with
285 * get_tile(). Convert to requested format/type with Mesa image routines.
286 * Image transfer ops are done in software too.
287 */
288 static void
289 st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
290 GLenum format, GLenum type,
291 const struct gl_pixelstore_attrib *pack,
292 GLvoid *dest)
293 {
294 struct pipe_context *pipe = ctx->st->pipe;
295 struct pipe_screen *screen = pipe->screen;
296 GLfloat temp[MAX_WIDTH][4];
297 const GLbitfield transferOps = ctx->_ImageTransferState;
298 GLsizei i, j;
299 GLint yStep, dfStride;
300 GLfloat *df;
301 struct st_renderbuffer *strb;
302 struct gl_pixelstore_attrib clippedPacking = *pack;
303 struct pipe_surface *surf;
304
305 assert(ctx->ReadBuffer->Width > 0);
306
307 /* XXX convolution not done yet */
308 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
309
310 /* Do all needed clipping here, so that we can forget about it later */
311 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
312 /* The ReadPixels surface is totally outside the window bounds */
313 return;
314 }
315
316 dest = _mesa_map_readpix_pbo(ctx, &clippedPacking, dest);
317 if (!dest)
318 return;
319
320 /* make sure rendering has completed */
321 st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
322
323 if (format == GL_STENCIL_INDEX) {
324 st_read_stencil_pixels(ctx, x, y, width, height, type, pack, dest);
325 return;
326 }
327 else if (format == GL_DEPTH_COMPONENT) {
328 strb = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
329 }
330 else {
331 /* Read color buffer */
332 strb = st_get_color_read_renderbuffer(ctx);
333 }
334
335 if (!strb)
336 return;
337
338 /* try a fast-path readpixels before anything else */
339 if (st_fast_readpixels(ctx, strb, x, y, width, height,
340 format, type, pack, dest)) {
341 /* success! */
342 _mesa_unmap_readpix_pbo(ctx, &clippedPacking);
343 return;
344 }
345
346 if (format == GL_RGBA && type == GL_FLOAT) {
347 /* write tile(row) directly into user's buffer */
348 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
349 height, format, type, 0, 0);
350 dfStride = width * 4;
351 }
352 else {
353 /* write tile(row) into temp row buffer */
354 df = (GLfloat *) temp;
355 dfStride = 0;
356 }
357
358 /* determine bottom-to-top vs. top-to-bottom order */
359 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
360 y = strb->Base.Height - 1 - y;
361 yStep = -1;
362 }
363 else {
364 yStep = 1;
365 }
366
367 /* Create a CPU-READ surface/view into the renderbuffer's texture */
368 surf = screen->get_tex_surface(screen, strb->texture, 0, 0, 0,
369 PIPE_BUFFER_USAGE_CPU_READ);
370
371 /*
372 * Copy pixels from pipe_surface to user memory
373 */
374 {
375 /* dest of first pixel in client memory */
376 GLubyte *dst = _mesa_image_address2d(&clippedPacking, dest, width,
377 height, format, type, 0, 0);
378 /* dest row stride */
379 const GLint dstStride = _mesa_image_row_stride(&clippedPacking, width,
380 format, type);
381
382 if (surf->format == PIPE_FORMAT_S8Z24_UNORM ||
383 surf->format == PIPE_FORMAT_X8Z24_UNORM) {
384 if (format == GL_DEPTH_COMPONENT) {
385 for (i = 0; i < height; i++) {
386 GLuint ztemp[MAX_WIDTH];
387 GLfloat zfloat[MAX_WIDTH];
388 const double scale = 1.0 / ((1 << 24) - 1);
389 pipe_get_tile_raw(surf, x, y, width, 1, ztemp, 0);
390 y += yStep;
391 for (j = 0; j < width; j++) {
392 zfloat[j] = (float) (scale * (ztemp[j] & 0xffffff));
393 }
394 _mesa_pack_depth_span(ctx, width, dst, type,
395 zfloat, &clippedPacking);
396 dst += dstStride;
397 }
398 }
399 else {
400 /* untested, but simple: */
401 assert(format == GL_DEPTH_STENCIL_EXT);
402 for (i = 0; i < height; i++) {
403 pipe_get_tile_raw(surf, x, y, width, 1, dst, 0);
404 y += yStep;
405 dst += dstStride;
406 }
407 }
408 }
409 else if (surf->format == PIPE_FORMAT_Z16_UNORM) {
410 for (i = 0; i < height; i++) {
411 GLushort ztemp[MAX_WIDTH];
412 GLfloat zfloat[MAX_WIDTH];
413 const double scale = 1.0 / 0xffff;
414 pipe_get_tile_raw(surf, x, y, width, 1, ztemp, 0);
415 y += yStep;
416 for (j = 0; j < width; j++) {
417 zfloat[j] = (float) (scale * ztemp[j]);
418 }
419 _mesa_pack_depth_span(ctx, width, dst, type,
420 zfloat, &clippedPacking);
421 dst += dstStride;
422 }
423 }
424 else if (surf->format == PIPE_FORMAT_Z32_UNORM) {
425 for (i = 0; i < height; i++) {
426 GLuint ztemp[MAX_WIDTH];
427 GLfloat zfloat[MAX_WIDTH];
428 const double scale = 1.0 / 0xffffffff;
429 pipe_get_tile_raw(surf, x, y, width, 1, ztemp, 0);
430 y += yStep;
431 for (j = 0; j < width; j++) {
432 zfloat[j] = (float) (scale * ztemp[j]);
433 }
434 _mesa_pack_depth_span(ctx, width, dst, type,
435 zfloat, &clippedPacking);
436 dst += dstStride;
437 }
438 }
439 else {
440 /* RGBA format */
441 /* Do a row at a time to flip image data vertically */
442 for (i = 0; i < height; i++) {
443 pipe_get_tile_rgba(surf, x, y, width, 1, df);
444 y += yStep;
445 df += dfStride;
446 if (!dfStride) {
447 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
448 &clippedPacking, transferOps);
449 dst += dstStride;
450 }
451 }
452 }
453 }
454
455 pipe_surface_reference(&surf, NULL);
456
457 _mesa_unmap_readpix_pbo(ctx, &clippedPacking);
458 }
459
460
461 void st_init_readpixels_functions(struct dd_function_table *functions)
462 {
463 functions->ReadPixels = st_readpixels;
464 }