-static bool
-etna_manual_blit(struct etna_resource *dst, struct etna_resource_level *dst_lev,
- unsigned int dst_offset, struct etna_resource *src,
- struct etna_resource_level *src_lev, unsigned int src_offset,
- const struct pipe_blit_info *blit_info)
-{
- void *smap, *srow, *dmap, *drow;
- size_t tile_size;
-
- assert(src->layout == ETNA_LAYOUT_TILED);
- assert(dst->layout == ETNA_LAYOUT_TILED);
- assert(src->base.nr_samples == 0);
- assert(dst->base.nr_samples == 0);
-
- tile_size = util_format_get_blocksize(blit_info->src.format) * 4 * 4;
-
- smap = etna_bo_map(src->bo);
- if (!smap)
- return false;
-
- dmap = etna_bo_map(dst->bo);
- if (!dmap)
- return false;
-
- srow = smap + src_offset;
- drow = dmap + dst_offset;
-
- etna_bo_cpu_prep(src->bo, DRM_ETNA_PREP_READ);
- etna_bo_cpu_prep(dst->bo, DRM_ETNA_PREP_WRITE);
-
- for (int y = 0; y < blit_info->src.box.height; y += 4) {
- memcpy(drow, srow, tile_size * blit_info->src.box.width);
- srow += src_lev->stride * 4;
- drow += dst_lev->stride * 4;
- }
-
- etna_bo_cpu_fini(dst->bo);
- etna_bo_cpu_fini(src->bo);
-
- return true;
-}
-
-static bool
-etna_try_rs_blit(struct pipe_context *pctx,
- const struct pipe_blit_info *blit_info)
-{
- struct etna_context *ctx = etna_context(pctx);
- struct etna_resource *src = etna_resource(blit_info->src.resource);
- struct etna_resource *dst = etna_resource(blit_info->dst.resource);
- struct compiled_rs_state copy_to_screen;
- uint32_t ts_mem_config = 0;
- int msaa_xscale = 1, msaa_yscale = 1;
-
- /* Ensure that the level is valid */
- assert(blit_info->src.level <= src->base.last_level);
- assert(blit_info->dst.level <= dst->base.last_level);
-
- if (!translate_samples_to_xyscale(src->base.nr_samples, &msaa_xscale, &msaa_yscale, NULL))
- return FALSE;
-
- /* The width/height are in pixels; they do not change as a result of
- * multi-sampling. So, when blitting from a 4x multisampled surface
- * to a non-multisampled surface, the width and height will be
- * identical. As we do not support scaling, reject different sizes. */
- if (blit_info->dst.box.width != blit_info->src.box.width ||
- blit_info->dst.box.height != blit_info->src.box.height) {
- DBG("scaling requested: source %dx%d destination %dx%d",
- blit_info->src.box.width, blit_info->src.box.height,
- blit_info->dst.box.width, blit_info->dst.box.height);
- return FALSE;
- }
-
- /* No masks - RS can't copy specific channels */
- unsigned mask = util_format_get_mask(blit_info->dst.format);
- if ((blit_info->mask & mask) != mask) {
- DBG("sub-mask requested: 0x%02x vs format mask 0x%02x", blit_info->mask, mask);
- return FALSE;
- }
-
- unsigned src_format = etna_compatible_rs_format(blit_info->src.format);
- unsigned dst_format = etna_compatible_rs_format(blit_info->src.format);
- if (translate_rs_format(src_format) == ETNA_NO_MATCH ||
- translate_rs_format(dst_format) == ETNA_NO_MATCH ||
- blit_info->scissor_enable || blit_info->src.box.x != 0 ||
- blit_info->src.box.y != 0 || blit_info->dst.box.x != 0 ||
- blit_info->dst.box.y != 0 ||
- blit_info->dst.box.depth != blit_info->src.box.depth ||
- blit_info->dst.box.depth != 1) {
- return FALSE;
- }
-
- /* Ensure that the Z coordinate is sane */
- if (dst->base.target != PIPE_TEXTURE_CUBE)
- assert(blit_info->dst.box.z == 0);
- if (src->base.target != PIPE_TEXTURE_CUBE)
- assert(blit_info->src.box.z == 0);
-
- assert(blit_info->src.box.z < src->base.array_size);
- assert(blit_info->dst.box.z < dst->base.array_size);
-
- struct etna_resource_level *src_lev = &src->levels[blit_info->src.level];
- struct etna_resource_level *dst_lev = &dst->levels[blit_info->dst.level];
-
- /* we may be given coordinates up to the padded width to avoid
- * any alignment issues with different tiling formats */
- assert((blit_info->src.box.x + blit_info->src.box.width) * msaa_xscale <= src_lev->padded_width);
- assert((blit_info->src.box.y + blit_info->src.box.height) * msaa_yscale <= src_lev->padded_height);
- assert(blit_info->dst.box.x + blit_info->dst.box.width <= dst_lev->padded_width);
- assert(blit_info->dst.box.y + blit_info->dst.box.height <= dst_lev->padded_height);
-
- unsigned src_offset =
- src_lev->offset + blit_info->src.box.z * src_lev->layer_stride;
- unsigned dst_offset =
- dst_lev->offset + blit_info->dst.box.z * dst_lev->layer_stride;
-
- if (src_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
- dst_lev->padded_width <= ETNA_RS_WIDTH_MASK ||
- src_lev->padded_height <= ETNA_RS_HEIGHT_MASK ||
- dst_lev->padded_height <= ETNA_RS_HEIGHT_MASK)
- goto manual;
-
- /* If the width is not aligned to the RS width, but is within our
- * padding, adjust the width to suite the RS width restriction.
- * Note: the RS width/height are converted to source samples here. */
- unsigned int width = blit_info->src.box.width * msaa_xscale;
- unsigned int height = blit_info->src.box.height * msaa_yscale;
- unsigned int w_align = ETNA_RS_WIDTH_MASK + 1;
- unsigned int h_align = (ETNA_RS_HEIGHT_MASK + 1) * ctx->specs.pixel_pipes;
-
- if (width & (w_align - 1) && width >= src_lev->width * msaa_xscale && width >= dst_lev->width)
- width = align(width, w_align);
-
- if (height & (h_align - 1) && height >= src_lev->height * msaa_yscale && height >= dst_lev->height)
- height = align(height, h_align);
-
- /* The padded dimensions are in samples */
- if (width > src_lev->padded_width ||
- width > dst_lev->padded_width * msaa_xscale ||
- height > src_lev->padded_height ||
- height > dst_lev->padded_height * msaa_yscale)
- goto manual;
-
- if (src->base.nr_samples > 1) {
- uint32_t msaa_format = translate_msaa_format(src_format);
- assert(msaa_format != ETNA_NO_MATCH);
- ts_mem_config |= VIVS_TS_MEM_CONFIG_MSAA | msaa_format;
- }
-
- uint32_t to_flush = 0;
-
- if (src->base.bind & PIPE_BIND_RENDER_TARGET)
- to_flush |= VIVS_GL_FLUSH_CACHE_COLOR;
- if (src->base.bind & PIPE_BIND_DEPTH_STENCIL)
- to_flush |= VIVS_GL_FLUSH_CACHE_DEPTH;
-
- if (to_flush) {
- etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, to_flush);
- etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
- }
-
- /* Set up color TS to source surface before blit, if needed */
- if (src->levels[blit_info->src.level].ts_size &&
- src->levels[blit_info->src.level].ts_valid) {
- struct etna_reloc reloc;
- unsigned ts_offset =
- src_lev->ts_offset + blit_info->src.box.z * src_lev->ts_layer_stride;
-
- etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG,
- VIVS_TS_MEM_CONFIG_COLOR_FAST_CLEAR | ts_mem_config);
-
- memset(&reloc, 0, sizeof(struct etna_reloc));
- reloc.bo = src->ts_bo;
- reloc.offset = ts_offset;
- reloc.flags = ETNA_RELOC_READ;
- etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_STATUS_BASE, &reloc);
-
- memset(&reloc, 0, sizeof(struct etna_reloc));
- reloc.bo = src->bo;
- reloc.offset = src_offset;
- reloc.flags = ETNA_RELOC_READ;
- etna_set_state_reloc(ctx->stream, VIVS_TS_COLOR_SURFACE_BASE, &reloc);
-
- etna_set_state(ctx->stream, VIVS_TS_COLOR_CLEAR_VALUE,
- src->levels[blit_info->src.level].clear_value);
- } else {
- etna_set_state(ctx->stream, VIVS_TS_MEM_CONFIG, ts_mem_config);
- }
- ctx->dirty |= ETNA_DIRTY_TS;
-
- /* Kick off RS here */
- etna_compile_rs_state(ctx, ©_to_screen, &(struct rs_state) {
- .source_format = translate_rs_format(src_format),
- .source_tiling = src->layout,
- .source = src->bo,
- .source_offset = src_offset,
- .source_stride = src_lev->stride,
- .source_padded_height = src_lev->padded_height,
- .dest_format = translate_rs_format(dst_format),
- .dest_tiling = dst->layout,
- .dest = dst->bo,
- .dest_offset = dst_offset,
- .dest_stride = dst_lev->stride,
- .dest_padded_height = dst_lev->padded_height,
- .downsample_x = msaa_xscale > 1,
- .downsample_y = msaa_yscale > 1,
- .swap_rb = translate_rb_src_dst_swap(src->base.format, dst->base.format),
- .dither = {0xffffffff, 0xffffffff}, // XXX dither when going from 24 to 16 bit?
- .clear_mode = VIVS_RS_CLEAR_CONTROL_MODE_DISABLED,
- .width = width,
- .height = height
- });
-
- etna_submit_rs_state(ctx, ©_to_screen);
- resource_written(ctx, &dst->base);
- dst->seqno++;
- dst->levels[blit_info->dst.level].ts_valid = false;
-
- return TRUE;
-
-manual:
- if (src->layout == ETNA_LAYOUT_TILED && dst->layout == ETNA_LAYOUT_TILED) {
- if ((src->status & ETNA_PENDING_WRITE) ||
- (dst->status & ETNA_PENDING_WRITE))
- pctx->flush(pctx, NULL, 0);
- return etna_manual_blit(dst, dst_lev, dst_offset, src, src_lev, src_offset, blit_info);
- }
-
- return FALSE;
-}
-
-static void
-etna_blit(struct pipe_context *pctx, const struct pipe_blit_info *blit_info)
-{
- /* This is a more extended version of resource_copy_region */
- /* TODO Some cases can be handled by RS; if not, fall back to rendering or
- * even CPU copy block of pixels from info->src to info->dst
- * (resource, level, box, format);
- * function is used for scaling, flipping in x and y direction (negative
- * width/height), format conversion, mask and filter and even a scissor rectangle
- *
- * What can the RS do for us:
- * convert between tiling formats (layouts)
- * downsample 2x in x and y
- * convert between a limited number of pixel formats
- *
- * For the rest, fall back to util_blitter
- * XXX this goes wrong when source surface is supertiled. */
- struct etna_context *ctx = etna_context(pctx);
- struct pipe_blit_info info = *blit_info;
-
- if (info.src.resource->nr_samples > 1 &&
- info.dst.resource->nr_samples <= 1 &&
- !util_format_is_depth_or_stencil(info.src.resource->format) &&
- !util_format_is_pure_integer(info.src.resource->format)) {
- DBG("color resolve unimplemented");
- return;
- }
-
- if (etna_try_rs_blit(pctx, blit_info))
- return;
-
- if (util_try_blit_via_copy_region(pctx, blit_info))
- return;
-
- if (info.mask & PIPE_MASK_S) {
- DBG("cannot blit stencil, skipping");
- info.mask &= ~PIPE_MASK_S;
- }
-
- if (!util_blitter_is_blit_supported(ctx->blitter, &info)) {
- DBG("blit unsupported %s -> %s",
- util_format_short_name(info.src.resource->format),
- util_format_short_name(info.dst.resource->format));
- return;
- }
-
- etna_blit_save_state(ctx);
- util_blitter_blit(ctx->blitter, &info);
-}
-