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"
74 #define SUBTEST_OPTS 1
77 struct termios saved_tio;
79 drmModeRes *resources;
81 int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
82 test_stereo_modes, enable_tiling;
83 int sleep_between_modes = 5;
84 int do_dpms = 0; /* This aliases to DPMS_ON */
85 uint32_t depth = 24, stride, bpp;
87 int specified_mode_num = -1, specified_disp_id = -1;
89 drmModeModeInfo force_timing;
91 int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
92 unsigned int plane_fb_id;
93 unsigned int plane_crtc_id;
94 unsigned int plane_id;
95 int plane_width, plane_height;
96 static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
99 * Mode setting with the kernel interfaces is a bit of a chore.
100 * First you have to find the connector in question and make sure the
101 * requested mode is available.
102 * Then you need to find the encoder attached to that connector so you
103 * can bind it with a free crtc.
108 drmModeModeInfo mode;
109 drmModeEncoder *encoder;
110 drmModeConnector *connector;
116 static void dump_connectors_fd(int drmfd)
120 drmModeRes *mode_resources = drmModeGetResources(drmfd);
122 if (!mode_resources) {
123 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
127 igt_info("Connectors:\n");
128 igt_info("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
129 for (i = 0; i < mode_resources->count_connectors; i++) {
130 drmModeConnector *connector;
132 connector = drmModeGetConnector(drmfd, mode_resources->connectors[i]);
134 igt_warn("could not get connector %i: %s\n", mode_resources->connectors[i], strerror(errno));
138 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);
140 if (!connector->count_modes)
143 igt_info(" modes:\n");
144 igt_info(" name refresh (Hz) hdisp hss hse htot vdisp ""vss vse vtot flags type clock\n");
145 for (j = 0; j < connector->count_modes; j++){
147 kmstest_dump_mode(&connector->modes[j]);
150 drmModeFreeConnector(connector);
154 drmModeFreeResources(mode_resources);
157 static void dump_crtcs_fd(int drmfd)
160 drmModeRes *mode_resources = drmModeGetResources(drmfd);
162 igt_info("CRTCs:\n");
163 igt_info("id\tfb\tpos\tsize\n");
164 for (i = 0; i < mode_resources->count_crtcs; i++) {
167 crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
169 igt_warn("could not get crtc %i: %s\n", mode_resources->crtcs[i], strerror(errno));
172 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);
173 kmstest_dump_mode(&crtc->mode);
175 drmModeFreeCrtc(crtc);
179 drmModeFreeResources(mode_resources);
182 static void dump_info(void)
184 dump_connectors_fd(drm_fd);
185 dump_crtcs_fd(drm_fd);
188 static void connector_find_preferred_mode(uint32_t connector_id,
189 unsigned long crtc_idx_mask,
190 int mode_num, struct connector *c)
192 struct kmstest_connector_config config;
194 if (kmstest_get_connector_config(drm_fd, connector_id, crtc_idx_mask,
200 c->connector = config.connector;
201 c->encoder = config.encoder;
202 c->crtc = config.crtc->crtc_id;
203 c->crtc_idx = config.crtc_idx;
204 c->pipe = config.pipe;
206 if (mode_num != -1) {
207 igt_assert(mode_num < config.connector->count_modes);
208 c->mode = config.connector->modes[mode_num];
210 c->mode = config.default_mode;
216 paint_color_key(struct igt_fb *fb_info)
221 fb_ptr = gem_mmap(drm_fd, fb_info->gem_handle,
222 fb_info->size, PROT_READ | PROT_WRITE);
225 for (i = crtc_y; i < crtc_y + crtc_h; i++)
226 for (j = crtc_x; j < crtc_x + crtc_w; j++) {
229 offset = (i * fb_info->stride / 4) + j;
230 fb_ptr[offset] = SPRITE_COLOR_KEY;
233 munmap(fb_ptr, fb_info->size);
236 static void paint_image(cairo_t *cr, const char *file)
238 int img_x, img_y, img_w, img_h, img_w_o, img_h_o;
239 double img_w_scale, img_h_scale;
241 cairo_surface_t *image;
243 img_y = height * (0.10 );
244 img_h = height * 0.08 * 4;
247 img_x = (width / 2) - (img_w / 2);
249 image = cairo_image_surface_create_from_png(file);
251 img_w_o = cairo_image_surface_get_width(image);
252 img_h_o = cairo_image_surface_get_height(image);
254 cairo_translate(cr, img_x, img_y);
256 img_w_scale = (double)img_w / (double)img_w_o;
257 img_h_scale = (double)img_h / (double)img_h_o;
258 cairo_scale(cr, img_w_scale, img_h_scale);
260 cairo_set_source_surface(cr, image, 0, 0);
261 cairo_scale(cr, 1, 1);
264 cairo_surface_destroy(image);
267 static void paint_output_info(struct connector *c, struct igt_fb *fb)
269 cairo_t *cr = igt_get_cairo_ctx(drm_fd, fb);
270 int l_width = fb->width;
271 int l_height = fb->height;
277 igt_paint_test_pattern(cr, l_width, l_height);
279 cairo_move_to(cr, l_width / 2, l_height / 2);
281 /* Print connector and mode name */
282 cairo_set_font_size(cr, 48);
283 igt_cairo_printf_line(cr, align_hcenter, 10, "%s",
284 kmstest_connector_type_str(c->connector->connector_type));
286 cairo_set_font_size(cr, 36);
287 str_width = igt_cairo_printf_line(cr, align_hcenter, 10,
288 "%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
289 kmstest_encoder_type_str(c->encoder->encoder_type));
291 cairo_rel_move_to(cr, -str_width / 2, 0);
293 /* List available modes */
294 cairo_set_font_size(cr, 18);
295 str_width = igt_cairo_printf_line(cr, align_left, 10,
297 cairo_rel_move_to(cr, str_width, 0);
298 cairo_get_current_point(cr, &x, &top_y);
301 for (i = 0; i < c->connector->count_modes; i++) {
302 cairo_get_current_point(cr, &x, &y);
306 cairo_move_to(cr, x, top_y);
308 str_width = igt_cairo_printf_line(cr, align_right, 10,
309 "%s @ %dHz", c->connector->modes[i].name,
310 c->connector->modes[i].vrefresh);
311 if (str_width > max_width)
312 max_width = str_width;
316 paint_image(cr, IGT_DATADIR"/pass.png");
318 igt_assert(!cairo_status(cr));
323 static void sighandler(int signo)
328 static void set_single(void)
330 int sigs[] = { SIGUSR1 };
332 sa.sa_handler = sighandler;
334 sigemptyset(&sa.sa_mask);
336 igt_warn_on_f(sigaction(sigs[0], &sa, NULL) == -1,
337 "Could not set signal handler");
341 set_mode(struct connector *c)
343 unsigned int fb_id = 0;
344 struct igt_fb fb_info[2] = { };
345 int j, test_mode_num, current_fb = 0, old_fb = -1;
349 memcpy( &c->mode, &force_timing, sizeof(force_timing));
350 c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
352 sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
353 } else if (test_all_modes)
354 test_mode_num = c->connector->count_modes;
356 for (j = 0; j < test_mode_num; j++) {
359 c->mode = c->connector->modes[j];
361 /* set_mode() only tests 2D modes */
362 if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
368 width = c->mode.hdisplay;
369 height = c->mode.vdisplay;
371 fb_id = igt_create_fb(drm_fd, width, height,
372 igt_bpp_depth_to_drm_format(bpp, depth),
373 enable_tiling, &fb_info[current_fb]);
374 paint_output_info(c, &fb_info[current_fb]);
375 paint_color_key(&fb_info[current_fb]);
377 igt_info("CRTC(%u):[%d]", c->crtc, j);
378 kmstest_dump_mode(&c->mode);
379 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
380 &c->id, 1, &c->mode)) {
381 igt_warn("failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
386 igt_remove_fb(drm_fd, &fb_info[old_fb]);
388 current_fb = 1 - current_fb;
390 if (sleep_between_modes && test_all_modes && !qr_code)
391 sleep(sleep_between_modes);
394 kmstest_set_connector_dpms(drm_fd, c->connector, do_dpms);
395 sleep(sleep_between_modes);
396 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
406 igt_remove_fb(drm_fd, &fb_info[old_fb]);
408 drmModeFreeEncoder(c->encoder);
409 drmModeFreeConnector(c->connector);
413 int x, y, width, height;
416 struct stereo_fb_layout {
417 int fb_width, fb_height;
418 struct box left, right;
421 static void box_init(struct box *box, int x, int y, int bwidth, int bheight)
426 box->height = bheight;
429 static void stereo_fb_layout_from_mode(struct stereo_fb_layout *layout,
430 drmModeModeInfo *mode)
432 unsigned int format = mode->flags & DRM_MODE_FLAG_3D_MASK;
433 const int hdisplay = mode->hdisplay, vdisplay = mode->vdisplay;
437 case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
438 layout->fb_width = hdisplay;
439 layout->fb_height = vdisplay;
441 middle = vdisplay / 2;
442 box_init(&layout->left, 0, 0, hdisplay, middle);
443 box_init(&layout->right,
444 0, middle, hdisplay, vdisplay - middle);
446 case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
447 layout->fb_width = hdisplay;
448 layout->fb_height = vdisplay;
450 middle = hdisplay / 2;
451 box_init(&layout->left, 0, 0, middle, vdisplay);
452 box_init(&layout->right,
453 middle, 0, hdisplay - middle, vdisplay);
455 case DRM_MODE_FLAG_3D_FRAME_PACKING:
457 int vactive_space = mode->vtotal - vdisplay;
459 layout->fb_width = hdisplay;
460 layout->fb_height = 2 * vdisplay + vactive_space;
462 box_init(&layout->left,
463 0, 0, hdisplay, vdisplay);
464 box_init(&layout->right,
465 0, vdisplay + vactive_space, hdisplay, vdisplay);
473 static const char *stereo_mode_str(drmModeModeInfo *mode)
475 unsigned int layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
478 case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
480 case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
482 case DRM_MODE_FLAG_3D_FRAME_PACKING:
489 static uint32_t create_stereo_fb(drmModeModeInfo *mode, struct igt_fb *fb)
491 struct stereo_fb_layout layout;
495 stereo_fb_layout_from_mode(&layout, mode);
496 fb_id = igt_create_fb(drm_fd, layout.fb_width, layout.fb_height,
497 igt_bpp_depth_to_drm_format(bpp, depth),
499 cr = igt_get_cairo_ctx(drm_fd, fb);
501 igt_paint_image(cr, IGT_DATADIR"/1080p-left.png",
502 layout.left.x, layout.left.y,
503 layout.left.width, layout.left.height);
504 igt_paint_image(cr, IGT_DATADIR"/1080p-right.png",
505 layout.right.x, layout.right.y,
506 layout.right.width, layout.right.height);
513 snprintf(buffer, sizeof(buffer), "%dx%d@%dHz-%s.png",
517 stereo_mode_str(mode));
519 igt_write_fb_to_png(drm_fd, fb, buffer);
525 static void do_set_stereo_mode(struct connector *c)
528 struct igt_fb fb_info;
530 fb_id = create_stereo_fb(&c->mode, &fb_info);
532 igt_warn_on_f(drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0, &c->id, 1, &c->mode),
533 "failed to set mode (%dx%d@%dHz): %s\n", width, height, c->mode.vrefresh, strerror(errno));
537 set_stereo_mode(struct connector *c)
542 if (specified_mode_num != -1)
545 n = c->connector->count_modes;
547 for (i = 0; i < n; i++) {
548 if (specified_mode_num == -1)
549 c->mode = c->connector->modes[i];
554 if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
557 igt_info("CRTC(%u): [%d]", c->crtc, i);
558 kmstest_dump_mode(&c->mode);
559 do_set_stereo_mode(c);
564 } else if (sleep_between_modes)
565 sleep(sleep_between_modes);
568 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_OFF);
569 sleep(sleep_between_modes);
570 kmstest_set_connector_dpms(drm_fd, c->connector, DRM_MODE_DPMS_ON);
574 drmModeFreeEncoder(c->encoder);
575 drmModeFreeConnector(c->connector);
579 * Re-probe outputs and light up as many as possible.
581 * On Intel, we have two CRTCs that we can drive independently with
582 * different timings and scanout buffers.
584 * Each connector has a corresponding encoder, except in the SDVO case
585 * where an encoder may have multiple connectors.
587 int update_display(void)
589 struct connector *connectors;
592 resources = drmModeGetResources(drm_fd);
594 igt_warn("drmModeGetResources failed: %s\n", strerror(errno));
598 connectors = calloc(resources->count_connectors,
599 sizeof(struct connector));
603 if (test_preferred_mode || test_all_modes ||
604 force_mode || specified_disp_id != -1) {
605 unsigned long crtc_idx_mask = -1UL;
607 /* Find any connected displays */
608 for (c = 0; c < resources->count_connectors; c++) {
609 struct connector *connector = &connectors[c];
611 connector->id = resources->connectors[c];
612 if (specified_disp_id != -1 &&
613 connector->id != specified_disp_id)
616 connector_find_preferred_mode(connector->id,
620 if (!connector->mode_valid)
625 if (test_preferred_mode || force_mode ||
626 specified_mode_num != -1)
627 crtc_idx_mask &= ~(1 << connector->crtc_idx);
632 if (test_stereo_modes) {
633 for (c = 0; c < resources->count_connectors; c++) {
634 struct connector *connector = &connectors[c];
636 connector->id = resources->connectors[c];
637 if (specified_disp_id != -1 &&
638 connector->id != specified_disp_id)
641 connector_find_preferred_mode(connector->id,
645 if (!connector->mode_valid)
648 set_stereo_mode(connector);
653 drmModeFreeResources(resources);
657 static char optstr[] = "3hiaf:s:d:p:mrto:j:";
659 static void __attribute__((noreturn)) usage(char *name)
661 igt_info("usage: %s [-hiasdpmtf]\n", name);
662 igt_info("\t-i\tdump info\n");
663 igt_info("\t-a\ttest all modes\n");
664 igt_info("\t-s\t<duration>\tsleep between each mode test\n");
665 igt_info("\t-d\t<depth>\tbit depth of scanout buffer\n");
666 igt_info("\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
667 igt_info("\t-m\ttest the preferred mode\n");
668 igt_info("\t-3\ttest all 3D modes\n");
669 igt_info("\t-t\tuse a tiled framebuffer\n");
670 igt_info("\t-j\tdo dpms off, optional arg to select dpms leve (1-3)\n");
671 igt_info("\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
672 igt_info("\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
673 igt_info("\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
674 igt_info("\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
675 igt_info("\t\ttest force mode\n");
676 igt_info("\tDefault is to test all modes.\n");
677 exit((optopt) ? -1 : 0);
680 #define dump_resource(res) if (res) dump_##res()
682 static void cleanup_and_exit(int ret)
688 static gboolean input_event(GIOChannel *source, GIOCondition condition,
694 count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
695 if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
702 static void enter_exec_path( char **argv )
704 char *exec_path = NULL;
709 len_path = strlen( argv[0] );
710 exec_path = (char*) malloc(len_path);
712 memcpy(exec_path, argv[0], len_path);
713 pos = strrchr(exec_path, '/');
717 ret = chdir(exec_path);
718 igt_assert(ret == 0);
722 static void restore_termio_mode(int sig)
724 tcsetattr(tio_fd, TCSANOW, &saved_tio);
728 static void set_termio_mode(void)
732 /* don't attempt to set terminal attributes if not in the foreground
734 if (getpgrp() != tcgetpgrp(STDOUT_FILENO))
737 tio_fd = dup(STDIN_FILENO);
738 tcgetattr(tio_fd, &saved_tio);
739 igt_install_exit_handler(restore_termio_mode);
741 tio.c_lflag &= ~(ICANON | ECHO);
742 tcsetattr(tio_fd, TCSANOW, &tio);
745 int main(int argc, char **argv)
749 GIOChannel *stdinchannel;
752 bool opt_dump_info = false;
753 struct option long_opts[] = {
754 {"list-subtests", 0, 0, SUBTEST_OPTS},
755 {"run-subtest", 1, 0, SUBTEST_OPTS},
759 igt_skip_on_simulation();
761 enter_exec_path( argv );
763 while ((c = getopt_long(argc, argv, optstr, long_opts, NULL)) != -1) {
766 test_stereo_modes = 1;
769 opt_dump_info = true;
776 if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
777 &force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
778 &force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
780 force_timing.clock = force_clock*1000;
784 sleep_between_modes = atoi(optarg);
787 do_dpms = atoi(optarg);
789 do_dpms = DRM_MODE_DPMS_OFF;
792 depth = atoi(optarg);
793 igt_info("using depth %d\n", depth);
796 if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
797 &plane_height, &crtc_x, &crtc_y,
798 &crtc_w, &crtc_h) != 6)
803 test_preferred_mode = 1;
812 sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
815 /* invalid subtest options */
816 exit(IGT_EXIT_INVALID);
830 else if (depth <= 16)
832 else if (depth <= 32)
835 if (!test_all_modes && !force_mode && !test_preferred_mode &&
836 specified_mode_num == -1 && !test_stereo_modes)
839 drm_fd = drm_open_any();
841 if (test_stereo_modes &&
842 drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
843 igt_warn("DRM_CLIENT_CAP_STEREO_3D failed\n");
852 igt_set_vt_graphics_mode();
854 mainloop = g_main_loop_new(NULL, FALSE);
856 igt_warn("failed to create glib mainloop\n");
861 if (!testdisplay_setup_hotplug()) {
862 igt_warn("failed to initialize hotplug support\n");
866 stdinchannel = g_io_channel_unix_new(0);
868 igt_warn("failed to create stdin GIO channel\n");
872 ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
875 igt_warn("failed to add watch on stdin GIO channel\n");
881 if (!update_display()) {
889 g_main_loop_run(mainloop);
892 g_io_channel_shutdown(stdinchannel, TRUE, NULL);
894 testdisplay_cleanup_hotplug();
896 g_main_loop_unref(mainloop);