blob: dd9e56dd54df5cc19bf8b3270ed6b55a7cec06f8 [file] [log] [blame]
Jesse Barnes5406c632010-12-21 09:38:23 -08001/*
2 * Copyright 2010 Intel Corporation
3 * Jesse Barnes <jesse.barnes@intel.com>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24/*
25 * This program is intended for testing of display functionality. It should
26 * allow for testing of
27 * - hotplug
28 * - mode setting
29 * - clone & twin modes
30 * - panel fitting
31 * - test patterns & pixel generators
32 * Additional programs can test the detected outputs against VBT provided
33 * device lists (both docked & undocked).
34 *
35 * TODO:
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)
40 * - allow mode force
41 * - expose output specific controls
42 * - e.g. DDC-CI brightness
43 * - HDMI controls
44 * - panel brightness
45 * - DP commands (e.g. poweroff)
46 * - verify outputs against VBT/physical connectors
47 */
Damien Lespiau7ee278f2013-02-20 14:40:07 +000048#ifdef HAVE_CONFIG_H
Jesse Barnes5406c632010-12-21 09:38:23 -080049#include "config.h"
Damien Lespiau7ee278f2013-02-20 14:40:07 +000050#endif
Jesse Barnes5406c632010-12-21 09:38:23 -080051
Jesse Barnes5406c632010-12-21 09:38:23 -080052#include <cairo.h>
53#include <errno.h>
Jesse Barnes5406c632010-12-21 09:38:23 -080054#include <math.h>
55#include <stdint.h>
Damien Lespiau66477a22013-09-05 16:49:11 +010056#include <stdbool.h>
Damien Lespiau9a8fda72012-09-10 13:33:26 +010057#include <strings.h>
Jesse Barnes5406c632010-12-21 09:38:23 -080058#include <unistd.h>
59#include <sys/poll.h>
60#include <sys/time.h>
Chris Wilson5534cb12011-02-11 10:48:04 +000061#include <sys/ioctl.h>
Damien Lespiau9a8fda72012-09-10 13:33:26 +010062#include <sys/types.h>
63#include <sys/stat.h>
Jesse Barnes5406c632010-12-21 09:38:23 -080064
Jesse Barnes5406c632010-12-21 09:38:23 -080065#include "i915_drm.h"
Daniel Vetter7a6042e2012-01-10 18:29:30 +010066#include "drmtest.h"
Daniel Vetter7f7cafe2012-01-24 10:50:05 +010067#include "testdisplay.h"
Oscar Mateo37f26d12013-11-12 11:50:38 +000068#include "igt_kms.h"
Jesse Barnes5406c632010-12-21 09:38:23 -080069
Yi Sun41fe8112012-07-26 14:23:36 +080070#include <stdlib.h>
71#include <signal.h>
72
Jesse Barnes5406c632010-12-21 09:38:23 -080073drmModeRes *resources;
Daniel Vetter73d1b882012-01-10 18:20:39 +010074int drm_fd, modes;
Damien Lespiau66477a22013-09-05 16:49:11 +010075int test_all_modes = 0, test_preferred_mode = 0, force_mode = 0, test_plane,
Damien Lespiau9a8fda72012-09-10 13:33:26 +010076 test_stereo_modes, enable_tiling;
Hai Lan62d51682011-02-11 11:14:15 -050077int sleep_between_modes = 5;
Jesse Barnes5a9d82c2011-12-07 08:22:41 -080078uint32_t depth = 24, stride, bpp;
Yi Sun41fe8112012-07-26 14:23:36 +080079int qr_code = 0;
Yi Sun6157d242013-02-12 23:11:35 +080080int specified_mode_num = -1, specified_disp_id = -1;
Hai Lan62d51682011-02-11 11:14:15 -050081
Yi Sun4cceae72012-02-03 19:23:55 +080082drmModeModeInfo force_timing;
Jesse Barnes5406c632010-12-21 09:38:23 -080083
Jesse Barnes5a9d82c2011-12-07 08:22:41 -080084int crtc_x, crtc_y, crtc_w, crtc_h, width, height;
Jesse Barnes1f4c37a2011-10-28 05:25:53 +020085unsigned int plane_fb_id;
86unsigned int plane_crtc_id;
87unsigned int plane_id;
88int plane_width, plane_height;
Jesse Barnes5a9d82c2011-12-07 08:22:41 -080089static const uint32_t SPRITE_COLOR_KEY = 0x00aaaaaa;
Jesse Barnes24e60382011-10-24 17:15:34 +020090
Jesse Barnes5406c632010-12-21 09:38:23 -080091/*
92 * Mode setting with the kernel interfaces is a bit of a chore.
93 * First you have to find the connector in question and make sure the
94 * requested mode is available.
95 * Then you need to find the encoder attached to that connector so you
96 * can bind it with a free crtc.
97 */
98struct connector {
99 uint32_t id;
100 int mode_valid;
101 drmModeModeInfo mode;
102 drmModeEncoder *encoder;
103 drmModeConnector *connector;
104 int crtc;
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300105 int crtc_idx;
Jesse Barnes9c29be42011-11-02 12:57:40 -0700106 int pipe;
Jesse Barnes5406c632010-12-21 09:38:23 -0800107};
108
Yi Sun3d46ca82012-02-03 22:45:39 +0800109static void dump_connectors_fd(int drmfd)
Hai Lan62d51682011-02-11 11:14:15 -0500110{
111 int i, j;
112
Yi Sun3d46ca82012-02-03 22:45:39 +0800113 drmModeRes *mode_resources = drmModeGetResources(drmfd);
114
115 if (!mode_resources) {
116 fprintf(stderr, "drmModeGetResources failed: %s\n",
117 strerror(errno));
118 return;
119 }
120
Hai Lan62d51682011-02-11 11:14:15 -0500121 printf("Connectors:\n");
122 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
Yi Sun3d46ca82012-02-03 22:45:39 +0800123 for (i = 0; i < mode_resources->count_connectors; i++) {
Hai Lan62d51682011-02-11 11:14:15 -0500124 drmModeConnector *connector;
125
Yi Sun3d46ca82012-02-03 22:45:39 +0800126 connector = drmModeGetConnector(drmfd, mode_resources->connectors[i]);
Hai Lan62d51682011-02-11 11:14:15 -0500127 if (!connector) {
128 fprintf(stderr, "could not get connector %i: %s\n",
Yi Sun3d46ca82012-02-03 22:45:39 +0800129 mode_resources->connectors[i], strerror(errno));
Hai Lan62d51682011-02-11 11:14:15 -0500130 continue;
131 }
132
133 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n",
134 connector->connector_id,
135 connector->encoder_id,
Imre Deak3a2aed12013-05-30 22:53:53 +0300136 kmstest_connector_status_str(connector->connection),
137 kmstest_connector_type_str(connector->connector_type),
Hai Lan62d51682011-02-11 11:14:15 -0500138 connector->mmWidth, connector->mmHeight,
139 connector->count_modes);
140
141 if (!connector->count_modes)
142 continue;
143
144 printf(" modes:\n");
145 printf(" name refresh (Hz) hdisp hss hse htot vdisp "
Sun Yi91f60972011-05-30 18:20:30 -0700146 "vss vse vtot flags type clock\n");
Yi Sun53895ac2012-09-25 17:13:17 +0800147 for (j = 0; j < connector->count_modes; j++){
148 fprintf(stdout, "[%d]", j );
Daniel Vetter17787f32012-05-22 16:15:15 +0200149 kmstest_dump_mode(&connector->modes[j]);
Yi Sun53895ac2012-09-25 17:13:17 +0800150 }
Hai Lan62d51682011-02-11 11:14:15 -0500151
152 drmModeFreeConnector(connector);
153 }
154 printf("\n");
Yi Sun3d46ca82012-02-03 22:45:39 +0800155
156 drmModeFreeResources(mode_resources);
Hai Lan62d51682011-02-11 11:14:15 -0500157}
158
Yi Sun3d46ca82012-02-03 22:45:39 +0800159static void dump_crtcs_fd(int drmfd)
Hai Lan62d51682011-02-11 11:14:15 -0500160{
161 int i;
Yi Sun3d46ca82012-02-03 22:45:39 +0800162 drmModeRes *mode_resources = drmModeGetResources(drmfd);
Hai Lan62d51682011-02-11 11:14:15 -0500163
164 printf("CRTCs:\n");
165 printf("id\tfb\tpos\tsize\n");
Yi Sun3d46ca82012-02-03 22:45:39 +0800166 for (i = 0; i < mode_resources->count_crtcs; i++) {
Hai Lan62d51682011-02-11 11:14:15 -0500167 drmModeCrtc *crtc;
168
Yi Sun3d46ca82012-02-03 22:45:39 +0800169 crtc = drmModeGetCrtc(drmfd, mode_resources->crtcs[i]);
Hai Lan62d51682011-02-11 11:14:15 -0500170 if (!crtc) {
171 fprintf(stderr, "could not get crtc %i: %s\n",
Yi Sun3d46ca82012-02-03 22:45:39 +0800172 mode_resources->crtcs[i], strerror(errno));
Hai Lan62d51682011-02-11 11:14:15 -0500173 continue;
174 }
175 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
176 crtc->crtc_id,
177 crtc->buffer_id,
178 crtc->x, crtc->y,
179 crtc->width, crtc->height);
Daniel Vetter17787f32012-05-22 16:15:15 +0200180 kmstest_dump_mode(&crtc->mode);
Hai Lan62d51682011-02-11 11:14:15 -0500181
182 drmModeFreeCrtc(crtc);
183 }
184 printf("\n");
Yi Sun3d46ca82012-02-03 22:45:39 +0800185
186 drmModeFreeResources(mode_resources);
Hai Lan62d51682011-02-11 11:14:15 -0500187}
188
Damien Lespiau66477a22013-09-05 16:49:11 +0100189static void dump_info(void)
190{
191 dump_connectors_fd(drm_fd);
192 dump_crtcs_fd(drm_fd);
193}
194
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300195static void connector_find_preferred_mode(uint32_t connector_id,
196 unsigned long crtc_idx_mask,
197 int mode_num, struct connector *c)
Jesse Barnes5406c632010-12-21 09:38:23 -0800198{
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300199 struct kmstest_connector_config config;
Jesse Barnes5406c632010-12-21 09:38:23 -0800200
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300201 if (kmstest_get_connector_config(drm_fd, connector_id, crtc_idx_mask,
202 &config) < 0) {
Jesse Barnes5406c632010-12-21 09:38:23 -0800203 c->mode_valid = 0;
204 return;
205 }
206
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300207 c->connector = config.connector;
208 c->encoder = config.encoder;
209 c->crtc = config.crtc->crtc_id;
210 c->crtc_idx = config.crtc_idx;
211 c->pipe = config.pipe;
212
213 if (mode_num != -1) {
Daniel Vetter83440952013-08-13 12:35:58 +0200214 igt_assert(mode_num < config.connector->count_modes);
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300215 c->mode = config.connector->modes[mode_num];
216 } else {
217 c->mode = config.default_mode;
Jesse Barnes5406c632010-12-21 09:38:23 -0800218 }
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300219 c->mode_valid = 1;
Jesse Barnes5406c632010-12-21 09:38:23 -0800220}
221
Jesse Barnes0d3043f2011-04-18 16:48:27 -0700222static void
Damien Lespiaub1188772012-11-27 19:14:05 +0000223paint_color_key(struct kmstest_fb *fb_info)
Jesse Barnes5a9d82c2011-12-07 08:22:41 -0800224{
225 int i, j;
Damien Lespiau5a1d8432013-09-04 12:12:37 +0100226 uint32_t *fb_ptr;
227
228 fb_ptr = gem_mmap(drm_fd, fb_info->gem_handle,
229 fb_info->size, PROT_READ | PROT_WRITE);
230 igt_assert(fb_ptr);
Jesse Barnes5a9d82c2011-12-07 08:22:41 -0800231
232 for (i = crtc_y; i < crtc_y + crtc_h; i++)
233 for (j = crtc_x; j < crtc_x + crtc_w; j++) {
234 uint32_t offset;
235
Damien Lespiaub1188772012-11-27 19:14:05 +0000236 offset = (i * fb_info->stride / 4) + j;
Jesse Barnes5a9d82c2011-12-07 08:22:41 -0800237 fb_ptr[offset] = SPRITE_COLOR_KEY;
238 }
Damien Lespiau5a1d8432013-09-04 12:12:37 +0100239
240 munmap(fb_ptr, fb_info->size);
Jesse Barnes5a9d82c2011-12-07 08:22:41 -0800241}
242
Yi Sun41fe8112012-07-26 14:23:36 +0800243static void paint_image(cairo_t *cr, const char *file)
244{
245 int img_x, img_y, img_w, img_h, img_w_o, img_h_o;
246 double img_w_scale, img_h_scale;
247
248 cairo_surface_t *image;
249
250 img_y = height * (0.10 );
251 img_h = height * 0.08 * 4;
252 img_w = img_h;
253
254 img_x = (width / 2) - (img_w / 2);
255
256 image = cairo_image_surface_create_from_png(file);
257
258 img_w_o = cairo_image_surface_get_width(image);
259 img_h_o = cairo_image_surface_get_height(image);
260
261 cairo_translate(cr, img_x, img_y);
262
Yi Sun41fe8112012-07-26 14:23:36 +0800263 img_w_scale = (double)img_w / (double)img_w_o;
264 img_h_scale = (double)img_h / (double)img_h_o;
265 cairo_scale(cr, img_w_scale, img_h_scale);
266
267 cairo_set_source_surface(cr, image, 0, 0);
268 cairo_scale(cr, 1, 1);
269
270 cairo_paint(cr);
271 cairo_surface_destroy(image);
272}
273
Imre Deakf68d9642013-05-24 17:26:54 +0300274static void paint_output_info(struct connector *c, struct kmstest_fb *fb)
Jesse Barnes0d3043f2011-04-18 16:48:27 -0700275{
Imre Deakf68d9642013-05-24 17:26:54 +0300276 cairo_t *cr = kmstest_get_cairo_ctx(drm_fd, fb);
277 int l_width = fb->width;
278 int l_height = fb->height;
Imre Deak542a40c2013-05-30 15:03:48 +0300279 double str_width;
280 double x, y, top_y;
281 double max_width;
282 int i;
Jesse Barnes5406c632010-12-21 09:38:23 -0800283
Imre Deakf68d9642013-05-24 17:26:54 +0300284 kmstest_paint_test_pattern(cr, l_width, l_height);
285
Jesse Barnes5406c632010-12-21 09:38:23 -0800286 cairo_select_font_face(cr, "Helvetica",
287 CAIRO_FONT_SLANT_NORMAL,
288 CAIRO_FONT_WEIGHT_NORMAL);
Imre Deak542a40c2013-05-30 15:03:48 +0300289 cairo_move_to(cr, l_width / 2, l_height / 2);
Jesse Barnes5406c632010-12-21 09:38:23 -0800290
Imre Deak542a40c2013-05-30 15:03:48 +0300291 /* Print connector and mode name */
Jesse Barnes5406c632010-12-21 09:38:23 -0800292 cairo_set_font_size(cr, 48);
Imre Deak542a40c2013-05-30 15:03:48 +0300293 kmstest_cairo_printf_line(cr, align_hcenter, 10, "%s",
294 kmstest_connector_type_str(c->connector->connector_type));
Jesse Barnes5406c632010-12-21 09:38:23 -0800295
Jesse Barnes5406c632010-12-21 09:38:23 -0800296 cairo_set_font_size(cr, 36);
Imre Deak542a40c2013-05-30 15:03:48 +0300297 str_width = kmstest_cairo_printf_line(cr, align_hcenter, 10,
298 "%s @ %dHz on %s encoder", c->mode.name, c->mode.vrefresh,
299 kmstest_encoder_type_str(c->encoder->encoder_type));
300
301 cairo_rel_move_to(cr, -str_width / 2, 0);
Jesse Barnes5406c632010-12-21 09:38:23 -0800302
303 /* List available modes */
Jesse Barnes5406c632010-12-21 09:38:23 -0800304 cairo_set_font_size(cr, 18);
Imre Deak542a40c2013-05-30 15:03:48 +0300305 str_width = kmstest_cairo_printf_line(cr, align_left, 10,
306 "Available modes:");
307 cairo_rel_move_to(cr, str_width, 0);
308 cairo_get_current_point(cr, &x, &top_y);
Jesse Barnes5406c632010-12-21 09:38:23 -0800309
Imre Deak542a40c2013-05-30 15:03:48 +0300310 max_width = 0;
Jesse Barnes5406c632010-12-21 09:38:23 -0800311 for (i = 0; i < c->connector->count_modes; i++) {
Imre Deak542a40c2013-05-30 15:03:48 +0300312 cairo_get_current_point(cr, &x, &y);
313 if (y >= l_height) {
314 x += max_width + 10;
315 max_width = 0;
316 cairo_move_to(cr, x, top_y);
Jesse Barnes5406c632010-12-21 09:38:23 -0800317 }
Imre Deak542a40c2013-05-30 15:03:48 +0300318 str_width = kmstest_cairo_printf_line(cr, align_right, 10,
319 "%s @ %dHz", c->connector->modes[i].name,
320 c->connector->modes[i].vrefresh);
321 if (str_width > max_width)
322 max_width = str_width;
Jesse Barnes5406c632010-12-21 09:38:23 -0800323 }
Yi Sun41fe8112012-07-26 14:23:36 +0800324
325 if (qr_code)
Damien Lespiau951b37e2013-09-06 11:44:41 +0100326 paint_image(cr, IGT_DATADIR"/pass.png");
Imre Deakf68d9642013-05-24 17:26:54 +0300327
Daniel Vetter83440952013-08-13 12:35:58 +0200328 igt_assert(!cairo_status(cr));
Yi Sun41fe8112012-07-26 14:23:36 +0800329}
330
331static void sighandler(int signo)
332{
333 return;
334}
335
336static void set_single(void)
337{
338 int sigs[] = { SIGUSR1 };
339 struct sigaction sa;
340 sa.sa_handler = sighandler;
341
342 sigemptyset(&sa.sa_mask);
343
344 if (sigaction(sigs[0], &sa, NULL) == -1)
345 perror("Could not set signal handler");
Jesse Barnes5406c632010-12-21 09:38:23 -0800346}
347
Jesse Barnes5406c632010-12-21 09:38:23 -0800348static void
349set_mode(struct connector *c)
350{
Daniel Vetter434be472012-07-21 17:36:36 +0200351 unsigned int fb_id = 0;
Damien Lespiaueecd0062013-09-03 19:46:19 +0100352 struct kmstest_fb fb_info[2] = { };
353 int j, test_mode_num, current_fb = 0, old_fb = -1;
Jesse Barnesc56cd102011-04-19 11:50:16 -0700354
Chris Wilson5534cb12011-02-11 10:48:04 +0000355 test_mode_num = 1;
356 if (force_mode){
Yi Sun4cceae72012-02-03 19:23:55 +0800357 memcpy( &c->mode, &force_timing, sizeof(force_timing));
358 c->mode.vrefresh =(force_timing.clock*1e3)/(force_timing.htotal*force_timing.vtotal);
Chris Wilson5534cb12011-02-11 10:48:04 +0000359 c->mode_valid = 1;
Yi Sun4cceae72012-02-03 19:23:55 +0800360 sprintf(c->mode.name, "%dx%d", force_timing.hdisplay, force_timing.vdisplay);
Chris Wilson5534cb12011-02-11 10:48:04 +0000361 } else if (test_all_modes)
Hai Lan62d51682011-02-11 11:14:15 -0500362 test_mode_num = c->connector->count_modes;
Jesse Barnes5406c632010-12-21 09:38:23 -0800363
Hai Lan62d51682011-02-11 11:14:15 -0500364 for (j = 0; j < test_mode_num; j++) {
Chris Wilson5534cb12011-02-11 10:48:04 +0000365
Hai Lan62d51682011-02-11 11:14:15 -0500366 if (test_all_modes)
367 c->mode = c->connector->modes[j];
Chris Wilson5534cb12011-02-11 10:48:04 +0000368
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100369 /* set_mode() only tests 2D modes */
Damien Lespiaua7d19372013-09-26 17:56:01 +0100370 if (c->mode.flags & DRM_MODE_FLAG_3D_MASK)
371 continue;
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100372
Chris Wilson5534cb12011-02-11 10:48:04 +0000373 if (!c->mode_valid)
374 continue;
375
376 width = c->mode.hdisplay;
377 height = c->mode.vdisplay;
378
Daniel Vetter662d7322012-05-22 14:37:19 +0200379 fb_id = kmstest_create_fb(drm_fd, width, height, bpp, depth,
Damien Lespiaueecd0062013-09-03 19:46:19 +0100380 enable_tiling, &fb_info[current_fb]);
381 paint_output_info(c, &fb_info[current_fb]);
382 paint_color_key(&fb_info[current_fb]);
Hai Lan62d51682011-02-11 11:14:15 -0500383
Damien Lespiaub9db1a62013-09-04 12:31:18 +0100384 fprintf(stdout, "CRTC(%u):[%d]",c->crtc, j);
Daniel Vetter17787f32012-05-22 16:15:15 +0200385 kmstest_dump_mode(&c->mode);
Daniel Vetter73d1b882012-01-10 18:20:39 +0100386 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
Chris Wilson5534cb12011-02-11 10:48:04 +0000387 &c->id, 1, &c->mode)) {
388 fprintf(stderr, "failed to set mode (%dx%d@%dHz): %s\n",
389 width, height, c->mode.vrefresh,
390 strerror(errno));
391 continue;
Hai Lan62d51682011-02-11 11:14:15 -0500392 }
393
Damien Lespiaueecd0062013-09-03 19:46:19 +0100394 if (old_fb != -1)
395 kmstest_remove_fb(drm_fd, &fb_info[old_fb]);
396 old_fb = current_fb;
397 current_fb = 1 - current_fb;
398
Yi Sun41fe8112012-07-26 14:23:36 +0800399 if (sleep_between_modes && test_all_modes && !qr_code)
Hai Landac602b2011-02-11 23:25:36 -0500400 sleep(sleep_between_modes);
Yi Sun1f41a302011-12-01 18:57:16 +0800401
Yi Sun41fe8112012-07-26 14:23:36 +0800402 if (qr_code){
403 set_single();
404 pause();
405 }
Yi Sun1f41a302011-12-01 18:57:16 +0800406 }
407
Damien Lespiaueecd0062013-09-03 19:46:19 +0100408 if (test_all_modes)
409 kmstest_remove_fb(drm_fd, &fb_info[old_fb]);
Chris Wilson5534cb12011-02-11 10:48:04 +0000410
Jesse Barnes5406c632010-12-21 09:38:23 -0800411 drmModeFreeEncoder(c->encoder);
412 drmModeFreeConnector(c->connector);
413}
414
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100415struct box {
416 int x, y, width, height;
417};
418
419struct stereo_fb_layout {
420 int fb_width, fb_height;
421 struct box left, right;
422};
423
424static void box_init(struct box *box, int x, int y, int bwidth, int bheight)
425{
426 box->x = x;
427 box->y = y;
428 box->width = bwidth;
429 box->height = bheight;
430}
431
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100432static void stereo_fb_layout_from_mode(struct stereo_fb_layout *layout,
433 drmModeModeInfo *mode)
434{
Damien Lespiaua7d19372013-09-26 17:56:01 +0100435 unsigned int format = mode->flags & DRM_MODE_FLAG_3D_MASK;
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100436 const int hdisplay = mode->hdisplay, vdisplay = mode->vdisplay;
437 int middle;
438
439 switch (format) {
440 case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
441 layout->fb_width = hdisplay;
442 layout->fb_height = vdisplay;
443
444 middle = vdisplay / 2;
445 box_init(&layout->left, 0, 0, hdisplay, middle);
446 box_init(&layout->right,
447 0, middle, hdisplay, vdisplay - middle);
448 break;
449 case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
450 layout->fb_width = hdisplay;
451 layout->fb_height = vdisplay;
452
453 middle = hdisplay / 2;
454 box_init(&layout->left, 0, 0, middle, vdisplay);
455 box_init(&layout->right,
456 middle, 0, hdisplay - middle, vdisplay);
457 break;
458 case DRM_MODE_FLAG_3D_FRAME_PACKING:
459 {
460 int vactive_space = mode->vtotal - vdisplay;
461
462 layout->fb_width = hdisplay;
463 layout->fb_height = 2 * vdisplay + vactive_space;
464
465 box_init(&layout->left,
466 0, 0, hdisplay, vdisplay);
467 box_init(&layout->right,
468 0, vdisplay + vactive_space, hdisplay, vdisplay);
469 break;
470 }
471 default:
472 assert(0);
473 }
474}
475
476static const char *stereo_mode_str(drmModeModeInfo *mode)
477{
Damien Lespiaua7d19372013-09-26 17:56:01 +0100478 unsigned int layout = mode->flags & DRM_MODE_FLAG_3D_MASK;
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100479
480 switch (layout) {
481 case DRM_MODE_FLAG_3D_TOP_AND_BOTTOM:
482 return "TB";
483 case DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF:
484 return "SbSH";
485 case DRM_MODE_FLAG_3D_FRAME_PACKING:
486 return "FP";
487 default:
488 assert(0);
489 }
490}
491
492static uint32_t create_stereo_fb(drmModeModeInfo *mode, struct kmstest_fb *fb)
493{
494 struct stereo_fb_layout layout;
495 cairo_t *cr;
496 uint32_t fb_id;
497
498 stereo_fb_layout_from_mode(&layout, mode);
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100499 fb_id = kmstest_create_fb(drm_fd, layout.fb_width, layout.fb_height,
500 bpp, depth, enable_tiling, fb);
501 cr = kmstest_get_cairo_ctx(drm_fd, fb);
502
503 kmstest_paint_image(cr, IGT_DATADIR"/1080p-left.png",
504 layout.left.x, layout.left.y,
505 layout.left.width, layout.left.height);
506 kmstest_paint_image(cr, IGT_DATADIR"/1080p-right.png",
507 layout.right.x, layout.right.y,
508 layout.right.width, layout.right.height);
509
510 {
511 char buffer[64];
512
513 snprintf(buffer, sizeof(buffer), "%dx%d@%dHz-%s.png",
514 mode->hdisplay,
515 mode->vdisplay,
516 mode->vrefresh,
517 stereo_mode_str(mode));
518
519 kmstest_write_fb(drm_fd, fb, buffer);
520 }
521
522 return fb_id;
523}
524
525static void do_set_stereo_mode(struct connector *c)
526{
527 uint32_t fb_id;
528 struct kmstest_fb fb_info;
529
530 fb_id = create_stereo_fb(&c->mode, &fb_info);
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100531
532 if (drmModeSetCrtc(drm_fd, c->crtc, fb_id, 0, 0,
533 &c->id, 1, &c->mode)) {
534 fprintf(stderr, "failed to set mode (%dx%d@%dHz): %s\n",
535 width, height, c->mode.vrefresh,
536 strerror(errno));
537 }
538}
539
540static void
541set_stereo_mode(struct connector *c)
542{
Damien Lespiaue836b602013-09-30 17:42:33 +0100543 int i, n;
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100544
Damien Lespiaue836b602013-09-30 17:42:33 +0100545
546 if (specified_mode_num != -1)
547 n = 1;
548 else
549 n = c->connector->count_modes;
550
551 for (i = 0; i < n; i++) {
552 if (specified_mode_num == -1)
553 c->mode = c->connector->modes[i];
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100554
555 if (!c->mode_valid)
556 continue;
557
Damien Lespiaua7d19372013-09-26 17:56:01 +0100558 if (!(c->mode.flags & DRM_MODE_FLAG_3D_MASK))
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100559 continue;
560
Damien Lespiau6de613f2013-09-30 18:03:33 +0100561 fprintf(stdout, "CRTC(%u): [%d]", c->crtc, i);
562 kmstest_dump_mode(&c->mode);
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100563 do_set_stereo_mode(c);
564
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100565 if (qr_code) {
566 set_single();
567 pause();
568 } else if (sleep_between_modes)
569 sleep(sleep_between_modes);
570 }
571
572 drmModeFreeEncoder(c->encoder);
573 drmModeFreeConnector(c->connector);
574}
575
Jesse Barnes5406c632010-12-21 09:38:23 -0800576/*
577 * Re-probe outputs and light up as many as possible.
578 *
579 * On Intel, we have two CRTCs that we can drive independently with
580 * different timings and scanout buffers.
581 *
582 * Each connector has a corresponding encoder, except in the SDVO case
583 * where an encoder may have multiple connectors.
584 */
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100585int update_display(void)
Jesse Barnes5406c632010-12-21 09:38:23 -0800586{
587 struct connector *connectors;
588 int c;
589
Daniel Vetter73d1b882012-01-10 18:20:39 +0100590 resources = drmModeGetResources(drm_fd);
Jesse Barnes5406c632010-12-21 09:38:23 -0800591 if (!resources) {
592 fprintf(stderr, "drmModeGetResources failed: %s\n",
593 strerror(errno));
Paulo Zanoni7ead7ba2011-09-15 15:14:18 -0300594 return 0;
Jesse Barnes5406c632010-12-21 09:38:23 -0800595 }
596
597 connectors = calloc(resources->count_connectors,
598 sizeof(struct connector));
599 if (!connectors)
Paulo Zanoni7ead7ba2011-09-15 15:14:18 -0300600 return 0;
Jesse Barnes5406c632010-12-21 09:38:23 -0800601
Damien Lespiaue836b602013-09-30 17:42:33 +0100602 if (test_preferred_mode || test_all_modes ||
603 force_mode || specified_disp_id != -1) {
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300604 unsigned long crtc_idx_mask = -1UL;
605
Hai Lan62d51682011-02-11 11:14:15 -0500606 /* Find any connected displays */
607 for (c = 0; c < resources->count_connectors; c++) {
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300608 struct connector *connector = &connectors[c];
609
610 connector->id = resources->connectors[c];
611 if (specified_disp_id != -1 &&
612 connector->id != specified_disp_id)
Yi Sun05bfbf42012-12-26 17:12:39 +0800613 continue;
614
Imre Deakbfb0cdd2013-05-31 00:21:43 +0300615 connector_find_preferred_mode(connector->id,
616 crtc_idx_mask,
617 specified_mode_num,
618 connector);
619 if (!connector->mode_valid)
620 continue;
621
622 set_mode(connector);
623
624 if (test_preferred_mode || force_mode ||
625 specified_mode_num != -1)
626 crtc_idx_mask &= ~(1 << connector->crtc_idx);
627
Hai Lan62d51682011-02-11 11:14:15 -0500628 }
Jesse Barnes5406c632010-12-21 09:38:23 -0800629 }
Damien Lespiau5d996342013-09-04 14:21:55 +0100630
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100631 if (test_stereo_modes) {
632 for (c = 0; c < resources->count_connectors; c++) {
633 struct connector *connector = &connectors[c];
634
635 connector->id = resources->connectors[c];
Damien Lespiauc128b732013-09-30 17:02:21 +0100636 if (specified_disp_id != -1 &&
637 connector->id != specified_disp_id)
638 continue;
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100639
640 connector_find_preferred_mode(connector->id,
641 -1UL,
642 specified_mode_num,
643 connector);
644 if (!connector->mode_valid)
645 continue;
646
647 set_stereo_mode(connector);
648 }
649 }
650
Damien Lespiau5d996342013-09-04 14:21:55 +0100651 free(connectors);
Jesse Barnes5406c632010-12-21 09:38:23 -0800652 drmModeFreeResources(resources);
Paulo Zanoni7ead7ba2011-09-15 15:14:18 -0300653 return 1;
Jesse Barnes5406c632010-12-21 09:38:23 -0800654}
655
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100656static char optstr[] = "3hiaf:s:d:p:mrto:";
Jesse Barnes5406c632010-12-21 09:38:23 -0800657
Ben Widawsky54ed9382012-08-30 14:03:54 -0700658static void __attribute__((noreturn)) usage(char *name)
Jesse Barnes5406c632010-12-21 09:38:23 -0800659{
Jesse Barnes24e60382011-10-24 17:15:34 +0200660 fprintf(stderr, "usage: %s [-hiasdpmtf]\n", name);
Hai Lan62d51682011-02-11 11:14:15 -0500661 fprintf(stderr, "\t-i\tdump info\n");
662 fprintf(stderr, "\t-a\ttest all modes\n");
663 fprintf(stderr, "\t-s\t<duration>\tsleep between each mode test\n");
Jesse Barnesc56cd102011-04-19 11:50:16 -0700664 fprintf(stderr, "\t-d\t<depth>\tbit depth of scanout buffer\n");
Jesse Barnes5a9d82c2011-12-07 08:22:41 -0800665 fprintf(stderr, "\t-p\t<planew,h>,<crtcx,y>,<crtcw,h> test overlay plane\n");
Paulo Zanoni9fe11482011-09-15 15:13:41 -0300666 fprintf(stderr, "\t-m\ttest the preferred mode\n");
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100667 fprintf(stderr, "\t-3\ttest all 3D modes\n");
Jesse Barnesb707feb2011-06-07 13:27:35 -0700668 fprintf(stderr, "\t-t\tuse a tiled framebuffer\n");
Yi Sun41fe8112012-07-26 14:23:36 +0800669 fprintf(stderr, "\t-r\tprint a QR code on the screen whose content is \"pass\" for the automatic test\n");
Yi Sun05bfbf42012-12-26 17:12:39 +0800670 fprintf(stderr, "\t-o\t<id of the display>,<number of the mode>\tonly test specified mode on the specified display\n");
Hai Lan62d51682011-02-11 11:14:15 -0500671 fprintf(stderr, "\t-f\t<clock MHz>,<hdisp>,<hsync-start>,<hsync-end>,<htotal>,\n");
672 fprintf(stderr, "\t\t<vdisp>,<vsync-start>,<vsync-end>,<vtotal>\n");
673 fprintf(stderr, "\t\ttest force mode\n");
Paulo Zanoni9fe11482011-09-15 15:13:41 -0300674 fprintf(stderr, "\tDefault is to test all modes.\n");
Jesse Barnes5406c632010-12-21 09:38:23 -0800675 exit(0);
676}
677
Hai Lan62d51682011-02-11 11:14:15 -0500678#define dump_resource(res) if (res) dump_##res()
679
Jesse Barnes5406c632010-12-21 09:38:23 -0800680static gboolean input_event(GIOChannel *source, GIOCondition condition,
Yi Sun41fe8112012-07-26 14:23:36 +0800681 gpointer data)
Jesse Barnes5406c632010-12-21 09:38:23 -0800682{
Daniel Vetter74d65452011-12-18 00:24:45 +0100683 gchar buf[2];
Jesse Barnes5406c632010-12-21 09:38:23 -0800684 gsize count;
685
Chris Wilson28032382011-02-11 10:56:41 +0000686 count = read(g_io_channel_unix_get_fd(source), buf, sizeof(buf));
Jesse Barnes24e60382011-10-24 17:15:34 +0200687 if (buf[0] == 'q' && (count == 1 || buf[1] == '\n')) {
Jesse Barnes5406c632010-12-21 09:38:23 -0800688 exit(0);
Jesse Barnesea416332012-02-27 12:46:11 -0800689 }
Jesse Barnes5406c632010-12-21 09:38:23 -0800690
691 return TRUE;
692}
693
Yi Sun41fe8112012-07-26 14:23:36 +0800694static void enter_exec_path( char **argv )
695{
696 char *exec_path = NULL;
697 char *pos = NULL;
698 short len_path = 0;
Imre Deak31dfc982012-10-10 16:04:45 +0300699 int ret;
Yi Sun41fe8112012-07-26 14:23:36 +0800700
701 len_path = strlen( argv[0] );
702 exec_path = (char*) malloc(len_path);
703
704 memcpy(exec_path, argv[0], len_path);
705 pos = strrchr(exec_path, '/');
706 if (pos != NULL)
707 *(pos+1) = '\0';
708
Imre Deak31dfc982012-10-10 16:04:45 +0300709 ret = chdir(exec_path);
Daniel Vetter83440952013-08-13 12:35:58 +0200710 igt_assert(ret == 0);
Yi Sun41fe8112012-07-26 14:23:36 +0800711 free(exec_path);
712}
713
Jesse Barnes5406c632010-12-21 09:38:23 -0800714int main(int argc, char **argv)
715{
716 int c;
Jesse Barnes5406c632010-12-21 09:38:23 -0800717 int ret = 0;
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100718 GIOChannel *stdinchannel;
Jesse Barnes5406c632010-12-21 09:38:23 -0800719 GMainLoop *mainloop;
Yi Sun4cceae72012-02-03 19:23:55 +0800720 float force_clock;
Damien Lespiau66477a22013-09-05 16:49:11 +0100721 bool opt_dump_info = false;
Jesse Barnes5406c632010-12-21 09:38:23 -0800722
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200723 igt_skip_on_simulation();
Damien Lespiau5fa15f72013-04-29 18:40:39 +0100724
Yi Sun41fe8112012-07-26 14:23:36 +0800725 enter_exec_path( argv );
726
Jesse Barnes5406c632010-12-21 09:38:23 -0800727 while ((c = getopt(argc, argv, optstr)) != -1) {
728 switch (c) {
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100729 case '3':
730 test_stereo_modes = 1;
731 break;
Hai Lan62d51682011-02-11 11:14:15 -0500732 case 'i':
Damien Lespiau66477a22013-09-05 16:49:11 +0100733 opt_dump_info = true;
Hai Lan62d51682011-02-11 11:14:15 -0500734 break;
735 case 'a':
736 test_all_modes = 1;
737 break;
738 case 'f':
739 force_mode = 1;
Yi Sun4cceae72012-02-03 19:23:55 +0800740 if(sscanf(optarg,"%f,%hu,%hu,%hu,%hu,%hu,%hu,%hu,%hu",
741 &force_clock,&force_timing.hdisplay, &force_timing.hsync_start,&force_timing.hsync_end,&force_timing.htotal,
742 &force_timing.vdisplay, &force_timing.vsync_start, &force_timing.vsync_end, &force_timing.vtotal)!= 9)
Hai Lan62d51682011-02-11 11:14:15 -0500743 usage(argv[0]);
Yi Sun4cceae72012-02-03 19:23:55 +0800744 force_timing.clock = force_clock*1000;
745
Hai Lan62d51682011-02-11 11:14:15 -0500746 break;
747 case 's':
748 sleep_between_modes = atoi(optarg);
749 break;
Jesse Barnesc56cd102011-04-19 11:50:16 -0700750 case 'd':
751 depth = atoi(optarg);
752 fprintf(stderr, "using depth %d\n", depth);
753 break;
Jesse Barnes7230c012011-06-07 13:17:46 -0700754 case 'p':
Jesse Barnes5a9d82c2011-12-07 08:22:41 -0800755 if (sscanf(optarg, "%d,%d,%d,%d,%d,%d", &plane_width,
756 &plane_height, &crtc_x, &crtc_y,
757 &crtc_w, &crtc_h) != 6)
Jesse Barnes24e60382011-10-24 17:15:34 +0200758 usage(argv[0]);
Jesse Barnes7230c012011-06-07 13:17:46 -0700759 test_plane = 1;
760 break;
Paulo Zanoni9fe11482011-09-15 15:13:41 -0300761 case 'm':
762 test_preferred_mode = 1;
763 break;
Jesse Barnesb707feb2011-06-07 13:27:35 -0700764 case 't':
765 enable_tiling = 1;
766 break;
Yi Sun41fe8112012-07-26 14:23:36 +0800767 case 'r':
768 qr_code = 1;
769 break;
Yi Sun53895ac2012-09-25 17:13:17 +0800770 case 'o':
Yi Sun05bfbf42012-12-26 17:12:39 +0800771 sscanf(optarg, "%d,%d", &specified_disp_id, &specified_mode_num);
Yi Sun53895ac2012-09-25 17:13:17 +0800772 break;
Jesse Barnes5406c632010-12-21 09:38:23 -0800773 default:
Jesse Barnes5406c632010-12-21 09:38:23 -0800774 /* fall through */
775 case 'h':
776 usage(argv[0]);
777 break;
778 }
779 }
Damien Lespiau03962732013-09-03 14:57:31 +0100780
781 if (depth <= 8)
782 bpp = 8;
783 else if (depth <= 16)
784 bpp = 16;
785 else if (depth <= 32)
786 bpp = 32;
787
Damien Lespiau66477a22013-09-05 16:49:11 +0100788 if (!test_all_modes && !force_mode && !test_preferred_mode &&
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100789 specified_mode_num == -1 && !test_stereo_modes)
Paulo Zanoni9fe11482011-09-15 15:13:41 -0300790 test_all_modes = 1;
Jesse Barnes5406c632010-12-21 09:38:23 -0800791
Daniel Vetter0732cc52012-05-22 12:01:36 +0200792 drm_fd = drm_open_any();
Jesse Barnes5406c632010-12-21 09:38:23 -0800793
Damien Lespiau9a8fda72012-09-10 13:33:26 +0100794 if (test_stereo_modes &&
795 drmSetClientCap(drm_fd, DRM_CLIENT_CAP_STEREO_3D, 1) < 0) {
796 fprintf(stderr, "DRM_CLIENT_CAP_STEREO_3D failed\n");
797 goto out_close;
798 }
799
Damien Lespiau66477a22013-09-05 16:49:11 +0100800 if (opt_dump_info) {
801 dump_info();
802 goto out_close;
803 }
804
Daniel Vetter1f0cf2d2013-10-31 17:02:41 +0100805 igt_set_vt_graphics_mode();
Imre Deak01b408e2013-05-08 19:06:44 +0300806
Jesse Barnes5406c632010-12-21 09:38:23 -0800807 mainloop = g_main_loop_new(NULL, FALSE);
808 if (!mainloop) {
809 fprintf(stderr, "failed to create glib mainloop\n");
810 ret = -1;
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100811 goto out_close;
Jesse Barnes5406c632010-12-21 09:38:23 -0800812 }
813
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100814 if (!testdisplay_setup_hotplug()) {
815 fprintf(stderr, "failed to initialize hotplug support\n");
816 goto out_mainloop;
Jesse Barnes5406c632010-12-21 09:38:23 -0800817 }
818
819 stdinchannel = g_io_channel_unix_new(0);
820 if (!stdinchannel) {
821 fprintf(stderr, "failed to create stdin GIO channel\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100822 goto out_hotplug;
Jesse Barnes5406c632010-12-21 09:38:23 -0800823 }
824
825 ret = g_io_add_watch(stdinchannel, G_IO_IN | G_IO_ERR, input_event,
826 NULL);
827 if (ret < 0) {
828 fprintf(stderr, "failed to add watch on stdin GIO channel\n");
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100829 goto out_stdio;
Jesse Barnes5406c632010-12-21 09:38:23 -0800830 }
831
Paulo Zanoni17cecce2011-09-16 18:06:27 -0300832 ret = 0;
833
Paulo Zanoni7ead7ba2011-09-15 15:14:18 -0300834 if (!update_display()) {
835 ret = 1;
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100836 goto out_stdio;
Paulo Zanoni7ead7ba2011-09-15 15:14:18 -0300837 }
838
Damien Lespiau66477a22013-09-05 16:49:11 +0100839 if (test_all_modes)
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100840 goto out_stdio;
Paulo Zanoni7ead7ba2011-09-15 15:14:18 -0300841
Jesse Barnes5406c632010-12-21 09:38:23 -0800842 g_main_loop_run(mainloop);
843
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100844out_stdio:
Jesse Barnes5406c632010-12-21 09:38:23 -0800845 g_io_channel_shutdown(stdinchannel, TRUE, NULL);
Daniel Vetter7f7cafe2012-01-24 10:50:05 +0100846out_hotplug:
847 testdisplay_cleanup_hotplug();
848out_mainloop:
Jesse Barnes5406c632010-12-21 09:38:23 -0800849 g_main_loop_unref(mainloop);
Jesse Barnes5406c632010-12-21 09:38:23 -0800850out_close:
Daniel Vetter0732cc52012-05-22 12:01:36 +0200851 close(drm_fd);
852
Jesse Barnes5406c632010-12-21 09:38:23 -0800853 return ret;
854}