Merge commit 'origin/master' into drm-gem
[mesa.git] / src / mesa / drivers / dri / i915 / 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 "glheader.h"
29 #include "enums.h"
30 #include "mtypes.h"
31 #include "macros.h"
32 #include "image.h"
33 #include "bufferobj.h"
34 #include "swrast/swrast.h"
35
36 #include "intel_screen.h"
37 #include "intel_context.h"
38 #include "intel_ioctl.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_blit.h"
41 #include "intel_buffers.h"
42 #include "intel_regions.h"
43 #include "intel_pixel.h"
44 #include "intel_buffer_objects.h"
45
46 /* For many applications, the new ability to pull the source buffers
47 * back out of the GTT and then do the packing/conversion operations
48 * in software will be as much of an improvement as trying to get the
49 * blitter and/or texture engine to do the work.
50 *
51 * This step is gated on private backbuffers.
52 *
53 * Obviously the frontbuffer can't be pulled back, so that is either
54 * an argument for blit/texture readpixels, or for blitting to a
55 * temporary and then pulling that back.
56 *
57 * When the destination is a pbo, however, it's not clear if it is
58 * ever going to be pulled to main memory (though the access param
59 * will be a good hint). So it sounds like we do want to be able to
60 * choose between blit/texture implementation on the gpu and pullback
61 * and cpu-based copying.
62 *
63 * Unless you can magically turn client memory into a PBO for the
64 * duration of this call, there will be a cpu-based copying step in
65 * any case.
66 */
67
68
69 static GLboolean
70 do_texture_readpixels(GLcontext * ctx,
71 GLint x, GLint y, GLsizei width, GLsizei height,
72 GLenum format, GLenum type,
73 const struct gl_pixelstore_attrib *pack,
74 struct intel_region *dest_region)
75 {
76 #if 0
77 struct intel_context *intel = intel_context(ctx);
78 intelScreenPrivate *screen = intel->intelScreen;
79 GLint pitch = pack->RowLength ? pack->RowLength : width;
80 __DRIdrawablePrivate *dPriv = intel->driDrawable;
81 int textureFormat;
82 GLenum glTextureFormat;
83 int destFormat, depthFormat, destPitch;
84 drm_clip_rect_t tmp;
85
86 if (INTEL_DEBUG & DEBUG_PIXEL)
87 fprintf(stderr, "%s\n", __FUNCTION__);
88
89
90 if (ctx->_ImageTransferState ||
91 pack->SwapBytes || pack->LsbFirst || !pack->Invert) {
92 if (INTEL_DEBUG & DEBUG_PIXEL)
93 fprintf(stderr, "%s: check_color failed\n", __FUNCTION__);
94 return GL_FALSE;
95 }
96
97 intel->vtbl.meta_texrect_source(intel, intel_readbuf_region(intel));
98
99 if (!intel->vtbl.meta_render_dest(intel, dest_region, type, format)) {
100 if (INTEL_DEBUG & DEBUG_PIXEL)
101 fprintf(stderr, "%s: couldn't set dest %s/%s\n",
102 __FUNCTION__,
103 _mesa_lookup_enum_by_nr(type),
104 _mesa_lookup_enum_by_nr(format));
105 return GL_FALSE;
106 }
107
108 LOCK_HARDWARE(intel);
109
110 if (intel->driDrawable->numClipRects) {
111 intel->vtbl.install_meta_state(intel);
112 intel->vtbl.meta_no_depth_write(intel);
113 intel->vtbl.meta_no_stencil_write(intel);
114
115 if (!driClipRectToFramebuffer(ctx->ReadBuffer, &x, &y, &width, &height)) {
116 UNLOCK_HARDWARE(intel);
117 SET_STATE(i830, state);
118 if (INTEL_DEBUG & DEBUG_PIXEL)
119 fprintf(stderr, "%s: cliprect failed\n", __FUNCTION__);
120 return GL_TRUE;
121 }
122
123 y = dPriv->h - y - height;
124 x += dPriv->x;
125 y += dPriv->y;
126
127
128 /* Set the frontbuffer up as a large rectangular texture.
129 */
130 intel->vtbl.meta_tex_rect_source(intel, src_region, textureFormat);
131
132
133 intel->vtbl.meta_texture_blend_replace(i830, glTextureFormat);
134
135
136 /* Set the 3d engine to draw into the destination region:
137 */
138
139 intel->vtbl.meta_draw_region(intel, dest_region);
140 intel->vtbl.meta_draw_format(intel, destFormat, depthFormat); /* ?? */
141
142
143 /* Draw a single quad, no cliprects:
144 */
145 intel->vtbl.meta_disable_cliprects(intel);
146
147 intel->vtbl.draw_quad(intel,
148 0, width, 0, height,
149 0x00ff00ff, x, x + width, y, y + height);
150
151 intel->vtbl.leave_meta_state(intel);
152 }
153 UNLOCK_HARDWARE(intel);
154
155 intel_region_wait_fence(ctx, dest_region); /* required by GL */
156 return GL_TRUE;
157 #endif
158
159 return GL_FALSE;
160 }
161
162
163
164
165 static GLboolean
166 do_blit_readpixels(GLcontext * ctx,
167 GLint x, GLint y, GLsizei width, GLsizei height,
168 GLenum format, GLenum type,
169 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
170 {
171 struct intel_context *intel = intel_context(ctx);
172 struct intel_region *src = intel_readbuf_region(intel);
173 struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
174 GLuint dst_offset;
175 GLuint rowLength;
176
177 if (INTEL_DEBUG & DEBUG_PIXEL)
178 _mesa_printf("%s\n", __FUNCTION__);
179
180 if (!src)
181 return GL_FALSE;
182
183 if (dst) {
184 /* XXX This validation should be done by core mesa:
185 */
186 if (!_mesa_validate_pbo_access(2, pack, width, height, 1,
187 format, type, pixels)) {
188 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawPixels");
189 return GL_TRUE;
190 }
191 }
192 else {
193 /* PBO only for now:
194 */
195 if (INTEL_DEBUG & DEBUG_PIXEL)
196 _mesa_printf("%s - not PBO\n", __FUNCTION__);
197 return GL_FALSE;
198 }
199
200
201 if (ctx->_ImageTransferState ||
202 !intel_check_blit_format(src, format, type)) {
203 if (INTEL_DEBUG & DEBUG_PIXEL)
204 _mesa_printf("%s - bad format for blit\n", __FUNCTION__);
205 return GL_FALSE;
206 }
207
208 if (pack->Alignment != 1 || pack->SwapBytes || pack->LsbFirst) {
209 if (INTEL_DEBUG & DEBUG_PIXEL)
210 _mesa_printf("%s: bad packing params\n", __FUNCTION__);
211 return GL_FALSE;
212 }
213
214 if (pack->RowLength > 0)
215 rowLength = pack->RowLength;
216 else
217 rowLength = width;
218
219 if (pack->Invert) {
220 if (INTEL_DEBUG & DEBUG_PIXEL)
221 _mesa_printf("%s: MESA_PACK_INVERT not done yet\n", __FUNCTION__);
222 return GL_FALSE;
223 }
224 else {
225 rowLength = -rowLength;
226 }
227
228 /* XXX 64-bit cast? */
229 dst_offset = (GLuint) _mesa_image_address(2, pack, pixels, width, height,
230 format, type, 0, 0, 0);
231
232
233 /* Although the blits go on the command buffer, need to do this and
234 * fire with lock held to guarentee cliprects are correct.
235 */
236 intelFlush(&intel->ctx);
237 LOCK_HARDWARE(intel);
238
239 if (intel->driDrawable->numClipRects) {
240 GLboolean all = (width * height * src->cpp == dst->Base.Size &&
241 x == 0 && dst_offset == 0);
242
243 dri_bo *dst_buffer = intel_bufferobj_buffer(intel, dst,
244 all ? INTEL_WRITE_FULL :
245 INTEL_WRITE_PART);
246 __DRIdrawablePrivate *dPriv = intel->driDrawable;
247 int nbox = dPriv->numClipRects;
248 drm_clip_rect_t *box = dPriv->pClipRects;
249 drm_clip_rect_t rect;
250 drm_clip_rect_t src_rect;
251 int i;
252
253 src_rect.x1 = dPriv->x + x;
254 src_rect.y1 = dPriv->y + dPriv->h - (y + height);
255 src_rect.x2 = src_rect.x1 + width;
256 src_rect.y2 = src_rect.y1 + height;
257
258
259
260 for (i = 0; i < nbox; i++) {
261 if (!intel_intersect_cliprects(&rect, &src_rect, &box[i]))
262 continue;
263
264 intelEmitCopyBlit(intel,
265 src->cpp,
266 src->pitch, src->buffer, 0, src->tiled,
267 rowLength, dst_buffer, dst_offset, GL_FALSE,
268 rect.x1,
269 rect.y1,
270 rect.x1 - src_rect.x1,
271 rect.y2 - src_rect.y2,
272 rect.x2 - rect.x1, rect.y2 - rect.y1,
273 GL_COPY);
274 }
275
276 intel_batchbuffer_flush(intel->batch);
277 }
278 UNLOCK_HARDWARE(intel);
279
280 if (INTEL_DEBUG & DEBUG_PIXEL)
281 _mesa_printf("%s - DONE\n", __FUNCTION__);
282
283 return GL_TRUE;
284 }
285
286 void
287 intelReadPixels(GLcontext * ctx,
288 GLint x, GLint y, GLsizei width, GLsizei height,
289 GLenum format, GLenum type,
290 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
291 {
292 if (INTEL_DEBUG & DEBUG_PIXEL)
293 fprintf(stderr, "%s\n", __FUNCTION__);
294
295 intelFlush(ctx);
296
297 if (do_blit_readpixels
298 (ctx, x, y, width, height, format, type, pack, pixels))
299 return;
300
301 if (do_texture_readpixels
302 (ctx, x, y, width, height, format, type, pack, pixels))
303 return;
304
305 if (INTEL_DEBUG & DEBUG_PIXEL)
306 _mesa_printf("%s: fallback to swrast\n", __FUNCTION__);
307
308 _swrast_ReadPixels(ctx, x, y, width, height, format, type, pack, pixels);
309 }