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"
61 drmModeRes *resources;
64 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
71 #define type_name_fn(res) \
72 char * res##_str(int type) { \
74 for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
75 if (res##_names[i].type == type) \
76 return res##_names[i].name; \
81 struct type_name encoder_type_names[] = {
82 { DRM_MODE_ENCODER_NONE, "none" },
83 { DRM_MODE_ENCODER_DAC, "DAC" },
84 { DRM_MODE_ENCODER_TMDS, "TMDS" },
85 { DRM_MODE_ENCODER_LVDS, "LVDS" },
86 { DRM_MODE_ENCODER_TVDAC, "TVDAC" },
89 type_name_fn(encoder_type)
91 struct type_name connector_status_names[] = {
92 { DRM_MODE_CONNECTED, "connected" },
93 { DRM_MODE_DISCONNECTED, "disconnected" },
94 { DRM_MODE_UNKNOWNCONNECTION, "unknown" },
97 type_name_fn(connector_status)
99 struct type_name connector_type_names[] = {
100 { DRM_MODE_CONNECTOR_Unknown, "unknown" },
101 { DRM_MODE_CONNECTOR_VGA, "VGA" },
102 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
103 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
104 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
105 { DRM_MODE_CONNECTOR_Composite, "composite" },
106 { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
107 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
108 { DRM_MODE_CONNECTOR_Component, "component" },
109 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
110 { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
111 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
112 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
113 { DRM_MODE_CONNECTOR_TV, "TV" },
114 { DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
117 type_name_fn(connector_type)
119 void dump_encoders(void)
121 drmModeEncoder *encoder;
124 printf("Encoders:\n");
125 printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
126 for (i = 0; i < resources->count_encoders; i++) {
127 encoder = drmModeGetEncoder(fd, resources->encoders[i]);
130 fprintf(stderr, "could not get encoder %i: %s\n",
131 resources->encoders[i], strerror(errno));
134 printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
137 encoder_type_str(encoder->encoder_type),
138 encoder->possible_crtcs,
139 encoder->possible_clones);
140 drmModeFreeEncoder(encoder);
145 void dump_mode(drmModeModeInfo *mode)
147 printf(" %s %d %d %d %d %d %d %d %d %d\n",
161 dump_props(drmModeConnector *connector)
163 drmModePropertyPtr props;
166 for (i = 0; i < connector->count_props; i++) {
167 props = drmModeGetProperty(fd, connector->props[i]);
168 printf("\t%s, flags %d\n", props->name, props->flags);
169 drmModeFreeProperty(props);
173 void dump_connectors(void)
175 drmModeConnector *connector;
178 printf("Connectors:\n");
179 printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
180 for (i = 0; i < resources->count_connectors; i++) {
181 connector = drmModeGetConnector(fd, resources->connectors[i]);
184 fprintf(stderr, "could not get connector %i: %s\n",
185 resources->connectors[i], strerror(errno));
189 printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
190 connector->connector_id,
191 connector->encoder_id,
192 connector_status_str(connector->connection),
193 connector_type_str(connector->connector_type),
194 connector->mmWidth, connector->mmHeight,
195 connector->count_modes);
197 for (j = 0; j < connector->count_encoders; j++)
198 printf("%s%d", j > 0 ? ", " : "", connector->encoders[j]);
201 if (!connector->count_modes)
205 printf(" name refresh (Hz) hdisp hss hse htot vdisp "
207 for (j = 0; j < connector->count_modes; j++)
208 dump_mode(&connector->modes[j]);
211 dump_props(connector);
213 drmModeFreeConnector(connector);
218 void dump_crtcs(void)
224 printf("id\tfb\tpos\tsize\n");
225 for (i = 0; i < resources->count_crtcs; i++) {
226 crtc = drmModeGetCrtc(fd, resources->crtcs[i]);
229 fprintf(stderr, "could not get crtc %i: %s\n",
230 resources->crtcs[i], strerror(errno));
233 printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
237 crtc->width, crtc->height);
238 dump_mode(&crtc->mode);
240 drmModeFreeCrtc(crtc);
245 void dump_framebuffers(void)
250 printf("Frame buffers:\n");
251 printf("id\tsize\tpitch\n");
252 for (i = 0; i < resources->count_fbs; i++) {
253 fb = drmModeGetFB(fd, resources->fbs[i]);
256 fprintf(stderr, "could not get fb %i: %s\n",
257 resources->fbs[i], strerror(errno));
260 printf("%u\t(%ux%u)\t%u\n",
262 fb->width, fb->height,
271 * Mode setting with the kernel interfaces is a bit of a chore.
272 * First you have to find the connector in question and make sure the
273 * requested mode is available.
274 * Then you need to find the encoder attached to that connector so you
275 * can bind it with a free crtc.
280 drmModeModeInfo *mode;
281 drmModeEncoder *encoder;
283 unsigned int fb_id[2], current_fb_id;
284 struct timeval start;
290 connector_find_mode(struct connector *c)
292 drmModeConnector *connector;
295 /* First, find the connector & mode */
297 for (i = 0; i < resources->count_connectors; i++) {
298 connector = drmModeGetConnector(fd, resources->connectors[i]);
301 fprintf(stderr, "could not get connector %i: %s\n",
302 resources->connectors[i], strerror(errno));
303 drmModeFreeConnector(connector);
307 if (!connector->count_modes) {
308 drmModeFreeConnector(connector);
312 if (connector->connector_id != c->id) {
313 drmModeFreeConnector(connector);
317 for (j = 0; j < connector->count_modes; j++) {
318 c->mode = &connector->modes[j];
319 if (!strcmp(c->mode->name, c->mode_str))
323 /* Found it, break out */
327 drmModeFreeConnector(connector);
331 fprintf(stderr, "failed to find mode \"%s\"\n", c->mode_str);
335 /* Now get the encoder */
336 for (i = 0; i < resources->count_encoders; i++) {
337 c->encoder = drmModeGetEncoder(fd, resources->encoders[i]);
340 fprintf(stderr, "could not get encoder %i: %s\n",
341 resources->encoders[i], strerror(errno));
342 drmModeFreeEncoder(c->encoder);
346 if (c->encoder->encoder_id == connector->encoder_id)
349 drmModeFreeEncoder(c->encoder);
353 c->crtc = c->encoder->crtc_id;
356 static struct kms_bo *
357 allocate_buffer(struct kms_driver *kms,
358 int width, int height, int *stride)
361 unsigned bo_attribs[] = {
364 KMS_BO_TYPE, KMS_BO_TYPE_SCANOUT_X8R8G8B8,
365 KMS_TERMINATE_PROP_LIST
369 bo_attribs[1] = width;
370 bo_attribs[3] = height;
372 ret = kms_bo_create(kms, bo_attribs, &bo);
374 fprintf(stderr, "failed to alloc buffer: %s\n",
379 ret = kms_bo_get_prop(bo, KMS_PITCH, stride);
381 fprintf(stderr, "failed to retreive buffer stride: %s\n",
391 make_pwetty(void *data, int width, int height, int stride)
394 cairo_surface_t *surface;
398 surface = cairo_image_surface_create_for_data(data,
402 cr = cairo_create(surface);
403 cairo_surface_destroy(surface);
405 cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
406 for (x = 0; x < width; x += 250)
407 for (y = 0; y < height; y += 250) {
410 cairo_move_to(cr, x, y - 20);
411 cairo_line_to(cr, x, y + 20);
412 cairo_move_to(cr, x - 20, y);
413 cairo_line_to(cr, x + 20, y);
414 cairo_new_sub_path(cr);
415 cairo_arc(cr, x, y, 10, 0, M_PI * 2);
416 cairo_set_line_width(cr, 4);
417 cairo_set_source_rgb(cr, 0, 0, 0);
418 cairo_stroke_preserve(cr);
419 cairo_set_source_rgb(cr, 1, 1, 1);
420 cairo_set_line_width(cr, 2);
423 snprintf(buf, sizeof buf, "%d, %d", x, y);
424 cairo_move_to(cr, x + 20, y + 20);
425 cairo_text_path(cr, buf);
426 cairo_set_source_rgb(cr, 0, 0, 0);
427 cairo_stroke_preserve(cr);
428 cairo_set_source_rgb(cr, 1, 1, 1);
437 create_test_buffer(struct kms_driver *kms,
438 int width, int height, int *stride_out,
439 struct kms_bo **bo_out)
442 int ret, i, j, stride;
445 bo = allocate_buffer(kms, width, height, &stride);
449 ret = kms_bo_map(bo, &virtual);
451 fprintf(stderr, "failed to map buffer: %s\n",
457 /* paint the buffer with colored tiles */
458 for (j = 0; j < height; j++) {
459 uint32_t *fb_ptr = (uint32_t*)((char*)virtual + j * stride);
460 for (i = 0; i < width; i++) {
461 div_t d = div(i, width);
463 0x00130502 * (d.quot >> 6) +
464 0x000a1120 * (d.rem >> 6);
468 make_pwetty(virtual, width, height, stride);
473 *stride_out = stride;
478 create_grey_buffer(struct kms_driver *kms,
479 int width, int height, int *stride_out,
480 struct kms_bo **bo_out)
483 int size, ret, stride;
486 bo = allocate_buffer(kms, width, height, &stride);
490 ret = kms_bo_map(bo, &virtual);
492 fprintf(stderr, "failed to map buffer: %s\n",
498 size = stride * height;
499 memset(virtual, 0x77, size);
503 *stride_out = stride;
509 page_flip_handler(int fd, unsigned int frame,
510 unsigned int sec, unsigned int usec, void *data)
513 unsigned int new_fb_id;
518 if (c->current_fb_id == c->fb_id[0])
519 new_fb_id = c->fb_id[1];
521 new_fb_id = c->fb_id[0];
523 drmModePageFlip(fd, c->crtc, new_fb_id,
524 DRM_MODE_PAGE_FLIP_EVENT, c);
525 c->current_fb_id = new_fb_id;
527 if (c->swap_count == 60) {
528 gettimeofday(&end, NULL);
529 t = end.tv_sec + end.tv_usec * 1e-6 -
530 (c->start.tv_sec + c->start.tv_usec * 1e-6);
531 fprintf(stderr, "freq: %.02fHz\n", c->swap_count / t);
538 set_mode(struct connector *c, int count, int page_flip)
540 struct kms_driver *kms;
541 struct kms_bo *bo, *other_bo;
542 unsigned int fb_id, other_fb_id;
543 int i, ret, width, height, x, stride;
545 drmEventContext evctx;
549 for (i = 0; i < count; i++) {
550 connector_find_mode(&c[i]);
551 if (c[i].mode == NULL)
553 width += c[i].mode->hdisplay;
554 if (height < c[i].mode->vdisplay)
555 height = c[i].mode->vdisplay;
558 ret = kms_create(fd, &kms);
560 fprintf(stderr, "failed to create kms driver: %s\n",
565 if (create_test_buffer(kms, width, height, &stride, &bo))
568 kms_bo_get_prop(bo, KMS_HANDLE, &handle);
569 ret = drmModeAddFB(fd, width, height, 24, 32, stride, handle, &fb_id);
571 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
576 for (i = 0; i < count; i++) {
577 if (c[i].mode == NULL)
580 printf("setting mode %s on connector %d, crtc %d\n",
581 c[i].mode_str, c[i].id, c[i].crtc);
583 ret = drmModeSetCrtc(fd, c[i].crtc, fb_id, x, 0,
584 &c[i].id, 1, c[i].mode);
585 x += c[i].mode->hdisplay;
588 fprintf(stderr, "failed to set mode: %s\n", strerror(errno));
596 if (create_grey_buffer(kms, width, height, &stride, &other_bo))
599 kms_bo_get_prop(other_bo, KMS_HANDLE, &handle);
600 ret = drmModeAddFB(fd, width, height, 32, 32, stride, handle,
603 fprintf(stderr, "failed to add fb: %s\n", strerror(errno));
607 for (i = 0; i < count; i++) {
608 if (c[i].mode == NULL)
611 drmModePageFlip(fd, c[i].crtc, other_fb_id,
612 DRM_MODE_PAGE_FLIP_EVENT, &c[i]);
613 gettimeofday(&c[i].start, NULL);
615 c[i].fb_id[0] = fb_id;
616 c[i].fb_id[1] = other_fb_id;
617 c[i].current_fb_id = other_fb_id;
620 memset(&evctx, 0, sizeof evctx);
621 evctx.version = DRM_EVENT_CONTEXT_VERSION;
622 evctx.vblank_handler = NULL;
623 evctx.page_flip_handler = page_flip_handler;
627 struct pollfd pfd[2];
630 pfd[0].events = POLLIN;
632 pfd[1].events = POLLIN;
634 if (poll(pfd, 2, -1) < 0) {
635 fprintf(stderr, "poll error\n");
642 struct timeval timeout = { .tv_sec = 3, .tv_usec = 0 };
649 ret = select(fd + 1, &fds, NULL, NULL, &timeout);
652 fprintf(stderr, "select timed out or error (ret %d)\n",
655 } else if (FD_ISSET(0, &fds)) {
660 drmHandleEvent(fd, &evctx);
664 kms_bo_destroy(&other_bo);
669 extern int optind, opterr, optopt;
670 static char optstr[] = "ecpmfs:v";
672 void usage(char *name)
674 fprintf(stderr, "usage: %s [-ecpmf]\n", name);
675 fprintf(stderr, "\t-e\tlist encoders\n");
676 fprintf(stderr, "\t-c\tlist connectors\n");
677 fprintf(stderr, "\t-p\tlist CRTCs (pipes)\n");
678 fprintf(stderr, "\t-m\tlist modes\n");
679 fprintf(stderr, "\t-f\tlist framebuffers\n");
680 fprintf(stderr, "\t-v\ttest vsynced page flipping\n");
681 fprintf(stderr, "\t-s <connector_id>:<mode>\tset a mode\n");
682 fprintf(stderr, "\t-s <connector_id>@<crtc_id>:<mode>\tset a mode\n");
683 fprintf(stderr, "\n\tDefault is to dump all info.\n");
687 #define dump_resource(res) if (res) dump_##res()
689 static int page_flipping_supported(int fd)
691 /*FIXME: generic ioctl needed? */
695 struct drm_i915_getparam gp;
697 gp.param = I915_PARAM_HAS_PAGEFLIPPING;
700 ret = drmCommandWriteRead(fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
702 fprintf(stderr, "drm_i915_getparam: %m\n");
710 int main(int argc, char **argv)
713 int encoders = 0, connectors = 0, crtcs = 0, framebuffers = 0;
715 char *modules[] = { "i915", "radeon", "nouveau", "vmwgfx" };
716 char *modeset = NULL;
718 struct connector con_args[2];
721 while ((c = getopt(argc, argv, optstr)) != -1) {
742 modeset = strdup(optarg);
743 con_args[count].crtc = -1;
744 if (sscanf(optarg, "%d:%64s",
746 con_args[count].mode_str) != 2 &&
747 sscanf(optarg, "%d@%d:%64s",
749 &con_args[count].crtc,
750 con_args[count].mode_str) != 3)
761 encoders = connectors = crtcs = modes = framebuffers = 1;
763 for (i = 0; i < ARRAY_SIZE(modules); i++) {
764 printf("trying to load module %s...", modules[i]);
765 fd = drmOpen(modules[i], NULL);
769 printf("success.\n");
774 if (test_vsync && !page_flipping_supported(fd)) {
775 fprintf(stderr, "page flipping not supported by drm.\n");
779 if (i == ARRAY_SIZE(modules)) {
780 fprintf(stderr, "failed to load any modules, aborting.\n");
784 resources = drmModeGetResources(fd);
786 fprintf(stderr, "drmModeGetResources failed: %s\n",
792 dump_resource(encoders);
793 dump_resource(connectors);
794 dump_resource(crtcs);
795 dump_resource(framebuffers);
798 set_mode(con_args, count, test_vsync);
802 drmModeFreeResources(resources);