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.
53 #include "xf86drmMode.h"
54 #include "intel_bufmgr.h"
62 drmModeRes *resources;
65 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
72 #define type_name_fn(res) \
73 char * res##_str(int type) { \
75 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
76 if (res##_names[i].type == type) \
77 return res##_names[i].name; \
82 struct type_name encoder_type_names[] = {
83 { DRM_MODE_ENCODER_NONE, "none" },
84 { DRM_MODE_ENCODER_DAC, "DAC" },
85 { DRM_MODE_ENCODER_TMDS, "TMDS" },
86 { DRM_MODE_ENCODER_LVDS, "LVDS" },
87 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
90 type_name_fn(encoder_type)
92 struct type_name connector_status_names[] = {
93 { DRM_MODE_CONNECTED, "connected" },
94 { DRM_MODE_DISCONNECTED, "disconnected" },
95 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
98 type_name_fn(connector_status)
100 struct type_name connector_type_names[] = {
101 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
102 { DRM_MODE_CONNECTOR_VGA, "VGA" },
103 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
104 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
105 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
106 { DRM_MODE_CONNECTOR_Composite, "composite" },
107 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
108 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
109 { DRM_MODE_CONNECTOR_Component, "component" },
110 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
111 { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
112 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
113 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
116 type_name_fn(connector_type)
118 void dump_encoders(void)
120 drmModeEncoder *encoder;
123 printf("Encoders:\n");
124 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
125 for (i = 0; i < resources->count_encoders; i++) {
126 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
129 fprintf(stderr, "could not get encoder %i: %s\n",
130 resources->encoders[i], strerror(errno));
133 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
136 encoder_type_str(encoder->encoder_type),
137 encoder->possible_crtcs,
138 encoder->possible_clones);
139 drmModeFreeEncoder(encoder);
144 void dump_mode(drmModeModeInfo *mode)
146 printf(" %s %.02f %d %d %d %d %d %d %d %d\n",
148 (float)mode->vrefresh / 1000,
160 dump_props(drmModeConnector *connector)
162 drmModePropertyPtr props;
165 for (i = 0; i < connector->count_props; i++) {
166 props = drmModeGetProperty(fd, connector->props[i]);
167 printf("\t%s, flags %d\n", props->name, props->flags);
168 drmModeFreeProperty(props);
172 void dump_connectors(void)
174 drmModeConnector *connector;
177 printf("Connectors:\n");
178 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
179 for (i = 0; i < resources->count_connectors; i++) {
180 connector = drmModeGetConnector(fd, resources->connectors[i]);
183 fprintf(stderr, "could not get connector %i: %s\n",
184 resources->connectors[i], strerror(errno));
188 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
189 connector->connector_id,
190 connector->encoder_id,
191 connector_status_str(connector->connection),
192 connector_type_str(connector->connector_type),
193 connector->mmWidth, connector->mmHeight,
194 connector->count_modes);
196 for (j = 0; j < connector->count_encoders; j++)
197 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
200 if (!connector->count_modes)
204 printf(" name refresh (Hz) hdisp hss hse htot vdisp "
206 for (j = 0; j < connector->count_modes; j++)
207 dump_mode(&connector->modes[j]);
209 drmModeFreeConnector(connector);
212 dump_props(connector);
217 void dump_crtcs(void)
223 printf("id\tfb\tpos\tsize\n");
224 for (i = 0; i < resources->count_crtcs; i++) {
225 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
228 fprintf(stderr, "could not get crtc %i: %s\n",
229 resources->crtcs[i], strerror(errno));
232 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
236 crtc->width, crtc->height);
237 dump_mode(&crtc->mode);
239 drmModeFreeCrtc(crtc);
244 void dump_framebuffers(void)
249 printf("Frame buffers:\n");
250 printf("id\tsize\tpitch\n");
251 for (i = 0; i < resources->count_fbs; i++) {
252 fb = drmModeGetFB(fd, resources->fbs[i]);
255 fprintf(stderr, "could not get fb %i: %s\n",
256 resources->fbs[i], strerror(errno));
259 printf("%u\t(%ux%u)\t%u\n",
261 fb->width, fb->height,
270 * Mode setting with the kernel interfaces is a bit of a chore.
271 * First you have to find the connector in question and make sure the
272 * requested mode is available.
273 * Then you need to find the encoder attached to that connector so you
274 * can bind it with a free crtc.
279 drmModeModeInfo *mode;
280 drmModeEncoder *encoder;
282 unsigned int fb_id[2], current_fb_id;
283 struct timeval start;
289 connector_find_mode(struct connector *c)
291 drmModeConnector *connector;
294 /* First, find the connector & mode */
296 for (i = 0; i < resources->count_connectors; i++) {
297 connector = drmModeGetConnector(fd, resources->connectors[i]);
300 fprintf(stderr, "could not get connector %i: %s\n",
301 resources->connectors[i], strerror(errno));
302 drmModeFreeConnector(connector);
306 if (!connector->count_modes) {
307 drmModeFreeConnector(connector);
311 if (connector->connector_id != c->id) {
312 drmModeFreeConnector(connector);
316 for (j = 0; j < connector->count_modes; j++) {
317 c->mode = &connector->modes[j];
318 if (!strcmp(c->mode->name, c->mode_str))
322 /* Found it, break out */
326 drmModeFreeConnector(connector);
330 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
334 /* Now get the encoder */
335 for (i = 0; i < resources->count_encoders; i++) {
336 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
339 fprintf(stderr, "could not get encoder %i: %s\n",
340 resources->encoders[i], strerror(errno));
341 drmModeFreeEncoder(c->encoder);
345 if (c->encoder->encoder_id == connector->encoder_id)
348 drmModeFreeEncoder(c->encoder);
352 c->crtc = c->encoder->crtc_id;
358 create_test_buffer(drm_intel_bufmgr *bufmgr,
359 int width, int height, int *stride_out, drm_intel_bo **bo_out)
362 unsigned int *fb_ptr;
365 cairo_surface_t *surface;
370 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
371 stride = cairo_image_surface_get_stride(surface);
372 size = stride * height;
373 fb_ptr = (unsigned int *) cairo_image_surface_get_data(surface);
375 /* paint the buffer with colored tiles */
376 for (i = 0; i < width * height; i++) {
378 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
381 cr = cairo_create(surface);
382 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
383 for (x = 0; x < width; x += 250)
384 for (y = 0; y < height; y += 250) {
385 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
386 cairo_move_to(cr, x, y - 20);
387 cairo_line_to(cr, x, y + 20);
388 cairo_move_to(cr, x - 20, y);
389 cairo_line_to(cr, x + 20, y);
390 cairo_new_sub_path(cr);
391 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
392 cairo_set_line_width(cr, 4);
393 cairo_set_source_rgb(cr, 0, 0, 0);
394 cairo_stroke_preserve(cr);
395 cairo_set_source_rgb(cr, 1, 1, 1);
396 cairo_set_line_width(cr, 2);
398 snprintf(buf, sizeof buf, "%d, %d", x, y);
399 cairo_move_to(cr, x + 20, y + 20);
400 cairo_text_path(cr, buf);
401 cairo_set_source_rgb(cr, 0, 0, 0);
402 cairo_stroke_preserve(cr);
403 cairo_set_source_rgb(cr, 1, 1, 1);
409 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
411 fprintf(stderr, "failed to alloc buffer: %s\n",
416 drm_intel_bo_subdata(bo, 0, size, fb_ptr);
418 cairo_surface_destroy(surface);
421 *stride_out = stride;
429 create_test_buffer(drm_intel_bufmgr *bufmgr,
430 int width, int height, int *stride_out, drm_intel_bo **bo_out)
433 unsigned int *fb_ptr;
434 int size, ret, i, stride;
437 /* Mode size at 32 bpp */
439 size = stride * height;
441 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
443 fprintf(stderr, "failed to alloc buffer: %s\n",
448 ret = drm_intel_gem_bo_map_gtt(bo);
450 fprintf(stderr, "failed to GTT map buffer: %s\n",
455 fb_ptr = bo->virtual;
457 /* paint the buffer with colored tiles */
458 for (i = 0; i < width * height; i++) {
460 fb_ptr[i] = 0x00130502 * (d.quot >> 6) + 0x000a1120 * (d.rem >> 6);
462 drm_intel_gem_bo_unmap_gtt(bo);
465 *stride_out = stride;
473 create_grey_buffer(drm_intel_bufmgr *bufmgr,
474 int width, int height, int *stride_out, drm_intel_bo **bo_out)
477 int size, ret, stride;
479 /* Mode size at 32 bpp */
481 size = stride * height;
483 bo = drm_intel_bo_alloc(bufmgr, "frontbuffer", size, 4096);
485 fprintf(stderr, "failed to alloc buffer: %s\n",
490 ret = drm_intel_gem_bo_map_gtt(bo);
492 fprintf(stderr, "failed to GTT map buffer: %s\n",
497 memset(bo->virtual, 0x77, size);
498 drm_intel_gem_bo_unmap_gtt(bo);
501 *stride_out = stride;
507 page_flip_handler(int fd, unsigned int frame,
508 unsigned int sec, unsigned int usec, void *data)
511 unsigned int new_fb_id;
516 if (c->current_fb_id == c->fb_id[0])
517 new_fb_id = c->fb_id[1];
519 new_fb_id = c->fb_id[0];
521 drmModePageFlip(fd, c->crtc, new_fb_id,
522 DRM_MODE_PAGE_FLIP_EVENT, c);
523 c->current_fb_id = new_fb_id;
525 if (c->swap_count == 60) {
526 gettimeofday(&end, NULL);
527 t = end.tv_sec + end.tv_usec * 1e-6 -
528 (c->start.tv_sec + c->start.tv_usec * 1e-6);
529 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
536 set_mode(struct connector *c, int count, int page_flip)
538 drm_intel_bufmgr *bufmgr;
539 drm_intel_bo *bo, *other_bo;
540 unsigned int fb_id, other_fb_id;
541 int i, ret, width, height, x, stride;
542 drmEventContext evctx;
546 for (i = 0; i < count; i++) {
547 connector_find_mode(&c[i]);
548 if (c[i].mode == NULL)
550 width += c[i].mode->hdisplay;
551 if (height < c[i].mode->vdisplay)
552 height = c[i].mode->vdisplay;
555 bufmgr = drm_intel_bufmgr_gem_init(fd, 2<<20);
557 fprintf(stderr, "failed to init bufmgr: %s\n", strerror(errno));
561 if (create_test_buffer(bufmgr, width, height, &stride, &bo))
564 ret = drmModeAddFB(fd, width, height, 32, 32, stride, bo->handle,
567 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
572 for (i = 0; i < count; i++) {
573 if (c[i].mode == NULL)
576 printf("setting mode %s on connector %d, crtc %d\n",
577 c[i].mode_str, c[i].id, c[i].crtc);
579 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
580 &c[i].id, 1, c[i].mode);
581 x += c[i].mode->hdisplay;
584 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
592 if (create_grey_buffer(bufmgr, width, height, &stride, &other_bo))
595 ret = drmModeAddFB(fd, width, height, 32, 32, stride, other_bo->handle,
598 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
602 for (i = 0; i < count; i++) {
603 if (c[i].mode == NULL)
606 drmModePageFlip(fd, c[i].crtc, other_fb_id,
607 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
608 gettimeofday(&c[i].start, NULL);
610 c[i].fb_id[0] = fb_id;
611 c[i].fb_id[1] = other_fb_id;
612 c[i].current_fb_id = fb_id;
615 memset(&evctx, 0, sizeof evctx);
616 evctx.version = DRM_EVENT_CONTEXT_VERSION;
617 evctx.vblank_handler = NULL;
618 evctx.page_flip_handler = page_flip_handler;
621 struct pollfd pfd[2];
624 pfd[0].events = POLLIN;
626 pfd[1].events = POLLIN;
628 if (poll(pfd, 2, -1) < 0) {
629 fprintf(stderr, "poll error\n");
636 drmHandleEvent(fd, &evctx);
641 extern int optind, opterr, optopt;
642 static char optstr[] = "ecpmfs:v";
644 void usage(char *name)
646 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
647 fprintf(stderr, "\t-e\tlist encoders\n");
648 fprintf(stderr, "\t-c\tlist connectors\n");
649 fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
650 fprintf(stderr, "\t-m\tlist modes\n");
651 fprintf(stderr, "\t-f\tlist framebuffers\n");
652 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
653 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
654 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
655 fprintf(stderr, "\n\tDefault is to dump all info.\n");
659 #define dump_resource(res) if (res) dump_##res()
661 static int page_flipping_supported(int fd)
664 struct drm_i915_getparam gp;
666 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
669 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
671 fprintf(stderr, "drm_i915_getparam: %m\n");
678 int main(int argc, char **argv)
681 int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
683 char *modules[] = { "i915", "radeon", "nouveau" };
684 char *modeset = NULL;
686 struct connector con_args[2];
689 while ((c = getopt(argc, argv, optstr)) != -1) {
710 modeset = strdup(optarg);
711 con_args[count].crtc = -1;
712 if (sscanf(optarg, "%d:%64s",
714 con_args[count].mode_str) != 2 &&
715 sscanf(optarg, "%d@%d:%64s",
717 &con_args[count].crtc,
718 con_args[count].mode_str) != 3)
729 encoders = connectors = crtcs = modes = framebuffers = 1;
731 for (i = 0; i < ARRAY_SIZE(modules); i++) {
732 printf("trying to load module %s...", modules[i]);
733 fd = drmOpen(modules[i], NULL);
737 printf("success.\n");
742 if (test_vsync && !page_flipping_supported(fd)) {
743 fprintf(stderr, "page flipping not supported by drm.\n");
747 if (i == ARRAY_SIZE(modules)) {
748 fprintf(stderr, "failed to load any modules, aborting.\n");
752 resources = drmModeGetResources(fd);
754 fprintf(stderr, "drmModeGetResources failed: %s\n",
760 dump_resource(encoders);
761 dump_resource(connectors);
762 dump_resource(crtcs);
763 dump_resource(framebuffers);
766 set_mode(con_args, count, test_vsync);
770 drmModeFreeResources(resources);