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