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
5013496ba1Ssnj#define ALWAYS_INVALIDATE 0
5113496ba1Ssnj#define ALWAYS_FLUSH 0
5213496ba1Ssnj#define ALWAYS_STALL 0
5313496ba1Ssnj
5403b705cfSriastradh#define NO_COMPOSITE 0
5503b705cfSriastradh#define NO_COMPOSITE_SPANS 0
5603b705cfSriastradh#define NO_COPY 0
5703b705cfSriastradh#define NO_COPY_BOXES 0
5803b705cfSriastradh#define NO_FILL 0
5903b705cfSriastradh#define NO_FILL_BOXES 0
6003b705cfSriastradh#define NO_FILL_ONE 0
6103b705cfSriastradh#define NO_FILL_CLEAR 0
6203b705cfSriastradh
6303b705cfSriastradh#define USE_8_PIXEL_DISPATCH 1
6403b705cfSriastradh#define USE_16_PIXEL_DISPATCH 1
6503b705cfSriastradh#define USE_32_PIXEL_DISPATCH 0
6603b705cfSriastradh
6703b705cfSriastradh#if !USE_8_PIXEL_DISPATCH && !USE_16_PIXEL_DISPATCH && !USE_32_PIXEL_DISPATCH
6803b705cfSriastradh#error "Must select at least 8, 16 or 32 pixel dispatch"
6903b705cfSriastradh#endif
7003b705cfSriastradh
7103b705cfSriastradh#define GEN6_MAX_SIZE 8192
7203b705cfSriastradh
7303b705cfSriastradhstruct gt_info {
7403b705cfSriastradh	const char *name;
7503b705cfSriastradh	int max_vs_threads;
7603b705cfSriastradh	int max_gs_threads;
7703b705cfSriastradh	int max_wm_threads;
7803b705cfSriastradh	struct {
7903b705cfSriastradh		int size;
8003b705cfSriastradh		int max_vs_entries;
8103b705cfSriastradh		int max_gs_entries;
8203b705cfSriastradh	} urb;
8342542f5fSchristos	int gt;
8403b705cfSriastradh};
8503b705cfSriastradh
8603b705cfSriastradhstatic const struct gt_info gt1_info = {
8703b705cfSriastradh	.name = "Sandybridge (gen6, gt1)",
8803b705cfSriastradh	.max_vs_threads = 24,
8903b705cfSriastradh	.max_gs_threads = 21,
9003b705cfSriastradh	.max_wm_threads = 40,
9103b705cfSriastradh	.urb = { 32, 256, 256 },
9242542f5fSchristos	.gt = 1,
9303b705cfSriastradh};
9403b705cfSriastradh
9503b705cfSriastradhstatic const struct gt_info gt2_info = {
9603b705cfSriastradh	.name = "Sandybridge (gen6, gt2)",
9703b705cfSriastradh	.max_vs_threads = 60,
9803b705cfSriastradh	.max_gs_threads = 60,
9903b705cfSriastradh	.max_wm_threads = 80,
10003b705cfSriastradh	.urb = { 64, 256, 256 },
10142542f5fSchristos	.gt = 2,
10203b705cfSriastradh};
10303b705cfSriastradh
104fe8aea9eSmrgstatic const uint32_t ps_kernel_packed_bt601[][4] = {
10503b705cfSriastradh#include "exa_wm_src_affine.g6b"
10603b705cfSriastradh#include "exa_wm_src_sample_argb.g6b"
107fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt601.g6b"
10803b705cfSriastradh#include "exa_wm_write.g6b"
10903b705cfSriastradh};
11003b705cfSriastradh
111fe8aea9eSmrgstatic const uint32_t ps_kernel_planar_bt601[][4] = {
11203b705cfSriastradh#include "exa_wm_src_affine.g6b"
11303b705cfSriastradh#include "exa_wm_src_sample_planar.g6b"
114fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt601.g6b"
115fe8aea9eSmrg#include "exa_wm_write.g6b"
116fe8aea9eSmrg};
117fe8aea9eSmrg
118fe8aea9eSmrgstatic const uint32_t ps_kernel_nv12_bt601[][4] = {
119fe8aea9eSmrg#include "exa_wm_src_affine.g6b"
120fe8aea9eSmrg#include "exa_wm_src_sample_nv12.g6b"
121fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt601.g6b"
122fe8aea9eSmrg#include "exa_wm_write.g6b"
123fe8aea9eSmrg};
124fe8aea9eSmrg
125fe8aea9eSmrgstatic const uint32_t ps_kernel_packed_bt709[][4] = {
126fe8aea9eSmrg#include "exa_wm_src_affine.g6b"
127fe8aea9eSmrg#include "exa_wm_src_sample_argb.g6b"
128fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt709.g6b"
129fe8aea9eSmrg#include "exa_wm_write.g6b"
130fe8aea9eSmrg};
131fe8aea9eSmrg
132fe8aea9eSmrgstatic const uint32_t ps_kernel_planar_bt709[][4] = {
133fe8aea9eSmrg#include "exa_wm_src_affine.g6b"
134fe8aea9eSmrg#include "exa_wm_src_sample_planar.g6b"
135fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt709.g6b"
136fe8aea9eSmrg#include "exa_wm_write.g6b"
137fe8aea9eSmrg};
138fe8aea9eSmrg
139fe8aea9eSmrgstatic const uint32_t ps_kernel_nv12_bt709[][4] = {
140fe8aea9eSmrg#include "exa_wm_src_affine.g6b"
141fe8aea9eSmrg#include "exa_wm_src_sample_nv12.g6b"
142fe8aea9eSmrg#include "exa_wm_yuv_rgb_bt709.g6b"
14303b705cfSriastradh#include "exa_wm_write.g6b"
14403b705cfSriastradh};
14503b705cfSriastradh
14603b705cfSriastradh#define NOKERNEL(kernel_enum, func, ns) \
14703b705cfSriastradh    [GEN6_WM_KERNEL_##kernel_enum] = {#kernel_enum, func, 0, ns}
14803b705cfSriastradh#define KERNEL(kernel_enum, kernel, ns) \
14903b705cfSriastradh    [GEN6_WM_KERNEL_##kernel_enum] = {#kernel_enum, kernel, sizeof(kernel), ns}
15003b705cfSriastradh
15103b705cfSriastradhstatic const struct wm_kernel_info {
15203b705cfSriastradh	const char *name;
15303b705cfSriastradh	const void *data;
15403b705cfSriastradh	unsigned int size;
15503b705cfSriastradh	unsigned int num_surfaces;
15603b705cfSriastradh} wm_kernels[] = {
15703b705cfSriastradh	NOKERNEL(NOMASK, brw_wm_kernel__affine, 2),
15803b705cfSriastradh	NOKERNEL(NOMASK_P, brw_wm_kernel__projective, 2),
15903b705cfSriastradh
16003b705cfSriastradh	NOKERNEL(MASK, brw_wm_kernel__affine_mask, 3),
16103b705cfSriastradh	NOKERNEL(MASK_P, brw_wm_kernel__projective_mask, 3),
16203b705cfSriastradh
16303b705cfSriastradh	NOKERNEL(MASKCA, brw_wm_kernel__affine_mask_ca, 3),
16403b705cfSriastradh	NOKERNEL(MASKCA_P, brw_wm_kernel__projective_mask_ca, 3),
16503b705cfSriastradh
16603b705cfSriastradh	NOKERNEL(MASKSA, brw_wm_kernel__affine_mask_sa, 3),
16703b705cfSriastradh	NOKERNEL(MASKSA_P, brw_wm_kernel__projective_mask_sa, 3),
16803b705cfSriastradh
16903b705cfSriastradh	NOKERNEL(OPACITY, brw_wm_kernel__affine_opacity, 2),
17003b705cfSriastradh	NOKERNEL(OPACITY_P, brw_wm_kernel__projective_opacity, 2),
17103b705cfSriastradh
172fe8aea9eSmrg
173fe8aea9eSmrg	KERNEL(VIDEO_PLANAR_BT601, ps_kernel_planar_bt601, 7),
174fe8aea9eSmrg	KERNEL(VIDEO_NV12_BT601, ps_kernel_nv12_bt601, 7),
175fe8aea9eSmrg	KERNEL(VIDEO_PACKED_BT601, ps_kernel_packed_bt601, 2),
176fe8aea9eSmrg
177fe8aea9eSmrg	KERNEL(VIDEO_PLANAR_BT709, ps_kernel_planar_bt709, 7),
178fe8aea9eSmrg	KERNEL(VIDEO_NV12_BT709, ps_kernel_nv12_bt709, 7),
179fe8aea9eSmrg	KERNEL(VIDEO_PACKED_BT709, ps_kernel_packed_bt709, 2),
18003b705cfSriastradh};
18103b705cfSriastradh#undef KERNEL
18203b705cfSriastradh
18303b705cfSriastradhstatic const struct blendinfo {
18403b705cfSriastradh	bool src_alpha;
18503b705cfSriastradh	uint32_t src_blend;
18603b705cfSriastradh	uint32_t dst_blend;
18703b705cfSriastradh} gen6_blend_op[] = {
18803b705cfSriastradh	/* Clear */	{0, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ZERO},
18903b705cfSriastradh	/* Src */	{0, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ZERO},
19003b705cfSriastradh	/* Dst */	{0, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ONE},
19103b705cfSriastradh	/* Over */	{1, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
19203b705cfSriastradh	/* OverReverse */ {0, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_ONE},
19303b705cfSriastradh	/* In */	{0, GEN6_BLENDFACTOR_DST_ALPHA, GEN6_BLENDFACTOR_ZERO},
19403b705cfSriastradh	/* InReverse */	{1, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_SRC_ALPHA},
19503b705cfSriastradh	/* Out */	{0, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_ZERO},
19603b705cfSriastradh	/* OutReverse */ {1, GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
19703b705cfSriastradh	/* Atop */	{1, GEN6_BLENDFACTOR_DST_ALPHA, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
19803b705cfSriastradh	/* AtopReverse */ {1, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_SRC_ALPHA},
19903b705cfSriastradh	/* Xor */	{1, GEN6_BLENDFACTOR_INV_DST_ALPHA, GEN6_BLENDFACTOR_INV_SRC_ALPHA},
20003b705cfSriastradh	/* Add */	{0, GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ONE},
20103b705cfSriastradh};
20203b705cfSriastradh
20303b705cfSriastradh/**
20403b705cfSriastradh * Highest-valued BLENDFACTOR used in gen6_blend_op.
20503b705cfSriastradh *
20603b705cfSriastradh * This leaves out GEN6_BLENDFACTOR_INV_DST_COLOR,
20703b705cfSriastradh * GEN6_BLENDFACTOR_INV_CONST_{COLOR,ALPHA},
20803b705cfSriastradh * GEN6_BLENDFACTOR_INV_SRC1_{COLOR,ALPHA}
20903b705cfSriastradh */
21003b705cfSriastradh#define GEN6_BLENDFACTOR_COUNT (GEN6_BLENDFACTOR_INV_DST_ALPHA + 1)
21103b705cfSriastradh
21203b705cfSriastradh#define GEN6_BLEND_STATE_PADDED_SIZE	ALIGN(sizeof(struct gen6_blend_state), 64)
21303b705cfSriastradh
21403b705cfSriastradh#define BLEND_OFFSET(s, d) \
21503b705cfSriastradh	(((s) * GEN6_BLENDFACTOR_COUNT + (d)) * GEN6_BLEND_STATE_PADDED_SIZE)
21603b705cfSriastradh
21703b705cfSriastradh#define NO_BLEND BLEND_OFFSET(GEN6_BLENDFACTOR_ONE, GEN6_BLENDFACTOR_ZERO)
21803b705cfSriastradh#define CLEAR BLEND_OFFSET(GEN6_BLENDFACTOR_ZERO, GEN6_BLENDFACTOR_ZERO)
21903b705cfSriastradh
22003b705cfSriastradh#define SAMPLER_OFFSET(sf, se, mf, me) \
22103b705cfSriastradh	(((((sf) * EXTEND_COUNT + (se)) * FILTER_COUNT + (mf)) * EXTEND_COUNT + (me) + 2) * 2 * sizeof(struct gen6_sampler_state))
22203b705cfSriastradh
22303b705cfSriastradh#define VERTEX_2s2s 0
22403b705cfSriastradh
22503b705cfSriastradh#define COPY_SAMPLER 0
22603b705cfSriastradh#define COPY_VERTEX VERTEX_2s2s
22703b705cfSriastradh#define COPY_FLAGS(a) GEN6_SET_FLAGS(COPY_SAMPLER, (a) == GXcopy ? NO_BLEND : CLEAR, GEN6_WM_KERNEL_NOMASK, COPY_VERTEX)
22803b705cfSriastradh
22903b705cfSriastradh#define FILL_SAMPLER (2 * sizeof(struct gen6_sampler_state))
23003b705cfSriastradh#define FILL_VERTEX VERTEX_2s2s
23103b705cfSriastradh#define FILL_FLAGS(op, format) GEN6_SET_FLAGS(FILL_SAMPLER, gen6_get_blend((op), false, (format)), GEN6_WM_KERNEL_NOMASK, FILL_VERTEX)
23203b705cfSriastradh#define FILL_FLAGS_NOBLEND GEN6_SET_FLAGS(FILL_SAMPLER, NO_BLEND, GEN6_WM_KERNEL_NOMASK, FILL_VERTEX)
23303b705cfSriastradh
23403b705cfSriastradh#define GEN6_SAMPLER(f) (((f) >> 16) & 0xfff0)
23503b705cfSriastradh#define GEN6_BLEND(f) (((f) >> 0) & 0xfff0)
23603b705cfSriastradh#define GEN6_KERNEL(f) (((f) >> 16) & 0xf)
23703b705cfSriastradh#define GEN6_VERTEX(f) (((f) >> 0) & 0xf)
23803b705cfSriastradh#define GEN6_SET_FLAGS(S, B, K, V)  (((S) | (K)) << 16 | ((B) | (V)))
23903b705cfSriastradh
24003b705cfSriastradh#define OUT_BATCH(v) batch_emit(sna, v)
24103b705cfSriastradh#define OUT_VERTEX(x,y) vertex_emit_2s(sna, x,y)
24203b705cfSriastradh#define OUT_VERTEX_F(v) vertex_emit(sna, v)
24303b705cfSriastradh
24403b705cfSriastradhstatic inline bool too_large(int width, int height)
24503b705cfSriastradh{
24603b705cfSriastradh	return width > GEN6_MAX_SIZE || height > GEN6_MAX_SIZE;
24703b705cfSriastradh}
24803b705cfSriastradh
24903b705cfSriastradhstatic uint32_t gen6_get_blend(int op,
25003b705cfSriastradh			       bool has_component_alpha,
25103b705cfSriastradh			       uint32_t dst_format)
25203b705cfSriastradh{
25303b705cfSriastradh	uint32_t src, dst;
25403b705cfSriastradh
25503b705cfSriastradh	src = gen6_blend_op[op].src_blend;
25603b705cfSriastradh	dst = gen6_blend_op[op].dst_blend;
25703b705cfSriastradh
25803b705cfSriastradh	/* If there's no dst alpha channel, adjust the blend op so that
25903b705cfSriastradh	 * we'll treat it always as 1.
26003b705cfSriastradh	 */
26103b705cfSriastradh	if (PICT_FORMAT_A(dst_format) == 0) {
26203b705cfSriastradh		if (src == GEN6_BLENDFACTOR_DST_ALPHA)
26303b705cfSriastradh			src = GEN6_BLENDFACTOR_ONE;
26403b705cfSriastradh		else if (src == GEN6_BLENDFACTOR_INV_DST_ALPHA)
26503b705cfSriastradh			src = GEN6_BLENDFACTOR_ZERO;
26603b705cfSriastradh	}
26703b705cfSriastradh
26803b705cfSriastradh	/* If the source alpha is being used, then we should only be in a
26903b705cfSriastradh	 * case where the source blend factor is 0, and the source blend
27003b705cfSriastradh	 * value is the mask channels multiplied by the source picture's alpha.
27103b705cfSriastradh	 */
27203b705cfSriastradh	if (has_component_alpha && gen6_blend_op[op].src_alpha) {
27303b705cfSriastradh		if (dst == GEN6_BLENDFACTOR_SRC_ALPHA)
27403b705cfSriastradh			dst = GEN6_BLENDFACTOR_SRC_COLOR;
27503b705cfSriastradh		else if (dst == GEN6_BLENDFACTOR_INV_SRC_ALPHA)
27603b705cfSriastradh			dst = GEN6_BLENDFACTOR_INV_SRC_COLOR;
27703b705cfSriastradh	}
27803b705cfSriastradh
27903b705cfSriastradh	DBG(("blend op=%d, dst=%x [A=%d] => src=%d, dst=%d => offset=%x\n",
28003b705cfSriastradh	     op, dst_format, PICT_FORMAT_A(dst_format),
28103b705cfSriastradh	     src, dst, (int)BLEND_OFFSET(src, dst)));
28203b705cfSriastradh	return BLEND_OFFSET(src, dst);
28303b705cfSriastradh}
28403b705cfSriastradh
28503b705cfSriastradhstatic uint32_t gen6_get_card_format(PictFormat format)
28603b705cfSriastradh{
28703b705cfSriastradh	switch (format) {
28803b705cfSriastradh	default:
28903b705cfSriastradh		return -1;
29003b705cfSriastradh	case PICT_a8r8g8b8:
29103b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8A8_UNORM;
29203b705cfSriastradh	case PICT_x8r8g8b8:
29303b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8X8_UNORM;
29403b705cfSriastradh	case PICT_a8b8g8r8:
29503b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8A8_UNORM;
29603b705cfSriastradh	case PICT_x8b8g8r8:
29703b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8X8_UNORM;
298fe8aea9eSmrg#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,6,99,900,0)
29903b705cfSriastradh	case PICT_a2r10g10b10:
30003b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10A2_UNORM;
30103b705cfSriastradh	case PICT_x2r10g10b10:
30203b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10X2_UNORM;
30342542f5fSchristos#endif
30403b705cfSriastradh	case PICT_r8g8b8:
30503b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8_UNORM;
30603b705cfSriastradh	case PICT_r5g6b5:
30703b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G6R5_UNORM;
30803b705cfSriastradh	case PICT_a1r5g5b5:
30903b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G5R5A1_UNORM;
31003b705cfSriastradh	case PICT_a8:
31103b705cfSriastradh		return GEN6_SURFACEFORMAT_A8_UNORM;
31203b705cfSriastradh	case PICT_a4r4g4b4:
31303b705cfSriastradh		return GEN6_SURFACEFORMAT_B4G4R4A4_UNORM;
31403b705cfSriastradh	}
31503b705cfSriastradh}
31603b705cfSriastradh
31703b705cfSriastradhstatic uint32_t gen6_get_dest_format(PictFormat format)
31803b705cfSriastradh{
31903b705cfSriastradh	switch (format) {
32003b705cfSriastradh	default:
32103b705cfSriastradh		return -1;
32203b705cfSriastradh	case PICT_a8r8g8b8:
32303b705cfSriastradh	case PICT_x8r8g8b8:
32403b705cfSriastradh		return GEN6_SURFACEFORMAT_B8G8R8A8_UNORM;
32503b705cfSriastradh	case PICT_a8b8g8r8:
32603b705cfSriastradh	case PICT_x8b8g8r8:
32703b705cfSriastradh		return GEN6_SURFACEFORMAT_R8G8B8A8_UNORM;
328fe8aea9eSmrg#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,6,99,900,0)
32903b705cfSriastradh	case PICT_a2r10g10b10:
33003b705cfSriastradh	case PICT_x2r10g10b10:
33103b705cfSriastradh		return GEN6_SURFACEFORMAT_B10G10R10A2_UNORM;
33242542f5fSchristos#endif
33303b705cfSriastradh	case PICT_r5g6b5:
33403b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G6R5_UNORM;
33503b705cfSriastradh	case PICT_x1r5g5b5:
33603b705cfSriastradh	case PICT_a1r5g5b5:
33703b705cfSriastradh		return GEN6_SURFACEFORMAT_B5G5R5A1_UNORM;
33803b705cfSriastradh	case PICT_a8:
33903b705cfSriastradh		return GEN6_SURFACEFORMAT_A8_UNORM;
34003b705cfSriastradh	case PICT_a4r4g4b4:
34103b705cfSriastradh	case PICT_x4r4g4b4:
34203b705cfSriastradh		return GEN6_SURFACEFORMAT_B4G4R4A4_UNORM;
34303b705cfSriastradh	}
34403b705cfSriastradh}
34503b705cfSriastradh
34603b705cfSriastradhstatic bool gen6_check_dst_format(PictFormat format)
34703b705cfSriastradh{
34803b705cfSriastradh	if (gen6_get_dest_format(format) != -1)
34903b705cfSriastradh		return true;
35003b705cfSriastradh
35103b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
35203b705cfSriastradh	return false;
35303b705cfSriastradh}
35403b705cfSriastradh
35503b705cfSriastradhstatic bool gen6_check_format(uint32_t format)
35603b705cfSriastradh{
35703b705cfSriastradh	if (gen6_get_card_format(format) != -1)
35803b705cfSriastradh		return true;
35903b705cfSriastradh
36003b705cfSriastradh	DBG(("%s: unhandled format: %x\n", __FUNCTION__, (int)format));
36103b705cfSriastradh	return false;
36203b705cfSriastradh}
36303b705cfSriastradh
36403b705cfSriastradhstatic uint32_t gen6_filter(uint32_t filter)
36503b705cfSriastradh{
36603b705cfSriastradh	switch (filter) {
36703b705cfSriastradh	default:
36803b705cfSriastradh		assert(0);
36903b705cfSriastradh	case PictFilterNearest:
37003b705cfSriastradh		return SAMPLER_FILTER_NEAREST;
37103b705cfSriastradh	case PictFilterBilinear:
37203b705cfSriastradh		return SAMPLER_FILTER_BILINEAR;
37303b705cfSriastradh	}
37403b705cfSriastradh}
37503b705cfSriastradh
37603b705cfSriastradhstatic uint32_t gen6_check_filter(PicturePtr picture)
37703b705cfSriastradh{
37803b705cfSriastradh	switch (picture->filter) {
37903b705cfSriastradh	case PictFilterNearest:
38003b705cfSriastradh	case PictFilterBilinear:
38103b705cfSriastradh		return true;
38203b705cfSriastradh	default:
38303b705cfSriastradh		return false;
38403b705cfSriastradh	}
38503b705cfSriastradh}
38603b705cfSriastradh
38703b705cfSriastradhstatic uint32_t gen6_repeat(uint32_t repeat)
38803b705cfSriastradh{
38903b705cfSriastradh	switch (repeat) {
39003b705cfSriastradh	default:
39103b705cfSriastradh		assert(0);
39203b705cfSriastradh	case RepeatNone:
39303b705cfSriastradh		return SAMPLER_EXTEND_NONE;
39403b705cfSriastradh	case RepeatNormal:
39503b705cfSriastradh		return SAMPLER_EXTEND_REPEAT;
39603b705cfSriastradh	case RepeatPad:
39703b705cfSriastradh		return SAMPLER_EXTEND_PAD;
39803b705cfSriastradh	case RepeatReflect:
39903b705cfSriastradh		return SAMPLER_EXTEND_REFLECT;
40003b705cfSriastradh	}
40103b705cfSriastradh}
40203b705cfSriastradh
40303b705cfSriastradhstatic bool gen6_check_repeat(PicturePtr picture)
40403b705cfSriastradh{
40503b705cfSriastradh	if (!picture->repeat)
40603b705cfSriastradh		return true;
40703b705cfSriastradh
40803b705cfSriastradh	switch (picture->repeatType) {
40903b705cfSriastradh	case RepeatNone:
41003b705cfSriastradh	case RepeatNormal:
41103b705cfSriastradh	case RepeatPad:
41203b705cfSriastradh	case RepeatReflect:
41303b705cfSriastradh		return true;
41403b705cfSriastradh	default:
41503b705cfSriastradh		return false;
41603b705cfSriastradh	}
41703b705cfSriastradh}
41803b705cfSriastradh
41903b705cfSriastradhstatic int
42003b705cfSriastradhgen6_choose_composite_kernel(int op, bool has_mask, bool is_ca, bool is_affine)
42103b705cfSriastradh{
42203b705cfSriastradh	int base;
42303b705cfSriastradh
42403b705cfSriastradh	if (has_mask) {
42503b705cfSriastradh		if (is_ca) {
42603b705cfSriastradh			if (gen6_blend_op[op].src_alpha)
42703b705cfSriastradh				base = GEN6_WM_KERNEL_MASKSA;
42803b705cfSriastradh			else
42903b705cfSriastradh				base = GEN6_WM_KERNEL_MASKCA;
43003b705cfSriastradh		} else
43103b705cfSriastradh			base = GEN6_WM_KERNEL_MASK;
43203b705cfSriastradh	} else
43303b705cfSriastradh		base = GEN6_WM_KERNEL_NOMASK;
43403b705cfSriastradh
43503b705cfSriastradh	return base + !is_affine;
43603b705cfSriastradh}
43703b705cfSriastradh
43813496ba1Ssnjinline static void
43913496ba1Ssnjgen6_emit_pipe_invalidate(struct sna *sna)
44013496ba1Ssnj{
44113496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
44213496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL_WC_FLUSH |
44313496ba1Ssnj		  GEN6_PIPE_CONTROL_TC_FLUSH |
44413496ba1Ssnj		  GEN6_PIPE_CONTROL_CS_STALL);
44513496ba1Ssnj	OUT_BATCH(0);
44613496ba1Ssnj	OUT_BATCH(0);
44713496ba1Ssnj}
44813496ba1Ssnj
44913496ba1Ssnjinline static void
45013496ba1Ssnjgen6_emit_pipe_flush(struct sna *sna, bool need_stall)
45113496ba1Ssnj{
45213496ba1Ssnj	unsigned stall;
45313496ba1Ssnj
45413496ba1Ssnj	stall = 0;
45513496ba1Ssnj	if (need_stall)
45613496ba1Ssnj		stall = GEN6_PIPE_CONTROL_CS_STALL;
45713496ba1Ssnj
45813496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
45913496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL_WC_FLUSH | stall);
46013496ba1Ssnj	OUT_BATCH(0);
46113496ba1Ssnj	OUT_BATCH(0);
46213496ba1Ssnj}
46313496ba1Ssnj
46413496ba1Ssnjinline static void
46513496ba1Ssnjgen6_emit_pipe_stall(struct sna *sna)
46613496ba1Ssnj{
46713496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
46813496ba1Ssnj	OUT_BATCH(GEN6_PIPE_CONTROL_CS_STALL |
46913496ba1Ssnj		  GEN6_PIPE_CONTROL_STALL_AT_SCOREBOARD);
47013496ba1Ssnj	OUT_BATCH(0);
47113496ba1Ssnj	OUT_BATCH(0);
47213496ba1Ssnj}
47313496ba1Ssnj
47403b705cfSriastradhstatic void
47503b705cfSriastradhgen6_emit_urb(struct sna *sna)
47603b705cfSriastradh{
47703b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_URB | (3 - 2));
47803b705cfSriastradh	OUT_BATCH(((1 - 1) << GEN6_3DSTATE_URB_VS_SIZE_SHIFT) |
47903b705cfSriastradh		  (sna->render_state.gen6.info->urb.max_vs_entries << GEN6_3DSTATE_URB_VS_ENTRIES_SHIFT)); /* at least 24 on GEN6 */
48003b705cfSriastradh	OUT_BATCH((0 << GEN6_3DSTATE_URB_GS_SIZE_SHIFT) |
48103b705cfSriastradh		  (0 << GEN6_3DSTATE_URB_GS_ENTRIES_SHIFT)); /* no GS thread */
48203b705cfSriastradh}
48303b705cfSriastradh
48403b705cfSriastradhstatic void
48503b705cfSriastradhgen6_emit_state_base_address(struct sna *sna)
48603b705cfSriastradh{
48703b705cfSriastradh	OUT_BATCH(GEN6_STATE_BASE_ADDRESS | (10 - 2));
48803b705cfSriastradh	OUT_BATCH(0); /* general */
48903b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* surface */
49003b705cfSriastradh				 sna->kgem.nbatch,
49103b705cfSriastradh				 NULL,
49203b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
49303b705cfSriastradh				 BASE_ADDRESS_MODIFY));
49403b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, /* instruction */
49503b705cfSriastradh				 sna->kgem.nbatch,
49603b705cfSriastradh				 sna->render_state.gen6.general_bo,
49703b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
49803b705cfSriastradh				 BASE_ADDRESS_MODIFY));
49903b705cfSriastradh	OUT_BATCH(0); /* indirect */
50003b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem,
50103b705cfSriastradh				 sna->kgem.nbatch,
50203b705cfSriastradh				 sna->render_state.gen6.general_bo,
50303b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16,
50403b705cfSriastradh				 BASE_ADDRESS_MODIFY));
50503b705cfSriastradh
50603b705cfSriastradh	/* upper bounds, disable */
50703b705cfSriastradh	OUT_BATCH(0);
50803b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
50903b705cfSriastradh	OUT_BATCH(0);
51003b705cfSriastradh	OUT_BATCH(BASE_ADDRESS_MODIFY);
51103b705cfSriastradh}
51203b705cfSriastradh
51303b705cfSriastradhstatic void
51403b705cfSriastradhgen6_emit_viewports(struct sna *sna)
51503b705cfSriastradh{
51603b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VIEWPORT_STATE_POINTERS |
51703b705cfSriastradh		  GEN6_3DSTATE_VIEWPORT_STATE_MODIFY_CC |
51803b705cfSriastradh		  (4 - 2));
51903b705cfSriastradh	OUT_BATCH(0);
52003b705cfSriastradh	OUT_BATCH(0);
52103b705cfSriastradh	OUT_BATCH(0);
52203b705cfSriastradh}
52303b705cfSriastradh
52403b705cfSriastradhstatic void
52503b705cfSriastradhgen6_emit_vs(struct sna *sna)
52603b705cfSriastradh{
52703b705cfSriastradh	/* disable VS constant buffer */
52803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_VS | (5 - 2));
52903b705cfSriastradh	OUT_BATCH(0);
53003b705cfSriastradh	OUT_BATCH(0);
53103b705cfSriastradh	OUT_BATCH(0);
53203b705cfSriastradh	OUT_BATCH(0);
53303b705cfSriastradh
53403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VS | (6 - 2));
53503b705cfSriastradh	OUT_BATCH(0); /* no VS kernel */
53603b705cfSriastradh	OUT_BATCH(0);
53703b705cfSriastradh	OUT_BATCH(0);
53803b705cfSriastradh	OUT_BATCH(0);
53903b705cfSriastradh	OUT_BATCH(0); /* pass-through */
54003b705cfSriastradh}
54103b705cfSriastradh
54203b705cfSriastradhstatic void
54303b705cfSriastradhgen6_emit_gs(struct sna *sna)
54403b705cfSriastradh{
54503b705cfSriastradh	/* disable GS constant buffer */
54603b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_GS | (5 - 2));
54703b705cfSriastradh	OUT_BATCH(0);
54803b705cfSriastradh	OUT_BATCH(0);
54903b705cfSriastradh	OUT_BATCH(0);
55003b705cfSriastradh	OUT_BATCH(0);
55103b705cfSriastradh
55203b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_GS | (7 - 2));
55303b705cfSriastradh	OUT_BATCH(0); /* no GS kernel */
55403b705cfSriastradh	OUT_BATCH(0);
55503b705cfSriastradh	OUT_BATCH(0);
55603b705cfSriastradh	OUT_BATCH(0);
55703b705cfSriastradh	OUT_BATCH(0);
55803b705cfSriastradh	OUT_BATCH(0); /* pass-through */
55903b705cfSriastradh}
56003b705cfSriastradh
56103b705cfSriastradhstatic void
56203b705cfSriastradhgen6_emit_clip(struct sna *sna)
56303b705cfSriastradh{
56403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CLIP | (4 - 2));
56503b705cfSriastradh	OUT_BATCH(0);
56603b705cfSriastradh	OUT_BATCH(0); /* pass-through */
56703b705cfSriastradh	OUT_BATCH(0);
56803b705cfSriastradh}
56903b705cfSriastradh
57003b705cfSriastradhstatic void
57103b705cfSriastradhgen6_emit_wm_constants(struct sna *sna)
57203b705cfSriastradh{
57303b705cfSriastradh	/* disable WM constant buffer */
57403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CONSTANT_PS | (5 - 2));
57503b705cfSriastradh	OUT_BATCH(0);
57603b705cfSriastradh	OUT_BATCH(0);
57703b705cfSriastradh	OUT_BATCH(0);
57803b705cfSriastradh	OUT_BATCH(0);
57903b705cfSriastradh}
58003b705cfSriastradh
58103b705cfSriastradhstatic void
58203b705cfSriastradhgen6_emit_null_depth_buffer(struct sna *sna)
58303b705cfSriastradh{
58403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_DEPTH_BUFFER | (7 - 2));
58503b705cfSriastradh	OUT_BATCH(GEN6_SURFACE_NULL << GEN6_3DSTATE_DEPTH_BUFFER_TYPE_SHIFT |
58603b705cfSriastradh		  GEN6_DEPTHFORMAT_D32_FLOAT << GEN6_3DSTATE_DEPTH_BUFFER_FORMAT_SHIFT);
58703b705cfSriastradh	OUT_BATCH(0);
58803b705cfSriastradh	OUT_BATCH(0);
58903b705cfSriastradh	OUT_BATCH(0);
59003b705cfSriastradh	OUT_BATCH(0);
59103b705cfSriastradh	OUT_BATCH(0);
59203b705cfSriastradh
59303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CLEAR_PARAMS | (2 - 2));
59403b705cfSriastradh	OUT_BATCH(0);
59503b705cfSriastradh}
59603b705cfSriastradh
59703b705cfSriastradhstatic void
59803b705cfSriastradhgen6_emit_invariant(struct sna *sna)
59903b705cfSriastradh{
60003b705cfSriastradh	OUT_BATCH(GEN6_PIPELINE_SELECT | PIPELINE_SELECT_3D);
60103b705cfSriastradh
60203b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_MULTISAMPLE | (3 - 2));
60303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_MULTISAMPLE_PIXEL_LOCATION_CENTER |
60403b705cfSriastradh		  GEN6_3DSTATE_MULTISAMPLE_NUMSAMPLES_1); /* 1 sample/pixel */
60503b705cfSriastradh	OUT_BATCH(0);
60603b705cfSriastradh
60703b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SAMPLE_MASK | (2 - 2));
60803b705cfSriastradh	OUT_BATCH(1);
60903b705cfSriastradh
61003b705cfSriastradh	gen6_emit_urb(sna);
61103b705cfSriastradh
61203b705cfSriastradh	gen6_emit_state_base_address(sna);
61303b705cfSriastradh
61403b705cfSriastradh	gen6_emit_viewports(sna);
61503b705cfSriastradh	gen6_emit_vs(sna);
61603b705cfSriastradh	gen6_emit_gs(sna);
61703b705cfSriastradh	gen6_emit_clip(sna);
61803b705cfSriastradh	gen6_emit_wm_constants(sna);
61903b705cfSriastradh	gen6_emit_null_depth_buffer(sna);
62003b705cfSriastradh
62103b705cfSriastradh	sna->render_state.gen6.needs_invariant = false;
62203b705cfSriastradh}
62303b705cfSriastradh
62413496ba1Ssnjstatic void
62503b705cfSriastradhgen6_emit_cc(struct sna *sna, int blend)
62603b705cfSriastradh{
62703b705cfSriastradh	struct gen6_render_state *render = &sna->render_state.gen6;
62803b705cfSriastradh
62903b705cfSriastradh	if (render->blend == blend)
63013496ba1Ssnj		return;
63103b705cfSriastradh
63203b705cfSriastradh	DBG(("%s: blend = %x\n", __FUNCTION__, blend));
63303b705cfSriastradh
63403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_CC_STATE_POINTERS | (4 - 2));
63503b705cfSriastradh	OUT_BATCH((render->cc_blend + blend) | 1);
63603b705cfSriastradh	if (render->blend == (unsigned)-1) {
63703b705cfSriastradh		OUT_BATCH(1);
63803b705cfSriastradh		OUT_BATCH(1);
63903b705cfSriastradh	} else {
64003b705cfSriastradh		OUT_BATCH(0);
64103b705cfSriastradh		OUT_BATCH(0);
64203b705cfSriastradh	}
64303b705cfSriastradh
64403b705cfSriastradh	render->blend = blend;
64503b705cfSriastradh}
64603b705cfSriastradh
64703b705cfSriastradhstatic void
64803b705cfSriastradhgen6_emit_sampler(struct sna *sna, uint32_t state)
64903b705cfSriastradh{
65003b705cfSriastradh	if (sna->render_state.gen6.samplers == state)
65103b705cfSriastradh		return;
65203b705cfSriastradh
65303b705cfSriastradh	sna->render_state.gen6.samplers = state;
65403b705cfSriastradh
65503b705cfSriastradh	DBG(("%s: sampler = %x\n", __FUNCTION__, state));
65603b705cfSriastradh
65703b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SAMPLER_STATE_POINTERS |
65803b705cfSriastradh		  GEN6_3DSTATE_SAMPLER_STATE_MODIFY_PS |
65903b705cfSriastradh		  (4 - 2));
66003b705cfSriastradh	OUT_BATCH(0); /* VS */
66103b705cfSriastradh	OUT_BATCH(0); /* GS */
66203b705cfSriastradh	OUT_BATCH(sna->render_state.gen6.wm_state + state);
66303b705cfSriastradh}
66403b705cfSriastradh
66503b705cfSriastradhstatic void
66603b705cfSriastradhgen6_emit_sf(struct sna *sna, bool has_mask)
66703b705cfSriastradh{
66803b705cfSriastradh	int num_sf_outputs = has_mask ? 2 : 1;
66903b705cfSriastradh
67003b705cfSriastradh	if (sna->render_state.gen6.num_sf_outputs == num_sf_outputs)
67103b705cfSriastradh		return;
67203b705cfSriastradh
67303b705cfSriastradh	DBG(("%s: num_sf_outputs=%d, read_length=%d, read_offset=%d\n",
67403b705cfSriastradh	     __FUNCTION__, num_sf_outputs, 1, 0));
67503b705cfSriastradh
67603b705cfSriastradh	sna->render_state.gen6.num_sf_outputs = num_sf_outputs;
67703b705cfSriastradh
67803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SF | (20 - 2));
67903b705cfSriastradh	OUT_BATCH(num_sf_outputs << GEN6_3DSTATE_SF_NUM_OUTPUTS_SHIFT |
68003b705cfSriastradh		  1 << GEN6_3DSTATE_SF_URB_ENTRY_READ_LENGTH_SHIFT |
68103b705cfSriastradh		  1 << GEN6_3DSTATE_SF_URB_ENTRY_READ_OFFSET_SHIFT);
68203b705cfSriastradh	OUT_BATCH(0);
68303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_SF_CULL_NONE);
68403b705cfSriastradh	OUT_BATCH(2 << GEN6_3DSTATE_SF_TRIFAN_PROVOKE_SHIFT); /* DW4 */
68503b705cfSriastradh	OUT_BATCH(0);
68603b705cfSriastradh	OUT_BATCH(0);
68703b705cfSriastradh	OUT_BATCH(0);
68803b705cfSriastradh	OUT_BATCH(0);
68903b705cfSriastradh	OUT_BATCH(0); /* DW9 */
69003b705cfSriastradh	OUT_BATCH(0);
69103b705cfSriastradh	OUT_BATCH(0);
69203b705cfSriastradh	OUT_BATCH(0);
69303b705cfSriastradh	OUT_BATCH(0);
69403b705cfSriastradh	OUT_BATCH(0); /* DW14 */
69503b705cfSriastradh	OUT_BATCH(0);
69603b705cfSriastradh	OUT_BATCH(0);
69703b705cfSriastradh	OUT_BATCH(0);
69803b705cfSriastradh	OUT_BATCH(0);
69903b705cfSriastradh	OUT_BATCH(0); /* DW19 */
70003b705cfSriastradh}
70103b705cfSriastradh
70203b705cfSriastradhstatic void
70303b705cfSriastradhgen6_emit_wm(struct sna *sna, unsigned int kernel, bool has_mask)
70403b705cfSriastradh{
70503b705cfSriastradh	const uint32_t *kernels;
70603b705cfSriastradh
70703b705cfSriastradh	if (sna->render_state.gen6.kernel == kernel)
70803b705cfSriastradh		return;
70903b705cfSriastradh
71003b705cfSriastradh	sna->render_state.gen6.kernel = kernel;
71103b705cfSriastradh	kernels = sna->render_state.gen6.wm_kernel[kernel];
71203b705cfSriastradh
71303b705cfSriastradh	DBG(("%s: switching to %s, num_surfaces=%d (8-pixel? %d, 16-pixel? %d,32-pixel? %d)\n",
71403b705cfSriastradh	     __FUNCTION__,
71503b705cfSriastradh	     wm_kernels[kernel].name, wm_kernels[kernel].num_surfaces,
71603b705cfSriastradh	    kernels[0], kernels[1], kernels[2]));
71703b705cfSriastradh
71803b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_WM | (9 - 2));
71903b705cfSriastradh	OUT_BATCH(kernels[0] ?: kernels[1] ?: kernels[2]);
72003b705cfSriastradh	OUT_BATCH(1 << GEN6_3DSTATE_WM_SAMPLER_COUNT_SHIFT |
72103b705cfSriastradh		  wm_kernels[kernel].num_surfaces << GEN6_3DSTATE_WM_BINDING_TABLE_ENTRY_COUNT_SHIFT);
72203b705cfSriastradh	OUT_BATCH(0); /* scratch space */
72303b705cfSriastradh	OUT_BATCH((kernels[0] ? 4 : kernels[1] ? 6 : 8) << GEN6_3DSTATE_WM_DISPATCH_0_START_GRF_SHIFT |
72403b705cfSriastradh		  8 << GEN6_3DSTATE_WM_DISPATCH_1_START_GRF_SHIFT |
72503b705cfSriastradh		  6 << GEN6_3DSTATE_WM_DISPATCH_2_START_GRF_SHIFT);
72603b705cfSriastradh	OUT_BATCH((sna->render_state.gen6.info->max_wm_threads - 1) << GEN6_3DSTATE_WM_MAX_THREADS_SHIFT |
72703b705cfSriastradh		  (kernels[0] ? GEN6_3DSTATE_WM_8_DISPATCH_ENABLE : 0) |
72803b705cfSriastradh		  (kernels[1] ? GEN6_3DSTATE_WM_16_DISPATCH_ENABLE : 0) |
72903b705cfSriastradh		  (kernels[2] ? GEN6_3DSTATE_WM_32_DISPATCH_ENABLE : 0) |
73003b705cfSriastradh		  GEN6_3DSTATE_WM_DISPATCH_ENABLE);
73103b705cfSriastradh	OUT_BATCH((1 + has_mask) << GEN6_3DSTATE_WM_NUM_SF_OUTPUTS_SHIFT |
73203b705cfSriastradh		  GEN6_3DSTATE_WM_PERSPECTIVE_PIXEL_BARYCENTRIC);
73303b705cfSriastradh	OUT_BATCH(kernels[2]);
73403b705cfSriastradh	OUT_BATCH(kernels[1]);
73503b705cfSriastradh}
73603b705cfSriastradh
73703b705cfSriastradhstatic bool
73803b705cfSriastradhgen6_emit_binding_table(struct sna *sna, uint16_t offset)
73903b705cfSriastradh{
74003b705cfSriastradh	if (sna->render_state.gen6.surface_table == offset)
74103b705cfSriastradh		return false;
74203b705cfSriastradh
74303b705cfSriastradh	/* Binding table pointers */
74403b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_BINDING_TABLE_POINTERS |
74503b705cfSriastradh		  GEN6_3DSTATE_BINDING_TABLE_MODIFY_PS |
74603b705cfSriastradh		  (4 - 2));
74703b705cfSriastradh	OUT_BATCH(0);		/* vs */
74803b705cfSriastradh	OUT_BATCH(0);		/* gs */
74903b705cfSriastradh	/* Only the PS uses the binding table */
75003b705cfSriastradh	OUT_BATCH(offset*4);
75103b705cfSriastradh
75203b705cfSriastradh	sna->render_state.gen6.surface_table = offset;
75303b705cfSriastradh	return true;
75403b705cfSriastradh}
75503b705cfSriastradh
75603b705cfSriastradhstatic bool
75703b705cfSriastradhgen6_emit_drawing_rectangle(struct sna *sna,
75803b705cfSriastradh			    const struct sna_composite_op *op)
75903b705cfSriastradh{
76003b705cfSriastradh	uint32_t limit = (op->dst.height - 1) << 16 | (op->dst.width - 1);
76103b705cfSriastradh	uint32_t offset = (uint16_t)op->dst.y << 16 | (uint16_t)op->dst.x;
76203b705cfSriastradh
76313496ba1Ssnj	assert(!too_large(abs(op->dst.x), abs(op->dst.y)));
76403b705cfSriastradh	assert(!too_large(op->dst.width, op->dst.height));
76503b705cfSriastradh
76603b705cfSriastradh	if (sna->render_state.gen6.drawrect_limit  == limit &&
76703b705cfSriastradh	    sna->render_state.gen6.drawrect_offset == offset)
76813496ba1Ssnj		return true;
76903b705cfSriastradh
77003b705cfSriastradh	/* [DevSNB-C+{W/A}] Before any depth stall flush (including those
77103b705cfSriastradh	 * produced by non-pipelined state commands), software needs to first
77203b705cfSriastradh	 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
77303b705cfSriastradh	 * 0.
77403b705cfSriastradh	 *
77503b705cfSriastradh	 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
77603b705cfSriastradh	 * BEFORE the pipe-control with a post-sync op and no write-cache
77703b705cfSriastradh	 * flushes.
77803b705cfSriastradh	 */
77913496ba1Ssnj	if (!sna->render_state.gen6.first_state_packet)
78013496ba1Ssnj		gen6_emit_pipe_stall(sna);
78103b705cfSriastradh
78203b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL | (4 - 2));
78303b705cfSriastradh	OUT_BATCH(GEN6_PIPE_CONTROL_WRITE_TIME);
78403b705cfSriastradh	OUT_BATCH(kgem_add_reloc(&sna->kgem, sna->kgem.nbatch,
78503b705cfSriastradh				 sna->render_state.gen6.general_bo,
78603b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION << 16 |
78703b705cfSriastradh				 I915_GEM_DOMAIN_INSTRUCTION,
78803b705cfSriastradh				 64));
78903b705cfSriastradh	OUT_BATCH(0);
79003b705cfSriastradh
79142542f5fSchristos	DBG(("%s: offset=(%d, %d), limit=(%d, %d)\n",
79213496ba1Ssnj	     __FUNCTION__, op->dst.x, op->dst.y, op->dst.width, op->dst.height));
79303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
79403b705cfSriastradh	OUT_BATCH(0);
79503b705cfSriastradh	OUT_BATCH(limit);
79603b705cfSriastradh	OUT_BATCH(offset);
79703b705cfSriastradh
79803b705cfSriastradh	sna->render_state.gen6.drawrect_offset = offset;
79903b705cfSriastradh	sna->render_state.gen6.drawrect_limit = limit;
80013496ba1Ssnj	return false;
80103b705cfSriastradh}
80203b705cfSriastradh
80303b705cfSriastradhstatic void
80403b705cfSriastradhgen6_emit_vertex_elements(struct sna *sna,
80503b705cfSriastradh			  const struct sna_composite_op *op)
80603b705cfSriastradh{
80703b705cfSriastradh	/*
80803b705cfSriastradh	 * vertex data in vertex buffer
80903b705cfSriastradh	 *    position: (x, y)
81003b705cfSriastradh	 *    texture coordinate 0: (u0, v0) if (is_affine is true) else (u0, v0, w0)
81103b705cfSriastradh	 *    texture coordinate 1 if (has_mask is true): same as above
81203b705cfSriastradh	 */
81303b705cfSriastradh	struct gen6_render_state *render = &sna->render_state.gen6;
81403b705cfSriastradh	uint32_t src_format, dw;
81503b705cfSriastradh	int id = GEN6_VERTEX(op->u.gen6.flags);
81603b705cfSriastradh	bool has_mask;
81703b705cfSriastradh
81803b705cfSriastradh	DBG(("%s: setup id=%d\n", __FUNCTION__, id));
81903b705cfSriastradh
82003b705cfSriastradh	if (render->ve_id == id)
82103b705cfSriastradh		return;
82203b705cfSriastradh	render->ve_id = id;
82303b705cfSriastradh
82403b705cfSriastradh	/* The VUE layout
82503b705cfSriastradh	 *    dword 0-3: pad (0.0, 0.0, 0.0. 0.0)
82603b705cfSriastradh	 *    dword 4-7: position (x, y, 1.0, 1.0),
82703b705cfSriastradh	 *    dword 8-11: texture coordinate 0 (u0, v0, w0, 1.0)
82803b705cfSriastradh	 *    dword 12-15: texture coordinate 1 (u1, v1, w1, 1.0)
82903b705cfSriastradh	 *
83003b705cfSriastradh	 * dword 4-15 are fetched from vertex buffer
83103b705cfSriastradh	 */
83203b705cfSriastradh	has_mask = (id >> 2) != 0;
83303b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VERTEX_ELEMENTS |
83403b705cfSriastradh		((2 * (3 + has_mask)) + 1 - 2));
83503b705cfSriastradh
83603b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
83703b705cfSriastradh		  GEN6_SURFACEFORMAT_R32G32B32A32_FLOAT << VE0_FORMAT_SHIFT |
83803b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
83903b705cfSriastradh	OUT_BATCH(GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_0_SHIFT |
84003b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT |
84103b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
84203b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_3_SHIFT);
84303b705cfSriastradh
84403b705cfSriastradh	/* x,y */
84503b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
84603b705cfSriastradh		  GEN6_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
84703b705cfSriastradh		  0 << VE0_OFFSET_SHIFT);
84803b705cfSriastradh	OUT_BATCH(GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
84903b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
85003b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
85103b705cfSriastradh		  GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
85203b705cfSriastradh
85303b705cfSriastradh	/* u0, v0, w0 */
85403b705cfSriastradh	DBG(("%s: first channel %d floats, offset=4b\n", __FUNCTION__, id & 3));
85503b705cfSriastradh	dw = GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
85603b705cfSriastradh	switch (id & 3) {
85703b705cfSriastradh	default:
85803b705cfSriastradh		assert(0);
85903b705cfSriastradh	case 0:
86003b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R16G16_SSCALED;
86103b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
86203b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
86303b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
86403b705cfSriastradh		break;
86503b705cfSriastradh	case 1:
86603b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32_FLOAT;
86703b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
86803b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
86903b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
87003b705cfSriastradh		break;
87103b705cfSriastradh	case 2:
87203b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32G32_FLOAT;
87303b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
87403b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
87503b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
87603b705cfSriastradh		break;
87703b705cfSriastradh	case 3:
87803b705cfSriastradh		src_format = GEN6_SURFACEFORMAT_R32G32B32_FLOAT;
87903b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
88003b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
88103b705cfSriastradh		dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
88203b705cfSriastradh		break;
88303b705cfSriastradh	}
88403b705cfSriastradh	OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
88503b705cfSriastradh		  src_format << VE0_FORMAT_SHIFT |
88603b705cfSriastradh		  4 << VE0_OFFSET_SHIFT);
88703b705cfSriastradh	OUT_BATCH(dw);
88803b705cfSriastradh
88903b705cfSriastradh	/* u1, v1, w1 */
89003b705cfSriastradh	if (has_mask) {
89103b705cfSriastradh		unsigned offset = 4 + ((id & 3) ?: 1) * sizeof(float);
89203b705cfSriastradh		DBG(("%s: second channel %d floats, offset=%db\n", __FUNCTION__, id >> 2, offset));
89303b705cfSriastradh		dw = GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT;
89403b705cfSriastradh		switch (id >> 2) {
89503b705cfSriastradh		case 1:
89603b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32_FLOAT;
89703b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
89803b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT;
89903b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
90003b705cfSriastradh			break;
90103b705cfSriastradh		default:
90203b705cfSriastradh			assert(0);
90303b705cfSriastradh		case 2:
90403b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32G32_FLOAT;
90503b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
90603b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
90703b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT;
90803b705cfSriastradh			break;
90903b705cfSriastradh		case 3:
91003b705cfSriastradh			src_format = GEN6_SURFACEFORMAT_R32G32B32_FLOAT;
91103b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT;
91203b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT;
91303b705cfSriastradh			dw |= GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_2_SHIFT;
91403b705cfSriastradh			break;
91503b705cfSriastradh		}
91603b705cfSriastradh		OUT_BATCH(id << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
91703b705cfSriastradh			  src_format << VE0_FORMAT_SHIFT |
91803b705cfSriastradh			  offset << VE0_OFFSET_SHIFT);
91903b705cfSriastradh		OUT_BATCH(dw);
92003b705cfSriastradh	}
92103b705cfSriastradh}
92203b705cfSriastradh
92303b705cfSriastradhstatic void
92403b705cfSriastradhgen6_emit_state(struct sna *sna,
92503b705cfSriastradh		const struct sna_composite_op *op,
92603b705cfSriastradh		uint16_t wm_binding_table)
92703b705cfSriastradh{
92813496ba1Ssnj	bool need_invalidate;
92913496ba1Ssnj	bool need_flush;
93013496ba1Ssnj	bool need_stall;
93103b705cfSriastradh
93203b705cfSriastradh	assert(op->dst.bo->exec);
93303b705cfSriastradh
93413496ba1Ssnj	need_flush = wm_binding_table & 1;
93513496ba1Ssnj	if (ALWAYS_FLUSH)
93613496ba1Ssnj		need_flush = true;
93703b705cfSriastradh
93813496ba1Ssnj	wm_binding_table &= ~1;
93913496ba1Ssnj	need_stall = sna->render_state.gen6.surface_table != wm_binding_table;
94013496ba1Ssnj	if (ALWAYS_STALL)
94113496ba1Ssnj		need_stall = true;
94213496ba1Ssnj
94313496ba1Ssnj	need_invalidate = kgem_bo_is_dirty(op->src.bo) || kgem_bo_is_dirty(op->mask.bo);
94413496ba1Ssnj	if (ALWAYS_INVALIDATE)
94513496ba1Ssnj		need_invalidate = true;
94613496ba1Ssnj
94713496ba1Ssnj	if (need_invalidate) {
94813496ba1Ssnj		gen6_emit_pipe_invalidate(sna);
94903b705cfSriastradh		kgem_clear_dirty(&sna->kgem);
95003b705cfSriastradh		assert(op->dst.bo->exec);
95103b705cfSriastradh		kgem_bo_mark_dirty(op->dst.bo);
95213496ba1Ssnj
95313496ba1Ssnj		need_flush = false;
95403b705cfSriastradh		need_stall = false;
95513496ba1Ssnj		sna->render_state.gen6.first_state_packet = true;
95603b705cfSriastradh	}
95713496ba1Ssnj	if (need_flush) {
95813496ba1Ssnj		gen6_emit_pipe_flush(sna, need_stall);
95913496ba1Ssnj		need_stall = false;
96013496ba1Ssnj		sna->render_state.gen6.first_state_packet = true;
96103b705cfSriastradh	}
96213496ba1Ssnj
96313496ba1Ssnj	need_stall &= gen6_emit_drawing_rectangle(sna, op);
96413496ba1Ssnj	if (need_stall)
96513496ba1Ssnj		gen6_emit_pipe_stall(sna);
96613496ba1Ssnj
96713496ba1Ssnj	gen6_emit_cc(sna, GEN6_BLEND(op->u.gen6.flags));
96813496ba1Ssnj	gen6_emit_sampler(sna, GEN6_SAMPLER(op->u.gen6.flags));
96913496ba1Ssnj	gen6_emit_sf(sna, GEN6_VERTEX(op->u.gen6.flags) >> 2);
97013496ba1Ssnj	gen6_emit_wm(sna, GEN6_KERNEL(op->u.gen6.flags), GEN6_VERTEX(op->u.gen6.flags) >> 2);
97113496ba1Ssnj	gen6_emit_vertex_elements(sna, op);
97213496ba1Ssnj	gen6_emit_binding_table(sna, wm_binding_table);
97313496ba1Ssnj
97403b705cfSriastradh	sna->render_state.gen6.first_state_packet = false;
97503b705cfSriastradh}
97603b705cfSriastradh
97703b705cfSriastradhstatic bool gen6_magic_ca_pass(struct sna *sna,
97803b705cfSriastradh			       const struct sna_composite_op *op)
97903b705cfSriastradh{
98003b705cfSriastradh	struct gen6_render_state *state = &sna->render_state.gen6;
98103b705cfSriastradh
98203b705cfSriastradh	if (!op->need_magic_ca_pass)
98303b705cfSriastradh		return false;
98403b705cfSriastradh
98503b705cfSriastradh	DBG(("%s: CA fixup (%d -> %d)\n", __FUNCTION__,
98603b705cfSriastradh	     sna->render.vertex_start, sna->render.vertex_index));
98703b705cfSriastradh
98813496ba1Ssnj	gen6_emit_pipe_stall(sna);
98903b705cfSriastradh
99003b705cfSriastradh	gen6_emit_cc(sna, gen6_get_blend(PictOpAdd, true, op->dst.format));
99103b705cfSriastradh	gen6_emit_wm(sna,
99203b705cfSriastradh		     gen6_choose_composite_kernel(PictOpAdd,
99303b705cfSriastradh						  true, true,
99403b705cfSriastradh						  op->is_affine),
99503b705cfSriastradh		     true);
99603b705cfSriastradh
99703b705cfSriastradh	OUT_BATCH(GEN6_3DPRIMITIVE |
99803b705cfSriastradh		  GEN6_3DPRIMITIVE_VERTEX_SEQUENTIAL |
99903b705cfSriastradh		  _3DPRIM_RECTLIST << GEN6_3DPRIMITIVE_TOPOLOGY_SHIFT |
100003b705cfSriastradh		  0 << 9 |
100103b705cfSriastradh		  4);
100203b705cfSriastradh	OUT_BATCH(sna->render.vertex_index - sna->render.vertex_start);
100303b705cfSriastradh	OUT_BATCH(sna->render.vertex_start);
100403b705cfSriastradh	OUT_BATCH(1);	/* single instance */
100503b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
100603b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
100703b705cfSriastradh
100803b705cfSriastradh	state->last_primitive = sna->kgem.nbatch;
100903b705cfSriastradh	return true;
101003b705cfSriastradh}
101103b705cfSriastradh
101203b705cfSriastradhtypedef struct gen6_surface_state_padded {
101303b705cfSriastradh	struct gen6_surface_state state;
101403b705cfSriastradh	char pad[32 - sizeof(struct gen6_surface_state)];
101503b705cfSriastradh} gen6_surface_state_padded;
101603b705cfSriastradh
101703b705cfSriastradhstatic void null_create(struct sna_static_stream *stream)
101803b705cfSriastradh{
101903b705cfSriastradh	/* A bunch of zeros useful for legacy border color and depth-stencil */
102003b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
102103b705cfSriastradh}
102203b705cfSriastradh
102303b705cfSriastradhstatic void scratch_create(struct sna_static_stream *stream)
102403b705cfSriastradh{
102503b705cfSriastradh	/* 64 bytes of scratch space for random writes, such as
102603b705cfSriastradh	 * the pipe-control w/a.
102703b705cfSriastradh	 */
102803b705cfSriastradh	sna_static_stream_map(stream, 64, 64);
102903b705cfSriastradh}
103003b705cfSriastradh
103103b705cfSriastradhstatic void
103203b705cfSriastradhsampler_state_init(struct gen6_sampler_state *sampler_state,
103303b705cfSriastradh		   sampler_filter_t filter,
103403b705cfSriastradh		   sampler_extend_t extend)
103503b705cfSriastradh{
103603b705cfSriastradh	sampler_state->ss0.lod_preclamp = 1;	/* GL mode */
103703b705cfSriastradh
103803b705cfSriastradh	/* We use the legacy mode to get the semantics specified by
103903b705cfSriastradh	 * the Render extension. */
104003b705cfSriastradh	sampler_state->ss0.border_color_mode = GEN6_BORDER_COLOR_MODE_LEGACY;
104103b705cfSriastradh
104203b705cfSriastradh	switch (filter) {
104303b705cfSriastradh	default:
104403b705cfSriastradh	case SAMPLER_FILTER_NEAREST:
104503b705cfSriastradh		sampler_state->ss0.min_filter = GEN6_MAPFILTER_NEAREST;
104603b705cfSriastradh		sampler_state->ss0.mag_filter = GEN6_MAPFILTER_NEAREST;
104703b705cfSriastradh		break;
104803b705cfSriastradh	case SAMPLER_FILTER_BILINEAR:
104903b705cfSriastradh		sampler_state->ss0.min_filter = GEN6_MAPFILTER_LINEAR;
105003b705cfSriastradh		sampler_state->ss0.mag_filter = GEN6_MAPFILTER_LINEAR;
105103b705cfSriastradh		break;
105203b705cfSriastradh	}
105303b705cfSriastradh
105403b705cfSriastradh	switch (extend) {
105503b705cfSriastradh	default:
105603b705cfSriastradh	case SAMPLER_EXTEND_NONE:
105703b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
105803b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
105903b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_CLAMP_BORDER;
106003b705cfSriastradh		break;
106103b705cfSriastradh	case SAMPLER_EXTEND_REPEAT:
106203b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
106303b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
106403b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_WRAP;
106503b705cfSriastradh		break;
106603b705cfSriastradh	case SAMPLER_EXTEND_PAD:
106703b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
106803b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
106903b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
107003b705cfSriastradh		break;
107103b705cfSriastradh	case SAMPLER_EXTEND_REFLECT:
107203b705cfSriastradh		sampler_state->ss1.r_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
107303b705cfSriastradh		sampler_state->ss1.s_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
107403b705cfSriastradh		sampler_state->ss1.t_wrap_mode = GEN6_TEXCOORDMODE_MIRROR;
107503b705cfSriastradh		break;
107603b705cfSriastradh	}
107703b705cfSriastradh}
107803b705cfSriastradh
107903b705cfSriastradhstatic void
108003b705cfSriastradhsampler_copy_init(struct gen6_sampler_state *ss)
108103b705cfSriastradh{
108203b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
108303b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
108403b705cfSriastradh
108503b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
108603b705cfSriastradh}
108703b705cfSriastradh
108803b705cfSriastradhstatic void
108903b705cfSriastradhsampler_fill_init(struct gen6_sampler_state *ss)
109003b705cfSriastradh{
109103b705cfSriastradh	sampler_state_init(ss, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_REPEAT);
109203b705cfSriastradh	ss->ss3.non_normalized_coord = 1;
109303b705cfSriastradh
109403b705cfSriastradh	sampler_state_init(ss+1, SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE);
109503b705cfSriastradh}
109603b705cfSriastradh
109703b705cfSriastradhstatic uint32_t
109803b705cfSriastradhgen6_tiling_bits(uint32_t tiling)
109903b705cfSriastradh{
110003b705cfSriastradh	switch (tiling) {
110103b705cfSriastradh	default: assert(0);
110203b705cfSriastradh	case I915_TILING_NONE: return 0;
110303b705cfSriastradh	case I915_TILING_X: return GEN6_SURFACE_TILED;
110403b705cfSriastradh	case I915_TILING_Y: return GEN6_SURFACE_TILED | GEN6_SURFACE_TILED_Y;
110503b705cfSriastradh	}
110603b705cfSriastradh}
110703b705cfSriastradh
110803b705cfSriastradh/**
110903b705cfSriastradh * Sets up the common fields for a surface state buffer for the given
111003b705cfSriastradh * picture in the given surface state buffer.
111103b705cfSriastradh */
111203b705cfSriastradhstatic int
111303b705cfSriastradhgen6_bind_bo(struct sna *sna,
111403b705cfSriastradh	     struct kgem_bo *bo,
111503b705cfSriastradh	     uint32_t width,
111603b705cfSriastradh	     uint32_t height,
111703b705cfSriastradh	     uint32_t format,
111803b705cfSriastradh	     bool is_dst)
111903b705cfSriastradh{
112003b705cfSriastradh	uint32_t *ss;
112103b705cfSriastradh	uint32_t domains;
112203b705cfSriastradh	uint16_t offset;
112303b705cfSriastradh	uint32_t is_scanout = is_dst && bo->scanout;
112403b705cfSriastradh
112503b705cfSriastradh	/* After the first bind, we manage the cache domains within the batch */
112603b705cfSriastradh	offset = kgem_bo_get_binding(bo, format | is_dst << 30 | is_scanout << 31);
112703b705cfSriastradh	if (offset) {
112803b705cfSriastradh		DBG(("[%x]  bo(handle=%d), format=%d, reuse %s binding\n",
112903b705cfSriastradh		     offset, bo->handle, format,
113003b705cfSriastradh		     is_dst ? "render" : "sampler"));
113142542f5fSchristos		assert(offset >= sna->kgem.surface);
113203b705cfSriastradh		if (is_dst)
113303b705cfSriastradh			kgem_bo_mark_dirty(bo);
113403b705cfSriastradh		return offset * sizeof(uint32_t);
113503b705cfSriastradh	}
113603b705cfSriastradh
113703b705cfSriastradh	offset = sna->kgem.surface -=
113803b705cfSriastradh		sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
113903b705cfSriastradh	ss = sna->kgem.batch + offset;
114003b705cfSriastradh	ss[0] = (GEN6_SURFACE_2D << GEN6_SURFACE_TYPE_SHIFT |
114103b705cfSriastradh		 GEN6_SURFACE_BLEND_ENABLED |
114203b705cfSriastradh		 format << GEN6_SURFACE_FORMAT_SHIFT);
114303b705cfSriastradh	if (is_dst) {
114403b705cfSriastradh		ss[0] |= GEN6_SURFACE_RC_READ_WRITE;
114503b705cfSriastradh		domains = I915_GEM_DOMAIN_RENDER << 16 |I915_GEM_DOMAIN_RENDER;
114603b705cfSriastradh	} else
114703b705cfSriastradh		domains = I915_GEM_DOMAIN_SAMPLER << 16;
114803b705cfSriastradh	ss[1] = kgem_add_reloc(&sna->kgem, offset + 1, bo, domains, 0);
114903b705cfSriastradh	ss[2] = ((width - 1)  << GEN6_SURFACE_WIDTH_SHIFT |
115003b705cfSriastradh		 (height - 1) << GEN6_SURFACE_HEIGHT_SHIFT);
115103b705cfSriastradh	assert(bo->pitch <= (1 << 18));
115203b705cfSriastradh	ss[3] = (gen6_tiling_bits(bo->tiling) |
115303b705cfSriastradh		 (bo->pitch - 1) << GEN6_SURFACE_PITCH_SHIFT);
115403b705cfSriastradh	ss[4] = 0;
115542542f5fSchristos	ss[5] = (is_scanout || bo->io) ? 0 : 3 << 16;
115603b705cfSriastradh
115703b705cfSriastradh	kgem_bo_set_binding(bo, format | is_dst << 30 | is_scanout << 31, offset);
115803b705cfSriastradh
115903b705cfSriastradh	DBG(("[%x] bind bo(handle=%d, addr=%d), format=%d, width=%d, height=%d, pitch=%d, tiling=%d -> %s\n",
116003b705cfSriastradh	     offset, bo->handle, ss[1],
116103b705cfSriastradh	     format, width, height, bo->pitch, bo->tiling,
116203b705cfSriastradh	     domains & 0xffff ? "render" : "sampler"));
116303b705cfSriastradh
116403b705cfSriastradh	return offset * sizeof(uint32_t);
116503b705cfSriastradh}
116603b705cfSriastradh
116703b705cfSriastradhstatic void gen6_emit_vertex_buffer(struct sna *sna,
116803b705cfSriastradh				    const struct sna_composite_op *op)
116903b705cfSriastradh{
117003b705cfSriastradh	int id = GEN6_VERTEX(op->u.gen6.flags);
117103b705cfSriastradh
117203b705cfSriastradh	OUT_BATCH(GEN6_3DSTATE_VERTEX_BUFFERS | 3);
117303b705cfSriastradh	OUT_BATCH(id << VB0_BUFFER_INDEX_SHIFT | VB0_VERTEXDATA |
117403b705cfSriastradh		  4*op->floats_per_vertex << VB0_BUFFER_PITCH_SHIFT);
117503b705cfSriastradh	sna->render.vertex_reloc[sna->render.nvertex_reloc++] = sna->kgem.nbatch;
117603b705cfSriastradh	OUT_BATCH(0);
117703b705cfSriastradh	OUT_BATCH(~0); /* max address: disabled */
117803b705cfSriastradh	OUT_BATCH(0);
117903b705cfSriastradh
118003b705cfSriastradh	sna->render.vb_id |= 1 << id;
118103b705cfSriastradh}
118203b705cfSriastradh
118303b705cfSriastradhstatic void gen6_emit_primitive(struct sna *sna)
118403b705cfSriastradh{
118503b705cfSriastradh	if (sna->kgem.nbatch == sna->render_state.gen6.last_primitive) {
118603b705cfSriastradh		DBG(("%s: continuing previous primitive, start=%d, index=%d\n",
118703b705cfSriastradh		     __FUNCTION__,
118803b705cfSriastradh		     sna->render.vertex_start,
118903b705cfSriastradh		     sna->render.vertex_index));
119003b705cfSriastradh		sna->render.vertex_offset = sna->kgem.nbatch - 5;
119103b705cfSriastradh		return;
119203b705cfSriastradh	}
119303b705cfSriastradh
119403b705cfSriastradh	OUT_BATCH(GEN6_3DPRIMITIVE |
119503b705cfSriastradh		  GEN6_3DPRIMITIVE_VERTEX_SEQUENTIAL |
119603b705cfSriastradh		  _3DPRIM_RECTLIST << GEN6_3DPRIMITIVE_TOPOLOGY_SHIFT |
119703b705cfSriastradh		  0 << 9 |
119803b705cfSriastradh		  4);
119903b705cfSriastradh	sna->render.vertex_offset = sna->kgem.nbatch;
120003b705cfSriastradh	OUT_BATCH(0);	/* vertex count, to be filled in later */
120103b705cfSriastradh	OUT_BATCH(sna->render.vertex_index);
120203b705cfSriastradh	OUT_BATCH(1);	/* single instance */
120303b705cfSriastradh	OUT_BATCH(0);	/* start instance location */
120403b705cfSriastradh	OUT_BATCH(0);	/* index buffer offset, ignored */
120503b705cfSriastradh	sna->render.vertex_start = sna->render.vertex_index;
120603b705cfSriastradh	DBG(("%s: started new primitive: index=%d\n",
120703b705cfSriastradh	     __FUNCTION__, sna->render.vertex_start));
120803b705cfSriastradh
120903b705cfSriastradh	sna->render_state.gen6.last_primitive = sna->kgem.nbatch;
121003b705cfSriastradh}
121103b705cfSriastradh
121203b705cfSriastradhstatic bool gen6_rectangle_begin(struct sna *sna,
121303b705cfSriastradh				 const struct sna_composite_op *op)
121403b705cfSriastradh{
121503b705cfSriastradh	int id = 1 << GEN6_VERTEX(op->u.gen6.flags);
121603b705cfSriastradh	int ndwords;
121703b705cfSriastradh
121803b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render) && sna->render.vertex_offset)
121903b705cfSriastradh		return true;
122003b705cfSriastradh
122103b705cfSriastradh	ndwords = op->need_magic_ca_pass ? 60 : 6;
122203b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
122303b705cfSriastradh		ndwords += 5;
122403b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, ndwords))
122503b705cfSriastradh		return false;
122603b705cfSriastradh
122703b705cfSriastradh	if ((sna->render.vb_id & id) == 0)
122803b705cfSriastradh		gen6_emit_vertex_buffer(sna, op);
122903b705cfSriastradh
123003b705cfSriastradh	gen6_emit_primitive(sna);
123103b705cfSriastradh	return true;
123203b705cfSriastradh}
123303b705cfSriastradh
123403b705cfSriastradhstatic int gen6_get_rectangles__flush(struct sna *sna,
123503b705cfSriastradh				      const struct sna_composite_op *op)
123603b705cfSriastradh{
123703b705cfSriastradh	/* Preventing discarding new vbo after lock contention */
123803b705cfSriastradh	if (sna_vertex_wait__locked(&sna->render)) {
123903b705cfSriastradh		int rem = vertex_space(sna);
124003b705cfSriastradh		if (rem > op->floats_per_rect)
124103b705cfSriastradh			return rem;
124203b705cfSriastradh	}
124303b705cfSriastradh
124403b705cfSriastradh	if (!kgem_check_batch(&sna->kgem, op->need_magic_ca_pass ? 65 : 5))
124503b705cfSriastradh		return 0;
124603b705cfSriastradh	if (!kgem_check_reloc_and_exec(&sna->kgem, 2))
124703b705cfSriastradh		return 0;
124803b705cfSriastradh
124903b705cfSriastradh	if (sna->render.vertex_offset) {
125003b705cfSriastradh		gen4_vertex_flush(sna);
125103b705cfSriastradh		if (gen6_magic_ca_pass(sna, op)) {
125213496ba1Ssnj			gen6_emit_pipe_stall(sna);
125303b705cfSriastradh			gen6_emit_cc(sna, GEN6_BLEND(op->u.gen6.flags));
125403b705cfSriastradh			gen6_emit_wm(sna,
125503b705cfSriastradh				     GEN6_KERNEL(op->u.gen6.flags),
125603b705cfSriastradh				     GEN6_VERTEX(op->u.gen6.flags) >> 2);
125703b705cfSriastradh		}
125803b705cfSriastradh	}
125903b705cfSriastradh
126003b705cfSriastradh	return gen4_vertex_finish(sna);
126103b705cfSriastradh}
126203b705cfSriastradh
126303b705cfSriastradhinline static int gen6_get_rectangles(struct sna *sna,
126403b705cfSriastradh				      const struct sna_composite_op *op,
126503b705cfSriastradh				      int want,
126603b705cfSriastradh				      void (*emit_state)(struct sna *, const struct sna_composite_op *op))
126703b705cfSriastradh{
126803b705cfSriastradh	int rem;
126903b705cfSriastradh
127003b705cfSriastradh	assert(want);
127103b705cfSriastradh
127203b705cfSriastradhstart:
127303b705cfSriastradh	rem = vertex_space(sna);
127403b705cfSriastradh	if (unlikely(rem < op->floats_per_rect)) {
127503b705cfSriastradh		DBG(("flushing vbo for %s: %d < %d\n",
127603b705cfSriastradh		     __FUNCTION__, rem, op->floats_per_rect));
127703b705cfSriastradh		rem = gen6_get_rectangles__flush(sna, op);
127803b705cfSriastradh		if (unlikely(rem == 0))
127903b705cfSriastradh			goto flush;
128003b705cfSriastradh	}
128103b705cfSriastradh
128203b705cfSriastradh	if (unlikely(sna->render.vertex_offset == 0)) {
128303b705cfSriastradh		if (!gen6_rectangle_begin(sna, op))
128403b705cfSriastradh			goto flush;
128503b705cfSriastradh		else
128603b705cfSriastradh			goto start;
128703b705cfSriastradh	}
128803b705cfSriastradh
128903b705cfSriastradh	assert(rem <= vertex_space(sna));
129003b705cfSriastradh	assert(op->floats_per_rect <= rem);
129103b705cfSriastradh	if (want > 1 && want * op->floats_per_rect > rem)
129203b705cfSriastradh		want = rem / op->floats_per_rect;
129303b705cfSriastradh
129403b705cfSriastradh	assert(want > 0);
129503b705cfSriastradh	sna->render.vertex_index += 3*want;
129603b705cfSriastradh	return want;
129703b705cfSriastradh
129803b705cfSriastradhflush:
129903b705cfSriastradh	if (sna->render.vertex_offset) {
130003b705cfSriastradh		gen4_vertex_flush(sna);
130103b705cfSriastradh		gen6_magic_ca_pass(sna, op);
130203b705cfSriastradh	}
130303b705cfSriastradh	sna_vertex_wait__locked(&sna->render);
130403b705cfSriastradh	_kgem_submit(&sna->kgem);
130503b705cfSriastradh	emit_state(sna, op);
130603b705cfSriastradh	goto start;
130703b705cfSriastradh}
130803b705cfSriastradh
130903b705cfSriastradhinline static uint32_t *gen6_composite_get_binding_table(struct sna *sna,
131003b705cfSriastradh							 uint16_t *offset)
131103b705cfSriastradh{
131203b705cfSriastradh	uint32_t *table;
131303b705cfSriastradh
131403b705cfSriastradh	sna->kgem.surface -=
131503b705cfSriastradh		sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
131603b705cfSriastradh	/* Clear all surplus entries to zero in case of prefetch */
131703b705cfSriastradh	table = memset(sna->kgem.batch + sna->kgem.surface,
131803b705cfSriastradh		       0, sizeof(struct gen6_surface_state_padded));
131903b705cfSriastradh
132003b705cfSriastradh	DBG(("%s(%x)\n", __FUNCTION__, 4*sna->kgem.surface));
132103b705cfSriastradh
132203b705cfSriastradh	*offset = sna->kgem.surface;
132303b705cfSriastradh	return table;
132403b705cfSriastradh}
132503b705cfSriastradh
132603b705cfSriastradhstatic bool
132703b705cfSriastradhgen6_get_batch(struct sna *sna, const struct sna_composite_op *op)
132803b705cfSriastradh{
132903b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, op->dst.bo);
133003b705cfSriastradh
133103b705cfSriastradh	if (!kgem_check_batch_with_surfaces(&sna->kgem, 150, 4)) {
133203b705cfSriastradh		DBG(("%s: flushing batch: %d < %d+%d\n",
133303b705cfSriastradh		     __FUNCTION__, sna->kgem.surface - sna->kgem.nbatch,
133403b705cfSriastradh		     150, 4*8));
133503b705cfSriastradh		kgem_submit(&sna->kgem);
133603b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
133703b705cfSriastradh	}
133803b705cfSriastradh
133903b705cfSriastradh	if (sna->render_state.gen6.needs_invariant)
134003b705cfSriastradh		gen6_emit_invariant(sna);
134103b705cfSriastradh
134203b705cfSriastradh	return kgem_bo_is_dirty(op->dst.bo);
134303b705cfSriastradh}
134403b705cfSriastradh
134503b705cfSriastradhstatic void gen6_emit_composite_state(struct sna *sna,
134603b705cfSriastradh				      const struct sna_composite_op *op)
134703b705cfSriastradh{
134803b705cfSriastradh	uint32_t *binding_table;
134903b705cfSriastradh	uint16_t offset;
135003b705cfSriastradh	bool dirty;
135103b705cfSriastradh
135203b705cfSriastradh	dirty = gen6_get_batch(sna, op);
135303b705cfSriastradh
135403b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
135503b705cfSriastradh
135603b705cfSriastradh	binding_table[0] =
135703b705cfSriastradh		gen6_bind_bo(sna,
135803b705cfSriastradh			    op->dst.bo, op->dst.width, op->dst.height,
135903b705cfSriastradh			    gen6_get_dest_format(op->dst.format),
136003b705cfSriastradh			    true);
136103b705cfSriastradh	binding_table[1] =
136203b705cfSriastradh		gen6_bind_bo(sna,
136303b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
136403b705cfSriastradh			     op->src.card_format,
136503b705cfSriastradh			     false);
136603b705cfSriastradh	if (op->mask.bo) {
136703b705cfSriastradh		binding_table[2] =
136803b705cfSriastradh			gen6_bind_bo(sna,
136903b705cfSriastradh				     op->mask.bo,
137003b705cfSriastradh				     op->mask.width,
137103b705cfSriastradh				     op->mask.height,
137203b705cfSriastradh				     op->mask.card_format,
137303b705cfSriastradh				     false);
137403b705cfSriastradh	}
137503b705cfSriastradh
137603b705cfSriastradh	if (sna->kgem.surface == offset &&
137703b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table &&
137803b705cfSriastradh	    (op->mask.bo == NULL ||
137903b705cfSriastradh	     sna->kgem.batch[sna->render_state.gen6.surface_table+2] == binding_table[2])) {
138003b705cfSriastradh		sna->kgem.surface += sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
138103b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
138203b705cfSriastradh	}
138303b705cfSriastradh
138403b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
138503b705cfSriastradh}
138603b705cfSriastradh
138703b705cfSriastradhstatic void
138803b705cfSriastradhgen6_align_vertex(struct sna *sna, const struct sna_composite_op *op)
138903b705cfSriastradh{
139003b705cfSriastradh	assert (sna->render.vertex_offset == 0);
139103b705cfSriastradh	if (op->floats_per_vertex != sna->render_state.gen6.floats_per_vertex) {
139242542f5fSchristos		DBG(("aligning vertex: was %d, now %d floats per vertex\n",
139303b705cfSriastradh		     sna->render_state.gen6.floats_per_vertex,
139442542f5fSchristos		     op->floats_per_vertex));
139542542f5fSchristos		gen4_vertex_align(sna, op);
139603b705cfSriastradh		sna->render_state.gen6.floats_per_vertex = op->floats_per_vertex;
139703b705cfSriastradh	}
139803b705cfSriastradh	assert((sna->render.vertex_used % op->floats_per_vertex) == 0);
139903b705cfSriastradh}
140003b705cfSriastradh
140103b705cfSriastradhfastcall static void
140203b705cfSriastradhgen6_render_composite_blt(struct sna *sna,
140303b705cfSriastradh			  const struct sna_composite_op *op,
140403b705cfSriastradh			  const struct sna_composite_rectangles *r)
140503b705cfSriastradh{
140603b705cfSriastradh	gen6_get_rectangles(sna, op, 1, gen6_emit_composite_state);
140703b705cfSriastradh	op->prim_emit(sna, op, r);
140803b705cfSriastradh}
140903b705cfSriastradh
141003b705cfSriastradhfastcall static void
141103b705cfSriastradhgen6_render_composite_box(struct sna *sna,
141203b705cfSriastradh			  const struct sna_composite_op *op,
141303b705cfSriastradh			  const BoxRec *box)
141403b705cfSriastradh{
141503b705cfSriastradh	struct sna_composite_rectangles r;
141603b705cfSriastradh
141703b705cfSriastradh	gen6_get_rectangles(sna, op, 1, gen6_emit_composite_state);
141803b705cfSriastradh
141903b705cfSriastradh	DBG(("  %s: (%d, %d), (%d, %d)\n",
142003b705cfSriastradh	     __FUNCTION__,
142103b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
142203b705cfSriastradh
142303b705cfSriastradh	r.dst.x = box->x1;
142403b705cfSriastradh	r.dst.y = box->y1;
142503b705cfSriastradh	r.width  = box->x2 - box->x1;
142603b705cfSriastradh	r.height = box->y2 - box->y1;
142703b705cfSriastradh	r.src = r.mask = r.dst;
142803b705cfSriastradh
142903b705cfSriastradh	op->prim_emit(sna, op, &r);
143003b705cfSriastradh}
143103b705cfSriastradh
143203b705cfSriastradhstatic void
143303b705cfSriastradhgen6_render_composite_boxes__blt(struct sna *sna,
143403b705cfSriastradh				 const struct sna_composite_op *op,
143503b705cfSriastradh				 const BoxRec *box, int nbox)
143603b705cfSriastradh{
143703b705cfSriastradh	DBG(("composite_boxes(%d)\n", nbox));
143803b705cfSriastradh
143903b705cfSriastradh	do {
144003b705cfSriastradh		int nbox_this_time;
144103b705cfSriastradh
144203b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
144303b705cfSriastradh						     gen6_emit_composite_state);
144403b705cfSriastradh		nbox -= nbox_this_time;
144503b705cfSriastradh
144603b705cfSriastradh		do {
144703b705cfSriastradh			struct sna_composite_rectangles r;
144803b705cfSriastradh
144903b705cfSriastradh			DBG(("  %s: (%d, %d), (%d, %d)\n",
145003b705cfSriastradh			     __FUNCTION__,
145103b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
145203b705cfSriastradh
145303b705cfSriastradh			r.dst.x = box->x1;
145403b705cfSriastradh			r.dst.y = box->y1;
145503b705cfSriastradh			r.width  = box->x2 - box->x1;
145603b705cfSriastradh			r.height = box->y2 - box->y1;
145703b705cfSriastradh			r.src = r.mask = r.dst;
145803b705cfSriastradh
145903b705cfSriastradh			op->prim_emit(sna, op, &r);
146003b705cfSriastradh			box++;
146103b705cfSriastradh		} while (--nbox_this_time);
146203b705cfSriastradh	} while (nbox);
146303b705cfSriastradh}
146403b705cfSriastradh
146503b705cfSriastradhstatic void
146603b705cfSriastradhgen6_render_composite_boxes(struct sna *sna,
146703b705cfSriastradh			    const struct sna_composite_op *op,
146803b705cfSriastradh			    const BoxRec *box, int nbox)
146903b705cfSriastradh{
147003b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
147103b705cfSriastradh
147203b705cfSriastradh	do {
147303b705cfSriastradh		int nbox_this_time;
147403b705cfSriastradh		float *v;
147503b705cfSriastradh
147603b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
147703b705cfSriastradh						     gen6_emit_composite_state);
147803b705cfSriastradh		assert(nbox_this_time);
147903b705cfSriastradh		nbox -= nbox_this_time;
148003b705cfSriastradh
148103b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
148203b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
148303b705cfSriastradh
148403b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
148503b705cfSriastradh		box += nbox_this_time;
148603b705cfSriastradh	} while (nbox);
148703b705cfSriastradh}
148803b705cfSriastradh
148903b705cfSriastradhstatic void
149003b705cfSriastradhgen6_render_composite_boxes__thread(struct sna *sna,
149103b705cfSriastradh				    const struct sna_composite_op *op,
149203b705cfSriastradh				    const BoxRec *box, int nbox)
149303b705cfSriastradh{
149403b705cfSriastradh	DBG(("%s: nbox=%d\n", __FUNCTION__, nbox));
149503b705cfSriastradh
149603b705cfSriastradh	sna_vertex_lock(&sna->render);
149703b705cfSriastradh	do {
149803b705cfSriastradh		int nbox_this_time;
149903b705cfSriastradh		float *v;
150003b705cfSriastradh
150103b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, op, nbox,
150203b705cfSriastradh						     gen6_emit_composite_state);
150303b705cfSriastradh		assert(nbox_this_time);
150403b705cfSriastradh		nbox -= nbox_this_time;
150503b705cfSriastradh
150603b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
150703b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->floats_per_rect;
150803b705cfSriastradh
150903b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
151003b705cfSriastradh		sna_vertex_unlock(&sna->render);
151103b705cfSriastradh
151203b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
151303b705cfSriastradh		box += nbox_this_time;
151403b705cfSriastradh
151503b705cfSriastradh		sna_vertex_lock(&sna->render);
151603b705cfSriastradh		sna_vertex_release__locked(&sna->render);
151703b705cfSriastradh	} while (nbox);
151803b705cfSriastradh	sna_vertex_unlock(&sna->render);
151903b705cfSriastradh}
152003b705cfSriastradh
152103b705cfSriastradh#ifndef MAX
152203b705cfSriastradh#define MAX(a,b) ((a) > (b) ? (a) : (b))
152303b705cfSriastradh#endif
152403b705cfSriastradh
152503b705cfSriastradhstatic uint32_t
152603b705cfSriastradhgen6_composite_create_blend_state(struct sna_static_stream *stream)
152703b705cfSriastradh{
152803b705cfSriastradh	char *base, *ptr;
152903b705cfSriastradh	int src, dst;
153003b705cfSriastradh
153103b705cfSriastradh	base = sna_static_stream_map(stream,
153203b705cfSriastradh				     GEN6_BLENDFACTOR_COUNT * GEN6_BLENDFACTOR_COUNT * GEN6_BLEND_STATE_PADDED_SIZE,
153303b705cfSriastradh				     64);
153403b705cfSriastradh
153503b705cfSriastradh	ptr = base;
153603b705cfSriastradh	for (src = 0; src < GEN6_BLENDFACTOR_COUNT; src++) {
153703b705cfSriastradh		for (dst= 0; dst < GEN6_BLENDFACTOR_COUNT; dst++) {
153803b705cfSriastradh			struct gen6_blend_state *blend =
153903b705cfSriastradh				(struct gen6_blend_state *)ptr;
154003b705cfSriastradh
154103b705cfSriastradh			blend->blend0.dest_blend_factor = dst;
154203b705cfSriastradh			blend->blend0.source_blend_factor = src;
154303b705cfSriastradh			blend->blend0.blend_func = GEN6_BLENDFUNCTION_ADD;
154403b705cfSriastradh			blend->blend0.blend_enable =
154503b705cfSriastradh				!(dst == GEN6_BLENDFACTOR_ZERO && src == GEN6_BLENDFACTOR_ONE);
154603b705cfSriastradh
154703b705cfSriastradh			blend->blend1.post_blend_clamp_enable = 1;
154803b705cfSriastradh			blend->blend1.pre_blend_clamp_enable = 1;
154903b705cfSriastradh
155003b705cfSriastradh			ptr += GEN6_BLEND_STATE_PADDED_SIZE;
155103b705cfSriastradh		}
155203b705cfSriastradh	}
155303b705cfSriastradh
155403b705cfSriastradh	return sna_static_stream_offsetof(stream, base);
155503b705cfSriastradh}
155603b705cfSriastradh
155703b705cfSriastradhstatic uint32_t gen6_bind_video_source(struct sna *sna,
155803b705cfSriastradh				       struct kgem_bo *src_bo,
155903b705cfSriastradh				       uint32_t src_offset,
156003b705cfSriastradh				       int src_width,
156103b705cfSriastradh				       int src_height,
156203b705cfSriastradh				       int src_pitch,
156303b705cfSriastradh				       uint32_t src_surf_format)
156403b705cfSriastradh{
156503b705cfSriastradh	struct gen6_surface_state *ss;
156603b705cfSriastradh
156703b705cfSriastradh	sna->kgem.surface -= sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
156803b705cfSriastradh
156903b705cfSriastradh	ss = memset(sna->kgem.batch + sna->kgem.surface, 0, sizeof(*ss));
157003b705cfSriastradh	ss->ss0.surface_type = GEN6_SURFACE_2D;
157103b705cfSriastradh	ss->ss0.surface_format = src_surf_format;
157203b705cfSriastradh
157303b705cfSriastradh	ss->ss1.base_addr =
157403b705cfSriastradh		kgem_add_reloc(&sna->kgem,
157503b705cfSriastradh			       sna->kgem.surface + 1,
157603b705cfSriastradh			       src_bo,
157703b705cfSriastradh			       I915_GEM_DOMAIN_SAMPLER << 16,
157803b705cfSriastradh			       src_offset);
157903b705cfSriastradh
158003b705cfSriastradh	ss->ss2.width  = src_width - 1;
158103b705cfSriastradh	ss->ss2.height = src_height - 1;
158203b705cfSriastradh	ss->ss3.pitch  = src_pitch - 1;
158303b705cfSriastradh
158403b705cfSriastradh	return sna->kgem.surface * sizeof(uint32_t);
158503b705cfSriastradh}
158603b705cfSriastradh
158703b705cfSriastradhstatic void gen6_emit_video_state(struct sna *sna,
158803b705cfSriastradh				  const struct sna_composite_op *op)
158903b705cfSriastradh{
159003b705cfSriastradh	struct sna_video_frame *frame = op->priv;
1591fe8aea9eSmrg	uint32_t src_surf_format[6];
159203b705cfSriastradh	uint32_t src_surf_base[6];
159303b705cfSriastradh	int src_width[6];
159403b705cfSriastradh	int src_height[6];
159503b705cfSriastradh	int src_pitch[6];
159603b705cfSriastradh	uint32_t *binding_table;
159703b705cfSriastradh	uint16_t offset;
159803b705cfSriastradh	bool dirty;
159903b705cfSriastradh	int n_src, n;
160003b705cfSriastradh
160103b705cfSriastradh	dirty = gen6_get_batch(sna, op);
160203b705cfSriastradh
160303b705cfSriastradh	src_surf_base[0] = 0;
160403b705cfSriastradh	src_surf_base[1] = 0;
160503b705cfSriastradh	src_surf_base[2] = frame->VBufOffset;
160603b705cfSriastradh	src_surf_base[3] = frame->VBufOffset;
160703b705cfSriastradh	src_surf_base[4] = frame->UBufOffset;
160803b705cfSriastradh	src_surf_base[5] = frame->UBufOffset;
160903b705cfSriastradh
161003b705cfSriastradh	if (is_planar_fourcc(frame->id)) {
1611fe8aea9eSmrg		for (n = 0; n < 2; n++) {
1612fe8aea9eSmrg			src_surf_format[n] = GEN6_SURFACEFORMAT_R8_UNORM;
1613fe8aea9eSmrg			src_width[n]  = frame->width;
1614fe8aea9eSmrg			src_height[n] = frame->height;
1615fe8aea9eSmrg			src_pitch[n]  = frame->pitch[1];
1616fe8aea9eSmrg		}
1617fe8aea9eSmrg		for (; n < 6; n++) {
1618fe8aea9eSmrg			if (is_nv12_fourcc(frame->id))
1619fe8aea9eSmrg				src_surf_format[n] = GEN6_SURFACEFORMAT_R8G8_UNORM;
1620fe8aea9eSmrg			else
1621fe8aea9eSmrg				src_surf_format[n] = GEN6_SURFACEFORMAT_R8_UNORM;
1622fe8aea9eSmrg			src_width[n]  = frame->width / 2;
1623fe8aea9eSmrg			src_height[n] = frame->height / 2;
1624fe8aea9eSmrg			src_pitch[n]  = frame->pitch[0];
1625fe8aea9eSmrg		}
162603b705cfSriastradh		n_src = 6;
162703b705cfSriastradh	} else {
162803b705cfSriastradh		if (frame->id == FOURCC_UYVY)
1629fe8aea9eSmrg			src_surf_format[0] = GEN6_SURFACEFORMAT_YCRCB_SWAPY;
163003b705cfSriastradh		else
1631fe8aea9eSmrg			src_surf_format[0] = GEN6_SURFACEFORMAT_YCRCB_NORMAL;
163203b705cfSriastradh
163303b705cfSriastradh		src_width[0]  = frame->width;
163403b705cfSriastradh		src_height[0] = frame->height;
163503b705cfSriastradh		src_pitch[0]  = frame->pitch[0];
163603b705cfSriastradh		n_src = 1;
163703b705cfSriastradh	}
163803b705cfSriastradh
163903b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
164003b705cfSriastradh
164103b705cfSriastradh	binding_table[0] =
164203b705cfSriastradh		gen6_bind_bo(sna,
164303b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
164403b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
164503b705cfSriastradh			     true);
164603b705cfSriastradh	for (n = 0; n < n_src; n++) {
164703b705cfSriastradh		binding_table[1+n] =
164803b705cfSriastradh			gen6_bind_video_source(sna,
164903b705cfSriastradh					       frame->bo,
165003b705cfSriastradh					       src_surf_base[n],
165103b705cfSriastradh					       src_width[n],
165203b705cfSriastradh					       src_height[n],
165303b705cfSriastradh					       src_pitch[n],
1654fe8aea9eSmrg					       src_surf_format[n]);
165503b705cfSriastradh	}
165603b705cfSriastradh
165703b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
165803b705cfSriastradh}
165903b705cfSriastradh
1660fe8aea9eSmrgstatic unsigned select_video_kernel(const struct sna_video *video,
1661fe8aea9eSmrg				    const struct sna_video_frame *frame)
1662fe8aea9eSmrg{
1663fe8aea9eSmrg	switch (frame->id) {
1664fe8aea9eSmrg	case FOURCC_YV12:
1665fe8aea9eSmrg	case FOURCC_I420:
1666fe8aea9eSmrg	case FOURCC_XVMC:
1667fe8aea9eSmrg		return video->colorspace ?
1668fe8aea9eSmrg			GEN6_WM_KERNEL_VIDEO_PLANAR_BT709 :
1669fe8aea9eSmrg			GEN6_WM_KERNEL_VIDEO_PLANAR_BT601;
1670fe8aea9eSmrg
1671fe8aea9eSmrg	case FOURCC_NV12:
1672fe8aea9eSmrg		return video->colorspace ?
1673fe8aea9eSmrg			GEN6_WM_KERNEL_VIDEO_NV12_BT709 :
1674fe8aea9eSmrg			GEN6_WM_KERNEL_VIDEO_NV12_BT601;
1675fe8aea9eSmrg
1676fe8aea9eSmrg	default:
1677fe8aea9eSmrg		return video->colorspace ?
1678fe8aea9eSmrg			GEN6_WM_KERNEL_VIDEO_PACKED_BT709 :
1679fe8aea9eSmrg			GEN6_WM_KERNEL_VIDEO_PACKED_BT601;
1680fe8aea9eSmrg	}
1681fe8aea9eSmrg}
1682fe8aea9eSmrg
168303b705cfSriastradhstatic bool
168403b705cfSriastradhgen6_render_video(struct sna *sna,
168503b705cfSriastradh		  struct sna_video *video,
168603b705cfSriastradh		  struct sna_video_frame *frame,
168703b705cfSriastradh		  RegionPtr dstRegion,
168803b705cfSriastradh		  PixmapPtr pixmap)
168903b705cfSriastradh{
169003b705cfSriastradh	struct sna_composite_op tmp;
169142542f5fSchristos	struct sna_pixmap *priv = sna_pixmap(pixmap);
169203b705cfSriastradh	int dst_width = dstRegion->extents.x2 - dstRegion->extents.x1;
169303b705cfSriastradh	int dst_height = dstRegion->extents.y2 - dstRegion->extents.y1;
169403b705cfSriastradh	int src_width = frame->src.x2 - frame->src.x1;
169503b705cfSriastradh	int src_height = frame->src.y2 - frame->src.y1;
169603b705cfSriastradh	float src_offset_x, src_offset_y;
169703b705cfSriastradh	float src_scale_x, src_scale_y;
169803b705cfSriastradh	unsigned filter;
169942542f5fSchristos	const BoxRec *box;
1700fe8aea9eSmrg	int nbox;
170103b705cfSriastradh
170242542f5fSchristos	DBG(("%s: src=(%d, %d), dst=(%d, %d), %dx[(%d, %d), (%d, %d)...]\n",
170303b705cfSriastradh	     __FUNCTION__,
170403b705cfSriastradh	     src_width, src_height, dst_width, dst_height,
170542542f5fSchristos	     region_num_rects(dstRegion),
170603b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x1,
170703b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y1,
170803b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->x2,
170903b705cfSriastradh	     REGION_EXTENTS(NULL, dstRegion)->y2));
171003b705cfSriastradh
171142542f5fSchristos	assert(priv->gpu_bo);
171203b705cfSriastradh	memset(&tmp, 0, sizeof(tmp));
171303b705cfSriastradh
171403b705cfSriastradh	tmp.dst.pixmap = pixmap;
171503b705cfSriastradh	tmp.dst.width  = pixmap->drawable.width;
171603b705cfSriastradh	tmp.dst.height = pixmap->drawable.height;
171703b705cfSriastradh	tmp.dst.format = sna_render_format_for_depth(pixmap->drawable.depth);
171803b705cfSriastradh	tmp.dst.bo = priv->gpu_bo;
171903b705cfSriastradh
172003b705cfSriastradh	tmp.src.bo = frame->bo;
172103b705cfSriastradh	tmp.mask.bo = NULL;
172203b705cfSriastradh
172303b705cfSriastradh	tmp.floats_per_vertex = 3;
172403b705cfSriastradh	tmp.floats_per_rect = 9;
172503b705cfSriastradh
172603b705cfSriastradh	if (src_width == dst_width && src_height == dst_height)
172703b705cfSriastradh		filter = SAMPLER_FILTER_NEAREST;
172803b705cfSriastradh	else
172903b705cfSriastradh		filter = SAMPLER_FILTER_BILINEAR;
173003b705cfSriastradh
173103b705cfSriastradh	tmp.u.gen6.flags =
173203b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(filter, SAMPLER_EXTEND_PAD,
173303b705cfSriastradh					       SAMPLER_FILTER_NEAREST, SAMPLER_EXTEND_NONE),
173403b705cfSriastradh			       NO_BLEND,
1735fe8aea9eSmrg			       select_video_kernel(video, frame),
173603b705cfSriastradh			       2);
173703b705cfSriastradh	tmp.priv = frame;
173803b705cfSriastradh
173903b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
174003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL)) {
174103b705cfSriastradh		kgem_submit(&sna->kgem);
174203b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, tmp.dst.bo, frame->bo, NULL));
174303b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
174403b705cfSriastradh	}
174503b705cfSriastradh
174603b705cfSriastradh	gen6_align_vertex(sna, &tmp);
174742542f5fSchristos	gen6_emit_video_state(sna, &tmp);
174803b705cfSriastradh
174903b705cfSriastradh	src_scale_x = (float)src_width / dst_width / frame->width;
175003b705cfSriastradh	src_offset_x = (float)frame->src.x1 / frame->width - dstRegion->extents.x1 * src_scale_x;
175103b705cfSriastradh
175203b705cfSriastradh	src_scale_y = (float)src_height / dst_height / frame->height;
175303b705cfSriastradh	src_offset_y = (float)frame->src.y1 / frame->height - dstRegion->extents.y1 * src_scale_y;
175403b705cfSriastradh
175542542f5fSchristos	box = region_rects(dstRegion);
175642542f5fSchristos	nbox = region_num_rects(dstRegion);
175703b705cfSriastradh	while (nbox--) {
175803b705cfSriastradh		gen6_get_rectangles(sna, &tmp, 1, gen6_emit_video_state);
175903b705cfSriastradh
1760fe8aea9eSmrg		OUT_VERTEX(box->x2, box->y2);
176103b705cfSriastradh		OUT_VERTEX_F(box->x2 * src_scale_x + src_offset_x);
176203b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
176303b705cfSriastradh
1764fe8aea9eSmrg		OUT_VERTEX(box->x1, box->y2);
176503b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
176603b705cfSriastradh		OUT_VERTEX_F(box->y2 * src_scale_y + src_offset_y);
176703b705cfSriastradh
1768fe8aea9eSmrg		OUT_VERTEX(box->x1, box->y1);
176903b705cfSriastradh		OUT_VERTEX_F(box->x1 * src_scale_x + src_offset_x);
177003b705cfSriastradh		OUT_VERTEX_F(box->y1 * src_scale_y + src_offset_y);
177103b705cfSriastradh
177203b705cfSriastradh		box++;
177303b705cfSriastradh	}
177403b705cfSriastradh	gen4_vertex_flush(sna);
1775fe8aea9eSmrg
1776fe8aea9eSmrg	if (!DAMAGE_IS_ALL(priv->gpu_damage))
1777fe8aea9eSmrg		sna_damage_add(&priv->gpu_damage, dstRegion);
1778fe8aea9eSmrg
177903b705cfSriastradh	return true;
178003b705cfSriastradh}
178103b705cfSriastradh
178203b705cfSriastradhstatic int
178303b705cfSriastradhgen6_composite_picture(struct sna *sna,
178403b705cfSriastradh		       PicturePtr picture,
178503b705cfSriastradh		       struct sna_composite_channel *channel,
178603b705cfSriastradh		       int x, int y,
178703b705cfSriastradh		       int w, int h,
178803b705cfSriastradh		       int dst_x, int dst_y,
178903b705cfSriastradh		       bool precise)
179003b705cfSriastradh{
179103b705cfSriastradh	PixmapPtr pixmap;
179203b705cfSriastradh	uint32_t color;
179303b705cfSriastradh	int16_t dx, dy;
179403b705cfSriastradh
179542542f5fSchristos	DBG(("%s: (%d, %d)x(%d, %d), dst=(%d, %d), precise=%d\n",
179642542f5fSchristos	     __FUNCTION__, x, y, w, h, dst_x, dst_y, precise));
179703b705cfSriastradh
179803b705cfSriastradh	channel->is_solid = false;
179903b705cfSriastradh	channel->card_format = -1;
180003b705cfSriastradh
180103b705cfSriastradh	if (sna_picture_is_solid(picture, &color))
180203b705cfSriastradh		return gen4_channel_init_solid(sna, channel, color);
180303b705cfSriastradh
180403b705cfSriastradh	if (picture->pDrawable == NULL) {
180503b705cfSriastradh		int ret;
180603b705cfSriastradh
180703b705cfSriastradh		if (picture->pSourcePict->type == SourcePictTypeLinear)
180803b705cfSriastradh			return gen4_channel_init_linear(sna, picture, channel,
180903b705cfSriastradh							x, y,
181003b705cfSriastradh							w, h,
181103b705cfSriastradh							dst_x, dst_y);
181203b705cfSriastradh
181303b705cfSriastradh		DBG(("%s -- fixup, gradient\n", __FUNCTION__));
181403b705cfSriastradh		ret = -1;
181503b705cfSriastradh		if (!precise)
181603b705cfSriastradh			ret = sna_render_picture_approximate_gradient(sna, picture, channel,
181703b705cfSriastradh								      x, y, w, h, dst_x, dst_y);
181803b705cfSriastradh		if (ret == -1)
181903b705cfSriastradh			ret = sna_render_picture_fixup(sna, picture, channel,
182003b705cfSriastradh						       x, y, w, h, dst_x, dst_y);
182103b705cfSriastradh		return ret;
182203b705cfSriastradh	}
182303b705cfSriastradh
182403b705cfSriastradh	if (picture->alphaMap) {
182503b705cfSriastradh		DBG(("%s -- fixup, alphamap\n", __FUNCTION__));
182603b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
182703b705cfSriastradh						x, y, w, h, dst_x, dst_y);
182803b705cfSriastradh	}
182903b705cfSriastradh
183003b705cfSriastradh	if (!gen6_check_repeat(picture))
183103b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
183203b705cfSriastradh						x, y, w, h, dst_x, dst_y);
183303b705cfSriastradh
183403b705cfSriastradh	if (!gen6_check_filter(picture))
183503b705cfSriastradh		return sna_render_picture_fixup(sna, picture, channel,
183603b705cfSriastradh						x, y, w, h, dst_x, dst_y);
183703b705cfSriastradh
183803b705cfSriastradh	channel->repeat = picture->repeat ? picture->repeatType : RepeatNone;
183903b705cfSriastradh	channel->filter = picture->filter;
184003b705cfSriastradh
184103b705cfSriastradh	pixmap = get_drawable_pixmap(picture->pDrawable);
184203b705cfSriastradh	get_drawable_deltas(picture->pDrawable, pixmap, &dx, &dy);
184303b705cfSriastradh
184403b705cfSriastradh	x += dx + picture->pDrawable->x;
184503b705cfSriastradh	y += dy + picture->pDrawable->y;
184603b705cfSriastradh
184703b705cfSriastradh	channel->is_affine = sna_transform_is_affine(picture->transform);
184842542f5fSchristos	if (sna_transform_is_imprecise_integer_translation(picture->transform, picture->filter, precise, &dx, &dy)) {
184903b705cfSriastradh		DBG(("%s: integer translation (%d, %d), removing\n",
185003b705cfSriastradh		     __FUNCTION__, dx, dy));
185103b705cfSriastradh		x += dx;
185203b705cfSriastradh		y += dy;
185303b705cfSriastradh		channel->transform = NULL;
185403b705cfSriastradh		channel->filter = PictFilterNearest;
185542542f5fSchristos
185642542f5fSchristos		if (channel->repeat &&
185742542f5fSchristos		    (x >= 0 &&
185842542f5fSchristos		     y >= 0 &&
1859fe8aea9eSmrg		     x + w <= pixmap->drawable.width &&
1860fe8aea9eSmrg		     y + h <= pixmap->drawable.height)) {
186142542f5fSchristos			struct sna_pixmap *priv = sna_pixmap(pixmap);
186242542f5fSchristos			if (priv && priv->clear) {
186342542f5fSchristos				DBG(("%s: converting large pixmap source into solid [%08x]\n", __FUNCTION__, priv->clear_color));
1864fe8aea9eSmrg				return gen4_channel_init_solid(sna, channel, solid_color(picture->format, priv->clear_color));
186542542f5fSchristos			}
186642542f5fSchristos		}
186703b705cfSriastradh	} else
186803b705cfSriastradh		channel->transform = picture->transform;
186903b705cfSriastradh
187003b705cfSriastradh	channel->pict_format = picture->format;
187103b705cfSriastradh	channel->card_format = gen6_get_card_format(picture->format);
187203b705cfSriastradh	if (channel->card_format == (unsigned)-1)
187303b705cfSriastradh		return sna_render_picture_convert(sna, picture, channel, pixmap,
187403b705cfSriastradh						  x, y, w, h, dst_x, dst_y,
187503b705cfSriastradh						  false);
187603b705cfSriastradh
187703b705cfSriastradh	if (too_large(pixmap->drawable.width, pixmap->drawable.height)) {
187803b705cfSriastradh		DBG(("%s: extracting from pixmap %dx%d\n", __FUNCTION__,
187903b705cfSriastradh		     pixmap->drawable.width, pixmap->drawable.height));
188003b705cfSriastradh		return sna_render_picture_extract(sna, picture, channel,
188103b705cfSriastradh						  x, y, w, h, dst_x, dst_y);
188203b705cfSriastradh	}
188303b705cfSriastradh
188442542f5fSchristos	DBG(("%s: pixmap, repeat=%d, filter=%d, transform?=%d [affine? %d], format=%08x\n",
188542542f5fSchristos	     __FUNCTION__,
188642542f5fSchristos	     channel->repeat, channel->filter,
188742542f5fSchristos	     channel->transform != NULL, channel->is_affine,
188842542f5fSchristos	     channel->pict_format));
188942542f5fSchristos	if (channel->transform) {
189042542f5fSchristos#define f2d(x) (((double)(x))/65536.)
189142542f5fSchristos		DBG(("%s: transform=[%f %f %f, %f %f %f, %f %f %f] (raw [%x %x %x, %x %x %x, %x %x %x])\n",
189242542f5fSchristos		     __FUNCTION__,
189342542f5fSchristos		     f2d(channel->transform->matrix[0][0]),
189442542f5fSchristos		     f2d(channel->transform->matrix[0][1]),
189542542f5fSchristos		     f2d(channel->transform->matrix[0][2]),
189642542f5fSchristos		     f2d(channel->transform->matrix[1][0]),
189742542f5fSchristos		     f2d(channel->transform->matrix[1][1]),
189842542f5fSchristos		     f2d(channel->transform->matrix[1][2]),
189942542f5fSchristos		     f2d(channel->transform->matrix[2][0]),
190042542f5fSchristos		     f2d(channel->transform->matrix[2][1]),
190142542f5fSchristos		     f2d(channel->transform->matrix[2][2]),
190242542f5fSchristos		     channel->transform->matrix[0][0],
190342542f5fSchristos		     channel->transform->matrix[0][1],
190442542f5fSchristos		     channel->transform->matrix[0][2],
190542542f5fSchristos		     channel->transform->matrix[1][0],
190642542f5fSchristos		     channel->transform->matrix[1][1],
190742542f5fSchristos		     channel->transform->matrix[1][2],
190842542f5fSchristos		     channel->transform->matrix[2][0],
190942542f5fSchristos		     channel->transform->matrix[2][1],
191042542f5fSchristos		     channel->transform->matrix[2][2]));
191142542f5fSchristos#undef f2d
191242542f5fSchristos	}
191342542f5fSchristos
191403b705cfSriastradh	return sna_render_pixmap_bo(sna, channel, pixmap,
191503b705cfSriastradh				    x, y, w, h, dst_x, dst_y);
191603b705cfSriastradh}
191703b705cfSriastradh
191803b705cfSriastradhinline static void gen6_composite_channel_convert(struct sna_composite_channel *channel)
191903b705cfSriastradh{
192003b705cfSriastradh	channel->repeat = gen6_repeat(channel->repeat);
192103b705cfSriastradh	channel->filter = gen6_filter(channel->filter);
192203b705cfSriastradh	if (channel->card_format == (unsigned)-1)
192303b705cfSriastradh		channel->card_format = gen6_get_card_format(channel->pict_format);
192403b705cfSriastradh	assert(channel->card_format != (unsigned)-1);
192503b705cfSriastradh}
192603b705cfSriastradh
192703b705cfSriastradhstatic void gen6_render_composite_done(struct sna *sna,
192803b705cfSriastradh				       const struct sna_composite_op *op)
192903b705cfSriastradh{
193003b705cfSriastradh	DBG(("%s\n", __FUNCTION__));
193103b705cfSriastradh
193203b705cfSriastradh	assert(!sna->render.active);
193303b705cfSriastradh	if (sna->render.vertex_offset) {
193403b705cfSriastradh		gen4_vertex_flush(sna);
193503b705cfSriastradh		gen6_magic_ca_pass(sna, op);
193603b705cfSriastradh	}
193703b705cfSriastradh
193803b705cfSriastradh	if (op->mask.bo)
193903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->mask.bo);
194003b705cfSriastradh	if (op->src.bo)
194103b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->src.bo);
194203b705cfSriastradh
194303b705cfSriastradh	sna_render_composite_redirect_done(sna, op);
194403b705cfSriastradh}
194503b705cfSriastradh
194642542f5fSchristosinline static bool
194703b705cfSriastradhgen6_composite_set_target(struct sna *sna,
194803b705cfSriastradh			  struct sna_composite_op *op,
194903b705cfSriastradh			  PicturePtr dst,
195003b705cfSriastradh			  int x, int y, int w, int h,
195103b705cfSriastradh			  bool partial)
195203b705cfSriastradh{
195303b705cfSriastradh	BoxRec box;
195442542f5fSchristos	unsigned int hint;
195542542f5fSchristos
195642542f5fSchristos	DBG(("%s: (%d, %d)x(%d, %d), partial?=%d\n", __FUNCTION__, x, y, w, h, partial));
195703b705cfSriastradh
195803b705cfSriastradh	op->dst.pixmap = get_drawable_pixmap(dst->pDrawable);
195903b705cfSriastradh	op->dst.format = dst->format;
196003b705cfSriastradh	op->dst.width = op->dst.pixmap->drawable.width;
196103b705cfSriastradh	op->dst.height = op->dst.pixmap->drawable.height;
196203b705cfSriastradh
196303b705cfSriastradh	if (w && h) {
196403b705cfSriastradh		box.x1 = x;
196503b705cfSriastradh		box.y1 = y;
196603b705cfSriastradh		box.x2 = x + w;
196703b705cfSriastradh		box.y2 = y + h;
196803b705cfSriastradh	} else
196903b705cfSriastradh		sna_render_picture_extents(dst, &box);
197003b705cfSriastradh
1971fe8aea9eSmrg	hint = PREFER_GPU | RENDER_GPU;
1972fe8aea9eSmrg	if (!need_tiling(sna, op->dst.width, op->dst.height))
1973fe8aea9eSmrg		hint |= FORCE_GPU;
197442542f5fSchristos	if (!partial) {
197542542f5fSchristos		hint |= IGNORE_DAMAGE;
197642542f5fSchristos		if (w == op->dst.width && h == op->dst.height)
197742542f5fSchristos			hint |= REPLACES;
197842542f5fSchristos	}
197942542f5fSchristos
198042542f5fSchristos	op->dst.bo = sna_drawable_use_bo(dst->pDrawable, hint, &box, &op->damage);
198103b705cfSriastradh	if (op->dst.bo == NULL)
198203b705cfSriastradh		return false;
198303b705cfSriastradh
198442542f5fSchristos	if (hint & REPLACES) {
198542542f5fSchristos		struct sna_pixmap *priv = sna_pixmap(op->dst.pixmap);
198642542f5fSchristos		kgem_bo_pair_undo(&sna->kgem, priv->gpu_bo, priv->cpu_bo);
198742542f5fSchristos	}
198842542f5fSchristos
198903b705cfSriastradh	get_drawable_deltas(dst->pDrawable, op->dst.pixmap,
199003b705cfSriastradh			    &op->dst.x, &op->dst.y);
199103b705cfSriastradh
199242542f5fSchristos	DBG(("%s: pixmap=%ld, format=%08x, size=%dx%d, pitch=%d, delta=(%d,%d),damage=%p\n",
199303b705cfSriastradh	     __FUNCTION__,
199442542f5fSchristos	     op->dst.pixmap->drawable.serialNumber, (int)op->dst.format,
199503b705cfSriastradh	     op->dst.width, op->dst.height,
199603b705cfSriastradh	     op->dst.bo->pitch,
199703b705cfSriastradh	     op->dst.x, op->dst.y,
199803b705cfSriastradh	     op->damage ? *op->damage : (void *)-1));
199903b705cfSriastradh
200003b705cfSriastradh	assert(op->dst.bo->proxy == NULL);
200103b705cfSriastradh
200203b705cfSriastradh	if (too_large(op->dst.width, op->dst.height) &&
200303b705cfSriastradh	    !sna_render_composite_redirect(sna, op, x, y, w, h, partial))
200403b705cfSriastradh		return false;
200503b705cfSriastradh
200603b705cfSriastradh	return true;
200703b705cfSriastradh}
200803b705cfSriastradh
200903b705cfSriastradhstatic bool
201003b705cfSriastradhtry_blt(struct sna *sna,
2011fe8aea9eSmrg	uint8_t op,
2012fe8aea9eSmrg	PicturePtr src,
2013fe8aea9eSmrg	PicturePtr mask,
2014fe8aea9eSmrg	PicturePtr dst,
2015fe8aea9eSmrg	int16_t src_x, int16_t src_y,
2016fe8aea9eSmrg	int16_t msk_x, int16_t msk_y,
2017fe8aea9eSmrg	int16_t dst_x, int16_t dst_y,
2018fe8aea9eSmrg	int16_t width, int16_t height,
2019fe8aea9eSmrg	unsigned flags,
2020fe8aea9eSmrg	struct sna_composite_op *tmp)
202103b705cfSriastradh{
202203b705cfSriastradh	struct kgem_bo *bo;
202303b705cfSriastradh
202442542f5fSchristos	if (sna->kgem.mode == KGEM_BLT) {
202503b705cfSriastradh		DBG(("%s: already performing BLT\n", __FUNCTION__));
2026fe8aea9eSmrg		goto execute;
202703b705cfSriastradh	}
202803b705cfSriastradh
202903b705cfSriastradh	if (too_large(width, height)) {
203003b705cfSriastradh		DBG(("%s: operation too large for 3D pipe (%d, %d)\n",
203103b705cfSriastradh		     __FUNCTION__, width, height));
2032fe8aea9eSmrg		goto execute;
203303b705cfSriastradh	}
203403b705cfSriastradh
203542542f5fSchristos	bo = __sna_drawable_peek_bo(dst->pDrawable);
203642542f5fSchristos	if (bo == NULL)
2037fe8aea9eSmrg		goto execute;
2038fe8aea9eSmrg
2039fe8aea9eSmrg	if (untiled_tlb_miss(bo))
2040fe8aea9eSmrg		goto execute;
2041fe8aea9eSmrg
2042fe8aea9eSmrg	if (bo->rq) {
2043fe8aea9eSmrg		if (RQ_IS_BLT(bo->rq))
2044fe8aea9eSmrg			goto execute;
2045fe8aea9eSmrg
2046fe8aea9eSmrg		return false;
2047fe8aea9eSmrg	}
2048fe8aea9eSmrg
2049fe8aea9eSmrg	if (bo->tiling == I915_TILING_Y)
2050fe8aea9eSmrg		goto upload;
2051fe8aea9eSmrg
2052fe8aea9eSmrg	if (src->pDrawable == dst->pDrawable &&
2053fe8aea9eSmrg	    can_switch_to_blt(sna, bo, 0))
2054fe8aea9eSmrg		goto execute;
205542542f5fSchristos
205642542f5fSchristos	if (sna_picture_is_solid(src, NULL) && can_switch_to_blt(sna, bo, 0))
2057fe8aea9eSmrg		goto execute;
205842542f5fSchristos
205942542f5fSchristos	if (src->pDrawable) {
2060fe8aea9eSmrg		struct kgem_bo *s = __sna_drawable_peek_bo(src->pDrawable);
2061fe8aea9eSmrg		if (s == NULL)
2062fe8aea9eSmrg			goto execute;
206342542f5fSchristos
2064fe8aea9eSmrg		if (prefer_blt_bo(sna, s, bo))
2065fe8aea9eSmrg			goto execute;
206642542f5fSchristos	}
206742542f5fSchristos
206842542f5fSchristos	if (sna->kgem.ring == KGEM_BLT) {
206942542f5fSchristos		DBG(("%s: already performing BLT\n", __FUNCTION__));
2070fe8aea9eSmrg		goto execute;
207142542f5fSchristos	}
207203b705cfSriastradh
2073fe8aea9eSmrgupload:
2074fe8aea9eSmrg	flags |= COMPOSITE_UPLOAD;
2075fe8aea9eSmrgexecute:
2076fe8aea9eSmrg	return sna_blt_composite(sna, op,
2077fe8aea9eSmrg				 src, dst,
2078fe8aea9eSmrg				 src_x, src_y,
2079fe8aea9eSmrg				 dst_x, dst_y,
2080fe8aea9eSmrg				 width, height,
2081fe8aea9eSmrg				 flags, tmp);
208203b705cfSriastradh}
208303b705cfSriastradh
208403b705cfSriastradhstatic bool
208503b705cfSriastradhcheck_gradient(PicturePtr picture, bool precise)
208603b705cfSriastradh{
208703b705cfSriastradh	if (picture->pDrawable)
208803b705cfSriastradh		return false;
208903b705cfSriastradh
209003b705cfSriastradh	switch (picture->pSourcePict->type) {
209103b705cfSriastradh	case SourcePictTypeSolidFill:
209203b705cfSriastradh	case SourcePictTypeLinear:
209303b705cfSriastradh		return false;
209403b705cfSriastradh	default:
209503b705cfSriastradh		return precise;
209603b705cfSriastradh	}
209703b705cfSriastradh}
209803b705cfSriastradh
209903b705cfSriastradhstatic bool
210003b705cfSriastradhhas_alphamap(PicturePtr p)
210103b705cfSriastradh{
210203b705cfSriastradh	return p->alphaMap != NULL;
210303b705cfSriastradh}
210403b705cfSriastradh
210503b705cfSriastradhstatic bool
210603b705cfSriastradhneed_upload(PicturePtr p)
210703b705cfSriastradh{
210803b705cfSriastradh	return p->pDrawable && unattached(p->pDrawable) && untransformed(p);
210903b705cfSriastradh}
211003b705cfSriastradh
211103b705cfSriastradhstatic bool
211203b705cfSriastradhsource_is_busy(PixmapPtr pixmap)
211303b705cfSriastradh{
211403b705cfSriastradh	struct sna_pixmap *priv = sna_pixmap(pixmap);
211503b705cfSriastradh	if (priv == NULL || priv->clear)
211603b705cfSriastradh		return false;
211703b705cfSriastradh
211803b705cfSriastradh	if (priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo))
211903b705cfSriastradh		return true;
212003b705cfSriastradh
212103b705cfSriastradh	if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
212203b705cfSriastradh		return true;
212303b705cfSriastradh
212403b705cfSriastradh	return priv->gpu_damage && !priv->cpu_damage;
212503b705cfSriastradh}
212603b705cfSriastradh
212703b705cfSriastradhstatic bool
212803b705cfSriastradhsource_fallback(PicturePtr p, PixmapPtr pixmap, bool precise)
212903b705cfSriastradh{
213003b705cfSriastradh	if (sna_picture_is_solid(p, NULL))
213103b705cfSriastradh		return false;
213203b705cfSriastradh
213303b705cfSriastradh	if (p->pSourcePict)
213403b705cfSriastradh		return check_gradient(p, precise);
213503b705cfSriastradh
213603b705cfSriastradh	if (!gen6_check_repeat(p) || !gen6_check_format(p->format))
213703b705cfSriastradh		return true;
213803b705cfSriastradh
213903b705cfSriastradh	if (pixmap && source_is_busy(pixmap))
214003b705cfSriastradh		return false;
214103b705cfSriastradh
214203b705cfSriastradh	return has_alphamap(p) || !gen6_check_filter(p) || need_upload(p);
214303b705cfSriastradh}
214403b705cfSriastradh
214503b705cfSriastradhstatic bool
214603b705cfSriastradhgen6_composite_fallback(struct sna *sna,
214703b705cfSriastradh			PicturePtr src,
214803b705cfSriastradh			PicturePtr mask,
214903b705cfSriastradh			PicturePtr dst)
215003b705cfSriastradh{
215103b705cfSriastradh	PixmapPtr src_pixmap;
215203b705cfSriastradh	PixmapPtr mask_pixmap;
215303b705cfSriastradh	PixmapPtr dst_pixmap;
215403b705cfSriastradh	bool src_fallback, mask_fallback;
215503b705cfSriastradh
215603b705cfSriastradh	if (!gen6_check_dst_format(dst->format)) {
215703b705cfSriastradh		DBG(("%s: unknown destination format: %d\n",
215803b705cfSriastradh		     __FUNCTION__, dst->format));
215903b705cfSriastradh		return true;
216003b705cfSriastradh	}
216103b705cfSriastradh
216203b705cfSriastradh	dst_pixmap = get_drawable_pixmap(dst->pDrawable);
216303b705cfSriastradh
216403b705cfSriastradh	src_pixmap = src->pDrawable ? get_drawable_pixmap(src->pDrawable) : NULL;
216503b705cfSriastradh	src_fallback = source_fallback(src, src_pixmap,
216603b705cfSriastradh				       dst->polyMode == PolyModePrecise);
216703b705cfSriastradh
216803b705cfSriastradh	if (mask) {
216903b705cfSriastradh		mask_pixmap = mask->pDrawable ? get_drawable_pixmap(mask->pDrawable) : NULL;
217003b705cfSriastradh		mask_fallback = source_fallback(mask, mask_pixmap,
217103b705cfSriastradh						dst->polyMode == PolyModePrecise);
217203b705cfSriastradh	} else {
217303b705cfSriastradh		mask_pixmap = NULL;
217403b705cfSriastradh		mask_fallback = false;
217503b705cfSriastradh	}
217603b705cfSriastradh
217703b705cfSriastradh	/* If we are using the destination as a source and need to
217803b705cfSriastradh	 * readback in order to upload the source, do it all
217903b705cfSriastradh	 * on the cpu.
218003b705cfSriastradh	 */
218103b705cfSriastradh	if (src_pixmap == dst_pixmap && src_fallback) {
218203b705cfSriastradh		DBG(("%s: src is dst and will fallback\n",__FUNCTION__));
218303b705cfSriastradh		return true;
218403b705cfSriastradh	}
218503b705cfSriastradh	if (mask_pixmap == dst_pixmap && mask_fallback) {
218603b705cfSriastradh		DBG(("%s: mask is dst and will fallback\n",__FUNCTION__));
218703b705cfSriastradh		return true;
218803b705cfSriastradh	}
218903b705cfSriastradh
219003b705cfSriastradh	/* If anything is on the GPU, push everything out to the GPU */
219103b705cfSriastradh	if (dst_use_gpu(dst_pixmap)) {
219203b705cfSriastradh		DBG(("%s: dst is already on the GPU, try to use GPU\n",
219303b705cfSriastradh		     __FUNCTION__));
219403b705cfSriastradh		return false;
219503b705cfSriastradh	}
219603b705cfSriastradh
219703b705cfSriastradh	if (src_pixmap && !src_fallback) {
219803b705cfSriastradh		DBG(("%s: src is already on the GPU, try to use GPU\n",
219903b705cfSriastradh		     __FUNCTION__));
220003b705cfSriastradh		return false;
220103b705cfSriastradh	}
220203b705cfSriastradh	if (mask_pixmap && !mask_fallback) {
220303b705cfSriastradh		DBG(("%s: mask is already on the GPU, try to use GPU\n",
220403b705cfSriastradh		     __FUNCTION__));
220503b705cfSriastradh		return false;
220603b705cfSriastradh	}
220703b705cfSriastradh
220803b705cfSriastradh	/* However if the dst is not on the GPU and we need to
220903b705cfSriastradh	 * render one of the sources using the CPU, we may
221003b705cfSriastradh	 * as well do the entire operation in place onthe CPU.
221103b705cfSriastradh	 */
221203b705cfSriastradh	if (src_fallback) {
221303b705cfSriastradh		DBG(("%s: dst is on the CPU and src will fallback\n",
221403b705cfSriastradh		     __FUNCTION__));
221503b705cfSriastradh		return true;
221603b705cfSriastradh	}
221703b705cfSriastradh
221803b705cfSriastradh	if (mask && mask_fallback) {
221903b705cfSriastradh		DBG(("%s: dst is on the CPU and mask will fallback\n",
222003b705cfSriastradh		     __FUNCTION__));
222103b705cfSriastradh		return true;
222203b705cfSriastradh	}
222303b705cfSriastradh
222403b705cfSriastradh	if (too_large(dst_pixmap->drawable.width,
222503b705cfSriastradh		      dst_pixmap->drawable.height) &&
222603b705cfSriastradh	    dst_is_cpu(dst_pixmap)) {
222703b705cfSriastradh		DBG(("%s: dst is on the CPU and too large\n", __FUNCTION__));
222803b705cfSriastradh		return true;
222903b705cfSriastradh	}
223003b705cfSriastradh
223103b705cfSriastradh	DBG(("%s: dst is not on the GPU and the operation should not fallback\n",
223203b705cfSriastradh	     __FUNCTION__));
223303b705cfSriastradh	return dst_use_cpu(dst_pixmap);
223403b705cfSriastradh}
223503b705cfSriastradh
223603b705cfSriastradhstatic int
223703b705cfSriastradhreuse_source(struct sna *sna,
223803b705cfSriastradh	     PicturePtr src, struct sna_composite_channel *sc, int src_x, int src_y,
223903b705cfSriastradh	     PicturePtr mask, struct sna_composite_channel *mc, int msk_x, int msk_y)
224003b705cfSriastradh{
224103b705cfSriastradh	uint32_t color;
224203b705cfSriastradh
224303b705cfSriastradh	if (src_x != msk_x || src_y != msk_y)
224403b705cfSriastradh		return false;
224503b705cfSriastradh
224603b705cfSriastradh	if (src == mask) {
224703b705cfSriastradh		DBG(("%s: mask is source\n", __FUNCTION__));
224803b705cfSriastradh		*mc = *sc;
224903b705cfSriastradh		mc->bo = kgem_bo_reference(mc->bo);
225003b705cfSriastradh		return true;
225103b705cfSriastradh	}
225203b705cfSriastradh
225303b705cfSriastradh	if (sna_picture_is_solid(mask, &color))
225403b705cfSriastradh		return gen4_channel_init_solid(sna, mc, color);
225503b705cfSriastradh
225603b705cfSriastradh	if (sc->is_solid)
225703b705cfSriastradh		return false;
225803b705cfSriastradh
225903b705cfSriastradh	if (src->pDrawable == NULL || mask->pDrawable != src->pDrawable)
226003b705cfSriastradh		return false;
226103b705cfSriastradh
226203b705cfSriastradh	DBG(("%s: mask reuses source drawable\n", __FUNCTION__));
226303b705cfSriastradh
226403b705cfSriastradh	if (!sna_transform_equal(src->transform, mask->transform))
226503b705cfSriastradh		return false;
226603b705cfSriastradh
226703b705cfSriastradh	if (!sna_picture_alphamap_equal(src, mask))
226803b705cfSriastradh		return false;
226903b705cfSriastradh
227003b705cfSriastradh	if (!gen6_check_repeat(mask))
227103b705cfSriastradh		return false;
227203b705cfSriastradh
227303b705cfSriastradh	if (!gen6_check_filter(mask))
227403b705cfSriastradh		return false;
227503b705cfSriastradh
227603b705cfSriastradh	if (!gen6_check_format(mask->format))
227703b705cfSriastradh		return false;
227803b705cfSriastradh
227903b705cfSriastradh	DBG(("%s: reusing source channel for mask with a twist\n",
228003b705cfSriastradh	     __FUNCTION__));
228103b705cfSriastradh
228203b705cfSriastradh	*mc = *sc;
228303b705cfSriastradh	mc->repeat = gen6_repeat(mask->repeat ? mask->repeatType : RepeatNone);
228403b705cfSriastradh	mc->filter = gen6_filter(mask->filter);
228503b705cfSriastradh	mc->pict_format = mask->format;
228603b705cfSriastradh	mc->card_format = gen6_get_card_format(mask->format);
228703b705cfSriastradh	mc->bo = kgem_bo_reference(mc->bo);
228803b705cfSriastradh	return true;
228903b705cfSriastradh}
229003b705cfSriastradh
229103b705cfSriastradhstatic bool
229203b705cfSriastradhgen6_render_composite(struct sna *sna,
229303b705cfSriastradh		      uint8_t op,
229403b705cfSriastradh		      PicturePtr src,
229503b705cfSriastradh		      PicturePtr mask,
229603b705cfSriastradh		      PicturePtr dst,
229703b705cfSriastradh		      int16_t src_x, int16_t src_y,
229803b705cfSriastradh		      int16_t msk_x, int16_t msk_y,
229903b705cfSriastradh		      int16_t dst_x, int16_t dst_y,
230003b705cfSriastradh		      int16_t width, int16_t height,
230142542f5fSchristos		      unsigned flags,
230203b705cfSriastradh		      struct sna_composite_op *tmp)
230303b705cfSriastradh{
230403b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op))
230503b705cfSriastradh		return false;
230603b705cfSriastradh
230703b705cfSriastradh	DBG(("%s: %dx%d, current mode=%d\n", __FUNCTION__,
230803b705cfSriastradh	     width, height, sna->kgem.ring));
230903b705cfSriastradh
231003b705cfSriastradh	if (mask == NULL &&
2311fe8aea9eSmrg	    try_blt(sna, op,
2312fe8aea9eSmrg		    src, mask, dst,
2313fe8aea9eSmrg		    src_x, src_y,
2314fe8aea9eSmrg		    msk_x, msk_y,
2315fe8aea9eSmrg		    dst_x, dst_y,
2316fe8aea9eSmrg		    width, height,
2317fe8aea9eSmrg		    flags, tmp))
231803b705cfSriastradh		return true;
231903b705cfSriastradh
232003b705cfSriastradh	if (gen6_composite_fallback(sna, src, mask, dst))
232142542f5fSchristos		goto fallback;
232203b705cfSriastradh
232303b705cfSriastradh	if (need_tiling(sna, width, height))
232403b705cfSriastradh		return sna_tiling_composite(op, src, mask, dst,
232503b705cfSriastradh					    src_x, src_y,
232603b705cfSriastradh					    msk_x, msk_y,
232703b705cfSriastradh					    dst_x, dst_y,
232803b705cfSriastradh					    width, height,
232903b705cfSriastradh					    tmp);
233003b705cfSriastradh
233142542f5fSchristos	if (op == PictOpClear && src == sna->clear)
233203b705cfSriastradh		op = PictOpSrc;
233303b705cfSriastradh	tmp->op = op;
233403b705cfSriastradh	if (!gen6_composite_set_target(sna, tmp, dst,
233503b705cfSriastradh				       dst_x, dst_y, width, height,
233642542f5fSchristos				       flags & COMPOSITE_PARTIAL || op > PictOpSrc))
233742542f5fSchristos		goto fallback;
233803b705cfSriastradh
233903b705cfSriastradh	switch (gen6_composite_picture(sna, src, &tmp->src,
234003b705cfSriastradh				       src_x, src_y,
234103b705cfSriastradh				       width, height,
234203b705cfSriastradh				       dst_x, dst_y,
234303b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
234403b705cfSriastradh	case -1:
234503b705cfSriastradh		goto cleanup_dst;
234603b705cfSriastradh	case 0:
234703b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->src, 0))
234803b705cfSriastradh			goto cleanup_dst;
234903b705cfSriastradh		/* fall through to fixup */
235003b705cfSriastradh	case 1:
235103b705cfSriastradh		/* Did we just switch rings to prepare the source? */
235203b705cfSriastradh		if (mask == NULL &&
235303b705cfSriastradh		    prefer_blt_composite(sna, tmp) &&
235403b705cfSriastradh		    sna_blt_composite__convert(sna,
235503b705cfSriastradh					       dst_x, dst_y, width, height,
235603b705cfSriastradh					       tmp))
235703b705cfSriastradh			return true;
235803b705cfSriastradh
235903b705cfSriastradh		gen6_composite_channel_convert(&tmp->src);
236003b705cfSriastradh		break;
236103b705cfSriastradh	}
236203b705cfSriastradh
236303b705cfSriastradh	tmp->is_affine = tmp->src.is_affine;
236403b705cfSriastradh	tmp->has_component_alpha = false;
236503b705cfSriastradh	tmp->need_magic_ca_pass = false;
236603b705cfSriastradh
236703b705cfSriastradh	tmp->mask.bo = NULL;
236803b705cfSriastradh	tmp->mask.filter = SAMPLER_FILTER_NEAREST;
236903b705cfSriastradh	tmp->mask.repeat = SAMPLER_EXTEND_NONE;
237003b705cfSriastradh
237103b705cfSriastradh	if (mask) {
237203b705cfSriastradh		if (mask->componentAlpha && PICT_FORMAT_RGB(mask->format)) {
237303b705cfSriastradh			tmp->has_component_alpha = true;
237403b705cfSriastradh
237503b705cfSriastradh			/* Check if it's component alpha that relies on a source alpha and on
237603b705cfSriastradh			 * the source value.  We can only get one of those into the single
237703b705cfSriastradh			 * source value that we get to blend with.
237803b705cfSriastradh			 */
237903b705cfSriastradh			if (gen6_blend_op[op].src_alpha &&
238003b705cfSriastradh			    (gen6_blend_op[op].src_blend != GEN6_BLENDFACTOR_ZERO)) {
238103b705cfSriastradh				if (op != PictOpOver)
238203b705cfSriastradh					goto cleanup_src;
238303b705cfSriastradh
238403b705cfSriastradh				tmp->need_magic_ca_pass = true;
238503b705cfSriastradh				tmp->op = PictOpOutReverse;
238603b705cfSriastradh			}
238703b705cfSriastradh		}
238803b705cfSriastradh
238903b705cfSriastradh		if (!reuse_source(sna,
239003b705cfSriastradh				  src, &tmp->src, src_x, src_y,
239103b705cfSriastradh				  mask, &tmp->mask, msk_x, msk_y)) {
239203b705cfSriastradh			switch (gen6_composite_picture(sna, mask, &tmp->mask,
239303b705cfSriastradh						       msk_x, msk_y,
239403b705cfSriastradh						       width, height,
239503b705cfSriastradh						       dst_x, dst_y,
239603b705cfSriastradh						       dst->polyMode == PolyModePrecise)) {
239703b705cfSriastradh			case -1:
239803b705cfSriastradh				goto cleanup_src;
239903b705cfSriastradh			case 0:
240003b705cfSriastradh				if (!gen4_channel_init_solid(sna, &tmp->mask, 0))
240103b705cfSriastradh					goto cleanup_src;
240203b705cfSriastradh				/* fall through to fixup */
240303b705cfSriastradh			case 1:
240403b705cfSriastradh				gen6_composite_channel_convert(&tmp->mask);
240503b705cfSriastradh				break;
240603b705cfSriastradh			}
240703b705cfSriastradh		}
240803b705cfSriastradh
240903b705cfSriastradh		tmp->is_affine &= tmp->mask.is_affine;
241003b705cfSriastradh	}
241103b705cfSriastradh
241203b705cfSriastradh	tmp->u.gen6.flags =
241303b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(tmp->src.filter,
241403b705cfSriastradh					      tmp->src.repeat,
241503b705cfSriastradh					      tmp->mask.filter,
241603b705cfSriastradh					      tmp->mask.repeat),
241703b705cfSriastradh			       gen6_get_blend(tmp->op,
241803b705cfSriastradh					      tmp->has_component_alpha,
241903b705cfSriastradh					      tmp->dst.format),
242003b705cfSriastradh			       gen6_choose_composite_kernel(tmp->op,
242103b705cfSriastradh							    tmp->mask.bo != NULL,
242203b705cfSriastradh							    tmp->has_component_alpha,
242303b705cfSriastradh							    tmp->is_affine),
242403b705cfSriastradh			       gen4_choose_composite_emitter(sna, tmp));
242503b705cfSriastradh
242603b705cfSriastradh	tmp->blt   = gen6_render_composite_blt;
242703b705cfSriastradh	tmp->box   = gen6_render_composite_box;
242803b705cfSriastradh	tmp->boxes = gen6_render_composite_boxes__blt;
242903b705cfSriastradh	if (tmp->emit_boxes) {
243003b705cfSriastradh		tmp->boxes = gen6_render_composite_boxes;
243103b705cfSriastradh		tmp->thread_boxes = gen6_render_composite_boxes__thread;
243203b705cfSriastradh	}
243303b705cfSriastradh	tmp->done  = gen6_render_composite_done;
243403b705cfSriastradh
243503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->dst.bo);
243603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
243703b705cfSriastradh			   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
243803b705cfSriastradh			   NULL)) {
243903b705cfSriastradh		kgem_submit(&sna->kgem);
244003b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
244103b705cfSriastradh				   tmp->dst.bo, tmp->src.bo, tmp->mask.bo,
244203b705cfSriastradh				   NULL))
244303b705cfSriastradh			goto cleanup_mask;
244403b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
244503b705cfSriastradh	}
244603b705cfSriastradh
244703b705cfSriastradh	gen6_align_vertex(sna, tmp);
244842542f5fSchristos	gen6_emit_composite_state(sna, tmp);
244903b705cfSriastradh	return true;
245003b705cfSriastradh
245103b705cfSriastradhcleanup_mask:
245242542f5fSchristos	if (tmp->mask.bo) {
245303b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->mask.bo);
245442542f5fSchristos		tmp->mask.bo = NULL;
245542542f5fSchristos	}
245603b705cfSriastradhcleanup_src:
245742542f5fSchristos	if (tmp->src.bo) {
245803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->src.bo);
245942542f5fSchristos		tmp->src.bo = NULL;
246042542f5fSchristos	}
246103b705cfSriastradhcleanup_dst:
246242542f5fSchristos	if (tmp->redirect.real_bo) {
246303b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->dst.bo);
246442542f5fSchristos		tmp->redirect.real_bo = NULL;
246542542f5fSchristos	}
246642542f5fSchristosfallback:
246742542f5fSchristos	return (mask == NULL &&
246842542f5fSchristos		sna_blt_composite(sna, op,
246942542f5fSchristos				  src, dst,
247042542f5fSchristos				  src_x, src_y,
247142542f5fSchristos				  dst_x, dst_y,
247242542f5fSchristos				  width, height,
247342542f5fSchristos				  flags | COMPOSITE_FALLBACK, tmp));
247403b705cfSriastradh}
247503b705cfSriastradh
247603b705cfSriastradh#if !NO_COMPOSITE_SPANS
247703b705cfSriastradhfastcall static void
247803b705cfSriastradhgen6_render_composite_spans_box(struct sna *sna,
247903b705cfSriastradh				const struct sna_composite_spans_op *op,
248003b705cfSriastradh				const BoxRec *box, float opacity)
248103b705cfSriastradh{
248203b705cfSriastradh	DBG(("%s: src=+(%d, %d), opacity=%f, dst=+(%d, %d), box=(%d, %d) x (%d, %d)\n",
248303b705cfSriastradh	     __FUNCTION__,
248403b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
248503b705cfSriastradh	     opacity,
248603b705cfSriastradh	     op->base.dst.x, op->base.dst.y,
248703b705cfSriastradh	     box->x1, box->y1,
248803b705cfSriastradh	     box->x2 - box->x1,
248903b705cfSriastradh	     box->y2 - box->y1));
249003b705cfSriastradh
249103b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_composite_state);
249203b705cfSriastradh	op->prim_emit(sna, op, box, opacity);
249303b705cfSriastradh}
249403b705cfSriastradh
249503b705cfSriastradhstatic void
249603b705cfSriastradhgen6_render_composite_spans_boxes(struct sna *sna,
249703b705cfSriastradh				  const struct sna_composite_spans_op *op,
249803b705cfSriastradh				  const BoxRec *box, int nbox,
249903b705cfSriastradh				  float opacity)
250003b705cfSriastradh{
250103b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), opacity=%f, dst=+(%d, %d)\n",
250203b705cfSriastradh	     __FUNCTION__, nbox,
250303b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
250403b705cfSriastradh	     opacity,
250503b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
250603b705cfSriastradh
250703b705cfSriastradh	do {
250803b705cfSriastradh		int nbox_this_time;
250903b705cfSriastradh
251003b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
251103b705cfSriastradh						     gen6_emit_composite_state);
251203b705cfSriastradh		nbox -= nbox_this_time;
251303b705cfSriastradh
251403b705cfSriastradh		do {
251503b705cfSriastradh			DBG(("  %s: (%d, %d) x (%d, %d)\n", __FUNCTION__,
251603b705cfSriastradh			     box->x1, box->y1,
251703b705cfSriastradh			     box->x2 - box->x1,
251803b705cfSriastradh			     box->y2 - box->y1));
251903b705cfSriastradh
252003b705cfSriastradh			op->prim_emit(sna, op, box++, opacity);
252103b705cfSriastradh		} while (--nbox_this_time);
252203b705cfSriastradh	} while (nbox);
252303b705cfSriastradh}
252403b705cfSriastradh
252503b705cfSriastradhfastcall static void
252603b705cfSriastradhgen6_render_composite_spans_boxes__thread(struct sna *sna,
252703b705cfSriastradh					  const struct sna_composite_spans_op *op,
252803b705cfSriastradh					  const struct sna_opacity_box *box,
252903b705cfSriastradh					  int nbox)
253003b705cfSriastradh{
253103b705cfSriastradh	DBG(("%s: nbox=%d, src=+(%d, %d), dst=+(%d, %d)\n",
253203b705cfSriastradh	     __FUNCTION__, nbox,
253303b705cfSriastradh	     op->base.src.offset[0], op->base.src.offset[1],
253403b705cfSriastradh	     op->base.dst.x, op->base.dst.y));
253503b705cfSriastradh
253603b705cfSriastradh	sna_vertex_lock(&sna->render);
253703b705cfSriastradh	do {
253803b705cfSriastradh		int nbox_this_time;
253903b705cfSriastradh		float *v;
254003b705cfSriastradh
254103b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
254203b705cfSriastradh						     gen6_emit_composite_state);
254303b705cfSriastradh		assert(nbox_this_time);
254403b705cfSriastradh		nbox -= nbox_this_time;
254503b705cfSriastradh
254603b705cfSriastradh		v = sna->render.vertices + sna->render.vertex_used;
254703b705cfSriastradh		sna->render.vertex_used += nbox_this_time * op->base.floats_per_rect;
254803b705cfSriastradh
254903b705cfSriastradh		sna_vertex_acquire__locked(&sna->render);
255003b705cfSriastradh		sna_vertex_unlock(&sna->render);
255103b705cfSriastradh
255203b705cfSriastradh		op->emit_boxes(op, box, nbox_this_time, v);
255303b705cfSriastradh		box += nbox_this_time;
255403b705cfSriastradh
255503b705cfSriastradh		sna_vertex_lock(&sna->render);
255603b705cfSriastradh		sna_vertex_release__locked(&sna->render);
255703b705cfSriastradh	} while (nbox);
255803b705cfSriastradh	sna_vertex_unlock(&sna->render);
255903b705cfSriastradh}
256003b705cfSriastradh
256103b705cfSriastradhfastcall static void
256203b705cfSriastradhgen6_render_composite_spans_done(struct sna *sna,
256303b705cfSriastradh				 const struct sna_composite_spans_op *op)
256403b705cfSriastradh{
256503b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
256603b705cfSriastradh	assert(!sna->render.active);
256703b705cfSriastradh
256803b705cfSriastradh	if (sna->render.vertex_offset)
256903b705cfSriastradh		gen4_vertex_flush(sna);
257003b705cfSriastradh
257103b705cfSriastradh	if (op->base.src.bo)
257203b705cfSriastradh		kgem_bo_destroy(&sna->kgem, op->base.src.bo);
257303b705cfSriastradh
257403b705cfSriastradh	sna_render_composite_redirect_done(sna, &op->base);
257503b705cfSriastradh}
257603b705cfSriastradh
257703b705cfSriastradhstatic bool
257803b705cfSriastradhgen6_check_composite_spans(struct sna *sna,
257903b705cfSriastradh			   uint8_t op, PicturePtr src, PicturePtr dst,
258003b705cfSriastradh			   int16_t width, int16_t height,
258103b705cfSriastradh			   unsigned flags)
258203b705cfSriastradh{
258303b705cfSriastradh	DBG(("%s: op=%d, width=%d, height=%d, flags=%x\n",
258403b705cfSriastradh	     __FUNCTION__, op, width, height, flags));
258503b705cfSriastradh
258603b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op))
258703b705cfSriastradh		return false;
258803b705cfSriastradh
258903b705cfSriastradh	if (gen6_composite_fallback(sna, src, NULL, dst)) {
259003b705cfSriastradh		DBG(("%s: operation would fallback\n", __FUNCTION__));
259103b705cfSriastradh		return false;
259203b705cfSriastradh	}
259303b705cfSriastradh
259403b705cfSriastradh	if (need_tiling(sna, width, height) &&
259503b705cfSriastradh	    !is_gpu(sna, dst->pDrawable, PREFER_GPU_SPANS)) {
259603b705cfSriastradh		DBG(("%s: fallback, tiled operation not on GPU\n",
259703b705cfSriastradh		     __FUNCTION__));
259803b705cfSriastradh		return false;
259903b705cfSriastradh	}
260003b705cfSriastradh
260103b705cfSriastradh	if ((flags & COMPOSITE_SPANS_RECTILINEAR) == 0) {
260203b705cfSriastradh		struct sna_pixmap *priv = sna_pixmap_from_drawable(dst->pDrawable);
260303b705cfSriastradh		assert(priv);
260403b705cfSriastradh
260503b705cfSriastradh		if (priv->cpu_bo && kgem_bo_is_busy(priv->cpu_bo))
260603b705cfSriastradh			return true;
260703b705cfSriastradh
260803b705cfSriastradh		if (flags & COMPOSITE_SPANS_INPLACE_HINT)
260903b705cfSriastradh			return false;
261003b705cfSriastradh
261103b705cfSriastradh		return priv->gpu_bo && kgem_bo_is_busy(priv->gpu_bo);
261203b705cfSriastradh	}
261303b705cfSriastradh
261403b705cfSriastradh	return true;
261503b705cfSriastradh}
261603b705cfSriastradh
261703b705cfSriastradhstatic bool
261803b705cfSriastradhgen6_render_composite_spans(struct sna *sna,
261903b705cfSriastradh			    uint8_t op,
262003b705cfSriastradh			    PicturePtr src,
262103b705cfSriastradh			    PicturePtr dst,
262203b705cfSriastradh			    int16_t src_x,  int16_t src_y,
262303b705cfSriastradh			    int16_t dst_x,  int16_t dst_y,
262403b705cfSriastradh			    int16_t width,  int16_t height,
262503b705cfSriastradh			    unsigned flags,
262603b705cfSriastradh			    struct sna_composite_spans_op *tmp)
262703b705cfSriastradh{
262803b705cfSriastradh	DBG(("%s: %dx%d with flags=%x, current mode=%d\n", __FUNCTION__,
262903b705cfSriastradh	     width, height, flags, sna->kgem.ring));
263003b705cfSriastradh
263103b705cfSriastradh	assert(gen6_check_composite_spans(sna, op, src, dst, width, height, flags));
263203b705cfSriastradh
263303b705cfSriastradh	if (need_tiling(sna, width, height)) {
263403b705cfSriastradh		DBG(("%s: tiling, operation (%dx%d) too wide for pipeline\n",
263503b705cfSriastradh		     __FUNCTION__, width, height));
263603b705cfSriastradh		return sna_tiling_composite_spans(op, src, dst,
263703b705cfSriastradh						  src_x, src_y, dst_x, dst_y,
263803b705cfSriastradh						  width, height, flags, tmp);
263903b705cfSriastradh	}
264003b705cfSriastradh
264103b705cfSriastradh	tmp->base.op = op;
264203b705cfSriastradh	if (!gen6_composite_set_target(sna, &tmp->base, dst,
264303b705cfSriastradh				       dst_x, dst_y, width, height, true))
264403b705cfSriastradh		return false;
264503b705cfSriastradh
264603b705cfSriastradh	switch (gen6_composite_picture(sna, src, &tmp->base.src,
264703b705cfSriastradh				       src_x, src_y,
264803b705cfSriastradh				       width, height,
264903b705cfSriastradh				       dst_x, dst_y,
265003b705cfSriastradh				       dst->polyMode == PolyModePrecise)) {
265103b705cfSriastradh	case -1:
265203b705cfSriastradh		goto cleanup_dst;
265303b705cfSriastradh	case 0:
265403b705cfSriastradh		if (!gen4_channel_init_solid(sna, &tmp->base.src, 0))
265503b705cfSriastradh			goto cleanup_dst;
265603b705cfSriastradh		/* fall through to fixup */
265703b705cfSriastradh	case 1:
265803b705cfSriastradh		gen6_composite_channel_convert(&tmp->base.src);
265903b705cfSriastradh		break;
266003b705cfSriastradh	}
266103b705cfSriastradh	tmp->base.mask.bo = NULL;
266203b705cfSriastradh
266303b705cfSriastradh	tmp->base.is_affine = tmp->base.src.is_affine;
266403b705cfSriastradh	tmp->base.need_magic_ca_pass = false;
266503b705cfSriastradh
266603b705cfSriastradh	tmp->base.u.gen6.flags =
266703b705cfSriastradh		GEN6_SET_FLAGS(SAMPLER_OFFSET(tmp->base.src.filter,
266803b705cfSriastradh					      tmp->base.src.repeat,
266903b705cfSriastradh					      SAMPLER_FILTER_NEAREST,
267003b705cfSriastradh					      SAMPLER_EXTEND_PAD),
267103b705cfSriastradh			       gen6_get_blend(tmp->base.op, false, tmp->base.dst.format),
267203b705cfSriastradh			       GEN6_WM_KERNEL_OPACITY | !tmp->base.is_affine,
267303b705cfSriastradh			       gen4_choose_spans_emitter(sna, tmp));
267403b705cfSriastradh
267503b705cfSriastradh	tmp->box   = gen6_render_composite_spans_box;
267603b705cfSriastradh	tmp->boxes = gen6_render_composite_spans_boxes;
267703b705cfSriastradh	if (tmp->emit_boxes)
267803b705cfSriastradh		tmp->thread_boxes = gen6_render_composite_spans_boxes__thread;
267903b705cfSriastradh	tmp->done  = gen6_render_composite_spans_done;
268003b705cfSriastradh
268103b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp->base.dst.bo);
268203b705cfSriastradh	if (!kgem_check_bo(&sna->kgem,
268303b705cfSriastradh			   tmp->base.dst.bo, tmp->base.src.bo,
268403b705cfSriastradh			   NULL)) {
268503b705cfSriastradh		kgem_submit(&sna->kgem);
268603b705cfSriastradh		if (!kgem_check_bo(&sna->kgem,
268703b705cfSriastradh				   tmp->base.dst.bo, tmp->base.src.bo,
268803b705cfSriastradh				   NULL))
268903b705cfSriastradh			goto cleanup_src;
269003b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
269103b705cfSriastradh	}
269203b705cfSriastradh
269303b705cfSriastradh	gen6_align_vertex(sna, &tmp->base);
269442542f5fSchristos	gen6_emit_composite_state(sna, &tmp->base);
269503b705cfSriastradh	return true;
269603b705cfSriastradh
269703b705cfSriastradhcleanup_src:
269803b705cfSriastradh	if (tmp->base.src.bo)
269903b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.src.bo);
270003b705cfSriastradhcleanup_dst:
270103b705cfSriastradh	if (tmp->base.redirect.real_bo)
270203b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp->base.dst.bo);
270303b705cfSriastradh	return false;
270403b705cfSriastradh}
270503b705cfSriastradh#endif
270603b705cfSriastradh
270703b705cfSriastradhstatic void
270803b705cfSriastradhgen6_emit_copy_state(struct sna *sna,
270903b705cfSriastradh		     const struct sna_composite_op *op)
271003b705cfSriastradh{
271103b705cfSriastradh	uint32_t *binding_table;
271203b705cfSriastradh	uint16_t offset;
271303b705cfSriastradh	bool dirty;
271403b705cfSriastradh
271503b705cfSriastradh	dirty = gen6_get_batch(sna, op);
271603b705cfSriastradh
271703b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
271803b705cfSriastradh
271903b705cfSriastradh	binding_table[0] =
272003b705cfSriastradh		gen6_bind_bo(sna,
272103b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
272203b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
272303b705cfSriastradh			     true);
272403b705cfSriastradh	binding_table[1] =
272503b705cfSriastradh		gen6_bind_bo(sna,
272603b705cfSriastradh			     op->src.bo, op->src.width, op->src.height,
272703b705cfSriastradh			     op->src.card_format,
272803b705cfSriastradh			     false);
272903b705cfSriastradh
273003b705cfSriastradh	if (sna->kgem.surface == offset &&
273103b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table) {
273203b705cfSriastradh		sna->kgem.surface += sizeof(struct gen6_surface_state_padded) / sizeof(uint32_t);
273303b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
273403b705cfSriastradh	}
273503b705cfSriastradh
273603b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
273703b705cfSriastradh}
273803b705cfSriastradh
273903b705cfSriastradhstatic inline bool prefer_blt_copy(struct sna *sna,
274003b705cfSriastradh				   struct kgem_bo *src_bo,
274103b705cfSriastradh				   struct kgem_bo *dst_bo,
274203b705cfSriastradh				   unsigned flags)
274303b705cfSriastradh{
274403b705cfSriastradh	if (flags & COPY_SYNC)
274503b705cfSriastradh		return false;
274603b705cfSriastradh
274703b705cfSriastradh	if (PREFER_RENDER)
274803b705cfSriastradh		return PREFER_RENDER > 0;
274903b705cfSriastradh
275003b705cfSriastradh	if (sna->kgem.ring == KGEM_BLT)
275103b705cfSriastradh		return true;
275203b705cfSriastradh
2753fe8aea9eSmrg	if (flags & COPY_DRI && !sna->kgem.has_semaphores)
2754fe8aea9eSmrg		return false;
2755fe8aea9eSmrg
2756fe8aea9eSmrg	if ((flags & COPY_SMALL || src_bo == dst_bo) &&
2757fe8aea9eSmrg	    can_switch_to_blt(sna, dst_bo, flags))
275803b705cfSriastradh		return true;
275903b705cfSriastradh
276003b705cfSriastradh	if (untiled_tlb_miss(src_bo) ||
276103b705cfSriastradh	    untiled_tlb_miss(dst_bo))
276203b705cfSriastradh		return true;
276303b705cfSriastradh
2764fe8aea9eSmrg	if (force_blt_ring(sna, dst_bo))
276542542f5fSchristos		return true;
276642542f5fSchristos
276703b705cfSriastradh	if (kgem_bo_is_render(dst_bo) ||
276803b705cfSriastradh	    kgem_bo_is_render(src_bo))
276903b705cfSriastradh		return false;
277003b705cfSriastradh
2771fe8aea9eSmrg	if (flags & COPY_LAST &&
2772fe8aea9eSmrg            can_switch_to_blt(sna, dst_bo, flags))
2773fe8aea9eSmrg		return true;
2774fe8aea9eSmrg
277542542f5fSchristos	if (prefer_render_ring(sna, dst_bo))
277603b705cfSriastradh		return false;
277703b705cfSriastradh
277842542f5fSchristos	if (!prefer_blt_ring(sna, dst_bo, flags))
277903b705cfSriastradh		return false;
278003b705cfSriastradh
2781fe8aea9eSmrg	return prefer_blt_bo(sna, src_bo, dst_bo);
278203b705cfSriastradh}
278303b705cfSriastradh
278403b705cfSriastradhstatic bool
278503b705cfSriastradhgen6_render_copy_boxes(struct sna *sna, uint8_t alu,
278642542f5fSchristos		       const DrawableRec *src, struct kgem_bo *src_bo, int16_t src_dx, int16_t src_dy,
278742542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo, int16_t dst_dx, int16_t dst_dy,
278803b705cfSriastradh		       const BoxRec *box, int n, unsigned flags)
278903b705cfSriastradh{
279003b705cfSriastradh	struct sna_composite_op tmp;
279103b705cfSriastradh	BoxRec extents;
279203b705cfSriastradh
279303b705cfSriastradh	DBG(("%s (%d, %d)->(%d, %d) x %d, alu=%x, self-copy=%d, overlaps? %d\n",
279403b705cfSriastradh	     __FUNCTION__, src_dx, src_dy, dst_dx, dst_dy, n, alu,
279503b705cfSriastradh	     src_bo == dst_bo,
279603b705cfSriastradh	     overlaps(sna,
279703b705cfSriastradh		      src_bo, src_dx, src_dy,
279803b705cfSriastradh		      dst_bo, dst_dx, dst_dy,
279942542f5fSchristos		      box, n, flags, &extents)));
280003b705cfSriastradh
280103b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, flags) &&
280242542f5fSchristos	    sna_blt_compare_depth(src, dst) &&
280303b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
280403b705cfSriastradh			       src_bo, src_dx, src_dy,
280503b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
280642542f5fSchristos			       dst->bitsPerPixel,
280703b705cfSriastradh			       box, n))
280803b705cfSriastradh		return true;
280903b705cfSriastradh
281003b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear)) {
281103b705cfSriastradhfallback_blt:
281242542f5fSchristos		if (!sna_blt_compare_depth(src, dst))
281303b705cfSriastradh			return false;
281403b705cfSriastradh
281503b705cfSriastradh		return sna_blt_copy_boxes_fallback(sna, alu,
281603b705cfSriastradh						   src, src_bo, src_dx, src_dy,
281703b705cfSriastradh						   dst, dst_bo, dst_dx, dst_dy,
281803b705cfSriastradh						   box, n);
281903b705cfSriastradh	}
282003b705cfSriastradh
282103b705cfSriastradh	if (overlaps(sna,
282203b705cfSriastradh		     src_bo, src_dx, src_dy,
282303b705cfSriastradh		     dst_bo, dst_dx, dst_dy,
282442542f5fSchristos		     box, n, flags,
282542542f5fSchristos		     &extents)) {
282642542f5fSchristos		bool big = too_large(extents.x2-extents.x1, extents.y2-extents.y1);
282703b705cfSriastradh
282842542f5fSchristos		if ((big || can_switch_to_blt(sna, dst_bo, flags)) &&
282903b705cfSriastradh		    sna_blt_copy_boxes(sna, alu,
283003b705cfSriastradh				       src_bo, src_dx, src_dy,
283103b705cfSriastradh				       dst_bo, dst_dx, dst_dy,
283242542f5fSchristos				       dst->bitsPerPixel,
283303b705cfSriastradh				       box, n))
283403b705cfSriastradh			return true;
283503b705cfSriastradh
283642542f5fSchristos		if (big)
283742542f5fSchristos			goto fallback_blt;
283842542f5fSchristos
283942542f5fSchristos		assert(src_bo == dst_bo);
284042542f5fSchristos		assert(src->depth == dst->depth);
284142542f5fSchristos		assert(src->width == dst->width);
284242542f5fSchristos		assert(src->height == dst->height);
2843fe8aea9eSmrg		return sna_render_copy_boxes__overlap(sna, alu, dst, dst_bo,
284442542f5fSchristos						      src_dx, src_dy,
284542542f5fSchristos						      dst_dx, dst_dy,
284603b705cfSriastradh						      box, n, &extents);
284703b705cfSriastradh	}
284803b705cfSriastradh
284942542f5fSchristos	if (dst->depth == src->depth) {
285042542f5fSchristos		tmp.dst.format = sna_render_format_for_depth(dst->depth);
285103b705cfSriastradh		tmp.src.pict_format = tmp.dst.format;
285203b705cfSriastradh	} else {
285342542f5fSchristos		tmp.dst.format = sna_format_for_depth(dst->depth);
285442542f5fSchristos		tmp.src.pict_format = sna_format_for_depth(src->depth);
285503b705cfSriastradh	}
285603b705cfSriastradh	if (!gen6_check_format(tmp.src.pict_format))
285703b705cfSriastradh		goto fallback_blt;
285803b705cfSriastradh
285942542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
286042542f5fSchristos	tmp.dst.width  = dst->width;
286142542f5fSchristos	tmp.dst.height = dst->height;
286203b705cfSriastradh	tmp.dst.bo = dst_bo;
286303b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
286403b705cfSriastradh	tmp.damage = NULL;
286503b705cfSriastradh
286603b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
286703b705cfSriastradh	if (too_large(tmp.dst.width, tmp.dst.height)) {
286803b705cfSriastradh		int i;
286903b705cfSriastradh
287003b705cfSriastradh		extents = box[0];
287103b705cfSriastradh		for (i = 1; i < n; i++) {
287203b705cfSriastradh			if (box[i].x1 < extents.x1)
287303b705cfSriastradh				extents.x1 = box[i].x1;
287403b705cfSriastradh			if (box[i].y1 < extents.y1)
287503b705cfSriastradh				extents.y1 = box[i].y1;
287603b705cfSriastradh
287703b705cfSriastradh			if (box[i].x2 > extents.x2)
287803b705cfSriastradh				extents.x2 = box[i].x2;
287903b705cfSriastradh			if (box[i].y2 > extents.y2)
288003b705cfSriastradh				extents.y2 = box[i].y2;
288103b705cfSriastradh		}
288203b705cfSriastradh
288303b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
288403b705cfSriastradh						   extents.x1 + dst_dx,
288503b705cfSriastradh						   extents.y1 + dst_dy,
288603b705cfSriastradh						   extents.x2 - extents.x1,
288703b705cfSriastradh						   extents.y2 - extents.y1,
288803b705cfSriastradh						   n > 1))
288903b705cfSriastradh			goto fallback_tiled;
289003b705cfSriastradh	}
289103b705cfSriastradh
289203b705cfSriastradh	tmp.src.card_format = gen6_get_card_format(tmp.src.pict_format);
289342542f5fSchristos	if (too_large(src->width, src->height)) {
289403b705cfSriastradh		int i;
289503b705cfSriastradh
289603b705cfSriastradh		extents = box[0];
289703b705cfSriastradh		for (i = 1; i < n; i++) {
289803b705cfSriastradh			if (box[i].x1 < extents.x1)
289903b705cfSriastradh				extents.x1 = box[i].x1;
290003b705cfSriastradh			if (box[i].y1 < extents.y1)
290103b705cfSriastradh				extents.y1 = box[i].y1;
290203b705cfSriastradh
290303b705cfSriastradh			if (box[i].x2 > extents.x2)
290403b705cfSriastradh				extents.x2 = box[i].x2;
290503b705cfSriastradh			if (box[i].y2 > extents.y2)
290603b705cfSriastradh				extents.y2 = box[i].y2;
290703b705cfSriastradh		}
290803b705cfSriastradh
290903b705cfSriastradh		if (!sna_render_pixmap_partial(sna, src, src_bo, &tmp.src,
291003b705cfSriastradh					       extents.x1 + src_dx,
291103b705cfSriastradh					       extents.y1 + src_dy,
291203b705cfSriastradh					       extents.x2 - extents.x1,
291303b705cfSriastradh					       extents.y2 - extents.y1)) {
291403b705cfSriastradh			DBG(("%s: unable to extract partial pixmap\n", __FUNCTION__));
291503b705cfSriastradh			goto fallback_tiled_dst;
291603b705cfSriastradh		}
291703b705cfSriastradh	} else {
291803b705cfSriastradh		tmp.src.bo = src_bo;
291942542f5fSchristos		tmp.src.width  = src->width;
292042542f5fSchristos		tmp.src.height = src->height;
292142542f5fSchristos		tmp.src.offset[0] = tmp.src.offset[1] = 0;
292203b705cfSriastradh	}
292303b705cfSriastradh
292403b705cfSriastradh	tmp.mask.bo = NULL;
292503b705cfSriastradh
292603b705cfSriastradh	tmp.floats_per_vertex = 2;
292703b705cfSriastradh	tmp.floats_per_rect = 6;
292803b705cfSriastradh	tmp.need_magic_ca_pass = 0;
292903b705cfSriastradh
293003b705cfSriastradh	tmp.u.gen6.flags = COPY_FLAGS(alu);
293103b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
293203b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == COPY_SAMPLER);
293303b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == COPY_VERTEX);
293403b705cfSriastradh
293503b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, tmp.dst.bo);
293603b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
293703b705cfSriastradh		kgem_submit(&sna->kgem);
293803b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, tmp.dst.bo, tmp.src.bo, NULL)) {
293903b705cfSriastradh			DBG(("%s: too large for a single operation\n",
294003b705cfSriastradh			     __FUNCTION__));
294142542f5fSchristos			if (tmp.src.bo != src_bo)
294242542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.src.bo);
294342542f5fSchristos			if (tmp.redirect.real_bo)
294442542f5fSchristos				kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
294542542f5fSchristos			goto fallback_blt;
294603b705cfSriastradh		}
294703b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
294803b705cfSriastradh	}
294903b705cfSriastradh
295042542f5fSchristos	src_dx += tmp.src.offset[0];
295142542f5fSchristos	src_dy += tmp.src.offset[1];
295242542f5fSchristos
295342542f5fSchristos	dst_dx += tmp.dst.x;
295442542f5fSchristos	dst_dy += tmp.dst.y;
295542542f5fSchristos
295642542f5fSchristos	tmp.dst.x = tmp.dst.y = 0;
295742542f5fSchristos
295803b705cfSriastradh	gen6_align_vertex(sna, &tmp);
295942542f5fSchristos	gen6_emit_copy_state(sna, &tmp);
296003b705cfSriastradh
296103b705cfSriastradh	do {
296203b705cfSriastradh		int16_t *v;
296303b705cfSriastradh		int n_this_time;
296403b705cfSriastradh
296503b705cfSriastradh		n_this_time = gen6_get_rectangles(sna, &tmp, n,
296603b705cfSriastradh						  gen6_emit_copy_state);
296703b705cfSriastradh		n -= n_this_time;
296803b705cfSriastradh
296903b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
297003b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
297103b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
297203b705cfSriastradh		do {
297303b705cfSriastradh
297403b705cfSriastradh			DBG(("	(%d, %d) -> (%d, %d) + (%d, %d)\n",
297503b705cfSriastradh			     box->x1 + src_dx, box->y1 + src_dy,
297603b705cfSriastradh			     box->x1 + dst_dx, box->y1 + dst_dy,
297703b705cfSriastradh			     box->x2 - box->x1, box->y2 - box->y1));
297803b705cfSriastradh			v[0] = box->x2 + dst_dx;
297903b705cfSriastradh			v[2] = box->x2 + src_dx;
298003b705cfSriastradh			v[1]  = v[5] = box->y2 + dst_dy;
298103b705cfSriastradh			v[3]  = v[7] = box->y2 + src_dy;
298203b705cfSriastradh			v[8]  = v[4] = box->x1 + dst_dx;
298303b705cfSriastradh			v[10] = v[6] = box->x1 + src_dx;
298403b705cfSriastradh			v[9]  = box->y1 + dst_dy;
298503b705cfSriastradh			v[11] = box->y1 + src_dy;
298603b705cfSriastradh			v += 12; box++;
298703b705cfSriastradh		} while (--n_this_time);
298803b705cfSriastradh	} while (n);
298903b705cfSriastradh
299003b705cfSriastradh	gen4_vertex_flush(sna);
299103b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
299203b705cfSriastradh	if (tmp.src.bo != src_bo)
299303b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.src.bo);
299403b705cfSriastradh	return true;
299503b705cfSriastradh
299603b705cfSriastradhfallback_tiled_dst:
299703b705cfSriastradh	if (tmp.redirect.real_bo)
299803b705cfSriastradh		kgem_bo_destroy(&sna->kgem, tmp.dst.bo);
299903b705cfSriastradhfallback_tiled:
300042542f5fSchristos	if (sna_blt_compare_depth(src, dst) &&
300103b705cfSriastradh	    sna_blt_copy_boxes(sna, alu,
300203b705cfSriastradh			       src_bo, src_dx, src_dy,
300303b705cfSriastradh			       dst_bo, dst_dx, dst_dy,
300442542f5fSchristos			       dst->bitsPerPixel,
300503b705cfSriastradh			       box, n))
300603b705cfSriastradh		return true;
300703b705cfSriastradh
300803b705cfSriastradh	return sna_tiling_copy_boxes(sna, alu,
300903b705cfSriastradh				     src, src_bo, src_dx, src_dy,
301003b705cfSriastradh				     dst, dst_bo, dst_dx, dst_dy,
301103b705cfSriastradh				     box, n);
301203b705cfSriastradh}
301303b705cfSriastradh
301403b705cfSriastradhstatic void
301503b705cfSriastradhgen6_render_copy_blt(struct sna *sna,
301603b705cfSriastradh		     const struct sna_copy_op *op,
301703b705cfSriastradh		     int16_t sx, int16_t sy,
301803b705cfSriastradh		     int16_t w,  int16_t h,
301903b705cfSriastradh		     int16_t dx, int16_t dy)
302003b705cfSriastradh{
302103b705cfSriastradh	int16_t *v;
302203b705cfSriastradh
302303b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_copy_state);
302403b705cfSriastradh
302503b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
302603b705cfSriastradh	sna->render.vertex_used += 6;
302703b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
302803b705cfSriastradh
302903b705cfSriastradh	v[0]  = dx+w; v[1]  = dy+h;
303003b705cfSriastradh	v[2]  = sx+w; v[3]  = sy+h;
303103b705cfSriastradh	v[4]  = dx;   v[5]  = dy+h;
303203b705cfSriastradh	v[6]  = sx;   v[7]  = sy+h;
303303b705cfSriastradh	v[8]  = dx;   v[9]  = dy;
303403b705cfSriastradh	v[10] = sx;   v[11] = sy;
303503b705cfSriastradh}
303603b705cfSriastradh
303703b705cfSriastradhstatic void
303803b705cfSriastradhgen6_render_copy_done(struct sna *sna, const struct sna_copy_op *op)
303903b705cfSriastradh{
304003b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
304103b705cfSriastradh
304203b705cfSriastradh	assert(!sna->render.active);
304303b705cfSriastradh	if (sna->render.vertex_offset)
304403b705cfSriastradh		gen4_vertex_flush(sna);
304503b705cfSriastradh}
304603b705cfSriastradh
304703b705cfSriastradhstatic bool
304803b705cfSriastradhgen6_render_copy(struct sna *sna, uint8_t alu,
304903b705cfSriastradh		 PixmapPtr src, struct kgem_bo *src_bo,
305003b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
305103b705cfSriastradh		 struct sna_copy_op *op)
305203b705cfSriastradh{
305303b705cfSriastradh	DBG(("%s (alu=%d, src=(%dx%d), dst=(%dx%d))\n",
305403b705cfSriastradh	     __FUNCTION__, alu,
305503b705cfSriastradh	     src->drawable.width, src->drawable.height,
305603b705cfSriastradh	     dst->drawable.width, dst->drawable.height));
305703b705cfSriastradh
305803b705cfSriastradh	if (prefer_blt_copy(sna, src_bo, dst_bo, 0) &&
305903b705cfSriastradh	    sna_blt_compare_depth(&src->drawable, &dst->drawable) &&
306003b705cfSriastradh	    sna_blt_copy(sna, alu,
306103b705cfSriastradh			 src_bo, dst_bo,
306203b705cfSriastradh			 dst->drawable.bitsPerPixel,
306303b705cfSriastradh			 op))
306403b705cfSriastradh		return true;
306503b705cfSriastradh
306603b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) || src_bo == dst_bo ||
306703b705cfSriastradh	    too_large(src->drawable.width, src->drawable.height) ||
306803b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height)) {
306903b705cfSriastradhfallback:
307003b705cfSriastradh		if (!sna_blt_compare_depth(&src->drawable, &dst->drawable))
307103b705cfSriastradh			return false;
307203b705cfSriastradh
307303b705cfSriastradh		return sna_blt_copy(sna, alu, src_bo, dst_bo,
307403b705cfSriastradh				    dst->drawable.bitsPerPixel,
307503b705cfSriastradh				    op);
307603b705cfSriastradh	}
307703b705cfSriastradh
307803b705cfSriastradh	if (dst->drawable.depth == src->drawable.depth) {
307903b705cfSriastradh		op->base.dst.format = sna_render_format_for_depth(dst->drawable.depth);
308003b705cfSriastradh		op->base.src.pict_format = op->base.dst.format;
308103b705cfSriastradh	} else {
308203b705cfSriastradh		op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
308303b705cfSriastradh		op->base.src.pict_format = sna_format_for_depth(src->drawable.depth);
308403b705cfSriastradh	}
308503b705cfSriastradh	if (!gen6_check_format(op->base.src.pict_format))
308603b705cfSriastradh		goto fallback;
308703b705cfSriastradh
308803b705cfSriastradh	op->base.dst.pixmap = dst;
308903b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
309003b705cfSriastradh	op->base.dst.height = dst->drawable.height;
309103b705cfSriastradh	op->base.dst.bo = dst_bo;
309203b705cfSriastradh
309303b705cfSriastradh	op->base.src.bo = src_bo;
309403b705cfSriastradh	op->base.src.card_format =
309503b705cfSriastradh		gen6_get_card_format(op->base.src.pict_format);
309603b705cfSriastradh	op->base.src.width  = src->drawable.width;
309703b705cfSriastradh	op->base.src.height = src->drawable.height;
309803b705cfSriastradh
309903b705cfSriastradh	op->base.mask.bo = NULL;
310003b705cfSriastradh
310103b705cfSriastradh	op->base.floats_per_vertex = 2;
310203b705cfSriastradh	op->base.floats_per_rect = 6;
310303b705cfSriastradh
310403b705cfSriastradh	op->base.u.gen6.flags = COPY_FLAGS(alu);
310503b705cfSriastradh	assert(GEN6_KERNEL(op->base.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
310603b705cfSriastradh	assert(GEN6_SAMPLER(op->base.u.gen6.flags) == COPY_SAMPLER);
310703b705cfSriastradh	assert(GEN6_VERTEX(op->base.u.gen6.flags) == COPY_VERTEX);
310803b705cfSriastradh
310903b705cfSriastradh	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
311003b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL)) {
311103b705cfSriastradh		kgem_submit(&sna->kgem);
311203b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, dst_bo, src_bo, NULL))
311303b705cfSriastradh			goto fallback;
311403b705cfSriastradh		_kgem_set_mode(&sna->kgem, KGEM_RENDER);
311503b705cfSriastradh	}
311603b705cfSriastradh
311703b705cfSriastradh	gen6_align_vertex(sna, &op->base);
311842542f5fSchristos	gen6_emit_copy_state(sna, &op->base);
311903b705cfSriastradh
312003b705cfSriastradh	op->blt  = gen6_render_copy_blt;
312103b705cfSriastradh	op->done = gen6_render_copy_done;
312203b705cfSriastradh	return true;
312303b705cfSriastradh}
312403b705cfSriastradh
312503b705cfSriastradhstatic void
312603b705cfSriastradhgen6_emit_fill_state(struct sna *sna, const struct sna_composite_op *op)
312703b705cfSriastradh{
312803b705cfSriastradh	uint32_t *binding_table;
312903b705cfSriastradh	uint16_t offset;
313003b705cfSriastradh	bool dirty;
313103b705cfSriastradh
313203b705cfSriastradh	dirty = gen6_get_batch(sna, op);
313303b705cfSriastradh
313403b705cfSriastradh	binding_table = gen6_composite_get_binding_table(sna, &offset);
313503b705cfSriastradh
313603b705cfSriastradh	binding_table[0] =
313703b705cfSriastradh		gen6_bind_bo(sna,
313803b705cfSriastradh			     op->dst.bo, op->dst.width, op->dst.height,
313903b705cfSriastradh			     gen6_get_dest_format(op->dst.format),
314003b705cfSriastradh			     true);
314103b705cfSriastradh	binding_table[1] =
314203b705cfSriastradh		gen6_bind_bo(sna,
314303b705cfSriastradh			     op->src.bo, 1, 1,
314403b705cfSriastradh			     GEN6_SURFACEFORMAT_B8G8R8A8_UNORM,
314503b705cfSriastradh			     false);
314603b705cfSriastradh
314703b705cfSriastradh	if (sna->kgem.surface == offset &&
314803b705cfSriastradh	    *(uint64_t *)(sna->kgem.batch + sna->render_state.gen6.surface_table) == *(uint64_t*)binding_table) {
314903b705cfSriastradh		sna->kgem.surface +=
315003b705cfSriastradh			sizeof(struct gen6_surface_state_padded)/sizeof(uint32_t);
315103b705cfSriastradh		offset = sna->render_state.gen6.surface_table;
315203b705cfSriastradh	}
315303b705cfSriastradh
315403b705cfSriastradh	gen6_emit_state(sna, op, offset | dirty);
315503b705cfSriastradh}
315603b705cfSriastradh
315703b705cfSriastradhstatic bool
315803b705cfSriastradhgen6_render_fill_boxes(struct sna *sna,
315903b705cfSriastradh		       CARD8 op,
316003b705cfSriastradh		       PictFormat format,
316103b705cfSriastradh		       const xRenderColor *color,
316242542f5fSchristos		       const DrawableRec *dst, struct kgem_bo *dst_bo,
316303b705cfSriastradh		       const BoxRec *box, int n)
316403b705cfSriastradh{
316503b705cfSriastradh	struct sna_composite_op tmp;
316603b705cfSriastradh	uint32_t pixel;
316703b705cfSriastradh
316803b705cfSriastradh	DBG(("%s (op=%d, color=(%04x, %04x, %04x, %04x) [%08x])\n",
316903b705cfSriastradh	     __FUNCTION__, op,
317003b705cfSriastradh	     color->red, color->green, color->blue, color->alpha, (int)format));
317103b705cfSriastradh
317203b705cfSriastradh	if (op >= ARRAY_SIZE(gen6_blend_op)) {
317303b705cfSriastradh		DBG(("%s: fallback due to unhandled blend op: %d\n",
317403b705cfSriastradh		     __FUNCTION__, op));
317503b705cfSriastradh		return false;
317603b705cfSriastradh	}
317703b705cfSriastradh
317842542f5fSchristos	if (prefer_blt_fill(sna, dst_bo, FILL_BOXES) ||
317942542f5fSchristos	    !gen6_check_dst_format(format)) {
318003b705cfSriastradh		uint8_t alu = GXinvalid;
318103b705cfSriastradh
318203b705cfSriastradh		if (op <= PictOpSrc) {
318303b705cfSriastradh			pixel = 0;
318403b705cfSriastradh			if (op == PictOpClear)
318503b705cfSriastradh				alu = GXclear;
318603b705cfSriastradh			else if (sna_get_pixel_from_rgba(&pixel,
318703b705cfSriastradh							 color->red,
318803b705cfSriastradh							 color->green,
318903b705cfSriastradh							 color->blue,
319003b705cfSriastradh							 color->alpha,
319103b705cfSriastradh							 format))
319203b705cfSriastradh				alu = GXcopy;
319303b705cfSriastradh		}
319403b705cfSriastradh
319503b705cfSriastradh		if (alu != GXinvalid &&
319603b705cfSriastradh		    sna_blt_fill_boxes(sna, alu,
319742542f5fSchristos				       dst_bo, dst->bitsPerPixel,
319803b705cfSriastradh				       pixel, box, n))
319903b705cfSriastradh			return true;
320003b705cfSriastradh
320103b705cfSriastradh		if (!gen6_check_dst_format(format))
320203b705cfSriastradh			return false;
320303b705cfSriastradh	}
320403b705cfSriastradh
320503b705cfSriastradh	if (op == PictOpClear) {
320603b705cfSriastradh		pixel = 0;
320703b705cfSriastradh		op = PictOpSrc;
320803b705cfSriastradh	} else if (!sna_get_pixel_from_rgba(&pixel,
320903b705cfSriastradh					    color->red,
321003b705cfSriastradh					    color->green,
321103b705cfSriastradh					    color->blue,
321203b705cfSriastradh					    color->alpha,
321303b705cfSriastradh					    PICT_a8r8g8b8))
321403b705cfSriastradh		return false;
321503b705cfSriastradh
321603b705cfSriastradh	DBG(("%s(%08x x %d [(%d, %d), (%d, %d) ...])\n",
321703b705cfSriastradh	     __FUNCTION__, pixel, n,
321803b705cfSriastradh	     box[0].x1, box[0].y1, box[0].x2, box[0].y2));
321903b705cfSriastradh
322042542f5fSchristos	tmp.dst.pixmap = (PixmapPtr)dst;
322142542f5fSchristos	tmp.dst.width  = dst->width;
322242542f5fSchristos	tmp.dst.height = dst->height;
322303b705cfSriastradh	tmp.dst.format = format;
322403b705cfSriastradh	tmp.dst.bo = dst_bo;
322503b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
322603b705cfSriastradh	tmp.damage = NULL;
322703b705cfSriastradh
322803b705cfSriastradh	sna_render_composite_redirect_init(&tmp);
322942542f5fSchristos	if (too_large(dst->width, dst->height)) {
323003b705cfSriastradh		BoxRec extents;
323103b705cfSriastradh
323203b705cfSriastradh		boxes_extents(box, n, &extents);
323303b705cfSriastradh		if (!sna_render_composite_redirect(sna, &tmp,
323403b705cfSriastradh						   extents.x1, extents.y1,
323503b705cfSriastradh						   extents.x2 - extents.x1,
323603b705cfSriastradh						   extents.y2 - extents.y1,
323703b705cfSriastradh						   n > 1))
323803b705cfSriastradh			return sna_tiling_fill_boxes(sna, op, format, color,
323903b705cfSriastradh						     dst, dst_bo, box, n);
324003b705cfSriastradh	}
324103b705cfSriastradh
324203b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, pixel);
324303b705cfSriastradh	tmp.mask.bo = NULL;
324403b705cfSriastradh
324503b705cfSriastradh	tmp.floats_per_vertex = 2;
324603b705cfSriastradh	tmp.floats_per_rect = 6;
324703b705cfSriastradh	tmp.need_magic_ca_pass = false;
324803b705cfSriastradh
324903b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS(op, format);
325003b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
325103b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
325203b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
325303b705cfSriastradh
325442542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
325503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
325603b705cfSriastradh		kgem_submit(&sna->kgem);
325703b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, dst_bo, NULL));
325803b705cfSriastradh	}
325903b705cfSriastradh
326003b705cfSriastradh	gen6_align_vertex(sna, &tmp);
326142542f5fSchristos	gen6_emit_fill_state(sna, &tmp);
326203b705cfSriastradh
326303b705cfSriastradh	do {
326403b705cfSriastradh		int n_this_time;
326503b705cfSriastradh		int16_t *v;
326603b705cfSriastradh
326703b705cfSriastradh		n_this_time = gen6_get_rectangles(sna, &tmp, n,
326803b705cfSriastradh						  gen6_emit_fill_state);
326903b705cfSriastradh		n -= n_this_time;
327003b705cfSriastradh
327103b705cfSriastradh		v = (int16_t *)(sna->render.vertices + sna->render.vertex_used);
327203b705cfSriastradh		sna->render.vertex_used += 6 * n_this_time;
327303b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
327403b705cfSriastradh		do {
327503b705cfSriastradh			DBG(("	(%d, %d), (%d, %d)\n",
327603b705cfSriastradh			     box->x1, box->y1, box->x2, box->y2));
327703b705cfSriastradh
327803b705cfSriastradh			v[0] = box->x2;
327903b705cfSriastradh			v[5] = v[1] = box->y2;
328003b705cfSriastradh			v[8] = v[4] = box->x1;
328103b705cfSriastradh			v[9] = box->y1;
328203b705cfSriastradh			v[2] = v[3]  = v[7]  = 1;
328303b705cfSriastradh			v[6] = v[10] = v[11] = 0;
328403b705cfSriastradh			v += 12; box++;
328503b705cfSriastradh		} while (--n_this_time);
328603b705cfSriastradh	} while (n);
328703b705cfSriastradh
328803b705cfSriastradh	gen4_vertex_flush(sna);
328903b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
329003b705cfSriastradh	sna_render_composite_redirect_done(sna, &tmp);
329103b705cfSriastradh	return true;
329203b705cfSriastradh}
329303b705cfSriastradh
329403b705cfSriastradhstatic void
329503b705cfSriastradhgen6_render_op_fill_blt(struct sna *sna,
329603b705cfSriastradh			const struct sna_fill_op *op,
329703b705cfSriastradh			int16_t x, int16_t y, int16_t w, int16_t h)
329803b705cfSriastradh{
329903b705cfSriastradh	int16_t *v;
330003b705cfSriastradh
330103b705cfSriastradh	DBG(("%s: (%d, %d)x(%d, %d)\n", __FUNCTION__, x, y, w, h));
330203b705cfSriastradh
330303b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_fill_state);
330403b705cfSriastradh
330503b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
330603b705cfSriastradh	sna->render.vertex_used += 6;
330703b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
330803b705cfSriastradh
330903b705cfSriastradh	v[0] = x+w;
331003b705cfSriastradh	v[4] = v[8] = x;
331103b705cfSriastradh	v[1] = v[5] = y+h;
331203b705cfSriastradh	v[9] = y;
331303b705cfSriastradh
331403b705cfSriastradh	v[2] = v[3]  = v[7]  = 1;
331503b705cfSriastradh	v[6] = v[10] = v[11] = 0;
331603b705cfSriastradh}
331703b705cfSriastradh
331803b705cfSriastradhfastcall static void
331903b705cfSriastradhgen6_render_op_fill_box(struct sna *sna,
332003b705cfSriastradh			const struct sna_fill_op *op,
332103b705cfSriastradh			const BoxRec *box)
332203b705cfSriastradh{
332303b705cfSriastradh	int16_t *v;
332403b705cfSriastradh
332503b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)\n", __FUNCTION__,
332603b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2));
332703b705cfSriastradh
332803b705cfSriastradh	gen6_get_rectangles(sna, &op->base, 1, gen6_emit_fill_state);
332903b705cfSriastradh
333003b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
333103b705cfSriastradh	sna->render.vertex_used += 6;
333203b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
333303b705cfSriastradh
333403b705cfSriastradh	v[0] = box->x2;
333503b705cfSriastradh	v[8] = v[4] = box->x1;
333603b705cfSriastradh	v[5] = v[1] = box->y2;
333703b705cfSriastradh	v[9] = box->y1;
333803b705cfSriastradh
333903b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
334003b705cfSriastradh	v[6] = v[10] = v[11] = 0;
334103b705cfSriastradh}
334203b705cfSriastradh
334303b705cfSriastradhfastcall static void
334403b705cfSriastradhgen6_render_op_fill_boxes(struct sna *sna,
334503b705cfSriastradh			  const struct sna_fill_op *op,
334603b705cfSriastradh			  const BoxRec *box,
334703b705cfSriastradh			  int nbox)
334803b705cfSriastradh{
334903b705cfSriastradh	DBG(("%s: (%d, %d),(%d, %d)... x %d\n", __FUNCTION__,
335003b705cfSriastradh	     box->x1, box->y1, box->x2, box->y2, nbox));
335103b705cfSriastradh
335203b705cfSriastradh	do {
335303b705cfSriastradh		int nbox_this_time;
335403b705cfSriastradh		int16_t *v;
335503b705cfSriastradh
335603b705cfSriastradh		nbox_this_time = gen6_get_rectangles(sna, &op->base, nbox,
335703b705cfSriastradh						     gen6_emit_fill_state);
335803b705cfSriastradh		nbox -= nbox_this_time;
335903b705cfSriastradh
336003b705cfSriastradh		v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
336103b705cfSriastradh		sna->render.vertex_used += 6 * nbox_this_time;
336203b705cfSriastradh		assert(sna->render.vertex_used <= sna->render.vertex_size);
336303b705cfSriastradh
336403b705cfSriastradh		do {
336503b705cfSriastradh			v[0] = box->x2;
336603b705cfSriastradh			v[8] = v[4] = box->x1;
336703b705cfSriastradh			v[5] = v[1] = box->y2;
336803b705cfSriastradh			v[9] = box->y1;
336903b705cfSriastradh			v[7] = v[2]  = v[3]  = 1;
337003b705cfSriastradh			v[6] = v[10] = v[11] = 0;
337103b705cfSriastradh			box++; v += 12;
337203b705cfSriastradh		} while (--nbox_this_time);
337303b705cfSriastradh	} while (nbox);
337403b705cfSriastradh}
337503b705cfSriastradh
337603b705cfSriastradhstatic void
337703b705cfSriastradhgen6_render_op_fill_done(struct sna *sna, const struct sna_fill_op *op)
337803b705cfSriastradh{
337903b705cfSriastradh	DBG(("%s()\n", __FUNCTION__));
338003b705cfSriastradh
338103b705cfSriastradh	assert(!sna->render.active);
338203b705cfSriastradh	if (sna->render.vertex_offset)
338303b705cfSriastradh		gen4_vertex_flush(sna);
338403b705cfSriastradh	kgem_bo_destroy(&sna->kgem, op->base.src.bo);
338503b705cfSriastradh}
338603b705cfSriastradh
338703b705cfSriastradhstatic bool
338803b705cfSriastradhgen6_render_fill(struct sna *sna, uint8_t alu,
338903b705cfSriastradh		 PixmapPtr dst, struct kgem_bo *dst_bo,
339042542f5fSchristos		 uint32_t color, unsigned flags,
339103b705cfSriastradh		 struct sna_fill_op *op)
339203b705cfSriastradh{
339303b705cfSriastradh	DBG(("%s: (alu=%d, color=%x)\n", __FUNCTION__, alu, color));
339403b705cfSriastradh
339542542f5fSchristos	if (prefer_blt_fill(sna, dst_bo, flags) &&
339603b705cfSriastradh	    sna_blt_fill(sna, alu,
339703b705cfSriastradh			 dst_bo, dst->drawable.bitsPerPixel,
339803b705cfSriastradh			 color,
339903b705cfSriastradh			 op))
340003b705cfSriastradh		return true;
340103b705cfSriastradh
340203b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
340303b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
340403b705cfSriastradh		return sna_blt_fill(sna, alu,
340503b705cfSriastradh				    dst_bo, dst->drawable.bitsPerPixel,
340603b705cfSriastradh				    color,
340703b705cfSriastradh				    op);
340803b705cfSriastradh
340903b705cfSriastradh	if (alu == GXclear)
341003b705cfSriastradh		color = 0;
341103b705cfSriastradh
341203b705cfSriastradh	op->base.dst.pixmap = dst;
341303b705cfSriastradh	op->base.dst.width  = dst->drawable.width;
341403b705cfSriastradh	op->base.dst.height = dst->drawable.height;
341503b705cfSriastradh	op->base.dst.format = sna_format_for_depth(dst->drawable.depth);
341603b705cfSriastradh	op->base.dst.bo = dst_bo;
341703b705cfSriastradh	op->base.dst.x = op->base.dst.y = 0;
341803b705cfSriastradh
341903b705cfSriastradh	op->base.src.bo =
342003b705cfSriastradh		sna_render_get_solid(sna,
342103b705cfSriastradh				     sna_rgba_for_color(color,
342203b705cfSriastradh							dst->drawable.depth));
342303b705cfSriastradh	op->base.mask.bo = NULL;
342403b705cfSriastradh
342503b705cfSriastradh	op->base.need_magic_ca_pass = false;
342603b705cfSriastradh	op->base.floats_per_vertex = 2;
342703b705cfSriastradh	op->base.floats_per_rect = 6;
342803b705cfSriastradh
342903b705cfSriastradh	op->base.u.gen6.flags = FILL_FLAGS_NOBLEND;
343003b705cfSriastradh	assert(GEN6_KERNEL(op->base.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
343103b705cfSriastradh	assert(GEN6_SAMPLER(op->base.u.gen6.flags) == FILL_SAMPLER);
343203b705cfSriastradh	assert(GEN6_VERTEX(op->base.u.gen6.flags) == FILL_VERTEX);
343303b705cfSriastradh
343442542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, dst_bo);
343503b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, dst_bo, NULL)) {
343603b705cfSriastradh		kgem_submit(&sna->kgem);
343703b705cfSriastradh		assert(kgem_check_bo(&sna->kgem, dst_bo, NULL));
343803b705cfSriastradh	}
343903b705cfSriastradh
344003b705cfSriastradh	gen6_align_vertex(sna, &op->base);
344142542f5fSchristos	gen6_emit_fill_state(sna, &op->base);
344203b705cfSriastradh
344303b705cfSriastradh	op->blt  = gen6_render_op_fill_blt;
344403b705cfSriastradh	op->box  = gen6_render_op_fill_box;
344503b705cfSriastradh	op->boxes = gen6_render_op_fill_boxes;
344642542f5fSchristos	op->points = NULL;
344703b705cfSriastradh	op->done = gen6_render_op_fill_done;
344803b705cfSriastradh	return true;
344903b705cfSriastradh}
345003b705cfSriastradh
345103b705cfSriastradhstatic bool
345203b705cfSriastradhgen6_render_fill_one_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
345303b705cfSriastradh			     uint32_t color,
345403b705cfSriastradh			     int16_t x1, int16_t y1, int16_t x2, int16_t y2,
345503b705cfSriastradh			     uint8_t alu)
345603b705cfSriastradh{
345703b705cfSriastradh	BoxRec box;
345803b705cfSriastradh
345903b705cfSriastradh	box.x1 = x1;
346003b705cfSriastradh	box.y1 = y1;
346103b705cfSriastradh	box.x2 = x2;
346203b705cfSriastradh	box.y2 = y2;
346303b705cfSriastradh
346403b705cfSriastradh	return sna_blt_fill_boxes(sna, alu,
346503b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
346603b705cfSriastradh				  color, &box, 1);
346703b705cfSriastradh}
346803b705cfSriastradh
346903b705cfSriastradhstatic bool
347003b705cfSriastradhgen6_render_fill_one(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo,
347103b705cfSriastradh		     uint32_t color,
347203b705cfSriastradh		     int16_t x1, int16_t y1,
347303b705cfSriastradh		     int16_t x2, int16_t y2,
347403b705cfSriastradh		     uint8_t alu)
347503b705cfSriastradh{
347603b705cfSriastradh	struct sna_composite_op tmp;
347703b705cfSriastradh	int16_t *v;
347803b705cfSriastradh
347903b705cfSriastradh	/* Prefer to use the BLT if already engaged */
348042542f5fSchristos	if (prefer_blt_fill(sna, bo, FILL_BOXES) &&
348103b705cfSriastradh	    gen6_render_fill_one_try_blt(sna, dst, bo, color,
348203b705cfSriastradh					 x1, y1, x2, y2, alu))
348303b705cfSriastradh		return true;
348403b705cfSriastradh
348503b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
348603b705cfSriastradh	if (!(alu == GXcopy || alu == GXclear) ||
348703b705cfSriastradh	    too_large(dst->drawable.width, dst->drawable.height))
348803b705cfSriastradh		return gen6_render_fill_one_try_blt(sna, dst, bo, color,
348903b705cfSriastradh						    x1, y1, x2, y2, alu);
349003b705cfSriastradh
349103b705cfSriastradh	if (alu == GXclear)
349203b705cfSriastradh		color = 0;
349303b705cfSriastradh
349403b705cfSriastradh	tmp.dst.pixmap = dst;
349503b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
349603b705cfSriastradh	tmp.dst.height = dst->drawable.height;
349703b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
349803b705cfSriastradh	tmp.dst.bo = bo;
349903b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
350003b705cfSriastradh
350103b705cfSriastradh	tmp.src.bo =
350203b705cfSriastradh		sna_render_get_solid(sna,
350303b705cfSriastradh				     sna_rgba_for_color(color,
350403b705cfSriastradh							dst->drawable.depth));
350503b705cfSriastradh	tmp.mask.bo = NULL;
350603b705cfSriastradh
350703b705cfSriastradh	tmp.floats_per_vertex = 2;
350803b705cfSriastradh	tmp.floats_per_rect = 6;
350903b705cfSriastradh	tmp.need_magic_ca_pass = false;
351003b705cfSriastradh
351103b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS_NOBLEND;
351203b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
351303b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
351403b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
351503b705cfSriastradh
351642542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, bo);
351703b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
351803b705cfSriastradh		kgem_submit(&sna->kgem);
351903b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
352003b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
352103b705cfSriastradh			return false;
352203b705cfSriastradh		}
352303b705cfSriastradh	}
352403b705cfSriastradh
352503b705cfSriastradh	gen6_align_vertex(sna, &tmp);
352642542f5fSchristos	gen6_emit_fill_state(sna, &tmp);
352703b705cfSriastradh
352803b705cfSriastradh	gen6_get_rectangles(sna, &tmp, 1, gen6_emit_fill_state);
352903b705cfSriastradh
353003b705cfSriastradh	DBG(("	(%d, %d), (%d, %d)\n", x1, y1, x2, y2));
353103b705cfSriastradh
353203b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
353303b705cfSriastradh	sna->render.vertex_used += 6;
353403b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
353503b705cfSriastradh
353603b705cfSriastradh	v[0] = x2;
353703b705cfSriastradh	v[8] = v[4] = x1;
353803b705cfSriastradh	v[5] = v[1] = y2;
353903b705cfSriastradh	v[9] = y1;
354003b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
354103b705cfSriastradh	v[6] = v[10] = v[11] = 0;
354203b705cfSriastradh
354303b705cfSriastradh	gen4_vertex_flush(sna);
354403b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
354503b705cfSriastradh
354603b705cfSriastradh	return true;
354703b705cfSriastradh}
354803b705cfSriastradh
354903b705cfSriastradhstatic bool
355003b705cfSriastradhgen6_render_clear_try_blt(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
355103b705cfSriastradh{
355203b705cfSriastradh	BoxRec box;
355303b705cfSriastradh
355403b705cfSriastradh	box.x1 = 0;
355503b705cfSriastradh	box.y1 = 0;
355603b705cfSriastradh	box.x2 = dst->drawable.width;
355703b705cfSriastradh	box.y2 = dst->drawable.height;
355803b705cfSriastradh
355903b705cfSriastradh	return sna_blt_fill_boxes(sna, GXclear,
356003b705cfSriastradh				  bo, dst->drawable.bitsPerPixel,
356103b705cfSriastradh				  0, &box, 1);
356203b705cfSriastradh}
356303b705cfSriastradh
356403b705cfSriastradhstatic bool
356503b705cfSriastradhgen6_render_clear(struct sna *sna, PixmapPtr dst, struct kgem_bo *bo)
356603b705cfSriastradh{
356703b705cfSriastradh	struct sna_composite_op tmp;
356803b705cfSriastradh	int16_t *v;
356903b705cfSriastradh
357003b705cfSriastradh	DBG(("%s: %dx%d\n",
357103b705cfSriastradh	     __FUNCTION__,
357203b705cfSriastradh	     dst->drawable.width,
357303b705cfSriastradh	     dst->drawable.height));
357403b705cfSriastradh
357503b705cfSriastradh	/* Prefer to use the BLT if, and only if, already engaged */
357603b705cfSriastradh	if (sna->kgem.ring == KGEM_BLT &&
357703b705cfSriastradh	    gen6_render_clear_try_blt(sna, dst, bo))
357803b705cfSriastradh		return true;
357903b705cfSriastradh
358003b705cfSriastradh	/* Must use the BLT if we can't RENDER... */
358103b705cfSriastradh	if (too_large(dst->drawable.width, dst->drawable.height))
358203b705cfSriastradh		return gen6_render_clear_try_blt(sna, dst, bo);
358303b705cfSriastradh
358403b705cfSriastradh	tmp.dst.pixmap = dst;
358503b705cfSriastradh	tmp.dst.width  = dst->drawable.width;
358603b705cfSriastradh	tmp.dst.height = dst->drawable.height;
358703b705cfSriastradh	tmp.dst.format = sna_format_for_depth(dst->drawable.depth);
358803b705cfSriastradh	tmp.dst.bo = bo;
358903b705cfSriastradh	tmp.dst.x = tmp.dst.y = 0;
359003b705cfSriastradh
359103b705cfSriastradh	tmp.src.bo = sna_render_get_solid(sna, 0);
359203b705cfSriastradh	tmp.mask.bo = NULL;
359303b705cfSriastradh
359403b705cfSriastradh	tmp.floats_per_vertex = 2;
359503b705cfSriastradh	tmp.floats_per_rect = 6;
359603b705cfSriastradh	tmp.need_magic_ca_pass = false;
359703b705cfSriastradh
359803b705cfSriastradh	tmp.u.gen6.flags = FILL_FLAGS_NOBLEND;
359903b705cfSriastradh	assert(GEN6_KERNEL(tmp.u.gen6.flags) == GEN6_WM_KERNEL_NOMASK);
360003b705cfSriastradh	assert(GEN6_SAMPLER(tmp.u.gen6.flags) == FILL_SAMPLER);
360103b705cfSriastradh	assert(GEN6_VERTEX(tmp.u.gen6.flags) == FILL_VERTEX);
360203b705cfSriastradh
360342542f5fSchristos	kgem_set_mode(&sna->kgem, KGEM_RENDER, bo);
360403b705cfSriastradh	if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
360503b705cfSriastradh		kgem_submit(&sna->kgem);
360603b705cfSriastradh		if (!kgem_check_bo(&sna->kgem, bo, NULL)) {
360703b705cfSriastradh			kgem_bo_destroy(&sna->kgem, tmp.src.bo);
360803b705cfSriastradh			return false;
360903b705cfSriastradh		}
361003b705cfSriastradh	}
361103b705cfSriastradh
361203b705cfSriastradh	gen6_align_vertex(sna, &tmp);
361342542f5fSchristos	gen6_emit_fill_state(sna, &tmp);
361403b705cfSriastradh
361503b705cfSriastradh	gen6_get_rectangles(sna, &tmp, 1, gen6_emit_fill_state);
361603b705cfSriastradh
361703b705cfSriastradh	v = (int16_t *)&sna->render.vertices[sna->render.vertex_used];
361803b705cfSriastradh	sna->render.vertex_used += 6;
361903b705cfSriastradh	assert(sna->render.vertex_used <= sna->render.vertex_size);
362003b705cfSriastradh
362103b705cfSriastradh	v[0] = dst->drawable.width;
362203b705cfSriastradh	v[5] = v[1] = dst->drawable.height;
362303b705cfSriastradh	v[8] = v[4] = 0;
362403b705cfSriastradh	v[9] = 0;
362503b705cfSriastradh
362603b705cfSriastradh	v[7] = v[2]  = v[3]  = 1;
362703b705cfSriastradh	v[6] = v[10] = v[11] = 0;
362803b705cfSriastradh
362903b705cfSriastradh	gen4_vertex_flush(sna);
363003b705cfSriastradh	kgem_bo_destroy(&sna->kgem, tmp.src.bo);
363103b705cfSriastradh
363203b705cfSriastradh	return true;
363303b705cfSriastradh}
363403b705cfSriastradh
363503b705cfSriastradhstatic void gen6_render_reset(struct sna *sna)
363603b705cfSriastradh{
363703b705cfSriastradh	sna->render_state.gen6.needs_invariant = true;
363803b705cfSriastradh	sna->render_state.gen6.first_state_packet = true;
363903b705cfSriastradh	sna->render_state.gen6.ve_id = 3 << 2;
364003b705cfSriastradh	sna->render_state.gen6.last_primitive = -1;
364103b705cfSriastradh
364203b705cfSriastradh	sna->render_state.gen6.num_sf_outputs = 0;
364303b705cfSriastradh	sna->render_state.gen6.samplers = -1;
364403b705cfSriastradh	sna->render_state.gen6.blend = -1;
364503b705cfSriastradh	sna->render_state.gen6.kernel = -1;
364603b705cfSriastradh	sna->render_state.gen6.drawrect_offset = -1;
364703b705cfSriastradh	sna->render_state.gen6.drawrect_limit = -1;
364803b705cfSriastradh	sna->render_state.gen6.surface_table = -1;
364903b705cfSriastradh
365042542f5fSchristos	if (sna->render.vbo && !kgem_bo_can_map(&sna->kgem, sna->render.vbo)) {
365142542f5fSchristos		DBG(("%s: discarding unmappable vbo\n", __FUNCTION__));
365242542f5fSchristos		discard_vbo(sna);
365342542f5fSchristos	}
365442542f5fSchristos
365503b705cfSriastradh	sna->render.vertex_offset = 0;
365603b705cfSriastradh	sna->render.nvertex_reloc = 0;
365703b705cfSriastradh	sna->render.vb_id = 0;
365803b705cfSriastradh}
365903b705cfSriastradh
366003b705cfSriastradhstatic void gen6_render_fini(struct sna *sna)
366103b705cfSriastradh{
366203b705cfSriastradh	kgem_bo_destroy(&sna->kgem, sna->render_state.gen6.general_bo);
366303b705cfSriastradh}
366403b705cfSriastradh
366542542f5fSchristosstatic bool is_gt2(struct sna *sna, int devid)
366603b705cfSriastradh{
366742542f5fSchristos	return devid & 0x30;
366803b705cfSriastradh}
366903b705cfSriastradh
367042542f5fSchristosstatic bool is_mobile(struct sna *sna, int devid)
367103b705cfSriastradh{
367242542f5fSchristos	return (devid & 0xf) == 0x6;
367303b705cfSriastradh}
367403b705cfSriastradh
367542542f5fSchristosstatic bool gen6_render_setup(struct sna *sna, int devid)
367603b705cfSriastradh{
367703b705cfSriastradh	struct gen6_render_state *state = &sna->render_state.gen6;
367803b705cfSriastradh	struct sna_static_stream general;
367903b705cfSriastradh	struct gen6_sampler_state *ss;
368003b705cfSriastradh	int i, j, k, l, m;
368103b705cfSriastradh
368203b705cfSriastradh	state->info = &gt1_info;
368342542f5fSchristos	if (is_gt2(sna, devid))
368403b705cfSriastradh		state->info = &gt2_info; /* XXX requires GT_MODE WiZ disabled */
368542542f5fSchristos	state->gt = state->info->gt;
368603b705cfSriastradh
368703b705cfSriastradh	sna_static_stream_init(&general);
368803b705cfSriastradh
368903b705cfSriastradh	/* Zero pad the start. If you see an offset of 0x0 in the batchbuffer
369003b705cfSriastradh	 * dumps, you know it points to zero.
369103b705cfSriastradh	 */
369203b705cfSriastradh	null_create(&general);
369303b705cfSriastradh	scratch_create(&general);
369403b705cfSriastradh
369503b705cfSriastradh	for (m = 0; m < GEN6_KERNEL_COUNT; m++) {
369603b705cfSriastradh		if (wm_kernels[m].size) {
369703b705cfSriastradh			state->wm_kernel[m][1] =
369803b705cfSriastradh				sna_static_stream_add(&general,
369903b705cfSriastradh						      wm_kernels[m].data,
370003b705cfSriastradh						      wm_kernels[m].size,
370103b705cfSriastradh						      64);
370203b705cfSriastradh		} else {
370303b705cfSriastradh			if (USE_8_PIXEL_DISPATCH) {
370403b705cfSriastradh				state->wm_kernel[m][0] =
370503b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
370603b705cfSriastradh								     wm_kernels[m].data, 8);
370703b705cfSriastradh			}
370803b705cfSriastradh
370903b705cfSriastradh			if (USE_16_PIXEL_DISPATCH) {
371003b705cfSriastradh				state->wm_kernel[m][1] =
371103b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
371203b705cfSriastradh								     wm_kernels[m].data, 16);
371303b705cfSriastradh			}
371403b705cfSriastradh
371503b705cfSriastradh			if (USE_32_PIXEL_DISPATCH) {
371603b705cfSriastradh				state->wm_kernel[m][2] =
371703b705cfSriastradh					sna_static_stream_compile_wm(sna, &general,
371803b705cfSriastradh								     wm_kernels[m].data, 32);
371903b705cfSriastradh			}
372003b705cfSriastradh		}
372103b705cfSriastradh		if ((state->wm_kernel[m][0]|state->wm_kernel[m][1]|state->wm_kernel[m][2]) == 0) {
372203b705cfSriastradh			state->wm_kernel[m][1] =
372303b705cfSriastradh				sna_static_stream_compile_wm(sna, &general,
372403b705cfSriastradh							     wm_kernels[m].data, 16);
372503b705cfSriastradh		}
372603b705cfSriastradh	}
372703b705cfSriastradh
372803b705cfSriastradh	ss = sna_static_stream_map(&general,
372903b705cfSriastradh				   2 * sizeof(*ss) *
373003b705cfSriastradh				   (2 +
373103b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT *
373203b705cfSriastradh				    FILTER_COUNT * EXTEND_COUNT),
373303b705cfSriastradh				   32);
373403b705cfSriastradh	state->wm_state = sna_static_stream_offsetof(&general, ss);
373503b705cfSriastradh	sampler_copy_init(ss); ss += 2;
373603b705cfSriastradh	sampler_fill_init(ss); ss += 2;
373703b705cfSriastradh	for (i = 0; i < FILTER_COUNT; i++) {
373803b705cfSriastradh		for (j = 0; j < EXTEND_COUNT; j++) {
373903b705cfSriastradh			for (k = 0; k < FILTER_COUNT; k++) {
374003b705cfSriastradh				for (l = 0; l < EXTEND_COUNT; l++) {
374103b705cfSriastradh					sampler_state_init(ss++, i, j);
374203b705cfSriastradh					sampler_state_init(ss++, k, l);
374303b705cfSriastradh				}
374403b705cfSriastradh			}
374503b705cfSriastradh		}
374603b705cfSriastradh	}
374703b705cfSriastradh
374803b705cfSriastradh	state->cc_blend = gen6_composite_create_blend_state(&general);
374903b705cfSriastradh
375003b705cfSriastradh	state->general_bo = sna_static_stream_fini(sna, &general);
375103b705cfSriastradh	return state->general_bo != NULL;
375203b705cfSriastradh}
375303b705cfSriastradh
375403b705cfSriastradhconst char *gen6_render_init(struct sna *sna, const char *backend)
375503b705cfSriastradh{
375613496ba1Ssnj	int devid = intel_get_device_id(sna->dev);
375742542f5fSchristos
375842542f5fSchristos	if (!gen6_render_setup(sna, devid))
375903b705cfSriastradh		return backend;
376003b705cfSriastradh
376103b705cfSriastradh	sna->kgem.context_switch = gen6_render_context_switch;
376203b705cfSriastradh	sna->kgem.retire = gen6_render_retire;
376342542f5fSchristos	sna->kgem.expire = gen4_render_expire;
376403b705cfSriastradh
376503b705cfSriastradh#if !NO_COMPOSITE
376603b705cfSriastradh	sna->render.composite = gen6_render_composite;
376703b705cfSriastradh	sna->render.prefer_gpu |= PREFER_GPU_RENDER;
376803b705cfSriastradh#endif
376903b705cfSriastradh
377003b705cfSriastradh#if !NO_COMPOSITE_SPANS
377103b705cfSriastradh	sna->render.check_composite_spans = gen6_check_composite_spans;
377203b705cfSriastradh	sna->render.composite_spans = gen6_render_composite_spans;
377342542f5fSchristos	if (is_mobile(sna, devid))
377403b705cfSriastradh		sna->render.prefer_gpu |= PREFER_GPU_SPANS;
377503b705cfSriastradh#endif
377603b705cfSriastradh	sna->render.video = gen6_render_video;
377703b705cfSriastradh
377803b705cfSriastradh#if !NO_COPY_BOXES
377903b705cfSriastradh	sna->render.copy_boxes = gen6_render_copy_boxes;
378003b705cfSriastradh#endif
378103b705cfSriastradh#if !NO_COPY
378203b705cfSriastradh	sna->render.copy = gen6_render_copy;
378303b705cfSriastradh#endif
378403b705cfSriastradh
378503b705cfSriastradh#if !NO_FILL_BOXES
378603b705cfSriastradh	sna->render.fill_boxes = gen6_render_fill_boxes;
378703b705cfSriastradh#endif
378803b705cfSriastradh#if !NO_FILL
378903b705cfSriastradh	sna->render.fill = gen6_render_fill;
379003b705cfSriastradh#endif
379103b705cfSriastradh#if !NO_FILL_ONE
379203b705cfSriastradh	sna->render.fill_one = gen6_render_fill_one;
379303b705cfSriastradh#endif
379403b705cfSriastradh#if !NO_FILL_CLEAR
379503b705cfSriastradh	sna->render.clear = gen6_render_clear;
379603b705cfSriastradh#endif
379703b705cfSriastradh
379842542f5fSchristos	sna->render.flush = gen4_render_flush;
379903b705cfSriastradh	sna->render.reset = gen6_render_reset;
380003b705cfSriastradh	sna->render.fini = gen6_render_fini;
380103b705cfSriastradh
380203b705cfSriastradh	sna->render.max_3d_size = GEN6_MAX_SIZE;
380303b705cfSriastradh	sna->render.max_3d_pitch = 1 << 18;
380403b705cfSriastradh	return sna->render_state.gen6.info->name;
380503b705cfSriastradh}
3806