gen6_render.c revision 03b705cf
103b705cfSriastradh/*
203b705cfSriastradh * Copyright © 2006,2008,2011 Intel Corporation
303b705cfSriastradh * Copyright © 2007 Red Hat, Inc.
403b705cfSriastradh *
503b705cfSriastradh * Permission is hereby granted, free of charge, to any person obtaining a
603b705cfSriastradh * copy of this software and associated documentation files (the "Software"),
703b705cfSriastradh * to deal in the Software without restriction, including without limitation
803b705cfSriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
903b705cfSriastradh * and/or sell copies of the Software, and to permit persons to whom the
1003b705cfSriastradh * Software is furnished to do so, subject to the following conditions:
1103b705cfSriastradh *
1203b705cfSriastradh * The above copyright notice and this permission notice (including the next
1303b705cfSriastradh * paragraph) shall be included in all copies or substantial portions of the
1403b705cfSriastradh * Software.
1503b705cfSriastradh *
1603b705cfSriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1703b705cfSriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1803b705cfSriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1903b705cfSriastradh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2003b705cfSriastradh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2103b705cfSriastradh * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2203b705cfSriastradh * SOFTWARE.
2303b705cfSriastradh *
2403b705cfSriastradh * Authors:
2503b705cfSriastradh *    Wang Zhenyu <zhenyu.z.wang@sna.com>
2603b705cfSriastradh *    Eric Anholt <eric@anholt.net>
2703b705cfSriastradh *    Carl Worth <cworth@redhat.com>
2803b705cfSriastradh *    Keith Packard <keithp@keithp.com>
2903b705cfSriastradh *    Chris Wilson <chris@chris-wilson.co.uk>
3003b705cfSriastradh *
3103b705cfSriastradh */
3203b705cfSriastradh
3303b705cfSriastradh#ifdef HAVE_CONFIG_H
3403b705cfSriastradh#include "config.h"
3503b705cfSriastradh#endif
3603b705cfSriastradh
3703b705cfSriastradh#include "sna.h"
3803b705cfSriastradh#include "sna_reg.h"
3903b705cfSriastradh#include "sna_render.h"
4003b705cfSriastradh#include "sna_render_inline.h"
4103b705cfSriastradh#include "sna_video.h"
4203b705cfSriastradh
4303b705cfSriastradh#include "brw/brw.h"
4403b705cfSriastradh#include "gen6_render.h"
4503b705cfSriastradh#include "gen4_source.h"
4603b705cfSriastradh#include "gen4_vertex.h"
4703b705cfSriastradh
4803b705cfSriastradh#define NO_COMPOSITE 0
4903b705cfSriastradh#define NO_COMPOSITE_SPANS 0
5003b705cfSriastradh#define NO_COPY 0
5103b705cfSriastradh#define NO_COPY_BOXES 0
5203b705cfSriastradh#define NO_FILL 0
5303b705cfSriastradh#define NO_FILL_BOXES 0
5403b705cfSriastradh#define NO_FILL_ONE 0
5503b705cfSriastradh#define NO_FILL_CLEAR 0
5603b705cfSriastradh
5703b705cfSriastradh#define NO_RING_SWITCH 0
5803b705cfSriastradh#define PREFER_RENDER 0
5903b705cfSriastradh
6003b705cfSriastradh#define USE_8_PIXEL_DISPATCH 1
6103b705cfSriastradh#define USE_16_PIXEL_DISPATCH 1
6203b705cfSriastradh#define USE_32_PIXEL_DISPATCH 0
6303b705cfSriastradh
6403b705cfSriastradh#if !USE_8_PIXEL_DISPATCH && !USE_16_PIXEL_DISPATCH && !USE_32_PIXEL_DISPATCH
6503b705cfSriastradh#error "Must select at least 8, 16 or 32 pixel dispatch"
6603b705cfSriastradh#endif
6703b705cfSriastradh
6803b705cfSriastradh#define GEN6_MAX_SIZE 8192
6903b705cfSriastradh
7003b705cfSriastradhstruct gt_info {
7103b705cfSriastradh	const char *name;
7203b705cfSriastradh	int max_vs_threads;
7303b705cfSriastradh	int max_gs_threads;
7403b705cfSriastradh	int max_wm_threads;
7503b705cfSriastradh	struct {
7603b705cfSriastradh		int size;
7703b705cfSriastradh		int max_vs_entries;
7803b705cfSriastradh		int max_gs_entries;
7903b705cfSriastradh	} urb;
8003b705cfSriastradh};
8103b705cfSriastradh
8203b705cfSriastradhstatic const struct gt_info gt1_info = {
8303b705cfSriastradh	.name = "Sandybridge (gen6, gt1)",
8403b705cfSriastradh	.max_vs_threads = 24,
8503b705cfSriastradh	.max_gs_threads = 21,
8603b705cfSriastradh	.max_wm_threads = 40,
8703b705cfSriastradh	.urb = { 32, 256, 256 },
8803b705cfSriastradh};
8903b705cfSriastradh
9003b705cfSriastradhstatic const struct gt_info gt2_info = {
9103b705cfSriastradh	.name = "Sandybridge (gen6, gt2)",
9203b705cfSriastradh	.max_vs_threads = 60,
9303b705cfSriastradh	.max_gs_threads = 60,
9403b705cfSriastradh	.max_wm_threads = 80,
9503b705cfSriastradh	.urb = { 64, 256, 256 },
9603b705cfSriastradh};
9703b705cfSriastradh
9803b705cfSriastradhstatic const uint32_t ps_kernel_packed[][4] = {
9903b705cfSriastradh#include "exa_wm_src_affine.g6b"
10003b705cfSriastradh#include "exa_wm_src_sample_argb.g6b"
10103b705cfSriastradh#include "exa_wm_yuv_rgb.g6b"
10203b705cfSriastradh#include "exa_wm_write.g6b"
10303b705cfSriastradh};
10403b705cfSriastradh
10503b705cfSriastradhstatic const uint32_t ps_kernel_planar[][4] = {
10603b705cfSriastradh#include "exa_wm_src_affine.g6b"
10703b705cfSriastradh#include "exa_wm_src_sample_planar.g6b"
10803b705cfSriastradh#include "exa_wm_yuv_rgb.g6b"
10903b705cfSriastradh#include "exa_wm_write.g6b"
11003b705cfSriastradh};
11103b705cfSriastradh
11203b705cfSriastradh#define NOKERNEL(kernel_enum, func, ns) \
11303b705cfSriastradh    [GEN6_WM_KERNEL_##kernel_enum] = {#kernel_enum, func, 0, ns}
11403b705cfSriastradh#define KERNEL(kernel_enum, kernel, ns) \
11503b705cfSriastradh    [GEN6_WM_KERNEL_##kernel_enum] = {#kernel_enum, kernel, sizeof(kernel), ns}
11603b705cfSriastradh
11703b705cfSriastradhstatic const struct wm_kernel_info {
11803b705cfSriastradh	const char *name;
11903b705cfSriastradh	const void *data;
12003b705cfSriastradh	unsigned int size;
12103b705cfSriastradh	unsigned int num_surfaces;
12203b705cfSriastradh} wm_kernels[] = {
12303b705cfSriastradh	NOKERNEL(NOMASK, brw_wm_kernel__affine, 2),
12403b705cfSriastradh	NOKERNEL(NOMASK_P, brw_wm_kernel__projective, 2),
12503b705cfSriastradh
12603b705cfSriastradh	NOKERNEL(MASK, brw_wm_kernel__affine_mask, 3),
12703b705cfSriastradh	NOKERNEL(MASK_P, brw_wm_kernel__projective_mask, 3),
12803b705cfSriastradh
12903b705cfSriastradh	NOKERNEL(MASKCA, brw_wm_kernel__affine_mask_ca, 3),
13003b705cfSriastradh	NOKERNEL(MASKCA_P, brw_wm_kernel__projective_mask_ca, 3),
13103b705cfSriastradh
13203b705cfSriastradh	NOKERNEL(MASKSA, brw_wm_kernel__affine_mask_sa, 3),
13303b705cfSriastradh	NOKERNEL(MASKSA_P, brw_wm_kernel__projective_mask_sa, 3),
13403b705cfSriastradh
13503b705cfSriastradh	NOKERNEL(OPACITY, brw_wm_kernel__affine_opacity, 2),
13603b705cfSriastradh	NOKERNEL(OPACITY_P, brw_wm_kernel__projective_opacity, 2),
13703b705cfSriastradh
13803b705cfSriastradh	KERNEL(VIDEO_PLANAR, ps_kernel_planar, 7),
13903b705cfSriastradh	KERNEL(VIDEO_PACKED, ps_kernel_packed, 2),
14003b705cfSriastradh};
14103b705cfSriastradh#undef KERNEL
14203b705cfSriastradh
14303b705cfSriastradhstatic const struct blendinfo {
14403b705cfSriastradh	bool src_alpha;
14503b705cfSriastradh	uint32_t src_blend;
14603b705cfSriastradh	uint32_t dst_blend;
14703b705cfSriastradh} gen6_blend_op[] = {
14803b705cfSriastradh	/* Clear */	{0, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ZERO},
14903b705cfSriastradh	/* Src */	{0, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ZERO},
15003b705cfSriastradh	/* Dst */	{0, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ONE},
15103b705cfSriastradh	/* Over */	{1, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
15203b705cfSriastradh	/* OverReverse */ {0, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_ONE},
15303b705cfSriastradh	/* In */	{0, GEN6_BLENDFACTOR_DST_ALPHA, GEN6_BLENDFACTOR_ZERO},
15403b705cfSriastradh	/* InReverse */	{1, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_SRC_ALPHA},
15503b705cfSriastradh	/* Out */	{0, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_ZERO},
15603b705cfSriastradh	/* OutReverse */ {1, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
15703b705cfSriastradh	/* Atop */	{1, GEN6_BLENDFACTOR_DST_ALPHA, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
15803b705cfSriastradh	/* AtopReverse */ {1, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_SRC_ALPHA},
15903b705cfSriastradh	/* Xor */	{1, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
16003b705cfSriastradh	/* Add */	{0, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ONE},
16103b705cfSriastradh};
16203b705cfSriastradh
16303b705cfSriastradh/**
16403b705cfSriastradh * Highest-valued BLENDFACTOR used in gen6_blend_op.
16503b705cfSriastradh *
16603b705cfSriastradh * This leaves out GEN6_BLENDFACTOR_INV_DST_COLOR,
16703b705cfSriastradh * GEN6_BLENDFACTOR_INV_CONST_{COLOR,ALPHA},
16803b705cfSriastradh * GEN6_BLENDFACTOR_INV_SRC1_{COLOR,ALPHA}
16903b705cfSriastradh */
17003b705cfSriastradh#define GEN6_BLENDFACTOR_COUNT (GEN6_BLENDFACTOR_INV_DST_ALPHA + 1)
17103b705cfSriastradh
17203b705cfSriastradh#define GEN6_BLEND_STATE_PADDED_SIZE	ALIGN(sizeof(struct gen6_blend_state), 64)
17303b705cfSriastradh
17403b705cfSriastradh#define BLEND_OFFSET(s, d) \
17503b705cfSriastradh	(((s) * GEN6_BLENDFACTOR_COUNT + (d)) * GEN6_BLEND_STATE_PADDED_SIZE)
17603b705cfSriastradh
17703b705cfSriastradh#define NO_BLEND BLEND_OFFSET(GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ZERO)
17803b705cfSriastradh#define CLEAR BLEND_OFFSET(GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ZERO)
17903b705cfSriastradh
18003b705cfSriastradh#define SAMPLER_OFFSET(sf, se, mf, me) \
18103b705cfSriastradh	(((((sf) * EXTEND_COUNT + (se)) * FILTER_COUNT + (mf)) * EXTEND_COUNT + (me) + 2) * 2 * sizeof(struct gen6_sampler_state))
18203b705cfSriastradh
18303b705cfSriastradh#define VERTEX_2s2s 0
18403b705cfSriastradh
18503b705cfSriastradh#define COPY_SAMPLER 0
18603b705cfSriastradh#define COPY_VERTEX VERTEX_2s2s
18703b705cfSriastradh#define COPY_FLAGS(a) GEN6_SET_FLAGS(COPY_SAMPLER, (a) == GXcopy ? NO_BLEND : CLEAR, GEN6_WM_KERNEL_NOMASK, COPY_VERTEX)
18803b705cfSriastradh
18903b705cfSriastradh#define FILL_SAMPLER (2 * sizeof(struct gen6_sampler_state))
19003b705cfSriastradh#define FILL_VERTEX VERTEX_2s2s
19103b705cfSriastradh#define FILL_FLAGS(op, format) GEN6_SET_FLAGS(FILL_SAMPLER, gen6_get_blend((op), false, (format)), GEN6_WM_KERNEL_NOMASK, FILL_VERTEX)
19203b705cfSriastradh#define FILL_FLAGS_NOBLEND GEN6_SET_FLAGS(FILL_SAMPLER, NO_BLEND, GEN6_WM_KERNEL_NOMASK, FILL_VERTEX)
19303b705cfSriastradh
19403b705cfSriastradh#define GEN6_SAMPLER(f) (((f) >> 16) & 0xfff0)
19503b705cfSriastradh#define GEN6_BLEND(f) (((f) >> 0) & 0xfff0)
19603b705cfSriastradh#define GEN6_KERNEL(f) (((f) >> 16) & 0xf)
19703b705cfSriastradh#define GEN6_VERTEX(f) (((f) >> 0) & 0xf)
19803b705cfSriastradh#define GEN6_SET_FLAGS(S, B, K, V)  (((S) | (K)) << 16 | ((B) | (V)))
19903b705cfSriastradh
20003b705cfSriastradh#define OUT_BATCH(v) batch_emit(sna, v)
20103b705cfSriastradh#define OUT_VERTEX(x,y) vertex_emit_2s(sna, x,y)
20203b705cfSriastradh#define OUT_VERTEX_F(v) vertex_emit(sna, v)
20303b705cfSriastradh
20403b705cfSriastradhstatic inline bool too_large(int width, int height)
20503b705cfSriastradh{
20603b705cfSriastradh	return width > GEN6_MAX_SIZE || height > GEN6_MAX_SIZE;
20703b705cfSriastradh}
20803b705cfSriastradh
20903b705cfSriastradhstatic uint32_t gen6_get_blend(int op,
21003b705cfSriastradh			       bool has_component_alpha,
21103b705cfSriastradh			       uint32_t dst_format)
21203b705cfSriastradh{
21303b705cfSriastradh	uint32_t src, dst;
21403b705cfSriastradh
21503b705cfSriastradh	src = gen6_blend_op[op].src_blend;
21603b705cfSriastradh	dst = gen6_blend_op[op].dst_blend;
21703b705cfSriastradh
21803b705cfSriastradh	/* If there's no dst alpha channel, adjust the blend op so that
21903b705cfSriastradh	 * we'll treat it always as 1.
22003b705cfSriastradh	 */
22103b705cfSriastradh	if (PICT_FORMAT_A(dst_format) == 0) {
22203b705cfSriastradh		if (src == GEN6_BLENDFACTOR_DST_ALPHA)
22303b705cfSriastradh			src = GEN6_BLENDFACTOR_ONE;
22403b705cfSriastradh		else if (src == GEN6_BLENDFACTOR_INV_DST_ALPHA)
22503b705cfSriastradh			src = GEN6_BLENDFACTOR_ZERO;
22603b705cfSriastradh	}
22703b705cfSriastradh
22803b705cfSriastradh	/* If the source alpha is being used, then we should only be in a
22903b705cfSriastradh	 * case where the source blend factor is 0, and the source blend
23003b705cfSriastradh	 * value is the mask channels multiplied by the source picture's alpha.
23103b705cfSriastradh	 */
23203b705cfSriastradh	if (has_component_alpha && gen6_blend_op[op].src_alpha) {
23303b705cfSriastradh		if (dst == GEN6_BLENDFACTOR_SRC_ALPHA)
23403b705cfSriastradh			dst = GEN6_BLENDFACTOR_SRC_COLOR;
23503b705cfSriastradh		else if (dst == GEN6_BLENDFACTOR_INV_SRC_ALPHA)
23603b705cfSriastradh			dst = GEN6_BLENDFACTOR_INV_SRC_COLOR;
23703b705cfSriastradh	}
23803b705cfSriastradh
23903b705cfSriastradh	DBG(("blend op=%d, dst=%x [A=%d] => src=%d, dst=%d => offset=%x\n",
24003b705cfSriastradh	     op, dst_format, PICT_FORMAT_A(dst_format),
24103b705cfSriastradh	     src, dst, (int)BLEND_OFFSET(src, dst)));
24203b705cfSriastradh	return BLEND_OFFSET(src, dst);
24303b705cfSriastradh}
24403b705cfSriastradh
24503b705cfSriastradhstatic uint32_t gen6_get_card_format(PictFormat format)
24603b705cfSriastradh{
24703b705cfSriastradh	switch (format) {
24803b705cfSriastradh	default:
24903b705cfSriastradh		return -1;
25003b705cfSriastradh	case PICT_a8r8g8b8:
25103b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8A8_UNORM;
25203b705cfSriastradh	case PICT_x8r8g8b8:
25303b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8X8_UNORM;
25403b705cfSriastradh	case PICT_a8b8g8r8:
25503b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8A8_UNORM;
25603b705cfSriastradh	case PICT_x8b8g8r8:
25703b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8X8_UNORM;
25803b705cfSriastradh	case PICT_a2r10g10b10:
25903b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10A2_UNORM;
26003b705cfSriastradh	case PICT_x2r10g10b10:
26103b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10X2_UNORM;
26203b705cfSriastradh	case PICT_r8g8b8:
26303b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8_UNORM;
26403b705cfSriastradh	case PICT_r5g6b5:
26503b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G6R5_UNORM;
26603b705cfSriastradh	case PICT_a1r5g5b5:
26703b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G5R5A1_UNORM;
26803b705cfSriastradh	case PICT_a8:
26903b705cfSriastradh		return GEN6_SURFACEFORMAT_A8_UNORM;
27003b705cfSriastradh	case PICT_a4r4g4b4:
27103b705cfSriastradh		return GEN6_SURFACEFORMAT_B4G4R4A4_UNORM;
27203b705cfSriastradh	}
27303b705cfSriastradh}
27403b705cfSriastradh
27503b705cfSriastradhstatic uint32_t gen6_get_dest_format(PictFormat format)
27603b705cfSriastradh{
27703b705cfSriastradh	switch (format) {
27803b705cfSriastradh	default:
27903b705cfSriastradh		return -1;
28003b705cfSriastradh	case PICT_a8r8g8b8:
28103b705cfSriastradh	case PICT_x8r8g8b8:
28203b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8A8_UNORM;
28303b705cfSriastradh	case PICT_a8b8g8r8:
28403b705cfSriastradh	case PICT_x8b8g8r8:
28503b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8A8_UNORM;
28603b705cfSriastradh	case PICT_a2r10g10b10:
28703b705cfSriastradh	case PICT_x2r10g10b10:
28803b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10A2_UNORM;
28903b705cfSriastradh	case PICT_r5g6b5:
29003b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G6R5_UNORM;
29103b705cfSriastradh	case PICT_x1r5g5b5:
29203b705cfSriastradh	case PICT_a1r5g5b5:
29303b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G5R5A1_UNORM;
29403b705cfSriastradh	case PICT_a8:
29503b705cfSriastradh		return GEN6_SURFACEFORMAT_A8_UNORM;
29603b705cfSriastradh	case PICT_a4r4g4b4:
29703b705cfSriastradh	case PICT_x4r4g4b4:
29803b705cfSriastradh		return GEN6_SURFACEFORMAT_B4G4R4A4_UNORM;
29903b705cfSriastradh	}
30003b705cfSriastradh}
30103b705cfSriastradh
30203b705cfSriastradhstatic bool gen6_check_dst_format(PictFormat format)
30303b705cfSriastradh{
30403b705cfSriastradh	if (gen6_get_dest_format(format) != -1)
30503b705cfSriastradh		return true;
30603b705cfSriastradh
30703b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
30803b705cfSriastradh	return false;
30903b705cfSriastradh}
31003b705cfSriastradh
31103b705cfSriastradhstatic bool gen6_check_format(uint32_t format)
31203b705cfSriastradh{
31303b705cfSriastradh	if (gen6_get_card_format(format) != -1)
31403b705cfSriastradh		return true;
31503b705cfSriastradh
31603b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
31703b705cfSriastradh	return false;
31803b705cfSriastradh}
31903b705cfSriastradh
32003b705cfSriastradhstatic uint32_t gen6_filter(uint32_t filter)
32103b705cfSriastradh{
32203b705cfSriastradh	switch (filter) {
32303b705cfSriastradh	default:
32403b705cfSriastradh		assert(0);
32503b705cfSriastradh	case PictFilterNearest:
32603b705cfSriastradh		return SAMPLER_FILTER_NEAREST;
32703b705cfSriastradh	case PictFilterBilinear:
32803b705cfSriastradh		return SAMPLER_FILTER_BILINEAR;
32903b705cfSriastradh	}
33003b705cfSriastradh}
33103b705cfSriastradh
33203b705cfSriastradhstatic uint32_t gen6_check_filter(PicturePtr picture)
33303b705cfSriastradh{
33403b705cfSriastradh	switch (picture->filter) {
33503b705cfSriastradh	case PictFilterNearest:
33603b705cfSriastradh	case PictFilterBilinear:
33703b705cfSriastradh		return true;
33803b705cfSriastradh	default:
33903b705cfSriastradh		return false;
34003b705cfSriastradh	}
34103b705cfSriastradh}
34203b705cfSriastradh
34303b705cfSriastradhstatic uint32_t gen6_repeat(uint32_t repeat)
34403b705cfSriastradh{
34503b705cfSriastradh	switch (repeat) {
34603b705cfSriastradh	default:
34703b705cfSriastradh		assert(0);
34803b705cfSriastradh	case RepeatNone:
34903b705cfSriastradh		return SAMPLER_EXTEND_NONE;
35003b705cfSriastradh	case RepeatNormal:
35103b705cfSriastradh		return SAMPLER_EXTEND_REPEAT;
35203b705cfSriastradh	case RepeatPad:
35303b705cfSriastradh		return SAMPLER_EXTEND_PAD;
35403b705cfSriastradh	case RepeatReflect:
35503b705cfSriastradh		return SAMPLER_EXTEND_REFLECT;
35603b705cfSriastradh	}
35703b705cfSriastradh}
35803b705cfSriastradh
35903b705cfSriastradhstatic bool gen6_check_repeat(PicturePtr picture)
36003b705cfSriastradh{
36103b705cfSriastradh	if (!picture->repeat)
36203b705cfSriastradh		return true;
36303b705cfSriastradh
36403b705cfSriastradh	switch (picture->repeatType) {
36503b705cfSriastradh	case RepeatNone:
36603b705cfSriastradh	case RepeatNormal:
36703b705cfSriastradh	case RepeatPad:
36803b705cfSriastradh	case RepeatReflect:
36903b705cfSriastradh		return true;
37003b705cfSriastradh	default:
37103b705cfSriastradh		return false;
37203b705cfSriastradh	}
37303b705cfSriastradh}
37403b705cfSriastradh
37503b705cfSriastradhstatic int
37603b705cfSriastradhgen6_choose_composite_kernel(int op, bool has_mask, bool is_ca, bool is_affine)
37703b705cfSriastradh{
37803b705cfSriastradh	int base;
37903b705cfSriastradh
38003b705cfSriastradh	if (has_mask) {
38103b705cfSriastradh		if (is_ca) {
38203b705cfSriastradh			if (gen6_blend_op[op].src_alpha)
38303b705cfSriastradh				base = GEN6_WM_KERNEL_MASKSA;
38403b705cfSriastradh			else
38503b705cfSriastradh				base = GEN6_WM_KERNEL_MASKCA;
38603b705cfSriastradh		} else
38703b705cfSriastradh			base = GEN6_WM_KERNEL_MASK;
38803b705cfSriastradh	} else
38903b705cfSriastradh		base = GEN6_WM_KERNEL_NOMASK;
39003b705cfSriastradh
39103b705cfSriastradh	return base + !is_affine;
39203b705cfSriastradh}
39303b705cfSriastradh
39403b705cfSriastradhstatic void
39503b705cfSriastradhgen6_emit_urb(struct sna *sna)
39603b705cfSriastradh{
39703b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_URB | (3 - 2));
39803b705cfSriastradh	OUT_BATCH(((1 - 1) << GEN6_3DSTATE_URB_VS_SIZE_SHIFT) |
39903b705cfSriastradh		  (sna->render_state.gen6.info->urb.max_vs_entries << GEN6_3DSTATE_URB_VS_ENTRIES_SHIFT)); /* at least 24 on GEN6 */
40003b705cfSriastradh	OUT_BATCH((0 << GEN6_3DSTATE_URB_GS_SIZE_SHIFT) |
40103b705cfSriastradh		  (0 << GEN6_3DSTATE_URB_GS_ENTRIES_SHIFT)); /* no GS thread */
40203b705cfSriastradh}
40303b705cfSriastradh
40403b705cfSriastradhstatic void
40503b705cfSriastradhgen6_emit_state_base_address(struct sna *sna)
40603b705cfSriastradh{
40703b705cfSriastradh	OUT_BATCH(GEN6_STATE_BASE_ADDRESS | (10 - 2));
40803b705cfSriastradh	OUT_BATCH(0); /* general */
40903b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* surface */
41003b705cfSriastradh				 sna->kgem.nbatch,
41103b705cfSriastradh				 NULL,
41203b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
41303b705cfSriastradh				 BASE_ADDRESS_MODIFY));
41403b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* instruction */
41503b705cfSriastradh				 sna->kgem.nbatch,
41603b705cfSriastradh				 sna->render_state.gen6.general_bo,
41703b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
41803b705cfSriastradh				 BASE_ADDRESS_MODIFY));
41903b705cfSriastradh	OUT_BATCH(0); /* indirect */
42003b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem,
42103b705cfSriastradh				 sna->kgem.nbatch,
42203b705cfSriastradh				 sna->render_state.gen6.general_bo,
42303b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
42403b705cfSriastradh				 BASE_ADDRESS_MODIFY));
42503b705cfSriastradh
42603b705cfSriastradh	/* upper bounds, disable */
42703b705cfSriastradh	OUT_BATCH(0);
42803b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
42903b705cfSriastradh	OUT_BATCH(0);
43003b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
43103b705cfSriastradh}
43203b705cfSriastradh
43303b705cfSriastradhstatic void
43403b705cfSriastradhgen6_emit_viewports(struct sna *sna)
43503b705cfSriastradh{
43603b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VIEWPORT_STATE_POINTERS |
43703b705cfSriastradh		  GEN6_3DSTATE_VIEWPORT_STATE_MODIFY_CC |
43803b705cfSriastradh		  (4 - 2));
43903b705cfSriastradh	OUT_BATCH(0);
44003b705cfSriastradh	OUT_BATCH(0);
44103b705cfSriastradh	OUT_BATCH(0);
44203b705cfSriastradh}
44303b705cfSriastradh
44403b705cfSriastradhstatic void
44503b705cfSriastradhgen6_emit_vs(struct sna *sna)
44603b705cfSriastradh{
44703b705cfSriastradh	/* disable VS constant buffer */
44803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_VS | (5 - 2));
44903b705cfSriastradh	OUT_BATCH(0);
45003b705cfSriastradh	OUT_BATCH(0);
45103b705cfSriastradh	OUT_BATCH(0);
45203b705cfSriastradh	OUT_BATCH(0);
45303b705cfSriastradh
45403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VS | (6 - 2));
45503b705cfSriastradh	OUT_BATCH(0); /* no VS kernel */
45603b705cfSriastradh	OUT_BATCH(0);
45703b705cfSriastradh	OUT_BATCH(0);
45803b705cfSriastradh	OUT_BATCH(0);
45903b705cfSriastradh	OUT_BATCH(0); /* pass-through */
46003b705cfSriastradh}
46103b705cfSriastradh
46203b705cfSriastradhstatic void
46303b705cfSriastradhgen6_emit_gs(struct sna *sna)
46403b705cfSriastradh{
46503b705cfSriastradh	/* disable GS constant buffer */
46603b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_GS | (5 - 2));
46703b705cfSriastradh	OUT_BATCH(0);
46803b705cfSriastradh	OUT_BATCH(0);
46903b705cfSriastradh	OUT_BATCH(0);
47003b705cfSriastradh	OUT_BATCH(0);
47103b705cfSriastradh
47203b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_GS | (7 - 2));
47303b705cfSriastradh	OUT_BATCH(0); /* no GS kernel */
47403b705cfSriastradh	OUT_BATCH(0);
47503b705cfSriastradh	OUT_BATCH(0);
47603b705cfSriastradh	OUT_BATCH(0);
47703b705cfSriastradh	OUT_BATCH(0);
47803b705cfSriastradh	OUT_BATCH(0); /* pass-through */
47903b705cfSriastradh}
48003b705cfSriastradh
48103b705cfSriastradhstatic void
48203b705cfSriastradhgen6_emit_clip(struct sna *sna)
48303b705cfSriastradh{
48403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CLIP | (4 - 2));
48503b705cfSriastradh	OUT_BATCH(0);
48603b705cfSriastradh	OUT_BATCH(0); /* pass-through */
48703b705cfSriastradh	OUT_BATCH(0);
48803b705cfSriastradh}
48903b705cfSriastradh
49003b705cfSriastradhstatic void
49103b705cfSriastradhgen6_emit_wm_constants(struct sna *sna)
49203b705cfSriastradh{
49303b705cfSriastradh	/* disable WM constant buffer */
49403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_PS | (5 - 2));
49503b705cfSriastradh	OUT_BATCH(0);
49603b705cfSriastradh	OUT_BATCH(0);
49703b705cfSriastradh	OUT_BATCH(0);
49803b705cfSriastradh	OUT_BATCH(0);
49903b705cfSriastradh}
50003b705cfSriastradh
50103b705cfSriastradhstatic void
50203b705cfSriastradhgen6_emit_null_depth_buffer(struct sna *sna)
50303b705cfSriastradh{
50403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_DEPTH_BUFFER | (7 - 2));
50503b705cfSriastradh	OUT_BATCH(GEN6_SURFACE_NULL << GEN6_3DSTATE_DEPTH_BUFFER_TYPE_SHIFT |
50603b705cfSriastradh		  GEN6_DEPTHFORMAT_D32_FLOAT << GEN6_3DSTATE_DEPTH_BUFFER_FORMAT_SHIFT);
50703b705cfSriastradh	OUT_BATCH(0);
50803b705cfSriastradh	OUT_BATCH(0);
50903b705cfSriastradh	OUT_BATCH(0);
51003b705cfSriastradh	OUT_BATCH(0);
51103b705cfSriastradh	OUT_BATCH(0);
51203b705cfSriastradh
51303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CLEAR_PARAMS | (2 - 2));
51403b705cfSriastradh	OUT_BATCH(0);
51503b705cfSriastradh}
51603b705cfSriastradh
51703b705cfSriastradhstatic void
51803b705cfSriastradhgen6_emit_invariant(struct sna *sna)
51903b705cfSriastradh{
52003b705cfSriastradh	OUT_BATCH(GEN6_PIPELINE_SELECT | PIPELINE_SELECT_3D);
52103b705cfSriastradh
52203b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_MULTISAMPLE | (3 - 2));
52303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_MULTISAMPLE_PIXEL_LOCATION_CENTER |
52403b705cfSriastradh		  GEN6_3DSTATE_MULTISAMPLE_NUMSAMPLES_1); /* 1 sample/pixel */
52503b705cfSriastradh	OUT_BATCH(0);
52603b705cfSriastradh
52703b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SAMPLE_MASK | (2 - 2));
52803b705cfSriastradh	OUT_BATCH(1);
52903b705cfSriastradh
53003b705cfSriastradh	gen6_emit_urb(sna);
53103b705cfSriastradh
53203b705cfSriastradh	gen6_emit_state_base_address(sna);
53303b705cfSriastradh
53403b705cfSriastradh	gen6_emit_viewports(sna);
53503b705cfSriastradh	gen6_emit_vs(sna);
53603b705cfSriastradh	gen6_emit_gs(sna);
53703b705cfSriastradh	gen6_emit_clip(sna);
53803b705cfSriastradh	gen6_emit_wm_constants(sna);
53903b705cfSriastradh	gen6_emit_null_depth_buffer(sna);
54003b705cfSriastradh
54103b705cfSriastradh	sna->render_state.gen6.needs_invariant = false;
54203b705cfSriastradh}
54303b705cfSriastradh
54403b705cfSriastradhstatic bool
54503b705cfSriastradhgen6_emit_cc(struct sna *sna, int blend)
54603b705cfSriastradh{
54703b705cfSriastradh	struct gen6_render_state *render = &sna->render_state.gen6;
54803b705cfSriastradh
54903b705cfSriastradh	if (render->blend == blend)
55003b705cfSriastradh		return blend != NO_BLEND;
55103b705cfSriastradh
55203b705cfSriastradh	DBG(("%s: blend = %x\n", __FUNCTION__, blend));
55303b705cfSriastradh
55403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CC_STATE_POINTERS | (4 - 2));
55503b705cfSriastradh	OUT_BATCH((render->cc_blend + blend) | 1);
55603b705cfSriastradh	if (render->blend == (unsigned)-1) {
55703b705cfSriastradh		OUT_BATCH(1);
55803b705cfSriastradh		OUT_BATCH(1);
55903b705cfSriastradh	} else {
56003b705cfSriastradh		OUT_BATCH(0);
56103b705cfSriastradh		OUT_BATCH(0);
56203b705cfSriastradh	}
56303b705cfSriastradh
56403b705cfSriastradh	render->blend = blend;
56503b705cfSriastradh	return blend != NO_BLEND;
56603b705cfSriastradh}
56703b705cfSriastradh
56803b705cfSriastradhstatic void
56903b705cfSriastradhgen6_emit_sampler(struct sna *sna, uint32_t state)
57003b705cfSriastradh{
57103b705cfSriastradh	if (sna->render_state.gen6.samplers == state)
57203b705cfSriastradh		return;
57303b705cfSriastradh
57403b705cfSriastradh	sna->render_state.gen6.samplers = state;
57503b705cfSriastradh
57603b705cfSriastradh	DBG(("%s: sampler = %x\n", __FUNCTION__, state));
57703b705cfSriastradh
57803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SAMPLER_STATE_POINTERS |
57903b705cfSriastradh		  GEN6_3DSTATE_SAMPLER_STATE_MODIFY_PS |
58003b705cfSriastradh		  (4 - 2));
58103b705cfSriastradh	OUT_BATCH(0); /* VS */
58203b705cfSriastradh	OUT_BATCH(0); /* GS */
58303b705cfSriastradh	OUT_BATCH(sna->render_state.gen6.wm_state + state);
58403b705cfSriastradh}
58503b705cfSriastradh
58603b705cfSriastradhstatic void
58703b705cfSriastradhgen6_emit_sf(struct sna *sna, bool has_mask)
58803b705cfSriastradh{
58903b705cfSriastradh	int num_sf_outputs = has_mask ? 2 : 1;
59003b705cfSriastradh
59103b705cfSriastradh	if (sna->render_state.gen6.num_sf_outputs == num_sf_outputs)
59203b705cfSriastradh		return;
59303b705cfSriastradh
59403b705cfSriastradh	DBG(("%s: num_sf_outputs=%d, read_length=%d, read_offset=%d\n",
59503b705cfSriastradh	     __FUNCTION__, num_sf_outputs, 1, 0));
59603b705cfSriastradh
59703b705cfSriastradh	sna->render_state.gen6.num_sf_outputs = num_sf_outputs;
59803b705cfSriastradh
59903b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SF | (20 - 2));
60003b705cfSriastradh	OUT_BATCH(num_sf_outputs << GEN6_3DSTATE_SF_NUM_OUTPUTS_SHIFT |
60103b705cfSriastradh		  1 << GEN6_3DSTATE_SF_URB_ENTRY_READ_LENGTH_SHIFT |
60203b705cfSriastradh		  1 << GEN6_3DSTATE_SF_URB_ENTRY_READ_OFFSET_SHIFT);
60303b705cfSriastradh	OUT_BATCH(0);
60403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SF_CULL_NONE);
60503b705cfSriastradh	OUT_BATCH(2 << GEN6_3DSTATE_SF_TRIFAN_PROVOKE_SHIFT); /* DW4 */
60603b705cfSriastradh	OUT_BATCH(0);
60703b705cfSriastradh	OUT_BATCH(0);
60803b705cfSriastradh	OUT_BATCH(0);
60903b705cfSriastradh	OUT_BATCH(0);
61003b705cfSriastradh	OUT_BATCH(0); /* DW9 */
61103b705cfSriastradh	OUT_BATCH(0);
61203b705cfSriastradh	OUT_BATCH(0);
61303b705cfSriastradh	OUT_BATCH(0);
61403b705cfSriastradh	OUT_BATCH(0);
61503b705cfSriastradh	OUT_BATCH(0); /* DW14 */
61603b705cfSriastradh	OUT_BATCH(0);
61703b705cfSriastradh	OUT_BATCH(0);
61803b705cfSriastradh	OUT_BATCH(0);
61903b705cfSriastradh	OUT_BATCH(0);
62003b705cfSriastradh	OUT_BATCH(0); /* DW19 */
62103b705cfSriastradh}
62203b705cfSriastradh
62303b705cfSriastradhstatic void
62403b705cfSriastradhgen6_emit_wm(struct sna *sna, unsigned int kernel, bool has_mask)
62503b705cfSriastradh{
62603b705cfSriastradh	const uint32_t *kernels;
62703b705cfSriastradh
62803b705cfSriastradh	if (sna->render_state.gen6.kernel == kernel)
62903b705cfSriastradh		return;
63003b705cfSriastradh
63103b705cfSriastradh	sna->render_state.gen6.kernel = kernel;
63203b705cfSriastradh	kernels = sna->render_state.gen6.wm_kernel[kernel];
63303b705cfSriastradh
63403b705cfSriastradh	DBG(("%s: switching to %s, num_surfaces=%d (8-pixel? %d, 16-pixel? %d,32-pixel? %d)\n",
63503b705cfSriastradh	     __FUNCTION__,
63603b705cfSriastradh	     wm_kernels[kernel].name, wm_kernels[kernel].num_surfaces,
63703b705cfSriastradh	    kernels[0], kernels[1], kernels[2]));
63803b705cfSriastradh
63903b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_WM | (9 - 2));
64003b705cfSriastradh	OUT_BATCH(kernels[0] ?: kernels[1] ?: kernels[2]);
64103b705cfSriastradh	OUT_BATCH(1 << GEN6_3DSTATE_WM_SAMPLER_COUNT_SHIFT |
64203b705cfSriastradh		  wm_kernels[kernel].num_surfaces << GEN6_3DSTATE_WM_BINDING_TABLE_ENTRY_COUNT_SHIFT);
64303b705cfSriastradh	OUT_BATCH(0); /* scratch space */
64403b705cfSriastradh	OUT_BATCH((kernels[0] ? 4 : kernels[1] ? 6 : 8) << GEN6_3DSTATE_WM_DISPATCH_0_START_GRF_SHIFT |
64503b705cfSriastradh		  8 << GEN6_3DSTATE_WM_DISPATCH_1_START_GRF_SHIFT |
64603b705cfSriastradh		  6 << GEN6_3DSTATE_WM_DISPATCH_2_START_GRF_SHIFT);
64703b705cfSriastradh	OUT_BATCH((sna->render_state.gen6.info->max_wm_threads - 1) << GEN6_3DSTATE_WM_MAX_THREADS_SHIFT |
64803b705cfSriastradh		  (kernels[0] ? GEN6_3DSTATE_WM_8_DISPATCH_ENABLE : 0) |
64903b705cfSriastradh		  (kernels[1] ? GEN6_3DSTATE_WM_16_DISPATCH_ENABLE : 0) |
65003b705cfSriastradh		  (kernels[2] ? GEN6_3DSTATE_WM_32_DISPATCH_ENABLE : 0) |
65103b705cfSriastradh		  GEN6_3DSTATE_WM_DISPATCH_ENABLE);
65203b705cfSriastradh	OUT_BATCH((1 + has_mask) << GEN6_3DSTATE_WM_NUM_SF_OUTPUTS_SHIFT |
65303b705cfSriastradh		  GEN6_3DSTATE_WM_PERSPECTIVE_PIXEL_BARYCENTRIC);
65403b705cfSriastradh	OUT_BATCH(kernels[2]);
65503b705cfSriastradh	OUT_BATCH(kernels[1]);
65603b705cfSriastradh}
65703b705cfSriastradh
65803b705cfSriastradhstatic bool
65903b705cfSriastradhgen6_emit_binding_table(struct sna *sna, uint16_t offset)
66003b705cfSriastradh{
66103b705cfSriastradh	if (sna->render_state.gen6.surface_table == offset)
66203b705cfSriastradh		return false;
66303b705cfSriastradh
66403b705cfSriastradh	/* Binding table pointers */
66503b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_BINDING_TABLE_POINTERS |
66603b705cfSriastradh		  GEN6_3DSTATE_BINDING_TABLE_MODIFY_PS |
66703b705cfSriastradh		  (4 - 2));
66803b705cfSriastradh	OUT_BATCH(0);		/* vs */
66903b705cfSriastradh	OUT_BATCH(0);		/* gs */
67003b705cfSriastradh	/* Only the PS uses the binding table */
67103b705cfSriastradh	OUT_BATCH(offset*4);
67203b705cfSriastradh
67303b705cfSriastradh	sna->render_state.gen6.surface_table = offset;
67403b705cfSriastradh	return true;
67503b705cfSriastradh}
67603b705cfSriastradh
67703b705cfSriastradhstatic bool
67803b705cfSriastradhgen6_emit_drawing_rectangle(struct sna *sna,
67903b705cfSriastradh			    const struct sna_composite_op *op)
68003b705cfSriastradh{
68103b705cfSriastradh	uint32_t limit = (op->dst.height - 1) << 16 | (op->dst.width - 1);
68203b705cfSriastradh	uint32_t offset = (uint16_t)op->dst.y << 16 | (uint16_t)op->dst.x;
68303b705cfSriastradh
68403b705cfSriastradh	assert(!too_large(op->dst.x, op->dst.y));
68503b705cfSriastradh	assert(!too_large(op->dst.width, op->dst.height));
68603b705cfSriastradh
68703b705cfSriastradh	if (sna->render_state.gen6.drawrect_limit  == limit &&
68803b705cfSriastradh	    sna->render_state.gen6.drawrect_offset == offset)
68903b705cfSriastradh		return false;
69003b705cfSriastradh
69103b705cfSriastradh	/* [DevSNB-C+{W/A}] Before any depth stall flush (including those
69203b705cfSriastradh	 * produced by non-pipelined state commands), software needs to first
69303b705cfSriastradh	 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
69403b705cfSriastradh	 * 0.
69503b705cfSriastradh	 *
69603b705cfSriastradh	 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
69703b705cfSriastradh	 * BEFORE the pipe-control with a post-sync op and no write-cache
69803b705cfSriastradh	 * flushes.
69903b705cfSriastradh	 */
70003b705cfSriastradh	if (!sna->render_state.gen6.first_state_packet) {
70103b705cfSriastradh		OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
70203b705cfSriastradh		OUT_BATCH(GEN6_PIPE_CONTROL_CS_STALL |
70303b705cfSriastradh			  GEN6_PIPE_CONTROL_STALL_AT_SCOREBOARD);
70403b705cfSriastradh		OUT_BATCH(0);
70503b705cfSriastradh		OUT_BATCH(0);
70603b705cfSriastradh	}
70703b705cfSriastradh
70803b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
70903b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL_WRITE_TIME);
71003b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, sna->kgem.nbatch,
71103b705cfSriastradh				 sna->render_state.gen6.general_bo,
71203b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16 |
71303b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION,
71403b705cfSriastradh				 64));
71503b705cfSriastradh	OUT_BATCH(0);
71603b705cfSriastradh
71703b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
71803b705cfSriastradh	OUT_BATCH(0);
71903b705cfSriastradh	OUT_BATCH(limit);
72003b705cfSriastradh	OUT_BATCH(offset);
72103b705cfSriastradh
72203b705cfSriastradh	sna->render_state.gen6.drawrect_offset = offset;
72303b705cfSriastradh	sna->render_state.gen6.drawrect_limit = limit;
72403b705cfSriastradh	return true;
72503b705cfSriastradh}
72603b705cfSriastradh
72703b705cfSriastradhstatic void
72803b705cfSriastradhgen6_emit_vertex_elements(struct sna *sna,
72903b705cfSriastradh			  const struct sna_composite_op *op)
73003b705cfSriastradh{
73103b705cfSriastradh	/*
73203b705cfSriastradh	 * vertex data in vertex buffer
73303b705cfSriastradh	 *    position: (x, y)
73403b705cfSriastradh	 *    texture coordinate 0: (u0, v0) if (is_affine is true) else (u0, v0, w0)
73503b705cfSriastradh	 *    texture coordinate 1 if (has_mask is true): same as above
73603b705cfSriastradh	 */
73703b705cfSriastradh	struct gen6_render_state *render = &sna->render_state.gen6;
73803b705cfSriastradh	uint32_t src_format, dw;
73903b705cfSriastradh	int id = GEN6_VERTEX(op->u.gen6.flags);
74003b705cfSriastradh	bool has_mask;
74103b705cfSriastradh
74203b705cfSriastradh	DBG(("%s: setup id=%d\n", __FUNCTION__, id));
74303b705cfSriastradh
74403b705cfSriastradh	if (render->ve_id == id)
74503b705cfSriastradh		return;
74603b705cfSriastradh	render->ve_id = id;
74703b705cfSriastradh
74803b705cfSriastradh	/* The VUE layout
74903b705cfSriastradh	 *    dword 0-3: pad (0.0, 0.0, 0.0. 0.0)
75003b705cfSriastradh	 *    dword 4-7: position (x, y, 1.0, 1.0),
75103b705cfSriastradh	 *    dword 8-11: texture coordinate 0 (u0, v0, w0, 1.0)
75203b705cfSriastradh	 *    dword 12-15: texture coordinate 1 (u1, v1, w1, 1.0)
75303b705cfSriastradh	 *
75403b705cfSriastradh	 * dword 4-15 are fetched from vertex buffer
75503b705cfSriastradh	 */
75603b705cfSriastradh	has_mask = (id >> 2) != 0;
75703b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VERTEX_ELEMENTS |
75803b705cfSriastradh		((2 * (3 + has_mask)) + 1 - 2));
75903b705cfSriastradh
76003b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
76103b705cfSriastradh		  GEN6_SURFACEFORMAT_R32G32B32A32_FLOAT << VE0_FORMAT_SHIFT |
76203b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
76303b705cfSriastradh	OUT_BATCH(GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_0_SHIFT |
76403b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT |
76503b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
76603b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_3_SHIFT);
76703b705cfSriastradh
76803b705cfSriastradh	/* x,y */
76903b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
77003b705cfSriastradh		  GEN6_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
77103b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
77203b705cfSriastradh	OUT_BATCH(GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
77303b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
77403b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
77503b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
77603b705cfSriastradh
77703b705cfSriastradh	/* u0, v0, w0 */
77803b705cfSriastradh	DBG(("%s: first channel %d floats, offset=4b\n", __FUNCTION__, id & 3));
77903b705cfSriastradh	dw = GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
78003b705cfSriastradh	switch (id & 3) {
78103b705cfSriastradh	default:
78203b705cfSriastradh		assert(0);
78303b705cfSriastradh	case 0:
78403b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R16G16_SSCALED;
78503b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
78603b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
78703b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
78803b705cfSriastradh		break;
78903b705cfSriastradh	case 1:
79003b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32_FLOAT;
79103b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
79203b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
79303b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
79403b705cfSriastradh		break;
79503b705cfSriastradh	case 2:
79603b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32G32_FLOAT;
79703b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
79803b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
79903b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
80003b705cfSriastradh		break;
80103b705cfSriastradh	case 3:
80203b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32G32B32_FLOAT;
80303b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
80403b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
80503b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
80603b705cfSriastradh		break;
80703b705cfSriastradh	}
80803b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
80903b705cfSriastradh		  src_format << VE0_FORMAT_SHIFT |
81003b705cfSriastradh		  4 << VE0_OFFSET_SHIFT);
81103b705cfSriastradh	OUT_BATCH(dw);
81203b705cfSriastradh
81303b705cfSriastradh	/* u1, v1, w1 */
81403b705cfSriastradh	if (has_mask) {
81503b705cfSriastradh		unsigned offset = 4 + ((id & 3) ?: 1) * sizeof(float);
81603b705cfSriastradh		DBG(("%s: second channel %d floats, offset=%db\n", __FUNCTION__, id >> 2, offset));
81703b705cfSriastradh		dw = GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
81803b705cfSriastradh		switch (id >> 2) {
81903b705cfSriastradh		case 1:
82003b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32_FLOAT;
82103b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
82203b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
82303b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
82403b705cfSriastradh			break;
82503b705cfSriastradh		default:
82603b705cfSriastradh			assert(0);
82703b705cfSriastradh		case 2:
82803b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32G32_FLOAT;
82903b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
83003b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
83103b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
83203b705cfSriastradh			break;
83303b705cfSriastradh		case 3:
83403b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32G32B32_FLOAT;
83503b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
83603b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
83703b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
83803b705cfSriastradh			break;
83903b705cfSriastradh		}
84003b705cfSriastradh		OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
84103b705cfSriastradh			  src_format << VE0_FORMAT_SHIFT |
84203b705cfSriastradh			  offset << VE0_OFFSET_SHIFT);
84303b705cfSriastradh		OUT_BATCH(dw);
84403b705cfSriastradh	}
84503b705cfSriastradh}
84603b705cfSriastradh
84703b705cfSriastradhstatic void
84803b705cfSriastradhgen6_emit_flush(struct sna *sna)
84903b705cfSriastradh{
85003b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
85103b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL_WC_FLUSH |
85203b705cfSriastradh		  GEN6_PIPE_CONTROL_TC_FLUSH |
85303b705cfSriastradh		  GEN6_PIPE_CONTROL_CS_STALL);
85403b705cfSriastradh	OUT_BATCH(0);
85503b705cfSriastradh	OUT_BATCH(0);
85603b705cfSriastradh}
85703b705cfSriastradh
85803b705cfSriastradhstatic void
85903b705cfSriastradhgen6_emit_state(struct sna *sna,
86003b705cfSriastradh		const struct sna_composite_op *op,
86103b705cfSriastradh		uint16_t wm_binding_table)
86203b705cfSriastradh{
86303b705cfSriastradh	bool need_stall = wm_binding_table & 1;
86403b705cfSriastradh
86503b705cfSriastradh	assert(op->dst.bo->exec);
86603b705cfSriastradh
86703b705cfSriastradh	if (gen6_emit_cc(sna, GEN6_BLEND(op->u.gen6.flags)))
86803b705cfSriastradh		need_stall = false;
86903b705cfSriastradh	gen6_emit_sampler(sna, GEN6_SAMPLER(op->u.gen6.flags));
87003b705cfSriastradh	gen6_emit_sf(sna, GEN6_VERTEX(op->u.gen6.flags) >> 2);
87103b705cfSriastradh	gen6_emit_wm(sna, GEN6_KERNEL(op->u.gen6.flags), GEN6_VERTEX(op->u.gen6.flags) >> 2);
87203b705cfSriastradh	gen6_emit_vertex_elements(sna, op);
87303b705cfSriastradh
87403b705cfSriastradh	need_stall |= gen6_emit_binding_table(sna, wm_binding_table & ~1);
87503b705cfSriastradh	if (gen6_emit_drawing_rectangle(sna, op))
87603b705cfSriastradh		need_stall = false;
87703b705cfSriastradh	if (kgem_bo_is_dirty(op->src.bo) || kgem_bo_is_dirty(op->mask.bo)) {
87803b705cfSriastradh		gen6_emit_flush(sna);
87903b705cfSriastradh		kgem_clear_dirty(&sna->kgem);
88003b705cfSriastradh		assert(op->dst.bo->exec);
88103b705cfSriastradh		kgem_bo_mark_dirty(op->dst.bo);
88203b705cfSriastradh		need_stall = false;
88303b705cfSriastradh	}
88403b705cfSriastradh	if (need_stall) {
88503b705cfSriastradh		OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
88603b705cfSriastradh		OUT_BATCH(GEN6_PIPE_CONTROL_CS_STALL |
88703b705cfSriastradh			  GEN6_PIPE_CONTROL_STALL_AT_SCOREBOARD);
88803b705cfSriastradh		OUT_BATCH(0);
88903b705cfSriastradh		OUT_BATCH(0);
89003b705cfSriastradh	}
89103b705cfSriastradh	sna->render_state.gen6.first_state_packet = false;
89203b705cfSriastradh}
89303b705cfSriastradh
89403b705cfSriastradhstatic bool gen6_magic_ca_pass(struct sna *sna,
89503b705cfSriastradh			       const struct sna_composite_op *op)
89603b705cfSriastradh{
89703b705cfSriastradh	struct gen6_render_state *state = &sna->render_state.gen6;
89803b705cfSriastradh
89903b705cfSriastradh	if (!op->need_magic_ca_pass)
90003b705cfSriastradh		return false;
90103b705cfSriastradh
90203b705cfSriastradh	DBG(("%s: CA fixup (%d -> %d)\n", __FUNCTION__,
90303b705cfSriastradh	     sna->render.vertex_start, sna->render.vertex_index));
90403b705cfSriastradh
90503b705cfSriastradh	gen6_emit_flush(sna);
90603b705cfSriastradh
90703b705cfSriastradh	gen6_emit_cc(sna, gen6_get_blend(PictOpAdd, true, op->dst.format));
90803b705cfSriastradh	gen6_emit_wm(sna,
90903b705cfSriastradh		     gen6_choose_composite_kernel(PictOpAdd,
91003b705cfSriastradh						  true, true,
91103b705cfSriastradh						  op->is_affine),
91203b705cfSriastradh		     true);
91303b705cfSriastradh
91403b705cfSriastradh	OUT_BATCH(GEN6_3DPRIMITIVE |
91503b705cfSriastradh		  GEN6_3DPRIMITIVE_VERTEX_SEQUENTIAL |
91603b705cfSriastradh		  _3DPRIM_RECTLIST << GEN6_3DPRIMITIVE_TOPOLOGY_SHIFT |
91703b705cfSriastradh		  0 << 9 |
91803b705cfSriastradh		  4);
91903b705cfSriastradh	OUT_BATCH(sna->render.vertex_index - sna->render.vertex_start);
92003b705cfSriastradh	OUT_BATCH(sna->render.vertex_start);
92103b705cfSriastradh	OUT_BATCH(1);	/* single instance */
92203b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
92303b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
92403b705cfSriastradh
92503b705cfSriastradh	state->last_primitive = sna->kgem.nbatch;
92603b705cfSriastradh	return true;
92703b705cfSriastradh}
92803b705cfSriastradh
92903b705cfSriastradhtypedef struct gen6_surface_state_padded {
93003b705cfSriastradh	struct gen6_surface_state state;
93103b705cfSriastradh	char pad[32 - sizeof(struct gen6_surface_state)];
93203b705cfSriastradh} gen6_surface_state_padded;
93303b705cfSriastradh
93403b705cfSriastradhstatic void null_create(struct sna_static_stream *stream)
93503b705cfSriastradh{
93603b705cfSriastradh	/* A bunch of zeros useful for legacy border color and depth-stencil */
93703b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
93803b705cfSriastradh}
93903b705cfSriastradh
94003b705cfSriastradhstatic void scratch_create(struct sna_static_stream *stream)
94103b705cfSriastradh{
94203b705cfSriastradh	/* 64 bytes of scratch space for random writes, such as
94303b705cfSriastradh	 * the pipe-control w/a.
94403b705cfSriastradh	 */
94503b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
94603b705cfSriastradh}
94703b705cfSriastradh
94803b705cfSriastradhstatic void
94903b705cfSriastradhsampler_state_init(struct gen6_sampler_state *sampler_state,
95003b705cfSriastradh		   sampler_filter_t filter,
95103b705cfSriastradh		   sampler_extend_t extend)
95203b705cfSriastradh{
95303b705cfSriastradh	sampler_state->ss0.lod_preclamp = 1;	/* GL mode */
95403b705cfSriastradh
95503b705cfSriastradh	/* We use the legacy mode to get the semantics specified by
95603b705cfSriastradh	 * the Render extension. */
95703b705cfSriastradh	sampler_state->ss0.border_color_mode = GEN6_BORDER_COLOR_MODE_LEGACY;
95803b705cfSriastradh
95903b705cfSriastradh	switch (filter) {
96003b705cfSriastradh	default:
96103b705cfSriastradh	case SAMPLER_FILTER_NEAREST:
96203b705cfSriastradh		sampler_state->ss0.min_filter = GEN6_MAPFILTER_NEAREST;
96303b705cfSriastradh		sampler_state->ss0.mag_filter = GEN6_MAPFILTER_NEAREST;
96403b705cfSriastradh		break;
96503b705cfSriastradh	case SAMPLER_FILTER_BILINEAR:
96603b705cfSriastradh		sampler_state->ss0.min_filter = GEN6_MAPFILTER_LINEAR;
96703b705cfSriastradh		sampler_state->ss0.mag_filter = GEN6_MAPFILTER_LINEAR;
96803b705cfSriastradh		break;
96903b705cfSriastradh	}
97003b705cfSriastradh
97103b705cfSriastradh	switch (extend) {
97203b705cfSriastradh	default:
97303b705cfSriastradh	case SAMPLER_EXTEND_NONE:
97403b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
97503b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
97603b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
97703b705cfSriastradh		break;
97803b705cfSriastradh	case SAMPLER_EXTEND_REPEAT:
97903b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
98003b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
98103b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
98203b705cfSriastradh		break;
98303b705cfSriastradh	case SAMPLER_EXTEND_PAD:
98403b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
98503b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
98603b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
98703b705cfSriastradh		break;
98803b705cfSriastradh	case SAMPLER_EXTEND_REFLECT:
98903b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
99003b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
99103b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
99203b705cfSriastradh		break;
99303b705cfSriastradh	}
99403b705cfSriastradh}
99503b705cfSriastradh
99603b705cfSriastradhstatic void
99703b705cfSriastradhsampler_copy_init(struct gen6_sampler_state *ss)
99803b705cfSriastradh{
99903b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
100003b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
100103b705cfSriastradh
100203b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
100303b705cfSriastradh}
100403b705cfSriastradh
100503b705cfSriastradhstatic void
100603b705cfSriastradhsampler_fill_init(struct gen6_sampler_state *ss)
100703b705cfSriastradh{
100803b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_REPEAT);
100903b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
101003b705cfSriastradh
101103b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
101203b705cfSriastradh}
101303b705cfSriastradh
101403b705cfSriastradhstatic uint32_t
101503b705cfSriastradhgen6_tiling_bits(uint32_t tiling)
101603b705cfSriastradh{
101703b705cfSriastradh	switch (tiling) {
101803b705cfSriastradh	default: assert(0);
101903b705cfSriastradh	case I915_TILING_NONE: return 0;
102003b705cfSriastradh	case I915_TILING_X: return GEN6_SURFACE_TILED;
102103b705cfSriastradh	case I915_TILING_Y: return GEN6_SURFACE_TILED | GEN6_SURFACE_TILED_Y;
102203b705cfSriastradh	}
102303b705cfSriastradh}
102403b705cfSriastradh
102503b705cfSriastradh/**
102603b705cfSriastradh * Sets up the common fields for a surface state buffer for the given
102703b705cfSriastradh * picture in the given surface state buffer.
102803b705cfSriastradh */
102903b705cfSriastradhstatic int
103003b705cfSriastradhgen6_bind_bo(struct sna *sna,
103103b705cfSriastradh	     struct kgem_bo *bo,
103203b705cfSriastradh	     uint32_t width,
103303b705cfSriastradh	     uint32_t height,
103403b705cfSriastradh	     uint32_t format,
103503b705cfSriastradh	     bool is_dst)
103603b705cfSriastradh{
103703b705cfSriastradh	uint32_t *ss;
103803b705cfSriastradh	uint32_t domains;
103903b705cfSriastradh	uint16_t offset;
104003b705cfSriastradh	uint32_t is_scanout = is_dst && bo->scanout;
104103b705cfSriastradh
104203b705cfSriastradh	/* After the first bind, we manage the cache domains within the batch */
104303b705cfSriastradh	offset = kgem_bo_get_binding(bo, format | is_dst << 30 | is_scanout << 31);
104403b705cfSriastradh	if (offset) {
104503b705cfSriastradh		DBG(("[%x]  bo(handle=%d), format=%d, reuse %s binding\n",
104603b705cfSriastradh		     offset, bo->handle, format,
104703b705cfSriastradh		     is_dst ? "render" : "sampler"));
104803b705cfSriastradh		if (is_dst)
104903b705cfSriastradh			kgem_bo_mark_dirty(bo);
105003b705cfSriastradh		return offset * sizeof(uint32_t);
105103b705cfSriastradh	}
105203b705cfSriastradh
105303b705cfSriastradh	offset = sna->kgem.surface -=
105403b705cfSriastradh		sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
105503b705cfSriastradh	ss = sna->kgem.batch + offset;
105603b705cfSriastradh	ss[0] = (GEN6_SURFACE_2D << GEN6_SURFACE_TYPE_SHIFT |
105703b705cfSriastradh		 GEN6_SURFACE_BLEND_ENABLED |
105803b705cfSriastradh		 format << GEN6_SURFACE_FORMAT_SHIFT);
105903b705cfSriastradh	if (is_dst) {
106003b705cfSriastradh		ss[0] |= GEN6_SURFACE_RC_READ_WRITE;
106103b705cfSriastradh		domains = I915_GEM_DOMAIN_RENDER << 16 |I915_GEM_DOMAIN_RENDER;
106203b705cfSriastradh	} else
106303b705cfSriastradh		domains = I915_GEM_DOMAIN_SAMPLER << 16;
106403b705cfSriastradh	ss[1] = kgem_add_reloc(&sna->kgem, offset + 1, bo, domains, 0);
106503b705cfSriastradh	ss[2] = ((width - 1)  << GEN6_SURFACE_WIDTH_SHIFT |
106603b705cfSriastradh		 (height - 1) << GEN6_SURFACE_HEIGHT_SHIFT);
106703b705cfSriastradh	assert(bo->pitch <= (1 << 18));
106803b705cfSriastradh	ss[3] = (gen6_tiling_bits(bo->tiling) |
106903b705cfSriastradh		 (bo->pitch - 1) << GEN6_SURFACE_PITCH_SHIFT);
107003b705cfSriastradh	ss[4] = 0;
107103b705cfSriastradh	ss[5] = is_scanout ? 0 : 3 << 16;
107203b705cfSriastradh
107303b705cfSriastradh	kgem_bo_set_binding(bo, format | is_dst << 30 | is_scanout << 31, offset);
107403b705cfSriastradh
107503b705cfSriastradh	DBG(("[%x] bind bo(handle=%d, addr=%d), format=%d, width=%d, height=%d, pitch=%d, tiling=%d -> %s\n",
107603b705cfSriastradh	     offset, bo->handle, ss[1],
107703b705cfSriastradh	     format, width, height, bo->pitch, bo->tiling,
107803b705cfSriastradh	     domains & 0xffff ? "render" : "sampler"));
107903b705cfSriastradh
108003b705cfSriastradh	return offset * sizeof(uint32_t);
108103b705cfSriastradh}
108203b705cfSriastradh
108303b705cfSriastradhstatic void gen6_emit_vertex_buffer(struct sna *sna,
108403b705cfSriastradh				    const struct sna_composite_op *op)
108503b705cfSriastradh{
108603b705cfSriastradh	int id = GEN6_VERTEX(op->u.gen6.flags);
108703b705cfSriastradh
108803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VERTEX_BUFFERS | 3);
108903b705cfSriastradh	OUT_BATCH(id << VB0_BUFFER_INDEX_SHIFT | VB0_VERTEXDATA |
109003b705cfSriastradh		  4*op->floats_per_vertex << VB0_BUFFER_PITCH_SHIFT);
109103b705cfSriastradh	sna->render.vertex_reloc[sna->render.nvertex_reloc++] = sna->kgem.nbatch;
109203b705cfSriastradh	OUT_BATCH(0);
109303b705cfSriastradh	OUT_BATCH(~0); /* max address: disabled */
109403b705cfSriastradh	OUT_BATCH(0);
109503b705cfSriastradh
109603b705cfSriastradh	sna->render.vb_id |= 1 << id;
109703b705cfSriastradh}
109803b705cfSriastradh
109903b705cfSriastradhstatic void gen6_emit_primitive(struct sna *sna)
110003b705cfSriastradh{
110103b705cfSriastradh	if (sna->kgem.nbatch == sna->render_state.gen6.last_primitive) {
110203b705cfSriastradh		DBG(("%s: continuing previous primitive, start=%d, index=%d\n",
110303b705cfSriastradh		     __FUNCTION__,
110403b705cfSriastradh		     sna->render.vertex_start,
110503b705cfSriastradh		     sna->render.vertex_index));
110603b705cfSriastradh		sna->render.vertex_offset = sna->kgem.nbatch - 5;
110703b705cfSriastradh		return;
110803b705cfSriastradh	}
110903b705cfSriastradh
111003b705cfSriastradh	OUT_BATCH(GEN6_3DPRIMITIVE |
111103b705cfSriastradh		  GEN6_3DPRIMITIVE_VERTEX_SEQUENTIAL |
111203b705cfSriastradh		  _3DPRIM_RECTLIST << GEN6_3DPRIMITIVE_TOPOLOGY_SHIFT |
111303b705cfSriastradh		  0 << 9 |
111403b705cfSriastradh		  4);
111503b705cfSriastradh	sna->render.vertex_offset = sna->kgem.nbatch;
111603b705cfSriastradh	OUT_BATCH(0);	/* vertex count, to be filled in later */
111703b705cfSriastradh	OUT_BATCH(sna->render.vertex_index);
111803b705cfSriastradh	OUT_BATCH(1);	/* single instance */
111903b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
112003b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
112103b705cfSriastradh	sna->render.vertex_start = sna->render.vertex_index;
112203b705cfSriastradh	DBG(("%s: started new primitive: index=%d\n",
112303b705cfSriastradh	     __FUNCTION__, sna->render.vertex_start));
112403b705cfSriastradh
112503b705cfSriastradh	sna->render_state.gen6.last_primitive = sna->kgem.nbatch;
112603b705cfSriastradh}
112703b705cfSriastradh
112803b705cfSriastradhstatic bool gen6_rectangle_begin(struct sna *sna,
112903b705cfSriastradh				 const struct sna_composite_op *op)
113003b705cfSriastradh{
113103b705cfSriastradh	int id = 1 << GEN6_VERTEX(op->u.gen6.flags);
113203b705cfSriastradh	int ndwords;
113303b705cfSriastradh
113403b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render) && sna->render.vertex_offset)
113503b705cfSriastradh		return true;
113603b705cfSriastradh
113703b705cfSriastradh	ndwords = op->need_magic_ca_pass ? 60 : 6;
113803b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
113903b705cfSriastradh		ndwords += 5;
114003b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, ndwords))
114103b705cfSriastradh		return false;
114203b705cfSriastradh
114303b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
114403b705cfSriastradh		gen6_emit_vertex_buffer(sna, op);
114503b705cfSriastradh
114603b705cfSriastradh	gen6_emit_primitive(sna);
114703b705cfSriastradh	return true;
114803b705cfSriastradh}
114903b705cfSriastradh
115003b705cfSriastradhstatic int gen6_get_rectangles__flush(struct sna *sna,
115103b705cfSriastradh				      const struct sna_composite_op *op)
115203b705cfSriastradh{
115303b705cfSriastradh	/* Preventing discarding new vbo after lock contention */
115403b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render)) {
115503b705cfSriastradh		int rem = vertex_space(sna);
115603b705cfSriastradh		if (rem > op->floats_per_rect)
115703b705cfSriastradh			return rem;
115803b705cfSriastradh	}
115903b705cfSriastradh
116003b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, op->need_magic_ca_pass ? 65 : 5))
116103b705cfSriastradh		return 0;
116203b705cfSriastradh	if (!kgem_check_reloc_and_exec(&sna->kgem, 2))
116303b705cfSriastradh		return 0;
116403b705cfSriastradh
116503b705cfSriastradh	if (sna->render.vertex_offset) {
116603b705cfSriastradh		gen4_vertex_flush(sna);
116703b705cfSriastradh		if (gen6_magic_ca_pass(sna, op)) {
116803b705cfSriastradh			gen6_emit_flush(sna);
116903b705cfSriastradh			gen6_emit_cc(sna, GEN6_BLEND(op->u.gen6.flags));
117003b705cfSriastradh			gen6_emit_wm(sna,
117103b705cfSriastradh				     GEN6_KERNEL(op->u.gen6.flags),
117203b705cfSriastradh				     GEN6_VERTEX(op->u.gen6.flags) >> 2);
117303b705cfSriastradh		}
117403b705cfSriastradh	}
117503b705cfSriastradh
117603b705cfSriastradh	return gen4_vertex_finish(sna);
117703b705cfSriastradh}
117803b705cfSriastradh
117903b705cfSriastradhinline static int gen6_get_rectangles(struct sna *sna,
118003b705cfSriastradh				      const struct sna_composite_op *op,
118103b705cfSriastradh				      int want,
118203b705cfSriastradh				      void (*emit_state)(struct sna *, const struct sna_composite_op *op))
118303b705cfSriastradh{
118403b705cfSriastradh	int rem;
118503b705cfSriastradh
118603b705cfSriastradh	assert(want);
118703b705cfSriastradh
118803b705cfSriastradhstart:
118903b705cfSriastradh	rem = vertex_space(sna);
119003b705cfSriastradh	if (unlikely(rem < op->floats_per_rect)) {
119103b705cfSriastradh		DBG(("flushing vbo for %s: %d < %d\n",
119203b705cfSriastradh		     __FUNCTION__, rem, op->floats_per_rect));
119303b705cfSriastradh		rem = gen6_get_rectangles__flush(sna, op);
119403b705cfSriastradh		if (unlikely(rem == 0))
119503b705cfSriastradh			goto flush;
119603b705cfSriastradh	}
119703b705cfSriastradh
119803b705cfSriastradh	if (unlikely(sna->render.vertex_offset == 0)) {
119903b705cfSriastradh		if (!gen6_rectangle_begin(sna, op))
120003b705cfSriastradh			goto flush;
120103b705cfSriastradh		else
120203b705cfSriastradh			goto start;
120303b705cfSriastradh	}
120403b705cfSriastradh
120503b705cfSriastradh	assert(rem <= vertex_space(sna));
120603b705cfSriastradh	assert(op->floats_per_rect <= rem);
120703b705cfSriastradh	if (want > 1 && want * op->floats_per_rect > rem)
120803b705cfSriastradh		want = rem / op->floats_per_rect;
120903b705cfSriastradh
121003b705cfSriastradh	assert(want > 0);
121103b705cfSriastradh	sna->render.vertex_index += 3*want;
121203b705cfSriastradh	return want;
121303b705cfSriastradh
121403b705cfSriastradhflush:
121503b705cfSriastradh	if (sna->render.vertex_offset) {
121603b705cfSriastradh		gen4_vertex_flush(sna);
121703b705cfSriastradh		gen6_magic_ca_pass(sna, op);
121803b705cfSriastradh	}
121903b705cfSriastradh	sna_vertex_wait__locked(&sna->render);
122003b705cfSriastradh	_kgem_submit(&sna->kgem);
122103b705cfSriastradh	emit_state(sna, op);
122203b705cfSriastradh	goto start;
122303b705cfSriastradh}
122403b705cfSriastradh
122503b705cfSriastradhinline static uint32_t *gen6_composite_get_binding_table(struct sna *sna,
122603b705cfSriastradh							 uint16_t *offset)
122703b705cfSriastradh{
122803b705cfSriastradh	uint32_t *table;
122903b705cfSriastradh
123003b705cfSriastradh	sna->kgem.surface -=
123103b705cfSriastradh		sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
123203b705cfSriastradh	/* Clear all surplus entries to zero in case of prefetch */
123303b705cfSriastradh	table = memset(sna->kgem.batch + sna->kgem.surface,
123403b705cfSriastradh		       0, sizeof(struct gen6_surface_state_padded));
123503b705cfSriastradh
123603b705cfSriastradh	DBG(("%s(%x)\n", __FUNCTION__, 4*sna->kgem.surface));
123703b705cfSriastradh
123803b705cfSriastradh	*offset = sna->kgem.surface;
123903b705cfSriastradh	return table;
124003b705cfSriastradh}
124103b705cfSriastradh
124203b705cfSriastradhstatic bool
124303b705cfSriastradhgen6_get_batch(struct sna *sna, const struct sna_composite_op *op)
124403b705cfSriastradh{
124503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, op->dst.bo);
124603b705cfSriastradh
124703b705cfSriastradh	if (!kgem_check_batch_with_surfaces(&sna->kgem, 150, 4)) {
124803b705cfSriastradh		DBG(("%s: flushing batch: %d < %d+%d\n",
124903b705cfSriastradh		     __FUNCTION__, sna->kgem.surface - sna->kgem.nbatch,
125003b705cfSriastradh		     150, 4*8));
125103b705cfSriastradh		kgem_submit(&sna->kgem);
125203b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
125303b705cfSriastradh	}
125403b705cfSriastradh
125503b705cfSriastradh	if (sna->render_state.gen6.needs_invariant)
125603b705cfSriastradh		gen6_emit_invariant(sna);
125703b705cfSriastradh
125803b705cfSriastradh	return kgem_bo_is_dirty(op->dst.bo);
125903b705cfSriastradh}
126003b705cfSriastradh
126103b705cfSriastradhstatic void gen6_emit_composite_state(struct sna *sna,
126203b705cfSriastradh				      const struct sna_composite_op *op)
126303b705cfSriastradh{
126403b705cfSriastradh	uint32_t *binding_table;
126503b705cfSriastradh	uint16_t offset;
126603b705cfSriastradh	bool dirty;
126703b705cfSriastradh
126803b705cfSriastradh	dirty = gen6_get_batch(sna, op);
126903b705cfSriastradh
127003b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
127103b705cfSriastradh
127203b705cfSriastradh	binding_table[0] =
127303b705cfSriastradh		gen6_bind_bo(sna,
127403b705cfSriastradh			    op->dst.bo, op->dst.width, op->dst.height,
127503b705cfSriastradh			    gen6_get_dest_format(op->dst.format),
127603b705cfSriastradh			    true);
127703b705cfSriastradh	binding_table[1] =
127803b705cfSriastradh		gen6_bind_bo(sna,
127903b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
128003b705cfSriastradh			     op->src.card_format,
128103b705cfSriastradh			     false);
128203b705cfSriastradh	if (op->mask.bo) {
128303b705cfSriastradh		binding_table[2] =
128403b705cfSriastradh			gen6_bind_bo(sna,
128503b705cfSriastradh				     op->mask.bo,
128603b705cfSriastradh				     op->mask.width,
128703b705cfSriastradh				     op->mask.height,
128803b705cfSriastradh				     op->mask.card_format,
128903b705cfSriastradh				     false);
129003b705cfSriastradh	}
129103b705cfSriastradh
129203b705cfSriastradh	if (sna->kgem.surface == offset &&
129303b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table &&
129403b705cfSriastradh	    (op->mask.bo == NULL ||
129503b705cfSriastradh	     sna->kgem.batch[sna->render_state.gen6.surface_table+2] == binding_table[2])) {
129603b705cfSriastradh		sna->kgem.surface += sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
129703b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
129803b705cfSriastradh	}
129903b705cfSriastradh
130003b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
130103b705cfSriastradh}
130203b705cfSriastradh
130303b705cfSriastradhstatic void
130403b705cfSriastradhgen6_align_vertex(struct sna *sna, const struct sna_composite_op *op)
130503b705cfSriastradh{
130603b705cfSriastradh	assert (sna->render.vertex_offset == 0);
130703b705cfSriastradh	if (op->floats_per_vertex != sna->render_state.gen6.floats_per_vertex) {
130803b705cfSriastradh		if (sna->render.vertex_size - sna->render.vertex_used < 2*op->floats_per_rect)
130903b705cfSriastradh			gen4_vertex_finish(sna);
131003b705cfSriastradh
131103b705cfSriastradh		DBG(("aligning vertex: was %d, now %d floats per vertex, %d->%d\n",
131203b705cfSriastradh		     sna->render_state.gen6.floats_per_vertex,
131303b705cfSriastradh		     op->floats_per_vertex,
131403b705cfSriastradh		     sna->render.vertex_index,
131503b705cfSriastradh		     (sna->render.vertex_used + op->floats_per_vertex - 1) / op->floats_per_vertex));
131603b705cfSriastradh		sna->render.vertex_index = (sna->render.vertex_used + op->floats_per_vertex - 1) / op->floats_per_vertex;
131703b705cfSriastradh		sna->render.vertex_used = sna->render.vertex_index * op->floats_per_vertex;
131803b705cfSriastradh		sna->render_state.gen6.floats_per_vertex = op->floats_per_vertex;
131903b705cfSriastradh	}
132003b705cfSriastradh	assert((sna->render.vertex_used % op->floats_per_vertex) == 0);
132103b705cfSriastradh}
132203b705cfSriastradh
132303b705cfSriastradhfastcall static void
132403b705cfSriastradhgen6_render_composite_blt(struct sna *sna,
132503b705cfSriastradh			  const struct sna_composite_op *op,
132603b705cfSriastradh			  const struct sna_composite_rectangles *r)
132703b705cfSriastradh{
132803b705cfSriastradh	gen6_get_rectangles(sna, op, 1, gen6_emit_composite_state);
132903b705cfSriastradh	op->prim_emit(sna, op, r);
133003b705cfSriastradh}
133103b705cfSriastradh
133203b705cfSriastradhfastcall static void
133303b705cfSriastradhgen6_render_composite_box(struct sna *sna,
133403b705cfSriastradh			  const struct sna_composite_op *op,
133503b705cfSriastradh			  const BoxRec *box)
133603b705cfSriastradh{
133703b705cfSriastradh	struct sna_composite_rectangles r;
133803b705cfSriastradh
133903b705cfSriastradh	gen6_get_rectangles(sna, op, 1, gen6_emit_composite_state);
134003b705cfSriastradh
134103b705cfSriastradh	DBG(("  %s: (%d, %d), (%d, %d)\n",
134203b705cfSriastradh	     __FUNCTION__,
134303b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
134403b705cfSriastradh
134503b705cfSriastradh	r.dst.x = box->x1;
134603b705cfSriastradh	r.dst.y = box->y1;
134703b705cfSriastradh	r.width  = box->x2 - box->x1;
134803b705cfSriastradh	r.height = box->y2 - box->y1;
134903b705cfSriastradh	r.src = r.mask = r.dst;
135003b705cfSriastradh
135103b705cfSriastradh	op->prim_emit(sna, op, &r);
135203b705cfSriastradh}
135303b705cfSriastradh
135403b705cfSriastradhstatic void
135503b705cfSriastradhgen6_render_composite_boxes__blt(struct sna *sna,
135603b705cfSriastradh				 const struct sna_composite_op *op,
135703b705cfSriastradh				 const BoxRec *box, int nbox)
135803b705cfSriastradh{
135903b705cfSriastradh	DBG(("composite_boxes(%d)\n", nbox));
136003b705cfSriastradh
136103b705cfSriastradh	do {
136203b705cfSriastradh		int nbox_this_time;
136303b705cfSriastradh
136403b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
136503b705cfSriastradh						     gen6_emit_composite_state);
136603b705cfSriastradh		nbox -= nbox_this_time;
136703b705cfSriastradh
136803b705cfSriastradh		do {
136903b705cfSriastradh			struct sna_composite_rectangles r;
137003b705cfSriastradh
137103b705cfSriastradh			DBG(("  %s: (%d, %d), (%d, %d)\n",
137203b705cfSriastradh			     __FUNCTION__,
137303b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
137403b705cfSriastradh
137503b705cfSriastradh			r.dst.x = box->x1;
137603b705cfSriastradh			r.dst.y = box->y1;
137703b705cfSriastradh			r.width  = box->x2 - box->x1;
137803b705cfSriastradh			r.height = box->y2 - box->y1;
137903b705cfSriastradh			r.src = r.mask = r.dst;
138003b705cfSriastradh
138103b705cfSriastradh			op->prim_emit(sna, op, &r);
138203b705cfSriastradh			box++;
138303b705cfSriastradh		} while (--nbox_this_time);
138403b705cfSriastradh	} while (nbox);
138503b705cfSriastradh}
138603b705cfSriastradh
138703b705cfSriastradhstatic void
138803b705cfSriastradhgen6_render_composite_boxes(struct sna *sna,
138903b705cfSriastradh			    const struct sna_composite_op *op,
139003b705cfSriastradh			    const BoxRec *box, int nbox)
139103b705cfSriastradh{
139203b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
139303b705cfSriastradh
139403b705cfSriastradh	do {
139503b705cfSriastradh		int nbox_this_time;
139603b705cfSriastradh		float *v;
139703b705cfSriastradh
139803b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
139903b705cfSriastradh						     gen6_emit_composite_state);
140003b705cfSriastradh		assert(nbox_this_time);
140103b705cfSriastradh		nbox -= nbox_this_time;
140203b705cfSriastradh
140303b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
140403b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
140503b705cfSriastradh
140603b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
140703b705cfSriastradh		box += nbox_this_time;
140803b705cfSriastradh	} while (nbox);
140903b705cfSriastradh}
141003b705cfSriastradh
141103b705cfSriastradhstatic void
141203b705cfSriastradhgen6_render_composite_boxes__thread(struct sna *sna,
141303b705cfSriastradh				    const struct sna_composite_op *op,
141403b705cfSriastradh				    const BoxRec *box, int nbox)
141503b705cfSriastradh{
141603b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
141703b705cfSriastradh
141803b705cfSriastradh	sna_vertex_lock(&sna->render);
141903b705cfSriastradh	do {
142003b705cfSriastradh		int nbox_this_time;
142103b705cfSriastradh		float *v;
142203b705cfSriastradh
142303b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
142403b705cfSriastradh						     gen6_emit_composite_state);
142503b705cfSriastradh		assert(nbox_this_time);
142603b705cfSriastradh		nbox -= nbox_this_time;
142703b705cfSriastradh
142803b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
142903b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
143003b705cfSriastradh
143103b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
143203b705cfSriastradh		sna_vertex_unlock(&sna->render);
143303b705cfSriastradh
143403b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
143503b705cfSriastradh		box += nbox_this_time;
143603b705cfSriastradh
143703b705cfSriastradh		sna_vertex_lock(&sna->render);
143803b705cfSriastradh		sna_vertex_release__locked(&sna->render);
143903b705cfSriastradh	} while (nbox);
144003b705cfSriastradh	sna_vertex_unlock(&sna->render);
144103b705cfSriastradh}
144203b705cfSriastradh
144303b705cfSriastradh#ifndef MAX
144403b705cfSriastradh#define MAX(a,b) ((a) > (b) ? (a) : (b))
144503b705cfSriastradh#endif
144603b705cfSriastradh
144703b705cfSriastradhstatic uint32_t
144803b705cfSriastradhgen6_composite_create_blend_state(struct sna_static_stream *stream)
144903b705cfSriastradh{
145003b705cfSriastradh	char *base, *ptr;
145103b705cfSriastradh	int src, dst;
145203b705cfSriastradh
145303b705cfSriastradh	base = sna_static_stream_map(stream,
145403b705cfSriastradh				     GEN6_BLENDFACTOR_COUNT * GEN6_BLENDFACTOR_COUNT * GEN6_BLEND_STATE_PADDED_SIZE,
145503b705cfSriastradh				     64);
145603b705cfSriastradh
145703b705cfSriastradh	ptr = base;
145803b705cfSriastradh	for (src = 0; src < GEN6_BLENDFACTOR_COUNT; src++) {
145903b705cfSriastradh		for (dst= 0; dst < GEN6_BLENDFACTOR_COUNT; dst++) {
146003b705cfSriastradh			struct gen6_blend_state *blend =
146103b705cfSriastradh				(struct gen6_blend_state *)ptr;
146203b705cfSriastradh
146303b705cfSriastradh			blend->blend0.dest_blend_factor = dst;
146403b705cfSriastradh			blend->blend0.source_blend_factor = src;
146503b705cfSriastradh			blend->blend0.blend_func = GEN6_BLENDFUNCTION_ADD;
146603b705cfSriastradh			blend->blend0.blend_enable =
146703b705cfSriastradh				!(dst == GEN6_BLENDFACTOR_ZERO && src == GEN6_BLENDFACTOR_ONE);
146803b705cfSriastradh
146903b705cfSriastradh			blend->blend1.post_blend_clamp_enable = 1;
147003b705cfSriastradh			blend->blend1.pre_blend_clamp_enable = 1;
147103b705cfSriastradh
147203b705cfSriastradh			ptr += GEN6_BLEND_STATE_PADDED_SIZE;
147303b705cfSriastradh		}
147403b705cfSriastradh	}
147503b705cfSriastradh
147603b705cfSriastradh	return sna_static_stream_offsetof(stream, base);
147703b705cfSriastradh}
147803b705cfSriastradh
147903b705cfSriastradhstatic uint32_t gen6_bind_video_source(struct sna *sna,
148003b705cfSriastradh				       struct kgem_bo *src_bo,
148103b705cfSriastradh				       uint32_t src_offset,
148203b705cfSriastradh				       int src_width,
148303b705cfSriastradh				       int src_height,
148403b705cfSriastradh				       int src_pitch,
148503b705cfSriastradh				       uint32_t src_surf_format)
148603b705cfSriastradh{
148703b705cfSriastradh	struct gen6_surface_state *ss;
148803b705cfSriastradh
148903b705cfSriastradh	sna->kgem.surface -= sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
149003b705cfSriastradh
149103b705cfSriastradh	ss = memset(sna->kgem.batch + sna->kgem.surface, 0, sizeof(*ss));
149203b705cfSriastradh	ss->ss0.surface_type = GEN6_SURFACE_2D;
149303b705cfSriastradh	ss->ss0.surface_format = src_surf_format;
149403b705cfSriastradh
149503b705cfSriastradh	ss->ss1.base_addr =
149603b705cfSriastradh		kgem_add_reloc(&sna->kgem,
149703b705cfSriastradh			       sna->kgem.surface + 1,
149803b705cfSriastradh			       src_bo,
149903b705cfSriastradh			       I915_GEM_DOMAIN_SAMPLER << 16,
150003b705cfSriastradh			       src_offset);
150103b705cfSriastradh
150203b705cfSriastradh	ss->ss2.width  = src_width - 1;
150303b705cfSriastradh	ss->ss2.height = src_height - 1;
150403b705cfSriastradh	ss->ss3.pitch  = src_pitch - 1;
150503b705cfSriastradh
150603b705cfSriastradh	return sna->kgem.surface * sizeof(uint32_t);
150703b705cfSriastradh}
150803b705cfSriastradh
150903b705cfSriastradhstatic void gen6_emit_video_state(struct sna *sna,
151003b705cfSriastradh				  const struct sna_composite_op *op)
151103b705cfSriastradh{
151203b705cfSriastradh	struct sna_video_frame *frame = op->priv;
151303b705cfSriastradh	uint32_t src_surf_format;
151403b705cfSriastradh	uint32_t src_surf_base[6];
151503b705cfSriastradh	int src_width[6];
151603b705cfSriastradh	int src_height[6];
151703b705cfSriastradh	int src_pitch[6];
151803b705cfSriastradh	uint32_t *binding_table;
151903b705cfSriastradh	uint16_t offset;
152003b705cfSriastradh	bool dirty;
152103b705cfSriastradh	int n_src, n;
152203b705cfSriastradh
152303b705cfSriastradh	dirty = gen6_get_batch(sna, op);
152403b705cfSriastradh
152503b705cfSriastradh	src_surf_base[0] = 0;
152603b705cfSriastradh	src_surf_base[1] = 0;
152703b705cfSriastradh	src_surf_base[2] = frame->VBufOffset;
152803b705cfSriastradh	src_surf_base[3] = frame->VBufOffset;
152903b705cfSriastradh	src_surf_base[4] = frame->UBufOffset;
153003b705cfSriastradh	src_surf_base[5] = frame->UBufOffset;
153103b705cfSriastradh
153203b705cfSriastradh	if (is_planar_fourcc(frame->id)) {
153303b705cfSriastradh		src_surf_format = GEN6_SURFACEFORMAT_R8_UNORM;
153403b705cfSriastradh		src_width[1]  = src_width[0]  = frame->width;
153503b705cfSriastradh		src_height[1] = src_height[0] = frame->height;
153603b705cfSriastradh		src_pitch[1]  = src_pitch[0]  = frame->pitch[1];
153703b705cfSriastradh		src_width[4]  = src_width[5]  = src_width[2]  = src_width[3] =
153803b705cfSriastradh			frame->width / 2;
153903b705cfSriastradh		src_height[4] = src_height[5] = src_height[2] = src_height[3] =
154003b705cfSriastradh			frame->height / 2;
154103b705cfSriastradh		src_pitch[4]  = src_pitch[5]  = src_pitch[2]  = src_pitch[3] =
154203b705cfSriastradh			frame->pitch[0];
154303b705cfSriastradh		n_src = 6;
154403b705cfSriastradh	} else {
154503b705cfSriastradh		if (frame->id == FOURCC_UYVY)
154603b705cfSriastradh			src_surf_format = GEN6_SURFACEFORMAT_YCRCB_SWAPY;
154703b705cfSriastradh		else
154803b705cfSriastradh			src_surf_format = GEN6_SURFACEFORMAT_YCRCB_NORMAL;
154903b705cfSriastradh
155003b705cfSriastradh		src_width[0]  = frame->width;
155103b705cfSriastradh		src_height[0] = frame->height;
155203b705cfSriastradh		src_pitch[0]  = frame->pitch[0];
155303b705cfSriastradh		n_src = 1;
155403b705cfSriastradh	}
155503b705cfSriastradh
155603b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
155703b705cfSriastradh
155803b705cfSriastradh	binding_table[0] =
155903b705cfSriastradh		gen6_bind_bo(sna,
156003b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
156103b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
156203b705cfSriastradh			     true);
156303b705cfSriastradh	for (n = 0; n < n_src; n++) {
156403b705cfSriastradh		binding_table[1+n] =
156503b705cfSriastradh			gen6_bind_video_source(sna,
156603b705cfSriastradh					       frame->bo,
156703b705cfSriastradh					       src_surf_base[n],
156803b705cfSriastradh					       src_width[n],
156903b705cfSriastradh					       src_height[n],
157003b705cfSriastradh					       src_pitch[n],
157103b705cfSriastradh					       src_surf_format);
157203b705cfSriastradh	}
157303b705cfSriastradh
157403b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
157503b705cfSriastradh}
157603b705cfSriastradh
157703b705cfSriastradhstatic bool
157803b705cfSriastradhgen6_render_video(struct sna *sna,
157903b705cfSriastradh		  struct sna_video *video,
158003b705cfSriastradh		  struct sna_video_frame *frame,
158103b705cfSriastradh		  RegionPtr dstRegion,
158203b705cfSriastradh		  PixmapPtr pixmap)
158303b705cfSriastradh{
158403b705cfSriastradh	struct sna_composite_op tmp;
158503b705cfSriastradh	int dst_width = dstRegion->extents.x2 - dstRegion->extents.x1;
158603b705cfSriastradh	int dst_height = dstRegion->extents.y2 - dstRegion->extents.y1;
158703b705cfSriastradh	int src_width = frame->src.x2 - frame->src.x1;
158803b705cfSriastradh	int src_height = frame->src.y2 - frame->src.y1;
158903b705cfSriastradh	float src_offset_x, src_offset_y;
159003b705cfSriastradh	float src_scale_x, src_scale_y;
159103b705cfSriastradh	int nbox, pix_xoff, pix_yoff;
159203b705cfSriastradh	struct sna_pixmap *priv;
159303b705cfSriastradh	unsigned filter;
159403b705cfSriastradh	BoxPtr box;
159503b705cfSriastradh
159603b705cfSriastradh	DBG(("%s: src=(%d, %d), dst=(%d, %d), %ldx[(%d, %d), (%d, %d)...]\n",
159703b705cfSriastradh	     __FUNCTION__,
159803b705cfSriastradh	     src_width, src_height, dst_width, dst_height,
159903b705cfSriastradh	     (long)REGION_NUM_RECTS(dstRegion),
160003b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x1,
160103b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y1,
160203b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x2,
160303b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y2));
160403b705cfSriastradh
160503b705cfSriastradh	priv = sna_pixmap_force_to_gpu(pixmap, MOVE_READ | MOVE_WRITE);
160603b705cfSriastradh	if (priv == NULL)
160703b705cfSriastradh		return false;
160803b705cfSriastradh
160903b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
161003b705cfSriastradh
161103b705cfSriastradh	tmp.dst.pixmap = pixmap;
161203b705cfSriastradh	tmp.dst.width  = pixmap->drawable.width;
161303b705cfSriastradh	tmp.dst.height = pixmap->drawable.height;
161403b705cfSriastradh	tmp.dst.format = sna_render_format_for_depth(pixmap->drawable.depth);
161503b705cfSriastradh	tmp.dst.bo = priv->gpu_bo;
161603b705cfSriastradh
161703b705cfSriastradh	tmp.src.bo = frame->bo;
161803b705cfSriastradh	tmp.mask.bo = NULL;
161903b705cfSriastradh
162003b705cfSriastradh	tmp.floats_per_vertex = 3;
162103b705cfSriastradh	tmp.floats_per_rect = 9;
162203b705cfSriastradh
162303b705cfSriastradh	if (src_width == dst_width && src_height == dst_height)
162403b705cfSriastradh		filter = SAMPLER_FILTER_NEAREST;
162503b705cfSriastradh	else
162603b705cfSriastradh		filter = SAMPLER_FILTER_BILINEAR;
162703b705cfSriastradh
162803b705cfSriastradh	tmp.u.gen6.flags =
162903b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(filter, SAMPLER_EXTEND_PAD,
163003b705cfSriastradh					       SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE),
163103b705cfSriastradh			       NO_BLEND,
163203b705cfSriastradh			       is_planar_fourcc(frame->id) ?
163303b705cfSriastradh			       GEN6_WM_KERNEL_VIDEO_PLANAR :
163403b705cfSriastradh			       GEN6_WM_KERNEL_VIDEO_PACKED,
163503b705cfSriastradh			       2);
163603b705cfSriastradh	tmp.priv = frame;
163703b705cfSriastradh
163803b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
163903b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL)) {
164003b705cfSriastradh		kgem_submit(&sna->kgem);
164103b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL));
164203b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
164303b705cfSriastradh	}
164403b705cfSriastradh
164503b705cfSriastradh	gen6_emit_video_state(sna, &tmp);
164603b705cfSriastradh	gen6_align_vertex(sna, &tmp);
164703b705cfSriastradh
164803b705cfSriastradh	/* Set up the offset for translating from the given region (in screen
164903b705cfSriastradh	 * coordinates) to the backing pixmap.
165003b705cfSriastradh	 */
165103b705cfSriastradh#ifdef COMPOSITE
165203b705cfSriastradh	pix_xoff = -pixmap->screen_x + pixmap->drawable.x;
165303b705cfSriastradh	pix_yoff = -pixmap->screen_y + pixmap->drawable.y;
165403b705cfSriastradh#else
165503b705cfSriastradh	pix_xoff = 0;
165603b705cfSriastradh	pix_yoff = 0;
165703b705cfSriastradh#endif
165803b705cfSriastradh
165903b705cfSriastradh	src_scale_x = (float)src_width / dst_width / frame->width;
166003b705cfSriastradh	src_offset_x = (float)frame->src.x1 / frame->width - dstRegion->extents.x1 * src_scale_x;
166103b705cfSriastradh
166203b705cfSriastradh	src_scale_y = (float)src_height / dst_height / frame->height;
166303b705cfSriastradh	src_offset_y = (float)frame->src.y1 / frame->height - dstRegion->extents.y1 * src_scale_y;
166403b705cfSriastradh
166503b705cfSriastradh	box = REGION_RECTS(dstRegion);
166603b705cfSriastradh	nbox = REGION_NUM_RECTS(dstRegion);
166703b705cfSriastradh	while (nbox--) {
166803b705cfSriastradh		BoxRec r;
166903b705cfSriastradh
167003b705cfSriastradh		r.x1 = box->x1 + pix_xoff;
167103b705cfSriastradh		r.x2 = box->x2 + pix_xoff;
167203b705cfSriastradh		r.y1 = box->y1 + pix_yoff;
167303b705cfSriastradh		r.y2 = box->y2 + pix_yoff;
167403b705cfSriastradh
167503b705cfSriastradh		gen6_get_rectangles(sna, &tmp, 1, gen6_emit_video_state);
167603b705cfSriastradh
167703b705cfSriastradh		OUT_VERTEX(r.x2, r.y2);
167803b705cfSriastradh		OUT_VERTEX_F(box->x2 * src_scale_x + src_offset_x);
167903b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
168003b705cfSriastradh
168103b705cfSriastradh		OUT_VERTEX(r.x1, r.y2);
168203b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
168303b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
168403b705cfSriastradh
168503b705cfSriastradh		OUT_VERTEX(r.x1, r.y1);
168603b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
168703b705cfSriastradh		OUT_VERTEX_F(box->y1 * src_scale_y + src_offset_y);
168803b705cfSriastradh
168903b705cfSriastradh		if (!DAMAGE_IS_ALL(priv->gpu_damage)) {
169003b705cfSriastradh			sna_damage_add_box(&priv->gpu_damage, &r);
169103b705cfSriastradh			sna_damage_subtract_box(&priv->cpu_damage, &r);
169203b705cfSriastradh		}
169303b705cfSriastradh		box++;
169403b705cfSriastradh	}
169503b705cfSriastradh
169603b705cfSriastradh	gen4_vertex_flush(sna);
169703b705cfSriastradh	return true;
169803b705cfSriastradh}
169903b705cfSriastradh
170003b705cfSriastradhstatic int
170103b705cfSriastradhgen6_composite_picture(struct sna *sna,
170203b705cfSriastradh		       PicturePtr picture,
170303b705cfSriastradh		       struct sna_composite_channel *channel,
170403b705cfSriastradh		       int x, int y,
170503b705cfSriastradh		       int w, int h,
170603b705cfSriastradh		       int dst_x, int dst_y,
170703b705cfSriastradh		       bool precise)
170803b705cfSriastradh{
170903b705cfSriastradh	PixmapPtr pixmap;
171003b705cfSriastradh	uint32_t color;
171103b705cfSriastradh	int16_t dx, dy;
171203b705cfSriastradh
171303b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d), dst=(%d, %d)\n",
171403b705cfSriastradh	     __FUNCTION__, x, y, w, h, dst_x, dst_y));
171503b705cfSriastradh
171603b705cfSriastradh	channel->is_solid = false;
171703b705cfSriastradh	channel->card_format = -1;
171803b705cfSriastradh
171903b705cfSriastradh	if (sna_picture_is_solid(picture, &color))
172003b705cfSriastradh		return gen4_channel_init_solid(sna, channel, color);
172103b705cfSriastradh
172203b705cfSriastradh	if (picture->pDrawable == NULL) {
172303b705cfSriastradh		int ret;
172403b705cfSriastradh
172503b705cfSriastradh		if (picture->pSourcePict->type == SourcePictTypeLinear)
172603b705cfSriastradh			return gen4_channel_init_linear(sna, picture, channel,
172703b705cfSriastradh							x, y,
172803b705cfSriastradh							w, h,
172903b705cfSriastradh							dst_x, dst_y);
173003b705cfSriastradh
173103b705cfSriastradh		DBG(("%s -- fixup, gradient\n", __FUNCTION__));
173203b705cfSriastradh		ret = -1;
173303b705cfSriastradh		if (!precise)
173403b705cfSriastradh			ret = sna_render_picture_approximate_gradient(sna, picture, channel,
173503b705cfSriastradh								      x, y, w, h, dst_x, dst_y);
173603b705cfSriastradh		if (ret == -1)
173703b705cfSriastradh			ret = sna_render_picture_fixup(sna, picture, channel,
173803b705cfSriastradh						       x, y, w, h, dst_x, dst_y);
173903b705cfSriastradh		return ret;
174003b705cfSriastradh	}
174103b705cfSriastradh
174203b705cfSriastradh	if (picture->alphaMap) {
174303b705cfSriastradh		DBG(("%s -- fixup, alphamap\n", __FUNCTION__));
174403b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
174503b705cfSriastradh						x, y, w, h, dst_x, dst_y);
174603b705cfSriastradh	}
174703b705cfSriastradh
174803b705cfSriastradh	if (!gen6_check_repeat(picture))
174903b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
175003b705cfSriastradh						x, y, w, h, dst_x, dst_y);
175103b705cfSriastradh
175203b705cfSriastradh	if (!gen6_check_filter(picture))
175303b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
175403b705cfSriastradh						x, y, w, h, dst_x, dst_y);
175503b705cfSriastradh
175603b705cfSriastradh	channel->repeat = picture->repeat ? picture->repeatType : RepeatNone;
175703b705cfSriastradh	channel->filter = picture->filter;
175803b705cfSriastradh
175903b705cfSriastradh	pixmap = get_drawable_pixmap(picture->pDrawable);
176003b705cfSriastradh	get_drawable_deltas(picture->pDrawable, pixmap, &dx, &dy);
176103b705cfSriastradh
176203b705cfSriastradh	x += dx + picture->pDrawable->x;
176303b705cfSriastradh	y += dy + picture->pDrawable->y;
176403b705cfSriastradh
176503b705cfSriastradh	channel->is_affine = sna_transform_is_affine(picture->transform);
176603b705cfSriastradh	if (sna_transform_is_integer_translation(picture->transform, &dx, &dy)) {
176703b705cfSriastradh		DBG(("%s: integer translation (%d, %d), removing\n",
176803b705cfSriastradh		     __FUNCTION__, dx, dy));
176903b705cfSriastradh		x += dx;
177003b705cfSriastradh		y += dy;
177103b705cfSriastradh		channel->transform = NULL;
177203b705cfSriastradh		channel->filter = PictFilterNearest;
177303b705cfSriastradh	} else
177403b705cfSriastradh		channel->transform = picture->transform;
177503b705cfSriastradh
177603b705cfSriastradh	channel->pict_format = picture->format;
177703b705cfSriastradh	channel->card_format = gen6_get_card_format(picture->format);
177803b705cfSriastradh	if (channel->card_format == (unsigned)-1)
177903b705cfSriastradh		return sna_render_picture_convert(sna, picture, channel, pixmap,
178003b705cfSriastradh						  x, y, w, h, dst_x, dst_y,
178103b705cfSriastradh						  false);
178203b705cfSriastradh
178303b705cfSriastradh	if (too_large(pixmap->drawable.width, pixmap->drawable.height)) {
178403b705cfSriastradh		DBG(("%s: extracting from pixmap %dx%d\n", __FUNCTION__,
178503b705cfSriastradh		     pixmap->drawable.width, pixmap->drawable.height));
178603b705cfSriastradh		return sna_render_picture_extract(sna, picture, channel,
178703b705cfSriastradh						  x, y, w, h, dst_x, dst_y);
178803b705cfSriastradh	}
178903b705cfSriastradh
179003b705cfSriastradh	return sna_render_pixmap_bo(sna, channel, pixmap,
179103b705cfSriastradh				    x, y, w, h, dst_x, dst_y);
179203b705cfSriastradh}
179303b705cfSriastradh
179403b705cfSriastradhinline static void gen6_composite_channel_convert(struct sna_composite_channel *channel)
179503b705cfSriastradh{
179603b705cfSriastradh	channel->repeat = gen6_repeat(channel->repeat);
179703b705cfSriastradh	channel->filter = gen6_filter(channel->filter);
179803b705cfSriastradh	if (channel->card_format == (unsigned)-1)
179903b705cfSriastradh		channel->card_format = gen6_get_card_format(channel->pict_format);
180003b705cfSriastradh	assert(channel->card_format != (unsigned)-1);
180103b705cfSriastradh}
180203b705cfSriastradh
180303b705cfSriastradhstatic void gen6_render_composite_done(struct sna *sna,
180403b705cfSriastradh				       const struct sna_composite_op *op)
180503b705cfSriastradh{
180603b705cfSriastradh	DBG(("%s\n", __FUNCTION__));
180703b705cfSriastradh
180803b705cfSriastradh	assert(!sna->render.active);
180903b705cfSriastradh	if (sna->render.vertex_offset) {
181003b705cfSriastradh		gen4_vertex_flush(sna);
181103b705cfSriastradh		gen6_magic_ca_pass(sna, op);
181203b705cfSriastradh	}
181303b705cfSriastradh
181403b705cfSriastradh	if (op->mask.bo)
181503b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->mask.bo);
181603b705cfSriastradh	if (op->src.bo)
181703b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->src.bo);
181803b705cfSriastradh
181903b705cfSriastradh	sna_render_composite_redirect_done(sna, op);
182003b705cfSriastradh}
182103b705cfSriastradh
182203b705cfSriastradhstatic bool
182303b705cfSriastradhgen6_composite_set_target(struct sna *sna,
182403b705cfSriastradh			  struct sna_composite_op *op,
182503b705cfSriastradh			  PicturePtr dst,
182603b705cfSriastradh			  int x, int y, int w, int h,
182703b705cfSriastradh			  bool partial)
182803b705cfSriastradh{
182903b705cfSriastradh	BoxRec box;
183003b705cfSriastradh
183103b705cfSriastradh	op->dst.pixmap = get_drawable_pixmap(dst->pDrawable);
183203b705cfSriastradh	op->dst.format = dst->format;
183303b705cfSriastradh	op->dst.width = op->dst.pixmap->drawable.width;
183403b705cfSriastradh	op->dst.height = op->dst.pixmap->drawable.height;
183503b705cfSriastradh
183603b705cfSriastradh	if (w && h) {
183703b705cfSriastradh		box.x1 = x;
183803b705cfSriastradh		box.y1 = y;
183903b705cfSriastradh		box.x2 = x + w;
184003b705cfSriastradh		box.y2 = y + h;
184103b705cfSriastradh	} else
184203b705cfSriastradh		sna_render_picture_extents(dst, &box);
184303b705cfSriastradh
184403b705cfSriastradh	op->dst.bo = sna_drawable_use_bo(dst->pDrawable,
184503b705cfSriastradh					 PREFER_GPU | FORCE_GPU | RENDER_GPU,
184603b705cfSriastradh					 &box, &op->damage);
184703b705cfSriastradh	if (op->dst.bo == NULL)
184803b705cfSriastradh		return false;
184903b705cfSriastradh
185003b705cfSriastradh	get_drawable_deltas(dst->pDrawable, op->dst.pixmap,
185103b705cfSriastradh			    &op->dst.x, &op->dst.y);
185203b705cfSriastradh
185303b705cfSriastradh	DBG(("%s: pixmap=%p, format=%08x, size=%dx%d, pitch=%d, delta=(%d,%d),damage=%p\n",
185403b705cfSriastradh	     __FUNCTION__,
185503b705cfSriastradh	     op->dst.pixmap, (int)op->dst.format,
185603b705cfSriastradh	     op->dst.width, op->dst.height,
185703b705cfSriastradh	     op->dst.bo->pitch,
185803b705cfSriastradh	     op->dst.x, op->dst.y,
185903b705cfSriastradh	     op->damage ? *op->damage : (void *)-1));
186003b705cfSriastradh
186103b705cfSriastradh	assert(op->dst.bo->proxy == NULL);
186203b705cfSriastradh
186303b705cfSriastradh	if (too_large(op->dst.width, op->dst.height) &&
186403b705cfSriastradh	    !sna_render_composite_redirect(sna, op, x, y, w, h, partial))
186503b705cfSriastradh		return false;
186603b705cfSriastradh
186703b705cfSriastradh	return true;
186803b705cfSriastradh}
186903b705cfSriastradh
187003b705cfSriastradhinline static bool can_switch_to_blt(struct sna *sna,
187103b705cfSriastradh				     struct kgem_bo *bo,
187203b705cfSriastradh				     unsigned flags)
187303b705cfSriastradh{
187403b705cfSriastradh	if (sna->kgem.ring != KGEM_RENDER)
187503b705cfSriastradh		return true;
187603b705cfSriastradh
187703b705cfSriastradh	if (NO_RING_SWITCH)
187803b705cfSriastradh		return false;
187903b705cfSriastradh
188003b705cfSriastradh	if (!sna->kgem.has_semaphores)
188103b705cfSriastradh		return false;
188203b705cfSriastradh
188303b705cfSriastradh	if (flags & COPY_LAST)
188403b705cfSriastradh		return true;
188503b705cfSriastradh
188603b705cfSriastradh	if (bo && RQ_IS_BLT(bo->rq))
188703b705cfSriastradh		return true;
188803b705cfSriastradh
188903b705cfSriastradh	return kgem_ring_is_idle(&sna->kgem, KGEM_BLT);
189003b705cfSriastradh}
189103b705cfSriastradh
189203b705cfSriastradhstatic inline bool untiled_tlb_miss(struct kgem_bo *bo)
189303b705cfSriastradh{
189403b705cfSriastradh	return bo->tiling == I915_TILING_NONE && bo->pitch >= 4096;
189503b705cfSriastradh}
189603b705cfSriastradh
189703b705cfSriastradhstatic int prefer_blt_bo(struct sna *sna, struct kgem_bo *bo)
189803b705cfSriastradh{
189903b705cfSriastradh	if (RQ_IS_BLT(bo->rq))
190003b705cfSriastradh		return true;
190103b705cfSriastradh
190203b705cfSriastradh	return bo->tiling == I915_TILING_NONE || bo->scanout;
190303b705cfSriastradh}
190403b705cfSriastradh
190503b705cfSriastradhinline static bool prefer_blt_ring(struct sna *sna,
190603b705cfSriastradh				   struct kgem_bo *bo,
190703b705cfSriastradh				   unsigned flags)
190803b705cfSriastradh{
190903b705cfSriastradh	return can_switch_to_blt(sna, bo, flags);
191003b705cfSriastradh}
191103b705cfSriastradh
191203b705cfSriastradhstatic bool
191303b705cfSriastradhtry_blt(struct sna *sna,
191403b705cfSriastradh	PicturePtr dst, PicturePtr src,
191503b705cfSriastradh	int width, int height)
191603b705cfSriastradh{
191703b705cfSriastradh	struct kgem_bo *bo;
191803b705cfSriastradh
191903b705cfSriastradh	bo = __sna_drawable_peek_bo(dst->pDrawable);
192003b705cfSriastradh	if (bo == NULL)
192103b705cfSriastradh		return true;
192203b705cfSriastradh
192303b705cfSriastradh	if (bo->rq)
192403b705cfSriastradh		return RQ_IS_BLT(bo->rq);
192503b705cfSriastradh
192603b705cfSriastradh	if (sna->kgem.ring == KGEM_BLT) {
192703b705cfSriastradh		DBG(("%s: already performing BLT\n", __FUNCTION__));
192803b705cfSriastradh		return true;
192903b705cfSriastradh	}
193003b705cfSriastradh
193103b705cfSriastradh	if (too_large(width, height)) {
193203b705cfSriastradh		DBG(("%s: operation too large for 3D pipe (%d, %d)\n",
193303b705cfSriastradh		     __FUNCTION__, width, height));
193403b705cfSriastradh		return true;
193503b705cfSriastradh	}
193603b705cfSriastradh
193703b705cfSriastradh	if (sna_picture_is_solid(src, NULL) && can_switch_to_blt(sna, NULL, 0))
193803b705cfSriastradh		return true;
193903b705cfSriastradh
194003b705cfSriastradh	return false;
194103b705cfSriastradh}
194203b705cfSriastradh
194303b705cfSriastradhstatic bool
194403b705cfSriastradhcheck_gradient(PicturePtr picture, bool precise)
194503b705cfSriastradh{
194603b705cfSriastradh	if (picture->pDrawable)
194703b705cfSriastradh		return false;
194803b705cfSriastradh
194903b705cfSriastradh	switch (picture->pSourcePict->type) {
195003b705cfSriastradh	case SourcePictTypeSolidFill:
195103b705cfSriastradh	case SourcePictTypeLinear:
195203b705cfSriastradh		return false;
195303b705cfSriastradh	default:
195403b705cfSriastradh		return precise;
195503b705cfSriastradh	}
195603b705cfSriastradh}
195703b705cfSriastradh
195803b705cfSriastradhstatic bool
195903b705cfSriastradhhas_alphamap(PicturePtr p)
196003b705cfSriastradh{
196103b705cfSriastradh	return p->alphaMap != NULL;
196203b705cfSriastradh}
196303b705cfSriastradh
196403b705cfSriastradhstatic bool
196503b705cfSriastradhneed_upload(PicturePtr p)
196603b705cfSriastradh{
196703b705cfSriastradh	return p->pDrawable && unattached(p->pDrawable) && untransformed(p);
196803b705cfSriastradh}
196903b705cfSriastradh
197003b705cfSriastradhstatic bool
197103b705cfSriastradhsource_is_busy(PixmapPtr pixmap)
197203b705cfSriastradh{
197303b705cfSriastradh	struct sna_pixmap *priv = sna_pixmap(pixmap);
197403b705cfSriastradh	if (priv == NULL || priv->clear)
197503b705cfSriastradh		return false;
197603b705cfSriastradh
197703b705cfSriastradh	if (priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo))
197803b705cfSriastradh		return true;
197903b705cfSriastradh
198003b705cfSriastradh	if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
198103b705cfSriastradh		return true;
198203b705cfSriastradh
198303b705cfSriastradh	return priv->gpu_damage && !priv->cpu_damage;
198403b705cfSriastradh}
198503b705cfSriastradh
198603b705cfSriastradhstatic bool
198703b705cfSriastradhsource_fallback(PicturePtr p, PixmapPtr pixmap, bool precise)
198803b705cfSriastradh{
198903b705cfSriastradh	if (sna_picture_is_solid(p, NULL))
199003b705cfSriastradh		return false;
199103b705cfSriastradh
199203b705cfSriastradh	if (p->pSourcePict)
199303b705cfSriastradh		return check_gradient(p, precise);
199403b705cfSriastradh
199503b705cfSriastradh	if (!gen6_check_repeat(p) || !gen6_check_format(p->format))
199603b705cfSriastradh		return true;
199703b705cfSriastradh
199803b705cfSriastradh	if (pixmap && source_is_busy(pixmap))
199903b705cfSriastradh		return false;
200003b705cfSriastradh
200103b705cfSriastradh	return has_alphamap(p) || !gen6_check_filter(p) || need_upload(p);
200203b705cfSriastradh}
200303b705cfSriastradh
200403b705cfSriastradhstatic bool
200503b705cfSriastradhgen6_composite_fallback(struct sna *sna,
200603b705cfSriastradh			PicturePtr src,
200703b705cfSriastradh			PicturePtr mask,
200803b705cfSriastradh			PicturePtr dst)
200903b705cfSriastradh{
201003b705cfSriastradh	PixmapPtr src_pixmap;
201103b705cfSriastradh	PixmapPtr mask_pixmap;
201203b705cfSriastradh	PixmapPtr dst_pixmap;
201303b705cfSriastradh	bool src_fallback, mask_fallback;
201403b705cfSriastradh
201503b705cfSriastradh	if (!gen6_check_dst_format(dst->format)) {
201603b705cfSriastradh		DBG(("%s: unknown destination format: %d\n",
201703b705cfSriastradh		     __FUNCTION__, dst->format));
201803b705cfSriastradh		return true;
201903b705cfSriastradh	}
202003b705cfSriastradh
202103b705cfSriastradh	dst_pixmap = get_drawable_pixmap(dst->pDrawable);
202203b705cfSriastradh
202303b705cfSriastradh	src_pixmap = src->pDrawable ? get_drawable_pixmap(src->pDrawable) : NULL;
202403b705cfSriastradh	src_fallback = source_fallback(src, src_pixmap,
202503b705cfSriastradh				       dst->polyMode == PolyModePrecise);
202603b705cfSriastradh
202703b705cfSriastradh	if (mask) {
202803b705cfSriastradh		mask_pixmap = mask->pDrawable ? get_drawable_pixmap(mask->pDrawable) : NULL;
202903b705cfSriastradh		mask_fallback = source_fallback(mask, mask_pixmap,
203003b705cfSriastradh						dst->polyMode == PolyModePrecise);
203103b705cfSriastradh	} else {
203203b705cfSriastradh		mask_pixmap = NULL;
203303b705cfSriastradh		mask_fallback = false;
203403b705cfSriastradh	}
203503b705cfSriastradh
203603b705cfSriastradh	/* If we are using the destination as a source and need to
203703b705cfSriastradh	 * readback in order to upload the source, do it all
203803b705cfSriastradh	 * on the cpu.
203903b705cfSriastradh	 */
204003b705cfSriastradh	if (src_pixmap == dst_pixmap && src_fallback) {
204103b705cfSriastradh		DBG(("%s: src is dst and will fallback\n",__FUNCTION__));
204203b705cfSriastradh		return true;
204303b705cfSriastradh	}
204403b705cfSriastradh	if (mask_pixmap == dst_pixmap && mask_fallback) {
204503b705cfSriastradh		DBG(("%s: mask is dst and will fallback\n",__FUNCTION__));
204603b705cfSriastradh		return true;
204703b705cfSriastradh	}
204803b705cfSriastradh
204903b705cfSriastradh	/* If anything is on the GPU, push everything out to the GPU */
205003b705cfSriastradh	if (dst_use_gpu(dst_pixmap)) {
205103b705cfSriastradh		DBG(("%s: dst is already on the GPU, try to use GPU\n",
205203b705cfSriastradh		     __FUNCTION__));
205303b705cfSriastradh		return false;
205403b705cfSriastradh	}
205503b705cfSriastradh
205603b705cfSriastradh	if (src_pixmap && !src_fallback) {
205703b705cfSriastradh		DBG(("%s: src is already on the GPU, try to use GPU\n",
205803b705cfSriastradh		     __FUNCTION__));
205903b705cfSriastradh		return false;
206003b705cfSriastradh	}
206103b705cfSriastradh	if (mask_pixmap && !mask_fallback) {
206203b705cfSriastradh		DBG(("%s: mask is already on the GPU, try to use GPU\n",
206303b705cfSriastradh		     __FUNCTION__));
206403b705cfSriastradh		return false;
206503b705cfSriastradh	}
206603b705cfSriastradh
206703b705cfSriastradh	/* However if the dst is not on the GPU and we need to
206803b705cfSriastradh	 * render one of the sources using the CPU, we may
206903b705cfSriastradh	 * as well do the entire operation in place onthe CPU.
207003b705cfSriastradh	 */
207103b705cfSriastradh	if (src_fallback) {
207203b705cfSriastradh		DBG(("%s: dst is on the CPU and src will fallback\n",
207303b705cfSriastradh		     __FUNCTION__));
207403b705cfSriastradh		return true;
207503b705cfSriastradh	}
207603b705cfSriastradh
207703b705cfSriastradh	if (mask && mask_fallback) {
207803b705cfSriastradh		DBG(("%s: dst is on the CPU and mask will fallback\n",
207903b705cfSriastradh		     __FUNCTION__));
208003b705cfSriastradh		return true;
208103b705cfSriastradh	}
208203b705cfSriastradh
208303b705cfSriastradh	if (too_large(dst_pixmap->drawable.width,
208403b705cfSriastradh		      dst_pixmap->drawable.height) &&
208503b705cfSriastradh	    dst_is_cpu(dst_pixmap)) {
208603b705cfSriastradh		DBG(("%s: dst is on the CPU and too large\n", __FUNCTION__));
208703b705cfSriastradh		return true;
208803b705cfSriastradh	}
208903b705cfSriastradh
209003b705cfSriastradh	DBG(("%s: dst is not on the GPU and the operation should not fallback\n",
209103b705cfSriastradh	     __FUNCTION__));
209203b705cfSriastradh	return dst_use_cpu(dst_pixmap);
209303b705cfSriastradh}
209403b705cfSriastradh
209503b705cfSriastradhstatic int
209603b705cfSriastradhreuse_source(struct sna *sna,
209703b705cfSriastradh	     PicturePtr src, struct sna_composite_channel *sc, int src_x, int src_y,
209803b705cfSriastradh	     PicturePtr mask, struct sna_composite_channel *mc, int msk_x, int msk_y)
209903b705cfSriastradh{
210003b705cfSriastradh	uint32_t color;
210103b705cfSriastradh
210203b705cfSriastradh	if (src_x != msk_x || src_y != msk_y)
210303b705cfSriastradh		return false;
210403b705cfSriastradh
210503b705cfSriastradh	if (src == mask) {
210603b705cfSriastradh		DBG(("%s: mask is source\n", __FUNCTION__));
210703b705cfSriastradh		*mc = *sc;
210803b705cfSriastradh		mc->bo = kgem_bo_reference(mc->bo);
210903b705cfSriastradh		return true;
211003b705cfSriastradh	}
211103b705cfSriastradh
211203b705cfSriastradh	if (sna_picture_is_solid(mask, &color))
211303b705cfSriastradh		return gen4_channel_init_solid(sna, mc, color);
211403b705cfSriastradh
211503b705cfSriastradh	if (sc->is_solid)
211603b705cfSriastradh		return false;
211703b705cfSriastradh
211803b705cfSriastradh	if (src->pDrawable == NULL || mask->pDrawable != src->pDrawable)
211903b705cfSriastradh		return false;
212003b705cfSriastradh
212103b705cfSriastradh	DBG(("%s: mask reuses source drawable\n", __FUNCTION__));
212203b705cfSriastradh
212303b705cfSriastradh	if (!sna_transform_equal(src->transform, mask->transform))
212403b705cfSriastradh		return false;
212503b705cfSriastradh
212603b705cfSriastradh	if (!sna_picture_alphamap_equal(src, mask))
212703b705cfSriastradh		return false;
212803b705cfSriastradh
212903b705cfSriastradh	if (!gen6_check_repeat(mask))
213003b705cfSriastradh		return false;
213103b705cfSriastradh
213203b705cfSriastradh	if (!gen6_check_filter(mask))
213303b705cfSriastradh		return false;
213403b705cfSriastradh
213503b705cfSriastradh	if (!gen6_check_format(mask->format))
213603b705cfSriastradh		return false;
213703b705cfSriastradh
213803b705cfSriastradh	DBG(("%s: reusing source channel for mask with a twist\n",
213903b705cfSriastradh	     __FUNCTION__));
214003b705cfSriastradh
214103b705cfSriastradh	*mc = *sc;
214203b705cfSriastradh	mc->repeat = gen6_repeat(mask->repeat ? mask->repeatType : RepeatNone);
214303b705cfSriastradh	mc->filter = gen6_filter(mask->filter);
214403b705cfSriastradh	mc->pict_format = mask->format;
214503b705cfSriastradh	mc->card_format = gen6_get_card_format(mask->format);
214603b705cfSriastradh	mc->bo = kgem_bo_reference(mc->bo);
214703b705cfSriastradh	return true;
214803b705cfSriastradh}
214903b705cfSriastradh
215003b705cfSriastradhstatic bool
215103b705cfSriastradhprefer_blt_composite(struct sna *sna, struct sna_composite_op *tmp)
215203b705cfSriastradh{
215303b705cfSriastradh	if (untiled_tlb_miss(tmp->dst.bo) ||
215403b705cfSriastradh	    untiled_tlb_miss(tmp->src.bo))
215503b705cfSriastradh		return true;
215603b705cfSriastradh
215703b705cfSriastradh	if (kgem_bo_is_render(tmp->dst.bo) ||
215803b705cfSriastradh	    kgem_bo_is_render(tmp->src.bo))
215903b705cfSriastradh		return false;
216003b705cfSriastradh
216103b705cfSriastradh	if (!prefer_blt_ring(sna, tmp->dst.bo, 0))
216203b705cfSriastradh		return false;
216303b705cfSriastradh
216403b705cfSriastradh	return prefer_blt_bo(sna, tmp->dst.bo) || prefer_blt_bo(sna, tmp->src.bo);
216503b705cfSriastradh}
216603b705cfSriastradh
216703b705cfSriastradhstatic bool
216803b705cfSriastradhgen6_render_composite(struct sna *sna,
216903b705cfSriastradh		      uint8_t op,
217003b705cfSriastradh		      PicturePtr src,
217103b705cfSriastradh		      PicturePtr mask,
217203b705cfSriastradh		      PicturePtr dst,
217303b705cfSriastradh		      int16_t src_x, int16_t src_y,
217403b705cfSriastradh		      int16_t msk_x, int16_t msk_y,
217503b705cfSriastradh		      int16_t dst_x, int16_t dst_y,
217603b705cfSriastradh		      int16_t width, int16_t height,
217703b705cfSriastradh		      struct sna_composite_op *tmp)
217803b705cfSriastradh{
217903b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op))
218003b705cfSriastradh		return false;
218103b705cfSriastradh
218203b705cfSriastradh	DBG(("%s: %dx%d, current mode=%d\n", __FUNCTION__,
218303b705cfSriastradh	     width, height, sna->kgem.ring));
218403b705cfSriastradh
218503b705cfSriastradh	if (mask == NULL &&
218603b705cfSriastradh	    try_blt(sna, dst, src, width, height) &&
218703b705cfSriastradh	    sna_blt_composite(sna, op,
218803b705cfSriastradh			      src, dst,
218903b705cfSriastradh			      src_x, src_y,
219003b705cfSriastradh			      dst_x, dst_y,
219103b705cfSriastradh			      width, height,
219203b705cfSriastradh			      tmp, false))
219303b705cfSriastradh		return true;
219403b705cfSriastradh
219503b705cfSriastradh	if (gen6_composite_fallback(sna, src, mask, dst))
219603b705cfSriastradh		return false;
219703b705cfSriastradh
219803b705cfSriastradh	if (need_tiling(sna, width, height))
219903b705cfSriastradh		return sna_tiling_composite(op, src, mask, dst,
220003b705cfSriastradh					    src_x, src_y,
220103b705cfSriastradh					    msk_x, msk_y,
220203b705cfSriastradh					    dst_x, dst_y,
220303b705cfSriastradh					    width, height,
220403b705cfSriastradh					    tmp);
220503b705cfSriastradh
220603b705cfSriastradh	if (op == PictOpClear)
220703b705cfSriastradh		op = PictOpSrc;
220803b705cfSriastradh	tmp->op = op;
220903b705cfSriastradh	if (!gen6_composite_set_target(sna, tmp, dst,
221003b705cfSriastradh				       dst_x, dst_y, width, height,
221103b705cfSriastradh				       op > PictOpSrc || dst->pCompositeClip->data))
221203b705cfSriastradh		return false;
221303b705cfSriastradh
221403b705cfSriastradh	switch (gen6_composite_picture(sna, src, &tmp->src,
221503b705cfSriastradh				       src_x, src_y,
221603b705cfSriastradh				       width, height,
221703b705cfSriastradh				       dst_x, dst_y,
221803b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
221903b705cfSriastradh	case -1:
222003b705cfSriastradh		goto cleanup_dst;
222103b705cfSriastradh	case 0:
222203b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->src, 0))
222303b705cfSriastradh			goto cleanup_dst;
222403b705cfSriastradh		/* fall through to fixup */
222503b705cfSriastradh	case 1:
222603b705cfSriastradh		/* Did we just switch rings to prepare the source? */
222703b705cfSriastradh		if (mask == NULL &&
222803b705cfSriastradh		    prefer_blt_composite(sna, tmp) &&
222903b705cfSriastradh		    sna_blt_composite__convert(sna,
223003b705cfSriastradh					       dst_x, dst_y, width, height,
223103b705cfSriastradh					       tmp))
223203b705cfSriastradh			return true;
223303b705cfSriastradh
223403b705cfSriastradh		gen6_composite_channel_convert(&tmp->src);
223503b705cfSriastradh		break;
223603b705cfSriastradh	}
223703b705cfSriastradh
223803b705cfSriastradh	tmp->is_affine = tmp->src.is_affine;
223903b705cfSriastradh	tmp->has_component_alpha = false;
224003b705cfSriastradh	tmp->need_magic_ca_pass = false;
224103b705cfSriastradh
224203b705cfSriastradh	tmp->mask.bo = NULL;
224303b705cfSriastradh	tmp->mask.filter = SAMPLER_FILTER_NEAREST;
224403b705cfSriastradh	tmp->mask.repeat = SAMPLER_EXTEND_NONE;
224503b705cfSriastradh
224603b705cfSriastradh	if (mask) {
224703b705cfSriastradh		if (mask->componentAlpha && PICT_FORMAT_RGB(mask->format)) {
224803b705cfSriastradh			tmp->has_component_alpha = true;
224903b705cfSriastradh
225003b705cfSriastradh			/* Check if it's component alpha that relies on a source alpha and on
225103b705cfSriastradh			 * the source value.  We can only get one of those into the single
225203b705cfSriastradh			 * source value that we get to blend with.
225303b705cfSriastradh			 */
225403b705cfSriastradh			if (gen6_blend_op[op].src_alpha &&
225503b705cfSriastradh			    (gen6_blend_op[op].src_blend != GEN6_BLENDFACTOR_ZERO)) {
225603b705cfSriastradh				if (op != PictOpOver)
225703b705cfSriastradh					goto cleanup_src;
225803b705cfSriastradh
225903b705cfSriastradh				tmp->need_magic_ca_pass = true;
226003b705cfSriastradh				tmp->op = PictOpOutReverse;
226103b705cfSriastradh			}
226203b705cfSriastradh		}
226303b705cfSriastradh
226403b705cfSriastradh		if (!reuse_source(sna,
226503b705cfSriastradh				  src, &tmp->src, src_x, src_y,
226603b705cfSriastradh				  mask, &tmp->mask, msk_x, msk_y)) {
226703b705cfSriastradh			switch (gen6_composite_picture(sna, mask, &tmp->mask,
226803b705cfSriastradh						       msk_x, msk_y,
226903b705cfSriastradh						       width, height,
227003b705cfSriastradh						       dst_x, dst_y,
227103b705cfSriastradh						       dst->polyMode == PolyModePrecise)) {
227203b705cfSriastradh			case -1:
227303b705cfSriastradh				goto cleanup_src;
227403b705cfSriastradh			case 0:
227503b705cfSriastradh				if (!gen4_channel_init_solid(sna, &tmp->mask, 0))
227603b705cfSriastradh					goto cleanup_src;
227703b705cfSriastradh				/* fall through to fixup */
227803b705cfSriastradh			case 1:
227903b705cfSriastradh				gen6_composite_channel_convert(&tmp->mask);
228003b705cfSriastradh				break;
228103b705cfSriastradh			}
228203b705cfSriastradh		}
228303b705cfSriastradh
228403b705cfSriastradh		tmp->is_affine &= tmp->mask.is_affine;
228503b705cfSriastradh	}
228603b705cfSriastradh
228703b705cfSriastradh	tmp->u.gen6.flags =
228803b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(tmp->src.filter,
228903b705cfSriastradh					      tmp->src.repeat,
229003b705cfSriastradh					      tmp->mask.filter,
229103b705cfSriastradh					      tmp->mask.repeat),
229203b705cfSriastradh			       gen6_get_blend(tmp->op,
229303b705cfSriastradh					      tmp->has_component_alpha,
229403b705cfSriastradh					      tmp->dst.format),
229503b705cfSriastradh			       gen6_choose_composite_kernel(tmp->op,
229603b705cfSriastradh							    tmp->mask.bo != NULL,
229703b705cfSriastradh							    tmp->has_component_alpha,
229803b705cfSriastradh							    tmp->is_affine),
229903b705cfSriastradh			       gen4_choose_composite_emitter(sna, tmp));
230003b705cfSriastradh
230103b705cfSriastradh	tmp->blt   = gen6_render_composite_blt;
230203b705cfSriastradh	tmp->box   = gen6_render_composite_box;
230303b705cfSriastradh	tmp->boxes = gen6_render_composite_boxes__blt;
230403b705cfSriastradh	if (tmp->emit_boxes) {
230503b705cfSriastradh		tmp->boxes = gen6_render_composite_boxes;
230603b705cfSriastradh		tmp->thread_boxes = gen6_render_composite_boxes__thread;
230703b705cfSriastradh	}
230803b705cfSriastradh	tmp->done  = gen6_render_composite_done;
230903b705cfSriastradh
231003b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->dst.bo);
231103b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
231203b705cfSriastradh			   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
231303b705cfSriastradh			   NULL)) {
231403b705cfSriastradh		kgem_submit(&sna->kgem);
231503b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
231603b705cfSriastradh				   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
231703b705cfSriastradh				   NULL))
231803b705cfSriastradh			goto cleanup_mask;
231903b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
232003b705cfSriastradh	}
232103b705cfSriastradh
232203b705cfSriastradh	gen6_emit_composite_state(sna, tmp);
232303b705cfSriastradh	gen6_align_vertex(sna, tmp);
232403b705cfSriastradh	return true;
232503b705cfSriastradh
232603b705cfSriastradhcleanup_mask:
232703b705cfSriastradh	if (tmp->mask.bo)
232803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->mask.bo);
232903b705cfSriastradhcleanup_src:
233003b705cfSriastradh	if (tmp->src.bo)
233103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->src.bo);
233203b705cfSriastradhcleanup_dst:
233303b705cfSriastradh	if (tmp->redirect.real_bo)
233403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->dst.bo);
233503b705cfSriastradh	return false;
233603b705cfSriastradh}
233703b705cfSriastradh
233803b705cfSriastradh#if !NO_COMPOSITE_SPANS
233903b705cfSriastradhfastcall static void
234003b705cfSriastradhgen6_render_composite_spans_box(struct sna *sna,
234103b705cfSriastradh				const struct sna_composite_spans_op *op,
234203b705cfSriastradh				const BoxRec *box, float opacity)
234303b705cfSriastradh{
234403b705cfSriastradh	DBG(("%s: src=+(%d, %d), opacity=%f, dst=+(%d, %d), box=(%d, %d) x (%d, %d)\n",
234503b705cfSriastradh	     __FUNCTION__,
234603b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
234703b705cfSriastradh	     opacity,
234803b705cfSriastradh	     op->base.dst.x, op->base.dst.y,
234903b705cfSriastradh	     box->x1, box->y1,
235003b705cfSriastradh	     box->x2 - box->x1,
235103b705cfSriastradh	     box->y2 - box->y1));
235203b705cfSriastradh
235303b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_composite_state);
235403b705cfSriastradh	op->prim_emit(sna, op, box, opacity);
235503b705cfSriastradh}
235603b705cfSriastradh
235703b705cfSriastradhstatic void
235803b705cfSriastradhgen6_render_composite_spans_boxes(struct sna *sna,
235903b705cfSriastradh				  const struct sna_composite_spans_op *op,
236003b705cfSriastradh				  const BoxRec *box, int nbox,
236103b705cfSriastradh				  float opacity)
236203b705cfSriastradh{
236303b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), opacity=%f, dst=+(%d, %d)\n",
236403b705cfSriastradh	     __FUNCTION__, nbox,
236503b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
236603b705cfSriastradh	     opacity,
236703b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
236803b705cfSriastradh
236903b705cfSriastradh	do {
237003b705cfSriastradh		int nbox_this_time;
237103b705cfSriastradh
237203b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
237303b705cfSriastradh						     gen6_emit_composite_state);
237403b705cfSriastradh		nbox -= nbox_this_time;
237503b705cfSriastradh
237603b705cfSriastradh		do {
237703b705cfSriastradh			DBG(("  %s: (%d, %d) x (%d, %d)\n", __FUNCTION__,
237803b705cfSriastradh			     box->x1, box->y1,
237903b705cfSriastradh			     box->x2 - box->x1,
238003b705cfSriastradh			     box->y2 - box->y1));
238103b705cfSriastradh
238203b705cfSriastradh			op->prim_emit(sna, op, box++, opacity);
238303b705cfSriastradh		} while (--nbox_this_time);
238403b705cfSriastradh	} while (nbox);
238503b705cfSriastradh}
238603b705cfSriastradh
238703b705cfSriastradhfastcall static void
238803b705cfSriastradhgen6_render_composite_spans_boxes__thread(struct sna *sna,
238903b705cfSriastradh					  const struct sna_composite_spans_op *op,
239003b705cfSriastradh					  const struct sna_opacity_box *box,
239103b705cfSriastradh					  int nbox)
239203b705cfSriastradh{
239303b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), dst=+(%d, %d)\n",
239403b705cfSriastradh	     __FUNCTION__, nbox,
239503b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
239603b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
239703b705cfSriastradh
239803b705cfSriastradh	sna_vertex_lock(&sna->render);
239903b705cfSriastradh	do {
240003b705cfSriastradh		int nbox_this_time;
240103b705cfSriastradh		float *v;
240203b705cfSriastradh
240303b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
240403b705cfSriastradh						     gen6_emit_composite_state);
240503b705cfSriastradh		assert(nbox_this_time);
240603b705cfSriastradh		nbox -= nbox_this_time;
240703b705cfSriastradh
240803b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
240903b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->base.floats_per_rect;
241003b705cfSriastradh
241103b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
241203b705cfSriastradh		sna_vertex_unlock(&sna->render);
241303b705cfSriastradh
241403b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
241503b705cfSriastradh		box += nbox_this_time;
241603b705cfSriastradh
241703b705cfSriastradh		sna_vertex_lock(&sna->render);
241803b705cfSriastradh		sna_vertex_release__locked(&sna->render);
241903b705cfSriastradh	} while (nbox);
242003b705cfSriastradh	sna_vertex_unlock(&sna->render);
242103b705cfSriastradh}
242203b705cfSriastradh
242303b705cfSriastradhfastcall static void
242403b705cfSriastradhgen6_render_composite_spans_done(struct sna *sna,
242503b705cfSriastradh				 const struct sna_composite_spans_op *op)
242603b705cfSriastradh{
242703b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
242803b705cfSriastradh	assert(!sna->render.active);
242903b705cfSriastradh
243003b705cfSriastradh	if (sna->render.vertex_offset)
243103b705cfSriastradh		gen4_vertex_flush(sna);
243203b705cfSriastradh
243303b705cfSriastradh	if (op->base.src.bo)
243403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->base.src.bo);
243503b705cfSriastradh
243603b705cfSriastradh	sna_render_composite_redirect_done(sna, &op->base);
243703b705cfSriastradh}
243803b705cfSriastradh
243903b705cfSriastradhstatic bool
244003b705cfSriastradhgen6_check_composite_spans(struct sna *sna,
244103b705cfSriastradh			   uint8_t op, PicturePtr src, PicturePtr dst,
244203b705cfSriastradh			   int16_t width, int16_t height,
244303b705cfSriastradh			   unsigned flags)
244403b705cfSriastradh{
244503b705cfSriastradh	DBG(("%s: op=%d, width=%d, height=%d, flags=%x\n",
244603b705cfSriastradh	     __FUNCTION__, op, width, height, flags));
244703b705cfSriastradh
244803b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op))
244903b705cfSriastradh		return false;
245003b705cfSriastradh
245103b705cfSriastradh	if (gen6_composite_fallback(sna, src, NULL, dst)) {
245203b705cfSriastradh		DBG(("%s: operation would fallback\n", __FUNCTION__));
245303b705cfSriastradh		return false;
245403b705cfSriastradh	}
245503b705cfSriastradh
245603b705cfSriastradh	if (need_tiling(sna, width, height) &&
245703b705cfSriastradh	    !is_gpu(sna, dst->pDrawable, PREFER_GPU_SPANS)) {
245803b705cfSriastradh		DBG(("%s: fallback, tiled operation not on GPU\n",
245903b705cfSriastradh		     __FUNCTION__));
246003b705cfSriastradh		return false;
246103b705cfSriastradh	}
246203b705cfSriastradh
246303b705cfSriastradh	if ((flags & COMPOSITE_SPANS_RECTILINEAR) == 0) {
246403b705cfSriastradh		struct sna_pixmap *priv = sna_pixmap_from_drawable(dst->pDrawable);
246503b705cfSriastradh		assert(priv);
246603b705cfSriastradh
246703b705cfSriastradh		if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
246803b705cfSriastradh			return true;
246903b705cfSriastradh
247003b705cfSriastradh		if (flags & COMPOSITE_SPANS_INPLACE_HINT)
247103b705cfSriastradh			return false;
247203b705cfSriastradh
247303b705cfSriastradh		return priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo);
247403b705cfSriastradh	}
247503b705cfSriastradh
247603b705cfSriastradh	return true;
247703b705cfSriastradh}
247803b705cfSriastradh
247903b705cfSriastradhstatic bool
248003b705cfSriastradhgen6_render_composite_spans(struct sna *sna,
248103b705cfSriastradh			    uint8_t op,
248203b705cfSriastradh			    PicturePtr src,
248303b705cfSriastradh			    PicturePtr dst,
248403b705cfSriastradh			    int16_t src_x,  int16_t src_y,
248503b705cfSriastradh			    int16_t dst_x,  int16_t dst_y,
248603b705cfSriastradh			    int16_t width,  int16_t height,
248703b705cfSriastradh			    unsigned flags,
248803b705cfSriastradh			    struct sna_composite_spans_op *tmp)
248903b705cfSriastradh{
249003b705cfSriastradh	DBG(("%s: %dx%d with flags=%x, current mode=%d\n", __FUNCTION__,
249103b705cfSriastradh	     width, height, flags, sna->kgem.ring));
249203b705cfSriastradh
249303b705cfSriastradh	assert(gen6_check_composite_spans(sna, op, src, dst, width, height, flags));
249403b705cfSriastradh
249503b705cfSriastradh	if (need_tiling(sna, width, height)) {
249603b705cfSriastradh		DBG(("%s: tiling, operation (%dx%d) too wide for pipeline\n",
249703b705cfSriastradh		     __FUNCTION__, width, height));
249803b705cfSriastradh		return sna_tiling_composite_spans(op, src, dst,
249903b705cfSriastradh						  src_x, src_y, dst_x, dst_y,
250003b705cfSriastradh						  width, height, flags, tmp);
250103b705cfSriastradh	}
250203b705cfSriastradh
250303b705cfSriastradh	tmp->base.op = op;
250403b705cfSriastradh	if (!gen6_composite_set_target(sna, &tmp->base, dst,
250503b705cfSriastradh				       dst_x, dst_y, width, height, true))
250603b705cfSriastradh		return false;
250703b705cfSriastradh
250803b705cfSriastradh	switch (gen6_composite_picture(sna, src, &tmp->base.src,
250903b705cfSriastradh				       src_x, src_y,
251003b705cfSriastradh				       width, height,
251103b705cfSriastradh				       dst_x, dst_y,
251203b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
251303b705cfSriastradh	case -1:
251403b705cfSriastradh		goto cleanup_dst;
251503b705cfSriastradh	case 0:
251603b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->base.src, 0))
251703b705cfSriastradh			goto cleanup_dst;
251803b705cfSriastradh		/* fall through to fixup */
251903b705cfSriastradh	case 1:
252003b705cfSriastradh		gen6_composite_channel_convert(&tmp->base.src);
252103b705cfSriastradh		break;
252203b705cfSriastradh	}
252303b705cfSriastradh	tmp->base.mask.bo = NULL;
252403b705cfSriastradh
252503b705cfSriastradh	tmp->base.is_affine = tmp->base.src.is_affine;
252603b705cfSriastradh	tmp->base.need_magic_ca_pass = false;
252703b705cfSriastradh
252803b705cfSriastradh	tmp->base.u.gen6.flags =
252903b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(tmp->base.src.filter,
253003b705cfSriastradh					      tmp->base.src.repeat,
253103b705cfSriastradh					      SAMPLER_FILTER_NEAREST,
253203b705cfSriastradh					      SAMPLER_EXTEND_PAD),
253303b705cfSriastradh			       gen6_get_blend(tmp->base.op, false, tmp->base.dst.format),
253403b705cfSriastradh			       GEN6_WM_KERNEL_OPACITY | !tmp->base.is_affine,
253503b705cfSriastradh			       gen4_choose_spans_emitter(sna, tmp));
253603b705cfSriastradh
253703b705cfSriastradh	tmp->box   = gen6_render_composite_spans_box;
253803b705cfSriastradh	tmp->boxes = gen6_render_composite_spans_boxes;
253903b705cfSriastradh	if (tmp->emit_boxes)
254003b705cfSriastradh		tmp->thread_boxes = gen6_render_composite_spans_boxes__thread;
254103b705cfSriastradh	tmp->done  = gen6_render_composite_spans_done;
254203b705cfSriastradh
254303b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->base.dst.bo);
254403b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
254503b705cfSriastradh			   tmp->base.dst.bo, tmp->base.src.bo,
254603b705cfSriastradh			   NULL)) {
254703b705cfSriastradh		kgem_submit(&sna->kgem);
254803b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
254903b705cfSriastradh				   tmp->base.dst.bo, tmp->base.src.bo,
255003b705cfSriastradh				   NULL))
255103b705cfSriastradh			goto cleanup_src;
255203b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
255303b705cfSriastradh	}
255403b705cfSriastradh
255503b705cfSriastradh	gen6_emit_composite_state(sna, &tmp->base);
255603b705cfSriastradh	gen6_align_vertex(sna, &tmp->base);
255703b705cfSriastradh	return true;
255803b705cfSriastradh
255903b705cfSriastradhcleanup_src:
256003b705cfSriastradh	if (tmp->base.src.bo)
256103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.src.bo);
256203b705cfSriastradhcleanup_dst:
256303b705cfSriastradh	if (tmp->base.redirect.real_bo)
256403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.dst.bo);
256503b705cfSriastradh	return false;
256603b705cfSriastradh}
256703b705cfSriastradh#endif
256803b705cfSriastradh
256903b705cfSriastradhstatic void
257003b705cfSriastradhgen6_emit_copy_state(struct sna *sna,
257103b705cfSriastradh		     const struct sna_composite_op *op)
257203b705cfSriastradh{
257303b705cfSriastradh	uint32_t *binding_table;
257403b705cfSriastradh	uint16_t offset;
257503b705cfSriastradh	bool dirty;
257603b705cfSriastradh
257703b705cfSriastradh	dirty = gen6_get_batch(sna, op);
257803b705cfSriastradh
257903b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
258003b705cfSriastradh
258103b705cfSriastradh	binding_table[0] =
258203b705cfSriastradh		gen6_bind_bo(sna,
258303b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
258403b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
258503b705cfSriastradh			     true);
258603b705cfSriastradh	binding_table[1] =
258703b705cfSriastradh		gen6_bind_bo(sna,
258803b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
258903b705cfSriastradh			     op->src.card_format,
259003b705cfSriastradh			     false);
259103b705cfSriastradh
259203b705cfSriastradh	if (sna->kgem.surface == offset &&
259303b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table) {
259403b705cfSriastradh		sna->kgem.surface += sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
259503b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
259603b705cfSriastradh	}
259703b705cfSriastradh
259803b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
259903b705cfSriastradh}
260003b705cfSriastradh
260103b705cfSriastradhstatic inline bool prefer_blt_copy(struct sna *sna,
260203b705cfSriastradh				   struct kgem_bo *src_bo,
260303b705cfSriastradh				   struct kgem_bo *dst_bo,
260403b705cfSriastradh				   unsigned flags)
260503b705cfSriastradh{
260603b705cfSriastradh	if (flags & COPY_SYNC)
260703b705cfSriastradh		return false;
260803b705cfSriastradh
260903b705cfSriastradh	if (PREFER_RENDER)
261003b705cfSriastradh		return PREFER_RENDER > 0;
261103b705cfSriastradh
261203b705cfSriastradh	if (sna->kgem.ring == KGEM_BLT)
261303b705cfSriastradh		return true;
261403b705cfSriastradh
261503b705cfSriastradh	if (src_bo == dst_bo && can_switch_to_blt(sna, dst_bo, flags))
261603b705cfSriastradh		return true;
261703b705cfSriastradh
261803b705cfSriastradh	if (untiled_tlb_miss(src_bo) ||
261903b705cfSriastradh	    untiled_tlb_miss(dst_bo))
262003b705cfSriastradh		return true;
262103b705cfSriastradh
262203b705cfSriastradh	if (kgem_bo_is_render(dst_bo) ||
262303b705cfSriastradh	    kgem_bo_is_render(src_bo))
262403b705cfSriastradh		return false;
262503b705cfSriastradh
262603b705cfSriastradh	if (!prefer_blt_ring(sna, dst_bo, flags))
262703b705cfSriastradh		return false;
262803b705cfSriastradh
262903b705cfSriastradh	return prefer_blt_bo(sna, src_bo) || prefer_blt_bo(sna, dst_bo);
263003b705cfSriastradh}
263103b705cfSriastradh
263203b705cfSriastradhinline static void boxes_extents(const BoxRec *box, int n, BoxRec *extents)
263303b705cfSriastradh{
263403b705cfSriastradh	*extents = box[0];
263503b705cfSriastradh	while (--n) {
263603b705cfSriastradh		box++;
263703b705cfSriastradh
263803b705cfSriastradh		if (box->x1 < extents->x1)
263903b705cfSriastradh			extents->x1 = box->x1;
264003b705cfSriastradh		if (box->x2 > extents->x2)
264103b705cfSriastradh			extents->x2 = box->x2;
264203b705cfSriastradh
264303b705cfSriastradh		if (box->y1 < extents->y1)
264403b705cfSriastradh			extents->y1 = box->y1;
264503b705cfSriastradh		if (box->y2 > extents->y2)
264603b705cfSriastradh			extents->y2 = box->y2;
264703b705cfSriastradh	}
264803b705cfSriastradh}
264903b705cfSriastradh
265003b705cfSriastradhstatic inline bool
265103b705cfSriastradhoverlaps(struct sna *sna,
265203b705cfSriastradh	 struct kgem_bo *src_bo, int16_t src_dx, int16_t src_dy,
265303b705cfSriastradh	 struct kgem_bo *dst_bo, int16_t dst_dx, int16_t dst_dy,
265403b705cfSriastradh	 const BoxRec *box, int n, BoxRec *extents)
265503b705cfSriastradh{
265603b705cfSriastradh	if (src_bo != dst_bo)
265703b705cfSriastradh		return false;
265803b705cfSriastradh
265903b705cfSriastradh	boxes_extents(box, n, extents);
266003b705cfSriastradh	return (extents->x2 + src_dx > extents->x1 + dst_dx &&
266103b705cfSriastradh		extents->x1 + src_dx < extents->x2 + dst_dx &&
266203b705cfSriastradh		extents->y2 + src_dy > extents->y1 + dst_dy &&
266303b705cfSriastradh		extents->y1 + src_dy < extents->y2 + dst_dy);
266403b705cfSriastradh}
266503b705cfSriastradh
266603b705cfSriastradhstatic bool
266703b705cfSriastradhgen6_render_copy_boxes(struct sna *sna, uint8_t alu,
266803b705cfSriastradh		       PixmapPtr src, struct kgem_bo *src_bo, int16_t src_dx, int16_t src_dy,
266903b705cfSriastradh		       PixmapPtr dst, struct kgem_bo *dst_bo, int16_t dst_dx, int16_t dst_dy,
267003b705cfSriastradh		       const BoxRec *box, int n, unsigned flags)
267103b705cfSriastradh{
267203b705cfSriastradh	struct sna_composite_op tmp;
267303b705cfSriastradh	BoxRec extents;
267403b705cfSriastradh
267503b705cfSriastradh	DBG(("%s (%d, %d)->(%d, %d) x %d, alu=%x, self-copy=%d, overlaps? %d\n",
267603b705cfSriastradh	     __FUNCTION__, src_dx, src_dy, dst_dx, dst_dy, n, alu,
267703b705cfSriastradh	     src_bo == dst_bo,
267803b705cfSriastradh	     overlaps(sna,
267903b705cfSriastradh		      src_bo, src_dx, src_dy,
268003b705cfSriastradh		      dst_bo, dst_dx, dst_dy,
268103b705cfSriastradh		      box, n, &extents)));
268203b705cfSriastradh
268303b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, flags) &&
268403b705cfSriastradh	    sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
268503b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
268603b705cfSriastradh			       src_bo, src_dx, src_dy,
268703b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
268803b705cfSriastradh			       dst->drawable.bitsPerPixel,
268903b705cfSriastradh			       box, n))
269003b705cfSriastradh		return true;
269103b705cfSriastradh
269203b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear)) {
269303b705cfSriastradhfallback_blt:
269403b705cfSriastradh		if (!sna_blt_compare_depth(&src->drawable, &dst->drawable))
269503b705cfSriastradh			return false;
269603b705cfSriastradh
269703b705cfSriastradh		return sna_blt_copy_boxes_fallback(sna, alu,
269803b705cfSriastradh						   src, src_bo, src_dx, src_dy,
269903b705cfSriastradh						   dst, dst_bo, dst_dx, dst_dy,
270003b705cfSriastradh						   box, n);
270103b705cfSriastradh	}
270203b705cfSriastradh
270303b705cfSriastradh	if (overlaps(sna,
270403b705cfSriastradh		     src_bo, src_dx, src_dy,
270503b705cfSriastradh		     dst_bo, dst_dx, dst_dy,
270603b705cfSriastradh		     box, n, &extents)) {
270703b705cfSriastradh		if (too_large(extents.x2-extents.x1, extents.y2-extents.y1))
270803b705cfSriastradh			goto fallback_blt;
270903b705cfSriastradh
271003b705cfSriastradh		if (can_switch_to_blt(sna, dst_bo, flags) &&
271103b705cfSriastradh		    sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
271203b705cfSriastradh		    sna_blt_copy_boxes(sna, alu,
271303b705cfSriastradh				       src_bo, src_dx, src_dy,
271403b705cfSriastradh				       dst_bo, dst_dx, dst_dy,
271503b705cfSriastradh				       dst->drawable.bitsPerPixel,
271603b705cfSriastradh				       box, n))
271703b705cfSriastradh			return true;
271803b705cfSriastradh
271903b705cfSriastradh		return sna_render_copy_boxes__overlap(sna, alu,
272003b705cfSriastradh						      src, src_bo, src_dx, src_dy,
272103b705cfSriastradh						      dst, dst_bo, dst_dx, dst_dy,
272203b705cfSriastradh						      box, n, &extents);
272303b705cfSriastradh	}
272403b705cfSriastradh
272503b705cfSriastradh	if (dst->drawable.depth == src->drawable.depth) {
272603b705cfSriastradh		tmp.dst.format = sna_render_format_for_depth(dst->drawable.depth);
272703b705cfSriastradh		tmp.src.pict_format = tmp.dst.format;
272803b705cfSriastradh	} else {
272903b705cfSriastradh		tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
273003b705cfSriastradh		tmp.src.pict_format = sna_format_for_depth(src->drawable.depth);
273103b705cfSriastradh	}
273203b705cfSriastradh	if (!gen6_check_format(tmp.src.pict_format))
273303b705cfSriastradh		goto fallback_blt;
273403b705cfSriastradh
273503b705cfSriastradh	tmp.dst.pixmap = dst;
273603b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
273703b705cfSriastradh	tmp.dst.height = dst->drawable.height;
273803b705cfSriastradh	tmp.dst.bo = dst_bo;
273903b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
274003b705cfSriastradh	tmp.damage = NULL;
274103b705cfSriastradh
274203b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
274303b705cfSriastradh	if (too_large(tmp.dst.width, tmp.dst.height)) {
274403b705cfSriastradh		int i;
274503b705cfSriastradh
274603b705cfSriastradh		extents = box[0];
274703b705cfSriastradh		for (i = 1; i < n; i++) {
274803b705cfSriastradh			if (box[i].x1 < extents.x1)
274903b705cfSriastradh				extents.x1 = box[i].x1;
275003b705cfSriastradh			if (box[i].y1 < extents.y1)
275103b705cfSriastradh				extents.y1 = box[i].y1;
275203b705cfSriastradh
275303b705cfSriastradh			if (box[i].x2 > extents.x2)
275403b705cfSriastradh				extents.x2 = box[i].x2;
275503b705cfSriastradh			if (box[i].y2 > extents.y2)
275603b705cfSriastradh				extents.y2 = box[i].y2;
275703b705cfSriastradh		}
275803b705cfSriastradh
275903b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
276003b705cfSriastradh						   extents.x1 + dst_dx,
276103b705cfSriastradh						   extents.y1 + dst_dy,
276203b705cfSriastradh						   extents.x2 - extents.x1,
276303b705cfSriastradh						   extents.y2 - extents.y1,
276403b705cfSriastradh						   n > 1))
276503b705cfSriastradh			goto fallback_tiled;
276603b705cfSriastradh
276703b705cfSriastradh		dst_dx += tmp.dst.x;
276803b705cfSriastradh		dst_dy += tmp.dst.y;
276903b705cfSriastradh
277003b705cfSriastradh		tmp.dst.x = tmp.dst.y = 0;
277103b705cfSriastradh	}
277203b705cfSriastradh
277303b705cfSriastradh	tmp.src.card_format = gen6_get_card_format(tmp.src.pict_format);
277403b705cfSriastradh	if (too_large(src->drawable.width, src->drawable.height)) {
277503b705cfSriastradh		int i;
277603b705cfSriastradh
277703b705cfSriastradh		extents = box[0];
277803b705cfSriastradh		for (i = 1; i < n; i++) {
277903b705cfSriastradh			if (box[i].x1 < extents.x1)
278003b705cfSriastradh				extents.x1 = box[i].x1;
278103b705cfSriastradh			if (box[i].y1 < extents.y1)
278203b705cfSriastradh				extents.y1 = box[i].y1;
278303b705cfSriastradh
278403b705cfSriastradh			if (box[i].x2 > extents.x2)
278503b705cfSriastradh				extents.x2 = box[i].x2;
278603b705cfSriastradh			if (box[i].y2 > extents.y2)
278703b705cfSriastradh				extents.y2 = box[i].y2;
278803b705cfSriastradh		}
278903b705cfSriastradh
279003b705cfSriastradh		if (!sna_render_pixmap_partial(sna, src, src_bo, &tmp.src,
279103b705cfSriastradh					       extents.x1 + src_dx,
279203b705cfSriastradh					       extents.y1 + src_dy,
279303b705cfSriastradh					       extents.x2 - extents.x1,
279403b705cfSriastradh					       extents.y2 - extents.y1)) {
279503b705cfSriastradh			DBG(("%s: unable to extract partial pixmap\n", __FUNCTION__));
279603b705cfSriastradh			goto fallback_tiled_dst;
279703b705cfSriastradh		}
279803b705cfSriastradh
279903b705cfSriastradh		src_dx += tmp.src.offset[0];
280003b705cfSriastradh		src_dy += tmp.src.offset[1];
280103b705cfSriastradh	} else {
280203b705cfSriastradh		tmp.src.bo = src_bo;
280303b705cfSriastradh		tmp.src.width  = src->drawable.width;
280403b705cfSriastradh		tmp.src.height = src->drawable.height;
280503b705cfSriastradh	}
280603b705cfSriastradh
280703b705cfSriastradh	tmp.mask.bo = NULL;
280803b705cfSriastradh
280903b705cfSriastradh	tmp.floats_per_vertex = 2;
281003b705cfSriastradh	tmp.floats_per_rect = 6;
281103b705cfSriastradh	tmp.need_magic_ca_pass = 0;
281203b705cfSriastradh
281303b705cfSriastradh	tmp.u.gen6.flags = COPY_FLAGS(alu);
281403b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
281503b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == COPY_SAMPLER);
281603b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == COPY_VERTEX);
281703b705cfSriastradh
281803b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
281903b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
282003b705cfSriastradh		kgem_submit(&sna->kgem);
282103b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
282203b705cfSriastradh			DBG(("%s: too large for a single operation\n",
282303b705cfSriastradh			     __FUNCTION__));
282403b705cfSriastradh			goto fallback_tiled_src;
282503b705cfSriastradh		}
282603b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
282703b705cfSriastradh	}
282803b705cfSriastradh
282903b705cfSriastradh	gen6_emit_copy_state(sna, &tmp);
283003b705cfSriastradh	gen6_align_vertex(sna, &tmp);
283103b705cfSriastradh
283203b705cfSriastradh	do {
283303b705cfSriastradh		int16_t *v;
283403b705cfSriastradh		int n_this_time;
283503b705cfSriastradh
283603b705cfSriastradh		n_this_time = gen6_get_rectangles(sna, &tmp, n,
283703b705cfSriastradh						  gen6_emit_copy_state);
283803b705cfSriastradh		n -= n_this_time;
283903b705cfSriastradh
284003b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
284103b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
284203b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
284303b705cfSriastradh		do {
284403b705cfSriastradh
284503b705cfSriastradh			DBG(("	(%d, %d) -> (%d, %d) + (%d, %d)\n",
284603b705cfSriastradh			     box->x1 + src_dx, box->y1 + src_dy,
284703b705cfSriastradh			     box->x1 + dst_dx, box->y1 + dst_dy,
284803b705cfSriastradh			     box->x2 - box->x1, box->y2 - box->y1));
284903b705cfSriastradh			v[0] = box->x2 + dst_dx;
285003b705cfSriastradh			v[2] = box->x2 + src_dx;
285103b705cfSriastradh			v[1]  = v[5] = box->y2 + dst_dy;
285203b705cfSriastradh			v[3]  = v[7] = box->y2 + src_dy;
285303b705cfSriastradh			v[8]  = v[4] = box->x1 + dst_dx;
285403b705cfSriastradh			v[10] = v[6] = box->x1 + src_dx;
285503b705cfSriastradh			v[9]  = box->y1 + dst_dy;
285603b705cfSriastradh			v[11] = box->y1 + src_dy;
285703b705cfSriastradh			v += 12; box++;
285803b705cfSriastradh		} while (--n_this_time);
285903b705cfSriastradh	} while (n);
286003b705cfSriastradh
286103b705cfSriastradh	gen4_vertex_flush(sna);
286203b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
286303b705cfSriastradh	if (tmp.src.bo != src_bo)
286403b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.src.bo);
286503b705cfSriastradh	return true;
286603b705cfSriastradh
286703b705cfSriastradhfallback_tiled_src:
286803b705cfSriastradh	if (tmp.src.bo != src_bo)
286903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.src.bo);
287003b705cfSriastradhfallback_tiled_dst:
287103b705cfSriastradh	if (tmp.redirect.real_bo)
287203b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
287303b705cfSriastradhfallback_tiled:
287403b705cfSriastradh	if (sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
287503b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
287603b705cfSriastradh			       src_bo, src_dx, src_dy,
287703b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
287803b705cfSriastradh			       dst->drawable.bitsPerPixel,
287903b705cfSriastradh			       box, n))
288003b705cfSriastradh		return true;
288103b705cfSriastradh
288203b705cfSriastradh	return sna_tiling_copy_boxes(sna, alu,
288303b705cfSriastradh				     src, src_bo, src_dx, src_dy,
288403b705cfSriastradh				     dst, dst_bo, dst_dx, dst_dy,
288503b705cfSriastradh				     box, n);
288603b705cfSriastradh}
288703b705cfSriastradh
288803b705cfSriastradhstatic void
288903b705cfSriastradhgen6_render_copy_blt(struct sna *sna,
289003b705cfSriastradh		     const struct sna_copy_op *op,
289103b705cfSriastradh		     int16_t sx, int16_t sy,
289203b705cfSriastradh		     int16_t w,  int16_t h,
289303b705cfSriastradh		     int16_t dx, int16_t dy)
289403b705cfSriastradh{
289503b705cfSriastradh	int16_t *v;
289603b705cfSriastradh
289703b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_copy_state);
289803b705cfSriastradh
289903b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
290003b705cfSriastradh	sna->render.vertex_used += 6;
290103b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
290203b705cfSriastradh
290303b705cfSriastradh	v[0]  = dx+w; v[1]  = dy+h;
290403b705cfSriastradh	v[2]  = sx+w; v[3]  = sy+h;
290503b705cfSriastradh	v[4]  = dx;   v[5]  = dy+h;
290603b705cfSriastradh	v[6]  = sx;   v[7]  = sy+h;
290703b705cfSriastradh	v[8]  = dx;   v[9]  = dy;
290803b705cfSriastradh	v[10] = sx;   v[11] = sy;
290903b705cfSriastradh}
291003b705cfSriastradh
291103b705cfSriastradhstatic void
291203b705cfSriastradhgen6_render_copy_done(struct sna *sna, const struct sna_copy_op *op)
291303b705cfSriastradh{
291403b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
291503b705cfSriastradh
291603b705cfSriastradh	assert(!sna->render.active);
291703b705cfSriastradh	if (sna->render.vertex_offset)
291803b705cfSriastradh		gen4_vertex_flush(sna);
291903b705cfSriastradh}
292003b705cfSriastradh
292103b705cfSriastradhstatic bool
292203b705cfSriastradhgen6_render_copy(struct sna *sna, uint8_t alu,
292303b705cfSriastradh		 PixmapPtr src, struct kgem_bo *src_bo,
292403b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
292503b705cfSriastradh		 struct sna_copy_op *op)
292603b705cfSriastradh{
292703b705cfSriastradh	DBG(("%s (alu=%d, src=(%dx%d), dst=(%dx%d))\n",
292803b705cfSriastradh	     __FUNCTION__, alu,
292903b705cfSriastradh	     src->drawable.width, src->drawable.height,
293003b705cfSriastradh	     dst->drawable.width, dst->drawable.height));
293103b705cfSriastradh
293203b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, 0) &&
293303b705cfSriastradh	    sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
293403b705cfSriastradh	    sna_blt_copy(sna, alu,
293503b705cfSriastradh			 src_bo, dst_bo,
293603b705cfSriastradh			 dst->drawable.bitsPerPixel,
293703b705cfSriastradh			 op))
293803b705cfSriastradh		return true;
293903b705cfSriastradh
294003b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo ||
294103b705cfSriastradh	    too_large(src->drawable.width, src->drawable.height) ||
294203b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height)) {
294303b705cfSriastradhfallback:
294403b705cfSriastradh		if (!sna_blt_compare_depth(&src->drawable, &dst->drawable))
294503b705cfSriastradh			return false;
294603b705cfSriastradh
294703b705cfSriastradh		return sna_blt_copy(sna, alu, src_bo, dst_bo,
294803b705cfSriastradh				    dst->drawable.bitsPerPixel,
294903b705cfSriastradh				    op);
295003b705cfSriastradh	}
295103b705cfSriastradh
295203b705cfSriastradh	if (dst->drawable.depth == src->drawable.depth) {
295303b705cfSriastradh		op->base.dst.format = sna_render_format_for_depth(dst->drawable.depth);
295403b705cfSriastradh		op->base.src.pict_format = op->base.dst.format;
295503b705cfSriastradh	} else {
295603b705cfSriastradh		op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
295703b705cfSriastradh		op->base.src.pict_format = sna_format_for_depth(src->drawable.depth);
295803b705cfSriastradh	}
295903b705cfSriastradh	if (!gen6_check_format(op->base.src.pict_format))
296003b705cfSriastradh		goto fallback;
296103b705cfSriastradh
296203b705cfSriastradh	op->base.dst.pixmap = dst;
296303b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
296403b705cfSriastradh	op->base.dst.height = dst->drawable.height;
296503b705cfSriastradh	op->base.dst.bo = dst_bo;
296603b705cfSriastradh
296703b705cfSriastradh	op->base.src.bo = src_bo;
296803b705cfSriastradh	op->base.src.card_format =
296903b705cfSriastradh		gen6_get_card_format(op->base.src.pict_format);
297003b705cfSriastradh	op->base.src.width  = src->drawable.width;
297103b705cfSriastradh	op->base.src.height = src->drawable.height;
297203b705cfSriastradh
297303b705cfSriastradh	op->base.mask.bo = NULL;
297403b705cfSriastradh
297503b705cfSriastradh	op->base.floats_per_vertex = 2;
297603b705cfSriastradh	op->base.floats_per_rect = 6;
297703b705cfSriastradh
297803b705cfSriastradh	op->base.u.gen6.flags = COPY_FLAGS(alu);
297903b705cfSriastradh	assert(GEN6_KERNEL(op->base.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
298003b705cfSriastradh	assert(GEN6_SAMPLER(op->base.u.gen6.flags) == COPY_SAMPLER);
298103b705cfSriastradh	assert(GEN6_VERTEX(op->base.u.gen6.flags) == COPY_VERTEX);
298203b705cfSriastradh
298303b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
298403b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
298503b705cfSriastradh		kgem_submit(&sna->kgem);
298603b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL))
298703b705cfSriastradh			goto fallback;
298803b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
298903b705cfSriastradh	}
299003b705cfSriastradh
299103b705cfSriastradh	gen6_emit_copy_state(sna, &op->base);
299203b705cfSriastradh	gen6_align_vertex(sna, &op->base);
299303b705cfSriastradh
299403b705cfSriastradh	op->blt  = gen6_render_copy_blt;
299503b705cfSriastradh	op->done = gen6_render_copy_done;
299603b705cfSriastradh	return true;
299703b705cfSriastradh}
299803b705cfSriastradh
299903b705cfSriastradhstatic void
300003b705cfSriastradhgen6_emit_fill_state(struct sna *sna, const struct sna_composite_op *op)
300103b705cfSriastradh{
300203b705cfSriastradh	uint32_t *binding_table;
300303b705cfSriastradh	uint16_t offset;
300403b705cfSriastradh	bool dirty;
300503b705cfSriastradh
300603b705cfSriastradh	dirty = gen6_get_batch(sna, op);
300703b705cfSriastradh
300803b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
300903b705cfSriastradh
301003b705cfSriastradh	binding_table[0] =
301103b705cfSriastradh		gen6_bind_bo(sna,
301203b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
301303b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
301403b705cfSriastradh			     true);
301503b705cfSriastradh	binding_table[1] =
301603b705cfSriastradh		gen6_bind_bo(sna,
301703b705cfSriastradh			     op->src.bo, 1, 1,
301803b705cfSriastradh			     GEN6_SURFACEFORMAT_B8G8R8A8_UNORM,
301903b705cfSriastradh			     false);
302003b705cfSriastradh
302103b705cfSriastradh	if (sna->kgem.surface == offset &&
302203b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table) {
302303b705cfSriastradh		sna->kgem.surface +=
302403b705cfSriastradh			sizeof(struct gen6_surface_state_padded)/sizeof(uint32_t);
302503b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
302603b705cfSriastradh	}
302703b705cfSriastradh
302803b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
302903b705cfSriastradh}
303003b705cfSriastradh
303103b705cfSriastradhstatic inline bool prefer_blt_fill(struct sna *sna,
303203b705cfSriastradh				   struct kgem_bo *bo)
303303b705cfSriastradh{
303403b705cfSriastradh	if (PREFER_RENDER)
303503b705cfSriastradh		return PREFER_RENDER < 0;
303603b705cfSriastradh
303703b705cfSriastradh	if (kgem_bo_is_render(bo))
303803b705cfSriastradh		return false;
303903b705cfSriastradh
304003b705cfSriastradh	if (untiled_tlb_miss(bo))
304103b705cfSriastradh		return true;
304203b705cfSriastradh
304303b705cfSriastradh	if (!prefer_blt_ring(sna, bo, 0))
304403b705cfSriastradh		return false;
304503b705cfSriastradh
304603b705cfSriastradh	return prefer_blt_bo(sna, bo);
304703b705cfSriastradh}
304803b705cfSriastradh
304903b705cfSriastradhstatic bool
305003b705cfSriastradhgen6_render_fill_boxes(struct sna *sna,
305103b705cfSriastradh		       CARD8 op,
305203b705cfSriastradh		       PictFormat format,
305303b705cfSriastradh		       const xRenderColor *color,
305403b705cfSriastradh		       PixmapPtr dst, struct kgem_bo *dst_bo,
305503b705cfSriastradh		       const BoxRec *box, int n)
305603b705cfSriastradh{
305703b705cfSriastradh	struct sna_composite_op tmp;
305803b705cfSriastradh	uint32_t pixel;
305903b705cfSriastradh
306003b705cfSriastradh	DBG(("%s (op=%d, color=(%04x, %04x, %04x, %04x) [%08x])\n",
306103b705cfSriastradh	     __FUNCTION__, op,
306203b705cfSriastradh	     color->red, color->green, color->blue, color->alpha, (int)format));
306303b705cfSriastradh
306403b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op)) {
306503b705cfSriastradh		DBG(("%s: fallback due to unhandled blend op: %d\n",
306603b705cfSriastradh		     __FUNCTION__, op));
306703b705cfSriastradh		return false;
306803b705cfSriastradh	}
306903b705cfSriastradh
307003b705cfSriastradh	if (prefer_blt_fill(sna, dst_bo) || !gen6_check_dst_format(format)) {
307103b705cfSriastradh		uint8_t alu = GXinvalid;
307203b705cfSriastradh
307303b705cfSriastradh		if (op <= PictOpSrc) {
307403b705cfSriastradh			pixel = 0;
307503b705cfSriastradh			if (op == PictOpClear)
307603b705cfSriastradh				alu = GXclear;
307703b705cfSriastradh			else if (sna_get_pixel_from_rgba(&pixel,
307803b705cfSriastradh							 color->red,
307903b705cfSriastradh							 color->green,
308003b705cfSriastradh							 color->blue,
308103b705cfSriastradh							 color->alpha,
308203b705cfSriastradh							 format))
308303b705cfSriastradh				alu = GXcopy;
308403b705cfSriastradh		}
308503b705cfSriastradh
308603b705cfSriastradh		if (alu != GXinvalid &&
308703b705cfSriastradh		    sna_blt_fill_boxes(sna, alu,
308803b705cfSriastradh				       dst_bo, dst->drawable.bitsPerPixel,
308903b705cfSriastradh				       pixel, box, n))
309003b705cfSriastradh			return true;
309103b705cfSriastradh
309203b705cfSriastradh		if (!gen6_check_dst_format(format))
309303b705cfSriastradh			return false;
309403b705cfSriastradh	}
309503b705cfSriastradh
309603b705cfSriastradh	if (op == PictOpClear) {
309703b705cfSriastradh		pixel = 0;
309803b705cfSriastradh		op = PictOpSrc;
309903b705cfSriastradh	} else if (!sna_get_pixel_from_rgba(&pixel,
310003b705cfSriastradh					    color->red,
310103b705cfSriastradh					    color->green,
310203b705cfSriastradh					    color->blue,
310303b705cfSriastradh					    color->alpha,
310403b705cfSriastradh					    PICT_a8r8g8b8))
310503b705cfSriastradh		return false;
310603b705cfSriastradh
310703b705cfSriastradh	DBG(("%s(%08x x %d [(%d, %d), (%d, %d) ...])\n",
310803b705cfSriastradh	     __FUNCTION__, pixel, n,
310903b705cfSriastradh	     box[0].x1, box[0].y1, box[0].x2, box[0].y2));
311003b705cfSriastradh
311103b705cfSriastradh	tmp.dst.pixmap = dst;
311203b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
311303b705cfSriastradh	tmp.dst.height = dst->drawable.height;
311403b705cfSriastradh	tmp.dst.format = format;
311503b705cfSriastradh	tmp.dst.bo = dst_bo;
311603b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
311703b705cfSriastradh	tmp.damage = NULL;
311803b705cfSriastradh
311903b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
312003b705cfSriastradh	if (too_large(dst->drawable.width, dst->drawable.height)) {
312103b705cfSriastradh		BoxRec extents;
312203b705cfSriastradh
312303b705cfSriastradh		boxes_extents(box, n, &extents);
312403b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
312503b705cfSriastradh						   extents.x1, extents.y1,
312603b705cfSriastradh						   extents.x2 - extents.x1,
312703b705cfSriastradh						   extents.y2 - extents.y1,
312803b705cfSriastradh						   n > 1))
312903b705cfSriastradh			return sna_tiling_fill_boxes(sna, op, format, color,
313003b705cfSriastradh						     dst, dst_bo, box, n);
313103b705cfSriastradh	}
313203b705cfSriastradh
313303b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, pixel);
313403b705cfSriastradh	tmp.mask.bo = NULL;
313503b705cfSriastradh
313603b705cfSriastradh	tmp.floats_per_vertex = 2;
313703b705cfSriastradh	tmp.floats_per_rect = 6;
313803b705cfSriastradh	tmp.need_magic_ca_pass = false;
313903b705cfSriastradh
314003b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS(op, format);
314103b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
314203b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
314303b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
314403b705cfSriastradh
314503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
314603b705cfSriastradh		kgem_submit(&sna->kgem);
314703b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, dst_bo, NULL));
314803b705cfSriastradh	}
314903b705cfSriastradh
315003b705cfSriastradh	gen6_emit_fill_state(sna, &tmp);
315103b705cfSriastradh	gen6_align_vertex(sna, &tmp);
315203b705cfSriastradh
315303b705cfSriastradh	do {
315403b705cfSriastradh		int n_this_time;
315503b705cfSriastradh		int16_t *v;
315603b705cfSriastradh
315703b705cfSriastradh		n_this_time = gen6_get_rectangles(sna, &tmp, n,
315803b705cfSriastradh						  gen6_emit_fill_state);
315903b705cfSriastradh		n -= n_this_time;
316003b705cfSriastradh
316103b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
316203b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
316303b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
316403b705cfSriastradh		do {
316503b705cfSriastradh			DBG(("	(%d, %d), (%d, %d)\n",
316603b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
316703b705cfSriastradh
316803b705cfSriastradh			v[0] = box->x2;
316903b705cfSriastradh			v[5] = v[1] = box->y2;
317003b705cfSriastradh			v[8] = v[4] = box->x1;
317103b705cfSriastradh			v[9] = box->y1;
317203b705cfSriastradh			v[2] = v[3]  = v[7]  = 1;
317303b705cfSriastradh			v[6] = v[10] = v[11] = 0;
317403b705cfSriastradh			v += 12; box++;
317503b705cfSriastradh		} while (--n_this_time);
317603b705cfSriastradh	} while (n);
317703b705cfSriastradh
317803b705cfSriastradh	gen4_vertex_flush(sna);
317903b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
318003b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
318103b705cfSriastradh	return true;
318203b705cfSriastradh}
318303b705cfSriastradh
318403b705cfSriastradhstatic void
318503b705cfSriastradhgen6_render_op_fill_blt(struct sna *sna,
318603b705cfSriastradh			const struct sna_fill_op *op,
318703b705cfSriastradh			int16_t x, int16_t y, int16_t w, int16_t h)
318803b705cfSriastradh{
318903b705cfSriastradh	int16_t *v;
319003b705cfSriastradh
319103b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d)\n", __FUNCTION__, x, y, w, h));
319203b705cfSriastradh
319303b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_fill_state);
319403b705cfSriastradh
319503b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
319603b705cfSriastradh	sna->render.vertex_used += 6;
319703b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
319803b705cfSriastradh
319903b705cfSriastradh	v[0] = x+w;
320003b705cfSriastradh	v[4] = v[8] = x;
320103b705cfSriastradh	v[1] = v[5] = y+h;
320203b705cfSriastradh	v[9] = y;
320303b705cfSriastradh
320403b705cfSriastradh	v[2] = v[3]  = v[7]  = 1;
320503b705cfSriastradh	v[6] = v[10] = v[11] = 0;
320603b705cfSriastradh}
320703b705cfSriastradh
320803b705cfSriastradhfastcall static void
320903b705cfSriastradhgen6_render_op_fill_box(struct sna *sna,
321003b705cfSriastradh			const struct sna_fill_op *op,
321103b705cfSriastradh			const BoxRec *box)
321203b705cfSriastradh{
321303b705cfSriastradh	int16_t *v;
321403b705cfSriastradh
321503b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
321603b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
321703b705cfSriastradh
321803b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_fill_state);
321903b705cfSriastradh
322003b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
322103b705cfSriastradh	sna->render.vertex_used += 6;
322203b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
322303b705cfSriastradh
322403b705cfSriastradh	v[0] = box->x2;
322503b705cfSriastradh	v[8] = v[4] = box->x1;
322603b705cfSriastradh	v[5] = v[1] = box->y2;
322703b705cfSriastradh	v[9] = box->y1;
322803b705cfSriastradh
322903b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
323003b705cfSriastradh	v[6] = v[10] = v[11] = 0;
323103b705cfSriastradh}
323203b705cfSriastradh
323303b705cfSriastradhfastcall static void
323403b705cfSriastradhgen6_render_op_fill_boxes(struct sna *sna,
323503b705cfSriastradh			  const struct sna_fill_op *op,
323603b705cfSriastradh			  const BoxRec *box,
323703b705cfSriastradh			  int nbox)
323803b705cfSriastradh{
323903b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)... x %d\n", __FUNCTION__,
324003b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2, nbox));
324103b705cfSriastradh
324203b705cfSriastradh	do {
324303b705cfSriastradh		int nbox_this_time;
324403b705cfSriastradh		int16_t *v;
324503b705cfSriastradh
324603b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
324703b705cfSriastradh						     gen6_emit_fill_state);
324803b705cfSriastradh		nbox -= nbox_this_time;
324903b705cfSriastradh
325003b705cfSriastradh		v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
325103b705cfSriastradh		sna->render.vertex_used += 6 * nbox_this_time;
325203b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
325303b705cfSriastradh
325403b705cfSriastradh		do {
325503b705cfSriastradh			v[0] = box->x2;
325603b705cfSriastradh			v[8] = v[4] = box->x1;
325703b705cfSriastradh			v[5] = v[1] = box->y2;
325803b705cfSriastradh			v[9] = box->y1;
325903b705cfSriastradh			v[7] = v[2]  = v[3]  = 1;
326003b705cfSriastradh			v[6] = v[10] = v[11] = 0;
326103b705cfSriastradh			box++; v += 12;
326203b705cfSriastradh		} while (--nbox_this_time);
326303b705cfSriastradh	} while (nbox);
326403b705cfSriastradh}
326503b705cfSriastradh
326603b705cfSriastradhstatic void
326703b705cfSriastradhgen6_render_op_fill_done(struct sna *sna, const struct sna_fill_op *op)
326803b705cfSriastradh{
326903b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
327003b705cfSriastradh
327103b705cfSriastradh	assert(!sna->render.active);
327203b705cfSriastradh	if (sna->render.vertex_offset)
327303b705cfSriastradh		gen4_vertex_flush(sna);
327403b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
327503b705cfSriastradh}
327603b705cfSriastradh
327703b705cfSriastradhstatic bool
327803b705cfSriastradhgen6_render_fill(struct sna *sna, uint8_t alu,
327903b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
328003b705cfSriastradh		 uint32_t color,
328103b705cfSriastradh		 struct sna_fill_op *op)
328203b705cfSriastradh{
328303b705cfSriastradh	DBG(("%s: (alu=%d, color=%x)\n", __FUNCTION__, alu, color));
328403b705cfSriastradh
328503b705cfSriastradh	if (prefer_blt_fill(sna, dst_bo) &&
328603b705cfSriastradh	    sna_blt_fill(sna, alu,
328703b705cfSriastradh			 dst_bo, dst->drawable.bitsPerPixel,
328803b705cfSriastradh			 color,
328903b705cfSriastradh			 op))
329003b705cfSriastradh		return true;
329103b705cfSriastradh
329203b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
329303b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
329403b705cfSriastradh		return sna_blt_fill(sna, alu,
329503b705cfSriastradh				    dst_bo, dst->drawable.bitsPerPixel,
329603b705cfSriastradh				    color,
329703b705cfSriastradh				    op);
329803b705cfSriastradh
329903b705cfSriastradh	if (alu == GXclear)
330003b705cfSriastradh		color = 0;
330103b705cfSriastradh
330203b705cfSriastradh	op->base.dst.pixmap = dst;
330303b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
330403b705cfSriastradh	op->base.dst.height = dst->drawable.height;
330503b705cfSriastradh	op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
330603b705cfSriastradh	op->base.dst.bo = dst_bo;
330703b705cfSriastradh	op->base.dst.x = op->base.dst.y = 0;
330803b705cfSriastradh
330903b705cfSriastradh	op->base.src.bo =
331003b705cfSriastradh		sna_render_get_solid(sna,
331103b705cfSriastradh				     sna_rgba_for_color(color,
331203b705cfSriastradh							dst->drawable.depth));
331303b705cfSriastradh	op->base.mask.bo = NULL;
331403b705cfSriastradh
331503b705cfSriastradh	op->base.need_magic_ca_pass = false;
331603b705cfSriastradh	op->base.floats_per_vertex = 2;
331703b705cfSriastradh	op->base.floats_per_rect = 6;
331803b705cfSriastradh
331903b705cfSriastradh	op->base.u.gen6.flags = FILL_FLAGS_NOBLEND;
332003b705cfSriastradh	assert(GEN6_KERNEL(op->base.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
332103b705cfSriastradh	assert(GEN6_SAMPLER(op->base.u.gen6.flags) == FILL_SAMPLER);
332203b705cfSriastradh	assert(GEN6_VERTEX(op->base.u.gen6.flags) == FILL_VERTEX);
332303b705cfSriastradh
332403b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
332503b705cfSriastradh		kgem_submit(&sna->kgem);
332603b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, dst_bo, NULL));
332703b705cfSriastradh	}
332803b705cfSriastradh
332903b705cfSriastradh	gen6_emit_fill_state(sna, &op->base);
333003b705cfSriastradh	gen6_align_vertex(sna, &op->base);
333103b705cfSriastradh
333203b705cfSriastradh	op->blt  = gen6_render_op_fill_blt;
333303b705cfSriastradh	op->box  = gen6_render_op_fill_box;
333403b705cfSriastradh	op->boxes = gen6_render_op_fill_boxes;
333503b705cfSriastradh	op->done = gen6_render_op_fill_done;
333603b705cfSriastradh	return true;
333703b705cfSriastradh}
333803b705cfSriastradh
333903b705cfSriastradhstatic bool
334003b705cfSriastradhgen6_render_fill_one_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
334103b705cfSriastradh			     uint32_t color,
334203b705cfSriastradh			     int16_t x1, int16_t y1, int16_t x2, int16_t y2,
334303b705cfSriastradh			     uint8_t alu)
334403b705cfSriastradh{
334503b705cfSriastradh	BoxRec box;
334603b705cfSriastradh
334703b705cfSriastradh	box.x1 = x1;
334803b705cfSriastradh	box.y1 = y1;
334903b705cfSriastradh	box.x2 = x2;
335003b705cfSriastradh	box.y2 = y2;
335103b705cfSriastradh
335203b705cfSriastradh	return sna_blt_fill_boxes(sna, alu,
335303b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
335403b705cfSriastradh				  color, &box, 1);
335503b705cfSriastradh}
335603b705cfSriastradh
335703b705cfSriastradhstatic bool
335803b705cfSriastradhgen6_render_fill_one(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
335903b705cfSriastradh		     uint32_t color,
336003b705cfSriastradh		     int16_t x1, int16_t y1,
336103b705cfSriastradh		     int16_t x2, int16_t y2,
336203b705cfSriastradh		     uint8_t alu)
336303b705cfSriastradh{
336403b705cfSriastradh	struct sna_composite_op tmp;
336503b705cfSriastradh	int16_t *v;
336603b705cfSriastradh
336703b705cfSriastradh	/* Prefer to use the BLT if already engaged */
336803b705cfSriastradh	if (prefer_blt_fill(sna, bo) &&
336903b705cfSriastradh	    gen6_render_fill_one_try_blt(sna, dst, bo, color,
337003b705cfSriastradh					 x1, y1, x2, y2, alu))
337103b705cfSriastradh		return true;
337203b705cfSriastradh
337303b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
337403b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
337503b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
337603b705cfSriastradh		return gen6_render_fill_one_try_blt(sna, dst, bo, color,
337703b705cfSriastradh						    x1, y1, x2, y2, alu);
337803b705cfSriastradh
337903b705cfSriastradh	if (alu == GXclear)
338003b705cfSriastradh		color = 0;
338103b705cfSriastradh
338203b705cfSriastradh	tmp.dst.pixmap = dst;
338303b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
338403b705cfSriastradh	tmp.dst.height = dst->drawable.height;
338503b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
338603b705cfSriastradh	tmp.dst.bo = bo;
338703b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
338803b705cfSriastradh
338903b705cfSriastradh	tmp.src.bo =
339003b705cfSriastradh		sna_render_get_solid(sna,
339103b705cfSriastradh				     sna_rgba_for_color(color,
339203b705cfSriastradh							dst->drawable.depth));
339303b705cfSriastradh	tmp.mask.bo = NULL;
339403b705cfSriastradh
339503b705cfSriastradh	tmp.floats_per_vertex = 2;
339603b705cfSriastradh	tmp.floats_per_rect = 6;
339703b705cfSriastradh	tmp.need_magic_ca_pass = false;
339803b705cfSriastradh
339903b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS_NOBLEND;
340003b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
340103b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
340203b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
340303b705cfSriastradh
340403b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
340503b705cfSriastradh		kgem_submit(&sna->kgem);
340603b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
340703b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
340803b705cfSriastradh			return false;
340903b705cfSriastradh		}
341003b705cfSriastradh	}
341103b705cfSriastradh
341203b705cfSriastradh	gen6_emit_fill_state(sna, &tmp);
341303b705cfSriastradh	gen6_align_vertex(sna, &tmp);
341403b705cfSriastradh
341503b705cfSriastradh	gen6_get_rectangles(sna, &tmp, 1, gen6_emit_fill_state);
341603b705cfSriastradh
341703b705cfSriastradh	DBG(("	(%d, %d), (%d, %d)\n", x1, y1, x2, y2));
341803b705cfSriastradh
341903b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
342003b705cfSriastradh	sna->render.vertex_used += 6;
342103b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
342203b705cfSriastradh
342303b705cfSriastradh	v[0] = x2;
342403b705cfSriastradh	v[8] = v[4] = x1;
342503b705cfSriastradh	v[5] = v[1] = y2;
342603b705cfSriastradh	v[9] = y1;
342703b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
342803b705cfSriastradh	v[6] = v[10] = v[11] = 0;
342903b705cfSriastradh
343003b705cfSriastradh	gen4_vertex_flush(sna);
343103b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
343203b705cfSriastradh
343303b705cfSriastradh	return true;
343403b705cfSriastradh}
343503b705cfSriastradh
343603b705cfSriastradhstatic bool
343703b705cfSriastradhgen6_render_clear_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
343803b705cfSriastradh{
343903b705cfSriastradh	BoxRec box;
344003b705cfSriastradh
344103b705cfSriastradh	box.x1 = 0;
344203b705cfSriastradh	box.y1 = 0;
344303b705cfSriastradh	box.x2 = dst->drawable.width;
344403b705cfSriastradh	box.y2 = dst->drawable.height;
344503b705cfSriastradh
344603b705cfSriastradh	return sna_blt_fill_boxes(sna, GXclear,
344703b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
344803b705cfSriastradh				  0, &box, 1);
344903b705cfSriastradh}
345003b705cfSriastradh
345103b705cfSriastradhstatic bool
345203b705cfSriastradhgen6_render_clear(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
345303b705cfSriastradh{
345403b705cfSriastradh	struct sna_composite_op tmp;
345503b705cfSriastradh	int16_t *v;
345603b705cfSriastradh
345703b705cfSriastradh	DBG(("%s: %dx%d\n",
345803b705cfSriastradh	     __FUNCTION__,
345903b705cfSriastradh	     dst->drawable.width,
346003b705cfSriastradh	     dst->drawable.height));
346103b705cfSriastradh
346203b705cfSriastradh	/* Prefer to use the BLT if, and only if, already engaged */
346303b705cfSriastradh	if (sna->kgem.ring == KGEM_BLT &&
346403b705cfSriastradh	    gen6_render_clear_try_blt(sna, dst, bo))
346503b705cfSriastradh		return true;
346603b705cfSriastradh
346703b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
346803b705cfSriastradh	if (too_large(dst->drawable.width, dst->drawable.height))
346903b705cfSriastradh		return gen6_render_clear_try_blt(sna, dst, bo);
347003b705cfSriastradh
347103b705cfSriastradh	tmp.dst.pixmap = dst;
347203b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
347303b705cfSriastradh	tmp.dst.height = dst->drawable.height;
347403b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
347503b705cfSriastradh	tmp.dst.bo = bo;
347603b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
347703b705cfSriastradh
347803b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, 0);
347903b705cfSriastradh	tmp.mask.bo = NULL;
348003b705cfSriastradh
348103b705cfSriastradh	tmp.floats_per_vertex = 2;
348203b705cfSriastradh	tmp.floats_per_rect = 6;
348303b705cfSriastradh	tmp.need_magic_ca_pass = false;
348403b705cfSriastradh
348503b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS_NOBLEND;
348603b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
348703b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
348803b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
348903b705cfSriastradh
349003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
349103b705cfSriastradh		kgem_submit(&sna->kgem);
349203b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
349303b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
349403b705cfSriastradh			return false;
349503b705cfSriastradh		}
349603b705cfSriastradh	}
349703b705cfSriastradh
349803b705cfSriastradh	gen6_emit_fill_state(sna, &tmp);
349903b705cfSriastradh	gen6_align_vertex(sna, &tmp);
350003b705cfSriastradh
350103b705cfSriastradh	gen6_get_rectangles(sna, &tmp, 1, gen6_emit_fill_state);
350203b705cfSriastradh
350303b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
350403b705cfSriastradh	sna->render.vertex_used += 6;
350503b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
350603b705cfSriastradh
350703b705cfSriastradh	v[0] = dst->drawable.width;
350803b705cfSriastradh	v[5] = v[1] = dst->drawable.height;
350903b705cfSriastradh	v[8] = v[4] = 0;
351003b705cfSriastradh	v[9] = 0;
351103b705cfSriastradh
351203b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
351303b705cfSriastradh	v[6] = v[10] = v[11] = 0;
351403b705cfSriastradh
351503b705cfSriastradh	gen4_vertex_flush(sna);
351603b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
351703b705cfSriastradh
351803b705cfSriastradh	return true;
351903b705cfSriastradh}
352003b705cfSriastradh
352103b705cfSriastradhstatic void gen6_render_flush(struct sna *sna)
352203b705cfSriastradh{
352303b705cfSriastradh	gen4_vertex_close(sna);
352403b705cfSriastradh
352503b705cfSriastradh	assert(sna->render.vb_id == 0);
352603b705cfSriastradh	assert(sna->render.vertex_offset == 0);
352703b705cfSriastradh}
352803b705cfSriastradh
352903b705cfSriastradhstatic void
353003b705cfSriastradhgen6_render_context_switch(struct kgem *kgem,
353103b705cfSriastradh			   int new_mode)
353203b705cfSriastradh{
353303b705cfSriastradh	if (kgem->nbatch) {
353403b705cfSriastradh		DBG(("%s: from %d to %d\n", __FUNCTION__, kgem->mode, new_mode));
353503b705cfSriastradh		_kgem_submit(kgem);
353603b705cfSriastradh	}
353703b705cfSriastradh
353803b705cfSriastradh	kgem->ring = new_mode;
353903b705cfSriastradh}
354003b705cfSriastradh
354103b705cfSriastradhstatic void
354203b705cfSriastradhgen6_render_retire(struct kgem *kgem)
354303b705cfSriastradh{
354403b705cfSriastradh	struct sna *sna;
354503b705cfSriastradh
354603b705cfSriastradh	if (kgem->ring && (kgem->has_semaphores || !kgem->need_retire))
354703b705cfSriastradh		kgem->ring = kgem->mode;
354803b705cfSriastradh
354903b705cfSriastradh	sna = container_of(kgem, struct sna, kgem);
355003b705cfSriastradh	if (kgem->nbatch == 0 && sna->render.vbo && !kgem_bo_is_busy(sna->render.vbo)) {
355103b705cfSriastradh		DBG(("%s: resetting idle vbo handle=%d\n", __FUNCTION__, sna->render.vbo->handle));
355203b705cfSriastradh		sna->render.vertex_used = 0;
355303b705cfSriastradh		sna->render.vertex_index = 0;
355403b705cfSriastradh	}
355503b705cfSriastradh}
355603b705cfSriastradh
355703b705cfSriastradhstatic void
355803b705cfSriastradhgen6_render_expire(struct kgem *kgem)
355903b705cfSriastradh{
356003b705cfSriastradh	struct sna *sna;
356103b705cfSriastradh
356203b705cfSriastradh	sna = container_of(kgem, struct sna, kgem);
356303b705cfSriastradh	if (sna->render.vbo && !sna->render.vertex_used) {
356403b705cfSriastradh		DBG(("%s: discarding vbo handle=%d\n", __FUNCTION__, sna->render.vbo->handle));
356503b705cfSriastradh		kgem_bo_destroy(kgem, sna->render.vbo);
356603b705cfSriastradh		assert(!sna->render.active);
356703b705cfSriastradh		sna->render.vbo = NULL;
356803b705cfSriastradh		sna->render.vertices = sna->render.vertex_data;
356903b705cfSriastradh		sna->render.vertex_size = ARRAY_SIZE(sna->render.vertex_data);
357003b705cfSriastradh		sna->render.vertex_used = 0;
357103b705cfSriastradh		sna->render.vertex_index = 0;
357203b705cfSriastradh	}
357303b705cfSriastradh}
357403b705cfSriastradh
357503b705cfSriastradhstatic void gen6_render_reset(struct sna *sna)
357603b705cfSriastradh{
357703b705cfSriastradh	sna->render_state.gen6.needs_invariant = true;
357803b705cfSriastradh	sna->render_state.gen6.first_state_packet = true;
357903b705cfSriastradh	sna->render_state.gen6.ve_id = 3 << 2;
358003b705cfSriastradh	sna->render_state.gen6.last_primitive = -1;
358103b705cfSriastradh
358203b705cfSriastradh	sna->render_state.gen6.num_sf_outputs = 0;
358303b705cfSriastradh	sna->render_state.gen6.samplers = -1;
358403b705cfSriastradh	sna->render_state.gen6.blend = -1;
358503b705cfSriastradh	sna->render_state.gen6.kernel = -1;
358603b705cfSriastradh	sna->render_state.gen6.drawrect_offset = -1;
358703b705cfSriastradh	sna->render_state.gen6.drawrect_limit = -1;
358803b705cfSriastradh	sna->render_state.gen6.surface_table = -1;
358903b705cfSriastradh
359003b705cfSriastradh	sna->render.vertex_offset = 0;
359103b705cfSriastradh	sna->render.nvertex_reloc = 0;
359203b705cfSriastradh	sna->render.vb_id = 0;
359303b705cfSriastradh}
359403b705cfSriastradh
359503b705cfSriastradhstatic void gen6_render_fini(struct sna *sna)
359603b705cfSriastradh{
359703b705cfSriastradh	kgem_bo_destroy(&sna->kgem, sna->render_state.gen6.general_bo);
359803b705cfSriastradh}
359903b705cfSriastradh
360003b705cfSriastradhstatic bool is_gt2(struct sna *sna)
360103b705cfSriastradh{
360203b705cfSriastradh	return sna->PciInfo->device_id & 0x30;
360303b705cfSriastradh}
360403b705cfSriastradh
360503b705cfSriastradhstatic bool is_mobile(struct sna *sna)
360603b705cfSriastradh{
360703b705cfSriastradh	return (sna->PciInfo->device_id & 0xf) == 0x6;
360803b705cfSriastradh}
360903b705cfSriastradh
361003b705cfSriastradhstatic bool gen6_render_setup(struct sna *sna)
361103b705cfSriastradh{
361203b705cfSriastradh	struct gen6_render_state *state = &sna->render_state.gen6;
361303b705cfSriastradh	struct sna_static_stream general;
361403b705cfSriastradh	struct gen6_sampler_state *ss;
361503b705cfSriastradh	int i, j, k, l, m;
361603b705cfSriastradh
361703b705cfSriastradh	state->info = &gt1_info;
361803b705cfSriastradh	if (is_gt2(sna))
361903b705cfSriastradh		state->info = &gt2_info; /* XXX requires GT_MODE WiZ disabled */
362003b705cfSriastradh
362103b705cfSriastradh	sna_static_stream_init(&general);
362203b705cfSriastradh
362303b705cfSriastradh	/* Zero pad the start. If you see an offset of 0x0 in the batchbuffer
362403b705cfSriastradh	 * dumps, you know it points to zero.
362503b705cfSriastradh	 */
362603b705cfSriastradh	null_create(&general);
362703b705cfSriastradh	scratch_create(&general);
362803b705cfSriastradh
362903b705cfSriastradh	for (m = 0; m < GEN6_KERNEL_COUNT; m++) {
363003b705cfSriastradh		if (wm_kernels[m].size) {
363103b705cfSriastradh			state->wm_kernel[m][1] =
363203b705cfSriastradh				sna_static_stream_add(&general,
363303b705cfSriastradh						      wm_kernels[m].data,
363403b705cfSriastradh						      wm_kernels[m].size,
363503b705cfSriastradh						      64);
363603b705cfSriastradh		} else {
363703b705cfSriastradh			if (USE_8_PIXEL_DISPATCH) {
363803b705cfSriastradh				state->wm_kernel[m][0] =
363903b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
364003b705cfSriastradh								     wm_kernels[m].data, 8);
364103b705cfSriastradh			}
364203b705cfSriastradh
364303b705cfSriastradh			if (USE_16_PIXEL_DISPATCH) {
364403b705cfSriastradh				state->wm_kernel[m][1] =
364503b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
364603b705cfSriastradh								     wm_kernels[m].data, 16);
364703b705cfSriastradh			}
364803b705cfSriastradh
364903b705cfSriastradh			if (USE_32_PIXEL_DISPATCH) {
365003b705cfSriastradh				state->wm_kernel[m][2] =
365103b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
365203b705cfSriastradh								     wm_kernels[m].data, 32);
365303b705cfSriastradh			}
365403b705cfSriastradh		}
365503b705cfSriastradh		if ((state->wm_kernel[m][0]|state->wm_kernel[m][1]|state->wm_kernel[m][2]) == 0) {
365603b705cfSriastradh			state->wm_kernel[m][1] =
365703b705cfSriastradh				sna_static_stream_compile_wm(sna, &general,
365803b705cfSriastradh							     wm_kernels[m].data, 16);
365903b705cfSriastradh		}
366003b705cfSriastradh	}
366103b705cfSriastradh
366203b705cfSriastradh	ss = sna_static_stream_map(&general,
366303b705cfSriastradh				   2 * sizeof(*ss) *
366403b705cfSriastradh				   (2 +
366503b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT *
366603b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT),
366703b705cfSriastradh				   32);
366803b705cfSriastradh	state->wm_state = sna_static_stream_offsetof(&general, ss);
366903b705cfSriastradh	sampler_copy_init(ss); ss += 2;
367003b705cfSriastradh	sampler_fill_init(ss); ss += 2;
367103b705cfSriastradh	for (i = 0; i < FILTER_COUNT; i++) {
367203b705cfSriastradh		for (j = 0; j < EXTEND_COUNT; j++) {
367303b705cfSriastradh			for (k = 0; k < FILTER_COUNT; k++) {
367403b705cfSriastradh				for (l = 0; l < EXTEND_COUNT; l++) {
367503b705cfSriastradh					sampler_state_init(ss++, i, j);
367603b705cfSriastradh					sampler_state_init(ss++, k, l);
367703b705cfSriastradh				}
367803b705cfSriastradh			}
367903b705cfSriastradh		}
368003b705cfSriastradh	}
368103b705cfSriastradh
368203b705cfSriastradh	state->cc_blend = gen6_composite_create_blend_state(&general);
368303b705cfSriastradh
368403b705cfSriastradh	state->general_bo = sna_static_stream_fini(sna, &general);
368503b705cfSriastradh	return state->general_bo != NULL;
368603b705cfSriastradh}
368703b705cfSriastradh
368803b705cfSriastradhconst char *gen6_render_init(struct sna *sna, const char *backend)
368903b705cfSriastradh{
369003b705cfSriastradh	if (!gen6_render_setup(sna))
369103b705cfSriastradh		return backend;
369203b705cfSriastradh
369303b705cfSriastradh	sna->kgem.context_switch = gen6_render_context_switch;
369403b705cfSriastradh	sna->kgem.retire = gen6_render_retire;
369503b705cfSriastradh	sna->kgem.expire = gen6_render_expire;
369603b705cfSriastradh
369703b705cfSriastradh#if !NO_COMPOSITE
369803b705cfSriastradh	sna->render.composite = gen6_render_composite;
369903b705cfSriastradh	sna->render.prefer_gpu |= PREFER_GPU_RENDER;
370003b705cfSriastradh#endif
370103b705cfSriastradh
370203b705cfSriastradh#if !NO_COMPOSITE_SPANS
370303b705cfSriastradh	sna->render.check_composite_spans = gen6_check_composite_spans;
370403b705cfSriastradh	sna->render.composite_spans = gen6_render_composite_spans;
370503b705cfSriastradh	if (is_mobile(sna))
370603b705cfSriastradh		sna->render.prefer_gpu |= PREFER_GPU_SPANS;
370703b705cfSriastradh#endif
370803b705cfSriastradh	sna->render.video = gen6_render_video;
370903b705cfSriastradh
371003b705cfSriastradh#if !NO_COPY_BOXES
371103b705cfSriastradh	sna->render.copy_boxes = gen6_render_copy_boxes;
371203b705cfSriastradh#endif
371303b705cfSriastradh#if !NO_COPY
371403b705cfSriastradh	sna->render.copy = gen6_render_copy;
371503b705cfSriastradh#endif
371603b705cfSriastradh
371703b705cfSriastradh#if !NO_FILL_BOXES
371803b705cfSriastradh	sna->render.fill_boxes = gen6_render_fill_boxes;
371903b705cfSriastradh#endif
372003b705cfSriastradh#if !NO_FILL
372103b705cfSriastradh	sna->render.fill = gen6_render_fill;
372203b705cfSriastradh#endif
372303b705cfSriastradh#if !NO_FILL_ONE
372403b705cfSriastradh	sna->render.fill_one = gen6_render_fill_one;
372503b705cfSriastradh#endif
372603b705cfSriastradh#if !NO_FILL_CLEAR
372703b705cfSriastradh	sna->render.clear = gen6_render_clear;
372803b705cfSriastradh#endif
372903b705cfSriastradh
373003b705cfSriastradh	sna->render.flush = gen6_render_flush;
373103b705cfSriastradh	sna->render.reset = gen6_render_reset;
373203b705cfSriastradh	sna->render.fini = gen6_render_fini;
373303b705cfSriastradh
373403b705cfSriastradh	sna->render.max_3d_size = GEN6_MAX_SIZE;
373503b705cfSriastradh	sna->render.max_3d_pitch = 1 << 18;
373603b705cfSriastradh	return sna->render_state.gen6.info->name;
373703b705cfSriastradh}
3738