kms_draw_crc.c 8.62 KB
Newer Older
Paulo Zanoni's avatar
Paulo Zanoni committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright © 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

/* This program tests whether the igt_draw library actually works. */

27
#include "igt.h"
Paulo Zanoni's avatar
Paulo Zanoni committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

#define MAX_CONNECTORS 32

struct modeset_params {
	uint32_t crtc_id;
	uint32_t connector_id;
	drmModeModeInfoPtr mode;
};

int drm_fd;
drmModeResPtr drm_res;
drmModeConnectorPtr drm_connectors[MAX_CONNECTORS];
drm_intel_bufmgr *bufmgr;
igt_pipe_crc_t *pipe_crc;

43 44 45 46 47 48 49
#define N_FORMATS 3
static const uint32_t formats[N_FORMATS] = {
	DRM_FORMAT_XRGB8888,
	DRM_FORMAT_RGB565,
	DRM_FORMAT_XRGB2101010,
};

50 51 52 53 54 55 56
#define N_TILING_METHODS 3
static const uint64_t tilings[N_TILING_METHODS] = {
	LOCAL_DRM_FORMAT_MOD_NONE,
	LOCAL_I915_FORMAT_MOD_X_TILED,
	LOCAL_I915_FORMAT_MOD_Y_TILED,
};

57 58 59 60 61
struct base_crc {
	bool set;
	igt_crc_t crc;
};
struct base_crc base_crcs[N_FORMATS];
Paulo Zanoni's avatar
Paulo Zanoni committed
62 63 64 65 66 67

struct modeset_params ms;

static void find_modeset_params(void)
{
	int i;
68 69
	uint32_t crtc_id;
	drmModeConnectorPtr connector = NULL;
Paulo Zanoni's avatar
Paulo Zanoni committed
70 71 72 73 74 75
	drmModeModeInfoPtr mode = NULL;

	for (i = 0; i < drm_res->count_connectors; i++) {
		drmModeConnectorPtr c = drm_connectors[i];

		if (c->count_modes) {
76
			connector = c;
Paulo Zanoni's avatar
Paulo Zanoni committed
77 78 79 80
			mode = &c->modes[0];
			break;
		}
	}
81
	igt_require(connector);
Paulo Zanoni's avatar
Paulo Zanoni committed
82

83 84
	crtc_id = kmstest_find_crtc_for_connector(drm_fd, drm_res, connector,
						  0);
Paulo Zanoni's avatar
Paulo Zanoni committed
85 86
	igt_assert(mode);

87
	ms.connector_id = connector->connector_id;
Paulo Zanoni's avatar
Paulo Zanoni committed
88 89 90 91 92
	ms.crtc_id = crtc_id;
	ms.mode = mode;

}

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static uint32_t get_color(uint32_t drm_format, bool r, bool g, bool b)
{
	uint32_t color = 0;

	switch (drm_format) {
	case DRM_FORMAT_RGB565:
		color |= r ? 0x1F << 11 : 0;
		color |= g ? 0x3F << 5 : 0;
		color |= b ? 0x1F : 0;
		break;
	case DRM_FORMAT_XRGB8888:
		color |= r ? 0xFF << 16 : 0;
		color |= g ? 0xFF << 8 : 0;
		color |= b ? 0xFF : 0;
		break;
	case DRM_FORMAT_XRGB2101010:
		color |= r ? 0x3FF << 20 : 0;
		color |= g ? 0x3FF << 10 : 0;
		color |= b ? 0x3FF : 0;
		break;
	default:
		igt_assert(false);
	}

	return color;
}

static void get_method_crc(enum igt_draw_method method, uint32_t drm_format,
			   uint64_t tiling, igt_crc_t *crc)
Paulo Zanoni's avatar
Paulo Zanoni committed
122 123 124 125 126
{
	struct igt_fb fb;
	int rc;

	igt_create_fb(drm_fd, ms.mode->hdisplay, ms.mode->vdisplay,
127
		      drm_format, tiling, &fb);
Paulo Zanoni's avatar
Paulo Zanoni committed
128
	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
129 130
			 0, 0, fb.width, fb.height,
			 get_color(drm_format, 0, 0, 1));
Paulo Zanoni's avatar
Paulo Zanoni committed
131 132 133

	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
			 fb.width / 4, fb.height / 4,
134 135
			 fb.width / 2, fb.height / 2,
			 get_color(drm_format, 0, 1, 0));
Paulo Zanoni's avatar
Paulo Zanoni committed
136 137
	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
			 fb.width / 8, fb.height / 8,
138 139
			 fb.width / 4, fb.height / 4,
			 get_color(drm_format, 1, 0, 0));
Paulo Zanoni's avatar
Paulo Zanoni committed
140 141
	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method,
			 fb.width / 2, fb.height / 2,
142 143 144 145
			 fb.width / 3, fb.height / 3,
			 get_color(drm_format, 1, 0, 1));
	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, method, 1, 1, 15, 15,
			 get_color(drm_format, 0, 1, 1));
Paulo Zanoni's avatar
Paulo Zanoni committed
146 147 148

	rc = drmModeSetCrtc(drm_fd, ms.crtc_id, fb.fb_id, 0, 0,
			    &ms.connector_id, 1, ms.mode);
Daniel Stone's avatar
Daniel Stone committed
149
	igt_assert_eq(rc, 0);
Paulo Zanoni's avatar
Paulo Zanoni committed
150 151 152 153 154 155

	igt_pipe_crc_collect_crc(pipe_crc, crc);

	igt_remove_fb(drm_fd, &fb);
}

156 157 158
static bool format_is_supported(uint32_t format, uint64_t modifier)
{
	uint32_t gem_handle, fb_id;
159
	unsigned int offsets[4] = {};
160
	unsigned int strides[4] = {};
161 162 163 164
	int ret;

	gem_handle = igt_create_bo_with_dimensions(drm_fd, 64, 64,
						   format, modifier,
165
						   0, NULL, &strides[0], NULL);
166
	ret =  __kms_addfb(drm_fd, gem_handle, 64, 64,
167
			   format, modifier, strides, offsets, 1,
168 169 170 171 172 173 174
			   LOCAL_DRM_MODE_FB_MODIFIERS, &fb_id);
	drmModeRmFB(drm_fd, fb_id);
	gem_close(drm_fd, gem_handle);

	return ret == 0;
}

175 176
static void draw_method_subtest(enum igt_draw_method method,
				uint32_t format_index, uint64_t tiling)
Paulo Zanoni's avatar
Paulo Zanoni committed
177 178 179
{
	igt_crc_t crc;

180 181
	igt_skip_on(method == IGT_DRAW_MMAP_WC && !gem_mmap__has_wc(drm_fd));

182 183
	igt_require(format_is_supported(formats[format_index], tiling));

Paulo Zanoni's avatar
Paulo Zanoni committed
184 185 186
	/* Use IGT_DRAW_MMAP_GTT on an untiled buffer as the parameter for
	 * comparison. Cache the value so we don't recompute it for every single
	 * subtest. */
187 188 189 190 191
	if (!base_crcs[format_index].set) {
		get_method_crc(IGT_DRAW_MMAP_GTT, formats[format_index],
			       LOCAL_DRM_FORMAT_MOD_NONE,
			       &base_crcs[format_index].crc);
		base_crcs[format_index].set = true;
Paulo Zanoni's avatar
Paulo Zanoni committed
192 193
	}

194 195
	get_method_crc(method, formats[format_index], tiling, &crc);
	igt_assert_crc_equal(&crc, &base_crcs[format_index].crc);
Paulo Zanoni's avatar
Paulo Zanoni committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209
}

static void get_fill_crc(uint64_t tiling, igt_crc_t *crc)
{
	struct igt_fb fb;
	int rc;

	igt_create_fb(drm_fd, ms.mode->hdisplay, ms.mode->vdisplay,
		      DRM_FORMAT_XRGB8888, tiling, &fb);

	igt_draw_fill_fb(drm_fd, &fb, 0xFF);

	rc = drmModeSetCrtc(drm_fd, ms.crtc_id, fb.fb_id, 0, 0,
			    &ms.connector_id, 1, ms.mode);
Daniel Stone's avatar
Daniel Stone committed
210
	igt_assert_eq(rc, 0);
Paulo Zanoni's avatar
Paulo Zanoni committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

	igt_pipe_crc_collect_crc(pipe_crc, crc);

	igt_remove_fb(drm_fd, &fb);
}

static void fill_fb_subtest(void)
{
	int rc;
	struct igt_fb fb;
	igt_crc_t base_crc, crc;

	igt_create_fb(drm_fd, ms.mode->hdisplay, ms.mode->vdisplay,
		      DRM_FORMAT_XRGB8888, LOCAL_DRM_FORMAT_MOD_NONE, &fb);

	igt_draw_rect_fb(drm_fd, bufmgr, NULL, &fb, IGT_DRAW_MMAP_GTT,
			 0, 0, fb.width, fb.height, 0xFF);

	rc = drmModeSetCrtc(drm_fd, ms.crtc_id, fb.fb_id, 0, 0,
			    &ms.connector_id, 1, ms.mode);
Daniel Stone's avatar
Daniel Stone committed
231
	igt_assert_eq(rc, 0);
Paulo Zanoni's avatar
Paulo Zanoni committed
232 233 234 235 236 237 238 239 240

	igt_pipe_crc_collect_crc(pipe_crc, &base_crc);

	get_fill_crc(LOCAL_DRM_FORMAT_MOD_NONE, &crc);
	igt_assert_crc_equal(&crc, &base_crc);

	get_fill_crc(LOCAL_I915_FORMAT_MOD_X_TILED, &crc);
	igt_assert_crc_equal(&crc, &base_crc);

241 242 243 244 245
	if (intel_gen(intel_get_drm_devid(drm_fd)) >= 9) {
		get_fill_crc(LOCAL_I915_FORMAT_MOD_Y_TILED, &crc);
		igt_assert_crc_equal(&crc, &base_crc);
	}

Paulo Zanoni's avatar
Paulo Zanoni committed
246 247 248 249 250 251 252
	igt_remove_fb(drm_fd, &fb);
}

static void setup_environment(void)
{
	int i;

253
	drm_fd = drm_open_driver_master(DRIVER_INTEL);
Paulo Zanoni's avatar
Paulo Zanoni committed
254 255 256
	igt_require(drm_fd >= 0);

	drm_res = drmModeGetResources(drm_fd);
257
	igt_require(drm_res);
Paulo Zanoni's avatar
Paulo Zanoni committed
258 259 260
	igt_assert(drm_res->count_connectors <= MAX_CONNECTORS);

	for (i = 0; i < drm_res->count_connectors; i++)
261 262
		drm_connectors[i] = drmModeGetConnectorCurrent(drm_fd,
							       drm_res->connectors[i]);
Paulo Zanoni's avatar
Paulo Zanoni committed
263 264 265 266 267 268 269

	kmstest_set_vt_graphics_mode();

	bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
	igt_assert(bufmgr);
	drm_intel_bufmgr_gem_enable_reuse(bufmgr);

270
	find_modeset_params();
271
	pipe_crc = igt_pipe_crc_new(drm_fd, kmstest_get_crtc_idx(drm_res, ms.crtc_id),
272
				    INTEL_PIPE_CRC_SOURCE_AUTO);
Paulo Zanoni's avatar
Paulo Zanoni committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

static void teardown_environment(void)
{
	int i;

	igt_pipe_crc_free(pipe_crc);

	drm_intel_bufmgr_destroy(bufmgr);

	for (i = 0; i < drm_res->count_connectors; i++)
		drmModeFreeConnector(drm_connectors[i]);

	drmModeFreeResources(drm_res);
	close(drm_fd);
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303
static const char *format_str(int format_index)
{
	switch (formats[format_index]) {
	case DRM_FORMAT_RGB565:
		return "rgb565";
	case DRM_FORMAT_XRGB8888:
		return "xrgb8888";
	case DRM_FORMAT_XRGB2101010:
		return "xrgb2101010";
	default:
		igt_assert(false);
	}
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317
static const char *tiling_str(int tiling_index)
{
	switch (tilings[tiling_index]) {
	case LOCAL_DRM_FORMAT_MOD_NONE:
		return "untiled";
	case LOCAL_I915_FORMAT_MOD_X_TILED:
		return "xtiled";
	case LOCAL_I915_FORMAT_MOD_Y_TILED:
		return "ytiled";
	default:
		igt_assert(false);
	}
}

Paulo Zanoni's avatar
Paulo Zanoni committed
318 319 320
igt_main
{
	enum igt_draw_method method;
321
	int format_idx, tiling_idx;
Paulo Zanoni's avatar
Paulo Zanoni committed
322 323 324 325

	igt_fixture
		setup_environment();

326 327 328 329 330 331 332 333 334 335
	for (format_idx = 0; format_idx < N_FORMATS; format_idx++) {
	for (method = 0; method < IGT_DRAW_METHOD_COUNT; method++) {
	for (tiling_idx = 0; tiling_idx < N_TILING_METHODS; tiling_idx++) {
		igt_subtest_f("draw-method-%s-%s-%s",
			      format_str(format_idx),
			      igt_draw_get_method_name(method),
			      tiling_str(tiling_idx))
			draw_method_subtest(method, format_idx,
					    tilings[tiling_idx]);
	} } }
Paulo Zanoni's avatar
Paulo Zanoni committed
336 337 338 339 340 341 342

	igt_subtest("fill-fb")
		fill_fb_subtest();

	igt_fixture
		teardown_environment();
}