testdisplay.c 19.5 KB
Newer Older
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * Copyright 2010 Intel Corporation
 *   Jesse Barnes <jesse.barnes@intel.com>
 *
 * 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 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 is intended for testing of display functionality.  It should
 * allow for testing of
 *   - hotplug
 *   - mode setting
 *   - clone & twin modes
 *   - panel fitting
 *   - test patterns & pixel generators
 * Additional programs can test the detected outputs against VBT provided
 * device lists (both docked & undocked).
 *
 * TODO:
 * - pixel generator in transcoder
 * - test pattern reg in pipe
 * - test patterns on outputs (e.g. TV)
 * - handle hotplug (leaks crtcs, can't handle clones)
 * - allow mode force
 * - expose output specific controls
 *  - e.g. DDC-CI brightness
 *  - HDMI controls
 *  - panel brightness
 *  - DP commands (e.g. poweroff)
 * - verify outputs against VBT/physical connectors
 */
48
#ifdef HAVE_CONFIG_H
49
#include "config.h"
50
#endif
51

52
#include "igt.h"
53 54
#include <cairo.h>
#include <errno.h>
55
#include <getopt.h>
56 57
#include <math.h>
#include <stdint.h>
58
#include <stdbool.h>
59
#include <strings.h>
60
#include <unistd.h>
61
#include <termios.h>
62 63
#include <sys/poll.h>
#include <sys/time.h>
Chris Wilson's avatar
Chris Wilson committed
64
#include <sys/ioctl.h>
65 66
#include <sys/types.h>
#include <sys/stat.h>
67

68
#include "testdisplay.h"
69

70 71 72
#include <stdlib.h>
#include <signal.h>

73
#define SUBTEST_OPTS	 1
74
#define HELP_DESCRIPTION 2
75 76
#define Yb_OPT		 3
#define Yf_OPT		 4
77

78
static int tio_fd;
79 80
struct termios saved_tio;

81
drmModeRes *resources;
82
int drm_fd, modes;
83
int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
84
    test_stereo_modes;
85
uint64_t tiling = LOCAL_DRM_FORMAT_MOD_NONE;
86
int sleep_between_modes = 5;
87
int do_dpms = 0; /* This aliases to DPMS_ON */
88
uint32_t depth = 24, stride, bpp;
89
int qr_code = 0;
90
int specified_mode_num = -1, specified_disp_id = -1;
91

92
drmModeModeInfo force_timing;
93

94
int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
95 96 97 98
unsigned int plane_fb_id;
unsigned int plane_crtc_id;
unsigned int plane_id;
int plane_width, plane_height;
99
static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
100

101 102 103 104 105 106 107 108 109 110 111 112 113 114
/*
 * Mode setting with the kernel interfaces is a bit of a chore.
 * First you have to find the connector in question and make sure the
 * requested mode is available.
 * Then you need to find the encoder attached to that connector so you
 * can bind it with a free crtc.
 */
struct connector {
	uint32_t id;
	int mode_valid;
	drmModeModeInfo mode;
	drmModeEncoder *encoder;
	drmModeConnector *connector;
	int crtc;
115
	int crtc_idx;
116
	int pipe;
117 118
};

119
static void dump_connectors_fd(int drmfd)
120 121 122
{
	int i, j;

123 124 125
	drmModeRes *mode_resources = drmModeGetResources(drmfd);

	if (!mode_resources) {
Daniel Vetter's avatar
Daniel Vetter committed
126
		igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
127 128 129
		return;
	}

Daniel Vetter's avatar
Daniel Vetter committed
130 131
	igt_info("Connectors:\n");
	igt_info("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
132
	for (i = 0; i < mode_resources->count_connectors; i++) {
133 134
		drmModeConnector *connector;

135 136
		connector = drmModeGetConnectorCurrent(drmfd,
						       mode_resources->connectors[i]);
137
		if (!connector) {
Daniel Vetter's avatar
Daniel Vetter committed
138
			igt_warn("could not get connector %i: %s\n", mode_resources->connectors[i], strerror(errno));
139 140 141
			continue;
		}

Daniel Vetter's avatar
Daniel Vetter committed
142
		igt_info("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n", connector->connector_id, connector->encoder_id, kmstest_connector_status_str(connector->connection), kmstest_connector_type_str(connector->connector_type), connector->mmWidth, connector->mmHeight, connector->count_modes);
143 144 145 146

		if (!connector->count_modes)
			continue;

Daniel Vetter's avatar
Daniel Vetter committed
147 148
		igt_info("  modes:\n");
		igt_info("  name refresh (Hz) hdisp hss hse htot vdisp ""vss vse vtot flags type clock\n");
149
		for (j = 0; j < connector->count_modes; j++){
Daniel Vetter's avatar
Daniel Vetter committed
150
			igt_info("[%d]", j);
151
			kmstest_dump_mode(&connector->modes[j]);
152
		}
153 154 155

		drmModeFreeConnector(connector);
	}
Daniel Vetter's avatar
Daniel Vetter committed
156
	igt_info("\n");
157 158

	drmModeFreeResources(mode_resources);
159 160
}

161
static void dump_crtcs_fd(int drmfd)
162 163
{
	int i;
164
	drmModeRes *mode_resources = drmModeGetResources(drmfd);
165

Daniel Vetter's avatar
Daniel Vetter committed
166 167
	igt_info("CRTCs:\n");
	igt_info("id\tfb\tpos\tsize\n");
168
	for (i = 0; i < mode_resources->count_crtcs; i++) {
169 170
		drmModeCrtc *crtc;

171
		crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
172
		if (!crtc) {
Daniel Vetter's avatar
Daniel Vetter committed
173
			igt_warn("could not get crtc %i: %s\n", mode_resources->crtcs[i], strerror(errno));
174 175
			continue;
		}
Daniel Vetter's avatar
Daniel Vetter committed
176
		igt_info("%d\t%d\t(%d,%d)\t(%dx%d)\n", crtc->crtc_id, crtc->buffer_id, crtc->x, crtc->y, crtc->width, crtc->height);
177
		kmstest_dump_mode(&crtc->mode);
178 179 180

		drmModeFreeCrtc(crtc);
	}
Daniel Vetter's avatar
Daniel Vetter committed
181
	igt_info("\n");
182 183

	drmModeFreeResources(mode_resources);
184 185
}

186 187 188 189 190 191
static void dump_info(void)
{
	dump_connectors_fd(drm_fd);
	dump_crtcs_fd(drm_fd);
}

192 193
static void connector_find_preferred_mode(uint32_t connector_id,
					  unsigned long crtc_idx_mask,
194 195
					  int mode_num, struct connector *c,
					  bool probe)
196
{
197
	struct kmstest_connector_config config;
198
	bool ret;
199

200 201 202 203 204 205 206
	if (probe)
		ret = kmstest_probe_connector_config(drm_fd, connector_id,
						     crtc_idx_mask, &config);
	else
		ret = kmstest_get_connector_config(drm_fd, connector_id,
						   crtc_idx_mask, &config);
	if (!ret) {
207 208 209 210
		c->mode_valid = 0;
		return;
	}

211 212 213 214 215 216 217
	c->connector = config.connector;
	c->encoder = config.encoder;
	c->crtc = config.crtc->crtc_id;
	c->crtc_idx = config.crtc_idx;
	c->pipe = config.pipe;

	if (mode_num != -1) {
Daniel Vetter's avatar
Daniel Vetter committed
218
		igt_assert(mode_num < config.connector->count_modes);
219 220 221
		c->mode = config.connector->modes[mode_num];
	} else {
		c->mode = config.default_mode;
222
	}
223
	c->mode_valid = 1;
224 225
}

226
static void
227
paint_color_key(struct igt_fb *fb_info)
228 229
{
	int i, j;
230 231
	uint32_t *fb_ptr;

232 233
	fb_ptr = gem_mmap__gtt(drm_fd, fb_info->gem_handle, fb_info->size,
			       PROT_READ | PROT_WRITE);
234 235 236 237 238

	for (i = crtc_y; i < crtc_y + crtc_h; i++)
		for (j = crtc_x; j < crtc_x + crtc_w; j++) {
			uint32_t offset;

239
			offset = (i * fb_info->stride / 4) + j;
240 241
			fb_ptr[offset] = SPRITE_COLOR_KEY;
		}
242 243

	munmap(fb_ptr, fb_info->size);
244 245
}

246 247
static void paint_image(cairo_t *cr, const char *file)
{
248
	int img_x, img_y, img_w, img_h;
249 250 251 252 253 254 255

	img_y = height * (0.10 );
	img_h = height * 0.08 * 4;
	img_w = img_h;

	img_x = (width / 2) - (img_w / 2);

256
	igt_paint_image(cr, file, img_x, img_y, img_h, img_w);
257 258
}

259
static void paint_output_info(struct connector *c, struct igt_fb *fb)
260
{
261
	cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
Imre Deak's avatar
Imre Deak committed
262 263
	int l_width = fb->width;
	int l_height = fb->height;
264 265 266 267
	double str_width;
	double x, y, top_y;
	double max_width;
	int i;
268

269
	cairo_move_to(cr, l_width / 2, l_height / 2);
270

271
	/* Print connector and mode name */
272
	cairo_set_font_size(cr, 48);
273
	igt_cairo_printf_line(cr, align_hcenter, 10, "%s",
274 275
		 kmstest_connector_type_str(c->connector->connector_type));

276
	cairo_set_font_size(cr, 36);
277
	str_width = igt_cairo_printf_line(cr, align_hcenter, 10,
278 279 280 281
		"%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
		kmstest_encoder_type_str(c->encoder->encoder_type));

	cairo_rel_move_to(cr, -str_width / 2, 0);
282 283 284

	/* List available modes */
	cairo_set_font_size(cr, 18);
285
	str_width = igt_cairo_printf_line(cr, align_left, 10,
286 287 288
					      "Available modes:");
	cairo_rel_move_to(cr, str_width, 0);
	cairo_get_current_point(cr, &x, &top_y);
289

290
	max_width = 0;
291
	for (i = 0; i < c->connector->count_modes; i++) {
292 293 294 295 296
		cairo_get_current_point(cr, &x, &y);
		if (y >= l_height) {
			x += max_width + 10;
			max_width = 0;
			cairo_move_to(cr, x, top_y);
297
		}
298
		str_width = igt_cairo_printf_line(cr, align_right, 10,
299 300 301 302
			"%s @ %dHz", c->connector->modes[i].name,
			 c->connector->modes[i].vrefresh);
		if (str_width > max_width)
			max_width = str_width;
303
	}
304 305

	if (qr_code)
306
		paint_image(cr, "pass.png");
Imre Deak's avatar
Imre Deak committed
307

Daniel Vetter's avatar
Daniel Vetter committed
308
	igt_assert(!cairo_status(cr));
309 310

	cairo_destroy(cr);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
}

static void sighandler(int signo)
{
	return;
}

static void set_single(void)
{
	int sigs[] = { SIGUSR1 };
	struct sigaction sa;
	sa.sa_handler = sighandler;

	sigemptyset(&sa.sa_mask);

Daniel Vetter's avatar
Daniel Vetter committed
326 327
	igt_warn_on_f(sigaction(sigs[0], &sa, NULL) == -1,
		      "Could not set signal handler");
328 329 330 331 332
}

static void
set_mode(struct connector *c)
{
Daniel Vetter's avatar
Daniel Vetter committed
333
	unsigned int fb_id = 0;
334
	struct igt_fb fb_info[2] = { };
335
	int j, test_mode_num, current_fb = 0, old_fb = -1;
336

Chris Wilson's avatar
Chris Wilson committed
337 338
	test_mode_num = 1;
	if (force_mode){
339 340
		memcpy( &c->mode, &force_timing, sizeof(force_timing));
		c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
Chris Wilson's avatar
Chris Wilson committed
341
		c->mode_valid = 1;
342
		sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
Chris Wilson's avatar
Chris Wilson committed
343
	} else if (test_all_modes)
344 345 346
		test_mode_num = c->connector->count_modes;

	for (j = 0; j < test_mode_num; j++) {
Chris Wilson's avatar
Chris Wilson committed
347

348 349
		if (test_all_modes)
			c->mode = c->connector->modes[j];
350

351
		/* set_mode() only tests 2D modes */
352 353
		if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
			continue;
354

Chris Wilson's avatar
Chris Wilson committed
355 356
		if (!c->mode_valid)
			continue;
357

Chris Wilson's avatar
Chris Wilson committed
358 359 360
		width = c->mode.hdisplay;
		height = c->mode.vdisplay;

361 362 363
		fb_id = igt_create_pattern_fb(drm_fd, width, height,
					      igt_bpp_depth_to_drm_format(bpp, depth),
					      tiling, &fb_info[current_fb]);
364 365
		paint_output_info(c, &fb_info[current_fb]);
		paint_color_key(&fb_info[current_fb]);
366

Daniel Vetter's avatar
Daniel Vetter committed
367
		igt_info("CRTC(%u):[%d]", c->crtc, j);
368
		kmstest_dump_mode(&c->mode);
369
		if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
Chris Wilson's avatar
Chris Wilson committed
370
				   &c->id, 1, &c->mode)) {
Daniel Vetter's avatar
Daniel Vetter committed
371
			igt_warn("failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
372
			igt_remove_fb(drm_fd, &fb_info[current_fb]);
Chris Wilson's avatar
Chris Wilson committed
373
			continue;
374
		}
375

376
		if (old_fb != -1)
377
			igt_remove_fb(drm_fd, &fb_info[old_fb]);
378 379 380
		old_fb = current_fb;
		current_fb = 1 - current_fb;

381
		if (sleep_between_modes && test_all_modes && !qr_code)
382
			sleep(sleep_between_modes);
383

384
		if (do_dpms) {
385
			kmstest_set_connector_dpms(drm_fd, c->connector, do_dpms);
386
			sleep(sleep_between_modes);
387
			kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
388 389
		}

390 391 392 393
		if (qr_code){
			set_single();
			pause();
		}
394 395
	}

396
	if (test_all_modes && old_fb != -1)
397
		igt_remove_fb(drm_fd, &fb_info[old_fb]);
Chris Wilson's avatar
Chris Wilson committed
398

399 400 401 402
	drmModeFreeEncoder(c->encoder);
	drmModeFreeConnector(c->connector);
}

403 404 405 406
static void do_set_stereo_mode(struct connector *c)
{
	uint32_t fb_id;

407 408 409
	fb_id = igt_create_stereo_fb(drm_fd, &c->mode,
				     igt_bpp_depth_to_drm_format(bpp, depth),
				     tiling);
410

Daniel Vetter's avatar
Daniel Vetter committed
411 412
	igt_warn_on_f(drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0, &c->id, 1, &c->mode),
		      "failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
413 414 415 416 417
}

static void
set_stereo_mode(struct connector *c)
{
418
	int i, n;
419

420 421 422 423 424 425 426 427 428

	if (specified_mode_num != -1)
		n = 1;
	else
		n = c->connector->count_modes;

	for (i = 0; i < n; i++) {
		if (specified_mode_num == -1)
			c->mode = c->connector->modes[i];
429 430 431 432

		if (!c->mode_valid)
			continue;

433
		if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
434 435
			continue;

Daniel Vetter's avatar
Daniel Vetter committed
436
		igt_info("CRTC(%u): [%d]", c->crtc, i);
437
		kmstest_dump_mode(&c->mode);
438 439 440 441 442 443 444
		do_set_stereo_mode(c);

		if (qr_code) {
			set_single();
			pause();
		} else if (sleep_between_modes)
			sleep(sleep_between_modes);
445 446

		if (do_dpms) {
447
			kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_OFF);
448
			sleep(sleep_between_modes);
449
			kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
450
		}
451 452 453 454 455 456
	}

	drmModeFreeEncoder(c->encoder);
	drmModeFreeConnector(c->connector);
}

457 458 459 460 461 462 463 464 465
/*
 * Re-probe outputs and light up as many as possible.
 *
 * On Intel, we have two CRTCs that we can drive independently with
 * different timings and scanout buffers.
 *
 * Each connector has a corresponding encoder, except in the SDVO case
 * where an encoder may have multiple connectors.
 */
466
int update_display(bool probe)
467 468 469 470
{
	struct connector *connectors;
	int c;

471
	resources = drmModeGetResources(drm_fd);
472
	if (!resources) {
Daniel Vetter's avatar
Daniel Vetter committed
473
		igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
474
		return 0;
475 476 477 478 479
	}

	connectors = calloc(resources->count_connectors,
			    sizeof(struct connector));
	if (!connectors)
480
		return 0;
481

482 483
	if (test_preferred_mode || test_all_modes ||
	    force_mode || specified_disp_id != -1) {
484 485
		unsigned long crtc_idx_mask = -1UL;

486 487
		/* Find any connected displays */
		for (c = 0; c < resources->count_connectors; c++) {
488 489 490 491 492 493 494 495 496 497
			struct connector *connector = &connectors[c];

			connector->id = resources->connectors[c];
			if (specified_disp_id != -1 &&
			    connector->id != specified_disp_id)
				continue;

			connector_find_preferred_mode(connector->id,
						      crtc_idx_mask,
						      specified_mode_num,
498
						      connector, probe);
499
			if (!connector->mode_valid)
500 501
				continue;

502 503 504 505 506 507
			set_mode(connector);

			if (test_preferred_mode || force_mode ||
			    specified_mode_num != -1)
				crtc_idx_mask &= ~(1 << connector->crtc_idx);

508
		}
509
	}
510

511 512 513 514 515
	if (test_stereo_modes) {
		for (c = 0; c < resources->count_connectors; c++) {
			struct connector *connector = &connectors[c];

			connector->id = resources->connectors[c];
516 517 518
			if (specified_disp_id != -1 &&
			    connector->id != specified_disp_id)
				continue;
519 520 521 522

			connector_find_preferred_mode(connector->id,
						      -1UL,
						      specified_mode_num,
523
						      connector, probe);
524 525 526 527 528 529 530
			if (!connector->mode_valid)
				continue;

			set_stereo_mode(connector);
		}
	}

531
	free(connectors);
532
	drmModeFreeResources(resources);
533
	return 1;
534 535
}

536
static char optstr[] = "3hiaf:s:d:p:mrto:j:y";
537

538
static void __attribute__((noreturn)) usage(char *name, char opt)
539
{
Daniel Vetter's avatar
Daniel Vetter committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	igt_info("usage: %s [-hiasdpmtf]\n", name);
	igt_info("\t-i\tdump info\n");
	igt_info("\t-a\ttest all modes\n");
	igt_info("\t-s\t<duration>\tsleep between each mode test\n");
	igt_info("\t-d\t<depth>\tbit depth of scanout buffer\n");
	igt_info("\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
	igt_info("\t-m\ttest the preferred mode\n");
	igt_info("\t-3\ttest all 3D modes\n");
	igt_info("\t-t\tuse a tiled framebuffer\n");
	igt_info("\t-j\tdo dpms off, optional arg to select dpms leve (1-3)\n");
	igt_info("\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
	igt_info("\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
	igt_info("\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
	igt_info("\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
	igt_info("\t\ttest force mode\n");
	igt_info("\tDefault is to test all modes.\n");
556
	exit((opt != 'h') ? -1 : 0);
557 558
}

559 560
#define dump_resource(res) if (res) dump_##res()

561
static void __attribute__((noreturn)) cleanup_and_exit(int ret)
562 563 564 565 566
{
	close(drm_fd);
	exit(ret);
}

567
static gboolean input_event(GIOChannel *source, GIOCondition condition,
568
				gpointer data)
569
{
570
	gchar buf[2];
571 572
	gsize count;

573
	count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
574
	if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
575
		cleanup_and_exit(0);
576
	}
577 578 579 580

	return TRUE;
}

581 582 583 584 585
static void enter_exec_path( char **argv )
{
	char *exec_path = NULL;
	char *pos = NULL;
	short len_path = 0;
586
	int ret;
587 588 589 590 591 592 593 594 595

	len_path = strlen( argv[0] );
	exec_path = (char*) malloc(len_path);

	memcpy(exec_path, argv[0], len_path);
	pos = strrchr(exec_path, '/');
	if (pos != NULL)
		*(pos+1) = '\0';

596
	ret = chdir(exec_path);
597
	igt_assert_eq(ret, 0);
598 599 600
	free(exec_path);
}

601 602 603 604 605 606
static void restore_termio_mode(int sig)
{
	tcsetattr(tio_fd, TCSANOW, &saved_tio);
	close(tio_fd);
}

607 608 609 610
static void set_termio_mode(void)
{
	struct termios tio;

611 612 613 614 615
	/* don't attempt to set terminal attributes if not in the foreground
	 * process group */
	if (getpgrp() != tcgetpgrp(STDOUT_FILENO))
		return;

616 617 618
	tio_fd = dup(STDIN_FILENO);
	tcgetattr(tio_fd, &saved_tio);
	igt_install_exit_handler(restore_termio_mode);
619 620
	tio = saved_tio;
	tio.c_lflag &= ~(ICANON | ECHO);
621
	tcsetattr(tio_fd, TCSANOW, &tio);
622 623
}

624 625 626 627
int main(int argc, char **argv)
{
	int c;
	int ret = 0;
628
	GIOChannel *stdinchannel;
629
	GMainLoop *mainloop;
630
	float force_clock;
631
	bool opt_dump_info = false;
632 633 634
	struct option long_opts[] = {
		{"list-subtests", 0, 0, SUBTEST_OPTS},
		{"run-subtest", 1, 0, SUBTEST_OPTS},
635
		{"help-description", 0, 0, HELP_DESCRIPTION},
636
		{"help", 0, 0, 'h'},
637 638
		{"yb", 0, 0, Yb_OPT},
		{"yf", 0, 0, Yf_OPT},
639 640
		{ 0, 0, 0, 0 }
	};
641

Daniel Vetter's avatar
Daniel Vetter committed
642
	igt_skip_on_simulation();
643

644 645
	enter_exec_path( argv );

646
	while ((c = getopt_long(argc, argv, optstr, long_opts, NULL)) != -1) {
647
		switch (c) {
648 649 650
		case '3':
			test_stereo_modes = 1;
			break;
651
		case 'i':
652
			opt_dump_info = true;
653 654 655 656 657 658
			break;
		case 'a':
			test_all_modes = 1;
			break;
		case 'f':
			force_mode = 1;
659 660 661
			if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
				&force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
				&force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
662
				usage(argv[0], c);
663 664
			force_timing.clock = force_clock*1000;

665 666 667 668
			break;
		case 's':
			sleep_between_modes = atoi(optarg);
			break;
669
		case 'j':
670 671 672
			do_dpms = atoi(optarg);
			if (do_dpms == 0)
				do_dpms = DRM_MODE_DPMS_OFF;
673
			break;
674 675
		case 'd':
			depth = atoi(optarg);
Daniel Vetter's avatar
Daniel Vetter committed
676
			igt_info("using depth %d\n", depth);
677
			break;
678
		case 'p':
679 680 681
			if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
				   &plane_height, &crtc_x, &crtc_y,
				   &crtc_w, &crtc_h) != 6)
682
				usage(argv[0], c);
683 684
			test_plane = 1;
			break;
685 686 687
		case 'm':
			test_preferred_mode = 1;
			break;
688
		case 't':
689
			tiling = LOCAL_I915_FORMAT_MOD_X_TILED;
690
			break;
691 692 693 694 695 696 697
		case 'y':
		case Yb_OPT:
			tiling = LOCAL_I915_FORMAT_MOD_Y_TILED;
			break;
		case Yf_OPT:
			tiling = LOCAL_I915_FORMAT_MOD_Yf_TILED;
			break;
698 699 700
		case 'r':
			qr_code = 1;
			break;
701
		case 'o':
702
			sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
703
			break;
704 705 706 707
		case SUBTEST_OPTS:
			/* invalid subtest options */
			exit(IGT_EXIT_INVALID);
			break;
708
		case HELP_DESCRIPTION:
709
			igt_info("Tests display functionality.");
710 711
			exit(0);
			break;
712 713 714
		default:
			/* fall through */
		case 'h':
715
			usage(argv[0], c);
716 717 718
			break;
		}
	}
719

720 721
	set_termio_mode();

722 723 724 725 726 727 728
	if (depth <= 8)
		bpp = 8;
	else if (depth <= 16)
		bpp = 16;
	else if (depth <= 32)
		bpp = 32;

729
	if (!test_all_modes && !force_mode && !test_preferred_mode &&
730
	    specified_mode_num == -1 && !test_stereo_modes)
731
		test_all_modes = 1;
732

733
	drm_fd = drm_open_driver(DRIVER_INTEL);
734

735 736
	if (test_stereo_modes &&
	    drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
Daniel Vetter's avatar
Daniel Vetter committed
737
		igt_warn("DRM_CLIENT_CAP_STEREO_3D failed\n");
738 739 740
		goto out_close;
	}

741 742 743 744 745
	if (opt_dump_info) {
		dump_info();
		goto out_close;
	}

746
	kmstest_set_vt_graphics_mode();
747

748 749
	mainloop = g_main_loop_new(NULL, FALSE);
	if (!mainloop) {
Daniel Vetter's avatar
Daniel Vetter committed
750
		igt_warn("failed to create glib mainloop\n");
751
		ret = -1;
752
		goto out_close;
753 754
	}

755
	if (!testdisplay_setup_hotplug()) {
Daniel Vetter's avatar
Daniel Vetter committed
756
		igt_warn("failed to initialize hotplug support\n");
757
		goto out_mainloop;
758 759 760 761
	}

	stdinchannel = g_io_channel_unix_new(0);
	if (!stdinchannel) {
Daniel Vetter's avatar
Daniel Vetter committed
762
		igt_warn("failed to create stdin GIO channel\n");
763
		goto out_hotplug;
764 765 766 767 768
	}

	ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
			     NULL);
	if (ret < 0) {
Daniel Vetter's avatar
Daniel Vetter committed
769
		igt_warn("failed to add watch on stdin GIO channel\n");
770
		goto out_stdio;
771 772
	}

773 774
	ret = 0;

775
	if (!update_display(false)) {
776
		ret = 1;
777
		goto out_stdio;
778 779
	}

780
	if (test_all_modes)
781
		goto out_stdio;
782

783 784
	g_main_loop_run(mainloop);

785
out_stdio:
786
	g_io_channel_shutdown(stdinchannel, TRUE, NULL);
787 788 789
out_hotplug:
	testdisplay_cleanup_hotplug();
out_mainloop:
790 791
	g_main_loop_unref(mainloop);
out_close:
Daniel Vetter's avatar
Daniel Vetter committed
792 793
	close(drm_fd);

794
	igt_assert_eq(ret, 0);
795 796

	igt_exit();
797
}