i965: Fix crash in do_blit_readpixels()
[mesa.git] / src / mesa / drivers / dri / i965 / intel_pixel_read.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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/fbobject.h"
33 #include "main/image.h"
34 #include "main/bufferobj.h"
35 #include "main/readpix.h"
36 #include "main/state.h"
37 #include "main/glformats.h"
38
39 #include "brw_context.h"
40 #include "intel_screen.h"
41 #include "intel_blit.h"
42 #include "intel_buffers.h"
43 #include "intel_fbo.h"
44 #include "intel_mipmap_tree.h"
45 #include "intel_regions.h"
46 #include "intel_pixel.h"
47 #include "intel_buffer_objects.h"
48
49 #define FILE_DEBUG_FLAG DEBUG_PIXEL
50
51 /* For many applications, the new ability to pull the source buffers
52 * back out of the GTT and then do the packing/conversion operations
53 * in software will be as much of an improvement as trying to get the
54 * blitter and/or texture engine to do the work.
55 *
56 * This step is gated on private backbuffers.
57 *
58 * Obviously the frontbuffer can't be pulled back, so that is either
59 * an argument for blit/texture readpixels, or for blitting to a
60 * temporary and then pulling that back.
61 *
62 * When the destination is a pbo, however, it's not clear if it is
63 * ever going to be pulled to main memory (though the access param
64 * will be a good hint). So it sounds like we do want to be able to
65 * choose between blit/texture implementation on the gpu and pullback
66 * and cpu-based copying.
67 *
68 * Unless you can magically turn client memory into a PBO for the
69 * duration of this call, there will be a cpu-based copying step in
70 * any case.
71 */
72
73 static bool
74 do_blit_readpixels(struct gl_context * ctx,
75 GLint x, GLint y, GLsizei width, GLsizei height,
76 GLenum format, GLenum type,
77 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
78 {
79 struct brw_context *brw = brw_context(ctx);
80 struct intel_buffer_object *dst = intel_buffer_object(pack->BufferObj);
81 GLuint dst_offset;
82 drm_intel_bo *dst_buffer;
83 GLint dst_x, dst_y;
84 GLuint dirty;
85
86 DBG("%s\n", __FUNCTION__);
87
88 assert(_mesa_is_bufferobj(pack->BufferObj));
89
90 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
91 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
92
93 /* Currently this function only supports reading from color buffers. */
94 if (!_mesa_is_color_format(format))
95 return false;
96
97 assert(irb != NULL);
98
99 if (ctx->_ImageTransferState ||
100 !_mesa_format_matches_format_and_type(irb->mt->format, format, type,
101 false)) {
102 DBG("%s - bad format for blit\n", __FUNCTION__);
103 return false;
104 }
105
106 if (pack->SwapBytes || pack->LsbFirst) {
107 DBG("%s: bad packing params\n", __FUNCTION__);
108 return false;
109 }
110
111 int dst_stride = _mesa_image_row_stride(pack, width, format, type);
112 bool dst_flip = false;
113 /* Mesa flips the dst_stride for pack->Invert, but we want our mt to have a
114 * normal dst_stride.
115 */
116 struct gl_pixelstore_attrib uninverted_pack = *pack;
117 if (pack->Invert) {
118 dst_stride = -dst_stride;
119 dst_flip = true;
120 uninverted_pack.Invert = false;
121 }
122
123 dst_offset = (GLintptr)pixels;
124 dst_offset += _mesa_image_offset(2, &uninverted_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 = brw->front_buffer_dirty;
135 intel_prepare_render(brw);
136 brw->front_buffer_dirty = dirty;
137
138 dst_buffer = intel_bufferobj_buffer(brw, dst,
139 dst_offset, height * dst_stride);
140
141 struct intel_mipmap_tree *pbo_mt =
142 intel_miptree_create_for_bo(brw,
143 dst_buffer,
144 irb->mt->format,
145 dst_offset,
146 width, height,
147 dst_stride, I915_TILING_NONE);
148
149 if (!intel_miptree_blit(brw,
150 irb->mt, irb->mt_level, irb->mt_layer,
151 x, y, _mesa_is_winsys_fbo(ctx->ReadBuffer),
152 pbo_mt, 0, 0,
153 0, 0, dst_flip,
154 width, height, GL_COPY)) {
155 return false;
156 }
157
158 intel_miptree_release(&pbo_mt);
159
160 DBG("%s - DONE\n", __FUNCTION__);
161
162 return true;
163 }
164
165 void
166 intelReadPixels(struct gl_context * 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 brw_context *brw = brw_context(ctx);
172 bool dirty;
173
174 DBG("%s\n", __FUNCTION__);
175
176 if (_mesa_is_bufferobj(pack->BufferObj)) {
177 /* Using PBOs, so try the BLT based path. */
178 if (do_blit_readpixels(ctx, x, y, width, height, format, type, pack,
179 pixels)) {
180 return;
181 }
182
183 perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__);
184 }
185
186 /* glReadPixels() wont dirty the front buffer, so reset the dirty
187 * flag after calling intel_prepare_render(). */
188 dirty = brw->front_buffer_dirty;
189 intel_prepare_render(brw);
190 brw->front_buffer_dirty = dirty;
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 brw->front_buffer_dirty = dirty;
204 }