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 "brw_screen.h"
41#include "brw_batch.h"
42#include "brw_buffers.h"
43#include "brw_fbo.h"
44#include "brw_mipmap_tree.h"
45#include "brw_pixel.h"
46#include "brw_buffer_objects.h"
47
48#define FILE_DEBUG_FLAG DEBUG_PIXEL
49
50/**
51 * \brief A fast path for glReadPixels
52 *
53 * This fast path is taken when the source format is BGRA, RGBA,
54 * A or L and when the texture memory is X- or Y-tiled.  It downloads
55 * the source data by directly mapping the memory without a GTT fence.
56 * This then needs to be de-tiled on the CPU before presenting the data to
57 * the user in the linear fasion.
58 *
59 * This is a performance win over the conventional texture download path.
60 * In the conventional texture download path, the texture is either mapped
61 * through the GTT or copied to a linear buffer with the blitter before
62 * handing off to a software path.  This allows us to avoid round-tripping
63 * through the GPU (in the case where we would be blitting) and do only a
64 * single copy operation.
65 */
66static bool
67brw_readpixels_tiled_memcpy(struct gl_context *ctx,
68                            GLint xoffset, GLint yoffset,
69                            GLsizei width, GLsizei height,
70                            GLenum format, GLenum type,
71                            GLvoid * pixels,
72                            const struct gl_pixelstore_attrib *pack)
73{
74   struct brw_context *brw = brw_context(ctx);
75   struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
76   const struct intel_device_info *devinfo = &brw->screen->devinfo;
77
78   /* This path supports reading from color buffers only */
79   if (rb == NULL)
80      return false;
81
82   struct brw_renderbuffer *irb = brw_renderbuffer(rb);
83   int dst_pitch;
84
85   /* The miptree's buffer. */
86   struct brw_bo *bo;
87
88   uint32_t cpp;
89   isl_memcpy_type copy_type;
90
91   /* This fastpath is restricted to specific renderbuffer types:
92    * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
93    * more types.
94    */
95   if (!devinfo->has_llc ||
96       !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
97       pixels == NULL ||
98       pack->BufferObj ||
99       pack->Alignment > 4 ||
100       pack->SkipPixels > 0 ||
101       pack->SkipRows > 0 ||
102       (pack->RowLength != 0 && pack->RowLength != width) ||
103       pack->SwapBytes ||
104       pack->LsbFirst ||
105       pack->Invert)
106      return false;
107
108   /* Only a simple blit, no scale, bias or other mapping. */
109   if (ctx->_ImageTransferState)
110      return false;
111
112   /* It is possible that the renderbuffer (or underlying texture) is
113    * multisampled.  Since ReadPixels from a multisampled buffer requires a
114    * multisample resolve, we can't handle this here
115    */
116   if (rb->NumSamples > 1)
117      return false;
118
119   /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
120    * function doesn't set the last channel to 1. Note this checks BaseFormat
121    * rather than TexFormat in case the RGBX format is being simulated with an
122    * RGBA format.
123    */
124   if (rb->_BaseFormat == GL_RGB)
125      return false;
126
127   copy_type = brw_miptree_get_memcpy_type(rb->Format, format, type, &cpp);
128   if (copy_type == ISL_MEMCPY_INVALID)
129      return false;
130
131   if (!irb->mt ||
132       (irb->mt->surf.tiling != ISL_TILING_X &&
133        irb->mt->surf.tiling != ISL_TILING_Y0)) {
134      /* The algorithm is written only for X- or Y-tiled memory. */
135      return false;
136   }
137
138   /* tiled_to_linear() assumes that if the object is swizzled, it is using
139    * I915_BIT6_SWIZZLE_9_10 for X and I915_BIT6_SWIZZLE_9 for Y.  This is only
140    * true on gfx5 and above.
141    *
142    * The killer on top is that some gfx4 have an L-shaped swizzle mode, where
143    * parts of the memory aren't swizzled at all. Userspace just can't handle
144    * that.
145    */
146   if (devinfo->ver < 5 && brw->has_swizzling)
147      return false;
148
149   /* Since we are going to read raw data to the miptree, we need to resolve
150    * any pending fast color clears before we start.
151    */
152   brw_miptree_access_raw(brw, irb->mt, irb->mt_level, irb->mt_layer, false);
153
154   bo = irb->mt->bo;
155
156   if (brw_batch_references(&brw->batch, bo)) {
157      perf_debug("Flushing before mapping a referenced bo.\n");
158      brw_batch_flush(brw);
159   }
160
161   void *map = brw_bo_map(brw, bo, MAP_READ | MAP_RAW);
162   if (map == NULL) {
163      DBG("%s: failed to map bo\n", __func__);
164      return false;
165   }
166
167   unsigned slice_offset_x, slice_offset_y;
168   brw_miptree_get_image_offset(irb->mt, irb->mt_level, irb->mt_layer,
169                                  &slice_offset_x, &slice_offset_y);
170   xoffset += slice_offset_x;
171   yoffset += slice_offset_y;
172
173   dst_pitch = _mesa_image_row_stride(pack, width, format, type);
174
175   /* For a window-system renderbuffer, the buffer is actually flipped
176    * vertically, so we need to handle that.  Since the detiling function
177    * can only really work in the forwards direction, we have to be a
178    * little creative.  First, we compute the Y-offset of the first row of
179    * the renderbuffer (in renderbuffer coordinates).  We then match that
180    * with the last row of the client's data.  Finally, we give
181    * tiled_to_linear a negative pitch so that it walks through the
182    * client's data backwards as it walks through the renderbufer forwards.
183    */
184   if (ctx->ReadBuffer->FlipY) {
185      yoffset = rb->Height - yoffset - height;
186      pixels += (ptrdiff_t) (height - 1) * dst_pitch;
187      dst_pitch = -dst_pitch;
188   }
189
190   /* We postponed printing this message until having committed to executing
191    * the function.
192    */
193   DBG("%s: x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
194       "mesa_format=0x%x tiling=%d "
195       "pack=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
196       __func__, xoffset, yoffset, width, height,
197       format, type, rb->Format, irb->mt->surf.tiling,
198       pack->Alignment, pack->RowLength, pack->SkipPixels,
199       pack->SkipRows);
200
201   isl_memcpy_tiled_to_linear(
202      xoffset * cpp, (xoffset + width) * cpp,
203      yoffset, yoffset + height,
204      pixels,
205      map + irb->mt->offset,
206      dst_pitch, irb->mt->surf.row_pitch_B,
207      brw->has_swizzling,
208      irb->mt->surf.tiling,
209      copy_type
210   );
211
212   brw_bo_unmap(bo);
213   return true;
214}
215
216static bool
217brw_readpixels_blorp(struct gl_context *ctx,
218                     unsigned x, unsigned y,
219                     unsigned w, unsigned h,
220                     GLenum format, GLenum type, const void *pixels,
221                     const struct gl_pixelstore_attrib *packing)
222{
223   struct brw_context *brw = brw_context(ctx);
224   struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
225   if (!rb)
226      return false;
227
228   struct brw_renderbuffer *irb = brw_renderbuffer(rb);
229
230   /* _mesa_get_readpixels_transfer_ops() includes the cases of read
231    * color clamping along with the ctx->_ImageTransferState.
232    */
233   if (_mesa_get_readpixels_transfer_ops(ctx, rb->Format, format,
234                                         type, GL_FALSE))
235      return false;
236
237   GLenum dst_base_format = _mesa_unpack_format_to_base_format(format);
238   if (_mesa_need_rgb_to_luminance_conversion(rb->_BaseFormat,
239                                              dst_base_format))
240      return false;
241
242   unsigned swizzle;
243   if (irb->Base.Base._BaseFormat == GL_RGB) {
244      swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
245   } else {
246      swizzle = SWIZZLE_XYZW;
247   }
248
249   return brw_blorp_download_miptree(brw, irb->mt, rb->Format, swizzle,
250                                     irb->mt_level, x, y, irb->mt_layer,
251                                     w, h, 1, GL_TEXTURE_2D, format, type,
252                                     ctx->ReadBuffer->FlipY, pixels, packing);
253}
254
255void
256brw_readpixels(struct gl_context *ctx,
257               GLint x, GLint y, GLsizei width, GLsizei height,
258               GLenum format, GLenum type,
259               const struct gl_pixelstore_attrib *pack, GLvoid *pixels)
260{
261   bool ok;
262
263   struct brw_context *brw = brw_context(ctx);
264   bool dirty;
265
266   DBG("%s\n", __func__);
267
268   /* Reading pixels wont dirty the front buffer, so reset the dirty
269    * flag after calling brw_prepare_render().
270    */
271   dirty = brw->front_buffer_dirty;
272   brw_prepare_render(brw);
273   brw->front_buffer_dirty = dirty;
274
275   if (pack->BufferObj) {
276      if (brw_readpixels_blorp(ctx, x, y, width, height,
277                               format, type, pixels, pack))
278         return;
279
280      perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
281   }
282
283   ok = brw_readpixels_tiled_memcpy(ctx, x, y, width, height,
284                                    format, type, pixels, pack);
285   if(ok)
286      return;
287
288   /* Update Mesa state before calling _mesa_readpixels().
289    * XXX this may not be needed since ReadPixels no longer uses the
290    * span code.
291    */
292
293   if (ctx->NewState)
294      _mesa_update_state(ctx);
295
296   _mesa_readpixels(ctx, x, y, width, height, format, type, pack, pixels);
297
298   /* There's an brw_prepare_render() call in intelSpanRenderStart(). */
299   brw->front_buffer_dirty = dirty;
300}
301