+ /* Swap the backing bo's, so shadow becomes the old buffer,
+ * blit from shadow to new buffer. From here on out, we
+ * cannot fail.
+ *
+ * Note that we need to do it in this order, otherwise if
+ * we go down cpu blit path, the recursive transfer_map()
+ * sees the wrong status..
+ */
+ struct fd_resource *shadow = fd_resource(pshadow);
+
+ DBG("shadow: %p (%d) -> %p (%d)\n", rsc, rsc->base.reference.count,
+ shadow, shadow->base.reference.count);
+
+ /* TODO valid_buffer_range?? */
+ swap(rsc->bo, shadow->bo);
+ swap(rsc->write_batch, shadow->write_batch);
+ swap(rsc->offset, shadow->offset);
+ swap(rsc->ubwc_offset, shadow->ubwc_offset);
+ swap(rsc->ubwc_pitch, shadow->ubwc_pitch);
+ swap(rsc->ubwc_size, shadow->ubwc_size);
+ rsc->seqno = p_atomic_inc_return(&ctx->screen->rsc_seqno);
+
+ /* at this point, the newly created shadow buffer is not referenced
+ * by any batches, but the existing rsc (probably) is. We need to
+ * transfer those references over:
+ */
+ debug_assert(shadow->batch_mask == 0);
+ struct fd_batch *batch;
+ foreach_batch(batch, &ctx->screen->batch_cache, rsc->batch_mask) {
+ struct set_entry *entry = _mesa_set_search(batch->resources, rsc);
+ _mesa_set_remove(batch->resources, entry);
+ _mesa_set_add(batch->resources, shadow);
+ }
+ swap(rsc->batch_mask, shadow->batch_mask);
+
+ mtx_unlock(&ctx->screen->lock);
+
+ struct pipe_blit_info blit = {};
+ blit.dst.resource = prsc;
+ blit.dst.format = prsc->format;
+ blit.src.resource = pshadow;
+ blit.src.format = pshadow->format;
+ blit.mask = util_format_get_mask(prsc->format);
+ blit.filter = PIPE_TEX_FILTER_NEAREST;
+
+#define set_box(field, val) do { \
+ blit.dst.field = (val); \
+ blit.src.field = (val); \
+ } while (0)
+
+ /* blit the other levels in their entirety: */
+ for (unsigned l = 0; l <= prsc->last_level; l++) {
+ if (box && l == level)
+ continue;
+
+ /* just blit whole level: */
+ set_box(level, l);
+ set_box(box.width, u_minify(prsc->width0, l));
+ set_box(box.height, u_minify(prsc->height0, l));
+ set_box(box.depth, u_minify(prsc->depth0, l));
+
+ do_blit(ctx, &blit, fallback);
+ }
+
+ /* deal w/ current level specially, since we might need to split
+ * it up into a couple blits:
+ */
+ if (box && !discard_whole_level) {
+ set_box(level, level);
+
+ switch (prsc->target) {
+ case PIPE_BUFFER:
+ case PIPE_TEXTURE_1D:
+ set_box(box.y, 0);
+ set_box(box.z, 0);
+ set_box(box.height, 1);
+ set_box(box.depth, 1);
+
+ if (box->x > 0) {
+ set_box(box.x, 0);
+ set_box(box.width, box->x);
+
+ do_blit(ctx, &blit, fallback);
+ }
+ if ((box->x + box->width) < u_minify(prsc->width0, level)) {
+ set_box(box.x, box->x + box->width);
+ set_box(box.width, u_minify(prsc->width0, level) - (box->x + box->width));
+
+ do_blit(ctx, &blit, fallback);
+ }
+ break;
+ case PIPE_TEXTURE_2D:
+ /* TODO */
+ default:
+ unreachable("TODO");
+ }
+ }
+
+ ctx->in_shadow = false;
+
+ pipe_resource_reference(&pshadow, NULL);
+
+ return true;