+ const struct gen_device_info *devinfo = &brw->screen->devinfo;
+ struct gl_context *ctx = &brw->ctx;
+
+ intel_miptree_unmap_raw(map->linear_mt);
+
+ if (map->mode & GL_MAP_WRITE_BIT) {
+ if (devinfo->gen >= 6) {
+ brw_blorp_copy_miptrees(brw, map->linear_mt, 0, 0,
+ mt, level, slice,
+ 0, 0, map->x, map->y, map->w, map->h);
+ } else {
+ bool ok = intel_miptree_copy(brw,
+ map->linear_mt, 0, 0, 0, 0,
+ mt, level, slice, map->x, map->y,
+ map->w, map->h);
+ WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
+ }
+ }
+
+ intel_miptree_release(&map->linear_mt);
+}
+
+/* Compute extent parameters for use with tiled_memcpy functions.
+ * xs are in units of bytes and ys are in units of strides.
+ */
+static inline void
+tile_extents(struct intel_mipmap_tree *mt, struct intel_miptree_map *map,
+ unsigned int level, unsigned int slice, unsigned int *x1_B,
+ unsigned int *x2_B, unsigned int *y1_el, unsigned int *y2_el)
+{
+ unsigned int block_width, block_height;
+ unsigned int x0_el, y0_el;
+
+ _mesa_get_format_block_size(mt->format, &block_width, &block_height);
+
+ assert(map->x % block_width == 0);
+ assert(map->y % block_height == 0);
+
+ intel_miptree_get_image_offset(mt, level, slice, &x0_el, &y0_el);
+ *x1_B = (map->x / block_width + x0_el) * mt->cpp;
+ *y1_el = map->y / block_height + y0_el;
+ *x2_B = (DIV_ROUND_UP(map->x + map->w, block_width) + x0_el) * mt->cpp;
+ *y2_el = DIV_ROUND_UP(map->y + map->h, block_height) + y0_el;
+}
+
+static void
+intel_miptree_unmap_tiled_memcpy(struct brw_context *brw,
+ struct intel_mipmap_tree *mt,
+ struct intel_miptree_map *map,
+ unsigned int level,
+ unsigned int slice)
+{
+ if (map->mode & GL_MAP_WRITE_BIT) {
+ unsigned int x1, x2, y1, y2;
+ tile_extents(mt, map, level, slice, &x1, &x2, &y1, &y2);
+
+ char *dst = intel_miptree_map_raw(brw, mt, map->mode | MAP_RAW);
+ dst += mt->offset;
+
+ isl_memcpy_linear_to_tiled(
+ x1, x2, y1, y2, dst, map->ptr, mt->surf.row_pitch_B, map->stride,
+ brw->has_swizzling, mt->surf.tiling, ISL_MEMCPY);
+
+ intel_miptree_unmap_raw(mt);
+ }
+ _mesa_align_free(map->buffer);
+ map->buffer = map->ptr = NULL;
+}
+
+/**
+ * Determine which copy function to use for the given format combination
+ *
+ * The only two possible copy functions which are ever returned are a
+ * direct memcpy and a RGBA <-> BGRA copy function. Since RGBA -> BGRA and
+ * BGRA -> RGBA are exactly the same operation (and memcpy is obviously
+ * symmetric), it doesn't matter whether the copy is from the tiled image
+ * to the untiled or vice versa. The copy function required is the same in
+ * either case so this function can be used.
+ *
+ * \param[in] tiledFormat The format of the tiled image
+ * \param[in] format The GL format of the client data
+ * \param[in] type The GL type of the client data
+ * \param[out] mem_copy Will be set to one of either the standard
+ * library's memcpy or a different copy function
+ * that performs an RGBA to BGRA conversion
+ * \param[out] cpp Number of bytes per channel
+ *
+ * \return true if the format and type combination are valid
+ */
+MAYBE_UNUSED isl_memcpy_type
+intel_miptree_get_memcpy_type(mesa_format tiledFormat, GLenum format, GLenum type,
+ uint32_t *cpp)
+{
+ if (type == GL_UNSIGNED_INT_8_8_8_8_REV &&
+ !(format == GL_RGBA || format == GL_BGRA))
+ return ISL_MEMCPY_INVALID; /* Invalid type/format combination */
+
+ if ((tiledFormat == MESA_FORMAT_L_UNORM8 && format == GL_LUMINANCE) ||
+ (tiledFormat == MESA_FORMAT_A_UNORM8 && format == GL_ALPHA)) {
+ *cpp = 1;
+ return ISL_MEMCPY;
+ } else if ((tiledFormat == MESA_FORMAT_B8G8R8A8_UNORM) ||
+ (tiledFormat == MESA_FORMAT_B8G8R8X8_UNORM) ||
+ (tiledFormat == MESA_FORMAT_B8G8R8A8_SRGB) ||
+ (tiledFormat == MESA_FORMAT_B8G8R8X8_SRGB)) {
+ *cpp = 4;
+ if (format == GL_BGRA) {
+ return ISL_MEMCPY;
+ } else if (format == GL_RGBA) {
+ return ISL_MEMCPY_BGRA8;
+ }
+ } else if ((tiledFormat == MESA_FORMAT_R8G8B8A8_UNORM) ||
+ (tiledFormat == MESA_FORMAT_R8G8B8X8_UNORM) ||
+ (tiledFormat == MESA_FORMAT_R8G8B8A8_SRGB) ||
+ (tiledFormat == MESA_FORMAT_R8G8B8X8_SRGB)) {
+ *cpp = 4;
+ if (format == GL_BGRA) {
+ /* Copying from RGBA to BGRA is the same as BGRA to RGBA so we can
+ * use the same function.
+ */
+ return ISL_MEMCPY_BGRA8;
+ } else if (format == GL_RGBA) {
+ return ISL_MEMCPY;
+ }
+ }
+
+ return ISL_MEMCPY_INVALID;
+}
+
+static void
+intel_miptree_map_tiled_memcpy(struct brw_context *brw,
+ struct intel_mipmap_tree *mt,
+ struct intel_miptree_map *map,
+ unsigned int level, unsigned int slice)
+{
+ intel_miptree_access_raw(brw, mt, level, slice,
+ map->mode & GL_MAP_WRITE_BIT);
+
+ unsigned int x1, x2, y1, y2;
+ tile_extents(mt, map, level, slice, &x1, &x2, &y1, &y2);
+ map->stride = ALIGN(_mesa_format_row_stride(mt->format, map->w), 16);
+
+ /* The tiling and detiling functions require that the linear buffer
+ * has proper 16-byte alignment (that is, its `x0` is 16-byte
+ * aligned). Here we over-allocate the linear buffer by enough
+ * bytes to get the proper alignment.
+ */
+ map->buffer = _mesa_align_malloc(map->stride * (y2 - y1) + (x1 & 0xf), 16);
+ map->ptr = (char *)map->buffer + (x1 & 0xf);
+ assert(map->buffer);
+
+ if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
+ char *src = intel_miptree_map_raw(brw, mt, map->mode | MAP_RAW);
+ src += mt->offset;
+
+ const isl_memcpy_type copy_type =
+#if defined(USE_SSE41)
+ cpu_has_sse4_1 ? ISL_MEMCPY_STREAMING_LOAD :
+#endif
+ ISL_MEMCPY;
+
+ isl_memcpy_tiled_to_linear(
+ x1, x2, y1, y2, map->ptr, src, map->stride,
+ mt->surf.row_pitch_B, brw->has_swizzling, mt->surf.tiling,
+ copy_type);
+
+ intel_miptree_unmap_raw(mt);
+ }
+
+ map->unmap = intel_miptree_unmap_tiled_memcpy;