1 /*
2  * Copyright 2003 VMware, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include "main/enums.h"
27 #include "main/mtypes.h"
28 #include "main/macros.h"
29 #include "main/fbobject.h"
30 #include "main/image.h"
31 #include "main/bufferobj.h"
32 #include "main/readpix.h"
33 #include "main/state.h"
34 #include "main/glformats.h"
35 #include "program/prog_instruction.h"
36 #include "drivers/common/meta.h"
37 
38 #include "brw_context.h"
39 #include "brw_blorp.h"
40 #include "intel_screen.h"
41 #include "intel_batchbuffer.h"
42 #include "intel_blit.h"
43 #include "intel_buffers.h"
44 #include "intel_fbo.h"
45 #include "intel_mipmap_tree.h"
46 #include "intel_pixel.h"
47 #include "intel_buffer_objects.h"
48 #include "intel_tiled_memcpy.h"
49 
50 #define FILE_DEBUG_FLAG DEBUG_PIXEL
51 
52 /**
53  * \brief A fast path for glReadPixels
54  *
55  * This fast path is taken when the source format is BGRA, RGBA,
56  * A or L and when the texture memory is X- or Y-tiled.  It downloads
57  * the source data by directly mapping the memory without a GTT fence.
58  * This then needs to be de-tiled on the CPU before presenting the data to
59  * the user in the linear fasion.
60  *
61  * This is a performance win over the conventional texture download path.
62  * In the conventional texture download path, the texture is either mapped
63  * through the GTT or copied to a linear buffer with the blitter before
64  * handing off to a software path.  This allows us to avoid round-tripping
65  * through the GPU (in the case where we would be blitting) and do only a
66  * single copy operation.
67  */
68 static bool
intel_readpixels_tiled_memcpy(struct gl_context * ctx,GLint xoffset,GLint yoffset,GLsizei width,GLsizei height,GLenum format,GLenum type,GLvoid * pixels,const struct gl_pixelstore_attrib * pack)69 intel_readpixels_tiled_memcpy(struct gl_context * ctx,
70                               GLint xoffset, GLint yoffset,
71                               GLsizei width, GLsizei height,
72                               GLenum format, GLenum type,
73                               GLvoid * pixels,
74                               const struct gl_pixelstore_attrib *pack)
75 {
76    struct brw_context *brw = brw_context(ctx);
77    struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
78    const struct gen_device_info *devinfo = &brw->screen->devinfo;
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    struct brw_bo *bo;
89 
90    uint32_t cpp;
91    mem_copy_fn mem_copy = NULL;
92 
93    /* This fastpath is restricted to specific renderbuffer types:
94     * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
95     * more types.
96     */
97    if (!devinfo->has_llc ||
98        !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
99        pixels == NULL ||
100        _mesa_is_bufferobj(pack->BufferObj) ||
101        pack->Alignment > 4 ||
102        pack->SkipPixels > 0 ||
103        pack->SkipRows > 0 ||
104        (pack->RowLength != 0 && pack->RowLength != width) ||
105        pack->SwapBytes ||
106        pack->LsbFirst ||
107        pack->Invert)
108       return false;
109 
110    /* Only a simple blit, no scale, bias or other mapping. */
111    if (ctx->_ImageTransferState)
112       return false;
113 
114    /* It is possible that the renderbuffer (or underlying texture) is
115     * multisampled.  Since ReadPixels from a multisampled buffer requires a
116     * multisample resolve, we can't handle this here
117     */
118    if (rb->NumSamples > 1)
119       return false;
120 
121    /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
122     * function doesn't set the last channel to 1. Note this checks BaseFormat
123     * rather than TexFormat in case the RGBX format is being simulated with an
124     * RGBA format.
125     */
126    if (rb->_BaseFormat == GL_RGB)
127       return false;
128 
129    if (!intel_get_memcpy(rb->Format, format, type, &mem_copy, &cpp))
130       return false;
131 
132    if (!irb->mt ||
133        (irb->mt->surf.tiling != ISL_TILING_X &&
134         irb->mt->surf.tiling != ISL_TILING_Y0)) {
135       /* The algorithm is written only for X- or Y-tiled memory. */
136       return false;
137    }
138 
139    /* tiled_to_linear() assumes that if the object is swizzled, it is using
140     * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y.  This is only
141     * true on gen5 and above.
142     *
143     * The killer on top is that some gen4 have an L-shaped swizzle mode, where
144     * parts of the memory aren't swizzled at all. Userspace just can't handle
145     * that.
146     */
147    if (devinfo->gen < 5 && brw->has_swizzling)
148       return false;
149 
150    /* Since we are going to read raw data to the miptree, we need to resolve
151     * any pending fast color clears before we start.
152     */
153    intel_miptree_access_raw(brw, irb->mt, irb->mt_level, irb->mt_layer, false);
154 
155    bo = irb->mt->bo;
156 
157    if (brw_batch_references(&brw->batch, bo)) {
158       perf_debug("Flushing before mapping a referenced bo.\n");
159       intel_batchbuffer_flush(brw);
160    }
161 
162    void *map = brw_bo_map(brw, bo, MAP_READ | MAP_RAW);
163    if (map == NULL) {
164       DBG("%s: failed to map bo\n", __func__);
165       return false;
166    }
167 
168    unsigned slice_offset_x, slice_offset_y;
169    intel_miptree_get_image_offset(irb->mt, irb->mt_level, irb->mt_layer,
170                                   &slice_offset_x, &slice_offset_y);
171    xoffset += slice_offset_x;
172    yoffset += slice_offset_y;
173 
174    dst_pitch = _mesa_image_row_stride(pack, width, format, type);
175 
176    /* For a window-system renderbuffer, the buffer is actually flipped
177     * vertically, so we need to handle that.  Since the detiling function
178     * can only really work in the forwards direction, we have to be a
179     * little creative.  First, we compute the Y-offset of the first row of
180     * the renderbuffer (in renderbuffer coordinates).  We then match that
181     * with the last row of the client's data.  Finally, we give
182     * tiled_to_linear a negative pitch so that it walks through the
183     * client's data backwards as it walks through the renderbufer forwards.
184     */
185    if (rb->Name == 0) {
186       yoffset = rb->Height - yoffset - height;
187       pixels += (ptrdiff_t) (height - 1) * dst_pitch;
188       dst_pitch = -dst_pitch;
189    }
190 
191    /* We postponed printing this message until having committed to executing
192     * the function.
193     */
194    DBG("%s: x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
195        "mesa_format=0x%x tiling=%d "
196        "pack=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
197        __func__, xoffset, yoffset, width, height,
198        format, type, rb->Format, irb->mt->surf.tiling,
199        pack->Alignment, pack->RowLength, pack->SkipPixels,
200        pack->SkipRows);
201 
202    tiled_to_linear(
203       xoffset * cpp, (xoffset + width) * cpp,
204       yoffset, yoffset + height,
205       pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
206       map + irb->mt->offset,
207       dst_pitch, irb->mt->surf.row_pitch,
208       brw->has_swizzling,
209       irb->mt->surf.tiling,
210       mem_copy
211    );
212 
213    brw_bo_unmap(bo);
214    return true;
215 }
216 
217 static bool
intel_readpixels_blorp(struct gl_context * ctx,unsigned x,unsigned y,unsigned w,unsigned h,GLenum format,GLenum type,const void * pixels,const struct gl_pixelstore_attrib * packing)218 intel_readpixels_blorp(struct gl_context *ctx,
219                        unsigned x, unsigned y,
220                        unsigned w, unsigned h,
221                        GLenum format, GLenum type, const void *pixels,
222                        const struct gl_pixelstore_attrib *packing)
223 {
224    struct brw_context *brw = brw_context(ctx);
225    struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
226    if (!rb)
227       return false;
228 
229    struct intel_renderbuffer *irb = intel_renderbuffer(rb);
230 
231    /* _mesa_get_readpixels_transfer_ops() includes the cases of read
232     * color clamping along with the ctx->_ImageTransferState.
233     */
234    if (_mesa_get_readpixels_transfer_ops(ctx, rb->Format, format,
235                                          type, GL_FALSE))
236       return false;
237 
238    GLenum dst_base_format = _mesa_unpack_format_to_base_format(format);
239    if (_mesa_need_rgb_to_luminance_conversion(rb->_BaseFormat,
240                                               dst_base_format))
241       return false;
242 
243    unsigned swizzle;
244    if (irb->Base.Base._BaseFormat == GL_RGB) {
245       swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
246    } else {
247       swizzle = SWIZZLE_XYZW;
248    }
249 
250    return brw_blorp_download_miptree(brw, irb->mt, rb->Format, swizzle,
251                                      irb->mt_level, x, y, irb->mt_layer,
252                                      w, h, 1, GL_TEXTURE_2D, format, type,
253                                      rb->Name == 0, pixels, packing);
254 }
255 
256 void
intelReadPixels(struct gl_context * ctx,GLint x,GLint y,GLsizei width,GLsizei height,GLenum format,GLenum type,const struct gl_pixelstore_attrib * pack,GLvoid * pixels)257 intelReadPixels(struct gl_context * ctx,
258                 GLint x, GLint y, GLsizei width, GLsizei height,
259                 GLenum format, GLenum type,
260                 const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
261 {
262    bool ok;
263 
264    struct brw_context *brw = brw_context(ctx);
265    bool dirty;
266 
267    DBG("%s\n", __func__);
268 
269    /* Reading pixels wont dirty the front buffer, so reset the dirty
270     * flag after calling intel_prepare_render().
271     */
272    dirty = brw->front_buffer_dirty;
273    intel_prepare_render(brw);
274    brw->front_buffer_dirty = dirty;
275 
276    if (_mesa_is_bufferobj(pack->BufferObj)) {
277       if (intel_readpixels_blorp(ctx, x, y, width, height,
278                                  format, type, pixels, pack))
279          return;
280 
281       perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
282    }
283 
284    ok = intel_readpixels_tiled_memcpy(ctx, x, y, width, height,
285                                       format, type, pixels, pack);
286    if(ok)
287       return;
288 
289    /* Update Mesa state before calling _mesa_readpixels().
290     * XXX this may not be needed since ReadPixels no longer uses the
291     * span code.
292     */
293 
294    if (ctx->NewState)
295       _mesa_update_state(ctx);
296 
297    _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
298 
299    /* There's an intel_prepare_render() call in intelSpanRenderStart(). */
300    brw->front_buffer_dirty = dirty;
301 }
302