intel/pixel_read: Properly flip the results for window system buffers
[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 #include "drivers/common/meta.h"
39
40 #include "brw_context.h"
41 #include "intel_screen.h"
42 #include "intel_batchbuffer.h"
43 #include "intel_blit.h"
44 #include "intel_buffers.h"
45 #include "intel_fbo.h"
46 #include "intel_mipmap_tree.h"
47 #include "intel_pixel.h"
48 #include "intel_buffer_objects.h"
49 #include "intel_tiled_memcpy.h"
50
51 #define FILE_DEBUG_FLAG DEBUG_PIXEL
52
53 /**
54 * \brief A fast path for glReadPixels
55 *
56 * This fast path is taken when the source format is BGRA, RGBA,
57 * A or L and when the texture memory is X- or Y-tiled. It downloads
58 * the source data by directly mapping the memory without a GTT fence.
59 * This then needs to be de-tiled on the CPU before presenting the data to
60 * the user in the linear fasion.
61 *
62 * This is a performance win over the conventional texture download path.
63 * In the conventional texture download path, the texture is either mapped
64 * through the GTT or copied to a linear buffer with the blitter before
65 * handing off to a software path. This allows us to avoid round-tripping
66 * through the GPU (in the case where we would be blitting) and do only a
67 * single copy operation.
68 */
69 static bool
70 intel_readpixels_tiled_memcpy(struct gl_context * ctx,
71 GLint xoffset, GLint yoffset,
72 GLsizei width, GLsizei height,
73 GLenum format, GLenum type,
74 GLvoid * pixels,
75 const struct gl_pixelstore_attrib *pack)
76 {
77 struct brw_context *brw = brw_context(ctx);
78 struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
79
80 /* This path supports reading from color buffers only */
81 if (rb == NULL)
82 return false;
83
84 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
85 int dst_pitch;
86
87 /* The miptree's buffer. */
88 drm_intel_bo *bo;
89
90 int error = 0;
91
92 uint32_t cpp;
93 mem_copy_fn mem_copy = NULL;
94
95 /* This fastpath is restricted to specific renderbuffer types:
96 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
97 * more types.
98 */
99 if (!brw->has_llc ||
100 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
101 pixels == NULL ||
102 _mesa_is_bufferobj(pack->BufferObj) ||
103 pack->Alignment > 4 ||
104 pack->SkipPixels > 0 ||
105 pack->SkipRows > 0 ||
106 (pack->RowLength != 0 && pack->RowLength != width) ||
107 pack->SwapBytes ||
108 pack->LsbFirst ||
109 pack->Invert)
110 return false;
111
112 /* This renderbuffer can come from a texture. In this case, we impose
113 * some of the same restrictions we have for textures and adjust for
114 * miplevels.
115 */
116 if (rb->TexImage) {
117 if (rb->TexImage->TexObject->Target != GL_TEXTURE_2D &&
118 rb->TexImage->TexObject->Target != GL_TEXTURE_RECTANGLE)
119 return false;
120
121 int level = rb->TexImage->Level + rb->TexImage->TexObject->MinLevel;
122
123 /* Adjust x and y offset based on miplevel */
124 xoffset += irb->mt->level[level].level_x;
125 yoffset += irb->mt->level[level].level_y;
126 }
127
128 if (!intel_get_memcpy(rb->Format, format, type, &mem_copy, &cpp))
129 return false;
130
131 if (!irb->mt ||
132 (irb->mt->tiling != I915_TILING_X &&
133 irb->mt->tiling != I915_TILING_Y)) {
134 /* The algorithm is written only for X- or Y-tiled memory. */
135 return false;
136 }
137
138 /* Since we are going to read raw data to the miptree, we need to resolve
139 * any pending fast color clears before we start.
140 */
141 intel_miptree_resolve_color(brw, irb->mt);
142
143 bo = irb->mt->bo;
144
145 if (drm_intel_bo_references(brw->batch.bo, bo)) {
146 perf_debug("Flushing before mapping a referenced bo.\n");
147 intel_batchbuffer_flush(brw);
148 }
149
150 error = brw_bo_map(brw, bo, false /* write enable */, "miptree");
151 if (error) {
152 DBG("%s: failed to map bo\n", __FUNCTION__);
153 return false;
154 }
155
156 dst_pitch = _mesa_image_row_stride(pack, width, format, type);
157
158 /* For a window-system renderbuffer, the buffer is actually flipped
159 * vertically, so we need to handle that. Since the detiling function
160 * can only really work in the forwards direction, we have to be a
161 * little creative. First, we compute the Y-offset of the first row of
162 * the renderbuffer (in renderbuffer coordinates). We then match that
163 * with the last row of the client's data. Finally, we give
164 * tiled_to_linear a negative pitch so that it walks through the
165 * client's data backwards as it walks through the renderbufer forwards.
166 */
167 if (rb->Name == 0) {
168 yoffset = rb->Height - yoffset - height;
169 pixels += (ptrdiff_t) (height - 1) * dst_pitch;
170 dst_pitch = -dst_pitch;
171 }
172
173 /* We postponed printing this message until having committed to executing
174 * the function.
175 */
176 DBG("%s: x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
177 "mesa_format=0x%x tiling=%d "
178 "pack=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
179 __FUNCTION__, xoffset, yoffset, width, height,
180 format, type, rb->Format, irb->mt->tiling,
181 pack->Alignment, pack->RowLength, pack->SkipPixels,
182 pack->SkipRows);
183
184 tiled_to_linear(
185 xoffset * cpp, (xoffset + width) * cpp,
186 yoffset, yoffset + height,
187 pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
188 bo->virtual,
189 dst_pitch, irb->mt->pitch,
190 brw->has_swizzling,
191 irb->mt->tiling,
192 mem_copy
193 );
194
195 drm_intel_bo_unmap(bo);
196 return true;
197 }
198
199 void
200 intelReadPixels(struct gl_context * ctx,
201 GLint x, GLint y, GLsizei width, GLsizei height,
202 GLenum format, GLenum type,
203 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
204 {
205 bool ok;
206
207 struct brw_context *brw = brw_context(ctx);
208 bool dirty;
209
210 DBG("%s\n", __FUNCTION__);
211
212 if (_mesa_is_bufferobj(pack->BufferObj)) {
213 if (_mesa_meta_pbo_GetTexSubImage(ctx, 2, NULL, x, y, 0, width, height, 1,
214 format, type, pixels, pack))
215 return;
216
217 perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__);
218 }
219
220 ok = intel_readpixels_tiled_memcpy(ctx, x, y, width, height,
221 format, type, pixels, pack);
222 if(ok)
223 return;
224
225 /* glReadPixels() wont dirty the front buffer, so reset the dirty
226 * flag after calling intel_prepare_render(). */
227 dirty = brw->front_buffer_dirty;
228 intel_prepare_render(brw);
229 brw->front_buffer_dirty = dirty;
230
231 /* Update Mesa state before calling _mesa_readpixels().
232 * XXX this may not be needed since ReadPixels no longer uses the
233 * span code.
234 */
235
236 if (ctx->NewState)
237 _mesa_update_state(ctx);
238
239 _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
240
241 /* There's an intel_prepare_render() call in intelSpanRenderStart(). */
242 brw->front_buffer_dirty = dirty;
243 }