Tizen 2.1 base
[sdk/emulator/qemu.git] / gl / mesa / src / mesa / drivers / dri / intel / intel_pixel_read.c
1 /**************************************************************************
2  *
3  * Copyright 2003 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 #include "main/glheader.h"
29 #include "main/enums.h"
30 #include "main/mtypes.h"
31 #include "main/macros.h"
32 #include "main/image.h"
33 #include "main/bufferobj.h"
34 #include "main/readpix.h"
35 #include "main/state.h"
36
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_blit.h"
40 #include "intel_buffers.h"
41 #include "intel_regions.h"
42 #include "intel_pixel.h"
43 #include "intel_buffer_objects.h"
44
45 #define FILE_DEBUG_FLAG DEBUG_PIXEL
46
47 /* For many applications, the new ability to pull the source buffers
48  * back out of the GTT and then do the packing/conversion operations
49  * in software will be as much of an improvement as trying to get the
50  * blitter and/or texture engine to do the work.
51  *
52  * This step is gated on private backbuffers.
53  *
54  * Obviously the frontbuffer can't be pulled back, so that is either
55  * an argument for blit/texture readpixels, or for blitting to a
56  * temporary and then pulling that back.
57  *
58  * When the destination is a pbo, however, it's not clear if it is
59  * ever going to be pulled to main memory (though the access param
60  * will be a good hint).  So it sounds like we do want to be able to
61  * choose between blit/texture implementation on the gpu and pullback
62  * and cpu-based copying.
63  *
64  * Unless you can magically turn client memory into a PBO for the
65  * duration of this call, there will be a cpu-based copying step in
66  * any case.
67  */
68
69 static bool
70 do_blit_readpixels(struct gl_context * ctx,
71                    GLint x, GLint y, GLsizei width, GLsizei height,
72                    GLenum format, GLenum type,
73                    const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
74 {
75    struct intel_context *intel = intel_context(ctx);
76    struct intel_region *src = intel_readbuf_region(intel);
77    struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
78    GLuint dst_offset;
79    GLuint rowLength;
80    drm_intel_bo *dst_buffer;
81    bool all;
82    GLint dst_x, dst_y;
83    GLuint dirty;
84
85    DBG("%s\n", __FUNCTION__);
86
87    if (!src)
88       return false;
89
90    if (!_mesa_is_bufferobj(pack->BufferObj)) {
91       /* PBO only for now:
92        */
93       DBG("%s - not PBO\n", __FUNCTION__);
94       return false;
95    }
96
97
98    if (ctx->_ImageTransferState ||
99        !intel_check_blit_format(src, format, type)) {
100       DBG("%s - bad format for blit\n", __FUNCTION__);
101       return false;
102    }
103
104    if (pack->Alignment != 1 || pack->SwapBytes || pack->LsbFirst) {
105       DBG("%s: bad packing params\n", __FUNCTION__);
106       return false;
107    }
108
109    if (pack->RowLength > 0)
110       rowLength = pack->RowLength;
111    else
112       rowLength = width;
113
114    if (pack->Invert) {
115       DBG("%s: MESA_PACK_INVERT not done yet\n", __FUNCTION__);
116       return false;
117    }
118    else {
119       if (ctx->ReadBuffer->Name == 0)
120          rowLength = -rowLength;
121    }
122
123    dst_offset = (GLintptr)pixels;
124    dst_offset += _mesa_image_offset(2, pack, width, height,
125                                     format, type, 0, 0, 0);
126
127    if (!_mesa_clip_copytexsubimage(ctx,
128                                    &dst_x, &dst_y,
129                                    &x, &y,
130                                    &width, &height)) {
131       return true;
132    }
133
134    dirty = intel->front_buffer_dirty;
135    intel_prepare_render(intel);
136    intel->front_buffer_dirty = dirty;
137
138    all = (width * height * src->cpp == dst->Base.Size &&
139           x == 0 && dst_offset == 0);
140
141    dst_x = 0;
142    dst_y = 0;
143
144    dst_buffer = intel_bufferobj_buffer(intel, dst,
145                                        all ? INTEL_WRITE_FULL :
146                                        INTEL_WRITE_PART);
147
148    if (ctx->ReadBuffer->Name == 0)
149       y = ctx->ReadBuffer->Height - (y + height);
150
151    if (!intelEmitCopyBlit(intel,
152                           src->cpp,
153                           src->pitch, src->bo, 0, src->tiling,
154                           rowLength, dst_buffer, dst_offset, false,
155                           x, y,
156                           dst_x, dst_y,
157                           width, height,
158                           GL_COPY)) {
159       return false;
160    }
161
162    DBG("%s - DONE\n", __FUNCTION__);
163
164    return true;
165 }
166
167 void
168 intelReadPixels(struct gl_context * ctx,
169                 GLint x, GLint y, GLsizei width, GLsizei height,
170                 GLenum format, GLenum type,
171                 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
172 {
173    struct intel_context *intel = intel_context(ctx);
174    bool dirty;
175
176    intel_flush_rendering_to_batch(ctx);
177
178    DBG("%s\n", __FUNCTION__);
179
180    if (do_blit_readpixels
181        (ctx, x, y, width, height, format, type, pack, pixels))
182       return;
183
184    /* glReadPixels() wont dirty the front buffer, so reset the dirty
185     * flag after calling intel_prepare_render(). */
186    dirty = intel->front_buffer_dirty;
187    intel_prepare_render(intel);
188    intel->front_buffer_dirty = dirty;
189
190    fallback_debug("%s: fallback to swrast\n", __FUNCTION__);
191
192    /* Update Mesa state before calling _mesa_readpixels().
193     * XXX this may not be needed since ReadPixels no longer uses the
194     * span code.
195     */
196
197    if (ctx->NewState)
198       _mesa_update_state(ctx);
199
200    _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
201
202    /* There's an intel_prepare_render() call in intelSpanRenderStart(). */
203    intel->front_buffer_dirty = dirty;
204 }