2 * Copyright 2010 Intel Corporation
3 * Jesse Barnes <jesse.barnes@intel.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * This program is intended for testing of display functionality. It should
26 * allow for testing of
29 * - clone & twin modes
31 * - test patterns & pixel generators
32 * Additional programs can test the detected outputs against VBT provided
33 * device lists (both docked & undocked).
36 * - pixel generator in transcoder
37 * - test pattern reg in pipe
38 * - test patterns on outputs (e.g. TV)
39 * - handle hotplug (leaks crtcs, can't handle clones)
41 * - expose output specific controls
42 * - e.g. DDC-CI brightness
45 * - DP commands (e.g. poweroff)
46 * - verify outputs against VBT/physical connectors
62 #include <sys/ioctl.h>
63 #include <sys/types.h>
66 #include "ioctl_wrappers.h"
68 #include "testdisplay.h"
75 struct termios saved_tio;
77 drmModeRes *resources;
79 int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
80 test_stereo_modes, enable_tiling;
81 int sleep_between_modes = 5;
82 int do_dpms = 0; /* This aliases to DPMS_ON */
83 uint32_t depth = 24, stride, bpp;
85 int specified_mode_num = -1, specified_disp_id = -1;
87 drmModeModeInfo force_timing;
89 int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
90 unsigned int plane_fb_id;
91 unsigned int plane_crtc_id;
92 unsigned int plane_id;
93 int plane_width, plane_height;
94 static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
97 * Mode setting with the kernel interfaces is a bit of a chore.
98 * First you have to find the connector in question and make sure the
99 * requested mode is available.
100 * Then you need to find the encoder attached to that connector so you
101 * can bind it with a free crtc.
106 drmModeModeInfo mode;
107 drmModeEncoder *encoder;
108 drmModeConnector *connector;
114 static void dump_connectors_fd(int drmfd)
118 drmModeRes *mode_resources = drmModeGetResources(drmfd);
120 if (!mode_resources) {
121 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
125 igt_info("Connectors:\n");
126 igt_info("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
127 for (i = 0; i < mode_resources->count_connectors; i++) {
128 drmModeConnector *connector;
130 connector = drmModeGetConnector(drmfd, mode_resources->connectors[i]);
132 igt_warn("could not get connector %i: %s\n", mode_resources->connectors[i], strerror(errno));
136 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);
138 if (!connector->count_modes)
141 igt_info(" modes:\n");
142 igt_info(" name refresh (Hz) hdisp hss hse htot vdisp ""vss vse vtot flags type clock\n");
143 for (j = 0; j < connector->count_modes; j++){
145 kmstest_dump_mode(&connector->modes[j]);
148 drmModeFreeConnector(connector);
152 drmModeFreeResources(mode_resources);
155 static void dump_crtcs_fd(int drmfd)
158 drmModeRes *mode_resources = drmModeGetResources(drmfd);
160 igt_info("CRTCs:\n");
161 igt_info("id\tfb\tpos\tsize\n");
162 for (i = 0; i < mode_resources->count_crtcs; i++) {
165 crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
167 igt_warn("could not get crtc %i: %s\n", mode_resources->crtcs[i], strerror(errno));
170 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);
171 kmstest_dump_mode(&crtc->mode);
173 drmModeFreeCrtc(crtc);
177 drmModeFreeResources(mode_resources);
180 static void dump_info(void)
182 dump_connectors_fd(drm_fd);
183 dump_crtcs_fd(drm_fd);
186 static void connector_find_preferred_mode(uint32_t connector_id,
187 unsigned long crtc_idx_mask,
188 int mode_num, struct connector *c)
190 struct kmstest_connector_config config;
192 if (kmstest_get_connector_config(drm_fd, connector_id, crtc_idx_mask,
198 c->connector = config.connector;
199 c->encoder = config.encoder;
200 c->crtc = config.crtc->crtc_id;
201 c->crtc_idx = config.crtc_idx;
202 c->pipe = config.pipe;
204 if (mode_num != -1) {
205 igt_assert(mode_num < config.connector->count_modes);
206 c->mode = config.connector->modes[mode_num];
208 c->mode = config.default_mode;
214 paint_color_key(struct igt_fb *fb_info)
219 fb_ptr = gem_mmap(drm_fd, fb_info->gem_handle,
220 fb_info->size, PROT_READ | PROT_WRITE);
223 for (i = crtc_y; i < crtc_y + crtc_h; i++)
224 for (j = crtc_x; j < crtc_x + crtc_w; j++) {
227 offset = (i * fb_info->stride / 4) + j;
228 fb_ptr[offset] = SPRITE_COLOR_KEY;
231 munmap(fb_ptr, fb_info->size);
234 static void paint_image(cairo_t *cr, const char *file)
236 int img_x, img_y, img_w, img_h, img_w_o, img_h_o;
237 double img_w_scale, img_h_scale;
239 cairo_surface_t *image;
241 img_y = height * (0.10 );
242 img_h = height * 0.08 * 4;
245 img_x = (width / 2) - (img_w / 2);
247 image = cairo_image_surface_create_from_png(file);
249 img_w_o = cairo_image_surface_get_width(image);
250 img_h_o = cairo_image_surface_get_height(image);
252 cairo_translate(cr, img_x, img_y);
254 img_w_scale = (double)img_w / (double)img_w_o;
255 img_h_scale = (double)img_h / (double)img_h_o;
256 cairo_scale(cr, img_w_scale, img_h_scale);
258 cairo_set_source_surface(cr, image, 0, 0);
259 cairo_scale(cr, 1, 1);
262 cairo_surface_destroy(image);
265 static void paint_output_info(struct connector *c, struct igt_fb *fb)
267 cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
268 int l_width = fb->width;
269 int l_height = fb->height;
275 igt_paint_test_pattern(cr, l_width, l_height);
277 cairo_move_to(cr, l_width / 2, l_height / 2);
279 /* Print connector and mode name */
280 cairo_set_font_size(cr, 48);
281 igt_cairo_printf_line(cr, align_hcenter, 10, "%s",
282 kmstest_connector_type_str(c->connector->connector_type));
284 cairo_set_font_size(cr, 36);
285 str_width = igt_cairo_printf_line(cr, align_hcenter, 10,
286 "%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
287 kmstest_encoder_type_str(c->encoder->encoder_type));
289 cairo_rel_move_to(cr, -str_width / 2, 0);
291 /* List available modes */
292 cairo_set_font_size(cr, 18);
293 str_width = igt_cairo_printf_line(cr, align_left, 10,
295 cairo_rel_move_to(cr, str_width, 0);
296 cairo_get_current_point(cr, &x, &top_y);
299 for (i = 0; i < c->connector->count_modes; i++) {
300 cairo_get_current_point(cr, &x, &y);
304 cairo_move_to(cr, x, top_y);
306 str_width = igt_cairo_printf_line(cr, align_right, 10,
307 "%s @ %dHz", c->connector->modes[i].name,
308 c->connector->modes[i].vrefresh);
309 if (str_width > max_width)
310 max_width = str_width;
314 paint_image(cr, IGT_DATADIR"/pass.png");
316 igt_assert(!cairo_status(cr));
319 static void sighandler(int signo)
324 static void set_single(void)
326 int sigs[] = { SIGUSR1 };
328 sa.sa_handler = sighandler;
330 sigemptyset(&sa.sa_mask);
332 igt_warn_on_f(sigaction(sigs[0], &sa, NULL) == -1,
333 "Could not set signal handler");
337 set_mode(struct connector *c)
339 unsigned int fb_id = 0;
340 struct igt_fb fb_info[2] = { };
341 int j, test_mode_num, current_fb = 0, old_fb = -1;
345 memcpy( &c->mode, &force_timing, sizeof(force_timing));
346 c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
348 sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
349 } else if (test_all_modes)
350 test_mode_num = c->connector->count_modes;
352 for (j = 0; j < test_mode_num; j++) {
355 c->mode = c->connector->modes[j];
357 /* set_mode() only tests 2D modes */
358 if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
364 width = c->mode.hdisplay;
365 height = c->mode.vdisplay;
367 fb_id = igt_create_fb(drm_fd, width, height,
368 igt_bpp_depth_to_drm_format(bpp, depth),
369 enable_tiling, &fb_info[current_fb]);
370 paint_output_info(c, &fb_info[current_fb]);
371 paint_color_key(&fb_info[current_fb]);
373 igt_info("CRTC(%u):[%d]", c->crtc, j);
374 kmstest_dump_mode(&c->mode);
375 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
376 &c->id, 1, &c->mode)) {
377 igt_warn("failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
382 igt_remove_fb(drm_fd, &fb_info[old_fb]);
384 current_fb = 1 - current_fb;
386 if (sleep_between_modes && test_all_modes && !qr_code)
387 sleep(sleep_between_modes);
390 kmstest_set_connector_dpms(drm_fd, c->connector, do_dpms);
391 sleep(sleep_between_modes);
392 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
402 igt_remove_fb(drm_fd, &fb_info[old_fb]);
404 drmModeFreeEncoder(c->encoder);
405 drmModeFreeConnector(c->connector);
409 int x, y, width, height;
412 struct stereo_fb_layout {
413 int fb_width, fb_height;
414 struct box left, right;
417 static void box_init(struct box *box, int x, int y, int bwidth, int bheight)
422 box->height = bheight;
425 static void stereo_fb_layout_from_mode(struct stereo_fb_layout *layout,
426 drmModeModeInfo *mode)
428 unsigned int format = mode->flags & DRM_MODE_FLAG_3D_MASK;
429 const int hdisplay = mode->hdisplay, vdisplay = mode->vdisplay;
433 case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
434 layout->fb_width = hdisplay;
435 layout->fb_height = vdisplay;
437 middle = vdisplay / 2;
438 box_init(&layout->left, 0, 0, hdisplay, middle);
439 box_init(&layout->right,
440 0, middle, hdisplay, vdisplay - middle);
442 case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
443 layout->fb_width = hdisplay;
444 layout->fb_height = vdisplay;
446 middle = hdisplay / 2;
447 box_init(&layout->left, 0, 0, middle, vdisplay);
448 box_init(&layout->right,
449 middle, 0, hdisplay - middle, vdisplay);
451 case DRM_MODE_FLAG_3D_FRAME_PACKING:
453 int vactive_space = mode->vtotal - vdisplay;
455 layout->fb_width = hdisplay;
456 layout->fb_height = 2 * vdisplay + vactive_space;
458 box_init(&layout->left,
459 0, 0, hdisplay, vdisplay);
460 box_init(&layout->right,
461 0, vdisplay + vactive_space, hdisplay, vdisplay);
469 static const char *stereo_mode_str(drmModeModeInfo *mode)
471 unsigned int layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
474 case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
476 case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
478 case DRM_MODE_FLAG_3D_FRAME_PACKING:
485 static uint32_t create_stereo_fb(drmModeModeInfo *mode, struct igt_fb *fb)
487 struct stereo_fb_layout layout;
491 stereo_fb_layout_from_mode(&layout, mode);
492 fb_id = igt_create_fb(drm_fd, layout.fb_width, layout.fb_height,
493 igt_bpp_depth_to_drm_format(bpp, depth),
495 cr = igt_get_cairo_ctx(drm_fd, fb);
497 igt_paint_image(cr, IGT_DATADIR"/1080p-left.png",
498 layout.left.x, layout.left.y,
499 layout.left.width, layout.left.height);
500 igt_paint_image(cr, IGT_DATADIR"/1080p-right.png",
501 layout.right.x, layout.right.y,
502 layout.right.width, layout.right.height);
509 snprintf(buffer, sizeof(buffer), "%dx%d@%dHz-%s.png",
513 stereo_mode_str(mode));
515 igt_write_fb_to_png(drm_fd, fb, buffer);
521 static void do_set_stereo_mode(struct connector *c)
524 struct igt_fb fb_info;
526 fb_id = create_stereo_fb(&c->mode, &fb_info);
528 igt_warn_on_f(drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0, &c->id, 1, &c->mode),
529 "failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
533 set_stereo_mode(struct connector *c)
538 if (specified_mode_num != -1)
541 n = c->connector->count_modes;
543 for (i = 0; i < n; i++) {
544 if (specified_mode_num == -1)
545 c->mode = c->connector->modes[i];
550 if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
553 igt_info("CRTC(%u): [%d]", c->crtc, i);
554 kmstest_dump_mode(&c->mode);
555 do_set_stereo_mode(c);
560 } else if (sleep_between_modes)
561 sleep(sleep_between_modes);
564 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_OFF);
565 sleep(sleep_between_modes);
566 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
570 drmModeFreeEncoder(c->encoder);
571 drmModeFreeConnector(c->connector);
575 * Re-probe outputs and light up as many as possible.
577 * On Intel, we have two CRTCs that we can drive independently with
578 * different timings and scanout buffers.
580 * Each connector has a corresponding encoder, except in the SDVO case
581 * where an encoder may have multiple connectors.
583 int update_display(void)
585 struct connector *connectors;
588 resources = drmModeGetResources(drm_fd);
590 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
594 connectors = calloc(resources->count_connectors,
595 sizeof(struct connector));
599 if (test_preferred_mode || test_all_modes ||
600 force_mode || specified_disp_id != -1) {
601 unsigned long crtc_idx_mask = -1UL;
603 /* Find any connected displays */
604 for (c = 0; c < resources->count_connectors; c++) {
605 struct connector *connector = &connectors[c];
607 connector->id = resources->connectors[c];
608 if (specified_disp_id != -1 &&
609 connector->id != specified_disp_id)
612 connector_find_preferred_mode(connector->id,
616 if (!connector->mode_valid)
621 if (test_preferred_mode || force_mode ||
622 specified_mode_num != -1)
623 crtc_idx_mask &= ~(1 << connector->crtc_idx);
628 if (test_stereo_modes) {
629 for (c = 0; c < resources->count_connectors; c++) {
630 struct connector *connector = &connectors[c];
632 connector->id = resources->connectors[c];
633 if (specified_disp_id != -1 &&
634 connector->id != specified_disp_id)
637 connector_find_preferred_mode(connector->id,
641 if (!connector->mode_valid)
644 set_stereo_mode(connector);
649 drmModeFreeResources(resources);
653 static char optstr[] = "3hiaf:s:d:p:mrto:j:";
655 static void __attribute__((noreturn)) usage(char *name)
657 igt_info("usage: %s [-hiasdpmtf]\n", name);
658 igt_info("\t-i\tdump info\n");
659 igt_info("\t-a\ttest all modes\n");
660 igt_info("\t-s\t<duration>\tsleep between each mode test\n");
661 igt_info("\t-d\t<depth>\tbit depth of scanout buffer\n");
662 igt_info("\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
663 igt_info("\t-m\ttest the preferred mode\n");
664 igt_info("\t-3\ttest all 3D modes\n");
665 igt_info("\t-t\tuse a tiled framebuffer\n");
666 igt_info("\t-j\tdo dpms off, optional arg to select dpms leve (1-3)\n");
667 igt_info("\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
668 igt_info("\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
669 igt_info("\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
670 igt_info("\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
671 igt_info("\t\ttest force mode\n");
672 igt_info("\tDefault is to test all modes.\n");
676 #define dump_resource(res) if (res) dump_##res()
678 static void cleanup_and_exit(int ret)
684 static gboolean input_event(GIOChannel *source, GIOCondition condition,
690 count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
691 if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
698 static void enter_exec_path( char **argv )
700 char *exec_path = NULL;
705 len_path = strlen( argv[0] );
706 exec_path = (char*) malloc(len_path);
708 memcpy(exec_path, argv[0], len_path);
709 pos = strrchr(exec_path, '/');
713 ret = chdir(exec_path);
714 igt_assert(ret == 0);
718 static void restore_termio_mode(int sig)
720 tcsetattr(tio_fd, TCSANOW, &saved_tio);
724 static void set_termio_mode(void)
728 tio_fd = dup(STDIN_FILENO);
729 tcgetattr(tio_fd, &saved_tio);
730 igt_install_exit_handler(restore_termio_mode);
732 tio.c_lflag &= ~(ICANON | ECHO);
733 tcsetattr(tio_fd, TCSANOW, &tio);
736 int main(int argc, char **argv)
740 GIOChannel *stdinchannel;
743 bool opt_dump_info = false;
745 igt_skip_on_simulation();
747 enter_exec_path( argv );
749 while ((c = getopt(argc, argv, optstr)) != -1) {
752 test_stereo_modes = 1;
755 opt_dump_info = true;
762 if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
763 &force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
764 &force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
766 force_timing.clock = force_clock*1000;
770 sleep_between_modes = atoi(optarg);
773 do_dpms = atoi(optarg);
775 do_dpms = DRM_MODE_DPMS_OFF;
778 depth = atoi(optarg);
779 igt_info("using depth %d\n", depth);
782 if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
783 &plane_height, &crtc_x, &crtc_y,
784 &crtc_w, &crtc_h) != 6)
789 test_preferred_mode = 1;
798 sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
812 else if (depth <= 16)
814 else if (depth <= 32)
817 if (!test_all_modes && !force_mode && !test_preferred_mode &&
818 specified_mode_num == -1 && !test_stereo_modes)
821 drm_fd = drm_open_any();
823 if (test_stereo_modes &&
824 drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
825 igt_warn("DRM_CLIENT_CAP_STEREO_3D failed\n");
834 igt_set_vt_graphics_mode();
836 mainloop = g_main_loop_new(NULL, FALSE);
838 igt_warn("failed to create glib mainloop\n");
843 if (!testdisplay_setup_hotplug()) {
844 igt_warn("failed to initialize hotplug support\n");
848 stdinchannel = g_io_channel_unix_new(0);
850 igt_warn("failed to create stdin GIO channel\n");
854 ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
857 igt_warn("failed to add watch on stdin GIO channel\n");
863 if (!update_display()) {
871 g_main_loop_run(mainloop);
874 g_io_channel_shutdown(stdinchannel, TRUE, NULL);
876 testdisplay_cleanup_hotplug();
878 g_main_loop_unref(mainloop);