2 * DRM based mode setting test program
3 * Copyright 2008 Tungsten Graphics
4 * Jakob Bornecrantz <jakob@tungstengraphics.com>
5 * Copyright 2008 Intel Corporation
6 * Jesse Barnes <jesse.barnes@intel.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * This fairly simple test program dumps output in a similar format to the
29 * "xrandr" tool everyone knows & loves. It's necessarily slightly different
30 * since the kernel separates outputs into encoder and connector structures,
31 * each with their own unique ID. The program also allows test testing of the
32 * memory management and mode setting APIs by allowing the user to specify a
33 * connector and mode to use for mode setting. If all works as expected, a
34 * blue background should be painted on the monitor attached to the specified
35 * connector after the selected mode is set.
37 * TODO: use cairo to write the mode info on the selected output once
38 * the mode has been programmed, along with possible test patterns.
51 #include "xf86drmMode.h"
52 #include "intel_bufmgr.h"
59 drmModeRes *resources;
62 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
69 #define type_name_fn(res) \
70 char * res##_str(int type) { \
72 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
73 if (res##_names[i].type == type) \
74 return res##_names[i].name; \
79 struct type_name encoder_type_names[] = {
80 { DRM_MODE_ENCODER_NONE, "none" },
81 { DRM_MODE_ENCODER_DAC, "DAC" },
82 { DRM_MODE_ENCODER_TMDS, "TMDS" },
83 { DRM_MODE_ENCODER_LVDS, "LVDS" },
84 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
87 type_name_fn(encoder_type)
89 struct type_name connector_status_names[] = {
90 { DRM_MODE_CONNECTED, "connected" },
91 { DRM_MODE_DISCONNECTED, "disconnected" },
92 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
95 type_name_fn(connector_status)
97 struct type_name connector_type_names[] = {
98 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
99 { DRM_MODE_CONNECTOR_VGA, "VGA" },
100 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
101 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
102 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
103 { DRM_MODE_CONNECTOR_Composite, "composite" },
104 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
105 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
106 { DRM_MODE_CONNECTOR_Component, "component" },
107 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
108 { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
109 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
110 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
113 type_name_fn(connector_type)
115 void dump_encoders(void)
117 drmModeEncoder *encoder;
120 printf("Encoders:\n");
121 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
122 for (i = 0; i < resources->count_encoders; i++) {
123 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
126 fprintf(stderr, "could not get encoder %i: %s\n",
127 resources->encoders[i], strerror(errno));
130 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
133 encoder_type_str(encoder->encoder_type),
134 encoder->possible_crtcs,
135 encoder->possible_clones);
136 drmModeFreeEncoder(encoder);
141 void dump_mode(drmModeModeInfo *mode)
143 printf(" %s %.02f %d %d %d %d %d %d %d %d\n",
145 (float)mode->vrefresh / 1000,
157 dump_props(drmModeConnector *connector)
159 drmModePropertyPtr props;
162 for (i = 0; i < connector->count_props; i++) {
163 props = drmModeGetProperty(fd, connector->props[i]);
164 printf("\t%s, flags %d\n", props->name, props->flags);
165 drmModeFreeProperty(props);
169 void dump_connectors(void)
171 drmModeConnector *connector;
174 printf("Connectors:\n");
175 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\n");
176 for (i = 0; i < resources->count_connectors; i++) {
177 connector = drmModeGetConnector(fd, resources->connectors[i]);
180 fprintf(stderr, "could not get connector %i: %s\n",
181 resources->connectors[i], strerror(errno));
185 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\n",
186 connector->connector_id,
187 connector->encoder_id,
188 connector_status_str(connector->connection),
189 connector_type_str(connector->connector_type),
190 connector->mmWidth, connector->mmHeight,
191 connector->count_modes);
193 if (!connector->count_modes)
197 printf(" name refresh (Hz) hdisp hss hse htot vdisp "
199 for (j = 0; j < connector->count_modes; j++)
200 dump_mode(&connector->modes[j]);
202 drmModeFreeConnector(connector);
205 dump_props(connector);
210 void dump_crtcs(void)
216 printf("id\tfb\tpos\tsize\n");
217 for (i = 0; i < resources->count_crtcs; i++) {
218 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
221 fprintf(stderr, "could not get crtc %i: %s\n",
222 resources->crtcs[i], strerror(errno));
225 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
229 crtc->width, crtc->height);
230 dump_mode(&crtc->mode);
232 drmModeFreeCrtc(crtc);
237 void dump_framebuffers(void)
242 printf("Frame buffers:\n");
243 printf("id\tsize\tpitch\n");
244 for (i = 0; i < resources->count_fbs; i++) {
245 fb = drmModeGetFB(fd, resources->fbs[i]);
248 fprintf(stderr, "could not get fb %i: %s\n",
249 resources->fbs[i], strerror(errno));
252 printf("%d\t(%dx%d)\t%d\n",
254 fb->width, fb->height);
262 * Mode setting with the kernel interfaces is a bit of a chore.
263 * First you have to find the connector in question and make sure the
264 * requested mode is available.
265 * Then you need to find the encoder attached to that connector so you
266 * can bind it with a free crtc.
271 drmModeModeInfo *mode;
272 drmModeEncoder *encoder;
277 connector_find_mode(struct connector *c)
279 drmModeConnector *connector;
280 int i, j, size, ret, width, height;
282 /* First, find the connector & mode */
284 for (i = 0; i < resources->count_connectors; i++) {
285 connector = drmModeGetConnector(fd, resources->connectors[i]);
288 fprintf(stderr, "could not get connector %i: %s\n",
289 resources->connectors[i], strerror(errno));
290 drmModeFreeConnector(connector);
294 if (!connector->count_modes) {
295 drmModeFreeConnector(connector);
299 if (connector->connector_id != c->id) {
300 drmModeFreeConnector(connector);
304 for (j = 0; j < connector->count_modes; j++) {
305 c->mode = &connector->modes[j];
306 if (!strcmp(c->mode->name, c->mode_str))
310 /* Found it, break out */
314 drmModeFreeConnector(connector);
318 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
322 /* Now get the encoder */
323 for (i = 0; i < resources->count_encoders; i++) {
324 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
327 fprintf(stderr, "could not get encoder %i: %s\n",
328 resources->encoders[i], strerror(errno));
329 drmModeFreeEncoder(c->encoder);
333 if (c->encoder->encoder_id == connector->encoder_id)
336 drmModeFreeEncoder(c->encoder);
340 c->crtc = c->encoder->crtc_id;
346 create_test_buffer(drm_intel_bufmgr *bufmgr,
347 int width, int height, int *stride_out, drm_intel_bo **bo_out)
350 unsigned int *fb_ptr;
351 int size, ret, i, stride;
353 cairo_surface_t *surface;
358 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
359 stride = cairo_image_surface_get_stride(surface);
360 size = stride * height;
361 fb_ptr = (unsigned int *) cairo_image_surface_get_data(surface);
363 /* paint the buffer with colored tiles */
364 for (i = 0; i < width * height; i++) {
366 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
369 cr = cairo_create(surface);
370 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
371 for (x = 0; x < width; x += 250)
372 for (y = 0; y < height; y += 250) {
373 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
374 cairo_move_to(cr, x, y - 20);
375 cairo_line_to(cr, x, y + 20);
376 cairo_move_to(cr, x - 20, y);
377 cairo_line_to(cr, x + 20, y);
378 cairo_new_sub_path(cr);
379 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
380 cairo_set_line_width(cr, 4);
381 cairo_set_source_rgb(cr, 0, 0, 0);
382 cairo_stroke_preserve(cr);
383 cairo_set_source_rgb(cr, 1, 1, 1);
384 cairo_set_line_width(cr, 2);
386 snprintf(buf, sizeof buf, "%d, %d", x, y);
387 cairo_move_to(cr, x + 20, y + 20);
388 cairo_text_path(cr, buf);
389 cairo_set_source_rgb(cr, 0, 0, 0);
390 cairo_stroke_preserve(cr);
391 cairo_set_source_rgb(cr, 1, 1, 1);
397 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
399 fprintf(stderr, "failed to alloc buffer: %s\n",
404 drm_intel_bo_subdata(bo, 0, size, fb_ptr);
406 cairo_surface_destroy(surface);
409 *stride_out = stride;
417 create_test_buffer(drm_intel_bufmgr *bufmgr,
418 int width, int height, int *stride_out, drm_intel_bo **bo_out)
421 unsigned int *fb_ptr;
422 int size, ret, i, stride;
425 /* Mode size at 32 bpp */
427 size = stride * height;
429 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
431 fprintf(stderr, "failed to alloc buffer: %s\n",
436 ret = drm_intel_gem_bo_map_gtt(bo);
438 fprintf(stderr, "failed to GTT map buffer: %s\n",
443 fb_ptr = bo->virtual;
445 /* paint the buffer with colored tiles */
446 for (i = 0; i < width * height; i++) {
448 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
450 drm_intel_bo_unmap(bo);
453 *stride_out = stride;
461 set_mode(struct connector *c, int count)
463 drmModeConnector *connector;
464 drmModeEncoder *encoder = NULL;
465 struct drm_mode_modeinfo *mode = NULL;
466 drm_intel_bufmgr *bufmgr;
469 int i, j, ret, width, height, x, stride;
473 for (i = 0; i < count; i++) {
474 connector_find_mode(&c[i]);
475 if (c[i].mode == NULL)
477 width += c[i].mode->hdisplay;
478 if (height < c[i].mode->vdisplay)
479 height = c[i].mode->vdisplay;
482 bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
484 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
488 if (create_test_buffer(bufmgr, width, height, &stride, &bo))
491 ret = drmModeAddFB(fd, width, height, 32, 32, stride, bo->handle,
494 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
499 for (i = 0; i < count; i++) {
501 if (c[i].mode == NULL)
504 printf("setting mode %s on connector %d, crtc %d\n",
505 c[i].mode_str, c[i].id, c[i].crtc);
507 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
508 &c[i].id, 1, c[i].mode);
509 x += c[i].mode->hdisplay;
512 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
519 extern int optind, opterr, optopt;
520 static char optstr[] = "ecpmfs:";
522 void usage(char *name)
524 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
525 fprintf(stderr, "\t-e\tlist encoders\n");
526 fprintf(stderr, "\t-c\tlist connectors\n");
527 fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
528 fprintf(stderr, "\t-m\tlist modes\n");
529 fprintf(stderr, "\t-f\tlist framebuffers\n");
530 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
531 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
532 fprintf(stderr, "\n\tDefault is to dump all info.\n");
536 #define dump_resource(res) if (res) dump_##res()
538 int main(int argc, char **argv)
541 int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
542 char *modules[] = { "i915", "radeon" };
543 char *modeset = NULL, *mode, *connector;
544 int i, connector_id, count = 0;
545 struct connector con_args[2];
548 while ((c = getopt(argc, argv, optstr)) != -1) {
566 modeset = strdup(optarg);
567 con_args[count].crtc = -1;
568 if (sscanf(optarg, "%d:%64s",
570 &con_args[count].mode_str) != 2 &&
571 sscanf(optarg, "%d@%d:%64s",
573 &con_args[count].crtc,
574 &con_args[count].mode_str) != 3)
585 encoders = connectors = crtcs = modes = framebuffers = 1;
587 for (i = 0; i < ARRAY_SIZE(modules); i++) {
588 printf("trying to load module %s...", modules[i]);
589 fd = drmOpen(modules[i], NULL);
593 printf("success.\n");
598 if (i == ARRAY_SIZE(modules)) {
599 fprintf(stderr, "failed to load any modules, aborting.\n");
603 resources = drmModeGetResources(fd);
605 fprintf(stderr, "drmModeGetResources failed: %s\n",
611 dump_resource(encoders);
612 dump_resource(connectors);
613 dump_resource(crtcs);
614 dump_resource(framebuffers);
617 set_mode(con_args, count);
621 drmModeFreeResources(resources);